]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-prompt.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / eshell / em-prompt.el
1 ;;; em-prompt.el --- command prompts
2
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;; Most of the prompt navigation commands of `comint-mode' are
25 ;; supported, such as C-c C-n, C-c C-p, etc.
26
27 ;;; Code:
28
29 (eval-when-compile (require 'eshell))
30
31 ;;;###autoload
32 (eshell-defgroup eshell-prompt nil
33 "This module provides command prompts, and navigation between them,
34 as is common with most shells."
35 :tag "Command prompts"
36 :group 'eshell-module)
37
38 ;;; User Variables:
39
40 (defcustom eshell-prompt-load-hook '(eshell-prompt-initialize)
41 "A list of functions to call when loading `eshell-prompt'."
42 :type 'hook
43 :group 'eshell-prompt)
44
45 (defcustom eshell-prompt-function
46 (function
47 (lambda ()
48 (concat (abbreviate-file-name (eshell/pwd))
49 (if (= (user-uid) 0) " # " " $ "))))
50 "A function that returns the Eshell prompt string.
51 Make sure to update `eshell-prompt-regexp' so that it will match your
52 prompt."
53 :type 'function
54 :group 'eshell-prompt)
55
56 (defcustom eshell-prompt-regexp "^[^#$\n]* [#$] "
57 "A regexp which fully matches your eshell prompt.
58 This setting is important, since it affects how eshell will interpret
59 the lines that are passed to it.
60 If this variable is changed, all Eshell buffers must be exited and
61 re-entered for it to take effect."
62 :type 'regexp
63 :group 'eshell-prompt)
64
65 (defcustom eshell-highlight-prompt t
66 "If non-nil, Eshell should highlight the prompt."
67 :type 'boolean
68 :group 'eshell-prompt)
69
70 (defface eshell-prompt
71 '((((class color) (background light)) (:foreground "Red" :bold t))
72 (((class color) (background dark)) (:foreground "Pink" :bold t))
73 (t (:bold t)))
74 "The face used to highlight prompt strings.
75 For highlighting other kinds of strings -- similar to shell mode's
76 behavior -- simply use an output filer which changes text properties."
77 :group 'eshell-prompt)
78 (define-obsolete-face-alias 'eshell-prompt-face 'eshell-prompt "22.1")
79
80 (defcustom eshell-before-prompt-hook nil
81 "A list of functions to call before outputting the prompt."
82 :type 'hook
83 :options '(eshell-begin-on-new-line)
84 :group 'eshell-prompt)
85
86 (defcustom eshell-after-prompt-hook nil
87 "A list of functions to call after outputting the prompt.
88 Note that if `eshell-scroll-show-maximum-output' is non-nil, then
89 setting `eshell-show-maximum-output' here won't do much. It depends
90 on whether the user wants the resizing to happen while output is
91 arriving, or after."
92 :type 'hook
93 :options '(eshell-show-maximum-output)
94 :group 'eshell-prompt)
95
96 ;;; Functions:
97
98 (defun eshell-prompt-initialize ()
99 "Initialize the prompting code."
100 (unless eshell-non-interactive-p
101 (add-hook 'eshell-post-command-hook 'eshell-emit-prompt nil t)
102
103 (make-local-variable 'eshell-prompt-regexp)
104 (if eshell-prompt-regexp
105 (set (make-local-variable 'paragraph-start) eshell-prompt-regexp))
106
107 (set (make-local-variable 'eshell-skip-prompt-function)
108 'eshell-skip-prompt)
109
110 (define-key eshell-command-map [(control ?n)] 'eshell-next-prompt)
111 (define-key eshell-command-map [(control ?p)] 'eshell-previous-prompt)))
112
113 (defun eshell-emit-prompt ()
114 "Emit a prompt if eshell is being used interactively."
115 (run-hooks 'eshell-before-prompt-hook)
116 (if (not eshell-prompt-function)
117 (set-marker eshell-last-output-end (point))
118 (let ((prompt (funcall eshell-prompt-function)))
119 (and eshell-highlight-prompt
120 (add-text-properties 0 (length prompt)
121 '(read-only t
122 face eshell-prompt
123 rear-nonsticky (face read-only))
124 prompt))
125 (eshell-interactive-print prompt)))
126 (run-hooks 'eshell-after-prompt-hook))
127
128 (defun eshell-backward-matching-input (regexp arg)
129 "Search backward through buffer for match for REGEXP.
130 Matches are searched for on lines that match `eshell-prompt-regexp'.
131 With prefix argument N, search for Nth previous match.
132 If N is negative, find the next or Nth next match."
133 (interactive (eshell-regexp-arg "Backward input matching (regexp): "))
134 (let* ((re (concat eshell-prompt-regexp ".*" regexp))
135 (pos (save-excursion (end-of-line (if (> arg 0) 0 1))
136 (if (re-search-backward re nil t arg)
137 (point)))))
138 (if (null pos)
139 (progn (message "Not found")
140 (ding))
141 (goto-char pos)
142 (eshell-bol))))
143
144 (defun eshell-forward-matching-input (regexp arg)
145 "Search forward through buffer for match for REGEXP.
146 Matches are searched for on lines that match `eshell-prompt-regexp'.
147 With prefix argument N, search for Nth following match.
148 If N is negative, find the previous or Nth previous match."
149 (interactive (eshell-regexp-arg "Forward input matching (regexp): "))
150 (eshell-backward-matching-input regexp (- arg)))
151
152 (defun eshell-next-prompt (n)
153 "Move to end of Nth next prompt in the buffer.
154 See `eshell-prompt-regexp'."
155 (interactive "p")
156 (forward-paragraph n)
157 (eshell-skip-prompt))
158
159 (defun eshell-previous-prompt (n)
160 "Move to end of Nth previous prompt in the buffer.
161 See `eshell-prompt-regexp'."
162 (interactive "p")
163 (eshell-next-prompt (- (1+ n))))
164
165 (defun eshell-skip-prompt ()
166 "Skip past the text matching regexp `eshell-prompt-regexp'.
167 If this takes us past the end of the current line, don't skip at all."
168 (let ((eol (line-end-position)))
169 (if (and (looking-at eshell-prompt-regexp)
170 (<= (match-end 0) eol))
171 (goto-char (match-end 0)))))
172
173 (provide 'em-prompt)
174
175 ;; Local Variables:
176 ;; generated-autoload-file: "esh-groups.el"
177 ;; End:
178
179 ;;; em-prompt.el ends here