(vc-dired-mode): Make dired-after-readin-hook a local hook, not a local variable.
[bpt/emacs.git] / lisp / menu-bar.el
CommitLineData
235aa29b
ER
1;;; menu-bar.el --- define a default menu bar.
2
b578f267
EN
3;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
4
235aa29b 5;; Author: RMS
e4874521 6;; Maintainer: FSF
b7f66977 7;; Keywords: internal
235aa29b 8
1db87953
RS
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
1db87953 25
260130bb
KH
26;; Avishai Yacobi suggested some menu rearrangements.
27
b578f267
EN
28;;; Code:
29
d537086b
RS
30;;; User options:
31
30e19aee 32(defcustom buffers-menu-max-size 10
d537086b
RS
33 "*Maximum number of entries which may appear on the Buffers menu.
34If this is 10, then only the ten most-recently-selected buffers are shown.
35If this is nil, then all buffers are shown.
30e19aee
RS
36A large number or nil slows down menu responsiveness."
37 :type '(choice integer
38 (const :tag "All" nil))
39 :group 'mouse)
d537086b 40
b132f2b1
RM
41;; Don't clobber an existing menu-bar keymap, to preserve any menu-bar key
42;; definitions made in loaddefs.el.
43(or (lookup-key global-map [menu-bar])
44 (define-key global-map [menu-bar] (make-sparse-keymap "menu-bar")))
22390f50 45(defvar menu-bar-help-menu (make-sparse-keymap "Help"))
ffbdd83d
RS
46
47;; Force Help item to come last, after the major mode's own items.
0d31efcd
KH
48;; The symbol used to be called `help', but that gets confused with the
49;; help key.
50(setq menu-bar-final-items '(help-menu))
ffbdd83d 51
0d31efcd 52(define-key global-map [menu-bar help-menu] (cons "Help" menu-bar-help-menu))
ffbdd83d
RS
53(defvar menu-bar-search-menu (make-sparse-keymap "Search"))
54(define-key global-map [menu-bar search] (cons "Search" menu-bar-search-menu))
22390f50 55(defvar menu-bar-edit-menu (make-sparse-keymap "Edit"))
40954111 56(define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
ffbdd83d
RS
57(defvar menu-bar-tools-menu (make-sparse-keymap "Tools"))
58(define-key global-map [menu-bar tools] (cons "Tools" menu-bar-tools-menu))
59(defvar menu-bar-files-menu (make-sparse-keymap "Files"))
60(define-key global-map [menu-bar files] (cons "Files" menu-bar-files-menu))
1b009b97
RS
61
62;; This alias is for compatibility with 19.28 and before.
63(defvar menu-bar-file-menu menu-bar-files-menu)
9bb8e471 64\f
9dcdc43d
RS
65(defvar vc-menu-map (make-sparse-keymap "Version Control"))
66
147f1d2a
RS
67(define-key menu-bar-tools-menu [gdb] '("Debugger..." . gdb))
68(define-key menu-bar-tools-menu [compile] '("Compile..." . compile))
69(define-key menu-bar-tools-menu [grep] '("Search Files..." . grep))
70
71(define-key menu-bar-tools-menu [separator-1]
72 '("--"))
73
e1726722 74(define-key menu-bar-tools-menu [calendar] '("Display Calendar" . calendar))
c0965058 75(define-key menu-bar-tools-menu [compose-mail] '("Send Mail" . compose-mail))
ffbdd83d 76(define-key menu-bar-tools-menu [rmail] '("Read Mail" . rmail))
96a699d0 77(define-key menu-bar-tools-menu [gnus] '("Read Net News" . gnus))
ffbdd83d
RS
78
79(define-key menu-bar-tools-menu [separator-vc]
80 '("--"))
81
fe74180c 82(define-key menu-bar-tools-menu [vc]
ffbdd83d 83 (cons "Version Control" vc-menu-map))
9bb8e471 84
ffbdd83d 85(define-key menu-bar-tools-menu [separator-compare]
9bb8e471
RS
86 '("--"))
87
dd824ce4
MK
88(define-key menu-bar-tools-menu [ediff-misc]
89 '("Ediff Miscellanea" . menu-bar-ediff-misc-menu))
ffbdd83d 90(define-key menu-bar-tools-menu [epatch]
080ac1ac 91 '("Apply Patch" . menu-bar-epatch-menu))
ffbdd83d 92(define-key menu-bar-tools-menu [ediff-merge]
5438ecd3 93 '("Merge" . menu-bar-ediff-merge-menu))
57eed34a 94(define-key menu-bar-tools-menu [compare]
40a8cde1 95 '("Compare" . menu-bar-ediff-menu))
9bb8e471 96
ffbdd83d 97(define-key menu-bar-tools-menu [separator-print]
9bb8e471
RS
98 '("--"))
99
24f86c2f 100(defvar menu-bar-print-menu (make-sparse-keymap "Print"))
ffbdd83d 101
24f86c2f 102(define-key menu-bar-print-menu [ps-print-region]
ffbdd83d 103 '("Postscript Print Region" . ps-print-region-with-faces))
24f86c2f 104(define-key menu-bar-print-menu [ps-print-buffer]
ffbdd83d 105 '("Postscript Print Buffer" . ps-print-buffer-with-faces))
24f86c2f
SM
106(define-key menu-bar-print-menu [separator-ps-print]
107 '("--"))
108(define-key menu-bar-print-menu [print-region]
ffbdd83d 109 '("Print Region" . print-region))
24f86c2f 110(define-key menu-bar-print-menu [print-buffer]
ffbdd83d 111 '("Print Buffer" . print-buffer))
24f86c2f
SM
112
113(define-key menu-bar-tools-menu [print]
114 (cons "Print" menu-bar-print-menu))
115
116(put 'print-region 'menu-enable 'mark-active)
117(put 'ps-print-region-with-faces 'menu-enable 'mark-active)
ffbdd83d
RS
118\f
119(define-key menu-bar-files-menu [exit-emacs]
120 '("Exit Emacs" . save-buffers-kill-emacs))
121
122(define-key menu-bar-files-menu [separator-exit]
123 '("--"))
124
125(define-key menu-bar-files-menu [one-window]
126 '("One Window" . delete-other-windows))
127
128(define-key menu-bar-files-menu [split-window]
129 '("Split Window" . split-window-vertically))
9bb8e471 130
ca1a9692
RS
131(if (fboundp 'delete-frame)
132 (progn
a4e4b916
RS
133 ;; Don't use delete-frame as event name
134 ;; because that is a special event.
135 (define-key menu-bar-files-menu [delete-this-frame]
ca1a9692 136 '("Delete Frame" . delete-frame))
ffbdd83d
RS
137 (define-key menu-bar-files-menu [make-frame-on-display]
138 '("Open New Display..." . make-frame-on-display))
139 (define-key menu-bar-files-menu [make-frame]
dbe82e01 140 '("Make New Frame" . make-frame-command))))
9bb8e471 141
ffbdd83d 142(define-key menu-bar-files-menu [separator-buffers]
9bb8e471
RS
143 '("--"))
144
ffbdd83d
RS
145(define-key menu-bar-files-menu [kill-buffer]
146 '("Kill Current Buffer" . kill-this-buffer))
147(define-key menu-bar-files-menu [insert-file]
1f50590a 148 '("Insert File..." . insert-file))
f3e1e0dd
RS
149(define-key menu-bar-files-menu [recover-session]
150 '("Recover Session..." . recover-session))
ffbdd83d 151(define-key menu-bar-files-menu [revert-buffer]
2f1139a4 152 '("Revert Buffer" . revert-buffer))
ffbdd83d 153(define-key menu-bar-files-menu [write-file]
2f1139a4 154 '("Save Buffer As..." . write-file))
ffbdd83d
RS
155(define-key menu-bar-files-menu [save-buffer] '("Save Buffer" . save-buffer))
156(define-key menu-bar-files-menu [dired] '("Open Directory..." . dired))
157(define-key menu-bar-files-menu [open-file] '("Open File..." . find-file))
40a8cde1 158
f3e1e0dd
RS
159(put 'recover-session 'menu-enable
160 '(and auto-save-list-file-prefix
161 (directory-files
162 (file-name-directory auto-save-list-file-prefix)
163 nil
164 (concat "\\`"
165 (regexp-quote (file-name-nondirectory
166 auto-save-list-file-prefix)))
167 t)))
9bb8e471 168\f
ffbdd83d
RS
169(defun nonincremental-search-forward (string)
170 "Read a string and search for it nonincrementally."
171 (interactive "sSearch for string: ")
172 (if (equal string "")
173 (search-forward (car search-ring))
174 (isearch-update-ring string nil)
175 (search-forward string)))
176
177(defun nonincremental-search-backward (string)
178 "Read a string and search backward for it nonincrementally."
179 (interactive "sSearch for string: ")
180 (if (equal string "")
181 (search-backward (car search-ring))
182 (isearch-update-ring string nil)
183 (search-backward string)))
184
185(defun nonincremental-re-search-forward (string)
186 "Read a regular expression and search for it nonincrementally."
187 (interactive "sSearch for regexp: ")
188 (if (equal string "")
189 (re-search-forward (car regexp-search-ring))
190 (isearch-update-ring string t)
191 (re-search-forward string)))
192
193(defun nonincremental-re-search-backward (string)
194 "Read a regular expression and search backward for it nonincrementally."
195 (interactive "sSearch for regexp: ")
196 (if (equal string "")
197 (re-search-backward (car regexp-search-ring))
198 (isearch-update-ring string t)
199 (re-search-backward string)))
200
1f50590a 201(defun nonincremental-repeat-search-forward ()
ffbdd83d
RS
202 "Search forward for the previous search string."
203 (interactive)
008c8493
RS
204 (if (null search-ring)
205 (error "No previous search"))
ffbdd83d
RS
206 (search-forward (car search-ring)))
207
1f50590a 208(defun nonincremental-repeat-search-backward ()
ffbdd83d
RS
209 "Search backward for the previous search string."
210 (interactive)
008c8493
RS
211 (if (null search-ring)
212 (error "No previous search"))
ffbdd83d
RS
213 (search-backward (car search-ring)))
214
1f50590a 215(defun nonincremental-repeat-re-search-forward ()
ffbdd83d
RS
216 "Search forward for the previous regular expression."
217 (interactive)
97565dd9 218 (if (null regexp-search-ring)
008c8493 219 (error "No previous search"))
ffbdd83d
RS
220 (re-search-forward (car regexp-search-ring)))
221
1f50590a 222(defun nonincremental-repeat-re-search-backward ()
ffbdd83d
RS
223 "Search backward for the previous regular expression."
224 (interactive)
97565dd9 225 (if (null regexp-search-ring)
008c8493 226 (error "No previous search"))
ffbdd83d
RS
227 (re-search-backward (car regexp-search-ring)))
228
1f50590a
RS
229(define-key menu-bar-search-menu [query-replace-regexp]
230 '("Query Replace Regexp..." . query-replace-regexp))
ffbdd83d 231(define-key menu-bar-search-menu [query-replace]
1f50590a 232 '("Query Replace..." . query-replace))
ffbdd83d 233(define-key menu-bar-search-menu [find-tag]
1f50590a 234 '("Find Tag..." . find-tag))
ffbdd83d
RS
235(define-key menu-bar-search-menu [bookmark]
236 '("Bookmarks" . menu-bar-bookmark-map))
237
238(define-key menu-bar-search-menu [separator-search]
239 '("--"))
240
fe74180c 241(define-key menu-bar-search-menu [repeat-regexp-back]
ffbdd83d 242 '("Repeat Regexp Backwards" . nonincremental-repeat-re-search-backward))
fe74180c 243(define-key menu-bar-search-menu [repeat-search-back]
ffbdd83d 244 '("Repeat Backwards" . nonincremental-repeat-search-backward))
fe74180c 245(define-key menu-bar-search-menu [repeat-regexp-fwd]
ffbdd83d 246 '("Repeat Regexp" . nonincremental-repeat-re-search-forward))
fe74180c 247(define-key menu-bar-search-menu [repeat-search-fwd]
ffbdd83d
RS
248 '("Repeat Search" . nonincremental-repeat-search-forward))
249
250(define-key menu-bar-search-menu [separator-repeat]
9bb8e471
RS
251 '("--"))
252
fe74180c 253(define-key menu-bar-search-menu [re-search-backward]
1f50590a 254 '("Regexp Search Backwards..." . nonincremental-re-search-backward))
fe74180c 255(define-key menu-bar-search-menu [search-backward]
1f50590a 256 '("Search Backwards..." . nonincremental-search-backward))
fe74180c 257(define-key menu-bar-search-menu [re-search-forward]
1f50590a 258 '("Regexp Search..." . nonincremental-re-search-forward))
fe74180c 259(define-key menu-bar-search-menu [search-forward]
1f50590a 260 '("Search..." . nonincremental-search-forward))
ffbdd83d 261\f
5b42ec2b
RS
262(if (fboundp 'start-process)
263 (define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map)))
9bb8e471 264(define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
6507ffb1 265(define-key menu-bar-edit-menu [props] '("Text Properties" . facemenu-menu))
9bb8e471
RS
266
267(define-key menu-bar-edit-menu [separator-edit]
268 '("--"))
269
270(define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
3dd92899 271
e1726722 272(define-key menu-bar-edit-menu [paste] '("Paste Most Recent" . yank))
3dd92899
KH
273
274(defvar yank-menu (cons "Select Yank" nil))
275(fset 'yank-menu (cons 'keymap yank-menu))
276(define-key menu-bar-edit-menu [select-paste] '("Select and Paste" . yank-menu))
4c0317b1 277(define-key menu-bar-edit-menu [copy] '("Copy" . menu-bar-kill-ring-save))
057d49d1
RS
278(define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
279(define-key menu-bar-edit-menu [undo] '("Undo" . undo))
280
4c0317b1
RS
281(defun menu-bar-kill-ring-save (beg end)
282 (interactive "r")
283 (if (mouse-region-match)
284 (message "Select a region with the mouse does `copy' automatically")
285 (kill-ring-save beg end)))
286
25b048ee
RS
287(put 'fill-region 'menu-enable '(and mark-active (not buffer-read-only)))
288(put 'kill-region 'menu-enable '(and mark-active (not buffer-read-only)))
4c0317b1 289(put 'menu-bar-kill-ring-save 'menu-enable 'mark-active)
25b048ee
RS
290(put 'yank 'menu-enable '(and (x-selection-exists-p) (not buffer-read-only)))
291(put 'yank-menu 'menu-enable '(and (cdr yank-menu) (not buffer-read-only)))
4c0317b1 292(put 'delete-region 'menu-enable '(and mark-active
25b048ee 293 (not buffer-read-only)
4c0317b1 294 (not (mouse-region-match))))
25b048ee
RS
295(put 'undo 'menu-enable '(and (not buffer-read-only)
296 (if (eq last-command 'undo)
297 pending-undo-list
298 (consp buffer-undo-list))))
3a841b0b 299(put 'query-replace 'menu-enable '(not buffer-read-only))
25b048ee 300(put 'query-replace-regexp 'menu-enable '(not buffer-read-only))
db774a16 301
9e18f0a0
RS
302(autoload 'ispell-menu-map "ispell" nil t 'keymap)
303
f9cf0be2 304;; These are alternative definitions for the cut, paste and copy
4c0317b1 305;; menu items. Use them if your system expects these to use the clipboard.
f9cf0be2 306
f9cf0be2
RS
307(put 'clipboard-kill-region 'menu-enable 'mark-active)
308(put 'clipboard-kill-ring-save 'menu-enable 'mark-active)
309(put 'clipboard-yank 'menu-enable
310 '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD)))
311
312(defun clipboard-yank ()
313 "Reinsert the last stretch of killed text, or the clipboard contents."
314 (interactive)
315 (let ((x-select-enable-clipboard t))
316 (yank)))
317
318(defun clipboard-kill-ring-save (beg end)
319 "Copy region to kill ring, and save in the X clipboard."
320 (interactive "r")
321 (let ((x-select-enable-clipboard t))
322 (kill-ring-save beg end)))
323
324(defun clipboard-kill-region (beg end)
325 "Kill the region, and save it in the X clipboard."
326 (interactive "r")
327 (let ((x-select-enable-clipboard t))
328 (kill-region beg end)))
329
330(defun menu-bar-enable-clipboard ()
5cbdeb30
RS
331 "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard.
332Do the same for the keys of the same name."
f9cf0be2 333 (interactive)
5754d7f2
RS
334 ;; We can't use constant list structure here because it becomes pure,
335 ;; and because it gets modified with cache data.
336 (define-key menu-bar-edit-menu [paste]
337 (cons "Paste" 'clipboard-yank))
338 (define-key menu-bar-edit-menu [copy]
339 (cons "Copy" 'clipboard-kill-ring-save))
340 (define-key menu-bar-edit-menu [cut]
5cbdeb30
RS
341 (cons "Cut" 'clipboard-kill-region))
342
343 (define-key global-map [f20] 'clipboard-kill-region)
344 (define-key global-map [f16] 'clipboard-kill-ring-save)
345 (define-key global-map [f18] 'clipboard-yank)
346 ;; X11R6 versions
347 (define-key global-map [cut] 'clipboard-kill-region)
348 (define-key global-map [copy] 'clipboard-kill-ring-save)
349 (define-key global-map [paste] 'clipboard-yank))
f9cf0be2 350\f
a9f53ffb
RS
351
352;;; Menu support
353
354(defvar menu-bar-custom-menu (make-sparse-keymap "Customize"))
355
b0f661de
RS
356(define-key menu-bar-custom-menu [customize-apropos-groups]
357 '("Apropos Groups..." . customize-apropos-groups))
358(define-key menu-bar-custom-menu [customize-apropos-faces]
359 '("Apropos Faces..." . customize-apropos-faces))
360(define-key menu-bar-custom-menu [customize-apropos-options]
361 '("Apropos Options..." . customize-apropos-options))
a9f53ffb
RS
362(define-key menu-bar-custom-menu [customize-apropos]
363 '("Apropos..." . customize-apropos))
b0f661de
RS
364(define-key menu-bar-custom-menu [separator-2]
365 '("--"))
c9a86e71
RS
366(define-key menu-bar-custom-menu [customize-group]
367 '("Specific Group..." . customize-group))
a9f53ffb 368(define-key menu-bar-custom-menu [customize-face]
c9a86e71 369 '("Specific Face..." . customize-face))
b0f661de
RS
370(define-key menu-bar-custom-menu [customize-option]
371 '("Specific Option..." . customize-option))
372(define-key menu-bar-custom-menu [separator-3]
373 '("--"))
e6354b3e 374(define-key menu-bar-custom-menu [customize-browse]
b0f661de
RS
375 '("Browse Customization Groups" . customize-browse))
376(define-key menu-bar-custom-menu [customize]
377 '("Top-level Customization Group" . customize))
a9f53ffb 378
bacb2d12 379;; Options menu
21efd27b 380(defvar menu-bar-options-menu (make-sparse-keymap "Global Options"))
bacb2d12
RS
381
382(defmacro menu-bar-make-toggle (name variable doc message &rest body)
383 `(progn
384 (defun ,name ()
385 ,(concat doc ".")
386 (interactive)
387 (if ,(if body `(progn . ,body)
388 `(setq ,variable (not ,variable)))
389 (message ,message "enabled")
390 (message ,message "disabled")))
89f1709f
RS
391 '(menu-item ,doc ,name .
392 (:button (:toggle . (and (boundp ',variable) ,variable))))))
bacb2d12
RS
393
394(define-key menu-bar-options-menu [debug-on-quit]
395 (menu-bar-make-toggle toggle-debug-on-quit debug-on-quit
89f1709f 396 "Debug on Quit" "Debug on Quit %s"))
bacb2d12
RS
397(define-key menu-bar-options-menu [debug-on-error]
398 (menu-bar-make-toggle toggle-debug-on-error debug-on-error
89f1709f 399 "Debug on Error" "Debug on Error %s"))
bacb2d12
RS
400(define-key menu-bar-options-menu [options-separator]
401 '("--"))
402(define-key menu-bar-options-menu [save-place]
403 (menu-bar-make-toggle toggle-save-place-globally save-place
89f1709f 404 "Save Place in Files between Sessions"
bacb2d12 405 "Saving place in files %s"
89f1709f 406 (require 'saveplace)
bacb2d12
RS
407 (setq-default save-place (not (default-value save-place)))))
408(define-key menu-bar-options-menu [uniquify]
409 (menu-bar-make-toggle toggle-uniquify-buffer-names uniquify-buffer-name-style
89f1709f 410 "Use Directory Names in Buffer Names"
bacb2d12
RS
411 "Directory name in buffer names (uniquify) %s"
412 (require 'uniquify)
413 (setq uniquify-buffer-name-style
414 (if (not uniquify-buffer-name-style)
415 'forward))))
416(define-key menu-bar-options-menu [transient-mark-mode]
417 (menu-bar-make-toggle toggle-transient-mark-mode transient-mark-mode
89f1709f 418 "Transient Mark Mode (highlights region)"
bacb2d12
RS
419 "Transient Mark mode %s"))
420(define-key menu-bar-options-menu [toggle-auto-compression]
89f1709f
RS
421 '(menu-item "Automatic File De/compression"
422 auto-compression-mode .
423 (:button (:toggle . (rassq 'jka-compr-handler
424 file-name-handler-alist)))))
bacb2d12 425(define-key menu-bar-options-menu [auto-fill-mode]
89f1709f
RS
426 '(menu-item "Auto Fill (word wrap) in Text modes"
427 toggle-text-mode-auto-fill .
428 (:button (:toggle . (member 'turn-on-auto-fill text-mode-hook)))))
8ad97b87 429(define-key menu-bar-options-menu [toggle-global-lazy-font-lock-mode]
89f1709f
RS
430 (menu-bar-make-toggle toggle-global-lazy-font-lock-mode global-font-lock-mode
431 "Global Font Lock (highlights syntax)"
8ad97b87
RS
432 "Global Font Lock mode %s"
433 ;; Make sure a support mode is used;
434 ;; otherwise Font Lock will be too slow.
21efd27b 435 (require 'font-lock)
8ad97b87
RS
436 (if (not global-font-lock-mode)
437 (or font-lock-support-mode
438 (setq font-lock-support-mode 'lazy-lock-mode)))
439 (global-font-lock-mode)))
e5683624
RS
440\f
441(defvar menu-bar-describe-menu (make-sparse-keymap "Describe"))
442
443(define-key menu-bar-describe-menu [describe-variable]
444 '("Describe Variable..." . describe-variable))
445(define-key menu-bar-describe-menu [describe-function]
446 '("Describe Function..." . describe-function))
447(define-key menu-bar-describe-menu [describe-key]
448 '("Describe Key..." . describe-key))
449(define-key menu-bar-describe-menu [list-keybindings]
450 '("List Key Bindings" . describe-bindings))
a16f6b16 451(define-key menu-bar-describe-menu [apropos-variables]
e5683624 452 '("Apropos Variables..." . apropos-variable))
a16f6b16
SM
453(define-key menu-bar-describe-menu [apropos-commands]
454 '("Apropos Commands..." . apropos-command))
e5683624
RS
455(define-key menu-bar-describe-menu [describe-mode]
456 '("Describe Buffer Modes" . describe-mode))
457
458(defvar menu-bar-manuals-menu (make-sparse-keymap "Manuals"))
459
460(define-key menu-bar-manuals-menu [man]
461 '("Read Man Page..." . manual-entry))
462(define-key menu-bar-manuals-menu [sep2]
463 '("--"))
464(define-key menu-bar-manuals-menu [key]
465 '("Find Key in Manual" . Info-goto-emacs-key-command-node))
466(define-key menu-bar-manuals-menu [command]
467 '("Find Command in Manual" . Info-goto-emacs-command-node))
468(define-key menu-bar-manuals-menu [info]
469 '("Browse Manuals with Info" . info))
470(define-key menu-bar-manuals-menu [sep1]
471 '("--"))
472(define-key menu-bar-manuals-menu [emacs-faq]
473 '("Emacs FAQ" . view-emacs-FAQ))
474(define-key menu-bar-manuals-menu [emacs-news]
475 '("Emacs News" . view-emacs-news))
bacb2d12 476
f3e1e0dd
RS
477(define-key menu-bar-help-menu [describe-no-warranty]
478 '("(Non)Warranty" . describe-no-warranty))
479(define-key menu-bar-help-menu [describe-copying]
480 '("Copying Conditions" . describe-copying))
481(define-key menu-bar-help-menu [describe-distribution]
482 '("Getting New Versions" . describe-distribution))
efb166ff
RS
483(define-key menu-bar-help-menu [emacs-version]
484 '("Show Version" . emacs-version))
48433a65 485(define-key menu-bar-help-menu [report-emacs-bug]
1c6e18f6 486 '("Send Bug Report..." . report-emacs-bug))
e5683624
RS
487(define-key menu-bar-help-menu [sep2]
488 '("--"))
260130bb 489(define-key menu-bar-help-menu [finder-by-keyword]
e5683624
RS
490 '("Find Emacs Packages..." . finder-by-keyword))
491(define-key menu-bar-help-menu [describe]
492 (cons "Describe" menu-bar-describe-menu))
493(define-key menu-bar-help-menu [manuals]
494 (cons "Manuals" menu-bar-manuals-menu))
ca9b40a1 495(define-key menu-bar-help-menu [emacs-tutorial]
db774a16 496 '("Emacs Tutorial" . help-with-tutorial))
e5683624
RS
497(define-key menu-bar-help-menu [sep1]
498 '("--"))
499(define-key menu-bar-help-menu [options]
bacb2d12 500 (cons "Options" menu-bar-options-menu))
e5683624 501(define-key menu-bar-help-menu [customize]
a0cd87a4 502 (cons "Customize" menu-bar-custom-menu))
889560ed 503
db774a16
RS
504(defun kill-this-buffer () ; for the menubar
505 "Kills the current buffer."
506 (interactive)
507 (kill-buffer (current-buffer)))
508
2f1139a4
RS
509(defun kill-this-buffer-enabled-p ()
510 (let ((count 0)
511 (buffers (buffer-list)))
512 (while buffers
513 (or (string-match "^ " (buffer-name (car buffers)))
514 (setq count (1+ count)))
515 (setq buffers (cdr buffers)))
a6f8cbf1 516 (and (not (window-minibuffer-p (frame-selected-window menu-updating-frame)))
1f50590a
RS
517 (> count 1))))
518
519(put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
520
521(put 'save-buffer 'menu-enable
522 '(and (buffer-modified-p)
a6f8cbf1 523 (not (window-minibuffer-p (frame-selected-window menu-updating-frame)))))
1f50590a
RS
524
525(put 'write-file 'menu-enable
a6f8cbf1 526 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
1f50590a
RS
527
528(put 'find-file 'menu-enable
a6f8cbf1 529 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
1f50590a
RS
530
531(put 'dired 'menu-enable
a6f8cbf1 532 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
1f50590a
RS
533
534(put 'insert-file 'menu-enable
a6f8cbf1 535 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
2f1139a4 536
da183f87
RS
537(put 'revert-buffer 'menu-enable
538 '(or revert-buffer-function revert-buffer-insert-file-contents-function
539 (and (buffer-file-name)
660fa562
RM
540 (or (buffer-modified-p)
541 (not (verify-visited-file-modtime (current-buffer)))))))
82a74107 542
a0213a97
RS
543;; Permit deleting frame if it would leave a visible or iconified frame.
544(put 'delete-frame 'menu-enable
82a74107
KH
545 '(delete-frame-enabled-p))
546
547(defun delete-frame-enabled-p ()
548 "Return non-nil if `delete-frame' should be enabled in the menu bar."
549 (let ((frames (frame-list))
27aa8132
RS
550 (count 0))
551 (while frames
552 (if (frame-visible-p (car frames))
553 (setq count (1+ count)))
554 (setq frames (cdr frames)))
555 (> count 1)))
2f1139a4 556
db774a16
RS
557(put 'advertised-undo 'menu-enable
558 '(and (not (eq t buffer-undo-list))
559 (if (eq last-command 'undo)
2f1139a4
RS
560 (and (boundp 'pending-undo-list)
561 pending-undo-list)
562 buffer-undo-list)))
2877eac2 563
30e19aee
RS
564(defcustom yank-menu-length 20
565 "*Maximum length to display in the yank-menu."
566 :type 'integer
567 :group 'mouse)
3dd92899
KH
568
569(defun menu-bar-update-yank-menu (string old)
570 (let ((front (car (cdr yank-menu)))
571 (menu-string (if (<= (length string) yank-menu-length)
572 string
c3e1d435
RS
573 (concat
574 (substring string 0 (/ yank-menu-length 2))
575 "..."
576 (substring string (- (/ yank-menu-length 2)))))))
7c70a955
RS
577 ;; Don't let the menu string be all dashes
578 ;; because that has a special meaning in a menu.
579 (if (string-match "\\`-+\\'" menu-string)
580 (setq menu-string (concat menu-string " ")))
3dd92899
KH
581 ;; If we're supposed to be extending an existing string, and that
582 ;; string really is at the front of the menu, then update it in place.
583 (if (and old (or (eq old (car front))
584 (string= old (car front))))
dfabc98f 585 (progn
3dd92899
KH
586 (setcar front string)
587 (setcar (cdr front) menu-string))
588 (setcdr yank-menu
589 (cons
590 (cons string (cons menu-string 'menu-bar-select-yank))
591 (cdr yank-menu)))))
592 (if (> (length (cdr yank-menu)) kill-ring-max)
593 (setcdr (nthcdr kill-ring-max yank-menu) nil)))
594
4cca6fbd 595(put 'menu-bar-select-yank 'apropos-inhibit t)
3dd92899
KH
596(defun menu-bar-select-yank ()
597 (interactive "*")
598 (push-mark (point))
599 (insert last-command-event))
40954111 600\f
3f557298
RS
601;; This definition is just to show what this looks like.
602;; It gets overridden below when menu-bar-update-buffers is called.
603(define-key global-map [menu-bar buffer]
604 (cons "Buffers" (make-sparse-keymap "Buffers")))
40954111 605
d0690d12
RS
606(defvar list-buffers-directory nil)
607
08e8171f
RS
608(defvar menu-bar-update-buffers-maxbuf)
609
09642d97
RS
610(defun menu-bar-select-buffer ()
611 (interactive)
612 (switch-to-buffer last-command-event))
613
614(defun menu-bar-select-frame ()
615 (interactive)
616 (make-frame-visible last-command-event)
617 (raise-frame last-command-event)
618 (select-frame last-command-event))
619
c171b42f
RS
620(defun menu-bar-update-buffers-1 (elt)
621 (cons (format
08e8171f 622 (format "%%%ds %%s%%s %%s" menu-bar-update-buffers-maxbuf)
c171b42f
RS
623 (cdr elt)
624 (if (buffer-modified-p (car elt))
625 "*" " ")
626 (save-excursion
627 (set-buffer (car elt))
628 (if buffer-read-only "%" " "))
629 (let ((file
630 (or (buffer-file-name (car elt))
631 (save-excursion
632 (set-buffer (car elt))
633 list-buffers-directory)
634 "")))
635 (setq file (or (file-name-directory file)
636 ""))
637 (if (> (length file) 20)
638 (setq file (concat "..." (substring file -17))))
639 file))
640 (car elt)))
641
06ff7539
RS
642(defvar menu-bar-buffers-menu-list-buffers-entry nil)
643
09642d97 644(defun menu-bar-update-buffers ()
29397c58 645 ;; If user discards the Buffers item, play along.
4d587a6c 646 (and (lookup-key (current-global-map) [menu-bar buffer])
29397c58
RS
647 (frame-or-buffer-changed-p)
648 (let ((buffers (buffer-list))
649 (frames (frame-list))
b3398af1 650 (maxlen 0)
29397c58
RS
651 buffers-menu frames-menu)
652 ;; If requested, list only the N most recently selected buffers.
653 (if (and (integerp buffers-menu-max-size)
654 (> buffers-menu-max-size 1))
655 (if (> (length buffers) buffers-menu-max-size)
656 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
657
658 ;; Make the menu of buffers proper.
659 (setq buffers-menu
660 (cons "Select Buffer"
c171b42f
RS
661 (let* ((buffer-list
662 (mapcar 'list buffers))
663 tail
08e8171f 664 (menu-bar-update-buffers-maxbuf 0)
c171b42f
RS
665 alist
666 head)
667 ;; Put into each element of buffer-list
668 ;; the name for actual display,
669 ;; perhaps truncated in the middle.
670 (setq tail buffer-list)
671 (while tail
672 (let ((name (buffer-name (car (car tail)))))
673 (setcdr (car tail)
674 (if (> (length name) 27)
675 (concat (substring name 0 12)
676 "..."
677 (substring name -12))
678 name)))
679 (setq tail (cdr tail)))
680 ;; Compute the maximum length of any name.
681 (setq tail buffer-list)
29397c58 682 (while tail
c171b42f 683 (or (eq ?\ (aref (cdr (car tail)) 0))
08e8171f
RS
684 (setq menu-bar-update-buffers-maxbuf
685 (max menu-bar-update-buffers-maxbuf
c171b42f 686 (length (cdr (car tail))))))
29397c58 687 (setq tail (cdr tail)))
c171b42f
RS
688 ;; Set ALIST to an alist of the form
689 ;; ITEM-STRING . BUFFER
690 (setq tail buffer-list)
29397c58
RS
691 (while tail
692 (let ((elt (car tail)))
c171b42f 693 (or (eq ?\ (aref (cdr elt) 0))
29397c58 694 (setq alist (cons
c171b42f 695 (menu-bar-update-buffers-1 elt)
29397c58
RS
696 alist)))
697 (and alist (> (length (car (car alist))) maxlen)
698 (setq maxlen (length (car (car alist))))))
699 (setq tail (cdr tail)))
700 (setq alist (nreverse alist))
06ff7539
RS
701 ;; Make the menu item for list-buffers
702 ;; or reuse the one we already have.
703 ;; The advantage in reusing one
704 ;; is that it already has the keyboard equivalent
705 ;; cached, so we save the time to look that up again.
706 (or menu-bar-buffers-menu-list-buffers-entry
707 (setq menu-bar-buffers-menu-list-buffers-entry
708 (cons
709 'list-buffers
710 (cons
711 ""
712 'list-buffers))))
713 ;; Update the item string for menu's new width.
714 (setcar (cdr menu-bar-buffers-menu-list-buffers-entry)
715 (concat (make-string (max (- (/ maxlen 2) 8) 0)
716 ?\ )
717 "List All Buffers"))
718 ;; Now make the actual list of items,
719 ;; ending with the list-buffers item.
29397c58
RS
720 (nconc (mapcar '(lambda (pair)
721 ;; This is somewhat risque, to use
722 ;; the buffer name itself as the event
723 ;; type to define, but it works.
724 ;; It would not work to use the buffer
725 ;; since a buffer as an event has its
726 ;; own meaning.
727 (nconc (list (buffer-name (cdr pair))
728 (car pair)
729 (cons nil nil))
730 'menu-bar-select-buffer))
731 alist)
06ff7539 732 (list menu-bar-buffers-menu-list-buffers-entry)))))
29397c58
RS
733
734
735 ;; Make a Frames menu if we have more than one frame.
736 (if (cdr frames)
b3398af1
RS
737 (let ((name (concat (make-string (max (- (/ maxlen 2) 3) 0)
738 ?\ )
739 "Frames"))
740 (frames-menu
741 (cons 'keymap
742 (cons "Select Frame"
743 (mapcar '(lambda (frame)
744 (nconc (list frame
745 (cdr (assq 'name
746 (frame-parameters frame)))
747 (cons nil nil))
748 'menu-bar-select-frame))
749 frames)))))
750 ;; Put it underneath the Buffers menu.
751 (setq buffers-menu (cons (cons 'frames (cons name frames-menu))
752 buffers-menu))))
29397c58
RS
753 (if buffers-menu
754 (setq buffers-menu (cons 'keymap buffers-menu)))
4d587a6c 755 (define-key (current-global-map) [menu-bar buffer]
b3398af1 756 (cons "Buffers" buffers-menu)))))
09642d97
RS
757
758(add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
2f1139a4 759
3f557298
RS
760(menu-bar-update-buffers)
761
40954111
RS
762;; this version is too slow
763;;;(defun format-buffers-menu-line (buffer)
764;;; "Returns a string to represent the given buffer in the Buffer menu.
765;;;nil means the buffer shouldn't be listed. You can redefine this."
766;;; (if (string-match "\\` " (buffer-name buffer))
767;;; nil
768;;; (save-excursion
769;;; (set-buffer buffer)
770;;; (let ((size (buffer-size)))
771;;; (format "%s%s %-19s %6s %-15s %s"
772;;; (if (buffer-modified-p) "*" " ")
773;;; (if buffer-read-only "%" " ")
774;;; (buffer-name)
775;;; size
776;;; mode-name
777;;; (or (buffer-file-name) ""))))))
778\f
25b048ee
RS
779;;; Set up a menu bar menu for the minibuffer.
780
781(mapcar
782 (function
783 (lambda (map)
784 (define-key map [menu-bar minibuf]
785 (cons "Minibuf" (make-sparse-keymap "Minibuf")))))
786 (list minibuffer-local-ns-map
787 minibuffer-local-must-match-map
788 minibuffer-local-isearch-map
789 minibuffer-local-map
790 minibuffer-local-completion-map))
791
792(mapcar
793 (function
794 (lambda (map)
795 (define-key map [menu-bar minibuf ?\?]
796 '("List Completions" . minibuffer-completion-help))
797 (define-key map [menu-bar minibuf space]
798 '("Complete Word" . minibuffer-complete-word))
799 (define-key map [menu-bar minibuf tab]
4e9d1906 800 '("Complete" . minibuffer-complete))
25b048ee
RS
801 ))
802 (list minibuffer-local-must-match-map
803 minibuffer-local-completion-map))
804
805(mapcar
806 (function
807 (lambda (map)
808 (define-key map [menu-bar minibuf quit]
809 '("Quit" . keyboard-escape-quit))
810 (define-key map [menu-bar minibuf return]
811 '("Enter" . exit-minibuffer))
812 ))
813 (list minibuffer-local-ns-map
814 minibuffer-local-must-match-map
815 minibuffer-local-isearch-map
816 minibuffer-local-map
817 minibuffer-local-completion-map))
818\f
7b7d6615
RS
819(defvar menu-bar-mode nil)
820
057d49d1 821(defun menu-bar-mode (flag)
dfd29450 822 "Toggle display of a menu bar on each frame.
057d49d1
RS
823This command applies to all frames that exist and frames to be
824created in the future.
825With a numeric argument, if the argument is negative,
dfd29450 826turn off menu bars; otherwise, turn on menu bars."
dad8e392 827 (interactive "P")
dad8e392 828
7b7d6615
RS
829 ;; Make menu-bar-mode and default-frame-alist consistent.
830 (let ((default (assq 'menu-bar-lines default-frame-alist)))
831 (if default
832 (setq menu-bar-mode (not (eq (cdr default) 0)))
833 (setq default-frame-alist
834 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
835 default-frame-alist))))
836
837 ;; Toggle or set the mode, according to FLAG.
838 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
839 (> (prefix-numeric-value flag) 0)))
840
841 ;; Apply it to default-frame-alist.
842 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
843 (if (consp parameter)
844 (setcdr parameter (if menu-bar-mode 1 0))
845 (setq default-frame-alist
846 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
847 default-frame-alist))))
848
849 ;; Apply it to existing frames.
850 (let ((frames (frame-list)))
851 (while frames
852 (let ((height (cdr (assq 'height (frame-parameters (car frames))))))
853 (modify-frame-parameters (car frames)
854 (list (cons 'menu-bar-lines
855 (if menu-bar-mode 1 0))))
856 (modify-frame-parameters (car frames)
857 (list (cons 'height height))))
858 (setq frames (cdr frames)))))
1db87953 859
bffa5d4d
RS
860(provide 'menu-bar)
861
235aa29b 862;;; menu-bar.el ends here