- (list (let* ((default (car regexp-history))
- (input
- (read-from-minibuffer
- (if default
- (format "List lines matching regexp (default `%s'): "
- default)
- "List lines matching regexp: ")
- nil nil nil 'regexp-history default t)))
- (and (equal input "") default
- (setq input default))
- input)
- current-prefix-arg))
- (let* ((nlines (if nlines
- (prefix-numeric-value nlines)
- list-matching-lines-default-context-lines))
- (current-tab-width tab-width)
- (inhibit-read-only t)
- ;; Minimum width of line number plus trailing colon.
- (min-line-number-width 6)
- ;; Width of line number prefix without the colon. Choose a
- ;; width that's a multiple of `tab-width' in the original
- ;; buffer so that lines in *Occur* appear right.
- (line-number-width (1- (* (/ (- (+ min-line-number-width
- tab-width)
- 1)
- tab-width)
- tab-width)))
- ;; Format string for line numbers.
- (line-number-format (format "%%%dd" line-number-width))
- (empty (make-string line-number-width ?\ ))
- (first t)
- ;;flag to prevent printing separator for first match
- (occur-num-matches 0)
- (buffer (current-buffer))
- (dir default-directory)
- (linenum 1)
- (prevpos
- ;;position of most recent match
- (point-min))
- (case-fold-search (and case-fold-search
- (isearch-no-upper-case-p regexp t)))
- (final-context-start
- ;; Marker to the start of context immediately following
- ;; the matched text in *Occur*.
- (make-marker)))
-;;; (save-excursion
-;;; (beginning-of-line)
-;;; (setq linenum (1+ (count-lines (point-min) (point))))
-;;; (setq prevpos (point)))
- (save-excursion
- (goto-char (point-min))
- ;; Check first whether there are any matches at all.
- (if (not (re-search-forward regexp nil t))
- (message "No matches for `%s'" regexp)
- ;; Back up, so the search loop below will find the first match.
- (goto-char (match-beginning 0))
- (with-output-to-temp-buffer "*Occur*"
- (save-excursion
- (set-buffer standard-output)
- (setq default-directory dir)
- ;; We will insert the number of lines, and "lines", later.
- (insert " matching ")
- (let ((print-escape-newlines t))
- (prin1 regexp))
- (insert " in buffer " (buffer-name buffer) ?. ?\n)
- (occur-mode)
- (setq occur-buffer buffer)
- (setq occur-nlines nlines)
- (setq occur-command-arguments
- (list regexp nlines)))
- (if (eq buffer standard-output)
- (goto-char (point-max)))
- (save-excursion
- ;; Find next match, but give up if prev match was at end of buffer.
- (while (and (not (eobp))
- (re-search-forward regexp nil t))
- (goto-char (match-beginning 0))
- (beginning-of-line)
- (save-match-data
- (setq linenum (+ linenum (count-lines prevpos (point)))))
- (setq prevpos (point))
- (goto-char (match-end 0))
- (let* (;;start point of text in source buffer to be put
- ;;into *Occur*
- (start (save-excursion
- (goto-char (match-beginning 0))
- (forward-line (if (< nlines 0)
- nlines
- (- nlines)))
- (point)))
- ;; end point of text in source buffer to be put
- ;; into *Occur*
- (end (save-excursion
- (goto-char (match-end 0))
- (if (> nlines 0)
- (forward-line (1+ nlines))
- (forward-line 1))
- (point)))
- ;; Amount of context before matching text
- (match-beg (- (match-beginning 0) start))
- ;; Length of matching text
- (match-len (- (match-end 0) (match-beginning 0)))
- (tag (format line-number-format linenum))
- tem
- insertion-start
- ;; Number of lines of context to show for current match.
- occur-marker
- ;; Marker pointing to end of match in source buffer.
- (text-beg
- ;; Marker pointing to start of text for one
- ;; match in *Occur*.
- (make-marker))
- (text-end
- ;; Marker pointing to end of text for one match
- ;; in *Occur*.
- (make-marker)))
- (save-excursion
- (setq occur-marker (make-marker))
- (set-marker occur-marker (point))
- (set-buffer standard-output)
- (setq occur-num-matches (1+ occur-num-matches))
- (or first (zerop nlines)
- (insert "--------\n"))
- (setq first nil)
- (save-excursion
- (set-buffer "*Occur*")
- (setq tab-width current-tab-width))
-
- ;; Insert matching text including context lines from
- ;; source buffer into *Occur*
- (set-marker text-beg (point))
- (setq insertion-start (point))
- (insert-buffer-substring buffer start end)
- (or (and (/= (+ start match-beg) end)
- (with-current-buffer buffer
- (eq (char-before end) ?\n)))
- (insert "\n"))
- (set-marker final-context-start
- (+ (- (point) (- end (match-end 0)))
- (if (save-excursion
- (set-buffer buffer)
- (save-excursion
- (goto-char (match-end 0))
- (end-of-line)
- (bolp)))
- 1 0)))
- (set-marker text-end (point))
-
- ;; Highlight text that was matched.
- (if list-matching-lines-face
- (put-text-property
- (+ (marker-position text-beg) match-beg)
- (+ (marker-position text-beg) match-beg match-len)
- 'face list-matching-lines-face))
-
- ;; `occur-point' property is used by occur-next and
- ;; occur-prev to move between matching lines.
- (put-text-property
- (+ (marker-position text-beg) match-beg match-len)
- (+ (marker-position text-beg) match-beg match-len 1)
- 'occur-point t)
-
- ;; Now go back to the start of the matching text
- ;; adding the space and colon to the start of each line.
- (goto-char insertion-start)
- ;; Insert space and colon for lines of context before match.
- (setq tem (if (< linenum nlines)
- (- nlines linenum)
- nlines))
- (while (> tem 0)
- (insert empty ?:)
- (forward-line 1)
- (setq tem (1- tem)))
-
- ;; Insert line number and colon for the lines of
- ;; matching text.
- (let ((this-linenum linenum))
- (while (< (point) final-context-start)
- (if (null tag)
- (setq tag (format line-number-format this-linenum)))
- (insert tag ?:)
- (forward-line 1)
- (setq tag nil)
- (setq this-linenum (1+ this-linenum)))
- (while (and (not (eobp)) (<= (point) final-context-start))
- (insert empty ?:)
- (forward-line 1)
- (setq this-linenum (1+ this-linenum))))
-
- ;; Insert space and colon for lines of context after match.
- (while (and (< (point) (point-max)) (< tem nlines))
- (insert empty ?:)
- (forward-line 1)
- (setq tem (1+ tem)))
-
- ;; Add text properties. The `occur' prop is used to
- ;; store the marker of the matching text in the
- ;; source buffer.
- (add-text-properties
- (marker-position text-beg) (- (marker-position text-end) 1)
- '(mouse-face highlight
- help-echo "mouse-2: go to this occurrence"))
- (put-text-property (marker-position text-beg)
- (marker-position text-end)
- 'occur occur-marker)
- (goto-char (point-max)))
- (forward-line 1)))
- (set-buffer standard-output)
- ;; Go back to top of *Occur* and finish off by printing the
- ;; number of matching lines.
- (goto-char (point-min))
- (let ((message-string
- (if (= occur-num-matches 1)
- "1 line"
- (format "%d lines" occur-num-matches))))
- (insert message-string)
- (if (interactive-p)
- (message "%s matched" message-string)))
- (setq buffer-read-only t)))))))
+ (cons
+ (let* ((bufs (list (read-buffer "First buffer to search: "
+ (current-buffer) t)))
+ (buf nil)
+ (ido-ignore-item-temp-list bufs))
+ (while (not (string-equal
+ (setq buf (read-buffer
+ (if (eq read-buffer-function 'ido-read-buffer)
+ "Next buffer to search (C-j to end): "
+ "Next buffer to search (RET to end): ")
+ nil t))
+ ""))
+ (add-to-list 'bufs buf)
+ (setq ido-ignore-item-temp-list bufs))
+ (nreverse (mapcar #'get-buffer bufs)))
+ (occur-read-primary-args)))
+ (occur-1 regexp nlines bufs))
+
+(defun multi-occur-by-filename-regexp (bufregexp regexp &optional nlines)
+ "Show all lines matching REGEXP in buffers named by BUFREGEXP.
+See also `multi-occur'."
+ (interactive
+ (cons
+ (let* ((default (car regexp-history))
+ (input
+ (read-from-minibuffer
+ "List lines in buffers whose filename matches regexp: "
+ nil
+ nil
+ nil
+ 'regexp-history)))
+ (if (equal input "")
+ default
+ input))
+ (occur-read-primary-args)))
+ (when bufregexp
+ (occur-1 regexp nlines
+ (delq nil
+ (mapcar (lambda (buf)
+ (when (and (buffer-file-name buf)
+ (string-match bufregexp
+ (buffer-file-name buf)))
+ buf))
+ (buffer-list))))))
+
+(defun occur-1 (regexp nlines bufs &optional buf-name)
+ (unless buf-name
+ (setq buf-name "*Occur*"))
+ (let ((occur-buf (get-buffer-create buf-name))
+ (made-temp-buf nil)
+ (active-bufs (delq nil (mapcar #'(lambda (buf)
+ (when (buffer-live-p buf) buf))
+ bufs))))
+ ;; Handle the case where one of the buffers we're searching is the
+ ;; *Occur* buffer itself.
+ (when (memq occur-buf bufs)
+ (setq occur-buf (with-current-buffer occur-buf
+ (clone-buffer "*Occur-temp*"))
+ made-temp-buf t))
+ (with-current-buffer occur-buf
+ (setq buffer-read-only nil)
+ (occur-mode)
+ (erase-buffer)
+ (let ((count (occur-engine
+ regexp active-bufs occur-buf
+ (or nlines list-matching-lines-default-context-lines)
+ (and case-fold-search
+ (isearch-no-upper-case-p regexp t))
+ list-matching-lines-buffer-name-face
+ nil list-matching-lines-face nil)))
+ (let* ((bufcount (length active-bufs))
+ (diff (- (length bufs) bufcount)))
+ (message "Searched %d buffer%s%s; %s match%s for `%s'"
+ bufcount (if (= bufcount 1) "" "s")
+ (if (zerop diff) "" (format " (%d killed)" diff))
+ (if (zerop count) "no" (format "%d" count))
+ (if (= count 1) "" "es")
+ regexp))
+ ;; If we had to make a temporary buffer, make it the *Occur*
+ ;; buffer now.
+ (when made-temp-buf
+ (with-current-buffer (get-buffer buf-name)
+ (kill-buffer (current-buffer)))
+ (rename-buffer buf-name))
+ (setq occur-revert-arguments (list regexp nlines bufs)
+ buffer-read-only t)
+ (if (> count 0)
+ (progn
+ (display-buffer occur-buf)
+ (setq next-error-last-buffer occur-buf))
+ (kill-buffer occur-buf)))
+ (run-hooks 'occur-hook))))
+
+(defun occur-engine-add-prefix (lines)
+ (mapcar
+ #'(lambda (line)
+ (concat " :" line "\n"))
+ lines))
+
+(defun occur-engine (regexp buffers out-buf nlines case-fold-search
+ title-face prefix-face match-face keep-props)
+ (with-current-buffer out-buf
+ (setq buffer-read-only nil)
+ (let ((globalcount 0)
+ (coding nil))
+ ;; Map over all the buffers
+ (dolist (buf buffers)
+ (when (buffer-live-p buf)
+ (let ((matches 0) ;; count of matched lines
+ (lines 1) ;; line count
+ (matchbeg 0)
+ (matchend 0)
+ (origpt nil)
+ (begpt nil)
+ (endpt nil)
+ (marker nil)
+ (curstring "")
+ (headerpt (with-current-buffer out-buf (point))))
+ (save-excursion
+ (set-buffer buf)
+ (or coding
+ ;; Set CODING only if the current buffer locally
+ ;; binds buffer-file-coding-system.
+ (not (local-variable-p 'buffer-file-coding-system))
+ (setq coding buffer-file-coding-system))
+ (save-excursion
+ (goto-char (point-min)) ;; begin searching in the buffer
+ (while (not (eobp))
+ (setq origpt (point))
+ (when (setq endpt (re-search-forward regexp nil t))
+ (setq matches (1+ matches)) ;; increment match count
+ (setq matchbeg (match-beginning 0)
+ matchend (match-end 0))
+ (setq begpt (save-excursion
+ (goto-char matchbeg)
+ (line-beginning-position)))
+ (setq lines (+ lines (1- (count-lines origpt endpt))))
+ (setq marker (make-marker))
+ (set-marker marker matchbeg)
+ (setq curstring (buffer-substring begpt
+ (line-end-position)))
+ ;; Depropertize the string, and maybe
+ ;; highlight the matches
+ (let ((len (length curstring))
+ (start 0))
+ (unless keep-props
+ (set-text-properties 0 len nil curstring))
+ (while (and (< start len)
+ (string-match regexp curstring start))
+ (add-text-properties (match-beginning 0)
+ (match-end 0)
+ (append
+ `(occur-match t)
+ (when match-face
+ `(font-lock-face ,match-face)))
+ curstring)
+ (setq start (match-end 0))))
+ ;; Generate the string to insert for this match
+ (let* ((out-line
+ (concat
+ ;; Using 7 digits aligns tabs properly.
+ (apply #'propertize (format "%7d:" lines)
+ (append
+ (when prefix-face
+ `(font-lock-face prefix-face))
+ '(occur-prefix t)))
+ curstring
+ "\n"))
+ (data
+ (if (= nlines 0)
+ ;; The simple display style
+ out-line
+ ;; The complex multi-line display
+ ;; style. Generate a list of lines,
+ ;; concatenate them all together.
+ (apply #'concat
+ (nconc
+ (occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ nlines)) keep-props))))
+ (list out-line)
+ (occur-engine-add-prefix (cdr (occur-accumulate-lines (1+ nlines) keep-props))))))))
+ ;; Actually insert the match display data
+ (with-current-buffer out-buf
+ (let ((beg (point))
+ (end (progn (insert data) (point))))
+ (unless (= nlines 0)
+ (insert "-------\n"))
+ (add-text-properties
+ beg end
+ `(occur-target ,marker help-echo "mouse-2: go to this occurrence"))
+ ;; We don't put `mouse-face' on the newline,
+ ;; because that loses.
+ (add-text-properties beg (1- end) '(mouse-face highlight)))))
+ (goto-char endpt))
+ (if endpt
+ (progn
+ (setq lines (1+ lines))
+ ;; On to the next match...
+ (forward-line 1))
+ (goto-char (point-max))))))
+ (when (not (zerop matches)) ;; is the count zero?
+ (setq globalcount (+ globalcount matches))
+ (with-current-buffer out-buf
+ (goto-char headerpt)
+ (let ((beg (point))
+ end)
+ (insert (format "%d match%s for \"%s\" in buffer: %s\n"
+ matches (if (= matches 1) "" "es")
+ regexp (buffer-name buf)))
+ (setq end (point))
+ (add-text-properties beg end
+ (append
+ (when title-face
+ `(font-lock-face ,title-face))
+ `(occur-title ,buf))))
+ (goto-char (point-min)))))))
+ (if coding
+ ;; CODING is buffer-file-coding-system of the first buffer
+ ;; that locally binds it. Let's use it also for the output
+ ;; buffer.
+ (set-buffer-file-coding-system coding))
+ ;; Return the number of matches
+ globalcount)))
+