bindings.el 47.3 KB
Newer Older
1
;;; bindings.el --- define standard key bindings and some variables
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3
;; Copyright (C) 1985-1987, 1992-1996, 1999-2011
4
;;   Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
5 6 7

;; Maintainer: FSF
;; Keywords: internal
8
;; Package: emacs
Richard M. Stallman's avatar
Richard M. Stallman committed
9 10 11

;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18 19 20 21 22

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28

;;; Commentary:

;;; Code:

29 30 31
(defun make-mode-line-mouse-map (mouse function) "\
Return a keymap with single entry for mouse key MOUSE on the mode line.
MOUSE is defined to run function FUNCTION with no args in the buffer
32 33
corresponding to the mode line clicked."
  (let ((map (make-sparse-keymap)))
34
    (define-key map (vector 'mode-line mouse) function)
35 36
    map))

Gerd Moellmann's avatar
Gerd Moellmann committed
37 38 39 40 41 42 43 44 45 46 47 48

(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)))
    (toggle-read-only)
    (force-mode-line-update)))


(defun mode-line-toggle-modified (event)
  "Toggle the buffer-modified flag from the mode-line."
49
  (interactive "e")
Gerd Moellmann's avatar
Gerd Moellmann committed
50 51 52 53 54 55 56 57
  (save-selected-window
    (select-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."
58
  (interactive "e")
Gerd Moellmann's avatar
Gerd Moellmann committed
59 60 61 62 63 64
  (save-selected-window
    (select-window (posn-window (event-start event)))
    (widen)
    (force-mode-line-update)))


65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
(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)))
	  (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)))
	  (describe-current-input-method))))
    (purecopy map)))

84 85 86

(defvar mode-line-coding-system-map
  (let ((map (make-sparse-keymap)))
87
    (define-key map [mode-line mouse-1]
88 89 90 91 92 93 94 95 96 97 98
      (lambda (e)
	(interactive "e")
	(save-selected-window
	  (select-window (posn-window (event-start e)))
	  (when (and enable-multibyte-characters
		     buffer-file-coding-system)
	    (describe-coding-system buffer-file-coding-system)))))
    (purecopy map))
  "Local keymap for the coding-system part of the mode line.")


