1 ;;; menu-bar.el --- define a default menu bar.
6 ;; Copyright (C) 1993 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 (define-key global-map
[menu-bar
] (make-sparse-keymap "menu-bar"))
27 (defvar menu-bar-help-menu
(make-sparse-keymap "Help"))
28 ;; Put Help item last.
29 (setq menu-bar-final-items
'(help))
30 (define-key global-map
[menu-bar help
] (cons "Help" menu-bar-help-menu
))
31 (defvar menu-bar-edit-menu
(make-sparse-keymap "Edit"))
32 (define-key global-map
[menu-bar edit
] (cons "Edit" menu-bar-edit-menu
))
33 (defvar menu-bar-file-menu
(make-sparse-keymap "File"))
34 (define-key global-map
[menu-bar file
] (cons "File" menu-bar-file-menu
))
36 (define-key menu-bar-file-menu
[exit-emacs
]
37 '("Exit Emacs" . save-buffers-kill-emacs
))
38 (define-key menu-bar-file-menu
[kill-buffer
]
39 '("Kill Buffer" . kill-this-buffer
))
40 (define-key menu-bar-file-menu
[delete-frame
] '("Delete Frame" . delete-frame
))
41 (define-key menu-bar-file-menu
[print-buffer
] '("Print Buffer" . print-buffer
))
42 (define-key menu-bar-file-menu
[revert-buffer
]
43 '("Revert Buffer" . revert-buffer
))
44 (define-key menu-bar-file-menu
[write-file
]
45 '("Save Buffer As..." . write-file
))
46 (define-key menu-bar-file-menu
[save-buffer
] '("Save Buffer" . save-buffer
))
47 (define-key menu-bar-file-menu
[dired] '("Open Directory..." . dired))
48 (define-key menu-bar-file-menu [open-file] '("Open File..." . find-file))
49 (define-key menu-bar-file-menu [new-frame] '("New Frame" . new-frame))
52 (define-key menu-bar-edit-menu [spell] '("Spell..." . ispell-menu-map))
53 (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
54 (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
55 (define-key menu-bar-edit-menu [choose-next-paste]
56 '("Choose Next Paste" . mouse-menu-choose-yank))
57 (define-key menu-bar-edit-menu [paste] '("Paste" . yank))
58 (define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save))
59 (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
60 (define-key menu-bar-edit-menu [undo] '("Undo" . undo))
62 (put 'fill-region 'menu-enable 'mark-active)
63 (put 'kill-region 'menu-enable 'mark-active)
64 (put 'kill-ring-save 'menu-enable 'mark-active)
65 (put 'yank 'menu-enable '(x-selection-exists-p))
66 (put 'delete-region 'menu-enable 'mark-active)
67 (put 'undo 'menu-enable '(if (eq last-command 'undo)
69 (consp buffer-undo-list)))
71 (autoload 'ispell-menu-map "ispell" nil t 'keymap)
73 (define-key menu-bar-help-menu [emacs-tutorial]
74 '("Emacs Tutorial" . help-with-tutorial))
75 (define-key menu-bar-help-menu [man] '("Man..." . manual-entry))
76 (define-key menu-bar-help-menu [describe-variable]
77 '("Describe Variable..." . describe-variable))
78 (define-key menu-bar-help-menu [describe-function]
79 '("Describe Function..." . describe-function))
80 (define-key menu-bar-help-menu [describe-key]
81 '("Describe Key..." . describe-key))
82 (define-key menu-bar-help-menu [list-keybindings]
83 '("List Keybindings" . describe-bindings))
84 (define-key menu-bar-help-menu [command-apropos]
85 '("Command Apropos..." . command-apropos))
86 (define-key menu-bar-help-menu [describe-mode]
87 '("Describe Mode" . describe-mode))
88 (define-key menu-bar-help-menu [info] '("Info" . info))
90 (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
91 (defun kill-this-buffer () ; for the menubar
92 "Kills the current buffer."
94 (kill-buffer (current-buffer)))
96 (defun kill-this-buffer-enabled-p ()
98 (buffers (buffer-list)))
100 (or (string-match "^ " (buffer-name (car buffers)))
101 (setq count (1+ count)))
102 (setq buffers (cdr buffers)))
105 (put 'save-buffer 'menu-enable '(buffer-modified-p))
106 (put 'revert-buffer 'menu-enable
107 '(or revert-buffer-function revert-buffer-insert-file-contents-function
108 (and (buffer-file-name)
109 (not (verify-visited-file-modtime (current-buffer))))))
110 (put 'delete-frame 'menu-enable '(cdr (visible-frame-list)))
111 (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
113 (put 'advertised-undo 'menu-enable
114 '(and (not (eq t buffer-undo-list))
115 (if (eq last-command 'undo)
116 (and (boundp 'pending-undo-list)
120 (defvar yank-menu-length 100
121 "*Maximum length of an item in the menu for \
122 \\[mouse-menu-choose-yank].")
124 (defun mouse-menu-choose-yank (event)
125 "Pop up a menu of the kill-ring for selection with the mouse.
126 The kill-ring-yank-pointer is moved to the selected element.
127 A subsequent \\[yank] yanks the choice just selected."
130 (menu (mapcar (lambda (string)
131 (if (> (length string) yank-menu-length)
132 (setq string (substring string
133 0 yank-menu-length)))
134 (prog1 (cons string count)
135 (setq count (1+ count))))
137 (arg (x-popup-menu event
139 (cons "Choose Next Yank" menu)))))
140 ;; A mouse click outside the menu returns nil.
141 ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
142 ;; XXX should this perhaps do something other than simply return? -rm
145 (rotate-yank-pointer arg)
147 (message "The next yank will insert the selected text.")
148 (current-kill 0))))))
149 (put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
151 (define-key global-map [menu-bar buffer] '("Buffers" . mouse-menu-bar-buffers))
153 (defvar complex-buffers-menu-p nil
154 "*Non-nil says, offer a choice of actions after you pick a buffer.
155 This applies to the Buffers menu from the menu bar.")
157 (defvar buffers-menu-max-size 10
158 "*Maximum number of entries which may appear on the Buffers menu.
159 If this is 10, then only the ten most-recently-selected buffers are shown.
160 If this is nil, then all buffers are shown.
161 A large number or nil slows down menu responsiveness.")
163 (defun mouse-menu-bar-buffers (event)
164 "Pop up a menu of buffers for selection with the mouse.
165 This switches buffers in the window that you clicked on,
166 and selects that window."
168 (let ((buffers (buffer-list))
170 ;; If requested, list only the N most recently selected buffers.
171 (if (and (integerp buffers-menu-max-size)
172 (> buffers-menu-max-size 1))
173 (if (> (length buffers) buffers-menu-max-size)
174 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
176 (cons "Select Buffer"
182 (or (eq ?\ (aref (buffer-name (car tail)) 0))
185 (length (buffer-name (car tail))))))
186 (setq tail (cdr tail)))
189 (let ((elt (car tail)))
190 (if (not (string-match "^ "
195 (format "%%%ds %%s%%s %%s"
198 (if (buffer-modified-p elt)
202 (if buffer-read-only "%" " "))
203 (or (buffer-file-name elt) ""))
206 (and head (> (length (car (car head))) maxlen)
207 (setq maxlen (length (car (car head))))))
208 (setq tail (cdr tail)))
209 (nconc (nreverse head)
211 (concat (make-string (max (- (/ maxlen
217 (setq menu (list menu))
219 (if (cdr (frame-list))
221 (cons (cons "Select Frame"
222 (mapcar (lambda (frame)
223 (cons (cdr (assq 'name
224 (frame-parameters frame)))
228 (setq menu (cons "Buffer and Frame Menu" menu))
230 (let ((buf (x-popup-menu (if (listp event) event
231 (cons '(0 0) (selected-frame)))
233 (window (and (listp event) (posn-window (event-start event)))))
235 (make-frame-visible buf)
238 ((eq buf 'list-buffers)
241 (if complex-buffers-menu-p
242 (let ((action (x-popup-menu
243 (if (listp event) event
244 (cons '(0 0) (selected-frame)))
247 ("Save Buffer" . save-buffer)
248 ("Kill Buffer" . kill-buffer)
249 ("Select Buffer" . switch-to-buffer))))))
250 (if (eq action 'save-buffer)
254 (funcall action buf)))
255 (and (windowp window)
256 (select-window window))
257 (switch-to-buffer buf)))))))
259 ;; this version is too slow
260 ;;;(defun format-buffers-menu-line (buffer)
261 ;;; "Returns a string to represent the given buffer in the Buffer menu.
262 ;;;nil means the buffer shouldn't be listed. You can redefine this."
263 ;;; (if (string-match "\\` " (buffer-name buffer))
266 ;;; (set-buffer buffer)
267 ;;; (let ((size (buffer-size)))
268 ;;; (format "%s%s %-19s %6s %-15s %s"
269 ;;; (if (buffer-modified-p) "*" " ")
270 ;;; (if buffer-read-only "%" " ")
274 ;;; (or (buffer-file-name) ""))))))
276 (defvar menu-bar-mode nil)
278 (defun menu-bar-mode (flag)
279 "Toggle display of a menu bar on each frame.
280 This command applies to all frames that exist and frames to be
281 created in the future.
282 With a numeric argument, if the argument is negative,
283 turn off menu bars; otherwise, turn on menu bars."
285 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
286 (or (not (numberp flag)) (>= flag 0))))
287 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
288 (if (consp parameter)
289 (setcdr parameter (if menu-bar-mode 1 0))
290 (setq default-frame-alist
291 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
292 default-frame-alist))))
293 (let ((frames (frame-list)))
295 ;; Turn menu bar on or off in existing frames.
296 ;; (Except for minibuffer-only frames.)
297 (or (eq 'only (cdr (assq 'minibuffer (frame-parameters (car frames)))))
298 (modify-frame-parameters
300 (list (if menu-bar-mode
301 '(menu-bar-lines . 1)
302 '(menu-bar-lines . 0)))))
303 (setq frames (cdr frames)))))
305 ;; Make frames created from now on have a menu bar.
311 ;;; menu-bar.el ends here