aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDima Kogan2016-12-25 11:35:26 -0800
committerDima Kogan2018-06-17 22:58:46 -0700
commit74f377b3955198d6f66afa34bbbf6d004aad134a (patch)
tree20b81a85d4f8d25113618157795c6b600ca08e54
parent2d1b774dbc31b753527321ae1e441d5e424a5265 (diff)
downloademacs-74f377b3955198d6f66afa34bbbf6d004aad134a.tar.gz
emacs-74f377b3955198d6f66afa34bbbf6d004aad134a.zip
comint-insert-previous-argument counts args from start or from end
This function is invoked in shell-mode by the user, and is meant to emulate what M-. does in zsh and bash: it inserts an argument from a previous command. Without a prefix argument, it inserts the last arg from the previous command; with an argument INDEX, it inserts the INDEX-th argument. bash counts from the start, while zsh counts from the end. This patch adds a variable `comint-insert-previous-argument-from-end' that emulates the zsh behavior if non-nil. * lisp/comint.el (comint-arguments): can take in negative arguments to count from the end, same as indexing in python. (comint-insert-previous-argument): if comint-insert-previous-argument-from-end is non-nil, INDEX counts arguments from the end; if nil, from the beginning (Bug#25271) * etc/NEWS: Document this.
-rw-r--r--etc/NEWS10
-rw-r--r--lisp/comint.el44
2 files changed, 41 insertions, 13 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 5a3a27ee4af..8bf1da470fc 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -190,6 +190,16 @@ navigation and editing of large files.
190It now treats the optional 2nd argument to mean that the URL should be 190It now treats the optional 2nd argument to mean that the URL should be
191shown in the currently selected window. 191shown in the currently selected window.
192 192
193** Comint
194*** 'comint-insert-previous-argument' knows how to count args
195from the beginning or from the end. This is useful because
196'comint-insert-previous-argument' exists to emulate M-. in bash and zsh; and
197bash counts from the start while zsh counts from the end.
198
199*** New variable 'comint-insert-previous-argument-from-end' controls whether
200args passed to 'comint-insert-previous-argument' count from the beginning or
201from the end
202
193** Flymake 203** Flymake
194 204
195+++ 205+++
diff --git a/lisp/comint.el b/lisp/comint.el
index f334a4ca4d4..f66e40b150b 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -1682,12 +1682,13 @@ characters), and are not considered to be delimiters."
1682 1682
1683(defun comint-arguments (string nth mth) 1683(defun comint-arguments (string nth mth)
1684 "Return from STRING the NTH to MTH arguments. 1684 "Return from STRING the NTH to MTH arguments.
1685NTH and/or MTH can be nil, which means the last argument. 1685NTH and/or MTH can be nil, which means the last argument. NTH
1686Returned arguments are separated by single spaces. 1686and MTH can be <0 to count from the end; -1 means last argument.
1687We assume whitespace separates arguments, except within quotes 1687Returned arguments are separated by single spaces. We assume
1688and except for a space or tab that immediately follows a backslash. 1688whitespace separates arguments, except within quotes and except
1689Also, a run of one or more of a single character 1689for a space or tab that immediately follows a backslash. Also, a
1690in `comint-delimiter-argument-list' is a separate argument. 1690run of one or more of a single character in
1691`comint-delimiter-argument-list' is a separate argument.
1691Argument 0 is the command name." 1692Argument 0 is the command name."
1692 ;; The first line handles ordinary characters and backslash-sequences 1693 ;; The first line handles ordinary characters and backslash-sequences
1693 ;; (except with w32 msdos-like shells, where backslashes are valid). 1694 ;; (except with w32 msdos-like shells, where backslashes are valid).
@@ -1709,7 +1710,7 @@ Argument 0 is the command name."
1709 (count 0) 1710 (count 0)
1710 beg str quotes) 1711 beg str quotes)
1711 ;; Build a list of all the args until we have as many as we want. 1712 ;; Build a list of all the args until we have as many as we want.
1712 (while (and (or (null mth) (<= count mth)) 1713 (while (and (or (null mth) (< mth 0) (<= count mth))
1713 (string-match argpart string pos)) 1714 (string-match argpart string pos))
1714 ;; Apply the `literal' text property to backslash-escaped 1715 ;; Apply the `literal' text property to backslash-escaped
1715 ;; characters, so that `comint-delim-arg' won't break them up. 1716 ;; characters, so that `comint-delim-arg' won't break them up.
@@ -1736,8 +1737,14 @@ Argument 0 is the command name."
1736 args (if quotes (cons str args) 1737 args (if quotes (cons str args)
1737 (nconc (comint-delim-arg str) args)))) 1738 (nconc (comint-delim-arg str) args))))
1738 (setq count (length args)) 1739 (setq count (length args))
1739 (let ((n (or nth (1- count))) 1740 (let ((n (cond
1740 (m (if mth (1- (- count mth)) 0))) 1741 ((null nth) (1- count))
1742 ((>= nth 0) nth)
1743 (t (+ count nth))))
1744 (m (cond
1745 ((null mth) 0)
1746 ((>= mth 0) (1- (- count mth)))
1747 (t (1- (- mth))))))
1741 (mapconcat 1748 (mapconcat
1742 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " ")))) 1749 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
1743 1750
@@ -2652,8 +2659,16 @@ text matching `comint-prompt-regexp'."
2652(defvar-local comint-insert-previous-argument-last-start-pos nil) 2659(defvar-local comint-insert-previous-argument-last-start-pos nil)
2653(defvar-local comint-insert-previous-argument-last-index nil) 2660(defvar-local comint-insert-previous-argument-last-index nil)
2654 2661
2655;; Needs fixing: 2662(defcustom comint-insert-previous-argument-from-end nil
2656;; make comint-arguments understand negative indices as bash does 2663 "If nil, the INDEX argument to
2664`comint-insert-previous-argument' refers to the INDEX-th
2665argument, counting from the beginning; if non-nil, counting from
2666the end. This exists to emulate the bahavior of `M-number M-.'
2667in bash and zsh: in bash, `number' counts from the
2668beginning (variable in nil), while in zsh it counts from the end."
2669 :type 'boolean
2670 :group 'comint)
2671
2657(defun comint-insert-previous-argument (index) 2672(defun comint-insert-previous-argument (index)
2658 "Insert the INDEXth argument from the previous Comint command-line at point. 2673 "Insert the INDEXth argument from the previous Comint command-line at point.
2659Spaces are added at beginning and/or end of the inserted string if 2674Spaces are added at beginning and/or end of the inserted string if
@@ -2661,8 +2676,8 @@ necessary to ensure that it's separated from adjacent arguments.
2661Interactively, if no prefix argument is given, the last argument is inserted. 2676Interactively, if no prefix argument is given, the last argument is inserted.
2662Repeated interactive invocations will cycle through the same argument 2677Repeated interactive invocations will cycle through the same argument
2663from progressively earlier commands (using the value of INDEX specified 2678from progressively earlier commands (using the value of INDEX specified
2664with the first command). 2679with the first command). Values of INDEX<0 count from the end, so INDEX=-1
2665This command is like `M-.' in bash." 2680is the last argument. This command is like `M-.' in bash and zsh."
2666 (interactive "P") 2681 (interactive "P")
2667 (unless (null index) 2682 (unless (null index)
2668 (setq index (prefix-numeric-value index))) 2683 (setq index (prefix-numeric-value index)))
@@ -2672,6 +2687,9 @@ This command is like `M-.' in bash."
2672 (setq index comint-insert-previous-argument-last-index)) 2687 (setq index comint-insert-previous-argument-last-index))
2673 (t 2688 (t
2674 ;; This is a non-repeat invocation, so initialize state. 2689 ;; This is a non-repeat invocation, so initialize state.
2690 (when (and index
2691 comint-insert-previous-argument-from-end)
2692 (setq index (- index)))
2675 (setq comint-input-ring-index nil) 2693 (setq comint-input-ring-index nil)
2676 (setq comint-insert-previous-argument-last-index index) 2694 (setq comint-insert-previous-argument-last-index index)
2677 (when (null comint-insert-previous-argument-last-start-pos) 2695 (when (null comint-insert-previous-argument-last-start-pos)