2008-11-22 Michael Kifer <kifer@cs.stonybrook.edu>
[bpt/emacs.git] / lisp / emulation / pc-select.el
index c7ea973..bb977cc 100644 (file)
@@ -2,7 +2,8 @@
 ;;;                 (or MAC GUI or MS-windoze (bah)) look-and-feel
 ;;;                 including key bindings.
 
-;; Copyright (C) 1995, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Michael Staats <michael@thp.Uni-Duisburg.DE>
 ;; Keywords: convenience emulation
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,9 +22,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -31,7 +30,7 @@
 ;; programs (which is the same as the MAC gui and (sorry for that) MS-Windows).
 ;; It modifies the keybindings of the cursor keys and the next, prior,
 ;; home and end keys. They will modify mark-active.
-;; You can still get the old behaviour of cursor moving with the
+;; You can still get the old behavior of cursor moving with the
 ;; control sequences C-f, C-b, etc.
 ;; This package uses transient-mark-mode and
 ;; delete-selection-mode.
@@ -61,7 +60,7 @@
 ;; Eli Barzilay (eli@cs.bgu.ac.il) suggested the sexps functions and
 ;; keybindings.
 ;;
-;; Ok, some details about the idea of pc-selection-mode:
+;; Ok, some details about the idea of PC Selection mode:
 ;;
 ;;  o The standard keys for moving around (right, left, up, down, home, end,
 ;;    prior, next, called "move-keys" from now on) will always de-activate
@@ -72,7 +71,7 @@
 ;;    which will operate on the active region
 ;;    It was not possible to bind them to C-v, C-x and C-c for obvious
 ;;    emacs reasons.
-;;    They will be bound according to the "old" behaviour to S-delete (cut),
+;;    They will be bound according to the "old" behavior to S-delete (cut),
 ;;    S-insert (paste) and C-insert (copy). These keys do the same in many
 ;;    other programs.
 ;;
