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