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