rmailsort.el 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; rmailsort.el --- Rmail: sort messages
  2. ;; Copyright (C) 1990, 1993-1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
  4. ;; Maintainer: FSF
  5. ;; Keywords: mail
  6. ;; Package: rmail
  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. ;; Functions for sorting messages in an Rmail buffer.
  20. ;;; Code:
  21. (require 'rmail)
  22. ;;;###autoload
  23. (defun rmail-sort-by-date (reverse)
  24. "Sort messages of current Rmail buffer by \"Date\" header.
  25. If prefix argument REVERSE is non-nil, sorts in reverse order."
  26. (interactive "P")
  27. (rmail-sort-messages reverse
  28. (lambda (msg)
  29. (rmail-make-date-sortable
  30. (rmail-get-header "Date" msg)))))
  31. ;;;###autoload
  32. (defun rmail-sort-by-subject (reverse)
  33. "Sort messages of current Rmail buffer by \"Subject\" header.
  34. Ignores any \"Re: \" prefix. If prefix argument REVERSE is
  35. non-nil, sorts in reverse order."
  36. ;; Note this is a case-sensitive sort.
  37. (interactive "P")
  38. (rmail-sort-messages reverse
  39. (lambda (msg)
  40. (let ((key (or (rmail-get-header "Subject" msg) ""))
  41. (case-fold-search t))
  42. ;; Remove `Re:'
  43. (if (string-match "^\\(re:[ \t]*\\)*" key)
  44. (substring key (match-end 0))
  45. key)))))
  46. ;;;###autoload
  47. (defun rmail-sort-by-author (reverse)
  48. "Sort messages of current Rmail buffer by author.
  49. This uses either the \"From\" or \"Sender\" header, downcased.
  50. If prefix argument REVERSE is non-nil, sorts in reverse order."
  51. (interactive "P")
  52. (rmail-sort-messages reverse
  53. (lambda (msg)
  54. (downcase ; canonical name
  55. (mail-strip-quoted-names
  56. (or (rmail-get-header "From" msg)
  57. (rmail-get-header "Sender" msg) ""))))))
  58. ;;;###autoload
  59. (defun rmail-sort-by-recipient (reverse)
  60. "Sort messages of current Rmail buffer by recipient.
  61. This uses either the \"To\" or \"Apparently-To\" header, downcased.
  62. If prefix argument REVERSE is non-nil, sorts in reverse order."
  63. (interactive "P")
  64. (rmail-sort-messages reverse
  65. (lambda (msg)
  66. (downcase ; canonical name
  67. (mail-strip-quoted-names
  68. (or (rmail-get-header "To" msg)
  69. (rmail-get-header "Apparently-To" msg) ""))))))
  70. ;;;###autoload
  71. (defun rmail-sort-by-correspondent (reverse)
  72. "Sort messages of current Rmail buffer by other correspondent.
  73. This uses either the \"From\", \"Sender\", \"To\", or
  74. \"Apparently-To\" header, downcased. Uses the first header not
  75. excluded by `mail-dont-reply-to-names'. If prefix argument
  76. REVERSE is non-nil, sorts in reverse order."
  77. (interactive "P")
  78. (rmail-sort-messages reverse
  79. (lambda (msg)
  80. (downcase
  81. (rmail-select-correspondent
  82. msg
  83. '("From" "Sender" "To" "Apparently-To"))))))
  84. (defun rmail-select-correspondent (msg fields)
  85. "Find the first header not excluded by `mail-dont-reply-to-names'.
  86. MSG is a message number. FIELDS is a list of header names."
  87. (let ((ans ""))
  88. (while (and fields (string= ans ""))
  89. (setq ans
  90. (mail-dont-reply-to
  91. (mail-strip-quoted-names
  92. (or (rmail-get-header (car fields) msg) ""))))
  93. (setq fields (cdr fields)))
  94. ans))
  95. ;;;###autoload
  96. (defun rmail-sort-by-lines (reverse)
  97. "Sort messages of current Rmail buffer by the number of lines.
  98. If prefix argument REVERSE is non-nil, sorts in reverse order."
  99. (interactive "P")
  100. (rmail-sort-messages reverse
  101. (lambda (msg)
  102. (count-lines (rmail-msgbeg msg)
  103. (rmail-msgend msg)))))
  104. ;;;###autoload
  105. (defun rmail-sort-by-labels (reverse labels)
  106. "Sort messages of current Rmail buffer by labels.
  107. LABELS is a comma-separated list of labels. The order of these
  108. labels specifies the order of messages: messages with the first
  109. label come first, messages with the second label come second, and
  110. so on. Messages that have none of these labels come last.
  111. If prefix argument REVERSE is non-nil, sorts in reverse order."
  112. (interactive "P\nsSort by labels: ")
  113. (or (string-match "[^ \t]" labels) ; need some non-whitespace
  114. (error "No labels specified"))
  115. ;; Remove leading whitespace, add trailing comma.
  116. (setq labels (concat (substring labels (match-beginning 0)) ","))
  117. (let (labelvec nmax)
  118. ;; Convert "l1,..." into "\\(, \\|\\`\\)l1\\(,\\|\\'\\)" "..." ...
  119. (while (string-match "[ \t]*,[ \t]*" labels)
  120. (setq labelvec (cons
  121. (concat "\\(, \\|\\`\\)"
  122. (substring labels 0 (match-beginning 0))
  123. "\\(,\\|\\'\\)")
  124. labelvec))
  125. (setq labels (substring labels (match-end 0))))
  126. (setq labelvec (apply 'vector (nreverse labelvec))
  127. nmax (length labelvec))
  128. (rmail-sort-messages reverse
  129. ;; If no labels match, returns nmax; if they
  130. ;; match the first specified in LABELS,
  131. ;; returns 0; if they match the second, returns 1; etc.
  132. ;; Hence sorts as described in the doc-string.
  133. (lambda (msg)
  134. (let ((n 0)
  135. (str (concat (rmail-get-attr-names msg)
  136. ", "
  137. (rmail-get-keywords msg))))
  138. ;; No labels: can't match anything.
  139. (if (string-equal ", " str)
  140. nmax
  141. (while (and (< n nmax)
  142. (not (string-match (aref labelvec n)
  143. str)))
  144. (setq n (1+ n)))
  145. n))))))
  146. ;; Basic functions
  147. (declare-function rmail-update-summary "rmailsum" (&rest ignore))
  148. (defun rmail-sort-messages (reverse keyfun)
  149. "Sort messages of current Rmail buffer.
  150. If REVERSE is non-nil, sorts in reverse order. Calls the
  151. function KEYFUN with a message number (it should return a sort key).
  152. Numeric keys are sorted numerically, all others as strings."
  153. (with-current-buffer rmail-buffer
  154. (let ((return-to-point
  155. (if (rmail-buffers-swapped-p)
  156. (point)))
  157. (sort-lists nil))
  158. (rmail-swap-buffers-maybe)
  159. (message "Finding sort keys...")
  160. (widen)
  161. (let ((msgnum 1))
  162. (while (>= rmail-total-messages msgnum)
  163. (setq sort-lists
  164. (cons (list (funcall keyfun msgnum) ;Make sorting key
  165. (eq rmail-current-message msgnum) ;True if current
  166. (aref rmail-message-vector msgnum)
  167. (aref rmail-message-vector (1+ msgnum)))
  168. sort-lists))
  169. (if (zerop (% msgnum 10))
  170. (message "Finding sort keys...%d" msgnum))
  171. (setq msgnum (1+ msgnum))))
  172. (or reverse (setq sort-lists (nreverse sort-lists)))
  173. (setq sort-lists
  174. (sort sort-lists
  175. ;; Decide predicate: < or string-lessp
  176. (if (numberp (car (car sort-lists))) ;Is a key numeric?
  177. 'car-less-than-car
  178. (lambda (a b)
  179. (string-lessp (car a) (car b))))))
  180. (if reverse (setq sort-lists (nreverse sort-lists)))
  181. ;; Now we enter critical region. So, keyboard quit is disabled.
  182. (message "Reordering messages...")
  183. (let ((inhibit-quit t) ;Inhibit quit
  184. (inhibit-read-only t)
  185. (current-message nil)
  186. (msgnum 1)
  187. (msginfo nil)
  188. (undo (not (eq buffer-undo-list t))))
  189. ;; There's little hope that we can easily undo after that.
  190. (buffer-disable-undo (current-buffer))
  191. (goto-char (rmail-msgbeg 1))
  192. ;; To force update of all markers,
  193. ;; keep the new copies separated from the remaining old messages.
  194. (insert-before-markers ?Z)
  195. (backward-char 1)
  196. ;; Now reorder messages.
  197. (dolist (msginfo sort-lists)
  198. ;; Swap two messages.
  199. (insert-buffer-substring
  200. (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
  201. ;; The last message may not have \n\n after it.
  202. (rmail-ensure-blank-line)
  203. (delete-region (nth 2 msginfo) (nth 3 msginfo))
  204. ;; Is current message?
  205. (if (nth 1 msginfo)
  206. (setq current-message msgnum))
  207. (if (zerop (% msgnum 10))
  208. (message "Reordering messages...%d" msgnum))
  209. (setq msgnum (1+ msgnum)))
  210. ;; Delete the dummy separator Z inserted before.
  211. (delete-char 1)
  212. (setq quit-flag nil)
  213. ;; If undo was on before, re-enable it. But note that it is
  214. ;; disabled in mbox Rmail, so this is kind of pointless.
  215. (if undo (buffer-enable-undo))
  216. (rmail-set-message-counters)
  217. (rmail-show-message-1 current-message)
  218. (if return-to-point
  219. (goto-char return-to-point))
  220. (if (rmail-summary-exists)
  221. (rmail-select-summary (rmail-update-summary)))))))
  222. (autoload 'timezone-make-date-sortable "timezone")
  223. (defun rmail-make-date-sortable (date)
  224. "Make DATE sortable using the function `string-lessp'."
  225. ;; Assume the default time zone is GMT.
  226. (timezone-make-date-sortable date "GMT" "GMT"))
  227. (provide 'rmailsort)
  228. ;; Local Variables:
  229. ;; generated-autoload-file: "rmail.el"
  230. ;; End:
  231. ;;; rmailsort.el ends here