unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements
@ 2016-12-25 19:54 Dima Kogan
  2018-05-02  1:36 ` Noam Postavsky
  2018-06-18  6:05 ` bug#25271: Committed Dima Kogan
  0 siblings, 2 replies; 6+ messages in thread
From: Dima Kogan @ 2016-12-25 19:54 UTC (permalink / raw)
  To: 25271

[-- Attachment #1: Type: text/plain, Size: 656 bytes --]

These patches come from

  http://lists.gnu.org/archive/html/emacs-devel/2016-11/msg00520.html

There are two patches to fix some deficiencies in
comint-insert-previous-argument:

1. a prefix argument to comint-insert-previous-argument inserts the
INDEX-th argument, not just the last one. bash counts from the beginning
of the argument list, but zsh counts from the end. A new variable is
added to allow the user to choose the zsh behavior

2. comint-insert-previous-argument had logic to find trailing &, which
was buggy and got confused if && was encountered. This logic is not
present in bash or zsh, so it was removed, and the bug went away with
it.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-comint-insert-previous-argument-counts-args-from-the.patch --]
[-- Type: text/x-diff, Size: 5508 bytes --]

From 2d0c476fe02494837c6d0e035807e93dfd54677b Mon Sep 17 00:00:00 2001
From: Dima Kogan <dima@secretsauce.net>
Date: Sun, 25 Dec 2016 11:35:26 -0800
Subject: [PATCH 1/2] comint-insert-previous-argument counts args from the
 start or from the 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 counds 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
