(sc-cite-original): Add autoload.
[bpt/emacs.git] / lisp / menu-bar.el
1 ;;; menu-bar.el --- define a default menu bar.
2
3 ;; Author: RMS
4 ;; Keywords: internal
5
6 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
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
24 ;;; Code:
25
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))
38
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 [make-frame] '("Make New Frame" . make-frame))
59
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))
79
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)
86 pending-undo-list
87 (consp buffer-undo-list)))
88 (put 'query-replace 'menu-enable (not buffer-read-only))
89
90 (autoload 'ispell-menu-map "ispell" nil t 'keymap)
91
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
94
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)))
99
100 (defun clipboard-yank ()
101 "Reinsert the last stretch of killed text, or the clipboard contents."
102 (interactive)
103 (let ((x-select-enable-clipboard t))
104 (yank)))
105
106 (defun clipboard-kill-ring-save (beg end)
107 "Copy region to kill ring, and save in the X clipboard."
108 (interactive "r")
109 (let ((x-select-enable-clipboard t))
110 (kill-ring-save beg end)))
111
112 (defun clipboard-kill-region (beg end)
113 "Kill the region, and save it in the X clipboard."
114 (interactive "r")
115 (let ((x-select-enable-clipboard t))
116 (kill-region beg end)))
117
118 (defun menu-bar-enable-clipboard ()
119 "Make the menu bar CUT, PASTE and COPY items use the clipboard."
120 (interactive)
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)))
129
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)
134 \f
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))
155
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."
159 (interactive)
160 (kill-buffer (current-buffer)))
161
162 (defun kill-this-buffer-enabled-p ()
163 (let ((count 0)
164 (buffers (buffer-list)))
165 (while buffers
166 (or (string-match "^ " (buffer-name (car buffers)))
167 (setq count (1+ count)))
168 (setq buffers (cdr buffers)))
169 (> count 1)))
170
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))
180 (count 0))
181 (while frames
182 (if (cdr (assq 'visibility (frame-parameters (car frames))))
183 (setq count (1+ count)))
184 (setq frames (cdr frames)))
185 (> count 1)))
186 (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
187
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)
192 pending-undo-list)
193 buffer-undo-list)))
194
195 (defvar yank-menu-length 100
196 "*Maximum length of an item in the menu for \
197 \\[mouse-menu-choose-yank].")
198
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."
203 (interactive "e")
204 (let* ((count 0)
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))))
211 kill-ring))
212 (arg (x-popup-menu event
213 (list "Yank Menu"
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
218 (if arg
219 (progn
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))
226 (if (interactive-p)
227 (message "The next yank will insert the selected text.")
228 (current-kill 0))))))
229 (put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
230 \f
231 (define-key global-map [menu-bar buffer] '("Buffers" . menu-bar-buffers))
232
233 (defalias 'menu-bar-buffers (make-sparse-keymap "Buffers"))
234
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.")
238
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.")
244
245 (defvar list-buffers-directory nil)
246
247 (defun menu-bar-select-buffer ()
248 (interactive)
249 (switch-to-buffer last-command-event))
250
251 (defun menu-bar-select-frame ()
252 (interactive)
253 (make-frame-visible last-command-event)
254 (raise-frame last-command-event)
255 (select-frame last-command-event))
256
257 (defvar menu-bar-update-buffers-last-buffers nil)
258 (defvar menu-bar-update-buffers-last-frames nil)
259
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))
266 nil
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)))
274
275 ;; Make the menu of buffers proper.
276 (setq buffers-menu
277 (cons "Select Buffer"
278 (let ((tail buffers)
279 (maxbuf 0)
280 (maxlen 0)
281 alist
282 head)
283 (while tail
284 (or (eq ?\ (aref (buffer-name (car tail)) 0))
285 (setq maxbuf
286 (max maxbuf
287 (length (buffer-name (car tail))))))
288 (setq tail (cdr tail)))
289 (setq tail buffers)
290 (while tail
291 (let ((elt (car tail)))
292 (if (not (string-match "^ "
293 (buffer-name elt)))
294 (setq alist (cons
295 (cons
296 (format
297 (format "%%%ds %%s%%s %%s"
298 maxbuf)
299 (buffer-name elt)
300 (if (buffer-modified-p elt)
301 "*" " ")
302 (save-excursion
303 (set-buffer elt)
304 (if buffer-read-only "%" " "))
305 (or (buffer-file-name elt)
306 (save-excursion
307 (set-buffer elt)
308 list-buffers-directory)
309 ""))
310 elt)
311 alist)))
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
322 ;; own meaning.
323 (nconc (list (buffer-name (cdr pair))
324 (car pair)
325 (cons nil nil))
326 'menu-bar-select-buffer))
327 alist)
328 (list (cons 'list-buffers
329 (cons
330 (concat (make-string (max (- (/ maxlen
331 2)
332 8)
333 0) ?\ )
334 "List All Buffers")
335 'list-buffers)))))))
336
337
338 ;; Make a Frames menu if we have more than one frame.
339 (if (cdr frames)
340 (setq frames-menu
341 (cons "Select Frame"
342 (mapcar '(lambda (frame)
343 (nconc (list frame
344 (cdr (assq 'name
345 (frame-parameters frame)))
346 (cons nil nil))
347 'menu-bar-select-frame))
348 frames))))
349 (if buffers-menu
350 (setq buffers-menu (cons 'keymap buffers-menu)))
351 (if frames-menu
352 (setq frames-menu (cons 'keymap frames-menu)))
353 (define-key global-map [menu-bar buffer]
354 (cons "Buffers"
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)))))))
360
361 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
362
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))
368 ;;; nil
369 ;;; (save-excursion
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 "%" " ")
375 ;;; (buffer-name)
376 ;;; size
377 ;;; mode-name
378 ;;; (or (buffer-file-name) ""))))))
379 \f
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."
386 (interactive "P")
387
388 ;; Obtain the current setting by looking at default-frame-alist.
389 (let ((menu-bar-mode
390 (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist)))
391 (if assq (cdr assq) 0))))))
392
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)))
396
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))))
404
405 ;; Apply it to existing frames.
406 (let ((frames (frame-list)))
407 (while frames
408 (modify-frame-parameters (car frames)
409 (list (cons 'menu-bar-lines
410 (if menu-bar-mode 1 0))))
411 (setq frames (cdr frames))))))
412
413 (provide 'menu-bar)
414
415 ;;; menu-bar.el ends here