(Fset_window_buffer): Set window's force_start to Qnil.
[emacs.git] / lisp / menu-bar.el
blob7494867bfa8911ae58ec406406ef51e4da910e1b
1 ;;; menu-bar.el --- define a default menu bar.
3 ;; Author: RMS
4 ;; Keywords: internals
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)
13 ;; any later version.
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.
24 ;;; Code:
26 (define-key global-map [menu-bar] (make-sparse-keymap "menu-bar"))
27 (setq menu-bar-help-menu (make-sparse-keymap "Help"))
28 (define-key global-map [menu-bar help] (cons "Help" menu-bar-help-menu))
29 (setq menu-bar-edit-menu (make-sparse-keymap "Edit"))
30 (define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
31 (setq menu-bar-file-menu (make-sparse-keymap "File"))
32 (define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
34 (define-key menu-bar-file-menu [exit-emacs]
35 '("Exit Emacs" . save-buffers-kill-emacs))
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))
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))
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)))
64 (define-key menu-bar-help-menu [emacs-tutorial]
65 '("Emacs Tutorial" . help-with-tutorial))
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))
81 (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
82 (defun kill-this-buffer () ; for the menubar
83 "Kills the current buffer."
84 (interactive)
85 (kill-buffer (current-buffer)))
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)))
96 (put 'save-buffer 'menu-enable '(buffer-modified-p))
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))
101 (put 'advertised-undo 'menu-enable
102 '(and (not (eq t buffer-undo-list))
103 (if (eq last-command 'undo)
104 (and (boundp 'pending-undo-list)
105 pending-undo-list)
106 buffer-undo-list)))
108 (define-key global-map [menu-bar buffer] '("Buffers" . mouse-buffer-menu))
110 (defvar complex-buffers-menu-p nil
111 "*Non-nil says, offer a choice of actions after you pick a buffer.
112 This applies to the Buffers menu from the menu bar.")
114 (defvar buffers-menu-max-size 10
115 "*Maximum number of entries which may appear on the Buffers menu.
116 If this is 10, then only the ten most-recently-selected buffers are shown.
117 If this is nil, then all buffers are shown.
118 A large number or nil slows down menu responsiveness.")
120 (defun mouse-buffer-menu (event)
121 "Pop up a menu of buffers for selection with the mouse.
122 This switches buffers in the window that you clicked on,
123 and selects that window."
124 (interactive "e")
125 (let ((buffers (buffer-list))
126 menu)
127 ;; If requested, list only the N most recently selected buffers.
128 (if (and (integerp buffers-menu-max-size)
129 (> buffers-menu-max-size 1))
130 (if (> (length buffers) buffers-menu-max-size)
131 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
132 (setq menu
133 (list "Buffer Menu"
134 (cons "Select Buffer"
135 (let ((tail buffers)
136 (maxlen 0)
137 head)
138 (while tail
139 (let ((elt (car tail)))
140 (if (not (string-match "^ "
141 (buffer-name elt)))
142 (setq head (cons
143 (cons
144 (format
145 "%14s %s"
146 (buffer-name elt)
147 (or (buffer-file-name elt) ""))
148 elt)
149 head)))
150 (and head (> (length (car (car head))) maxlen)
151 (setq maxlen (length (car (car head))))))
152 (setq tail (cdr tail)))
153 (nconc (reverse head)
154 (list (cons (concat (make-string (- (/ maxlen 2) 8) ?\ )
155 "List All Buffers")
156 'list-buffers)))))))
159 (let ((buf (x-popup-menu (if (listp event) event
160 (cons '(0 0) (selected-frame)))
161 menu))
162 (window (and (listp event) (posn-window (event-start event)))))
163 (if (eq buf 'list-buffers)
164 (list-buffers)
165 (if buf
166 (if complex-buffers-menu-p
167 (let ((action (x-popup-menu (if (listp event) event
168 (cons '(0 0) (selected-frame)))
169 '("Buffer Action"
171 ("Save Buffer" . save-buffer)
172 ("Kill Buffer" . kill-buffer)
173 ("Select Buffer" . switch-to-buffer))))))
174 (if (eq action 'save-buffer)
175 (save-excursion
176 (set-buffer buf)
177 (save-buffer))
178 (funcall action buf)))
179 (and (windowp window)
180 (select-window window))
181 (switch-to-buffer buf)))))))
183 ;; this version is too slow
184 ;;;(defun format-buffers-menu-line (buffer)
185 ;;; "Returns a string to represent the given buffer in the Buffer menu.
186 ;;;nil means the buffer shouldn't be listed. You can redefine this."
187 ;;; (if (string-match "\\` " (buffer-name buffer))
188 ;;; nil
189 ;;; (save-excursion
190 ;;; (set-buffer buffer)
191 ;;; (let ((size (buffer-size)))
192 ;;; (format "%s%s %-19s %6s %-15s %s"
193 ;;; (if (buffer-modified-p) "*" " ")
194 ;;; (if buffer-read-only "%" " ")
195 ;;; (buffer-name)
196 ;;; size
197 ;;; mode-name
198 ;;; (or (buffer-file-name) ""))))))
200 (defvar menu-bar-mode nil)
202 (defun menu-bar-mode (flag)
203 "Toggle display of a menu bar on each frame.
204 This command applies to all frames that exist and frames to be
205 created in the future.
206 With a numeric argument, if the argument is negative,
207 turn off menu bars; otherwise, turn on menu bars."
208 (interactive "P")
209 (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
210 (or (not (numberp flag)) (>= flag 0))))
211 (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
212 (if (consp parameter)
213 (setcdr parameter (if menu-bar-mode 1 0))
214 (setq default-frame-alist
215 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
216 default-frame-alist))))
217 (let ((frames (frame-list)))
218 (while frames
219 ;; Turn menu bar on or off in existing frames.
220 ;; (Except for minibuffer-only frames.)
221 (or (eq 'only (cdr (assq 'minibuffer (frame-parameters (car frames)))))
222 (modify-frame-parameters
223 (car frames)
224 (list (if menu-bar-mode
225 '(menu-bar-lines . 1)
226 '(menu-bar-lines . 0)))))
227 (setq frames (cdr frames)))))
229 ;; Make frames created from now on have a menu bar.
230 (menu-bar-mode t)
232 (provide 'menu-bar)
234 ;;; menu-bar.el ends here