home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / rep-region.el < prev    next >
Encoding:
Text File  |  1992-11-25  |  3.7 KB  |  96 lines

  1. ;;!emacs
  2. ;;
  3. ;; LCD Archive Entry:
  4. ;; rep-region|Bob Weiner|rsw@cs.brown.edu|
  5. ;; Replace things in region only.|
  6. ;; 92-04-02||~/functions/rep-region.el.Z|
  7. ;;
  8. ;; SUMMARY:      Replace things in region only.
  9. ;; USAGE:        GNU Emacs Lisp Library
  10. ;;
  11. ;; AUTHOR:       Bob Weiner, Applied Research, Motorola, Inc.
  12. ;; E-MAIL:       weiner@mot.com
  13. ;;
  14. ;; ORIG-DATE:    04/14/88
  15. ;; LAST-MOD:      2-Apr-92 at 17:18:40 by Bob Weiner
  16. ;;
  17. ;; Copyright (C) 1988-92 Bob Weiner and Free Software Foundation, Inc.
  18. ;; Available for use and distribution under the same terms as GNU Emacs.
  19. ;;
  20. ;; This file is not yet part of GNU Emacs.
  21. ;;
  22. ;; DESCRIPTION:  
  23. ;;
  24. ;;   Simplifies life by replacing regular expressions or strings in current
  25. ;;   region only, whether point is at start or end of region.
  26. ;;
  27. ;;   I personally use the following key binding:
  28. ;;
  29. ;;     (global-set-key "\M-r" 'replace-region)
  30. ;;
  31. ;; DESCRIP-END.
  32. ;;
  33.  
  34. ;;; ************************************************************************
  35. ;;; Public functions
  36. ;;; ************************************************************************
  37.  
  38. (defun replace-region (str-replace)
  39.   "Replaces a regular expression or string, given prefix arg STR-REPLACE, in a region."
  40.   (interactive "P")
  41.   (let (current-prefix-arg)
  42.     (call-interactively
  43.       (if str-replace
  44.       'replace-string-region
  45.     'replace-regexp-region))))
  46.  
  47. (defun replace-regexp-region (regexp to-string &optional delimited no-msg)
  48.   "Replace things in region, even before point, matching REGEXP with
  49. TO-STRING.  Preserve case in each match if case-replace and case-fold-search
  50. are non-nil and REGEXP has no uppercase letters.  Optional third arg DELIMITED,
  51. prefix arg if interactive, non-nil means replace only matches surrounded by
  52. word boundaries.  In TO-STRING, \\& means insert what matched REGEXP, and
  53. \\=\\<n> means insert what matched <n>th \\(...\\) in REGEXP.  Optional fourth
  54. arg NO-MSG non-nil means skip printing of 'Done' message."
  55.   (interactive "sReplace regexp in region: \nsReplace regexp %s with: \nP")
  56.   (let ((not-inter (not (interactive-p))))
  57.     (if (null (mark))
  58.     (error "Mark is not set, no region to replace within. ")
  59.       (save-excursion
  60.     (save-restriction
  61.       (narrow-to-region (point) (mark))
  62.       (goto-char (point-min))
  63.       (and delimited (setq regexp (concat "\\b" regexp "\\b")))
  64.       (while (re-search-forward regexp nil t)
  65.         (replace-match to-string not-inter nil))))
  66.       (or no-msg not-inter (message "Done")))))
  67.  
  68. (defun replace-string-region (from-string to-string &optional delimited no-msg)
  69.   "Replace occurrences in region, even before point, of FROM-STRING with
  70. TO-STRING.  Preserve case in each match if case-replace and case-fold-search
  71. are non-nil and FROM-STRING has no uppercase letters.  Optional third arg
  72. DELIMITED, prefix arg if interactive, non-nil means replace only matches
  73. surrounded by word boundaries.  Optional fourth arg NO-MSG non-nil means skip
  74. printing of 'Done' message."
  75.   (interactive "sReplace string in region: \nsReplace string %s with: \nP")
  76.   (let ((not-inter (not (interactive-p))))
  77.     (if (null (mark))
  78.     (error "Mark is not set, no region to replace within. ")
  79.       (save-excursion
  80.     (save-restriction
  81.       (narrow-to-region (point) (mark))
  82.       (goto-char (point-min))
  83.       (let ((search-function 'search-forward))
  84.         (and delimited
  85.          (setq from-string
  86.                (concat "\\b" (regexp-quote from-string) "\\b")
  87.                search-function 're-search-forward))
  88.         (while (funcall search-function from-string nil t)
  89.           (replace-match to-string not-inter t)))))
  90.       (or no-msg not-inter (message "Done")))))
  91.  
  92. (provide 'rep-region)
  93. --
  94. Send me detailed ideas on what you want in a Personalized Information
  95. Environment.  You have to know what you want or need to get it.  -- Bob
  96.