mairix.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. ;;; mairix.el --- Mairix interface for Emacs
  2. ;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
  3. ;; Author: David Engster <dengste@eml.cc>
  4. ;; Keywords: mail searching
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This is an interface to the mairix mail search engine. Mairix is
  18. ;; written by Richard Curnow and is licensed under the GPL. See the
  19. ;; home page for details:
  20. ;;
  21. ;; http://www.rpcurnow.force9.co.uk/mairix/
  22. ;;
  23. ;; Features of mairix.el:
  24. ;;
  25. ;; * Query mairix with a search term.
  26. ;; * Currently supported Emacs mail programs: RMail, Gnus (mbox only),
  27. ;; and VM.
  28. ;; * Generate search queries using graphical widgets.
  29. ;; * Generate search queries based on currently displayed mail.
  30. ;; * Save regularly used searches in your .emacs customize section.
  31. ;; * Major mode for viewing, editing and querying saved searches.
  32. ;; * Update mairix database.
  33. ;;
  34. ;; Please note: There are currently no pre-defined key bindings, since
  35. ;; I guess these would depend on the used mail program. See the docs
  36. ;; for an overview of the provided interactive functions.
  37. ;;
  38. ;; Attention Gnus users: If you use Gnus with maildir or nnml, you
  39. ;; should use the native Gnus back end nnmairix.el instead, since it
  40. ;; has more features and is better integrated with Gnus. This
  41. ;; interface is essentially a stripped down version of nnmairix.el.
  42. ;;
  43. ;; Currently, RMail, Gnus (with mbox files), and VM are supported as
  44. ;; mail programs, but it is pretty easy to interface it with other
  45. ;; ones as well. Please see the docs and the source for details.
  46. ;; In a nutshell: include your favorite mail program in
  47. ;; `mairix-mail-program' and write functions for
  48. ;; `mairix-display-functions' and `mairix-get-mail-header-functions'.
  49. ;; If you have written such functions for your Emacs mail program of
  50. ;; choice, please let me know, so that I can eventually include them
  51. ;; in future version of mairix.el.
  52. ;;; History:
  53. ;; 07/28/2008: version 0.2. Added VM interface, written by Ulrich
  54. ;; Mueller.
  55. ;; 07/14/2008: Initial release
  56. ;;; Code:
  57. (require 'widget)
  58. (require 'cus-edit)
  59. ;;; Keymappings
  60. ;; (currently none - please create them yourself)
  61. ;;; Customizable variables
  62. (defgroup mairix nil
  63. "Mairix interface for Emacs."
  64. :group 'mail)
  65. (defcustom mairix-file-path "~/"
  66. "Path where output files produced by Mairix should be stored."
  67. :type 'directory
  68. :group 'mairix)
  69. (defcustom mairix-search-file "mairixsearch.mbox"
  70. "Name of the default file for storing the searches.
  71. Note that this will be prefixed by `mairix-file-path'."
  72. :type 'string
  73. :group 'mairix)
  74. (defcustom mairix-command "mairix"
  75. "Command for calling mairix.
  76. You can add further options here if you want to, but better use
  77. `mairix-update-options' instead."
  78. :type 'string
  79. :group 'mairix)
  80. (defcustom mairix-output-buffer "*mairix output*"
  81. "Name of the buffer for the output of the mairix binary."
  82. :type 'string
  83. :group 'mairix)
  84. (defcustom mairix-customize-query-buffer "*mairix query*"
  85. "Name of the buffer for customizing a search query."
  86. :type 'string
  87. :group 'mairix)
  88. (defcustom mairix-saved-searches-buffer "*mairix searches*"
  89. "Name of the buffer for displaying saved searches."
  90. :type 'string
  91. :group 'mairix)
  92. (defcustom mairix-update-options '("-F" "-Q")
  93. "Options when calling mairix for updating the database.
  94. The default is \"-F\" and \"-Q\" for making updates faster. You
  95. should call mairix without these options from time to
  96. time (e.g. via cron job)."
  97. :type '(repeat string)
  98. :group 'mairix)
  99. (defcustom mairix-search-options '("-Q")
  100. "Options when calling mairix for searching.
  101. The default is \"-Q\" for making searching faster."
  102. :type '(repeat string)
  103. :group 'mairix)
  104. (defcustom mairix-synchronous-update nil
  105. "Defines if Emacs should wait for the mairix database update."
  106. :type 'boolean
  107. :group 'mairix)
  108. (defcustom mairix-saved-searches nil
  109. "Saved mairix searches.
  110. The entries are: Name of the search, Mairix query string, Name of
  111. the file (nil: use `mairix-search-file' as default), Search whole
  112. threads (nil or t). Note that the file will be prefixed by
  113. `mairix-file-path'."
  114. :type '(repeat (list (string :tag "Name")
  115. (string :tag "Query")
  116. (choice :tag "File"
  117. (const :tag "default")
  118. file)
  119. (boolean :tag "Threads")))
  120. :group 'mairix)
  121. (defcustom mairix-mail-program 'rmail
  122. "Mail program used to display search results.
  123. Currently RMail, Gnus (mbox), and VM are supported. If you use Gnus
  124. with maildir, use nnmairix.el instead."
  125. :type '(choice (const :tag "RMail" rmail)
  126. (const :tag "Gnus mbox" gnus)
  127. (const :tag "VM" vm))
  128. :group 'mairix)
  129. (defcustom mairix-display-functions
  130. '((rmail mairix-rmail-display)
  131. (gnus mairix-gnus-ephemeral-nndoc)
  132. (vm mairix-vm-display))
  133. "Specifies which function should be called for displaying search results.
  134. This is an alist where each entry consists of a symbol from
  135. `mairix-mail-program' and the corresponding function for
  136. displaying the search results. The function will be called with
  137. the mailbox file produced by mairix as the single argument."
  138. :type '(repeat (list (symbol :tag "Mail program")
  139. (function)))
  140. :group 'mairix)
  141. (defcustom mairix-get-mail-header-functions
  142. '((rmail mairix-rmail-fetch-field)
  143. (gnus mairix-gnus-fetch-field)
  144. (vm mairix-vm-fetch-field))
  145. "Specifies function for obtaining a header field from the current mail.
  146. This is an alist where each entry consists of a symbol from
  147. `mairix-mail-program' and the corresponding function for
  148. obtaining a header field from the current displayed mail. The
  149. function will be called with the mail header string as single
  150. argument. You can use nil if you do not have such a function for
  151. your mail program, but then searches based on the current mail
  152. won't work."
  153. :type '(repeat (list (symbol :tag "Mail program")
  154. (choice :tag "Header function"
  155. (const :tag "none")
  156. function)))
  157. :group 'mairix)
  158. (defcustom mairix-widget-select-window-function
  159. (lambda () (select-window (get-largest-window)))
  160. "Function for selecting the window for customizing the mairix query.
  161. The default chooses the largest window in the current frame."
  162. :type 'function
  163. :group 'mairix)
  164. ;; Other variables
  165. (defvar mairix-widget-fields-list
  166. '(("from" "f" "From") ("to" "t" "To") ("cc" "c" "Cc")
  167. ("subject" "s" "Subject") ("to" "tc" "To or Cc")
  168. ("from" "a" "Address") (nil "b" "Body") (nil "n" "Attachment")
  169. ("Message-ID" "m" "Message ID") (nil "s" "Size") (nil "d" "Date"))
  170. "Fields that should be editable during interactive query customization.
  171. Header, corresponding mairix command and description for editable
  172. fields in interactive query customization. The header specifies
  173. which header contents should be inserted into the editable field
  174. when creating a Mairix query based on the current message (can be
  175. nil for disabling this).")
  176. (defvar mairix-widget-other
  177. '(threads flags)
  178. "Other editable mairix commands when using customization widgets.
  179. Currently there are `threads' and `flags'.")
  180. ;;;; Internal variables
  181. (defvar mairix-last-search nil)
  182. (defvar mairix-searches-changed nil)
  183. ;;;; Interface functions for Emacs mail programs
  184. ;;; RMail
  185. (declare-function rmail-summary-displayed "rmail" ())
  186. (declare-function rmail-summary "rmailsum" ()) ; autoloaded in rmail
  187. (defun mairix-rmail-display (folder)
  188. "Display mbox file FOLDER with RMail."
  189. (require 'rmail)
  190. (let (show-summary)
  191. ;; If it exists, select existing RMail window
  192. (when (and (boundp 'rmail-buffer)
  193. rmail-buffer)
  194. (set-buffer rmail-buffer)
  195. (when (get-buffer-window rmail-buffer)
  196. (select-window (get-buffer-window rmail-buffer))
  197. (setq show-summary (rmail-summary-displayed))))
  198. ;; check if folder is already open and if so, kill it
  199. (when (get-buffer (file-name-nondirectory folder))
  200. (set-buffer
  201. (get-buffer (file-name-nondirectory folder)))
  202. (set-buffer-modified-p nil)
  203. (kill-buffer nil))
  204. (rmail folder)
  205. ;; Update summary if necessary
  206. (when show-summary
  207. (rmail-summary))))
  208. (defvar rmail-buffer)
  209. ;; Fetching mail header field:
  210. (defun mairix-rmail-fetch-field (field)
  211. "Get mail header FIELD for current message using RMail."
  212. (unless (and (boundp 'rmail-buffer)
  213. rmail-buffer)
  214. (error "No RMail buffer available"))
  215. ;; At this point, we are in rmail mode, so the rmail funcs are loaded.
  216. (if (fboundp 'rmail-get-header) ; Emacs 23
  217. (rmail-get-header field)
  218. (with-current-buffer rmail-buffer
  219. (save-restriction
  220. ;; Don't warn about this when compiling Emacs 23.
  221. (with-no-warnings (rmail-narrow-to-non-pruned-header))
  222. (mail-fetch-field field)))))
  223. ;;; Gnus
  224. ;; For gnus-buffer-exists-p, although it seems that could be replaced by:
  225. ;; (and buffer (get-buffer buffer))
  226. (eval-when-compile (require 'gnus-util))
  227. (defvar gnus-article-buffer)
  228. (declare-function gnus-group-read-ephemeral-group "gnus-group"
  229. (group method &optional activate quit-config
  230. request-only select-articles parameters number))
  231. (declare-function gnus-summary-toggle-header "gnus-sum" (&optional arg))
  232. (declare-function message-field-value "message" (header &optional not-all))
  233. ;; Display function:
  234. (defun mairix-gnus-ephemeral-nndoc (folder)
  235. "Create ephemeral nndoc group for reading mbox file FOLDER in Gnus."
  236. (unless (and (fboundp 'gnus-alive-p)
  237. (gnus-alive-p))
  238. (error "Gnus is not running"))
  239. (gnus-group-read-ephemeral-group
  240. ;; add randomness to group string to prevent Gnus from using a
  241. ;; cached version
  242. (format "mairix.%s" (number-to-string (random 10000)))
  243. `(nndoc "mairix"
  244. (nndoc-address ,folder)
  245. (nndoc-article-type mbox))))
  246. ;; Fetching mail header field:
  247. (defun mairix-gnus-fetch-field (field)
  248. "Get mail header FIELD for current message using Gnus."
  249. (unless (and (fboundp 'gnus-alive-p)
  250. (gnus-alive-p))
  251. (error "Gnus is not running"))
  252. (unless (gnus-buffer-exists-p gnus-article-buffer)
  253. (error "No article buffer available"))
  254. (with-current-buffer gnus-article-buffer
  255. ;; gnus-art requires gnus-sum and message.
  256. (gnus-summary-toggle-header 1)
  257. (message-field-value field)))
  258. ;;; VM
  259. ;;; written by Ulrich Mueller
  260. (declare-function vm-quit "ext:vm-folder" (&optional no-change))
  261. (declare-function vm-visit-folder "ext:vm-startup"
  262. (folder &optional read-only))
  263. (declare-function vm-select-folder-buffer "ext:vm-macro" ()) ; defsubst
  264. (declare-function vm-check-for-killed-summary "ext:vm-misc" ())
  265. (declare-function vm-error-if-folder-empty "ext:vm-misc" ())
  266. (declare-function vm-get-header-contents "ext:vm-summary"
  267. (message header-name-regexp &optional clump-sep))
  268. (declare-function vm-select-marked-or-prefixed-messages "ext:vm-folder"
  269. (prefix))
  270. ;; Display function
  271. (defun mairix-vm-display (folder)
  272. "Display mbox file FOLDER with VM."
  273. (require 'vm)
  274. ;; check if folder is already open and if so, kill it
  275. (let ((buf (get-file-buffer folder)))
  276. (when buf
  277. (set-buffer buf)
  278. (set-buffer-modified-p nil)
  279. (condition-case nil
  280. (vm-quit t)
  281. (error nil))
  282. (kill-buffer buf)))
  283. (vm-visit-folder folder t))
  284. ;; Fetching mail header field
  285. (defun mairix-vm-fetch-field (field)
  286. "Get mail header FIELD for current message using VM."
  287. (save-excursion
  288. (vm-select-folder-buffer)
  289. (vm-check-for-killed-summary)
  290. (vm-error-if-folder-empty)
  291. (vm-get-header-contents
  292. (car (vm-select-marked-or-prefixed-messages 1)) field)))
  293. ;;;; Main interactive functions
  294. (defun mairix-search (search threads)
  295. "Call Mairix with SEARCH.
  296. If THREADS is non-nil, also display whole threads of found
  297. messages. Results will be put into the default search file."
  298. (interactive
  299. (list
  300. (read-string "Query: ")
  301. (y-or-n-p "Include threads? ")))
  302. (when (mairix-call-mairix
  303. (split-string search)
  304. nil
  305. threads)
  306. (mairix-show-folder mairix-search-file)))
  307. (defun mairix-use-saved-search ()
  308. "Use a saved search for querying Mairix."
  309. (interactive)
  310. (let* ((completions
  311. (mapcar (lambda (el) (list (car el))) mairix-saved-searches))
  312. (search (completing-read "Name of search: " completions))
  313. (query (assoc search mairix-saved-searches))
  314. (folder (nth 2 query)))
  315. (when (not folder)
  316. (setq folder mairix-search-file))
  317. (when query
  318. (mairix-call-mairix
  319. (split-string (nth 1 query))
  320. folder
  321. (car (last query)))
  322. (mairix-show-folder folder))))
  323. (defun mairix-save-search ()
  324. "Save the last search."
  325. (interactive)
  326. (let* ((name (read-string "Name of the search: "))
  327. (exist (assoc name mairix-saved-searches)))
  328. (if (not exist)
  329. (add-to-list 'mairix-saved-searches
  330. (append (list name) mairix-last-search))
  331. (when
  332. (y-or-n-p
  333. "There is already a search with this name. \
  334. Overwrite existing entry? ")
  335. (setcdr (assoc name mairix-saved-searches) mairix-last-search))))
  336. (mairix-select-save))
  337. (defun mairix-edit-saved-searches-customize ()
  338. "Edit the list of saved searches in a customization buffer."
  339. (interactive)
  340. (custom-buffer-create (list (list 'mairix-saved-searches 'custom-variable))
  341. "*Customize Mairix Query*"
  342. (concat "\n\n" (make-string 65 ?=)
  343. "\nYou can now customize your saved Mairix searches by modifying\n\
  344. the variable mairix-saved-searches. Don't forget to save your\nchanges \
  345. in your .emacs by pressing `Save for Future Sessions'.\n"
  346. (make-string 65 ?=) "\n")))
  347. (autoload 'mail-strip-quoted-names "mail-utils")
  348. (defun mairix-search-from-this-article (threads)
  349. "Search messages from sender of the current article.
  350. This is effectively a shortcut for calling `mairix-search' with
  351. f:current_from. If prefix THREADS is non-nil, include whole
  352. threads."
  353. (interactive "P")
  354. (let ((get-mail-header
  355. (cadr (assq mairix-mail-program mairix-get-mail-header-functions))))
  356. (if get-mail-header
  357. (mairix-search
  358. (format "f:%s"
  359. (mail-strip-quoted-names
  360. (funcall get-mail-header "from")))
  361. threads)
  362. (error "No function for obtaining mail header specified"))))
  363. (defun mairix-search-thread-this-article ()
  364. "Search thread for the current article.
  365. This is effectively a shortcut for calling `mairix-search'
  366. with m:msgid of the current article and enabled threads."
  367. (interactive)
  368. (let ((get-mail-header
  369. (cadr (assq mairix-mail-program mairix-get-mail-header-functions)))
  370. mid)
  371. (unless get-mail-header
  372. (error "No function for obtaining mail header specified"))
  373. (setq mid (funcall get-mail-header "message-id"))
  374. (while (string-match "[<>]" mid)
  375. (setq mid (replace-match "" t t mid)))
  376. ;; mairix somehow does not like '$' in message-id
  377. (when (string-match "\\$" mid)
  378. (setq mid (concat mid "=")))
  379. (while (string-match "\\$" mid)
  380. (setq mid (replace-match "=," t t mid)))
  381. (mairix-search
  382. (format "m:%s" mid) t)))
  383. (defun mairix-widget-search-based-on-article ()
  384. "Create mairix query based on current article using widgets."
  385. (interactive)
  386. (mairix-widget-search
  387. (mairix-widget-get-values)))
  388. (defun mairix-edit-saved-searches ()
  389. "Edit current mairix searches."
  390. (interactive)
  391. (switch-to-buffer mairix-saved-searches-buffer)
  392. (erase-buffer)
  393. (setq mairix-searches-changed nil)
  394. (mairix-build-search-list)
  395. (mairix-searches-mode)
  396. (hl-line-mode))
  397. (defvar mairix-widgets)
  398. (defun mairix-widget-search (&optional mvalues)
  399. "Create mairix query interactively using graphical widgets.
  400. MVALUES may contain values from current article."
  401. (interactive)
  402. ;; Select window for mairix customization
  403. (funcall mairix-widget-select-window-function)
  404. ;; generate widgets
  405. (mairix-widget-create-query mvalues)
  406. ;; generate Buttons
  407. (widget-create 'push-button
  408. :notify
  409. (lambda (&rest ignore)
  410. (mairix-widget-send-query mairix-widgets))
  411. "Send Query")
  412. (widget-insert " ")
  413. (widget-create 'push-button
  414. :notify
  415. (lambda (&rest ignore)
  416. (mairix-widget-save-search mairix-widgets))
  417. "Save search")
  418. (widget-insert " ")
  419. (widget-create 'push-button
  420. :notify (lambda (&rest ignore)
  421. (kill-buffer mairix-customize-query-buffer))
  422. "Cancel")
  423. (use-local-map widget-keymap)
  424. (widget-setup)
  425. (goto-char (point-min)))
  426. (defun mairix-update-database ()
  427. "Call mairix for updating the database for SERVERS.
  428. Mairix will be called asynchronously unless
  429. `mairix-synchronous-update' is t. Mairix will be called with
  430. `mairix-update-options'."
  431. (interactive)
  432. (let ((commandsplit (split-string mairix-command))
  433. args)
  434. (if mairix-synchronous-update
  435. (progn
  436. (setq args (append (list (car commandsplit) nil
  437. (get-buffer-create mairix-output-buffer)
  438. nil)))
  439. (if (> (length commandsplit) 1)
  440. (setq args (append args
  441. (cdr commandsplit)
  442. mairix-update-options))
  443. (setq args (append args mairix-update-options)))
  444. (apply 'call-process args))
  445. (progn
  446. (message "Updating mairix database...")
  447. (setq args (append (list "mairixupdate" (get-buffer-create mairix-output-buffer)
  448. (car commandsplit))))
  449. (if (> (length commandsplit) 1)
  450. (setq args (append args (cdr commandsplit) mairix-update-options))
  451. (setq args (append args mairix-update-options)))
  452. (set-process-sentinel
  453. (apply 'start-process args)
  454. 'mairix-sentinel-mairix-update-finished)))))
  455. ;;;; Helper functions
  456. (defun mairix-show-folder (folder)
  457. "Display mail FOLDER with mail program.
  458. The mail program is given by `mairix-mail-program'."
  459. (let ((display-function
  460. (cadr (assq mairix-mail-program mairix-display-functions))))
  461. (if display-function
  462. (funcall display-function
  463. (concat
  464. (file-name-as-directory
  465. (expand-file-name mairix-file-path))
  466. folder))
  467. (error "No mail program set"))))
  468. (defun mairix-call-mairix (query file threads)
  469. "Call Mairix with QUERY and output FILE.
  470. If FILE is nil, use default. If THREADS is non-nil, also return
  471. whole threads. Function returns t if messages were found."
  472. (let* ((commandsplit (split-string mairix-command))
  473. (args (cons (car commandsplit)
  474. `(nil ,(get-buffer-create mairix-output-buffer) nil)))
  475. rval)
  476. (with-current-buffer mairix-output-buffer
  477. (erase-buffer))
  478. (when (> (length commandsplit) 1)
  479. (setq args (append args (cdr commandsplit))))
  480. (when threads
  481. (setq args (append args '("-t"))))
  482. (when (stringp query)
  483. (setq query (split-string query)))
  484. (setq mairix-last-search (list (mapconcat 'identity query " ")
  485. file threads))
  486. (when (not file)
  487. (setq file mairix-search-file))
  488. (setq file
  489. (concat
  490. (file-name-as-directory
  491. (expand-file-name
  492. mairix-file-path))
  493. file))
  494. (setq rval
  495. (apply 'call-process
  496. (append args (list "-o" file) query)))
  497. (if (zerop rval)
  498. (with-current-buffer mairix-output-buffer
  499. (goto-char (point-min))
  500. (re-search-forward "^Matched.*messages")
  501. (message (match-string 0)))
  502. (if (and (= rval 1)
  503. (with-current-buffer mairix-output-buffer
  504. (goto-char (point-min))
  505. (looking-at "^Matched 0 messages")))
  506. (message "No messages found")
  507. (error "Error running Mairix. See buffer %s for details"
  508. mairix-output-buffer)))
  509. (zerop rval)))
  510. (defun mairix-replace-invalid-chars (header)
  511. "Replace invalid characters in HEADER for mairix query."
  512. (when header
  513. (while (string-match "[^-.@/,^=~& [:alnum:]]" header)
  514. (setq header (replace-match "" t t header)))
  515. (while (string-match "[& ]" header)
  516. (setq header (replace-match "," t t header)))
  517. header))
  518. (defun mairix-sentinel-mairix-update-finished (proc status)
  519. "Sentinel for mairix update process PROC with STATUS."
  520. (if (equal status "finished\n")
  521. (message "Updating mairix database... done")
  522. (error "There was an error updating the mairix database. \
  523. See %s for details" mairix-output-buffer)))
  524. ;;;; Widget stuff
  525. (defun mairix-widget-send-query (widgets)
  526. "Send query from WIDGETS to mairix binary."
  527. (mairix-search
  528. (mairix-widget-make-query-from-widgets widgets)
  529. (if (widget-value (cadr (assoc "Threads" widgets))) t))
  530. (kill-buffer mairix-customize-query-buffer))
  531. (defun mairix-widget-save-search (widgets)
  532. "Save search based on WIDGETS for future use."
  533. (let ((mairix-last-search
  534. `( ,(mairix-widget-make-query-from-widgets widgets)
  535. nil
  536. ,(widget-value (cadr (assoc "Threads" widgets))))))
  537. (mairix-save-search)
  538. (kill-buffer mairix-customize-query-buffer)))
  539. (defun mairix-widget-make-query-from-widgets (widgets)
  540. "Create mairix query from widget values WIDGETS."
  541. (let (query temp flag)
  542. ;; first we do the editable fields
  543. (dolist (cur mairix-widget-fields-list)
  544. ;; See if checkbox is checked
  545. (when (widget-value
  546. (cadr (assoc (concat "c" (car (cddr cur))) widgets)))
  547. ;; create query for the field
  548. (push
  549. (concat
  550. (nth 1 cur)
  551. ":"
  552. (mairix-replace-invalid-chars
  553. (widget-value
  554. (cadr (assoc (concat "e" (car (cddr cur))) widgets)))))
  555. query)))
  556. ;; Flags
  557. (when (member 'flags mairix-widget-other)
  558. (setq flag
  559. (mapconcat
  560. (function
  561. (lambda (flag)
  562. (setq temp
  563. (widget-value (cadr (assoc (car flag) mairix-widgets))))
  564. (if (string= "yes" temp)
  565. (cadr flag)
  566. (if (string= "no" temp)
  567. (concat "-" (cadr flag))))))
  568. '(("seen" "s") ("replied" "r") ("flagged" "f")) ""))
  569. (when (not (zerop (length flag)))
  570. (push (concat "F:" flag) query)))
  571. ;; return query string
  572. (mapconcat 'identity query " ")))
  573. (defun mairix-widget-create-query (&optional values)
  574. "Create widgets for creating mairix queries.
  575. Fill in VALUES if based on an article."
  576. (let (allwidgets)
  577. (when (get-buffer mairix-customize-query-buffer)
  578. (kill-buffer mairix-customize-query-buffer))
  579. (switch-to-buffer mairix-customize-query-buffer)
  580. (kill-all-local-variables)
  581. (erase-buffer)
  582. (widget-insert
  583. "Specify your query for Mairix using check boxes for activating fields.\n\n")
  584. (widget-insert
  585. (concat "Use ~word to match messages "
  586. (propertize "not" 'face 'italic)
  587. " containing the word)\n"
  588. " substring= to match words containing the substring\n"
  589. " substring=N to match words containing the substring, allowing\n"
  590. " up to N errors(missing/extra/different letters)\n"
  591. " ^substring= to match the substring at the beginning of a word.\n"))
  592. (widget-insert
  593. (format-message
  594. "Whitespace will be converted to `,' (i.e. AND). Use `/' for OR.\n\n"))
  595. (setq mairix-widgets (mairix-widget-build-editable-fields values))
  596. (when (member 'flags mairix-widget-other)
  597. (widget-insert "\nFlags:\n Seen: ")
  598. (mairix-widget-add "seen"
  599. 'menu-choice
  600. :value "ignore"
  601. '(item "yes") '(item "no") '(item "ignore"))
  602. (widget-insert " Replied: ")
  603. (mairix-widget-add "replied"
  604. 'menu-choice
  605. :value "ignore"
  606. '(item "yes") '(item "no") '(item "ignore"))
  607. (widget-insert " Ticked: ")
  608. (mairix-widget-add "flagged"
  609. 'menu-choice
  610. :value "ignore"
  611. '(item "yes") '(item "no") '(item "ignore")))
  612. (when (member 'threads mairix-widget-other)
  613. (widget-insert "\n")
  614. (mairix-widget-add "Threads" 'checkbox nil))
  615. (widget-insert " Show full threads\n\n")))
  616. (defun mairix-widget-build-editable-fields (values)
  617. "Build editable field widgets in `nnmairix-widget-fields-list'.
  618. VALUES may contain values for editable fields from current article."
  619. (let ((ret))
  620. (mapc
  621. (function
  622. (lambda (field)
  623. (setq field (car (cddr field)))
  624. (setq
  625. ret
  626. (nconc
  627. (list
  628. (list
  629. (concat "c" field)
  630. (widget-create 'checkbox
  631. :tag field
  632. :notify (lambda (widget &rest ignore)
  633. (mairix-widget-toggle-activate widget))
  634. nil)))
  635. (list
  636. (list
  637. (concat "e" field)
  638. (widget-create 'editable-field
  639. :size 60
  640. :format (concat " " field ":"
  641. (make-string
  642. (- 11 (length field)) ?\ )
  643. "%v")
  644. :value (or (cadr (assoc field values)) ""))))
  645. ret))
  646. (widget-insert "\n")
  647. ;; Deactivate editable field
  648. (widget-apply (cadr (nth 1 ret)) :deactivate)))
  649. mairix-widget-fields-list)
  650. ret))
  651. (defun mairix-widget-add (name &rest args)
  652. "Add a widget NAME with optional ARGS."
  653. (push
  654. (list name
  655. (apply 'widget-create args))
  656. mairix-widgets))
  657. (defun mairix-widget-toggle-activate (widget)
  658. "Toggle activation status of WIDGET depending on checkbox value."
  659. (let ((field (widget-get widget :tag)))
  660. (if (widget-value widget)
  661. (widget-apply
  662. (cadr (assoc (concat "e" field) mairix-widgets))
  663. :activate)
  664. (widget-apply
  665. (cadr (assoc (concat "e" field) mairix-widgets))
  666. :deactivate)))
  667. (widget-setup))
  668. ;;;; Major mode for editing/deleting/saving searches
  669. (defvar mairix-searches-mode-map
  670. (let ((map (make-keymap)))
  671. (define-key map [(return)] 'mairix-select-search)
  672. (define-key map [(down)] 'mairix-next-search)
  673. (define-key map [(up)] 'mairix-previous-search)
  674. (define-key map [(right)] 'mairix-next-search)
  675. (define-key map [(left)] 'mairix-previous-search)
  676. (define-key map "\C-p" 'mairix-previous-search)
  677. (define-key map "\C-n" 'mairix-next-search)
  678. (define-key map [(q)] 'mairix-select-quit)
  679. (define-key map [(e)] 'mairix-select-edit)
  680. (define-key map [(d)] 'mairix-select-delete)
  681. (define-key map [(s)] 'mairix-select-save)
  682. map)
  683. "`mairix-searches-mode' keymap.")
  684. (defvar mairix-searches-mode-font-lock-keywords
  685. '(("^\\([0-9]+\\)"
  686. (1 font-lock-constant-face))
  687. ("^[0-9 ]+\\(Name:\\) \\(.*\\)"
  688. (1 font-lock-keyword-face) (2 font-lock-string-face))
  689. ("^[ ]+\\(Query:\\) \\(.*\\) , "
  690. (1 font-lock-keyword-face) (2 font-lock-string-face))
  691. (", \\(Threads:\\) \\(.*\\)"
  692. (1 font-lock-keyword-face) (2 font-lock-constant-face))
  693. ("^\\([A-Z].*\\)$"
  694. (1 font-lock-comment-face))
  695. ("^[ ]+\\(Folder:\\) \\(.*\\)"
  696. (1 font-lock-keyword-face) (2 font-lock-string-face))))
  697. (define-derived-mode mairix-searches-mode fundamental-mode "mairix-searches"
  698. "Major mode for editing mairix searches."
  699. :syntax-table text-mode-syntax-table
  700. (setq-local font-lock-defaults '(mairix-searches-mode-font-lock-keywords)))
  701. (defun mairix-build-search-list ()
  702. "Display saved searches in current buffer."
  703. (insert "These are your current saved mairix searches.\n\
  704. You may use the following keys in this buffer: \n\
  705. Return: execute search, e: edit, d: delete, s: save, q: quit\n\
  706. Use cursor keys or C-n,C-p to select next/previous search.\n\n")
  707. (let ((num 0)
  708. (beg (point))
  709. current)
  710. (while (< num (length mairix-saved-searches))
  711. (setq current (nth num mairix-saved-searches))
  712. (setq num (1+ num))
  713. (mairix-insert-search-line num current)
  714. (insert "\n"))
  715. (goto-char beg)))
  716. (defun mairix-insert-search-line (number field)
  717. "Insert new mairix query with NUMBER and values FIELD in buffer."
  718. (insert
  719. (format "%d Name: %s\n Query: %s , Threads: %s\n Folder: %s\n"
  720. number
  721. (car field)
  722. (nth 1 field)
  723. (if (nth 3 field)
  724. "Yes"
  725. "No")
  726. (if (nth 2 field)
  727. (nth 2 field)
  728. "Default"))))
  729. (defun mairix-select-search ()
  730. "Call mairix with currently selected search."
  731. (interactive)
  732. (beginning-of-line)
  733. (if (not (looking-at "[0-9]+ Name"))
  734. (progn
  735. (ding)
  736. (message "Put cursor on a line with a search name first"))
  737. (progn
  738. (let* ((query (nth
  739. (1- (read (current-buffer)))
  740. mairix-saved-searches))
  741. (folder (nth 2 query)))
  742. (when (not folder)
  743. (setq folder mairix-search-file))
  744. (mairix-call-mairix
  745. (split-string (nth 1 query))
  746. folder
  747. (car (last query)))
  748. (mairix-select-quit)
  749. (mairix-show-folder folder)))))
  750. (defun mairix-next-search ()
  751. "Jump to next search."
  752. (interactive)
  753. (if (search-forward-regexp "^[0-9]+"
  754. (point-max)
  755. t
  756. 2)
  757. (beginning-of-line)
  758. (ding)))
  759. (defun mairix-previous-search ()
  760. "Jump to previous search."
  761. (interactive)
  762. (if (search-backward-regexp "^[0-9]+"
  763. (point-min)
  764. t)
  765. (beginning-of-line)
  766. (ding)))
  767. (defun mairix-select-quit ()
  768. "Quit mairix search mode."
  769. (interactive)
  770. (when mairix-searches-changed
  771. (mairix-select-save))
  772. (kill-buffer nil))
  773. (defun mairix-select-save ()
  774. "Save current mairix searches."
  775. (interactive)
  776. (when (y-or-n-p "Save mairix searches permanently in your .emacs? ")
  777. (customize-save-variable 'mairix-saved-searches mairix-saved-searches)))
  778. (defun mairix-select-edit ()
  779. "Edit currently selected mairix search."
  780. (interactive)
  781. (beginning-of-line)
  782. (if (not (looking-at "[0-9]+ Name"))
  783. (error "Put cursor on a line with a search name first")
  784. (progn
  785. (let* ((number (1- (read (current-buffer))))
  786. (query (nth number mairix-saved-searches))
  787. (folder (nth 2 query))
  788. newname newquery newfolder threads)
  789. (backward-char)
  790. (setq newname (read-string "Name of the search: " (car query)))
  791. (when (assoc newname (remq (nth number mairix-saved-searches)
  792. mairix-saved-searches))
  793. (error "This name does already exist"))
  794. (setq newquery (read-string "Query: " (nth 1 query)))
  795. (setq threads (y-or-n-p "Include whole threads? "))
  796. (setq newfolder
  797. (read-string "Mail folder (use empty string for default): "
  798. folder))
  799. (when (zerop (length newfolder))
  800. (setq newfolder nil))
  801. ;; set new values
  802. (setcar (nth number mairix-saved-searches) newname)
  803. (setcdr (nth number mairix-saved-searches)
  804. (list newquery newfolder threads))
  805. (setq mairix-searches-changed t)
  806. (let ((beg (point)))
  807. (forward-line 3)
  808. (end-of-line)
  809. (delete-region beg (point))
  810. (mairix-insert-search-line (1+ number)
  811. (nth number mairix-saved-searches))
  812. (goto-char beg))))))
  813. (defun mairix-select-delete ()
  814. "Delete currently selected mairix search."
  815. (interactive)
  816. (if (not (looking-at "[0-9]+ Name"))
  817. (error "Put cursor on a line with a search name first")
  818. (progn
  819. (let* ((number (1- (read (current-buffer))))
  820. (query (nth number mairix-saved-searches))
  821. beg)
  822. (backward-char)
  823. (when (y-or-n-p (format "Delete search %s ? " (car query)))
  824. (setq mairix-saved-searches
  825. (delq query mairix-saved-searches))
  826. (setq mairix-searches-changed t)
  827. (setq beg (point))
  828. (forward-line 4)
  829. (beginning-of-line)
  830. (delete-region beg (point))
  831. (while (search-forward-regexp "^[0-9]+"
  832. (point-max)
  833. t
  834. 1)
  835. (replace-match (number-to-string
  836. (setq number (1+ number)))))))
  837. (beginning-of-line))))
  838. (defun mairix-widget-get-values ()
  839. "Create values for editable fields from current article."
  840. (let ((get-mail-header
  841. (cadr (assq mairix-mail-program mairix-get-mail-header-functions))))
  842. (if get-mail-header
  843. (save-excursion
  844. (save-restriction
  845. (mapcar
  846. (function
  847. (lambda (field)
  848. (list (car (cddr field))
  849. (if (car field)
  850. (mairix-replace-invalid-chars
  851. (funcall get-mail-header (car field)))
  852. nil))))
  853. mairix-widget-fields-list)))
  854. (error "No function for obtaining mail header specified"))))
  855. (provide 'mairix)
  856. ;;; mairix.el ends here