~ubuntu-branches/ubuntu/karmic/emacs-snapshot/karmic

« back to all changes in this revision

Viewing changes to lisp/replace.el

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2009-04-05 09:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090405091430-nw07lynn2arotjbe
Tags: upstream-20090320
ImportĀ upstreamĀ versionĀ 20090320

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
;;; replace.el --- replace commands for Emacs
2
2
 
3
3
;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001,
4
 
;;   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
4
;;   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
 
5
;;   Free Software Foundation, Inc.
5
6
 
6
7
;; Maintainer: FSF
7
8
 
28
29
;;; Code:
29
30
 
30
31
(defcustom case-replace t
31
 
  "*Non-nil means `query-replace' should preserve case in replacements."
 
32
  "Non-nil means `query-replace' should preserve case in replacements."
32
33
  :type 'boolean
33
34
  :group 'matching)
34
35
 
62
63
  :version "20.3")
63
64
 
64
65
(defcustom query-replace-skip-read-only nil
65
 
  "*Non-nil means `query-replace' and friends ignore read-only matches."
 
66
  "Non-nil means `query-replace' and friends ignore read-only matches."
66
67
  :type 'boolean
67
68
  :group 'matching
68
69
  :version "22.1")
69
70
 
70
71
(defcustom query-replace-show-replacement t
71
 
  "*Non-nil means to show what actual replacement text will be."
 
72
  "Non-nil means to show what actual replacement text will be."
72
73
  :type 'boolean
73
74
  :group 'matching
74
75
  :version "23.1")
75
76
 
