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