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