---
 lisp/comint.el | 49 ++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 13 deletions(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index b9c65b0..171caef 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -1660,12 +1660,13 @@ comint-delim-arg
 
 (defun comint-arguments (string nth mth)
   "Return from STRING the NTH to MTH arguments.
-NTH and/or MTH can be nil, which means the last argument.
-Returned arguments are separated by single spaces.
-We assume whitespace separates arguments, except within quotes
-and except for a space or tab that immediately follows a backslash.
-Also, a run of one or more of a single character
-in `comint-delimiter-argument-list' is a separate argument.
+NTH and/or MTH can be nil, which means the last argument.  NTH
+and MTH can be <0 to count from the end; -1 means last argument.
+Returned arguments are separated by single spaces.  We assume
+whitespace separates arguments, except within quotes and except
+for a space or tab that immediately follows a backslash.  Also, a
+run of one or more of a single character in
+`comint-delimiter-argument-list' is a separate argument.
 Argument 0 is the command name."
   ;; The first line handles ordinary characters and backslash-sequences
   ;; (except with w32 msdos-like shells, where backslashes are valid).
@@ -1687,7 +1688,7 @@ comint-arguments
 	 (count 0)
 	 beg str quotes)
     ;; Build a list of all the args until we have as many as we want.
-    (while (and (or (null mth) (<= count mth))
+    (while (and (or (null mth) (< mth 0) (<= count mth))
 		(string-match argpart string pos))
       ;; Apply the `literal' text property to backslash-escaped
       ;; characters, so that `comint-delim-arg' won't break them up.
@@ -1714,8 +1715,14 @@ comint-arguments
 	      args (if quotes (cons str args)
 		     (nconc (comint-delim-arg str) args))))
     (setq count (length args))
-    (let ((n (or nth (1- count)))
-	  (m (if mth (1- (- count mth)) 0)))
+    (let ((n (cond
+              ((null nth) (1- count))
+              ((>= nth 0) nth)
+              (t          (+ count nth))))
+          (m (cond
+              ((null mth) 0)
+              ((>= mth 0) (1- (- count mth)))
+              (t          (1- (- mth))))))
       (mapconcat
        (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
 \f
@@ -2634,8 +2641,21 @@ comint-previous-prompt
 (defvar-local comint-insert-previous-argument-last-start-pos nil)
 (defvar-local comint-insert-previous-argument-last-index nil)
 
-;; Needs fixing:
-;;  make comint-arguments understand negative indices as bash does
+(defvar-local comint-insert-previous-argument-from-end nil
+  "If nil, the INDEX argument to
+`comint-insert-previous-argument' refers to the INDEX-th
+argument, counting from the beginning; if non-nil, counting from
+the end.  This exists to emulate the bahavior of `M-number M-.'
+in bash and zsh: in bash, `number' counts from the
+beginning (variable in nil), while in zsh it counts from the end.
+This variable is buffer-local.  To get the zsh behavior in `M-x
+shell' sessions, do something like this:
+
+    (add-hook
+      'shell-mode-hook
+        (lambda ()
+          (setq comint-insert-previous-argument-from-end t)))")
+
 (defun comint-insert-previous-argument (index)
   "Insert the INDEXth argument from the previous Comint command-line at point.
 Spaces are added at beginning and/or end of the inserted string if
@@ -2643,8 +2663,8 @@ comint-insert-previous-argument
 Interactively, if no prefix argument is given, the last argument is inserted.
 Repeated interactive invocations will cycle through the same argument
 from progressively earlier commands (using the value of INDEX specified
-with the first command).
-This command is like `M-.' in bash."
+with the first command).  Values of INDEX<0 count from the end, so INDEX=-1
+is the last argument.  This command is like `M-.' in bash."
   (interactive "P")
   (unless (null index)
     (setq index (prefix-numeric-value index)))
@@ -2654,6 +2674,9 @@ comint-insert-previous-argument
 	 (setq index comint-insert-previous-argument-last-index))
 	(t
 	 ;; This is a non-repeat invocation, so initialize state.
+         (when (and index
+                    comint-insert-previous-argument-from-end)
+           (setq index (- index)))
 	 (setq comint-input-ring-index nil)
 	 (setq comint-insert-previous-argument-last-index index)
 	 (when (null comint-insert-previous-argument-last-start-pos)
-- 
2.10.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-comint-insert-previous-argument-doesn-t-detect-and-i.patch --]
[-- Type: text/x-diff, Size: 1559 bytes --]

From 45de3e8dfa8a267c7af7732ef5fc6cb2c1606a66 Mon Sep 17 00:00:00 2001
From: Dima Kogan <dima@secretsauce.net>
Date: Sun, 25 Dec 2016 11:49:44 -0800
Subject: [PATCH 2/2] comint-insert-previous-argument doesn't detect and ignore
 trailing &

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.
Neither zsh nor bash treat a trailing & specially: M-. simply inserts it if it
is encountered.  Emacs DID have extra logic to detect and discard trailing &,
but this logic was buggy, and a && anywhere in the sequence would confuse it.
This patch simply removes that logic to fix the bug and to emulate zsh and bash
more closely

* lisp/comint.el (comint-insert-previous-argument): don't detect and ignore
  trailing &
---
 lisp/comint.el | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index 171caef..83dbce0 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -2692,9 +2692,6 @@ comint-insert-previous-argument
   (set-marker comint-insert-previous-argument-last-start-pos (point))
   ;; Insert the argument.
   (let ((input-string (comint-previous-input-string 0)))
-    (when (string-match "[ \t\n]*&" input-string)
-      ;; strip terminating '&'
-      (setq input-string (substring input-string 0 (match-beginning 0))))
     (insert (comint-arguments input-string index index)))
   ;; Make next invocation return arg from previous input
   (setq comint-input-ring-index (1+ (or comint-input-ring-index 0)))
-- 
2.10.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements
  2016-12-25 19:54 bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements Dima Kogan
@ 2018-05-02  1:36 ` Noam Postavsky
  2018-06-18  2:25   ` Dima Kogan
  2018-06-18  6:05 ` bug#25271: Committed Dima Kogan
  1 sibling, 1 reply; 6+ messages in thread
From: Noam Postavsky @ 2018-05-02  1:36 UTC (permalink / raw)
  To: Dima Kogan; +Cc: 25271

Dima Kogan <dima@secretsauce.net> writes:

> +(defvar-local comint-insert-previous-argument-from-end nil

> +This variable is buffer-local.  To get the zsh behavior in `M-x
> +shell' sessions, do something like this:
> +
> +    (add-hook
> +      'shell-mode-hook
> +        (lambda ()
> +          (setq comint-insert-previous-argument-from-end t)))")
> +

Seems simpler to just make it a defcustom.  The user could still
setq-local if they really want per-buffer settings.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements
  2018-05-02  1:36 ` Noam Postavsky
@ 2018-06-18  2:25   ` Dima Kogan
  2018-06-18  2:30     ` Noam Postavsky
  0 siblings, 1 reply; 6+ messages in thread
From: Dima Kogan @ 2018-06-18  2:25 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: 25271

Noam Postavsky <npostavs@gmail.com> writes:

> Dima Kogan <dima@secretsauce.net> writes:
>
>> +(defvar-local comint-insert-previous-argument-from-end nil
>
>> +This variable is buffer-local.  To get the zsh behavior in `M-x
>> +shell' sessions, do something like this:
>> +
>> +    (add-hook
>> +      'shell-mode-hook
>> +        (lambda ()
>> +          (setq comint-insert-previous-argument-from-end t)))")
>> +
>
> Seems simpler to just make it a defcustom.  The user could still
> setq-local if they really want per-buffer settings.

Yes. I agree, actually. Want a new patch, and we can merge this?





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements
  2018-06-18  2:25   ` Dima Kogan
@ 2018-06-18  2:30     ` Noam Postavsky
  0 siblings, 0 replies; 6+ messages in thread
From: Noam Postavsky @ 2018-06-18  2:30 UTC (permalink / raw)
  To: Dima Kogan; +Cc: 25271

Dima Kogan <dima@secretsauce.net> writes:

>> Seems simpler to just make it a defcustom.  The user could still
>> setq-local if they really want per-buffer settings.
>
> Yes. I agree, actually. Want a new patch, and we can merge this?

Yeah, please add a NEWS entry for the new option and it should be good
to go for master.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#25271: Committed
  2016-12-25 19:54 bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements Dima Kogan
  2018-05-02  1:36 ` Noam Postavsky
@ 2018-06-18  6:05 ` Dima Kogan
  2018-06-18 16:29   ` Eli Zaretskii
  1 sibling, 1 reply; 6+ messages in thread
From: Dima Kogan @ 2018-06-18  6:05 UTC (permalink / raw)
  To: 25271-done

Patches committed in ba2ddadb5378351e8003c8e172b52bfabaa27554





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#25271: Committed
  2018-06-18  6:05 ` bug#25271: Committed Dima Kogan
@ 2018-06-18 16:29   ` Eli Zaretskii
  0 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2018-06-18 16:29 UTC (permalink / raw)
  To: Dima Kogan; +Cc: 25271

> From: Dima Kogan <dima@secretsauce.net>
> Date: Sun, 17 Jun 2018 23:05:36 -0700
> 
> Patches committed in ba2ddadb5378351e8003c8e172b52bfabaa27554

Thank you for working on this.

In the future, please observe the following minor gotchas (I fixed
them for this changeset):

  . The commit log entries should be full sentences: start with a
    capital letter and end with a period.

  . A new or modified defcustom should have a suitable :version tag
    that records the version where it was introduced or its value
    changed.

  . Please update the manual(s) to reflect the changes, otherwise this
    job is left for the pretest phase, where it delays the release,
    and also increases the risk of having inaccurate documentation
    (due to the time that passed and people who forget).

  . NEWS entries should preferably have their first line a complete
    sentence that summarizes the entry.  This is so NEWS could be
    usefully read in Outline mode after folding the body of the
    entries.





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-06-18 16:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-25 19:54 bug#25271: 26.0.50; [PATCH] comint-insert-previous-argument logic improvements Dima Kogan
2018-05-02  1:36 ` Noam Postavsky
2018-06-18  2:25   ` Dima Kogan
2018-06-18  2:30     ` Noam Postavsky
2018-06-18  6:05 ` bug#25271: Committed Dima Kogan
2018-06-18 16:29   ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).