]> git.eshelyaron.com Git - emacs.git/commitdiff
Make eshell history expansion more like bash (Bug#29821)
authorJay Kamat <jaygkamat@gmail.com>
Fri, 22 Dec 2017 23:34:44 +0000 (15:34 -0800)
committerNoam Postavsky <npostavs@gmail.com>
Fri, 5 Jan 2018 14:29:00 +0000 (09:29 -0500)
- Prevent expansion of quick substitutions when the initial "^" is not
  at start of line (Bug#29157).
- Allow spaces inside substitutions, so "^foo bar^baz" works.
- Allow trailing characters after substitution, so "^foo^bar^trailing"
  works.
- Throw an error when substitution does not match.

* lisp/eshell/em-hist.el (eshell-expand-history-references): Expand
history substitution before other types of expansions, and expand them
with the whole line.
(eshell-history-substitution): New function to expand only
substitutions, taking in the entire typed line rather than individual
arguments.

lisp/eshell/em-hist.el
lisp/eshell/em-pred.el

index 83595847bdfcd8ad9c2a94b745cdfb04abd994c2..62e2f57d0fd5374aa83a96b74c0344ef4fdda39c 100644 (file)
@@ -581,21 +581,30 @@ See also `eshell-read-history'."
 
 (defun eshell-expand-history-references (beg end)
   "Parse and expand any history references in current input."
-  (let ((result (eshell-hist-parse-arguments beg end)))
+  (let ((result (eshell-hist-parse-arguments beg end))
+       (full-line (buffer-substring-no-properties beg end)))
     (when result
       (let ((textargs (nreverse (nth 0 result)))
            (posb (nreverse (nth 1 result)))
-           (pose (nreverse (nth 2 result))))
+           (pose (nreverse (nth 2 result)))
+           (full-line-subst (eshell-history-substitution full-line)))
        (save-excursion
-         (while textargs
-           (let ((str (eshell-history-reference (car textargs))))
-             (unless (eq str (car textargs))
-               (goto-char (car posb))
-               (insert-and-inherit str)
-               (delete-char (- (car pose) (car posb)))))
-           (setq textargs (cdr textargs)
-                 posb (cdr posb)
-                 pose (cdr pose))))))))
+         (if full-line-subst
+             ;; Found a ^foo^bar substitution
+             (progn
+               (goto-char beg)
+               (insert-and-inherit full-line-subst)
+               (delete-char (- end beg)))
+           ;; Try to expand other substitutions
+           (while textargs
+             (let ((str (eshell-history-reference (car textargs))))
+               (unless (eq str (car textargs))
+                 (goto-char (car posb))
+                 (insert-and-inherit str)
+                 (delete-char (- (car pose) (car posb)))))
+             (setq textargs (cdr textargs)
+                   posb (cdr posb)
+                   pose (cdr pose)))))))))
 
 (defvar pcomplete-stub)
 (defvar pcomplete-last-completion-raw)
@@ -630,20 +639,31 @@ See also `eshell-read-history'."
                   (setq history (cdr history)))
                 (cdr fhist)))))))
 
+(defun eshell-history-substitution (line)
+  "Expand quick hist substitutions formatted as ^foo^bar^.
+Returns nil if string does not match quick substitution format,
+and acts like !!:s/foo/bar/ otherwise."
+  ;; `^string1^string2^'
+  ;;      Quick Substitution.  Repeat the last command, replacing
+  ;;      STRING1 with STRING2.  Equivalent to `!!:s/string1/string2/'
+  (when (and (eshell-using-module 'eshell-pred)
+            (string-match
+             "^\\^\\([^^]+\\)\\^\\([^^]+\\)\\(?:\\^\\(.*\\)\\)?$"
+             line))
+    ;; Save trailing match as `eshell-history-reference' runs string-match.
+    (let ((matched-end (match-string 3 line)))
+      (concat
+       (eshell-history-reference
+       (format "!!:s/%s/%s/"
+               (match-string 1 line)
+               (match-string 2 line)))
+       matched-end))))
+
 (defun eshell-history-reference (reference)
   "Expand directory stack REFERENCE.
 The syntax used here was taken from the Bash info manual.
 Returns the resultant reference, or the same string REFERENCE if none
 matched."
-  ;; `^string1^string2^'
-  ;;      Quick Substitution.  Repeat the last command, replacing
-  ;;      STRING1 with STRING2.  Equivalent to `!!:s/string1/string2/'
-  (if (and (eshell-using-module 'eshell-pred)
-          (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$"
-                        reference))
-      (setq reference (format "!!:s/%s/%s/"
-                             (match-string 1 reference)
-                             (match-string 2 reference))))
   ;; `!'
   ;;      Start a history substitution, except when followed by a
   ;;      space, tab, the end of the line, = or (.
index 2c12cacfff87632aa8ca77539e1f8d72bf20a440..61af4048d549bb833cece7e5c0ac55b77f6cf142 100644 (file)
@@ -545,7 +545,8 @@ that `ls -l' will show in the first column of its display. "
          (function
           (lambda (str)
             (if (string-match ,match str)
-                (setq str (replace-match ,replace t nil str)))
+                (setq str (replace-match ,replace t nil str))
+              (error (concat str ": substitution failed")))
             str)) lst)))))
 
 (defun eshell-include-members (&optional invert-p)