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