rmailkwd.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; rmailkwd.el --- part of the "RMAIL" mail reader for Emacs
  2. ;; Copyright (C) 1985, 1988, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: mail
  5. ;; Package: rmail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'rmail)
  20. ;; Global to all RMAIL buffers. It exists for the sake of completion.
  21. ;; It is better to use strings with the label functions and let them
  22. ;; worry about making the label.
  23. (defvar rmail-label-obarray (make-vector 47 0)
  24. "Obarray of labels used by Rmail.
  25. `rmail-read-label' uses this to offer completion.")
  26. ;; Initialize with the standard labels.
  27. (mapc (lambda (s) (intern (cadr s) rmail-label-obarray))
  28. rmail-attr-array)
  29. (defun rmail-make-label (s)
  30. "Intern string S as a downcased symbol in `rmail-label-obarray'."
  31. (intern (downcase s) rmail-label-obarray))
  32. ;;;###autoload
  33. (defun rmail-add-label (label)
  34. "Add LABEL to labels associated with current RMAIL message.
  35. Completes (see `rmail-read-label') over known labels when reading.
  36. LABEL may be a symbol or string. Only one label is allowed."
  37. (interactive (list (rmail-read-label "Add label")))
  38. (rmail-set-label label t))
  39. ;;;###autoload
  40. (defun rmail-kill-label (label)
  41. "Remove LABEL from labels associated with current RMAIL message.
  42. Completes (see `rmail-read-label') over known labels when reading.
  43. LABEL may be a symbol or string. Only one label is allowed."
  44. (interactive (list (rmail-read-label "Remove label")))
  45. (rmail-set-label label nil))
  46. ;;;###autoload
  47. (defun rmail-read-label (prompt)
  48. "Read a label with completion, prompting with PROMPT.
  49. Completions are chosen from `rmail-label-obarray'. The default
  50. is `rmail-last-label', if that is non-nil. Updates `rmail-last-label'
  51. according to the choice made, and returns a symbol."
  52. (let* ((old nil)
  53. (result
  54. (progn
  55. ;; If the summary exists, we've already read all the
  56. ;; existing labels. If not, read the ones in this message.
  57. (or (eq major-mode 'rmail-summary-mode)
  58. (rmail-summary-exists)
  59. (and (setq old (rmail-get-keywords))
  60. (mapc 'rmail-make-label (split-string old ", "))))
  61. (completing-read (concat prompt
  62. (if rmail-last-label
  63. (concat " (default "
  64. (symbol-name rmail-last-label)
  65. "): ")
  66. ": "))
  67. rmail-label-obarray
  68. nil
  69. nil))))
  70. (if (string= result "")
  71. rmail-last-label
  72. (setq rmail-last-label (rmail-make-label result)))))
  73. (declare-function rmail-summary-update-line "rmailsum" (n))
  74. (defun rmail-set-label (label state &optional msg)
  75. "Set LABEL as present or absent according to STATE in message MSG.
  76. LABEL may be a symbol or string."
  77. (or (stringp label) (setq label (symbol-name label)))
  78. (if (string-match "," label)
  79. (error "More than one label specified"))
  80. (with-current-buffer rmail-buffer
  81. (rmail-maybe-set-message-counters)
  82. (if (zerop (or msg (setq msg rmail-current-message)))
  83. (error "No message"))
  84. ;; Force recalculation of summary for this message.
  85. (aset rmail-summary-vector (1- msg) nil)
  86. (let (attr-index)
  87. ;; Is this label an attribute?
  88. (dotimes (i (length rmail-attr-array))
  89. (if (string= (cadr (aref rmail-attr-array i)) label)
  90. (setq attr-index i)))
  91. (if attr-index
  92. ;; If so, set it as an attribute.
  93. (rmail-set-attribute attr-index state msg)
  94. ;; Is this keyword already present in msg's keyword list?
  95. (let* ((header (rmail-get-keywords msg))
  96. (regexp (concat ", " (regexp-quote label) ","))
  97. (present (not (null
  98. (string-match regexp (concat ", " header ","))))))
  99. ;; If current state is not correct,
  100. (unless (eq present state)
  101. ;; either add it or delete it.
  102. (rmail-set-header
  103. rmail-keyword-header msg
  104. (if state
  105. ;; Add this keyword at the end.
  106. (if (and header (not (string= header "")))
  107. (concat header ", " label)
  108. label)
  109. ;; Delete this keyword.
  110. (let ((before (substring header 0
  111. (max 0 (- (match-beginning 0) 2))))
  112. (after (substring header
  113. (min (length header)
  114. (- (match-end 0) 1)))))
  115. (cond ((string= before "")
  116. ;; If before and after both empty, delete the header.
  117. (unless (string= after "")
  118. after))
  119. ((string= after "")
  120. before)
  121. (t (concat before ", " after))))))))))
  122. (if (rmail-summary-exists)
  123. (rmail-select-summary (rmail-summary-update-line msg)))
  124. (if (= msg rmail-current-message)
  125. (rmail-display-labels))))
  126. ;; Motion on messages with keywords.
  127. ;;;###autoload
  128. (defun rmail-previous-labeled-message (n labels)
  129. "Show previous message with one of the labels LABELS.
  130. LABELS should be a comma-separated list of label names.
  131. If LABELS is empty, the last set of labels specified is used.
  132. With prefix argument N moves backward N messages with these labels."
  133. (interactive "p\nsMove to previous msg with labels: ")
  134. (rmail-next-labeled-message (- n) labels))
  135. (declare-function mail-comma-list-regexp "mail-utils" (labels))
  136. ;;;###autoload
  137. (defun rmail-next-labeled-message (n labels)
  138. "Show next message with one of the labels LABELS.
  139. LABELS should be a comma-separated list of label names.
  140. If LABELS is empty, the last set of labels specified is used.
  141. With prefix argument N moves forward N messages with these labels."
  142. ;; FIXME show the default in the prompt.
  143. (interactive "p\nsMove to next msg with labels: ")
  144. (if (string= labels "")
  145. (setq labels rmail-last-multi-labels))
  146. (or labels
  147. (error "No labels to find have been specified previously"))
  148. (set-buffer rmail-buffer)
  149. (setq rmail-last-multi-labels labels)
  150. (rmail-maybe-set-message-counters)
  151. (let ((lastwin rmail-current-message)
  152. (current rmail-current-message)
  153. (regexp (concat " \\("
  154. (mail-comma-list-regexp labels)
  155. "\\)\\(,\\|\\'\\)")))
  156. (while (and (> n 0) (< current rmail-total-messages))
  157. (setq current (1+ current))
  158. (if (string-match regexp (rmail-get-labels current))
  159. (setq lastwin current n (1- n))))
  160. (while (and (< n 0) (> current 1))
  161. (setq current (1- current))
  162. (if (string-match regexp (rmail-get-labels current))
  163. (setq lastwin current n (1+ n))))
  164. (if (< n 0)
  165. (error "No previous message with labels %s" labels)
  166. (if (> n 0)
  167. (error "No following message with labels %s" labels)
  168. (rmail-show-message-1 lastwin)))))
  169. (provide 'rmailkwd)
  170. ;; Local Variables:
  171. ;; generated-autoload-file: "rmail.el"
  172. ;; End:
  173. ;;; rmailkwd.el ends here