comparison lisp/calendar/diary-lib.el @ 55249:c51143d3c644

From Dave Love <fx@gnu.org>: (diary-outlook-formats): New variable. (diary-from-outlook-internal, diary-from-outlook) (diary-from-outlook-gnus, diary-from-outlook-rmail): New functions to import diary entries from Outlook-format appointments in mail messages.
author Glenn Morris <rgm@gnu.org>
date Fri, 30 Apr 2004 18:50:08 +0000
parents 8c93a61e3b54
children b278cb498cc8
comparison
equal deleted inserted replaced
55248:8b3e4d251e69 55249:c51143d3c644
1857 '("[0-9]?[0-9]\\([:.]?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\(-[0-9]?[0-9]\\([:.]?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\)?" 1857 '("[0-9]?[0-9]\\([:.]?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\(-[0-9]?[0-9]\\([:.]?[0-9][0-9]\\)?\\(am\\|pm\\|AM\\|PM\\)\\)?"
1858 . font-lock-function-name-face))) 1858 . font-lock-function-name-face)))
1859 "Forms to highlight in diary-mode") 1859 "Forms to highlight in diary-mode")
1860 1860
1861 1861
1862 ;; Following code from Dave Love <fx@gnu.org>.
1863 ;; Import Outlook-format appointments from mail messages in Gnus or
1864 ;; Rmail using command `diary-from-outlook'. This, or the specialized
1865 ;; functions `diary-from-outlook-gnus' and `diary-from-outlook-rmail',
1866 ;; could be run from hooks to notice appointments automatically (in
1867 ;; which case they will prompt about adding to the diary). The
1868 ;; message formats recognized are customizable through
1869 ;; `diary-outlook-formats'.
1870
1871 (defcustom diary-outlook-formats
1872 '(
1873 ;; When: 11 October 2001 12:00-14:00 (GMT) Greenwich Mean Time : Dublin, ...
1874 ;; [Current UK format? The timezone is meaningless. Sometimes the
1875 ;; Where is missing.]
1876 ("When: \\([0-9]+ [[:alpha:]]+ [0-9]+\\) \
1877 \\([^ ]+\\) [^\n]+
1878 \[^\n]+
1879 \\(?:Where: \\([^\n]+\\)\n+\\)?
1880 \\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*~\\*"
1881 . "\\1\n \\2 %s, \\3")
1882 ;; When: Tuesday, April 30, 2002 03:00 PM-03:30 PM (GMT) Greenwich Mean ...
1883 ;; [Old UK format?]
1884 ("^When: [[:alpha:]]+, \\([[:alpha:]]+\\) \\([0-9][0-9]*\\), \\([0-9]\\{4\\}\\) \
1885 \\([^ ]+\\) [^\n]+
1886 \[^\n]+
1887 \\(?:Where: \\([^\n]+\\)\\)?\n+"
1888 . "\\2 \\1 \\3\n \\4 %s, \\5")
1889 (
1890 ;; German format, apparently.
1891 "^Zeit: [^ ]+, +\\([0-9]+\\)\. +\\([[:upper:]][[:lower:]][[:lower:]]\\)[^ ]* +\\([0-9]+\\) +\\([^ ]+\\).*$"
1892 . "\\1 \\2 \\3\n \\4 %s"))
1893 "Alist of regexps matching message text and replacement text.
1894
1895 The regexp must match the start of the message text containing an
1896 appointment, but need not include a leading `^'. If it matches the
1897 current message, a diary entry is made from the corresponding
1898 template. If the template is a string, it should be suitable for
1899 passing to `replace-match', and so will have occurrences of `\\D' to
1900 substitute the match for the Dth subexpression. It must also contain
1901 a single `%s' which will be replaced with the text of the message's
1902 Subject field. Any other `%' characters must be doubled, so that the
1903 template can be passed to `format'.
1904
1905 If the template is actually a function, it is called with the message
1906 body text as argument, and may use `match-string' etc. to make a
1907 template following the rules above."
1908 :type '(alist :key-type (regexp :tag "Regexp matching time/place")
1909 :value-type (choice
1910 (string :tag "Template for entry")
1911 (function :tag "Unary function providing template")))
1912 :version "21.4"
1913 :group 'diary)
1914
1915
1916 ;; Dynamically bound.
1917 (defvar body)
1918 (defvar subject)
1919
1920 (defun diary-from-outlook-internal (&optional test-only)
1921 "Snarf a diary entry from a message assumed to be from MS Outlook.
1922 Assumes `body' is bound to a string comprising the body of the message and
1923 `subject' is bound to a string comprising its subject.
1924 Arg TEST-ONLY non-nil means return non-nil if and only if the
1925 message contains an appointment, don't make a diary entry."
1926 (catch 'finished
1927 (let (format-string)
1928 (dotimes (i (length diary-outlook-formats))
1929 (when (eq 0 (string-match (car (nth i diary-outlook-formats))
1930 body))
1931 (unless test-only
1932 (setq format-string (cdr (nth i diary-outlook-formats)))
1933 (save-excursion
1934 (save-window-excursion
1935 ;; Fixme: References to optional fields in the format
1936 ;; are treated literally, not replaced by the empty
1937 ;; string. I think this is an Emacs bug.
1938 (make-diary-entry
1939 (format (replace-match (if (functionp format-string)
1940 (funcall format-string body)
1941 format-string)
1942 t nil (match-string 0 body))
1943 subject))
1944 (save-buffer))))
1945 (throw 'finished t))))
1946 nil))
1947
1948 (defun diary-from-outlook ()
1949 "Maybe snarf diary entry from current Outlook-generated message.
1950 Currently knows about Gnus and Rmail modes."
1951 (interactive)
1952 (let ((func (cond
1953 ((eq major-mode 'rmail-mode)
1954 #'diary-from-outlook-rmail)
1955 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
1956 #'diary-from-outlook-gnus)
1957 (t (error "Don't know how to snarf in `%s'" major-mode)))))
1958 (if (interactive-p)
1959 (call-interactively func)
1960 (funcall func))))
1961
1962
1963 (defvar gnus-article-mime-handles)
1964 (defvar gnus-article-buffer)
1965
1966 (autoload 'gnus-fetch-field "gnus-util")
1967 (autoload 'gnus-narrow-to-body "gnus")
1968 (autoload 'mm-get-part "mm-decode")
1969
1970 (defun diary-from-outlook-gnus ()
1971 "Maybe snarf diary entry from Outlook-generated message in Gnus.
1972 Add this to `gnus-article-prepare-hook' to notice appointments
1973 automatically."
1974 (interactive)
1975 (with-current-buffer gnus-article-buffer
1976 (let ((subject (gnus-fetch-field "subject"))
1977 (body (if gnus-article-mime-handles
1978 ;; We're multipart. Don't get confused by part
1979 ;; buttons &c. Assume info is in first part.
1980 (mm-get-part (nth 1 gnus-article-mime-handles))
1981 (save-restriction
1982 (gnus-narrow-to-body)
1983 (buffer-string)))))
1984 (when (diary-from-outlook-internal t)
1985 (when (or (interactive-p)
1986 (y-or-n-p "Snarf diary entry? "))
1987 (diary-from-outlook-internal)
1988 (message "Diary entry added"))))))
1989
1990 (custom-add-option 'gnus-article-prepare-hook 'diary-from-outlook-gnus)
1991
1992
1993 (defvar rmail-buffer)
1994
1995 (defun diary-from-outlook-rmail ()
1996 "Maybe snarf diary entry from Outlook-generated message in Rmail."
1997 (interactive)
1998 (with-current-buffer rmail-buffer
1999 (let ((subject (mail-fetch-field "subject"))
2000 (body (buffer-substring (save-excursion
2001 (rfc822-goto-eoh)
2002 (point))
2003 (point-max))))
2004 (when (diary-from-outlook-internal t)
2005 (when (or (interactive-p)
2006 (y-or-n-p "Snarf diary entry? "))
2007 (diary-from-outlook-internal)
2008 (message "Diary entry added"))))))
2009
2010
1862 (provide 'diary-lib) 2011 (provide 'diary-lib)
1863 2012
1864 ;;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010 2013 ;;; arch-tag: 22dd506e-2e33-410d-9ae1-095a0c1b2010
1865 ;;; diary-lib.el ends here 2014 ;;; diary-lib.el ends here