home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / emcs1857 / 1857bi~1.zoo / lisp / replace.el < prev    next >
Encoding:
Text File  |  1992-02-03  |  10.2 KB  |  308 lines

  1. ;; Replace commands for Emacs.
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (fset 'delete-non-matching-lines 'keep-lines)
  22. (defun keep-lines (regexp)
  23.   "Delete all lines except those containing matches for REGEXP.
  24. A match split across lines preserves all the lines it lies in.
  25. Applies to all lines after point."
  26.   (interactive "sKeep lines (containing match for regexp): ")
  27.   (save-excursion
  28.     (or (bolp) (forward-line 1))
  29.     (let ((start (point)))
  30.       (while (not (eobp))
  31.     ;; Start is first char not preserved by previous match.
  32.     (if (not (re-search-forward regexp nil 'move))
  33.         (delete-region start (point-max))
  34.       (let ((end (save-excursion (goto-char (match-beginning 0))
  35.                      (beginning-of-line)
  36.                      (point))))
  37.         ;; Now end is first char preserved by the new match.
  38.         (if (< start end)
  39.         (delete-region start end))))
  40.     (setq start (save-excursion (forward-line 1)
  41.                     (point)))))))
  42.  
  43. (fset 'delete-matching-lines 'flush-lines)
  44. (defun flush-lines (regexp)
  45.   "Delete lines containing matches for REGEXP.
  46. If a match is split across lines, all the lines it lies in are deleted.
  47. Applies to lines after point."
  48.   (interactive "sFlush lines (containing match for regexp): ")
  49.   (save-excursion
  50.     (while (and (not (eobp))
  51.         (re-search-forward regexp nil t))
  52.       (delete-region (save-excursion (goto-char (match-beginning 0))
  53.                      (beginning-of-line)
  54.                      (point))
  55.              (progn (forward-line 1) (point))))))
  56.  
  57. (fset 'count-matches 'how-many)
  58. (defun how-many (regexp)
  59.   "Print number of matches for REGEXP following point."
  60.   (interactive "sHow many matches for (regexp): ")
  61.   (let ((count 0) opoint)
  62.     (save-excursion
  63.      (while (and (not (eobp))
  64.          (progn (setq opoint (point))
  65.             (re-search-forward regexp nil t)))
  66.        (if (= opoint (point))
  67.        (forward-char 1)
  68.      (setq count (1+ count))))
  69.      (message "%d occurrences" count))))
  70.  
  71. (defvar occur-mode-map ())
  72. (if occur-mode-map
  73.     ()
  74.   (setq occur-mode-map (make-sparse-keymap))
  75.   (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence))
  76.  
  77. (defvar occur-buffer nil)
  78. (defvar occur-nlines nil)
  79. (defvar occur-pos-list nil)
  80.  
  81. (defun occur-mode ()
  82.   "Major mode for output from \\[occur].
  83. Move point to one of the occurrences in this buffer,
  84. then use \\[occur-mode-goto-occurrence] to go to the same occurrence
  85. in the buffer that the occurrences were found in.
  86. \\{occur-mode-map}"
  87.   (kill-all-local-variables)
  88.   (use-local-map occur-mode-map)
  89.   (setq major-mode 'occur-mode)
  90.   (setq mode-name "Occur")
  91.   (make-local-variable 'occur-buffer)
  92.   (make-local-variable 'occur-nlines)
  93.   (make-local-variable 'occur-pos-list))
  94.  
  95. (defun occur-mode-goto-occurrence ()
  96.   "Go to the line this occurrence was found in, in the buffer it was found in."
  97.   (interactive)
  98.   (if (or (null occur-buffer)
  99.       (null (buffer-name occur-buffer)))
  100.       (progn
  101.     (setq occur-buffer nil
  102.           occur-pos-list nil)
  103.     (error "Buffer in which occurences were found is deleted.")))
  104.   (let* ((occur-number (save-excursion
  105.             (beginning-of-line)
  106.             (/ (1- (count-lines (point-min) (point)))
  107.                (cond ((< occur-nlines 0)
  108.                   (- 2 occur-nlines))
  109.                  ((> occur-nlines 0)
  110.                   (+ 2 (* 2 occur-nlines)))
  111.                  (t 1)))))
  112.      (pos (nth occur-number occur-pos-list)))
  113.     (pop-to-buffer occur-buffer)
  114.     (goto-char (marker-position pos))))
  115.  
  116. (defvar list-matching-lines-default-context-lines 0
  117.   "*Default number of context lines to include around a list-matching-lines
  118. match.  A negative number means to include that many lines before the match.
  119. A positive number means to include that many lines both before and after.")
  120.  
  121. (fset 'list-matching-lines 'occur)
  122. (defun occur (regexp &optional nlines)
  123.   "Show all lines following point containing a match for REGEXP.
  124. Display each line with NLINES lines before and after,
  125.  or -NLINES before if NLINES is negative.
  126. NLINES defaults to list-matching-lines-default-context-lines.
  127. Interactively it is the prefix arg.
  128.  
  129. The lines are shown in a buffer named *Occur*.
  130. It serves as a menu to find any of the occurrences in this buffer.
  131. \\[describe-mode] in that buffer will explain how."
  132.   (interactive "sList lines matching regexp: \nP")
  133.   (setq nlines (if nlines (prefix-numeric-value nlines)
  134.          list-matching-lines-default-context-lines))
  135.   (let ((first t)
  136.     (buffer (current-buffer))
  137.     linenum prevpos)
  138.     (save-excursion
  139.       (beginning-of-line)
  140.       (setq linenum (1+ (count-lines (point-min) (point))))
  141.       (setq prevpos (point)))
  142.     (with-output-to-temp-buffer "*Occur*"
  143.       (save-excursion
  144.     (set-buffer standard-output)
  145.     (insert "Lines matching ")
  146.     (prin1 regexp)
  147.     (insert " in buffer " (buffer-name buffer) ?. ?\n)
  148.     (occur-mode)
  149.     (setq occur-buffer buffer)
  150.     (setq occur-nlines nlines)
  151.     (setq occur-pos-list ()))
  152.       (if (eq buffer standard-output)
  153.       (goto-char (point-max)))
  154.       (save-excursion
  155.     ;; Find next match, but give up if prev match was at end of buffer.
  156.     (while (and (not (= prevpos (point-max)))
  157.             (re-search-forward regexp nil t))
  158.       (beginning-of-line 1)
  159.       (save-excursion
  160.         (setq linenum (+ linenum (count-lines prevpos (point))))
  161.         (setq prevpos (point)))
  162.       (let* ((start (save-excursion
  163.               (forward-line (if (< nlines 0) nlines (- nlines)))
  164.               (point)))
  165.          (end (save-excursion
  166.             (if (> nlines 0)
  167.                 (forward-line (1+ nlines))
  168.                 (forward-line 1))
  169.             (point)))
  170.          (tag (format "%3d" linenum))
  171.          (empty (make-string (length tag) ?\ ))
  172.          tem)
  173.         (save-excursion
  174.           (setq tem (make-marker))
  175.           (set-marker tem (point))
  176.           (set-buffer standard-output)
  177.           (setq occur-pos-list (cons tem occur-pos-list))
  178.           (or first (zerop nlines)
  179.           (insert "--------\n"))
  180.           (setq first nil)
  181.           (insert-buffer-substring buffer start end)
  182.           (backward-char (- end start))
  183.           (setq tem (if (< nlines 0) (- nlines) nlines))
  184.           (while (> tem 0)
  185.         (insert empty ?:)
  186.         (forward-line 1)
  187.         (setq tem (1- tem)))
  188.           (insert tag ?:)
  189.           (forward-line 1)
  190.           (while (< tem nlines)
  191.         (insert empty ?:)
  192.         (forward-line 1)
  193.         (setq tem (1+ tem))))                
  194.         (forward-line 1)))
  195.     (set-buffer standard-output)
  196.     ;; Put positions in increasing order to go with buffer.
  197.     (setq occur-pos-list (nreverse occur-pos-list))
  198.     (if (interactive-p)
  199.         (message "%d matching lines." (length occur-pos-list)))))))
  200.  
  201. (defconst query-replace-help
  202.   "Type Space or `y' to replace one match, Delete or `n' to skip to next,
  203. ESC or `q' to exit, Period to replace one match and exit,
  204. Comma to replace but not move point immediately,
  205. C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
  206. C-w to delete match and recursive edit,
  207. C-l to clear the screen, redisplay, and offer same replacement again,
  208. ! to replace all remaining matches with no more questions,
  209. ^ to move point back to previous match."
  210.   "Help message while in query-replace")
  211.  
  212. (defun perform-replace (from-string to-string
  213.                 query-flag regexp-flag delimited-flag)
  214.   (let ((nocasify (not (and case-fold-search case-replace
  215.                 (string-equal from-string
  216.                       (downcase from-string)))))
  217.     (literal (not regexp-flag))
  218.     (search-function (if regexp-flag 're-search-forward 'search-forward))
  219.     (search-string from-string)
  220.     (keep-going t)
  221.     (lastrepl nil))            ;Position after last match considered.
  222.     (if delimited-flag
  223.     (setq search-function 're-search-forward
  224.           search-string (concat "\\b"
  225.                     (if regexp-flag from-string
  226.                       (regexp-quote from-string))
  227.                     "\\b")))
  228.     (push-mark)
  229.     (push-mark)
  230.     (while (and keep-going
  231.         (not (eobp))
  232.         (progn
  233.          (set-mark (point))
  234.          (funcall search-function search-string nil t)))
  235.       ;; Don't replace the null string 
  236.       ;; right after end of previous replacement.
  237.       (if (eq lastrepl (point))
  238.       (forward-char 1)
  239.     (undo-boundary)
  240.     (if (not query-flag)
  241.         (replace-match to-string nocasify literal)
  242.       (let (done replaced)
  243.         (while (not done)
  244.           ;; Preserve the match data.  Process filters and sentinels
  245.           ;; could run inside read-char..
  246.           (let ((data (match-data))
  247.             (help-form
  248.              '(concat "Query replacing "
  249.                   (if regexp-flag "regexp " "")
  250.                   from-string " with " to-string ".\n\n"
  251.                   (substitute-command-keys query-replace-help))))
  252.         (setq char help-char)
  253.         (while (= char help-char)
  254.           (message "Query replacing %s with %s: " from-string to-string)
  255.           (setq char (read-char))
  256.           (if (= char ??)
  257.               (setq unread-command-char help-char char help-char)))
  258.         (store-match-data data))
  259.           (cond ((or (= char ?\e)
  260.              (= char ?q))
  261.              (setq keep-going nil)
  262.              (setq done t))
  263.             ((= char ?^)
  264.              (goto-char (mark))
  265.              (setq replaced t))
  266.             ((or (= char ?\ )
  267.              (= char ?y))
  268.              (or replaced
  269.              (replace-match to-string nocasify literal))
  270.              (setq done t))
  271.             ((= char ?\.)
  272.              (or replaced
  273.              (replace-match to-string nocasify literal))
  274.              (setq keep-going nil)
  275.              (setq done t))
  276.             ((= char ?\,)
  277.              (if (not replaced)
  278.              (progn
  279.                (replace-match to-string nocasify literal)
  280.                (setq replaced t))))
  281.             ((= char ?!)
  282.              (or replaced
  283.              (replace-match to-string nocasify literal))
  284.              (setq done t query-flag nil))
  285.             ((or (= char ?\177)
  286.              (= char ?n))
  287.              (setq done t))
  288.             ((= char ?\C-l)
  289.              (recenter nil))
  290.             ((= char ?\C-r)
  291.              (store-match-data
  292.                (prog1 (match-data)
  293.              (save-excursion (recursive-edit)))))
  294.             ((= char ?\C-w)
  295.              (delete-region (match-beginning 0) (match-end 0))
  296.              (store-match-data
  297.                (prog1 (match-data)
  298.              (save-excursion (recursive-edit))))
  299.              (setq replaced t))
  300.             (t
  301.              (setq keep-going nil)
  302.              (setq unread-command-char char)
  303.              (setq done t))))))
  304.     (setq lastrepl (point))))
  305.     (pop-mark)
  306.     keep-going))
  307.  
  308.