(vc-rcs-status): Remove unused var `status'.
[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"))
2f1139a4 28(define-key global-map [menu-bar help] (cons "Help" menu-bar-help-menu))
22390f50 29(defvar menu-bar-edit-menu (make-sparse-keymap "Edit"))
40954111 30(define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
22390f50 31(defvar menu-bar-file-menu (make-sparse-keymap "File"))
40954111 32(define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
db774a16 33
ca9b40a1 34(define-key menu-bar-file-menu [exit-emacs]
db774a16 35 '("Exit Emacs" . save-buffers-kill-emacs))
2f1139a4
RS
36(define-key menu-bar-file-menu [kill-buffer]
37 '("Kill Buffer" . kill-this-buffer))
38(define-key menu-bar-file-menu [delete-frame] '("Delete Frame" . delete-frame))
39(define-key menu-bar-file-menu [print-buffer] '("Print Buffer" . print-buffer))
40(define-key menu-bar-file-menu [revert-buffer]
41 '("Revert Buffer" . revert-buffer))
42(define-key menu-bar-file-menu [write-file]
43 '("Save Buffer As..." . write-file))
44(define-key menu-bar-file-menu [save-buffer] '("Save Buffer" . save-buffer))
45(define-key menu-bar-file-menu [open-file] '("Open File..." . find-file))
46(define-key menu-bar-file-menu [new-frame] '("New Frame" . new-frame))
db774a16 47
057d49d1
RS
48(define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
49(define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
b289b04e
RS
50(define-key menu-bar-edit-menu [choose-selection]
51 '("Choose Next Paste" . mouse-menu-choose-yank))
057d49d1
RS
52(define-key menu-bar-edit-menu [paste] '("Paste" . yank))
53(define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save))
54(define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
55(define-key menu-bar-edit-menu [undo] '("Undo" . undo))
56
57(put 'fill-region 'menu-enable 'mark-active)
58(put 'kill-region 'menu-enable 'mark-active)
59(put 'kill-ring-save 'menu-enable 'mark-active)
60(put 'yank 'menu-enable '(x-selection-exists-p))
61(put 'delete-region 'menu-enable 'mark-active)
62(put 'undo 'menu-enable '(if (eq last-command 'undo)
63 pending-undo-list
64 (consp buffer-undo-list)))
db774a16 65
ca9b40a1 66(define-key menu-bar-help-menu [emacs-tutorial]
db774a16 67 '("Emacs Tutorial" . help-with-tutorial))
2f1139a4
RS
68(define-key menu-bar-help-menu [man] '("Man..." . manual-entry))
69(define-key menu-bar-help-menu [describe-variable]
70 '("Describe Variable..." . describe-variable))
71(define-key menu-bar-help-menu [describe-function]
72 '("Describe Function..." . describe-function))
73(define-key menu-bar-help-menu [describe-key]
74 '("Describe Key..." . describe-key))
75(define-key menu-bar-help-menu [list-keybindings]
76 '("List Keybindings" . describe-bindings))
77(define-key menu-bar-help-menu [command-apropos]
78 '("Command Apropos..." . command-apropos))
79(define-key menu-bar-help-menu [describe-mode]
80 '("Describe Mode" . describe-mode))
81(define-key menu-bar-help-menu [info] '("Info" . info))
db774a16 82
2f1139a4 83(define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
db774a16
RS
84(defun kill-this-buffer () ; for the menubar
85 "Kills the current buffer."
86 (interactive)
87 (kill-buffer (current-buffer)))
88
2f1139a4
RS
89(defun kill-this-buffer-enabled-p ()
90 (let ((count 0)
91 (buffers (buffer-list)))
92 (while buffers
93 (or (string-match "^ " (buffer-name (car buffers)))
94 (setq count (1+ count)))
95 (setq buffers (cdr buffers)))
96 (> count 1)))
97
db774a16 98(put 'save-buffer 'menu-enable '(buffer-modified-p))
2f1139a4
RS
99(put 'revert-buffer 'menu-enable '(and (buffer-modified-p) (buffer-file-name)))
100(put 'delete-frame 'menu-enable '(cdr (visible-frame-list)))
101(put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
102
db774a16
RS
103(put 'advertised-undo 'menu-enable
104 '(and (not (eq t buffer-undo-list))
105 (if (eq last-command 'undo)
2f1139a4
RS
106 (and (boundp 'pending-undo-list)
107 pending-undo-list)
108 buffer-undo-list)))
2877eac2
RS
109
110(defvar yank-menu-length 100
111 "*Maximum length of an item in the menu for \
112\\[mouse-menu-choose-yank].")
113
114(defun mouse-menu-choose-yank (event)
115 "Pop up a menu of the kill-ring for selection with the mouse.
116The kill-ring-yank-pointer is moved to the selected element.
117A subsequent \\[yank] yanks the choice just selected."
118 (interactive "e")
119 (let* ((count 0)
120 (menu (mapcar (lambda (string)
121 (if (> (length string) yank-menu-length)
122 (setq string (substring string
123 0 yank-menu-length)))
124 (prog1 (cons string count)
125 (setq count (1+ count))))
dfabc98f
RM
126 kill-ring))
127 (arg (x-popup-menu event
128 (list "Yank Menu"
129 (cons "Pick Selection" menu)))))
130 ;; A mouse click outside the menu returns nil.
131 ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
132 ;; XXX should this perhaps do something other than simply return? -rm
133 (if arg
134 (progn
135 (rotate-yank-pointer arg)
136 (if (interactive-p)
137 (message "The next yank will insert the selected text.")
138 (current-kill 0))))))
139(put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
40954111 140\f
98bee59b 141(define-key global-map [menu-bar buffer] '("Buffers" . mouse-menu-bar-buffers))
40954111
RS
142
143(defvar complex-buffers-menu-p nil
144 "*Non-nil says, offer a choice of actions after you pick a buffer.
145This applies to the Buffers menu from the menu bar.")
146
147(defvar buffers-menu-max-size 10
148 "*Maximum number of entries which may appear on the Buffers menu.
149If this is 10, then only the ten most-recently-selected buffers are shown.
150If this is nil, then all buffers are shown.
151A large number or nil slows down menu responsiveness.")
152
98bee59b 153(defun mouse-menu-bar-buffers (event)
40954111
RS
154 "Pop up a menu of buffers for selection with the mouse.
155This switches buffers in the window that you clicked on,
156and selects that window."
157 (interactive "e")
158 (let ((buffers (buffer-list))
159 menu)
160 ;; If requested, list only the N most recently selected buffers.
161 (if (and (integerp buffers-menu-max-size)
162 (> buffers-menu-max-size 1))
163 (if (> (length buffers) buffers-menu-max-size)
164 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
165 (setq menu
166 (list "Buffer Menu"
167 (cons "Select Buffer"
168 (let ((tail buffers)
b9d07cf7 169 (maxbuf 0)
40954111
RS
170 (maxlen 0)
171 head)
b9d07cf7
RS
172 (while tail
173 (or (eq ?\ (aref (buffer-name (car tail)) 0))
174 (setq maxbuf
175 (max maxbuf
176 (length (buffer-name (car tail))))))
177 (setq tail (cdr tail)))
178 (setq tail buffers)
40954111
RS
179 (while tail
180 (let ((elt (car tail)))
181 (if (not (string-match "^ "
182 (buffer-name elt)))
183 (setq head (cons
184 (cons
185 (format
b9d07cf7
RS
186 (format "%%%ds %%s%%s %%s"
187 maxbuf)
40954111 188 (buffer-name elt)
b9d07cf7
RS
189 (if (buffer-modified-p elt) "*" " ")
190 (save-excursion
191 (set-buffer elt)
192 (if buffer-read-only "%" " "))
40954111
RS
193 (or (buffer-file-name elt) ""))
194 elt)
195 head)))
196 (and head (> (length (car (car head))) maxlen)
197 (setq maxlen (length (car (car head))))))
198 (setq tail (cdr tail)))
199 (nconc (reverse head)
200 (list (cons (concat (make-string (- (/ maxlen 2) 8) ?\ )
201 "List All Buffers")
202 'list-buffers)))))))
203
204
205 (let ((buf (x-popup-menu (if (listp event) event
206 (cons '(0 0) (selected-frame)))
207 menu))
208 (window (and (listp event) (posn-window (event-start event)))))
209 (if (eq buf 'list-buffers)
210 (list-buffers)
211 (if buf
212 (if complex-buffers-menu-p
213 (let ((action (x-popup-menu (if (listp event) event
214 (cons '(0 0) (selected-frame)))
215 '("Buffer Action"
216 (""
217 ("Save Buffer" . save-buffer)
218 ("Kill Buffer" . kill-buffer)
219 ("Select Buffer" . switch-to-buffer))))))
220 (if (eq action 'save-buffer)
221 (save-excursion
222 (set-buffer buf)
223 (save-buffer))
224 (funcall action buf)))
225 (and (windowp window)
226 (select-window window))
227 (switch-to-buffer buf)))))))
2f1139a4 228
40954111
RS
229;; this version is too slow
230;;;(defun format-buffers-menu-line (buffer)
231;;; "Returns a string to represent the given buffer in the Buffer menu.
232;;;nil means the buffer shouldn't be listed. You can redefine this."
233;;; (if (string-match "\\` " (buffer-name buffer))
234;;; nil
235;;; (save-excursion
236;;; (set-buffer buffer)
237;;; (let ((size (buffer-size)))
238;;; (format "%s%s %-19s %6s %-15s %s"
239;;; (if (buffer-modified-p) "*" " ")
240;;; (if buffer-read-only "%" " ")
241;;; (buffer-name)
242;;; size
243;;; mode-name
244;;; (or (buffer-file-name) ""))))))
245\f
057d49d1
RS
246(defvar menu-bar-mode nil)
247
248(defun menu-bar-mode (flag)
dfd29450 249 "Toggle display of a menu bar on each frame.
057d49d1
RS
250This command applies to all frames that exist and frames to be
251created in the future.
252With a numeric argument, if the argument is negative,
dfd29450 253turn off menu bars; otherwise, turn on menu bars."
057d49d1
RS
254 (interactive "P")
255 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
256 (or (not (numberp flag)) (>= flag 0))))
257 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
258 (if (consp parameter)
259 (setcdr parameter (if menu-bar-mode 1 0))
260 (setq default-frame-alist
261 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
262 default-frame-alist))))
263 (let ((frames (frame-list)))
264 (while frames
265 ;; Turn menu bar on or off in existing frames.
266 ;; (Except for minibuffer-only frames.)
267 (or (eq 'only (cdr (assq 'minibuffer (frame-parameters (car frames)))))
268 (modify-frame-parameters
269 (car frames)
270 (list (if menu-bar-mode
271 '(menu-bar-lines . 1)
272 '(menu-bar-lines . 0)))))
273 (setq frames (cdr frames)))))
1db87953
RS
274
275;; Make frames created from now on have a menu bar.
d0dbd3bf
RS
276(if window-system
277 (menu-bar-mode t))
1db87953 278
bffa5d4d
RS
279(provide 'menu-bar)
280
235aa29b 281;;; menu-bar.el ends here