(bzero, bcmp, bcopy): New macros.
[bpt/emacs.git] / lisp / menu-bar.el
CommitLineData
235aa29b
ER
1;;; menu-bar.el --- define a default menu bar.
2
3;; Author: RMS
4;; Keywords: internals
5
1db87953
RS
6;; Copyright (C) 1993 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
235aa29b 24;;; Code:
1db87953 25
db774a16 26(define-key global-map [menu-bar] (make-sparse-keymap "menu-bar"))
22390f50 27(defvar menu-bar-help-menu (make-sparse-keymap "Help"))
da183f87
RS
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))
22390f50 31(defvar menu-bar-edit-menu (make-sparse-keymap "Edit"))
40954111 32(define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
22390f50 33(defvar menu-bar-file-menu (make-sparse-keymap "File"))
40954111 34(define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
db774a16 35
ca9b40a1 36(define-key menu-bar-file-menu [exit-emacs]
db774a16 37 '("Exit Emacs" . save-buffers-kill-emacs))
2f1139a4
RS
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))
76202b57 47(define-key menu-bar-file-menu [dired] '("Open Directory..." . dired))
2f1139a4
RS
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))
db774a16 50
9e18f0a0
RS
51
52(define-key menu-bar-edit-menu [spell] '("Spell..." . ispell-menu-map))
057d49d1
RS
53(define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
54(define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
75340584 55(define-key menu-bar-edit-menu [choose-next-paste]
b289b04e 56 '("Choose Next Paste" . mouse-menu-choose-yank))
057d49d1
RS
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))
61
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)
68 pending-undo-list
69 (consp buffer-undo-list)))
db774a16 70
9e18f0a0
RS
71(autoload 'ispell-menu-map "ispell" nil t 'keymap)
72
ca9b40a1 73(define-key menu-bar-help-menu [emacs-tutorial]
db774a16 74 '("Emacs Tutorial" . help-with-tutorial))
2f1139a4
RS
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))
db774a16 89
2f1139a4 90(define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
db774a16
RS
91(defun kill-this-buffer () ; for the menubar
92 "Kills the current buffer."
93 (interactive)
94 (kill-buffer (current-buffer)))
95
2f1139a4
RS
96(defun kill-this-buffer-enabled-p ()
97 (let ((count 0)
98 (buffers (buffer-list)))
99 (while buffers
100 (or (string-match "^ " (buffer-name (car buffers)))
101 (setq count (1+ count)))
102 (setq buffers (cdr buffers)))
103 (> count 1)))
104
db774a16 105(put 'save-buffer 'menu-enable '(buffer-modified-p))
da183f87
RS
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))))))
2f1139a4
RS
110(put 'delete-frame 'menu-enable '(cdr (visible-frame-list)))
111(put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
112
db774a16
RS
113(put 'advertised-undo 'menu-enable
114 '(and (not (eq t buffer-undo-list))
115 (if (eq last-command 'undo)
2f1139a4
RS
116 (and (boundp 'pending-undo-list)
117 pending-undo-list)
118 buffer-undo-list)))
2877eac2
RS
119
120(defvar yank-menu-length 100
121 "*Maximum length of an item in the menu for \
122\\[mouse-menu-choose-yank].")
123
124(defun mouse-menu-choose-yank (event)
125 "Pop up a menu of the kill-ring for selection with the mouse.
126The kill-ring-yank-pointer is moved to the selected element.
127A subsequent \\[yank] yanks the choice just selected."
128 (interactive "e")
129 (let* ((count 0)
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))))
dfabc98f
RM
136 kill-ring))
137 (arg (x-popup-menu event
138 (list "Yank Menu"
75340584 139 (cons "Choose Next Yank" menu)))))
dfabc98f
RM
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
143 (if arg
144 (progn
145 (rotate-yank-pointer arg)
146 (if (interactive-p)
147 (message "The next yank will insert the selected text.")
148 (current-kill 0))))))
149(put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
40954111 150\f
98bee59b 151(define-key global-map [menu-bar buffer] '("Buffers" . mouse-menu-bar-buffers))
40954111
RS
152
153(defvar complex-buffers-menu-p nil
154 "*Non-nil says, offer a choice of actions after you pick a buffer.
155This applies to the Buffers menu from the menu bar.")
156
157(defvar buffers-menu-max-size 10
158 "*Maximum number of entries which may appear on the Buffers menu.
159If this is 10, then only the ten most-recently-selected buffers are shown.
160If this is nil, then all buffers are shown.
161A large number or nil slows down menu responsiveness.")
162
98bee59b 163(defun mouse-menu-bar-buffers (event)
40954111
RS
164 "Pop up a menu of buffers for selection with the mouse.
165This switches buffers in the window that you clicked on,
166and selects that window."
167 (interactive "e")
168 (let ((buffers (buffer-list))
169 menu)
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)))
175 (setq menu
e936d399
RM
176 (cons "Select Buffer"
177 (let ((tail buffers)
178 (maxbuf 0)
179 (maxlen 0)
180 head)
181 (while tail
182 (or (eq ?\ (aref (buffer-name (car tail)) 0))
183 (setq maxbuf
184 (max maxbuf
185 (length (buffer-name (car tail))))))
186 (setq tail (cdr tail)))
187 (setq tail buffers)
188 (while tail
189 (let ((elt (car tail)))
190 (if (not (string-match "^ "
191 (buffer-name elt)))
192 (setq head (cons
193 (cons
194 (format
195 (format "%%%ds %%s%%s %%s"
196 maxbuf)
197 (buffer-name elt)
198 (if (buffer-modified-p elt)
199 "*" " ")
200 (save-excursion
201 (set-buffer elt)
202 (if buffer-read-only "%" " "))
203 (or (buffer-file-name elt) ""))
204 elt)
205 head)))
206 (and head (> (length (car (car head))) maxlen)
207 (setq maxlen (length (car (car head))))))
208 (setq tail (cdr tail)))
209 (nconc (nreverse head)
210 (list (cons
211 (concat (make-string (max (- (/ maxlen
212 2)
213 8)
214 0) ?\ )
215 "List All Buffers")
216 'list-buffers))))))
217 (setq menu (list menu))
40954111 218
e936d399
RM
219 (if (cdr (frame-list))
220 (setq menu
221 (cons (cons "Select Frame"
222 (mapcar (lambda (frame)
223 (cons (cdr (assq 'name
224 (frame-parameters frame)))
225 frame))
226 (frame-list)))
227 menu)))
228 (setq menu (cons "Buffer and Frame Menu" menu))
40954111
RS
229
230 (let ((buf (x-popup-menu (if (listp event) event
231 (cons '(0 0) (selected-frame)))
232 menu))
233 (window (and (listp event) (posn-window (event-start event)))))
e936d399
RM
234 (cond ((framep buf)
235 (make-frame-visible buf)
236 (raise-frame buf)
237 (select-frame buf))
238 ((eq buf 'list-buffers)
239 (list-buffers))
240 (buf
241 (if complex-buffers-menu-p
242 (let ((action (x-popup-menu
243 (if (listp event) event
244 (cons '(0 0) (selected-frame)))
245 '("Buffer Action"
246 (""
247 ("Save Buffer" . save-buffer)
248 ("Kill Buffer" . kill-buffer)
249 ("Select Buffer" . switch-to-buffer))))))
250 (if (eq action 'save-buffer)
251 (save-excursion
252 (set-buffer buf)
253 (save-buffer))
254 (funcall action buf)))
255 (and (windowp window)
256 (select-window window))
257 (switch-to-buffer buf)))))))
2f1139a4 258
40954111
RS
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))
264;;; nil
265;;; (save-excursion
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 "%" " ")
271;;; (buffer-name)
272;;; size
273;;; mode-name
274;;; (or (buffer-file-name) ""))))))
275\f
057d49d1
RS
276(defvar menu-bar-mode nil)
277
278(defun menu-bar-mode (flag)
dfd29450 279 "Toggle display of a menu bar on each frame.
057d49d1
RS
280This command applies to all frames that exist and frames to be
281created in the future.
282With a numeric argument, if the argument is negative,
dfd29450 283turn off menu bars; otherwise, turn on menu bars."
057d49d1
RS
284 (interactive "P")
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)))
294 (while frames
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
299 (car frames)
300 (list (if menu-bar-mode
301 '(menu-bar-lines . 1)
302 '(menu-bar-lines . 0)))))
303 (setq frames (cdr frames)))))
1db87953
RS
304
305;; Make frames created from now on have a menu bar.
d0dbd3bf
RS
306(if window-system
307 (menu-bar-mode t))
1db87953 308
bffa5d4d
RS
309(provide 'menu-bar)
310
235aa29b 311;;; menu-bar.el ends here