em-rebind.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ;;; em-rebind.el --- rebind keys when point is at current input
  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. ;;; Code:
  17. (eval-when-compile (require 'eshell))
  18. ;;;###autoload
  19. (eshell-defgroup eshell-rebind nil
  20. "This module allows for special keybindings that only take effect
  21. while the point is in a region of input text. By default, it binds
  22. C-a to move to the beginning of the input text (rather than just the
  23. beginning of the line), and C-p and C-n to move through the input
  24. history, C-u kills the current input text, etc. It also, if
  25. `eshell-confine-point-to-input' is non-nil, does not allow certain
  26. commands to cause the point to leave the input area, such as
  27. `backward-word', `previous-line', etc. This module intends to mimic
  28. the behavior of normal shells while the user editing new input text."
  29. :tag "Rebind keys at input"
  30. :group 'eshell-module)
  31. ;;; User Variables:
  32. (defcustom eshell-rebind-load-hook nil
  33. "A list of functions to call when loading `eshell-rebind'."
  34. :version "24.1" ; removed eshell-rebind-initialize
  35. :type 'hook
  36. :group 'eshell-rebind)
  37. (defcustom eshell-rebind-keys-alist
  38. '(([(control ?a)] . eshell-bol)
  39. ([home] . eshell-bol)
  40. ([(control ?d)] . eshell-delchar-or-maybe-eof)
  41. ([backspace] . eshell-delete-backward-char)
  42. ([delete] . eshell-delete-backward-char)
  43. ([(control ?w)] . backward-kill-word)
  44. ([(control ?u)] . eshell-kill-input))
  45. "Bind some keys differently if point is in input text."
  46. :type '(repeat (cons (vector :tag "Keys to bind"
  47. (repeat :inline t sexp))
  48. (function :tag "Command")))
  49. :group 'eshell-rebind)
  50. (defcustom eshell-confine-point-to-input t
  51. "If non-nil, do not allow the point to leave the current input.
  52. This is more difficult to do nicely in Emacs than one might think.
  53. Basically, the `point-left' attribute is added to the input text, and
  54. a function is placed on that hook to take the point back to
  55. `eshell-last-output-end' every time the user tries to move away. But
  56. since there are many cases in which the point _ought_ to move away
  57. \(for programmatic reasons), the variable
  58. `eshell-cannot-leave-input-list' defines commands which are affected
  59. from this rule. However, this list is by no means as complete as it
  60. probably should be, so basically all one can hope for is that other
  61. people will left the point alone in the Eshell buffer. Sigh."
  62. :type 'boolean
  63. :group 'eshell-rebind)
  64. (defcustom eshell-error-if-move-away t
  65. "If non-nil, consider it an error to try to move outside current input.
  66. This is default behavior of shells like bash."
  67. :type 'boolean
  68. :group 'eshell-rebind)
  69. (defcustom eshell-remap-previous-input t
  70. "If non-nil, remap input keybindings on previous prompts as well."
  71. :type 'boolean
  72. :group 'eshell-rebind)
  73. (defcustom eshell-cannot-leave-input-list
  74. '(beginning-of-line-text
  75. beginning-of-line
  76. move-to-column
  77. move-to-left-margin
  78. move-to-tab-stop
  79. forward-char
  80. backward-char
  81. delete-char
  82. delete-backward-char
  83. backward-delete-char
  84. backward-delete-char-untabify
  85. kill-paragraph
  86. backward-kill-paragraph
  87. kill-sentence
  88. backward-kill-sentence
  89. kill-sexp
  90. backward-kill-sexp
  91. kill-word
  92. backward-kill-word
  93. kill-region
  94. forward-list
  95. backward-list
  96. forward-page
  97. backward-page
  98. forward-point
  99. forward-paragraph
  100. backward-paragraph
  101. backward-prefix-chars
  102. forward-sentence
  103. backward-sentence
  104. forward-sexp
  105. backward-sexp
  106. forward-to-indentation
  107. backward-to-indentation
  108. backward-up-list
  109. forward-word
  110. backward-word
  111. forward-line
  112. previous-line
  113. next-line
  114. forward-visible-line
  115. forward-comment
  116. forward-thing)
  117. "A list of commands that cannot leave the input area."
  118. :type '(repeat function)
  119. :group 'eshell-rebind)
  120. ;; Internal Variables:
  121. (defvar eshell-input-keymap)
  122. (defvar eshell-previous-point)
  123. (defvar eshell-lock-keymap)
  124. ;;; Functions:
  125. (defun eshell-rebind-initialize ()
  126. "Initialize the inputting code."
  127. (unless eshell-non-interactive-p
  128. (add-hook 'eshell-mode-hook 'eshell-setup-input-keymap nil t)
  129. (make-local-variable 'eshell-previous-point)
  130. (add-hook 'pre-command-hook 'eshell-save-previous-point nil t)
  131. (make-local-variable 'overriding-local-map)
  132. (add-hook 'post-command-hook 'eshell-rebind-input-map nil t)
  133. (set (make-local-variable 'eshell-lock-keymap) nil)
  134. (define-key eshell-command-map [(meta ?l)] 'eshell-lock-local-map)))
  135. (defun eshell-lock-local-map (&optional arg)
  136. "Lock or unlock the current local keymap.
  137. Within a prefix arg, set the local keymap to its normal value, and
  138. lock it at that."
  139. (interactive "P")
  140. (if (or arg (not eshell-lock-keymap))
  141. (progn
  142. (use-local-map eshell-mode-map)
  143. (setq eshell-lock-keymap t)
  144. (message "Local keymap locked in normal mode"))
  145. (use-local-map eshell-input-keymap)
  146. (setq eshell-lock-keymap nil)
  147. (message "Local keymap unlocked: obey context")))
  148. (defun eshell-save-previous-point ()
  149. "Save the location of point before the next command is run."
  150. (setq eshell-previous-point (point)))
  151. (defsubst eshell-point-within-input-p (pos)
  152. "Test whether POS is within an input range."
  153. (let (begin)
  154. (or (and (>= pos eshell-last-output-end)
  155. eshell-last-output-end)
  156. (and eshell-remap-previous-input
  157. (setq begin
  158. (save-excursion
  159. (eshell-bol)
  160. (and (not (bolp)) (point))))
  161. (>= pos begin)
  162. (<= pos (line-end-position))
  163. begin))))
  164. (defun eshell-rebind-input-map ()
  165. "Rebind the input keymap based on the location of the cursor."
  166. (ignore-errors
  167. (unless eshell-lock-keymap
  168. (if (eshell-point-within-input-p (point))
  169. (use-local-map eshell-input-keymap)
  170. (let (begin)
  171. (if (and eshell-confine-point-to-input
  172. (setq begin
  173. (eshell-point-within-input-p eshell-previous-point))
  174. (memq this-command eshell-cannot-leave-input-list))
  175. (progn
  176. (use-local-map eshell-input-keymap)
  177. (goto-char begin)
  178. (if (and eshell-error-if-move-away
  179. (not (eq this-command 'kill-region)))
  180. (beep)))
  181. (use-local-map eshell-mode-map)))))))
  182. (defun eshell-setup-input-keymap ()
  183. "Setup the input keymap to be used during input editing."
  184. (make-local-variable 'eshell-input-keymap)
  185. (setq eshell-input-keymap (make-sparse-keymap))
  186. (set-keymap-parent eshell-input-keymap eshell-mode-map)
  187. (let ((bindings eshell-rebind-keys-alist))
  188. (while bindings
  189. (define-key eshell-input-keymap (caar bindings)
  190. (cdar bindings))
  191. (setq bindings (cdr bindings)))))
  192. (defun eshell-delete-backward-char (n &optional killflag)
  193. "Delete the last character, unless it's part of the output."
  194. (interactive "P")
  195. (let ((count (prefix-numeric-value n)))
  196. (if (eshell-point-within-input-p (- (point) count))
  197. (delete-backward-char count n)
  198. (beep))))
  199. (defun eshell-delchar-or-maybe-eof (arg)
  200. "Delete ARG characters forward or send an EOF to subprocess.
  201. Sends an EOF only if point is at the end of the buffer and there is no
  202. input."
  203. (interactive "p")
  204. (let ((proc (eshell-interactive-process)))
  205. (if (eobp)
  206. (cond
  207. ((/= (point) eshell-last-output-end)
  208. (beep))
  209. (proc
  210. (process-send-eof))
  211. (t
  212. (eshell-life-is-too-much)))
  213. (eshell-delete-backward-char (- arg)))))
  214. (provide 'em-rebind)
  215. ;; Local Variables:
  216. ;; generated-autoload-file: "esh-groups.el"
  217. ;; End:
  218. ;;; em-rebind.el ends here