* admin/gitmerge.el (gitmerge-missing):
[emacs.git] / lisp / menu-bar.el
blob2b38cb5f2b0c11cd26246255248afe16073ce2db
1 ;;; menu-bar.el --- define a default menu bar
3 ;; Copyright (C) 1993-1995, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Richard M. Stallman
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal, mouse
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; Avishai Yacobi suggested some menu rearrangements.
27 ;;; Commentary:
29 ;;; Code:
31 ;; This is referenced by some code below; it is defined in uniquify.el
32 (defvar uniquify-buffer-name-style)
34 ;; From emulation/cua-base.el; used below
35 (defvar cua-enable-cua-keys)
38 ;; Don't clobber an existing menu-bar keymap, to preserve any menu-bar key
39 ;; definitions made in loaddefs.el.
40 (or (lookup-key global-map [menu-bar])
41 (define-key global-map [menu-bar] (make-sparse-keymap "menu-bar")))
43 ;; Force Help item to come last, after the major mode's own items.
44 ;; The symbol used to be called `help', but that gets confused with the
45 ;; help key.
46 (setq menu-bar-final-items '(help-menu))
48 ;; This definition is just to show what this looks like.
49 ;; It gets modified in place when menu-bar-update-buffers is called.
50 (defvar global-buffers-menu-map (make-sparse-keymap "Buffers"))
52 ;; Only declared obsolete (and only made a proper alias) in 23.3.
53 (define-obsolete-variable-alias
54 'menu-bar-files-menu 'menu-bar-file-menu "22.1")
55 (defvar menu-bar-file-menu
56 (let ((menu (make-sparse-keymap "File")))
58 ;; The "File" menu items
59 (bindings--define-key menu [exit-emacs]
60 '(menu-item "Quit" save-buffers-kill-terminal
61 :help "Save unsaved buffers, then exit"))
63 (bindings--define-key menu [separator-exit]
64 menu-bar-separator)
66 ;; Don't use delete-frame as event name because that is a special
67 ;; event.
68 (bindings--define-key menu [delete-this-frame]
69 '(menu-item "Delete Frame" delete-frame
70 :visible (fboundp 'delete-frame)
71 :enable (delete-frame-enabled-p)
72 :help "Delete currently selected frame"))
73 (bindings--define-key menu [make-frame-on-display]
74 '(menu-item "New Frame on Display..." make-frame-on-display
75 :visible (fboundp 'make-frame-on-display)
76 :help "Open a new frame on another display"))
77 (bindings--define-key menu [make-frame]
78 '(menu-item "New Frame" make-frame-command
79 :visible (fboundp 'make-frame-command)
80 :help "Open a new frame"))
82 (bindings--define-key menu [separator-frame]
83 menu-bar-separator)
85 (bindings--define-key menu [one-window]
86 '(menu-item "Remove Other Windows" delete-other-windows
87 :enable (not (one-window-p t nil))
88 :help "Make selected window fill whole frame"))
90 (bindings--define-key menu [new-window-on-right]
91 '(menu-item "New Window on Right" split-window-right
92 :enable (and (menu-bar-menu-frame-live-and-visible-p)
93 (menu-bar-non-minibuffer-window-p))
94 :help "Make new window on right of selected one"))
96 (bindings--define-key menu [new-window-below]
97 '(menu-item "New Window Below" split-window-below
98 :enable (and (menu-bar-menu-frame-live-and-visible-p)
99 (menu-bar-non-minibuffer-window-p))
100 :help "Make new window below selected one"))
102 (bindings--define-key menu [separator-window]
103 menu-bar-separator)
105 (bindings--define-key menu [ps-print-region]
106 '(menu-item "PostScript Print Region (B+W)" ps-print-region
107 :enable mark-active
108 :help "Pretty-print marked region in black and white to PostScript printer"))
109 (bindings--define-key menu [ps-print-buffer]
110 '(menu-item "PostScript Print Buffer (B+W)" ps-print-buffer
111 :enable (menu-bar-menu-frame-live-and-visible-p)
112 :help "Pretty-print current buffer in black and white to PostScript printer"))
113 (bindings--define-key menu [ps-print-region-faces]
114 '(menu-item "PostScript Print Region"
115 ps-print-region-with-faces
116 :enable mark-active
117 :help "Pretty-print marked region to PostScript printer"))
118 (bindings--define-key menu [ps-print-buffer-faces]
119 '(menu-item "PostScript Print Buffer"
120 ps-print-buffer-with-faces
121 :enable (menu-bar-menu-frame-live-and-visible-p)
122 :help "Pretty-print current buffer to PostScript printer"))
123 (bindings--define-key menu [print-region]
124 '(menu-item "Print Region" print-region
125 :enable mark-active
126 :help "Print region between mark and current position"))
127 (bindings--define-key menu [print-buffer]
128 '(menu-item "Print Buffer" print-buffer
129 :enable (menu-bar-menu-frame-live-and-visible-p)
130 :help "Print current buffer with page headings"))
132 (bindings--define-key menu [separator-print]
133 menu-bar-separator)
135 (bindings--define-key menu [recover-session]
136 '(menu-item "Recover Crashed Session" recover-session
137 :enable
138 (and auto-save-list-file-prefix
139 (file-directory-p
140 (file-name-directory auto-save-list-file-prefix))
141 (directory-files
142 (file-name-directory auto-save-list-file-prefix)
144 (concat "\\`"
145 (regexp-quote
146 (file-name-nondirectory
147 auto-save-list-file-prefix)))
149 :help "Recover edits from a crashed session"))
150 (bindings--define-key menu [revert-buffer]
151 '(menu-item "Revert Buffer" revert-buffer
152 :enable (or (not (eq revert-buffer-function
153 'revert-buffer--default))
154 (not (eq
155 revert-buffer-insert-file-contents-function
156 'revert-buffer-insert-file-contents--default-function))
157 (and buffer-file-number
158 (or (buffer-modified-p)
159 (not (verify-visited-file-modtime
160 (current-buffer))))))
161 :help "Re-read current buffer from its file"))
162 (bindings--define-key menu [write-file]
163 '(menu-item "Save As..." write-file
164 :enable (and (menu-bar-menu-frame-live-and-visible-p)
165 (menu-bar-non-minibuffer-window-p))
166 :help "Write current buffer to another file"))
167 (bindings--define-key menu [save-buffer]
168 '(menu-item "Save" save-buffer
169 :enable (and (buffer-modified-p)
170 (buffer-file-name)
171 (menu-bar-non-minibuffer-window-p))
172 :help "Save current buffer to its file"))
174 (bindings--define-key menu [separator-save]
175 menu-bar-separator)
178 (bindings--define-key menu [kill-buffer]
179 '(menu-item "Close" kill-this-buffer
180 :enable (kill-this-buffer-enabled-p)
181 :help "Discard (kill) current buffer"))
182 (bindings--define-key menu [insert-file]
183 '(menu-item "Insert File..." insert-file
184 :enable (menu-bar-non-minibuffer-window-p)
185 :help "Insert another file into current buffer"))
186 (bindings--define-key menu [dired]
187 '(menu-item "Open Directory..." dired
188 :enable (menu-bar-non-minibuffer-window-p)
189 :help "Read a directory, to operate on its files"))
190 (bindings--define-key menu [open-file]
191 '(menu-item "Open File..." menu-find-file-existing
192 :enable (menu-bar-non-minibuffer-window-p)
193 :help "Read an existing file into an Emacs buffer"))
194 (bindings--define-key menu [new-file]
195 '(menu-item "Visit New File..." find-file
196 :enable (menu-bar-non-minibuffer-window-p)
197 :help "Specify a new file's name, to edit the file"))
199 menu))
201 (defun menu-find-file-existing ()
202 "Edit the existing file FILENAME."
203 (interactive)
204 (let* ((mustmatch (not (and (fboundp 'x-uses-old-gtk-dialog)
205 (x-uses-old-gtk-dialog))))
206 (filename (car (find-file-read-args "Find file: " mustmatch))))
207 (if mustmatch
208 (find-file-existing filename)
209 (find-file filename))))
211 ;; The "Edit->Search" submenu
212 (defvar menu-bar-last-search-type nil
213 "Type of last non-incremental search command called from the menu.")
215 (defun nonincremental-repeat-search-forward ()
216 "Search forward for the previous search string or regexp."
217 (interactive)
218 (cond
219 ((and (eq menu-bar-last-search-type 'string)
220 search-ring)
221 (nonincremental-search-forward))
222 ((and (eq menu-bar-last-search-type 'regexp)
223 regexp-search-ring)
224 (re-search-forward (car regexp-search-ring)))
226 (error "No previous search"))))
228 (defun nonincremental-repeat-search-backward ()
229 "Search backward for the previous search string or regexp."
230 (interactive)
231 (cond
232 ((and (eq menu-bar-last-search-type 'string)
233 search-ring)
234 (nonincremental-search-backward))
235 ((and (eq menu-bar-last-search-type 'regexp)
236 regexp-search-ring)
237 (re-search-backward (car regexp-search-ring)))
239 (error "No previous search"))))
241 (defun nonincremental-search-forward (&optional string backward)
242 "Read a string and search for it nonincrementally."
243 (interactive "sSearch for string: ")
244 (setq menu-bar-last-search-type 'string)
245 ;; Ideally, this whole command would be equivalent to `C-s RET'.
246 (let ((isearch-forward (not backward))
247 (isearch-regexp-function search-default-mode)
248 (isearch-regexp nil))
249 (if (or (equal string "") (not string))
250 (funcall (isearch-search-fun-default) (car search-ring))
251 (isearch-update-ring string nil)
252 (funcall (isearch-search-fun-default) string))))
254 (defun nonincremental-search-backward (&optional string)
255 "Read a string and search backward for it nonincrementally."
256 (interactive "sSearch backwards for string: ")
257 (nonincremental-search-forward string 'backward))
259 (defun nonincremental-re-search-forward (string)
260 "Read a regular expression and search for it nonincrementally."
261 (interactive "sSearch for regexp: ")
262 (setq menu-bar-last-search-type 'regexp)
263 (if (equal string "")
264 (re-search-forward (car regexp-search-ring))
265 (isearch-update-ring string t)
266 (re-search-forward string)))
268 (defun nonincremental-re-search-backward (string)
269 "Read a regular expression and search backward for it nonincrementally."
270 (interactive "sSearch for regexp: ")
271 (setq menu-bar-last-search-type 'regexp)
272 (if (equal string "")
273 (re-search-backward (car regexp-search-ring))
274 (isearch-update-ring string t)
275 (re-search-backward string)))
277 ;; The Edit->Search->Incremental Search menu
278 (defvar menu-bar-i-search-menu
279 (let ((menu (make-sparse-keymap "Incremental Search")))
280 (bindings--define-key menu [isearch-backward-regexp]
281 '(menu-item "Backward Regexp..." isearch-backward-regexp
282 :help "Search backwards for a regular expression as you type it"))
283 (bindings--define-key menu [isearch-forward-regexp]
284 '(menu-item "Forward Regexp..." isearch-forward-regexp
285 :help "Search forward for a regular expression as you type it"))
286 (bindings--define-key menu [isearch-backward]
287 '(menu-item "Backward String..." isearch-backward
288 :help "Search backwards for a string as you type it"))
289 (bindings--define-key menu [isearch-forward]
290 '(menu-item "Forward String..." isearch-forward
291 :help "Search forward for a string as you type it"))
292 menu))
294 (defvar menu-bar-search-menu
295 (let ((menu (make-sparse-keymap "Search")))
297 (bindings--define-key menu [i-search]
298 `(menu-item "Incremental Search" ,menu-bar-i-search-menu))
299 (bindings--define-key menu [separator-tag-isearch]
300 menu-bar-separator)
302 (bindings--define-key menu [tags-continue]
303 '(menu-item "Continue Tags Search" tags-loop-continue
304 :help "Continue last tags search operation"))
305 (bindings--define-key menu [tags-srch]
306 '(menu-item "Search Tagged Files..." tags-search
307 :help "Search for a regexp in all tagged files"))
308 (bindings--define-key menu [separator-tag-search] menu-bar-separator)
310 (bindings--define-key menu [repeat-search-back]
311 '(menu-item "Repeat Backwards"
312 nonincremental-repeat-search-backward
313 :enable (or (and (eq menu-bar-last-search-type 'string)
314 search-ring)
315 (and (eq menu-bar-last-search-type 'regexp)
316 regexp-search-ring))
317 :help "Repeat last search backwards"))
318 (bindings--define-key menu [repeat-search-fwd]
319 '(menu-item "Repeat Forward"
320 nonincremental-repeat-search-forward
321 :enable (or (and (eq menu-bar-last-search-type 'string)
322 search-ring)
323 (and (eq menu-bar-last-search-type 'regexp)
324 regexp-search-ring))
325 :help "Repeat last search forward"))
326 (bindings--define-key menu [separator-repeat-search]
327 menu-bar-separator)
329 (bindings--define-key menu [re-search-backward]
330 '(menu-item "Regexp Backwards..."
331 nonincremental-re-search-backward
332 :help "Search backwards for a regular expression"))
333 (bindings--define-key menu [re-search-forward]
334 '(menu-item "Regexp Forward..."
335 nonincremental-re-search-forward
336 :help "Search forward for a regular expression"))
338 (bindings--define-key menu [search-backward]
339 '(menu-item "String Backwards..."
340 nonincremental-search-backward
341 :help "Search backwards for a string"))
342 (bindings--define-key menu [search-forward]
343 '(menu-item "String Forward..." nonincremental-search-forward
344 :help "Search forward for a string"))
345 menu))
347 ;; The Edit->Replace submenu
349 (defvar menu-bar-replace-menu
350 (let ((menu (make-sparse-keymap "Replace")))
351 (bindings--define-key menu [tags-repl-continue]
352 '(menu-item "Continue Replace" tags-loop-continue
353 :help "Continue last tags replace operation"))
354 (bindings--define-key menu [tags-repl]
355 '(menu-item "Replace in Tagged Files..." tags-query-replace
356 :help "Interactively replace a regexp in all tagged files"))
357 (bindings--define-key menu [separator-replace-tags]
358 menu-bar-separator)
360 (bindings--define-key menu [query-replace-regexp]
361 '(menu-item "Replace Regexp..." query-replace-regexp
362 :enable (not buffer-read-only)
363 :help "Replace regular expression interactively, ask about each occurrence"))
364 (bindings--define-key menu [query-replace]
365 '(menu-item "Replace String..." query-replace
366 :enable (not buffer-read-only)
367 :help "Replace string interactively, ask about each occurrence"))
368 menu))
370 ;;; Assemble the top-level Edit menu items.
371 (defvar menu-bar-goto-menu
372 (let ((menu (make-sparse-keymap "Go To")))
374 (bindings--define-key menu [set-tags-name]
375 '(menu-item "Set Tags File Name..." visit-tags-table
376 :visible (menu-bar-goto-uses-etags-p)
377 :help "Tell navigation commands which tag table file to use"))
379 (bindings--define-key menu [separator-tag-file]
380 '(menu-item "--" nil :visible (menu-bar-goto-uses-etags-p)))
382 (bindings--define-key menu [xref-pop]
383 '(menu-item "Back" xref-pop-marker-stack
384 :visible (and (featurep 'xref)
385 (not (xref-marker-stack-empty-p)))
386 :help "Back to the position of the last search"))
388 (bindings--define-key menu [xref-apropos]
389 '(menu-item "Find Apropos..." xref-find-apropos
390 :help "Find function/variables whose names match regexp"))
392 (bindings--define-key menu [xref-find-otherw]
393 '(menu-item "Find Definition in Other Window..."
394 xref-find-definitions-other-window
395 :help "Find function/variable definition in another window"))
396 (bindings--define-key menu [xref-find-def]
397 '(menu-item "Find Definition..." xref-find-definitions
398 :help "Find definition of function or variable"))
400 (bindings--define-key menu [separator-xref]
401 menu-bar-separator)
403 (bindings--define-key menu [end-of-buf]
404 '(menu-item "Goto End of Buffer" end-of-buffer))
405 (bindings--define-key menu [beg-of-buf]
406 '(menu-item "Goto Beginning of Buffer" beginning-of-buffer))
407 (bindings--define-key menu [go-to-pos]
408 '(menu-item "Goto Buffer Position..." goto-char
409 :help "Read a number N and go to buffer position N"))
410 (bindings--define-key menu [go-to-line]
411 '(menu-item "Goto Line..." goto-line
412 :help "Read a line number and go to that line"))
413 menu))
415 (defun menu-bar-goto-uses-etags-p ()
416 (or (not (boundp 'xref-backend-functions))
417 (eq (car xref-backend-functions) 'etags--xref-backend)))
419 (defvar yank-menu (cons (purecopy "Select Yank") nil))
420 (fset 'yank-menu (cons 'keymap yank-menu))
422 (defvar menu-bar-edit-menu
423 (let ((menu (make-sparse-keymap "Edit")))
425 (bindings--define-key menu [props]
426 `(menu-item "Text Properties" facemenu-menu))
428 ;; ns-win.el said: Add spell for platform consistency.
429 (if (featurep 'ns)
430 (bindings--define-key menu [spell]
431 `(menu-item "Spell" ispell-menu-map)))
433 (bindings--define-key menu [fill]
434 `(menu-item "Fill" fill-region
435 :enable (and mark-active (not buffer-read-only))
436 :help
437 "Fill text in region to fit between left and right margin"))
439 (bindings--define-key menu [separator-bookmark]
440 menu-bar-separator)
442 (bindings--define-key menu [bookmark]
443 `(menu-item "Bookmarks" menu-bar-bookmark-map))
445 (bindings--define-key menu [goto]
446 `(menu-item "Go To" ,menu-bar-goto-menu))
448 (bindings--define-key menu [replace]
449 `(menu-item "Replace" ,menu-bar-replace-menu))
451 (bindings--define-key menu [search]
452 `(menu-item "Search" ,menu-bar-search-menu))
454 (bindings--define-key menu [separator-search]
455 menu-bar-separator)
457 (bindings--define-key menu [mark-whole-buffer]
458 '(menu-item "Select All" mark-whole-buffer
459 :help "Mark the whole buffer for a subsequent cut/copy"))
460 (bindings--define-key menu [clear]
461 '(menu-item "Clear" delete-region
462 :enable (and mark-active
463 (not buffer-read-only))
464 :help
465 "Delete the text in region between mark and current position"))
468 (bindings--define-key menu (if (featurep 'ns) [select-paste]
469 [paste-from-menu])
470 ;; ns-win.el said: Change text to be more consistent with
471 ;; surrounding menu items `paste', etc."
472 `(menu-item ,(if (featurep 'ns) "Select and Paste" "Paste from Kill Menu")
473 yank-menu
474 :enable (and (cdr yank-menu) (not buffer-read-only))
475 :help "Choose a string from the kill ring and paste it"))
476 (bindings--define-key menu [paste]
477 `(menu-item "Paste" yank
478 :enable (funcall
479 ',(lambda ()
480 (and (or
481 (gui-backend-selection-exists-p 'CLIPBOARD)
482 (if (featurep 'ns) ; like paste-from-menu
483 (cdr yank-menu)
484 kill-ring))
485 (not buffer-read-only))))
486 :help "Paste (yank) text most recently cut/copied"))
487 (bindings--define-key menu [copy]
488 ;; ns-win.el said: Substitute a Copy function that works better
489 ;; under X (for GNUstep).
490 `(menu-item "Copy" ,(if (featurep 'ns)
491 'ns-copy-including-secondary
492 'kill-ring-save)
493 :enable mark-active
494 :help "Copy text in region between mark and current position"
495 :keys ,(if (featurep 'ns)
496 "\\[ns-copy-including-secondary]"
497 "\\[kill-ring-save]")))
498 (bindings--define-key menu [cut]
499 '(menu-item "Cut" kill-region
500 :enable (and mark-active (not buffer-read-only))
501 :help
502 "Cut (kill) text in region between mark and current position"))
503 ;; ns-win.el said: Separate undo from cut/paste section.
504 (if (featurep 'ns)
505 (bindings--define-key menu [separator-undo] menu-bar-separator))
507 (bindings--define-key menu [undo]
508 '(menu-item "Undo" undo
509 :enable (and (not buffer-read-only)
510 (not (eq t buffer-undo-list))
511 (if (eq last-command 'undo)
512 (listp pending-undo-list)
513 (consp buffer-undo-list)))
514 :help "Undo last operation"))
516 menu))
518 (define-obsolete-function-alias
519 'menu-bar-kill-ring-save 'kill-ring-save "24.1")
521 ;; These are alternative definitions for the cut, paste and copy
522 ;; menu items. Use them if your system expects these to use the clipboard.
524 (put 'clipboard-kill-region 'menu-enable
525 '(and mark-active (not buffer-read-only)))
526 (put 'clipboard-kill-ring-save 'menu-enable 'mark-active)
527 (put 'clipboard-yank 'menu-enable
528 `(funcall ',(lambda ()
529 (and (or (gui-backend-selection-exists-p 'PRIMARY)
530 (gui-backend-selection-exists-p 'CLIPBOARD))
531 (not buffer-read-only)))))
533 (defun clipboard-yank ()
534 "Insert the clipboard contents, or the last stretch of killed text."
535 (interactive "*")
536 (let ((select-enable-clipboard t))
537 (yank)))
539 (defun clipboard-kill-ring-save (beg end &optional region)
540 "Copy region to kill ring, and save in the GUI's clipboard.
541 If the optional argument REGION is non-nil, the function ignores
542 BEG and END, and saves the current region instead."
543 (interactive "r\np")
544 (let ((select-enable-clipboard t))
545 (kill-ring-save beg end region)))
547 (defun clipboard-kill-region (beg end &optional region)
548 "Kill the region, and save it in the GUI's clipboard.
549 If the optional argument REGION is non-nil, the function ignores
550 BEG and END, and kills the current region instead."
551 (interactive "r\np")
552 (let ((select-enable-clipboard t))
553 (kill-region beg end region)))
555 (defun menu-bar-enable-clipboard ()
556 "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard.
557 Do the same for the keys of the same name."
558 (interactive)
559 ;; These are Sun server keysyms for the Cut, Copy and Paste keys
560 ;; (also for XFree86 on Sun keyboard):
561 (define-key global-map [f20] 'clipboard-kill-region)
562 (define-key global-map [f16] 'clipboard-kill-ring-save)
563 (define-key global-map [f18] 'clipboard-yank)
564 ;; X11R6 versions:
565 (define-key global-map [cut] 'clipboard-kill-region)
566 (define-key global-map [copy] 'clipboard-kill-ring-save)
567 (define-key global-map [paste] 'clipboard-yank))
569 ;; The "Options" menu items
571 (defvar menu-bar-custom-menu
572 (let ((menu (make-sparse-keymap "Customize")))
574 (bindings--define-key menu [customize-apropos-faces]
575 '(menu-item "Faces Matching..." customize-apropos-faces
576 :help "Browse faces matching a regexp or word list"))
577 (bindings--define-key menu [customize-apropos-options]
578 '(menu-item "Options Matching..." customize-apropos-options
579 :help "Browse options matching a regexp or word list"))
580 (bindings--define-key menu [customize-apropos]
581 '(menu-item "All Settings Matching..." customize-apropos
582 :help "Browse customizable settings matching a regexp or word list"))
583 (bindings--define-key menu [separator-1]
584 menu-bar-separator)
585 (bindings--define-key menu [customize-group]
586 '(menu-item "Specific Group..." customize-group
587 :help "Customize settings of specific group"))
588 (bindings--define-key menu [customize-face]
589 '(menu-item "Specific Face..." customize-face
590 :help "Customize attributes of specific face"))
591 (bindings--define-key menu [customize-option]
592 '(menu-item "Specific Option..." customize-option
593 :help "Customize value of specific option"))
594 (bindings--define-key menu [separator-2]
595 menu-bar-separator)
596 (bindings--define-key menu [customize-changed-options]
597 '(menu-item "New Options..." customize-changed-options
598 :help "Options added or changed in recent Emacs versions"))
599 (bindings--define-key menu [customize-saved]
600 '(menu-item "Saved Options" customize-saved
601 :help "Customize previously saved options"))
602 (bindings--define-key menu [separator-3]
603 menu-bar-separator)
604 (bindings--define-key menu [customize-browse]
605 '(menu-item "Browse Customization Groups" customize-browse
606 :help "Browse all customization groups"))
607 (bindings--define-key menu [customize]
608 '(menu-item "Top-level Customization Group" customize
609 :help "The master group called `Emacs'"))
610 (bindings--define-key menu [customize-themes]
611 '(menu-item "Custom Themes" customize-themes
612 :help "Choose a pre-defined customization theme"))
613 menu))
614 ;(defvar menu-bar-preferences-menu (make-sparse-keymap "Preferences"))
616 (defmacro menu-bar-make-mm-toggle (fname doc help &optional props)
617 "Make a menu-item for a global minor mode toggle.
618 FNAME is the minor mode's name (variable and function).
619 DOC is the text to use for the menu entry.
620 HELP is the text to use for the tooltip.
621 PROPS are additional properties."
622 `'(menu-item ,doc ,fname
623 ,@props
624 :help ,help
625 :button (:toggle . (and (default-boundp ',fname)
626 (default-value ',fname)))))
628 (defmacro menu-bar-make-toggle (name variable doc message help &rest body)
629 `(progn
630 (defun ,name (&optional interactively)
631 ,(concat "Toggle whether to " (downcase (substring help 0 1))
632 (substring help 1) ".
633 In an interactive call, record this option as a candidate for saving
634 by \"Save Options\" in Custom buffers.")
635 (interactive "p")
636 (if ,(if body `(progn . ,body)
637 `(progn
638 (custom-load-symbol ',variable)
639 (let ((set (or (get ',variable 'custom-set) 'set-default))
640 (get (or (get ',variable 'custom-get) 'default-value)))
641 (funcall set ',variable (not (funcall get ',variable))))))
642 (message ,message "enabled globally")
643 (message ,message "disabled globally"))
644 ;; The function `customize-mark-as-set' must only be called when
645 ;; a variable is set interactively, as the purpose is to mark it as
646 ;; a candidate for "Save Options", and we do not want to save options
647 ;; the user have already set explicitly in his init file.
648 (if interactively (customize-mark-as-set ',variable)))
649 '(menu-item ,doc ,name
650 :help ,help
651 :button (:toggle . (and (default-boundp ',variable)
652 (default-value ',variable))))))
654 ;; Function for setting/saving default font.
656 (defun menu-set-font ()
657 "Interactively select a font and make it the default on all frames.
659 The selected font will be the default on both the existing and future frames."
660 (interactive)
661 (set-frame-font (if (fboundp 'x-select-font)
662 (x-select-font)
663 (mouse-select-font))
664 nil t))
666 (defun menu-bar-options-save ()
667 "Save current values of Options menu items using Custom."
668 (interactive)
669 (let ((need-save nil))
670 ;; These are set with menu-bar-make-mm-toggle, which does not
671 ;; put on a customized-value property.
672 (dolist (elt '(global-display-line-numbers-mode display-line-numbers-type
673 line-number-mode column-number-mode size-indication-mode
674 cua-mode show-paren-mode transient-mark-mode
675 blink-cursor-mode display-time-mode display-battery-mode
676 ;; These are set by other functions that don't set
677 ;; the customized state. Having them here has the
678 ;; side-effect that turning them off via X
679 ;; resources acts like having customized them, but
680 ;; that seems harmless.
681 menu-bar-mode tool-bar-mode))
682 ;; FIXME ? It's a little annoying that running this command
683 ;; always loads cua-base, paren, time, and battery, even if they
684 ;; have not been customized in any way. (Due to custom-load-symbol.)
685 (and (customize-mark-to-save elt)
686 (setq need-save t)))
687 ;; These are set with `customize-set-variable'.
688 (dolist (elt '(scroll-bar-mode
689 debug-on-quit debug-on-error
690 ;; Somehow this works, when tool-bar and menu-bar don't.
691 tooltip-mode window-divider-mode
692 save-place uniquify-buffer-name-style fringe-mode
693 indicate-empty-lines indicate-buffer-boundaries
694 case-fold-search font-use-system-font
695 current-language-environment default-input-method
696 ;; Saving `text-mode-hook' is somewhat questionable,
697 ;; as we might get more than we bargain for, if
698 ;; other code may has added hooks as well.
699 ;; Nonetheless, not saving it would like be confuse
700 ;; more often.
701 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2002-02-11.
702 text-mode-hook tool-bar-position))
703 (and (get elt 'customized-value)
704 (customize-mark-to-save elt)
705 (setq need-save t)))
706 (when (get 'default 'customized-face)
707 (put 'default 'saved-face (get 'default 'customized-face))
708 (put 'default 'customized-face nil)
709 (setq need-save t))
710 ;; Save if we changed anything.
711 (when need-save
712 (custom-save-all))))
715 ;;; Assemble all the top-level items of the "Options" menu
717 ;; The "Show/Hide" submenu of menu "Options"
719 (defun menu-bar-window-divider-customize ()
720 "Show customization buffer for `window-divider' group."
721 (interactive)
722 (customize-group 'window-divider))
724 (defun menu-bar-bottom-and-right-window-divider ()
725 "Display dividers on the bottom and right of each window."
726 (interactive)
727 (customize-set-variable 'window-divider-default-places t)
728 (window-divider-mode 1))
730 (defun menu-bar-right-window-divider ()
731 "Display dividers only on the right of each window."
732 (interactive)
733 (customize-set-variable 'window-divider-default-places 'right-only)
734 (window-divider-mode 1))
736 (defun menu-bar-bottom-window-divider ()
737 "Display dividers only at the bottom of each window."
738 (interactive)
739 (customize-set-variable 'window-divider-default-places 'bottom-only)
740 (window-divider-mode 1))
742 (defun menu-bar-no-window-divider ()
743 "Do not display window dividers."
744 (interactive)
745 (window-divider-mode -1))
747 ;; For the radio buttons below we check whether the respective dividers
748 ;; are displayed on the selected frame. This is not fully congruent
749 ;; with `window-divider-mode' but makes the menu entries work also when
750 ;; dividers are displayed by manipulating frame parameters directly.
751 (defvar menu-bar-showhide-window-divider-menu
752 (let ((menu (make-sparse-keymap "Window Divider")))
753 (bindings--define-key menu [customize]
754 '(menu-item "Customize" menu-bar-window-divider-customize
755 :help "Customize window dividers"
756 :visible (memq (window-system) '(x w32))))
758 (bindings--define-key menu [bottom-and-right]
759 '(menu-item "Bottom and Right"
760 menu-bar-bottom-and-right-window-divider
761 :help "Display window divider on the bottom and right of each window"
762 :visible (memq (window-system) '(x w32))
763 :button (:radio
764 . (and (window-divider-width-valid-p
765 (cdr (assq 'bottom-divider-width
766 (frame-parameters))))
767 (window-divider-width-valid-p
768 (cdr (assq 'right-divider-width
769 (frame-parameters))))))))
770 (bindings--define-key menu [right-only]
771 '(menu-item "Right Only"
772 menu-bar-right-window-divider
773 :help "Display window divider on the right of each window only"
774 :visible (memq (window-system) '(x w32))
775 :button (:radio
776 . (and (not (window-divider-width-valid-p
777 (cdr (assq 'bottom-divider-width
778 (frame-parameters)))))
779 (window-divider-width-valid-p
780 (cdr (assq 'right-divider-width
781 (frame-parameters))))))))
782 (bindings--define-key menu [bottom-only]
783 '(menu-item "Bottom Only"
784 menu-bar-bottom-window-divider
785 :help "Display window divider on the bottom of each window only"
786 :visible (memq (window-system) '(x w32))
787 :button (:radio
788 . (and (window-divider-width-valid-p
789 (cdr (assq 'bottom-divider-width
790 (frame-parameters))))
791 (not (window-divider-width-valid-p
792 (cdr (assq 'right-divider-width
793 (frame-parameters)))))))))
794 (bindings--define-key menu [no-divider]
795 '(menu-item "None"
796 menu-bar-no-window-divider
797 :help "Do not display window dividers"
798 :visible (memq (window-system) '(x w32))
799 :button (:radio
800 . (and (not (window-divider-width-valid-p
801 (cdr (assq 'bottom-divider-width
802 (frame-parameters)))))
803 (not (window-divider-width-valid-p
804 (cdr (assq 'right-divider-width
805 (frame-parameters)))))))))
806 menu))
808 (defun menu-bar-showhide-fringe-ind-customize ()
809 "Show customization buffer for `indicate-buffer-boundaries'."
810 (interactive)
811 (customize-variable 'indicate-buffer-boundaries))
813 (defun menu-bar-showhide-fringe-ind-mixed ()
814 "Display top and bottom indicators in opposite fringes, arrows in right."
815 (interactive)
816 (customize-set-variable 'indicate-buffer-boundaries
817 '((t . right) (top . left))))
819 (defun menu-bar-showhide-fringe-ind-box ()
820 "Display top and bottom indicators in opposite fringes."
821 (interactive)
822 (customize-set-variable 'indicate-buffer-boundaries
823 '((top . left) (bottom . right))))
825 (defun menu-bar-showhide-fringe-ind-right ()
826 "Display buffer boundaries and arrows in the right fringe."
827 (interactive)
828 (customize-set-variable 'indicate-buffer-boundaries 'right))
830 (defun menu-bar-showhide-fringe-ind-left ()
831 "Display buffer boundaries and arrows in the left fringe."
832 (interactive)
833 (customize-set-variable 'indicate-buffer-boundaries 'left))
835 (defun menu-bar-showhide-fringe-ind-none ()
836 "Do not display any buffer boundary indicators."
837 (interactive)
838 (customize-set-variable 'indicate-buffer-boundaries nil))
840 (defvar menu-bar-showhide-fringe-ind-menu
841 (let ((menu (make-sparse-keymap "Buffer boundaries")))
843 (bindings--define-key menu [customize]
844 '(menu-item "Other (Customize)"
845 menu-bar-showhide-fringe-ind-customize
846 :help "Additional choices available through Custom buffer"
847 :visible (display-graphic-p)
848 :button (:radio . (not (member indicate-buffer-boundaries
849 '(nil left right
850 ((top . left) (bottom . right))
851 ((t . right) (top . left))))))))
853 (bindings--define-key menu [mixed]
854 '(menu-item "Opposite, Arrows Right" menu-bar-showhide-fringe-ind-mixed
855 :help
856 "Show top/bottom indicators in opposite fringes, arrows in right"
857 :visible (display-graphic-p)
858 :button (:radio . (equal indicate-buffer-boundaries
859 '((t . right) (top . left))))))
861 (bindings--define-key menu [box]
862 '(menu-item "Opposite, No Arrows" menu-bar-showhide-fringe-ind-box
863 :help "Show top/bottom indicators in opposite fringes, no arrows"
864 :visible (display-graphic-p)
865 :button (:radio . (equal indicate-buffer-boundaries
866 '((top . left) (bottom . right))))))
868 (bindings--define-key menu [right]
869 '(menu-item "In Right Fringe" menu-bar-showhide-fringe-ind-right
870 :help "Show buffer boundaries and arrows in right fringe"
871 :visible (display-graphic-p)
872 :button (:radio . (eq indicate-buffer-boundaries 'right))))
874 (bindings--define-key menu [left]
875 '(menu-item "In Left Fringe" menu-bar-showhide-fringe-ind-left
876 :help "Show buffer boundaries and arrows in left fringe"
877 :visible (display-graphic-p)
878 :button (:radio . (eq indicate-buffer-boundaries 'left))))
880 (bindings--define-key menu [none]
881 '(menu-item "No Indicators" menu-bar-showhide-fringe-ind-none
882 :help "Hide all buffer boundary indicators and arrows"
883 :visible (display-graphic-p)
884 :button (:radio . (eq indicate-buffer-boundaries nil))))
885 menu))
887 (defun menu-bar-showhide-fringe-menu-customize ()
888 "Show customization buffer for `fringe-mode'."
889 (interactive)
890 (customize-variable 'fringe-mode))
892 (defun menu-bar-showhide-fringe-menu-customize-reset ()
893 "Reset the fringe mode: display fringes on both sides of a window."
894 (interactive)
895 (customize-set-variable 'fringe-mode nil))
897 (defun menu-bar-showhide-fringe-menu-customize-right ()
898 "Display fringes only on the right of each window."
899 (interactive)
900 (require 'fringe)
901 (customize-set-variable 'fringe-mode '(0 . nil)))
903 (defun menu-bar-showhide-fringe-menu-customize-left ()
904 "Display fringes only on the left of each window."
905 (interactive)
906 (require 'fringe)
907 (customize-set-variable 'fringe-mode '(nil . 0)))
909 (defun menu-bar-showhide-fringe-menu-customize-disable ()
910 "Do not display window fringes."
911 (interactive)
912 (require 'fringe)
913 (customize-set-variable 'fringe-mode 0))
915 (defvar menu-bar-showhide-fringe-menu
916 (let ((menu (make-sparse-keymap "Fringe")))
918 (bindings--define-key menu [showhide-fringe-ind]
919 `(menu-item "Buffer Boundaries" ,menu-bar-showhide-fringe-ind-menu
920 :visible (display-graphic-p)
921 :help "Indicate buffer boundaries in fringe"))
923 (bindings--define-key menu [indicate-empty-lines]
924 (menu-bar-make-toggle toggle-indicate-empty-lines indicate-empty-lines
925 "Empty Line Indicators"
926 "Indicating of empty lines %s"
927 "Indicate trailing empty lines in fringe, globally"))
929 (bindings--define-key menu [customize]
930 '(menu-item "Customize Fringe" menu-bar-showhide-fringe-menu-customize
931 :help "Detailed customization of fringe"
932 :visible (display-graphic-p)))
934 (bindings--define-key menu [default]
935 '(menu-item "Default" menu-bar-showhide-fringe-menu-customize-reset
936 :help "Default width fringe on both left and right side"
937 :visible (display-graphic-p)
938 :button (:radio . (eq fringe-mode nil))))
940 (bindings--define-key menu [right]
941 '(menu-item "On the Right" menu-bar-showhide-fringe-menu-customize-right
942 :help "Fringe only on the right side"
943 :visible (display-graphic-p)
944 :button (:radio . (equal fringe-mode '(0 . nil)))))
946 (bindings--define-key menu [left]
947 '(menu-item "On the Left" menu-bar-showhide-fringe-menu-customize-left
948 :help "Fringe only on the left side"
949 :visible (display-graphic-p)
950 :button (:radio . (equal fringe-mode '(nil . 0)))))
952 (bindings--define-key menu [none]
953 '(menu-item "None" menu-bar-showhide-fringe-menu-customize-disable
954 :help "Turn off fringe"
955 :visible (display-graphic-p)
956 :button (:radio . (eq fringe-mode 0))))
957 menu))
959 (defun menu-bar-right-scroll-bar ()
960 "Display scroll bars on the right of each window."
961 (interactive)
962 (customize-set-variable 'scroll-bar-mode 'right))
964 (defun menu-bar-left-scroll-bar ()
965 "Display scroll bars on the left of each window."
966 (interactive)
967 (customize-set-variable 'scroll-bar-mode 'left))
969 (defun menu-bar-no-scroll-bar ()
970 "Turn off scroll bars."
971 (interactive)
972 (customize-set-variable 'scroll-bar-mode nil))
974 (defvar menu-bar-showhide-scroll-bar-menu
975 (let ((menu (make-sparse-keymap "Scroll Bar")))
977 (bindings--define-key menu [horizontal]
978 (menu-bar-make-mm-toggle horizontal-scroll-bar-mode
979 "Horizontal"
980 "Horizontal scroll bar"))
982 (bindings--define-key menu [scrollbar-separator]
983 menu-bar-separator)
985 (bindings--define-key menu [right]
986 '(menu-item "On the Right" menu-bar-right-scroll-bar
987 :help "Scroll bar on the right side"
988 :visible (display-graphic-p)
989 :button (:radio . (and scroll-bar-mode
990 (eq (frame-parameter
991 nil 'vertical-scroll-bars)
992 'right)))))
994 (bindings--define-key menu [left]
995 '(menu-item "On the Left" menu-bar-left-scroll-bar
996 :help "Scroll bar on the left side"
997 :visible (display-graphic-p)
998 :button (:radio . (and scroll-bar-mode
999 (eq (frame-parameter
1000 nil 'vertical-scroll-bars)
1001 'left)))))
1003 (bindings--define-key menu [none]
1004 '(menu-item "No Vertical Scroll Bar" menu-bar-no-scroll-bar
1005 :help "Turn off vertical scroll bar"
1006 :visible (display-graphic-p)
1007 :button (:radio . (eq scroll-bar-mode nil))))
1008 menu))
1010 (defun menu-bar-frame-for-menubar ()
1011 "Return the frame suitable for updating the menu bar."
1012 (or (and (framep menu-updating-frame)
1013 menu-updating-frame)
1014 (selected-frame)))
1016 (defun menu-bar-positive-p (val)
1017 "Return non-nil if VAL is a positive number."
1018 (and (numberp val)
1019 (> val 0)))
1021 (defun menu-bar-set-tool-bar-position (position)
1022 (customize-set-variable 'tool-bar-mode t)
1023 (customize-set-variable 'tool-bar-position position))
1024 (defun menu-bar-showhide-tool-bar-menu-customize-disable ()
1025 "Do not display tool bars."
1026 (interactive)
1027 (customize-set-variable 'tool-bar-mode nil))
1028 (defun menu-bar-showhide-tool-bar-menu-customize-enable-left ()
1029 "Display tool bars on the left side."
1030 (interactive)
1031 (menu-bar-set-tool-bar-position 'left))
1032 (defun menu-bar-showhide-tool-bar-menu-customize-enable-right ()
1033 "Display tool bars on the right side."
1034 (interactive)
1035 (menu-bar-set-tool-bar-position 'right))
1036 (defun menu-bar-showhide-tool-bar-menu-customize-enable-top ()
1037 "Display tool bars on the top side."
1038 (interactive)
1039 (menu-bar-set-tool-bar-position 'top))
1040 (defun menu-bar-showhide-tool-bar-menu-customize-enable-bottom ()
1041 "Display tool bars on the bottom side."
1042 (interactive)
1043 (menu-bar-set-tool-bar-position 'bottom))
1045 (when (featurep 'move-toolbar)
1046 (defvar menu-bar-showhide-tool-bar-menu
1047 (let ((menu (make-sparse-keymap "Tool Bar")))
1049 (bindings--define-key menu [showhide-tool-bar-left]
1050 '(menu-item "On the Left"
1051 menu-bar-showhide-tool-bar-menu-customize-enable-left
1052 :help "Tool bar at the left side"
1053 :visible (display-graphic-p)
1054 :button
1055 (:radio . (and tool-bar-mode
1056 (frame-parameter
1057 (menu-bar-frame-for-menubar)
1058 'tool-bar-position)
1059 'left))))
1061 (bindings--define-key menu [showhide-tool-bar-right]
1062 '(menu-item "On the Right"
1063 menu-bar-showhide-tool-bar-menu-customize-enable-right
1064 :help "Tool bar at the right side"
1065 :visible (display-graphic-p)
1066 :button
1067 (:radio . (and tool-bar-mode
1068 (eq (frame-parameter
1069 (menu-bar-frame-for-menubar)
1070 'tool-bar-position)
1071 'right)))))
1073 (bindings--define-key menu [showhide-tool-bar-bottom]
1074 '(menu-item "On the Bottom"
1075 menu-bar-showhide-tool-bar-menu-customize-enable-bottom
1076 :help "Tool bar at the bottom"
1077 :visible (display-graphic-p)
1078 :button
1079 (:radio . (and tool-bar-mode
1080 (eq (frame-parameter
1081 (menu-bar-frame-for-menubar)
1082 'tool-bar-position)
1083 'bottom)))))
1085 (bindings--define-key menu [showhide-tool-bar-top]
1086 '(menu-item "On the Top"
1087 menu-bar-showhide-tool-bar-menu-customize-enable-top
1088 :help "Tool bar at the top"
1089 :visible (display-graphic-p)
1090 :button
1091 (:radio . (and tool-bar-mode
1092 (eq (frame-parameter
1093 (menu-bar-frame-for-menubar)
1094 'tool-bar-position)
1095 'top)))))
1097 (bindings--define-key menu [showhide-tool-bar-none]
1098 '(menu-item "None"
1099 menu-bar-showhide-tool-bar-menu-customize-disable
1100 :help "Turn tool bar off"
1101 :visible (display-graphic-p)
1102 :button (:radio . (eq tool-bar-mode nil))))
1103 menu)))
1105 (defvar display-line-numbers-type)
1106 (defun menu-bar-display-line-numbers-mode (type)
1107 (setq display-line-numbers-type type)
1108 (if global-display-line-numbers-mode
1109 (global-display-line-numbers-mode)
1110 (display-line-numbers-mode)))
1112 (defvar menu-bar-showhide-line-numbers-menu
1113 (let ((menu (make-sparse-keymap "Line Numbers")))
1115 (bindings--define-key menu [visual]
1116 `(menu-item "Visual Line Numbers"
1117 ,(lambda ()
1118 (interactive)
1119 (menu-bar-display-line-numbers-mode 'visual)
1120 (message "Visual line numbers enabled"))
1121 :help "Enable visual line numbers"
1122 :button (:radio . (eq display-line-numbers 'visual))
1123 :visible (menu-bar-menu-frame-live-and-visible-p)))
1125 (bindings--define-key menu [relative]
1126 `(menu-item "Relative Line Numbers"
1127 ,(lambda ()
1128 (interactive)
1129 (menu-bar-display-line-numbers-mode 'relative)
1130 (message "Relative line numbers enabled"))
1131 :help "Enable relative line numbers"
1132 :button (:radio . (eq display-line-numbers 'relative))
1133 :visible (menu-bar-menu-frame-live-and-visible-p)))
1135 (bindings--define-key menu [absolute]
1136 `(menu-item "Absolute Line Numbers"
1137 ,(lambda ()
1138 (interactive)
1139 (menu-bar-display-line-numbers-mode t)
1140 (setq display-line-numbers t)
1141 (message "Absolute line numbers enabled"))
1142 :help "Enable absolute line numbers"
1143 :button (:radio . (eq display-line-numbers t))
1144 :visible (menu-bar-menu-frame-live-and-visible-p)))
1146 (bindings--define-key menu [none]
1147 `(menu-item "No Line Numbers"
1148 ,(lambda ()
1149 (interactive)
1150 (menu-bar-display-line-numbers-mode nil)
1151 (message "Line numbers disabled"))
1152 :help "Disable line numbers"
1153 :button (:radio . (null display-line-numbers))
1154 :visible (menu-bar-menu-frame-live-and-visible-p)))
1156 (bindings--define-key menu [global]
1157 (menu-bar-make-mm-toggle global-display-line-numbers-mode
1158 "Global Line Numbers Mode"
1159 "Set line numbers globally"))
1160 menu))
1162 (defvar menu-bar-showhide-menu
1163 (let ((menu (make-sparse-keymap "Show/Hide")))
1165 (bindings--define-key menu [display-line-numbers]
1166 `(menu-item "Line Numbers for All Lines"
1167 ,menu-bar-showhide-line-numbers-menu))
1169 (bindings--define-key menu [column-number-mode]
1170 (menu-bar-make-mm-toggle column-number-mode
1171 "Column Numbers in Mode Line"
1172 "Show the current column number in the mode line"))
1174 (bindings--define-key menu [line-number-mode]
1175 (menu-bar-make-mm-toggle line-number-mode
1176 "Line Numbers in Mode Line"
1177 "Show the current line number in the mode line"))
1179 (bindings--define-key menu [size-indication-mode]
1180 (menu-bar-make-mm-toggle size-indication-mode
1181 "Size Indication"
1182 "Show the size of the buffer in the mode line"))
1184 (bindings--define-key menu [linecolumn-separator]
1185 menu-bar-separator)
1187 (bindings--define-key menu [showhide-battery]
1188 (menu-bar-make-mm-toggle display-battery-mode
1189 "Battery Status"
1190 "Display battery status information in mode line"))
1192 (bindings--define-key menu [showhide-date-time]
1193 (menu-bar-make-mm-toggle display-time-mode
1194 "Time, Load and Mail"
1195 "Display time, system load averages and \
1196 mail status in mode line"))
1198 (bindings--define-key menu [datetime-separator]
1199 menu-bar-separator)
1201 (bindings--define-key menu [showhide-speedbar]
1202 '(menu-item "Speedbar" speedbar-frame-mode
1203 :help "Display a Speedbar quick-navigation frame"
1204 :button (:toggle
1205 . (and (boundp 'speedbar-frame)
1206 (frame-live-p (symbol-value 'speedbar-frame))
1207 (frame-visible-p
1208 (symbol-value 'speedbar-frame))))))
1210 (bindings--define-key menu [showhide-window-divider]
1211 `(menu-item "Window Divider" ,menu-bar-showhide-window-divider-menu
1212 :visible (memq (window-system) '(x w32))))
1214 (bindings--define-key menu [showhide-fringe]
1215 `(menu-item "Fringe" ,menu-bar-showhide-fringe-menu
1216 :visible (display-graphic-p)))
1218 (bindings--define-key menu [showhide-scroll-bar]
1219 `(menu-item "Scroll Bar" ,menu-bar-showhide-scroll-bar-menu
1220 :visible (display-graphic-p)))
1222 (bindings--define-key menu [showhide-tooltip-mode]
1223 '(menu-item "Tooltips" tooltip-mode
1224 :help "Turn tooltips on/off"
1225 :visible (and (display-graphic-p) (fboundp 'x-show-tip))
1226 :button (:toggle . tooltip-mode)))
1228 (bindings--define-key menu [menu-bar-mode]
1229 '(menu-item "Menu Bar" toggle-menu-bar-mode-from-frame
1230 :help "Turn menu bar on/off"
1231 :button
1232 (:toggle . (menu-bar-positive-p
1233 (frame-parameter (menu-bar-frame-for-menubar)
1234 'menu-bar-lines)))))
1236 (if (and (boundp 'menu-bar-showhide-tool-bar-menu)
1237 (keymapp menu-bar-showhide-tool-bar-menu))
1238 (bindings--define-key menu [showhide-tool-bar]
1239 `(menu-item "Tool Bar" ,menu-bar-showhide-tool-bar-menu
1240 :visible (display-graphic-p)))
1241 ;; else not tool bar that can move.
1242 (bindings--define-key menu [showhide-tool-bar]
1243 '(menu-item "Tool Bar" toggle-tool-bar-mode-from-frame
1244 :help "Turn tool bar on/off"
1245 :visible (display-graphic-p)
1246 :button
1247 (:toggle . (menu-bar-positive-p
1248 (frame-parameter (menu-bar-frame-for-menubar)
1249 'tool-bar-lines))))))
1250 menu))
1252 (defvar menu-bar-line-wrapping-menu
1253 (let ((menu (make-sparse-keymap "Line Wrapping")))
1255 (bindings--define-key menu [word-wrap]
1256 `(menu-item "Word Wrap (Visual Line mode)"
1257 ,(lambda ()
1258 (interactive)
1259 (unless visual-line-mode
1260 (visual-line-mode 1))
1261 (message "Visual-Line mode enabled"))
1262 :help "Wrap long lines at word boundaries"
1263 :button (:radio
1264 . (and (null truncate-lines)
1265 (not (truncated-partial-width-window-p))
1266 word-wrap))
1267 :visible (menu-bar-menu-frame-live-and-visible-p)))
1269 (bindings--define-key menu [truncate]
1270 `(menu-item "Truncate Long Lines"
1271 ,(lambda ()
1272 (interactive)
1273 (if visual-line-mode (visual-line-mode 0))
1274 (setq word-wrap nil)
1275 (toggle-truncate-lines 1))
1276 :help "Truncate long lines at window edge"
1277 :button (:radio . (or truncate-lines
1278 (truncated-partial-width-window-p)))
1279 :visible (menu-bar-menu-frame-live-and-visible-p)
1280 :enable (not (truncated-partial-width-window-p))))
1282 (bindings--define-key menu [window-wrap]
1283 `(menu-item "Wrap at Window Edge"
1284 ,(lambda () (interactive)
1285 (if visual-line-mode (visual-line-mode 0))
1286 (setq word-wrap nil)
1287 (if truncate-lines (toggle-truncate-lines -1)))
1288 :help "Wrap long lines at window edge"
1289 :button (:radio
1290 . (and (null truncate-lines)
1291 (not (truncated-partial-width-window-p))
1292 (not word-wrap)))
1293 :visible (menu-bar-menu-frame-live-and-visible-p)
1294 :enable (not (truncated-partial-width-window-p))))
1295 menu))
1297 (defvar menu-bar-search-options-menu
1298 (let ((menu (make-sparse-keymap "Search Options")))
1300 (dolist (x '((char-fold-to-regexp "Fold Characters" "Character folding")
1301 (isearch-symbol-regexp "Whole Symbols" "Whole symbol")
1302 (word-search-regexp "Whole Words" "Whole word")))
1303 (bindings--define-key menu (vector (nth 0 x))
1304 `(menu-item ,(nth 1 x)
1305 (lambda ()
1306 (interactive)
1307 (setq search-default-mode #',(nth 0 x))
1308 (message ,(format "%s search enabled" (nth 2 x))))
1309 :help ,(format "Enable %s search" (downcase (nth 2 x)))
1310 :button (:radio . (eq search-default-mode #',(nth 0 x))))))
1312 (bindings--define-key menu [regexp-search]
1313 '(menu-item "Regular Expression"
1314 (lambda ()
1315 (interactive)
1316 (setq search-default-mode t)
1317 (message "Regular-expression search enabled"))
1318 :help "Enable regular-expression search"
1319 :button (:radio . (eq search-default-mode t))))
1321 (bindings--define-key menu [regular-search]
1322 '(menu-item "Literal Search"
1323 (lambda ()
1324 (interactive)
1325 (when search-default-mode
1326 (setq search-default-mode nil)
1327 (when (symbolp search-default-mode)
1328 (message "Literal search enabled"))))
1329 :help "Disable special search modes"
1330 :button (:radio . (not search-default-mode))))
1332 (bindings--define-key menu [custom-separator]
1333 menu-bar-separator)
1334 (bindings--define-key menu [case-fold-search]
1335 (menu-bar-make-toggle
1336 toggle-case-fold-search case-fold-search
1337 "Ignore Case"
1338 "Case-Insensitive Search %s"
1339 "Ignore letter-case in search commands"))
1341 menu))
1343 (defvar menu-bar-options-menu
1344 (let ((menu (make-sparse-keymap "Options")))
1345 (bindings--define-key menu [customize]
1346 `(menu-item "Customize Emacs" ,menu-bar-custom-menu))
1348 (bindings--define-key menu [package]
1349 '(menu-item "Manage Emacs Packages" package-list-packages
1350 :help "Install or uninstall additional Emacs packages"))
1352 (bindings--define-key menu [save]
1353 '(menu-item "Save Options" menu-bar-options-save
1354 :help "Save options set from the menu above"))
1356 (bindings--define-key menu [custom-separator]
1357 menu-bar-separator)
1359 (bindings--define-key menu [menu-set-font]
1360 '(menu-item "Set Default Font..." menu-set-font
1361 :visible (display-multi-font-p)
1362 :help "Select a default font"))
1364 (if (featurep 'system-font-setting)
1365 (bindings--define-key menu [menu-system-font]
1366 (menu-bar-make-toggle
1367 toggle-use-system-font font-use-system-font
1368 "Use System Font"
1369 "Use system font: %s"
1370 "Use the monospaced font defined by the system")))
1372 (bindings--define-key menu [showhide]
1373 `(menu-item "Show/Hide" ,menu-bar-showhide-menu))
1375 (bindings--define-key menu [showhide-separator]
1376 menu-bar-separator)
1378 (bindings--define-key menu [mule]
1379 ;; It is better not to use backquote here,
1380 ;; because that makes a bootstrapping problem
1381 ;; if you need to recompile all the Lisp files using interpreted code.
1382 `(menu-item "Multilingual Environment" ,mule-menu-keymap
1383 ;; Most of the MULE menu actually does make sense in
1384 ;; unibyte mode, e.g. language selection.
1385 ;; :visible '(default-value 'enable-multibyte-characters)
1387 ;;(setq menu-bar-final-items (cons 'mule menu-bar-final-items))
1388 ;;(bindings--define-key menu [preferences]
1389 ;; `(menu-item "Preferences" ,menu-bar-preferences-menu
1390 ;; :help "Toggle important global options"))
1392 (bindings--define-key menu [mule-separator]
1393 menu-bar-separator)
1395 (bindings--define-key menu [debug-on-quit]
1396 (menu-bar-make-toggle toggle-debug-on-quit debug-on-quit
1397 "Enter Debugger on Quit/C-g" "Debug on Quit %s"
1398 "Enter Lisp debugger when C-g is pressed"))
1399 (bindings--define-key menu [debug-on-error]
1400 (menu-bar-make-toggle toggle-debug-on-error debug-on-error
1401 "Enter Debugger on Error" "Debug on Error %s"
1402 "Enter Lisp debugger when an error is signaled"))
1403 (bindings--define-key menu [debugger-separator]
1404 menu-bar-separator)
1406 (bindings--define-key menu [blink-cursor-mode]
1407 (menu-bar-make-mm-toggle
1408 blink-cursor-mode
1409 "Blink Cursor"
1410 "Whether the cursor blinks (Blink Cursor mode)"))
1411 (bindings--define-key menu [cursor-separator]
1412 menu-bar-separator)
1414 (bindings--define-key menu [save-place]
1415 (menu-bar-make-toggle
1416 toggle-save-place-globally save-place
1417 "Save Place in Files between Sessions"
1418 "Saving place in files %s"
1419 "Visit files of previous session when restarting Emacs"
1420 (require 'saveplace)
1421 ;; Do it by name, to avoid a free-variable
1422 ;; warning during byte compilation.
1423 (set-default
1424 'save-place (not (symbol-value 'save-place)))))
1426 (bindings--define-key menu [uniquify]
1427 (menu-bar-make-toggle
1428 toggle-uniquify-buffer-names uniquify-buffer-name-style
1429 "Use Directory Names in Buffer Names"
1430 "Directory name in buffer names (uniquify) %s"
1431 "Uniquify buffer names by adding parent directory names"
1432 (setq uniquify-buffer-name-style
1433 (if (not uniquify-buffer-name-style)
1434 'post-forward-angle-brackets))))
1436 (bindings--define-key menu [edit-options-separator]
1437 menu-bar-separator)
1438 (bindings--define-key menu [cua-mode]
1439 (menu-bar-make-mm-toggle
1440 cua-mode
1441 "Use CUA Keys (Cut/Paste with C-x/C-c/C-v)"
1442 "Use C-z/C-x/C-c/C-v keys for undo/cut/copy/paste"
1443 (:visible (or (not (boundp 'cua-enable-cua-keys))
1444 cua-enable-cua-keys))))
1446 (bindings--define-key menu [cua-emulation-mode]
1447 (menu-bar-make-mm-toggle
1448 cua-mode
1449 "Shift movement mark region (CUA)"
1450 "Use shifted movement keys to set and extend the region"
1451 (:visible (and (boundp 'cua-enable-cua-keys)
1452 (not cua-enable-cua-keys)))))
1454 (bindings--define-key menu [search-options]
1455 `(menu-item "Default Search Options"
1456 ,menu-bar-search-options-menu))
1458 (bindings--define-key menu [line-wrapping]
1459 `(menu-item "Line Wrapping in This Buffer"
1460 ,menu-bar-line-wrapping-menu))
1463 (bindings--define-key menu [highlight-separator]
1464 menu-bar-separator)
1465 (bindings--define-key menu [highlight-paren-mode]
1466 (menu-bar-make-mm-toggle
1467 show-paren-mode
1468 "Highlight Matching Parentheses"
1469 "Highlight matching/mismatched parentheses at cursor (Show Paren mode)"))
1470 (bindings--define-key menu [transient-mark-mode]
1471 (menu-bar-make-mm-toggle
1472 transient-mark-mode
1473 "Highlight Active Region"
1474 "Make text in active region stand out in color (Transient Mark mode)"
1475 (:enable (not cua-mode))))
1476 menu))
1479 ;; The "Tools" menu items
1481 (defvar menu-bar-games-menu
1482 (let ((menu (make-sparse-keymap "Games")))
1484 (bindings--define-key menu [zone]
1485 '(menu-item "Zone Out" zone
1486 :help "Play tricks with Emacs display when Emacs is idle"))
1487 (bindings--define-key menu [tetris]
1488 '(menu-item "Tetris" tetris
1489 :help "Falling blocks game"))
1490 (bindings--define-key menu [solitaire]
1491 '(menu-item "Solitaire" solitaire
1492 :help "Get rid of all the stones"))
1493 (bindings--define-key menu [snake]
1494 '(menu-item "Snake" snake
1495 :help "Move snake around avoiding collisions"))
1496 (bindings--define-key menu [pong]
1497 '(menu-item "Pong" pong
1498 :help "Bounce the ball to your opponent"))
1499 (bindings--define-key menu [mult]
1500 '(menu-item "Multiplication Puzzle" mpuz
1501 :help "Exercise brain with multiplication"))
1502 (bindings--define-key menu [life]
1503 '(menu-item "Life" life
1504 :help "Watch how John Conway's cellular automaton evolves"))
1505 (bindings--define-key menu [hanoi]
1506 '(menu-item "Towers of Hanoi" hanoi
1507 :help "Watch Towers-of-Hanoi puzzle solved by Emacs"))
1508 (bindings--define-key menu [gomoku]
1509 '(menu-item "Gomoku" gomoku
1510 :help "Mark 5 contiguous squares (like tic-tac-toe)"))
1511 (bindings--define-key menu [bubbles]
1512 '(menu-item "Bubbles" bubbles
1513 :help "Remove all bubbles using the fewest moves"))
1514 (bindings--define-key menu [black-box]
1515 '(menu-item "Blackbox" blackbox
1516 :help "Find balls in a black box by shooting rays"))
1517 (bindings--define-key menu [adventure]
1518 '(menu-item "Adventure" dunnet
1519 :help "Dunnet, a text Adventure game for Emacs"))
1520 (bindings--define-key menu [5x5]
1521 '(menu-item "5x5" 5x5
1522 :help "Fill in all the squares on a 5x5 board"))
1523 menu))
1525 (defvar menu-bar-encryption-decryption-menu
1526 (let ((menu (make-sparse-keymap "Encryption/Decryption")))
1527 (bindings--define-key menu [insert-keys]
1528 '(menu-item "Insert Keys" epa-insert-keys
1529 :help "Insert public keys after the current point"))
1531 (bindings--define-key menu [export-keys]
1532 '(menu-item "Export Keys" epa-export-keys
1533 :help "Export public keys to a file"))
1535 (bindings--define-key menu [import-keys-region]
1536 '(menu-item "Import Keys from Region" epa-import-keys-region
1537 :help "Import public keys from the current region"))
1539 (bindings--define-key menu [import-keys]
1540 '(menu-item "Import Keys from File..." epa-import-keys
1541 :help "Import public keys from a file"))
1543 (bindings--define-key menu [list-keys]
1544 '(menu-item "List Keys" epa-list-keys
1545 :help "Browse your public keyring"))
1547 (bindings--define-key menu [separator-keys]
1548 menu-bar-separator)
1550 (bindings--define-key menu [sign-region]
1551 '(menu-item "Sign Region" epa-sign-region
1552 :help "Create digital signature of the current region"))
1554 (bindings--define-key menu [verify-region]
1555 '(menu-item "Verify Region" epa-verify-region
1556 :help "Verify digital signature of the current region"))
1558 (bindings--define-key menu [encrypt-region]
1559 '(menu-item "Encrypt Region" epa-encrypt-region
1560 :help "Encrypt the current region"))
1562 (bindings--define-key menu [decrypt-region]
1563 '(menu-item "Decrypt Region" epa-decrypt-region
1564 :help "Decrypt the current region"))
1566 (bindings--define-key menu [separator-file]
1567 menu-bar-separator)
1569 (bindings--define-key menu [sign-file]
1570 '(menu-item "Sign File..." epa-sign-file
1571 :help "Create digital signature of a file"))
1573 (bindings--define-key menu [verify-file]
1574 '(menu-item "Verify File..." epa-verify-file
1575 :help "Verify digital signature of a file"))
1577 (bindings--define-key menu [encrypt-file]
1578 '(menu-item "Encrypt File..." epa-encrypt-file
1579 :help "Encrypt a file"))
1581 (bindings--define-key menu [decrypt-file]
1582 '(menu-item "Decrypt File..." epa-decrypt-file
1583 :help "Decrypt a file"))
1585 menu))
1587 (defun menu-bar-read-mail ()
1588 "Read mail using `read-mail-command'."
1589 (interactive)
1590 (call-interactively read-mail-command))
1592 (defvar menu-bar-tools-menu
1593 (let ((menu (make-sparse-keymap "Tools")))
1595 (bindings--define-key menu [games]
1596 `(menu-item "Games" ,menu-bar-games-menu))
1598 (bindings--define-key menu [separator-games]
1599 menu-bar-separator)
1601 (bindings--define-key menu [encryption-decryption]
1602 `(menu-item "Encryption/Decryption"
1603 ,menu-bar-encryption-decryption-menu))
1605 (bindings--define-key menu [separator-encryption-decryption]
1606 menu-bar-separator)
1608 (bindings--define-key menu [simple-calculator]
1609 '(menu-item "Simple Calculator" calculator
1610 :help "Invoke the Emacs built-in quick calculator"))
1611 (bindings--define-key menu [calc]
1612 '(menu-item "Programmable Calculator" calc
1613 :help "Invoke the Emacs built-in full scientific calculator"))
1614 (bindings--define-key menu [calendar]
1615 '(menu-item "Calendar" calendar
1616 :help "Invoke the Emacs built-in calendar"))
1618 (bindings--define-key menu [separator-net]
1619 menu-bar-separator)
1621 (bindings--define-key menu [browse-web]
1622 '(menu-item "Browse the Web..." browse-web))
1623 (bindings--define-key menu [directory-search]
1624 '(menu-item "Directory Servers" eudc-tools-menu))
1625 (bindings--define-key menu [compose-mail]
1626 '(menu-item "Compose New Mail" compose-mail
1627 :visible (and mail-user-agent (not (eq mail-user-agent 'ignore)))
1628 :help "Start writing a new mail message"))
1629 (bindings--define-key menu [rmail]
1630 '(menu-item "Read Mail" menu-bar-read-mail
1631 :visible (and read-mail-command
1632 (not (eq read-mail-command 'ignore)))
1633 :help "Read your mail"))
1635 (bindings--define-key menu [gnus]
1636 '(menu-item "Read Net News" gnus
1637 :help "Read network news groups"))
1639 (bindings--define-key menu [separator-vc]
1640 menu-bar-separator)
1642 (bindings--define-key menu [vc] nil) ;Create the place for the VC menu.
1644 (bindings--define-key menu [separator-compare]
1645 menu-bar-separator)
1647 (bindings--define-key menu [epatch]
1648 '(menu-item "Apply Patch" menu-bar-epatch-menu))
1649 (bindings--define-key menu [ediff-merge]
1650 '(menu-item "Merge" menu-bar-ediff-merge-menu))
1651 (bindings--define-key menu [compare]
1652 '(menu-item "Compare (Ediff)" menu-bar-ediff-menu))
1654 (bindings--define-key menu [separator-spell]
1655 menu-bar-separator)
1657 (bindings--define-key menu [spell]
1658 '(menu-item "Spell Checking" ispell-menu-map))
1660 (bindings--define-key menu [separator-prog]
1661 menu-bar-separator)
1663 (bindings--define-key menu [semantic]
1664 '(menu-item "Source Code Parsers (Semantic)"
1665 semantic-mode
1666 :help "Toggle automatic parsing in source code buffers (Semantic mode)"
1667 :button (:toggle . (bound-and-true-p semantic-mode))))
1669 (bindings--define-key menu [ede]
1670 '(menu-item "Project Support (EDE)"
1671 global-ede-mode
1672 :help "Toggle the Emacs Development Environment (Global EDE mode)"
1673 :button (:toggle . (bound-and-true-p global-ede-mode))))
1675 (bindings--define-key menu [gdb]
1676 '(menu-item "Debugger (GDB)..." gdb
1677 :help "Debug a program from within Emacs with GDB"))
1678 (bindings--define-key menu [shell-on-region]
1679 '(menu-item "Shell Command on Region..." shell-command-on-region
1680 :enable mark-active
1681 :help "Pass marked region to a shell command"))
1682 (bindings--define-key menu [shell]
1683 '(menu-item "Shell Command..." shell-command
1684 :help "Invoke a shell command and catch its output"))
1685 (bindings--define-key menu [compile]
1686 '(menu-item "Compile..." compile
1687 :help "Invoke compiler or Make, view compilation errors"))
1688 (bindings--define-key menu [grep]
1689 '(menu-item "Search Files (Grep)..." grep
1690 :help "Search files for strings or regexps (with Grep)"))
1691 menu))
1693 ;; The "Help" menu items
1695 (defvar menu-bar-describe-menu
1696 (let ((menu (make-sparse-keymap "Describe")))
1698 (bindings--define-key menu [mule-diag]
1699 '(menu-item "Show All of Mule Status" mule-diag
1700 :visible (default-value 'enable-multibyte-characters)
1701 :help "Display multilingual environment settings"))
1702 (bindings--define-key menu [describe-coding-system-briefly]
1703 '(menu-item "Describe Coding System (Briefly)"
1704 describe-current-coding-system-briefly
1705 :visible (default-value 'enable-multibyte-characters)))
1706 (bindings--define-key menu [describe-coding-system]
1707 '(menu-item "Describe Coding System..." describe-coding-system
1708 :visible (default-value 'enable-multibyte-characters)))
1709 (bindings--define-key menu [describe-input-method]
1710 '(menu-item "Describe Input Method..." describe-input-method
1711 :visible (default-value 'enable-multibyte-characters)
1712 :help "Keyboard layout for specific input method"))
1713 (bindings--define-key menu [describe-language-environment]
1714 `(menu-item "Describe Language Environment"
1715 ,describe-language-environment-map))
1717 (bindings--define-key menu [separator-desc-mule]
1718 menu-bar-separator)
1720 (bindings--define-key menu [list-keybindings]
1721 '(menu-item "List Key Bindings" describe-bindings
1722 :help "Display all current key bindings (keyboard shortcuts)"))
1723 (bindings--define-key menu [describe-current-display-table]
1724 '(menu-item "Describe Display Table" describe-current-display-table
1725 :help "Describe the current display table"))
1726 (bindings--define-key menu [describe-package]
1727 '(menu-item "Describe Package..." describe-package
1728 :help "Display documentation of a Lisp package"))
1729 (bindings--define-key menu [describe-face]
1730 '(menu-item "Describe Face..." describe-face
1731 :help "Display the properties of a face"))
1732 (bindings--define-key menu [describe-variable]
1733 '(menu-item "Describe Variable..." describe-variable
1734 :help "Display documentation of variable/option"))
1735 (bindings--define-key menu [describe-function]
1736 '(menu-item "Describe Function..." describe-function
1737 :help "Display documentation of function/command"))
1738 (bindings--define-key menu [describe-key-1]
1739 '(menu-item "Describe Key or Mouse Operation..." describe-key
1740 ;; Users typically don't identify keys and menu items...
1741 :help "Display documentation of command bound to a \
1742 key, a click, or a menu-item"))
1743 (bindings--define-key menu [describe-mode]
1744 '(menu-item "Describe Buffer Modes" describe-mode
1745 :help "Describe this buffer's major and minor mode"))
1746 menu))
1748 (defun menu-bar-read-lispref ()
1749 "Display the Emacs Lisp Reference manual in Info mode."
1750 (interactive)
1751 (info "elisp"))
1753 (defun menu-bar-read-lispintro ()
1754 "Display the Introduction to Emacs Lisp Programming in Info mode."
1755 (interactive)
1756 (info "eintr"))
1758 (defun search-emacs-glossary ()
1759 "Display the Glossary node of the Emacs manual in Info mode."
1760 (interactive)
1761 (info "(emacs)Glossary"))
1763 (defun emacs-index-search (topic)
1764 "Look up TOPIC in the indices of the Emacs User Manual."
1765 (interactive "sSubject to look up: ")
1766 (info "emacs")
1767 (Info-index topic))
1769 (defun elisp-index-search (topic)
1770 "Look up TOPIC in the indices of the Emacs Lisp Reference Manual."
1771 (interactive "sSubject to look up: ")
1772 (info "elisp")
1773 (Info-index topic))
1775 (defvar menu-bar-search-documentation-menu
1776 (let ((menu (make-sparse-keymap "Search Documentation")))
1778 (bindings--define-key menu [search-documentation-strings]
1779 '(menu-item "Search Documentation Strings..." apropos-documentation
1780 :help
1781 "Find functions and variables whose doc strings match a regexp"))
1782 (bindings--define-key menu [find-any-object-by-name]
1783 '(menu-item "Find Any Object by Name..." apropos
1784 :help "Find symbols of any kind whose names match a regexp"))
1785 (bindings--define-key menu [find-option-by-value]
1786 '(menu-item "Find Options by Value..." apropos-value
1787 :help "Find variables whose values match a regexp"))
1788 (bindings--define-key menu [find-options-by-name]
1789 '(menu-item "Find Options by Name..." apropos-user-option
1790 :help "Find user options whose names match a regexp"))
1791 (bindings--define-key menu [find-commands-by-name]
1792 '(menu-item "Find Commands by Name..." apropos-command
1793 :help "Find commands whose names match a regexp"))
1794 (bindings--define-key menu [sep1]
1795 menu-bar-separator)
1796 (bindings--define-key menu [lookup-command-in-manual]
1797 '(menu-item "Look Up Command in User Manual..." Info-goto-emacs-command-node
1798 :help "Display manual section that describes a command"))
1799 (bindings--define-key menu [lookup-key-in-manual]
1800 '(menu-item "Look Up Key in User Manual..." Info-goto-emacs-key-command-node
1801 :help "Display manual section that describes a key"))
1802 (bindings--define-key menu [lookup-subject-in-elisp-manual]
1803 '(menu-item "Look Up Subject in ELisp Manual..." elisp-index-search
1804 :help "Find description of a subject in Emacs Lisp manual"))
1805 (bindings--define-key menu [lookup-subject-in-emacs-manual]
1806 '(menu-item "Look Up Subject in User Manual..." emacs-index-search
1807 :help "Find description of a subject in Emacs User manual"))
1808 (bindings--define-key menu [emacs-terminology]
1809 '(menu-item "Emacs Terminology" search-emacs-glossary
1810 :help "Display the Glossary section of the Emacs manual"))
1811 menu))
1813 (defvar menu-bar-manuals-menu
1814 (let ((menu (make-sparse-keymap "More Manuals")))
1816 (bindings--define-key menu [man]
1817 '(menu-item "Read Man Page..." manual-entry
1818 :help "Man-page docs for external commands and libraries"))
1819 (bindings--define-key menu [sep2]
1820 menu-bar-separator)
1821 (bindings--define-key menu [order-emacs-manuals]
1822 '(menu-item "Ordering Manuals" view-order-manuals
1823 :help "How to order manuals from the Free Software Foundation"))
1824 (bindings--define-key menu [lookup-subject-in-all-manuals]
1825 '(menu-item "Lookup Subject in all Manuals..." info-apropos
1826 :help "Find description of a subject in all installed manuals"))
1827 (bindings--define-key menu [other-manuals]
1828 '(menu-item "All Other Manuals (Info)" Info-directory
1829 :help "Read any of the installed manuals"))
1830 (bindings--define-key menu [emacs-lisp-reference]
1831 '(menu-item "Emacs Lisp Reference" menu-bar-read-lispref
1832 :help "Read the Emacs Lisp Reference manual"))
1833 (bindings--define-key menu [emacs-lisp-intro]
1834 '(menu-item "Introduction to Emacs Lisp" menu-bar-read-lispintro
1835 :help "Read the Introduction to Emacs Lisp Programming"))
1836 menu))
1838 (defun help-with-tutorial-spec-language ()
1839 "Use the Emacs tutorial, specifying which language you want."
1840 (interactive)
1841 (help-with-tutorial t))
1843 (defvar menu-bar-help-menu
1844 (let ((menu (make-sparse-keymap "Help")))
1845 (bindings--define-key menu [about-gnu-project]
1846 '(menu-item "About GNU" describe-gnu-project
1847 :help "About the GNU System, GNU Project, and GNU/Linux"))
1848 (bindings--define-key menu [about-emacs]
1849 '(menu-item "About Emacs" about-emacs
1850 :help "Display version number, copyright info, and basic help"))
1851 (bindings--define-key menu [sep4]
1852 menu-bar-separator)
1853 (bindings--define-key menu [describe-no-warranty]
1854 '(menu-item "(Non)Warranty" describe-no-warranty
1855 :help "Explain that Emacs has NO WARRANTY"))
1856 (bindings--define-key menu [describe-copying]
1857 '(menu-item "Copying Conditions" describe-copying
1858 :help "Show the Emacs license (GPL)"))
1859 (bindings--define-key menu [getting-new-versions]
1860 '(menu-item "Getting New Versions" describe-distribution
1861 :help "How to get the latest version of Emacs"))
1862 (bindings--define-key menu [sep2]
1863 menu-bar-separator)
1864 (bindings--define-key menu [external-packages]
1865 '(menu-item "Finding Extra Packages" view-external-packages
1866 :help "How to get more Lisp packages for use in Emacs"))
1867 (bindings--define-key menu [find-emacs-packages]
1868 '(menu-item "Search Built-in Packages" finder-by-keyword
1869 :help "Find built-in packages and features by keyword"))
1870 (bindings--define-key menu [more-manuals]
1871 `(menu-item "More Manuals" ,menu-bar-manuals-menu))
1872 (bindings--define-key menu [emacs-manual]
1873 '(menu-item "Read the Emacs Manual" info-emacs-manual
1874 :help "Full documentation of Emacs features"))
1875 (bindings--define-key menu [describe]
1876 `(menu-item "Describe" ,menu-bar-describe-menu))
1877 (bindings--define-key menu [search-documentation]
1878 `(menu-item "Search Documentation" ,menu-bar-search-documentation-menu))
1879 (bindings--define-key menu [sep1]
1880 menu-bar-separator)
1881 (bindings--define-key menu [emacs-psychotherapist]
1882 '(menu-item "Emacs Psychotherapist" doctor
1883 :help "Our doctor will help you feel better"))
1884 (bindings--define-key menu [send-emacs-bug-report]
1885 '(menu-item "Send Bug Report..." report-emacs-bug
1886 :help "Send e-mail to Emacs maintainers"))
1887 (bindings--define-key menu [emacs-manual-bug]
1888 '(menu-item "How to Report a Bug" info-emacs-bug
1889 :help "Read about how to report an Emacs bug"))
1890 (bindings--define-key menu [emacs-known-problems]
1891 '(menu-item "Emacs Known Problems" view-emacs-problems
1892 :help "Read about known problems with Emacs"))
1893 (bindings--define-key menu [emacs-news]
1894 '(menu-item "Emacs News" view-emacs-news
1895 :help "New features of this version"))
1896 (bindings--define-key menu [emacs-faq]
1897 '(menu-item "Emacs FAQ" view-emacs-FAQ
1898 :help "Frequently asked (and answered) questions about Emacs"))
1900 (bindings--define-key menu [emacs-tutorial-language-specific]
1901 '(menu-item "Emacs Tutorial (choose language)..."
1902 help-with-tutorial-spec-language
1903 :help "Learn how to use Emacs (choose a language)"))
1904 (bindings--define-key menu [emacs-tutorial]
1905 '(menu-item "Emacs Tutorial" help-with-tutorial
1906 :help "Learn how to use Emacs"))
1908 ;; In macOS it's in the app menu already.
1909 ;; FIXME? There already is an "About Emacs" (sans ...) entry in the Help menu.
1910 (and (featurep 'ns)
1911 (not (eq system-type 'darwin))
1912 (bindings--define-key menu [info-panel]
1913 '(menu-item "About Emacs..." ns-do-emacs-info-panel)))
1914 menu))
1916 (bindings--define-key global-map [menu-bar tools]
1917 (cons "Tools" menu-bar-tools-menu))
1918 (bindings--define-key global-map [menu-bar buffer]
1919 (cons "Buffers" global-buffers-menu-map))
1920 (bindings--define-key global-map [menu-bar options]
1921 (cons "Options" menu-bar-options-menu))
1922 (bindings--define-key global-map [menu-bar edit]
1923 (cons "Edit" menu-bar-edit-menu))
1924 (bindings--define-key global-map [menu-bar file]
1925 (cons "File" menu-bar-file-menu))
1926 (bindings--define-key global-map [menu-bar help-menu]
1927 (cons (purecopy "Help") menu-bar-help-menu))
1929 (defun menu-bar-menu-frame-live-and-visible-p ()
1930 "Return non-nil if the menu frame is alive and visible.
1931 The menu frame is the frame for which we are updating the menu."
1932 (let ((menu-frame (or menu-updating-frame (selected-frame))))
1933 (and (frame-live-p menu-frame)
1934 (frame-visible-p menu-frame))))
1936 (defun menu-bar-non-minibuffer-window-p ()
1937 "Return non-nil if the menu frame's selected window is no minibuffer window.
1938 Return nil if the menu frame is dead or its selected window is a
1939 minibuffer window. The menu frame is the frame for which we are
1940 updating the menu."
1941 (let ((menu-frame (or menu-updating-frame (selected-frame))))
1942 (and (frame-live-p menu-frame)
1943 (not (window-minibuffer-p
1944 (frame-selected-window menu-frame))))))
1946 (defun kill-this-buffer () ; for the menu bar
1947 "Kill the current buffer.
1948 When called in the minibuffer, get out of the minibuffer
1949 using `abort-recursive-edit'.
1951 This command can be reliably invoked only from the menu bar,
1952 otherwise it could decide to silently do nothing."
1953 (interactive)
1954 (cond
1955 ;; Don't do anything when `menu-frame' is not alive or visible
1956 ;; (Bug#8184).
1957 ((not (menu-bar-menu-frame-live-and-visible-p)))
1958 ((menu-bar-non-minibuffer-window-p)
1959 (kill-buffer (current-buffer)))
1961 (abort-recursive-edit))))
1963 (defun kill-this-buffer-enabled-p ()
1964 "Return non-nil if the `kill-this-buffer' menu item should be enabled."
1965 (or (not (menu-bar-non-minibuffer-window-p))
1966 (let (found-1)
1967 ;; Instead of looping over entire buffer list, stop once we've
1968 ;; found two "killable" buffers (Bug#8184).
1969 (catch 'found-2
1970 (dolist (buffer (buffer-list))
1971 (unless (string-match-p "^ " (buffer-name buffer))
1972 (if (not found-1)
1973 (setq found-1 t)
1974 (throw 'found-2 t))))))))
1976 (put 'dired 'menu-enable '(menu-bar-non-minibuffer-window-p))
1978 ;; Permit deleting frame if it would leave a visible or iconified frame.
1979 (defun delete-frame-enabled-p ()
1980 "Return non-nil if `delete-frame' should be enabled in the menu bar."
1981 (let ((frames (frame-list))
1982 (count 0))
1983 (while frames
1984 (if (frame-visible-p (car frames))
1985 (setq count (1+ count)))
1986 (setq frames (cdr frames)))
1987 (> count 1)))
1989 (defcustom yank-menu-length 20
1990 "Maximum length to display in the yank-menu."
1991 :type 'integer
1992 :group 'menu)
1994 (defun menu-bar-update-yank-menu (string old)
1995 (let ((front (car (cdr yank-menu)))
1996 (menu-string (if (<= (length string) yank-menu-length)
1997 string
1998 (concat
1999 (substring string 0 (/ yank-menu-length 2))
2000 "..."
2001 (substring string (- (/ yank-menu-length 2)))))))
2002 ;; Don't let the menu string be all dashes
2003 ;; because that has a special meaning in a menu.
2004 (if (string-match "\\`-+\\'" menu-string)
2005 (setq menu-string (concat menu-string " ")))
2006 ;; If we're supposed to be extending an existing string, and that
2007 ;; string really is at the front of the menu, then update it in place.
2008 (if (and old (or (eq old (car front))
2009 (string= old (car front))))
2010 (progn
2011 (setcar front string)
2012 (setcar (cdr front) menu-string))
2013 (setcdr yank-menu
2014 (cons
2015 (cons string (cons menu-string 'menu-bar-select-yank))
2016 (cdr yank-menu)))))
2017 (if (> (length (cdr yank-menu)) kill-ring-max)
2018 (setcdr (nthcdr kill-ring-max yank-menu) nil)))
2020 (put 'menu-bar-select-yank 'apropos-inhibit t)
2021 (defun menu-bar-select-yank ()
2022 "Insert the stretch of previously-killed text selected from menu.
2023 The menu shows all the killed text sequences stored in `kill-ring'."
2024 (interactive "*")
2025 (push-mark)
2026 (insert last-command-event))
2029 ;;; Buffers Menu
2031 (defcustom buffers-menu-max-size 10
2032 "Maximum number of entries which may appear on the Buffers menu.
2033 If this is 10, then only the ten most-recently-selected buffers are shown.
2034 If this is nil, then all buffers are shown.
2035 A large number or nil slows down menu responsiveness."
2036 :type '(choice integer
2037 (const :tag "All" nil))
2038 :group 'menu)
2040 (defcustom buffers-menu-buffer-name-length 30
2041 "Maximum length of the buffer name on the Buffers menu.
2042 If this is a number, then buffer names are truncated to this length.
2043 If this is nil, then buffer names are shown in full.
2044 A large number or nil makes the menu too wide."
2045 :type '(choice integer
2046 (const :tag "Full length" nil))
2047 :group 'menu)
2049 (defcustom buffers-menu-show-directories 'unless-uniquify
2050 "If non-nil, show directories in the Buffers menu for buffers that have them.
2051 The special value `unless-uniquify' means that directories will be shown
2052 unless `uniquify-buffer-name-style' is non-nil (in which case, buffer
2053 names should include enough of a buffer's directory to distinguish it
2054 from other buffers).
2056 Setting this variable directly does not take effect until next time the
2057 Buffers menu is regenerated."
2058 :set (lambda (symbol value)
2059 (set symbol value)
2060 (menu-bar-update-buffers t))
2061 :initialize 'custom-initialize-default
2062 :type '(choice (const :tag "Never" nil)
2063 (const :tag "Unless uniquify is enabled" unless-uniquify)
2064 (const :tag "Always" t))
2065 :group 'menu)
2067 (defcustom buffers-menu-show-status t
2068 "If non-nil, show modified/read-only status of buffers in the Buffers menu.
2069 Setting this variable directly does not take effect until next time the
2070 Buffers menu is regenerated."
2071 :set (lambda (symbol value)
2072 (set symbol value)
2073 (menu-bar-update-buffers t))
2074 :initialize 'custom-initialize-default
2075 :type 'boolean
2076 :group 'menu)
2078 (defvar list-buffers-directory nil
2079 "String to display in buffer listings for buffers not visiting a file.")
2080 (make-variable-buffer-local 'list-buffers-directory)
2082 (defun menu-bar-select-buffer ()
2083 (interactive)
2084 (switch-to-buffer last-command-event))
2086 (defun menu-bar-select-frame (frame)
2087 (make-frame-visible frame)
2088 (raise-frame frame)
2089 (select-frame frame))
2091 (defun menu-bar-update-buffers-1 (elt)
2092 (let* ((buf (car elt))
2093 (file
2094 (and (if (eq buffers-menu-show-directories 'unless-uniquify)
2095 (or (not (boundp 'uniquify-buffer-name-style))
2096 (null uniquify-buffer-name-style))
2097 buffers-menu-show-directories)
2098 (or (buffer-file-name buf)
2099 (buffer-local-value 'list-buffers-directory buf)))))
2100 (when file
2101 (setq file (file-name-directory file)))
2102 (when (and file (> (length file) 20))
2103 (setq file (concat "..." (substring file -17))))
2104 (cons (if buffers-menu-show-status
2105 (let ((mod (if (buffer-modified-p buf) "*" ""))
2106 (ro (if (buffer-local-value 'buffer-read-only buf) "%" "")))
2107 (if file
2108 (format "%s %s%s -- %s" (cdr elt) mod ro file)
2109 (format "%s %s%s" (cdr elt) mod ro)))
2110 (if file
2111 (format "%s -- %s" (cdr elt) file)
2112 (cdr elt)))
2113 buf)))
2115 ;; Used to cache the menu entries for commands in the Buffers menu
2116 (defvar menu-bar-buffers-menu-command-entries nil)
2118 (defvar menu-bar-select-buffer-function 'switch-to-buffer
2119 "Function to select the buffer chosen from the `Buffers' menu-bar menu.
2120 It must accept a buffer as its only required argument.")
2122 (defun menu-bar-buffer-vector (alist)
2123 ;; turn ((name . buffer) ...) into a menu
2124 (let ((buffers-vec (make-vector (length alist) nil))
2125 (i (length alist)))
2126 (dolist (pair alist)
2127 (setq i (1- i))
2128 (aset buffers-vec i
2129 (cons (car pair)
2130 `(lambda ()
2131 (interactive)
2132 (funcall menu-bar-select-buffer-function ,(cdr pair))))))
2133 buffers-vec))
2135 (defun menu-bar-update-buffers (&optional force)
2136 ;; If user discards the Buffers item, play along.
2137 (and (lookup-key (current-global-map) [menu-bar buffer])
2138 (or force (frame-or-buffer-changed-p))
2139 (let ((buffers (buffer-list))
2140 (frames (frame-list))
2141 buffers-menu)
2143 ;; Make the menu of buffers proper.
2144 (setq buffers-menu
2145 (let ((i 0)
2146 (limit (if (and (integerp buffers-menu-max-size)
2147 (> buffers-menu-max-size 1))
2148 buffers-menu-max-size most-positive-fixnum))
2149 alist)
2150 ;; Put into each element of buffer-list
2151 ;; the name for actual display,
2152 ;; perhaps truncated in the middle.
2153 (while buffers
2154 (let* ((buf (pop buffers))
2155 (name (buffer-name buf)))
2156 (unless (eq ?\s (aref name 0))
2157 (push (menu-bar-update-buffers-1
2158 (cons buf
2159 (if (and (integerp buffers-menu-buffer-name-length)
2160 (> (length name) buffers-menu-buffer-name-length))
2161 (concat
2162 (substring
2163 name 0 (/ buffers-menu-buffer-name-length 2))
2164 "..."
2165 (substring
2166 name (- (/ buffers-menu-buffer-name-length 2))))
2167 name)
2169 alist)
2170 ;; If requested, list only the N most recently
2171 ;; selected buffers.
2172 (when (= limit (setq i (1+ i)))
2173 (setq buffers nil)))))
2174 (list (menu-bar-buffer-vector alist))))
2176 ;; Make a Frames menu if we have more than one frame.
2177 (when (cdr frames)
2178 (let* ((frames-vec (make-vector (length frames) nil))
2179 (frames-menu
2180 (cons 'keymap
2181 (list "Select Frame" frames-vec)))
2182 (i 0))
2183 (dolist (frame frames)
2184 (aset frames-vec i
2185 (cons
2186 (frame-parameter frame 'name)
2187 `(lambda ()
2188 (interactive) (menu-bar-select-frame ,frame))))
2189 (setq i (1+ i)))
2190 ;; Put it after the normal buffers
2191 (setq buffers-menu
2192 (nconc buffers-menu
2193 `((frames-separator "--")
2194 (frames menu-item "Frames" ,frames-menu))))))
2196 ;; Add in some normal commands at the end of the menu. We use
2197 ;; the copy cached in `menu-bar-buffers-menu-command-entries'
2198 ;; if it's been set already. Note that we can't use constant
2199 ;; lists for the menu-entries, because the low-level menu-code
2200 ;; modifies them.
2201 (unless menu-bar-buffers-menu-command-entries
2202 (setq menu-bar-buffers-menu-command-entries
2203 (list '(command-separator "--")
2204 (list 'next-buffer
2205 'menu-item
2206 "Next Buffer"
2207 'next-buffer
2208 :help "Switch to the \"next\" buffer in a cyclic order")
2209 (list 'previous-buffer
2210 'menu-item
2211 "Previous Buffer"
2212 'previous-buffer
2213 :help "Switch to the \"previous\" buffer in a cyclic order")
2214 (list 'select-named-buffer
2215 'menu-item
2216 "Select Named Buffer..."
2217 'switch-to-buffer
2218 :help "Prompt for a buffer name, and select that buffer in the current window")
2219 (list 'list-all-buffers
2220 'menu-item
2221 "List All Buffers"
2222 'list-buffers
2223 :help "Pop up a window listing all Emacs buffers"
2224 ))))
2225 (setq buffers-menu
2226 (nconc buffers-menu menu-bar-buffers-menu-command-entries))
2228 ;; We used to "(define-key (current-global-map) [menu-bar buffer]"
2229 ;; but that did not do the right thing when the [menu-bar buffer]
2230 ;; entry above had been moved (e.g. to a parent keymap).
2231 (setcdr global-buffers-menu-map (cons "Buffers" buffers-menu)))))
2233 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
2235 (menu-bar-update-buffers)
2237 ;; this version is too slow
2238 ;;(defun format-buffers-menu-line (buffer)
2239 ;; "Returns a string to represent the given buffer in the Buffer menu.
2240 ;;nil means the buffer shouldn't be listed. You can redefine this."
2241 ;; (if (string-match "\\` " (buffer-name buffer))
2242 ;; nil
2243 ;; (with-current-buffer buffer
2244 ;; (let ((size (buffer-size)))
2245 ;; (format "%s%s %-19s %6s %-15s %s"
2246 ;; (if (buffer-modified-p) "*" " ")
2247 ;; (if buffer-read-only "%" " ")
2248 ;; (buffer-name)
2249 ;; size
2250 ;; mode-name
2251 ;; (or (buffer-file-name) ""))))))
2253 ;;; Set up a menu bar menu for the minibuffer.
2255 (dolist (map (list minibuffer-local-map
2256 ;; This shouldn't be necessary, but there's a funny
2257 ;; bug in keymap.c that I don't understand yet. -stef
2258 minibuffer-local-completion-map))
2259 (bindings--define-key map [menu-bar minibuf]
2260 (cons "Minibuf" (make-sparse-keymap "Minibuf"))))
2262 (let ((map minibuffer-local-completion-map))
2263 (bindings--define-key map [menu-bar minibuf ?\?]
2264 '(menu-item "List Completions" minibuffer-completion-help
2265 :help "Display all possible completions"))
2266 (bindings--define-key map [menu-bar minibuf space]
2267 '(menu-item "Complete Word" minibuffer-complete-word
2268 :help "Complete at most one word"))
2269 (bindings--define-key map [menu-bar minibuf tab]
2270 '(menu-item "Complete" minibuffer-complete
2271 :help "Complete as far as possible")))
2273 (let ((map minibuffer-local-map))
2274 (bindings--define-key map [menu-bar minibuf quit]
2275 '(menu-item "Quit" abort-recursive-edit
2276 :help "Abort input and exit minibuffer"))
2277 (bindings--define-key map [menu-bar minibuf return]
2278 '(menu-item "Enter" exit-minibuffer
2279 :key-sequence "\r"
2280 :help "Terminate input and exit minibuffer"))
2281 (bindings--define-key map [menu-bar minibuf isearch-forward]
2282 '(menu-item "Isearch History Forward" isearch-forward
2283 :help "Incrementally search minibuffer history forward"))
2284 (bindings--define-key map [menu-bar minibuf isearch-backward]
2285 '(menu-item "Isearch History Backward" isearch-backward
2286 :help "Incrementally search minibuffer history backward"))
2287 (bindings--define-key map [menu-bar minibuf next]
2288 '(menu-item "Next History Item" next-history-element
2289 :help "Put next minibuffer history element in the minibuffer"))
2290 (bindings--define-key map [menu-bar minibuf previous]
2291 '(menu-item "Previous History Item" previous-history-element
2292 :help "Put previous minibuffer history element in the minibuffer")))
2294 (define-minor-mode menu-bar-mode
2295 "Toggle display of a menu bar on each frame (Menu Bar mode).
2296 With a prefix argument ARG, enable Menu Bar mode if ARG is
2297 positive, and disable it otherwise. If called from Lisp, enable
2298 Menu Bar mode if ARG is omitted or nil.
2300 This command applies to all frames that exist and frames to be
2301 created in the future."
2302 :init-value t
2303 :global t
2304 ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again.
2305 :variable menu-bar-mode
2307 ;; Turn the menu-bars on all frames on or off.
2308 (let ((val (if menu-bar-mode 1 0)))
2309 (dolist (frame (frame-list))
2310 (set-frame-parameter frame 'menu-bar-lines val))
2311 ;; If the user has given `default-frame-alist' a `menu-bar-lines'
2312 ;; parameter, replace it.
2313 (if (assq 'menu-bar-lines default-frame-alist)
2314 (setq default-frame-alist
2315 (cons (cons 'menu-bar-lines val)
2316 (assq-delete-all 'menu-bar-lines
2317 default-frame-alist)))))
2318 ;; Make the message appear when Emacs is idle. We can not call message
2319 ;; directly. The minor-mode message "Menu Bar mode disabled" comes
2320 ;; after this function returns, overwriting any message we do here.
2321 (when (and (called-interactively-p 'interactive) (not menu-bar-mode))
2322 (run-with-idle-timer 0 nil 'message
2323 "Menu Bar mode disabled. Use M-x menu-bar-mode to make the menu bar appear.")))
2325 ;;;###autoload
2326 ;; (This does not work right unless it comes after the above definition.)
2327 ;; This comment is taken from tool-bar.el near
2328 ;; (put 'tool-bar-mode ...)
2329 ;; We want to pretend the menu bar by standard is on, as this will make
2330 ;; customize consider disabling the menu bar a customization, and save
2331 ;; that. We could do this for real by setting :init-value above, but
2332 ;; that would overwrite disabling the menu bar from X resources.
2333 (put 'menu-bar-mode 'standard-value '(t))
2335 (defun toggle-menu-bar-mode-from-frame (&optional arg)
2336 "Toggle menu bar on or off, based on the status of the current frame.
2337 See `menu-bar-mode' for more information."
2338 (interactive (list (or current-prefix-arg 'toggle)))
2339 (if (eq arg 'toggle)
2340 (menu-bar-mode
2341 (if (menu-bar-positive-p
2342 (frame-parameter (menu-bar-frame-for-menubar) 'menu-bar-lines))
2343 0 1))
2344 (menu-bar-mode arg)))
2346 (declare-function x-menu-bar-open "term/x-win" (&optional frame))
2347 (declare-function w32-menu-bar-open "term/w32-win" (&optional frame))
2349 (defun lookup-key-ignore-too-long (map key)
2350 "Call `lookup-key' and convert numeric values to nil."
2351 (let ((binding (lookup-key map key)))
2352 (if (numberp binding) ; `too long'
2354 binding)))
2356 (defun popup-menu (menu &optional position prefix from-menu-bar)
2357 "Popup the given menu and call the selected option.
2358 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
2359 `x-popup-menu'.
2360 The menu is shown at the place where POSITION specifies.
2361 For the form of POSITION, see `popup-menu-normalize-position'.
2362 PREFIX is the prefix argument (if any) to pass to the command.
2363 FROM-MENU-BAR, if non-nil, means we are dropping one of menu-bar's menus."
2364 (let* ((map (cond
2365 ((keymapp menu) menu)
2366 ((and (listp menu) (keymapp (car menu))) menu)
2367 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
2368 (filter (when (symbolp map)
2369 (plist-get (get map 'menu-prop) :filter))))
2370 (if filter (funcall filter (symbol-function map)) map)))))
2371 (frame (selected-frame))
2372 event cmd)
2373 (if from-menu-bar
2374 (let* ((xy (posn-x-y position))
2375 (menu-symbol (menu-bar-menu-at-x-y (car xy) (cdr xy))))
2376 (setq position (list menu-symbol (list frame '(menu-bar)
2377 xy 0))))
2378 (setq position (popup-menu-normalize-position position)))
2379 ;; The looping behavior was taken from lmenu's popup-menu-popup
2380 (while (and map (setq event
2381 ;; map could be a prefix key, in which case
2382 ;; we need to get its function cell
2383 ;; definition.
2384 (x-popup-menu position (indirect-function map))))
2385 ;; Strangely x-popup-menu returns a list.
2386 ;; mouse-major-mode-menu was using a weird:
2387 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
2388 (setq cmd
2389 (cond
2390 ((and from-menu-bar
2391 (consp event)
2392 (numberp (car event))
2393 (numberp (cdr event)))
2394 (let ((x (car event))
2395 (y (cdr event))
2396 menu-symbol)
2397 (setq menu-symbol (menu-bar-menu-at-x-y x y))
2398 (setq position (list menu-symbol (list frame '(menu-bar)
2399 event 0)))
2400 (setq map
2401 (key-binding (vector 'menu-bar menu-symbol)))))
2402 ((and (not (keymapp map)) (listp map))
2403 ;; We were given a list of keymaps. Search them all
2404 ;; in sequence until a first binding is found.
2405 (let ((mouse-click (apply 'vector event))
2406 binding)
2407 (while (and map (null binding))
2408 (setq binding (lookup-key-ignore-too-long (car map) mouse-click))
2409 (setq map (cdr map)))
2410 binding))
2412 ;; We were given a single keymap.
2413 (lookup-key map (apply 'vector event)))))
2414 ;; Clear out echoing, which perhaps shows a prefix arg.
2415 (message "")
2416 ;; Maybe try again but with the submap.
2417 (setq map (if (keymapp cmd) cmd)))
2418 ;; If the user did not cancel by refusing to select,
2419 ;; and if the result is a command, run it.
2420 (when (and (null map) (commandp cmd))
2421 (setq prefix-arg prefix)
2422 ;; `setup-specified-language-environment', for instance,
2423 ;; expects this to be set from a menu keymap.
2424 (setq last-command-event (car (last event)))
2425 ;; mouse-major-mode-menu was using `command-execute' instead.
2426 (call-interactively cmd))))
2428 (defun popup-menu-normalize-position (position)
2429 "Convert the POSITION to the form which `popup-menu' expects internally.
2430 POSITION can be an event, a posn- value, a value having the
2431 form ((XOFFSET YOFFSET) WINDOW), or nil.
2432 If nil, the current mouse position is used, or nil if there is no mouse."
2433 (pcase position
2434 ;; nil -> mouse cursor position
2435 (`nil
2436 (let ((mp (mouse-pixel-position)))
2437 (list (list (cadr mp) (cddr mp)) (car mp))))
2438 ;; Value returned from `event-end' or `posn-at-point'.
2439 ((pred posnp)
2440 (let ((xy (posn-x-y position)))
2441 (list (list (car xy) (cdr xy))
2442 (posn-window position))))
2443 ;; Event.
2444 ((pred eventp)
2445 (popup-menu-normalize-position (event-end position)))
2446 (_ position)))
2448 (defcustom tty-menu-open-use-tmm nil
2449 "If non-nil, \\[menu-bar-open] on a TTY will invoke `tmm-menubar'.
2451 If nil, \\[menu-bar-open] will drop down the menu corresponding to the
2452 first (leftmost) menu-bar item; you can select other items by typing
2453 \\[forward-char], \\[backward-char], \\[right-char] and \\[left-char]."
2454 :type '(choice (const :tag "F10 drops down TTY menus" nil)
2455 (const :tag "F10 invokes tmm-menubar" t))
2456 :group 'display
2457 :version "24.4")
2459 (defvar tty-menu--initial-menu-x 1
2460 "X coordinate of the first menu-bar menu dropped by F10.
2462 This is meant to be used only for debugging TTY menus.")
2464 (defun menu-bar-open (&optional frame)
2465 "Start key navigation of the menu bar in FRAME.
2467 This function decides which method to use to access the menu
2468 depending on FRAME's terminal device. On X displays, it calls
2469 `x-menu-bar-open'; on Windows, `w32-menu-bar-open'; otherwise it
2470 calls either `popup-menu' or `tmm-menubar' depending on whether
2471 `tty-menu-open-use-tmm' is nil or not.
2473 If FRAME is nil or not given, use the selected frame."
2474 (interactive)
2475 (let ((type (framep (or frame (selected-frame)))))
2476 (cond
2477 ((eq type 'x) (x-menu-bar-open frame))
2478 ((eq type 'w32) (w32-menu-bar-open frame))
2479 ((and (null tty-menu-open-use-tmm)
2480 (not (zerop (or (frame-parameter nil 'menu-bar-lines) 0))))
2481 ;; Make sure the menu bar is up to date. One situation where
2482 ;; this is important is when this function is invoked by name
2483 ;; via M-x, in which case the menu bar includes the "Minibuf"
2484 ;; menu item that should be removed when we exit the minibuffer.
2485 (force-mode-line-update)
2486 (redisplay)
2487 (let* ((x tty-menu--initial-menu-x)
2488 (menu (menu-bar-menu-at-x-y x 0 frame)))
2489 (popup-menu (or
2490 (lookup-key-ignore-too-long
2491 global-map (vector 'menu-bar menu))
2492 (lookup-key-ignore-too-long
2493 (current-local-map) (vector 'menu-bar menu))
2494 (cdar (minor-mode-key-binding (vector 'menu-bar menu)))
2495 (mouse-menu-bar-map))
2496 (posn-at-x-y x 0 nil t) nil t)))
2497 (t (with-selected-frame (or frame (selected-frame))
2498 (tmm-menubar))))))
2500 (global-set-key [f10] 'menu-bar-open)
2502 (defun buffer-menu-open ()
2503 "Start key navigation of the buffer menu.
2504 This is the keyboard interface to \\[mouse-buffer-menu]."
2505 (interactive)
2506 (popup-menu (mouse-buffer-menu-keymap)
2507 (posn-at-x-y 0 0 nil t)))
2509 (global-set-key [C-f10] 'buffer-menu-open)
2511 (defun mouse-buffer-menu-keymap ()
2512 (let* ((menu (mouse-buffer-menu-map))
2513 (km (make-sparse-keymap (pop menu))))
2514 (dolist (item (nreverse menu))
2515 (let* ((name (pop item)))
2516 (define-key km (vector (intern name))
2517 (list name 'keymap name
2518 (menu-bar-buffer-vector item)))))
2519 km))
2521 (defvar tty-menu-navigation-map
2522 (let ((map (make-sparse-keymap)))
2523 ;; The next line is disabled because it breaks interpretation of
2524 ;; escape sequences, produced by TTY arrow keys, as tty-menu-*
2525 ;; commands. Instead, we explicitly bind some keys to
2526 ;; tty-menu-exit.
2527 ;;(define-key map [t] 'tty-menu-exit)
2529 ;; The tty-menu-* are just symbols interpreted by term.c, they are
2530 ;; not real commands.
2531 (dolist (bind '((keyboard-quit . tty-menu-exit)
2532 (keyboard-escape-quit . tty-menu-exit)
2533 ;; The following two will need to be revised if we ever
2534 ;; support a right-to-left menu bar.
2535 (forward-char . tty-menu-next-menu)
2536 (backward-char . tty-menu-prev-menu)
2537 (right-char . tty-menu-next-menu)
2538 (left-char . tty-menu-prev-menu)
2539 (next-line . tty-menu-next-item)
2540 (previous-line . tty-menu-prev-item)
2541 (newline . tty-menu-select)
2542 (newline-and-indent . tty-menu-select)
2543 (menu-bar-open . tty-menu-exit)))
2544 (substitute-key-definition (car bind) (cdr bind)
2545 map (current-global-map)))
2547 ;; The bindings of menu-bar items are so that clicking on the menu
2548 ;; bar when a menu is already shown pops down that menu.
2549 (define-key map [menu-bar t] 'tty-menu-exit)
2551 (define-key map [?\C-r] 'tty-menu-select)
2552 (define-key map [?\C-j] 'tty-menu-select)
2553 (define-key map [return] 'tty-menu-select)
2554 (define-key map [linefeed] 'tty-menu-select)
2555 (define-key map [mouse-1] 'tty-menu-select)
2556 (define-key map [drag-mouse-1] 'tty-menu-select)
2557 (define-key map [mouse-2] 'tty-menu-select)
2558 (define-key map [drag-mouse-2] 'tty-menu-select)
2559 (define-key map [mouse-3] 'tty-menu-select)
2560 (define-key map [drag-mouse-3] 'tty-menu-select)
2561 (define-key map [wheel-down] 'tty-menu-next-item)
2562 (define-key map [wheel-up] 'tty-menu-prev-item)
2563 (define-key map [wheel-left] 'tty-menu-prev-menu)
2564 (define-key map [wheel-right] 'tty-menu-next-menu)
2565 ;; The following 4 bindings are for those whose text-mode mouse
2566 ;; lack the wheel.
2567 (define-key map [S-mouse-1] 'tty-menu-next-item)
2568 (define-key map [S-drag-mouse-1] 'tty-menu-next-item)
2569 (define-key map [S-mouse-2] 'tty-menu-prev-item)
2570 (define-key map [S-drag-mouse-2] 'tty-menu-prev-item)
2571 (define-key map [S-mouse-3] 'tty-menu-prev-item)
2572 (define-key map [S-drag-mouse-3] 'tty-menu-prev-item)
2573 (define-key map [header-line mouse-1] 'tty-menu-select)
2574 (define-key map [header-line drag-mouse-1] 'tty-menu-select)
2575 ;; The down-mouse events must be bound to tty-menu-ignore, so that
2576 ;; only releasing the mouse button pops up the menu.
2577 (define-key map [mode-line down-mouse-1] 'tty-menu-ignore)
2578 (define-key map [mode-line down-mouse-2] 'tty-menu-ignore)
2579 (define-key map [mode-line down-mouse-3] 'tty-menu-ignore)
2580 (define-key map [mode-line C-down-mouse-1] 'tty-menu-ignore)
2581 (define-key map [mode-line C-down-mouse-2] 'tty-menu-ignore)
2582 (define-key map [mode-line C-down-mouse-3] 'tty-menu-ignore)
2583 (define-key map [down-mouse-1] 'tty-menu-ignore)
2584 (define-key map [C-down-mouse-1] 'tty-menu-ignore)
2585 (define-key map [C-down-mouse-2] 'tty-menu-ignore)
2586 (define-key map [C-down-mouse-3] 'tty-menu-ignore)
2587 (define-key map [mouse-movement] 'tty-menu-mouse-movement)
2588 map)
2589 "Keymap used while processing TTY menus.")
2591 (provide 'menu-bar)
2593 ;;; menu-bar.el ends here