home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / c-macexp.el < prev    next >
Encoding:
Text File  |  1991-07-03  |  6.1 KB  |  152 lines

  1. ; Path: dg-rtp!rock!mcnc!stanford.edu!leland.Stanford.EDU!leland!jsegal
  2. ; From: jsegal@elaine5.Stanford.EDU (Jonathan Segal)
  3. ; Newsgroups: gnu.emacs.sources
  4. ; Subject: Improve c-macro-expand
  5. ; Date: 22 Jun 91 18:32:03 GMT
  6. ; Organization: The antithesis of chaos.
  7. ; Hello.
  8. ; I have modified the c-macro-expand function (defined in cmacexp.el) so
  9. ; that one can specify an include path for header files, instead of
  10. ; depending upon the header files to reside in the same directory as the
  11. ; .c files you are editing.  One can also configure it to use a
  12. ; preprocessor other than /lib/cpp (for instance, if you want to use the
  13. ; gnu cpp).  There is more information in the description section of the
  14. ; comments below.
  15. ; This is a repost, as it didn't seem to make it past my local site last
  16. ; time...
  17. ; Enjoy!!!
  18. ; -----------cut here (and don't forget the .sig at the bottom) --
  19. ;;; -*-Emacs-Lisp-*-
  20. ;;;%Header
  21. ;;; New and Improved c-macro-expand, c-macexp.el
  22. ;;; Copyright (C) 1991 Free Software Foundation, Inc.
  23. ;;; Modified 1991 Jonathan Segal, jsegal@oracle.com
  24.  
  25. ;;; LCD Archive Entry:
  26. ;;; c-macexp|Jonathan Segal|jsegal@oracle.com
  27. ;;; |This is a modified version of the c-macro-expand
  28. ;;; |91-06-22||~/functions/c-macexp.el.Z
  29.  
  30. ;;; This file is part of GNU Emacs.
  31.  
  32. ;;; GNU Emacs is distributed in the hope that it will be useful,
  33. ;;; but WITHOUT ANY WARRANTY.  No author or distributor
  34. ;;; accepts responsibility to anyone for the consequences of using it
  35. ;;; or for whether it serves any particular purpose or works at all,
  36. ;;; unless he says so in writing.  Refer to the GNU Emacs General Public
  37. ;;; License for full details.
  38.  
  39. ;;; Everyone is granted permission to copy, modify and redistribute
  40. ;;; GNU Emacs, but only under the conditions described in the
  41. ;;; GNU Emacs General Public License.   A copy of this license is
  42. ;;; supposed to have been given to you along with GNU Emacs so you
  43. ;;; can know your rights and responsibilities.  It should be in a
  44. ;;; file named COPYING.  Among other things, the copyright notice
  45. ;;; and this notice must be preserved on all copies.
  46. ;;;
  47.  
  48. ;;; DESCRIPTION:  This is a modified version of the c-macro-expand
  49. ;;; initially distributed with the standard GNU Emacs distribution.  I
  50. ;;; have modified it to allow you to specify include file search
  51. ;;; paths, if you have relevant .h files which are not in the same
  52. ;;; directory as the c files you are editing.  To use, in your .emacs
  53. ;;; file set the variable c-macexp-include-dirs to be a list of
  54. ;;; directories to be searched for your include files.  If you wish,
  55. ;;; you can also set the variable c-macexp-cpp if you do not want to
  56. ;;; use the default cpp in /lib/cpp, and if you use a really funky
  57. ;;; preprocessor which uses a flag other than -I to specify
  58. ;;; directories to be searched for include files, you can set that in
  59. ;;; c-macexp-include-prefix.  All these variables have standard
  60. ;;; default settings.   To invoke, select a region of code for which
  61. ;;; you want to see the preprocessed output, and call the function
  62. ;;; c-macro-expand  (In my c-mode hook I bind C-c C-m to this
  63. ;;; function).  It will spawn off a cpp process, and create a new
  64. ;;; buffer containing the preprocessed version of the code you have
  65. ;;; selected.  This can be useful for debugging various pre-compiler
  66. ;;; tricks, as well as ensuring that you have your macros correct.
  67.  
  68. ;;; This file could replace the current cmacexp.el in the standard
  69. ;;; distribution (upon which this is based).
  70.  
  71. ;;; If you have any problems, I can be reached at jsegal@oracle.com
  72.  
  73. (defvar c-macexp-include-dirs 
  74.   '("/usr/include" "/usr/include/sys" ".")
  75.   "*A list of directories to be searched for include files by c-macro-expand"
  76. )
  77.  
  78. (defvar c-macexp-include-prefix "-I" 
  79. "*The prefix for cpp to know the include files.  Unless you are using a weird
  80. cpp, this should probably be \"-I\""
  81. )
  82.  
  83. (defvar c-macexp-cpp "/lib/cpp" "*Command to use for preprocessor.  Should probably be /lib/cpp or some variant")
  84.  
  85. (defun c-macro-expand (beg end)
  86.   "Display the result of expanding all C macros occurring in the region.
  87. The expansion is entirely correct because it uses the C preprocessor.
  88. It will use the preprocessor specified in the variable c-macexp-cpp,
  89. and will search for include files in the path specified in
  90. c-macexp-include-dirs.  c-macexp-include-prefix will be prepended to
  91. each directory specified, and is probably \"-I\""
  92.   (interactive "r")
  93.   (let ((outbuf (get-buffer-create "*Macroexpansion*"))
  94.     (tempfile "%%macroexpand%%")
  95.         proc-arg-list
  96.     process
  97.     last-needed)
  98.     (save-excursion
  99.       (set-buffer outbuf)
  100.       (erase-buffer))
  101.     (setq proc-arg-list (append (list "macros" outbuf c-macexp-cpp)
  102.     (c-macexp-add-prefixes c-macexp-include-dirs c-macexp-include-prefix)
  103.     ))
  104.     (setq process (apply 'start-process proc-arg-list
  105.     ))
  106.     (set-process-sentinel process '(lambda (&rest x)))
  107.     (save-restriction
  108.       (widen)
  109.       (save-excursion
  110.     (goto-char beg)
  111.     (beginning-of-line)
  112.     (setq last-needed (point))
  113.     (if (re-search-backward "^[ \t]*#" nil t)
  114.         (progn
  115.           ;; Skip continued lines.
  116.           (while (progn (end-of-line) (= (preceding-char) ?\\))
  117.         (forward-line 1))
  118.           ;; Skip the last line of the macro definition we found.
  119.           (forward-line 1)
  120.           (setq last-needed (point)))))
  121.       (write-region (point-min) last-needed tempfile nil 'nomsg)
  122.       (process-send-string process (concat "#include \"" tempfile "\"\n"))
  123.       (process-send-string process "\n")
  124.       (process-send-region process beg end)
  125.       (process-send-string process "\n")
  126.       (process-send-eof process))
  127.     (while (eq (process-status process) 'run)
  128.       (accept-process-output))
  129.     (delete-file tempfile)
  130.     (save-excursion
  131.       (set-buffer outbuf)
  132.       (goto-char (point-max))
  133.       (re-search-backward "\n# [12] \"\"")
  134.       (forward-line 2)
  135.       (while (eolp) (delete-char 1))
  136.       (delete-region (point-min) (point)))
  137.     (display-buffer outbuf)))
  138.  
  139. (defun c-macexp-add-prefixes (list pref) 
  140.   "*Passed a list of strings, will return a list with each of the strings
  141. prefixed by the string pref"
  142.   (if (null list)
  143.     nil
  144.     (cons (concat pref (car list)) (c-macexp-add-prefixes (cdr list) pref))
  145. ))
  146.  
  147. (provide 'c-macexp)
  148.