em-prompt.el 6.0 KB

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