mh-limit.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. ;;; mh-limit.el --- MH-E display limits
  2. ;; Copyright (C) 2001-2003, 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Peter S. Galbraith <psg@debian.org>
  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. ;; "Poor man's threading" by psg.
  20. ;;; Change Log:
  21. ;;; Code:
  22. (require 'mh-e)
  23. (mh-require-cl)
  24. (require 'mh-scan)
  25. (autoload 'message-fetch-field "message")
  26. ;;; MH-Folder Commands
  27. ;; Alphabetical.
  28. ;;;###mh-autoload
  29. (defun mh-delete-subject ()
  30. "Delete messages with same subject\\<mh-folder-mode-map>.
  31. To delete messages faster, you can use this command to delete all
  32. the messages with the same subject as the current message. This
  33. command puts these messages in a sequence named \"subject\". You
  34. can undo this action by using \\[mh-undo] with a prefix argument
  35. and then specifying the \"subject\" sequence."
  36. (interactive)
  37. (let ((count (mh-subject-to-sequence nil)))
  38. (cond
  39. ((not count) ; No subject line, delete msg anyway
  40. (mh-delete-msg (mh-get-msg-num t)))
  41. ((= 0 count) ; No other msgs, delete msg anyway.
  42. (message "No other messages with same Subject following this one")
  43. (mh-delete-msg (mh-get-msg-num t)))
  44. (t ; We have a subject sequence.
  45. (message "Marked %d messages for deletion" count)
  46. (mh-delete-msg 'subject)))))
  47. ;;;###mh-autoload
  48. (defun mh-delete-subject-or-thread ()
  49. "Delete messages with same subject or thread\\<mh-folder-mode-map>.
  50. To delete messages faster, you can use this command to delete all
  51. the messages with the same subject as the current message. This
  52. command puts these messages in a sequence named \"subject\". You
  53. can undo this action by using \\[mh-undo] with a prefix argument
  54. and then specifying the \"subject\" sequence.
  55. However, if the buffer is displaying a threaded view of the
  56. folder then this command behaves like \\[mh-thread-delete]."
  57. (interactive)
  58. (if (memq 'unthread mh-view-ops)
  59. (mh-thread-delete)
  60. (mh-delete-subject)))
  61. ;;;###mh-autoload
  62. (defun mh-narrow-to-cc (&optional pick-expr)
  63. "Limit to messages with the same \"Cc:\" field.
  64. With a prefix argument, edit PICK-EXPR.
  65. Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
  66. (interactive
  67. (list (mh-edit-pick-expr
  68. (mh-quote-pick-expr (mh-current-message-header-field 'cc)))))
  69. (mh-narrow-to-header-field 'cc pick-expr))
  70. ;;;###mh-autoload
  71. (defun mh-narrow-to-from (&optional pick-expr)
  72. "Limit to messages with the same \"From:\" field.
  73. With a prefix argument, edit PICK-EXPR.
  74. Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
  75. (interactive
  76. (list (mh-edit-pick-expr
  77. (mh-quote-pick-expr (mh-current-message-header-field 'from)))))
  78. (mh-narrow-to-header-field 'from pick-expr))
  79. ;;;###mh-autoload
  80. (defun mh-narrow-to-range (range)
  81. "Limit to RANGE.
  82. Check the documentation of `mh-interactive-range' to see how
  83. RANGE is read in interactive use.
  84. Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
  85. (interactive (list (mh-interactive-range "Narrow to")))
  86. (when (assoc 'range mh-seq-list) (mh-delete-seq 'range))
  87. (mh-add-msgs-to-seq (mh-range-to-msg-list range) 'range)
  88. (mh-narrow-to-seq 'range))
  89. ;;;###mh-autoload
  90. (defun mh-narrow-to-subject (&optional pick-expr)
  91. "Limit to messages with same subject.
  92. With a prefix argument, edit PICK-EXPR.
  93. The string Re: is removed from the search.
  94. Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
  95. (interactive
  96. (list (mh-edit-pick-expr
  97. (mh-quote-pick-expr (mh-current-message-header-field 'subject)))))
  98. (setq pick-expr
  99. (let ((case-fold-search t))
  100. (loop for s in pick-expr
  101. collect (mh-replace-regexp-in-string "re: *" "" s))))
  102. (mh-narrow-to-header-field 'subject pick-expr))
  103. ;;;###mh-autoload
  104. (defun mh-narrow-to-to (&optional pick-expr)
  105. "Limit to messages with the same \"To:\" field.
  106. With a prefix argument, edit PICK-EXPR.
  107. Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
  108. (interactive
  109. (list (mh-edit-pick-expr
  110. (mh-quote-pick-expr (mh-current-message-header-field 'to)))))
  111. (mh-narrow-to-header-field 'to pick-expr))
  112. ;;; Support Routines
  113. (defun mh-subject-to-sequence (all)
  114. "Put all following messages with same subject in sequence 'subject.
  115. If arg ALL is t, move to beginning of folder buffer to collect all
  116. messages.
  117. If arg ALL is nil, collect only messages fron current one on forward.
  118. Return number of messages put in the sequence:
  119. nil -> there was no subject line.
  120. 0 -> there were no later messages with the same
  121. subject (sequence not made)
  122. >1 -> the total number of messages including current one."
  123. (if (memq 'unthread mh-view-ops)
  124. (mh-subject-to-sequence-threaded all)
  125. (mh-subject-to-sequence-unthreaded all)))
  126. (defun mh-subject-to-sequence-threaded (all)
  127. "Put all messages with the same subject in the 'subject sequence.
  128. This function works when the folder is threaded. In this
  129. situation the subject could get truncated and so the normal
  130. matching doesn't work.
  131. The parameter ALL is non-nil then all the messages in the buffer
  132. are considered, otherwise only the messages after the current one
  133. are taken into account."
  134. (let* ((cur (mh-get-msg-num nil))
  135. (subject (mh-thread-find-msg-subject cur))
  136. region msgs)
  137. (if (null subject)
  138. (and (message "No subject line") nil)
  139. (setq region (cons (if all (point-min) (point)) (point-max)))
  140. (mh-iterate-on-range msg region
  141. (when (eq (mh-thread-find-msg-subject msg) subject)
  142. (push msg msgs)))
  143. (setq msgs (sort msgs #'mh-lessp))
  144. (if (null msgs)
  145. 0
  146. (when (assoc 'subject mh-seq-list)
  147. (mh-delete-seq 'subject))
  148. (mh-add-msgs-to-seq msgs 'subject)
  149. (length msgs)))))
  150. (defvar mh-limit-max-subject-size 41
  151. "Maximum size of the subject part.
  152. It would be desirable to avoid hard-coding this.")
  153. (defun mh-subject-to-sequence-unthreaded (all)
  154. "Put all following messages with same subject in sequence 'subject.
  155. This function only works with an unthreaded folder. If arg ALL is
  156. t, move to beginning of folder buffer to collect all messages. If
  157. arg ALL is nil, collect only messages fron current one on
  158. forward.
  159. Return number of messages put in the sequence:
  160. nil -> there was no subject line.
  161. 0 -> there were no later messages with the same
  162. subject (sequence not made)
  163. >1 -> the total number of messages including current one."
  164. (if (not (eq major-mode 'mh-folder-mode))
  165. (error "Not in a folder buffer"))
  166. (save-excursion
  167. (beginning-of-line)
  168. (if (or (not (looking-at mh-scan-subject-regexp))
  169. (not (match-string 3))
  170. (string-equal "" (match-string 3)))
  171. (progn (message "No subject line")
  172. nil)
  173. (let ((subject (mh-match-string-no-properties 3))
  174. (list))
  175. (if (> (length subject) mh-limit-max-subject-size)
  176. (setq subject (substring subject 0 mh-limit-max-subject-size)))
  177. (save-excursion
  178. (if all
  179. (goto-char (point-min)))
  180. (while (re-search-forward mh-scan-subject-regexp nil t)
  181. (let ((this-subject (mh-match-string-no-properties 3)))
  182. (if (> (length this-subject) mh-limit-max-subject-size)
  183. (setq this-subject (substring this-subject
  184. 0 mh-limit-max-subject-size)))
  185. (if (string-equal this-subject subject)
  186. (setq list (cons (mh-get-msg-num t) list))))))
  187. (cond
  188. (list
  189. ;; If we created a new sequence, add the initial message to it too.
  190. (if (not (member (mh-get-msg-num t) list))
  191. (setq list (cons (mh-get-msg-num t) list)))
  192. (if (assoc 'subject mh-seq-list) (mh-delete-seq 'subject))
  193. ;; sort the result into a sequence
  194. (let ((sorted-list (sort (copy-sequence list) 'mh-lessp)))
  195. (while sorted-list
  196. (mh-add-msgs-to-seq (car sorted-list) 'subject nil)
  197. (setq sorted-list (cdr sorted-list)))
  198. (safe-length list)))
  199. (t
  200. 0))))))
  201. (defun mh-edit-pick-expr (default)
  202. "With prefix arg edit a pick expression.
  203. If no prefix arg is given, then return DEFAULT."
  204. (let ((default-string (loop for x in default concat (format " %s" x))))
  205. (if (or current-prefix-arg (equal default-string ""))
  206. (mh-pick-args-list (read-string "Pick expression: "
  207. default-string))
  208. default)))
  209. (defun mh-pick-args-list (s)
  210. "Form list by grouping elements in string S suitable for pick arguments.
  211. For example, the string \"-subject a b c -from Joe User
  212. <user@domain.com>\" is converted to (\"-subject\" \"a b c\"
  213. \"-from\" \"Joe User <user@domain.com>\""
  214. (let ((full-list (split-string s))
  215. current-arg collection arg-list)
  216. (while full-list
  217. (setq current-arg (car full-list))
  218. (if (null (string-match "^-" current-arg))
  219. (setq collection
  220. (if (null collection)
  221. current-arg
  222. (format "%s %s" collection current-arg)))
  223. (when collection
  224. (setq arg-list (append arg-list (list collection)))
  225. (setq collection nil))
  226. (setq arg-list (append arg-list (list current-arg))))
  227. (setq full-list (cdr full-list)))
  228. (when collection
  229. (setq arg-list (append arg-list (list collection))))
  230. arg-list))
  231. (defun mh-current-message-header-field (header-field)
  232. "Return a pick regexp to match HEADER-FIELD of the message at point."
  233. (let ((num (mh-get-msg-num nil)))
  234. (when num
  235. (let ((folder mh-current-folder))
  236. (with-temp-buffer
  237. (insert-file-contents-literally (mh-msg-filename num folder))
  238. (goto-char (point-min))
  239. (when (search-forward "\n\n" nil t)
  240. (narrow-to-region (point-min) (point)))
  241. (let* ((field (or (message-fetch-field (format "%s" header-field))
  242. ""))
  243. (field-option (format "-%s" header-field))
  244. (patterns (loop for x in (split-string field "[ ]*,[ ]*")
  245. unless (equal x "")
  246. collect (if (string-match "<\\(.*@.*\\)>" x)
  247. (match-string 1 x)
  248. x))))
  249. (when patterns
  250. (loop with accum = `(,field-option ,(car patterns))
  251. for e in (cdr patterns)
  252. do (setq accum `(,field-option ,e "-or" ,@accum))
  253. finally return accum))))))))
  254. (defun mh-narrow-to-header-field (header-field pick-expr)
  255. "Limit to messages whose HEADER-FIELD match PICK-EXPR.
  256. The MH command pick is used to do the match."
  257. (let ((folder mh-current-folder)
  258. (original (mh-coalesce-msg-list
  259. (mh-range-to-msg-list (cons (point-min) (point-max)))))
  260. (msg-list ()))
  261. (with-temp-buffer
  262. (apply #'mh-exec-cmd-output "pick" nil folder
  263. (append original (list "-list") pick-expr))
  264. (goto-char (point-min))
  265. (while (not (eobp))
  266. (let ((num (ignore-errors
  267. (string-to-number
  268. (buffer-substring (point) (mh-line-end-position))))))
  269. (when num (push num msg-list))
  270. (forward-line))))
  271. (if (null msg-list)
  272. (message "No matches")
  273. (when (assoc 'header mh-seq-list) (mh-delete-seq 'header))
  274. (mh-add-msgs-to-seq msg-list 'header)
  275. (mh-narrow-to-seq 'header))))
  276. (provide 'mh-limit)
  277. ;; Local Variables:
  278. ;; indent-tabs-mode: nil
  279. ;; sentence-end-double-space: nil
  280. ;; End:
  281. ;;; mh-limit.el ends here