Move lisp/emacs-lisp/authors.el to admin/
[bpt/emacs.git] / lisp / eshell / em-prompt.el
CommitLineData
ae5e4c48 1;;; em-prompt.el --- command prompts -*- lexical-binding:t -*-
affbf647 2
ba318903 3;; Copyright (C) 1999-2014 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
f87b1284 29(require 'esh-mode)
dbba8a04 30(eval-when-compile (require 'eshell))
affbf647 31
3146b070 32;;;###autoload
35ff222c
GM
33(progn
34(defgroup eshell-prompt nil
affbf647
GM
35 "This module provides command prompts, and navigation between them,
36as is common with most shells."
37 :tag "Command prompts"
35ff222c 38 :group 'eshell-module))
affbf647 39
affbf647
GM
40;;; User Variables:
41
d783d303 42(defcustom eshell-prompt-load-hook nil
ec60da52 43 "A list of functions to call when loading `eshell-prompt'."
d783d303 44 :version "24.1" ; removed eshell-prompt-initialize
affbf647
GM
45 :type 'hook
46 :group 'eshell-prompt)
47
fcef2e13
GM
48(autoload 'eshell/pwd "em-dirs")
49
affbf647
GM
50(defcustom eshell-prompt-function
51 (function
52 (lambda ()
f5467d3f 53 (concat (abbreviate-file-name (eshell/pwd))
affbf647 54 (if (= (user-uid) 0) " # " " $ "))))
f5467d3f 55 "A function that returns the Eshell prompt string.
affbf647
GM
56Make sure to update `eshell-prompt-regexp' so that it will match your
57prompt."
58 :type 'function
59 :group 'eshell-prompt)
60
61(defcustom eshell-prompt-regexp "^[^#$\n]* [#$] "
ec60da52 62 "A regexp which fully matches your eshell prompt.
affbf647
GM
63This setting is important, since it affects how eshell will interpret
64the lines that are passed to it.
65If this variable is changed, all Eshell buffers must be exited and
66re-entered for it to take effect."
67 :type 'regexp
68 :group 'eshell-prompt)
69
70(defcustom eshell-highlight-prompt t
ec60da52 71 "If non-nil, Eshell should highlight the prompt."
affbf647
GM
72 :type 'boolean
73 :group 'eshell-prompt)
74
958e6876 75(defface eshell-prompt
4b56d0fe
CY
76 '((default :weight bold)
77 (((class color) (background light)) :foreground "Red")
78 (((class color) (background dark)) :foreground "Pink"))
ec60da52 79 "The face used to highlight prompt strings.
affbf647
GM
80For highlighting other kinds of strings -- similar to shell mode's
81behavior -- simply use an output filer which changes text properties."
82 :group 'eshell-prompt)
2fb1ec93 83(define-obsolete-face-alias 'eshell-prompt-face 'eshell-prompt "22.1")
affbf647
GM
84
85(defcustom eshell-before-prompt-hook nil
ec60da52 86 "A list of functions to call before outputting the prompt."
affbf647
GM
87 :type 'hook
88 :options '(eshell-begin-on-new-line)
89 :group 'eshell-prompt)
90
91(defcustom eshell-after-prompt-hook nil
ec60da52 92 "A list of functions to call after outputting the prompt.
affbf647
GM
93Note that if `eshell-scroll-show-maximum-output' is non-nil, then
94setting `eshell-show-maximum-output' here won't do much. It depends
95on whether the user wants the resizing to happen while output is
96arriving, or after."
97 :type 'hook
98 :options '(eshell-show-maximum-output)
99 :group 'eshell-prompt)
100
101;;; Functions:
102
103(defun eshell-prompt-initialize ()
104 "Initialize the prompting code."
105 (unless eshell-non-interactive-p
affbf647
GM
106 (add-hook 'eshell-post-command-hook 'eshell-emit-prompt nil t)
107
108 (make-local-variable 'eshell-prompt-regexp)
109 (if eshell-prompt-regexp
110 (set (make-local-variable 'paragraph-start) eshell-prompt-regexp))
111
112 (set (make-local-variable 'eshell-skip-prompt-function)
113 'eshell-skip-prompt)
114
115 (define-key eshell-command-map [(control ?n)] 'eshell-next-prompt)
116 (define-key eshell-command-map [(control ?p)] 'eshell-previous-prompt)))
117
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
2b84d763 127 font-lock-face eshell-prompt
1d1c55dc
LL
128 front-sticky (font-lock-face read-only)
129 rear-nonsticky (font-lock-face read-only))
affbf647
GM
130 prompt))
131 (eshell-interactive-print prompt)))
132 (run-hooks 'eshell-after-prompt-hook))
133
134(defun eshell-backward-matching-input (regexp arg)
135 "Search backward through buffer for match for REGEXP.
136Matches are searched for on lines that match `eshell-prompt-regexp'.
137With prefix argument N, search for Nth previous match.
138If 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))))
149
150(defun eshell-forward-matching-input (regexp arg)
151 "Search forward through buffer for match for REGEXP.
152Matches are searched for on lines that match `eshell-prompt-regexp'.
153With prefix argument N, search for Nth following match.
154If 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)))
157
158(defun eshell-next-prompt (n)
159 "Move to end of Nth next prompt in the buffer.
160See `eshell-prompt-regexp'."
161 (interactive "p")
162 (forward-paragraph n)
163 (eshell-skip-prompt))
164
165(defun eshell-previous-prompt (n)
166 "Move to end of Nth previous prompt in the buffer.
167See `eshell-prompt-regexp'."
168 (interactive "p")
169 (eshell-next-prompt (- (1+ n))))
170
171(defun eshell-skip-prompt ()
172 "Skip past the text matching regexp `eshell-prompt-regexp'.
173If 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)))))
178
dbba8a04 179(provide 'em-prompt)
affbf647 180
3146b070
GM
181;; Local Variables:
182;; generated-autoload-file: "esh-groups.el"
183;; End:
184
affbf647 185;;; em-prompt.el ends here