home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / gnu / emacs / help / 3484 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.5 KB  |  91 lines

  1. Newsgroups: gnu.emacs.help
  2. Path: sparky!uunet!cis.ohio-state.edu!src.bae.co.UK!moore
  3. From: moore@src.bae.co.UK (Chris Moore)
  4. Subject: adding new functions
  5. Message-ID: <9207241243.AA29061@sun19.src.bae.co.uk>
  6. Sender: daemon@cis.ohio-state.edu
  7. Organization: Gatewayed from the GNU Project mailing list help-gnu-emacs@prep.ai.mit.edu
  8. References: <Jul23.203033.61716@yuma.ACNS.ColoState.EDU>
  9. Date: Fri, 24 Jul 1992 12:43:26 GMT
  10. Lines: 79
  11.  
  12.  
  13. ryanr@lamar.colostate.edu (Richard Ryan) said:
  14.  
  15. > (defun gt-indent-region ()
  16. >   (shell-command-on-region 4 "sed -e '/^\(.\)/s//> \1/'"))
  17.  
  18. You need to say the function is interactive before it becomes available to
  19. the user via the M-x way of calling functions.  That's why you were seeing
  20. 'no match'.  Adding the interactive will now let you call the function.
  21.  
  22. ie.
  23.  
  24. (defun gt-indent-region ()
  25.   (interactive)
  26.   (shell-command-on-region 4 "sed -e '/^\(.\)/s//> \1/'"))
  27.  
  28. It won't work, since shell-command-on-region needs at least 3 arguments -
  29. the beginning and end of the region and the command to execute.  I can't
  30. quite see what the 4 is supposed to mean in your function.  Fortunately,
  31. interactive provides a way of automatically passing the beginning and end
  32. of the current region to a function.  (See C-h f interactive RET for
  33. details).
  34.  
  35. (defun gt-indent-region (start end)
  36.   (interactive "r")
  37.   (shell-command-on-region start end "sed -e '/^\(.\)/s//> \1/'"))
  38.  
  39. This now calls sed on the region with the expression you give.  I don't
  40. know enough sed to understand what this means, but it seems to me that it
  41. just copies input to output.  Emacs puts the output from sed in a "*Shell
  42. Command Output*" buffer.  Giving shell-command-on-region an extra true
  43. argument will cause emacs to replace the region in the original buffer with
  44. the output of sed:
  45.  
  46. (defun gt-indent-region (start end)
  47.   (interactive "r")
  48.   (shell-command-on-region start end "sed -e '/^\(.\)/s//> \1/'" t))
  49.  
  50. Another problem:  When you write a string in emacs lisp, backslashes are
  51. used to create certain characters in the string, like the familiar \n, \t
  52. etc of C.  So if you really want a backslash in your string you have to
  53. type two backslashes.  Now your function becomes the following:
  54.  
  55. (defun gt-indent-region (start end)
  56.   (interactive "r")
  57.   (shell-command-on-region start end "sed -e '/^\\(.\\)/s//> \\1/'" t))
  58.  
  59. And now it actually works!
  60.  
  61. > Also, what I'm trying to do with this command is put a "> "
  62. > in the left margin.  If you've got a better function, please
  63. > send me a copy.
  64.  
  65. Probably something using replace-regexp instead of forking a shell would be
  66. quicker.  Unfortunately, it appears that replace-regexp doesn't work on
  67. regions.  It works from point to end-of-buffer.  One solution to this is to
  68. narrow the buffer to the region of interest before calling replace-regexp.
  69.  
  70. [later, after much hacking] try this:
  71.  
  72. (defun gt-indent-region (start end)
  73.   (interactive "r")
  74.   (save-excursion
  75.     ;; Point could currently be at end of region.  We want it at the
  76.     ;; beginning.
  77.     (goto-char start)
  78.     ;; Without this it's possible to insert the '>' halfway along a line.
  79.     (beginning-of-line)
  80.     (save-restriction
  81.       (narrow-to-region (point) end)
  82.       (replace-regexp "^" "> "))))
  83.  
  84. The save-excursion is used to ensure that point is not moved by the function.
  85. The save-restriction is used to ensure that the buffer is restricted in the
  86. same way after the function returns as it was before it was called.
  87. Although longer, this function is much quicker than forking a shell to run
  88. sed, since all the work's done by lisp.
  89.  
  90. Chris.
  91.