@@ -99,7 +98,7 @@ errors are suppressed."
 (defcustom pc-select-selection-keys-only nil
   "*Non-nil means only bind the basic selection keys when started.
 Other keys that emulate pc-behavior will be untouched.
-This gives mostly Emacs-like behaviour with only the selection keys enabled."
+This gives mostly Emacs-like behavior with only the selection keys enabled."
   :type 'boolean
   :group 'pc-select)
 
@@ -114,28 +113,28 @@ This gives mostly Emacs-like behaviour with only the selection keys enabled."
   :group 'pc-select)
 
 (defvar pc-select-saved-settings-alist nil
-  "The values of the variables before `pc-selection-mode' was toggled on.
-When `pc-selection-mode' is toggled on, it sets quite a few variables
+  "The values of the variables before PC Selection mode was toggled on.
+When PC Selection mode is toggled on, it sets quite a few variables
 for its own purposes.  This alist holds the original values of the
-variables `pc-selection-mode' had set, so that these variables can be
-restored to their original values when `pc-selection-mode' is toggled off.")
+variables PC Selection mode had set, so that these variables can be
+restored to their original values when PC Selection mode is toggled off.")
 
 (defvar pc-select-map nil
-  "The keymap used as the global map when `pc-selection-mode' is on." )
+  "The keymap used as the global map when PC Selection mode is on." )
 
 (defvar pc-select-saved-global-map nil
-  "The global map that was in effect when `pc-selection-mode' was toggled on.")
+  "The global map that was in effect when PC Selection mode was toggled on.")
 
 (defvar pc-select-key-bindings-alist nil
-  "This alist holds all the key bindings `pc-selection-mode' sets.")
+  "This alist holds all the key bindings PC Selection mode sets.")
 
 (defvar pc-select-default-key-bindings nil
-  "These key bindings always get set by `pc-selection-mode'.")
+  "These key bindings always get set by PC Selection mode.")
 
 (unless pc-select-default-key-bindings
   (let ((lst
-        ;; This is to avoid confusion with the delete-selection-mode
-       ;; On simple displays you cant see that a region is active and
+        ;; This is to avoid confusion with the delete-selection-mode.
+         ;; On simple displays you can't see that a region is active and
         ;; will be deleted on the next keypress IMHO especially for
         ;; copy-region-as-kill this is confusing.
         ;; The same goes for exchange-point-and-mark
@@ -181,7 +180,7 @@ restored to their original values when `pc-selection-mode' is toggled off.")
           ([prior]     . scroll-down-nomark)
 
           ;; Next four lines are from Pete Forman.
-          ([C-down]    . forward-paragraph-nomark) ; KNextPara     cDn
+          ([C-down]    . forward-paragraph-nomark)  ; KNextPara     cDn
           ([C-up]      . backward-paragraph-nomark) ; KPrevPara     cUp
           ([S-C-down]  . forward-paragraph-mark)
           ([S-C-up]    . backward-paragraph-mark))))
@@ -250,7 +249,7 @@ These key bindings get installed when running in a tty, but only if
 (defvar pc-select-old-M-delete-binding nil
   "Holds the old mapping of [M-delete] in the `function-key-map'.
 This variable holds the value associated with [M-delete] in the
-`function-key-map' before `pc-selection-mode' had changed that
+`function-key-map' before PC Selection mode had changed that
 association.")
 
 ;;;;
@@ -280,10 +279,17 @@ and `transient-mark-mode'."
 ;;;;
 ;; non-interactive
 ;;;;
-(defun ensure-mark()
+(defun pc-select-ensure-mark ()
   ;; make sure mark is active
   ;; test if it is active, if it isn't, set it and activate it
-  (or mark-active (set-mark-command nil)))
+  (or mark-active (set-mark-command nil))
+  ;; Remember who activated the mark.
+  (setq mark-active 'pc-select))
+
+(defun pc-select-maybe-deactivate-mark ()
+  ;; maybe switch off mark (only if *we* switched it on)
+  (when (eq mark-active 'pc-select)
+    (deactivate-mark)))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;; forward and mark
@@ -293,7 +299,7 @@ and `transient-mark-mode'."
   "Ensure mark is active; move point right ARG characters (left if ARG negative).
 On reaching end of buffer, stop and signal error."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (forward-char arg))
 
 (defun forward-word-mark (&optional arg)
@@ -302,13 +308,13 @@ Normally returns t.
 If an edge of the buffer is reached, point is left there
 and nil is returned."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (forward-word arg))
 
 (defun forward-line-mark (&optional arg)
   "Ensure mark is active; move cursor vertically down ARG lines."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (forward-line arg)
   (setq this-command 'forward-line)
 )
@@ -318,7 +324,7 @@ and nil is returned."
 With argument, do it that many times.  Negative arg -N means
 move backward across N balanced expressions."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (forward-sexp arg))
 
 (defun forward-paragraph-mark (&optional arg)
@@ -330,7 +336,7 @@ A line which `paragraph-start' matches either separates paragraphs
 A paragraph end is the beginning of a line which is not part of the paragraph
 to which the end of the previous line belongs, or the end of the buffer."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (forward-paragraph arg))
 
 (defun next-line-mark (&optional arg)
@@ -349,8 +355,8 @@ a semipermanent goal column to which this command always moves.
 Then it does not try to move vertically.  This goal column is stored
 in `goal-column', which is nil when there is none."
   (interactive "p")
-  (ensure-mark)
-  (next-line arg)
+  (pc-select-ensure-mark)
+  (with-no-warnings (next-line arg))
   (setq this-command 'next-line))
 
 (defun end-of-line-mark (&optional arg)
@@ -358,14 +364,14 @@ in `goal-column', which is nil when there is none."
 With argument ARG not nil or 1, move forward ARG - 1 lines first.
 If scan reaches end of buffer, stop there without error."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (end-of-line arg)
   (setq this-command 'end-of-line))
 
 (defun backward-line-mark (&optional arg)
   "Ensure mark is active; move cursor vertically up ARG lines."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (if (null arg)
       (setq arg 1))
   (forward-line (- arg))
@@ -378,7 +384,7 @@ A near full screen is `next-screen-context-lines' less than a full screen.
 Negative ARG means scroll upward.
 When calling from a program, supply a number as argument or nil."
   (interactive "P")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (cond (pc-select-override-scroll-error
         (condition-case nil (scroll-down arg)
           (beginning-of-buffer (goto-char (point-min)))))
@@ -394,7 +400,7 @@ of the accessible part of the buffer.
 Don't use this command in Lisp programs!
 \(goto-char \(point-max)) is faster and avoids clobbering the mark."
   (interactive "P")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (let ((size (- (point-max) (point-min))))
     (goto-char (if arg
                   (- (point-max)
@@ -426,7 +432,7 @@ Don't use this command in Lisp programs!
   "Deactivate mark; move point right ARG characters \(left if ARG negative).
 On reaching end of buffer, stop and signal error."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (forward-char arg))
 
 (defun forward-word-nomark (&optional arg)
@@ -435,13 +441,13 @@ Normally returns t.
 If an edge of the buffer is reached, point is left there
 and nil is returned."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (forward-word arg))
 
 (defun forward-line-nomark (&optional arg)
   "Deactivate mark; move cursor vertically down ARG lines."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (forward-line arg)
   (setq this-command 'forward-line)
 )
@@ -451,7 +457,7 @@ and nil is returned."
 With argument, do it that many times.  Negative arg -N means
 move backward across N balanced expressions."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (forward-sexp arg))
 
 (defun forward-paragraph-nomark (&optional arg)
@@ -463,7 +469,7 @@ A line which `paragraph-start' matches either separates paragraphs
 A paragraph end is the beginning of a line which is not part of the paragraph
 to which the end of the previous line belongs, or the end of the buffer."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (forward-paragraph arg))
 
 (defun next-line-nomark (&optional arg)
@@ -482,8 +488,8 @@ a semipermanent goal column to which this command always moves.
 Then it does not try to move vertically.  This goal column is stored
 in `goal-column', which is nil when there is none."
   (interactive "p")
-  (setq mark-active nil)
-  (next-line arg)
+  (pc-select-maybe-deactivate-mark)
+  (with-no-warnings (next-line arg))
   (setq this-command 'next-line))
 
 (defun end-of-line-nomark (&optional arg)
@@ -491,14 +497,14 @@ in `goal-column', which is nil when there is none."
 With argument ARG not nil or 1, move forward ARG - 1 lines first.
 If scan reaches end of buffer, stop there without error."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (end-of-line arg)
   (setq this-command 'end-of-line))
 
 (defun backward-line-nomark (&optional arg)
   "Deactivate mark; move cursor vertically up ARG lines."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (if (null arg)
       (setq arg 1))
   (forward-line (- arg))
@@ -511,7 +517,7 @@ A near full screen is `next-screen-context-lines' less than a full screen.
 Negative ARG means scroll upward.
 When calling from a program, supply a number as argument or nil."
   (interactive "P")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (cond (pc-select-override-scroll-error
         (condition-case nil (scroll-down arg)
           (beginning-of-buffer (goto-char (point-min)))))
@@ -527,7 +533,7 @@ of the accessible part of the buffer.
 Don't use this command in Lisp programs!
 \(goto-char (point-max)) is faster and avoids clobbering the mark."
   (interactive "P")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (let ((size (- (point-max) (point-min))))
     (goto-char (if arg
                   (- (point-max)
@@ -560,14 +566,14 @@ Don't use this command in Lisp programs!
 "Ensure mark is active; move point left ARG characters (right if ARG negative).
 On attempt to pass beginning or end of buffer, stop and signal error."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (backward-char arg))
 
 (defun backward-word-mark (&optional arg)
   "Ensure mark is active; move backward until encountering the end of a word.
 With argument, do this that many times."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (backward-word arg))
 
 (defun backward-sexp-mark (&optional arg)
@@ -575,7 +581,7 @@ With argument, do this that many times."
 With argument, do it that many times.  Negative arg -N means
 move forward across N balanced expressions."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (backward-sexp arg))
 
 (defun backward-paragraph-mark (&optional arg)
@@ -590,7 +596,7 @@ blank line.
 
 See `forward-paragraph' for more information."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (backward-paragraph arg))
 
 (defun previous-line-mark (&optional arg)
@@ -607,8 +613,8 @@ If you are thinking of using this in a Lisp program, consider using
 `forward-line' with a negative argument instead.  It is usually easier
 to use and more reliable (no dependence on goal column, etc.)."
   (interactive "p")
-  (ensure-mark)
-  (previous-line arg)
+  (pc-select-ensure-mark)
+  (with-no-warnings (previous-line arg))
   (setq this-command 'previous-line))
 
 (defun beginning-of-line-mark (&optional arg)
@@ -616,7 +622,7 @@ to use and more reliable (no dependence on goal column, etc.)."
 With argument ARG not nil or 1, move forward ARG - 1 lines first.
 If scan reaches end of buffer, stop there without error."
   (interactive "p")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (beginning-of-line arg))
 
 
@@ -626,7 +632,7 @@ A near full screen is `next-screen-context-lines' less than a full screen.
 Negative ARG means scroll downward.
 When calling from a program, supply a number as argument or nil."
   (interactive "P")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (cond (pc-select-override-scroll-error
         (condition-case nil (scroll-up arg)
           (end-of-buffer (goto-char (point-max)))))
@@ -642,7 +648,7 @@ of the accessible part of the buffer.
 Don't use this command in Lisp programs!
 \(goto-char (p\oint-min)) is faster and avoids clobbering the mark."
   (interactive "P")
-  (ensure-mark)
+  (pc-select-ensure-mark)
   (let ((size (- (point-max) (point-min))))
     (goto-char (if arg
                   (+ (point-min)
@@ -662,14 +668,14 @@ Don't use this command in Lisp programs!
   "Deactivate mark; move point left ARG characters (right if ARG negative).
 On attempt to pass beginning or end of buffer, stop and signal error."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (backward-char arg))
 
 (defun backward-word-nomark (&optional arg)
   "Deactivate mark; move backward until encountering the end of a word.
 With argument, do this that many times."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (backward-word arg))
 
 (defun backward-sexp-nomark (&optional arg)
@@ -677,7 +683,7 @@ With argument, do this that many times."
 With argument, do it that many times.  Negative arg -N means
 move forward across N balanced expressions."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (backward-sexp arg))
 
 (defun backward-paragraph-nomark (&optional arg)
@@ -692,7 +698,7 @@ blank line.
 
 See `forward-paragraph' for more information."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (backward-paragraph arg))
 
 (defun previous-line-nomark (&optional arg)
@@ -705,8 +711,8 @@ The command \\[set-goal-column] can be used to create
 a semipermanent goal column to which this command always moves.
 Then it does not try to move vertically."
   (interactive "p")
-  (setq mark-active nil)
-  (previous-line arg)
+  (pc-select-maybe-deactivate-mark)
+  (with-no-warnings (previous-line arg))
   (setq this-command 'previous-line))
 
 (defun beginning-of-line-nomark (&optional arg)
@@ -714,7 +720,7 @@ Then it does not try to move vertically."
 With argument ARG not nil or 1, move forward ARG - 1 lines first.
 If scan reaches end of buffer, stop there without error."
   (interactive "p")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (beginning-of-line arg))
 
 (defun scroll-up-nomark (&optional arg)
@@ -723,7 +729,7 @@ A near full screen is `next-screen-context-lines' less than a full screen.
 Negative ARG means scroll downward.
 When calling from a program, supply a number as argument or nil."
   (interactive "P")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (cond (pc-select-override-scroll-error
         (condition-case nil (scroll-up arg)
           (end-of-buffer (goto-char (point-max)))))
@@ -739,7 +745,7 @@ of the accessible part of the buffer.
 Don't use this command in Lisp programs!
 \(goto-char (point-min)) is faster and avoids clobbering the mark."
   (interactive "P")
-  (setq mark-active nil)
+  (pc-select-maybe-deactivate-mark)
   (let ((size (- (point-max) (point-min))))
     (goto-char (if arg
                   (+ (point-min)
@@ -825,7 +831,7 @@ If the value is non-nil, call the function MODE with an argument of
 
 ;;;###autoload
 (define-minor-mode pc-selection-mode
-  "Change mark behaviour to emulate Motif, MAC or MS-Windows cut and paste style.
+  "Change mark behavior to emulate Motif, MAC or MS-Windows cut and paste style.
 
 This mode enables Delete Selection mode and Transient Mark mode.
 
@@ -842,7 +848,7 @@ M-LEFT and M-RIGHT move back or forward one word or sexp, disabling the mark.
 S-M-LEFT and S-M-RIGHT move back or forward one word or sexp, leaving the mark
 behind.  To control whether these keys move word-wise or sexp-wise set the
 variable `pc-select-meta-moves-sexps' after loading pc-select.el but before
-turning `pc-selection-mode' on.
+turning PC Selection mode on.
 
 C-DOWN and C-UP move back or forward a paragraph, disabling the mark.
 S-C-DOWN and S-C-UP move back or forward a paragraph, leaving the mark behind.
@@ -864,7 +870,7 @@ C-INSERT copies the region into the kill ring (`copy-region-as-kill').
 
 In addition, certain other PC bindings are imitated (to avoid this, set
 the variable `pc-select-selection-keys-only' to t after loading pc-select.el
-but before calling `pc-selection-mode'):
+but before calling PC Selection mode):
 
   F6           other-window
   DELETE       delete-char
@@ -967,20 +973,5 @@ but before calling `pc-selection-mode'):
       (setq pc-select-key-bindings-alist nil
            pc-select-saved-settings-alist nil))))
 
-
-;;;###autoload
-(defcustom pc-selection-mode nil
-  "Toggle PC Selection mode.
-Change mark behaviour to emulate Motif, MAC or MS-Windows cut and paste style,
-and cursor movement commands.
-This mode enables Delete Selection mode and Transient Mark mode.
-You must modify via \\[customize] for this variable to have an effect."
-  :set (lambda (symbol value)
-        (pc-selection-mode (if value 1 -1)))
-  :initialize 'custom-initialize-default
-  :type 'boolean
-  :group 'pc-select
-  :require 'pc-select)
-
-;;; arch-tag: 10697b70-ae07-4f3e-ad23-7814a3f418c2
+;; arch-tag: 10697b70-ae07-4f3e-ad23-7814a3f418c2
 ;;; pc-select.el ends here