diff options
| author | Tino Calancha | 2017-02-02 19:13:27 +0900 |
|---|---|---|
| committer | Tino Calancha | 2017-02-02 19:13:27 +0900 |
| commit | e280b94dcd6ed42439718ddf9dd704169f6bb536 (patch) | |
| tree | 2d1fc5428a6748c22bc1aed0ddde5309ec06cdc5 | |
| parent | 8e871aef10455eefc34790a9ec011c6fec5e93fe (diff) | |
| download | emacs-e280b94dcd6ed42439718ddf9dd704169f6bb536.tar.gz emacs-e280b94dcd6ed42439718ddf9dd704169f6bb536.zip | |
Show current line highlighted in *Occur* buffer
* lisp/replace.el (list-matching-lines-current-line-face)
(list-matching-lines-jump-to-current-line): New user options.
(occur--orig-line, occur--orig-line-str): New variables.
(occur, occur-engine): Use them.
(occur--final-pos): New variable.
(occur-1): Use it.
(occur-engine): Idem.
Show the current line with 'list-matching-lines-current-line-face'.
Set point on the first matching line after the current one.
* etc/NEWS: Add entry for the new option.
| -rw-r--r-- | doc/emacs/search.texi | 4 | ||||
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | lisp/replace.el | 72 |
3 files changed, 76 insertions, 5 deletions
diff --git a/doc/emacs/search.texi b/doc/emacs/search.texi index 2a67619678b..a6cb1a4c9f5 100644 --- a/doc/emacs/search.texi +++ b/doc/emacs/search.texi | |||
| @@ -1726,6 +1726,10 @@ face. A numeric argument @var{n} specifies that @var{n} lines of | |||
| 1726 | context are to be displayed before and after each matching line. | 1726 | context are to be displayed before and after each matching line. |
| 1727 | The default number of context lines is specified by the variable | 1727 | The default number of context lines is specified by the variable |
| 1728 | @code{list-matching-lines-default-context-lines}. | 1728 | @code{list-matching-lines-default-context-lines}. |
| 1729 | When @code{list-matching-lines-jump-to-current-line} is non-nil, | ||
| 1730 | the current line is shown highlighted with face | ||
| 1731 | @code{list-matching-lines-current-line-face} and the point is set | ||
| 1732 | at the first match after such line. | ||
| 1729 | 1733 | ||
| 1730 | You can also run @kbd{M-s o} when an incremental search is active; | 1734 | You can also run @kbd{M-s o} when an incremental search is active; |
| 1731 | this uses the current search string. | 1735 | this uses the current search string. |
| @@ -311,6 +311,11 @@ substituted by a home directory by writing it as "/foo:/:/~/file". | |||
| 311 | * Editing Changes in Emacs 26.1 | 311 | * Editing Changes in Emacs 26.1 |
| 312 | 312 | ||
| 313 | +++ | 313 | +++ |
| 314 | ** Two new user options 'list-matching-lines-jump-to-current-line' and | ||
| 315 | 'list-matching-lines-current-line-face' to show highlighted the current | ||
| 316 | line in *Occur* buffer. | ||
| 317 | |||
| 318 | +++ | ||
| 314 | ** The 'occur' command can now operate on the region. | 319 | ** The 'occur' command can now operate on the region. |
| 315 | 320 | ||
| 316 | +++ | 321 | +++ |
diff --git a/lisp/replace.el b/lisp/replace.el index 0a8e4804858..a825040a979 100644 --- a/lisp/replace.el +++ b/lisp/replace.el | |||
| @@ -1304,6 +1304,19 @@ If the value is nil, don't highlight the buffer names specially." | |||
| 1304 | :type 'face | 1304 | :type 'face |
| 1305 | :group 'matching) | 1305 | :group 'matching) |
| 1306 | 1306 | ||
| 1307 | (defcustom list-matching-lines-current-line-face 'lazy-highlight | ||
| 1308 | "Face used by \\[list-matching-lines] to highlight the current line." | ||
| 1309 | :type 'face | ||
| 1310 | :group 'matching | ||
| 1311 | :version "26.1") | ||
| 1312 | |||
| 1313 | (defcustom list-matching-lines-jump-to-current-line nil | ||
| 1314 | "If non-nil, \\[list-matching-lines] shows the current line highlighted. | ||
| 1315 | Set the point right after such line when there are matches after it." | ||
| 1316 | :type 'boolean | ||
| 1317 | :group 'matching | ||
| 1318 | :version "26.1") | ||
| 1319 | |||
| 1307 | (defcustom list-matching-lines-prefix-face 'shadow | 1320 | (defcustom list-matching-lines-prefix-face 'shadow |
| 1308 | "Face used by \\[list-matching-lines] to show the prefix column. | 1321 | "Face used by \\[list-matching-lines] to show the prefix column. |
| 1309 | If the face doesn't differ from the default face, | 1322 | If the face doesn't differ from the default face, |
| @@ -1364,6 +1377,9 @@ invoke `occur'." | |||
| 1364 | (defvar occur--region-start nil) | 1377 | (defvar occur--region-start nil) |
| 1365 | (defvar occur--region-end nil) | 1378 | (defvar occur--region-end nil) |
| 1366 | (defvar occur--matches-threshold nil) | 1379 | (defvar occur--matches-threshold nil) |
| 1380 | (defvar occur--orig-line nil) | ||
| 1381 | (defvar occur--orig-line-str nil) | ||
| 1382 | (defvar occur--final-pos nil) | ||
| 1367 | 1383 | ||
| 1368 | (defun occur (regexp &optional nlines region) | 1384 | (defun occur (regexp &optional nlines region) |
| 1369 | "Show all lines in the current buffer containing a match for REGEXP. | 1385 | "Show all lines in the current buffer containing a match for REGEXP. |
| @@ -1382,6 +1398,9 @@ REGION must be a list of (START . END) positions as returned by | |||
| 1382 | The lines are shown in a buffer named `*Occur*'. | 1398 | The lines are shown in a buffer named `*Occur*'. |
| 1383 | It serves as a menu to find any of the occurrences in this buffer. | 1399 | It serves as a menu to find any of the occurrences in this buffer. |
| 1384 | \\<occur-mode-map>\\[describe-mode] in that buffer will explain how. | 1400 | \\<occur-mode-map>\\[describe-mode] in that buffer will explain how. |
| 1401 | If `list-matching-lines-jump-to-current-line' is non-nil, then show | ||
| 1402 | the current line highlighted with `list-matching-lines-current-line-face' | ||
| 1403 | and set point at the first match after such line. | ||
| 1385 | 1404 | ||
| 1386 | If REGEXP contains upper case characters (excluding those preceded by `\\') | 1405 | If REGEXP contains upper case characters (excluding those preceded by `\\') |
| 1387 | and `search-upper-case' is non-nil, the matching is case-sensitive. | 1406 | and `search-upper-case' is non-nil, the matching is case-sensitive. |
| @@ -1409,7 +1428,13 @@ is not modified." | |||
| 1409 | (occur--region-end end) | 1428 | (occur--region-end end) |
| 1410 | (occur--matches-threshold | 1429 | (occur--matches-threshold |
| 1411 | (and in-region-p | 1430 | (and in-region-p |
| 1412 | (line-number-at-pos (min start end))))) | 1431 | (line-number-at-pos (min start end)))) |
| 1432 | (occur--orig-line | ||
| 1433 | (line-number-at-pos (point))) | ||
| 1434 | (occur--orig-line-str | ||
| 1435 | (buffer-substring-no-properties | ||
| 1436 | (line-beginning-position) | ||
| 1437 | (line-end-position)))) | ||
| 1413 | (save-excursion ; If no matches `occur-1' doesn't restore the point. | 1438 | (save-excursion ; If no matches `occur-1' doesn't restore the point. |
| 1414 | (and in-region-p (narrow-to-region start end)) | 1439 | (and in-region-p (narrow-to-region start end)) |
| 1415 | (occur-1 regexp nlines (list (current-buffer))) | 1440 | (occur-1 regexp nlines (list (current-buffer))) |
| @@ -1508,7 +1533,8 @@ See also `multi-occur'." | |||
| 1508 | (occur-mode)) | 1533 | (occur-mode)) |
| 1509 | (let ((inhibit-read-only t) | 1534 | (let ((inhibit-read-only t) |
| 1510 | ;; Don't generate undo entries for creation of the initial contents. | 1535 | ;; Don't generate undo entries for creation of the initial contents. |
| 1511 | (buffer-undo-list t)) | 1536 | (buffer-undo-list t) |
| 1537 | (occur--final-pos nil)) | ||
| 1512 | (erase-buffer) | 1538 | (erase-buffer) |
| 1513 | (let ((count | 1539 | (let ((count |
| 1514 | (if (stringp nlines) | 1540 | (if (stringp nlines) |
| @@ -1560,6 +1586,10 @@ See also `multi-occur'." | |||
| 1560 | (if (= count 0) | 1586 | (if (= count 0) |
| 1561 | (kill-buffer occur-buf) | 1587 | (kill-buffer occur-buf) |
| 1562 | (display-buffer occur-buf) | 1588 | (display-buffer occur-buf) |
| 1589 | (when occur--final-pos | ||
| 1590 | (set-window-point | ||
| 1591 | (get-buffer-window occur-buf 'all-frames) | ||
| 1592 | occur--final-pos)) | ||
| 1563 | (setq next-error-last-buffer occur-buf) | 1593 | (setq next-error-last-buffer occur-buf) |
| 1564 | (setq buffer-read-only t) | 1594 | (setq buffer-read-only t) |
| 1565 | (set-buffer-modified-p nil) | 1595 | (set-buffer-modified-p nil) |
| @@ -1572,7 +1602,8 @@ See also `multi-occur'." | |||
| 1572 | (global-matches 0) ;; total count of matches | 1602 | (global-matches 0) ;; total count of matches |
| 1573 | (coding nil) | 1603 | (coding nil) |
| 1574 | (case-fold-search case-fold) | 1604 | (case-fold-search case-fold) |
| 1575 | (in-region-p (and occur--region-start occur--region-end))) | 1605 | (in-region-p (and occur--region-start occur--region-end)) |
| 1606 | (multi-occur-p (cdr buffers))) | ||
| 1576 | ;; Map over all the buffers | 1607 | ;; Map over all the buffers |
| 1577 | (dolist (buf buffers) | 1608 | (dolist (buf buffers) |
| 1578 | (when (buffer-live-p buf) | 1609 | (when (buffer-live-p buf) |
| @@ -1580,12 +1611,16 @@ See also `multi-occur'." | |||
| 1580 | (matches 0) ;; count of matches | 1611 | (matches 0) ;; count of matches |
| 1581 | (curr-line ;; line count | 1612 | (curr-line ;; line count |
| 1582 | (or occur--matches-threshold 1)) | 1613 | (or occur--matches-threshold 1)) |
| 1614 | (orig-line occur--orig-line) | ||
| 1615 | (orig-line-str occur--orig-line-str) | ||
| 1616 | (orig-line-shown-p) | ||
| 1583 | (prev-line nil) ;; line number of prev match endpt | 1617 | (prev-line nil) ;; line number of prev match endpt |
| 1584 | (prev-after-lines nil) ;; context lines of prev match | 1618 | (prev-after-lines nil) ;; context lines of prev match |
| 1585 | (matchbeg 0) | 1619 | (matchbeg 0) |
| 1586 | (origpt nil) | 1620 | (origpt nil) |
| 1587 | (begpt nil) | 1621 | (begpt nil) |
| 1588 | (endpt nil) | 1622 | (endpt nil) |
| 1623 | (finalpt nil) | ||
| 1589 | (marker nil) | 1624 | (marker nil) |
| 1590 | (curstring "") | 1625 | (curstring "") |
| 1591 | (ret nil) | 1626 | (ret nil) |
| @@ -1686,6 +1721,18 @@ See also `multi-occur'." | |||
| 1686 | (nth 0 ret)))) | 1721 | (nth 0 ret)))) |
| 1687 | ;; Actually insert the match display data | 1722 | ;; Actually insert the match display data |
| 1688 | (with-current-buffer out-buf | 1723 | (with-current-buffer out-buf |
| 1724 | (when (and list-matching-lines-jump-to-current-line | ||
| 1725 | (not multi-occur-p) | ||
| 1726 | (not orig-line-shown-p) | ||
| 1727 | (>= curr-line orig-line)) | ||
| 1728 | (insert | ||
| 1729 | (concat | ||
| 1730 | (propertize | ||
| 1731 | (format "%7d:%s" orig-line orig-line-str) | ||
| 1732 | 'face list-matching-lines-current-line-face | ||
| 1733 | 'mouse-face 'mode-line-highlight | ||
| 1734 | 'help-echo "Current line") "\n")) | ||
| 1735 | (setq orig-line-shown-p t finalpt (point))) | ||
| 1689 | (insert data))) | 1736 | (insert data))) |
| 1690 | (goto-char endpt)) | 1737 | (goto-char endpt)) |
| 1691 | (if endpt | 1738 | (if endpt |
| @@ -1699,6 +1746,18 @@ See also `multi-occur'." | |||
| 1699 | (forward-line 1)) | 1746 | (forward-line 1)) |
| 1700 | (goto-char (point-max))) | 1747 | (goto-char (point-max))) |
| 1701 | (setq prev-line (1- curr-line))) | 1748 | (setq prev-line (1- curr-line))) |
| 1749 | ;; Insert original line if haven't done yet. | ||
| 1750 | (when (and list-matching-lines-jump-to-current-line | ||
| 1751 | (not multi-occur-p) | ||
| 1752 | (not orig-line-shown-p)) | ||
| 1753 | (with-current-buffer out-buf | ||
| 1754 | (insert | ||
| 1755 | (concat | ||
| 1756 | (propertize | ||
| 1757 | (format "%7d:%s" orig-line orig-line-str) | ||
| 1758 | 'face list-matching-lines-current-line-face | ||
| 1759 | 'mouse-face 'mode-line-highlight | ||
| 1760 | 'help-echo "Current line") "\n")))) | ||
| 1702 | ;; Flush remaining context after-lines. | 1761 | ;; Flush remaining context after-lines. |
| 1703 | (when prev-after-lines | 1762 | (when prev-after-lines |
| 1704 | (with-current-buffer out-buf | 1763 | (with-current-buffer out-buf |
| @@ -1732,8 +1791,11 @@ See also `multi-occur'." | |||
| 1732 | (setq end (point)) | 1791 | (setq end (point)) |
| 1733 | (add-text-properties beg end `(occur-title ,buf)) | 1792 | (add-text-properties beg end `(occur-title ,buf)) |
| 1734 | (when title-face | 1793 | (when title-face |
| 1735 | (add-face-text-property beg end title-face))) | 1794 | (add-face-text-property beg end title-face)) |
| 1736 | (goto-char (point-min))))))) | 1795 | (goto-char (if finalpt |
| 1796 | (setq occur--final-pos | ||
| 1797 | (cl-incf finalpt (- end beg))) | ||
| 1798 | (point-min))))))))) | ||
| 1737 | ;; Display total match count and regexp for multi-buffer. | 1799 | ;; Display total match count and regexp for multi-buffer. |
| 1738 | (when (and (not (zerop global-lines)) (> (length buffers) 1)) | 1800 | (when (and (not (zerop global-lines)) (> (length buffers) 1)) |
| 1739 | (goto-char (point-min)) | 1801 | (goto-char (point-min)) |