Kenichi Handa's avatar
Kenichi Handa committed
99
(defun mode-line-change-eol (event)
100
  "Cycle through the various possible kinds of end-of-line styles."
Kenichi Handa's avatar
Kenichi Handa committed
101
  (interactive "e")
102
  (with-selected-window (posn-window (event-start event))
Kenichi Handa's avatar
Kenichi Handa committed
103 104 105
    (let ((eol (coding-system-eol-type buffer-file-coding-system)))
      (set-buffer-file-coding-system
       (cond ((eq eol 0) 'dos) ((eq eol 1) 'mac) (t 'unix))))))
106 107 108 109 110 111

(defvar mode-line-eol-desc-cache nil)

(defun mode-line-eol-desc ()
  (let* ((eol (coding-system-eol-type buffer-file-coding-system))
	 (mnemonic (coding-system-eol-type-mnemonic buffer-file-coding-system))
112
	 (desc (assoc eol mode-line-eol-desc-cache)))
113 114 115 116 117 118
    (if (and desc (eq (cadr desc) mnemonic))
	(cddr desc)
      (if desc (setq mode-line-eol-desc-cache nil)) ;Flush the cache if stale.
      (setq desc
	    (propertize
	     mnemonic
Dan Nicolaescu's avatar
Dan Nicolaescu committed
119
	     'help-echo (format "End-of-line style: %s\nmouse-1 to cycle"
120
				(if (eq eol 0) "Unix-style LF"
121
				  (if (eq eol 1) "DOS-style CRLF"
122 123 124 125 126
				    (if (eq eol 2) "Mac-style CR"
				      "Undecided"))))
	     'keymap
	     (eval-when-compile
	       (let ((map (make-sparse-keymap)))
127
		 (define-key map [mode-line mouse-1] 'mode-line-change-eol)
128 129
		 map))
	     'mouse-face 'mode-line-highlight))
130 131 132
      (push (cons eol (cons mnemonic desc)) mode-line-eol-desc-cache)
      desc)))

133 134 135
(defvar mode-line-client
  `(""
    (:propertize ("" (:eval (if (frame-parameter nil 'client) "@" "")))
136
		 help-echo ,(purecopy "emacsclient frame")))
137
  "Mode-line control for identifying emacsclient frames.")
138
;;;###autoload
139
(put 'mode-line-client 'risky-local-variable t)
140

141 142
(defvar mode-line-mule-info
  `(""
143
    (current-input-method
144 145
     (:propertize ("" current-input-method-title)
		  help-echo (concat
146
			     ,(purecopy "Current input method: ")
147
			     current-input-method
148
			     ,(purecopy "\n\
149
mouse-2: Disable input method\n\
150
mouse-3: Describe current input method"))
151 152
		  local-map ,mode-line-input-method-map
		  mouse-face mode-line-highlight))
153
    ,(propertize
154
      "%z"
155
      'help-echo
156 157 158 159 160 161 162 163 164 165 166
      (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)))))))
167
      'mouse-face 'mode-line-highlight
168 169
      'local-map mode-line-coding-system-map)
    (:eval (mode-line-eol-desc)))
170 171 172 173 174
  "Mode-line control for displaying information of 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)
Dave Love's avatar
Dave Love committed
175
  coding system for terminal output (if Emacs is running on terminal)"
176 177 178
  ;; Currently not:
  ;;  coding system for decoding output of buffer process (if any)
  ;;  coding system for encoding text to send to buffer process (if any)."
Dave Love's avatar
Dave Love committed
179
)
180

181
;;;###autoload
182
(put 'mode-line-mule-info 'risky-local-variable t)
183 184
(make-variable-buffer-local 'mode-line-mule-info)

185 186 187 188
;; 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."
189 190
  (if (or (null window-system)
	  (eq window-system 'pc))
191 192 193
      "-%F  "
    "  "))

194 195
;; We need to defer the call to mode-line-frame-control to the time
;; the mode line is actually displayed.
196 197
(defvar mode-line-frame-identification '(:eval (mode-line-frame-control))
  "Mode-line control to describe the current frame.")
198
;;;###autoload
199
(put 'mode-line-frame-identification 'risky-local-variable t)
200

201 202
(defvar mode-line-process nil "\
Mode-line control for displaying info on process status.
Richard M. Stallman's avatar
Richard M. Stallman committed
203 204
Normally nil in most modes, since there is no process to display.")

205
;;;###autoload
206
(put 'mode-line-process 'risky-local-variable t)
Richard M. Stallman's avatar
Richard M. Stallman committed
207 208
(make-variable-buffer-local 'mode-line-process)

209 210
(defvar mode-line-modified
  (list (propertize
211
	 "%1*"
212
	 'help-echo (purecopy (lambda (window _object _point)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
213
 				(format "Buffer is %s\nmouse-1 toggles"
214 215 216
					(save-selected-window
					  (select-window window)
					  (if buffer-read-only
Dan Nicolaescu's avatar
Dan Nicolaescu committed
217 218
					      "read-only"
					    "writable")))))
219
	 'local-map (purecopy (make-mode-line-mouse-map
220
			       'mouse-1
221 222
			       #'mode-line-toggle-read-only))
	 'mouse-face 'mode-line-highlight)
223 224
	(propertize
	 "%1+"
225
	 'help-echo  (purecopy (lambda (window _object _point)
226
				 (format "Buffer is %sodified\nmouse-1 toggles modified state"
227 228 229
					 (save-selected-window
					   (select-window window)
					   (if (buffer-modified-p)
230 231
					     "m"
					   "not m")))))
232
	 'local-map (purecopy (make-mode-line-mouse-map
233
			       'mouse-1 #'mode-line-toggle-modified))
234
	 'mouse-face 'mode-line-highlight))
Richard M. Stallman's avatar
Richard M. Stallman committed
235 236
  "Mode-line control for displaying whether current buffer is modified.")

237
;;;###autoload
238
(put 'mode-line-modified 'risky-local-variable t)
Richard M. Stallman's avatar
Richard M. Stallman committed
239 240
(make-variable-buffer-local 'mode-line-modified)

241 242
(defvar mode-line-remote
  (list (propertize
243
	 "%1@"
244
	 'mouse-face 'mode-line-highlight
245
	 'help-echo (purecopy (lambda (window _object _point)
246 247 248
 				(format "%s"
					(save-selected-window
					  (select-window window)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
249
					  (concat
250 251 252 253
					   (if (file-remote-p default-directory)
					       "Current directory is remote: "
					     "Current directory is local: ")
					   default-directory)))))))
254
  "Mode-line flag to show if default-directory for current buffer is remote.")
255
;;;###autoload
256
(put 'mode-line-remote 'risky-local-variable t)
257 258 259

