diff options
| author | Juri Linkov | 2018-11-21 01:43:21 +0200 |
|---|---|---|
| committer | Juri Linkov | 2018-11-21 01:43:21 +0200 |
| commit | 166f6274b4118344612e60fba831e223728f3e89 (patch) | |
| tree | 5912a969d7e022807e2545ead7c23ffc5fdf0ff4 | |
| parent | 11c9343fe63fdc8bfef3246d95f42523d73fb733 (diff) | |
| download | emacs-166f6274b4118344612e60fba831e223728f3e89.tar.gz emacs-166f6274b4118344612e60fba831e223728f3e89.zip | |
Add prefix arg to isearch-repeat-forward/backward (bug#14563, bug#29321)
* lisp/isearch.el (isearch-repeat): Add optional arg COUNT.
Add a while-loop that calls `isearch-search' COUNT times.
(isearch-repeat-forward, isearch-repeat-backward):
Add optional prefix ARG passed down to `isearch-repeat'.
Handle reversed directions.
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | lisp/isearch.el | 81 |
2 files changed, 63 insertions, 23 deletions
| @@ -640,6 +640,11 @@ can now be searched via 'C-s'. | |||
| 640 | 640 | ||
| 641 | ** Search and Replace | 641 | ** Search and Replace |
| 642 | 642 | ||
| 643 | *** Isearch supports a prefix argument for 'C-s' ('isearch-repeat-forward') | ||
| 644 | and 'C-r' ('isearch-repeat-backward'). With a prefix argument, these | ||
| 645 | commands repeat the search for the specified occurrence of the search string. | ||
| 646 | A negative argument repeats the search in the opposite direction. | ||
| 647 | |||
| 643 | *** 'isearch-lazy-count' shows the current match number and total number | 648 | *** 'isearch-lazy-count' shows the current match number and total number |
| 644 | of matches in the Isearch prompt. Customizable variables | 649 | of matches in the Isearch prompt. Customizable variables |
| 645 | 'lazy-count-prefix-format' and 'lazy-count-suffix-format' define the | 650 | 'lazy-count-prefix-format' and 'lazy-count-suffix-format' define the |
diff --git a/lisp/isearch.el b/lisp/isearch.el index 87f4d495f4e..6d94ef66931 100644 --- a/lisp/isearch.el +++ b/lisp/isearch.el | |||
| @@ -1555,8 +1555,8 @@ Use `isearch-exit' to quit without signaling." | |||
| 1555 | (isearch-pop-state)) | 1555 | (isearch-pop-state)) |
| 1556 | (isearch-update))) | 1556 | (isearch-update))) |
| 1557 | 1557 | ||
| 1558 | (defun isearch-repeat (direction) | 1558 | (defun isearch-repeat (direction &optional count) |
| 1559 | ;; Utility for isearch-repeat-forward and -backward. | 1559 | ;; Utility for isearch-repeat-forward and isearch-repeat-backward. |
| 1560 | (if (eq isearch-forward (eq direction 'forward)) | 1560 | (if (eq isearch-forward (eq direction 'forward)) |
| 1561 | ;; C-s in forward or C-r in reverse. | 1561 | ;; C-s in forward or C-r in reverse. |
| 1562 | (if (equal isearch-string "") | 1562 | (if (equal isearch-string "") |
| @@ -1587,32 +1587,67 @@ Use `isearch-exit' to quit without signaling." | |||
| 1587 | 1587 | ||
| 1588 | (if (equal isearch-string "") | 1588 | (if (equal isearch-string "") |
| 1589 | (setq isearch-success t) | 1589 | (setq isearch-success t) |
| 1590 | (if (and isearch-success | 1590 | ;; For the case when count > 1, don't keep intermediate states |
| 1591 | (equal (point) isearch-other-end) | 1591 | ;; added to isearch-cmds by isearch-push-state in this loop. |
| 1592 | (not isearch-just-started)) | 1592 | (let ((isearch-cmds isearch-cmds)) |
| 1593 | ;; If repeating a search that found | 1593 | (while (<= 0 (setq count (1- (or count 1)))) |
| 1594 | ;; an empty string, ensure we advance. | 1594 | (if (and isearch-success |
| 1595 | (if (if isearch-forward (eobp) (bobp)) | 1595 | (equal (point) isearch-other-end) |
| 1596 | ;; If there's nowhere to advance to, fail (and wrap next time). | 1596 | (not isearch-just-started)) |
| 1597 | (progn | 1597 | ;; If repeating a search that found |
| 1598 | (setq isearch-success nil) | 1598 | ;; an empty string, ensure we advance. |
| 1599 | (ding)) | 1599 | (if (if isearch-forward (eobp) (bobp)) |
| 1600 | (forward-char (if isearch-forward 1 -1)) | 1600 | ;; If there's nowhere to advance to, fail (and wrap next time). |
| 1601 | (progn | ||
| 1602 | (setq isearch-success nil) | ||
| 1603 | (ding)) | ||
| 1604 | (forward-char (if isearch-forward 1 -1)) | ||
| 1605 | (isearch-search)) | ||
| 1601 | (isearch-search)) | 1606 | (isearch-search)) |
| 1602 | (isearch-search))) | 1607 | (when (> count 0) |
| 1608 | ;; Update isearch-cmds, so if isearch-search fails later, | ||
| 1609 | ;; it can restore old successful state from isearch-cmds. | ||
| 1610 | (isearch-push-state)) | ||
| 1611 | ;; Stop looping on failure. | ||
| 1612 | (when (or (not isearch-success) isearch-error) | ||
| 1613 | (setq count 0))))) | ||
| 1603 | 1614 | ||
| 1604 | (isearch-push-state) | 1615 | (isearch-push-state) |
| 1605 | (isearch-update)) | 1616 | (isearch-update)) |
| 1606 | 1617 | ||
| 1607 | (defun isearch-repeat-forward () | 1618 | (defun isearch-repeat-forward (&optional arg) |
| 1608 | "Repeat incremental search forwards." | 1619 | "Repeat incremental search forwards. |
| 1609 | (interactive) | 1620 | With a prefix argument, repeat the search ARG times. |
| 1610 | (isearch-repeat 'forward)) | 1621 | A negative argument searches backwards." |
| 1611 | 1622 | (interactive "P") | |
| 1612 | (defun isearch-repeat-backward () | 1623 | (if arg |
| 1613 | "Repeat incremental search backwards." | 1624 | (let ((count (prefix-numeric-value arg))) |
| 1614 | (interactive) | 1625 | (cond ((< count 0) |
| 1615 | (isearch-repeat 'backward)) | 1626 | (isearch-repeat-backward (abs count)) |
| 1627 | ;; Reverse the direction back | ||
| 1628 | (isearch-repeat 'forward)) | ||
| 1629 | (t | ||
| 1630 | ;; Take into account one iteration to reverse direction | ||
| 1631 | (when (not isearch-forward) (setq count (1+ count))) | ||
| 1632 | (isearch-repeat 'forward count)))) | ||
| 1633 | (isearch-repeat 'forward))) | ||
| 1634 | |||
| 1635 | (defun isearch-repeat-backward (&optional arg) | ||
| 1636 | "Repeat incremental search backwards. | ||
| 1637 | With a prefix argument, repeat the search ARG times. | ||
| 1638 | A negative argument searches forwards." | ||
| 1639 | (interactive "P") | ||
| 1640 | (if arg | ||
| 1641 | (let ((count (prefix-numeric-value arg))) | ||
| 1642 | (cond ((< count 0) | ||
| 1643 | (isearch-repeat-forward (abs count)) | ||
| 1644 | ;; Reverse the direction back | ||
| 1645 | (isearch-repeat 'backward)) | ||
| 1646 | (t | ||
| 1647 | ;; Take into account one iteration to reverse direction | ||
| 1648 | (when isearch-forward (setq count (1+ count))) | ||
| 1649 | (isearch-repeat 'backward count)))) | ||
| 1650 | (isearch-repeat 'backward))) | ||
| 1616 | 1651 | ||
| 1617 | 1652 | ||
| 1618 | ;;; Toggles for `isearch-regexp-function' and `search-default-mode'. | 1653 | ;;; Toggles for `isearch-regexp-function' and `search-default-mode'. |