Mercurial > emacs
comparison lisp/replace.el @ 14819:ece1210e739f
(occur): If no matches, just display in echo area.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 17 Mar 1996 17:58:42 +0000 |
parents | 83f275dcd93a |
children | 04f81516b6e0 |
comparison
equal
deleted
inserted
replaced
14818:3349089ced14 | 14819:ece1210e739f |
---|---|
1 ;;; replace.el --- replace commands for Emacs. | 1 ;;; replace.el --- replace commands for Emacs. |
2 | 2 |
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc. | 3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996 Free Software Foundation, Inc. |
4 | 4 |
5 ;; This file is part of GNU Emacs. | 5 ;; This file is part of GNU Emacs. |
6 | 6 |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | 7 ;; GNU Emacs is free software; you can redistribute it and/or modify |
8 ;; it under the terms of the GNU General Public License as published by | 8 ;; it under the terms of the GNU General Public License as published by |
351 (final-context-start (make-marker))) | 351 (final-context-start (make-marker))) |
352 ;;; (save-excursion | 352 ;;; (save-excursion |
353 ;;; (beginning-of-line) | 353 ;;; (beginning-of-line) |
354 ;;; (setq linenum (1+ (count-lines (point-min) (point)))) | 354 ;;; (setq linenum (1+ (count-lines (point-min) (point)))) |
355 ;;; (setq prevpos (point))) | 355 ;;; (setq prevpos (point))) |
356 (with-output-to-temp-buffer "*Occur*" | 356 (save-excursion |
357 (save-excursion | 357 (goto-char (point-min)) |
358 (set-buffer standard-output) | 358 ;; Check first whether there are any matches at all. |
359 (setq default-directory dir) | 359 (if (not (re-search-forward regexp nil t)) |
360 ;; We will insert the number of lines, and "lines", later. | 360 (message "No matches for `%s'" regexp) |
361 (insert " matching ") | 361 ;; Back up, so the search loop below will find the first match. |
362 (let ((print-escape-newlines t)) | 362 (goto-char (match-beginning 0)) |
363 (prin1 regexp)) | 363 (with-output-to-temp-buffer "*Occur*" |
364 (insert " in buffer " (buffer-name buffer) ?. ?\n) | 364 (save-excursion |
365 (occur-mode) | 365 (set-buffer standard-output) |
366 (setq occur-buffer buffer) | 366 (setq default-directory dir) |
367 (setq occur-nlines nlines) | 367 ;; We will insert the number of lines, and "lines", later. |
368 (setq occur-pos-list ())) | 368 (insert " matching ") |
369 (if (eq buffer standard-output) | 369 (let ((print-escape-newlines t)) |
370 (goto-char (point-max))) | 370 (prin1 regexp)) |
371 (save-excursion | 371 (insert " in buffer " (buffer-name buffer) ?. ?\n) |
372 (beginning-of-buffer) | 372 (occur-mode) |
373 ;; Find next match, but give up if prev match was at end of buffer. | 373 (setq occur-buffer buffer) |
374 (while (and (not (= prevpos (point-max))) | 374 (setq occur-nlines nlines) |
375 (re-search-forward regexp nil t)) | 375 (setq occur-pos-list ())) |
376 (goto-char (match-beginning 0)) | 376 (if (eq buffer standard-output) |
377 (beginning-of-line) | |
378 (save-match-data | |
379 (setq linenum (+ linenum (count-lines prevpos (point))))) | |
380 (setq prevpos (point)) | |
381 (goto-char (match-end 0)) | |
382 (let* ((start (save-excursion | |
383 (goto-char (match-beginning 0)) | |
384 (forward-line (if (< nlines 0) nlines (- nlines))) | |
385 (point))) | |
386 (end (save-excursion | |
387 (goto-char (match-end 0)) | |
388 (if (> nlines 0) | |
389 (forward-line (1+ nlines)) | |
390 (forward-line 1)) | |
391 (point))) | |
392 (tag (format "%5d" linenum)) | |
393 (empty (make-string (length tag) ?\ )) | |
394 tem) | |
395 (save-excursion | |
396 (setq tem (make-marker)) | |
397 (set-marker tem (point)) | |
398 (set-buffer standard-output) | |
399 (setq occur-pos-list (cons tem occur-pos-list)) | |
400 (or first (zerop nlines) | |
401 (insert "--------\n")) | |
402 (setq first nil) | |
403 (insert-buffer-substring buffer start end) | |
404 (set-marker final-context-start | |
405 (- (point) (- end (match-end 0)))) | |
406 (backward-char (- end start)) | |
407 (setq tem nlines) | |
408 (while (> tem 0) | |
409 (insert empty ?:) | |
410 (forward-line 1) | |
411 (setq tem (1- tem))) | |
412 (let ((this-linenum linenum)) | |
413 (while (< (point) final-context-start) | |
414 (if (null tag) | |
415 (setq tag (format "%5d" this-linenum))) | |
416 (insert tag ?:) | |
417 (put-text-property (save-excursion | |
418 (beginning-of-line) | |
419 (point)) | |
420 (save-excursion | |
421 (end-of-line) | |
422 (point)) | |
423 'mouse-face 'highlight) | |
424 (forward-line 1) | |
425 (setq tag nil) | |
426 (setq this-linenum (1+ this-linenum))) | |
427 (while (<= (point) final-context-start) | |
428 (insert empty ?:) | |
429 (forward-line 1) | |
430 (setq this-linenum (1+ this-linenum)))) | |
431 (while (< tem nlines) | |
432 (insert empty ?:) | |
433 (forward-line 1) | |
434 (setq tem (1+ tem))) | |
435 (goto-char (point-max))) | 377 (goto-char (point-max))) |
436 (forward-line 1))) | 378 (save-excursion |
437 (set-buffer standard-output) | 379 ;; Find next match, but give up if prev match was at end of buffer. |
438 ;; Put positions in increasing order to go with buffer. | 380 (while (and (not (= prevpos (point-max))) |
439 (setq occur-pos-list (nreverse occur-pos-list)) | 381 (re-search-forward regexp nil t)) |
440 (goto-char (point-min)) | 382 (goto-char (match-beginning 0)) |
441 (if (= (length occur-pos-list) 1) | 383 (beginning-of-line) |
442 (insert "1 line") | 384 (save-match-data |
443 (insert (format "%d lines" (length occur-pos-list)))) | 385 (setq linenum (+ linenum (count-lines prevpos (point))))) |
444 (if (interactive-p) | 386 (setq prevpos (point)) |
445 (message "%d matching lines." (length occur-pos-list))))))) | 387 (goto-char (match-end 0)) |
388 (let* ((start (save-excursion | |
389 (goto-char (match-beginning 0)) | |
390 (forward-line (if (< nlines 0) nlines (- nlines))) | |
391 (point))) | |
392 (end (save-excursion | |
393 (goto-char (match-end 0)) | |
394 (if (> nlines 0) | |
395 (forward-line (1+ nlines)) | |
396 (forward-line 1)) | |
397 (point))) | |
398 (tag (format "%5d" linenum)) | |
399 (empty (make-string (length tag) ?\ )) | |
400 tem) | |
401 (save-excursion | |
402 (setq tem (make-marker)) | |
403 (set-marker tem (point)) | |
404 (set-buffer standard-output) | |
405 (setq occur-pos-list (cons tem occur-pos-list)) | |
406 (or first (zerop nlines) | |
407 (insert "--------\n")) | |
408 (setq first nil) | |
409 (insert-buffer-substring buffer start end) | |
410 (set-marker final-context-start | |
411 (- (point) (- end (match-end 0)))) | |
412 (backward-char (- end start)) | |
413 (setq tem nlines) | |
414 (while (> tem 0) | |
415 (insert empty ?:) | |
416 (forward-line 1) | |
417 (setq tem (1- tem))) | |
418 (let ((this-linenum linenum)) | |
419 (while (< (point) final-context-start) | |
420 (if (null tag) | |
421 (setq tag (format "%5d" this-linenum))) | |
422 (insert tag ?:) | |
423 (put-text-property (save-excursion | |
424 (beginning-of-line) | |
425 (point)) | |
426 (save-excursion | |
427 (end-of-line) | |
428 (point)) | |
429 'mouse-face 'highlight) | |
430 (forward-line 1) | |
431 (setq tag nil) | |
432 (setq this-linenum (1+ this-linenum))) | |
433 (while (<= (point) final-context-start) | |
434 (insert empty ?:) | |
435 (forward-line 1) | |
436 (setq this-linenum (1+ this-linenum)))) | |
437 (while (< tem nlines) | |
438 (insert empty ?:) | |
439 (forward-line 1) | |
440 (setq tem (1+ tem))) | |
441 (goto-char (point-max))) | |
442 (forward-line 1))) | |
443 (set-buffer standard-output) | |
444 ;; Put positions in increasing order to go with buffer. | |
445 (setq occur-pos-list (nreverse occur-pos-list)) | |
446 (goto-char (point-min)) | |
447 (if (= (length occur-pos-list) 1) | |
448 (insert "1 line") | |
449 (insert (format "%d lines" (length occur-pos-list)))) | |
450 (if (interactive-p) | |
451 (message "%d matching lines." (length occur-pos-list))))))))) | |
446 | 452 |
447 ;; It would be nice to use \\[...], but there is no reasonable way | 453 ;; It would be nice to use \\[...], but there is no reasonable way |
448 ;; to make that display both SPC and Y. | 454 ;; to make that display both SPC and Y. |
449 (defconst query-replace-help | 455 (defconst query-replace-help |
450 "Type Space or `y' to replace one match, Delete or `n' to skip to next, | 456 "Type Space or `y' to replace one match, Delete or `n' to skip to next, |