comparison lisp/replace.el @ 20658:545fec062c8c

(occur): If the matching line has no final newline, insert one anyway. Compensate for that when inserting line nums.
author Karl Heuer <kwzh@gnu.org>
date Sun, 18 Jan 1998 02:53:16 +0000
parents a30c6f1b3335
children 24a1fcdc31d7
comparison
equal deleted inserted replaced
20657:635d8acc7755 20658:545fec062c8c
489 ;; Length of matching text 489 ;; Length of matching text
490 (- (match-end 0) (match-beginning 0))) 490 (- (match-end 0) (match-beginning 0)))
491 (tag (format "%5d" linenum)) 491 (tag (format "%5d" linenum))
492 (empty (make-string (length tag) ?\ )) 492 (empty (make-string (length tag) ?\ ))
493 tem 493 tem
494 insertion-start
494 ;; Number of lines of context to show for current match. 495 ;; Number of lines of context to show for current match.
495 occur-marker 496 occur-marker
496 ;; Marker pointing to end of match in source buffer. 497 ;; Marker pointing to end of match in source buffer.
497 (text-beg 498 (text-beg
498 ;; Marker pointing to start of text for one 499 ;; Marker pointing to start of text for one
513 (setq first nil) 514 (setq first nil)
514 515
515 ;; Insert matching text including context lines from 516 ;; Insert matching text including context lines from
516 ;; source buffer into *Occur* 517 ;; source buffer into *Occur*
517 (set-marker text-beg (point)) 518 (set-marker text-beg (point))
519 (setq insertion-start (point))
518 (insert-buffer-substring buffer start end) 520 (insert-buffer-substring buffer start end)
521 (or (and (/= (+ start match-beg) end)
522 (with-current-buffer buffer
523 (eq (char-before end) ?\n)))
524 (insert "\n"))
525 (set-marker final-context-start
526 (+ (- (point) (- end (match-end 0)))
527 (if (save-excursion
528 (set-buffer buffer)
529 (save-excursion
530 (goto-char (match-end 0))
531 (end-of-line)
532 (bolp)))
533 1 0)))
519 (set-marker text-end (point)) 534 (set-marker text-end (point))
520 535
521 ;; Highlight text that was matched. 536 ;; Highlight text that was matched.
522 (if list-matching-lines-face 537 (if list-matching-lines-face
523 (put-text-property 538 (put-text-property
529 ;; occur-prev to move between matching lines. 544 ;; occur-prev to move between matching lines.
530 (put-text-property 545 (put-text-property
531 (+ (marker-position text-beg) match-beg match-len) 546 (+ (marker-position text-beg) match-beg match-len)
532 (+ (marker-position text-beg) match-beg match-len 1) 547 (+ (marker-position text-beg) match-beg match-len 1)
533 'occur-point t) 548 'occur-point t)
534 (set-marker final-context-start
535 (- (point) (- end (match-end 0))))
536 549
537 ;; Now go back to the start of the matching text 550 ;; Now go back to the start of the matching text
538 ;; adding the space and colon to the start of each line. 551 ;; adding the space and colon to the start of each line.
539 (goto-char (- (point) (- end start))) 552 (goto-char insertion-start)
540 ;; Insert space and colon for lines of context before match. 553 ;; Insert space and colon for lines of context before match.
541 (setq tem (if (< linenum nlines) 554 (setq tem (if (< linenum nlines)
542 (- nlines linenum) 555 (- nlines linenum)
543 nlines)) 556 nlines))
544 (while (> tem 0) 557 (while (> tem 0)
554 (setq tag (format "%5d" this-linenum))) 567 (setq tag (format "%5d" this-linenum)))
555 (insert tag ?:) 568 (insert tag ?:)
556 (forward-line 1) 569 (forward-line 1)
557 (setq tag nil) 570 (setq tag nil)
558 (setq this-linenum (1+ this-linenum))) 571 (setq this-linenum (1+ this-linenum)))
559 (while (<= (point) final-context-start) 572 (while (and (not (eobp)) (<= (point) final-context-start))
560 (insert empty ?:) 573 (insert empty ?:)
561 (forward-line 1) 574 (forward-line 1)
562 (setq this-linenum (1+ this-linenum)))) 575 (setq this-linenum (1+ this-linenum))))
563 576
564 ;; Insert space and colon for lines of context after match. 577 ;; Insert space and colon for lines of context after match.