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