Merge from gnulib
[emacs.git] / lisp / eshell / em-prompt.el
blob69c5bd62259dd17f94e50091be970b80bbea553b
1 ;;; em-prompt.el --- command prompts -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Most of the prompt navigation commands of `comint-mode' are
25 ;; supported, such as C-c C-n, C-c C-p, etc.
27 ;;; Code:
29 (require 'esh-mode)
30 (eval-when-compile (require 'eshell))
32 ;;;###autoload
33 (progn
34 (defgroup eshell-prompt nil
35 "This module provides command prompts, and navigation between them,
36 as is common with most shells."
37 :tag "Command prompts"
38 :group 'eshell-module))
40 ;;; User Variables:
42 (defcustom eshell-prompt-load-hook nil
43 "A list of functions to call when loading `eshell-prompt'."
44 :version "24.1" ; removed eshell-prompt-initialize
45 :type 'hook
46 :group 'eshell-prompt)
48 (autoload 'eshell/pwd "em-dirs")
50 (defcustom eshell-prompt-function
51 (function
52 (lambda ()
53 (concat (abbreviate-file-name (eshell/pwd))
54 (if (= (user-uid) 0) " # " " $ "))))
55 "A function that returns the Eshell prompt string.
56 Make sure to update `eshell-prompt-regexp' so that it will match your
57 prompt."
58 :type 'function
59 :group 'eshell-prompt)
61 (defcustom eshell-prompt-regexp "^[^#$\n]* [#$] "
62 "A regexp which fully matches your eshell prompt.
63 This setting is important, since it affects how eshell will interpret
64 the lines that are passed to it.
65 If this variable is changed, all Eshell buffers must be exited and
66 re-entered for it to take effect."
67 :type 'regexp
68 :group 'eshell-prompt)
70 (defcustom eshell-highlight-prompt t
71 "If non-nil, Eshell should highlight the prompt."
72 :type 'boolean
73 :group 'eshell-prompt)
75 (defface eshell-prompt
76 '((default :weight bold)
77 (((class color) (background light)) :foreground "Red")
78 (((class color) (background dark)) :foreground "Pink"))
79 "The face used to highlight prompt strings.
80 For highlighting other kinds of strings -- similar to shell mode's
81 behavior -- simply use an output filer which changes text properties."
82 :group 'eshell-prompt)
83 (define-obsolete-face-alias 'eshell-prompt-face 'eshell-prompt "22.1")
85 (defcustom eshell-before-prompt-hook nil
86 "A list of functions to call before outputting the prompt."
87 :type 'hook
88 :options '(eshell-begin-on-new-line)
89 :group 'eshell-prompt)
91 (defcustom eshell-after-prompt-hook nil
92 "A list of functions to call after outputting the prompt.
93 Note that if `eshell-scroll-show-maximum-output' is non-nil, then
94 setting `eshell-show-maximum-output' here won't do much. It depends
95 on whether the user wants the resizing to happen while output is
96 arriving, or after."
97 :type 'hook
98 :options '(eshell-show-maximum-output)
99 :group 'eshell-prompt)
101 ;;; Functions:
103 (defun eshell-prompt-initialize ()
104 "Initialize the prompting code."
105 (unless eshell-non-interactive-p
106 (add-hook 'eshell-post-command-hook 'eshell-emit-prompt nil t)
108 (make-local-variable 'eshell-prompt-regexp)
109 (if eshell-prompt-regexp
110 (set (make-local-variable 'paragraph-start) eshell-prompt-regexp))
112 (set (make-local-variable 'eshell-skip-prompt-function)
113 'eshell-skip-prompt)
115 (define-key eshell-command-map [(control ?n)] 'eshell-next-prompt)
116 (define-key eshell-command-map [(control ?p)] 'eshell-previous-prompt)))
118 (defun eshell-emit-prompt ()
119 "Emit a prompt if eshell is being used interactively."
120 (run-hooks 'eshell-before-prompt-hook)
121 (if (not eshell-prompt-function)
122 (set-marker eshell-last-output-end (point))
123 (let ((prompt (funcall eshell-prompt-function)))
124 (and eshell-highlight-prompt
125 (add-text-properties 0 (length prompt)
126 '(read-only t
127 font-lock-face eshell-prompt
128 front-sticky (font-lock-face read-only)
129 rear-nonsticky (font-lock-face read-only))
130 prompt))
131 (eshell-interactive-print prompt)))
132 (run-hooks 'eshell-after-prompt-hook))
134 (defun eshell-backward-matching-input (regexp arg)
135 "Search backward through buffer for match for REGEXP.
136 Matches are searched for on lines that match `eshell-prompt-regexp'.
137 With prefix argument N, search for Nth previous match.
138 If N is negative, find the next or Nth next match."
139 (interactive (eshell-regexp-arg "Backward input matching (regexp): "))
140 (let* ((re (concat eshell-prompt-regexp ".*" regexp))
141 (pos (save-excursion (end-of-line (if (> arg 0) 0 1))
142 (if (re-search-backward re nil t arg)
143 (point)))))
144 (if (null pos)
145 (progn (message "Not found")
146 (ding))
147 (goto-char pos)
148 (eshell-bol))))
150 (defun eshell-forward-matching-input (regexp arg)
151 "Search forward through buffer for match for REGEXP.
152 Matches are searched for on lines that match `eshell-prompt-regexp'.
153 With prefix argument N, search for Nth following match.
154 If N is negative, find the previous or Nth previous match."
155 (interactive (eshell-regexp-arg "Forward input matching (regexp): "))
156 (eshell-backward-matching-input regexp (- arg)))
158 (defun eshell-next-prompt (n)
159 "Move to end of Nth next prompt in the buffer.
160 See `eshell-prompt-regexp'."
161 (interactive "p")
162 (forward-paragraph n)
163 (eshell-skip-prompt))
165 (defun eshell-previous-prompt (n)
166 "Move to end of Nth previous prompt in the buffer.
167 See `eshell-prompt-regexp'."
168 (interactive "p")
169 (eshell-next-prompt (- (1+ n))))
171 (defun eshell-skip-prompt ()
172 "Skip past the text matching regexp `eshell-prompt-regexp'.
173 If this takes us past the end of the current line, don't skip at all."
174 (let ((eol (line-end-position)))
175 (if (and (looking-at eshell-prompt-regexp)
176 (<= (match-end 0) eol))
177 (goto-char (match-end 0)))))
179 (provide 'em-prompt)
181 ;; Local Variables:
182 ;; generated-autoload-file: "esh-groups.el"
183 ;; End:
185 ;;; em-prompt.el ends here