(report-emacs-bug): Insert at separator, not at end.
[emacs.git] / lisp / menu-bar.el
blob063854e14674c1c7fbdafae5a321d1393354ceeb
1 ;;; menu-bar.el --- define a default menu bar.
3 ;; Author: RMS
4 ;; Keywords: internal
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)
13 ;; any later version.
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.
24 ;;; Code:
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)
86 pending-undo-list
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."
102 (interactive)
103 (let ((x-select-enable-clipboard t))
104 (yank)))
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)))
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)))
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)))
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-tutorial]
136 '("Emacs Tutorial" . help-with-tutorial))
137 (define-key menu-bar-help-menu [man] '("Man..." . manual-entry))
138 (define-key menu-bar-help-menu [describe-variable]
139 '("Describe Variable..." . describe-variable))
140 (define-key menu-bar-help-menu [describe-function]
141 '("Describe Function..." . describe-function))
142 (define-key menu-bar-help-menu [describe-key]
143 '("Describe Key..." . describe-key))
144 (define-key menu-bar-help-menu [list-keybindings]
145 '("List Keybindings" . describe-bindings))
146 (define-key menu-bar-help-menu [command-apropos]
147 '("Command Apropos..." . command-apropos))
148 (define-key menu-bar-help-menu [describe-mode]
149 '("Describe Mode" . describe-mode))
150 (define-key menu-bar-help-menu [info] '("Info" . info))
152 (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
153 (defun kill-this-buffer () ; for the menubar
154 "Kills the current buffer."
155 (interactive)
156 (kill-buffer (current-buffer)))
158 (defun kill-this-buffer-enabled-p ()
159 (let ((count 0)
160 (buffers (buffer-list)))
161 (while buffers
162 (or (string-match "^ " (buffer-name (car buffers)))
163 (setq count (1+ count)))
164 (setq buffers (cdr buffers)))
165 (> count 1)))
167 (put 'save-buffer 'menu-enable '(buffer-modified-p))
168 (put 'revert-buffer 'menu-enable
169 '(or revert-buffer-function revert-buffer-insert-file-contents-function
170 (and (buffer-file-name)
171 (or (buffer-modified-p)
172 (not (verify-visited-file-modtime (current-buffer)))))))
173 ;; Permit deleting frame if it would leave a visible or iconified frame.
174 (put 'delete-frame 'menu-enable
175 '(let ((frames (frame-list))
176 (count 0))
177 (while frames
178 (if (cdr (assq 'visibility (frame-parameters (car frames))))
179 (setq count (1+ count)))
180 (setq frames (cdr frames)))
181 (> count 1)))
182 (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
184 (put 'advertised-undo 'menu-enable
185 '(and (not (eq t buffer-undo-list))
186 (if (eq last-command 'undo)
187 (and (boundp 'pending-undo-list)
188 pending-undo-list)
189 buffer-undo-list)))
191 (defvar yank-menu-length 100
192 "*Maximum length of an item in the menu for \
193 \\[mouse-menu-choose-yank].")
195 (defun mouse-menu-choose-yank (event)
196 "Pop up a menu of the kill-ring for selection with the mouse.
197 The kill-ring-yank-pointer is moved to the selected element.
198 A subsequent \\[yank] yanks the choice just selected."
199 (interactive "e")
200 (let* ((count 0)
201 (menu (mapcar (lambda (string)
202 (if (> (length string) yank-menu-length)
203 (setq string (substring string
204 0 yank-menu-length)))
205 (prog1 (cons string count)
206 (setq count (1+ count))))
207 kill-ring))
208 (arg (x-popup-menu event
209 (list "Yank Menu"
210 (cons "Choose Next Yank" menu)))))
211 ;; A mouse click outside the menu returns nil.
212 ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
213 ;; XXX should this perhaps do something other than simply return? -rm
214 (if arg
215 (progn
216 ;; We don't use `rotate-yank-pointer' because we want to move
217 ;; relative to the beginning of kill-ring, not the current
218 ;; position. Also, that would ask for any new X selection and
219 ;; thus change the list of items the user just chose from, which
220 ;; would be highly confusing.
221 (setq kill-ring-yank-pointer (nthcdr arg kill-ring))
222 (if (interactive-p)
223 (message "The next yank will insert the selected text.")
224 (current-kill 0))))))
225 (put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
227 (define-key global-map [menu-bar buffer] '("Buffers" . mouse-menu-bar-buffers))
229 (defvar complex-buffers-menu-p nil
230 "*Non-nil says, offer a choice of actions after you pick a buffer.
231 This applies to the Buffers menu from the menu bar.")
233 (defvar buffers-menu-max-size 10
234 "*Maximum number of entries which may appear on the Buffers menu.
235 If this is 10, then only the ten most-recently-selected buffers are shown.
236 If this is nil, then all buffers are shown.
237 A large number or nil slows down menu responsiveness.")
239 (defvar list-buffers-directory nil)
241 (defun mouse-menu-bar-buffers (event)
242 "Pop up a menu of buffers for selection with the mouse.
243 This switches buffers in the window that you clicked on,
244 and selects that window."
245 (interactive "e")
246 (let ((buffers (buffer-list))
247 menu)
248 ;; If requested, list only the N most recently selected buffers.
249 (if (and (integerp buffers-menu-max-size)
250 (> buffers-menu-max-size 1))
251 (if (> (length buffers) buffers-menu-max-size)
252 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
253 (setq menu
254 (cons "Select Buffer"
255 (let ((tail buffers)
256 (maxbuf 0)
257 (maxlen 0)
258 head)
259 (while tail
260 (or (eq ?\ (aref (buffer-name (car tail)) 0))
261 (setq maxbuf
262 (max maxbuf
263 (length (buffer-name (car tail))))))
264 (setq tail (cdr tail)))
265 (setq tail buffers)
266 (while tail
267 (let ((elt (car tail)))
268 (if (not (string-match "^ "
269 (buffer-name elt)))
270 (setq head (cons
271 (cons
272 (format
273 (format "%%%ds %%s%%s %%s"
274 maxbuf)
275 (buffer-name elt)
276 (if (buffer-modified-p elt)
277 "*" " ")
278 (save-excursion
279 (set-buffer elt)
280 (if buffer-read-only "%" " "))
281 (or (buffer-file-name elt)
282 (save-excursion
283 (set-buffer elt)
284 list-buffers-directory)
285 ""))
286 elt)
287 head)))
288 (and head (> (length (car (car head))) maxlen)
289 (setq maxlen (length (car (car head))))))
290 (setq tail (cdr tail)))
291 (nconc (nreverse head)
292 (list (cons
293 (concat (make-string (max (- (/ maxlen
296 0) ?\ )
297 "List All Buffers")
298 'list-buffers))))))
299 (if (cdr (frame-list))
300 (setq menu
301 (list menu
302 (cons "Select Frame"
303 (mapcar (lambda (frame)
304 (cons (cdr (assq 'name
305 (frame-parameters frame)))
306 frame))
307 (frame-list)))))
308 (setq menu (list menu)))
310 (setq menu (cons "Buffer and Frame Menu" menu))
312 (let ((buf (x-popup-menu (if (listp event) event
313 (list '(0 0) (selected-frame)))
314 menu))
315 (window (and (listp event) (posn-window (event-start event)))))
316 (cond ((framep buf)
317 (make-frame-visible buf)
318 (raise-frame buf)
319 (select-frame buf))
320 ((eq buf 'list-buffers)
321 (list-buffers))
322 (buf
323 (if complex-buffers-menu-p
324 (let ((action (x-popup-menu
325 (if (listp event) event
326 (list '(0 0) (selected-frame)))
327 '("Buffer Action"
329 ("Save Buffer" . save-buffer)
330 ("Kill Buffer" . kill-buffer)
331 ("Select Buffer" . switch-to-buffer))))))
332 (if (eq action 'save-buffer)
333 (save-excursion
334 (set-buffer buf)
335 (save-buffer))
336 (funcall action buf)))
337 (and (windowp window)
338 (select-window window))
339 (switch-to-buffer buf)))))))
341 ;; this version is too slow
342 ;;;(defun format-buffers-menu-line (buffer)
343 ;;; "Returns a string to represent the given buffer in the Buffer menu.
344 ;;;nil means the buffer shouldn't be listed. You can redefine this."
345 ;;; (if (string-match "\\` " (buffer-name buffer))
346 ;;; nil
347 ;;; (save-excursion
348 ;;; (set-buffer buffer)
349 ;;; (let ((size (buffer-size)))
350 ;;; (format "%s%s %-19s %6s %-15s %s"
351 ;;; (if (buffer-modified-p) "*" " ")
352 ;;; (if buffer-read-only "%" " ")
353 ;;; (buffer-name)
354 ;;; size
355 ;;; mode-name
356 ;;; (or (buffer-file-name) ""))))))
358 (defun menu-bar-mode (flag)
359 "Toggle display of a menu bar on each frame.
360 This command applies to all frames that exist and frames to be
361 created in the future.
362 With a numeric argument, if the argument is negative,
363 turn off menu bars; otherwise, turn on menu bars."
364 (interactive "P")
366 ;; Obtain the current setting by looking at default-frame-alist.
367 (let ((menu-bar-mode
368 (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist)))
369 (if assq (cdr assq) 0))))))
371 ;; Tweedle it according to the argument.
372 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
373 (> (prefix-numeric-value flag) 0)))
375 ;; Apply it to default-frame-alist.
376 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
377 (if (consp parameter)
378 (setcdr parameter (if menu-bar-mode 1 0))
379 (setq default-frame-alist
380 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
381 default-frame-alist))))
383 ;; Apply it to existing frames.
384 (let ((frames (frame-list)))
385 (while frames
386 (modify-frame-parameters (car frames)
387 (list (cons 'menu-bar-lines
388 (if menu-bar-mode 1 0))))
389 (setq frames (cdr frames))))))
391 ;; Make frames created from now on have a menu bar.
392 (if window-system
393 (menu-bar-mode t))
395 (provide 'menu-bar)
397 ;;; menu-bar.el ends here