| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
(require 'nndiary) |
|---|
| 47 |
(require 'message) |
|---|
| 48 |
(require 'gnus-art) |
|---|
| 49 |
|
|---|
| 50 |
(defgroup gnus-diary nil |
|---|
| 51 |
"Utilities on top of the nndiary back end for Gnus." |
|---|
| 52 |
:version "22.1" |
|---|
| 53 |
:group 'gnus) |
|---|
| 54 |
|
|---|
| 55 |
(defcustom gnus-diary-summary-line-format "%U%R%z %uD: %(%s%) (%ud)\n" |
|---|
| 56 |
"*Summary line format for nndiary groups." |
|---|
| 57 |
:type 'string |
|---|
| 58 |
:group 'gnus-diary |
|---|
| 59 |
:group 'gnus-summary-format) |
|---|
| 60 |
|
|---|
| 61 |
(defcustom gnus-diary-time-format "%a, %b %e %y, %H:%M" |
|---|
| 62 |
"*Time format to display appointments in nndiary summary buffers. |
|---|
| 63 |
Please refer to `format-time-string' for information on possible values." |
|---|
| 64 |
:type 'string |
|---|
| 65 |
:group 'gnus-diary) |
|---|
| 66 |
|
|---|
| 67 |
(defcustom gnus-diary-delay-format-function 'gnus-diary-delay-format-english |
|---|
| 68 |
"*Function called to format a diary delay string. |
|---|
| 69 |
It is passed two arguments. The first one is non-nil if the delay is in |
|---|
| 70 |
the past. The second one is of the form ((NUM . UNIT) ...) where NUM is |
|---|
| 71 |
an integer and UNIT is one of 'year 'month 'week 'day 'hour or 'minute. |
|---|
| 72 |
It should return strings like \"In 2 months, 3 weeks\", \"3 hours, |
|---|
| 73 |
1 minute ago\" and so on. |
|---|
| 74 |
|
|---|
| 75 |
There are currently two built-in format functions: |
|---|
| 76 |
`gnus-diary-delay-format-english' (the default) |
|---|
| 77 |
`gnus-diary-delay-format-french'" |
|---|
| 78 |
:type '(choice (const :tag "english" gnus-diary-delay-format-english) |
|---|
| 79 |
(const :tag "french" gnus-diary-delay-format-french) |
|---|
| 80 |
(symbol :tag "other")) |
|---|
| 81 |
:group 'gnus-diary) |
|---|
| 82 |
|
|---|
| 83 |
(defconst gnus-diary-version nndiary-version |
|---|
| 84 |
"Current Diary back end version.") |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
(eval-and-compile |
|---|
| 90 |
(if (fboundp 'kill-entire-line) |
|---|
| 91 |
(defalias 'gnus-diary-kill-entire-line 'kill-entire-line) |
|---|
| 92 |
(defun gnus-diary-kill-entire-line () |
|---|
| 93 |
(beginning-of-line) |
|---|
| 94 |
(let ((kill-whole-line t)) |
|---|
| 95 |
(kill-line))))) |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
(defun gnus-diary-delay-format-french (past delay) |
|---|
| 101 |
(if (null delay) |
|---|
| 102 |
"maintenant!" |
|---|
| 103 |
|
|---|
| 104 |
(and (> (length delay) 1) (setcdr (cdr delay) nil)) |
|---|
| 105 |
(concat (if past "il y a " "dans ") |
|---|
| 106 |
(let ((str "") |
|---|
| 107 |
del) |
|---|
| 108 |
(while (setq del (pop delay)) |
|---|
| 109 |
(setq str (concat str |
|---|
| 110 |
(int-to-string (car del)) " " |
|---|
| 111 |
(cond ((eq (cdr del) 'year) |
|---|
| 112 |
"an") |
|---|
| 113 |
((eq (cdr del) 'month) |
|---|
| 114 |
"mois") |
|---|
| 115 |
((eq (cdr del) 'week) |
|---|
| 116 |
"semaine") |
|---|
| 117 |
((eq (cdr del) 'day) |
|---|
| 118 |
"jour") |
|---|
| 119 |
((eq (cdr del) 'hour) |
|---|
| 120 |
"heure") |
|---|
| 121 |
((eq (cdr del) 'minute) |
|---|
| 122 |
"minute")) |
|---|
| 123 |
(unless (or (eq (cdr del) 'month) |
|---|
| 124 |
(= (car del) 1)) |
|---|
| 125 |
"s") |
|---|
| 126 |
(if delay ", ")))) |
|---|
| 127 |
str)))) |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
(defun gnus-diary-delay-format-english (past delay) |
|---|
| 131 |
(if (null delay) |
|---|
| 132 |
"now!" |
|---|
| 133 |
|
|---|
| 134 |
(and (> (length delay) 1) (setcdr (cdr delay) nil)) |
|---|
| 135 |
(concat (unless past "in ") |
|---|
| 136 |
(let ((str "") |
|---|
| 137 |
del) |
|---|
| 138 |
(while (setq del (pop delay)) |
|---|
| 139 |
(setq str (concat str |
|---|
| 140 |
(int-to-string (car del)) " " |
|---|
| 141 |
(symbol-name (cdr del)) |
|---|
| 142 |
(and (> (car del) 1) "s") |
|---|
| 143 |
(if delay ", ")))) |
|---|
| 144 |
str) |
|---|
| 145 |
(and past " ago")))) |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
(defun gnus-diary-header-schedule (headers) |
|---|
| 149 |
|
|---|
| 150 |
(mapcar |
|---|
| 151 |
(lambda (elt) |
|---|
| 152 |
(let ((head (cdr (assoc (intern (format "X-Diary-%s" (car elt))) |
|---|
| 153 |
headers)))) |
|---|
| 154 |
(when head |
|---|
| 155 |
(nndiary-parse-schedule-value head (cadr elt) (car (cddr elt)))))) |
|---|
| 156 |
nndiary-headers)) |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
(defun gnus-user-format-function-d (header) |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
(let* ((extras (mail-header-extra header)) |
|---|
| 167 |
(sched (gnus-diary-header-schedule extras)) |
|---|
| 168 |
(occur (nndiary-next-occurence sched (current-time))) |
|---|
| 169 |
(now (current-time)) |
|---|
| 170 |
(real-time (subtract-time occur now))) |
|---|
| 171 |
(if (null real-time) |
|---|
| 172 |
"?????" |
|---|
| 173 |
(let* ((sec (+ (* (float (car real-time)) 65536) (cadr real-time))) |
|---|
| 174 |
(past (< sec 0)) |
|---|
| 175 |
delay) |
|---|
| 176 |
(and past (setq sec (- sec))) |
|---|
| 177 |
(unless (zerop sec) |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
(let ((units `((year . ,(* 365.25 24 3600)) |
|---|
| 182 |
(month . ,(* 31 24 3600)) |
|---|
| 183 |
(week . ,(* 7 24 3600)) |
|---|
| 184 |
(day . ,(* 24 3600)) |
|---|
| 185 |
(hour . 3600) |
|---|
| 186 |
(minute . 60))) |
|---|
| 187 |
unit num) |
|---|
| 188 |
(while (setq unit (pop units)) |
|---|
| 189 |
(unless (zerop (setq num (ffloor (/ sec (cdr unit))))) |
|---|
| 190 |
(setq delay (append delay `((,(floor num) . ,(car unit)))))) |
|---|
| 191 |
(setq sec (- sec (* num (cdr unit))))))) |
|---|
| 192 |
(funcall gnus-diary-delay-format-function past delay))) |
|---|
| 193 |
)) |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
(defun gnus-user-format-function-D (header) |
|---|
| 200 |
|
|---|
| 201 |
(let* ((extras (mail-header-extra header)) |
|---|
| 202 |
(sched (gnus-diary-header-schedule extras)) |
|---|
| 203 |
(occur (nndiary-next-occurence sched (current-time)))) |
|---|
| 204 |
(format-time-string gnus-diary-time-format occur))) |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
(defun gnus-article-sort-by-schedule (h1 h2) |
|---|
| 210 |
(let* ((now (current-time)) |
|---|
| 211 |
(e1 (mail-header-extra h1)) |
|---|
| 212 |
(e2 (mail-header-extra h2)) |
|---|
| 213 |
(s1 (gnus-diary-header-schedule e1)) |
|---|
| 214 |
(s2 (gnus-diary-header-schedule e2)) |
|---|
| 215 |
(o1 (nndiary-next-occurence s1 now)) |
|---|
| 216 |
(o2 (nndiary-next-occurence s2 now))) |
|---|
| 217 |
(if (and (= (car o1) (car o2)) (= (cadr o1) (cadr o2))) |
|---|
| 218 |
(< (mail-header-number h1) (mail-header-number h2)) |
|---|
| 219 |
(time-less-p o1 o2)))) |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
(defun gnus-thread-sort-by-schedule (h1 h2) |
|---|
| 223 |
(gnus-article-sort-by-schedule (gnus-thread-header h1) |
|---|
| 224 |
(gnus-thread-header h2))) |
|---|
| 225 |
|
|---|
| 226 |
(defun gnus-summary-sort-by-schedule (&optional reverse) |
|---|
| 227 |
"Sort nndiary summary buffers by schedule of appointments. |
|---|
| 228 |
Optional prefix (or REVERSE argument) means sort in reverse order." |
|---|
| 229 |
(interactive "P") |
|---|
| 230 |
(gnus-summary-sort 'schedule reverse)) |
|---|
| 231 |
|
|---|
| 232 |
(defvar gnus-summary-misc-menu) |
|---|
| 233 |
(add-hook 'gnus-summary-menu-hook |
|---|
| 234 |
(lambda () |
|---|
| 235 |
(easy-menu-add-item gnus-summary-misc-menu |
|---|
| 236 |
'("Sort") |
|---|
| 237 |
["Sort by schedule" |
|---|
| 238 |
gnus-summary-sort-by-schedule |
|---|
| 239 |
(eq (car (gnus-find-method-for-group |
|---|
| 240 |
gnus-newsgroup-name)) |
|---|
| 241 |
'nndiary)] |
|---|
| 242 |
"Sort by number"))) |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
(defun gnus-diary-update-group-parameters (group) |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
(let ((posting-style (gnus-group-get-parameter group 'posting-style t))) |
|---|
| 255 |
|
|---|
| 256 |
(mapcar (lambda (elt) |
|---|
| 257 |
(let ((header (format "X-Diary-%s" (car elt)))) |
|---|
| 258 |
(unless (assoc header posting-style) |
|---|
| 259 |
(setq posting-style (append posting-style |
|---|
| 260 |
`((,header "*"))))) |
|---|
| 261 |
)) |
|---|
| 262 |
nndiary-headers) |
|---|
| 263 |
(gnus-group-set-parameter group 'posting-style posting-style) |
|---|
| 264 |
|
|---|
| 265 |
(unless (gnus-group-get-parameter group 'gnus-summary-line-format t) |
|---|
| 266 |
(gnus-group-set-parameter group 'gnus-summary-line-format |
|---|
| 267 |
`(,gnus-diary-summary-line-format))) |
|---|
| 268 |
|
|---|
| 269 |
(unless (gnus-group-get-parameter group 'gnus-article-sort-functions) |
|---|
| 270 |
(gnus-group-set-parameter group 'gnus-article-sort-functions |
|---|
| 271 |
'((append gnus-article-sort-functions |
|---|
| 272 |
(list |
|---|
| 273 |
'gnus-article-sort-by-schedule))))) |
|---|
| 274 |
(unless (gnus-group-get-parameter group 'gnus-thread-sort-functions) |
|---|
| 275 |
(gnus-group-set-parameter group 'gnus-thread-sort-functions |
|---|
| 276 |
'((append gnus-thread-sort-functions |
|---|
| 277 |
(list |
|---|
| 278 |
'gnus-thread-sort-by-schedule))))) |
|---|
| 279 |
)) |
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
(defun gnus-diary-maybe-update-group-parameters (group) |
|---|
| 285 |
(when (eq (car (gnus-find-method-for-group group)) 'nndiary) |
|---|
| 286 |
(gnus-diary-update-group-parameters group))) |
|---|
| 287 |
|
|---|
| 288 |
(add-hook 'nndiary-request-create-group-hooks |
|---|
| 289 |
'gnus-diary-update-group-parameters) |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
(add-hook 'nndiary-request-update-info-hooks |
|---|
| 293 |
'gnus-diary-update-group-parameters) |
|---|
| 294 |
(add-hook 'gnus-subscribe-newsgroup-hooks |
|---|
| 295 |
'gnus-diary-maybe-update-group-parameters) |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
(defvar gnus-diary-header-value-history nil |
|---|
| 301 |
|
|---|
| 302 |
) |
|---|
| 303 |
|
|---|
| 304 |
(defun gnus-diary-narrow-to-headers () |
|---|
| 305 |
"Narrow the current buffer to the header part. |
|---|
| 306 |
Point is left at the beginning of the region. |
|---|
| 307 |
The buffer is assumed to contain a message, but the format is unknown." |
|---|
| 308 |
(cond ((eq major-mode 'message-mode) |
|---|
| 309 |
(message-narrow-to-headers)) |
|---|
| 310 |
(t |
|---|
| 311 |
(goto-char (point-min)) |
|---|
| 312 |
(when (search-forward "\n\n" nil t) |
|---|
| 313 |
(narrow-to-region (point-min) (- (point) 1)) |
|---|
| 314 |
(goto-char (point-min)))) |
|---|
| 315 |
)) |
|---|
| 316 |
|
|---|
| 317 |
(defun gnus-diary-add-header (str) |
|---|
| 318 |
"Add a header to the current buffer. |
|---|
| 319 |
The buffer is assumed to contain a message, but the format is unknown." |
|---|
| 320 |
(cond ((eq major-mode 'message-mode) |
|---|
| 321 |
(message-add-header str)) |
|---|
| 322 |
(t |
|---|
| 323 |
(save-restriction |
|---|
| 324 |
(gnus-diary-narrow-to-headers) |
|---|
| 325 |
(goto-char (point-max)) |
|---|
| 326 |
(if (string-match "\n$" str) |
|---|
| 327 |
(insert str) |
|---|
| 328 |
(insert str ?\n)))) |
|---|
| 329 |
)) |
|---|
| 330 |
|
|---|
| 331 |
(defun gnus-diary-check-message (arg) |
|---|
| 332 |
"Ensure that the current message is a valid for NNDiary. |
|---|
| 333 |
This function checks that all NNDiary required headers are present and |
|---|
| 334 |
valid, and prompts for values / correction otherwise. |
|---|
| 335 |
|
|---|
| 336 |
If ARG (or prefix) is non-nil, force prompting for all fields." |
|---|
| 337 |
(interactive "P") |
|---|
| 338 |
(save-excursion |
|---|
| 339 |
(mapcar |
|---|
| 340 |
(lambda (head) |
|---|
| 341 |
(let ((header (concat "X-Diary-" (car head))) |
|---|
| 342 |
(ask arg) |
|---|
| 343 |
value invalid) |
|---|
| 344 |
|
|---|
| 345 |
(save-restriction |
|---|
| 346 |
(gnus-diary-narrow-to-headers) |
|---|
| 347 |
(when (re-search-forward (concat "^" header ":") nil t) |
|---|
| 348 |
(unless (eq (char-after) ? ) |
|---|
| 349 |
(insert " ")) |
|---|
| 350 |
(setq value (buffer-substring (point) (gnus-point-at-eol))) |
|---|
| 351 |
(and (string-match "[ \t]*\\([^ \t]+\\)[ \t]*" value) |
|---|
| 352 |
(setq value (match-string 1 value))) |
|---|
| 353 |
(condition-case () |
|---|
| 354 |
(nndiary-parse-schedule-value value |
|---|
| 355 |
(nth 1 head) (nth 2 head)) |
|---|
| 356 |
(t |
|---|
| 357 |
(setq invalid t))) |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
(when (or ask invalid) |
|---|
| 363 |
(gnus-diary-kill-entire-line)) |
|---|
| 364 |
)) |
|---|
| 365 |
|
|---|
| 366 |
(while (or ask (not value) invalid) |
|---|
| 367 |
(let ((prompt (concat (and invalid |
|---|
| 368 |
(prog1 "(current value invalid) " |
|---|
| 369 |
(beep))) |
|---|
| 370 |
header ": "))) |
|---|
| 371 |
(setq value |
|---|
| 372 |
(if (listp (nth 1 head)) |
|---|
| 373 |
(completing-read prompt (cons '("*" nil) (nth 1 head)) |
|---|
| 374 |
nil t value |
|---|
| 375 |
gnus-diary-header-value-history) |
|---|
| 376 |
(read-string prompt value |
|---|
| 377 |
gnus-diary-header-value-history)))) |
|---|
| 378 |
(setq ask nil) |
|---|
| 379 |
(setq invalid nil) |
|---|
| 380 |
(condition-case () |
|---|
| 381 |
(nndiary-parse-schedule-value value |
|---|
| 382 |
(nth 1 head) (nth 2 head)) |
|---|
| 383 |
(t |
|---|
| 384 |
(setq invalid t)))) |
|---|
| 385 |
(gnus-diary-add-header (concat header ": " value)) |
|---|
| 386 |
)) |
|---|
| 387 |
nndiary-headers) |
|---|
| 388 |
)) |
|---|
| 389 |
|
|---|
| 390 |
(add-hook 'nndiary-request-accept-article-hooks |
|---|
| 391 |
(lambda () (gnus-diary-check-message nil))) |
|---|
| 392 |
|
|---|
| 393 |
(define-key message-mode-map "\C-cDc" 'gnus-diary-check-message) |
|---|
| 394 |
(define-key gnus-article-edit-mode-map "\C-cDc" 'gnus-diary-check-message) |
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
(defun gnus-diary-version () |
|---|
| 400 |
"Current Diary back end version." |
|---|
| 401 |
(interactive) |
|---|
| 402 |
(message "NNDiary version %s" nndiary-version)) |
|---|
| 403 |
|
|---|
| 404 |
(define-key message-mode-map "\C-cDv" 'gnus-diary-version) |
|---|
| 405 |
(define-key gnus-article-edit-mode-map "\C-cDv" 'gnus-diary-version) |
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
(provide 'gnus-diary) |
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|