crisp.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. ;;; crisp.el --- CRiSP/Brief Emacs emulator
  2. ;; Copyright (C) 1997-1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Gary D. Foster <Gary.Foster@Corp.Sun.COM>
  4. ;; Keywords: emulations brief crisp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Keybindings and minor functions to duplicate the functionality and
  18. ;; finger-feel of the CRiSP/Brief editor. This package is designed to
  19. ;; facilitate transitioning from Brief to (XE|E)macs with a minimum
  20. ;; amount of hassles.
  21. ;; Enable this package by putting (require 'crisp) in your .emacs and
  22. ;; use M-x crisp-mode to toggle it on or off.
  23. ;; This package will automatically load the scroll-all.el package if
  24. ;; you put (setq crisp-load-scroll-all t) in your .emacs before
  25. ;; loading this package. If this feature is enabled, it will bind
  26. ;; meta-f1 to the scroll-all mode toggle. The scroll-all package
  27. ;; duplicates the scroll-all feature in CRiSP.
  28. ;; Also, the default keybindings for brief/CRiSP override the M-x
  29. ;; key to exit the editor. If you don't like this functionality, you
  30. ;; can prevent this behavior (or redefine it dynamically) by setting
  31. ;; the value of `crisp-override-meta-x' either in your .emacs or
  32. ;; interactively. The default setting is t, which means that M-x will
  33. ;; by default run `save-buffers-kill-emacs' instead of the command
  34. ;; `execute-extended-command'.
  35. ;; Finally, if you want to change the string displayed in the modeline
  36. ;; when this mode is in effect, override the definition of
  37. ;; `crisp-mode-modeline-string' in your .emacs. The default value is
  38. ;; " *Crisp*" which may be a bit lengthy if you have a lot of things
  39. ;; being displayed there.
  40. ;; All these overrides should go *before* the (require 'crisp) statement.
  41. ;;; Code:
  42. (eval-when-compile (require 'cl))
  43. ;; local variables
  44. (defgroup crisp nil
  45. "Emulator for CRiSP and Brief key bindings."
  46. :prefix "crisp-"
  47. :group 'emulations)
  48. (defvar crisp-mode-map
  49. (let ((map (make-sparse-keymap)))
  50. (define-key map [(f1)] 'other-window)
  51. (define-key map [(f2) (down)] 'enlarge-window)
  52. (define-key map [(f2) (left)] 'shrink-window-horizontally)
  53. (define-key map [(f2) (right)] 'enlarge-window-horizontally)
  54. (define-key map [(f2) (up)] 'shrink-window)
  55. (define-key map [(f3) (down)] 'split-window-below)
  56. (define-key map [(f3) (right)] 'split-window-right)
  57. (define-key map [(f4)] 'delete-window)
  58. (define-key map [(control f4)] 'delete-other-windows)
  59. (define-key map [(f5)] 'search-forward-regexp)
  60. (define-key map [(f19)] 'search-forward-regexp)
  61. (define-key map [(meta f5)] 'search-backward-regexp)
  62. (define-key map [(f6)] 'query-replace)
  63. (define-key map [(f7)] 'start-kbd-macro)
  64. (define-key map [(meta f7)] 'end-kbd-macro)
  65. (define-key map [(f8)] 'call-last-kbd-macro)
  66. (define-key map [(meta f8)] 'save-kbd-macro)
  67. (define-key map [(f9)] 'find-file)
  68. (define-key map [(meta f9)] 'load-library)
  69. (define-key map [(f10)] 'execute-extended-command)
  70. (define-key map [(meta f10)] 'compile)
  71. (define-key map [(SunF37)] 'kill-buffer)
  72. (define-key map [(kp-add)] 'crisp-copy-line)
  73. (define-key map [(kp-subtract)] 'crisp-kill-line)
  74. ;; just to cover all the bases (GNU Emacs, for instance)
  75. (define-key map [(f24)] 'crisp-kill-line)
  76. (define-key map [(insert)] 'crisp-yank-clipboard)
  77. (define-key map [(f16)] 'crisp-set-clipboard) ; copy on Sun5 kbd
  78. (define-key map [(f20)] 'crisp-kill-region) ; cut on Sun5 kbd
  79. (define-key map [(f18)] 'crisp-yank-clipboard) ; paste on Sun5 kbd
  80. (define-key map [(control f)] 'fill-paragraph-or-region)
  81. (define-key map [(meta d)] (lambda ()
  82. (interactive)
  83. (beginning-of-line) (kill-line)))
  84. (define-key map [(meta e)] 'find-file)
  85. (define-key map [(meta g)] 'goto-line)
  86. (define-key map [(meta h)] 'help)
  87. (define-key map [(meta i)] 'overwrite-mode)
  88. (define-key map [(meta j)] 'bookmark-jump)
  89. (define-key map [(meta l)] 'crisp-mark-line)
  90. (define-key map [(meta m)] 'set-mark-command)
  91. (define-key map [(meta n)] 'bury-buffer)
  92. (define-key map [(meta p)] 'crisp-unbury-buffer)
  93. (define-key map [(meta u)] 'undo)
  94. (define-key map [(f14)] 'undo)
  95. (define-key map [(meta w)] 'save-buffer)
  96. (define-key map [(meta x)] 'crisp-meta-x-wrapper)
  97. (define-key map [(meta ?0)] (lambda ()
  98. (interactive)
  99. (bookmark-set "0")))
  100. (define-key map [(meta ?1)] (lambda ()
  101. (interactive)
  102. (bookmark-set "1")))
  103. (define-key map [(meta ?2)] (lambda ()
  104. (interactive)
  105. (bookmark-set "2")))
  106. (define-key map [(meta ?3)] (lambda ()
  107. (interactive)
  108. (bookmark-set "3")))
  109. (define-key map [(meta ?4)] (lambda ()
  110. (interactive)
  111. (bookmark-set "4")))
  112. (define-key map [(meta ?5)] (lambda ()
  113. (interactive)
  114. (bookmark-set "5")))
  115. (define-key map [(meta ?6)] (lambda ()
  116. (interactive)
  117. (bookmark-set "6")))
  118. (define-key map [(meta ?7)] (lambda ()
  119. (interactive)
  120. (bookmark-set "7")))
  121. (define-key map [(meta ?8)] (lambda ()
  122. (interactive)
  123. (bookmark-set "8")))
  124. (define-key map [(meta ?9)] (lambda ()
  125. (interactive)
  126. (bookmark-set "9")))
  127. (define-key map [(shift delete)] 'kill-word)
  128. (define-key map [(shift backspace)] 'backward-kill-word)
  129. (define-key map [(control left)] 'backward-word)
  130. (define-key map [(control right)] 'forward-word)
  131. (define-key map [(home)] 'crisp-home)
  132. (define-key map [(control home)] (lambda ()
  133. (interactive)
  134. (move-to-window-line 0)))
  135. (define-key map [(meta home)] 'beginning-of-line)
  136. (define-key map [(end)] 'crisp-end)
  137. (define-key map [(control end)] (lambda ()
  138. (interactive)
  139. (move-to-window-line -1)))
  140. (define-key map [(meta end)] 'end-of-line)
  141. map)
  142. "Local keymap for CRiSP emulation mode.
  143. All the bindings are done here instead of globally to try and be
  144. nice to the world.")
  145. (defcustom crisp-mode-modeline-string " *CRiSP*"
  146. "String to display in the modeline when CRiSP emulation mode is enabled."
  147. :type 'string
  148. :group 'crisp)
  149. ;;;###autoload
  150. (defcustom crisp-mode nil
  151. "Track status of CRiSP emulation mode.
  152. A value of nil means CRiSP mode is not enabled. A value of t
  153. indicates CRiSP mode is enabled.
  154. Setting this variable directly does not take effect;
  155. use either M-x customize or the function `crisp-mode'."
  156. :set (lambda (symbol value) (crisp-mode (if value 1 0)))
  157. :initialize 'custom-initialize-default
  158. :require 'crisp
  159. :version "20.4"
  160. :type 'boolean
  161. :group 'crisp)
  162. (defcustom crisp-override-meta-x t
  163. "Controls overriding the normal Emacs M-x key binding in the CRiSP emulator.
  164. Normally the CRiSP emulator rebinds M-x to `save-buffers-exit-emacs', and
  165. provides the usual M-x functionality on the F10 key. If this variable
  166. is non-nil, M-x will exit Emacs."
  167. :type 'boolean
  168. :group 'crisp)
  169. (defcustom crisp-load-scroll-all nil
  170. "Controls loading of the Scroll Lock in the CRiSP emulator.
  171. Its default behavior is to load and enable the Scroll Lock minor mode
  172. package when enabling the CRiSP emulator.
  173. If this variable is nil when you start the CRiSP emulator, it
  174. does not load the scroll-all package."
  175. :type 'boolean
  176. :group 'crisp)
  177. (defcustom crisp-load-hook nil
  178. "Hooks to run after loading the CRiSP emulator package."
  179. :type 'hook
  180. :group 'crisp)
  181. (defcustom crisp-mode-hook nil
  182. "Hook run by the function `crisp-mode'."
  183. :type 'hook
  184. :group 'crisp)
  185. (defconst crisp-version "1.34"
  186. "The version of the CRiSP emulator.")
  187. (defconst crisp-mode-help-address "gfoster@suzieq.ml.org"
  188. "The email address of the CRiSP mode author/maintainer.")
  189. ;; Silence the byte-compiler.
  190. (defvar crisp-last-last-command nil
  191. "The previous value of `last-command'.")
  192. ;; The cut and paste routines are different between XEmacs and Emacs
  193. ;; so we need to set up aliases for the functions.
  194. (defalias 'crisp-set-clipboard
  195. (if (fboundp 'clipboard-kill-ring-save)
  196. 'clipboard-kill-ring-save
  197. 'copy-primary-selection))
  198. (defalias 'crisp-kill-region
  199. (if (fboundp 'clipboard-kill-region)
  200. 'clipboard-kill-region
  201. 'kill-primary-selection))
  202. (defalias 'crisp-yank-clipboard
  203. (if (fboundp 'clipboard-yank)
  204. 'clipboard-yank
  205. 'yank-clipboard-selection))
  206. (defun crisp-region-active ()
  207. "Compatibility function to test for an active region."
  208. (if (featurep 'xemacs)
  209. zmacs-region-active-p
  210. mark-active))
  211. (defun crisp-version (&optional arg)
  212. "Version number of the CRiSP emulator package.
  213. If ARG, insert results at point."
  214. (interactive "P")
  215. (let ((foo (concat "CRiSP version " crisp-version)))
  216. (if arg
  217. (insert (message foo))
  218. (message foo))))
  219. (defun crisp-mark-line (arg)
  220. "Set mark at the end of the line.
  221. Arg works as in `end-of-line'."
  222. (interactive "p")
  223. (let (newmark)
  224. (save-excursion
  225. (end-of-line arg)
  226. (setq newmark (point)))
  227. (push-mark newmark nil t)))
  228. (defun crisp-kill-line (arg)
  229. "Mark and kill line(s).
  230. Marks from point to end of the current line (honoring prefix arguments),
  231. copies the region to the kill ring and clipboard, and then deletes it."
  232. (interactive "*p")
  233. (if (crisp-region-active)
  234. (call-interactively 'crisp-kill-region)
  235. (crisp-mark-line arg)
  236. (call-interactively 'crisp-kill-region)))
  237. (defun crisp-copy-line (arg)
  238. "Mark and copy line(s).
  239. Marks from point to end of the current line (honoring prefix arguments),
  240. copies the region to the kill ring and clipboard, and then deactivates
  241. the region."
  242. (interactive "*p")
  243. (if (crisp-region-active)
  244. (call-interactively 'crisp-set-clipboard)
  245. (crisp-mark-line arg)
  246. (call-interactively 'crisp-set-clipboard))
  247. ;; clear the region after the operation is complete
  248. ;; XEmacs does this automagically, Emacs doesn't.
  249. (if (boundp 'mark-active)
  250. (setq mark-active nil)))
  251. (defun crisp-home ()
  252. "\"Home\" the point, the way CRiSP would do it.
  253. The first use moves point to beginning of the line. Second
  254. consecutive use moves point to beginning of the screen. Third
  255. consecutive use moves point to the beginning of the buffer."
  256. (interactive nil)
  257. (cond
  258. ((and (eq last-command 'crisp-home)
  259. (eq crisp-last-last-command 'crisp-home))
  260. (goto-char (point-min)))
  261. ((eq last-command 'crisp-home)
  262. (move-to-window-line 0))
  263. (t
  264. (beginning-of-line)))
  265. (setq crisp-last-last-command last-command))
  266. (defun crisp-end ()
  267. "\"End\" the point, the way CRiSP would do it.
  268. The first use moves point to end of the line. Second
  269. consecutive use moves point to the end of the screen. Third
  270. consecutive use moves point to the end of the buffer."
  271. (interactive nil)
  272. (cond
  273. ((and (eq last-command 'crisp-end)
  274. (eq crisp-last-last-command 'crisp-end))
  275. (goto-char (point-max)))
  276. ((eq last-command 'crisp-end)
  277. (move-to-window-line -1)
  278. (end-of-line))
  279. (t
  280. (end-of-line)))
  281. (setq crisp-last-last-command last-command))
  282. (defun crisp-unbury-buffer ()
  283. "Go back one buffer."
  284. (interactive)
  285. (switch-to-buffer (car (last (buffer-list)))))
  286. (defun crisp-meta-x-wrapper ()
  287. "Wrapper function to conditionally override the normal M-x bindings.
  288. When `crisp-override-meta-x' is non-nil, M-x will exit Emacs (the
  289. normal CRiSP binding) and when it is nil M-x will run
  290. `execute-extended-command' (the normal Emacs binding)."
  291. (interactive)
  292. (if crisp-override-meta-x
  293. (save-buffers-kill-emacs)
  294. (call-interactively 'execute-extended-command)))
  295. ;;;###autoload
  296. (define-minor-mode crisp-mode
  297. "Toggle CRiSP/Brief emulation (CRiSP mode).
  298. With a prefix argument ARG, enable CRiSP mode if ARG is positive,
  299. and disable it otherwise. If called from Lisp, enable the mode
  300. if ARG is omitted or nil."
  301. :keymap crisp-mode-map
  302. :lighter crisp-mode-modeline-string
  303. (when crisp-mode
  304. ;; Make menu entries show M-u or f14 in preference to C-x u.
  305. (put 'undo :advertised-binding
  306. (list* [?\M-u] [f14] (get 'undo :advertised-binding)))
  307. ;; Force transient-mark-mode, so that the marking routines work as
  308. ;; expected. If the user turns off transient mark mode, most
  309. ;; things will still work fine except the crisp-(copy|kill)
  310. ;; functions won't work quite as nicely when regions are marked
  311. ;; differently and could really confuse people. Caveat emptor.
  312. (if (fboundp 'transient-mark-mode)
  313. (transient-mark-mode t))
  314. (if crisp-load-scroll-all
  315. (require 'scroll-all))
  316. (if (featurep 'scroll-all)
  317. (define-key crisp-mode-map [(meta f1)] 'scroll-all-mode))))
  318. ;; People might use Apropos on `brief'.
  319. ;;;###autoload
  320. (defalias 'brief-mode 'crisp-mode)
  321. ;; Interaction with other packages.
  322. (put 'crisp-home 'CUA 'move)
  323. (put 'crisp-end 'CUA 'move)
  324. (run-hooks 'crisp-load-hook)
  325. (provide 'crisp)
  326. ;;; crisp.el ends here