home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / ivd.el < prev    next >
Encoding:
Text File  |  1990-03-21  |  8.7 KB  |  242 lines

  1. ;From nap1!ames!amdahl!apple!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!uflorida!novavax!hcx1!jason Wed Nov 22 20:28:01 EST 1989
  2. ;Article 914 of comp.emacs:
  3. ;Xref: ark1 comp.emacs:914 gnu.emacs:769
  4. ;Path: ark1!nap1!ames!amdahl!apple!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!uflorida!novavax!hcx1!jason
  5. ;>From: jason@SSD.HARRIS.COM (Jason Baietto)
  6. ;Newsgroups: comp.emacs,gnu.emacs
  7. ;Subject: Insert Variable Dump (the poor person's debugger)
  8. ;Message-ID: <2102@hcx1.UUCP>
  9. ;Date: 21 Nov 89 17:13:06 GMT
  10. ;References: <603@uncle.UUCP>
  11. ;Sender: news@hcx1.UUCP
  12. ;Distribution: na
  13. ;Lines: 225
  14. ;
  15. ;People who write test programs a lot, or people who "hack" away at
  16. ;bugs by inserting printf statements into their code should find this
  17. ;very useful.  While editing a C file, positioning the cursor on a
  18. ;variable and typing C-ci will insert the variable into a list of
  19. ;variables.  After the variables list is complete, typing C-cp will
  20. ;insert a fully formatted printf statement at point.  Typing C-cr will
  21. ;reset the variable list.  Typing C-cv will bring up the variable list
  22. ;window which allows you to enter variables manually and also to enter
  23. ;printf format specifiers to be used in the printf format string.
  24. ;
  25. ;I've used it extensively in the last week, but there may be bugs and
  26. ;I'd appreciate feedback.  Hope somebody finds this useful.  Enjoy!
  27. ;
  28. ;-------------------------------cut-here----------------------------------
  29.  
  30. ;; Insert Variable Dump
  31. ;; (the poor person's C debugger)
  32. ;; Author: Jason Baietto
  33. ;;         jason@ssd.csd.harris.com
  34. ;; Date:   November 20, 1989
  35. ;; This is not part of GNU emacs.
  36.  
  37. (define-key global-map "\C-ci" 'insert-in-variable-list)
  38. (define-key global-map "\C-cv" 'select-variable-list)
  39. (define-key global-map "\C-cr" 'reset-variable-list)
  40.  
  41. (defvar variable-list nil "List of variables used for insert-variable-dump")
  42.  
  43. (defun select-variable-list()
  44. "\nSelects the *variable-list* buffer in which C variables may be entered,
  45. one per line, optionally followed by space and a printf type specifier 
  46. (default \"%d\").  Upon invocation of the function insert-variable-dump,
  47. a printf statement with the variables listed in the *variable-list* window
  48. will be generated and inserted after point in the current buffer.  It
  49. will have the form:  printf(\"var1=%d var2=%s etc.\\n\", var1, var2, etc.);
  50. \nA sample *variable-list* buffer might look like:
  51. \nfoo\nvar 0x%08x\n(char)bar %c
  52. \nSince spaces separate the variable name from its type specifier, no spaces
  53. may exist in either the variable name or type specifier.  The function
  54. insert-in-variable-list can be used to insert the variable around point in
  55. the current buffer into the variable list.  Also, leading space effectively
  56. comments out a variable in the list (which can be useful for big lists)."
  57.    (interactive)
  58.    (if (not (equal (buffer-name) "*variable-list*"))
  59.       (switch-to-buffer-other-window "*variable-list*")
  60.    )
  61. )
  62.  
  63. (defun build-variable-list()
  64. "Build the variable-list from the contents of the *variable-list* buffer."
  65.    (save-excursion
  66.       (switch-to-buffer "*variable-list*")
  67.       (let
  68.          (
  69.             (lines (count-lines (point-min) (point-max)))
  70.             (i 0)
  71.             possible-variable
  72.             possible-format
  73.          )
  74.          (goto-char (point-min))
  75.          (while (<= i lines)
  76.             (beginning-of-line)
  77.             ;; Get the variable name.
  78.             (setq possible-variable (re-around-point "[^ \t\n]" "[ \t\n]"))
  79.             (if possible-variable
  80.                ;; We found a variable.
  81.                (progn
  82.                   ;; Get any format string.
  83.                   (if (re-search-forward "[ \t][ \t]*" (eol-location) t)
  84.                      ;; We found space.
  85.                      (progn
  86.                         (setq possible-format
  87.                            (re-around-point "[^ \t\n]" "[ \t\n]")
  88.                         )
  89.                         (if (not possible-format)
  90.                            (setq possible-format "%d")
  91.                         )
  92.                      )
  93.                      ;; No space, so no format string
  94.                      (setq possible-format "%d")
  95.                   )
  96.                   ;; Add variable/format list to the master list.
  97.                   (setq variable-list 
  98.                      (append 
  99.                         variable-list 
  100.                         (list (list possible-variable possible-format))
  101.                      )
  102.                   )
  103.                )
  104.             )
  105.             ;; Check the next line for variables.
  106.             (next-line 1)
  107.             (beginning-of-line)
  108.             (setq i (1+ i))
  109.          )
  110.       )
  111.       ;; Reset the state of the buffer to not modified
  112.       (set-buffer-modified-p nil)
  113.    )
  114. )
  115.  
  116. (defun insert-variable-dump()
  117. "Insert a printf statement composed of variables in the *variable-list*
  118. buffer.  See select-variable-list for more details."
  119.    (interactive)
  120.    (let*
  121.       (
  122.          (variable-list-buffer (get-buffer "*variable-list*"))
  123.          (variable-list-modified (buffer-modified-p variable-list-buffer))
  124.       )
  125.       (if variable-list-buffer
  126.          ;; The buffer exists
  127.          (if (not variable-list-modified)
  128.             ;; The buffer exists and is not modified so there is no
  129.             ;; reason to rebuild the list.  Just dump the old list.
  130.             (insert-printf-statement)
  131.             ;; The buffer exists but is modified so build and dump.
  132.             (setq variable-list nil)
  133.             (build-variable-list)
  134.             (insert-printf-statement)
  135.          )
  136.          ;; The buffer doesn't exist
  137.          (message "(no variable list)")
  138.       )
  139.    )
  140. )
  141.  
  142. (defun insert-printf-statement()
  143. "Does the actual insertion of the printf statement into current buffer."
  144.    (let
  145.       (
  146.          (current-list (car variable-list))
  147.          (rest-of-variable-list (cdr variable-list))
  148.       )
  149.       (if (not variable-list)
  150.          ;; variable list is empty
  151.          (message "(empty variable list)")
  152.          ;; else, print away.
  153.          (insert "printf(\"")
  154.          (while current-list
  155.             ;; Output each variable/format pair for the printf string.
  156.             (insert (car current-list) "=" (car (cdr current-list)) " ")
  157.             (setq current-list (car rest-of-variable-list))
  158.             (setq rest-of-variable-list (cdr rest-of-variable-list))
  159.          )
  160.          (delete-backward-char 1)
  161.          (insert "\\n\", ")
  162.          (setq current-list (car variable-list))
  163.          (setq rest-of-variable-list (cdr variable-list))
  164.          (while current-list
  165.             ;; Output each variable for the argument list.
  166.             (insert (car current-list) ", ")
  167.             (setq current-list (car rest-of-variable-list))
  168.             (setq rest-of-variable-list (cdr rest-of-variable-list))
  169.          )
  170.          (delete-backward-char 2)
  171.          (insert ");")
  172.       )
  173.    )
  174. )
  175.  
  176. (defun insert-in-variable-list()
  177. "Find the variable name around point and insert into *variable-list*.
  178. A variable name here is defined as that which is composed of only
  179. letters, digits, and underscores.  See select-variable-list."
  180.    (interactive)
  181.    (let
  182.       (
  183.          (possible-variable (re-around-point "[A-Za-z0-9_]" "[^A-Za-z0-9_]"))
  184.       )
  185.       (if possible-variable
  186.          ;; We found a variable, insert it into the *variable-list* buffer.
  187.          (save-excursion
  188.             (switch-to-buffer "*variable-list*")
  189.             (goto-char (point-max))
  190.             (insert possible-variable)
  191.             (newline)
  192.             (message (concat "\"" possible-variable "\""))
  193.          )
  194.          ;; Otherwise, complain
  195.          (message "(variable not found)")
  196.       )
  197.    )
  198. )
  199.    
  200. (defun re-around-point(containing-re delimiting-re)
  201. "Return a symbol composed of CONTAINING-RE delimited by DELIMITING-RE."
  202.    (save-excursion
  203.       (if 
  204.          (and 
  205.             (not (looking-at containing-re))
  206.             (not (looking-back containing-re))
  207.          )
  208.          ;; Point is not on a word, so return nil.
  209.          nil
  210.          ;; Otherwise, get the word
  211.          (if (re-search-backward delimiting-re (bol-location) 1)
  212.             (forward-char 1)
  213.          )
  214.          (setq variable-start (point))
  215.          (if (re-search-forward delimiting-re (eol-location) 1)
  216.             (backward-char 1)
  217.          )
  218.          (setq variable-end (point))
  219.          ;; Return the re-string found
  220.          (buffer-substring variable-start variable-end)
  221.       )
  222.    )
  223. )
  224.  
  225. (defun reset-variable-list()
  226. "Remove all variables currently in the *variable-list* buffer"
  227.    (interactive)
  228.    (save-excursion
  229.       (switch-to-buffer "*variable-list*")
  230.       (delete-region (point-min) (point-max))
  231.       (message "(variable list reset)")
  232.    )
  233. )
  234. ;=================================
  235. ;          Jason Baietto
  236. ;Harris Computer Systems Division
  237. ;     Fort Lauderdale, Florida
  238. ;jason@ssd.csd.harris.com (usenet)
  239. ;=================================
  240.  
  241.  
  242.