1 ;;; mh-limit.el --- MH-E display limits
3 ;; Copyright (C) 2001-2003, 2006-2013 Free Software Foundation, Inc.
5 ;; Author: Peter S. Galbraith <psg@debian.org>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; "Poor man's threading" by psg.
37 (autoload 'message-fetch-field
"message")
41 ;;; MH-Folder Commands
46 (defun mh-delete-subject ()
47 "Delete messages with same subject\\<mh-folder-mode-map>.
49 To delete messages faster, you can use this command to delete all
50 the messages with the same subject as the current message. This
51 command puts these messages in a sequence named \"subject\". You
52 can undo this action by using \\[mh-undo] with a prefix argument
53 and then specifying the \"subject\" sequence."
55 (let ((count (mh-subject-to-sequence nil
)))
57 ((not count
) ; No subject line, delete msg anyway
58 (mh-delete-msg (mh-get-msg-num t
)))
59 ((= 0 count
) ; No other msgs, delete msg anyway.
60 (message "No other messages with same Subject following this one")
61 (mh-delete-msg (mh-get-msg-num t
)))
62 (t ; We have a subject sequence.
63 (message "Marked %d messages for deletion" count
)
64 (mh-delete-msg 'subject
)))))
67 (defun mh-delete-subject-or-thread ()
68 "Delete messages with same subject or thread\\<mh-folder-mode-map>.
70 To delete messages faster, you can use this command to delete all
71 the messages with the same subject as the current message. This
72 command puts these messages in a sequence named \"subject\". You
73 can undo this action by using \\[mh-undo] with a prefix argument
74 and then specifying the \"subject\" sequence.
76 However, if the buffer is displaying a threaded view of the
77 folder then this command behaves like \\[mh-thread-delete]."
79 (if (memq 'unthread mh-view-ops
)
84 (defun mh-narrow-to-cc (&optional pick-expr
)
85 "Limit to messages with the same \"Cc:\" field.
86 With a prefix argument, edit PICK-EXPR.
88 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
90 (list (mh-edit-pick-expr
91 (mh-quote-pick-expr (mh-current-message-header-field 'cc
)))))
92 (mh-narrow-to-header-field 'cc pick-expr
))
95 (defun mh-narrow-to-from (&optional pick-expr
)
96 "Limit to messages with the same \"From:\" field.
97 With a prefix argument, edit PICK-EXPR.
99 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
101 (list (mh-edit-pick-expr
102 (mh-quote-pick-expr (mh-current-message-header-field 'from
)))))
103 (mh-narrow-to-header-field 'from pick-expr
))
106 (defun mh-narrow-to-range (range)
109 Check the documentation of `mh-interactive-range' to see how
110 RANGE is read in interactive use.
112 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
113 (interactive (list (mh-interactive-range "Narrow to")))
114 (when (assoc 'range mh-seq-list
) (mh-delete-seq 'range
))
115 (mh-add-msgs-to-seq (mh-range-to-msg-list range
) 'range
)
116 (mh-narrow-to-seq 'range
))
119 (defun mh-narrow-to-subject (&optional pick-expr
)
120 "Limit to messages with same subject.
121 With a prefix argument, edit PICK-EXPR.
122 The string Re: is removed from the search.
124 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
126 (list (mh-edit-pick-expr
127 (mh-quote-pick-expr (mh-current-message-header-field 'subject
)))))
129 (let ((case-fold-search t
))
130 (loop for s in pick-expr
131 collect
(mh-replace-regexp-in-string "re: *" "" s
))))
132 (mh-narrow-to-header-field 'subject pick-expr
))
135 (defun mh-narrow-to-to (&optional pick-expr
)
136 "Limit to messages with the same \"To:\" field.
137 With a prefix argument, edit PICK-EXPR.
139 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
141 (list (mh-edit-pick-expr
142 (mh-quote-pick-expr (mh-current-message-header-field 'to
)))))
143 (mh-narrow-to-header-field 'to pick-expr
))
149 (defun mh-subject-to-sequence (all)
150 "Put all following messages with same subject in sequence 'subject.
151 If arg ALL is t, move to beginning of folder buffer to collect all
153 If arg ALL is nil, collect only messages fron current one on forward.
155 Return number of messages put in the sequence:
157 nil -> there was no subject line.
159 0 -> there were no later messages with the same
160 subject (sequence not made)
162 >1 -> the total number of messages including current one."
163 (if (memq 'unthread mh-view-ops
)
164 (mh-subject-to-sequence-threaded all
)
165 (mh-subject-to-sequence-unthreaded all
)))
167 (defun mh-subject-to-sequence-threaded (all)
168 "Put all messages with the same subject in the 'subject sequence.
170 This function works when the folder is threaded. In this
171 situation the subject could get truncated and so the normal
172 matching doesn't work.
174 The parameter ALL is non-nil then all the messages in the buffer
175 are considered, otherwise only the messages after the current one
176 are taken into account."
177 (let* ((cur (mh-get-msg-num nil
))
178 (subject (mh-thread-find-msg-subject cur
))
181 (and (message "No subject line") nil
)
182 (setq region
(cons (if all
(point-min) (point)) (point-max)))
183 (mh-iterate-on-range msg region
184 (when (eq (mh-thread-find-msg-subject msg
) subject
)
186 (setq msgs
(sort msgs
#'mh-lessp
))
189 (when (assoc 'subject mh-seq-list
)
190 (mh-delete-seq 'subject
))
191 (mh-add-msgs-to-seq msgs
'subject
)
194 (defvar mh-limit-max-subject-size
41
195 "Maximum size of the subject part.
196 It would be desirable to avoid hard-coding this.")
198 (defun mh-subject-to-sequence-unthreaded (all)
199 "Put all following messages with same subject in sequence 'subject.
201 This function only works with an unthreaded folder. If arg ALL is
202 t, move to beginning of folder buffer to collect all messages. If
203 arg ALL is nil, collect only messages fron current one on
206 Return number of messages put in the sequence:
208 nil -> there was no subject line.
209 0 -> there were no later messages with the same
210 subject (sequence not made)
211 >1 -> the total number of messages including current one."
212 (if (not (eq major-mode
'mh-folder-mode
))
213 (error "Not in a folder buffer"))
216 (if (or (not (looking-at mh-scan-subject-regexp
))
217 (not (match-string 3))
218 (string-equal "" (match-string 3)))
219 (progn (message "No subject line")
221 (let ((subject (mh-match-string-no-properties 3))
223 (if (> (length subject
) mh-limit-max-subject-size
)
224 (setq subject
(substring subject
0 mh-limit-max-subject-size
)))
227 (goto-char (point-min)))
228 (while (re-search-forward mh-scan-subject-regexp nil t
)
229 (let ((this-subject (mh-match-string-no-properties 3)))
230 (if (> (length this-subject
) mh-limit-max-subject-size
)
231 (setq this-subject
(substring this-subject
232 0 mh-limit-max-subject-size
)))
233 (if (string-equal this-subject subject
)
234 (setq list
(cons (mh-get-msg-num t
) list
))))))
237 ;; If we created a new sequence, add the initial message to it too.
238 (if (not (member (mh-get-msg-num t
) list
))
239 (setq list
(cons (mh-get-msg-num t
) list
)))
240 (if (assoc 'subject mh-seq-list
) (mh-delete-seq 'subject
))
241 ;; sort the result into a sequence
242 (let ((sorted-list (sort (copy-sequence list
) 'mh-lessp
)))
244 (mh-add-msgs-to-seq (car sorted-list
) 'subject nil
)
245 (setq sorted-list
(cdr sorted-list
)))
250 (defun mh-edit-pick-expr (default)
251 "With prefix arg edit a pick expression.
252 If no prefix arg is given, then return DEFAULT."
253 (let ((default-string (loop for x in default concat
(format " %s" x
))))
254 (if (or current-prefix-arg
(equal default-string
""))
255 (mh-pick-args-list (read-string "Pick expression: "
259 (defun mh-pick-args-list (s)
260 "Form list by grouping elements in string S suitable for pick arguments.
261 For example, the string \"-subject a b c -from Joe User
262 <user@domain.com>\" is converted to (\"-subject\" \"a b c\"
263 \"-from\" \"Joe User <user@domain.com>\""
264 (let ((full-list (split-string s
))
265 current-arg collection arg-list
)
267 (setq current-arg
(car full-list
))
268 (if (null (string-match "^-" current-arg
))
270 (if (null collection
)
272 (format "%s %s" collection current-arg
)))
274 (setq arg-list
(append arg-list
(list collection
)))
275 (setq collection nil
))
276 (setq arg-list
(append arg-list
(list current-arg
))))
277 (setq full-list
(cdr full-list
)))
279 (setq arg-list
(append arg-list
(list collection
))))
282 (defun mh-current-message-header-field (header-field)
283 "Return a pick regexp to match HEADER-FIELD of the message at point."
284 (let ((num (mh-get-msg-num nil
)))
286 (let ((folder mh-current-folder
))
288 (insert-file-contents-literally (mh-msg-filename num folder
))
289 (goto-char (point-min))
290 (when (search-forward "\n\n" nil t
)
291 (narrow-to-region (point-min) (point)))
292 (let* ((field (or (message-fetch-field (format "%s" header-field
))
294 (field-option (format "-%s" header-field
))
295 (patterns (loop for x in
(split-string field
"[ ]*,[ ]*")
297 collect
(if (string-match "<\\(.*@.*\\)>" x
)
301 (loop with accum
= `(,field-option
,(car patterns
))
302 for e in
(cdr patterns
)
303 do
(setq accum
`(,field-option
,e
"-or" ,@accum
))
304 finally return accum
))))))))
306 (defun mh-narrow-to-header-field (header-field pick-expr
)
307 "Limit to messages whose HEADER-FIELD match PICK-EXPR.
308 The MH command pick is used to do the match."
309 (let ((folder mh-current-folder
)
310 (original (mh-coalesce-msg-list
311 (mh-range-to-msg-list (cons (point-min) (point-max)))))
314 (apply #'mh-exec-cmd-output
"pick" nil folder
315 (append original
(list "-list") pick-expr
))
316 (goto-char (point-min))
318 (let ((num (ignore-errors
320 (buffer-substring (point) (mh-line-end-position))))))
321 (when num
(push num msg-list
))
324 (message "No matches")
325 (when (assoc 'header mh-seq-list
) (mh-delete-seq 'header
))
326 (mh-add-msgs-to-seq msg-list
'header
)
327 (mh-narrow-to-seq 'header
))))
332 ;; indent-tabs-mode: nil
333 ;; sentence-end-double-space: nil
336 ;;; mh-limit.el ends here