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