76
77
(defcustom query-replace-highlight t
77
 
  "*Non-nil means to highlight matches during query replacement."
 
78
  "Non-nil means to highlight matches during query replacement."
78
79
  :type 'boolean
79
80
  :group 'matching)
80
81
 
81
82
(defcustom query-replace-lazy-highlight t
82
 
  "*Controls the lazy-highlighting during query replacements.
 
83
  "Controls the lazy-highlighting during query replacements.
83
84
When non-nil, all text in the buffer matching the current match
84
85
is highlighted lazily using isearch lazy highlighting (see
85
86
`lazy-highlight-initial-delay' and `lazy-highlight-interval')."
223
224
Fourth and fifth arg START and END specify the region to operate on.
224
225
 
225
226
To customize possible responses, change the \"bindings\" in `query-replace-map'."
226
 
  (interactive (let ((common
227
 
                      (query-replace-read-args
228
 
                       (if (and transient-mark-mode mark-active)
229
 
                         "Query replace in region"
230
 
                         "Query replace")
231
 
                         nil)))
232
 
                 (list (nth 0 common) (nth 1 common) (nth 2 common)
233
 
                       ;; These are done separately here
234
 
                       ;; so that command-history will record these expressions
235
 
                       ;; rather than the values they had this time.
236
 
                       (if (and transient-mark-mode mark-active)
237
 
                           (region-beginning))
238
 
                       (if (and transient-mark-mode mark-active)
239
 
                           (region-end)))))
 
227
  (interactive
 
228
   (let ((common
 
229
          (query-replace-read-args
 
230
           (concat "Query replace"
 
231
                   (if current-prefix-arg " word" "")
 
232
                   (if (and transient-mark-mode mark-active) " in region" ""))
 
233
           nil)))
 
234
     (list (nth 0 common) (nth 1 common) (nth 2 common)
 
235
           ;; These are done separately here
 
236
           ;; so that command-history will record these expressions
 
237
           ;; rather than the values they had this time.
 
238
           (if (and transient-mark-mode mark-active)
 
239
               (region-beginning))
 
240
           (if (and transient-mark-mode mark-active)
 
241
               (region-end)))))
240
242
  (perform-replace from-string to-string t nil delimited nil nil start end))
241
243
 
242
244
(define-key esc-map "%" 'query-replace)
289
291
  (interactive
290
292
   (let ((common
291
293
          (query-replace-read-args
292
 
           (if (and transient-mark-mode mark-active)
293
 
               "Query replace regexp in region"
294
 
             "Query replace regexp")
 
294
           (concat "Query replace"
 
295
                   (if current-prefix-arg " word" "")
 
296
                   " regexp"
 
297
                   (if (and transient-mark-mode mark-active) " in region" ""))
295
298
           t)))
296
299
     (list (nth 0 common) (nth 1 common) (nth 2 common)
297
300
           ;; These are done separately here
447
450
  (interactive
448
451
   (let ((common
449
452
          (query-replace-read-args
450
 
           (if (and transient-mark-mode mark-active)
451
 
               "Replace string in region"
452
 
             "Replace string")
 
453
           (concat "Replace"
 
454
                   (if current-prefix-arg " word" "")
 
455
                   " string"
 
456
                   (if (and transient-mark-mode mark-active) " in region" ""))
453
457
           nil)))
454
458
     (list (nth 0 common) (nth 1 common) (nth 2 common)
455
459
           (if (and transient-mark-mode mark-active)
504
508
  (interactive
505
509
   (let ((common
506
510
          (query-replace-read-args
507
 
           (if (and transient-mark-mode mark-active)
508
 
               "Replace regexp in region"
509
 
             "Replace regexp")
 
511
           (concat "Replace"
 
512
                   (if current-prefix-arg " word" "")
 
513
                   " regexp"
 
514
                   (if (and transient-mark-mode mark-active) " in region" ""))
510
515
           t)))
511
516
     (list (nth 0 common) (nth 1 common) (nth 2 common)
512
517
           (if (and transient-mark-mode mark-active)
522
527
Maximum length of the history list is determined by the value
523
528
of `history-length', which see.")
524
529
 
525
 
(defun read-regexp (prompt &optional default)
 
530
(defun read-regexp (prompt &optional default-value)
526
531
  "Read regexp as a string using the regexp history and some useful defaults.
527
532
Prompt for a regular expression with PROMPT (without a colon and
528
 
space) in the minibuffer.  The optional string argument DEFAULT
529
 
provides the basic default value, that is returned on typing RET.
530
 
Additional defaults are the string at point, the last isearch regexp,
531
 
the last isearch string, and the last replacement regexp."
 
533
space) in the minibuffer.  The optional argument DEFAULT-VALUE
 
534
provides the value to display in the minibuffer prompt that is
 
535
returned if the user just types RET.
 
536
Values available via M-n are the string at point, the last isearch
 
537
regexp, the last isearch string, and the last replacement regexp."
532
538
  (let* ((defaults
533
539
           (list (regexp-quote
534
540
                  (or (funcall (or find-tag-default-function
544
550
         (history-add-new-input nil)
545
551
         (input
546
552
          (read-from-minibuffer
547
 
           (if default
548
 
               (format "%s (default %s): " prompt (query-replace-descr default))
 
553
           (if default-value
 
554
               (format "%s (default %s): " prompt
 
555
                       (query-replace-descr default-value))
549
556
             (format "%s: " prompt))
550
557
           nil nil nil 'regexp-history defaults t)))
551
558
    (if (equal input "")
552
 
        default
 
559
        default-value
553
560
      (prog1 input
554
561
        (add-to-history 'regexp-history input)))))
555
562
 
563
570
  "Read arguments for `keep-lines' and friends.
564
571
Prompt for a regexp with PROMPT.
565
572
Value is a list, (REGEXP)."
566
 
  (list (read-regexp prompt) nil nil t))
 
573
  (list (read-regexp prompt "") nil nil t))
567
574
 
568
575
(defun keep-lines (regexp &optional rstart rend interactive)
569
576
  "Delete all lines except those containing matches for REGEXP.
697
704
  "Print and return number of matches for REGEXP following point.
698
705
When called from Lisp and INTERACTIVE is omitted or nil, just return
699
706
the number, do not print it; if INTERACTIVE is t, the function behaves
700
 
in all respects has if it had been called interactively.
 
707
in all respects as if it had been called interactively.
701
708
 
702
709
If REGEXP contains upper case characters (excluding those preceded by `\\')
703
710
and `search-upper-case' is non-nil, the matching is case-sensitive.
767
774
                               "Auto Occurrence Display"
768
775
                               "Display another occurrence when moving the cursor"))
769
776
    (define-key map [separator-1] '("--"))
770
 
    (define-key map [kill-this-buffer] 
 
777
    (define-key map [kill-this-buffer]
771
778
      '(menu-item "Kill occur buffer" kill-this-buffer
772
779
                  :help "Kill the current *Occur* buffer"))
773
 
    (define-key map [quit-window] 
 
780
    (define-key map [quit-window]
774
781
      '(menu-item "Quit occur window" quit-window
775
782
                  :help "Quit the current *Occur* buffer.  Bury it, and maybe delete the selected frame"))
776
 
    (define-key map [revert-buffer] 
 
783
    (define-key map [revert-buffer]
777
784
      '(menu-item "Revert occur buffer" revert-buffer
778
785
                  :help "Replace the text in the *Occur* buffer with the results of rerunning occur"))
779
 
    (define-key map [clone-buffer] 
 
786
    (define-key map [clone-buffer]
780
787
      '(menu-item "Clone occur buffer" clone-buffer
781
788
                  :help "Create and return a twin copy of the current *Occur* buffer"))
782
 
    (define-key map [occur-rename-buffer] 
 
789
    (define-key map [occur-rename-buffer]
783
790
      '(menu-item "Rename occur buffer" occur-rename-buffer
784
791
                  :help "Rename the current *Occur* buffer to *Occur: original-buffer-name*."))
785
792
    (define-key map [separator-2] '("--"))
792
799
    (define-key map [occur-mode-display-occurrence]
793
800
      '(menu-item "Display Occurrence" occur-mode-display-occurrence
794
801
                  :help "Display in another window the occurrence the current line describes"))
795
 
    (define-key map [occur-next] 
 
802
    (define-key map [occur-next]
796
803
      '(menu-item "Move to next match" occur-next
797
804
                  :help "Move to the Nth (default 1) next match in an Occur mode buffer"))
798
 
    (define-key map [occur-prev] 
 
805
    (define-key map [occur-prev]
799
806
      '(menu-item "Move to previous match" occur-prev
800
807
                  :help "Move to the Nth (default 1) previous match in an Occur mode buffer"))
801
808
    map)
964
971
  :version "22.1")
965
972
 
966
973
(defcustom list-matching-lines-default-context-lines 0
967
 
  "*Default number of context lines included around `list-matching-lines' matches.
 
974
  "Default number of context lines included around `list-matching-lines' matches.
968
975
A negative number means to include that many lines before the match.
969
976
A positive number means to include that many lines both before and after."
970
977
  :type 'integer
973
980
(defalias 'list-matching-lines 'occur)
974
981
 
975
982
(defcustom list-matching-lines-face 'match
976
 
  "*Face used by \\[list-matching-lines] to show the text that matches.
 
983
  "Face used by \\[list-matching-lines] to show the text that matches.
977
984
If the value is nil, don't highlight the matching portions specially."
978
985
  :type 'face
979
986
  :group 'matching)
980
987
 
981
988
(defcustom list-matching-lines-buffer-name-face 'underline
982
 
  "*Face used by \\[list-matching-lines] to show the names of buffers.
 
989
  "Face used by \\[list-matching-lines] to show the names of buffers.
983
990
If the value is nil, don't highlight the buffer names specially."
984
991
  :type 'face
985
992
  :group 'matching)
987
994
(defcustom occur-excluded-properties
988
995
  '(read-only invisible intangible field mouse-face help-echo local-map keymap
989
996
    yank-handler follow-link)
990
 
  "*Text properties to discard when copying lines to the *Occur* buffer.
 
997
  "Text properties to discard when copying lines to the *Occur* buffer.
991
998
The value should be a list of text properties to discard or t,
992
999
which means to discard all text properties."
993
1000
  :type '(choice (const :tag "All" t) (repeat symbol))
1565
1572
 
1566
1573
    ;; If last typed key in previous call of multi-buffer perform-replace
1567
1574
    ;; was `automatic-all', don't ask more questions in next files
1568
 
    (when (eq (lookup-key map (vector last-input-char)) 'automatic-all)
 
1575
    (when (eq (lookup-key map (vector last-input-event)) 'automatic-all)
1569
1576
      (setq query-flag nil multi-buffer t))
1570
1577
 
1571
1578
    ;; REPLACEMENTS is either a string, a list of strings, or a cons cell
1714
1721
                         (with-output-to-temp-buffer "*Help*"
1715
1722
                           (princ
1716
1723
                            (concat "Query replacing "
 
1724
                                    (if delimited-flag "word " "")
1717
1725
                                    (if regexp-flag "regexp " "")
1718
1726
                                    from-string " with "
1719
1727
                                    next-replacement ".\n\n"