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