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