mh-tool-bar.el 21 KB

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