(Fx_popup_menu): Adjust coords so they are relative to the inner window.
[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))))))
a0213a97
RS
110;; Permit deleting frame if it would leave a visible or iconified frame.
111(put 'delete-frame 'menu-enable
112 '(let ((frames (frame-list))
113 (count 0))
114 (while frames
115 (if (cdr (assq 'visibility (frame-parameters (car frames))))
116 (setq count (1+ count)))
117 (setq frames (cdr frames)))
118 (> count 1)))
2f1139a4
RS
119(put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
120
db774a16
RS
121(put 'advertised-undo 'menu-enable
122 '(and (not (eq t buffer-undo-list))
123 (if (eq last-command 'undo)
2f1139a4
RS
124 (and (boundp 'pending-undo-list)
125 pending-undo-list)
126 buffer-undo-list)))
2877eac2
RS
127
128(defvar yank-menu-length 100
129 "*Maximum length of an item in the menu for \
130\\[mouse-menu-choose-yank].")
131
132(defun mouse-menu-choose-yank (event)
133 "Pop up a menu of the kill-ring for selection with the mouse.
134The kill-ring-yank-pointer is moved to the selected element.
135A subsequent \\[yank] yanks the choice just selected."
136 (interactive "e")
137 (let* ((count 0)
138 (menu (mapcar (lambda (string)
139 (if (> (length string) yank-menu-length)
140 (setq string (substring string
141 0 yank-menu-length)))
142 (prog1 (cons string count)
143 (setq count (1+ count))))
dfabc98f
RM
144 kill-ring))
145 (arg (x-popup-menu event
146 (list "Yank Menu"
75340584 147 (cons "Choose Next Yank" menu)))))
dfabc98f
RM
148 ;; A mouse click outside the menu returns nil.
149 ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
150 ;; XXX should this perhaps do something other than simply return? -rm
151 (if arg
152 (progn
153 (rotate-yank-pointer arg)
154 (if (interactive-p)
155 (message "The next yank will insert the selected text.")
156 (current-kill 0))))))
157(put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
40954111 158\f
98bee59b 159(define-key global-map [menu-bar buffer] '("Buffers" . mouse-menu-bar-buffers))
40954111
RS
160
161(defvar complex-buffers-menu-p nil
162 "*Non-nil says, offer a choice of actions after you pick a buffer.
163This applies to the Buffers menu from the menu bar.")
164
165(defvar buffers-menu-max-size 10
166 "*Maximum number of entries which may appear on the Buffers menu.
167If this is 10, then only the ten most-recently-selected buffers are shown.
168If this is nil, then all buffers are shown.
169A large number or nil slows down menu responsiveness.")
170
98bee59b 171(defun mouse-menu-bar-buffers (event)
40954111
RS
172 "Pop up a menu of buffers for selection with the mouse.
173This switches buffers in the window that you clicked on,
174and selects that window."
175 (interactive "e")
176 (let ((buffers (buffer-list))
177 menu)
178 ;; If requested, list only the N most recently selected buffers.
179 (if (and (integerp buffers-menu-max-size)
180 (> buffers-menu-max-size 1))
181 (if (> (length buffers) buffers-menu-max-size)
182 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
183 (setq menu
e936d399
RM
184 (cons "Select Buffer"
185 (let ((tail buffers)
186 (maxbuf 0)
187 (maxlen 0)
188 head)
189 (while tail
190 (or (eq ?\ (aref (buffer-name (car tail)) 0))
191 (setq maxbuf
192 (max maxbuf
193 (length (buffer-name (car tail))))))
194 (setq tail (cdr tail)))
195 (setq tail buffers)
196 (while tail
197 (let ((elt (car tail)))
198 (if (not (string-match "^ "
199 (buffer-name elt)))
200 (setq head (cons
201 (cons
202 (format
203 (format "%%%ds %%s%%s %%s"
204 maxbuf)
205 (buffer-name elt)
206 (if (buffer-modified-p elt)
207 "*" " ")
208 (save-excursion
209 (set-buffer elt)
210 (if buffer-read-only "%" " "))
75e4aa06
RS
211 (or (buffer-file-name elt)
212 (save-excursion
213 (set-buffer elt)
214 list-buffers-directory)
215 ""))
e936d399
RM
216 elt)
217 head)))
218 (and head (> (length (car (car head))) maxlen)
219 (setq maxlen (length (car (car head))))))
220 (setq tail (cdr tail)))
221 (nconc (nreverse head)
222 (list (cons
223 (concat (make-string (max (- (/ maxlen
224 2)
225 8)
226 0) ?\ )
227 "List All Buffers")
228 'list-buffers))))))
229 (setq menu (list menu))
40954111 230
e936d399
RM
231 (if (cdr (frame-list))
232 (setq menu
233 (cons (cons "Select Frame"
234 (mapcar (lambda (frame)
235 (cons (cdr (assq 'name
236 (frame-parameters frame)))
237 frame))
238 (frame-list)))
239 menu)))
240 (setq menu (cons "Buffer and Frame Menu" menu))
40954111
RS
241
242 (let ((buf (x-popup-menu (if (listp event) event
243 (cons '(0 0) (selected-frame)))
244 menu))
245 (window (and (listp event) (posn-window (event-start event)))))
e936d399
RM
246 (cond ((framep buf)
247 (make-frame-visible buf)
248 (raise-frame buf)
249 (select-frame buf))
250 ((eq buf 'list-buffers)
251 (list-buffers))
252 (buf
253 (if complex-buffers-menu-p
254 (let ((action (x-popup-menu
255 (if (listp event) event
256 (cons '(0 0) (selected-frame)))
257 '("Buffer Action"
258 (""
259 ("Save Buffer" . save-buffer)
260 ("Kill Buffer" . kill-buffer)
261 ("Select Buffer" . switch-to-buffer))))))
262 (if (eq action 'save-buffer)
263 (save-excursion
264 (set-buffer buf)
265 (save-buffer))
266 (funcall action buf)))
267 (and (windowp window)
268 (select-window window))
269 (switch-to-buffer buf)))))))
2f1139a4 270
40954111
RS
271;; this version is too slow
272;;;(defun format-buffers-menu-line (buffer)
273;;; "Returns a string to represent the given buffer in the Buffer menu.
274;;;nil means the buffer shouldn't be listed. You can redefine this."
275;;; (if (string-match "\\` " (buffer-name buffer))
276;;; nil
277;;; (save-excursion
278;;; (set-buffer buffer)
279;;; (let ((size (buffer-size)))
280;;; (format "%s%s %-19s %6s %-15s %s"
281;;; (if (buffer-modified-p) "*" " ")
282;;; (if buffer-read-only "%" " ")
283;;; (buffer-name)
284;;; size
285;;; mode-name
286;;; (or (buffer-file-name) ""))))))
287\f
057d49d1 288(defun menu-bar-mode (flag)
dfd29450 289 "Toggle display of a menu bar on each frame.
057d49d1
RS
290This command applies to all frames that exist and frames to be
291created in the future.
292With a numeric argument, if the argument is negative,
dfd29450 293turn off menu bars; otherwise, turn on menu bars."
dad8e392
RM
294 (interactive "P")
295 (if flag (setq flag (prefix-numeric-value flag)))
296
297 ;; Obtain the current setting by looking at default-frame-alist.
298 (let ((menu-bar-mode
299 (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist)))
300 (if assq (cdr assq) 0))))))
301
302 ;; Tweedle it according to the argument.
303 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
304 (or (not (numberp flag)) (>= flag 0))))
305
306 ;; Apply it to default-frame-alist.
307 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
308 (if (consp parameter)
309 (setcdr parameter (if menu-bar-mode 1 0))
310 (setq default-frame-alist
311 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
312 default-frame-alist))))
313
314 ;; Apply it to existing frames.
315 (let ((frames (frame-list)))
316 (while frames
317 (modify-frame-parameters (car frames)
318 (list (cons 'menu-bar-lines
319 (if menu-bar-mode 1 0))))
320 (setq frames (cdr frames))))))
1db87953
RS
321
322;; Make frames created from now on have a menu bar.
d0dbd3bf
RS
323(if window-system
324 (menu-bar-mode t))
1db87953 325
bffa5d4d
RS
326(provide 'menu-bar)
327
235aa29b 328;;; menu-bar.el ends here