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