avoid recursive `require' when loading semantic
[bpt/emacs.git] / lisp / mh-e / mh-tool-bar.el
CommitLineData
dda00b2c
BW
1;;; mh-tool-bar.el --- MH-E tool bar support
2
ba318903 3;; Copyright (C) 2002-2003, 2005-2014 Free Software Foundation, Inc.
dda00b2c
BW
4
5;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
9
10;; This file is part of GNU Emacs.
11
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
dda00b2c 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
dda00b2c
BW
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
dda00b2c
BW
24
25;;; Commentary:
26
27;;; Change Log:
28
29;;; Code:
30
31(require 'mh-e)
f169fdd3
MB
32(mh-do-in-gnu-emacs
33 (require 'tool-bar))
34(mh-do-in-xemacs
35 (require 'toolbar))
dda00b2c
BW
36
37;;; Tool Bar Commands
38
39(defun mh-tool-bar-search (&optional arg)
40 "Interactively call `mh-tool-bar-search-function'.
41Optional argument ARG is not used."
42 (interactive "P")
43 (call-interactively mh-tool-bar-search-function))
44
45(defun mh-tool-bar-customize ()
46 "Call `mh-customize' from the tool bar."
47 (interactive)
48 (mh-customize t))
49
50(defun mh-tool-bar-folder-help ()
51 "Visit \"(mh-e)Top\"."
52 (interactive)
53 (info "(mh-e)Top")
54 (delete-other-windows))
55
56(defun mh-tool-bar-letter-help ()
57 "Visit \"(mh-e)Editing Drafts\"."
58 (interactive)
59 (info "(mh-e)Editing Drafts")
60 (delete-other-windows))
61
62(defmacro mh-tool-bar-reply-generator (function recipient folder-buffer-flag)
63 "Generate FUNCTION that replies to RECIPIENT.
64If FOLDER-BUFFER-FLAG is nil then the function generated...
65When INCLUDE-FLAG is non-nil, include message body being replied to."
66 `(defun ,function (&optional arg)
67 ,(format "Reply to \"%s\".\nWhen ARG is non-nil include message in reply."
68 recipient)
69 (interactive "P")
70 ,(if folder-buffer-flag nil '(set-buffer mh-show-folder-buffer))
71 (mh-reply (mh-get-msg-num nil) ,recipient arg)))
72
73(mh-tool-bar-reply-generator mh-tool-bar-reply-from "from" t)
74(mh-tool-bar-reply-generator mh-show-tool-bar-reply-from "from" nil)
75(mh-tool-bar-reply-generator mh-tool-bar-reply-to "to" t)
76(mh-tool-bar-reply-generator mh-show-tool-bar-reply-to "to" nil)
77(mh-tool-bar-reply-generator mh-tool-bar-reply-all "all" t)
78(mh-tool-bar-reply-generator mh-show-tool-bar-reply-all "all" nil)
79
80\f
81
82;;; Tool Bar Creation
83
d2464a9f
BW
84;; Shush compiler.
85(defvar image-load-path)
86
dda00b2c
BW
87(defmacro mh-tool-bar-define (defaults &rest buttons)
88 "Define a tool bar for MH-E.
89DEFAULTS is the list of buttons that are present by default. It
90is a list of lists where the sublists are of the following form:
91
92 (:KEYWORD FUNC1 FUNC2 FUNC3 ...)
93
94Here :KEYWORD is one of :folder or :letter. If it is :folder then
95the default buttons in the folder and show mode buffers are being
96specified. If it is :letter then the default buttons in the
97letter mode are listed. FUNC1, FUNC2, FUNC3, ... are the names of
98the functions that the buttons would execute.
99
100Each element of BUTTONS is a list consisting of four mandatory
101items and one optional item as follows:
102
103 (FUNCTION MODES ICON DOC &optional ENABLE-EXPR)
104
105where,
106
107 FUNCTION is the name of the function that will be executed when
108 the button is clicked.
109
110 MODES is a list of symbols. List elements must be from \"folder\",
111 \"letter\" and \"sequence\". If \"folder\" is present then the button is
112 available in the folder and show buffer. If the name of FUNCTION is
113 of the form \"mh-foo\", where foo is some arbitrary string, then we
114 check if the function `mh-show-foo' exists. If it exists then that
115 function is used in the show buffer. Otherwise the original function
116 `mh-foo' is used in the show buffer as well. Presence of \"sequence\"
117 is handled similar to the above. The only difference is that the
118 button is shown only when the folder is narrowed to a sequence. If
119 \"letter\" is present in MODES, then the button is available during
120 draft editing and runs FUNCTION when clicked.
121
122 ICON is the icon that is drawn in the button.
123
124 DOC is the documentation for the button. It is used in tool-tips and
125 in providing other help to the user. GNU Emacs uses only the first
126 line of the string. So the DOC should be formatted such that the
127 first line is useful and complete without the rest of the string.
128
129 Optional item ENABLE-EXPR is an arbitrary lisp expression. If it
e9a452d9 130 evaluates to nil, then the button is inactive, otherwise it is
dda00b2c
BW
131 active. If it isn't present then the button is always active."
132 ;; The following variable names have been carefully chosen to make code
133 ;; generation easier. Modifying the names should be done carefully.
134 (let (folder-buttons folder-docs folder-button-setter sequence-button-setter
135 show-buttons show-button-setter show-seq-button-setter
136 letter-buttons letter-docs letter-button-setter
137 folder-defaults letter-defaults
138 folder-vectors show-vectors letter-vectors)
139 (dolist (x defaults)
140 (cond ((eq (car x) :folder) (setq folder-defaults (cdr x)))
141 ((eq (car x) :letter) (setq letter-defaults (cdr x)))))
142 (dolist (button buttons)
143 (unless (and (listp button)
144 (or (equal (length button) 4) (equal (length button) 5)))
145 (error "Incorrect MH-E tool-bar button specification: %s" button))
146 (let* ((name (nth 0 button))
147 (name-str (symbol-name name))
148 (icon (nth 2 button))
149 (xemacs-icon (mh-do-in-xemacs
fbe4aef8 150 `(cdr (assoc (quote ,(intern icon)) mh-xemacs-icon-map))))
dda00b2c
BW
151 (full-doc (nth 3 button))
152 (doc (if (string-match "\\(.*\\)\n" full-doc)
153 (match-string 1 full-doc)
154 full-doc))
d36069f0 155 (enable-expr (if (eql (length button) 4) t (nth 4 button)))
dda00b2c
BW
156 (modes (nth 1 button))
157 functions show-sym)
158 (when (memq 'letter modes) (setq functions `(:letter ,name)))
159 (when (or (memq 'folder modes) (memq 'sequence modes))
160 (setq functions
161 (append `(,(if (memq 'folder modes) :folder :sequence) ,name)
162 functions))
163 (setq show-sym
164 (if (string-match "^mh-\\(.*\\)$" name-str)
165 (intern (concat "mh-show-" (match-string 1 name-str)))
166 name))
167 (setq functions
168 (append `(,(if (memq 'folder modes) :show :show-seq)
169 ,(if (fboundp show-sym) show-sym name))
170 functions)))
171 (do ((functions functions (cddr functions)))
172 ((null functions))
173 (let* ((type (car functions))
174 (function (cadr functions))
175 (type1 (substring (symbol-name type) 1))
176 (vector-list (cond ((eq type :show) 'show-vectors)
177 ((eq type :show-seq) 'show-vectors)
178 ((eq type :letter) 'letter-vectors)
179 (t 'folder-vectors)))
180 (list (cond ((eq type :letter) 'mh-tool-bar-letter-buttons)
181 (t 'mh-tool-bar-folder-buttons)))
d2464a9f 182 (key (intern (concat "mh-" type1 "-tool-bar-" name-str)))
dda00b2c
BW
183 (setter (intern (concat type1 "-button-setter")))
184 (mbuttons (cond ((eq type :letter) 'letter-buttons)
185 ((eq type :show) 'show-buttons)
186 ((eq type :show-seq) 'show-buttons)
187 (t 'folder-buttons)))
188 (docs (cond ((eq mbuttons 'letter-buttons) 'letter-docs)
189 ((eq mbuttons 'folder-buttons) 'folder-docs))))
fbe4aef8 190 (add-to-list vector-list `(vector ,xemacs-icon ',function t ,full-doc))
dda00b2c
BW
191 (add-to-list
192 setter `(when (member ',name ,list)
193 (mh-funcall-if-exists
194 tool-bar-add-item ,icon ',function ',key
195 :help ,doc :enable ',enable-expr)))
196 (add-to-list mbuttons name)
197 (if docs (add-to-list docs doc))))))
198 (setq folder-buttons (nreverse folder-buttons)
199 letter-buttons (nreverse letter-buttons)
200 show-buttons (nreverse show-buttons)
201 letter-docs (nreverse letter-docs)
202 folder-docs (nreverse folder-docs)
203 folder-vectors (nreverse folder-vectors)
204 show-vectors (nreverse show-vectors)
205 letter-vectors (nreverse letter-vectors))
206 (dolist (x folder-defaults)
207 (unless (memq x folder-buttons)
efc27af6 208 (error "Folder defaults contains unknown button %s" x)))
dda00b2c
BW
209 (dolist (x letter-defaults)
210 (unless (memq x letter-buttons)
efc27af6 211 (error "Letter defaults contains unknown button %s" x)))
dda00b2c 212 `(eval-when (compile load eval)
dda00b2c
BW
213 ;; GNU Emacs tool bar specific code
214 (mh-do-in-gnu-emacs
d2464a9f
BW
215 (defun mh-buffer-exists-p (mode)
216 "Test whether a buffer with major mode MODE is present."
217 (loop for buf in (buffer-list)
218 when (with-current-buffer buf
219 (eq major-mode mode))
220 return t))
dda00b2c
BW
221 ;; Tool bar initialization functions
222 (defun mh-tool-bar-folder-buttons-init ()
223 (when (mh-buffer-exists-p 'mh-folder-mode)
d2464a9f
BW
224 (let* ((load-path (mh-image-load-path-for-library "mh-e"
225 "mh-logo.xpm"))
226 (image-load-path (cons (car load-path)
227 (when (boundp 'image-load-path)
228 image-load-path))))
229 (setq mh-folder-tool-bar-map
230 (let ((tool-bar-map (make-sparse-keymap)))
231 ,@(nreverse folder-button-setter)
232 tool-bar-map))
233 (setq mh-folder-seq-tool-bar-map
234 (let ((tool-bar-map (copy-keymap mh-folder-tool-bar-map)))
235 ,@(nreverse sequence-button-setter)
236 tool-bar-map))
237 (setq mh-show-tool-bar-map
238 (let ((tool-bar-map (make-sparse-keymap)))
239 ,@(nreverse show-button-setter)
240 tool-bar-map))
241 (setq mh-show-seq-tool-bar-map
242 (let ((tool-bar-map (copy-keymap mh-show-tool-bar-map)))
243 ,@(nreverse show-seq-button-setter)
244 tool-bar-map)))))
dda00b2c
BW
245 (defun mh-tool-bar-letter-buttons-init ()
246 (when (mh-buffer-exists-p 'mh-letter-mode)
d2464a9f
BW
247 (let* ((load-path (mh-image-load-path-for-library "mh-e"
248 "mh-logo.xpm"))
249 (image-load-path (cons (car load-path)
250 (when (boundp 'image-load-path)
251 image-load-path))))
252 (setq mh-letter-tool-bar-map
253 (let ((tool-bar-map (make-sparse-keymap)))
254 ,@(nreverse letter-button-setter)
255 tool-bar-map)))))
dda00b2c 256 ;; Custom setter functions
d2464a9f
BW
257 (defun mh-tool-bar-update (mode default-map sequence-map)
258 "Update `tool-bar-map' in all buffers of MODE.
259Use SEQUENCE-MAP if display is limited; DEFAULT-MAP otherwise."
260 (loop for buf in (buffer-list)
261 do (with-current-buffer buf
262 (if (eq mode major-mode)
263 (let ((map (if mh-folder-view-stack
264 sequence-map
265 default-map)))
266 ;; Yes, make-local-variable is necessary since we
267 ;; get here during initialization when loading
268 ;; mh-e.el, after the +inbox buffer has been
269 ;; created, but before mh-folder-mode has run and
270 ;; created the local map.
271 (set (make-local-variable 'tool-bar-map) map))))))
dda00b2c
BW
272 (defun mh-tool-bar-folder-buttons-set (symbol value)
273 "Construct tool bar for `mh-folder-mode' and `mh-show-mode'."
274 (set-default symbol value)
d2464a9f
BW
275 (mh-tool-bar-folder-buttons-init)
276 (mh-tool-bar-update 'mh-folder-mode mh-folder-tool-bar-map
277 mh-folder-seq-tool-bar-map)
278 (mh-tool-bar-update 'mh-show-mode mh-show-tool-bar-map
279 mh-show-seq-tool-bar-map))
dda00b2c
BW
280 (defun mh-tool-bar-letter-buttons-set (symbol value)
281 "Construct tool bar for `mh-letter-mode'."
282 (set-default symbol value)
d2464a9f
BW
283 (mh-tool-bar-letter-buttons-init)
284 (mh-tool-bar-update 'mh-letter-mode mh-letter-tool-bar-map
285 mh-letter-tool-bar-map)))
dda00b2c
BW
286 ;; XEmacs specific code
287 (mh-do-in-xemacs
288 (defvar mh-tool-bar-folder-vector-map
fbe4aef8
BW
289 (list ,@(loop for button in folder-buttons
290 for vector in folder-vectors
291 collect `(cons ',button ,vector))))
dda00b2c 292 (defvar mh-tool-bar-show-vector-map
fbe4aef8
BW
293 (list ,@(loop for button in show-buttons
294 for vector in show-vectors
295 collect `(cons ',button ,vector))))
dda00b2c 296 (defvar mh-tool-bar-letter-vector-map
fbe4aef8
BW
297 (list ,@(loop for button in letter-buttons
298 for vector in letter-vectors
299 collect `(cons ',button ,vector))))
300 (defvar mh-tool-bar-folder-buttons)
301 (defvar mh-tool-bar-show-buttons)
302 (defvar mh-tool-bar-letter-buttons)
dda00b2c
BW
303 ;; Custom setter functions
304 (defun mh-tool-bar-letter-buttons-set (symbol value)
305 (set-default symbol value)
306 (when mh-xemacs-has-tool-bar-flag
307 (setq mh-tool-bar-letter-buttons
308 (loop for b in value
fbe4aef8
BW
309 collect (cdr
310 (assoc b mh-tool-bar-letter-vector-map))))))
dda00b2c
BW
311 (defun mh-tool-bar-folder-buttons-set (symbol value)
312 (set-default symbol value)
313 (when mh-xemacs-has-tool-bar-flag
314 (setq mh-tool-bar-folder-buttons
315 (loop for b in value
316 collect (cdr (assoc b mh-tool-bar-folder-vector-map))))
317 (setq mh-tool-bar-show-buttons
318 (loop for b in value
319 collect (cdr (assoc b mh-tool-bar-show-vector-map))))))
320 (defun mh-tool-bar-init (mode)
321 "Install tool bar in MODE."
fbe4aef8
BW
322 (when mh-xemacs-use-tool-bar-flag
323 (let ((tool-bar (cond ((eq mode :folder)
324 mh-tool-bar-folder-buttons)
325 ((eq mode :letter)
326 mh-tool-bar-letter-buttons)
327 ((eq mode :show)
328 mh-tool-bar-show-buttons)))
329 (height 37)
330 (width 40)
331 (buffer (current-buffer)))
dda00b2c
BW
332 (cond
333 ((eq mh-xemacs-tool-bar-position 'top)
334 (set-specifier top-toolbar tool-bar buffer)
335 (set-specifier top-toolbar-visible-p t)
336 (set-specifier top-toolbar-height height))
337 ((eq mh-xemacs-tool-bar-position 'bottom)
338 (set-specifier bottom-toolbar tool-bar buffer)
339 (set-specifier bottom-toolbar-visible-p t)
340 (set-specifier bottom-toolbar-height height))
341 ((eq mh-xemacs-tool-bar-position 'left)
342 (set-specifier left-toolbar tool-bar buffer)
343 (set-specifier left-toolbar-visible-p t)
344 (set-specifier left-toolbar-width width))
345 ((eq mh-xemacs-tool-bar-position 'right)
346 (set-specifier right-toolbar tool-bar buffer)
347 (set-specifier right-toolbar-visible-p t)
348 (set-specifier right-toolbar-width width))
349 (t (set-specifier default-toolbar tool-bar buffer)))))))
350 ;; Declare customizable tool bars
351 (custom-declare-variable
352 'mh-tool-bar-folder-buttons
353 '(list ,@(mapcar (lambda (x) `(quote ,x)) folder-defaults))
354 "List of buttons to include in MH-Folder tool bar."
d36069f0
BW
355 :group 'mh-tool-bar
356 :set 'mh-tool-bar-folder-buttons-set
dda00b2c
BW
357 :type '(set ,@(loop for x in folder-buttons
358 for y in folder-docs
23347d76
BW
359 collect `(const :tag ,y ,x)))
360 ;;:package-version '(MH-E "7.1")
361 )
dda00b2c
BW
362 (custom-declare-variable
363 'mh-tool-bar-letter-buttons
364 '(list ,@(mapcar (lambda (x) `(quote ,x)) letter-defaults))
365 "List of buttons to include in MH-Letter tool bar."
d36069f0
BW
366 :group 'mh-tool-bar
367 :set 'mh-tool-bar-letter-buttons-set
dda00b2c
BW
368 :type '(set ,@(loop for x in letter-buttons
369 for y in letter-docs
23347d76
BW
370 collect `(const :tag ,y ,x)))
371 ;;:package-version '(MH-E "7.1")
fbe4aef8 372 ))))
dda00b2c 373
fbe4aef8 374;; The icon names are duplicated in the Makefile and mh-xemacs.el.
dda00b2c 375(mh-tool-bar-define
84b57004
BW
376 ((:folder mh-inc-folder mh-mime-save-parts
377 mh-previous-undeleted-msg mh-page-msg
378 mh-next-undeleted-msg mh-delete-msg mh-refile-msg
efc27af6
BW
379 mh-undo mh-execute-commands mh-toggle-tick mh-reply
380 mh-alias-grab-from-field mh-send mh-rescan-folder
381 mh-tool-bar-search mh-visit-folder
84b57004
BW
382 mh-tool-bar-customize mh-tool-bar-folder-help
383 mh-widen)
384 (:letter mh-send-letter save-buffer mh-fully-kill-draft
385 mh-compose-insertion ispell-message undo
386 clipboard-kill-region clipboard-kill-ring-save
387 clipboard-yank mh-tool-bar-customize
388 mh-tool-bar-letter-help))
efc27af6 389 ;; Folder/Show buffer buttons
84b57004 390 (mh-inc-folder (folder) "mail/inbox" "Incorporate new mail in Inbox
dda00b2c 391This button runs `mh-inc-folder' which drags any
efc27af6
BW
392new mail into your Inbox folder")
393 (mh-mime-save-parts (folder) "attach" "Save MIME parts from this message
dda00b2c 394This button runs `mh-mime-save-parts' which saves a message's
efc27af6
BW
395different parts into separate files")
396 (mh-previous-undeleted-msg (folder) "left-arrow"
397 "Go to the previous undeleted message
dda00b2c 398This button runs `mh-previous-undeleted-msg'")
84b57004 399 (mh-page-msg (folder) "next-page" "Page the current message forwards
efc27af6
BW
400This button runs `mh-page-msg'")
401 (mh-next-undeleted-msg (folder) "right-arrow" "Go to the next undeleted message
402The button runs `mh-next-undeleted-msg'")
84b57004 403 (mh-delete-msg (folder) "delete" "Mark this message for deletion
efc27af6 404This button runs `mh-delete-msg'")
84b57004 405 (mh-refile-msg (folder) "mail/move" "Refile this message
efc27af6
BW
406This button runs `mh-refile-msg'")
407 (mh-undo (folder) "undo" "Undo last operation
408This button runs `undo'"
409 (mh-outstanding-commands-p))
84b57004 410 (mh-execute-commands (folder) "data-save" "Perform moves and deletes
efc27af6
BW
411This button runs `mh-execute-commands'"
412 (mh-outstanding-commands-p))
84b57004 413 (mh-toggle-tick (folder) "mail/flag-for-followup" "Toggle tick mark
efc27af6
BW
414This button runs `mh-toggle-tick'")
415 (mh-toggle-showing (folder) "show" "Toggle showing message
416This button runs `mh-toggle-showing'")
84b57004
BW
417 (mh-reply (folder) "mail/reply" "Reply to this message
418This button runs `mh-reply'")
efc27af6
BW
419 (mh-tool-bar-reply-from (folder) "mail/reply-from" "Reply to \"from\"")
420 (mh-tool-bar-reply-to (folder) "mail/reply-to" "Reply to \"to\"")
421 (mh-tool-bar-reply-all (folder) "mail/reply-all" "Reply to \"all\"")
84b57004 422 (mh-alias-grab-from-field (folder) "contact" "Create alias for sender
efc27af6
BW
423This button runs `mh-alias-grab-from-field'"
424 (and (mh-extract-from-header-value)
425 (not (mh-alias-for-from-p))))
426 (mh-send (folder) "mail/compose" "Compose new message
427This button runs `mh-send'")
428 (mh-rescan-folder (folder) "refresh" "Rescan this folder
429This button runs `mh-rescan-folder'")
430 (mh-pack-folder (folder) "mail/repack" "Repack this folder
431This button runs `mh-pack-folder'")
432 (mh-tool-bar-search (folder) "search" "Search
433This button runs `mh-tool-bar-search-function'")
84b57004 434 (mh-visit-folder (folder) "open" "Visit other folder
efc27af6
BW
435This button runs `mh-visit-folder'")
436 ;; Letter buffer buttons
437 (mh-send-letter (letter) "mail/send" "Send this letter")
efc27af6
BW
438 (save-buffer (letter) "save" "Save current buffer to its file"
439 (buffer-modified-p))
84b57004
BW
440 (mh-fully-kill-draft (letter) "delete" "Kill this draft")
441 (mh-compose-insertion (letter) "attach" "Insert attachment")
442 (ispell-message (letter) "spell" "Check spelling")
efc27af6 443 (undo (letter) "undo" "Undo last operation")
84b57004
BW
444 (clipboard-kill-region (letter) "cut"
445 "Cut (kill) text in region")
446 (clipboard-kill-ring-save (letter) "copy"
447 "Copy text in region")
448 (clipboard-yank (letter) "paste"
449 "Paste (yank) text cut or copied earlier")
efc27af6
BW
450 ;; Common buttons
451 (mh-tool-bar-customize (folder letter) "preferences" "MH-E Preferences")
452 (mh-tool-bar-folder-help (folder) "help" "Help! (general help)
453This button runs `info'")
454 (mh-tool-bar-letter-help (letter) "help" "Help! (general help)
455This button runs `info'")
456 ;; Folder narrowed to sequence buttons
84b57004 457 (mh-widen (sequence) "zoom-out" "Widen from the sequence
efc27af6 458This button runs `mh-widen'"))
dda00b2c
BW
459
460(provide 'mh-tool-bar)
461
462;; Local Variables:
463;; indent-tabs-mode: nil
464;; sentence-end-double-space: nil
465;; End:
466
467;;; mh-tool-bar.el ends here