]> code.delx.au - gnu-emacs/blob - lisp/emulation/pc-select.el
Merge from emacs-23
[gnu-emacs] / 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, 1997, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
7
8 ;; Author: Michael Staats <michael@thp.Uni-Duisburg.DE>
9 ;; Keywords: convenience emulations
10 ;; Created: 26 Sep 1995
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 behavior 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 ;; Dan Nicolaescu <done@ece.arizona.ro> suggested suppressing the
59 ;; scroll-up/scroll-down error.
60 ;; Eli Barzilay (eli@cs.bgu.ac.il) suggested the sexps functions and
61 ;; keybindings.
62 ;;
63 ;; Ok, some details about the idea of PC Selection mode:
64 ;;
65 ;; o The standard keys for moving around (right, left, up, down, home, end,
66 ;; prior, next, called "move-keys" from now on) will always de-activate
67 ;; the mark.
68 ;; o If you press "Shift" together with the "move-keys", the region
69 ;; you pass along is activated
70 ;; o You have the copy, cut and paste functions (as in many other programs)
71 ;; which will operate on the active region
72 ;; It was not possible to bind them to C-v, C-x and C-c for obvious
73 ;; emacs reasons.
74 ;; They will be bound according to the "old" behavior to S-delete (cut),
75 ;; S-insert (paste) and C-insert (copy). These keys do the same in many
76 ;; other programs.
77 ;;
78
79 ;;; Code:
80
81 ;; Customization:
82 (defgroup pc-select nil
83 "Emulate pc bindings."
84 :prefix "pc-select"
85 :group 'emulations)
86
87 (defcustom pc-select-override-scroll-error t
88 "Non-nil means don't generate error on scrolling past edge of buffer.
89 This variable applies in PC Selection mode only.
90 The scroll commands normally generate an error if you try to scroll
91 past the top or bottom of the buffer. This is annoying when selecting
92 text with these commands. If you set this variable to non-nil, these
93 errors are suppressed."
94 :type 'boolean
95 :group 'pc-select)
96 (define-obsolete-variable-alias 'pc-select-override-scroll-error
97 'scroll-error-top-bottom
98 "24.1")
99
100 (defcustom pc-select-selection-keys-only nil
101 "Non-nil means only bind the basic selection keys when started.
102 Other keys that emulate pc-behavior will be untouched.
103 This gives mostly Emacs-like behavior with only the selection keys enabled."
104 :type 'boolean
105 :group 'pc-select)
106
107 (defcustom pc-select-meta-moves-sexps nil
108 "Non-nil means move sexp-wise with Meta key, otherwise move word-wise."
109 :type 'boolean
110 :group 'pc-select)
111
112 (defcustom pc-selection-mode-hook nil
113 "The hook to run when PC Selection mode is toggled."
114 :type 'hook
115 :group 'pc-select)
116
117 (defvar pc-select-saved-settings-alist nil
118 "The values of the variables before PC Selection mode was toggled on.
119 When PC Selection mode is toggled on, it sets quite a few variables
120 for its own purposes. This alist holds the original values of the
121 variables PC Selection mode had set, so that these variables can be
122 restored to their original values when PC Selection mode is toggled off.")
123
124 (defvar pc-select-map nil
125 "The keymap used as the global map when PC Selection mode is on." )
126
127 (defvar pc-select-saved-global-map nil
128 "The global map that was in effect when PC Selection mode was toggled on.")
129
130 (defvar pc-select-key-bindings-alist nil
131 "This alist holds all the key bindings PC Selection mode sets.")
132
133 (defvar pc-select-default-key-bindings nil
134 "These key bindings always get set by PC Selection mode.")
135
136 (unless pc-select-default-key-bindings
137 (let ((lst
138 ;; This is to avoid confusion with the delete-selection-mode.
139 ;; On simple displays you can't see that a region is active and
140 ;; will be deleted on the next keypress IMHO especially for
141 ;; copy-region-as-kill this is confusing.
142 ;; The same goes for exchange-point-and-mark
143 '(("\M-w" . copy-region-as-kill-nomark)
144 ("\C-x\C-x" . exchange-point-and-mark-nomark)
145 ([S-right] . forward-char-mark)
146 ([right] . forward-char-nomark)
147 ([C-S-right] . forward-word-mark)
148 ([C-right] . forward-word-nomark)
149 ([S-left] . backward-char-mark)
150 ([left] . backward-char-nomark)
151 ([C-S-left] . backward-word-mark)
152 ([C-left] . backward-word-nomark)
153 ([S-down] . next-line-mark)
154 ([down] . next-line-nomark)
155
156 ([S-end] . end-of-line-mark)
157 ([end] . end-of-line-nomark)
158 ([S-C-end] . end-of-buffer-mark)
159 ([C-end] . end-of-buffer-nomark)
160 ([S-M-end] . end-of-buffer-mark)
161 ([M-end] . end-of-buffer-nomark)
162
163 ([S-next] . scroll-up-mark)
164 ([next] . scroll-up-nomark)
165
166 ([S-up] . previous-line-mark)
167 ([up] . previous-line-nomark)
168
169 ([S-home] . beginning-of-line-mark)
170 ([home] . beginning-of-line-nomark)
171 ([S-C-home] . beginning-of-buffer-mark)
172 ([C-home] . beginning-of-buffer-nomark)
173 ([S-M-home] . beginning-of-buffer-mark)
174 ([M-home] . beginning-of-buffer-nomark)
175
176 ([M-S-down] . forward-line-mark)
177 ([M-down] . forward-line-nomark)
178 ([M-S-up] . backward-line-mark)
179 ([M-up] . backward-line-nomark)
180
181 ([S-prior] . scroll-down-mark)
182 ([prior] . scroll-down-nomark)
183
184 ;; Next four lines are from Pete Forman.
185 ([C-down] . forward-paragraph-nomark) ; KNextPara cDn
186 ([C-up] . backward-paragraph-nomark) ; KPrevPara cUp
187 ([S-C-down] . forward-paragraph-mark)
188 ([S-C-up] . backward-paragraph-mark))))
189
190 (setq pc-select-default-key-bindings lst)))
191
192 (defvar pc-select-extra-key-bindings nil
193 "Key bindings to set only if `pc-select-selection-keys-only' is nil.")
194
195 ;; The following keybindings are for standard ISO keyboards
196 ;; as they are used with IBM compatible PCs, IBM RS/6000,
197 ;; MACs, many X-Stations and probably more
198 (unless pc-select-extra-key-bindings
199 (let ((lst
200 '(([S-insert] . yank)
201 ([C-insert] . copy-region-as-kill)
202 ([S-delete] . kill-region)
203
204 ;; The following bindings are useful on Sun Type 3 keyboards
205 ;; They implement the Get-Delete-Put (copy-cut-paste)
206 ;; functions from sunview on the L6, L8 and L10 keys
207 ;; Sam Steingold <sds@gnu.org> says that f16 is copy and f18 is paste.
208 ([f16] . copy-region-as-kill)
209 ([f18] . yank)
210 ([f20] . kill-region)
211
212 ;; The following bindings are from Pete Forman.
213 ([f6] . other-window) ; KNextPane F6
214 ([C-delete] . kill-line) ; KEraseEndLine cDel
215 ("\M-\d" . undo) ; KUndo aBS
216
217 ;; The following binding is taken from pc-mode.el
218 ;; as suggested by RMS.
219 ;; I only used the one that is not covered above.
220 ([C-M-delete] . kill-sexp)
221 ;; Next line proposed by Eli Barzilay
222 ([C-escape] . electric-buffer-list))))
223
224 (setq pc-select-extra-key-bindings lst)))
225
226 (defvar pc-select-meta-moves-sexps-key-bindings
227 '((([M-S-right] . forward-sexp-mark)
228 ([M-right] . forward-sexp-nomark)
229 ([M-S-left] . backward-sexp-mark)
230 ([M-left] . backward-sexp-nomark))
231 (([M-S-right] . forward-word-mark)
232 ([M-right] . forward-word-nomark)
233 ([M-S-left] . backward-word-mark)
234 ([M-left] . backward-word-nomark)))
235 "The list of key bindings controlled by `pc-select-meta-moves-sexp'.
236 The bindings in the car of this list get installed if
237 `pc-select-meta-moves-sexp' is t, the bindings in the cadr of this
238 list get installed otherwise.")
239
240 ;; This is for tty. We don't turn on normal-erase-is-backspace,
241 ;; but bind keys as pc-selection-mode did before
242 ;; normal-erase-is-backspace was invented, to keep us back
243 ;; compatible.
244 (defvar pc-select-tty-key-bindings
245 '(([delete] . delete-char) ; KDelete Del
246 ([C-backspace] . backward-kill-word))
247 "The list of key bindings controlled by `pc-select-selection-keys-only'.
248 These key bindings get installed when running in a tty, but only if
249 `pc-select-selection-keys-only' is nil.")
250
251 (defvar pc-select-old-M-delete-binding nil
252 "Holds the old mapping of [M-delete] in the `function-key-map'.
253 This variable holds the value associated with [M-delete] in the
254 `function-key-map' before PC Selection mode had changed that
255 association.")
256
257 ;;;;
258 ;; misc
259 ;;;;
260
261 (provide 'pc-select)
262
263 (defun copy-region-as-kill-nomark (beg end)
264 "Save the region as if killed, but don't kill it; deactivate mark.
265 If `interprogram-cut-function' is non-nil, also save the text for a window
266 system cut and paste.
267
268 Deactivating mark is to avoid confusion with `delete-selection-mode'
269 and `transient-mark-mode'."
270 (interactive "r")
271 (copy-region-as-kill beg end)
272 (setq mark-active nil)
273 (message "Region saved"))
274
275 (defun exchange-point-and-mark-nomark ()
276 "Like `exchange-point-and-mark' but without activating the mark."
277 (interactive)
278 (exchange-point-and-mark)
279 (setq mark-active nil))
280
281 ;;;;
282 ;; non-interactive
283 ;;;;
284 (defun pc-select-ensure-mark ()
285 ;; make sure mark is active
286 ;; test if it is active, if it isn't, set it and activate it
287 (or mark-active (set-mark-command nil))
288 ;; Remember who activated the mark.
289 (setq mark-active 'pc-select))
290
291 (defun pc-select-maybe-deactivate-mark ()
292 ;; maybe switch off mark (only if *we* switched it on)
293 (when (eq mark-active 'pc-select)
294 (deactivate-mark)))
295
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
297 ;;;;; forward and mark
298 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
299
300 (defun forward-char-mark (&optional arg)
301 "Ensure mark is active; move point right ARG characters (left if ARG negative).
302 On reaching end of buffer, stop and signal error."
303 (interactive "p")
304 (pc-select-ensure-mark)
305 (forward-char arg))
306
307 (defun forward-word-mark (&optional arg)
308 "Ensure mark is active; move point right ARG words (backward if ARG is negative).
309 Normally returns t.
310 If an edge of the buffer is reached, point is left there
311 and nil is returned."
312 (interactive "p")
313 (pc-select-ensure-mark)
314 (forward-word arg))
315
316 (defun forward-line-mark (&optional arg)
317 "Ensure mark is active; move cursor vertically down ARG lines."
318 (interactive "p")
319 (pc-select-ensure-mark)
320 (forward-line arg)
321 (setq this-command 'forward-line)
322 )
323
324 (defun forward-sexp-mark (&optional arg)
325 "Ensure mark is active; move forward across one balanced expression (sexp).
326 With argument, do it that many times. Negative arg -N means
327 move backward across N balanced expressions."
328 (interactive "p")
329 (pc-select-ensure-mark)
330 (forward-sexp arg))
331
332 (defun forward-paragraph-mark (&optional arg)
333 "Ensure mark is active; move forward to end of paragraph.
334 With arg N, do it N times; negative arg -N means move backward N paragraphs.
335
336 A line which `paragraph-start' matches either separates paragraphs
337 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
338 A paragraph end is the beginning of a line which is not part of the paragraph
339 to which the end of the previous line belongs, or the end of the buffer."
340 (interactive "p")
341 (pc-select-ensure-mark)
342 (forward-paragraph arg))
343
344 (defun next-line-mark (&optional arg)
345 "Ensure mark is active; move cursor vertically down ARG lines.
346 If there is no character in the target line exactly under the current column,
347 the cursor is positioned after the character in that line which spans this
348 column, or at the end of the line if it is not long enough.
349 If there is no line in the buffer after this one, behavior depends on the
350 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
351 to create a line, and moves the cursor to that line. Otherwise it moves the
352 cursor to the end of the buffer \(if already at the end of the buffer, an error
353 is signaled).
354
355 The command \\[set-goal-column] can be used to create
356 a semipermanent goal column to which this command always moves.
357 Then it does not try to move vertically. This goal column is stored
358 in `goal-column', which is nil when there is none."
359 (interactive "p")
360 (pc-select-ensure-mark)
361 (with-no-warnings (next-line arg))
362 (setq this-command 'next-line))
363
364 (defun end-of-line-mark (&optional arg)
365 "Ensure mark is active; move point to end of current line.
366 With argument ARG not nil or 1, move forward ARG - 1 lines first.
367 If scan reaches end of buffer, stop there without error."
368 (interactive "p")
369 (pc-select-ensure-mark)
370 (end-of-line arg)
371 (setq this-command 'end-of-line))
372
373 (defun backward-line-mark (&optional arg)
374 "Ensure mark is active; move cursor vertically up ARG lines."
375 (interactive "p")
376 (pc-select-ensure-mark)
377 (if (null arg)
378 (setq arg 1))
379 (forward-line (- arg))
380 (setq this-command 'forward-line)
381 )
382
383 (defun scroll-down-mark (&optional arg)
384 "Ensure mark is active; scroll down ARG lines; or near full screen if no ARG.
385 A near full screen is `next-screen-context-lines' less than a full screen.
386 Negative ARG means scroll upward.
387 When calling from a program, supply a number as argument or nil.
388 Attempting to scroll past the edge of buffer does not raise an
389 error, unless `pc-select-override-scroll-error' is nil."
390 (interactive "P")
391 (pc-select-ensure-mark)
392 (cond (pc-select-override-scroll-error
393 (condition-case nil (scroll-down arg)
394 (beginning-of-buffer (goto-char (point-min)))))
395 (t (scroll-down arg))))
396
397 (defun end-of-buffer-mark (&optional arg)
398 "Ensure mark is active; move point to the end of the buffer.
399 With arg N, put point N/10 of the way from the end.
400
401 If the buffer is narrowed, this command uses the beginning and size
402 of the accessible part of the buffer.
403
404 Don't use this command in Lisp programs!
405 \(goto-char \(point-max)) is faster and avoids clobbering the mark."
406 (interactive "P")
407 (pc-select-ensure-mark)
408 (let ((size (- (point-max) (point-min))))
409 (goto-char (if arg
410 (- (point-max)
411 (if (> size 10000)
412 ;; Avoid overflow for large buffer sizes!
413 (* (prefix-numeric-value arg)
414 (/ size 10))
415 (/ (* size (prefix-numeric-value arg)) 10)))
416 (point-max))))
417 ;; If we went to a place in the middle of the buffer,
418 ;; adjust it to the beginning of a line.
419 (if arg (forward-line 1)
420 ;; If the end of the buffer is not already on the screen,
421 ;; then scroll specially to put it near, but not at, the bottom.
422 (if (let ((old-point (point)))
423 (save-excursion
424 (goto-char (window-start))
425 (vertical-motion (window-height))
426 (< (point) old-point)))
427 (progn
428 (overlay-recenter (point))
429 (recenter -3)))))
430
431 ;;;;;;;;;
432 ;;;;; no mark
433 ;;;;;;;;;
434
435 (defun forward-char-nomark (&optional arg)
436 "Deactivate mark; move point right ARG characters \(left if ARG negative).
437 On reaching end of buffer, stop and signal error."
438 (interactive "p")
439 (pc-select-maybe-deactivate-mark)
440 (forward-char arg))
441
442 (defun forward-word-nomark (&optional arg)
443 "Deactivate mark; move point right ARG words \(backward if ARG is negative).
444 Normally returns t.
445 If an edge of the buffer is reached, point is left there
446 and nil is returned."
447 (interactive "p")
448 (pc-select-maybe-deactivate-mark)
449 (forward-word arg))
450
451 (defun forward-line-nomark (&optional arg)
452 "Deactivate mark; move cursor vertically down ARG lines."
453 (interactive "p")
454 (pc-select-maybe-deactivate-mark)
455 (forward-line arg)
456 (setq this-command 'forward-line)
457 )
458
459 (defun forward-sexp-nomark (&optional arg)
460 "Deactivate mark; move forward across one balanced expression (sexp).
461 With argument, do it that many times. Negative arg -N means
462 move backward across N balanced expressions."
463 (interactive "p")
464 (pc-select-maybe-deactivate-mark)
465 (forward-sexp arg))
466
467 (defun forward-paragraph-nomark (&optional arg)
468 "Deactivate mark; move forward to end of paragraph.
469 With arg N, do it N times; negative arg -N means move backward N paragraphs.
470
471 A line which `paragraph-start' matches either separates paragraphs
472 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
473 A paragraph end is the beginning of a line which is not part of the paragraph
474 to which the end of the previous line belongs, or the end of the buffer."
475 (interactive "p")
476 (pc-select-maybe-deactivate-mark)
477 (forward-paragraph arg))
478
479 (defun next-line-nomark (&optional arg)
480 "Deactivate mark; move cursor vertically down ARG lines.
481 If there is no character in the target line exactly under the current column,
482 the cursor is positioned after the character in that line which spans this
483 column, or at the end of the line if it is not long enough.
484 If there is no line in the buffer after this one, behavior depends on the
485 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
486 to create a line, and moves the cursor to that line. Otherwise it moves the
487 cursor to the end of the buffer (if already at the end of the buffer, an error
488 is signaled).
489
490 The command \\[set-goal-column] can be used to create
491 a semipermanent goal column to which this command always moves.
492 Then it does not try to move vertically. This goal column is stored
493 in `goal-column', which is nil when there is none."
494 (interactive "p")
495 (pc-select-maybe-deactivate-mark)
496 (with-no-warnings (next-line arg))
497 (setq this-command 'next-line))
498
499 (defun end-of-line-nomark (&optional arg)
500 "Deactivate mark; move point to end of current line.
501 With argument ARG not nil or 1, move forward ARG - 1 lines first.
502 If scan reaches end of buffer, stop there without error."
503 (interactive "p")
504 (pc-select-maybe-deactivate-mark)
505 (end-of-line arg)
506 (setq this-command 'end-of-line))
507
508 (defun backward-line-nomark (&optional arg)
509 "Deactivate mark; move cursor vertically up ARG lines."
510 (interactive "p")
511 (pc-select-maybe-deactivate-mark)
512 (if (null arg)
513 (setq arg 1))
514 (forward-line (- arg))
515 (setq this-command 'forward-line)
516 )
517
518 (defun scroll-down-nomark (&optional arg)
519 "Deactivate mark; scroll down ARG lines; or near full screen if no ARG.
520 A near full screen is `next-screen-context-lines' less than a full screen.
521 Negative ARG means scroll upward.
522 When calling from a program, supply a number as argument or nil.
523 Attempting to scroll past the edge of buffer does not raise an
524 error, unless `pc-select-override-scroll-error' is nil."
525 (interactive "P")
526 (pc-select-maybe-deactivate-mark)
527 (cond (pc-select-override-scroll-error
528 (condition-case nil (scroll-down arg)
529 (beginning-of-buffer (goto-char (point-min)))))
530 (t (scroll-down arg))))
531
532 (defun end-of-buffer-nomark (&optional arg)
533 "Deactivate mark; move point to the end of the buffer.
534 With arg N, put point N/10 of the way from the end.
535
536 If the buffer is narrowed, this command uses the beginning and size
537 of the accessible part of the buffer.
538
539 Don't use this command in Lisp programs!
540 \(goto-char (point-max)) is faster and avoids clobbering the mark."
541 (interactive "P")
542 (pc-select-maybe-deactivate-mark)
543 (let ((size (- (point-max) (point-min))))
544 (goto-char (if arg
545 (- (point-max)
546 (if (> size 10000)
547 ;; Avoid overflow for large buffer sizes!
548 (* (prefix-numeric-value arg)
549 (/ size 10))
550 (/ (* size (prefix-numeric-value arg)) 10)))
551 (point-max))))
552 ;; If we went to a place in the middle of the buffer,
553 ;; adjust it to the beginning of a line.
554 (if arg (forward-line 1)
555 ;; If the end of the buffer is not already on the screen,
556 ;; then scroll specially to put it near, but not at, the bottom.
557 (if (let ((old-point (point)))
558 (save-excursion
559 (goto-char (window-start))
560 (vertical-motion (window-height))
561 (< (point) old-point)))
562 (progn
563 (overlay-recenter (point))
564 (recenter -3)))))
565
566
567 ;;;;;;;;;;;;;;;;;;;;
568 ;;;;;; backwards and mark
569 ;;;;;;;;;;;;;;;;;;;;
570
571 (defun backward-char-mark (&optional arg)
572 "Ensure mark is active; move point left ARG characters (right if ARG negative).
573 On attempt to pass beginning or end of buffer, stop and signal error."
574 (interactive "p")
575 (pc-select-ensure-mark)
576 (backward-char arg))
577
578 (defun backward-word-mark (&optional arg)
579 "Ensure mark is active; move backward until encountering the end of a word.
580 With argument, do this that many times."
581 (interactive "p")
582 (pc-select-ensure-mark)
583 (backward-word arg))
584
585 (defun backward-sexp-mark (&optional arg)
586 "Ensure mark is active; move backward across one balanced expression (sexp).
587 With argument, do it that many times. Negative arg -N means
588 move forward across N balanced expressions."
589 (interactive "p")
590 (pc-select-ensure-mark)
591 (backward-sexp arg))
592
593 (defun backward-paragraph-mark (&optional arg)
594 "Ensure mark is active; move backward to start of paragraph.
595 With arg N, do it N times; negative arg -N means move forward N paragraphs.
596
597 A paragraph start is the beginning of a line which is a
598 `first-line-of-paragraph' or which is ordinary text and follows a
599 paragraph-separating line; except: if the first real line of a
600 paragraph is preceded by a blank line, the paragraph starts at that
601 blank line.
602
603 See `forward-paragraph' for more information."
604 (interactive "p")
605 (pc-select-ensure-mark)
606 (backward-paragraph arg))
607
608 (defun previous-line-mark (&optional arg)
609 "Ensure mark is active; move cursor vertically up ARG lines.
610 If there is no character in the target line exactly over the current column,
611 the cursor is positioned after the character in that line which spans this
612 column, or at the end of the line if it is not long enough.
613
614 The command \\[set-goal-column] can be used to create
615 a semipermanent goal column to which this command always moves.
616 Then it does not try to move vertically.
617
618 If you are thinking of using this in a Lisp program, consider using
619 `forward-line' with a negative argument instead. It is usually easier
620 to use and more reliable (no dependence on goal column, etc.)."
621 (interactive "p")
622 (pc-select-ensure-mark)
623 (with-no-warnings (previous-line arg))
624 (setq this-command 'previous-line))
625
626 (defun beginning-of-line-mark (&optional arg)
627 "Ensure mark is active; move point to beginning of current line.
628 With argument ARG not nil or 1, move forward ARG - 1 lines first.
629 If scan reaches end of buffer, stop there without error."
630 (interactive "p")
631 (pc-select-ensure-mark)
632 (beginning-of-line arg))
633
634
635 (defun scroll-up-mark (&optional arg)
636 "Ensure mark is active; scroll upward ARG lines; or near full screen if no ARG.
637 A near full screen is `next-screen-context-lines' less than a full screen.
638 Negative ARG means scroll downward.
639 When calling from a program, supply a number as argument or nil.
640 Attempting to scroll past the edge of buffer does not raise an
641 error, unless `pc-select-override-scroll-error' is nil."
642 (interactive "P")
643 (pc-select-ensure-mark)
644 (cond (pc-select-override-scroll-error
645 (condition-case nil (scroll-up arg)
646 (end-of-buffer (goto-char (point-max)))))
647 (t (scroll-up arg))))
648
649 (defun beginning-of-buffer-mark (&optional arg)
650 "Ensure mark is active; move point to the beginning of the buffer.
651 With arg N, put point N/10 of the way from the beginning.
652
653 If the buffer is narrowed, this command uses the beginning and size
654 of the accessible part of the buffer.
655
656 Don't use this command in Lisp programs!
657 \(goto-char (point-min)) is faster and avoids clobbering the mark."
658 (interactive "P")
659 (pc-select-ensure-mark)
660 (let ((size (- (point-max) (point-min))))
661 (goto-char (if arg
662 (+ (point-min)
663 (if (> size 10000)
664 ;; Avoid overflow for large buffer sizes!
665 (* (prefix-numeric-value arg)
666 (/ size 10))
667 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
668 (point-min))))
669 (if arg (forward-line 1)))
670
671 ;;;;;;;;
672 ;;; no mark
673 ;;;;;;;;
674
675 (defun backward-char-nomark (&optional arg)
676 "Deactivate mark; move point left ARG characters (right if ARG negative).
677 On attempt to pass beginning or end of buffer, stop and signal error."
678 (interactive "p")
679 (pc-select-maybe-deactivate-mark)
680 (backward-char arg))
681
682 (defun backward-word-nomark (&optional arg)
683 "Deactivate mark; move backward until encountering the end of a word.
684 With argument, do this that many times."
685 (interactive "p")
686 (pc-select-maybe-deactivate-mark)
687 (backward-word arg))
688
689 (defun backward-sexp-nomark (&optional arg)
690 "Deactivate mark; move backward across one balanced expression (sexp).
691 With argument, do it that many times. Negative arg -N means
692 move forward across N balanced expressions."
693 (interactive "p")
694 (pc-select-maybe-deactivate-mark)
695 (backward-sexp arg))
696
697 (defun backward-paragraph-nomark (&optional arg)
698 "Deactivate mark; move backward to start of paragraph.
699 With arg N, do it N times; negative arg -N means move forward N paragraphs.
700
701 A paragraph start is the beginning of a line which is a
702 `first-line-of-paragraph' or which is ordinary text and follows a
703 paragraph-separating line; except: if the first real line of a
704 paragraph is preceded by a blank line, the paragraph starts at that
705 blank line.
706
707 See `forward-paragraph' for more information."
708 (interactive "p")
709 (pc-select-maybe-deactivate-mark)
710 (backward-paragraph arg))
711
712 (defun previous-line-nomark (&optional arg)
713 "Deactivate mark; move cursor vertically up ARG lines.
714 If there is no character in the target line exactly over the current column,
715 the cursor is positioned after the character in that line which spans this
716 column, or at the end of the line if it is not long enough.
717
718 The command \\[set-goal-column] can be used to create
719 a semipermanent goal column to which this command always moves.
720 Then it does not try to move vertically."
721 (interactive "p")
722 (pc-select-maybe-deactivate-mark)
723 (with-no-warnings (previous-line arg))
724 (setq this-command 'previous-line))
725
726 (defun beginning-of-line-nomark (&optional arg)
727 "Deactivate mark; move point to beginning of current line.
728 With argument ARG not nil or 1, move forward ARG - 1 lines first.
729 If scan reaches end of buffer, stop there without error."
730 (interactive "p")
731 (pc-select-maybe-deactivate-mark)
732 (beginning-of-line arg))
733
734 (defun scroll-up-nomark (&optional arg)
735 "Deactivate mark; scroll upward ARG lines; or near full screen if no ARG.
736 A near full screen is `next-screen-context-lines' less than a full screen.
737 Negative ARG means scroll downward.
738 When calling from a program, supply a number as argument or nil.
739 Attempting to scroll past the edge of buffer does not raise an
740 error, unless `pc-select-override-scroll-error' is nil."
741 (interactive "P")
742 (pc-select-maybe-deactivate-mark)
743 (cond (pc-select-override-scroll-error
744 (condition-case nil (scroll-up arg)
745 (end-of-buffer (goto-char (point-max)))))
746 (t (scroll-up arg))))
747
748 (defun beginning-of-buffer-nomark (&optional arg)
749 "Deactivate mark; move point to the beginning of the buffer.
750 With arg N, put point N/10 of the way from the beginning.
751
752 If the buffer is narrowed, this command uses the beginning and size
753 of the accessible part of the buffer.
754
755 Don't use this command in Lisp programs!
756 \(goto-char (point-min)) is faster and avoids clobbering the mark."
757 (interactive "P")
758 (pc-select-maybe-deactivate-mark)
759 (let ((size (- (point-max) (point-min))))
760 (goto-char (if arg
761 (+ (point-min)
762 (if (> size 10000)
763 ;; Avoid overflow for large buffer sizes!
764 (* (prefix-numeric-value arg)
765 (/ size 10))
766 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
767 (point-min))))
768 (if arg (forward-line 1)))
769
770
771 (defun pc-select-define-keys (alist keymap)
772 "Make KEYMAP have the key bindings specified in ALIST."
773 (let ((lst alist))
774 (while lst
775 (define-key keymap (caar lst) (cdar lst))
776 (setq lst (cdr lst)))))
777
778 (defun pc-select-restore-keys (alist keymap saved-map)
779 "Use ALIST to restore key bindings from SAVED-MAP into KEYMAP.
780 Go through all the key bindings in ALIST, and, for each key
781 binding, if KEYMAP and ALIST still agree on the key binding,
782 restore the previous value of that key binding from SAVED-MAP."
783 (let ((lst alist))
784 (while lst
785 (when (equal (lookup-key keymap (caar lst)) (cdar lst))
786 (define-key keymap (caar lst) (lookup-key saved-map (caar lst))))
787 (setq lst (cdr lst)))))
788
789 (defmacro pc-select-add-to-alist (alist var val)
790 "Ensure that ALIST contains the cons cell (VAR . VAL).
791 If a cons cell whose car is VAR is already on the ALIST, update the
792 cdr of that cell with VAL. Otherwise, make a new cons cell
793 \(VAR . VAL), and prepend it onto ALIST."
794 (let ((elt (make-symbol "elt")))
795 `(let ((,elt (assq ',var ,alist)))
796 (if ,elt
797 (setcdr ,elt ,val)
798 (setq ,alist (cons (cons ',var ,val) ,alist))))))
799
800 (defmacro pc-select-save-and-set-var (var newval)
801 "Set VAR to NEWVAL; save the old value.
802 The old value is saved on the `pc-select-saved-settings-alist'."
803 `(when (boundp ',var)
804 (pc-select-add-to-alist pc-select-saved-settings-alist ,var ,var)
805 (setq ,var ,newval)))
806
807 (defmacro pc-select-save-and-set-mode (mode &optional arg mode-var)
808 "Call the function MODE; save the old value of the variable MODE.
809 MODE is presumed to be a function which turns on a minor mode. First,
810 save the value of the variable MODE on `pc-select-saved-settings-alist'.
811 Then, if ARG is specified, call MODE with ARG, otherwise call it with
812 nil as an argument. If MODE-VAR is specified, save the value of the
813 variable MODE-VAR (instead of the value of the variable MODE) on
814 `pc-select-saved-settings-alist'."
815 (unless mode-var (setq mode-var mode))
816 `(when (fboundp ',mode)
817 (pc-select-add-to-alist pc-select-saved-settings-alist
818 ,mode-var ,mode-var)
819 (,mode ,arg)))
820
821 (defmacro pc-select-restore-var (var)
822 "Restore the previous value of the variable VAR.
823 Look up VAR's previous value in `pc-select-saved-settings-alist', and,
824 if the value is found, set VAR to that value."
825 (let ((elt (make-symbol "elt")))
826 `(let ((,elt (assq ',var pc-select-saved-settings-alist)))
827 (unless (null ,elt)
828 (setq ,var (cdr ,elt))))))
829
830 (defmacro pc-select-restore-mode (mode)
831 "Restore the previous state (either on or off) of the minor mode MODE.
832 Look up the value of the variable MODE on `pc-select-saved-settings-alist'.
833 If the value is non-nil, call the function MODE with an argument of
834 1, otherwise call it with an argument of -1."
835 (let ((elt (make-symbol "elt")))
836 `(when (fboundp ',mode)
837 (let ((,elt (assq ',mode pc-select-saved-settings-alist)))
838 (unless (null ,elt)
839 (,mode (if (cdr ,elt) 1 -1)))))))
840
841
842 ;;;###autoload
843 (define-minor-mode pc-selection-mode
844 "Change mark behavior to emulate Motif, Mac or MS-Windows cut and paste style.
845
846 This mode enables Delete Selection mode and Transient Mark mode.
847
848 The arrow keys (and others) are bound to new functions
849 which modify the status of the mark.
850
851 The ordinary arrow keys disable the mark.
852 The shift-arrow keys move, leaving the mark behind.
853
854 C-LEFT and C-RIGHT move back or forward one word, disabling the mark.
855 S-C-LEFT and S-C-RIGHT move back or forward one word, leaving the mark behind.
856
857 M-LEFT and M-RIGHT move back or forward one word or sexp, disabling the mark.
858 S-M-LEFT and S-M-RIGHT move back or forward one word or sexp, leaving the mark
859 behind. To control whether these keys move word-wise or sexp-wise set the
860 variable `pc-select-meta-moves-sexps' after loading pc-select.el but before
861 turning PC Selection mode on.
862
863 C-DOWN and C-UP move back or forward a paragraph, disabling the mark.
864 S-C-DOWN and S-C-UP move back or forward a paragraph, leaving the mark behind.
865
866 HOME moves to beginning of line, disabling the mark.
867 S-HOME moves to beginning of line, leaving the mark behind.
868 With Ctrl or Meta, these keys move to beginning of buffer instead.
869
870 END moves to end of line, disabling the mark.
871 S-END moves to end of line, leaving the mark behind.
872 With Ctrl or Meta, these keys move to end of buffer instead.
873
874 PRIOR or PAGE-UP scrolls and disables the mark.
875 S-PRIOR or S-PAGE-UP scrolls and leaves the mark behind.
876
877 S-DELETE kills the region (`kill-region').
878 S-INSERT yanks text from the kill ring (`yank').
879 C-INSERT copies the region into the kill ring (`copy-region-as-kill').
880
881 In addition, certain other PC bindings are imitated (to avoid this, set
882 the variable `pc-select-selection-keys-only' to t after loading pc-select.el
883 but before calling PC Selection mode):
884
885 F6 other-window
886 DELETE delete-char
887 C-DELETE kill-line
888 M-DELETE kill-word
889 C-M-DELETE kill-sexp
890 C-BACKSPACE backward-kill-word
891 M-BACKSPACE undo"
892 ;; FIXME: bring pc-bindings-mode here ?
893 nil nil nil
894
895 :group 'pc-select
896 :global t
897
898 (if pc-selection-mode
899 (if (null pc-select-key-bindings-alist)
900 (progn
901 (setq pc-select-saved-global-map (copy-keymap (current-global-map)))
902 (setq pc-select-key-bindings-alist
903 (append pc-select-default-key-bindings
904 (if pc-select-selection-keys-only
905 nil
906 pc-select-extra-key-bindings)
907 (if pc-select-meta-moves-sexps
908 (car pc-select-meta-moves-sexps-key-bindings)
909 (cadr pc-select-meta-moves-sexps-key-bindings))
910 (if (or pc-select-selection-keys-only
911 (eq window-system 'x)
912 (memq system-name '(ms-dos windows-nt)))
913 nil
914 pc-select-tty-key-bindings)))
915
916 (pc-select-define-keys pc-select-key-bindings-alist
917 (current-global-map))
918
919 (unless (or pc-select-selection-keys-only
920 (eq window-system 'x)
921 (memq system-name '(ms-dos windows-nt)))
922 ;; it is not clear that we need the following line
923 ;; I hope it doesn't do too much harm to leave it in, though...
924 (setq pc-select-old-M-delete-binding
925 (lookup-key function-key-map [M-delete]))
926 (define-key function-key-map [M-delete] [?\M-d]))
927
928 (when (and (not pc-select-selection-keys-only)
929 (or (eq window-system 'x)
930 (memq system-name '(ms-dos windows-nt)))
931 (fboundp 'normal-erase-is-backspace-mode))
932 (pc-select-save-and-set-mode normal-erase-is-backspace-mode 1
933 normal-erase-is-backspace))
934 ;; the original author also had this above:
935 ;; (setq-default normal-erase-is-backspace t)
936 ;; However, the documentation for the variable says that
937 ;; "setting it with setq has no effect", so I'm removing it.
938
939 (pc-select-save-and-set-var highlight-nonselected-windows nil)
940 (pc-select-save-and-set-var transient-mark-mode t)
941 (pc-select-save-and-set-var mark-even-if-inactive t)
942 (pc-select-save-and-set-mode delete-selection-mode 1))
943 ;;else
944 ;; If the user turned on pc-selection-mode a second time
945 ;; do not clobber the values of the variables that were
946 ;; saved from before pc-selection mode was activated --
947 ;; just make sure the values are the way we like them.
948 (pc-select-define-keys pc-select-key-bindings-alist
949 (current-global-map))
950 (unless (or pc-select-selection-keys-only
951 (eq window-system 'x)
952 (memq system-name '(ms-dos windows-nt)))
953 ;; it is not clear that we need the following line
954 ;; I hope it doesn't do too much harm to leave it in, though...
955 (define-key function-key-map [M-delete] [?\M-d]))
956 (when (and (not pc-select-selection-keys-only)
957 (or (eq window-system 'x)
958 (memq system-name '(ms-dos windows-nt)))
959 (fboundp 'normal-erase-is-backspace-mode))
960 (normal-erase-is-backspace-mode 1))
961 (setq highlight-nonselected-windows nil)
962 (setq transient-mark-mode t)
963 (setq mark-even-if-inactive t)
964 (delete-selection-mode 1))
965 ;;else
966 (when pc-select-key-bindings-alist
967 (when (and (not pc-select-selection-keys-only)
968 (or (eq window-system 'x)
969 (memq system-name '(ms-dos windows-nt))))
970 (pc-select-restore-mode normal-erase-is-backspace-mode))
971
972 (pc-select-restore-keys
973 pc-select-key-bindings-alist (current-global-map)
974 pc-select-saved-global-map)
975
976 (pc-select-restore-var highlight-nonselected-windows)
977 (pc-select-restore-var transient-mark-mode)
978 (pc-select-restore-var mark-even-if-inactive)
979 (pc-select-restore-mode delete-selection-mode)
980 (and pc-select-old-M-delete-binding
981 (define-key function-key-map [M-delete]
982 pc-select-old-M-delete-binding))
983 (setq pc-select-key-bindings-alist nil
984 pc-select-saved-settings-alist nil))))
985
986 ;; arch-tag: 10697b70-ae07-4f3e-ad23-7814a3f418c2
987 ;;; pc-select.el ends here