From faccfdb5aa35595ef5746ff98fe254f8ab32288e Mon Sep 17 00:00:00 2001 From: Brian Leung Date: Sun, 30 Aug 2020 10:25:16 -0700 Subject: [PATCH 1/2] eshell/em-hist: Style and indentation cleanup * lisp/eshell/em-hist.el: Use spaces over tabs, when over single-clause if, and more consistent indentation. --- lisp/eshell/em-hist.el | 728 ++++++++++++++++++++--------------------- 1 file changed, 364 insertions(+), 364 deletions(-) diff --git a/lisp/eshell/em-hist.el b/lisp/eshell/em-hist.el index 5cee1bad36..99a1229295 100644 --- a/lisp/eshell/em-hist.el +++ b/lisp/eshell/em-hist.el @@ -65,10 +65,10 @@ ;;;###autoload (progn -(defgroup eshell-hist nil - "This module provides command history management." - :tag "History list management" - :group 'eshell-module)) + (defgroup eshell-hist nil + "This module provides command history management." + :tag "History list management" + :group 'eshell-module)) ;;; User Variables: @@ -93,13 +93,13 @@ eshell-history-file-name See also `eshell-read-history' and `eshell-write-history'. If it is nil, Eshell will use the value of HISTFILE." :type '(choice (const :tag "Use HISTFILE" nil) - file) + file) :group 'eshell-hist) (defcustom eshell-history-size 128 "Size of the input history ring. If nil, use envvar HISTSIZE." :type '(choice (const :tag "Use HISTSIZE" nil) - integer) + integer) :group 'eshell-hist) (defcustom eshell-hist-ignoredups nil @@ -117,8 +117,8 @@ eshell-save-history-on-exit If set to `ask', ask if any Eshell buffers are open at exit time. If set to t, history will always be saved, silently." :type '(choice (const :tag "Never" nil) - (const :tag "Ask" ask) - (const :tag "Always save" t)) + (const :tag "Ask" ask) + (const :tag "Always save" t)) :group 'eshell-hist) (defcustom eshell-input-filter 'eshell-input-filter-default @@ -179,8 +179,8 @@ eshell-hist-rebind-keys-alist ([down] . eshell-next-matching-input-from-input)) "History keys to bind differently if point is in input text." :type '(repeat (cons (vector :tag "Keys to bind" - (repeat :inline t sexp)) - (function :tag "Command"))) + (repeat :inline t sexp)) + (function :tag "Command"))) :group 'eshell-hist) ;;; Internal Variables: @@ -215,11 +215,11 @@ eshell-hist-mode-map ;; FIXME: Relies on `eshell-hist-match-partial' being set _before_ ;; em-hist is loaded and won't respect changes. (if eshell-hist-match-partial - (progn - (define-key map [(meta ?p)] 'eshell-previous-matching-input-from-input) - (define-key map [(meta ?n)] 'eshell-next-matching-input-from-input) - (define-key map (kbd "C-c M-p") #'eshell-previous-input) - (define-key map (kbd "C-c M-n") #'eshell-next-input)) + (progn + (define-key map [(meta ?p)] 'eshell-previous-matching-input-from-input) + (define-key map [(meta ?n)] 'eshell-next-matching-input-from-input) + (define-key map (kbd "C-c M-p") #'eshell-previous-input) + (define-key map (kbd "C-c M-n") #'eshell-next-input)) (define-key map [(meta ?p)] #'eshell-previous-input) (define-key map [(meta ?n)] #'eshell-next-input) (define-key map (kbd "C-c M-p") #'eshell-previous-matching-input-from-input) @@ -252,27 +252,27 @@ eshell-hist-initialize "Initialize the history management code for one Eshell buffer." (when (eshell-using-module 'eshell-cmpl) (add-hook 'pcomplete-try-first-hook - #'eshell-complete-history-reference nil t)) + #'eshell-complete-history-reference nil t)) (if (and (eshell-using-module 'eshell-rebind) - (not eshell-non-interactive-p)) + (not eshell-non-interactive-p)) (let ((rebind-alist eshell-rebind-keys-alist)) - (make-local-variable 'eshell-rebind-keys-alist) - (setq eshell-rebind-keys-alist - (append rebind-alist eshell-hist-rebind-keys-alist)) - (set (make-local-variable 'search-invisible) t) - (set (make-local-variable 'search-exit-option) t) - (add-hook 'isearch-mode-hook - (function - (lambda () - (if (>= (point) eshell-last-output-end) - (setq overriding-terminal-local-map - eshell-isearch-map)))) + (make-local-variable 'eshell-rebind-keys-alist) + (setq eshell-rebind-keys-alist + (append rebind-alist eshell-hist-rebind-keys-alist)) + (set (make-local-variable 'search-invisible) t) + (set (make-local-variable 'search-exit-option) t) + (add-hook 'isearch-mode-hook + (function + (lambda () + (when (>= (point) eshell-last-output-end) + (setq overriding-terminal-local-map + eshell-isearch-map)))) nil t) - (add-hook 'isearch-mode-end-hook - (function - (lambda () - (setq overriding-terminal-local-map nil))) + (add-hook 'isearch-mode-end-hook + (function + (lambda () + (setq overriding-terminal-local-map nil))) nil t)) (eshell-hist-mode)) @@ -280,11 +280,11 @@ eshell-hist-initialize (or eshell-history-size (let ((hsize (getenv "HISTSIZE"))) (setq eshell-history-size - (if (and (stringp hsize) - (integerp (setq hsize (string-to-number hsize))) - (> hsize 0)) - hsize - 128)))) + (if (and (stringp hsize) + (integerp (setq hsize (string-to-number hsize))) + (> hsize 0)) + hsize + 128)))) (make-local-variable 'eshell-history-file-name) (or eshell-history-file-name @@ -296,8 +296,8 @@ eshell-hist-initialize (if (minibuffer-window-active-p (selected-window)) (set (make-local-variable 'eshell-save-history-on-exit) nil) (set (make-local-variable 'eshell-history-ring) nil) - (if eshell-history-file-name - (eshell-read-history nil t)) + (when eshell-history-file-name + (eshell-read-history nil t)) (add-hook 'eshell-exit-hook #'eshell-write-history nil t)) @@ -314,17 +314,17 @@ eshell-hist-initialize (defun eshell-save-some-history () "Save the history for any open Eshell buffers." (dolist (buf (buffer-list)) - (if (buffer-live-p buf) - (with-current-buffer buf - (if (and eshell-mode - eshell-history-file-name - eshell-save-history-on-exit - (or (eq eshell-save-history-on-exit t) - (y-or-n-p - (format-message - "Save input history for Eshell buffer `%s'? " - (buffer-name buf))))) - (eshell-write-history)))))) + (when (buffer-live-p buf) + (with-current-buffer buf + (when (and eshell-mode + eshell-history-file-name + eshell-save-history-on-exit + (or (eq eshell-save-history-on-exit t) + (y-or-n-p + (format-message + "Save input history for Eshell buffer `%s'? " + (buffer-name buf))))) + (eshell-write-history)))))) (defun eshell/history (&rest args) "List in help buffer the buffer's input history." @@ -332,44 +332,44 @@ eshell/history (eshell-eval-using-options "history" args '((?r "read" nil read-history - "read from history file to current history list") + "read from history file to current history list") (?w "write" nil write-history - "write current history list to history file") + "write current history list to history file") (?a "append" nil append-history - "append current history list to history file") + "append current history list to history file") (?h "help" nil nil "display this usage message") :usage "[n] [-rwa [filename]]" :post-usage -"When Eshell is started, history is read from `eshell-history-file-name'. + "When Eshell is started, history is read from `eshell-history-file-name'. This is also the location where history info will be saved by this command, unless a different file is specified on the command line.") (and (or (not (ring-p eshell-history-ring)) - (ring-empty-p eshell-history-ring)) - (error "No history")) + (ring-empty-p eshell-history-ring)) + (error "No history")) (let (length file) (when (and args (string-match "^[0-9]+$" (car args))) (setq length (min (eshell-convert (car args)) - (ring-length eshell-history-ring)) - args (cdr args))) + (ring-length eshell-history-ring)) + args (cdr args))) (and length - (or read-history write-history append-history) - (error "history: extra arguments")) + (or read-history write-history append-history) + (error "history: extra arguments")) (when (and args (stringp (car args))) (setq file (car args) - args (cdr args))) + args (cdr args))) (cond (read-history (eshell-read-history file)) (write-history (eshell-write-history file)) (append-history (eshell-write-history file t)) (t (let* ((index (1- (or length (ring-length eshell-history-ring)))) - (ref (- (ring-length eshell-history-ring) index))) - ;; We have to build up a list ourselves from the ring vector. - (while (>= index 0) - (eshell-buffered-print - (format "%5d %s\n" ref (eshell-get-history index))) - (setq index (1- index) - ref (1+ ref))))))) + (ref (- (ring-length eshell-history-ring) index))) + ;; We have to build up a list ourselves from the ring vector. + (while (>= index 0) + (eshell-buffered-print + (format "%5d %s\n" ref (eshell-get-history index))) + (setq index (1- index) + ref (1+ ref))))))) (eshell-flush) nil)) @@ -389,12 +389,12 @@ eshell-add-input-to-history Input is entered into the input history ring, if the value of variable `eshell-input-filter' returns non-nil when called on the input." - (if (and (funcall eshell-input-filter input) - (or (null eshell-hist-ignoredups) - (not (ring-p eshell-history-ring)) - (ring-empty-p eshell-history-ring) - (not (string-equal (eshell-get-history 0) input)))) - (eshell-put-history input)) + (when (and (funcall eshell-input-filter input) + (or (null eshell-hist-ignoredups) + (not (ring-p eshell-history-ring)) + (ring-empty-p eshell-history-ring) + (not (string-equal (eshell-get-history 0) input)))) + (eshell-put-history input)) (setq eshell-save-history-index eshell-history-index) (setq eshell-history-index nil)) @@ -416,7 +416,7 @@ eshell-add-to-history command." (when (> (1- eshell-last-input-end) eshell-last-input-start) (let ((input (buffer-substring eshell-last-input-start - (1- eshell-last-input-end)))) + (1- eshell-last-input-end)))) (eshell-add-input-to-history input)))) (defun eshell-read-history (&optional filename silent) @@ -437,32 +437,32 @@ eshell-read-history (let ((file (or filename eshell-history-file-name))) (cond ((or (null file) - (equal file "")) + (equal file "")) nil) ((not (file-readable-p file)) (or silent - (message "Cannot read history file %s" file))) + (message "Cannot read history file %s" file))) (t (let* ((count 0) - (size eshell-history-size) - (ring (make-ring size)) - (ignore-dups eshell-hist-ignoredups)) - (with-temp-buffer - (insert-file-contents file) - ;; Watch for those date stamps in history files! - (goto-char (point-max)) - (while (and (< count size) - (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$" - nil t)) - (let ((history (match-string 1))) - (if (or (null ignore-dups) - (ring-empty-p ring) - (not (string-equal (ring-ref ring 0) history))) - (ring-insert-at-beginning - ring (subst-char-in-string ?\177 ?\n history)))) - (setq count (1+ count)))) - (setq eshell-history-ring ring - eshell-history-index nil)))))) + (size eshell-history-size) + (ring (make-ring size)) + (ignore-dups eshell-hist-ignoredups)) + (with-temp-buffer + (insert-file-contents file) + ;; Watch for those date stamps in history files! + (goto-char (point-max)) + (while (and (< count size) + (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$" + nil t)) + (let ((history (match-string 1))) + (when (or (null ignore-dups) + (ring-empty-p ring) + (not (string-equal (ring-ref ring 0) history))) + (ring-insert-at-beginning + ring (subst-char-in-string ?\177 ?\n history)))) + (setq count (1+ count)))) + (setq eshell-history-ring ring + eshell-history-index nil)))))) (defun eshell-write-history (&optional filename append) "Writes the buffer's `eshell-history-ring' to a history file. @@ -475,70 +475,70 @@ eshell-write-history See also `eshell-read-history'." (let* ((file (or filename eshell-history-file-name)) - (resolved-file (if (stringp file) (file-truename file)))) + (resolved-file (when (stringp file) (file-truename file)))) (cond ((or (null file) - (equal file "") - (null eshell-history-ring) - (ring-empty-p eshell-history-ring)) + (equal file "") + (null eshell-history-ring) + (ring-empty-p eshell-history-ring)) nil) ((not (file-writable-p resolved-file)) (message "Cannot write history file %s" resolved-file)) (t (let* ((ring eshell-history-ring) - (index (ring-length ring))) - ;; Write it all out into a buffer first. Much faster, but - ;; messier, than writing it one line at a time. - (with-temp-buffer - (while (> index 0) - (setq index (1- index)) - (let ((start (point))) + (index (ring-length ring))) + ;; Write it all out into a buffer first. Much faster, but + ;; messier, than writing it one line at a time. + (with-temp-buffer + (while (> index 0) + (setq index (1- index)) + (let ((start (point))) ;; Remove properties before inserting, to avoid trouble ;; with read-only strings (Bug#28700). (insert (substring-no-properties (ring-ref ring index)) ?\n) - (subst-char-in-region start (1- (point)) ?\n ?\177))) - (eshell-with-private-file-modes - (write-region (point-min) (point-max) resolved-file append - 'no-message)))))))) + (subst-char-in-region start (1- (point)) ?\n ?\177))) + (eshell-with-private-file-modes + (write-region (point-min) (point-max) resolved-file append + 'no-message)))))))) (defun eshell-list-history () "List in help buffer the buffer's input history." (interactive) (let (prefix prelen) (save-excursion - (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t) - (setq prefix (match-string 1) - prelen (length prefix)))) + (when (re-search-backward "!\\(.+\\)" (line-beginning-position) t) + (setq prefix (match-string 1) + prelen (length prefix)))) (if (or (not (ring-p eshell-history-ring)) - (ring-empty-p eshell-history-ring)) - (message "No history") + (ring-empty-p eshell-history-ring)) + (message "No history") (let ((history nil) - (history-buffer " *Input History*") - (index (1- (ring-length eshell-history-ring))) - (conf (current-window-configuration))) - ;; We have to build up a list ourselves from the ring vector. - (while (>= index 0) - (let ((hist (eshell-get-history index))) - (if (or (not prefix) - (and (>= (length hist) prelen) - (string= (substring hist 0 prelen) prefix))) - (setq history (cons hist history)))) - (setq index (1- index))) - ;; Change "completion" to "history reference" - ;; to make the display accurate. - (with-output-to-temp-buffer history-buffer - (display-completion-list - (completion-hilit-commonality history (length prefix))) - (set-buffer history-buffer) - (forward-line 3) - (while (search-backward "completion" nil 'move) - (replace-match "history reference"))) - (eshell-redisplay) - (message "Hit space to flush") - (let ((ch (read-event))) - (if (eq ch ?\ ) - (set-window-configuration conf) - (push ch unread-command-events))))))) + (history-buffer " *Input History*") + (index (1- (ring-length eshell-history-ring))) + (conf (current-window-configuration))) + ;; We have to build up a list ourselves from the ring vector. + (while (>= index 0) + (let ((hist (eshell-get-history index))) + (when (or (not prefix) + (and (>= (length hist) prelen) + (string= (substring hist 0 prelen) prefix))) + (setq history (cons hist history)))) + (setq index (1- index))) + ;; Change "completion" to "history reference" + ;; to make the display accurate. + (with-output-to-temp-buffer history-buffer + (display-completion-list + (completion-hilit-commonality history (length prefix))) + (set-buffer history-buffer) + (forward-line 3) + (while (search-backward "completion" nil 'move) + (replace-match "history reference"))) + (eshell-redisplay) + (message "Hit space to flush") + (let ((ch (read-event))) + (if (eq ch ?\ ) + (set-window-configuration conf) + (push ch unread-command-events))))))) (defun eshell-hist-word-reference (ref) "Return the word designator index referred to by REF." @@ -553,37 +553,37 @@ eshell-hist-word-reference (defun eshell-hist-parse-arguments (&optional b e) "Parse current command arguments in a history-code-friendly way." (let ((end (or e (point))) - (begin (or b (save-excursion (eshell-bol) (point)))) - (posb (list t)) - (pose (list t)) - (textargs (list t)) - hist args) + (begin (or b (save-excursion (eshell-bol) (point)))) + (posb (list t)) + (pose (list t)) + (textargs (list t)) + hist args) (unless (catch 'eshell-incomplete - (ignore - (setq args (eshell-parse-arguments begin end)))) + (ignore + (setq args (eshell-parse-arguments begin end)))) (save-excursion - (goto-char begin) - (while (< (point) end) - (if (get-text-property (point) 'arg-begin) - (nconc posb (list (point)))) - (if (get-text-property (point) 'arg-end) - (nconc pose - (list (if (= (1+ (point)) end) - (1+ (point)) - (point))))) - (forward-char)) - (setq posb (cdr posb) - pose (cdr pose)) - (cl-assert (= (length posb) (length args))) - (cl-assert (<= (length posb) (length pose)))) + (goto-char begin) + (while (< (point) end) + (when (get-text-property (point) 'arg-begin) + (nconc posb (list (point)))) + (when (get-text-property (point) 'arg-end) + (nconc pose + (list (if (= (1+ (point)) end) + (1+ (point)) + (point))))) + (forward-char)) + (setq posb (cdr posb) + pose (cdr pose)) + (cl-assert (= (length posb) (length args))) + (cl-assert (<= (length posb) (length pose)))) (setq hist (buffer-substring-no-properties begin end)) (let ((b posb) (e pose)) - (while b - (nconc textargs - (list (substring hist (- (car b) begin) - (- (car e) begin)))) - (setq b (cdr b) - e (cdr e)))) + (while b + (nconc textargs + (list (substring hist (- (car b) begin) + (- (car e) begin)))) + (setq b (cdr b) + e (cdr e)))) (setq textargs (cdr textargs)) (cl-assert (= (length textargs) (length args))) (list textargs posb pose)))) @@ -591,29 +591,29 @@ 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)) - (full-line (buffer-substring-no-properties 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))) - (full-line-subst (eshell-history-substitution full-line))) - (save-excursion - (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))))))))) + (posb (nreverse (nth 1 result))) + (pose (nreverse (nth 2 result))) + (full-line-subst (eshell-history-substitution full-line))) + (save-excursion + (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) @@ -624,29 +624,29 @@ eshell-complete-history-reference (let ((arg (pcomplete-actual-arg))) (when (string-match "\\`![^:^$*%]*\\'" arg) (setq pcomplete-stub (substring arg 1) - pcomplete-last-completion-raw t) + pcomplete-last-completion-raw t) (throw 'pcomplete-completions - (let ((history nil) - (index (1- (ring-length eshell-history-ring))) - (stublen (length pcomplete-stub))) - ;; We have to build up a list ourselves from the ring - ;; vector. - (while (>= index 0) - (let ((hist (eshell-get-history index))) - (if (and (>= (length hist) stublen) - (string= (substring hist 0 stublen) - pcomplete-stub) - (string-match "^\\([^:^$*% \t\n]+\\)" hist)) - (setq history (cons (match-string 1 hist) - history)))) - (setq index (1- index))) - (let ((fhist (list t))) - ;; uniquify the list, but preserve the order - (while history - (unless (member (car history) fhist) - (nconc fhist (list (car history)))) - (setq history (cdr history))) - (cdr fhist))))))) + (let ((history nil) + (index (1- (ring-length eshell-history-ring))) + (stublen (length pcomplete-stub))) + ;; We have to build up a list ourselves from the ring + ;; vector. + (while (>= index 0) + (let ((hist (eshell-get-history index))) + (when (and (>= (length hist) stublen) + (string= (substring hist 0 stublen) + pcomplete-stub) + (string-match "^\\([^:^$*% \t\n]+\\)" hist)) + (setq history (cons (match-string 1 hist) + history)))) + (setq index (1- index))) + (let ((fhist (list t))) + ;; uniquify the list, but preserve the order + (while history + (unless (member (car history) fhist) + (nconc fhist (list (car history)))) + (setq history (cdr history))) + (cdr fhist))))))) (defun eshell-history-substitution (line) "Expand quick hist substitutions formatted as ^foo^bar^. @@ -656,16 +656,16 @@ eshell-history-substitution ;; 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)) + (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))) + (format "!!:s/%s/%s/" + (match-string 1 line) + (match-string 2 line))) matched-end)))) (defun eshell-history-reference (reference) @@ -681,102 +681,102 @@ eshell-history-reference (setq eshell-history-index nil) (let ((event (eshell-hist-parse-event-designator reference))) (unless event - (error "Could not find history event `%s'" reference)) + (error "Could not find history event `%s'" reference)) (setq eshell-history-index (car event) - reference (substring reference (cdr event)) - event (eshell-get-history eshell-history-index)) + reference (substring reference (cdr event)) + event (eshell-get-history eshell-history-index)) (if (not (string-match "^[:^$*%]" reference)) - event - (let ((word (eshell-hist-parse-word-designator - event reference))) - (unless word - (error "Unable to honor word designator `%s'" reference)) - (unless (string-match "^[:^$*%][[$^*%0-9-]" reference) - (setcdr word 0)) - (setq event (car word) - reference (substring reference (cdr word))) - (if (not (and (eshell-using-module 'eshell-pred) - (string-match "^:" reference))) - event - (eshell-hist-parse-modifier event reference))))))) + event + (let ((word (eshell-hist-parse-word-designator + event reference))) + (unless word + (error "Unable to honor word designator `%s'" reference)) + (unless (string-match "^[:^$*%][[$^*%0-9-]" reference) + (setcdr word 0)) + (setq event (car word) + reference (substring reference (cdr word))) + (if (not (and (eshell-using-module 'eshell-pred) + (string-match "^:" reference))) + event + (eshell-hist-parse-modifier event reference))))))) (defun eshell-hist-parse-event-designator (reference) "Parse a history event designator beginning in REFERENCE." (let* ((index (string-match eshell-hist-event-designator reference)) - (end (and index (match-end 0)))) + (end (and index (match-end 0)))) (unless index (error "Invalid history event designator `%s'" reference)) (let* ((event (match-string 1 reference)) - (pos - (cond - ((string= event "!") (ring-length eshell-history-ring)) - ((string= event "#") (error "!# not yet implemented")) - ((string-match "^-?[0-9]+$" event) - (let ((num (string-to-number event))) - (if (>= num 0) - (- (ring-length eshell-history-ring) num) - (1- (abs num))))) - ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event) - (let ((pref (if (> (length (match-string 1 event)) 0) - "" "^")) - (str (match-string 2 event))) - (save-match-data - (eshell-previous-matching-input-string-position - (concat pref (regexp-quote str)) 1)))) - (t - (error "Failed to parse event designator `%s'" event))))) + (pos + (cond + ((string= event "!") (ring-length eshell-history-ring)) + ((string= event "#") (error "!# not yet implemented")) + ((string-match "^-?[0-9]+$" event) + (let ((num (string-to-number event))) + (if (>= num 0) + (- (ring-length eshell-history-ring) num) + (1- (abs num))))) + ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event) + (let ((pref (if (> (length (match-string 1 event)) 0) + "" "^")) + (str (match-string 2 event))) + (save-match-data + (eshell-previous-matching-input-string-position + (concat pref (regexp-quote str)) 1)))) + (t + (error "Failed to parse event designator `%s'" event))))) (and pos (cons pos end))))) (defun eshell-hist-parse-word-designator (hist reference) "Parse a history word designator beginning for HIST in REFERENCE." (let* ((index (string-match eshell-hist-word-designator reference)) - (end (and index (match-end 0)))) + (end (and index (match-end 0)))) (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%)) (error "Invalid history word designator `%s'" reference)) (let ((nth (match-string 1 reference)) - (mth (match-string 2 reference)) - (here (point)) - textargs) + (mth (match-string 2 reference)) + (here (point)) + textargs) (insert hist) (setq textargs (car (eshell-hist-parse-arguments here (point)))) (delete-region here (point)) - (if (string= nth "*") - (if mth - (error "Invalid history word designator `%s'" - reference) - (setq nth 1 mth "-$"))) + (when (string= nth "*") + (if mth + (error "Invalid history word designator `%s'" + reference) + (setq nth 1 mth "-$"))) (if (not mth) - (if nth - (setq mth nth) - (setq nth 0 mth "$")) - (if (string= mth "-") - (setq mth (- (length textargs) 2)) - (if (string= mth "*") - (setq mth "$") - (if (not (and (> (length mth) 1) - (eq (aref mth 0) ?-))) - (error "Invalid history word designator `%s'" - reference) - (setq mth (substring mth 1)))))) + (if nth + (setq mth nth) + (setq nth 0 mth "$")) + (if (string= mth "-") + (setq mth (- (length textargs) 2)) + (if (string= mth "*") + (setq mth "$") + (if (not (and (> (length mth) 1) + (eq (aref mth 0) ?-))) + (error "Invalid history word designator `%s'" + reference) + (setq mth (substring mth 1)))))) (unless (numberp nth) - (setq nth (eshell-hist-word-reference nth))) + (setq nth (eshell-hist-word-reference nth))) (unless (numberp mth) - (setq mth (eshell-hist-word-reference mth))) + (setq mth (eshell-hist-word-reference mth))) (cons (mapconcat #'identity (eshell-sublist textargs nth mth) " ") - end)))) + end)))) (defun eshell-hist-parse-modifier (hist reference) "Parse a history modifier beginning for HIST in REFERENCE." (let ((here (point))) (insert reference) (prog1 - (save-restriction - (narrow-to-region here (point)) - (goto-char (point-min)) - (let ((modifiers (cdr (eshell-parse-modifiers)))) - (dolist (mod modifiers) - (setq hist (car (funcall mod (list hist))))) - hist)) + (save-restriction + (narrow-to-region here (point)) + (goto-char (point-min)) + (let ((modifiers (cdr (eshell-parse-modifiers)))) + (dolist (mod modifiers) + (setq hist (car (funcall mod (list hist))))) + hist)) (delete-region here (point))))) (defun eshell-get-next-from-history () @@ -786,36 +786,36 @@ eshell-get-next-from-history (interactive) (if eshell-save-history-index (progn - (setq eshell-history-index (1+ eshell-save-history-index)) - (eshell-next-input 1)) + (setq eshell-history-index (1+ eshell-save-history-index)) + (eshell-next-input 1)) (message "No previous history command"))) (defun eshell-search-arg (arg) ;; First make sure there is a ring and that we are after the process ;; mark - (if (and eshell-hist-move-to-end - (< (point) eshell-last-output-end)) - (goto-char eshell-last-output-end)) + (when (and eshell-hist-move-to-end + (< (point) eshell-last-output-end)) + (goto-char eshell-last-output-end)) (cond ((or (null eshell-history-ring) - (ring-empty-p eshell-history-ring)) - (error "Empty input ring")) - ((zerop arg) - ;; arg of zero resets search from beginning, and uses arg of - ;; 1 - (setq eshell-history-index nil) - 1) - (t - arg))) + (ring-empty-p eshell-history-ring)) + (error "Empty input ring")) + ((zerop arg) + ;; arg of zero resets search from beginning, and uses arg of + ;; 1 + (setq eshell-history-index nil) + 1) + (t + arg))) (defun eshell-search-start (arg) "Index to start a directional search, starting at `eshell-history-index'." (if eshell-history-index ;; If a search is running, offset by 1 in direction of arg (mod (+ eshell-history-index (if (> arg 0) 1 -1)) - (ring-length eshell-history-ring)) + (ring-length eshell-history-ring)) ;; For a new search, start from beginning or end, as appropriate (if (>= arg 0) - 0 ; First elt for forward search + 0 ; First elt for forward search ;; Last elt for backward search (1- (ring-length eshell-history-ring))))) @@ -823,9 +823,9 @@ eshell-previous-input-string "Return the string ARG places along the input ring. Moves relative to `eshell-history-index'." (eshell-get-history (if eshell-history-index - (mod (+ arg eshell-history-index) - (ring-length eshell-history-ring)) - arg))) + (mod (+ arg eshell-history-index) + (ring-length eshell-history-ring)) + arg))) (defun eshell-previous-input (arg) "Cycle backwards through input history." @@ -841,37 +841,37 @@ eshell-previous-matching-input-string "Return the string matching REGEXP ARG places along the input ring. Moves relative to `eshell-history-index'." (let* ((pos (eshell-previous-matching-input-string-position regexp arg))) - (if pos (eshell-get-history pos)))) + (when pos (eshell-get-history pos)))) (defun eshell-previous-matching-input-string-position - (regexp arg &optional start) + (regexp arg &optional start) "Return the index matching REGEXP ARG places along the input ring. Moves relative to START, or `eshell-history-index'." - (if (or (not (ring-p eshell-history-ring)) - (ring-empty-p eshell-history-ring)) - (error "No history")) + (when (or (not (ring-p eshell-history-ring)) + (ring-empty-p eshell-history-ring)) + (error "No history")) (let* ((len (ring-length eshell-history-ring)) - (motion (if (> arg 0) 1 -1)) - (n (mod (- (or start (eshell-search-start arg)) motion) len)) - (tried-each-ring-item nil) - (case-fold-search (eshell-under-windows-p)) - (prev nil)) + (motion (if (> arg 0) 1 -1)) + (n (mod (- (or start (eshell-search-start arg)) motion) len)) + (tried-each-ring-item nil) + (case-fold-search (eshell-under-windows-p)) + (prev nil)) ;; Do the whole search as many times as the argument says. (while (and (/= arg 0) (not tried-each-ring-item)) ;; Step once. (setq prev n - n (mod (+ n motion) len)) + n (mod (+ n motion) len)) ;; If we haven't reached a match, step some more. (while (and (not tried-each-ring-item) - (not (string-match regexp (eshell-get-history n)))) - (setq n (mod (+ n motion) len) - ;; If we have gone all the way around in this search. - tried-each-ring-item (= n prev))) + (not (string-match regexp (eshell-get-history n)))) + (setq n (mod (+ n motion) len) + ;; If we have gone all the way around in this search. + tried-each-ring-item (= n prev))) (setq arg (if (> arg 0) (1- arg) (1+ arg)))) ;; Now that we know which ring element to use, if we found it, ;; return that. - (if (string-match regexp (eshell-get-history n)) - n))) + (when (string-match regexp (eshell-get-history n)) + n))) (defun eshell-previous-matching-input (regexp arg) "Search backwards through input history for match for REGEXP. @@ -880,15 +880,15 @@ eshell-previous-matching-input If N is negative, find the next or Nth next match." (interactive (eshell-regexp-arg "Previous input matching (regexp): ")) (setq arg (eshell-search-arg arg)) - (if (> eshell-last-output-end (point)) - (error "Point not located after prompt")) + (when (> eshell-last-output-end (point)) + (error "Point not located after prompt")) (let ((pos (eshell-previous-matching-input-string-position regexp arg))) ;; Has a match been found? (if (null pos) - (error "Not found") + (error "Not found") (setq eshell-history-index pos) (unless (minibuffer-window-active-p (selected-window)) - (message "History item: %d" (- (ring-length eshell-history-ring) pos))) + (message "History item: %d" (- (ring-length eshell-history-ring) pos))) ;; Can't use kill-region as it sets this-command (delete-region eshell-last-output-end (point)) (insert-and-inherit (eshell-get-history pos))))) @@ -907,13 +907,13 @@ eshell-previous-matching-input-from-input With prefix argument N, search for Nth previous match. If N is negative, search forwards for the -Nth following match." (interactive "p") - (if (not (memq last-command '(eshell-previous-matching-input-from-input - eshell-next-matching-input-from-input))) - ;; Starting a new search - (setq eshell-matching-input-from-input-string - (buffer-substring (save-excursion (eshell-bol) (point)) - (point)) - eshell-history-index nil)) + (when (not (memq last-command '(eshell-previous-matching-input-from-input + eshell-next-matching-input-from-input))) + ;; Starting a new search + (setq eshell-matching-input-from-input-string + (buffer-substring (save-excursion (eshell-bol) (point)) + (point)) + eshell-history-index nil)) (eshell-previous-matching-input (concat "^" (regexp-quote eshell-matching-input-from-input-string)) arg)) @@ -933,42 +933,42 @@ eshell-test-imatch (let ((before (point))) (eshell-bol) (if (and (not (bolp)) - (<= (point) before)) - t - (if isearch-forward - (progn - (end-of-line) - (forward-char)) - (beginning-of-line) - (backward-char)))))) + (<= (point) before)) + t + (if isearch-forward + (progn + (end-of-line) + (forward-char)) + (beginning-of-line) + (backward-char)))))) (defun eshell-return-to-prompt () "Once a search string matches, insert it at the end and go there." (setq isearch-other-end nil) (let ((found (eshell-test-imatch)) before) (while (and (not found) - (setq before - (funcall (if isearch-forward - 're-search-forward - 're-search-backward) - isearch-string nil t))) + (setq before + (funcall (if isearch-forward + 're-search-forward + 're-search-backward) + isearch-string nil t))) (setq found (eshell-test-imatch))) (if (not found) - (progn - (goto-char eshell-last-output-end) - (delete-region (point) (point-max))) + (progn + (goto-char eshell-last-output-end) + (delete-region (point) (point-max))) (setq before (point)) (let ((text (buffer-substring-no-properties - (point) (line-end-position))) - (orig (marker-position eshell-last-output-end))) - (goto-char eshell-last-output-end) - (delete-region (point) (point-max)) - (when (and text (> (length text) 0)) - (insert text) - (put-text-property (1- (point)) (point) - 'last-search-pos before) - (set-marker eshell-last-output-end orig) - (goto-char eshell-last-output-end)))))) + (point) (line-end-position))) + (orig (marker-position eshell-last-output-end))) + (goto-char eshell-last-output-end) + (delete-region (point) (point-max)) + (when (and text (> (length text) 0)) + (insert text) + (put-text-property (1- (point)) (point) + 'last-search-pos before) + (set-marker eshell-last-output-end orig) + (goto-char eshell-last-output-end)))))) (defun eshell-prepare-for-search () "Make sure the old history file is at the beginning of the buffer." @@ -976,9 +976,9 @@ eshell-prepare-for-search (save-excursion (goto-char (point-min)) (let ((end (copy-marker (point) t))) - (insert-file-contents eshell-history-file-name) - (set-text-properties (point-min) end - '(history t invisible t)))))) + (insert-file-contents eshell-history-file-name) + (set-text-properties (point-min) end + '(history t invisible t)))))) (defun eshell-isearch-backward (&optional invert) "Do incremental regexp search backward through past commands." @@ -994,12 +994,12 @@ eshell-isearch-repeat-backward "Do incremental regexp search backward through past commands." (interactive) (let ((old-pos (get-text-property (1- (point-max)) - 'last-search-pos))) + 'last-search-pos))) (when old-pos (goto-char old-pos) (if invert - (end-of-line) - (backward-char))) + (end-of-line) + (backward-char))) (setq isearch-forward invert) (isearch-search-and-update))) @@ -1028,7 +1028,7 @@ eshell-isearch-abort (defun eshell-isearch-delete-char () (interactive) (save-excursion - (isearch-delete-char))) + (isearch-delete-char))) (defun eshell-isearch-return () (interactive) -- 2.28.0