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