comparison lisp/replace.el @ 444:576fb035e263 r21-2-37

Import from CVS: tag r21-2-37
author cvs
date Mon, 13 Aug 2007 11:36:19 +0200
parents abe6d1db359e
children 3078fd1074e8
comparison
equal deleted inserted replaced
443:a8296e22da4e 444:576fb035e263
54 That becomes the \"string to replace\".") 54 That becomes the \"string to replace\".")
55 55
56 (defvar replace-search-function 56 (defvar replace-search-function
57 (lambda (str limit) 57 (lambda (str limit)
58 (search-forward str limit t)) 58 (search-forward str limit t))
59 "Function used by perform-replace to search forward for a string. It will be 59 "Function used by perform-replace to search forward for a string. It will be
60 called with two arguments: the string to search for and a limit bounding the 60 called with two arguments: the string to search for and a limit bounding the
61 search.") 61 search.")
62 62
63 (defvar replace-re-search-function 63 (defvar replace-re-search-function
64 (lambda (regexp limit) 64 (lambda (regexp limit)
320 ; (pop-to-buffer buffer) 320 ; (pop-to-buffer buffer)
321 ; (goto-char (marker-position pos)))) 321 ; (goto-char (marker-position pos))))
322 322
323 (defun occur-mode-mouse-goto (event) 323 (defun occur-mode-mouse-goto (event)
324 "Go to the occurrence highlighted by mouse. 324 "Go to the occurrence highlighted by mouse.
325 This function is only reasonable when bound to a mouse key in the occur buffer" 325 This function should be bound to a mouse key in the `*Occur*' buffer."
326 (interactive "e") 326 (interactive "e")
327 (let ((window-save (selected-window)) 327 (let ((window-save (selected-window))
328 (frame-save (selected-frame))) 328 (frame-save (selected-frame)))
329 ;; preserve the window/frame setup 329 ;; preserve the window/frame setup
330 (unwind-protect 330 (unwind-protect
496 (setq occur-pos-list (cons tem occur-pos-list)) 496 (setq occur-pos-list (cons tem occur-pos-list))
497 (or first (zerop nlines) 497 (or first (zerop nlines)
498 (insert "--------\n")) 498 (insert "--------\n"))
499 (setq first nil) 499 (setq first nil)
500 (insert-buffer-substring buffer start end) 500 (insert-buffer-substring buffer start end)
501 (set-marker final-context-start 501 (set-marker final-context-start
502 (- (point) (- end (match-end 0)))) 502 (- (point) (- end (match-end 0))))
503 (backward-char (- end start)) 503 (backward-char (- end start))
504 (setq tem (if (< nlines 0) (- nlines) nlines)) 504 (setq tem (if (< nlines 0) (- nlines) nlines))
505 (while (> tem 0) 505 (while (> tem 0)
506 (insert empty ?:) 506 (insert empty ?:)
509 (let ((this-linenum linenum)) 509 (let ((this-linenum linenum))
510 (while (< (point) final-context-start) 510 (while (< (point) final-context-start)
511 (if (null tag) 511 (if (null tag)
512 (setq tag (format "%5d" this-linenum))) 512 (setq tag (format "%5d" this-linenum)))
513 (insert tag ?:) 513 (insert tag ?:)
514 ;; FSFmacs -- 514 ;; FSFmacs --
515 ;; we handle this using mode-motion-highlight-line, above. 515 ;; we handle this using mode-motion-highlight-line, above.
516 ;; (put-text-property (save-excursion 516 ;; (put-text-property (save-excursion
517 ;; (beginning-of-line) 517 ;; (beginning-of-line)
518 ;; (point)) 518 ;; (point))
519 ;; (save-excursion 519 ;; (save-excursion
544 (message "%d matching lines." (length occur-pos-list)))))))) 544 (message "%d matching lines." (length occur-pos-list))))))))
545 545
546 ;; It would be nice to use \\[...], but there is no reasonable way 546 ;; It would be nice to use \\[...], but there is no reasonable way
547 ;; to make that display both SPC and Y. 547 ;; to make that display both SPC and Y.
548 (defconst query-replace-help 548 (defconst query-replace-help
549 (purecopy 549 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
550 "Type Space or `y' to replace one match, Delete or `n' to skip to next,
551 RET or `q' to exit, Period to replace one match and exit, 550 RET or `q' to exit, Period to replace one match and exit,
552 Comma to replace but not move point immediately, 551 Comma to replace but not move point immediately,
553 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again), 552 C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
554 C-w to delete match and recursive edit, 553 C-w to delete match and recursive edit,
555 C-l to clear the frame, redisplay, and offer same replacement again, 554 C-l to clear the frame, redisplay, and offer same replacement again,
556 ! to replace all remaining matches with no more questions, 555 ! to replace all remaining matches with no more questions,
557 ^ to move point back to previous match." 556 ^ to move point back to previous match."
558 ) 557
559 "Help message while in query-replace") 558 "Help message while in query-replace")
560 559
561 (defvar query-replace-map nil 560 (defvar query-replace-map nil
562 "Keymap that defines the responses to questions in `query-replace'. 561 "Keymap that defines the responses to questions in `query-replace'.
563 The \"bindings\" in this map are not commands; they are answers. 562 The \"bindings\" in this map are not commands; they are answers.
594 (define-key map "?" 'help) 593 (define-key map "?" 'help)
595 (define-key map "\C-g" 'quit) 594 (define-key map "\C-g" 'quit)
596 (define-key map "\C-]" 'quit) 595 (define-key map "\C-]" 'quit)
597 ;FSFmacs (define-key map "\e" 'exit-prefix) 596 ;FSFmacs (define-key map "\e" 'exit-prefix)
598 (define-key map [escape] 'exit-prefix) 597 (define-key map [escape] 'exit-prefix)
599 598
600 (setq query-replace-map map))) 599 (setq query-replace-map map)))
601 600
602 ;; isearch-mode is dumped, so don't autoload. 601 ;; isearch-mode is dumped, so don't autoload.
603 ;(autoload 'isearch-highlight "isearch") 602 ;(autoload 'isearch-highlight "isearch")
604 603
622 Don't use this in your own program unless you want to query and set the mark 621 Don't use this in your own program unless you want to query and set the mark
623 just as `query-replace' does. Instead, write a simple loop like this: 622 just as `query-replace' does. Instead, write a simple loop like this:
624 (while (re-search-forward \"foo[ \t]+bar\" nil t) 623 (while (re-search-forward \"foo[ \t]+bar\" nil t)
625 (replace-match \"foobar\" nil nil)) 624 (replace-match \"foobar\" nil nil))
626 which will run faster and probably do exactly what you want. 625 which will run faster and probably do exactly what you want.
627 When searching for a match, this function use `replace-search-function' and `replace-re-search-function'" 626 When searching for a match, this function uses
627 `replace-search-function' and `replace-re-search-function'."
628 (or map (setq map query-replace-map)) 628 (or map (setq map query-replace-map))
629 (let* ((event (make-event)) 629 (let* ((event (make-event))
630 (nocasify (not (and case-fold-search case-replace 630 (nocasify (not (and case-fold-search case-replace
631 (string-equal from-string 631 (string-equal from-string
632 (downcase from-string))))) 632 (downcase from-string)))))
633 (literal (not regexp-flag)) 633 (literal (not regexp-flag))
634 (search-function (if regexp-flag 634 (search-function (if regexp-flag
635 replace-re-search-function 635 replace-re-search-function
636 replace-search-function)) 636 replace-search-function))
637 (search-string from-string) 637 (search-string from-string)
638 (real-match-data nil) ; the match data for the current match 638 (real-match-data nil) ; the match data for the current match
639 (next-replacement nil) 639 (next-replacement nil)
640 (replacement-index 0) 640 (replacement-index 0)
690 (eq lastrepl (match-beginning 0)) 690 (eq lastrepl (match-beginning 0))
691 (not match-again))) 691 (not match-again)))
692 (if (or (eobp) 692 (if (or (eobp)
693 (and limit (>= (point) limit))) 693 (and limit (>= (point) limit)))
694 nil 694 nil
695 ;; Don't replace the null string 695 ;; Don't replace the null string
696 ;; right after end of previous replacement. 696 ;; right after end of previous replacement.
697 (forward-char 1) 697 (forward-char 1)
698 (let ((case-fold-search qr-case-fold-search)) 698 (let ((case-fold-search qr-case-fold-search))
699 (funcall search-function search-string limit))) 699 (funcall search-function search-string limit)))
700 t)) 700 t))
703 (setq real-match-data (match-data)) 703 (setq real-match-data (match-data))
704 704
705 ;; Before we make the replacement, decide whether the search string 705 ;; Before we make the replacement, decide whether the search string
706 ;; can match again just after this match. 706 ;; can match again just after this match.
707 (if regexp-flag 707 (if regexp-flag
708 (progn 708 (progn
709 (setq match-again (looking-at search-string)) 709 (setq match-again (looking-at search-string))
710 ;; XEmacs addition 710 ;; XEmacs addition
711 (store-match-data real-match-data))) 711 (store-match-data real-match-data)))
712 ;; If time for a change, advance to next replacement string. 712 ;; If time for a change, advance to next replacement string.
713 (if (and (listp replacements) 713 (if (and (listp replacements)