comparison lisp/calendar/diary-lib.el @ 93232:32f5a7f03231

Some re-ordering so that defcustoms are at start, functions defined before use.
author Glenn Morris <rgm@gnu.org>
date Wed, 26 Mar 2008 03:26:43 +0000
parents 9c718a4c0412
children 639cd5027418
comparison
equal deleted inserted replaced
93231:a0d87f80e5e8 93232:32f5a7f03231
303 (function :tag 303 (function :tag
304 "Unary function providing template"))) 304 "Unary function providing template")))
305 :version "22.1" 305 :version "22.1"
306 :group 'diary) 306 :group 'diary)
307 307
308 ;;; More user options below and in calendar.el. 308
309 ;; The first version of this also checked for diary-selective-display
310 ;; in the non-fancy case. This was an attempt to distinguish between
311 ;; displaying the diary and just visiting the diary file. However,
312 ;; when using fancy diary, calling diary when there are no entries to
313 ;; display does not create the fancy buffer, nor does it set
314 ;; diary-selective-display in the diary buffer. This means some
315 ;; customizations will not take effect, eg:
316 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
317 ;; So the check for diary-selective-display was dropped. This means the
318 ;; diary will be displayed if one customizes a diary variable while
319 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
320 ;;;###cal-autoload
321 (defun diary-live-p ()
322 "Return non-nil if the diary is being displayed."
323 (or (get-buffer fancy-diary-buffer)
324 (and diary-file
325 (find-buffer-visiting (substitute-in-file-name diary-file)))))
326
327 ;;;###cal-autoload
328 (defun diary-set-maybe-redraw (symbol value)
329 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
330 Redraws the diary if it is being displayed (note this is not the same as
331 just visiting the `diary-file'), and SYMBOL's value is to be changed."
332 (let ((oldvalue (eval symbol))) ; FIXME symbol-value?
333 (custom-set-default symbol value)
334 (and (not (equal value oldvalue))
335 (diary-live-p)
336 ;; Note this assumes diary was called without prefix arg.
337 (diary))))
338
339 (defvar diary-selective-display nil
340 "Internal diary variable; non-nil if some diary text is hidden.")
341
342
343 ;; This can be removed once the kill/yank treatment of invisible text
344 ;; (see etc/TODO) is fixed. -- gm
345 (defcustom diary-header-line-flag t
346 "Non-nil means `simple-diary-display' will show a header line.
347 The format of the header is specified by `diary-header-line-format'."
348 :group 'diary
349 :type 'boolean
350 :initialize 'custom-initialize-default
351 ;; FIXME overkill.
352 :set 'diary-set-maybe-redraw
353 :version "22.1")
354
355 (defcustom diary-header-line-format
356 '(:eval (calendar-string-spread
357 (list (if diary-selective-display
358 "Some text is hidden - press \"s\" in calendar \
359 before edit/copy"
360 "Diary"))
361 ?\s (frame-width)))
362 "Format of the header line displayed by `simple-diary-display'.
363 Only used if `diary-header-line-flag' is non-nil."
364 :group 'diary
365 :type 'sexp
366 :initialize 'custom-initialize-default
367 ;; FIXME overkill.
368 :set 'diary-set-maybe-redraw
369 :version "22.1")
370
371 (defcustom number-of-diary-entries 1
372 "Specifies how many days of diary entries are to be displayed initially.
373 This variable affects the diary display when the command \\[diary] is used,
374 or if the value of the variable `view-diary-entries-initially' is non-nil.
375 For example, if the default value 1 is used, then only the current day's diary
376 entries will be displayed. If the value 2 is used, then both the current
377 day's and the next day's entries will be displayed.
378
379 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
380 says to display no diary entries on Sunday, the entries for
381 the current date and the day after on Monday through Thursday,
382 Friday through Monday's entries on Friday, and only Saturday's
383 entries on Saturday.
384
385 This variable does not affect the diary display with the `d' command
386 from the calendar; in that case, the prefix argument controls the
387 number of days of diary entries displayed."
388 :type '(choice (integer :tag "Entries")
389 (vector :value [0 0 0 0 0 0 0]
390 (integer :tag "Sunday")
391 (integer :tag "Monday")
392 (integer :tag "Tuesday")
393 (integer :tag "Wednesday")
394 (integer :tag "Thursday")
395 (integer :tag "Friday")
396 (integer :tag "Saturday")))
397 :initialize 'custom-initialize-default
398 :set 'diary-set-maybe-redraw
399 :group 'diary)
400
401 ;;; More user options in calendar.el.
309 402
310 403
311 (defun diary-check-diary-file () 404 (defun diary-check-diary-file ()
312 "Check that the file specified by `diary-file' exists and is readable. 405 "Check that the file specified by `diary-file' exists and is readable.
313 If so, return the expanded file name, otherwise signal an error." 406 If so, return the expanded file name, otherwise signal an error."
327 (interactive "P") 420 (interactive "P")
328 (diary-check-diary-file) 421 (diary-check-diary-file)
329 (diary-list-entries (calendar-current-date) 422 (diary-list-entries (calendar-current-date)
330 (if arg (prefix-numeric-value arg)))) 423 (if arg (prefix-numeric-value arg))))
331 424
332 (define-obsolete-function-alias 'view-diary-entries 'diary-view-entries)
333 ;;;###cal-autoload 425 ;;;###cal-autoload
334 (defun diary-view-entries (&optional arg) 426 (defun diary-view-entries (&optional arg)
335 "Prepare and display a buffer with diary entries. 427 "Prepare and display a buffer with diary entries.
336 Searches the file named in `diary-file' for entries that 428 Searches the file named in `diary-file' for entries that
337 match ARG days starting with the date indicated by the cursor position 429 match ARG days starting with the date indicated by the cursor position
338 in the displayed three-month calendar." 430 in the displayed three-month calendar."
339 (interactive "p") 431 (interactive "p")
340 (diary-check-diary-file) 432 (diary-check-diary-file)
341 (diary-list-entries (calendar-cursor-to-date t) arg)) 433 (diary-list-entries (calendar-cursor-to-date t) arg))
434
435 (define-obsolete-function-alias 'view-diary-entries 'diary-view-entries)
436
342 437
343 ;;;###cal-autoload 438 ;;;###cal-autoload
344 (defun view-other-diary-entries (arg dfile) 439 (defun view-other-diary-entries (arg dfile)
345 "Prepare and display buffer of diary entries from an alternative diary file. 440 "Prepare and display buffer of diary entries from an alternative diary file.
346 Searches for entries that match ARG days, starting with the date indicated 441 Searches for entries that match ARG days, starting with the date indicated
411 (and attrvalue 506 (and attrvalue
412 (setq attrvalue (diary-attrtype-convert attrvalue type)) 507 (setq attrvalue (diary-attrtype-convert attrvalue type))
413 (setq ret-attr (append ret-attr (list attrname attrvalue)))))) 508 (setq ret-attr (append ret-attr (list attrname attrvalue))))))
414 (list entry ret-attr))) 509 (list entry ret-attr)))
415 510
416 ;; The first version of this also checked for diary-selective-display
417 ;; in the non-fancy case. This was an attempt to distinguish between
418 ;; displaying the diary and just visiting the diary file. However,
419 ;; when using fancy diary, calling diary when there are no entries to
420 ;; display does not create the fancy buffer, nor does it set
421 ;; diary-selective-display in the diary buffer. This means some
422 ;; customizations will not take effect, eg:
423 ;; http://lists.gnu.org/archive/html/emacs-pretest-bug/2007-03/msg00466.html
424 ;; So the check for diary-selective-display was dropped. This means the
425 ;; diary will be displayed if one customizes a diary variable while
426 ;; just visiting the diary-file. This is i) unlikely, and ii) no great loss.
427 ;;;###cal-autoload
428 (defun diary-live-p ()
429 "Return non-nil if the diary is being displayed."
430 (or (get-buffer fancy-diary-buffer)
431 (and diary-file
432 (find-buffer-visiting (substitute-in-file-name diary-file)))))
433
434 ;;;###cal-autoload
435 (defun diary-set-maybe-redraw (symbol value)
436 "Set SYMBOL's value to VALUE, and redraw the diary if necessary.
437 Redraws the diary if it is being displayed (note this is not the same as
438 just visiting the `diary-file'), and SYMBOL's value is to be changed."
439 (let ((oldvalue (eval symbol))) ; FIXME symbol-value?
440 (custom-set-default symbol value)
441 (and (not (equal value oldvalue))
442 (diary-live-p)
443 ;; Note this assumes diary was called without prefix arg.
444 (diary))))
445
446 ;; This can be removed once the kill/yank treatment of invisible text
447 ;; (see etc/TODO) is fixed. -- gm
448 (defcustom diary-header-line-flag t
449 "Non-nil means `simple-diary-display' will show a header line.
450 The format of the header is specified by `diary-header-line-format'."
451 :group 'diary
452 :type 'boolean
453 :initialize 'custom-initialize-default
454 ;; FIXME overkill.
455 :set 'diary-set-maybe-redraw
456 :version "22.1")
457
458 (defvar diary-selective-display nil
459 "Internal diary variable; non-nil if some diary text is hidden.")
460
461 (defcustom diary-header-line-format
462 '(:eval (calendar-string-spread
463 (list (if diary-selective-display
464 "Some text is hidden - press \"s\" in calendar \
465 before edit/copy"
466 "Diary"))
467 ?\s (frame-width)))
468 "Format of the header line displayed by `simple-diary-display'.
469 Only used if `diary-header-line-flag' is non-nil."
470 :group 'diary
471 :type 'sexp
472 :initialize 'custom-initialize-default
473 ;; FIXME overkill.
474 :set 'diary-set-maybe-redraw
475 :version "22.1")
476
477 (defcustom number-of-diary-entries 1
478 "Specifies how many days of diary entries are to be displayed initially.
479 This variable affects the diary display when the command \\[diary] is used,
480 or if the value of the variable `view-diary-entries-initially' is non-nil.
481 For example, if the default value 1 is used, then only the current day's diary
482 entries will be displayed. If the value 2 is used, then both the current
483 day's and the next day's entries will be displayed.
484
485 The value can also be a vector such as [0 2 2 2 2 4 1]; this value
486 says to display no diary entries on Sunday, the entries for
487 the current date and the day after on Monday through Thursday,
488 Friday through Monday's entries on Friday, and only Saturday's
489 entries on Saturday.
490
491 This variable does not affect the diary display with the `d' command
492 from the calendar; in that case, the prefix argument controls the
493 number of days of diary entries displayed."
494 :type '(choice (integer :tag "Entries")
495 (vector :value [0 0 0 0 0 0 0]
496 (integer :tag "Sunday")
497 (integer :tag "Monday")
498 (integer :tag "Tuesday")
499 (integer :tag "Wednesday")
500 (integer :tag "Thursday")
501 (integer :tag "Friday")
502 (integer :tag "Saturday")))
503 :initialize 'custom-initialize-default
504 :set 'diary-set-maybe-redraw
505 :group 'diary)
506 511
507 512
508 (defvar diary-modify-entry-list-string-function nil 513 (defvar diary-modify-entry-list-string-function nil
509 "Function applied to entry string before putting it into the entries list. 514 "Function applied to entry string before putting it into the entries list.
510 Can be used by programs integrating a diary list into other buffers (e.g. 515 Can be used by programs integrating a diary list into other buffers (e.g.
1219 (mark-sexp-diary-entries) 1224 (mark-sexp-diary-entries)
1220 (run-hooks 'nongregorian-diary-marking-hook 1225 (run-hooks 'nongregorian-diary-marking-hook
1221 'mark-diary-entries-hook)) 1226 'mark-diary-entries-hook))
1222 (message "Marking diary entries...done"))))) 1227 (message "Marking diary entries...done")))))
1223 1228
1229
1230 (defun diary-sexp-entry (sexp entry date)
1231 "Process a SEXP diary ENTRY for DATE."
1232 (let ((result (if calendar-debug-sexp
1233 (let ((stack-trace-on-error t))
1234 (eval (car (read-from-string sexp))))
1235 (condition-case nil
1236 (eval (car (read-from-string sexp)))
1237 (error
1238 (beep)
1239 (message "Bad sexp at line %d in %s: %s"
1240 (count-lines (point-min) (point))
1241 diary-file sexp)
1242 (sleep-for 2))))))
1243 (cond ((stringp result) result)
1244 ((and (consp result)
1245 (stringp (cdr result))) result)
1246 (result entry)
1247 (t nil))))
1248
1224 (defvar displayed-year) ; bound in generate-calendar 1249 (defvar displayed-year) ; bound in generate-calendar
1225 (defvar displayed-month) 1250 (defvar displayed-month)
1226 1251
1227 (defun mark-sexp-diary-entries () 1252 (defun mark-sexp-diary-entries ()
1228 "Mark days in the calendar window that have sexp diary entries. 1253 "Mark days in the calendar window that have sexp diary entries.
1460 (defun sort-diary-entries () 1485 (defun sort-diary-entries ()
1461 "Sort the list of diary entries by time of day." 1486 "Sort the list of diary entries by time of day."
1462 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare))) 1487 (setq diary-entries-list (sort diary-entries-list 'diary-entry-compare)))
1463 1488
1464 1489
1465 (defun diary-sexp-entry (sexp entry date)
1466 "Process a SEXP diary ENTRY for DATE."
1467 (let ((result (if calendar-debug-sexp
1468 (let ((stack-trace-on-error t))
1469 (eval (car (read-from-string sexp))))
1470 (condition-case nil
1471 (eval (car (read-from-string sexp)))
1472 (error
1473 (beep)
1474 (message "Bad sexp at line %d in %s: %s"
1475 (count-lines (point-min) (point))
1476 diary-file sexp)
1477 (sleep-for 2))))))
1478 (cond ((stringp result) result)
1479 ((and (consp result)
1480 (stringp (cdr result))) result)
1481 (result entry)
1482 (t nil))))
1483
1484 (defun list-sexp-diary-entries (date) 1490 (defun list-sexp-diary-entries (date)
1485 "Add sexp entries for DATE from the diary file to `diary-entries-list'. 1491 "Add sexp entries for DATE from the diary file to `diary-entries-list'.
1486 Also, make them visible in the diary file. Returns t if any entries were 1492 Also, make them visible in the diary file. Returns t if any entries were
1487 found. 1493 found.
1488 1494
1652 (while (re-search-forward s-entry nil t) 1658 (while (re-search-forward s-entry nil t)
1653 (backward-char 1) 1659 (backward-char 1)
1654 (let ((sexp-start (point)) 1660 (let ((sexp-start (point))
1655 sexp entry specifier entry-start line-start) 1661 sexp entry specifier entry-start line-start)
1656 (forward-sexp) 1662 (forward-sexp)
1657 (setq sexp (buffer-substring-no-properties sexp-start (point))) 1663 (setq sexp (buffer-substring-no-properties sexp-start (point))
1658 (setq line-start (line-end-position 0)) 1664 line-start (line-end-position 0)
1659 (setq specifier 1665 specifier
1660 (buffer-substring-no-properties (1+ line-start) (point)) 1666 (buffer-substring-no-properties (1+ line-start) (point))
1661 entry-start (1+ line-start)) 1667 entry-start (1+ line-start))
1662 (forward-char 1) 1668 (forward-char 1)
1663 (if (and (bolp) (not (looking-at "[ \t]"))) 1669 (if (and (bolp) (not (looking-at "[ \t]")))
1664 ;; Diary entry consists only of the sexp. 1670 ;; Diary entry consists only of the sexp.
1689 (if entry-start (copy-marker entry-start)) 1695 (if entry-start (copy-marker entry-start))
1690 marks 1696 marks
1691 literal) 1697 literal)
1692 (setq entry-found (or entry-found diary-entry))))) 1698 (setq entry-found (or entry-found diary-entry)))))
1693 entry-found)) 1699 entry-found))
1700
1701
1702 ;;; Sexp diary functions.
1694 1703
1695 (defvar date) 1704 (defvar date)
1696 (defvar entry) 1705 (defvar entry)
1697 1706
1698 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound. 1707 ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
1910 ;; Diary entry may apply to one of a list of days before date. 1919 ;; Diary entry may apply to one of a list of days before date.
1911 ((and (listp days) days) 1920 ((and (listp days) days)
1912 (or (diary-remind sexp (car days) marking) 1921 (or (diary-remind sexp (car days) marking)
1913 (diary-remind sexp (cdr days) marking)))))) 1922 (diary-remind sexp (cdr days) marking))))))
1914 1923
1915 (defun diary-redraw-calendar () 1924
1916 "If `calendar-buffer' is live and diary entries are marked, redraw it." 1925 ;;; Diary insertion functions.
1917 (and mark-diary-entries-in-calendar
1918 (save-excursion
1919 (redraw-calendar)))
1920 ;; Return value suitable for `write-contents-functions'.
1921 nil)
1922 1926
1923 ;;;###cal-autoload 1927 ;;;###cal-autoload
1924 (defun make-diary-entry (string &optional nonmarking file) 1928 (defun make-diary-entry (string &optional nonmarking file)
1925 "Insert a diary entry STRING which may be NONMARKING in FILE. 1929 "Insert a diary entry STRING which may be NONMARKING in FILE.
1926 If omitted, NONMARKING defaults to nil and FILE defaults to 1930 If omitted, NONMARKING defaults to nil and FILE defaults to
1937 (max (- (point-max) 3000) (point-min)) 1941 (max (- (point-max) 3000) (point-min))
1938 t)) 1942 t))
1939 (beginning-of-line) 1943 (beginning-of-line)
1940 (insert "\n") 1944 (insert "\n")
1941 (forward-line -1)) 1945 (forward-line -1))
1942
1943 (insert 1946 (insert
1944 (if (bolp) "" "\n") 1947 (if (bolp) "" "\n")
1945 (if nonmarking diary-nonmarking-symbol "") 1948 (if nonmarking diary-nonmarking-symbol "")
1946 string " ")) 1949 string " "))
1947 1950
2042 (lambda (x) (> x 0))) 2045 (lambda (x) (> x 0)))
2043 (calendar-date-string (calendar-cursor-to-date t) nil t)) 2046 (calendar-date-string (calendar-cursor-to-date t) nil t))
2044 arg))) 2047 arg)))
2045 2048
2046 ;;; Diary mode. 2049 ;;; Diary mode.
2050
2051 (defun diary-redraw-calendar ()
2052 "If `calendar-buffer' is live and diary entries are marked, redraw it."
2053 (and mark-diary-entries-in-calendar
2054 (save-excursion
2055 (redraw-calendar)))
2056 ;; Return value suitable for `write-contents-functions'.
2057 nil)
2047 2058
2048 (defvar diary-mode-map 2059 (defvar diary-mode-map
2049 (let ((map (make-sparse-keymap))) 2060 (let ((map (make-sparse-keymap)))
2050 (define-key map "\C-c\C-s" 'diary-show-all-entries) 2061 (define-key map "\C-c\C-s" 'diary-show-all-entries)
2051 (define-key map "\C-c\C-q" 'quit-window) 2062 (define-key map "\C-c\C-q" 'quit-window)