gnus-diary.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. ;;; gnus-diary.el --- Wrapper around the NNDiary Gnus back end
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Didier Verna <didier@xemacs.org>
  4. ;; Maintainer: Didier Verna <didier@xemacs.org>
  5. ;; Created: Tue Jul 20 10:42:55 1999
  6. ;; Keywords: calendar mail news
  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. ;; Contents management by FCM version 0.1.
  20. ;; Description:
  21. ;; ===========
  22. ;; gnus-diary is a utility toolkit used on top of the nndiary back end. It is
  23. ;; now fully documented in the Gnus manual.
  24. ;; Bugs / Todo:
  25. ;; ===========
  26. ;;; Code:
  27. (require 'nndiary)
  28. (require 'message)
  29. (require 'gnus-art)
  30. (defgroup gnus-diary nil
  31. "Utilities on top of the nndiary back end for Gnus."
  32. :version "22.1"
  33. :group 'gnus)
  34. (defcustom gnus-diary-summary-line-format "%U%R%z %uD: %(%s%) (%ud)\n"
  35. "*Summary line format for nndiary groups."
  36. :type 'string
  37. :group 'gnus-diary
  38. :group 'gnus-summary-format)
  39. (defcustom gnus-diary-time-format "%a, %b %e %y, %H:%M"
  40. "*Time format to display appointments in nndiary summary buffers.
  41. Please refer to `format-time-string' for information on possible values."
  42. :type 'string
  43. :group 'gnus-diary)
  44. (defcustom gnus-diary-delay-format-function 'gnus-diary-delay-format-english
  45. "*Function called to format a diary delay string.
  46. It is passed two arguments. The first one is non-nil if the delay is in
  47. the past. The second one is of the form ((NUM . UNIT) ...) where NUM is
  48. an integer and UNIT is one of 'year 'month 'week 'day 'hour or 'minute.
  49. It should return strings like \"In 2 months, 3 weeks\", \"3 hours,
  50. 1 minute ago\" and so on.
  51. There are currently two built-in format functions:
  52. `gnus-diary-delay-format-english' (the default)
  53. `gnus-diary-delay-format-french'"
  54. :type '(choice (const :tag "english" gnus-diary-delay-format-english)
  55. (const :tag "french" gnus-diary-delay-format-french)
  56. (symbol :tag "other"))
  57. :group 'gnus-diary)
  58. (defconst gnus-diary-version nndiary-version
  59. "Current Diary back end version.")
  60. ;; Compatibility functions ==================================================
  61. (eval-and-compile
  62. (if (fboundp 'kill-entire-line)
  63. (defalias 'gnus-diary-kill-entire-line 'kill-entire-line)
  64. (defun gnus-diary-kill-entire-line ()
  65. (beginning-of-line)
  66. (let ((kill-whole-line t))
  67. (kill-line)))))
  68. ;; Summary line format ======================================================
  69. (defun gnus-diary-delay-format-french (past delay)
  70. (if (null delay)
  71. "maintenant!"
  72. ;; Keep only a precision of two degrees
  73. (and (> (length delay) 1) (setcdr (cdr delay) nil))
  74. (concat (if past "il y a " "dans ")
  75. (let ((str "")
  76. del)
  77. (while (setq del (pop delay))
  78. (setq str (concat str
  79. (int-to-string (car del)) " "
  80. (cond ((eq (cdr del) 'year)
  81. "an")
  82. ((eq (cdr del) 'month)
  83. "mois")
  84. ((eq (cdr del) 'week)
  85. "semaine")
  86. ((eq (cdr del) 'day)
  87. "jour")
  88. ((eq (cdr del) 'hour)
  89. "heure")
  90. ((eq (cdr del) 'minute)
  91. "minute"))
  92. (unless (or (eq (cdr del) 'month)
  93. (= (car del) 1))
  94. "s")
  95. (if delay ", "))))
  96. str))))
  97. (defun gnus-diary-delay-format-english (past delay)
  98. (if (null delay)
  99. "now!"
  100. ;; Keep only a precision of two degrees
  101. (and (> (length delay) 1) (setcdr (cdr delay) nil))
  102. (concat (unless past "in ")
  103. (let ((str "")
  104. del)
  105. (while (setq del (pop delay))
  106. (setq str (concat str
  107. (int-to-string (car del)) " "
  108. (symbol-name (cdr del))
  109. (and (> (car del) 1) "s")
  110. (if delay ", "))))
  111. str)
  112. (and past " ago"))))
  113. (defun gnus-diary-header-schedule (headers)
  114. ;; Same as `nndiary-schedule', but given a set of headers HEADERS
  115. (mapcar
  116. (lambda (elt)
  117. (let ((head (cdr (assoc (intern (format "X-Diary-%s" (car elt)))
  118. headers))))
  119. (when head
  120. (nndiary-parse-schedule-value head (cadr elt) (car (cddr elt))))))
  121. nndiary-headers))
  122. ;; #### NOTE: Gnus sometimes gives me a HEADER not corresponding to any
  123. ;; message, with all fields set to nil here. I don't know what it is for, and
  124. ;; I just ignore it.
  125. ;;;###autoload
  126. (defun gnus-user-format-function-d (header)
  127. ;; Return an approximate delay string for the next occurrence of this
  128. ;; message. The delay is given only in the first non zero unit.
  129. ;; Code partly stolen from article-make-date-line
  130. (let* ((extras (mail-header-extra header))
  131. (sched (gnus-diary-header-schedule extras))
  132. (occur (nndiary-next-occurence sched (current-time)))
  133. (now (current-time))
  134. (real-time (subtract-time occur now)))
  135. (if (null real-time)
  136. "?????"
  137. (let* ((sec (+ (* (float (car real-time)) 65536) (cadr real-time)))
  138. (past (< sec 0))
  139. delay)
  140. (and past (setq sec (- sec)))
  141. (unless (zerop sec)
  142. ;; This is a bit convoluted, but basically we go through the time
  143. ;; units for years, weeks, etc, and divide things to see whether
  144. ;; that results in positive answers.
  145. (let ((units `((year . ,(* 365.25 24 3600))
  146. (month . ,(* 31 24 3600))
  147. (week . ,(* 7 24 3600))
  148. (day . ,(* 24 3600))
  149. (hour . 3600)
  150. (minute . 60)))
  151. unit num)
  152. (while (setq unit (pop units))
  153. (unless (zerop (setq num (ffloor (/ sec (cdr unit)))))
  154. (setq delay (append delay `((,(floor num) . ,(car unit))))))
  155. (setq sec (- sec (* num (cdr unit)))))))
  156. (funcall gnus-diary-delay-format-function past delay)))
  157. ))
  158. ;; #### NOTE: Gnus sometimes gives me a HEADER not corresponding to any
  159. ;; message, with all fields set to nil here. I don't know what it is for, and
  160. ;; I just ignore it.
  161. ;;;###autoload
  162. (defun gnus-user-format-function-D (header)
  163. ;; Returns a formatted time string for the next occurrence of this message.
  164. (let* ((extras (mail-header-extra header))
  165. (sched (gnus-diary-header-schedule extras))
  166. (occur (nndiary-next-occurence sched (current-time))))
  167. (format-time-string gnus-diary-time-format occur)))
  168. ;; Article sorting functions ================================================
  169. (defun gnus-article-sort-by-schedule (h1 h2)
  170. (let* ((now (current-time))
  171. (e1 (mail-header-extra h1))
  172. (e2 (mail-header-extra h2))
  173. (s1 (gnus-diary-header-schedule e1))
  174. (s2 (gnus-diary-header-schedule e2))
  175. (o1 (nndiary-next-occurence s1 now))
  176. (o2 (nndiary-next-occurence s2 now)))
  177. (if (and (= (car o1) (car o2)) (= (cadr o1) (cadr o2)))
  178. (< (mail-header-number h1) (mail-header-number h2))
  179. (time-less-p o1 o2))))
  180. (defun gnus-thread-sort-by-schedule (h1 h2)
  181. (gnus-article-sort-by-schedule (gnus-thread-header h1)
  182. (gnus-thread-header h2)))
  183. (defun gnus-summary-sort-by-schedule (&optional reverse)
  184. "Sort nndiary summary buffers by schedule of appointments.
  185. Optional prefix (or REVERSE argument) means sort in reverse order."
  186. (interactive "P")
  187. (gnus-summary-sort 'schedule reverse))
  188. (defvar gnus-summary-misc-menu) ;; Avoid byte compiler warning.
  189. (add-hook 'gnus-summary-menu-hook
  190. (lambda ()
  191. (easy-menu-add-item gnus-summary-misc-menu
  192. '("Sort")
  193. ["Sort by schedule"
  194. gnus-summary-sort-by-schedule
  195. (eq (car (gnus-find-method-for-group
  196. gnus-newsgroup-name))
  197. 'nndiary)]
  198. "Sort by number")))
  199. ;; Group parameters autosetting =============================================
  200. (defun gnus-diary-update-group-parameters (group)
  201. ;; Ensure that nndiary groups have convenient group parameters:
  202. ;; - a posting style containing X-Diary headers
  203. ;; - a nice summary line format
  204. ;; - NNDiary specific sorting by schedule functions
  205. ;; In general, try not to mess with what the user might have modified.
  206. ;; Posting style:
  207. (let ((posting-style (gnus-group-get-parameter group 'posting-style t))
  208. (headers nndiary-headers)
  209. header)
  210. (while headers
  211. (setq header (format "X-Diary-%s" (caar headers))
  212. headers (cdr headers))
  213. (unless (assoc header posting-style)
  214. (setq posting-style (append posting-style (list (list header "*"))))))
  215. (gnus-group-set-parameter group 'posting-style posting-style))
  216. ;; Summary line format:
  217. (unless (gnus-group-get-parameter group 'gnus-summary-line-format t)
  218. (gnus-group-set-parameter group 'gnus-summary-line-format
  219. `(,gnus-diary-summary-line-format)))
  220. ;; Sorting by schedule:
  221. (unless (gnus-group-get-parameter group 'gnus-article-sort-functions)
  222. (gnus-group-set-parameter group 'gnus-article-sort-functions
  223. '((append gnus-article-sort-functions
  224. (list
  225. 'gnus-article-sort-by-schedule)))))
  226. (unless (gnus-group-get-parameter group 'gnus-thread-sort-functions)
  227. (gnus-group-set-parameter group 'gnus-thread-sort-functions
  228. '((append gnus-thread-sort-functions
  229. (list
  230. 'gnus-thread-sort-by-schedule))))))
  231. ;; Called when a group is subscribed. This is needed because groups created
  232. ;; because of mail splitting are *not* created with the back end function.
  233. ;; Thus, `nndiary-request-create-group-hooks' is inoperative.
  234. (defun gnus-diary-maybe-update-group-parameters (group)
  235. (when (eq (car (gnus-find-method-for-group group)) 'nndiary)
  236. (gnus-diary-update-group-parameters group)))
  237. (add-hook 'nndiary-request-create-group-hooks
  238. 'gnus-diary-update-group-parameters)
  239. ;; Now that we have `gnus-subscribe-newsgroup-hooks', this is not needed
  240. ;; anymore. Maybe I should remove this completely.
  241. (add-hook 'nndiary-request-update-info-hooks
  242. 'gnus-diary-update-group-parameters)
  243. (add-hook 'gnus-subscribe-newsgroup-hooks
  244. 'gnus-diary-maybe-update-group-parameters)
  245. ;; Diary Message Checking ===================================================
  246. (defvar gnus-diary-header-value-history nil
  247. ;; History variable for header value prompting
  248. )
  249. (defun gnus-diary-narrow-to-headers ()
  250. "Narrow the current buffer to the header part.
  251. Point is left at the beginning of the region.
  252. The buffer is assumed to contain a message, but the format is unknown."
  253. (cond ((eq major-mode 'message-mode)
  254. (message-narrow-to-headers))
  255. (t
  256. (goto-char (point-min))
  257. (when (search-forward "\n\n" nil t)
  258. (narrow-to-region (point-min) (- (point) 1))
  259. (goto-char (point-min))))
  260. ))
  261. (defun gnus-diary-add-header (str)
  262. "Add a header to the current buffer.
  263. The buffer is assumed to contain a message, but the format is unknown."
  264. (cond ((eq major-mode 'message-mode)
  265. (message-add-header str))
  266. (t
  267. (save-restriction
  268. (gnus-diary-narrow-to-headers)
  269. (goto-char (point-max))
  270. (if (string-match "\n$" str)
  271. (insert str)
  272. (insert str ?\n))))
  273. ))
  274. (defun gnus-diary-check-message (arg)
  275. "Ensure that the current message is a valid for NNDiary.
  276. This function checks that all NNDiary required headers are present and
  277. valid, and prompts for values / correction otherwise.
  278. If ARG (or prefix) is non-nil, force prompting for all fields."
  279. (interactive "P")
  280. (save-excursion
  281. (mapcar
  282. (lambda (head)
  283. (let ((header (concat "X-Diary-" (car head)))
  284. (ask arg)
  285. value invalid)
  286. ;; First, try to find the header, and checks for validity:
  287. (save-restriction
  288. (gnus-diary-narrow-to-headers)
  289. (when (re-search-forward (concat "^" header ":") nil t)
  290. (unless (eq (char-after) ? )
  291. (insert " "))
  292. (setq value (buffer-substring (point) (point-at-eol)))
  293. (and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" value)
  294. (setq value (match-string 1 value)))
  295. (condition-case ()
  296. (nndiary-parse-schedule-value value
  297. (nth 1 head) (nth 2 head))
  298. (error
  299. (setq invalid t)))
  300. ;; #### NOTE: this (along with the `gnus-diary-add-header'
  301. ;; function) could be rewritten in a better way, in particular
  302. ;; not to blindly remove an already present header and reinsert
  303. ;; it somewhere else afterwards.
  304. (when (or ask invalid)
  305. (gnus-diary-kill-entire-line))
  306. ))
  307. ;; Now, loop until a valid value is provided:
  308. (while (or ask (not value) invalid)
  309. (let ((prompt (concat (and invalid
  310. (prog1 "(current value invalid) "
  311. (beep)))
  312. header ": ")))
  313. (setq value
  314. (if (listp (nth 1 head))
  315. (gnus-completing-read prompt (cons "*" (mapcar 'car (nth 1 head)))
  316. t value
  317. 'gnus-diary-header-value-history)
  318. (read-string prompt value
  319. 'gnus-diary-header-value-history))))
  320. (setq ask nil)
  321. (setq invalid nil)
  322. (condition-case ()
  323. (nndiary-parse-schedule-value value
  324. (nth 1 head) (nth 2 head))
  325. (error
  326. (setq invalid t))))
  327. (gnus-diary-add-header (concat header ": " value))
  328. ))
  329. nndiary-headers)
  330. ))
  331. (add-hook 'nndiary-request-accept-article-hooks
  332. (lambda () (gnus-diary-check-message nil)))
  333. (define-key message-mode-map "\C-c\C-fd" 'gnus-diary-check-message)
  334. (define-key gnus-article-edit-mode-map "\C-c\C-fd" 'gnus-diary-check-message)
  335. ;; The end ==================================================================
  336. (defun gnus-diary-version ()
  337. "Current Diary back end version."
  338. (interactive)
  339. (message "NNDiary version %s" nndiary-version))
  340. (provide 'gnus-diary)
  341. ;;; gnus-diary.el ends here