diary-mail-entries calls exit-calendar when finished.
[bpt/emacs.git] / lisp / menu-bar.el
1 ;;; menu-bar.el --- define a default menu bar.
2
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: RMS
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
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
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.
25
26 ;; Avishai Yacobi suggested some menu rearrangements.
27
28 ;;; Code:
29
30 ;;; User options:
31
32 (defcustom buffers-menu-max-size 10
33 "*Maximum number of entries which may appear on the Buffers menu.
34 If this is 10, then only the ten most-recently-selected buffers are shown.
35 If this is nil, then all buffers are shown.
36 A large number or nil slows down menu responsiveness."
37 :type '(choice integer
38 (const :tag "All" nil))
39 :group 'mouse)
40
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")))
45 (defvar menu-bar-help-menu (make-sparse-keymap "Help"))
46
47 ;; Force Help item to come last, after the major mode's own items.
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))
51
52 (define-key global-map [menu-bar help-menu] (cons "Help" menu-bar-help-menu))
53 (defvar menu-bar-search-menu (make-sparse-keymap "Search"))
54 (define-key global-map [menu-bar search] (cons "Search" menu-bar-search-menu))
55 (defvar menu-bar-edit-menu (make-sparse-keymap "Edit"))
56 (define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
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))
61
62 ;; This alias is for compatibility with 19.28 and before.
63 (defvar menu-bar-file-menu menu-bar-files-menu)
64 \f
65 (defvar vc-menu-map (make-sparse-keymap "Version Control"))
66
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
74 (define-key menu-bar-tools-menu [calendar] '("Display Calendar" . calendar))
75 (define-key menu-bar-tools-menu [compose-mail] '("Send Mail" . compose-mail))
76 (define-key menu-bar-tools-menu [rmail] '("Read Mail" . rmail))
77 (define-key menu-bar-tools-menu [gnus] '("Read Net News" . gnus))
78
79 (define-key menu-bar-tools-menu [separator-vc]
80 '("--"))
81
82 (define-key menu-bar-tools-menu [vc]
83 (cons "Version Control" vc-menu-map))
84
85 (define-key menu-bar-tools-menu [separator-compare]
86 '("--"))
87
88 (define-key menu-bar-tools-menu [ediff-misc]
89 '("Ediff Miscellanea" . menu-bar-ediff-misc-menu))
90 (define-key menu-bar-tools-menu [epatch]
91 '("Apply Patch" . menu-bar-epatch-menu))
92 (define-key menu-bar-tools-menu [ediff-merge]
93 '("Merge" . menu-bar-ediff-merge-menu))
94 (define-key menu-bar-tools-menu [compare]
95 '("Compare" . menu-bar-ediff-menu))
96
97 (define-key menu-bar-tools-menu [separator-print]
98 '("--"))
99
100 (defvar menu-bar-print-menu (make-sparse-keymap "Print"))
101
102 (define-key menu-bar-print-menu [ps-print-region]
103 '("Postscript Print Region" . ps-print-region-with-faces))
104 (define-key menu-bar-print-menu [ps-print-buffer]
105 '("Postscript Print Buffer" . ps-print-buffer-with-faces))
106 (define-key menu-bar-print-menu [separator-ps-print]
107 '("--"))
108 (define-key menu-bar-print-menu [print-region]
109 '("Print Region" . print-region))
110 (define-key menu-bar-print-menu [print-buffer]
111 '("Print Buffer" . print-buffer))
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)
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))
130
131 (if (fboundp 'delete-frame)
132 (progn
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]
136 '("Delete Frame" . delete-frame))
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]
140 '("Make New Frame" . make-frame-command))))
141
142 (define-key menu-bar-files-menu [separator-buffers]
143 '("--"))
144
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]
148 '("Insert File..." . insert-file))
149 (define-key menu-bar-files-menu [recover-session]
150 '("Recover Session..." . recover-session))
151 (define-key menu-bar-files-menu [revert-buffer]
152 '("Revert Buffer" . revert-buffer))
153 (define-key menu-bar-files-menu [write-file]
154 '("Save Buffer As..." . write-file))
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))
158
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)))
168 \f
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
201 (defun nonincremental-repeat-search-forward ()
202 "Search forward for the previous search string."
203 (interactive)
204 (if (null search-ring)
205 (error "No previous search"))
206 (search-forward (car search-ring)))
207
208 (defun nonincremental-repeat-search-backward ()
209 "Search backward for the previous search string."
210 (interactive)
211 (if (null search-ring)
212 (error "No previous search"))
213 (search-backward (car search-ring)))
214
215 (defun nonincremental-repeat-re-search-forward ()
216 "Search forward for the previous regular expression."
217 (interactive)
218 (if (null regexp-search-ring)
219 (error "No previous search"))
220 (re-search-forward (car regexp-search-ring)))
221
222 (defun nonincremental-repeat-re-search-backward ()
223 "Search backward for the previous regular expression."
224 (interactive)
225 (if (null regexp-search-ring)
226 (error "No previous search"))
227 (re-search-backward (car regexp-search-ring)))
228
229 (define-key menu-bar-search-menu [query-replace-regexp]
230 '("Query Replace Regexp..." . query-replace-regexp))
231 (define-key menu-bar-search-menu [query-replace]
232 '("Query Replace..." . query-replace))
233 (define-key menu-bar-search-menu [find-tag]
234 '("Find Tag..." . find-tag))
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
241 (define-key menu-bar-search-menu [repeat-regexp-back]
242 '("Repeat Regexp Backwards" . nonincremental-repeat-re-search-backward))
243 (define-key menu-bar-search-menu [repeat-search-back]
244 '("Repeat Backwards" . nonincremental-repeat-search-backward))
245 (define-key menu-bar-search-menu [repeat-regexp-fwd]
246 '("Repeat Regexp" . nonincremental-repeat-re-search-forward))
247 (define-key menu-bar-search-menu [repeat-search-fwd]
248 '("Repeat Search" . nonincremental-repeat-search-forward))
249
250 (define-key menu-bar-search-menu [separator-repeat]
251 '("--"))
252
253 (define-key menu-bar-search-menu [re-search-backward]
254 '("Regexp Search Backwards..." . nonincremental-re-search-backward))
255 (define-key menu-bar-search-menu [search-backward]
256 '("Search Backwards..." . nonincremental-search-backward))
257 (define-key menu-bar-search-menu [re-search-forward]
258 '("Regexp Search..." . nonincremental-re-search-forward))
259 (define-key menu-bar-search-menu [search-forward]
260 '("Search..." . nonincremental-search-forward))
261 \f
262 (if (fboundp 'start-process)
263 (define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map)))
264 (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
265 (define-key menu-bar-edit-menu [props] '("Text Properties" . facemenu-menu))
266
267 (define-key menu-bar-edit-menu [separator-edit]
268 '("--"))
269
270 (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
271
272 (define-key menu-bar-edit-menu [paste] '("Paste Most Recent" . yank))
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))
277 (define-key menu-bar-edit-menu [copy] '("Copy" . menu-bar-kill-ring-save))
278 (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
279 (define-key menu-bar-edit-menu [undo] '("Undo" . undo))
280
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
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)))
289 (put 'menu-bar-kill-ring-save 'menu-enable 'mark-active)
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)))
292 (put 'delete-region 'menu-enable '(and mark-active
293 (not buffer-read-only)
294 (not (mouse-region-match))))
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))))
299 (put 'query-replace 'menu-enable '(not buffer-read-only))
300 (put 'query-replace-regexp 'menu-enable '(not buffer-read-only))
301
302 (autoload 'ispell-menu-map "ispell" nil t 'keymap)
303
304 ;; These are alternative definitions for the cut, paste and copy
305 ;; menu items. Use them if your system expects these to use the clipboard.
306
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 ()
331 "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard.
332 Do the same for the keys of the same name."
333 (interactive)
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]
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))
350 \f
351
352 ;;; Menu support
353
354 (defvar menu-bar-custom-menu (make-sparse-keymap "Customize"))
355
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))
362 (define-key menu-bar-custom-menu [customize-apropos]
363 '("Apropos..." . customize-apropos))
364 (define-key menu-bar-custom-menu [separator-2]
365 '("--"))
366 (define-key menu-bar-custom-menu [customize-group]
367 '("Specific Group..." . customize-group))
368 (define-key menu-bar-custom-menu [customize-face]
369 '("Specific Face..." . customize-face))
370 (define-key menu-bar-custom-menu [customize-option]
371 '("Specific Option..." . customize-option))
372 (define-key menu-bar-custom-menu [customize-changed-options]
373 '("Changed Options..." . customize-changed-options))
374 (define-key menu-bar-custom-menu [separator-3]
375 '("--"))
376 (define-key menu-bar-custom-menu [customize-browse]
377 '("Browse Customization Groups" . customize-browse))
378 (define-key menu-bar-custom-menu [customize]
379 '("Top-level Customization Group" . customize))
380
381 ;; Options menu
382 (defvar menu-bar-options-menu (make-sparse-keymap "Global Options"))
383
384 (defmacro menu-bar-make-toggle (name variable doc message &rest body)
385 `(progn
386 (defun ,name ()
387 ,(concat doc ".")
388 (interactive)
389 (if ,(if body `(progn . ,body)
390 `(setq ,variable (not ,variable)))
391 (message ,message "enabled")
392 (message ,message "disabled")))
393 '(menu-item ,doc ,name .
394 (:button (:toggle . (and (boundp ',variable) ,variable))))))
395
396 (define-key menu-bar-options-menu [debug-on-quit]
397 (menu-bar-make-toggle toggle-debug-on-quit debug-on-quit
398 "Debug on Quit" "Debug on Quit %s"))
399 (define-key menu-bar-options-menu [debug-on-error]
400 (menu-bar-make-toggle toggle-debug-on-error debug-on-error
401 "Debug on Error" "Debug on Error %s"))
402 (define-key menu-bar-options-menu [options-separator]
403 '("--"))
404 (define-key menu-bar-options-menu [save-place]
405 (menu-bar-make-toggle toggle-save-place-globally save-place
406 "Save Place in Files between Sessions"
407 "Saving place in files %s"
408 (require 'saveplace)
409 (setq-default save-place (not (default-value save-place)))))
410 (define-key menu-bar-options-menu [uniquify]
411 (menu-bar-make-toggle toggle-uniquify-buffer-names uniquify-buffer-name-style
412 "Use Directory Names in Buffer Names"
413 "Directory name in buffer names (uniquify) %s"
414 (require 'uniquify)
415 (setq uniquify-buffer-name-style
416 (if (not uniquify-buffer-name-style)
417 'forward))))
418 (define-key menu-bar-options-menu [case-fold-search]
419 (menu-bar-make-toggle toggle-case-fold-search case-fold-search
420 "Case folding in searches"
421 "Case folding in searches %s"))
422 (define-key menu-bar-options-menu [transient-mark-mode]
423 (menu-bar-make-toggle toggle-transient-mark-mode transient-mark-mode
424 "Transient Mark Mode (highlights region)"
425 "Transient Mark mode %s"))
426 (define-key menu-bar-options-menu [toggle-auto-compression]
427 '(menu-item "Automatic File De/compression"
428 auto-compression-mode .
429 (:button (:toggle . (rassq 'jka-compr-handler
430 file-name-handler-alist)))))
431 (define-key menu-bar-options-menu [auto-fill-mode]
432 '(menu-item "Auto Fill (word wrap) in Text modes"
433 toggle-text-mode-auto-fill .
434 (:button (:toggle . (member 'turn-on-auto-fill text-mode-hook)))))
435 (define-key menu-bar-options-menu [toggle-global-lazy-font-lock-mode]
436 (menu-bar-make-toggle toggle-global-lazy-font-lock-mode global-font-lock-mode
437 "Global Font Lock (highlights syntax)"
438 "Global Font Lock mode %s"
439 ;; Make sure a support mode is used;
440 ;; otherwise Font Lock will be too slow.
441 (require 'font-lock)
442 (if (not global-font-lock-mode)
443 (or font-lock-support-mode
444 (setq font-lock-support-mode 'lazy-lock-mode)))
445 (global-font-lock-mode)))
446 \f
447 (defvar menu-bar-describe-menu (make-sparse-keymap "Describe"))
448
449 (define-key menu-bar-describe-menu [describe-variable]
450 '("Describe Variable..." . describe-variable))
451 (define-key menu-bar-describe-menu [describe-function]
452 '("Describe Function..." . describe-function))
453 (define-key menu-bar-describe-menu [describe-key]
454 '("Describe Key..." . describe-key))
455 (define-key menu-bar-describe-menu [list-keybindings]
456 '("List Key Bindings" . describe-bindings))
457 (define-key menu-bar-describe-menu [apropos-variables]
458 '("Apropos Variables..." . apropos-variable))
459 (define-key menu-bar-describe-menu [apropos-commands]
460 '("Apropos Commands..." . apropos-command))
461 (define-key menu-bar-describe-menu [describe-mode]
462 '("Describe Buffer Modes" . describe-mode))
463
464 (defvar menu-bar-manuals-menu (make-sparse-keymap "Manuals"))
465
466 (define-key menu-bar-manuals-menu [man]
467 '("Read Man Page..." . manual-entry))
468 (define-key menu-bar-manuals-menu [sep2]
469 '("--"))
470 (define-key menu-bar-manuals-menu [key]
471 '("Find Key in Manual" . Info-goto-emacs-key-command-node))
472 (define-key menu-bar-manuals-menu [command]
473 '("Find Command in Manual" . Info-goto-emacs-command-node))
474 (define-key menu-bar-manuals-menu [info]
475 '("Browse Manuals with Info" . info))
476 (define-key menu-bar-manuals-menu [sep1]
477 '("--"))
478 (define-key menu-bar-manuals-menu [emacs-faq]
479 '("Emacs FAQ" . view-emacs-FAQ))
480 (define-key menu-bar-manuals-menu [emacs-news]
481 '("Emacs News" . view-emacs-news))
482
483 (define-key menu-bar-help-menu [describe-no-warranty]
484 '("(Non)Warranty" . describe-no-warranty))
485 (define-key menu-bar-help-menu [describe-copying]
486 '("Copying Conditions" . describe-copying))
487 (define-key menu-bar-help-menu [describe-distribution]
488 '("Getting New Versions" . describe-distribution))
489 (define-key menu-bar-help-menu [emacs-version]
490 '("Show Version" . emacs-version))
491 (define-key menu-bar-help-menu [report-emacs-bug]
492 '("Send Bug Report..." . report-emacs-bug))
493 (define-key menu-bar-help-menu [sep2]
494 '("--"))
495 (define-key menu-bar-help-menu [finder-by-keyword]
496 '("Find Emacs Packages..." . finder-by-keyword))
497 (define-key menu-bar-help-menu [describe]
498 (cons "Describe" menu-bar-describe-menu))
499 (define-key menu-bar-help-menu [manuals]
500 (cons "Manuals" menu-bar-manuals-menu))
501 (define-key menu-bar-help-menu [emacs-tutorial]
502 '("Emacs Tutorial" . help-with-tutorial))
503 (define-key menu-bar-help-menu [sep1]
504 '("--"))
505 (define-key menu-bar-help-menu [options]
506 (cons "Options" menu-bar-options-menu))
507 (define-key menu-bar-help-menu [customize]
508 (cons "Customize" menu-bar-custom-menu))
509
510 (defun kill-this-buffer () ; for the menubar
511 "Kills the current buffer."
512 (interactive)
513 (kill-buffer (current-buffer)))
514
515 (defun kill-this-buffer-enabled-p ()
516 (let ((count 0)
517 (buffers (buffer-list)))
518 (while buffers
519 (or (string-match "^ " (buffer-name (car buffers)))
520 (setq count (1+ count)))
521 (setq buffers (cdr buffers)))
522 (and (not (window-minibuffer-p (frame-selected-window menu-updating-frame)))
523 (> count 1))))
524
525 (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
526
527 (put 'save-buffer 'menu-enable
528 '(and (buffer-modified-p)
529 (not (window-minibuffer-p (frame-selected-window menu-updating-frame)))))
530
531 (put 'write-file 'menu-enable
532 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
533
534 (put 'find-file 'menu-enable
535 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
536
537 (put 'dired 'menu-enable
538 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
539
540 (put 'insert-file 'menu-enable
541 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
542
543 (put 'revert-buffer 'menu-enable
544 '(or revert-buffer-function revert-buffer-insert-file-contents-function
545 (and (buffer-file-name)
546 (or (buffer-modified-p)
547 (not (verify-visited-file-modtime (current-buffer)))))))
548
549 ;; Permit deleting frame if it would leave a visible or iconified frame.
550 (put 'delete-frame 'menu-enable
551 '(delete-frame-enabled-p))
552
553 (defun delete-frame-enabled-p ()
554 "Return non-nil if `delete-frame' should be enabled in the menu bar."
555 (let ((frames (frame-list))
556 (count 0))
557 (while frames
558 (if (frame-visible-p (car frames))
559 (setq count (1+ count)))
560 (setq frames (cdr frames)))
561 (> count 1)))
562
563 (put 'advertised-undo 'menu-enable
564 '(and (not (eq t buffer-undo-list))
565 (if (eq last-command 'undo)
566 (and (boundp 'pending-undo-list)
567 pending-undo-list)
568 buffer-undo-list)))
569
570 (defcustom yank-menu-length 20
571 "*Maximum length to display in the yank-menu."
572 :type 'integer
573 :group 'mouse)
574
575 (defun menu-bar-update-yank-menu (string old)
576 (let ((front (car (cdr yank-menu)))
577 (menu-string (if (<= (length string) yank-menu-length)
578 string
579 (concat
580 (substring string 0 (/ yank-menu-length 2))
581 "..."
582 (substring string (- (/ yank-menu-length 2)))))))
583 ;; Don't let the menu string be all dashes
584 ;; because that has a special meaning in a menu.
585 (if (string-match "\\`-+\\'" menu-string)
586 (setq menu-string (concat menu-string " ")))
587 ;; If we're supposed to be extending an existing string, and that
588 ;; string really is at the front of the menu, then update it in place.
589 (if (and old (or (eq old (car front))
590 (string= old (car front))))
591 (progn
592 (setcar front string)
593 (setcar (cdr front) menu-string))
594 (setcdr yank-menu
595 (cons
596 (cons string (cons menu-string 'menu-bar-select-yank))
597 (cdr yank-menu)))))
598 (if (> (length (cdr yank-menu)) kill-ring-max)
599 (setcdr (nthcdr kill-ring-max yank-menu) nil)))
600
601 (put 'menu-bar-select-yank 'apropos-inhibit t)
602 (defun menu-bar-select-yank ()
603 (interactive "*")
604 (push-mark (point))
605 (insert last-command-event))
606 \f
607 ;; This definition is just to show what this looks like.
608 ;; It gets overridden below when menu-bar-update-buffers is called.
609 (define-key global-map [menu-bar buffer]
610 (cons "Buffers" (make-sparse-keymap "Buffers")))
611
612 (defvar list-buffers-directory nil)
613
614 (defvar menu-bar-update-buffers-maxbuf)
615
616 (defun menu-bar-select-buffer ()
617 (interactive)
618 (switch-to-buffer last-command-event))
619
620 (defun menu-bar-select-frame ()
621 (interactive)
622 (make-frame-visible last-command-event)
623 (raise-frame last-command-event)
624 (select-frame last-command-event))
625
626 (defun menu-bar-update-buffers-1 (elt)
627 (cons (format
628 (format "%%%ds %%s%%s %%s" menu-bar-update-buffers-maxbuf)
629 (cdr elt)
630 (if (buffer-modified-p (car elt))
631 "*" " ")
632 (save-excursion
633 (set-buffer (car elt))
634 (if buffer-read-only "%" " "))
635 (let ((file
636 (or (buffer-file-name (car elt))
637 (save-excursion
638 (set-buffer (car elt))
639 list-buffers-directory)
640 "")))
641 (setq file (or (file-name-directory file)
642 ""))
643 (if (> (length file) 20)
644 (setq file (concat "..." (substring file -17))))
645 file))
646 (car elt)))
647
648 (defvar menu-bar-buffers-menu-list-buffers-entry nil)
649
650 (defun menu-bar-update-buffers ()
651 ;; If user discards the Buffers item, play along.
652 (and (lookup-key (current-global-map) [menu-bar buffer])
653 (frame-or-buffer-changed-p)
654 (let ((buffers (buffer-list))
655 (frames (frame-list))
656 (maxlen 0)
657 buffers-menu frames-menu)
658 ;; If requested, list only the N most recently selected buffers.
659 (if (and (integerp buffers-menu-max-size)
660 (> buffers-menu-max-size 1))
661 (if (> (length buffers) buffers-menu-max-size)
662 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
663
664 ;; Make the menu of buffers proper.
665 (setq buffers-menu
666 (cons "Select Buffer"
667 (let* ((buffer-list
668 (mapcar 'list buffers))
669 tail
670 (menu-bar-update-buffers-maxbuf 0)
671 alist
672 head)
673 ;; Put into each element of buffer-list
674 ;; the name for actual display,
675 ;; perhaps truncated in the middle.
676 (setq tail buffer-list)
677 (while tail
678 (let ((name (buffer-name (car (car tail)))))
679 (setcdr (car tail)
680 (if (> (length name) 27)
681 (concat (substring name 0 12)
682 "..."
683 (substring name -12))
684 name)))
685 (setq tail (cdr tail)))
686 ;; Compute the maximum length of any name.
687 (setq tail buffer-list)
688 (while tail
689 (or (eq ?\ (aref (cdr (car tail)) 0))
690 (setq menu-bar-update-buffers-maxbuf
691 (max menu-bar-update-buffers-maxbuf
692 (length (cdr (car tail))))))
693 (setq tail (cdr tail)))
694 ;; Set ALIST to an alist of the form
695 ;; ITEM-STRING . BUFFER
696 (setq tail buffer-list)
697 (while tail
698 (let ((elt (car tail)))
699 (or (eq ?\ (aref (cdr elt) 0))
700 (setq alist (cons
701 (menu-bar-update-buffers-1 elt)
702 alist)))
703 (and alist (> (length (car (car alist))) maxlen)
704 (setq maxlen (length (car (car alist))))))
705 (setq tail (cdr tail)))
706 (setq alist (nreverse alist))
707 ;; Make the menu item for list-buffers
708 ;; or reuse the one we already have.
709 ;; The advantage in reusing one
710 ;; is that it already has the keyboard equivalent
711 ;; cached, so we save the time to look that up again.
712 (or menu-bar-buffers-menu-list-buffers-entry
713 (setq menu-bar-buffers-menu-list-buffers-entry
714 (cons
715 'list-buffers
716 (cons
717 ""
718 'list-buffers))))
719 ;; Update the item string for menu's new width.
720 (setcar (cdr menu-bar-buffers-menu-list-buffers-entry)
721 (concat (make-string (max (- (/ maxlen 2) 8) 0)
722 ?\ )
723 "List All Buffers"))
724 ;; Now make the actual list of items,
725 ;; ending with the list-buffers item.
726 (nconc (mapcar '(lambda (pair)
727 ;; This is somewhat risque, to use
728 ;; the buffer name itself as the event
729 ;; type to define, but it works.
730 ;; It would not work to use the buffer
731 ;; since a buffer as an event has its
732 ;; own meaning.
733 (nconc (list (buffer-name (cdr pair))
734 (car pair)
735 (cons nil nil))
736 'menu-bar-select-buffer))
737 alist)
738 (list menu-bar-buffers-menu-list-buffers-entry)))))
739
740
741 ;; Make a Frames menu if we have more than one frame.
742 (if (cdr frames)
743 (let ((name (concat (make-string (max (- (/ maxlen 2) 3) 0)
744 ?\ )
745 "Frames"))
746 (frames-menu
747 (cons 'keymap
748 (cons "Select Frame"
749 (mapcar '(lambda (frame)
750 (nconc (list frame
751 (cdr (assq 'name
752 (frame-parameters frame)))
753 (cons nil nil))
754 'menu-bar-select-frame))
755 frames)))))
756 ;; Put it underneath the Buffers menu.
757 (setq buffers-menu (cons (cons 'frames (cons name frames-menu))
758 buffers-menu))))
759 (if buffers-menu
760 (setq buffers-menu (cons 'keymap buffers-menu)))
761 (define-key (current-global-map) [menu-bar buffer]
762 (cons "Buffers" buffers-menu)))))
763
764 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
765
766 (menu-bar-update-buffers)
767
768 ;; this version is too slow
769 ;;;(defun format-buffers-menu-line (buffer)
770 ;;; "Returns a string to represent the given buffer in the Buffer menu.
771 ;;;nil means the buffer shouldn't be listed. You can redefine this."
772 ;;; (if (string-match "\\` " (buffer-name buffer))
773 ;;; nil
774 ;;; (save-excursion
775 ;;; (set-buffer buffer)
776 ;;; (let ((size (buffer-size)))
777 ;;; (format "%s%s %-19s %6s %-15s %s"
778 ;;; (if (buffer-modified-p) "*" " ")
779 ;;; (if buffer-read-only "%" " ")
780 ;;; (buffer-name)
781 ;;; size
782 ;;; mode-name
783 ;;; (or (buffer-file-name) ""))))))
784 \f
785 ;;; Set up a menu bar menu for the minibuffer.
786
787 (mapcar
788 (function
789 (lambda (map)
790 (define-key map [menu-bar minibuf]
791 (cons "Minibuf" (make-sparse-keymap "Minibuf")))))
792 (list minibuffer-local-ns-map
793 minibuffer-local-must-match-map
794 minibuffer-local-isearch-map
795 minibuffer-local-map
796 minibuffer-local-completion-map))
797
798 (mapcar
799 (function
800 (lambda (map)
801 (define-key map [menu-bar minibuf ?\?]
802 '("List Completions" . minibuffer-completion-help))
803 (define-key map [menu-bar minibuf space]
804 '("Complete Word" . minibuffer-complete-word))
805 (define-key map [menu-bar minibuf tab]
806 '("Complete" . minibuffer-complete))
807 ))
808 (list minibuffer-local-must-match-map
809 minibuffer-local-completion-map))
810
811 (mapcar
812 (function
813 (lambda (map)
814 (define-key map [menu-bar minibuf quit]
815 '("Quit" . keyboard-escape-quit))
816 (define-key map [menu-bar minibuf return]
817 '("Enter" . exit-minibuffer))
818 ))
819 (list minibuffer-local-ns-map
820 minibuffer-local-must-match-map
821 minibuffer-local-isearch-map
822 minibuffer-local-map
823 minibuffer-local-completion-map))
824 \f
825 (defvar menu-bar-mode nil)
826
827 (defun menu-bar-mode (flag)
828 "Toggle display of a menu bar on each frame.
829 This command applies to all frames that exist and frames to be
830 created in the future.
831 With a numeric argument, if the argument is negative,
832 turn off menu bars; otherwise, turn on menu bars."
833 (interactive "P")
834
835 ;; Make menu-bar-mode and default-frame-alist consistent.
836 (let ((default (assq 'menu-bar-lines default-frame-alist)))
837 (if default
838 (setq menu-bar-mode (not (eq (cdr default) 0)))
839 (setq default-frame-alist
840 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
841 default-frame-alist))))
842
843 ;; Toggle or set the mode, according to FLAG.
844 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
845 (> (prefix-numeric-value flag) 0)))
846
847 ;; Apply it to default-frame-alist.
848 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
849 (if (consp parameter)
850 (setcdr parameter (if menu-bar-mode 1 0))
851 (setq default-frame-alist
852 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
853 default-frame-alist))))
854
855 ;; Apply it to existing frames.
856 (let ((frames (frame-list)))
857 (while frames
858 (let ((height (cdr (assq 'height (frame-parameters (car frames))))))
859 (modify-frame-parameters (car frames)
860 (list (cons 'menu-bar-lines
861 (if menu-bar-mode 1 0))))
862 (modify-frame-parameters (car frames)
863 (list (cons 'height height))))
864 (setq frames (cdr frames)))))
865
866 (provide 'menu-bar)
867
868 ;;; menu-bar.el ends here