em-rebind.el 8.0 KB

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