[TMP] enable load_prefer_newer
[bpt/emacs.git] / lisp / bindings.el
index 69d96ab..59aa3df 100644 (file)
@@ -1,9 +1,9 @@
 ;;; bindings.el --- define standard key bindings and some variables
 
-;; Copyright (C) 1985-1987, 1992-1996, 1999-2012
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 1985-1987, 1992-1996, 1999-2014 Free Software
+;; Foundation, Inc.
 
-;; Maintainer: FSF
+;; Maintainer: emacs-devel@gnu.org
 ;; Keywords: internal
 ;; Package: emacs
 
@@ -38,64 +38,55 @@ corresponding to the mode line clicked."
 (defun mode-line-toggle-read-only (event)
   "Like `toggle-read-only', for the mode-line."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
-    (with-no-warnings (toggle-read-only))
-    (force-mode-line-update)))
-
+  (with-selected-window (posn-window (event-start event))
+    (read-only-mode 'toggle)))
 
 (defun mode-line-toggle-modified (event)
   "Toggle the buffer-modified flag from the mode-line."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
+  (with-selected-window (posn-window (event-start event))
     (set-buffer-modified-p (not (buffer-modified-p)))
     (force-mode-line-update)))
 
-
 (defun mode-line-widen (event)
   "Widen a buffer from the mode-line."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
+  (with-selected-window (posn-window (event-start event))
     (widen)
     (force-mode-line-update)))
 
-
 (defvar mode-line-input-method-map
   (let ((map (make-sparse-keymap)))
     (define-key map [mode-line mouse-2]
       (lambda (e)
        (interactive "e")
-       (save-selected-window
-         (select-window
-          (posn-window (event-start e)))
+       (with-selected-window (posn-window (event-start e))
          (toggle-input-method)
          (force-mode-line-update))))
     (define-key map [mode-line mouse-3]
       (lambda (e)
        (interactive "e")
-       (save-selected-window
-         (select-window
-          (posn-window (event-start e)))
+       (with-selected-window (posn-window (event-start e))
          (describe-current-input-method))))
     (purecopy map)))
 
-
 (defvar mode-line-coding-system-map
   (let ((map (make-sparse-keymap)))
     (define-key map [mode-line mouse-1]
       (lambda (e)
        (interactive "e")
-       (save-selected-window
-         (select-window (posn-window (event-start e)))
+       (with-selected-window (posn-window (event-start e))
          (when (and enable-multibyte-characters
                     buffer-file-coding-system)
            (describe-coding-system buffer-file-coding-system)))))
+    (define-key map [mode-line mouse-3]
+      (lambda (e)
+       (interactive "e")
+       (with-selected-window (posn-window (event-start e))
+         (call-interactively 'set-buffer-file-coding-system))))
     (purecopy map))
   "Local keymap for the coding-system part of the mode line.")
 
-
 (defun mode-line-change-eol (event)
   "Cycle through the various possible kinds of end-of-line styles."
   (interactive "e")
@@ -116,7 +107,7 @@ corresponding to the mode line clicked."
       (setq desc
            (propertize
             mnemonic
-            'help-echo (format "End-of-line style: %s\nmouse-1 to cycle"
+            'help-echo (format "End-of-line style: %s\nmouse-1: Cycle"
                                (if (eq eol 0) "Unix-style LF"
                                  (if (eq eol 1) "DOS-style CRLF"
                                    (if (eq eol 2) "Mac-style CR"
@@ -130,13 +121,39 @@ corresponding to the mode line clicked."
       (push (cons eol (cons mnemonic desc)) mode-line-eol-desc-cache)
       desc)))
 
-(defvar mode-line-client
-  `(""
-    (:propertize ("" (:eval (if (frame-parameter nil 'client) "@" "")))
-                help-echo ,(purecopy "emacsclient frame")))
-  "Mode-line control for identifying emacsclient frames.")
-;;;###autoload
-(put 'mode-line-client 'risky-local-variable t)
+\f
+;;; Mode line contents
+
+(defcustom mode-line-default-help-echo
+  "mouse-1: Select (drag to resize)\n\
+mouse-2: Make current window occupy the whole frame\n\
+mouse-3: Remove current window from display"
+  "Default help text for the mode line.
+If the value is a string, it specifies the tooltip or echo area
+message to display when the mouse is moved over the mode line.
+If the text at the mouse position has a `help-echo' text
+property, that overrides this variable."
+  :type '(choice (const :tag "No help" :value nil) string)
+  :version "24.3"
+  :group 'mode-line)
+
+(defvar mode-line-front-space '(:eval (if (display-graphic-p) " " "-"))
+  "Mode line construct to put at the front of the mode line.
+By default, this construct is displayed right at the beginning of
+the mode line, except that if there is a memory-full message, it
+is displayed first.")
+(put 'mode-line-front-space 'risky-local-variable t)
+
+(defun mode-line-mule-info-help-echo (window _object _point)
+  "Return help text specifying WINDOW's buffer coding system."
+  (with-current-buffer (window-buffer window)
+    (if buffer-file-coding-system
+       (format "Buffer coding system (%s): %s
+mouse-1: Describe coding system
+mouse-3: Set coding system"
+               (if enable-multibyte-characters "multi-byte" "unibyte")
+               (symbol-name buffer-file-coding-system))
+      "Buffer coding system: none specified")))
 
 (defvar mode-line-mule-info
   `(""
@@ -152,88 +169,55 @@ mouse-3: Describe current input method"))
                  mouse-face mode-line-highlight))
     ,(propertize
       "%z"
-      'help-echo
-      (lambda (window _object _point)
-       (with-current-buffer (window-buffer window)
-         ;; Don't show this tip if the coding system is nil,
-         ;; it reads like a bug, and is not useful anyway.
-         (when buffer-file-coding-system
-           (format "Buffer coding system %s\nmouse-1: describe coding system"
-                   (if enable-multibyte-characters
-                       (concat "(multi-byte): "
-                               (symbol-name buffer-file-coding-system))
-                     (concat "(unibyte): "
-                             (symbol-name buffer-file-coding-system)))))))
+      'help-echo 'mode-line-mule-info-help-echo
       'mouse-face 'mode-line-highlight
       'local-map mode-line-coding-system-map)
     (:eval (mode-line-eol-desc)))
-  "Mode-line control for displaying information of multilingual environment.
+  "Mode line construct to report the multilingual environment.
 Normally it displays current input method (if any activated) and
 mnemonics of the following coding systems:
   coding system for saving or writing the current buffer
-  coding system for keyboard input (if Emacs is running on terminal)
-  coding system for terminal output (if Emacs is running on terminal)"
-  ;; Currently not:
-  ;;  coding system for decoding output of buffer process (if any)
-  ;;  coding system for encoding text to send to buffer process (if any)."
-)
-
+  coding system for keyboard input (on a text terminal)
+  coding system for terminal output (on a text terminal)")
 ;;;###autoload
 (put 'mode-line-mule-info 'risky-local-variable t)
 (make-variable-buffer-local 'mode-line-mule-info)
 
-;; MSDOS frames have window-system, but want the Fn identification.
-(defun mode-line-frame-control ()
-  "Compute mode-line control for frame identification.
-Value is used for `mode-line-frame-identification', which see."
-  (if (or (null window-system)
-         (eq window-system 'pc))
-      "-%F  "
-    "  "))
-
-;; We need to defer the call to mode-line-frame-control to the time
-;; the mode line is actually displayed.
-(defvar mode-line-frame-identification '(:eval (mode-line-frame-control))
-  "Mode-line control to describe the current frame.")
+(defvar mode-line-client
+  `(""
+    (:propertize ("" (:eval (if (frame-parameter nil 'client) "@" "")))
+                help-echo ,(purecopy "emacsclient frame")))
+  "Mode line construct for identifying emacsclient frames.")
 ;;;###autoload
-(put 'mode-line-frame-identification 'risky-local-variable t)
+(put 'mode-line-client 'risky-local-variable t)
 
-(defvar mode-line-process nil "\
-Mode-line control for displaying info on process status.
-Normally nil in most modes, since there is no process to display.")
+(defun mode-line-read-only-help-echo (window _object _point)
+  "Return help text specifying WINDOW's buffer read-only status."
+  (format "Buffer is %s\nmouse-1: Toggle"
+         (if (buffer-local-value 'buffer-read-only (window-buffer window))
+             "read-only"
+           "writable")))
 
-;;;###autoload
-(put 'mode-line-process 'risky-local-variable t)
-(make-variable-buffer-local 'mode-line-process)
+(defun mode-line-modified-help-echo (window _object _point)
+  "Return help text specifying WINDOW's buffer modification status."
+  (format "Buffer is %smodified\nmouse-1: Toggle modification state"
+         (if (buffer-modified-p (window-buffer window)) "" "not ")))
 
 (defvar mode-line-modified
   (list (propertize
         "%1*"
-        'help-echo (purecopy (lambda (window _object _point)
-                               (format "Buffer is %s\nmouse-1 toggles"
-                                       (save-selected-window
-                                         (select-window window)
-                                         (if buffer-read-only
-                                             "read-only"
-                                           "writable")))))
+        'help-echo 'mode-line-read-only-help-echo
         'local-map (purecopy (make-mode-line-mouse-map
                               'mouse-1
                               #'mode-line-toggle-read-only))
         'mouse-face 'mode-line-highlight)
        (propertize
         "%1+"
-        'help-echo  (purecopy (lambda (window _object _point)
-                                (format "Buffer is %sodified\nmouse-1 toggles modified state"
-                                        (save-selected-window
-                                          (select-window window)
-                                          (if (buffer-modified-p)
-                                            "m"
-                                          "not m")))))
+        'help-echo 'mode-line-modified-help-echo
         'local-map (purecopy (make-mode-line-mouse-map
                               'mouse-1 #'mode-line-toggle-modified))
         'mouse-face 'mode-line-highlight))
-  "Mode-line control for displaying whether current buffer is modified.")
-
+  "Mode line construct for displaying whether current buffer is modified.")
 ;;;###autoload
 (put 'mode-line-modified 'risky-local-variable t)
 (make-variable-buffer-local 'mode-line-modified)
@@ -244,40 +228,68 @@ Normally nil in most modes, since there is no process to display.")
         'mouse-face 'mode-line-highlight
         'help-echo (purecopy (lambda (window _object _point)
                                (format "%s"
-                                       (save-selected-window
-                                         (select-window window)
+                                       (with-selected-window window
                                          (concat
                                           (if (file-remote-p default-directory)
                                               "Current directory is remote: "
                                             "Current directory is local: ")
                                           default-directory)))))))
-  "Mode-line flag to show if default-directory for current buffer is remote.")
+  "Mode line construct to indicate a remote buffer.")
 ;;;###autoload
 (put 'mode-line-remote 'risky-local-variable t)
-
 (make-variable-buffer-local 'mode-line-remote)
 
-;; Actual initialization is below.
-(defvar mode-line-position nil
-  "Mode-line control for displaying the position in the buffer.
-Normally displays the buffer percentage and, optionally, the
-buffer size, the line number and the column number.")
+;; MSDOS frames have window-system, but want the Fn identification.
+(defun mode-line-frame-control ()
+  "Compute mode line construct for frame identification.
+Value is used for `mode-line-frame-identification', which see."
+  (if (or (null window-system)
+         (eq window-system 'pc))
+      "-%F  "
+    "  "))
+
+;; We need to defer the call to mode-line-frame-control to the time
+;; the mode line is actually displayed.
+(defvar mode-line-frame-identification '(:eval (mode-line-frame-control))
+  "Mode line construct to describe the current frame.")
 ;;;###autoload
-(put 'mode-line-position 'risky-local-variable t)
+(put 'mode-line-frame-identification 'risky-local-variable t)
 
-(defvar mode-line-modes nil
-  "Mode-line control for displaying major and minor modes.")
+(defvar mode-line-process nil
+  "Mode line construct for displaying info on process status.
+Normally nil in most modes, since there is no process to display.")
 ;;;###autoload
-(put 'mode-line-modes 'risky-local-variable t)
+(put 'mode-line-process 'risky-local-variable t)
+(make-variable-buffer-local 'mode-line-process)
+
+(defun bindings--define-key (map key item)
+  "Make as much as possible of the menus pure."
+  (declare (indent 2))
+  (define-key map key
+    (cond
+     ((not (consp item)) item)     ;Not sure that could be other than a symbol.
+     ;; Keymaps can't be made pure otherwise users can't remove/add elements
+     ;; from/to them any more.
+     ((keymapp item) item)
+     ((stringp (car item))
+      (if (keymapp (cdr item))
+          (cons (purecopy (car item)) (cdr item))
+        (purecopy item)))
+     ((eq 'menu-item (car item))
+      (if (keymapp (nth 2 item))
+          `(menu-item ,(purecopy (nth 1 item)) ,(nth 2 item)
+                      ,@(purecopy (nthcdr 3 item)))
+        (purecopy item)))
+     (t (message "non-menu-item: %S" item) item))))
 
 (defvar mode-line-mode-menu (make-sparse-keymap "Minor Modes") "\
 Menu of mode operations in the mode line.")
 
 (defvar mode-line-major-mode-keymap
   (let ((map (make-sparse-keymap)))
-    (define-key map [mode-line down-mouse-1]
-      `(menu-item ,(purecopy "Menu Bar") ignore
-        :filter (lambda (_) (mouse-menu-major-mode-map))))
+    (bindings--define-key map [mode-line down-mouse-1]
+      `(menu-item "Menu Bar" ignore
+        :filter ,(lambda (_) (mouse-menu-major-mode-map))))
     (define-key map [mode-line mouse-2] 'describe-mode)
     (define-key map [mode-line down-mouse-3] mode-line-mode-menu)
     map) "\
@@ -292,129 +304,91 @@ Keymap to display on major mode.")
     map) "\
 Keymap to display on minor modes.")
 
+(defvar mode-line-modes
+  (let ((recursive-edit-help-echo "Recursive edit, type C-M-c to get out"))
+    (list (propertize "%[" 'help-echo recursive-edit-help-echo)
+         "("
+         `(:propertize ("" mode-name)
+                       help-echo "Major mode\n\
+mouse-1: Display major mode menu\n\
+mouse-2: Show help for major mode\n\
+mouse-3: Toggle minor modes"
+                       mouse-face mode-line-highlight
+                       local-map ,mode-line-major-mode-keymap)
+         '("" mode-line-process)
+         `(:propertize ("" minor-mode-alist)
+                       mouse-face mode-line-highlight
+                       help-echo "Minor mode\n\
+mouse-1: Display minor mode menu\n\
+mouse-2: Show help for minor mode\n\
+mouse-3: Toggle minor modes"
+                       local-map ,mode-line-minor-mode-keymap)
+         (propertize "%n" 'help-echo "mouse-2: Remove narrowing from buffer"
+                     'mouse-face 'mode-line-highlight
+                     'local-map (make-mode-line-mouse-map
+                                 'mouse-2 #'mode-line-widen))
+         ")"
+         (propertize "%]" 'help-echo recursive-edit-help-echo)
+         " "))
+  "Mode line construct for displaying major and minor modes.")
+(put 'mode-line-modes 'risky-local-variable t)
+
 (defvar mode-line-column-line-number-mode-map
   (let ((map (make-sparse-keymap))
        (menu-map (make-sparse-keymap "Toggle Line and Column Number Display")))
-    (define-key menu-map [line-number-mode]
-      `(menu-item ,(purecopy "Display Line Numbers") line-number-mode
-                 :help ,(purecopy "Toggle displaying line numbers in the mode-line")
+    (bindings--define-key menu-map [line-number-mode]
+      '(menu-item "Display Line Numbers" line-number-mode
+                 :help "Toggle displaying line numbers in the mode-line"
                  :button (:toggle . line-number-mode)))
-    (define-key menu-map [column-number-mode]
-      `(menu-item ,(purecopy "Display Column Numbers") column-number-mode
-                 :help ,(purecopy "Toggle displaying column numbers in the mode-line")
+    (bindings--define-key menu-map [column-number-mode]
+      '(menu-item "Display Column Numbers" column-number-mode
+                 :help "Toggle displaying column numbers in the mode-line"
                  :button (:toggle . column-number-mode)))
     (define-key map [mode-line down-mouse-1] menu-map)
     map) "\
 Keymap to display on column and line numbers.")
 
-(let* ((help-echo
-       ;; The multi-line message doesn't work terribly well on the
-       ;; bottom mode line...  Better ideas?
-       ;;        "\
-       ;; mouse-1: select window, mouse-2: delete others, mouse-3: delete,
-       ;; drag-mouse-1: resize, C-mouse-2: split horizontally"
-       "mouse-1: Select (drag to resize)\n\
-mouse-2: Make current window occupy the whole frame\n\
-mouse-3: Remove current window from display")
-       (recursive-edit-help-echo "Recursive edit, type C-M-c to get out")
-       (spaces (propertize " " 'help-echo help-echo))
-       (standard-mode-line-format
-       (list
-        "%e"
-        `(:eval (if (display-graphic-p)
-                    ,(propertize " " 'help-echo help-echo)
-                  ,(propertize "-" 'help-echo help-echo)))
-        'mode-line-mule-info
-        'mode-line-client
-        'mode-line-modified
-        'mode-line-remote
-        'mode-line-frame-identification
-        'mode-line-buffer-identification
-        (propertize "   " 'help-echo help-echo)
-        'mode-line-position
-        '(vc-mode vc-mode)
-        (propertize "  " 'help-echo help-echo)
-        'mode-line-modes
-        `(which-func-mode ("" which-func-format ,spaces))
-        `(global-mode-string ("" global-mode-string ,spaces))
-        `(:eval (unless (display-graphic-p)
-                  ,(propertize "-%-" 'help-echo help-echo)))))
-       (standard-mode-line-modes
-       (list
-        (propertize "%[" 'help-echo recursive-edit-help-echo)
-        (propertize "(" 'help-echo help-echo)
-        `(:propertize ("" mode-name)
-                      help-echo "Major mode\n\
-mouse-1: Display major mode menu\n\
-mouse-2: Show help for major mode\n\
-mouse-3: Toggle minor modes"
-                      mouse-face mode-line-highlight
-                      local-map ,mode-line-major-mode-keymap)
-        '("" mode-line-process)
-        `(:propertize ("" minor-mode-alist)
-                      mouse-face mode-line-highlight
-                      help-echo "Minor mode\n\
-mouse-1: Display minor mode menu\n\
-mouse-2: Show help for minor mode\n\
-mouse-3: Toggle minor modes"
-                      local-map ,mode-line-minor-mode-keymap)
-        (propertize "%n" 'help-echo "mouse-2: Remove narrowing from the current buffer"
-                    'mouse-face 'mode-line-highlight
-                    'local-map (make-mode-line-mouse-map
-                                'mouse-2 #'mode-line-widen))
-        (propertize ")" 'help-echo help-echo)
-        (propertize "%]" 'help-echo recursive-edit-help-echo)
-        spaces))
-
-       (standard-mode-line-position
-       `((-3 ,(propertize
-               "%p"
-               'local-map mode-line-column-line-number-mode-map
-               'mouse-face 'mode-line-highlight
-               ;; XXX needs better description
-               'help-echo "Size indication mode\n\
+(defvar mode-line-position
+  `((-3 ,(propertize
+         "%p"
+         'local-map mode-line-column-line-number-mode-map
+         'mouse-face 'mode-line-highlight
+         ;; XXX needs better description
+         'help-echo "Size indication mode\n\
 mouse-1: Display Line and Column Mode Menu"))
-         (size-indication-mode
-          (8 ,(propertize
-               " of %I"
-               'local-map mode-line-column-line-number-mode-map
-               'mouse-face 'mode-line-highlight
-               ;; XXX needs better description
-               'help-echo "Size indication mode\n\
+    (size-indication-mode
+     (8 ,(propertize
+         " of %I"
+         'local-map mode-line-column-line-number-mode-map
+         'mouse-face 'mode-line-highlight
+         ;; XXX needs better description
+         'help-echo "Size indication mode\n\
 mouse-1: Display Line and Column Mode Menu")))
-         (line-number-mode
-          ((column-number-mode
-            (10 ,(propertize
-                  " (%l,%c)"
-                  'local-map mode-line-column-line-number-mode-map
-                  'mouse-face 'mode-line-highlight
-                  'help-echo "Line number and Column number\n\
+    (line-number-mode
+     ((column-number-mode
+       (10 ,(propertize
+            " (%l,%c)"
+            'local-map mode-line-column-line-number-mode-map
+            'mouse-face 'mode-line-highlight
+            'help-echo "Line number and Column number\n\
 mouse-1: Display Line and Column Mode Menu"))
-            (6 ,(propertize
-                 " L%l"
-                 'local-map mode-line-column-line-number-mode-map
-                 'mouse-face 'mode-line-highlight
-                 'help-echo "Line Number\n\
+       (6 ,(propertize
+           " L%l"
+           'local-map mode-line-column-line-number-mode-map
+           'mouse-face 'mode-line-highlight
+           'help-echo "Line Number\n\
 mouse-1: Display Line and Column Mode Menu"))))
-          ((column-number-mode
-            (5 ,(propertize
-                 " C%c"
-                 'local-map mode-line-column-line-number-mode-map
-                 'mouse-face 'mode-line-highlight
-                 'help-echo "Column number\n\
-mouse-1: Display Line and Column Mode Menu"))))))))
-
-  (setq-default mode-line-format standard-mode-line-format)
-  (put 'mode-line-format 'standard-value
-       (list `(quote ,standard-mode-line-format)))
-
-  (setq-default mode-line-modes standard-mode-line-modes)
-  (put 'mode-line-modes 'standard-value
-       (list `(quote ,standard-mode-line-modes)))
-
-  (setq-default mode-line-position standard-mode-line-position)
-  (put 'mode-line-position 'standard-value
-       (list `(quote ,standard-mode-line-position))))
+     ((column-number-mode
+       (5 ,(propertize
+           " C%c"
+           'local-map mode-line-column-line-number-mode-map
+           'mouse-face 'mode-line-highlight
+           'help-echo "Column number\n\
+mouse-1: Display Line and Column Mode Menu"))))))
+  "Mode line construct for displaying the position in the buffer.
+Normally displays the buffer percentage and, optionally, the
+buffer size, the line number and the column number.")
+(put 'mode-line-position 'risky-local-variable t)
 
 (defvar mode-line-buffer-identification-keymap
   ;; Add menu of buffer operations to the buffer identification part
@@ -438,34 +412,65 @@ text properties for face, help-echo, and local-map to it."
   (list (propertize fmt
                    'face 'mode-line-buffer-id
                    'help-echo
-                   (purecopy "Buffer name\n\
-mouse-1: previous buffer\n\
-mouse-3: next buffer")
+                   (purecopy "Buffer name
+mouse-1: Previous buffer\nmouse-3: Next buffer")
                    'mouse-face 'mode-line-highlight
                    'local-map mode-line-buffer-identification-keymap)))
 
-(defvar mode-line-buffer-identification (propertized-buffer-identification "%12b") "\
-Mode-line control for identifying the buffer being displayed.
+(defvar mode-line-buffer-identification
+  (propertized-buffer-identification "%12b")
+  "Mode line construct for identifying the buffer being displayed.
 Its default value is (\"%12b\") with some text properties added.
 Major modes that edit things other than ordinary files may change this
 \(e.g. Info, Dired,...)")
-
 ;;;###autoload
 (put 'mode-line-buffer-identification 'risky-local-variable t)
 (make-variable-buffer-local 'mode-line-buffer-identification)
 
+(defvar mode-line-misc-info
+  '((which-func-mode ("" which-func-format " "))
+    (global-mode-string ("" global-mode-string " ")))
+  "Mode line construct for miscellaneous information.
+By default, this shows the information specified by
+`which-func-mode' and `global-mode-string'.")
+(put 'mode-line-misc-info 'risky-local-variable t)
+
+(defvar mode-line-end-spaces '(:eval (unless (display-graphic-p) "-%-"))
+  "Mode line construct to put at the end of the mode line.")
+(put 'mode-line-end-spaces 'risky-local-variable t)
+
+;; Default value of the top-level `mode-line-format' variable:
+(let ((standard-mode-line-format
+       (list "%e"
+            'mode-line-front-space
+            'mode-line-mule-info
+            'mode-line-client
+            'mode-line-modified
+            'mode-line-remote
+            'mode-line-frame-identification
+            'mode-line-buffer-identification
+            "   "
+            'mode-line-position
+            '(vc-mode vc-mode)
+            "  "
+            'mode-line-modes
+            'mode-line-misc-info
+            'mode-line-end-spaces)))
+  (setq-default mode-line-format standard-mode-line-format)
+  (put 'mode-line-format 'standard-value
+       (list `(quote ,standard-mode-line-format))))
+
+\f
 (defun mode-line-unbury-buffer (event) "\
 Call `unbury-buffer' in this window."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
+  (with-selected-window (posn-window (event-start event))
     (unbury-buffer)))
 
 (defun mode-line-bury-buffer (event) "\
 Like `bury-buffer', but temporarily select EVENT's window."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
+  (with-selected-window (posn-window (event-start event))
     (bury-buffer)))
 
 (defun mode-line-other-buffer () "\
@@ -476,15 +481,13 @@ Switch to the most recently selected buffer other than the current one."
 (defun mode-line-next-buffer (event)
   "Like `next-buffer', but temporarily select EVENT's window."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
+  (with-selected-window (posn-window (event-start event))
     (next-buffer)))
 
 (defun mode-line-previous-buffer (event)
   "Like `previous-buffer', but temporarily select EVENT's window."
   (interactive "e")
-  (save-selected-window
-    (select-window (posn-window (event-start event)))
+  (with-selected-window (posn-window (event-start event))
     (previous-buffer)))
 
 (defmacro bound-and-true-p (var)
@@ -493,51 +496,51 @@ Switch to the most recently selected buffer other than the current one."
 
 ;; Use mode-line-mode-menu for local minor-modes only.
 ;; Global ones can go on the menubar (Options --> Show/Hide).
-(define-key mode-line-mode-menu [overwrite-mode]
-  `(menu-item ,(purecopy "Overwrite (Ovwrt)") overwrite-mode
-             :help ,(purecopy "Overwrite mode: typed characters replace existing text")
+(bindings--define-key mode-line-mode-menu [overwrite-mode]
+  '(menu-item "Overwrite (Ovwrt)" overwrite-mode
+             :help "Overwrite mode: typed characters replace existing text"
              :button (:toggle . overwrite-mode)))
-(define-key mode-line-mode-menu [outline-minor-mode]
-  `(menu-item ,(purecopy "Outline (Outl)") outline-minor-mode
+(bindings--define-key mode-line-mode-menu [outline-minor-mode]
+  '(menu-item "Outline (Outl)" outline-minor-mode
              ;; XXX: This needs a good, brief description.
-             :help ,(purecopy "")
+             :help ""
              :button (:toggle . (bound-and-true-p outline-minor-mode))))
-(define-key mode-line-mode-menu [highlight-changes-mode]
-  `(menu-item ,(purecopy "Highlight changes (Chg)") highlight-changes-mode
-             :help ,(purecopy "Show changes in the buffer in a distinctive color")
+(bindings--define-key mode-line-mode-menu [highlight-changes-mode]
+  '(menu-item "Highlight changes (Chg)" highlight-changes-mode
+             :help "Show changes in the buffer in a distinctive color"
              :button (:toggle . (bound-and-true-p highlight-changes-mode))))
-(define-key mode-line-mode-menu [hide-ifdef-mode]
-  `(menu-item ,(purecopy "Hide ifdef (Ifdef)") hide-ifdef-mode
-             :help ,(purecopy "Show/Hide code within #ifdef constructs")
+(bindings--define-key mode-line-mode-menu [hide-ifdef-mode]
+  '(menu-item "Hide ifdef (Ifdef)" hide-ifdef-mode
+             :help "Show/Hide code within #ifdef constructs"
              :button (:toggle . (bound-and-true-p hide-ifdef-mode))))
-(define-key mode-line-mode-menu [glasses-mode]
-  `(menu-item ,(purecopy "Glasses (o^o)") glasses-mode
-             :help ,(purecopy "Insert virtual separators to make long identifiers easy to read")
+(bindings--define-key mode-line-mode-menu [glasses-mode]
+  '(menu-item "Glasses (o^o)" glasses-mode
+             :help "Insert virtual separators to make long identifiers easy to read"
              :button (:toggle . (bound-and-true-p glasses-mode))))
-(define-key mode-line-mode-menu [font-lock-mode]
-  `(menu-item ,(purecopy "Font Lock") font-lock-mode
-             :help ,(purecopy "Syntax coloring")
+(bindings--define-key mode-line-mode-menu [font-lock-mode]
+  '(menu-item "Font Lock" font-lock-mode
+             :help "Syntax coloring"
              :button (:toggle . font-lock-mode)))
-(define-key mode-line-mode-menu [flyspell-mode]
-  `(menu-item ,(purecopy "Flyspell (Fly)") flyspell-mode
-             :help ,(purecopy "Spell checking on the fly")
+(bindings--define-key mode-line-mode-menu [flyspell-mode]
+  '(menu-item "Flyspell (Fly)" flyspell-mode
+             :help "Spell checking on the fly"
              :button (:toggle . (bound-and-true-p flyspell-mode))))
-(define-key mode-line-mode-menu [auto-revert-tail-mode]
-  `(menu-item ,(purecopy "Auto revert tail (Tail)") auto-revert-tail-mode
-             :help ,(purecopy "Revert the tail of the buffer when buffer grows")
+(bindings--define-key mode-line-mode-menu [auto-revert-tail-mode]
+  '(menu-item "Auto revert tail (Tail)" auto-revert-tail-mode
+             :help "Revert the tail of the buffer when buffer grows"
              :enable (buffer-file-name)
              :button (:toggle . (bound-and-true-p auto-revert-tail-mode))))
-(define-key mode-line-mode-menu [auto-revert-mode]
-  `(menu-item ,(purecopy "Auto revert (ARev)") auto-revert-mode
-             :help ,(purecopy "Revert the buffer when the file on disk changes")
+(bindings--define-key mode-line-mode-menu [auto-revert-mode]
+  '(menu-item "Auto revert (ARev)" auto-revert-mode
+             :help "Revert the buffer when the file on disk changes"
              :button (:toggle . (bound-and-true-p auto-revert-mode))))
-(define-key mode-line-mode-menu [auto-fill-mode]
-  `(menu-item ,(purecopy "Auto fill (Fill)") auto-fill-mode
-             :help ,(purecopy "Automatically insert new lines")
+(bindings--define-key mode-line-mode-menu [auto-fill-mode]
+  '(menu-item "Auto fill (Fill)" auto-fill-mode
+             :help "Automatically insert new lines"
              :button (:toggle . auto-fill-function)))
-(define-key mode-line-mode-menu [abbrev-mode]
-  `(menu-item ,(purecopy "Abbrev (Abbrev)") abbrev-mode
-             :help ,(purecopy "Automatically expand abbreviations")
+(bindings--define-key mode-line-mode-menu [abbrev-mode]
+  '(menu-item "Abbrev (Abbrev)" abbrev-mode
+             :help "Automatically expand abbreviations"
              :button (:toggle . abbrev-mode)))
 
 (defun mode-line-minor-mode-help (event)
@@ -551,8 +554,8 @@ Alist saying how to show minor modes in the mode line.
 Each element looks like (VARIABLE STRING);
 STRING is included in the mode line if VARIABLE's value is non-nil.
 
-Actually, STRING need not be a string; any possible mode-line element
-is okay.  See `mode-line-format'.")
+Actually, STRING need not be a string; any mode-line construct is
+okay.  See `mode-line-format'.")
 ;;;###autoload
 (put 'minor-mode-alist 'risky-local-variable t)
 ;; Don't use purecopy here--some people want to change these strings.
@@ -632,9 +635,22 @@ is okay.  See `mode-line-format'.")
         user-error ;; That's the main one!
         ))
 
-
 (make-variable-buffer-local 'indent-tabs-mode)
 
+;; These per-buffer variables are never reset by
+;; `kill-all-local-variables', because they have no default value.
+;; For consistency, we give them the `permanent-local' property, even
+;; though `kill-all-local-variables' does not actually consult it.
+
+(mapc (lambda (sym) (put sym 'permanent-local t))
+      '(buffer-file-name default-directory buffer-backed-up
+       buffer-saved-size buffer-auto-save-file-name
+       buffer-read-only buffer-undo-list mark-active
+       point-before-scroll buffer-file-truename
+       buffer-file-format buffer-auto-save-file-format
+       buffer-display-count buffer-display-time
+       enable-multibyte-characters))
+
 ;; We have base64, md5 and sha1 functions built in now.
 (provide 'base64)
 (provide 'md5)
@@ -659,7 +675,7 @@ language you are using."
 (garbage-collect)
 \f
 
-(setq help-event-list '(help f1))
+(setq help-event-list '(help f1 ?\?))
 
 (make-variable-buffer-local 'minor-mode-overriding-map-alist)
 
@@ -680,29 +696,57 @@ language you are using."
 (put 'narrow-to-region 'disabled t)
 
 ;; Moving with arrows in bidi-sensitive direction.
+(defcustom visual-order-cursor-movement nil
+  "If non-nil, moving cursor with arrow keys follows the visual order.
+
+When this is non-nil, \\[right-char] will move to the character that is
+to the right of point on display, and \\[left-char] will move to the left,
+disregarding the surrounding bidirectional context.  Depending on the
+bidirectional context of the surrounding characters, this can move point
+many buffer positions away.
+
+When the text is entirely left-to-right, logical-order and visual-order
+cursor movements produce identical results."
+  :type '(choice (const :tag "Logical-order cursor movement" nil)
+                (const :tag "Visual-order cursor movement" t))
+  :group 'display
+  :version "24.4")
+
 (defun right-char (&optional n)
   "Move point N characters to the right (to the left if N is negative).
 On reaching beginning or end of buffer, stop and signal error.
 
-Depending on the bidirectional context, this may move either forward
-or backward in the buffer.  This is in contrast with \\[forward-char]
-and \\[backward-char], which see."
+If `visual-order-cursor-movement' is non-nil, this always moves
+to the right on display, wherever that is in the buffer.
+Otherwise, depending on the bidirectional context, this may move
+one position either forward or backward in the buffer.  This is
+in contrast with \\[forward-char] and \\[backward-char], which
+see."
   (interactive "^p")
-  (if (eq (current-bidi-paragraph-direction) 'left-to-right)
-      (forward-char n)
-    (backward-char n)))
+  (if visual-order-cursor-movement
+      (dotimes (i (if (numberp n) (abs n) 1))
+       (move-point-visually (if (and (numberp n) (< n 0)) -1 1)))
+    (if (eq (current-bidi-paragraph-direction) 'left-to-right)
+       (forward-char n)
+      (backward-char n))))
 
 (defun left-char ( &optional n)
   "Move point N characters to the left (to the right if N is negative).
 On reaching beginning or end of buffer, stop and signal error.
 
-Depending on the bidirectional context, this may move either backward
-or forward in the buffer.  This is in contrast with \\[backward-char]
-and \\[forward-char], which see."
+If `visual-order-cursor-movement' is non-nil, this always moves
+to the left on display, wherever that is in the buffer.
+Otherwise, depending on the bidirectional context, this may move
+one position either backward or forward in the buffer.  This is
+in contrast with \\[forward-char] and \\[backward-char], which
+see."
   (interactive "^p")
-  (if (eq (current-bidi-paragraph-direction) 'left-to-right)
-      (backward-char n)
-    (forward-char n)))
+  (if visual-order-cursor-movement
+      (dotimes (i (if (numberp n) (abs n) 1))
+       (move-point-visually (if (and (numberp n) (< n 0)) 1 -1)))
+    (if (eq (current-bidi-paragraph-direction) 'left-to-right)
+       (backward-char n)
+      (forward-char n))))
 
 (defun right-word (&optional n)
   "Move point N words to the right (to the left if N is negative).
@@ -751,7 +795,6 @@ if `inhibit-field-text-motion' is non-nil."
 ;; suspend only the relevant terminal.
 (substitute-key-definition 'suspend-emacs 'suspend-frame global-map)
 
-(define-key global-map "\C-j" 'newline-and-indent)
 (define-key global-map "\C-m" 'newline)
 (define-key global-map "\C-o" 'open-line)
 (define-key esc-map "\C-o" 'split-line)
@@ -786,16 +829,20 @@ if `inhibit-field-text-motion' is non-nil."
 
 (define-key ctl-x-map [right] 'next-buffer)
 (define-key ctl-x-map [C-right] 'next-buffer)
+(define-key global-map [XF86Forward] 'next-buffer)
 (define-key ctl-x-map [left] 'previous-buffer)
 (define-key ctl-x-map [C-left] 'previous-buffer)
+(define-key global-map [XF86Back] 'previous-buffer)
 
 (let ((map minibuffer-local-map))
   (define-key map "\en"   'next-history-element)
   (define-key map [next]  'next-history-element)
   (define-key map [down]  'next-history-element)
+  (define-key map [XF86Forward] 'next-history-element)
   (define-key map "\ep"   'previous-history-element)
   (define-key map [prior] 'previous-history-element)
   (define-key map [up]    'previous-history-element)
+  (define-key map [XF86Back] 'previous-history-element)
   (define-key map "\es"   'next-matching-history-element)
   (define-key map "\er"   'previous-matching-history-element)
   ;; Override the global binding (which calls indent-relative via
@@ -825,6 +872,11 @@ if `inhibit-field-text-motion' is non-nil."
 
 ;; Update tutorial--default-keys if you change these.
 (define-key global-map "\177" 'delete-backward-char)
+;; We explicitly want C-d to use `delete-char' instead of
+;; `delete-forward-char' so that it ignores `delete-active-region':
+;; Most C-d users are old-timers who don't expect
+;; `delete-active-region' here, while newer users who expect
+;; `delete-active-region' use C-d much less.
 (define-key global-map "\C-d" 'delete-char)
 
 (define-key global-map "\C-k" 'kill-line)
@@ -843,6 +895,7 @@ if `inhibit-field-text-motion' is non-nil."
 
 (define-key ctl-x-map "\C-x" 'exchange-point-and-mark)
 (define-key ctl-x-map "\C-@" 'pop-global-mark)
+(define-key ctl-x-map " " 'rectangle-mark-mode)
 (define-key ctl-x-map [?\C- ] 'pop-global-mark)
 
 (define-key global-map "\C-n" 'next-line)
@@ -864,6 +917,7 @@ if `inhibit-field-text-motion' is non-nil."
 (define-key goto-map "\M-n" 'next-error)
 (define-key goto-map    "p" 'previous-error)
 (define-key goto-map "\M-p" 'previous-error)
+(define-key goto-map   "\t" 'move-to-column)
 
 (defvar search-map (make-sparse-keymap)
   "Keymap for search related commands.")
@@ -873,6 +927,7 @@ if `inhibit-field-text-motion' is non-nil."
 (define-key search-map "hr" 'highlight-regexp)
 (define-key search-map "hp" 'highlight-phrase)
 (define-key search-map "hl" 'highlight-lines-matching-regexp)
+(define-key search-map "h." 'highlight-symbol-at-point)
 (define-key search-map "hu" 'unhighlight-regexp)
 (define-key search-map "hf" 'hi-lock-find-patterns)
 (define-key search-map "hw" 'hi-lock-write-interactive-patterns)
@@ -1005,36 +1060,34 @@ if `inhibit-field-text-motion' is non-nil."
 ;; FIXME: rather than list such mappings for every modifier-combination,
 ;;   we should come up with a way to do it generically, something like
 ;;   (define-key function-key-map [*-kp-home] [*-home])
-(define-key function-key-map [kp-home] [home])
-(define-key function-key-map [kp-left] [left])
-(define-key function-key-map [kp-up] [up])
-(define-key function-key-map [kp-right] [right])
-(define-key function-key-map [kp-down] [down])
-(define-key function-key-map [kp-prior] [prior])
-(define-key function-key-map [kp-next] [next])
-(define-key function-key-map [M-kp-next] [M-next])
-(define-key function-key-map [kp-end] [end])
-(define-key function-key-map [kp-begin] [begin])
-(define-key function-key-map [kp-insert] [insert])
+;; Currently we add keypad key combinations with basic modifiers
+;; (to complement plain bindings in "Keypad support" section in simple.el)
+;; Until [*-kp-home] is implemented, for more modifiers we could also use:
+;; (todo-powerset '(control meta shift hyper super alt))  (Bug#14397)
+(let ((modifiers '(nil (control) (meta) (control meta) (shift)
+                  (control shift) (meta shift) (control meta shift)))
+      (keys '((kp-delete delete) (kp-insert insert)
+             (kp-end end) (kp-down down) (kp-next next)
+             (kp-left left) (kp-begin begin) (kp-right right)
+             (kp-home home) (kp-up up) (kp-prior prior)
+             (kp-enter enter) (kp-decimal ?.)
+             (kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
+             (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
+             (kp-add ?+) (kp-subtract ?-) (kp-multiply ?*) (kp-divide ?/))))
+  (dolist (pair keys)
+    (let ((keypad (nth 0 pair))
+         (normal (nth 1 pair)))
+      (when (characterp normal)
+       (put keypad 'ascii-character normal))
+      (dolist (mod modifiers)
+       (define-key function-key-map
+         (vector (append mod (list keypad)))
+         (vector (append mod (list normal))))))))
+
 (define-key function-key-map [backspace] [?\C-?])
 (define-key function-key-map [delete] [?\C-?])
 (define-key function-key-map [kp-delete] [?\C-?])
-(define-key function-key-map [S-kp-end] [S-end])
-(define-key function-key-map [S-kp-down] [S-down])
-(define-key function-key-map [S-kp-next] [S-next])
-(define-key function-key-map [S-kp-left] [S-left])
-(define-key function-key-map [S-kp-right] [S-right])
-(define-key function-key-map [S-kp-home] [S-home])
-(define-key function-key-map [S-kp-up] [S-up])
-(define-key function-key-map [S-kp-prior] [S-prior])
-(define-key function-key-map [C-S-kp-end] [C-S-end])
-(define-key function-key-map [C-S-kp-down] [C-S-down])
-(define-key function-key-map [C-S-kp-next] [C-S-next])
-(define-key function-key-map [C-S-kp-left] [C-S-left])
-(define-key function-key-map [C-S-kp-right] [C-S-right])
-(define-key function-key-map [C-S-kp-home] [C-S-home])
-(define-key function-key-map [C-S-kp-up] [C-S-up])
-(define-key function-key-map [C-S-kp-prior] [C-S-prior])
+
 ;; Don't bind shifted keypad numeric keys, they reportedly
 ;; interfere with the feature of some keyboards to produce
 ;; numbers when NumLock is off.
@@ -1046,14 +1099,14 @@ if `inhibit-field-text-motion' is non-nil."
 ;(define-key function-key-map [S-kp-7] [S-home])
 ;(define-key function-key-map [S-kp-8] [S-up])
 ;(define-key function-key-map [S-kp-9] [S-prior])
-(define-key function-key-map [C-S-kp-1] [C-S-end])
-(define-key function-key-map [C-S-kp-2] [C-S-down])
-(define-key function-key-map [C-S-kp-3] [C-S-next])
-(define-key function-key-map [C-S-kp-4] [C-S-left])
-(define-key function-key-map [C-S-kp-6] [C-S-right])
-(define-key function-key-map [C-S-kp-7] [C-S-home])
-(define-key function-key-map [C-S-kp-8] [C-S-up])
-(define-key function-key-map [C-S-kp-9] [C-S-prior])
+;(define-key function-key-map [C-S-kp-1] [C-S-end])
+;(define-key function-key-map [C-S-kp-2] [C-S-down])
+;(define-key function-key-map [C-S-kp-3] [C-S-next])
+;(define-key function-key-map [C-S-kp-4] [C-S-left])
+;(define-key function-key-map [C-S-kp-6] [C-S-right])
+;(define-key function-key-map [C-S-kp-7] [C-S-home])
+;(define-key function-key-map [C-S-kp-8] [C-S-up])
+;(define-key function-key-map [C-S-kp-9] [C-S-prior])
 
 ;; Hitting C-SPC on text terminals, usually sends the ascii code 0 (aka C-@),
 ;; so we can't distinguish those two keys, but usually we consider C-SPC
@@ -1148,7 +1201,30 @@ if `inhibit-field-text-motion' is non-nil."
 (define-key ctl-x-5-map "m" 'compose-mail-other-frame)
 \f
 
-(defvar ctl-x-r-map (make-sparse-keymap)
+(defvar ctl-x-r-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "c" 'clear-rectangle)
+    (define-key map "k" 'kill-rectangle)
+    (define-key map "d" 'delete-rectangle)
+    (define-key map "y" 'yank-rectangle)
+    (define-key map "o" 'open-rectangle)
+    (define-key map "t" 'string-rectangle)
+    (define-key map "N" 'rectangle-number-lines)
+    (define-key map "\M-w" 'copy-rectangle-as-kill)
+    (define-key map "\C-@" 'point-to-register)
+    (define-key map [?\C-\ ] 'point-to-register)
+    (define-key map " " 'point-to-register)
+    (define-key map "j" 'jump-to-register)
+    (define-key map "s" 'copy-to-register)
+    (define-key map "x" 'copy-to-register)
+    (define-key map "i" 'insert-register)
+    (define-key map "g" 'insert-register)
+    (define-key map "r" 'copy-rectangle-to-register)
+    (define-key map "n" 'number-to-register)
+    (define-key map "+" 'increment-register)
+    (define-key map "w" 'window-configuration-to-register)
+    (define-key map "f" 'frameset-to-register)
+    map)
   "Keymap for subcommands of C-x r.")
 (define-key ctl-x-map "r" ctl-x-r-map)