(make-variable-buffer-local 'mode-line-remote)

260 261
;; Actual initialization is below.
(defvar mode-line-position nil
Kenichi Handa's avatar
Kenichi Handa committed
262 263 264
  "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.")
265
;;;###autoload
266
(put 'mode-line-position 'risky-local-variable t)
267 268 269

(defvar mode-line-modes nil
  "Mode-line control for displaying major and minor modes.")
270
;;;###autoload
271
(put 'mode-line-modes 'risky-local-variable t)
272

273 274 275
(defvar mode-line-mode-menu (make-sparse-keymap "Minor Modes") "\
Menu of mode operations in the mode line.")

Kenichi Handa's avatar
Kenichi Handa committed
276
(defvar mode-line-major-mode-keymap
277
  (let ((map (make-sparse-keymap)))
278
    (define-key map [mode-line down-mouse-1]
279
      `(menu-item ,(purecopy "Menu Bar") ignore
280
        :filter (lambda (_) (mouse-menu-major-mode-map))))
281
    (define-key map [mode-line mouse-2] 'describe-mode)
282
    (define-key map [mode-line down-mouse-3] mode-line-mode-menu)
283
    map) "\
284 285
Keymap to display on major mode.")

Kenichi Handa's avatar
Kenichi Handa committed
286
(defvar mode-line-minor-mode-keymap
287
  (let ((map (make-sparse-keymap)))
288
    (define-key map [mode-line down-mouse-1] 'mouse-minor-mode-menu)
289
    (define-key map [mode-line mouse-2] 'mode-line-minor-mode-help)
290 291
    (define-key map [mode-line down-mouse-3] mode-line-mode-menu)
    (define-key map [header-line down-mouse-3] mode-line-mode-menu)
292
    map) "\
293 294
Keymap to display on minor modes.")

295 296 297 298 299
(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
300
		  :help ,(purecopy "Toggle displaying line numbers in the mode-line")
301 302 303
		  :button (:toggle . line-number-mode)))
    (define-key menu-map [column-number-mode]
      `(menu-item ,(purecopy "Display Column Numbers") column-number-mode
304
		  :help ,(purecopy "Toggle displaying column numbers in the mode-line")
305 306 307 308 309
		  :button (:toggle . column-number-mode)))
    (define-key map [mode-line down-mouse-1] menu-map)
    map) "\
Keymap to display on column and line numbers.")

310 311 312
(let* ((help-echo
	;; The multi-line message doesn't work terribly well on the
	;; bottom mode line...  Better ideas?
313 314 315
	;; 	  "\
	;; mouse-1: select window, mouse-2: delete others, mouse-3: delete,
	;; drag-mouse-1: resize, C-mouse-2: split horizontally"
Dan Nicolaescu's avatar
Dan Nicolaescu committed
316 317 318
	"mouse-1: Select (drag to resize)\n\
mouse-2: Make current window occupy the whole frame\n\
mouse-3: Remove current window from display")
319
       (recursive-edit-help-echo "Recursive edit, type C-M-c to get out")
320
       (spaces (propertize " " 'help-echo help-echo))
321 322 323
       (standard-mode-line-format
	(list
	 "%e"
324 325 326
	 `(:eval (if (display-graphic-p)
		     ,(propertize " " 'help-echo help-echo)
		   ,(propertize "-" 'help-echo help-echo)))
327
	 'mode-line-mule-info
Karoly Lorentey's avatar
Karoly Lorentey committed
328
	 'mode-line-client
329
	 'mode-line-modified
330
	 'mode-line-remote
331 332 333 334 335 336 337
	 '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
338 339 340 341
	 `(which-func-mode ("" which-func-format ,spaces))
	 `(global-mode-string ("" global-mode-string ,spaces))
	 `(:eval (unless (display-graphic-p)
		   ,(propertize "-%-" 'help-echo help-echo)))))
342 343
       (standard-mode-line-modes
	(list
344
	 (propertize "%[" 'help-echo recursive-edit-help-echo)
345
	 (propertize "(" 'help-echo help-echo)
346
	 `(:propertize ("" mode-name)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
347 348 349 350
		       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"
351 352 353 354 355
		       mouse-face mode-line-highlight
		       local-map ,mode-line-major-mode-keymap)
	 '("" mode-line-process)
	 `(:propertize ("" minor-mode-alist)
		       mouse-face mode-line-highlight
Dan Nicolaescu's avatar
Dan Nicolaescu committed
356 357 358 359
		       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"
360
		       local-map ,mode-line-minor-mode-keymap)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
361
	 (propertize "%n" 'help-echo "mouse-2: Remove narrowing from the current buffer"
362 363 364
		     'mouse-face 'mode-line-highlight
		     'local-map (make-mode-line-mouse-map
				 'mouse-2 #'mode-line-widen))
365
	 (propertize ")" 'help-echo help-echo)
366
	 (propertize "%]" 'help-echo recursive-edit-help-echo)
367
	 spaces))
Karoly Lorentey's avatar
Karoly Lorentey committed
368

369
       (standard-mode-line-position
370 371 372
	`((-3 ,(propertize
		"%p"
		'local-map mode-line-column-line-number-mode-map
373
		'mouse-face 'mode-line-highlight
374 375 376
		;; XXX needs better description
		'help-echo "Size indication mode\n\
mouse-1: Display Line and Column Mode Menu"))
377
	  (size-indication-mode
Dan Nicolaescu's avatar
Dan Nicolaescu committed
378
	   (8 ,(propertize
Dan Nicolaescu's avatar
Dan Nicolaescu committed
379
		" of %I"
380
		'local-map mode-line-column-line-number-mode-map
381
		'mouse-face 'mode-line-highlight
Dan Nicolaescu's avatar
Dan Nicolaescu committed
382
		;; XXX needs better description
383 384
		'help-echo "Size indication mode\n\
mouse-1: Display Line and Column Mode Menu")))
385 386
	  (line-number-mode
	   ((column-number-mode
Dan Nicolaescu's avatar
Dan Nicolaescu committed
387 388
	     (10 ,(propertize
		   " (%l,%c)"
389
		   'local-map mode-line-column-line-number-mode-map
390
		   'mouse-face 'mode-line-highlight
391 392
		   'help-echo "Line number and Column number\n\
mouse-1: Display Line and Column Mode Menu"))
Dan Nicolaescu's avatar
Dan Nicolaescu committed
393 394
	     (6 ,(propertize
		  " L%l"
395
		  'local-map mode-line-column-line-number-mode-map
396
		  'mouse-face 'mode-line-highlight
397 398
		  'help-echo "Line Number\n\
mouse-1: Display Line and Column Mode Menu"))))
399
	   ((column-number-mode
Dan Nicolaescu's avatar
Dan Nicolaescu committed
400 401
	     (5 ,(propertize
		  " C%c"
402
		  'local-map mode-line-column-line-number-mode-map
403
		  'mouse-face 'mode-line-highlight
404 405
		  'help-echo "Column number\n\
mouse-1: Display Line and Column Mode Menu"))))))))
406 407 408 409 410 411 412 413 414 415 416 417

  (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))))
Richard M. Stallman's avatar
Richard M. Stallman committed
418

419 420 421 422 423 424 425 426 427 428 429 430 431
(defvar mode-line-buffer-identification-keymap
  ;; Add menu of buffer operations to the buffer identification part
  ;; of the mode line.or header line.
  (let ((map (make-sparse-keymap)))
    ;; Bind down- events so that the global keymap won't ``shine
    ;; through''.
    (define-key map [mode-line mouse-1] 'mode-line-previous-buffer)
    (define-key map [header-line down-mouse-1] 'ignore)
    (define-key map [header-line mouse-1] 'mode-line-previous-buffer)
    (define-key map [mode-line mouse-3] 'mode-line-next-buffer)
    (define-key map [header-line down-mouse-3] 'ignore)
    (define-key map [header-line mouse-3] 'mode-line-next-buffer)
    map) "\
432
Keymap for what is displayed by `mode-line-buffer-identification'.")
433

434 435 436 437
(defun propertized-buffer-identification (fmt)
  "Return a list suitable for `mode-line-buffer-identification'.
FMT is a format specifier such as \"%12b\".  This function adds
text properties for face, help-echo, and local-map to it."
438
  (list (propertize fmt
439 440
		    'face 'mode-line-buffer-id
		    'help-echo
441
		    (purecopy "Buffer name\n\
Dan Nicolaescu's avatar
Dan Nicolaescu committed
442 443
mouse-1: previous buffer\n\
mouse-3: next buffer")
444 445 446
		    'mouse-face 'mode-line-highlight
		    'local-map mode-line-buffer-identification-keymap)))

447
(defvar mode-line-buffer-identification (propertized-buffer-identification "%12b") "\
448 449 450 451 452
Mode-line control 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,...)")

453
;;;###autoload
454
(put 'mode-line-buffer-identification 'risky-local-variable t)
455 456
(make-variable-buffer-local 'mode-line-buffer-identification)

457
(defun mode-line-unbury-buffer (event) "\
458
Call `unbury-buffer' in this window."
459 460 461
  (interactive "e")
  (save-selected-window
    (select-window (posn-window (event-start event)))
462
    (unbury-buffer)))
463 464

(defun mode-line-bury-buffer (event) "\
Sam Steingold's avatar
Sam Steingold committed
465
Like `bury-buffer', but temporarily select EVENT's window."
466 467 468 469
  (interactive "e")
  (save-selected-window
    (select-window (posn-window (event-start event)))
    (bury-buffer)))
470

471 472
(defun mode-line-other-buffer () "\
Switch to the most recently selected buffer other than the current one."
473
  (interactive)
474
  (switch-to-buffer (other-buffer) nil t))
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489
(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)))
    (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)))
    (previous-buffer)))

Dave Love's avatar
Dave Love committed
490 491
(defmacro bound-and-true-p (var)
  "Return the value of symbol VAR if it is bound, else nil."
492
  `(and (boundp (quote ,var)) ,var))
Dave Love's avatar
Dave Love committed
493

494 495
;; Use mode-line-mode-menu for local minor-modes only.
;; Global ones can go on the menubar (Options --> Show/Hide).
496
(define-key mode-line-mode-menu [overwrite-mode]
497
  `(menu-item ,(purecopy "Overwrite (Ovwrt)") overwrite-mode
498
	      :help ,(purecopy "Overwrite mode: typed characters replace existing text")
499
	      :button (:toggle . overwrite-mode)))
500
(define-key mode-line-mode-menu [outline-minor-mode]
501
  `(menu-item ,(purecopy "Outline (Outl)") outline-minor-mode
Dan Nicolaescu's avatar
Dan Nicolaescu committed
502
	      ;; XXX: This needs a good, brief description.
503
	      :help ,(purecopy "")
504 505
	      :button (:toggle . (bound-and-true-p outline-minor-mode))))
(define-key mode-line-mode-menu [highlight-changes-mode]
506
  `(menu-item ,(purecopy "Highlight changes (Chg)") highlight-changes-mode
507
	      :help ,(purecopy "Show changes in the buffer in a distinctive color")
508
	      :button (:toggle . (bound-and-true-p highlight-changes-mode))))
509
(define-key mode-line-mode-menu [hide-ifdef-mode]
510
  `(menu-item ,(purecopy "Hide ifdef (Ifdef)") hide-ifdef-mode
511
	      :help ,(purecopy "Show/Hide code within #ifdef constructs")
512
	      :button (:toggle . (bound-and-true-p hide-ifdef-mode))))
513 514
(define-key mode-line-mode-menu [glasses-mode]
  `(menu-item ,(purecopy "Glasses (o^o)") glasses-mode
515
	      :help ,(purecopy "Insert virtual separators to make long identifiers easy to read")
516
	      :button (:toggle . (bound-and-true-p glasses-mode))))
517
(define-key mode-line-mode-menu [font-lock-mode]
518
  `(menu-item ,(purecopy "Font Lock") font-lock-mode
519
	      :help ,(purecopy "Syntax coloring")
520 521
	      :button (:toggle . font-lock-mode)))
(define-key mode-line-mode-menu [flyspell-mode]
522
  `(menu-item ,(purecopy "Flyspell (Fly)") flyspell-mode
523
	      :help ,(purecopy "Spell checking on the fly")
524
	      :button (:toggle . (bound-and-true-p flyspell-mode))))
525 526
(define-key mode-line-mode-menu [auto-revert-tail-mode]
  `(menu-item ,(purecopy "Auto revert tail (Tail)") auto-revert-tail-mode
527
	      :help ,(purecopy "Revert the tail of the buffer when buffer grows")
528
	      :enable (buffer-file-name)
529
	      :button (:toggle . (bound-and-true-p auto-revert-tail-mode))))
530
(define-key mode-line-mode-menu [auto-revert-mode]
531
  `(menu-item ,(purecopy "Auto revert (ARev)") auto-revert-mode
532
	      :help ,(purecopy "Revert the buffer when the file on disk changes")
533
	      :button (:toggle . (bound-and-true-p auto-revert-mode))))
534 535
(define-key mode-line-mode-menu [auto-fill-mode]
  `(menu-item ,(purecopy "Auto fill (Fill)") auto-fill-mode
536
	      :help ,(purecopy "Automatically insert new lines")
537
	      :button (:toggle . auto-fill-function)))
538
(define-key mode-line-mode-menu [abbrev-mode]
539
  `(menu-item ,(purecopy "Abbrev (Abbrev)") abbrev-mode
540
	      :help ,(purecopy "Automatically expand abbreviations")
541
	      :button (:toggle . abbrev-mode)))
542

543
(defun mode-line-minor-mode-help (event)
544
  "Describe minor mode for EVENT on minor modes area of the mode line."
545 546 547 548
  (interactive "@e")
  (let ((indicator (car (nth 4 (car (cdr event))))))
    (describe-minor-mode-from-indicator indicator)))

549 550 551
(defvar minor-mode-alist nil "\
Alist saying how to show minor modes in the mode line.
Each element looks like (VARIABLE STRING);
552
STRING is included in the mode line if VARIABLE's value is non-nil.
553 554 555

Actually, STRING need not be a string; any possible mode-line element
is okay.  See `mode-line-format'.")
556
;;;###autoload
557
(put 'minor-mode-alist 'risky-local-variable t)
558 559
;; Don't use purecopy here--some people want to change these strings.
(setq minor-mode-alist
560 561 562 563 564
      '((abbrev-mode " Abbrev")
        (overwrite-mode overwrite-mode)
        (auto-fill-function " Fill")
        ;; not really a minor mode...
        (defining-kbd-macro " Def")))
565

Richard M. Stallman's avatar
Richard M. Stallman committed
566 567 568 569 570 571 572 573
;; These variables are used by autoloadable packages.
;; They are defined here so that they do not get overridden
;; by the loading of those packages.

;; Names in directory that end in one of these
;; are ignored in completion,
;; making it more likely you will get a unique match.
(setq completion-ignored-extensions
574
      (append
575
       (cond ((memq system-type '(ms-dos windows-nt))
576
	      (mapcar 'purecopy
577
	      '(".o" "~" ".bin" ".bak" ".obj" ".map" ".ico" ".pif" ".lnk"
578
		".a" ".ln" ".blg" ".bbl" ".dll" ".drv" ".vxd" ".386")))
579
	     (t
580
	      (mapcar 'purecopy
581
	      '(".o" "~" ".bin" ".lbin" ".so"
582 583
		".a" ".ln" ".blg" ".bbl"))))
       (mapcar 'purecopy
584 585
       '(".elc" ".lof"
	 ".glo" ".idx" ".lot"
586 587
	 ;; VCS metadata directories
	 ".svn/" ".hg/" ".git/" ".bzr/" "CVS/" "_darcs/" "_MTN/"
588
	 ;; TeX-related
589
	 ".fmt" ".tfm"
590 591
	 ;; Java compiled
	 ".class"
592 593
	 ;; CLISP
	 ".fas" ".lib" ".mem"
594
	 ;; CMUCL
595
	 ".x86f" ".sparcf"
596 597 598 599 600 601
	 ;; OpenMCL / Clozure CL
	 ".dfsl" ".pfsl" ".d64fsl" ".p64fsl" ".lx64fsl" ".lx32fsl"
	 ".dx64fsl" ".dx32fsl" ".fx64fsl" ".fx32fsl" ".sx64fsl"
	 ".sx32fsl" ".wx64fsl" ".wx32fsl"
         ;; Other CL implementations (Allegro, LispWorks)
         ".fasl" ".ufsl" ".fsl" ".dxl"
602 603
	 ;; Libtool
	 ".lo" ".la"
604 605
	 ;; Gettext
	 ".gmo" ".mo"
606
	 ;; Texinfo-related
Kenichi Handa's avatar
Kenichi Handa committed
607 608 609
	 ;; This used to contain .log, but that's commonly used for log
	 ;; files you do want to see, not just TeX stuff.  -- fx
	 ".toc" ".aux"
610
	 ".cp" ".fn" ".ky" ".pg" ".tp" ".vr"
611 612
	 ".cps" ".fns" ".kys" ".pgs" ".tps" ".vrs"
	 ;; Python byte-compiled
613
	 ".pyc" ".pyo"))))
Richard M. Stallman's avatar
Richard M. Stallman committed
614

615 616 617 618 619 620 621 622
;; Suffixes used for executables.
(setq exec-suffixes
      (cond
       ((memq system-type '(ms-dos windows-nt))
	'(".exe" ".com" ".bat" ".cmd" ".btm" ""))
       (t
	'(""))))

623 624
;; Packages should add to this list appropriately when they are
;; loaded, rather than listing everything here.
Richard M. Stallman's avatar
Richard M. Stallman committed
625
(setq debug-ignored-errors
626
      `(beginning-of-line beginning-of-buffer end-of-line
Richard M. Stallman's avatar
Richard M. Stallman committed
627
	end-of-buffer end-of-file buffer-read-only
628
	file-supersession
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
      	,(purecopy "^Previous command was not a yank$")
	,(purecopy "^Minibuffer window is not active$")
	,(purecopy "^No previous history search regexp$")
	,(purecopy "^No later matching history item$")
	,(purecopy "^No earlier matching history item$")
	,(purecopy "^End of history; no default available$")
	,(purecopy "^End of defaults; no next item$")
	,(purecopy "^Beginning of history; no preceding item$")
	,(purecopy "^No recursive edit is in progress$")
	,(purecopy "^Changes to be undone are outside visible portion of buffer$")
	,(purecopy "^No undo information in this buffer$")
	,(purecopy "^No further undo information")
	,(purecopy "^Save not confirmed$")
	,(purecopy "^Recover-file cancelled\\.$")
	,(purecopy "^Cannot switch buffers in a dedicated window$")
644
        ))
Richard M. Stallman's avatar
Richard M. Stallman committed
645 646 647 648


(make-variable-buffer-local 'indent-tabs-mode)

649
;; We have base64, md5 and sha1 functions built in now.
650 651
(provide 'base64)
(provide 'md5)
652
(provide 'sha1)
653 654
(provide 'overlay '(display syntax-table field))
(provide 'text-properties '(display syntax-table field point-entered))
655

656 657
(define-key esc-map "\t" 'complete-symbol)

658 659 660 661 662 663 664 665 666 667
(defun complete-symbol (arg)
  "Perform completion on the text around point.
The completion method is determined by `completion-at-point-functions'.

With a prefix argument, this command does completion within
the collection of symbols listed in the index of the manual for the
language you are using."
  (interactive "P")
  (if arg (info-complete-symbol) (completion-at-point)))

Richard M. Stallman's avatar
Richard M. Stallman committed
668 669 670 671
;; Reduce total amount of space we must allocate during this function
;; that we will not need to keep permanently.
(garbage-collect)

672

Richard M. Stallman's avatar
Richard M. Stallman committed
673 674
(setq help-event-list '(help f1))

675 676
(make-variable-buffer-local 'minor-mode-overriding-map-alist)

677 678
;; From frame.c
(global-set-key [switch-frame] 'handle-switch-frame)
679 680 681 682 683
(global-set-key [select-window] 'handle-select-window)

;; FIXME: Do those 3 events really ever reach the global-map ?
;;        It seems that they can't because they're handled via
;;        special-event-map which is used at very low-level.  -stef
684 685 686 687 688
(global-set-key [delete-frame] 'handle-delete-frame)
(global-set-key [iconify-frame] 'ignore-event)
(global-set-key [make-frame-visible] 'ignore-event)


Richard M. Stallman's avatar
Richard M. Stallman committed
689 690 691
;These commands are defined in editfns.c
;but they are not assigned to keys there.
(put 'narrow-to-region 'disabled t)
692

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
;; Moving with arrows in bidi-sensitive direction.
(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."
  (interactive "^p")
  (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."
  (interactive "^p")
  (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).

Depending on the bidirectional context, this may move either forward
or backward in the buffer.  This is in contrast with \\[forward-word]
and \\[backward-word], which see.

Value is normally t.
If an edge of the buffer or a field boundary is reached, point is left there
there and the function returns nil.  Field boundaries are not noticed
if `inhibit-field-text-motion' is non-nil."
  (interactive "^p")
  (if (eq (current-bidi-paragraph-direction) 'left-to-right)
      (forward-word n)
    (backward-word n)))

(defun left-word (&optional n)
  "Move point N words to the left (to the right if N is negative).

Depending on the bidirectional context, this may move either backward
or forward in the buffer.  This is in contrast with \\[backward-word]
and \\[forward-word], which see.

Value is normally t.
If an edge of the buffer or a field boundary is reached, point is left there
there and the function returns nil.  Field boundaries are not noticed
if `inhibit-field-text-motion' is non-nil."
  (interactive "^p")
  (if (eq (current-bidi-paragraph-direction) 'left-to-right)
      (backward-word n)
    (forward-word n)))

750 751 752 753 754 755
(defvar narrow-map (make-sparse-keymap)
  "Keymap for narrowing commands.")
(define-key ctl-x-map "n" narrow-map)

(define-key narrow-map "n" 'narrow-to-region)
(define-key narrow-map "w" 'widen)
Richard M. Stallman's avatar
Richard M. Stallman committed
756

757 758 759 760
;; Quitting
(define-key global-map "\e\e\e" 'keyboard-escape-quit)
(define-key global-map "\C-g" 'keyboard-quit)

761 762 763 764
;; Used to be in termdev.el: when using several terminals, make C-z
;; suspend only the relevant terminal.
(substitute-key-definition 'suspend-emacs 'suspend-frame global-map)

Richard M. Stallman's avatar
Richard M. Stallman committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
(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)
(define-key global-map "\C-q" 'quoted-insert)
(define-key esc-map "^" 'delete-indentation)
(define-key esc-map "\\" 'delete-horizontal-space)
(define-key esc-map "m" 'back-to-indentation)
(define-key ctl-x-map "\C-o" 'delete-blank-lines)
(define-key esc-map " " 'just-one-space)
(define-key esc-map "z" 'zap-to-char)
(define-key esc-map "=" 'count-lines-region)
(define-key ctl-x-map "=" 'what-cursor-position)
(define-key esc-map ":" 'eval-expression)
;; Define ESC ESC : like ESC : for people who type ESC ESC out of habit.
(define-key esc-map "\M-:" 'eval-expression)
;; Changed from C-x ESC so that function keys work following C-x.
(define-key ctl-x-map "\e\e" 'repeat-complex-command)
;; New binding analogous to M-:.
(define-key ctl-x-map "\M-:" 'repeat-complex-command)
785 786
(define-key ctl-x-map "u" 'undo)
(put 'undo :advertised-binding [?\C-x ?u])
Richard M. Stallman's avatar
Richard M. Stallman committed
787 788 789
;; Many people are used to typing C-/ on X terminals and getting C-_.
(define-key global-map [?\C-/] 'undo)
(define-key global-map "\C-_" 'undo)
790 791 792 793
;; Richard said that we should not use C-x <uppercase letter> and I have
;; no idea whereas to bind it.  Any suggestion welcome.  -stef
;; (define-key ctl-x-map "U" 'undo-only)

Richard M. Stallman's avatar
Richard M. Stallman committed
794 795
(define-key esc-map "!" 'shell-command)
(define-key esc-map "|" 'shell-command-on-region)
796
(define-key esc-map "&" 'async-shell-command)
Richard M. Stallman's avatar
Richard M. Stallman committed
797

798 799 800 801
(define-key ctl-x-map [right] 'next-buffer)
(define-key ctl-x-map [C-right] 'next-buffer)
(define-key ctl-x-map [left] 'previous-buffer)
(define-key ctl-x-map [C-left] 'previous-buffer)
802

803 804 805 806 807 808 809 810
(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 "\ep"   'previous-history-element)
  (define-key map [prior] 'previous-history-element)
  (define-key map [up]    'previous-history-element)
  (define-key map "\es"   'next-matching-history-element)
811 812 813 814
  (define-key map "\er"   'previous-matching-history-element)
  ;; Override the global binding (which calls indent-relative via
  ;; indent-for-tab-command).  The alignment that indent-relative tries to
  ;; do doesn't make much sense here since the prompt messes it up.
815
  (define-key map "\t"    'self-insert-command)
816
  (define-key map [C-tab] 'file-cache-minibuffer-complete))
Richard M. Stallman's avatar
Richard M. Stallman committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

(define-key global-map "\C-u" 'universal-argument)
(let ((i ?0))
  (while (<= i ?9)
    (define-key esc-map (char-to-string i) 'digit-argument)
    (setq i (1+ i))))
(define-key esc-map "-" 'negative-argument)
;; Define control-digits.
(let ((i ?0))
  (while (<= i ?9)
    (define-key global-map (read (format "[?\\C-%c]" i)) 'digit-argument)
    (setq i (1+ i))))
(define-key global-map [?\C--] 'negative-argument)
;; Define control-meta-digits.
(let ((i ?0))
  (while (<= i ?9)
    (define-key esc-map (read (format "[?\\C-%c]" i)) 'digit-argument)
    (setq i (1+ i))))
(define-key global-map [?\C-\M--] 'negative-argument)

837
;; Update tutorial--default-keys if you change these.
838
(define-key global-map "\177" 'delete-backward-char)
839
(define-key global-map "\C-d" 'delete-char)
840

Richard M. Stallman's avatar
Richard M. Stallman committed
841 842 843 844 845 846 847 848 849 850 851
(define-key global-map "\C-k" 'kill-line)
(define-key global-map "\C-w" 'kill-region)
(define-key esc-map "w" 'kill-ring-save)
(define-key esc-map "\C-w" 'append-next-kill)
(define-key global-map "\C-y" 'yank)
(define-key esc-map "y" 'yank-pop)

;; (define-key ctl-x-map "a" 'append-to-buffer)

(define-key global-map "\C-@" 'set-mark-command)
;; Many people are used to typing C-SPC and getting C-@.
852
(define-key global-map [?\C- ] 'set-mark-command)
853 854
(put 'set-mark-command :advertised-binding [?\C- ])

Richard M. Stallman's avatar
Richard M. Stallman committed
855 856
(define-key ctl-x-map "\C-x" 'exchange-point-and-mark)
(define-key ctl-x-map "\C-@" 'pop-global-mark)
857
(define-key ctl-x-map [?\C- ] 'pop-global-mark)
Richard M. Stallman's avatar
Richard M. Stallman committed
858 859 860 861

(define-key global-map "\C-n" 'next-line)
(define-key global-map "\C-p" 'previous-line)
(define-key ctl-x-map "\C-n" 'set-goal-column)
862
(define-key global-map "\C-a" 'move-beginning-of-line)
863
(define-key global-map "\C-e" 'move-end-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
864

865 866
(define-key ctl-x-map "`" 'next-error)

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
(defvar goto-map (make-sparse-keymap)
  "Keymap for navigation commands.")
(define-key esc-map "g" goto-map)

(define-key goto-map    "g" 'goto-line)
(define-key goto-map "\M-g" 'goto-line)
(define-key goto-map    "n" 'next-error)
(define-key goto-map "\M-n" 'next-error)
(define-key goto-map    "p" 'previous-error)
(define-key goto-map "\M-p" 'previous-error)

(defvar search-map (make-sparse-keymap)
  "Keymap for search related commands.")
(define-key esc-map "s" search-map)

(define-key search-map "o"  'occur)
(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 "hu" 'unhighlight-regexp)
(define-key search-map "hf" 'hi-lock-find-patterns)
(define-key search-map "hw" 'hi-lock-write-interactive-patterns)
889

Richard M. Stallman's avatar
Richard M. Stallman committed
890 891
;;(defun function-key-error ()
;;  (interactive)
892
;;  (error "That function key is not bound to anything"))
Richard M. Stallman's avatar
Richard M. Stallman committed
893 894 895 896

(define-key global-map [menu] 'execute-extended-command)
(define-key global-map [find] 'search-forward)

897 898
;; Don't do this.  We define <delete> in function-key-map instead.
;(define-key global-map [delete] 'backward-delete-char)
899

Richard M. Stallman's avatar
Richard M. Stallman committed
900
;; natural bindings for terminal keycaps --- defined in X keysym order
901
(define-key global-map [C-S-backspace]  'kill-whole-line)
902
(define-key global-map [home]		'move-beginning-of-line)
903
(define-key global-map [C-home]		'beginning-of-buffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
904
(define-key global-map [M-home]		'beginning-of-buffer-other-window)
905
(define-key esc-map    [home]		'beginning-of-buffer-other-window)
906
(define-key global-map [left]		'left-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
907
(define-key global-map [up]		'previous-line)
908
(define-key global-map [right]		'right-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
909
(define-key global-map [down]		'next-line)
Juri Linkov's avatar