(auto-mode-alist): Add uppercase version of archive
[bpt/emacs.git] / lisp / emulation / pc-select.el
1 ;;; pc-select.el --- emulate mark, cut, copy and paste from motif
2 ;;; (or MAC GUI) or MS-windoze (bah)) look-and-feel
3 ;;; including key bindings
4
5 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
6
7 ;; Author: Michael Staats <michael@thp.Uni-Duisburg.DE>
8 ;; Created: 26 Sep 1995
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This package emulates the mark, copy, cut and paste look-and-feel of motif
30 ;; programs (which is the same as the MAC gui and (sorry for that) MS-Windows).
31 ;; It modifies the keybindings of the cursor keys and the next, prior,
32 ;; home and end keys. They will modify mark-active.
33 ;; You can still get the old behaviour of cursor moving with the
34 ;; control sequences C-f, C-b, etc.
35 ;; This package uses transient-mark-mode and
36 ;; delete-selection-mode.
37 ;;
38 ;; In addition to that all key-bindings from the pc-mode are
39 ;; done here too (as suggested by RMS).
40 ;;
41 ;; As I found out after I finished the first version, s-region.el tries
42 ;; to do the same.... But my code is a little more complete and using
43 ;; delete-selection-mode is very important for the look-and-feel.
44 ;; Pete Forman <pete.forman@airgun.wg.waii.com> provided some motif
45 ;; compliant keybindings which I added. I had to modify them a little
46 ;; to add the -mark and -nomark functionality of cursor moving.
47 ;;
48 ;; Credits:
49 ;; Many thanks to all who made comments.
50 ;; Thanks to RMS and Ralf Muschall <prm@rz.uni-jena.de> for criticism.
51 ;; Kevin Cutts <cutts@ukraine.corp.mot.com> added the beginning-of-buffer
52 ;; and end-of-buffer functions which I modified a little.
53 ;; David Biesack <sasdjb@unx.sas.com> suggested some more cleanup.
54 ;; Thanks to Pete Forman <pete.forman@airgun.wg.waii.com>
55 ;; for additional motif keybindings.
56 ;; Thanks to jvromans@squirrel.nl (Johan Vromans) for a bug report
57 ;; concerning setting of this-command.
58 ;;
59 ;;
60 ;; Ok, some details about the idea of pc-selection-mode:
61 ;;
62 ;; o The standard keys for moving around (right, left, up, down, home, end,
63 ;; prior, next, called "move-keys" from now on) will always de-activate
64 ;; the mark.
65 ;; o If you press "Shift" together with the "move-keys", the region
66 ;; you pass along is activated
67 ;; o You have the copy, cut and paste functions (as in many other programs)
68 ;; which will operate on the active region
69 ;; It was not possible to bind them to C-v, C-x and C-c for obvious
70 ;; emacs reasons.
71 ;; They will be bound according to the "old" behaviour to S-delete (cut),
72 ;; S-insert (paste) and C-insert (copy). These keys do the same in many
73 ;; other programs.
74
75 ;;; Code:
76
77 ;;;;
78 ;; misc
79 ;;;;
80
81 (provide 'pc-select)
82
83 (defun copy-region-as-kill-nomark (beg end)
84 "Save the region as if killed; but don't kill it; deactivate mark.
85 If `interprogram-cut-function' is non-nil, also save the text for a window
86 system cut and paste.\n
87 Deactivating mark is to avoid confusion with delete-selection-mode
88 and transient-mark-mode."
89 (interactive "r")
90 (copy-region-as-kill beg end)
91 (setq mark-active nil)
92 (message "Region saved"))
93
94 ;;;;
95 ;; non-interactive
96 ;;;;
97 (defun ensure-mark()
98 ;; make sure mark is active
99 ;; test if it is active, if it isn't, set it and activate it
100 (and (not mark-active) (set-mark-command nil)))
101
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 ;;;;; forward and mark
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
105
106 (defun forward-char-mark (&optional arg)
107 "Ensure mark is active; move point right ARG characters (left if ARG negative).
108 On reaching end of buffer, stop and signal error."
109 (interactive "p")
110 (ensure-mark)
111 (forward-char arg))
112
113 (defun forward-word-mark (&optional arg)
114 "Ensure mark is active; move point right ARG words (backward if ARG is negative).
115 Normally returns t.
116 If an edge of the buffer is reached, point is left there
117 and nil is returned."
118 (interactive "p")
119 (ensure-mark)
120 (forward-word arg))
121
122 (defun forward-paragraph-mark (&optional arg)
123 "Ensure mark is active; move forward to end of paragraph.
124 With arg N, do it N times; negative arg -N means move backward N paragraphs.\n
125 A line which `paragraph-start' matches either separates paragraphs
126 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
127 A paragraph end is the beginning of a line which is not part of the paragraph
128 to which the end of the previous line belongs, or the end of the buffer."
129 (interactive "p")
130 (ensure-mark)
131 (forward-paragraph arg))
132
133 (defun next-line-mark (&optional arg)
134 "Ensure mark is active; move cursor vertically down ARG lines.
135 If there is no character in the target line exactly under the current column,
136 the cursor is positioned after the character in that line which spans this
137 column, or at the end of the line if it is not long enough.
138 If there is no line in the buffer after this one, behavior depends on the
139 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
140 to create a line, and moves the cursor to that line. Otherwise it moves the
141 cursor to the end of the buffer \(if already at the end of the buffer, an error
142 is signaled).\n
143 The command C-x C-n can be used to create
144 a semipermanent goal column to which this command always moves.
145 Then it does not try to move vertically. This goal column is stored
146 in `goal-column', which is nil when there is none."
147 (interactive "p")
148 (ensure-mark)
149 (next-line arg)
150 (setq this-command 'next-line))
151
152 (defun end-of-line-mark (&optional arg)
153 "Ensure mark is active; move point to end of current line.
154 With argument ARG not nil or 1, move forward ARG - 1 lines first.
155 If scan reaches end of buffer, stop there without error."
156 (interactive "p")
157 (ensure-mark)
158 (end-of-line arg)
159 (setq this-command 'end-of-line))
160
161 (defun scroll-down-mark (&optional arg)
162 "Ensure mark is active; scroll down ARG lines; or near full screen if no ARG.
163 A near full screen is `next-screen-context-lines' less than a full screen.
164 Negative ARG means scroll upward.
165 When calling from a program, supply a number as argument or nil."
166 (interactive "P")
167 (ensure-mark)
168 (scroll-down arg))
169
170 (defun end-of-buffer-mark (&optional arg)
171 "Ensure mark is active; move point to the end of the buffer.
172 With arg N, put point N/10 of the way from the end.\n
173 If the buffer is narrowed, this command uses the beginning and size
174 of the accessible part of the buffer.\n
175 Don't use this command in Lisp programs!
176 \(goto-char \(point-max)) is faster and avoids clobbering the mark."
177 (interactive "P")
178 (ensure-mark)
179 (let ((size (- (point-max) (point-min))))
180 (goto-char (if arg
181 (- (point-max)
182 (if (> size 10000)
183 ;; Avoid overflow for large buffer sizes!
184 (* (prefix-numeric-value arg)
185 (/ size 10))
186 (/ (* size (prefix-numeric-value arg)) 10)))
187 (point-max))))
188 ;; If we went to a place in the middle of the buffer,
189 ;; adjust it to the beginning of a line.
190 (if arg (forward-line 1)
191 ;; If the end of the buffer is not already on the screen,
192 ;; then scroll specially to put it near, but not at, the bottom.
193 (if (let ((old-point (point)))
194 (save-excursion
195 (goto-char (window-start))
196 (vertical-motion (window-height))
197 (< (point) old-point)))
198 (progn
199 (overlay-recenter (point))
200 (recenter -3)))))
201
202 ;;;;;;;;;
203 ;;;;; no mark
204 ;;;;;;;;;
205
206 (defun forward-char-nomark (&optional arg)
207 "Deactivate mark; move point right ARG characters \(left if ARG negative).
208 On reaching end of buffer, stop and signal error."
209 (interactive "p")
210 (setq mark-active nil)
211 (forward-char arg))
212
213 (defun forward-word-nomark (&optional arg)
214 "Deactivate mark; move point right ARG words \(backward if ARG is negative).
215 Normally returns t.
216 If an edge of the buffer is reached, point is left there
217 and nil is returned."
218 (interactive "p")
219 (setq mark-active nil)
220 (forward-word arg))
221
222 (defun forward-paragraph-nomark (&optional arg)
223 "Deactivate mark; move forward to end of paragraph.
224 With arg N, do it N times; negative arg -N means move backward N paragraphs.\n
225 A line which `paragraph-start' matches either separates paragraphs
226 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
227 A paragraph end is the beginning of a line which is not part of the paragraph
228 to which the end of the previous line belongs, or the end of the buffer."
229 (interactive "p")
230 (setq mark-active nil)
231 (forward-paragraph arg))
232
233 (defun next-line-nomark (&optional arg)
234 "Deactivate mark; move cursor vertically down ARG lines.
235 If there is no character in the target line exactly under the current column,
236 the cursor is positioned after the character in that line which spans this
237 column, or at the end of the line if it is not long enough.
238 If there is no line in the buffer after this one, behavior depends on the
239 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
240 to create a line, and moves the cursor to that line. Otherwise it moves the
241 cursor to the end of the buffer (if already at the end of the buffer, an error
242 is signaled).\n
243 The command C-x C-n can be used to create
244 a semipermanent goal column to which this command always moves.
245 Then it does not try to move vertically. This goal column is stored
246 in `goal-column', which is nil when there is none."
247 (interactive "p")
248 (setq mark-active nil)
249 (next-line arg)
250 (setq this-command 'next-line))
251
252 (defun end-of-line-nomark (&optional arg)
253 "Deactivate mark; move point to end of current line.
254 With argument ARG not nil or 1, move forward ARG - 1 lines first.
255 If scan reaches end of buffer, stop there without error."
256 (interactive "p")
257 (setq mark-active nil)
258 (end-of-line arg)
259 (setq this-command 'end-of-line))
260
261 (defun scroll-down-nomark (&optional arg)
262 "Deactivate mark; scroll down ARG lines; or near full screen if no ARG.
263 A near full screen is `next-screen-context-lines' less than a full screen.
264 Negative ARG means scroll upward.
265 When calling from a program, supply a number as argument or nil."
266 (interactive "P")
267 (setq mark-active nil)
268 (scroll-down arg))
269
270 (defun end-of-buffer-nomark (&optional arg)
271 "Deactivate mark; move point to the end of the buffer.
272 With arg N, put point N/10 of the way from the end.\n
273 If the buffer is narrowed, this command uses the beginning and size
274 of the accessible part of the buffer.\n
275 Don't use this command in Lisp programs!
276 \(goto-char (point-max)) is faster and avoids clobbering the mark."
277 (interactive "P")
278 (setq mark-active nil)
279 (let ((size (- (point-max) (point-min))))
280 (goto-char (if arg
281 (- (point-max)
282 (if (> size 10000)
283 ;; Avoid overflow for large buffer sizes!
284 (* (prefix-numeric-value arg)
285 (/ size 10))
286 (/ (* size (prefix-numeric-value arg)) 10)))
287 (point-max))))
288 ;; If we went to a place in the middle of the buffer,
289 ;; adjust it to the beginning of a line.
290 (if arg (forward-line 1)
291 ;; If the end of the buffer is not already on the screen,
292 ;; then scroll specially to put it near, but not at, the bottom.
293 (if (let ((old-point (point)))
294 (save-excursion
295 (goto-char (window-start))
296 (vertical-motion (window-height))
297 (< (point) old-point)))
298 (progn
299 (overlay-recenter (point))
300 (recenter -3)))))
301
302
303 ;;;;;;;;;;;;;;;;;;;;
304 ;;;;;; backwards and mark
305 ;;;;;;;;;;;;;;;;;;;;
306
307 (defun backward-char-mark (&optional arg)
308 "Ensure mark is active; move point left ARG characters (right if ARG negative).
309 On attempt to pass beginning or end of buffer, stop and signal error."
310 (interactive "p")
311 (ensure-mark)
312 (backward-char arg))
313
314 (defun backward-word-mark (&optional arg)
315 "Ensure mark is active; move backward until encountering the end of a word.
316 With argument, do this that many times."
317 (interactive "p")
318 (ensure-mark)
319 (backward-word arg))
320
321 (defun backward-paragraph-mark (&optional arg)
322 "Ensure mark is active; move backward to start of paragraph.
323 With arg N, do it N times; negative arg -N means move forward N paragraphs.\n
324 A paragraph start is the beginning of a line which is a
325 `first-line-of-paragraph' or which is ordinary text and follows a
326 paragraph-separating line; except: if the first real line of a
327 paragraph is preceded by a blank line, the paragraph starts at that
328 blank line.\n
329 See `forward-paragraph' for more information."
330 (interactive "p")
331 (ensure-mark)
332 (backward-paragraph arg))
333
334 (defun previous-line-mark (&optional arg)
335 "Ensure mark is active; move cursor vertically up ARG lines.
336 If there is no character in the target line exactly over the current column,
337 the cursor is positioned after the character in that line which spans this
338 column, or at the end of the line if it is not long enough.\n
339 The command C-x C-n can be used to create
340 a semipermanent goal column to which this command always moves.
341 Then it does not try to move vertically.\n
342 If you are thinking of using this in a Lisp program, consider using
343 `forward-line' with a negative argument instead. It is usually easier
344 to use and more reliable (no dependence on goal column, etc.)."
345 (interactive "p")
346 (ensure-mark)
347 (previous-line arg)
348 (setq this-command 'previous-line))
349
350 (defun beginning-of-line-mark (&optional arg)
351 "Ensure mark is active; move point to beginning of current line.
352 With argument ARG not nil or 1, move forward ARG - 1 lines first.
353 If scan reaches end of buffer, stop there without error."
354 (interactive "p")
355 (ensure-mark)
356 (beginning-of-line arg))
357
358
359 (defun scroll-up-mark (&optional arg)
360 "Ensure mark is active; scroll upward ARG lines; or near full screen if no ARG.
361 A near full screen is `next-screen-context-lines' less than a full screen.
362 Negative ARG means scroll downward.
363 When calling from a program, supply a number as argument or nil."
364 (interactive "P")
365 (ensure-mark)
366 (scroll-up arg))
367
368 (defun beginning-of-buffer-mark (&optional arg)
369 "Ensure mark is active; move point to the beginning of the buffer.
370 With arg N, put point N/10 of the way from the beginning.\n
371 If the buffer is narrowed, this command uses the beginning and size
372 of the accessible part of the buffer.\n
373 Don't use this command in Lisp programs!
374 \(goto-char (p\oint-min)) is faster and avoids clobbering the mark."
375 (interactive "P")
376 (ensure-mark)
377 (let ((size (- (point-max) (point-min))))
378 (goto-char (if arg
379 (+ (point-min)
380 (if (> size 10000)
381 ;; Avoid overflow for large buffer sizes!
382 (* (prefix-numeric-value arg)
383 (/ size 10))
384 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
385 (point-min))))
386 (if arg (forward-line 1)))
387
388 ;;;;;;;;
389 ;;; no mark
390 ;;;;;;;;
391
392 (defun backward-char-nomark (&optional arg)
393 "Deactivate mark; move point left ARG characters (right if ARG negative).
394 On attempt to pass beginning or end of buffer, stop and signal error."
395 (interactive "p")
396 (setq mark-active nil)
397 (backward-char arg))
398
399 (defun backward-word-nomark (&optional arg)
400 "Deactivate mark; move backward until encountering the end of a word.
401 With argument, do this that many times."
402 (interactive "p")
403 (setq mark-active nil)
404 (backward-word arg))
405
406 (defun backward-paragraph-nomark (&optional arg)
407 "Deactivate mark; move backward to start of paragraph.
408 With arg N, do it N times; negative arg -N means move forward N paragraphs.\n
409 A paragraph start is the beginning of a line which is a
410 `first-line-of-paragraph' or which is ordinary text and follows a
411 paragraph-separating line; except: if the first real line of a
412 paragraph is preceded by a blank line, the paragraph starts at that
413 blank line.\n
414 See `forward-paragraph' for more information."
415 (interactive "p")
416 (setq mark-active nil)
417 (backward-paragraph arg))
418
419 (defun previous-line-nomark (&optional arg)
420 "Deactivate mark; move cursor vertically up ARG lines.
421 If there is no character in the target line exactly over the current column,
422 the cursor is positioned after the character in that line which spans this
423 column, or at the end of the line if it is not long enough.\n
424 The command C-x C-n can be used to create
425 a semipermanent goal column to which this command always moves.
426 Then it does not try to move vertically."
427 (interactive "p")
428 (setq mark-active nil)
429 (previous-line arg)
430 (setq this-command 'previous-line))
431
432 (defun beginning-of-line-nomark (&optional arg)
433 "Deactivate mark; move point to beginning of current line.
434 With argument ARG not nil or 1, move forward ARG - 1 lines first.
435 If scan reaches end of buffer, stop there without error."
436 (interactive "p")
437 (setq mark-active nil)
438 (beginning-of-line arg))
439
440 (defun scroll-up-nomark (&optional arg)
441 "Deactivate mark; scroll upward ARG lines; or near full screen if no ARG.
442 A near full screen is `next-screen-context-lines' less than a full screen.
443 Negative ARG means scroll downward.
444 When calling from a program, supply a number as argument or nil."
445 (interactive "P")
446 (setq mark-active nil)
447 (scroll-up arg))
448
449 (defun beginning-of-buffer-nomark (&optional arg)
450 "Deactivate mark; move point to the beginning of the buffer.
451 With arg N, put point N/10 of the way from the beginning.\n
452 If the buffer is narrowed, this command uses the beginning and size
453 of the accessible part of the buffer.\n
454 Don't use this command in Lisp programs!
455 \(goto-char (point-min)) is faster and avoids clobbering the mark."
456 (interactive "P")
457 (setq mark-active nil)
458 (let ((size (- (point-max) (point-min))))
459 (goto-char (if arg
460 (+ (point-min)
461 (if (> size 10000)
462 ;; Avoid overflow for large buffer sizes!
463 (* (prefix-numeric-value arg)
464 (/ size 10))
465 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
466 (point-min))))
467 (if arg (forward-line 1)))
468
469 ;;;###autoload
470 (defun pc-selection-mode ()
471 "Change mark behaviour to emulate motif, MAC or MS-Windows cut and paste style.\n
472 This mode will switch on delete-selection-mode and
473 transient-mark-mode.\n
474 The cursor keys (and others) are bound to new functions
475 which will modify the status of the mark. It will be
476 possible to select regions with shift-cursorkeys. All this
477 tries to emulate the look-and-feel of GUIs like motif,
478 the MAC GUI or MS-Windows (sorry for the last one)."
479 (interactive)
480 ;;
481 ;; keybindings
482 ;;
483
484 ;; This is to avoid confusion with the delete-selection-mode
485 ;; On simple displays you can't see that a region is active and
486 ;; will be deleted on the next keypress. IMHO especially for
487 ;; copy-region-as-kill this is confusing
488 (define-key global-map "\M-w" 'copy-region-as-kill-nomark)
489
490
491 ;; The following keybindings are for standard ISO keyboards
492 ;; as they are used with IBM compatible PCs, IBM RS/6000,
493 ;; MACs, many X-Stations and probably more
494 (define-key global-map [S-right] 'forward-char-mark)
495 (define-key global-map [right] 'forward-char-nomark)
496 (define-key global-map [C-S-right] 'forward-word-mark)
497 (define-key global-map [C-right] 'forward-word-nomark)
498 (define-key global-map [M-S-right] 'forward-word-mark)
499 (define-key global-map [M-right] 'forward-word-nomark)
500
501 (define-key global-map [S-down] 'next-line-mark)
502 (define-key global-map [down] 'next-line-nomark)
503
504 (define-key global-map [S-end] 'end-of-line-mark)
505 (define-key global-map [end] 'end-of-line-nomark)
506 (global-set-key [S-C-end] 'end-of-buffer-mark)
507 (global-set-key [C-end] 'end-of-buffer-nomark)
508 (global-set-key [S-M-end] 'end-of-buffer-mark)
509 (global-set-key [M-end] 'end-of-buffer-nomark)
510
511 (define-key global-map [S-next] 'scroll-up-mark)
512 (define-key global-map [next] 'scroll-up-nomark)
513
514 (define-key global-map [S-left] 'backward-char-mark)
515 (define-key global-map [left] 'backward-char-nomark)
516 (define-key global-map [C-S-left] 'backward-word-mark)
517 (define-key global-map [C-left] 'backward-word-nomark)
518 (define-key global-map [M-S-left] 'backward-word-mark)
519 (define-key global-map [M-left] 'backward-word-nomark)
520
521 (define-key global-map [S-up] 'previous-line-mark)
522 (define-key global-map [up] 'previous-line-nomark)
523
524 (define-key global-map [S-home] 'beginning-of-line-mark)
525 (define-key global-map [home] 'beginning-of-line-nomark)
526 (global-set-key [S-C-home] 'beginning-of-buffer-mark)
527 (global-set-key [C-home] 'beginning-of-buffer-nomark)
528 (global-set-key [S-M-home] 'beginning-of-buffer-mark)
529 (global-set-key [M-home] 'beginning-of-buffer-nomark)
530
531 (define-key global-map [S-prior] 'scroll-down-mark)
532 (define-key global-map [prior] 'scroll-down-nomark)
533
534 (define-key global-map [S-insert] 'yank)
535 (define-key global-map [C-insert] 'copy-region-as-kill)
536 (define-key global-map [S-delete] 'kill-region)
537
538 ;; The following bindings are useful on Sun Type 3 keyboards
539 ;; They implement the Get-Delete-Put (copy-cut-paste)
540 ;; functions from sunview on the L6, L8 and L10 keys
541 (define-key global-map [f16] 'yank)
542 (define-key global-map [f18] 'copy-region-as-kill)
543 (define-key global-map [f20] 'kill-region)
544
545 ;; The following bindings are from Pete Forman.
546 ;; I modified them a little to work together with the
547 ;; mark functionality I added.
548
549 (global-set-key [f1] 'help) ; KHelp F1
550 (global-set-key [f6] 'other-window) ; KNextPane F6
551 (global-set-key [delete] 'delete-char) ; KDelete Del
552 (global-set-key [C-delete] 'kill-line) ; KEraseEndLine cDel
553 (global-set-key [M-backspace] 'undo) ; KUndo aBS
554 (global-set-key [C-down] 'forward-paragraph-nomark) ; KNextPara cDn
555 (global-set-key [C-up] 'backward-paragraph-nomark) ; KPrevPara cUp
556 (global-set-key [S-C-down] 'forward-paragraph-mark)
557 (global-set-key [S-C-up] 'backward-paragraph-mark)
558
559 ;; The following bindings are taken from pc-mode.el
560 ;; as suggested by RMS.
561 ;; I only used the ones that are not covered above.
562 (define-key function-key-map [M-delete] [?\M-d])
563 (global-set-key [C-M-delete] 'kill-sexp)
564 (global-set-key [C-backspace] 'backward-kill-word)
565 (global-set-key [C-escape] 'list-buffers)
566
567 ;;
568 ;; setup
569 ;;
570 (setq transient-mark-mode t)
571 (setq mark-even-if-inactive t)
572 (delete-selection-mode 1))
573
574 ;;; pc-select.el ends here