mh-funcs.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ;;; mh-funcs.el --- MH-E functions not everyone will use right away
  2. ;; Copyright (C) 1993, 1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Bill Wohler <wohler@newt.com>
  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. ;; Putting these functions in a separate file lets MH-E start up faster,
  20. ;; since less Lisp code needs to be loaded all at once.
  21. ;; Please add the functions in alphabetical order. If only one or two
  22. ;; small support routines are needed, place them with the function;
  23. ;; otherwise, create a separate section for them.
  24. ;;; Change Log:
  25. ;;; Code:
  26. (require 'mh-e)
  27. (require 'mh-scan)
  28. ;;;###mh-autoload
  29. (defun mh-burst-digest ()
  30. "Break up digest into separate messages\\<mh-folder-mode-map>.
  31. This command uses the MH command \"burst\" to break out each
  32. message in the digest into its own message. Using this command,
  33. you can quickly delete unwanted messages, like this: Once the
  34. digest is split up, toggle out of MH-Folder Show mode with
  35. \\[mh-toggle-showing] so that the scan lines fill the screen and
  36. messages aren't displayed. Then use \\[mh-delete-msg] to quickly
  37. delete messages that you don't want to read (based on the
  38. \"Subject:\" header field). You can also burst the digest to
  39. reply directly to the people who posted the messages in the
  40. digest. One problem you may encounter is that the \"From:\"
  41. header fields are preceded with a \">\" so that your reply can't
  42. create the \"To:\" field correctly. In this case, you must
  43. correct the \"To:\" field yourself."
  44. (interactive)
  45. (let ((digest (mh-get-msg-num t)))
  46. (mh-process-or-undo-commands mh-current-folder)
  47. (mh-set-folder-modified-p t) ; lock folder while bursting
  48. (message "Bursting digest...")
  49. (mh-exec-cmd "burst" mh-current-folder digest "-inplace")
  50. (with-mh-folder-updating (t)
  51. (beginning-of-line)
  52. (delete-region (point) (point-max)))
  53. (mh-regenerate-headers (format "%d-last" digest) t)
  54. (mh-goto-cur-msg)
  55. (message "Bursting digest...done")))
  56. ;;;###mh-autoload
  57. (defun mh-copy-msg (range folder)
  58. "Copy RANGE to FOLDER\\<mh-folder-mode-map>.
  59. If you wish to copy a message to another folder, you can use this
  60. command (see the \"-link\" argument to \"refile\"). Like the
  61. command \\[mh-refile-msg], this command prompts you for the name
  62. of the target folder and you can specify a range. Note that
  63. unlike the command \\[mh-refile-msg], the copy takes place
  64. immediately. The original copy remains in the current folder.
  65. Check the documentation of `mh-interactive-range' to see how
  66. RANGE is read in interactive use."
  67. (interactive (list (mh-interactive-range "Copy")
  68. (mh-prompt-for-folder "Copy to" "" t)))
  69. (let ((msg-list (let ((result ()))
  70. (mh-iterate-on-range msg range
  71. (mh-notate nil mh-note-copied mh-cmd-note)
  72. (push msg result))
  73. result)))
  74. (mh-exec-cmd "refile" (mh-coalesce-msg-list msg-list)
  75. "-link" "-src" mh-current-folder folder)))
  76. ;;;###mh-autoload
  77. (defun mh-kill-folder ()
  78. "Remove folder.
  79. Remove all of the messages (files) within the current folder, and
  80. then remove the folder (directory) itself.
  81. Run the abnormal hook `mh-kill-folder-suppress-prompt-hooks'. The
  82. hook functions are called with no arguments and should return a
  83. non-nil value to suppress the normal prompt when you remove a
  84. folder. This is useful for folders that are easily regenerated."
  85. (interactive)
  86. (if (or (run-hook-with-args-until-success
  87. 'mh-kill-folder-suppress-prompt-hooks)
  88. (yes-or-no-p (format "Remove folder %s (and all included messages)? "
  89. mh-current-folder)))
  90. (let ((folder mh-current-folder)
  91. (window-config mh-previous-window-config))
  92. (mh-set-folder-modified-p t) ; lock folder to kill it
  93. (mh-exec-cmd-daemon "rmf" 'mh-rmf-daemon folder)
  94. (when (boundp 'mh-speed-folder-map)
  95. (mh-speed-invalidate-map folder))
  96. (mh-remove-from-sub-folders-cache folder)
  97. (mh-set-folder-modified-p nil) ; so kill-buffer doesn't complain
  98. (if (and mh-show-buffer (get-buffer mh-show-buffer))
  99. (kill-buffer mh-show-buffer))
  100. (if (get-buffer folder)
  101. (kill-buffer folder))
  102. (when window-config
  103. (set-window-configuration window-config))
  104. (message "Folder %s removed" folder))
  105. (message "Folder not removed")))
  106. (defun mh-rmf-daemon (process output)
  107. "The rmf PROCESS puts OUTPUT in temporary buffer.
  108. Display the results only if something went wrong."
  109. (set-buffer (get-buffer-create mh-temp-buffer))
  110. (insert-before-markers output)
  111. (when (save-excursion
  112. (goto-char (point-min))
  113. (re-search-forward "^rmf: " (point-max) t))
  114. (display-buffer mh-temp-buffer)))
  115. ;; Shush compiler.
  116. (defvar view-exit-action)
  117. ;;;###mh-autoload
  118. (defun mh-list-folders ()
  119. "List mail folders."
  120. (interactive)
  121. (let ((temp-buffer mh-folders-buffer))
  122. (with-output-to-temp-buffer temp-buffer
  123. (with-current-buffer temp-buffer
  124. (erase-buffer)
  125. (message "Listing folders...")
  126. (mh-exec-cmd-output "folders" t (if mh-recursive-folders-flag
  127. "-recurse"
  128. "-norecurse"))
  129. (goto-char (point-min))
  130. (mh-view-mode-enter)
  131. (setq view-exit-action 'kill-buffer)
  132. (message "Listing folders...done")))))
  133. ;;;###mh-autoload
  134. (defun mh-pack-folder (range)
  135. "Pack folder\\<mh-folder-mode-map>.
  136. This command packs the folder, removing gaps from the numbering
  137. sequence. If you don't want to rescan the entire folder
  138. afterward, this command will accept a RANGE. Check the
  139. documentation of `mh-interactive-range' to see how RANGE is read
  140. in interactive use.
  141. This command will ask if you want to process refiles or deletes
  142. first and then either run \\[mh-execute-commands] for you or undo
  143. the pending refiles and deletes.
  144. The hook `mh-pack-folder-hook' is run after the folder is packed;
  145. see its documentation for variables it can use."
  146. (interactive (list (if current-prefix-arg
  147. (mh-read-range "Scan" mh-current-folder t nil t
  148. mh-interpret-number-as-range-flag)
  149. '("all"))))
  150. (let ((threaded-flag (memq 'unthread mh-view-ops)))
  151. (mh-pack-folder-1 range)
  152. (mh-goto-cur-msg)
  153. (when mh-index-data
  154. (mh-index-update-maps mh-current-folder))
  155. (cond (threaded-flag (mh-toggle-threads))
  156. (mh-index-data (mh-index-insert-folder-headers))))
  157. (run-hooks 'mh-pack-folder-hook)
  158. (message "Packing folder...done"))
  159. (defun mh-pack-folder-1 (range)
  160. "Close and pack the current folder.
  161. Display RANGE after packing, or the entire folder if RANGE is nil."
  162. (mh-process-or-undo-commands mh-current-folder)
  163. (message "Packing folder...")
  164. (mh-set-folder-modified-p t) ; lock folder while packing
  165. (save-excursion
  166. (mh-exec-cmd-quiet t "folder" mh-current-folder "-pack"
  167. "-norecurse" "-fast"))
  168. (mh-reset-threads-and-narrowing)
  169. (mh-regenerate-headers range))
  170. ;;;###mh-autoload
  171. (defun mh-page-digest ()
  172. "Display next message in digest."
  173. (interactive)
  174. (mh-in-show-buffer (mh-show-buffer)
  175. ;; Go to top of screen (in case user moved point).
  176. (move-to-window-line 0)
  177. (let ((case-fold-search nil))
  178. ;; Search for blank line and then for From:
  179. (or (and (search-forward "\n\n" nil t)
  180. (re-search-forward "^From:" nil t))
  181. (error "No more messages in digest")))
  182. ;; Go back to previous blank line, then forward to the first non-blank.
  183. (search-backward "\n\n" nil t)
  184. (forward-line 2)
  185. (mh-recenter 0)))
  186. ;;;###mh-autoload
  187. (defun mh-page-digest-backwards ()
  188. "Display previous message in digest."
  189. (interactive)
  190. (mh-in-show-buffer (mh-show-buffer)
  191. ;; Go to top of screen (in case user moved point).
  192. (move-to-window-line 0)
  193. (let ((case-fold-search nil))
  194. (beginning-of-line)
  195. (or (and (search-backward "\n\n" nil t)
  196. (re-search-backward "^From:" nil t))
  197. (error "No previous message in digest")))
  198. ;; Go back to previous blank line, then forward to the first non-blank.
  199. (if (search-backward "\n\n" nil t)
  200. (forward-line 2))
  201. (mh-recenter 0)))
  202. ;;;###mh-autoload
  203. (defun mh-pipe-msg (command include-header)
  204. "Pipe message through shell command COMMAND.
  205. You are prompted for the Unix command through which you wish to
  206. run your message. If you give a prefix argument INCLUDE-HEADER to
  207. this command, the message header is included in the text passed
  208. to the command."
  209. (interactive
  210. (list (read-string "Shell command on message: ") current-prefix-arg))
  211. (let ((msg-file-to-pipe (mh-msg-filename (mh-get-msg-num t)))
  212. (message-directory default-directory))
  213. (with-current-buffer (get-buffer-create mh-temp-buffer)
  214. (erase-buffer)
  215. (insert-file-contents msg-file-to-pipe)
  216. (goto-char (point-min))
  217. (if (not include-header) (search-forward "\n\n"))
  218. (let ((default-directory message-directory))
  219. (shell-command-on-region (point) (point-max) command nil)))))
  220. ;;;###mh-autoload
  221. (defun mh-sort-folder (&optional extra-args)
  222. "Sort folder.
  223. By default, messages are sorted by date. The option
  224. `mh-sortm-args' holds extra arguments to pass on to the command
  225. \"sortm\" when a prefix argument EXTRA-ARGS is used."
  226. (interactive "P")
  227. (mh-process-or-undo-commands mh-current-folder)
  228. (setq mh-next-direction 'forward)
  229. (mh-set-folder-modified-p t) ; lock folder while sorting
  230. (message "Sorting folder...")
  231. (let ((threaded-flag (memq 'unthread mh-view-ops)))
  232. (mh-exec-cmd "sortm" mh-current-folder (if extra-args mh-sortm-args))
  233. (when mh-index-data
  234. (mh-index-update-maps mh-current-folder))
  235. (message "Sorting folder...done")
  236. (mh-scan-folder mh-current-folder "all")
  237. (cond (threaded-flag (mh-toggle-threads))
  238. (mh-index-data (mh-index-insert-folder-headers)))))
  239. ;;;###mh-autoload
  240. (defun mh-store-msg (directory)
  241. "Unpack message created with \"uudecode\" or \"shar\".
  242. The default DIRECTORY for extraction is the current directory;
  243. however, you have a chance to specify a different extraction
  244. directory. The next time you use this command, the default
  245. directory is the last directory you used. If you would like to
  246. change the initial default directory, customize the option
  247. `mh-store-default-directory', change the value from \"Current\"
  248. to \"Directory\", and then enter the name of the directory for
  249. storing the content of these messages."
  250. (interactive (list (let ((udir (or mh-store-default-directory
  251. default-directory)))
  252. (read-directory-name "Store message in directory: "
  253. udir udir nil))))
  254. (let ((msg-file-to-store (mh-msg-filename (mh-get-msg-num t))))
  255. (with-current-buffer (get-buffer-create mh-temp-buffer)
  256. (erase-buffer)
  257. (insert-file-contents msg-file-to-store)
  258. (mh-store-buffer directory))))
  259. (defun mh-store-buffer (directory)
  260. "Unpack buffer created with \"uudecode\" or \"shar\".
  261. See `mh-store-msg' for a description of DIRECTORY."
  262. (interactive (list (let ((udir (or mh-store-default-directory
  263. default-directory)))
  264. (read-directory-name "Store buffer in directory: "
  265. udir udir nil))))
  266. (let ((store-directory (expand-file-name directory))
  267. (sh-start (save-excursion
  268. (goto-char (point-min))
  269. (if (re-search-forward
  270. "^#![ \t]*/bin/sh\\|^#\\|^: " nil t)
  271. (progn
  272. ;; The "cut here" pattern was removed from above
  273. ;; because it seemed to hurt more than help.
  274. ;; But keep this to make it easier to put it back.
  275. (if (looking-at "^[^a-z0-9\"]*cut here\\b")
  276. (forward-line 1))
  277. (beginning-of-line)
  278. (if (looking-at "^[#:]....+\n\\( ?\n\\)?end$")
  279. nil ;most likely end of a uuencode
  280. (point))))))
  281. (command "sh")
  282. (uudecode-filename "(unknown filename)")
  283. log-begin)
  284. (if (not sh-start)
  285. (save-excursion
  286. (goto-char (point-min))
  287. (if (re-search-forward "^begin [0-7]+ " nil t)
  288. (setq uudecode-filename
  289. (buffer-substring (point)
  290. (progn (end-of-line) (point)))))))
  291. (with-current-buffer (get-buffer-create mh-log-buffer)
  292. (setq log-begin (mh-truncate-log-buffer))
  293. (if (not (file-directory-p store-directory))
  294. (progn
  295. (insert "mkdir " directory "\n")
  296. (call-process "mkdir" nil mh-log-buffer t store-directory)))
  297. (insert "cd " directory "\n")
  298. (setq mh-store-default-directory directory)
  299. (if (not sh-start)
  300. (progn
  301. (setq command "uudecode")
  302. (insert uudecode-filename " being uudecoded...\n"))))
  303. (set-window-start (display-buffer mh-log-buffer) log-begin) ;watch progress
  304. (let ((default-directory (file-name-as-directory store-directory)))
  305. (if (equal (call-process-region sh-start (point-max) command
  306. nil mh-log-buffer t)
  307. 0)
  308. (with-current-buffer mh-log-buffer
  309. (insert "\n(mh-store finished)\n"))
  310. (error "Error occurred during execution of %s" command)))))
  311. ;;;###mh-autoload
  312. (defun mh-undo-folder (&rest ignored)
  313. "Undo all refiles and deletes in the current folder.
  314. Arguments are IGNORED (for `revert-buffer')."
  315. (interactive)
  316. (cond ((or mh-do-not-confirm-flag
  317. (yes-or-no-p "Undo all commands in folder? "))
  318. (setq mh-delete-list nil
  319. mh-refile-list nil
  320. mh-seq-list nil
  321. mh-next-direction 'forward)
  322. (with-mh-folder-updating (nil)
  323. (mh-remove-all-notation)))
  324. (t
  325. (message "Commands not undone"))))
  326. (provide 'mh-funcs)
  327. ;; Local Variables:
  328. ;; indent-tabs-mode: nil
  329. ;; sentence-end-double-space: nil
  330. ;; End:
  331. ;;; mh-funcs.el ends here