(display-color-p): Use framep-on-display.
[emacs.git] / lisp / menu-bar.el
blob5da42f37c7fa7eb928d87c6d0c33ecc6919aed95
1 ;;; menu-bar.el --- define a default menu bar.
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: RMS
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;; Avishai Yacobi suggested some menu rearrangements.
28 ;;; Code:
30 ;;; User options:
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)
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"))
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))
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))
62 ;; This alias is for compatibility with 19.28 and before.
63 (defvar menu-bar-file-menu menu-bar-files-menu)
65 (defvar vc-menu-map (make-sparse-keymap "Version Control"))
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))
71 (define-key menu-bar-tools-menu [separator-1]
72 '("--"))
74 (define-key menu-bar-tools-menu [calendar] '("Display Calendar" . calendar))
75 (define-key menu-bar-tools-menu [speedbar]
76 '("Display Speedbar" . speedbar-frame-mode))
77 (define-key menu-bar-tools-menu [directory-search]
78 '("Directory Search" . eudc-tools-menu))
79 (define-key menu-bar-tools-menu [compose-mail] '("Send Mail" . compose-mail))
80 (define-key menu-bar-tools-menu [rmail] '("Read Mail" . read-mail-command))
81 (define-key menu-bar-tools-menu [gnus] '("Read Net News" . gnus))
83 (define-key menu-bar-tools-menu [separator-vc]
84 '("--"))
86 (define-key menu-bar-tools-menu [vc]
87 (cons "Version Control" vc-menu-map))
89 (define-key menu-bar-tools-menu [separator-compare]
90 '("--"))
92 (define-key menu-bar-tools-menu [ediff-misc]
93 '("Ediff Miscellanea" . menu-bar-ediff-misc-menu))
94 (define-key menu-bar-tools-menu [epatch]
95 '("Apply Patch" . menu-bar-epatch-menu))
96 (define-key menu-bar-tools-menu [ediff-merge]
97 '("Merge" . menu-bar-ediff-merge-menu))
98 (define-key menu-bar-tools-menu [compare]
99 '("Compare" . menu-bar-ediff-menu))
101 (define-key menu-bar-tools-menu [separator-print]
102 '("--"))
104 (defvar menu-bar-print-menu (make-sparse-keymap "Print"))
106 (define-key menu-bar-print-menu [ps-print-region]
107 '("Postscript Print Region" . ps-print-region-with-faces))
108 (define-key menu-bar-print-menu [ps-print-buffer]
109 '("Postscript Print Buffer" . ps-print-buffer-with-faces))
110 (define-key menu-bar-print-menu [separator-ps-print]
111 '("--"))
112 (define-key menu-bar-print-menu [print-region]
113 '("Print Region" . print-region))
114 (define-key menu-bar-print-menu [print-buffer]
115 '("Print Buffer" . print-buffer))
117 (define-key menu-bar-tools-menu [print]
118 (cons "Print" menu-bar-print-menu))
120 (put 'print-region 'menu-enable 'mark-active)
121 (put 'ps-print-region-with-faces 'menu-enable 'mark-active)
123 (define-key menu-bar-files-menu [exit-emacs]
124 '("Exit Emacs" . save-buffers-kill-emacs))
126 (define-key menu-bar-files-menu [separator-exit]
127 '("--"))
129 (define-key menu-bar-files-menu [one-window]
130 '("One Window" . delete-other-windows))
132 (define-key menu-bar-files-menu [split-window]
133 '("Split Window" . split-window-vertically))
135 (if (fboundp 'delete-frame)
136 (progn
137 ;; Don't use delete-frame as event name
138 ;; because that is a special event.
139 (define-key menu-bar-files-menu [delete-this-frame]
140 '("Delete Frame" . delete-frame))
141 (define-key menu-bar-files-menu [make-frame-on-display]
142 '("Open New Display..." . make-frame-on-display))
143 (define-key menu-bar-files-menu [make-frame]
144 '("Make New Frame" . make-frame-command))))
146 (define-key menu-bar-files-menu [separator-buffers]
147 '("--"))
149 (define-key menu-bar-files-menu [kill-buffer]
150 '("Kill Current Buffer" . kill-this-buffer))
151 (define-key menu-bar-files-menu [insert-file]
152 '("Insert File..." . insert-file))
153 (define-key menu-bar-files-menu [recover-session]
154 '("Recover Session..." . recover-session))
155 (define-key menu-bar-files-menu [revert-buffer]
156 '("Revert Buffer" . revert-buffer))
157 (define-key menu-bar-files-menu [write-file]
158 '("Save Buffer As..." . write-file))
159 (define-key menu-bar-files-menu [save-buffer] '("Save Buffer" . save-buffer))
160 (define-key menu-bar-files-menu [dired] '("Open Directory..." . dired))
161 (define-key menu-bar-files-menu [open-file] '("Open File..." . find-file))
163 (put 'recover-session 'menu-enable
164 '(and auto-save-list-file-prefix
165 (directory-files
166 (file-name-directory auto-save-list-file-prefix)
168 (concat "\\`"
169 (regexp-quote (file-name-nondirectory
170 auto-save-list-file-prefix)))
171 t)))
173 (defun nonincremental-search-forward (string)
174 "Read a string and search for it nonincrementally."
175 (interactive "sSearch for string: ")
176 (if (equal string "")
177 (search-forward (car search-ring))
178 (isearch-update-ring string nil)
179 (search-forward string)))
181 (defun nonincremental-search-backward (string)
182 "Read a string and search backward for it nonincrementally."
183 (interactive "sSearch for string: ")
184 (if (equal string "")
185 (search-backward (car search-ring))
186 (isearch-update-ring string nil)
187 (search-backward string)))
189 (defun nonincremental-re-search-forward (string)
190 "Read a regular expression and search for it nonincrementally."
191 (interactive "sSearch for regexp: ")
192 (if (equal string "")
193 (re-search-forward (car regexp-search-ring))
194 (isearch-update-ring string t)
195 (re-search-forward string)))
197 (defun nonincremental-re-search-backward (string)
198 "Read a regular expression and search backward for it nonincrementally."
199 (interactive "sSearch for regexp: ")
200 (if (equal string "")
201 (re-search-backward (car regexp-search-ring))
202 (isearch-update-ring string t)
203 (re-search-backward string)))
205 (defun nonincremental-repeat-search-forward ()
206 "Search forward for the previous search string."
207 (interactive)
208 (if (null search-ring)
209 (error "No previous search"))
210 (search-forward (car search-ring)))
212 (defun nonincremental-repeat-search-backward ()
213 "Search backward for the previous search string."
214 (interactive)
215 (if (null search-ring)
216 (error "No previous search"))
217 (search-backward (car search-ring)))
219 (defun nonincremental-repeat-re-search-forward ()
220 "Search forward for the previous regular expression."
221 (interactive)
222 (if (null regexp-search-ring)
223 (error "No previous search"))
224 (re-search-forward (car regexp-search-ring)))
226 (defun nonincremental-repeat-re-search-backward ()
227 "Search backward for the previous regular expression."
228 (interactive)
229 (if (null regexp-search-ring)
230 (error "No previous search"))
231 (re-search-backward (car regexp-search-ring)))
233 (define-key menu-bar-search-menu [query-replace-regexp]
234 '("Query Replace Regexp..." . query-replace-regexp))
235 (define-key menu-bar-search-menu [query-replace]
236 '("Query Replace..." . query-replace))
237 (define-key menu-bar-search-menu [find-tag]
238 '("Find Tag..." . find-tag))
239 (define-key menu-bar-search-menu [bookmark]
240 '("Bookmarks" . menu-bar-bookmark-map))
242 (define-key menu-bar-search-menu [separator-search]
243 '("--"))
245 (define-key menu-bar-search-menu [repeat-regexp-back]
246 '("Repeat Regexp Backwards" . nonincremental-repeat-re-search-backward))
247 (define-key menu-bar-search-menu [repeat-search-back]
248 '("Repeat Backwards" . nonincremental-repeat-search-backward))
249 (define-key menu-bar-search-menu [repeat-regexp-fwd]
250 '("Repeat Regexp" . nonincremental-repeat-re-search-forward))
251 (define-key menu-bar-search-menu [repeat-search-fwd]
252 '("Repeat Search" . nonincremental-repeat-search-forward))
254 (define-key menu-bar-search-menu [separator-repeat]
255 '("--"))
257 (define-key menu-bar-search-menu [re-search-backward]
258 '("Regexp Search Backwards..." . nonincremental-re-search-backward))
259 (define-key menu-bar-search-menu [search-backward]
260 '("Search Backwards..." . nonincremental-search-backward))
261 (define-key menu-bar-search-menu [re-search-forward]
262 '("Regexp Search..." . nonincremental-re-search-forward))
263 (define-key menu-bar-search-menu [search-forward]
264 '("Search..." . nonincremental-search-forward))
266 (define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map))
267 (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
268 (define-key menu-bar-edit-menu [props] '("Text Properties" . facemenu-menu))
270 (define-key menu-bar-edit-menu [separator-edit]
271 '("--"))
273 (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
275 (defvar yank-menu (cons "Select Yank" nil))
276 (fset 'yank-menu (cons 'keymap yank-menu))
277 (define-key menu-bar-edit-menu [select-paste] '("Select and Paste" . yank-menu))
278 (define-key menu-bar-edit-menu [paste] '("Paste" . yank))
279 (define-key menu-bar-edit-menu [copy] '("Copy" . menu-bar-kill-ring-save))
280 (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
281 (define-key menu-bar-edit-menu [undo] '("Undo" . undo))
283 (defun menu-bar-kill-ring-save (beg end)
284 (interactive "r")
285 (if (mouse-region-match)
286 (message "Selecting a region with the mouse does `copy' automatically")
287 (kill-ring-save beg end)))
289 (put 'fill-region 'menu-enable '(and mark-active (not buffer-read-only)))
290 (put 'kill-region 'menu-enable '(and mark-active (not buffer-read-only)))
291 (put 'menu-bar-kill-ring-save 'menu-enable 'mark-active)
292 (put 'yank 'menu-enable '(and (x-selection-exists-p) (not buffer-read-only)))
293 (put 'yank-menu 'menu-enable '(and (cdr yank-menu) (not buffer-read-only)))
294 (put 'delete-region 'menu-enable '(and mark-active
295 (not buffer-read-only)
296 (not (mouse-region-match))))
297 (put 'undo 'menu-enable '(and (not buffer-read-only)
298 (if (eq last-command 'undo)
299 pending-undo-list
300 (consp buffer-undo-list))))
301 (put 'query-replace 'menu-enable '(not buffer-read-only))
302 (put 'query-replace-regexp 'menu-enable '(not buffer-read-only))
304 (autoload 'ispell-menu-map "ispell" nil t 'keymap)
306 ;; These are alternative definitions for the cut, paste and copy
307 ;; menu items. Use them if your system expects these to use the clipboard.
309 (put 'clipboard-kill-region 'menu-enable 'mark-active)
310 (put 'clipboard-kill-ring-save 'menu-enable 'mark-active)
311 (put 'clipboard-yank 'menu-enable
312 '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD)))
314 (defun clipboard-yank ()
315 "Insert the clipboard contents, or the last stretch of killed text."
316 (interactive)
317 (let ((x-select-enable-clipboard t))
318 (yank)))
320 (defun clipboard-kill-ring-save (beg end)
321 "Copy region to kill ring, and save in the X clipboard."
322 (interactive "r")
323 (let ((x-select-enable-clipboard t))
324 (kill-ring-save beg end)))
326 (defun clipboard-kill-region (beg end)
327 "Kill the region, and save it in the X clipboard."
328 (interactive "r")
329 (let ((x-select-enable-clipboard t))
330 (kill-region beg end)))
332 (defun menu-bar-enable-clipboard ()
333 "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard.
334 Do the same for the keys of the same name."
335 (interactive)
336 ;; We can't use constant list structure here because it becomes pure,
337 ;; and because it gets modified with cache data.
338 (define-key menu-bar-edit-menu [paste]
339 (cons "Paste" 'clipboard-yank))
340 (define-key menu-bar-edit-menu [copy]
341 (cons "Copy" 'clipboard-kill-ring-save))
342 (define-key menu-bar-edit-menu [cut]
343 (cons "Cut" 'clipboard-kill-region))
345 (define-key global-map [f20] 'clipboard-kill-region)
346 (define-key global-map [f16] 'clipboard-kill-ring-save)
347 (define-key global-map [f18] 'clipboard-yank)
348 ;; X11R6 versions
349 (define-key global-map [cut] 'clipboard-kill-region)
350 (define-key global-map [copy] 'clipboard-kill-ring-save)
351 (define-key global-map [paste] 'clipboard-yank))
354 ;;; Menu support
356 (defvar menu-bar-custom-menu (make-sparse-keymap "Customize"))
358 (define-key menu-bar-custom-menu [customize-apropos-groups]
359 '("Apropos Groups..." . customize-apropos-groups))
360 (define-key menu-bar-custom-menu [customize-apropos-faces]
361 '("Apropos Faces..." . customize-apropos-faces))
362 (define-key menu-bar-custom-menu [customize-apropos-options]
363 '("Apropos Options..." . customize-apropos-options))
364 (define-key menu-bar-custom-menu [customize-apropos]
365 '("Apropos..." . customize-apropos))
366 (define-key menu-bar-custom-menu [separator-2]
367 '("--"))
368 (define-key menu-bar-custom-menu [customize-group]
369 '("Specific Group..." . customize-group))
370 (define-key menu-bar-custom-menu [customize-face]
371 '("Specific Face..." . customize-face))
372 (define-key menu-bar-custom-menu [customize-option]
373 '("Specific Option..." . customize-option))
374 (define-key menu-bar-custom-menu [customize-changed-options]
375 '("Changed Options..." . customize-changed-options))
376 (define-key menu-bar-custom-menu [separator-3]
377 '("--"))
378 (define-key menu-bar-custom-menu [customize-browse]
379 '("Browse Customization Groups" . customize-browse))
380 (define-key menu-bar-custom-menu [customize]
381 '("Top-level Customization Group" . customize))
383 ;; Options menu
384 (defvar menu-bar-options-menu (make-sparse-keymap "Global Options"))
386 (defmacro menu-bar-make-toggle (name variable doc message &rest body)
387 `(progn
388 (defun ,name ()
389 ,(concat doc ".")
390 (interactive)
391 (if ,(if body `(progn . ,body)
392 `(setq ,variable (not ,variable)))
393 (message ,message "enabled")
394 (message ,message "disabled")))
395 '(menu-item ,doc ,name .
396 (:button (:toggle . (and (boundp ',variable) ,variable))))))
398 (define-key menu-bar-options-menu [debug-on-quit]
399 (menu-bar-make-toggle toggle-debug-on-quit debug-on-quit
400 "Debug on Quit" "Debug on Quit %s"))
401 (define-key menu-bar-options-menu [debug-on-error]
402 (menu-bar-make-toggle toggle-debug-on-error debug-on-error
403 "Debug on Error" "Debug on Error %s"))
404 (define-key menu-bar-options-menu [options-separator]
405 '("--"))
406 (define-key menu-bar-options-menu [save-place]
407 (menu-bar-make-toggle toggle-save-place-globally save-place
408 "Save Place in Files between Sessions"
409 "Saving place in files %s"
410 (require 'saveplace)
411 (setq-default save-place (not (default-value save-place)))))
412 (define-key menu-bar-options-menu [uniquify]
413 (menu-bar-make-toggle toggle-uniquify-buffer-names uniquify-buffer-name-style
414 "Use Directory Names in Buffer Names"
415 "Directory name in buffer names (uniquify) %s"
416 (require 'uniquify)
417 (setq uniquify-buffer-name-style
418 (if (not uniquify-buffer-name-style)
419 'forward))))
420 (define-key menu-bar-options-menu [case-fold-search]
421 (menu-bar-make-toggle toggle-case-fold-search case-fold-search
422 "Case folding in searches"
423 "Case folding in searches %s"))
424 (define-key menu-bar-options-menu [truncate-lines]
425 (menu-bar-make-toggle
426 toggle-truncate-lines truncate-lines
427 "Line Truncation" "Line Truncation %s"
428 (prog1 (setq truncate-lines (not truncate-lines))
429 (set-buffer-modified-p (buffer-modified-p)))))
430 (define-key menu-bar-options-menu [transient-mark-mode]
431 (menu-bar-make-toggle toggle-transient-mark-mode transient-mark-mode
432 "Transient Mark Mode (highlights region)"
433 "Transient Mark mode %s"))
434 (define-key menu-bar-options-menu [toggle-auto-compression]
435 '(menu-item "Automatic File De/compression"
436 auto-compression-mode .
437 (:button (:toggle . (rassq 'jka-compr-handler
438 file-name-handler-alist)))))
439 (define-key menu-bar-options-menu [auto-fill-mode]
440 '(menu-item "Auto Fill (word wrap) in Text modes"
441 toggle-text-mode-auto-fill .
442 (:button (:toggle . (member 'turn-on-auto-fill text-mode-hook)))))
443 (define-key menu-bar-options-menu [toggle-global-lazy-font-lock-mode]
444 (menu-bar-make-toggle toggle-global-lazy-font-lock-mode global-font-lock-mode
445 "Global Font Lock (highlights syntax)"
446 "Global Font Lock mode %s"
447 ;; Make sure a support mode is used;
448 ;; otherwise Font Lock will be too slow.
449 (require 'font-lock)
450 (if (not global-font-lock-mode)
451 (or font-lock-support-mode
452 (setq font-lock-support-mode 'lazy-lock-mode)))
453 (global-font-lock-mode)))
455 (defvar menu-bar-describe-menu (make-sparse-keymap "Describe"))
457 (define-key menu-bar-describe-menu [describe-variable]
458 '("Describe Variable..." . describe-variable))
459 (define-key menu-bar-describe-menu [describe-function]
460 '("Describe Function..." . describe-function))
461 (define-key menu-bar-describe-menu [describe-key]
462 '("Describe Key..." . describe-key))
463 (define-key menu-bar-describe-menu [list-keybindings]
464 '("List Key Bindings" . describe-bindings))
465 (define-key menu-bar-describe-menu [apropos-variables]
466 '("Apropos Variables..." . apropos-variable))
467 (define-key menu-bar-describe-menu [apropos-commands]
468 '("Apropos Commands..." . apropos-command))
469 (define-key menu-bar-describe-menu [describe-mode]
470 '("Describe Buffer Modes" . describe-mode))
472 (defvar menu-bar-manuals-menu (make-sparse-keymap "Manuals"))
474 (define-key menu-bar-manuals-menu [man]
475 '("Read Man Page..." . manual-entry))
476 (define-key menu-bar-manuals-menu [sep2]
477 '("--"))
478 (define-key menu-bar-manuals-menu [key]
479 '("Find Key in Manual" . Info-goto-emacs-key-command-node))
480 (define-key menu-bar-manuals-menu [command]
481 '("Find Command in Manual" . Info-goto-emacs-command-node))
482 (define-key menu-bar-manuals-menu [info]
483 '("Browse Manuals with Info" . info))
484 (define-key menu-bar-manuals-menu [sep1]
485 '("--"))
486 (define-key menu-bar-manuals-menu [emacs-problems]
487 '("Emacs Known Problems" . view-emacs-problems))
488 (define-key menu-bar-manuals-menu [emacs-faq]
489 '("Emacs FAQ" . view-emacs-FAQ))
490 (define-key menu-bar-manuals-menu [emacs-news]
491 '("Emacs News" . view-emacs-news))
493 (define-key menu-bar-help-menu [describe-no-warranty]
494 '("(Non)Warranty" . describe-no-warranty))
495 (define-key menu-bar-help-menu [describe-copying]
496 '("Copying Conditions" . describe-copying))
497 (define-key menu-bar-help-menu [describe-distribution]
498 '("Getting New Versions" . describe-distribution))
499 (define-key menu-bar-help-menu [emacs-version]
500 '("Show Version" . emacs-version))
501 (define-key menu-bar-help-menu [report-emacs-bug]
502 '("Send Bug Report..." . report-emacs-bug))
503 (define-key menu-bar-help-menu [sep2]
504 '("--"))
505 (define-key menu-bar-help-menu [finder-by-keyword]
506 '("Find Emacs Packages..." . finder-by-keyword))
507 (define-key menu-bar-help-menu [describe]
508 (cons "Describe" menu-bar-describe-menu))
509 (define-key menu-bar-help-menu [manuals]
510 (cons "Manuals" menu-bar-manuals-menu))
511 (define-key menu-bar-help-menu [emacs-tutorial]
512 '("Emacs Tutorial" . help-with-tutorial))
513 (define-key menu-bar-help-menu [sep1]
514 '("--"))
515 (define-key menu-bar-help-menu [options]
516 (cons "Options" menu-bar-options-menu))
517 (define-key menu-bar-help-menu [customize]
518 (cons "Customize" menu-bar-custom-menu))
520 (defun kill-this-buffer () ; for the menubar
521 "Kills the current buffer."
522 (interactive)
523 (kill-buffer (current-buffer)))
525 (defun kill-this-buffer-enabled-p ()
526 (let ((count 0)
527 (buffers (buffer-list)))
528 (while buffers
529 (or (string-match "^ " (buffer-name (car buffers)))
530 (setq count (1+ count)))
531 (setq buffers (cdr buffers)))
532 (and (not (window-minibuffer-p (frame-selected-window menu-updating-frame)))
533 (> count 1))))
535 (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
537 (put 'save-buffer 'menu-enable
538 '(and (buffer-modified-p)
539 (not (window-minibuffer-p (frame-selected-window menu-updating-frame)))))
541 (put 'write-file 'menu-enable
542 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
544 (put 'find-file 'menu-enable
545 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
547 (put 'dired 'menu-enable
548 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
550 (put 'insert-file 'menu-enable
551 '(not (window-minibuffer-p (frame-selected-window menu-updating-frame))))
553 (put 'revert-buffer 'menu-enable
554 '(or revert-buffer-function revert-buffer-insert-file-contents-function
555 (and (buffer-file-name)
556 (or (buffer-modified-p)
557 (not (verify-visited-file-modtime (current-buffer)))))))
559 ;; Permit deleting frame if it would leave a visible or iconified frame.
560 (put 'delete-frame 'menu-enable
561 '(delete-frame-enabled-p))
563 (defun delete-frame-enabled-p ()
564 "Return non-nil if `delete-frame' should be enabled in the menu bar."
565 (let ((frames (frame-list))
566 (count 0))
567 (while frames
568 (if (frame-visible-p (car frames))
569 (setq count (1+ count)))
570 (setq frames (cdr frames)))
571 (> count 1)))
573 (put 'advertised-undo 'menu-enable
574 '(and (not (eq t buffer-undo-list))
575 (if (eq last-command 'undo)
576 (and (boundp 'pending-undo-list)
577 pending-undo-list)
578 buffer-undo-list)))
580 (defcustom yank-menu-length 20
581 "*Maximum length to display in the yank-menu."
582 :type 'integer
583 :group 'mouse)
585 (defun menu-bar-update-yank-menu (string old)
586 (let ((front (car (cdr yank-menu)))
587 (menu-string (if (<= (length string) yank-menu-length)
588 string
589 (concat
590 (substring string 0 (/ yank-menu-length 2))
591 "..."
592 (substring string (- (/ yank-menu-length 2)))))))
593 ;; Don't let the menu string be all dashes
594 ;; because that has a special meaning in a menu.
595 (if (string-match "\\`-+\\'" menu-string)
596 (setq menu-string (concat menu-string " ")))
597 ;; If we're supposed to be extending an existing string, and that
598 ;; string really is at the front of the menu, then update it in place.
599 (if (and old (or (eq old (car front))
600 (string= old (car front))))
601 (progn
602 (setcar front string)
603 (setcar (cdr front) menu-string))
604 (setcdr yank-menu
605 (cons
606 (cons string (cons menu-string 'menu-bar-select-yank))
607 (cdr yank-menu)))))
608 (if (> (length (cdr yank-menu)) kill-ring-max)
609 (setcdr (nthcdr kill-ring-max yank-menu) nil)))
611 (put 'menu-bar-select-yank 'apropos-inhibit t)
612 (defun menu-bar-select-yank ()
613 (interactive "*")
614 (push-mark (point))
615 (insert last-command-event))
617 ;; This definition is just to show what this looks like.
618 ;; It gets overridden below when menu-bar-update-buffers is called.
619 (define-key global-map [menu-bar buffer]
620 (cons "Buffers" (make-sparse-keymap "Buffers")))
622 (defvar list-buffers-directory nil)
624 (defvar menu-bar-update-buffers-maxbuf)
626 (defun menu-bar-select-buffer ()
627 (interactive)
628 (switch-to-buffer last-command-event))
630 (defun menu-bar-select-frame ()
631 (interactive)
632 (make-frame-visible last-command-event)
633 (raise-frame last-command-event)
634 (select-frame last-command-event))
636 (defun menu-bar-update-buffers-1 (elt)
637 (cons (format
638 (format "%%%ds %%s%%s %%s" menu-bar-update-buffers-maxbuf)
639 (cdr elt)
640 (if (buffer-modified-p (car elt))
641 "*" " ")
642 (save-excursion
643 (set-buffer (car elt))
644 (if buffer-read-only "%" " "))
645 (let ((file
646 (or (buffer-file-name (car elt))
647 (save-excursion
648 (set-buffer (car elt))
649 list-buffers-directory)
650 "")))
651 (setq file (or (file-name-directory file)
652 ""))
653 (if (> (length file) 20)
654 (setq file (concat "..." (substring file -17))))
655 file))
656 (car elt)))
658 (defvar menu-bar-buffers-menu-list-buffers-entry nil)
660 (defun menu-bar-update-buffers ()
661 ;; If user discards the Buffers item, play along.
662 (and (lookup-key (current-global-map) [menu-bar buffer])
663 (frame-or-buffer-changed-p)
664 (let ((buffers (buffer-list))
665 (frames (frame-list))
666 (maxlen 0)
667 buffers-menu frames-menu)
668 ;; If requested, list only the N most recently selected buffers.
669 (if (and (integerp buffers-menu-max-size)
670 (> buffers-menu-max-size 1))
671 (if (> (length buffers) buffers-menu-max-size)
672 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
674 ;; Make the menu of buffers proper.
675 (setq buffers-menu
676 (cons "Select Buffer"
677 (let* ((buffer-list
678 (mapcar 'list buffers))
679 tail
680 (menu-bar-update-buffers-maxbuf 0)
681 alist
682 head)
683 ;; Put into each element of buffer-list
684 ;; the name for actual display,
685 ;; perhaps truncated in the middle.
686 (setq tail buffer-list)
687 (while tail
688 (let ((name (buffer-name (car (car tail)))))
689 (setcdr (car tail)
690 (if (> (length name) 27)
691 (concat (substring name 0 12)
692 "..."
693 (substring name -12))
694 name)))
695 (setq tail (cdr tail)))
696 ;; Compute the maximum length of any name.
697 (setq tail buffer-list)
698 (while tail
699 (or (eq ?\ (aref (cdr (car tail)) 0))
700 (setq menu-bar-update-buffers-maxbuf
701 (max menu-bar-update-buffers-maxbuf
702 (length (cdr (car tail))))))
703 (setq tail (cdr tail)))
704 ;; Set ALIST to an alist of the form
705 ;; ITEM-STRING . BUFFER
706 (setq tail buffer-list)
707 (while tail
708 (let ((elt (car tail)))
709 (or (eq ?\ (aref (cdr elt) 0))
710 (setq alist (cons
711 (menu-bar-update-buffers-1 elt)
712 alist)))
713 (and alist (> (length (car (car alist))) maxlen)
714 (setq maxlen (length (car (car alist))))))
715 (setq tail (cdr tail)))
716 (setq alist (nreverse alist))
717 ;; Make the menu item for list-buffers
718 ;; or reuse the one we already have.
719 ;; The advantage in reusing one
720 ;; is that it already has the keyboard equivalent
721 ;; cached, so we save the time to look that up again.
722 (or menu-bar-buffers-menu-list-buffers-entry
723 (setq menu-bar-buffers-menu-list-buffers-entry
724 (cons
725 'list-buffers
726 (cons
728 'list-buffers))))
729 ;; Update the item string for menu's new width.
730 (setcar (cdr menu-bar-buffers-menu-list-buffers-entry)
731 (concat (make-string (max (- (/ maxlen 2) 8) 0)
732 ?\ )
733 "List All Buffers"))
734 ;; Now make the actual list of items,
735 ;; ending with the list-buffers item.
736 (nconc (mapcar '(lambda (pair)
737 ;; This is somewhat risque, to use
738 ;; the buffer name itself as the event
739 ;; type to define, but it works.
740 ;; It would not work to use the buffer
741 ;; since a buffer as an event has its
742 ;; own meaning.
743 (nconc (list (buffer-name (cdr pair))
744 (car pair)
745 (cons nil nil))
746 'menu-bar-select-buffer))
747 alist)
748 (list menu-bar-buffers-menu-list-buffers-entry)))))
751 ;; Make a Frames menu if we have more than one frame.
752 (if (cdr frames)
753 (let ((name (concat (make-string (max (- (/ maxlen 2) 3) 0)
754 ?\ )
755 "Frames"))
756 (frames-menu
757 (cons 'keymap
758 (cons "Select Frame"
759 (mapcar '(lambda (frame)
760 (nconc (list frame
761 (cdr (assq 'name
762 (frame-parameters frame)))
763 (cons nil nil))
764 'menu-bar-select-frame))
765 frames)))))
766 ;; Put it underneath the Buffers menu.
767 (setq buffers-menu (cons (cons 'frames (cons name frames-menu))
768 buffers-menu))))
769 (if buffers-menu
770 (setq buffers-menu (cons 'keymap buffers-menu)))
771 (define-key (current-global-map) [menu-bar buffer]
772 (cons "Buffers" buffers-menu)))))
774 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
776 (menu-bar-update-buffers)
778 ;; this version is too slow
779 ;;;(defun format-buffers-menu-line (buffer)
780 ;;; "Returns a string to represent the given buffer in the Buffer menu.
781 ;;;nil means the buffer shouldn't be listed. You can redefine this."
782 ;;; (if (string-match "\\` " (buffer-name buffer))
783 ;;; nil
784 ;;; (save-excursion
785 ;;; (set-buffer buffer)
786 ;;; (let ((size (buffer-size)))
787 ;;; (format "%s%s %-19s %6s %-15s %s"
788 ;;; (if (buffer-modified-p) "*" " ")
789 ;;; (if buffer-read-only "%" " ")
790 ;;; (buffer-name)
791 ;;; size
792 ;;; mode-name
793 ;;; (or (buffer-file-name) ""))))))
795 ;;; Set up a menu bar menu for the minibuffer.
797 (mapcar
798 (function
799 (lambda (map)
800 (define-key map [menu-bar minibuf]
801 (cons "Minibuf" (make-sparse-keymap "Minibuf")))))
802 (list minibuffer-local-ns-map
803 minibuffer-local-must-match-map
804 minibuffer-local-isearch-map
805 minibuffer-local-map
806 minibuffer-local-completion-map))
808 (mapcar
809 (function
810 (lambda (map)
811 (define-key map [menu-bar minibuf ?\?]
812 '("List Completions" . minibuffer-completion-help))
813 (define-key map [menu-bar minibuf space]
814 '("Complete Word" . minibuffer-complete-word))
815 (define-key map [menu-bar minibuf tab]
816 '("Complete" . minibuffer-complete))
818 (list minibuffer-local-must-match-map
819 minibuffer-local-completion-map))
821 (mapcar
822 (function
823 (lambda (map)
824 (define-key map [menu-bar minibuf quit]
825 '("Quit" . keyboard-escape-quit))
826 (define-key map [menu-bar minibuf return]
827 '("Enter" . exit-minibuffer))
829 (list minibuffer-local-ns-map
830 minibuffer-local-must-match-map
831 minibuffer-local-isearch-map
832 minibuffer-local-map
833 minibuffer-local-completion-map))
835 (defcustom menu-bar-mode nil
836 "Toggle display of a menu bar on each frame.
837 Setting this variable directly does not take effect;
838 use either \\[customize] or the function `menu-bar-mode'."
839 :set (lambda (symbol value)
840 (menu-bar-mode (or value 0)))
841 :initialize 'custom-initialize-default
842 :type 'boolean
843 :group 'frames)
845 (defun menu-bar-mode (flag)
846 "Toggle display of a menu bar on each frame.
847 This command applies to all frames that exist and frames to be
848 created in the future.
849 With a numeric argument, if the argument is positive,
850 turn on menu bars; otherwise, turn off menu bars."
851 (interactive "P")
853 ;; Make menu-bar-mode and default-frame-alist consistent.
854 (let ((default (assq 'menu-bar-lines default-frame-alist)))
855 (if default
856 (setq menu-bar-mode (not (eq (cdr default) 0)))
857 (setq default-frame-alist
858 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
859 default-frame-alist))))
861 ;; Toggle or set the mode, according to FLAG.
862 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
863 (> (prefix-numeric-value flag) 0)))
865 ;; Apply it to default-frame-alist.
866 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
867 (if (consp parameter)
868 (setcdr parameter (if menu-bar-mode 1 0))
869 (setq default-frame-alist
870 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
871 default-frame-alist))))
873 ;; Apply it to existing frames.
874 (let ((frames (frame-list)))
875 (while frames
876 (let ((height (cdr (assq 'height (frame-parameters (car frames))))))
877 (modify-frame-parameters (car frames)
878 (list (cons 'menu-bar-lines
879 (if menu-bar-mode 1 0))))
880 (modify-frame-parameters (car frames)
881 (list (cons 'height height))))
882 (setq frames (cdr frames)))))
884 (provide 'menu-bar)
886 ;;; menu-bar.el ends here