[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#29821: Ensure quick substitution only occurs at start of line
From: |
Jay Kamat |
Subject: |
bug#29821: Ensure quick substitution only occurs at start of line |
Date: |
Thu, 04 Jan 2018 17:53:14 -0800 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (gnu/linux) |
Noam Postavsky <npostavs@users.sourceforge.net> writes:
> There doesn't need to be trailing whitespace between the last "^" and
> subsequent text, bash:
> So I think this should do it?
>
> ...
> (string-match
> "^\\^\\([^^]+\\)\\^\\([^^]+\\)\\(?:\\^\\(.*\\)\\)?$"
> line))
> ;; Save trailing match as `eshell-history-reference' runs string-match.
> (let ((matched-end (match-string 3 line)))
> ...
Yup, that regex works perfectly for everything I tried, and preserves
the white-space at the end of the target (as bash does). I think it's a
good idea to follow bash as closely as possible.
For convenience, I've attached a new patch with those changes.
Thanks,
-Jay
>From b901ce56d646d7145989825f7c4b8d408bc500ac Mon Sep 17 00:00:00 2001
From: Jay Kamat <jaygkamat@gmail.com>
Date: Fri, 22 Dec 2017 15:34:44 -0800
Subject: [PATCH] Prevent expansion of quick substitutions when not at start of
line
See bug #29157 for an initial report
In Addition:
- 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 | 60 +++++++++++++++++++++++++++++++++-----------------
lisp/eshell/em-pred.el | 3 ++-
2 files changed, 42 insertions(+), 21 deletions(-)
diff --git a/lisp/eshell/em-hist.el b/lisp/eshell/em-hist.el
index df462a7058..f923ca679b 100644
--- a/lisp/eshell/em-hist.el
+++ b/lisp/eshell/em-hist.el
@@ -581,21 +581,30 @@ eshell-hist-parse-arguments
(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 @@ eshell-complete-history-reference
(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 (.
diff --git a/lisp/eshell/em-pred.el b/lisp/eshell/em-pred.el
index 72a7bc4afc..86a9d4262a 100644
--- a/lisp/eshell/em-pred.el
+++ b/lisp/eshell/em-pred.el
@@ -545,7 +545,8 @@ eshell-pred-substitute
(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)
--
2.11.0
- bug#29821: Ensure quick substitution only occurs at start of line, Andreas Schwab, 2018/01/01
- bug#29821: Ensure quick substitution only occurs at start of line, Noam Postavsky, 2018/01/01
- bug#29821: Ensure quick substitution only occurs at start of line, Jay Kamat, 2018/01/01
- bug#29821: Ensure quick substitution only occurs at start of line, Noam Postavsky, 2018/01/01
- bug#29821: Ensure quick substitution only occurs at start of line, Jay Kamat, 2018/01/02
- bug#29821: Ensure quick substitution only occurs at start of line, Noam Postavsky, 2018/01/02
- bug#29821: Ensure quick substitution only occurs at start of line, Jay Kamat, 2018/01/03
- bug#29821: Ensure quick substitution only occurs at start of line, Noam Postavsky, 2018/01/03
- bug#29821: Ensure quick substitution only occurs at start of line, Jay Kamat, 2018/01/04
- bug#29821: Ensure quick substitution only occurs at start of line, Noam Postavsky, 2018/01/04
- bug#29821: Ensure quick substitution only occurs at start of line,
Jay Kamat <=
- bug#29821: Ensure quick substitution only occurs at start of line, Noam Postavsky, 2018/01/05