]> code.delx.au - gnu-emacs/blob - lisp/menu-bar.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / menu-bar.el
1 ;;; menu-bar.el --- define a default menu bar
2
3 ;; Copyright (C) 1993-1995, 2000-2016 Free Software Foundation, Inc.
4
5 ;; Author: Richard M. Stallman
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal, mouse
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;; Avishai Yacobi suggested some menu rearrangements.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; This is referenced by some code below; it is defined in uniquify.el
32 (defvar uniquify-buffer-name-style)
33
34 ;; From emulation/cua-base.el; used below
35 (defvar cua-enable-cua-keys)
36
37
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")))
42
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))
47
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"))
51
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")))
57
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"))
62
63 (bindings--define-key menu [separator-exit]
64 menu-bar-separator)
65
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"))
81
82 (bindings--define-key menu [separator-frame]
83 menu-bar-separator)
84
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"))
89
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"))
95
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"))
101
102 (bindings--define-key menu [separator-window]
103 menu-bar-separator)
104
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"))
131
132 (bindings--define-key menu [separator-print]
133 menu-bar-separator)
134
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)
143 nil
144 (concat "\\`"
145 (regexp-quote
146 (file-name-nondirectory
147 auto-save-list-file-prefix)))
148 t))
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"))
173
174 (bindings--define-key menu [separator-save]
175 menu-bar-separator)
176
177
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"))
198
199 menu))
200
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))))
210
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.")
214
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)))
225 (t
226 (error "No previous search"))))
227
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)))
238 (t
239 (error "No previous search"))))
240
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))))
253
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))
258
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)))
267
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)))
276
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))
293
294 (defvar menu-bar-search-menu
295 (let ((menu (make-sparse-keymap "Search")))
296
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)
301
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)
309
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)
328
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"))
337
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))
346
347 ;; The Edit->Replace submenu
348
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)
359
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))
369
370 ;;; Assemble the top-level Edit menu items.
371 (defvar menu-bar-goto-menu
372 (let ((menu (make-sparse-keymap "Go To")))
373
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"))
378
379 (bindings--define-key menu [separator-tag-file]
380 '(menu-item "--" nil :visible (menu-bar-goto-uses-etags-p)))
381
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"))
387
388 (bindings--define-key menu [xref-apropos]
389 '(menu-item "Find Apropos..." xref-find-apropos
390 :help "Find function/variables whose names match regexp"))
391
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"))
399
400 (bindings--define-key menu [separator-xref]
401 menu-bar-separator)
402
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))
414
415 (defun menu-bar-goto-uses-etags-p ()
416 (or (not (boundp 'xref-backend-functions))
417 (eq (car xref-backend-functions) 'etags--xref-backend)))
418
419 (defvar yank-menu (cons (purecopy "Select Yank") nil))
420 (fset 'yank-menu (cons 'keymap yank-menu))
421
422 (defvar menu-bar-edit-menu
423 (let ((menu (make-sparse-keymap "Edit")))
424
425 (bindings--define-key menu [props]
426 `(menu-item "Text Properties" facemenu-menu))
427
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)))
432
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"))
438
439 (bindings--define-key menu [separator-bookmark]
440 menu-bar-separator)
441
442 (bindings--define-key menu [bookmark]
443 `(menu-item "Bookmarks" menu-bar-bookmark-map))
444
445 (bindings--define-key menu [goto]
446 `(menu-item "Go To" ,menu-bar-goto-menu))
447
448 (bindings--define-key menu [replace]
449 `(menu-item "Replace" ,menu-bar-replace-menu))
450
451 (bindings--define-key menu [search]
452 `(menu-item "Search" ,menu-bar-search-menu))
453
454 (bindings--define-key menu [separator-search]
455 menu-bar-separator)
456
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"))
466
467
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))
506
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"))
515
516 menu))
517
518 (define-obsolete-function-alias
519 'menu-bar-kill-ring-save 'kill-ring-save "24.1")
520
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.
523
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)))))
532
533 (defvar gui-select-enable-clipboard)
534
535 (defun clipboard-yank ()
536 "Insert the clipboard contents, or the last stretch of killed text."
537 (interactive "*")
538 (let ((gui-select-enable-clipboard t)
539 (interprogram-paste-function (or interprogram-paste-function
540 #'gui-selection-value)))
541 (yank)))
542
543 (defun clipboard-kill-ring-save (beg end &optional region)
544 "Copy region to kill ring, and save in the GUI's clipboard."
545 (interactive "r\np")
546 (let ((gui-select-enable-clipboard t)
547 (interprogram-cut-function (or interprogram-cut-function
548 #'gui-select-text)))
549 (kill-ring-save beg end region)))
550
551 (defun clipboard-kill-region (beg end &optional region)
552 "Kill the region, and save it in the GUI's clipboard."
553 (interactive "r\np")
554 (let ((gui-select-enable-clipboard t)
555 (interprogram-cut-function (or interprogram-cut-function
556 #'gui-select-text)))
557 (kill-region beg end region)))
558
559 (defun menu-bar-enable-clipboard ()
560 "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard.
561 Do the same for the keys of the same name."
562 (interactive)
563 ;; These are Sun server keysyms for the Cut, Copy and Paste keys
564 ;; (also for XFree86 on Sun keyboard):
565 (define-key global-map [f20] 'clipboard-kill-region)
566 (define-key global-map [f16] 'clipboard-kill-ring-save)
567 (define-key global-map [f18] 'clipboard-yank)
568 ;; X11R6 versions:
569 (define-key global-map [cut] 'clipboard-kill-region)
570 (define-key global-map [copy] 'clipboard-kill-ring-save)
571 (define-key global-map [paste] 'clipboard-yank))
572 \f
573 ;; The "Options" menu items
574
575 (defvar menu-bar-custom-menu
576 (let ((menu (make-sparse-keymap "Customize")))
577
578 (bindings--define-key menu [customize-apropos-faces]
579 '(menu-item "Faces Matching..." customize-apropos-faces
580 :help "Browse faces matching a regexp or word list"))
581 (bindings--define-key menu [customize-apropos-options]
582 '(menu-item "Options Matching..." customize-apropos-options
583 :help "Browse options matching a regexp or word list"))
584 (bindings--define-key menu [customize-apropos]
585 '(menu-item "All Settings Matching..." customize-apropos
586 :help "Browse customizable settings matching a regexp or word list"))
587 (bindings--define-key menu [separator-1]
588 menu-bar-separator)
589 (bindings--define-key menu [customize-group]
590 '(menu-item "Specific Group..." customize-group
591 :help "Customize settings of specific group"))
592 (bindings--define-key menu [customize-face]
593 '(menu-item "Specific Face..." customize-face
594 :help "Customize attributes of specific face"))
595 (bindings--define-key menu [customize-option]
596 '(menu-item "Specific Option..." customize-option
597 :help "Customize value of specific option"))
598 (bindings--define-key menu [separator-2]
599 menu-bar-separator)
600 (bindings--define-key menu [customize-changed-options]
601 '(menu-item "New Options..." customize-changed-options
602 :help "Options added or changed in recent Emacs versions"))
603 (bindings--define-key menu [customize-saved]
604 '(menu-item "Saved Options" customize-saved
605 :help "Customize previously saved options"))
606 (bindings--define-key menu [separator-3]
607 menu-bar-separator)
608 (bindings--define-key menu [customize-browse]
609 '(menu-item "Browse Customization Groups" customize-browse
610 :help "Browse all customization groups"))
611 (bindings--define-key menu [customize]
612 '(menu-item "Top-level Customization Group" customize
613 :help "The master group called `Emacs'"))
614 (bindings--define-key menu [customize-themes]
615 '(menu-item "Custom Themes" customize-themes
616 :help "Choose a pre-defined customization theme"))
617 menu))
618 ;(defvar menu-bar-preferences-menu (make-sparse-keymap "Preferences"))
619
620 (defmacro menu-bar-make-mm-toggle (fname doc help &optional props)
621 "Make a menu-item for a global minor mode toggle.
622 FNAME is the minor mode's name (variable and function).
623 DOC is the text to use for the menu entry.
624 HELP is the text to use for the tooltip.
625 PROPS are additional properties."
626 `'(menu-item ,doc ,fname
627 ,@props
628 :help ,help
629 :button (:toggle . (and (default-boundp ',fname)
630 (default-value ',fname)))))
631
632 (defmacro menu-bar-make-toggle (name variable doc message help &rest body)
633 `(progn
634 (defun ,name (&optional interactively)
635 ,(concat "Toggle whether to " (downcase (substring help 0 1))
636 (substring help 1) ".
637 In an interactive call, record this option as a candidate for saving
638 by \"Save Options\" in Custom buffers.")
639 (interactive "p")
640 (if ,(if body `(progn . ,body)
641 `(progn
642 (custom-load-symbol ',variable)
643 (let ((set (or (get ',variable 'custom-set) 'set-default))
644 (get (or (get ',variable 'custom-get) 'default-value)))
645 (funcall set ',variable (not (funcall get ',variable))))))
646 (message ,message "enabled globally")
647 (message ,message "disabled globally"))
648 ;; The function `customize-mark-as-set' must only be called when
649 ;; a variable is set interactively, as the purpose is to mark it as
650 ;; a candidate for "Save Options", and we do not want to save options
651 ;; the user have already set explicitly in his init file.
652 (if interactively (customize-mark-as-set ',variable)))
653 '(menu-item ,doc ,name
654 :help ,help
655 :button (:toggle . (and (default-boundp ',variable)
656 (default-value ',variable))))))
657
658 ;; Function for setting/saving default font.
659
660 (defun menu-set-font ()
661 "Interactively select a font and make it the default on all frames.
662
663 The selected font will be the default on both the existing and future frames."
664 (interactive)
665 (set-frame-font (if (fboundp 'x-select-font)
666 (x-select-font)
667 (mouse-select-font))
668 nil t))
669
670 (defun menu-bar-options-save ()
671 "Save current values of Options menu items using Custom."
672 (interactive)
673 (let ((need-save nil))
674 ;; These are set with menu-bar-make-mm-toggle, which does not
675 ;; put on a customized-value property.
676 (dolist (elt '(line-number-mode column-number-mode size-indication-mode
677 cua-mode show-paren-mode transient-mark-mode
678 blink-cursor-mode display-time-mode display-battery-mode
679 ;; These are set by other functions that don't set
680 ;; the customized state. Having them here has the
681 ;; side-effect that turning them off via X
682 ;; resources acts like having customized them, but
683 ;; that seems harmless.
684 menu-bar-mode tool-bar-mode))
685 ;; FIXME ? It's a little annoying that running this command
686 ;; always loads cua-base, paren, time, and battery, even if they
687 ;; have not been customized in any way. (Due to custom-load-symbol.)
688 (and (customize-mark-to-save elt)
689 (setq need-save t)))
690 ;; These are set with `customize-set-variable'.
691 (dolist (elt '(scroll-bar-mode
692 debug-on-quit debug-on-error
693 ;; Somehow this works, when tool-bar and menu-bar don't.
694 tooltip-mode window-divider-mode
695 save-place uniquify-buffer-name-style fringe-mode
696 indicate-empty-lines indicate-buffer-boundaries
697 case-fold-search font-use-system-font
698 current-language-environment default-input-method
699 ;; Saving `text-mode-hook' is somewhat questionable,
700 ;; as we might get more than we bargain for, if
701 ;; other code may has added hooks as well.
702 ;; Nonetheless, not saving it would like be confuse
703 ;; more often.
704 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2002-02-11.
705 text-mode-hook tool-bar-position))
706 (and (get elt 'customized-value)
707 (customize-mark-to-save elt)
708 (setq need-save t)))
709 (when (get 'default 'customized-face)
710 (put 'default 'saved-face (get 'default 'customized-face))
711 (put 'default 'customized-face nil)
712 (setq need-save t))
713 ;; Save if we changed anything.
714 (when need-save
715 (custom-save-all))))
716
717
718 ;;; Assemble all the top-level items of the "Options" menu
719
720 ;; The "Show/Hide" submenu of menu "Options"
721
722 (defun menu-bar-window-divider-customize ()
723 "Show customization buffer for `window-divider' group."
724 (interactive)
725 (customize-group 'window-divider))
726
727 (defun menu-bar-bottom-and-right-window-divider ()
728 "Display dividers on the bottom and right of each window."
729 (interactive)
730 (customize-set-variable 'window-divider-default-places t)
731 (window-divider-mode 1))
732
733 (defun menu-bar-right-window-divider ()
734 "Display dividers only on the right of each window."
735 (interactive)
736 (customize-set-variable 'window-divider-default-places 'right-only)
737 (window-divider-mode 1))
738
739 (defun menu-bar-bottom-window-divider ()
740 "Display dividers only at the bottom of each window."
741 (interactive)
742 (customize-set-variable 'window-divider-default-places 'bottom-only)
743 (window-divider-mode 1))
744
745 (defun menu-bar-no-window-divider ()
746 "Do not display window dividers."
747 (interactive)
748 (window-divider-mode -1))
749
750 ;; For the radio buttons below we check whether the respective dividers
751 ;; are displayed on the selected frame. This is not fully congruent
752 ;; with `window-divider-mode' but makes the menu entries work also when
753 ;; dividers are displayed by manipulating frame parameters directly.
754 (defvar menu-bar-showhide-window-divider-menu
755 (let ((menu (make-sparse-keymap "Window Divider")))
756 (bindings--define-key menu [customize]
757 '(menu-item "Customize" menu-bar-window-divider-customize
758 :help "Customize window dividers"
759 :visible (memq (window-system) '(x w32))))
760
761 (bindings--define-key menu [bottom-and-right]
762 '(menu-item "Bottom and Right"
763 menu-bar-bottom-and-right-window-divider
764 :help "Display window divider on the bottom and right of each window"
765 :visible (memq (window-system) '(x w32))
766 :button (:radio
767 . (and (window-divider-width-valid-p
768 (cdr (assq 'bottom-divider-width
769 (frame-parameters))))
770 (window-divider-width-valid-p
771 (cdr (assq 'right-divider-width
772 (frame-parameters))))))))
773 (bindings--define-key menu [right-only]
774 '(menu-item "Right Only"
775 menu-bar-right-window-divider
776 :help "Display window divider on the right of each window only"
777 :visible (memq (window-system) '(x w32))
778 :button (:radio
779 . (and (not (window-divider-width-valid-p
780 (cdr (assq 'bottom-divider-width
781 (frame-parameters)))))
782 (window-divider-width-valid-p
783 (cdr (assq 'right-divider-width
784 (frame-parameters))))))))
785 (bindings--define-key menu [bottom-only]
786 '(menu-item "Bottom Only"
787 menu-bar-bottom-window-divider
788 :help "Display window divider on the bottom of each window only"
789 :visible (memq (window-system) '(x w32))
790 :button (:radio
791 . (and (window-divider-width-valid-p
792 (cdr (assq 'bottom-divider-width
793 (frame-parameters))))
794 (not (window-divider-width-valid-p
795 (cdr (assq 'right-divider-width
796 (frame-parameters)))))))))
797 (bindings--define-key menu [no-divider]
798 '(menu-item "None"
799 menu-bar-no-window-divider
800 :help "Do not display window dividers"
801 :visible (memq (window-system) '(x w32))
802 :button (:radio
803 . (and (not (window-divider-width-valid-p
804 (cdr (assq 'bottom-divider-width
805 (frame-parameters)))))
806 (not (window-divider-width-valid-p
807 (cdr (assq 'right-divider-width
808 (frame-parameters)))))))))
809 menu))
810
811 (defun menu-bar-showhide-fringe-ind-customize ()
812 "Show customization buffer for `indicate-buffer-boundaries'."
813 (interactive)
814 (customize-variable 'indicate-buffer-boundaries))
815
816 (defun menu-bar-showhide-fringe-ind-mixed ()
817 "Display top and bottom indicators in opposite fringes, arrows in right."
818 (interactive)
819 (customize-set-variable 'indicate-buffer-boundaries
820 '((t . right) (top . left))))
821
822 (defun menu-bar-showhide-fringe-ind-box ()
823 "Display top and bottom indicators in opposite fringes."
824 (interactive)
825 (customize-set-variable 'indicate-buffer-boundaries
826 '((top . left) (bottom . right))))
827
828 (defun menu-bar-showhide-fringe-ind-right ()
829 "Display buffer boundaries and arrows in the right fringe."
830 (interactive)
831 (customize-set-variable 'indicate-buffer-boundaries 'right))
832
833 (defun menu-bar-showhide-fringe-ind-left ()
834 "Display buffer boundaries and arrows in the left fringe."
835 (interactive)
836 (customize-set-variable 'indicate-buffer-boundaries 'left))
837
838 (defun menu-bar-showhide-fringe-ind-none ()
839 "Do not display any buffer boundary indicators."
840 (interactive)
841 (customize-set-variable 'indicate-buffer-boundaries nil))
842
843 (defvar menu-bar-showhide-fringe-ind-menu
844 (let ((menu (make-sparse-keymap "Buffer boundaries")))
845
846 (bindings--define-key menu [customize]
847 '(menu-item "Other (Customize)"
848 menu-bar-showhide-fringe-ind-customize
849 :help "Additional choices available through Custom buffer"
850 :visible (display-graphic-p)
851 :button (:radio . (not (member indicate-buffer-boundaries
852 '(nil left right
853 ((top . left) (bottom . right))
854 ((t . right) (top . left))))))))
855
856 (bindings--define-key menu [mixed]
857 '(menu-item "Opposite, Arrows Right" menu-bar-showhide-fringe-ind-mixed
858 :help
859 "Show top/bottom indicators in opposite fringes, arrows in right"
860 :visible (display-graphic-p)
861 :button (:radio . (equal indicate-buffer-boundaries
862 '((t . right) (top . left))))))
863
864 (bindings--define-key menu [box]
865 '(menu-item "Opposite, No Arrows" menu-bar-showhide-fringe-ind-box
866 :help "Show top/bottom indicators in opposite fringes, no arrows"
867 :visible (display-graphic-p)
868 :button (:radio . (equal indicate-buffer-boundaries
869 '((top . left) (bottom . right))))))
870
871 (bindings--define-key menu [right]
872 '(menu-item "In Right Fringe" menu-bar-showhide-fringe-ind-right
873 :help "Show buffer boundaries and arrows in right fringe"
874 :visible (display-graphic-p)
875 :button (:radio . (eq indicate-buffer-boundaries 'right))))
876
877 (bindings--define-key menu [left]
878 '(menu-item "In Left Fringe" menu-bar-showhide-fringe-ind-left
879 :help "Show buffer boundaries and arrows in left fringe"
880 :visible (display-graphic-p)
881 :button (:radio . (eq indicate-buffer-boundaries 'left))))
882
883 (bindings--define-key menu [none]
884 '(menu-item "No Indicators" menu-bar-showhide-fringe-ind-none
885 :help "Hide all buffer boundary indicators and arrows"
886 :visible (display-graphic-p)
887 :button (:radio . (eq indicate-buffer-boundaries nil))))
888 menu))
889
890 (defun menu-bar-showhide-fringe-menu-customize ()
891 "Show customization buffer for `fringe-mode'."
892 (interactive)
893 (customize-variable 'fringe-mode))
894
895 (defun menu-bar-showhide-fringe-menu-customize-reset ()
896 "Reset the fringe mode: display fringes on both sides of a window."
897 (interactive)
898 (customize-set-variable 'fringe-mode nil))
899
900 (defun menu-bar-showhide-fringe-menu-customize-right ()
901 "Display fringes only on the right of each window."
902 (interactive)
903 (require 'fringe)
904 (customize-set-variable 'fringe-mode '(0 . nil)))
905
906 (defun menu-bar-showhide-fringe-menu-customize-left ()
907 "Display fringes only on the left of each window."
908 (interactive)
909 (require 'fringe)
910 (customize-set-variable 'fringe-mode '(nil . 0)))
911
912 (defun menu-bar-showhide-fringe-menu-customize-disable ()
913 "Do not display window fringes."
914 (interactive)
915 (require 'fringe)
916 (customize-set-variable 'fringe-mode 0))
917
918 (defvar menu-bar-showhide-fringe-menu
919 (let ((menu (make-sparse-keymap "Fringe")))
920
921 (bindings--define-key menu [showhide-fringe-ind]
922 `(menu-item "Buffer Boundaries" ,menu-bar-showhide-fringe-ind-menu
923 :visible (display-graphic-p)
924 :help "Indicate buffer boundaries in fringe"))
925
926 (bindings--define-key menu [indicate-empty-lines]
927 (menu-bar-make-toggle toggle-indicate-empty-lines indicate-empty-lines
928 "Empty Line Indicators"
929 "Indicating of empty lines %s"
930 "Indicate trailing empty lines in fringe, globally"))
931
932 (bindings--define-key menu [customize]
933 '(menu-item "Customize Fringe" menu-bar-showhide-fringe-menu-customize
934 :help "Detailed customization of fringe"
935 :visible (display-graphic-p)))
936
937 (bindings--define-key menu [default]
938 '(menu-item "Default" menu-bar-showhide-fringe-menu-customize-reset
939 :help "Default width fringe on both left and right side"
940 :visible (display-graphic-p)
941 :button (:radio . (eq fringe-mode nil))))
942
943 (bindings--define-key menu [right]
944 '(menu-item "On the Right" menu-bar-showhide-fringe-menu-customize-right
945 :help "Fringe only on the right side"
946 :visible (display-graphic-p)
947 :button (:radio . (equal fringe-mode '(0 . nil)))))
948
949 (bindings--define-key menu [left]
950 '(menu-item "On the Left" menu-bar-showhide-fringe-menu-customize-left
951 :help "Fringe only on the left side"
952 :visible (display-graphic-p)
953 :button (:radio . (equal fringe-mode '(nil . 0)))))
954
955 (bindings--define-key menu [none]
956 '(menu-item "None" menu-bar-showhide-fringe-menu-customize-disable
957 :help "Turn off fringe"
958 :visible (display-graphic-p)
959 :button (:radio . (eq fringe-mode 0))))
960 menu))
961
962 (defun menu-bar-right-scroll-bar ()
963 "Display scroll bars on the right of each window."
964 (interactive)
965 (customize-set-variable 'scroll-bar-mode 'right))
966
967 (defun menu-bar-left-scroll-bar ()
968 "Display scroll bars on the left of each window."
969 (interactive)
970 (customize-set-variable 'scroll-bar-mode 'left))
971
972 (defun menu-bar-no-scroll-bar ()
973 "Turn off scroll bars."
974 (interactive)
975 (customize-set-variable 'scroll-bar-mode nil))
976
977 (defun menu-bar-horizontal-scroll-bar ()
978 "Display horizontal scroll bars on each window."
979 (interactive)
980 (customize-set-variable 'horizontal-scroll-bar-mode t))
981
982 (defun menu-bar-no-horizontal-scroll-bar ()
983 "Turn off horizontal scroll bars."
984 (interactive)
985 (customize-set-variable 'horizontal-scroll-bar-mode nil))
986
987 (defvar menu-bar-showhide-scroll-bar-menu
988 (let ((menu (make-sparse-keymap "Scroll-bar")))
989 (bindings--define-key menu [horizontal]
990 '(menu-item "Horizontal"
991 menu-bar-horizontal-scroll-bar
992 :help "Horizontal scroll bar"
993 :visible (horizontal-scroll-bars-available-p)
994 :button (:radio . (cdr (assq 'horizontal-scroll-bars
995 (frame-parameters))))))
996
997 (bindings--define-key menu [none-horizontal]
998 '(menu-item "None-horizontal"
999 menu-bar-no-horizontal-scroll-bar
1000 :help "Turn off horizontal scroll bars"
1001 :visible (horizontal-scroll-bars-available-p)
1002 :button (:radio . (not (cdr (assq 'horizontal-scroll-bars
1003 (frame-parameters)))))))
1004
1005 (bindings--define-key menu [right]
1006 '(menu-item "On the Right"
1007 menu-bar-right-scroll-bar
1008 :help "Scroll-bar on the right side"
1009 :visible (display-graphic-p)
1010 :button (:radio . (eq (cdr (assq 'vertical-scroll-bars
1011 (frame-parameters)))
1012 'right))))
1013
1014 (bindings--define-key menu [left]
1015 '(menu-item "On the Left"
1016 menu-bar-left-scroll-bar
1017 :help "Scroll-bar on the left side"
1018 :visible (display-graphic-p)
1019 :button (:radio . (eq (cdr (assq 'vertical-scroll-bars
1020 (frame-parameters)))
1021 'left))))
1022
1023 (bindings--define-key menu [none]
1024 '(menu-item "None"
1025 menu-bar-no-scroll-bar
1026 :help "Turn off scroll-bar"
1027 :visible (display-graphic-p)
1028 :button (:radio . (eq (cdr (assq 'vertical-scroll-bars
1029 (frame-parameters)))
1030 nil))))
1031 menu))
1032
1033 (defun menu-bar-frame-for-menubar ()
1034 "Return the frame suitable for updating the menu bar."
1035 (or (and (framep menu-updating-frame)
1036 menu-updating-frame)
1037 (selected-frame)))
1038
1039 (defun menu-bar-positive-p (val)
1040 "Return non-nil if VAL is a positive number."
1041 (and (numberp val)
1042 (> val 0)))
1043
1044 (defun menu-bar-set-tool-bar-position (position)
1045 (customize-set-variable 'tool-bar-mode t)
1046 (customize-set-variable 'tool-bar-position position))
1047 (defun menu-bar-showhide-tool-bar-menu-customize-disable ()
1048 "Do not display tool bars."
1049 (interactive)
1050 (customize-set-variable 'tool-bar-mode nil))
1051 (defun menu-bar-showhide-tool-bar-menu-customize-enable-left ()
1052 "Display tool bars on the left side."
1053 (interactive)
1054 (menu-bar-set-tool-bar-position 'left))
1055 (defun menu-bar-showhide-tool-bar-menu-customize-enable-right ()
1056 "Display tool bars on the right side."
1057 (interactive)
1058 (menu-bar-set-tool-bar-position 'right))
1059 (defun menu-bar-showhide-tool-bar-menu-customize-enable-top ()
1060 "Display tool bars on the top side."
1061 (interactive)
1062 (menu-bar-set-tool-bar-position 'top))
1063 (defun menu-bar-showhide-tool-bar-menu-customize-enable-bottom ()
1064 "Display tool bars on the bottom side."
1065 (interactive)
1066 (menu-bar-set-tool-bar-position 'bottom))
1067
1068 (when (featurep 'move-toolbar)
1069 (defvar menu-bar-showhide-tool-bar-menu
1070 (let ((menu (make-sparse-keymap "Tool-bar")))
1071
1072 (bindings--define-key menu [showhide-tool-bar-left]
1073 '(menu-item "On the Left"
1074 menu-bar-showhide-tool-bar-menu-customize-enable-left
1075 :help "Tool-bar at the left side"
1076 :visible (display-graphic-p)
1077 :button
1078 (:radio . (and tool-bar-mode
1079 (eq (frame-parameter
1080 (menu-bar-frame-for-menubar)
1081 'tool-bar-position)
1082 'left)))))
1083
1084 (bindings--define-key menu [showhide-tool-bar-right]
1085 '(menu-item "On the Right"
1086 menu-bar-showhide-tool-bar-menu-customize-enable-right
1087 :help "Tool-bar at the right side"
1088 :visible (display-graphic-p)
1089 :button
1090 (:radio . (and tool-bar-mode
1091 (eq (frame-parameter
1092 (menu-bar-frame-for-menubar)
1093 'tool-bar-position)
1094 'right)))))
1095
1096 (bindings--define-key menu [showhide-tool-bar-bottom]
1097 '(menu-item "On the Bottom"
1098 menu-bar-showhide-tool-bar-menu-customize-enable-bottom
1099 :help "Tool-bar at the bottom"
1100 :visible (display-graphic-p)
1101 :button
1102 (:radio . (and tool-bar-mode
1103 (eq (frame-parameter
1104 (menu-bar-frame-for-menubar)
1105 'tool-bar-position)
1106 'bottom)))))
1107
1108 (bindings--define-key menu [showhide-tool-bar-top]
1109 '(menu-item "On the Top"
1110 menu-bar-showhide-tool-bar-menu-customize-enable-top
1111 :help "Tool-bar at the top"
1112 :visible (display-graphic-p)
1113 :button
1114 (:radio . (and tool-bar-mode
1115 (eq (frame-parameter
1116 (menu-bar-frame-for-menubar)
1117 'tool-bar-position)
1118 'top)))))
1119
1120 (bindings--define-key menu [showhide-tool-bar-none]
1121 '(menu-item "None"
1122 menu-bar-showhide-tool-bar-menu-customize-disable
1123 :help "Turn tool-bar off"
1124 :visible (display-graphic-p)
1125 :button (:radio . (eq tool-bar-mode nil))))
1126 menu)))
1127
1128 (defvar menu-bar-showhide-menu
1129 (let ((menu (make-sparse-keymap "Show/Hide")))
1130
1131 (bindings--define-key menu [column-number-mode]
1132 (menu-bar-make-mm-toggle column-number-mode
1133 "Column Numbers"
1134 "Show the current column number in the mode line"))
1135
1136 (bindings--define-key menu [line-number-mode]
1137 (menu-bar-make-mm-toggle line-number-mode
1138 "Line Numbers"
1139 "Show the current line number in the mode line"))
1140
1141 (bindings--define-key menu [size-indication-mode]
1142 (menu-bar-make-mm-toggle size-indication-mode
1143 "Size Indication"
1144 "Show the size of the buffer in the mode line"))
1145
1146 (bindings--define-key menu [linecolumn-separator]
1147 menu-bar-separator)
1148
1149 (bindings--define-key menu [showhide-battery]
1150 (menu-bar-make-mm-toggle display-battery-mode
1151 "Battery Status"
1152 "Display battery status information in mode line"))
1153
1154 (bindings--define-key menu [showhide-date-time]
1155 (menu-bar-make-mm-toggle display-time-mode
1156 "Time, Load and Mail"
1157 "Display time, system load averages and \
1158 mail status in mode line"))
1159
1160 (bindings--define-key menu [datetime-separator]
1161 menu-bar-separator)
1162
1163 (bindings--define-key menu [showhide-speedbar]
1164 '(menu-item "Speedbar" speedbar-frame-mode
1165 :help "Display a Speedbar quick-navigation frame"
1166 :button (:toggle
1167 . (and (boundp 'speedbar-frame)
1168 (frame-live-p (symbol-value 'speedbar-frame))
1169 (frame-visible-p
1170 (symbol-value 'speedbar-frame))))))
1171
1172 (bindings--define-key menu [showhide-window-divider]
1173 `(menu-item "Window Divider" ,menu-bar-showhide-window-divider-menu
1174 :visible (memq (window-system) '(x w32))))
1175
1176 (bindings--define-key menu [showhide-fringe]
1177 `(menu-item "Fringe" ,menu-bar-showhide-fringe-menu
1178 :visible (display-graphic-p)))
1179
1180 (bindings--define-key menu [showhide-scroll-bar]
1181 `(menu-item "Scroll-bar" ,menu-bar-showhide-scroll-bar-menu
1182 :visible (display-graphic-p)))
1183
1184 (bindings--define-key menu [showhide-tooltip-mode]
1185 '(menu-item "Tooltips" tooltip-mode
1186 :help "Turn tooltips on/off"
1187 :visible (and (display-graphic-p) (fboundp 'x-show-tip))
1188 :button (:toggle . tooltip-mode)))
1189
1190 (bindings--define-key menu [menu-bar-mode]
1191 '(menu-item "Menu-bar" toggle-menu-bar-mode-from-frame
1192 :help "Turn menu-bar on/off"
1193 :button
1194 (:toggle . (menu-bar-positive-p
1195 (frame-parameter (menu-bar-frame-for-menubar)
1196 'menu-bar-lines)))))
1197
1198 (if (and (boundp 'menu-bar-showhide-tool-bar-menu)
1199 (keymapp menu-bar-showhide-tool-bar-menu))
1200 (bindings--define-key menu [showhide-tool-bar]
1201 `(menu-item "Tool-bar" ,menu-bar-showhide-tool-bar-menu
1202 :visible (display-graphic-p)))
1203 ;; else not tool bar that can move.
1204 (bindings--define-key menu [showhide-tool-bar]
1205 '(menu-item "Tool-bar" toggle-tool-bar-mode-from-frame
1206 :help "Turn tool-bar on/off"
1207 :visible (display-graphic-p)
1208 :button
1209 (:toggle . (menu-bar-positive-p
1210 (frame-parameter (menu-bar-frame-for-menubar)
1211 'tool-bar-lines))))))
1212 menu))
1213
1214 (defvar menu-bar-line-wrapping-menu
1215 (let ((menu (make-sparse-keymap "Line Wrapping")))
1216
1217 (bindings--define-key menu [word-wrap]
1218 `(menu-item "Word Wrap (Visual Line mode)"
1219 ,(lambda ()
1220 (interactive)
1221 (unless visual-line-mode
1222 (visual-line-mode 1))
1223 (message "Visual-Line mode enabled"))
1224 :help "Wrap long lines at word boundaries"
1225 :button (:radio
1226 . (and (null truncate-lines)
1227 (not (truncated-partial-width-window-p))
1228 word-wrap))
1229 :visible (menu-bar-menu-frame-live-and-visible-p)))
1230
1231 (bindings--define-key menu [truncate]
1232 `(menu-item "Truncate Long Lines"
1233 ,(lambda ()
1234 (interactive)
1235 (if visual-line-mode (visual-line-mode 0))
1236 (setq word-wrap nil)
1237 (toggle-truncate-lines 1))
1238 :help "Truncate long lines at window edge"
1239 :button (:radio . (or truncate-lines
1240 (truncated-partial-width-window-p)))
1241 :visible (menu-bar-menu-frame-live-and-visible-p)
1242 :enable (not (truncated-partial-width-window-p))))
1243
1244 (bindings--define-key menu [window-wrap]
1245 `(menu-item "Wrap at Window Edge"
1246 ,(lambda () (interactive)
1247 (if visual-line-mode (visual-line-mode 0))
1248 (setq word-wrap nil)
1249 (if truncate-lines (toggle-truncate-lines -1)))
1250 :help "Wrap long lines at window edge"
1251 :button (:radio
1252 . (and (null truncate-lines)
1253 (not (truncated-partial-width-window-p))
1254 (not word-wrap)))
1255 :visible (menu-bar-menu-frame-live-and-visible-p)
1256 :enable (not (truncated-partial-width-window-p))))
1257 menu))
1258
1259 (defvar menu-bar-search-options-menu
1260 (let ((menu (make-sparse-keymap "Search Options")))
1261
1262 (dolist (x '((character-fold-to-regexp "Fold Characters" "Character folding")
1263 (isearch-symbol-regexp "Whole Symbols" "Whole symbol")
1264 (word-search-regexp "Whole Words" "Whole word")))
1265 (bindings--define-key menu (vector (nth 0 x))
1266 `(menu-item ,(nth 1 x)
1267 (lambda ()
1268 (interactive)
1269 (setq search-default-mode #',(nth 0 x))
1270 (message ,(format "%s search enabled" (nth 2 x))))
1271 :help ,(format "Enable %s search" (downcase (nth 2 x)))
1272 :button (:radio . (eq search-default-mode #',(nth 0 x))))))
1273
1274 (bindings--define-key menu [regexp-search]
1275 '(menu-item "Regular Expression"
1276 (lambda ()
1277 (interactive)
1278 (setq search-default-mode t)
1279 (message "Regular-expression search enabled"))
1280 :help "Enable regular-expression search"
1281 :button (:radio . (eq search-default-mode t))))
1282
1283 (bindings--define-key menu [regular-search]
1284 '(menu-item "Literal Search"
1285 (lambda ()
1286 (interactive)
1287 (when search-default-mode
1288 (setq search-default-mode nil)
1289 (when (symbolp search-default-mode)
1290 (message "Literal search enabled"))))
1291 :help "Disable special search modes"
1292 :button (:radio . (not search-default-mode))))
1293
1294 (bindings--define-key menu [custom-separator]
1295 menu-bar-separator)
1296 (bindings--define-key menu [case-fold-search]
1297 (menu-bar-make-toggle
1298 toggle-case-fold-search case-fold-search
1299 "Ignore Case"
1300 "Case-Insensitive Search %s"
1301 "Ignore letter-case in search commands"))
1302
1303 menu))
1304
1305 (defvar menu-bar-options-menu
1306 (let ((menu (make-sparse-keymap "Options")))
1307 (bindings--define-key menu [customize]
1308 `(menu-item "Customize Emacs" ,menu-bar-custom-menu))
1309
1310 (bindings--define-key menu [package]
1311 '(menu-item "Manage Emacs Packages" package-list-packages
1312 :help "Install or uninstall additional Emacs packages"))
1313
1314 (bindings--define-key menu [save]
1315 '(menu-item "Save Options" menu-bar-options-save
1316 :help "Save options set from the menu above"))
1317
1318 (bindings--define-key menu [custom-separator]
1319 menu-bar-separator)
1320
1321 (bindings--define-key menu [menu-set-font]
1322 '(menu-item "Set Default Font..." menu-set-font
1323 :visible (display-multi-font-p)
1324 :help "Select a default font"))
1325
1326 (if (featurep 'system-font-setting)
1327 (bindings--define-key menu [menu-system-font]
1328 (menu-bar-make-toggle
1329 toggle-use-system-font font-use-system-font
1330 "Use System Font"
1331 "Use system font: %s"
1332 "Use the monospaced font defined by the system")))
1333
1334 (bindings--define-key menu [showhide]
1335 `(menu-item "Show/Hide" ,menu-bar-showhide-menu))
1336
1337 (bindings--define-key menu [showhide-separator]
1338 menu-bar-separator)
1339
1340 (bindings--define-key menu [mule]
1341 ;; It is better not to use backquote here,
1342 ;; because that makes a bootstrapping problem
1343 ;; if you need to recompile all the Lisp files using interpreted code.
1344 `(menu-item "Multilingual Environment" ,mule-menu-keymap
1345 ;; Most of the MULE menu actually does make sense in
1346 ;; unibyte mode, e.g. language selection.
1347 ;; :visible '(default-value 'enable-multibyte-characters)
1348 ))
1349 ;;(setq menu-bar-final-items (cons 'mule menu-bar-final-items))
1350 ;;(bindings--define-key menu [preferences]
1351 ;; `(menu-item "Preferences" ,menu-bar-preferences-menu
1352 ;; :help "Toggle important global options"))
1353
1354 (bindings--define-key menu [mule-separator]
1355 menu-bar-separator)
1356
1357 (bindings--define-key menu [debug-on-quit]
1358 (menu-bar-make-toggle toggle-debug-on-quit debug-on-quit
1359 "Enter Debugger on Quit/C-g" "Debug on Quit %s"
1360 "Enter Lisp debugger when C-g is pressed"))
1361 (bindings--define-key menu [debug-on-error]
1362 (menu-bar-make-toggle toggle-debug-on-error debug-on-error
1363 "Enter Debugger on Error" "Debug on Error %s"
1364 "Enter Lisp debugger when an error is signaled"))
1365 (bindings--define-key menu [debugger-separator]
1366 menu-bar-separator)
1367
1368 (bindings--define-key menu [blink-cursor-mode]
1369 (menu-bar-make-mm-toggle
1370 blink-cursor-mode
1371 "Blink Cursor"
1372 "Whether the cursor blinks (Blink Cursor mode)"))
1373 (bindings--define-key menu [cursor-separator]
1374 menu-bar-separator)
1375
1376 (bindings--define-key menu [save-place]
1377 (menu-bar-make-toggle
1378 toggle-save-place-globally save-place
1379 "Save Place in Files between Sessions"
1380 "Saving place in files %s"
1381 "Visit files of previous session when restarting Emacs"
1382 (require 'saveplace)
1383 ;; Do it by name, to avoid a free-variable
1384 ;; warning during byte compilation.
1385 (set-default
1386 'save-place (not (symbol-value 'save-place)))))
1387
1388 (bindings--define-key menu [uniquify]
1389 (menu-bar-make-toggle
1390 toggle-uniquify-buffer-names uniquify-buffer-name-style
1391 "Use Directory Names in Buffer Names"
1392 "Directory name in buffer names (uniquify) %s"
1393 "Uniquify buffer names by adding parent directory names"
1394 (setq uniquify-buffer-name-style
1395 (if (not uniquify-buffer-name-style)
1396 'post-forward-angle-brackets))))
1397
1398 (bindings--define-key menu [edit-options-separator]
1399 menu-bar-separator)
1400 (bindings--define-key menu [cua-mode]
1401 (menu-bar-make-mm-toggle
1402 cua-mode
1403 "Use CUA Keys (Cut/Paste with C-x/C-c/C-v)"
1404 "Use C-z/C-x/C-c/C-v keys for undo/cut/copy/paste"
1405 (:visible (or (not (boundp 'cua-enable-cua-keys))
1406 cua-enable-cua-keys))))
1407
1408 (bindings--define-key menu [cua-emulation-mode]
1409 (menu-bar-make-mm-toggle
1410 cua-mode
1411 "Shift movement mark region (CUA)"
1412 "Use shifted movement keys to set and extend the region"
1413 (:visible (and (boundp 'cua-enable-cua-keys)
1414 (not cua-enable-cua-keys)))))
1415
1416 (bindings--define-key menu [search-options]
1417 `(menu-item "Default Search Options"
1418 ,menu-bar-search-options-menu))
1419
1420 (bindings--define-key menu [line-wrapping]
1421 `(menu-item "Line Wrapping in This Buffer"
1422 ,menu-bar-line-wrapping-menu))
1423
1424
1425 (bindings--define-key menu [highlight-separator]
1426 menu-bar-separator)
1427 (bindings--define-key menu [highlight-paren-mode]
1428 (menu-bar-make-mm-toggle
1429 show-paren-mode
1430 "Highlight Matching Parentheses"
1431 "Highlight matching/mismatched parentheses at cursor (Show Paren mode)"))
1432 (bindings--define-key menu [transient-mark-mode]
1433 (menu-bar-make-mm-toggle
1434 transient-mark-mode
1435 "Highlight Active Region"
1436 "Make text in active region stand out in color (Transient Mark mode)"
1437 (:enable (not cua-mode))))
1438 menu))
1439
1440 \f
1441 ;; The "Tools" menu items
1442
1443 (defvar menu-bar-games-menu
1444 (let ((menu (make-sparse-keymap "Games")))
1445
1446 (bindings--define-key menu [zone]
1447 '(menu-item "Zone Out" zone
1448 :help "Play tricks with Emacs display when Emacs is idle"))
1449 (bindings--define-key menu [tetris]
1450 '(menu-item "Tetris" tetris
1451 :help "Falling blocks game"))
1452 (bindings--define-key menu [solitaire]
1453 '(menu-item "Solitaire" solitaire
1454 :help "Get rid of all the stones"))
1455 (bindings--define-key menu [snake]
1456 '(menu-item "Snake" snake
1457 :help "Move snake around avoiding collisions"))
1458 (bindings--define-key menu [pong]
1459 '(menu-item "Pong" pong
1460 :help "Bounce the ball to your opponent"))
1461 (bindings--define-key menu [mult]
1462 '(menu-item "Multiplication Puzzle" mpuz
1463 :help "Exercise brain with multiplication"))
1464 (bindings--define-key menu [life]
1465 '(menu-item "Life" life
1466 :help "Watch how John Conway's cellular automaton evolves"))
1467 (bindings--define-key menu [hanoi]
1468 '(menu-item "Towers of Hanoi" hanoi
1469 :help "Watch Towers-of-Hanoi puzzle solved by Emacs"))
1470 (bindings--define-key menu [gomoku]
1471 '(menu-item "Gomoku" gomoku
1472 :help "Mark 5 contiguous squares (like tic-tac-toe)"))
1473 (bindings--define-key menu [bubbles]
1474 '(menu-item "Bubbles" bubbles
1475 :help "Remove all bubbles using the fewest moves"))
1476 (bindings--define-key menu [black-box]
1477 '(menu-item "Blackbox" blackbox
1478 :help "Find balls in a black box by shooting rays"))
1479 (bindings--define-key menu [adventure]
1480 '(menu-item "Adventure" dunnet
1481 :help "Dunnet, a text Adventure game for Emacs"))
1482 (bindings--define-key menu [5x5]
1483 '(menu-item "5x5" 5x5
1484 :help "Fill in all the squares on a 5x5 board"))
1485 menu))
1486
1487 (defvar menu-bar-encryption-decryption-menu
1488 (let ((menu (make-sparse-keymap "Encryption/Decryption")))
1489 (bindings--define-key menu [insert-keys]
1490 '(menu-item "Insert Keys" epa-insert-keys
1491 :help "Insert public keys after the current point"))
1492
1493 (bindings--define-key menu [export-keys]
1494 '(menu-item "Export Keys" epa-export-keys
1495 :help "Export public keys to a file"))
1496
1497 (bindings--define-key menu [import-keys-region]
1498 '(menu-item "Import Keys from Region" epa-import-keys-region
1499 :help "Import public keys from the current region"))
1500
1501 (bindings--define-key menu [import-keys]
1502 '(menu-item "Import Keys from File..." epa-import-keys
1503 :help "Import public keys from a file"))
1504
1505 (bindings--define-key menu [list-keys]
1506 '(menu-item "List Keys" epa-list-keys
1507 :help "Browse your public keyring"))
1508
1509 (bindings--define-key menu [separator-keys]
1510 menu-bar-separator)
1511
1512 (bindings--define-key menu [sign-region]
1513 '(menu-item "Sign Region" epa-sign-region
1514 :help "Create digital signature of the current region"))
1515
1516 (bindings--define-key menu [verify-region]
1517 '(menu-item "Verify Region" epa-verify-region
1518 :help "Verify digital signature of the current region"))
1519
1520 (bindings--define-key menu [encrypt-region]
1521 '(menu-item "Encrypt Region" epa-encrypt-region
1522 :help "Encrypt the current region"))
1523
1524 (bindings--define-key menu [decrypt-region]
1525 '(menu-item "Decrypt Region" epa-decrypt-region
1526 :help "Decrypt the current region"))
1527
1528 (bindings--define-key menu [separator-file]
1529 menu-bar-separator)
1530
1531 (bindings--define-key menu [sign-file]
1532 '(menu-item "Sign File..." epa-sign-file
1533 :help "Create digital signature of a file"))
1534
1535 (bindings--define-key menu [verify-file]
1536 '(menu-item "Verify File..." epa-verify-file
1537 :help "Verify digital signature of a file"))
1538
1539 (bindings--define-key menu [encrypt-file]
1540 '(menu-item "Encrypt File..." epa-encrypt-file
1541 :help "Encrypt a file"))
1542
1543 (bindings--define-key menu [decrypt-file]
1544 '(menu-item "Decrypt File..." epa-decrypt-file
1545 :help "Decrypt a file"))
1546
1547 menu))
1548
1549 (defun menu-bar-read-mail ()
1550 "Read mail using `read-mail-command'."
1551 (interactive)
1552 (call-interactively read-mail-command))
1553
1554 (defvar menu-bar-tools-menu
1555 (let ((menu (make-sparse-keymap "Tools")))
1556
1557 (bindings--define-key menu [games]
1558 `(menu-item "Games" ,menu-bar-games-menu))
1559
1560 (bindings--define-key menu [separator-games]
1561 menu-bar-separator)
1562
1563 (bindings--define-key menu [encryption-decryption]
1564 `(menu-item "Encryption/Decryption"
1565 ,menu-bar-encryption-decryption-menu))
1566
1567 (bindings--define-key menu [separator-encryption-decryption]
1568 menu-bar-separator)
1569
1570 (bindings--define-key menu [simple-calculator]
1571 '(menu-item "Simple Calculator" calculator
1572 :help "Invoke the Emacs built-in quick calculator"))
1573 (bindings--define-key menu [calc]
1574 '(menu-item "Programmable Calculator" calc
1575 :help "Invoke the Emacs built-in full scientific calculator"))
1576 (bindings--define-key menu [calendar]
1577 '(menu-item "Calendar" calendar
1578 :help "Invoke the Emacs built-in calendar"))
1579
1580 (bindings--define-key menu [separator-net]
1581 menu-bar-separator)
1582
1583 (bindings--define-key menu [browse-web]
1584 '(menu-item "Browse the Web..." browse-web))
1585 (bindings--define-key menu [directory-search]
1586 '(menu-item "Directory Search" eudc-tools-menu))
1587 (bindings--define-key menu [compose-mail]
1588 '(menu-item "Compose New Mail" compose-mail
1589 :visible (and mail-user-agent (not (eq mail-user-agent 'ignore)))
1590 :help "Start writing a new mail message"))
1591 (bindings--define-key menu [rmail]
1592 '(menu-item "Read Mail" menu-bar-read-mail
1593 :visible (and read-mail-command
1594 (not (eq read-mail-command 'ignore)))
1595 :help "Read your mail"))
1596
1597 (bindings--define-key menu [gnus]
1598 '(menu-item "Read Net News" gnus
1599 :help "Read network news groups"))
1600
1601 (bindings--define-key menu [separator-vc]
1602 menu-bar-separator)
1603
1604 (bindings--define-key menu [vc] nil) ;Create the place for the VC menu.
1605
1606 (bindings--define-key menu [separator-compare]
1607 menu-bar-separator)
1608
1609 (bindings--define-key menu [epatch]
1610 '(menu-item "Apply Patch" menu-bar-epatch-menu))
1611 (bindings--define-key menu [ediff-merge]
1612 '(menu-item "Merge" menu-bar-ediff-merge-menu))
1613 (bindings--define-key menu [compare]
1614 '(menu-item "Compare (Ediff)" menu-bar-ediff-menu))
1615
1616 (bindings--define-key menu [separator-spell]
1617 menu-bar-separator)
1618
1619 (bindings--define-key menu [spell]
1620 '(menu-item "Spell Checking" ispell-menu-map))
1621
1622 (bindings--define-key menu [separator-prog]
1623 menu-bar-separator)
1624
1625 (bindings--define-key menu [semantic]
1626 '(menu-item "Source Code Parsers (Semantic)"
1627 semantic-mode
1628 :help "Toggle automatic parsing in source code buffers (Semantic mode)"
1629 :button (:toggle . (bound-and-true-p semantic-mode))))
1630
1631 (bindings--define-key menu [ede]
1632 '(menu-item "Project Support (EDE)"
1633 global-ede-mode
1634 :help "Toggle the Emacs Development Environment (Global EDE mode)"
1635 :button (:toggle . (bound-and-true-p global-ede-mode))))
1636
1637 (bindings--define-key menu [gdb]
1638 '(menu-item "Debugger (GDB)..." gdb
1639 :help "Debug a program from within Emacs with GDB"))
1640 (bindings--define-key menu [shell-on-region]
1641 '(menu-item "Shell Command on Region..." shell-command-on-region
1642 :enable mark-active
1643 :help "Pass marked region to a shell command"))
1644 (bindings--define-key menu [shell]
1645 '(menu-item "Shell Command..." shell-command
1646 :help "Invoke a shell command and catch its output"))
1647 (bindings--define-key menu [compile]
1648 '(menu-item "Compile..." compile
1649 :help "Invoke compiler or Make, view compilation errors"))
1650 (bindings--define-key menu [grep]
1651 '(menu-item "Search Files (Grep)..." grep
1652 :help "Search files for strings or regexps (with Grep)"))
1653 menu))
1654 \f
1655 ;; The "Help" menu items
1656
1657 (defvar menu-bar-describe-menu
1658 (let ((menu (make-sparse-keymap "Describe")))
1659
1660 (bindings--define-key menu [mule-diag]
1661 '(menu-item "Show All of Mule Status" mule-diag
1662 :visible (default-value 'enable-multibyte-characters)
1663 :help "Display multilingual environment settings"))
1664 (bindings--define-key menu [describe-coding-system-briefly]
1665 '(menu-item "Describe Coding System (Briefly)"
1666 describe-current-coding-system-briefly
1667 :visible (default-value 'enable-multibyte-characters)))
1668 (bindings--define-key menu [describe-coding-system]
1669 '(menu-item "Describe Coding System..." describe-coding-system
1670 :visible (default-value 'enable-multibyte-characters)))
1671 (bindings--define-key menu [describe-input-method]
1672 '(menu-item "Describe Input Method..." describe-input-method
1673 :visible (default-value 'enable-multibyte-characters)
1674 :help "Keyboard layout for specific input method"))
1675 (bindings--define-key menu [describe-language-environment]
1676 `(menu-item "Describe Language Environment"
1677 ,describe-language-environment-map))
1678
1679 (bindings--define-key menu [separator-desc-mule]
1680 menu-bar-separator)
1681
1682 (bindings--define-key menu [list-keybindings]
1683 '(menu-item "List Key Bindings" describe-bindings
1684 :help "Display all current key bindings (keyboard shortcuts)"))
1685 (bindings--define-key menu [describe-current-display-table]
1686 '(menu-item "Describe Display Table" describe-current-display-table
1687 :help "Describe the current display table"))
1688 (bindings--define-key menu [describe-package]
1689 '(menu-item "Describe Package..." describe-package
1690 :help "Display documentation of a Lisp package"))
1691 (bindings--define-key menu [describe-face]
1692 '(menu-item "Describe Face..." describe-face
1693 :help "Display the properties of a face"))
1694 (bindings--define-key menu [describe-variable]
1695 '(menu-item "Describe Variable..." describe-variable
1696 :help "Display documentation of variable/option"))
1697 (bindings--define-key menu [describe-function]
1698 '(menu-item "Describe Function..." describe-function
1699 :help "Display documentation of function/command"))
1700 (bindings--define-key menu [describe-key-1]
1701 '(menu-item "Describe Key or Mouse Operation..." describe-key
1702 ;; Users typically don't identify keys and menu items...
1703 :help "Display documentation of command bound to a \
1704 key, a click, or a menu-item"))
1705 (bindings--define-key menu [describe-mode]
1706 '(menu-item "Describe Buffer Modes" describe-mode
1707 :help "Describe this buffer's major and minor mode"))
1708 menu))
1709
1710 (defun menu-bar-read-lispref ()
1711 "Display the Emacs Lisp Reference manual in Info mode."
1712 (interactive)
1713 (info "elisp"))
1714
1715 (defun menu-bar-read-lispintro ()
1716 "Display the Introduction to Emacs Lisp Programming in Info mode."
1717 (interactive)
1718 (info "eintr"))
1719
1720 (defun search-emacs-glossary ()
1721 "Display the Glossary node of the Emacs manual in Info mode."
1722 (interactive)
1723 (info "(emacs)Glossary"))
1724
1725 (defun emacs-index-search (topic)
1726 "Look up TOPIC in the indices of the Emacs User Manual."
1727 (interactive "sSubject to look up: ")
1728 (info "emacs")
1729 (Info-index topic))
1730
1731 (defun elisp-index-search (topic)
1732 "Look up TOPIC in the indices of the Emacs Lisp Reference Manual."
1733 (interactive "sSubject to look up: ")
1734 (info "elisp")
1735 (Info-index topic))
1736
1737 (defvar menu-bar-search-documentation-menu
1738 (let ((menu (make-sparse-keymap "Search Documentation")))
1739
1740 (bindings--define-key menu [search-documentation-strings]
1741 '(menu-item "Search Documentation Strings..." apropos-documentation
1742 :help
1743 "Find functions and variables whose doc strings match a regexp"))
1744 (bindings--define-key menu [find-any-object-by-name]
1745 '(menu-item "Find Any Object by Name..." apropos
1746 :help "Find symbols of any kind whose names match a regexp"))
1747 (bindings--define-key menu [find-option-by-value]
1748 '(menu-item "Find Options by Value..." apropos-value
1749 :help "Find variables whose values match a regexp"))
1750 (bindings--define-key menu [find-options-by-name]
1751 '(menu-item "Find Options by Name..." apropos-user-option
1752 :help "Find user options whose names match a regexp"))
1753 (bindings--define-key menu [find-commands-by-name]
1754 '(menu-item "Find Commands by Name..." apropos-command
1755 :help "Find commands whose names match a regexp"))
1756 (bindings--define-key menu [sep1]
1757 menu-bar-separator)
1758 (bindings--define-key menu [lookup-command-in-manual]
1759 '(menu-item "Look Up Command in User Manual..." Info-goto-emacs-command-node
1760 :help "Display manual section that describes a command"))
1761 (bindings--define-key menu [lookup-key-in-manual]
1762 '(menu-item "Look Up Key in User Manual..." Info-goto-emacs-key-command-node
1763 :help "Display manual section that describes a key"))
1764 (bindings--define-key menu [lookup-subject-in-elisp-manual]
1765 '(menu-item "Look Up Subject in ELisp Manual..." elisp-index-search
1766 :help "Find description of a subject in Emacs Lisp manual"))
1767 (bindings--define-key menu [lookup-subject-in-emacs-manual]
1768 '(menu-item "Look Up Subject in User Manual..." emacs-index-search
1769 :help "Find description of a subject in Emacs User manual"))
1770 (bindings--define-key menu [emacs-terminology]
1771 '(menu-item "Emacs Terminology" search-emacs-glossary
1772 :help "Display the Glossary section of the Emacs manual"))
1773 menu))
1774
1775 (defvar menu-bar-manuals-menu
1776 (let ((menu (make-sparse-keymap "More Manuals")))
1777
1778 (bindings--define-key menu [man]
1779 '(menu-item "Read Man Page..." manual-entry
1780 :help "Man-page docs for external commands and libraries"))
1781 (bindings--define-key menu [sep2]
1782 menu-bar-separator)
1783 (bindings--define-key menu [order-emacs-manuals]
1784 '(menu-item "Ordering Manuals" view-order-manuals
1785 :help "How to order manuals from the Free Software Foundation"))
1786 (bindings--define-key menu [lookup-subject-in-all-manuals]
1787 '(menu-item "Lookup Subject in all Manuals..." info-apropos
1788 :help "Find description of a subject in all installed manuals"))
1789 (bindings--define-key menu [other-manuals]
1790 '(menu-item "All Other Manuals (Info)" Info-directory
1791 :help "Read any of the installed manuals"))
1792 (bindings--define-key menu [emacs-lisp-reference]
1793 '(menu-item "Emacs Lisp Reference" menu-bar-read-lispref
1794 :help "Read the Emacs Lisp Reference manual"))
1795 (bindings--define-key menu [emacs-lisp-intro]
1796 '(menu-item "Introduction to Emacs Lisp" menu-bar-read-lispintro
1797 :help "Read the Introduction to Emacs Lisp Programming"))
1798 menu))
1799
1800 (defun help-with-tutorial-spec-language ()
1801 "Use the Emacs tutorial, specifying which language you want."
1802 (interactive)
1803 (help-with-tutorial t))
1804
1805 (defvar menu-bar-help-menu
1806 (let ((menu (make-sparse-keymap "Help")))
1807 (bindings--define-key menu [about-gnu-project]
1808 '(menu-item "About GNU" describe-gnu-project
1809 :help "About the GNU System, GNU Project, and GNU/Linux"))
1810 (bindings--define-key menu [about-emacs]
1811 '(menu-item "About Emacs" about-emacs
1812 :help "Display version number, copyright info, and basic help"))
1813 (bindings--define-key menu [sep4]
1814 menu-bar-separator)
1815 (bindings--define-key menu [describe-no-warranty]
1816 '(menu-item "(Non)Warranty" describe-no-warranty
1817 :help "Explain that Emacs has NO WARRANTY"))
1818 (bindings--define-key menu [describe-copying]
1819 '(menu-item "Copying Conditions" describe-copying
1820 :help "Show the Emacs license (GPL)"))
1821 (bindings--define-key menu [getting-new-versions]
1822 '(menu-item "Getting New Versions" describe-distribution
1823 :help "How to get the latest version of Emacs"))
1824 (bindings--define-key menu [sep2]
1825 menu-bar-separator)
1826 (bindings--define-key menu [external-packages]
1827 '(menu-item "Finding Extra Packages" view-external-packages
1828 :help "How to get more Lisp packages for use in Emacs"))
1829 (bindings--define-key menu [find-emacs-packages]
1830 '(menu-item "Search Built-in Packages" finder-by-keyword
1831 :help "Find built-in packages and features by keyword"))
1832 (bindings--define-key menu [more-manuals]
1833 `(menu-item "More Manuals" ,menu-bar-manuals-menu))
1834 (bindings--define-key menu [emacs-manual]
1835 '(menu-item "Read the Emacs Manual" info-emacs-manual
1836 :help "Full documentation of Emacs features"))
1837 (bindings--define-key menu [describe]
1838 `(menu-item "Describe" ,menu-bar-describe-menu))
1839 (bindings--define-key menu [search-documentation]
1840 `(menu-item "Search Documentation" ,menu-bar-search-documentation-menu))
1841 (bindings--define-key menu [sep1]
1842 menu-bar-separator)
1843 (bindings--define-key menu [emacs-psychotherapist]
1844 '(menu-item "Emacs Psychotherapist" doctor
1845 :help "Our doctor will help you feel better"))
1846 (bindings--define-key menu [send-emacs-bug-report]
1847 '(menu-item "Send Bug Report..." report-emacs-bug
1848 :help "Send e-mail to Emacs maintainers"))
1849 (bindings--define-key menu [emacs-manual-bug]
1850 '(menu-item "How to Report a Bug" info-emacs-bug
1851 :help "Read about how to report an Emacs bug"))
1852 (bindings--define-key menu [emacs-known-problems]
1853 '(menu-item "Emacs Known Problems" view-emacs-problems
1854 :help "Read about known problems with Emacs"))
1855 (bindings--define-key menu [emacs-news]
1856 '(menu-item "Emacs News" view-emacs-news
1857 :help "New features of this version"))
1858 (bindings--define-key menu [emacs-faq]
1859 '(menu-item "Emacs FAQ" view-emacs-FAQ
1860 :help "Frequently asked (and answered) questions about Emacs"))
1861
1862 (bindings--define-key menu [emacs-tutorial-language-specific]
1863 '(menu-item "Emacs Tutorial (choose language)..."
1864 help-with-tutorial-spec-language
1865 :help "Learn how to use Emacs (choose a language)"))
1866 (bindings--define-key menu [emacs-tutorial]
1867 '(menu-item "Emacs Tutorial" help-with-tutorial
1868 :help "Learn how to use Emacs"))
1869
1870 ;; In OS X it's in the app menu already.
1871 ;; FIXME? There already is an "About Emacs" (sans ...) entry in the Help menu.
1872 (and (featurep 'ns)
1873 (not (eq system-type 'darwin))
1874 (bindings--define-key menu [info-panel]
1875 '(menu-item "About Emacs..." ns-do-emacs-info-panel)))
1876 menu))
1877
1878 (bindings--define-key global-map [menu-bar tools]
1879 (cons "Tools" menu-bar-tools-menu))
1880 (bindings--define-key global-map [menu-bar buffer]
1881 (cons "Buffers" global-buffers-menu-map))
1882 (bindings--define-key global-map [menu-bar options]
1883 (cons "Options" menu-bar-options-menu))
1884 (bindings--define-key global-map [menu-bar edit]
1885 (cons "Edit" menu-bar-edit-menu))
1886 (bindings--define-key global-map [menu-bar file]
1887 (cons "File" menu-bar-file-menu))
1888 (bindings--define-key global-map [menu-bar help-menu]
1889 (cons (purecopy "Help") menu-bar-help-menu))
1890
1891 (defun menu-bar-menu-frame-live-and-visible-p ()
1892 "Return non-nil if the menu frame is alive and visible.
1893 The menu frame is the frame for which we are updating the menu."
1894 (let ((menu-frame (or menu-updating-frame (selected-frame))))
1895 (and (frame-live-p menu-frame)
1896 (frame-visible-p menu-frame))))
1897
1898 (defun menu-bar-non-minibuffer-window-p ()
1899 "Return non-nil if the menu frame's selected window is no minibuffer window.
1900 Return nil if the menu frame is dead or its selected window is a
1901 minibuffer window. The menu frame is the frame for which we are
1902 updating the menu."
1903 (let ((menu-frame (or menu-updating-frame (selected-frame))))
1904 (and (frame-live-p menu-frame)
1905 (not (window-minibuffer-p
1906 (frame-selected-window menu-frame))))))
1907
1908 (defun kill-this-buffer () ; for the menu bar
1909 "Kill the current buffer.
1910 When called in the minibuffer, get out of the minibuffer
1911 using `abort-recursive-edit'."
1912 (interactive)
1913 (cond
1914 ;; Don't do anything when `menu-frame' is not alive or visible
1915 ;; (Bug#8184).
1916 ((not (menu-bar-menu-frame-live-and-visible-p)))
1917 ((menu-bar-non-minibuffer-window-p)
1918 (kill-buffer (current-buffer)))
1919 (t
1920 (abort-recursive-edit))))
1921
1922 (defun kill-this-buffer-enabled-p ()
1923 "Return non-nil if the `kill-this-buffer' menu item should be enabled."
1924 (or (not (menu-bar-non-minibuffer-window-p))
1925 (let (found-1)
1926 ;; Instead of looping over entire buffer list, stop once we've
1927 ;; found two "killable" buffers (Bug#8184).
1928 (catch 'found-2
1929 (dolist (buffer (buffer-list))
1930 (unless (string-match-p "^ " (buffer-name buffer))
1931 (if (not found-1)
1932 (setq found-1 t)
1933 (throw 'found-2 t))))))))
1934
1935 (put 'dired 'menu-enable '(menu-bar-non-minibuffer-window-p))
1936
1937 ;; Permit deleting frame if it would leave a visible or iconified frame.
1938 (defun delete-frame-enabled-p ()
1939 "Return non-nil if `delete-frame' should be enabled in the menu bar."
1940 (let ((frames (frame-list))
1941 (count 0))
1942 (while frames
1943 (if (frame-visible-p (car frames))
1944 (setq count (1+ count)))
1945 (setq frames (cdr frames)))
1946 (> count 1)))
1947
1948 (defcustom yank-menu-length 20
1949 "Maximum length to display in the yank-menu."
1950 :type 'integer
1951 :group 'menu)
1952
1953 (defun menu-bar-update-yank-menu (string old)
1954 (let ((front (car (cdr yank-menu)))
1955 (menu-string (if (<= (length string) yank-menu-length)
1956 string
1957 (concat
1958 (substring string 0 (/ yank-menu-length 2))
1959 "..."
1960 (substring string (- (/ yank-menu-length 2)))))))
1961 ;; Don't let the menu string be all dashes
1962 ;; because that has a special meaning in a menu.
1963 (if (string-match "\\`-+\\'" menu-string)
1964 (setq menu-string (concat menu-string " ")))
1965 ;; If we're supposed to be extending an existing string, and that
1966 ;; string really is at the front of the menu, then update it in place.
1967 (if (and old (or (eq old (car front))
1968 (string= old (car front))))
1969 (progn
1970 (setcar front string)
1971 (setcar (cdr front) menu-string))
1972 (setcdr yank-menu
1973 (cons
1974 (cons string (cons menu-string 'menu-bar-select-yank))
1975 (cdr yank-menu)))))
1976 (if (> (length (cdr yank-menu)) kill-ring-max)
1977 (setcdr (nthcdr kill-ring-max yank-menu) nil)))
1978
1979 (put 'menu-bar-select-yank 'apropos-inhibit t)
1980 (defun menu-bar-select-yank ()
1981 "Insert the stretch of previously-killed text selected from menu.
1982 The menu shows all the killed text sequences stored in `kill-ring'."
1983 (interactive "*")
1984 (push-mark (point))
1985 (insert last-command-event))
1986
1987 \f
1988 ;;; Buffers Menu
1989
1990 (defcustom buffers-menu-max-size 10
1991 "Maximum number of entries which may appear on the Buffers menu.
1992 If this is 10, then only the ten most-recently-selected buffers are shown.
1993 If this is nil, then all buffers are shown.
1994 A large number or nil slows down menu responsiveness."
1995 :type '(choice integer
1996 (const :tag "All" nil))
1997 :group 'menu)
1998
1999 (defcustom buffers-menu-buffer-name-length 30
2000 "Maximum length of the buffer name on the Buffers menu.
2001 If this is a number, then buffer names are truncated to this length.
2002 If this is nil, then buffer names are shown in full.
2003 A large number or nil makes the menu too wide."
2004 :type '(choice integer
2005 (const :tag "Full length" nil))
2006 :group 'menu)
2007
2008 (defcustom buffers-menu-show-directories 'unless-uniquify
2009 "If non-nil, show directories in the Buffers menu for buffers that have them.
2010 The special value `unless-uniquify' means that directories will be shown
2011 unless `uniquify-buffer-name-style' is non-nil (in which case, buffer
2012 names should include enough of a buffer's directory to distinguish it
2013 from other buffers).
2014
2015 Setting this variable directly does not take effect until next time the
2016 Buffers menu is regenerated."
2017 :set (lambda (symbol value)
2018 (set symbol value)
2019 (menu-bar-update-buffers t))
2020 :initialize 'custom-initialize-default
2021 :type '(choice (const :tag "Never" nil)
2022 (const :tag "Unless uniquify is enabled" unless-uniquify)
2023 (const :tag "Always" t))
2024 :group 'menu)
2025
2026 (defcustom buffers-menu-show-status t
2027 "If non-nil, show modified/read-only status of buffers in the Buffers menu.
2028 Setting this variable directly does not take effect until next time the
2029 Buffers menu is regenerated."
2030 :set (lambda (symbol value)
2031 (set symbol value)
2032 (menu-bar-update-buffers t))
2033 :initialize 'custom-initialize-default
2034 :type 'boolean
2035 :group 'menu)
2036
2037 (defvar list-buffers-directory nil
2038 "String to display in buffer listings for buffers not visiting a file.")
2039 (make-variable-buffer-local 'list-buffers-directory)
2040
2041 (defun menu-bar-select-buffer ()
2042 (interactive)
2043 (switch-to-buffer last-command-event))
2044
2045 (defun menu-bar-select-frame (frame)
2046 (make-frame-visible frame)
2047 (raise-frame frame)
2048 (select-frame frame))
2049
2050 (defun menu-bar-update-buffers-1 (elt)
2051 (let* ((buf (car elt))
2052 (file
2053 (and (if (eq buffers-menu-show-directories 'unless-uniquify)
2054 (or (not (boundp 'uniquify-buffer-name-style))
2055 (null uniquify-buffer-name-style))
2056 buffers-menu-show-directories)
2057 (or (buffer-file-name buf)
2058 (buffer-local-value 'list-buffers-directory buf)))))
2059 (when file
2060 (setq file (file-name-directory file)))
2061 (when (and file (> (length file) 20))
2062 (setq file (concat "..." (substring file -17))))
2063 (cons (if buffers-menu-show-status
2064 (let ((mod (if (buffer-modified-p buf) "*" ""))
2065 (ro (if (buffer-local-value 'buffer-read-only buf) "%" "")))
2066 (if file
2067 (format "%s %s%s -- %s" (cdr elt) mod ro file)
2068 (format "%s %s%s" (cdr elt) mod ro)))
2069 (if file
2070 (format "%s -- %s" (cdr elt) file)
2071 (cdr elt)))
2072 buf)))
2073
2074 ;; Used to cache the menu entries for commands in the Buffers menu
2075 (defvar menu-bar-buffers-menu-command-entries nil)
2076
2077 (defvar menu-bar-select-buffer-function 'switch-to-buffer
2078 "Function to select the buffer chosen from the `Buffers' menu-bar menu.
2079 It must accept a buffer as its only required argument.")
2080
2081 (defun menu-bar-buffer-vector (alist)
2082 ;; turn ((name . buffer) ...) into a menu
2083 (let ((buffers-vec (make-vector (length alist) nil))
2084 (i (length alist)))
2085 (dolist (pair alist)
2086 (setq i (1- i))
2087 (aset buffers-vec i
2088 (cons (car pair)
2089 `(lambda ()
2090 (interactive)
2091 (funcall menu-bar-select-buffer-function ,(cdr pair))))))
2092 buffers-vec))
2093
2094 (defun menu-bar-update-buffers (&optional force)
2095 ;; If user discards the Buffers item, play along.
2096 (and (lookup-key (current-global-map) [menu-bar buffer])
2097 (or force (frame-or-buffer-changed-p))
2098 (let ((buffers (buffer-list))
2099 (frames (frame-list))
2100 buffers-menu)
2101
2102 ;; Make the menu of buffers proper.
2103 (setq buffers-menu
2104 (let ((i 0)
2105 (limit (if (and (integerp buffers-menu-max-size)
2106 (> buffers-menu-max-size 1))
2107 buffers-menu-max-size most-positive-fixnum))
2108 alist)
2109 ;; Put into each element of buffer-list
2110 ;; the name for actual display,
2111 ;; perhaps truncated in the middle.
2112 (while buffers
2113 (let* ((buf (pop buffers))
2114 (name (buffer-name buf)))
2115 (unless (eq ?\s (aref name 0))
2116 (push (menu-bar-update-buffers-1
2117 (cons buf
2118 (if (and (integerp buffers-menu-buffer-name-length)
2119 (> (length name) buffers-menu-buffer-name-length))
2120 (concat
2121 (substring
2122 name 0 (/ buffers-menu-buffer-name-length 2))
2123 "..."
2124 (substring
2125 name (- (/ buffers-menu-buffer-name-length 2))))
2126 name)
2127 ))
2128 alist)
2129 ;; If requested, list only the N most recently
2130 ;; selected buffers.
2131 (when (= limit (setq i (1+ i)))
2132 (setq buffers nil)))))
2133 (list (menu-bar-buffer-vector alist))))
2134
2135 ;; Make a Frames menu if we have more than one frame.
2136 (when (cdr frames)
2137 (let* ((frames-vec (make-vector (length frames) nil))
2138 (frames-menu
2139 (cons 'keymap
2140 (list "Select Frame" frames-vec)))
2141 (i 0))
2142 (dolist (frame frames)
2143 (aset frames-vec i
2144 (cons
2145 (frame-parameter frame 'name)
2146 `(lambda ()
2147 (interactive) (menu-bar-select-frame ,frame))))
2148 (setq i (1+ i)))
2149 ;; Put it after the normal buffers
2150 (setq buffers-menu
2151 (nconc buffers-menu
2152 `((frames-separator "--")
2153 (frames menu-item "Frames" ,frames-menu))))))
2154
2155 ;; Add in some normal commands at the end of the menu. We use
2156 ;; the copy cached in `menu-bar-buffers-menu-command-entries'
2157 ;; if it's been set already. Note that we can't use constant
2158 ;; lists for the menu-entries, because the low-level menu-code
2159 ;; modifies them.
2160 (unless menu-bar-buffers-menu-command-entries
2161 (setq menu-bar-buffers-menu-command-entries
2162 (list '(command-separator "--")
2163 (list 'next-buffer
2164 'menu-item
2165 "Next Buffer"
2166 'next-buffer
2167 :help "Switch to the \"next\" buffer in a cyclic order")
2168 (list 'previous-buffer
2169 'menu-item
2170 "Previous Buffer"
2171 'previous-buffer
2172 :help "Switch to the \"previous\" buffer in a cyclic order")
2173 (list 'select-named-buffer
2174 'menu-item
2175 "Select Named Buffer..."
2176 'switch-to-buffer
2177 :help "Prompt for a buffer name, and select that buffer in the current window")
2178 (list 'list-all-buffers
2179 'menu-item
2180 "List All Buffers"
2181 'list-buffers
2182 :help "Pop up a window listing all Emacs buffers"
2183 ))))
2184 (setq buffers-menu
2185 (nconc buffers-menu menu-bar-buffers-menu-command-entries))
2186
2187 ;; We used to "(define-key (current-global-map) [menu-bar buffer]"
2188 ;; but that did not do the right thing when the [menu-bar buffer]
2189 ;; entry above had been moved (e.g. to a parent keymap).
2190 (setcdr global-buffers-menu-map (cons "Buffers" buffers-menu)))))
2191
2192 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
2193
2194 (menu-bar-update-buffers)
2195
2196 ;; this version is too slow
2197 ;;(defun format-buffers-menu-line (buffer)
2198 ;; "Returns a string to represent the given buffer in the Buffer menu.
2199 ;;nil means the buffer shouldn't be listed. You can redefine this."
2200 ;; (if (string-match "\\` " (buffer-name buffer))
2201 ;; nil
2202 ;; (with-current-buffer buffer
2203 ;; (let ((size (buffer-size)))
2204 ;; (format "%s%s %-19s %6s %-15s %s"
2205 ;; (if (buffer-modified-p) "*" " ")
2206 ;; (if buffer-read-only "%" " ")
2207 ;; (buffer-name)
2208 ;; size
2209 ;; mode-name
2210 ;; (or (buffer-file-name) ""))))))
2211 \f
2212 ;;; Set up a menu bar menu for the minibuffer.
2213
2214 (dolist (map (list minibuffer-local-map
2215 ;; This shouldn't be necessary, but there's a funny
2216 ;; bug in keymap.c that I don't understand yet. -stef
2217 minibuffer-local-completion-map))
2218 (bindings--define-key map [menu-bar minibuf]
2219 (cons "Minibuf" (make-sparse-keymap "Minibuf"))))
2220
2221 (let ((map minibuffer-local-completion-map))
2222 (bindings--define-key map [menu-bar minibuf ?\?]
2223 '(menu-item "List Completions" minibuffer-completion-help
2224 :help "Display all possible completions"))
2225 (bindings--define-key map [menu-bar minibuf space]
2226 '(menu-item "Complete Word" minibuffer-complete-word
2227 :help "Complete at most one word"))
2228 (bindings--define-key map [menu-bar minibuf tab]
2229 '(menu-item "Complete" minibuffer-complete
2230 :help "Complete as far as possible")))
2231
2232 (let ((map minibuffer-local-map))
2233 (bindings--define-key map [menu-bar minibuf quit]
2234 '(menu-item "Quit" abort-recursive-edit
2235 :help "Abort input and exit minibuffer"))
2236 (bindings--define-key map [menu-bar minibuf return]
2237 '(menu-item "Enter" exit-minibuffer
2238 :key-sequence "\r"
2239 :help "Terminate input and exit minibuffer"))
2240 (bindings--define-key map [menu-bar minibuf isearch-forward]
2241 '(menu-item "Isearch History Forward" isearch-forward
2242 :help "Incrementally search minibuffer history forward"))
2243 (bindings--define-key map [menu-bar minibuf isearch-backward]
2244 '(menu-item "Isearch History Backward" isearch-backward
2245 :help "Incrementally search minibuffer history backward"))
2246 (bindings--define-key map [menu-bar minibuf next]
2247 '(menu-item "Next History Item" next-history-element
2248 :help "Put next minibuffer history element in the minibuffer"))
2249 (bindings--define-key map [menu-bar minibuf previous]
2250 '(menu-item "Previous History Item" previous-history-element
2251 :help "Put previous minibuffer history element in the minibuffer")))
2252 \f
2253 (define-minor-mode menu-bar-mode
2254 "Toggle display of a menu bar on each frame (Menu Bar mode).
2255 With a prefix argument ARG, enable Menu Bar mode if ARG is
2256 positive, and disable it otherwise. If called from Lisp, enable
2257 Menu Bar mode if ARG is omitted or nil.
2258
2259 This command applies to all frames that exist and frames to be
2260 created in the future."
2261 :init-value t
2262 :global t
2263 ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again.
2264 :variable menu-bar-mode
2265
2266 ;; Turn the menu-bars on all frames on or off.
2267 (let ((val (if menu-bar-mode 1 0)))
2268 (dolist (frame (frame-list))
2269 (set-frame-parameter frame 'menu-bar-lines val))
2270 ;; If the user has given `default-frame-alist' a `menu-bar-lines'
2271 ;; parameter, replace it.
2272 (if (assq 'menu-bar-lines default-frame-alist)
2273 (setq default-frame-alist
2274 (cons (cons 'menu-bar-lines val)
2275 (assq-delete-all 'menu-bar-lines
2276 default-frame-alist)))))
2277 ;; Make the message appear when Emacs is idle. We can not call message
2278 ;; directly. The minor-mode message "Menu-bar mode disabled" comes
2279 ;; after this function returns, overwriting any message we do here.
2280 (when (and (called-interactively-p 'interactive) (not menu-bar-mode))
2281 (run-with-idle-timer 0 nil 'message
2282 "Menu-bar mode disabled. Use M-x menu-bar-mode to make the menu bar appear.")))
2283
2284 ;;;###autoload
2285 ;; (This does not work right unless it comes after the above definition.)
2286 ;; This comment is taken from tool-bar.el near
2287 ;; (put 'tool-bar-mode ...)
2288 ;; We want to pretend the menu bar by standard is on, as this will make
2289 ;; customize consider disabling the menu bar a customization, and save
2290 ;; that. We could do this for real by setting :init-value above, but
2291 ;; that would overwrite disabling the menu bar from X resources.
2292 (put 'menu-bar-mode 'standard-value '(t))
2293
2294 (defun toggle-menu-bar-mode-from-frame (&optional arg)
2295 "Toggle menu bar on or off, based on the status of the current frame.
2296 See `menu-bar-mode' for more information."
2297 (interactive (list (or current-prefix-arg 'toggle)))
2298 (if (eq arg 'toggle)
2299 (menu-bar-mode
2300 (if (menu-bar-positive-p
2301 (frame-parameter (menu-bar-frame-for-menubar) 'menu-bar-lines))
2302 0 1))
2303 (menu-bar-mode arg)))
2304
2305 (declare-function x-menu-bar-open "term/x-win" (&optional frame))
2306 (declare-function w32-menu-bar-open "term/w32-win" (&optional frame))
2307
2308 (defun lookup-key-ignore-too-long (map key)
2309 "Call `lookup-key' and convert numeric values to nil."
2310 (let ((binding (lookup-key map key)))
2311 (if (numberp binding) ; `too long'
2312 nil
2313 binding)))
2314
2315 (defun popup-menu (menu &optional position prefix from-menu-bar)
2316 "Popup the given menu and call the selected option.
2317 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
2318 `x-popup-menu'.
2319 The menu is shown at the place where POSITION specifies.
2320 For the form of POSITION, see `popup-menu-normalize-position'.
2321 PREFIX is the prefix argument (if any) to pass to the command.
2322 FROM-MENU-BAR, if non-nil, means we are dropping one of menu-bar's menus."
2323 (let* ((map (cond
2324 ((keymapp menu) menu)
2325 ((and (listp menu) (keymapp (car menu))) menu)
2326 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
2327 (filter (when (symbolp map)
2328 (plist-get (get map 'menu-prop) :filter))))
2329 (if filter (funcall filter (symbol-function map)) map)))))
2330 (frame (selected-frame))
2331 event cmd)
2332 (if from-menu-bar
2333 (let* ((xy (posn-x-y position))
2334 (menu-symbol (menu-bar-menu-at-x-y (car xy) (cdr xy))))
2335 (setq position (list menu-symbol (list frame '(menu-bar)
2336 xy 0))))
2337 (setq position (popup-menu-normalize-position position)))
2338 ;; The looping behavior was taken from lmenu's popup-menu-popup
2339 (while (and map (setq event
2340 ;; map could be a prefix key, in which case
2341 ;; we need to get its function cell
2342 ;; definition.
2343 (x-popup-menu position (indirect-function map))))
2344 ;; Strangely x-popup-menu returns a list.
2345 ;; mouse-major-mode-menu was using a weird:
2346 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
2347 (setq cmd
2348 (cond
2349 ((and from-menu-bar
2350 (consp event)
2351 (numberp (car event))
2352 (numberp (cdr event)))
2353 (let ((x (car event))
2354 (y (cdr event))
2355 menu-symbol)
2356 (setq menu-symbol (menu-bar-menu-at-x-y x y))
2357 (setq position (list menu-symbol (list frame '(menu-bar)
2358 event 0)))
2359 (setq map
2360 (key-binding (vector 'menu-bar menu-symbol)))))
2361 ((and (not (keymapp map)) (listp map))
2362 ;; We were given a list of keymaps. Search them all
2363 ;; in sequence until a first binding is found.
2364 (let ((mouse-click (apply 'vector event))
2365 binding)
2366 (while (and map (null binding))
2367 (setq binding (lookup-key-ignore-too-long (car map) mouse-click))
2368 (setq map (cdr map)))
2369 binding))
2370 (t
2371 ;; We were given a single keymap.
2372 (lookup-key map (apply 'vector event)))))
2373 ;; Clear out echoing, which perhaps shows a prefix arg.
2374 (message "")
2375 ;; Maybe try again but with the submap.
2376 (setq map (if (keymapp cmd) cmd)))
2377 ;; If the user did not cancel by refusing to select,
2378 ;; and if the result is a command, run it.
2379 (when (and (null map) (commandp cmd))
2380 (setq prefix-arg prefix)
2381 ;; `setup-specified-language-environment', for instance,
2382 ;; expects this to be set from a menu keymap.
2383 (setq last-command-event (car (last event)))
2384 ;; mouse-major-mode-menu was using `command-execute' instead.
2385 (call-interactively cmd))))
2386
2387 (defun popup-menu-normalize-position (position)
2388 "Convert the POSITION to the form which `popup-menu' expects internally.
2389 POSITION can be an event, a posn- value, a value having the
2390 form ((XOFFSET YOFFSET) WINDOW), or nil.
2391 If nil, the current mouse position is used, or nil if there is no mouse."
2392 (pcase position
2393 ;; nil -> mouse cursor position
2394 (`nil
2395 (let ((mp (mouse-pixel-position)))
2396 (list (list (cadr mp) (cddr mp)) (car mp))))
2397 ;; Value returned from `event-end' or `posn-at-point'.
2398 ((pred posnp)
2399 (let ((xy (posn-x-y position)))
2400 (list (list (car xy) (cdr xy))
2401 (posn-window position))))
2402 ;; Event.
2403 ((pred eventp)
2404 (popup-menu-normalize-position (event-end position)))
2405 (_ position)))
2406
2407 (defcustom tty-menu-open-use-tmm nil
2408 "If non-nil, \\[menu-bar-open] on a TTY will invoke `tmm-menubar'.
2409
2410 If nil, \\[menu-bar-open] will drop down the menu corresponding to the
2411 first (leftmost) menu-bar item; you can select other items by typing
2412 \\[forward-char], \\[backward-char], \\[right-char] and \\[left-char]."
2413 :type '(choice (const :tag "F10 drops down TTY menus" nil)
2414 (const :tag "F10 invokes tmm-menubar" t))
2415 :group 'display
2416 :version "24.4")
2417
2418 (defvar tty-menu--initial-menu-x 1
2419 "X coordinate of the first menu-bar menu dropped by F10.
2420
2421 This is meant to be used only for debugging TTY menus.")
2422
2423 (defun menu-bar-open (&optional frame)
2424 "Start key navigation of the menu bar in FRAME.
2425
2426 This function decides which method to use to access the menu
2427 depending on FRAME's terminal device. On X displays, it calls
2428 `x-menu-bar-open'; on Windows, `w32-menu-bar-open'; otherwise it
2429 calls either `popup-menu' or `tmm-menubar' depending on whether
2430 `tty-menu-open-use-tmm' is nil or not.
2431
2432 If FRAME is nil or not given, use the selected frame."
2433 (interactive)
2434 (let ((type (framep (or frame (selected-frame)))))
2435 (cond
2436 ((eq type 'x) (x-menu-bar-open frame))
2437 ((eq type 'w32) (w32-menu-bar-open frame))
2438 ((and (null tty-menu-open-use-tmm)
2439 (not (zerop (or (frame-parameter nil 'menu-bar-lines) 0))))
2440 ;; Make sure the menu bar is up to date. One situation where
2441 ;; this is important is when this function is invoked by name
2442 ;; via M-x, in which case the menu bar includes the "Minibuf"
2443 ;; menu item that should be removed when we exit the minibuffer.
2444 (force-mode-line-update)
2445 (redisplay)
2446 (let* ((x tty-menu--initial-menu-x)
2447 (menu (menu-bar-menu-at-x-y x 0 frame)))
2448 (popup-menu (or
2449 (lookup-key-ignore-too-long
2450 global-map (vector 'menu-bar menu))
2451 (lookup-key-ignore-too-long
2452 (current-local-map) (vector 'menu-bar menu))
2453 (cdar (minor-mode-key-binding (vector 'menu-bar menu)))
2454 (mouse-menu-bar-map))
2455 (posn-at-x-y x 0 nil t) nil t)))
2456 (t (with-selected-frame (or frame (selected-frame))
2457 (tmm-menubar))))))
2458
2459 (global-set-key [f10] 'menu-bar-open)
2460
2461 (defun buffer-menu-open ()
2462 "Start key navigation of the buffer menu.
2463 This is the keyboard interface to \\[mouse-buffer-menu]."
2464 (interactive)
2465 (popup-menu (mouse-buffer-menu-keymap)
2466 (posn-at-x-y 0 0 nil t)))
2467
2468 (global-set-key [C-f10] 'buffer-menu-open)
2469
2470 (defun mouse-buffer-menu-keymap ()
2471 (let* ((menu (mouse-buffer-menu-map))
2472 (km (make-sparse-keymap (pop menu))))
2473 (dolist (item (nreverse menu))
2474 (let* ((name (pop item)))
2475 (define-key km (vector (intern name))
2476 (list name 'keymap name
2477 (menu-bar-buffer-vector item)))))
2478 km))
2479
2480 (defvar tty-menu-navigation-map
2481 (let ((map (make-sparse-keymap)))
2482 ;; The next line is disabled because it breaks interpretation of
2483 ;; escape sequences, produced by TTY arrow keys, as tty-menu-*
2484 ;; commands. Instead, we explicitly bind some keys to
2485 ;; tty-menu-exit.
2486 ;;(define-key map [t] 'tty-menu-exit)
2487
2488 ;; The tty-menu-* are just symbols interpreted by term.c, they are
2489 ;; not real commands.
2490 (dolist (bind '((keyboard-quit . tty-menu-exit)
2491 (keyboard-escape-quit . tty-menu-exit)
2492 ;; The following two will need to be revised if we ever
2493 ;; support a right-to-left menu bar.
2494 (forward-char . tty-menu-next-menu)
2495 (backward-char . tty-menu-prev-menu)
2496 (right-char . tty-menu-next-menu)
2497 (left-char . tty-menu-prev-menu)
2498 (next-line . tty-menu-next-item)
2499 (previous-line . tty-menu-prev-item)
2500 (newline . tty-menu-select)
2501 (newline-and-indent . tty-menu-select)
2502 (menu-bar-open . tty-menu-exit)))
2503 (substitute-key-definition (car bind) (cdr bind)
2504 map (current-global-map)))
2505
2506 ;; The bindings of menu-bar items are so that clicking on the menu
2507 ;; bar when a menu is already shown pops down that menu.
2508 (define-key map [menu-bar t] 'tty-menu-exit)
2509
2510 (define-key map [?\C-r] 'tty-menu-select)
2511 (define-key map [?\C-j] 'tty-menu-select)
2512 (define-key map [return] 'tty-menu-select)
2513 (define-key map [linefeed] 'tty-menu-select)
2514 (define-key map [mouse-1] 'tty-menu-select)
2515 (define-key map [drag-mouse-1] 'tty-menu-select)
2516 (define-key map [mouse-2] 'tty-menu-select)
2517 (define-key map [drag-mouse-2] 'tty-menu-select)
2518 (define-key map [mouse-3] 'tty-menu-select)
2519 (define-key map [drag-mouse-3] 'tty-menu-select)
2520 (define-key map [wheel-down] 'tty-menu-next-item)
2521 (define-key map [wheel-up] 'tty-menu-prev-item)
2522 (define-key map [wheel-left] 'tty-menu-prev-menu)
2523 (define-key map [wheel-right] 'tty-menu-next-menu)
2524 ;; The following 4 bindings are for those whose text-mode mouse
2525 ;; lack the wheel.
2526 (define-key map [S-mouse-1] 'tty-menu-next-item)
2527 (define-key map [S-drag-mouse-1] 'tty-menu-next-item)
2528 (define-key map [S-mouse-2] 'tty-menu-prev-item)
2529 (define-key map [S-drag-mouse-2] 'tty-menu-prev-item)
2530 (define-key map [S-mouse-3] 'tty-menu-prev-item)
2531 (define-key map [S-drag-mouse-3] 'tty-menu-prev-item)
2532 (define-key map [header-line mouse-1] 'tty-menu-select)
2533 (define-key map [header-line drag-mouse-1] 'tty-menu-select)
2534 ;; The down-mouse events must be bound to tty-menu-ignore, so that
2535 ;; only releasing the mouse button pops up the menu.
2536 (define-key map [mode-line down-mouse-1] 'tty-menu-ignore)
2537 (define-key map [mode-line down-mouse-2] 'tty-menu-ignore)
2538 (define-key map [mode-line down-mouse-3] 'tty-menu-ignore)
2539 (define-key map [mode-line C-down-mouse-1] 'tty-menu-ignore)
2540 (define-key map [mode-line C-down-mouse-2] 'tty-menu-ignore)
2541 (define-key map [mode-line C-down-mouse-3] 'tty-menu-ignore)
2542 (define-key map [down-mouse-1] 'tty-menu-ignore)
2543 (define-key map [C-down-mouse-1] 'tty-menu-ignore)
2544 (define-key map [C-down-mouse-2] 'tty-menu-ignore)
2545 (define-key map [C-down-mouse-3] 'tty-menu-ignore)
2546 (define-key map [mouse-movement] 'tty-menu-mouse-movement)
2547 map)
2548 "Keymap used while processing TTY menus.")
2549
2550 (provide 'menu-bar)
2551
2552 ;;; menu-bar.el ends here