comparison shared/diary.el @ 0:107d592c5f4a

DICE versions, used by pers/common, recursive, I think/hope
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Mon, 08 Feb 2021 11:44:37 +0000
parents
children 0a81352bd7d0
comparison
equal deleted inserted replaced
-1:000000000000 0:107d592c5f4a
1 ;; Last edited: Wed Oct 24 17:08:20 1990
2 ;; provide a simple diary facility on top of rmailsum
3 ;; Copyright (C) 1990 Henry S. Thompson
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
21
22 (provide 'diary)
23 (require 'mail-extras)
24
25 (autoload 'sort-subr "sort")
26
27 (defvar ht-diary-file-name "~/DIARY.babyl"
28 "default name of diary file")
29
30 (defvar ht-Calendar-directory "~/Calendar")
31
32 (defun xxx-date-lessp (date1 date2)
33 "Return T if DATE1 is earlyer than DATE2."
34 (string-lessp (gnus-comparable-date date1)
35 (gnus-comparable-date date2)))
36
37 (defun xxx-comparable-date (date)
38 "Make comparable string by string-lessp from DATE."
39 (let ((month '(("JAN" . " 1")("FEB" . " 2")("MAR" . " 3")
40 ("APR" . " 4")("MAY" . " 5")("JUN" . " 6")
41 ("JUL" . " 7")("AUG" . " 8")("SEP" . " 9")
42 ("OCT" . "10")("NOV" . "11")("DEC" . "12")))
43 (date (or date "")))
44 ;; Can understand the following styles:
45 ;; (1) 14 Apr 89 03:20:12 GMT
46 ;; (2) Fri, 17 March 89 4:01:33 GMT
47 (if (string-match
48 "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) *\\([0-9:]*\\)" date)
49 (let ((year (substring date (match-beginning 3) (match-end 3)))
50 (mn (substring date
51 (match-beginning 2)
52 (+ 3 (match-beginning 2))))
53 (day (substring date
54 (match-beginning 1) (match-end 1)))
55 (time (substring date (match-beginning 4) (match-end 4))))
56 (concat
57 ;; Year
58 (if (= (length year) 2)
59 (if (string-match "^9" year)
60 (concat "19" year)
61 (concat "20" year))
62 year)
63 ;; Month
64 (cdr
65 (assoc
66 (upcase mn)
67 month))
68 ;; Day
69 (format "%2d" (string-to-int day))
70 ;; Time
71 time))
72 ;; Cannot understand DATE string.
73 date
74 )
75 ))
76
77 (defun update-default-diary (arg) "update a diary - with arg, the one for
78 this file. Without arg, the default (named in ht-diary-file-name)"
79 (interactive "P")
80 (if arg
81 (update-diary (current-buffer))
82 (require-diary)
83 (update-diary (get-file-buffer ht-diary-file-name))))
84
85 (defun update-diary (buffer)
86 ;; (setq rmail-summary-buffer (get-buffer-create "*Diary*"))
87 (let ((obuf (current-buffer)))
88 (set-buffer buffer)
89 (if (not has-diary-summary)
90 (progn (make-local-variable 'has-diary-summary)
91 (setq has-diary-summary t)))
92 (rmail-summary)
93 (set-buffer obuf)))
94
95 (defun do-diary-update () "rmail-summary-mode-hook calls this"
96 (if (save-excursion (set-buffer rbuf)
97 has-diary-summary)
98 (progn
99 (make-local-variable 'diary-summary-buffer)
100 (setq diary-summary-buffer t)
101 (setq description (concat "Diary " description))
102 (setq buffer-read-only nil)
103 (sort-diary-hdrs)
104 (format-diary-hdrs)
105 (setq buffer-read-only t)
106 (not-modified)
107 (beginning-of-buffer)
108 (setq mesg nil) ; to go to earliest, not first in file
109 )))
110
111 (defun require-diary ()
112 (if (not (get-file-buffer ht-diary-file-name))
113 (progn (rmail-input ht-diary-file-name)
114 (rmail-show-message 1))
115 ))
116
117 (defvar diary-summary-buffer nil "flag to identify diary summaries")
118 (defvar has-diary-summary nil "flag to identify buffers with diary summaries")
119
120 (defun sort-diary-hdrs ()
121 (interactive)
122 (goto-char (point-min))
123 (sort-subr nil 'forward-line 'end-of-line 'get-diary-hdr-date nil))
124
125 (defun format-diary-hdrs ()
126 (goto-char (point-min))
127 (while (< (point)(point-max))
128 (forward-char 5)
129 (delete-char 35)
130 (looking-at " *\\([0-9]*\\) *\\([a-zA-Z]*\\) *\\([0-9]*\\) *\\([0-9]*\\)")
131 (if (match-beginning 0)
132 (let ((day (buffer-substring (match-beginning 1)(match-end 1)))
133 (month (capitalize (buffer-substring (match-beginning 2)
134 (min (+ (match-beginning 2)
135 3)
136 (match-end 2)))))
137 (year (buffer-substring (max
138 (- (match-end 3) 2)
139 (match-beginning 3))
140 (match-end 3)))
141 (time (buffer-substring (match-beginning 4)(match-end 4))))
142 (delete-char (+ 1
143 (if (= (match-end 4)
144 (match-beginning 4))
145 0 ; fix for no time case
146 1)
147 (- (match-end 4)(match-beginning 1))))
148 (insert (format " %2s %3s %2s %4s " day month year time))))
149 (forward-line 1))
150 (goto-char (point-min)))
151
152 (defun get-diary-hdr-date ()
153 (looking-at " *[^ ]* *[^ ]* *[^ ]* *\\(.*\\)$")
154 (xxx-comparable-date (buffer-substring (match-beginning 1)(match-end 1))))
155
156
157 (if (not (boundp 'rmail-edit-map))
158 (load-library "rmailedit"))
159 (if (not (boundp 'rmail-summary-mode-map))
160 (progn (load-library "rmailsum")
161 (rmail-summary-mode-fun1)))
162 (define-key rmail-edit-map "\C-c\C-c" 'ht-rmail-cease-edit)
163 (define-key rmail-edit-map "\C-c\C-]" 'ht-rmail-abort-edit)
164 ;(defvar diary-mode-map (copy-keymap rmail-summary-mode-map))
165 ;(define-key diary-mode-map "s" 'diary-save)
166 (define-key rmail-mode-map "h" 'ht-rmail-summarise)
167 (setq rmail-summary-mode-hook 'do-diary-update)
168
169 (defvar editing-diary-entry nil)
170
171 (defun diary-save ()
172 "save parent file and update"
173 (interactive)
174 (set-buffer rmail-buffer)
175 (rmail-expunge-and-save)
176 (if has-diary-summary (update-diary (current-buffer))))
177
178 (defun ht-rmail-summarise ()
179 "Display a summary of all messages, one line per message.
180 If file is named as ht-diary-file-name, or the summary buffer is already
181 a diary summary, make it a Diary summary (see
182 \\[describe-mode] rmail-summary-mode for info)."
183 (interactive)
184 (if (eq (current-buffer)
185 (get-file-buffer ht-diary-file-name))
186 (update-default-diary t)
187 (rmail-summary)))
188
189 (defun edit-and-move-to-diary ()
190 "try to add a date to subject field, move to diary on exit"
191 (interactive)
192 (make-local-variable 'editing-diary-entry)
193 (setq editing-diary-entry t)
194 (rmail-edit-current-message)
195 (goto-char (point-min))
196 (search-forward "\n\n")
197 (let ((try-date (and
198 (re-search-forward
199 "[0-9][-0-9 ]*[- ][a-zA-Z][a-zA-Z]*[- 0-9]*" nil t)
200 (buffer-substring (match-beginning 0)(match-end 0))))
201 sublp)
202 (goto-char (point-min))
203 (setq sublp (search-forward "Subject: " nil t))
204 (if try-date
205 (progn (set-mark (point))
206 (insert try-date)))))
207
208 (defun gnus-edit-and-move-to-diary (&optional no-delete)
209 "try to add a date to subject field, move to diary on exit"
210 (interactive "P")
211 (when (gnus-group-read-only-p)
212 (error "The current newsgroup does not support article editing"))
213 ;; Select article if needed.
214 (unless (eq (gnus-summary-article-number)
215 gnus-current-article)
216 (gnus-summary-select-article t))
217 (gnus-article-date-original)
218 (message "About to forward. . .")
219 (gnus-summary-mail-forward 1)
220 (message "Begin forward. . .")
221 (goto-char (point-min))
222 (search-forward "^To: " nil t)
223 (forward-char 4)
224 (insert "htcalendar@markup.co.uk")
225 (search-forward "------ Start of forwarded")
226 (let (sublp)
227 (save-excursion
228 (let ((try-date
229 (and
230 (or (re-search-forward "^\r?$" nil 1) t)
231 (re-search-forward
232 "[0-9][-0-9 ]*[- ][jfmasondJFMASOND][a-zA-Z]*[- 0-9]*"
233 (save-excursion (search-forward "\n--\n" nil t))
234 t)
235 (buffer-substring (match-beginning 0)(match-end 0)))))
236 (goto-char (point-min))
237 (setq sublp (search-forward "Subject: " nil t))
238 (delete-region (point)(progn (search-forward "] " nil t)))
239 (message (format "date: |%s| %s" try-date sublp))
240 (if (and sublp
241 try-date)
242 (progn (set-mark (point))
243 (insert try-date)))))
244 (make-local-hook 'message-send-hook)
245 (if (and no-delete (equal (car no-delete) 16))
246 (let ((hook '(lambda ()
247 (ht-gnus-cease-edit nil)
248 nil t)))
249 (add-hook 'message-send-hook hook nil t)
250
251 (message-send-and-exit)
252 (if (not (gnus-summary-next-unread-article))
253 (gnus-summary-exit)))
254 (add-hook 'message-send-hook
255 `(lambda ()
256 (ht-gnus-cease-edit ',no-delete)
257 ; (gnus-summary-edit-article-done
258 ; ,(or (mail-header-references gnus-current-headers) "")
259 ; ,(gnus-group-read-only-p) ,gnus-summary-buffer nil)
260 ; (switch-to-buffer gnus-summary-buffer)))
261 ; (goto-char (point-min))
262 ; (search-forward "\nSubject: " nil t))
263 )
264 nil t)
265 (split-window-vertically 6)
266 (other-window 1)
267 (search-forward "\n\n" nil t)
268 (other-window 1)
269 (goto-char sublp)
270 (message "Exiting to buffer, we hope")))
271 )
272
273 (defun ht-gnus-cease-edit (&optional no-delete)
274 "check if diary edit, move if so"
275 (interactive "P")
276 (message "ceasing. . .")
277 (ht-forward-to-Calendar)
278 (let ((rmail-summary-redo '(rmail-summary)))
279 (gnus-output-to-rmail ht-diary-file-name)
280 )
281 (unless no-delete
282 (with-current-buffer gnus-summary-buffer
283 (gnus-summary-delete-article)))
284 (if (get-buffer "diary.babyl-summary")
285 (kill-buffer "diary.babyl-summary"))
286 (with-current-buffer "diary.babyl"
287 (rmail-mode)
288 (save-buffer)
289 (ht-rmail-summarise))
290 (message "ceased"))
291
292 (defun ht-gnus-summary-save-in-diary (&optional filename)
293 (gnus-eval-in-buffer-window gnus-save-article-buffer
294 (save-excursion
295 (save-restriction
296 (widen)
297 (gnus-output-to-rmail ht-diary-file-name)))))
298
299 ;; private copy to simulate hook
300 (defun ht-rmail-cease-edit ()
301 "check if diary edit, move if so"
302 (interactive)
303 (rmail-cease-edit)
304 (if editing-diary-entry
305 (progn (setq editing-diary-entry nil)
306 (ht-forward-to-Calendar)
307 (rmail-output-to-rmail-file ht-diary-file-name 1)
308 (ht-rmail-delete-forward))))
309
310 ;; try to add a diary subject field line to the appropriate calendar file
311 (defun ht-forward-to-Calendar ()
312 (goto-char (point-min))
313 (search-forward "Subject: ")
314 (or (looking-at
315 "\\([0-9]+\\) \\([A-Za-z]+\\) \\([0-9]+\\) \\([0-9:]*\\) ?\\(.*\\)\n")
316 (error "not a recognisable diary line"))
317 (let ((day (buffer-substring (match-beginning 1) (match-end 1)))
318 (month (buffer-substring (match-beginning 2) (match-end 2)))
319 (year (buffer-substring (match-beginning 3) (match-end 3)))
320 (time (buffer-substring (match-beginning 4) (match-end 4)))
321 (message (buffer-substring (match-beginning 5) (match-end 5)))
322 (mb (match-beginning 4))
323 (me (match-end 5))
324 ends e-day e-month fn)
325 (let ((year (if (string-match "^\\(19\\|20\\).." year)
326 year
327 (if (eq (length year) 2)
328 (concat "20" year)
329 (progn (if (and (equal time "")(eq (length year) 4))
330 (setq time year))
331 (format-time-string "%Y")))))
332 (t-month (capitalize
333 (substring month 0 3))))
334 (let* ((n-day (read day))
335 (mon-table '((Jan . 1)
336 (Feb . 2)
337 (Mar . 3)
338 (Apr . 4)
339 (May . 5)
340 (Jun . 6)
341 (Jul . 7)
342 (Aug . 8)
343 (Sep . 9)
344 (Oct . 10)
345 (Nov . 11)
346 (Dec . 12)))
347 (a-month (assq (read t-month)
348 mon-table))
349 (n-month (if a-month (cdr a-month) 0))
350 (u-time (if (equal time "") "0" time))
351 (hour (/ (read u-time) 100))
352 (minute (mod (read u-time) 100))
353 (nhour (if (> minute 29)
354 (+ 1 hour)
355 hour))
356 (nminute (if (> minute 29)
357 (- minute 30)
358 (+ minute 30)))
359 (n-year (read year))
360 (r-subj (mail-fetch-field "Subject"))
361 (body (save-excursion
362 (buffer-substring
363 (progn
364 (goto-char (point-min))
365 (if (re-search-forward "^\r?$" nil 1)
366 (match-beginning 0)
367 (point-max)))
368 (point-max))))
369 (subj-matches (string-match "^\\([^(]*\\)\\((\\(.*\\))\\)?"
370 message))
371 (np-subj (match-string 1 message))
372 (p-subj (or (match-string 3 message) ""))
373 (uid (or (mail-fetch-field "Message-id")
374 (let ((ct (current-time)))
375 (format "%d-%d-%d"
376 (car ct)
377 (cadr ct)
378 (caddr ct)))))
379 )
380 (if (string-match " -- \\(.*\\)$" message)
381 (progn
382 (setq ends (substring message (match-beginning 1)
383 (match-end 1)))
384 (setq message (substring message 0 (match-beginning 0)))
385 (if (string-match "\\([0-9]+\\) \\([A-Za-z]+\\)" ends)
386 (progn
387 (setq e-day (substring ends (match-beginning 1)
388 (match-end 1)))
389 (setq e-month (assq
390 (read (capitalize
391 (substring
392 (substring ends (match-beginning 2)
393 (match-end 2))
394 0 3)))
395 mon-table))))))
396 (setq fn (build-vcal-message (my-time-iso8601
397 (encode-time
398 0 minute
399 hour
400 n-day
401 n-month
402 n-year))
403 (my-time-iso8601
404 (if e-day
405 (encode-time
406 0 (if (eq hour 0) 30 minute) ;nminute
407 (if (eq hour 0) 23 hour) ; nhour
408 (read e-day)
409 (if e-month (cdr e-month) 0)
410 n-year)
411 (encode-time
412 0 minute ; nminute
413 (+ hour 1) ; nhour
414 n-day
415 n-month
416 n-year)))
417 "ORGANIZER;CN=\"Henry S. Thompson\":mailto:htcalendar@markup.co.uk"
418 ;(concat "ORGANIZER:" (mail-fetch-field "From"))
419 p-subj
420 body
421 np-subj
422 (concat "ht-vcal-" uid)))
423 (if fn
424 (progn
425 (goto-char (point-min))
426 (if (search-forward "<#multipart " nil t)
427 (progn
428 (if (search-forward "<#multipart type=alternative" nil t)
429 (beginning-of-line)
430 (forward-line 2)
431 ;; now at beginning of forwarded text
432 (if (search-forward "<#part " nil t)
433 (progn
434 ;; now at beginning of _attachments_
435 (beginning-of-line))
436 ;; no attachments, probably never happens
437 (search-forward "<#/multipart>"))))
438 ;; plain text, make it multipart
439 (search-forward "-------- Start of forwarded")
440 (re-search-forward "^\r?$")
441 (forward-line 1)
442 (insert "<#multipart type=mixed>\n<#part type=text/plain charset=\"ISO-8859-1\" format=\"flowed\" disposition=inline nofile=yes>\n")
443 (search-forward "--------- End of forwarded")
444 (forward-line -1)
445 (insert "<#/multipart>\n")
446 (forward-line -1))
447 (mml-attach-file fn "application/octet-stream" "diary event")
448 ; (let ((res (shell-command-to-string
449 ; (concat "updateCal.pl < " fn))))
450 ; (if (not (equal res ""))
451 ; (message (format "update losing: %s" res))))
452 )))
453 (if (file-exists-p ht-Calendar-directory)
454 (let* ((dfn (concat ht-Calendar-directory
455 "/xy"
456 year
457 "/xc"
458 day
459 t-month
460 year))
461 (buf (find-file-noselect dfn))
462 )
463 (save-excursion
464 (set-buffer buf)
465 (goto-char (point-max))
466 (if (not (bolp))
467 (insert "\n"))
468 (if time
469 (insert time " "))
470 (insert message)
471 (let ((require-final-newline nil))
472 (save-buffer)))
473 (if ends
474 ;; an end date also given
475 (if e-day
476 (let (t-e-month msg)
477 (setq msg (concat
478 (substring message 0
479 (string-match " " message))
480 " continues"))
481 (if (string-equal (setq t-e-month
482 (if e-month (car e-month)
483 t-month))
484 t-month)
485 (fill-dates year t-month (1+ (car
486 (read-from-string day)))
487 (car
488 (read-from-string e-day))
489 msg)
490 (fill-dates year t-month (1+ (car
491 (read-from-string day)))
492 (cdr (assoc t-month
493 '(("Jan" . 31)
494 ("Feb" . 28)
495 ("Mar" . 31)
496 ("Apr" . 30)
497 ("May" . 31)
498 ("Jun" . 30)
499 ("Jul" . 31)
500 ("Aug" . 31)
501 ("Sep" . 30)
502 ("Oct" . 31)
503 ("Nov" . 30)
504 ("Dec" . 31))))
505 msg)
506 (fill-dates year t-e-month 1
507 (car (read-from-string e-day))
508 msg)))
509 (message "\C-g\C-gCouldn't parse end date: %s" ends)))
510 )))))
511
512 (defun fill-dates (year month start end mesg)
513 "fill the dates between start and end with message in the calendar"
514 (let ((day start))
515 (while (<= day end)
516 (let* ((dfn (concat ht-Calendar-directory
517 "/xy"
518 year
519 "/xc"
520 (format "%d" day)
521 (format "%s" month)
522 year))
523 (buf (find-file-noselect dfn)))
524 (save-excursion
525 (set-buffer buf)
526 (goto-char (point-max))
527 (if (not (bolp))
528 (insert "\n"))
529 (insert mesg)
530 (let ((require-final-newline nil))
531 (save-buffer))))
532 (setq day (1+ day)))))
533
534 ;; private copy
535 (defun ht-rmail-abort-edit ()
536 "add a hook"
537 (interactive)
538 (setq editing-diary-entry nil)
539 (rmail-abort-edit))
540
541 (defun rmail-edit-current-message ()
542 "Edit the contents of this message."
543 (interactive)
544 (rmail-edit-mode)
545 (make-local-variable 'rmail-old-text)
546 (setq rmail-old-text (buffer-substring (point-min) (point-max)))
547 (setq buffer-read-only nil)
548 (set-buffer-modified-p (buffer-modified-p))
549 ;; Make mode line update.
550 (if (and (eq (key-binding "\C-c\C-c") 'ht-rmail-cease-edit)
551 (eq (key-binding "\C-c\C-]") 'ht-rmail-abort-edit))
552 (if editing-diary-entry
553 (message "Editing: Type C-c C-c to move to diary and return to Rmail, C-c C-] to abort")
554 (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort"))
555 (message (substitute-command-keys
556 "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
557
558
559 (defun build-vcal-message (start end org location description summary uid)
560 (save-excursion
561 (let ((fn (concat "/tmp/" (make-temp-name "vcal") ".vcs")))
562 (find-file fn)
563 (insert "BEGIN:VCALENDAR\nMETHOD:PUBLISH\nPRODID:-//Henry S. Thompson//gnus diary hack//EN\nVERSION:0.1\nBEGIN:VEVENT\n")
564 (insert "UID\n :")(insert uid)(insert "\n")
565 (insert "SUMMARY")(insert-encoded-maybe summary)(insert "\n")
566 (insert "DESCRIPTION")(insert-folded description)(insert "\r\n")
567 (insert "LOCATION")(insert-encoded-maybe location)(insert "\n")
568 (insert "DTSTART\n :")(insert start)(insert "Z\n")
569 (insert "DTEND\n :")(insert end)(insert "Z\n")
570 (insert "DTSTAMP\n :")(insert
571 (my-time-iso8601 (current-time)))
572 (insert "Z\n")
573 ;(insert "ORGANIZER")(insert-encoded-maybe org)
574 (insert org)
575 (insert "\n")
576 (insert "BEGIN:VALARM\nTRIGGER:-PT15M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\n")
577 (insert "BEGIN:VALARM\nTRIGGER:-PT15M\nACTION:AUDIO\nDESCRIPTION:Reminder\nEND:VALARM\n")
578 (insert "END:VEVENT\nEND:VCALENDAR\n")
579 (save-buffer)
580 fn)))
581
582 (defun insert-encoded-maybe (string)
583 (if (string-match "[\000-\007\n\013\015-\037\200-\377=]" string)
584 (progn
585 (insert ";ENCODING=QUOTED-PRINTABLE:")
586 (let ((beg (point)))
587 (insert string)
588 (message (format "%d;%d" beg (point)))
589 (quoted-printable-encode-region
590 beg
591 (point)
592 t
593 "^\000-\007\n\013\015-\037\200-\377="))
594 (goto-char (point-max)))
595 (insert "\n :")
596 (insert string)))
597
598 (defun insert-folded (string)
599 (insert "\n :")
600 (let ((beg (point)))
601 (insert string)
602 (narrow-to-region beg (point))
603 (goto-char (point-min))
604 (replace-string "\n" "\\n")
605 (goto-char (point-min))
606 (replace-string "\r" "")
607 (goto-char (point-min))
608 (replace-string "," "\\,")
609 (goto-char (point-min))
610 (while (> (- (point-max) (point)) 72)
611 (forward-char 70)
612 (insert "\n "))
613 (goto-char (point-max))
614 (insert "\r\n")
615 (widen)))
616
617 (defun my-time-iso8601 (time)
618 (let ((tzo (car (current-time-zone time)))
619 (hi (car time))
620 (lo (cadr time))
621 (ignore (cddr time)))
622 (gnus-time-iso8601
623 (if (>= lo tzo)
624 (cons hi
625 (cons (- lo tzo)
626 ignore))
627 (cons (- hi 1)
628 (cons (- (+ lo 65536) tzo)
629 ignore)))
630 )))