1 ;;; menu-bar.el --- define a default menu bar.
6 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Don't clobber an existing menu-bar keymap, to preserve any menu-bar key
27 ;; definitions made in loaddefs.el.
28 (or (lookup-key global-map
[menu-bar
])
29 (define-key global-map
[menu-bar
] (make-sparse-keymap "menu-bar")))
30 (defvar menu-bar-help-menu
(make-sparse-keymap "Help"))
31 ;; Put Help item last.
32 (setq menu-bar-final-items
'(help))
33 (define-key global-map
[menu-bar help
] (cons "Help" menu-bar-help-menu
))
34 (defvar menu-bar-edit-menu
(make-sparse-keymap "Edit"))
35 (define-key global-map
[menu-bar edit
] (cons "Edit" menu-bar-edit-menu
))
36 (defvar menu-bar-file-menu
(make-sparse-keymap "File"))
37 (define-key global-map
[menu-bar file
] (cons "File" menu-bar-file-menu
))
39 (define-key menu-bar-file-menu
[exit-emacs
]
40 '("Exit Emacs" . save-buffers-kill-emacs
))
41 (define-key menu-bar-file-menu
[kill-buffer
]
42 '("Kill Buffer" . kill-this-buffer
))
43 (define-key menu-bar-file-menu
[delete-frame
] '("Delete Frame" . delete-frame
))
44 (define-key menu-bar-file-menu
[emerge] '("Emerge" . menu-bar-emerge-menu))
45 (define-key menu-bar-file-menu [calendar] '("Calendar" . calendar))
46 (define-key menu-bar-file-menu [rmail] '("Read Mail" . rmail))
47 (define-key menu-bar-file-menu [gnus] '("Read Net News" . gnus))
48 (define-key menu-bar-file-menu [bookmark]
49 '("Bookmarks" . menu-bar-bookmark-map))
50 (define-key menu-bar-file-menu [print-buffer] '("Print Buffer" . print-buffer))
51 (define-key menu-bar-file-menu [revert-buffer]
52 '("Revert Buffer" . revert-buffer))
53 (define-key menu-bar-file-menu [write-file]
54 '("Save Buffer As..." . write-file))
55 (define-key menu-bar-file-menu [save-buffer] '("Save Buffer" . save-buffer))
56 (define-key menu-bar-file-menu [dired] '("Open Directory..." . dired))
57 (define-key menu-bar-file-menu [open-file] '("Open File..." . find-file))
58 (define-key menu-bar-file-menu [new-frame] '("New Frame" . new-frame))
60 (define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map))
61 (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
62 (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
63 (define-key menu-bar-edit-menu [query-replace]
64 '("Query Replace" . query-replace))
65 (define-key menu-bar-edit-menu [re-search-back]
66 '("Regexp Search Backwards" . re-search-backward))
67 (define-key menu-bar-edit-menu [search-back]
68 '("Search Backwards" . search-backward))
69 (define-key menu-bar-edit-menu [re-search-fwd]
70 '("Regexp Search" . re-search-forward))
71 (define-key menu-bar-edit-menu [search-fwd]
72 '("Search" . search-forward))
73 (define-key menu-bar-edit-menu [choose-next-paste]
74 '("Choose Next Paste >" . mouse-menu-choose-yank))
75 (define-key menu-bar-edit-menu [paste] '("Paste" . yank))
76 (define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save))
77 (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
78 (define-key menu-bar-edit-menu [undo] '("Undo" . undo))
80 (put 'fill-region 'menu-enable 'mark-active)
81 (put 'kill-region 'menu-enable 'mark-active)
82 (put 'kill-ring-save 'menu-enable 'mark-active)
83 (put 'yank 'menu-enable '(x-selection-exists-p))
84 (put 'delete-region 'menu-enable 'mark-active)
85 (put 'undo 'menu-enable '(if (eq last-command 'undo)
87 (consp buffer-undo-list)))
88 (put 'query-replace 'menu-enable (not buffer-read-only))
90 (autoload 'ispell-menu-map "ispell" nil t 'keymap)
92 ;; These are alternative definitions for the cut, paste and copy
93 ;; menu items. Use them if your system expects these to use the clipboard
95 (put 'clipboard-kill-region 'menu-enable 'mark-active)
96 (put 'clipboard-kill-ring-save 'menu-enable 'mark-active)
97 (put 'clipboard-yank 'menu-enable
98 '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD)))
100 (defun clipboard-yank ()
101 "Reinsert the last stretch of killed text, or the clipboard contents."
103 (let ((x-select-enable-clipboard t))
106 (defun clipboard-kill-ring-save (beg end)
107 "Copy region to kill ring, and save in the X clipboard."
109 (let ((x-select-enable-clipboard t))
110 (kill-ring-save beg end)))
112 (defun clipboard-kill-region (beg end)
113 "Kill the region, and save it in the X clipboard."
115 (let ((x-select-enable-clipboard t))
116 (kill-region beg end)))
118 (defun menu-bar-enable-clipboard ()
119 "Make the menu bar CUT, PASTE and COPY items use the clipboard."
121 ;; We can't use constant list structure here because it becomes pure,
122 ;; and because it gets modified with cache data.
123 (define-key menu-bar-edit-menu [paste]
124 (cons "Paste" 'clipboard-yank))
125 (define-key menu-bar-edit-menu [copy]
126 (cons "Copy" 'clipboard-kill-ring-save))
127 (define-key menu-bar-edit-menu [cut]
128 (cons "Cut" 'clipboard-kill-region)))
130 ;; Sun expects these commands on these keys, so why not?
131 (define-key global-map [f20] 'clipboard-kill-region)
132 (define-key global-map [f16] 'clipboard-kill-ring-save)
133 (define-key global-map [f18] 'clipboard-yank)
135 (define-key menu-bar-help-menu [emacs-version]
136 '("Show Version" . emacs-version))
137 (define-key menu-bar-help-menu [report-emacs-bug]
138 '("Send Bug Report" . report-emacs-bug))
139 (define-key menu-bar-help-menu [emacs-tutorial]
140 '("Emacs Tutorial" . help-with-tutorial))
141 (define-key menu-bar-help-menu [man] '("Man..." . manual-entry))
142 (define-key menu-bar-help-menu [describe-variable]
143 '("Describe Variable..." . describe-variable))
144 (define-key menu-bar-help-menu [describe-function]
145 '("Describe Function..." . describe-function))
146 (define-key menu-bar-help-menu [describe-key]
147 '("Describe Key..." . describe-key))
148 (define-key menu-bar-help-menu [list-keybindings]
149 '("List Keybindings" . describe-bindings))
150 (define-key menu-bar-help-menu [command-apropos]
151 '("Command Apropos..." . command-apropos))
152 (define-key menu-bar-help-menu [describe-mode]
153 '("Describe Mode" . describe-mode))
154 (define-key menu-bar-help-menu [info] '("Info" . info))
156 (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
157 (defun kill-this-buffer () ; for the menubar
158 "Kills the current buffer."
160 (kill-buffer (current-buffer)))
162 (defun kill-this-buffer-enabled-p ()
164 (buffers (buffer-list)))
166 (or (string-match "^ " (buffer-name (car buffers)))
167 (setq count (1+ count)))
168 (setq buffers (cdr buffers)))
171 (put 'save-buffer 'menu-enable '(buffer-modified-p))
172 (put 'revert-buffer 'menu-enable
173 '(or revert-buffer-function revert-buffer-insert-file-contents-function
174 (and (buffer-file-name)
175 (or (buffer-modified-p)
176 (not (verify-visited-file-modtime (current-buffer)))))))
177 ;; Permit deleting frame if it would leave a visible or iconified frame.
178 (put 'delete-frame 'menu-enable
179 '(let ((frames (frame-list))
182 (if (cdr (assq 'visibility (frame-parameters (car frames))))
183 (setq count (1+ count)))
184 (setq frames (cdr frames)))
186 (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
188 (put 'advertised-undo 'menu-enable
189 '(and (not (eq t buffer-undo-list))
190 (if (eq last-command 'undo)
191 (and (boundp 'pending-undo-list)
195 (defvar yank-menu-length 100
196 "*Maximum length of an item in the menu for \
197 \\[mouse-menu-choose-yank].")
199 (defun mouse-menu-choose-yank (event)
200 "Pop up a menu of the kill-ring for selection with the mouse.
201 The kill-ring-yank-pointer is moved to the selected element.
202 A subsequent \\[yank] yanks the choice just selected."
205 (menu (mapcar (lambda (string)
206 (if (> (length string) yank-menu-length)
207 (setq string (substring string
208 0 yank-menu-length)))
209 (prog1 (cons string count)
210 (setq count (1+ count))))
212 (arg (x-popup-menu event
214 (cons "Choose Next Yank" menu)))))
215 ;; A mouse click outside the menu returns nil.
216 ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
217 ;; XXX should this perhaps do something other than simply return? -rm
220 ;; We don't use `rotate-yank-pointer' because we want to move
221 ;; relative to the beginning of kill-ring, not the current
222 ;; position. Also, that would ask for any new X selection and
223 ;; thus change the list of items the user just chose from, which
224 ;; would be highly confusing.
225 (setq kill-ring-yank-pointer (nthcdr arg kill-ring))
227 (message "The next yank will insert the selected text.")
228 (current-kill 0))))))
229 (put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
231 (define-key global-map [menu-bar buffer] '("Buffers" . menu-bar-buffers))
233 (defalias 'menu-bar-buffers (make-sparse-keymap "Buffers"))
235 (defvar complex-buffers-menu-p nil
236 "*Non-nil says, offer a choice of actions after you pick a buffer.
237 This applies to the Buffers menu from the menu bar.")
239 (defvar buffers-menu-max-size 10
240 "*Maximum number of entries which may appear on the Buffers menu.
241 If this is 10, then only the ten most-recently-selected buffers are shown.
242 If this is nil, then all buffers are shown.
243 A large number or nil slows down menu responsiveness.")
245 (defvar list-buffers-directory nil)
247 (defun menu-bar-select-buffer ()
249 (switch-to-buffer last-command-event))
251 (defun menu-bar-select-frame ()
253 (make-frame-visible last-command-event)
254 (raise-frame last-command-event)
255 (select-frame last-command-event))
257 (defvar menu-bar-update-buffers-last-buffers nil)
258 (defvar menu-bar-update-buffers-last-frames nil)
260 (defun menu-bar-update-buffers ()
261 (let ((buffers (buffer-list))
262 (frames (frame-list))
263 buffers-menu frames-menu)
264 (if (and (equal buffers menu-bar-update-buffers-last-buffers)
265 (equal frames menu-bar-update-buffers-last-frames))
267 (setq menu-bar-update-buffers-last-buffers buffers)
268 (setq menu-bar-update-buffers-last-frames frames)
269 ;; If requested, list only the N most recently selected buffers.
270 (if (and (integerp buffers-menu-max-size)
271 (> buffers-menu-max-size 1))
272 (if (> (length buffers) buffers-menu-max-size)
273 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
275 ;; Make the menu of buffers proper.
277 (cons "Select Buffer"
284 (or (eq ?\ (aref (buffer-name (car tail)) 0))
287 (length (buffer-name (car tail))))))
288 (setq tail (cdr tail)))
291 (let ((elt (car tail)))
292 (if (not (string-match "^ "
297 (format "%%%ds %%s%%s %%s"
300 (if (buffer-modified-p elt)
304 (if buffer-read-only "%" " "))
305 (or (buffer-file-name elt)
308 list-buffers-directory)
312 (and alist (> (length (car (car alist))) maxlen)
313 (setq maxlen (length (car (car alist))))))
314 (setq tail (cdr tail)))
315 (setq alist (nreverse alist))
316 (nconc (mapcar '(lambda (pair)
317 ;; This is somewhat risque, to use
318 ;; the buffer name itself as the event type
319 ;; to define, but it works.
320 ;; It would not work to use the buffer
321 ;; since a buffer as an event has its
323 (nconc (list (buffer-name (cdr pair))
326 'menu-bar-select-buffer))
328 (list (cons 'list-buffers
330 (concat (make-string (max (- (/ maxlen
338 ;; Make a Frames menu if we have more than one frame.
342 (mapcar '(lambda (frame)
345 (frame-parameters frame)))
347 'menu-bar-select-frame))
350 (setq buffers-menu (cons 'keymap buffers-menu)))
352 (setq frames-menu (cons 'keymap frames-menu)))
353 (define-key global-map [menu-bar buffer]
355 (if (and buffers-menu frames-menu)
356 (list 'keymap "Buffers and Frames"
357 (cons 'buffers (cons "Buffers" buffers-menu))
358 (cons 'frames (cons "Frames" frames-menu)))
359 (or buffers-menu frames-menu 'undefined)))))))
361 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
363 ;; this version is too slow
364 ;;;(defun format-buffers-menu-line (buffer)
365 ;;; "Returns a string to represent the given buffer in the Buffer menu.
366 ;;;nil means the buffer shouldn't be listed. You can redefine this."
367 ;;; (if (string-match "\\` " (buffer-name buffer))
370 ;;; (set-buffer buffer)
371 ;;; (let ((size (buffer-size)))
372 ;;; (format "%s%s %-19s %6s %-15s %s"
373 ;;; (if (buffer-modified-p) "*" " ")
374 ;;; (if buffer-read-only "%" " ")
378 ;;; (or (buffer-file-name) ""))))))
380 (defun menu-bar-mode (flag)
381 "Toggle display of a menu bar on each frame.
382 This command applies to all frames that exist and frames to be
383 created in the future.
384 With a numeric argument, if the argument is negative,
385 turn off menu bars; otherwise, turn on menu bars."
388 ;; Obtain the current setting by looking at default-frame-alist.
390 (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist)))
391 (if assq (cdr assq) 0))))))
393 ;; Tweedle it according to the argument.
394 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
395 (> (prefix-numeric-value flag) 0)))
397 ;; Apply it to default-frame-alist.
398 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
399 (if (consp parameter)
400 (setcdr parameter (if menu-bar-mode 1 0))
401 (setq default-frame-alist
402 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
403 default-frame-alist))))
405 ;; Apply it to existing frames.
406 (let ((frames (frame-list)))
408 (modify-frame-parameters (car frames)
409 (list (cons 'menu-bar-lines
410 (if menu-bar-mode 1 0))))
411 (setq frames (cdr frames))))))
415 ;;; menu-bar.el ends here