Function: term-replace-by-expanded-history-before-point
term-replace-by-expanded-history-before-point is a byte-compiled
function defined in term.el.gz.
Signature
(term-replace-by-expanded-history-before-point SILENT)
Documentation
Expand directory stack reference before point.
See term-replace-by-expanded-history. Returns t if successful.
Source Code
;; Defined in /usr/src/emacs/lisp/term.el.gz
(defun term-replace-by-expanded-history-before-point (silent)
"Expand directory stack reference before point.
See `term-replace-by-expanded-history'. Returns t if successful."
(save-excursion
(let ((toend (- (line-end-position) (point)))
(start (progn (term-bol nil) (point))))
(while (progn
(skip-chars-forward "^!^" (- (line-end-position) toend))
(< (point) (- (line-end-position) toend)))
;; This seems a bit complex. We look for references such as !!, !-num,
;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
;; If that wasn't enough, the plings can be suffixed with argument
;; range specifiers.
;; Argument ranges are complex too, so we hive off the input line,
;; referenced with plings, with the range string to `term-args'.
(setq term-input-ring-index nil)
(cond ((or (= (preceding-char) ?\\)
(term-within-quotes start (point)))
;; The history is quoted, or we're in quotes.
(goto-char (1+ (point))))
((looking-at "![0-9]+\\($\\|[^-]\\)")
;; We cannot know the interpreter's idea of input line numbers.
(goto-char (match-end 0))
(message "Absolute reference cannot be expanded"))
((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
;; Just a number of args from `number' lines backward.
(let ((number (1- (string-to-number
(buffer-substring (match-beginning 1)
(match-end 1))))))
(if (<= number (ring-length term-input-ring))
(progn
(replace-match
(term-args (term-previous-input-string number)
(match-beginning 2) (match-end 2))
t t)
(setq term-input-ring-index number)
(message "History item: %d" (1+ number)))
(goto-char (match-end 0))
(message "Relative reference exceeds input history size"))))
((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
;; Just a number of args from the previous input line.
(replace-match
(term-args (term-previous-input-string 0)
(match-beginning 1) (match-end 1))
t t)
(message "History item: previous"))
((looking-at
"!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
;; Most recent input starting with or containing (possibly
;; protected) string, maybe just a number of args. Phew.
(let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
(mb2 (match-beginning 2)) (me2 (match-end 2))
(exp (buffer-substring (or mb2 mb1) (or me2 me1)))
(pref (if (save-match-data (looking-at "!\\?")) "" "^"))
(pos (save-match-data
(term-previous-matching-input-string-position
(concat pref (regexp-quote exp)) 1))))
(if (null pos)
(progn
(goto-char (match-end 0))
(or silent
(progn (message "Not found")
(ding))))
(setq term-input-ring-index pos)
(replace-match
(term-args (ring-ref term-input-ring pos)
(match-beginning 4) (match-end 4))
t t)
(message "History item: %d" (1+ pos)))))
((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
;; Quick substitution on the previous input line.
(let ((old (buffer-substring (match-beginning 1) (match-end 1)))
(new (buffer-substring (match-beginning 2) (match-end 2)))
(pos nil))
(replace-match (term-previous-input-string 0) t t)
(setq pos (point))
(goto-char (match-beginning 0))
(if (not (search-forward old pos t))
(or silent
(error "Not found"))
(replace-match new t t)
(message "History item: substituted"))))
(t
(goto-char (match-end 0))))))))