(Fframe_parameters): Don't handle menu-bar-lines explicitly.
[bpt/emacs.git] / lisp / menu-bar.el
CommitLineData
235aa29b
ER
1;;; menu-bar.el --- define a default menu bar.
2
3;; Author: RMS
b7f66977 4;; Keywords: internal
235aa29b 5
03dcd202 6;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
1db87953
RS
7
8;; This file is part of GNU Emacs.
9
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)
13;; any later version.
14
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.
19
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.
23
235aa29b 24;;; Code:
1db87953 25
b132f2b1
RM
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")))
22390f50 30(defvar menu-bar-help-menu (make-sparse-keymap "Help"))
da183f87
RS
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))
22390f50 34(defvar menu-bar-edit-menu (make-sparse-keymap "Edit"))
40954111 35(define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
22390f50 36(defvar menu-bar-file-menu (make-sparse-keymap "File"))
40954111 37(define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
9bb8e471 38\f
ca9b40a1 39(define-key menu-bar-file-menu [exit-emacs]
db774a16 40 '("Exit Emacs" . save-buffers-kill-emacs))
9bb8e471
RS
41
42(define-key menu-bar-file-menu [separator-compare]
43 '("--"))
44
080ac1ac
RS
45(define-key menu-bar-file-menu [epatch]
46 '("Apply Patch" . menu-bar-epatch-menu))
47(define-key menu-bar-file-menu [ediff]
48 '("Compare Files" . menu-bar-ediff-menu))
966bcddd 49(define-key menu-bar-file-menu [emerge] '("Emerge" . menu-bar-emerge-menu))
9bb8e471
RS
50
51(define-key menu-bar-file-menu [separator-misc]
52 '("--"))
53
661b05a7
RS
54(define-key menu-bar-file-menu [calendar] '("Calendar" . calendar))
55(define-key menu-bar-file-menu [rmail] '("Read Mail" . rmail))
56(define-key menu-bar-file-menu [gnus] '("Read Net News" . gnus))
9bb8e471
RS
57
58(define-key menu-bar-file-menu [separator-frames]
59 '("--"))
60
61(define-key menu-bar-file-menu [delete-frame] '("Delete Frame" . delete-frame))
62(define-key menu-bar-file-menu [make-frame] '("Make New Frame" . make-frame))
63
64(define-key menu-bar-file-menu [separator-buffers]
65 '("--"))
66
9d441895 67(define-key menu-bar-file-menu [bookmark]
966bcddd 68 '("Bookmarks" . menu-bar-bookmark-map))
9bb8e471
RS
69(define-key menu-bar-file-menu [print-buffer]
70 '("Print Buffer" . print-buffer))
71(define-key menu-bar-file-menu [kill-buffer]
72 '("Kill Buffer" . kill-this-buffer))
2f1139a4
RS
73(define-key menu-bar-file-menu [revert-buffer]
74 '("Revert Buffer" . revert-buffer))
75(define-key menu-bar-file-menu [write-file]
76 '("Save Buffer As..." . write-file))
77(define-key menu-bar-file-menu [save-buffer] '("Save Buffer" . save-buffer))
76202b57 78(define-key menu-bar-file-menu [dired] '("Open Directory..." . dired))
2f1139a4 79(define-key menu-bar-file-menu [open-file] '("Open File..." . find-file))
9bb8e471 80\f
33aa8946
RS
81(define-key menu-bar-edit-menu [query-replace]
82 '("Query Replace" . query-replace))
50d195ee
RS
83(define-key menu-bar-edit-menu [re-search-back]
84 '("Regexp Search Backwards" . re-search-backward))
85(define-key menu-bar-edit-menu [search-back]
86 '("Search Backwards" . search-backward))
87(define-key menu-bar-edit-menu [re-search-fwd]
88 '("Regexp Search" . re-search-forward))
89(define-key menu-bar-edit-menu [search-fwd]
90 '("Search" . search-forward))
9bb8e471
RS
91
92(define-key menu-bar-edit-menu [separator-misc]
93 '("--"))
94
95(define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map))
96(define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
97
98(define-key menu-bar-edit-menu [separator-edit]
99 '("--"))
100
101(define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
75340584 102(define-key menu-bar-edit-menu [choose-next-paste]
f7f19201 103 '("Choose Next Paste >" . mouse-menu-choose-yank))
057d49d1
RS
104(define-key menu-bar-edit-menu [paste] '("Paste" . yank))
105(define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save))
106(define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
107(define-key menu-bar-edit-menu [undo] '("Undo" . undo))
108
109(put 'fill-region 'menu-enable 'mark-active)
110(put 'kill-region 'menu-enable 'mark-active)
111(put 'kill-ring-save 'menu-enable 'mark-active)
112(put 'yank 'menu-enable '(x-selection-exists-p))
113(put 'delete-region 'menu-enable 'mark-active)
114(put 'undo 'menu-enable '(if (eq last-command 'undo)
115 pending-undo-list
116 (consp buffer-undo-list)))
33aa8946 117(put 'query-replace 'menu-enable (not buffer-read-only))
db774a16 118
9e18f0a0
RS
119(autoload 'ispell-menu-map "ispell" nil t 'keymap)
120
f9cf0be2
RS
121;; These are alternative definitions for the cut, paste and copy
122;; menu items. Use them if your system expects these to use the clipboard
123
f9cf0be2
RS
124(put 'clipboard-kill-region 'menu-enable 'mark-active)
125(put 'clipboard-kill-ring-save 'menu-enable 'mark-active)
126(put 'clipboard-yank 'menu-enable
127 '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD)))
128
129(defun clipboard-yank ()
130 "Reinsert the last stretch of killed text, or the clipboard contents."
131 (interactive)
132 (let ((x-select-enable-clipboard t))
133 (yank)))
134
135(defun clipboard-kill-ring-save (beg end)
136 "Copy region to kill ring, and save in the X clipboard."
137 (interactive "r")
138 (let ((x-select-enable-clipboard t))
139 (kill-ring-save beg end)))
140
141(defun clipboard-kill-region (beg end)
142 "Kill the region, and save it in the X clipboard."
143 (interactive "r")
144 (let ((x-select-enable-clipboard t))
145 (kill-region beg end)))
146
147(defun menu-bar-enable-clipboard ()
148 "Make the menu bar CUT, PASTE and COPY items use the clipboard."
149 (interactive)
5754d7f2
RS
150 ;; We can't use constant list structure here because it becomes pure,
151 ;; and because it gets modified with cache data.
152 (define-key menu-bar-edit-menu [paste]
153 (cons "Paste" 'clipboard-yank))
154 (define-key menu-bar-edit-menu [copy]
155 (cons "Copy" 'clipboard-kill-ring-save))
156 (define-key menu-bar-edit-menu [cut]
157 (cons "Cut" 'clipboard-kill-region)))
2165f676
RS
158
159;; Sun expects these commands on these keys, so why not?
160(define-key global-map [f20] 'clipboard-kill-region)
161(define-key global-map [f16] 'clipboard-kill-ring-save)
162(define-key global-map [f18] 'clipboard-yank)
f9cf0be2 163\f
efb166ff
RS
164(define-key menu-bar-help-menu [emacs-version]
165 '("Show Version" . emacs-version))
48433a65
RS
166(define-key menu-bar-help-menu [report-emacs-bug]
167 '("Send Bug Report" . report-emacs-bug))
ca9b40a1 168(define-key menu-bar-help-menu [emacs-tutorial]
db774a16 169 '("Emacs Tutorial" . help-with-tutorial))
2f1139a4
RS
170(define-key menu-bar-help-menu [man] '("Man..." . manual-entry))
171(define-key menu-bar-help-menu [describe-variable]
172 '("Describe Variable..." . describe-variable))
173(define-key menu-bar-help-menu [describe-function]
174 '("Describe Function..." . describe-function))
175(define-key menu-bar-help-menu [describe-key]
176 '("Describe Key..." . describe-key))
177(define-key menu-bar-help-menu [list-keybindings]
178 '("List Keybindings" . describe-bindings))
179(define-key menu-bar-help-menu [command-apropos]
180 '("Command Apropos..." . command-apropos))
181(define-key menu-bar-help-menu [describe-mode]
182 '("Describe Mode" . describe-mode))
183(define-key menu-bar-help-menu [info] '("Info" . info))
db774a16 184
2f1139a4 185(define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
db774a16
RS
186(defun kill-this-buffer () ; for the menubar
187 "Kills the current buffer."
188 (interactive)
189 (kill-buffer (current-buffer)))
190
2f1139a4
RS
191(defun kill-this-buffer-enabled-p ()
192 (let ((count 0)
193 (buffers (buffer-list)))
194 (while buffers
195 (or (string-match "^ " (buffer-name (car buffers)))
196 (setq count (1+ count)))
197 (setq buffers (cdr buffers)))
198 (> count 1)))
199
db774a16 200(put 'save-buffer 'menu-enable '(buffer-modified-p))
da183f87
RS
201(put 'revert-buffer 'menu-enable
202 '(or revert-buffer-function revert-buffer-insert-file-contents-function
203 (and (buffer-file-name)
660fa562
RM
204 (or (buffer-modified-p)
205 (not (verify-visited-file-modtime (current-buffer)))))))
a0213a97
RS
206;; Permit deleting frame if it would leave a visible or iconified frame.
207(put 'delete-frame 'menu-enable
208 '(let ((frames (frame-list))
209 (count 0))
210 (while frames
211 (if (cdr (assq 'visibility (frame-parameters (car frames))))
212 (setq count (1+ count)))
213 (setq frames (cdr frames)))
214 (> count 1)))
2f1139a4
RS
215(put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
216
db774a16
RS
217(put 'advertised-undo 'menu-enable
218 '(and (not (eq t buffer-undo-list))
219 (if (eq last-command 'undo)
2f1139a4
RS
220 (and (boundp 'pending-undo-list)
221 pending-undo-list)
222 buffer-undo-list)))
2877eac2
RS
223
224(defvar yank-menu-length 100
225 "*Maximum length of an item in the menu for \
226\\[mouse-menu-choose-yank].")
227
228(defun mouse-menu-choose-yank (event)
229 "Pop up a menu of the kill-ring for selection with the mouse.
230The kill-ring-yank-pointer is moved to the selected element.
231A subsequent \\[yank] yanks the choice just selected."
232 (interactive "e")
233 (let* ((count 0)
234 (menu (mapcar (lambda (string)
235 (if (> (length string) yank-menu-length)
236 (setq string (substring string
237 0 yank-menu-length)))
238 (prog1 (cons string count)
239 (setq count (1+ count))))
dfabc98f
RM
240 kill-ring))
241 (arg (x-popup-menu event
242 (list "Yank Menu"
75340584 243 (cons "Choose Next Yank" menu)))))
dfabc98f
RM
244 ;; A mouse click outside the menu returns nil.
245 ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
246 ;; XXX should this perhaps do something other than simply return? -rm
247 (if arg
248 (progn
03dcd202
RM
249 ;; We don't use `rotate-yank-pointer' because we want to move
250 ;; relative to the beginning of kill-ring, not the current
251 ;; position. Also, that would ask for any new X selection and
252 ;; thus change the list of items the user just chose from, which
253 ;; would be highly confusing.
254 (setq kill-ring-yank-pointer (nthcdr arg kill-ring))
dfabc98f
RM
255 (if (interactive-p)
256 (message "The next yank will insert the selected text.")
257 (current-kill 0))))))
258(put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
40954111 259\f
09642d97
RS
260(define-key global-map [menu-bar buffer] '("Buffers" . menu-bar-buffers))
261
262(defalias 'menu-bar-buffers (make-sparse-keymap "Buffers"))
40954111
RS
263
264(defvar complex-buffers-menu-p nil
265 "*Non-nil says, offer a choice of actions after you pick a buffer.
266This applies to the Buffers menu from the menu bar.")
267
268(defvar buffers-menu-max-size 10
269 "*Maximum number of entries which may appear on the Buffers menu.
270If this is 10, then only the ten most-recently-selected buffers are shown.
271If this is nil, then all buffers are shown.
272A large number or nil slows down menu responsiveness.")
273
d0690d12
RS
274(defvar list-buffers-directory nil)
275
09642d97
RS
276(defun menu-bar-select-buffer ()
277 (interactive)
278 (switch-to-buffer last-command-event))
279
280(defun menu-bar-select-frame ()
281 (interactive)
282 (make-frame-visible last-command-event)
283 (raise-frame last-command-event)
284 (select-frame last-command-event))
285
286(defun menu-bar-update-buffers ()
096ec7e7
KH
287 (if (frame-or-buffer-changed-p)
288 (let ((buffers (buffer-list))
289 (frames (frame-list))
290 buffers-menu frames-menu)
291 ;; If requested, list only the N most recently selected buffers.
292 (if (and (integerp buffers-menu-max-size)
293 (> buffers-menu-max-size 1))
294 (if (> (length buffers) buffers-menu-max-size)
295 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
296
297 ;; Make the menu of buffers proper.
298 (setq buffers-menu
299 (cons "Select Buffer"
300 (let ((tail buffers)
301 (maxbuf 0)
302 (maxlen 0)
303 alist
304 head)
305 (while tail
306 (or (eq ?\ (aref (buffer-name (car tail)) 0))
307 (setq maxbuf
308 (max maxbuf
309 (length (buffer-name (car tail))))))
310 (setq tail (cdr tail)))
311 (setq tail buffers)
312 (while tail
313 (let ((elt (car tail)))
314 (or (eq ?\ (aref (buffer-name elt) 0))
315 (setq alist (cons
316 (cons
317 (format
318 (format "%%%ds %%s%%s %%s"
319 maxbuf)
320 (buffer-name elt)
321 (if (buffer-modified-p elt)
322 "*" " ")
323 (save-excursion
324 (set-buffer elt)
325 (if buffer-read-only "%" " "))
326 (or (buffer-file-name elt)
327 (save-excursion
328 (set-buffer elt)
329 list-buffers-directory)
330 ""))
331 elt)
332 alist)))
333 (and alist (> (length (car (car alist))) maxlen)
334 (setq maxlen (length (car (car alist))))))
335 (setq tail (cdr tail)))
336 (setq alist (nreverse alist))
337 (nconc (mapcar '(lambda (pair)
338 ;; This is somewhat risque, to use
339 ;; the buffer name itself as the event
340 ;; type to define, but it works.
341 ;; It would not work to use the buffer
342 ;; since a buffer as an event has its
343 ;; own meaning.
344 (nconc (list (buffer-name (cdr pair))
345 (car pair)
346 (cons nil nil))
347 'menu-bar-select-buffer))
348 alist)
349 (list
350 (cons
351 'list-buffers
352 (cons
353 (concat (make-string (max (- (/ maxlen 2) 8) 0)
354 ?\ )
355 "List All Buffers")
356 'list-buffers)))))))
357
358
359 ;; Make a Frames menu if we have more than one frame.
360 (if (cdr frames)
361 (setq frames-menu
362 (cons "Select Frame"
363 (mapcar '(lambda (frame)
364 (nconc (list frame
365 (cdr (assq 'name
366 (frame-parameters frame)))
367 (cons nil nil))
368 'menu-bar-select-frame))
369 frames))))
370 (if buffers-menu
371 (setq buffers-menu (cons 'keymap buffers-menu)))
372 (if frames-menu
373 (setq frames-menu (cons 'keymap frames-menu)))
374 (define-key global-map [menu-bar buffer]
375 (cons "Buffers"
376 (if (and buffers-menu frames-menu)
377 (list 'keymap "Buffers and Frames"
378 (cons 'buffers (cons "Buffers" buffers-menu))
379 (cons 'frames (cons "Frames" frames-menu)))
380 (or buffers-menu frames-menu 'undefined)))))))
09642d97
RS
381
382(add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
2f1139a4 383
40954111
RS
384;; this version is too slow
385;;;(defun format-buffers-menu-line (buffer)
386;;; "Returns a string to represent the given buffer in the Buffer menu.
387;;;nil means the buffer shouldn't be listed. You can redefine this."
388;;; (if (string-match "\\` " (buffer-name buffer))
389;;; nil
390;;; (save-excursion
391;;; (set-buffer buffer)
392;;; (let ((size (buffer-size)))
393;;; (format "%s%s %-19s %6s %-15s %s"
394;;; (if (buffer-modified-p) "*" " ")
395;;; (if buffer-read-only "%" " ")
396;;; (buffer-name)
397;;; size
398;;; mode-name
399;;; (or (buffer-file-name) ""))))))
400\f
057d49d1 401(defun menu-bar-mode (flag)
dfd29450 402 "Toggle display of a menu bar on each frame.
057d49d1
RS
403This command applies to all frames that exist and frames to be
404created in the future.
405With a numeric argument, if the argument is negative,
dfd29450 406turn off menu bars; otherwise, turn on menu bars."
dad8e392 407 (interactive "P")
dad8e392
RM
408
409 ;; Obtain the current setting by looking at default-frame-alist.
410 (let ((menu-bar-mode
411 (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist)))
412 (if assq (cdr assq) 0))))))
413
414 ;; Tweedle it according to the argument.
415 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
e48f95c9 416 (> (prefix-numeric-value flag) 0)))
dad8e392
RM
417
418 ;; Apply it to default-frame-alist.
419 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
420 (if (consp parameter)
421 (setcdr parameter (if menu-bar-mode 1 0))
422 (setq default-frame-alist
423 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
424 default-frame-alist))))
425
426 ;; Apply it to existing frames.
427 (let ((frames (frame-list)))
428 (while frames
c32b0bce 429 (let ((height (cdr (assq 'height (frame-parameters (car frames))))))
17c4c3c4
RS
430 (modify-frame-parameters (car frames)
431 (list (cons 'menu-bar-lines
dad8e392 432 (if menu-bar-mode 1 0))))
17c4c3c4
RS
433 (modify-frame-parameters (car frames)
434 (list (cons 'height height))))
dad8e392 435 (setq frames (cdr frames))))))
1db87953 436
bffa5d4d
RS
437(provide 'menu-bar)
438
235aa29b 439;;; menu-bar.el ends here