home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / emacs / help / 4132 < prev    next >
Encoding:
Text File  |  1992-09-14  |  4.2 KB  |  130 lines

  1. Xref: sparky gnu.emacs.help:4132 gnu.emacs.sources:658
  2. Newsgroups: gnu.emacs.help,gnu.emacs.sources
  3. Path: sparky!uunet!pipex!demon!edscom!kevin
  4. From: kevin@edscom.demon.co.uk (Kevin Broadey)
  5. Subject: Re: C autoindent and macros
  6. In-Reply-To: proctor@cme.nist.gov's message of 9 Sep 92 13:53:36 GMT
  7. Message-ID: <KEVIN.92Sep14164032@runningbear.edscom.demon.co.uk>
  8. X-Disclaimer: These opinions are mine: others available on request.
  9. Sender: kevin@edscom.demon.co.uk (Kevin Broadey)
  10. Reply-To: Kevin Broadey <kbroadey@edscom.demon.co.uk>
  11. Organization: EDS-Scicon, Milton Keynes, UK
  12. References: <PROCTOR.92Sep9095336@grumpy.cme.nist.gov>
  13. Date: Mon, 14 Sep 1992 16:40:32 GMT
  14. Lines: 114
  15.  
  16. >>>>> In article "C autoindent and macros", "Fred" ==
  17. >>>>> proctor@cme.nist.gov (Fred Proctor) writes:
  18.  
  19. Fred> I use a couple of the emacs variables for auto indentation of C
  20. Fred> code in my emacs buffers, but these don't apply for code in a
  21. Fred> macro which spans several lines that end in the backslash
  22. Fred> continuation character.  Does anyone know how to fix this so
  23. Fred> macros whose intermediate lines are terminated with the backslash
  24. Fred> are formatted as if they were straight C code?
  25.  
  26. The answer an hour ago was that c-mode can't be configured that way, so
  27. I wrote you a message detailing the way you could get round it by
  28. narrowing to the macro definition, deleting the backslashes, doing the
  29. edit (on code which now looks like a statement or function definition),
  30. replacing the backslashes and widening back to the full buffer.
  31.  
  32. Then I though "it would be useful to have a command to do the
  33. nitty-gritty - it should be fairly straightforward".
  34.  
  35. Here is the product of my efforts.
  36.  
  37. Thanks for the problem!
  38.  
  39. Enjoy.
  40.  
  41. --Kevin
  42.  
  43. -- snip -- snip -- snip -- snip -- snip -- snip -- snip -- snip -- snip --
  44.  
  45. ;; c-edit-macro.el
  46. ;;
  47. ;; Written by Kevin Broadey <kbroadey@edscom.demon.co.uk>
  48. ;; 14-Sep-92
  49. ;;
  50. ;; Usual GNU conditions - Copyleft and no warranty.
  51.  
  52. (provide 'c-edit-marco)
  53.  
  54. (defvar c-macro-backslash-column 72
  55.   "*Column for c-edit-macro to put \"\\\" at in multi-line macros.
  56.  
  57.     0 puts one space between code and backslash.
  58.     NIL puts backslash immediately after code.")
  59.  
  60. (defun c-edit-macro ()
  61.   (interactive)
  62.   (let (define-start name-start end)
  63.  
  64.     ;; Find the first line of the macro.
  65.     (save-excursion
  66.       (beginning-of-line)
  67.       (while (and (not (looking-at "#define[ \t]+"))
  68.           (not (bobp)))
  69.     (backward-char 1)        ; end of preceding line
  70.     (if (/= (preceding-char) ?\\ )
  71.         (error "Not in a macro definition."))
  72.     (beginning-of-line))
  73.       (if (not (match-beginning 0))
  74.       (error "Not in a macro definition."))
  75.  
  76.       (setq define-start (match-beginning 0))
  77.       (setq name-start (match-end 0)))
  78.  
  79.     ;; Find the last line of the macro.
  80.     (save-excursion
  81.       (end-of-line 1)
  82.       (while (and (= (preceding-char) ?\\ )
  83.           (not (eobp)))
  84.     (end-of-line 2))        ; go to end of next line
  85.       (setq end (point)))
  86.  
  87.     ;; Restrict buffer to macro definition, and set inner restriction to mask
  88.     ;; out "#define" so code looks like a function definition.
  89.     (save-restriction
  90.       (narrow-to-region define-start end)
  91.       (save-restriction
  92.     (narrow-to-region name-start end)
  93.  
  94.     ;;  Get rid of trailing backslashes.
  95.     (save-excursion
  96.       (goto-char (point-min))
  97.       (replace-regexp "[ \t]*\\\\\n" "\n"))
  98.  
  99.     ;; Enter recursive edit.
  100.     (message "Type M-C-c (exit-recursive-edit) to finish.")
  101.     (message (substitute-command-keys
  102.           "Type \\[exit-recursive-edit] to finish."))
  103.     (recursive-edit)
  104.  
  105.     ;; Recursive edit over - put back trailing backslashes.
  106.  
  107.     )                ; end of inner restriction - "#define"
  108.                     ; is now visible so `indent-to' will
  109.                     ; get the target column right.
  110.       (save-excursion
  111.     (goto-char (point-min))
  112.     (end-of-line 1)
  113.     (while (not (eobp))
  114.       (let ((eol (point)))
  115.         (skip-chars-backward " \t")
  116.         (or (eolp)
  117.         (delete-region (point) eol))) ; delete trailing whitespace
  118.       (if c-macro-backslash-column
  119.           (indent-to c-macro-backslash-column (if (bolp) 0 1)))
  120.       (insert "\\")
  121.       (end-of-line 2))        ; end of next line
  122.     ;; Delete trailing whitespace on last line.
  123.     (let ((eol (point)))
  124.       (skip-chars-backward " \t")
  125.       (or (eolp)
  126.           (delete-region (point) eol))) ; delete trailing whitespace
  127.     ))))
  128.  
  129. -- snip -- snip -- snip -- snip -- snip -- snip -- snip -- snip -- snip --
  130.