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