1 ;;; Todos.el --- facilities for making and maintaining Todo lists
3 ;; Copyright (C) 1997, 1999, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Oliver Seidel <privat@os10000.net>
6 ;; Stephen Berman <stephen.berman@gmx.net>
7 ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
9 ;; Keywords: calendar, todo
11 ;; This file is [not yet] part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; For remove-duplicates in todos-insertion-commands-args.
32 (eval-when-compile (require 'cl))
34 ;; ---------------------------------------------------------------------------
38 "Create and maintain categorized lists of todo items."
39 :link '(emacs-commentary-link "todos")
43 (defcustom todos-files-directory (locate-user-emacs-file "todos/")
44 "Directory where user's Todos files are saved."
48 (defun todos-files (&optional archives)
49 "Default value of `todos-files-function'.
50 This returns the case-insensitive alphabetically sorted list of
51 file truenames in `todos-files-directory' with the extension
52 \".todo\". With non-nil ARCHIVES return the list of archive file
53 truenames (those with the extension \".toda\")."
54 (let ((files (if (file-exists-p todos-files-directory)
55 (mapcar 'file-truename
56 (directory-files todos-files-directory t
57 (if archives "\.toda$" "\.todo$") t)))))
58 (sort files (lambda (s1 s2) (let ((cis1 (upcase s1))
60 (string< cis1 cis2))))))
62 (defcustom todos-files-function 'todos-files
63 "Function returning the value of the variable `todos-files'.
64 This function should take an optional argument that, if non-nil,
65 makes it return the value of the variable `todos-archives'."
69 (defun todos-short-file-name (file)
70 "Return short form of Todos FILE.
71 This lacks the extension and directory components."
72 (file-name-sans-extension (file-name-nondirectory file)))
74 (defcustom todos-default-todos-file (car (funcall todos-files-function))
75 "Todos file visited by first session invocation of `todos-show'."
76 :type `(radio ,@(mapcar (lambda (f) (list 'const f))
77 (mapcar 'todos-short-file-name
78 (funcall todos-files-function))))
81 ;; FIXME: is there a better alternative to this?
82 (defun todos-reevaluate-default-file-defcustom ()
83 "Reevaluate defcustom of `todos-default-todos-file'.
84 Called after adding or deleting a Todos file."
85 (eval (defcustom todos-default-todos-file (car (funcall todos-files-function))
86 "Todos file visited by first session invocation of `todos-show'."
87 :type `(radio ,@(mapcar (lambda (f) (list 'const f))
88 (mapcar 'todos-short-file-name
89 (funcall todos-files-function))))
92 (defcustom todos-show-current-file t
93 "Non-nil to make `todos-show' visit the current Todos file.
94 Otherwise, `todos-show' always visits `todos-default-todos-file'."
96 :initialize 'custom-initialize-default
97 :set 'todos-set-show-current-file
100 (defun todos-set-show-current-file (symbol value)
101 "The :set function for user option `todos-show-current-file'."
102 (custom-set-default symbol value)
104 (add-hook 'pre-command-hook 'todos-show-current-file nil t)
105 (remove-hook 'pre-command-hook 'todos-show-current-file t)))
107 (defcustom todos-visit-files-commands (list 'find-file 'dired-find-file)
108 "List of commands to visit files for `todos-after-find-file'.
109 Invoking these commands to visit a Todos or Todos Archive file
110 calls `todos-show' or `todos-show-archive', so that the file is
111 displayed correctly."
112 :type '(repeat function)
115 (defcustom todos-initial-file "Todo"
116 "Default file name offered on adding first Todos file."
120 (defcustom todos-initial-category "Todo"
121 "Default category name offered on initializing a new Todos file."
125 (defcustom todos-display-categories-first nil
126 "Non-nil to display category list on first visit to a Todos file."
130 (defcustom todos-prefix ""
131 "String prefixed to todo items for visual distinction."
133 :initialize 'custom-initialize-default
134 :set 'todos-reset-prefix
137 (defcustom todos-number-priorities t
138 "Non-nil to prefix items with consecutively increasing integers.
139 These reflect the priorities of the items in each category."
141 :initialize 'custom-initialize-default
142 :set 'todos-reset-prefix
145 (defun todos-reset-prefix (symbol value)
146 "The :set function for `todos-prefix' and `todos-number-priorities'."
147 (let ((oldvalue (symbol-value symbol))
148 (files (append todos-files todos-archives)))
149 (custom-set-default symbol value)
150 (when (not (equal value oldvalue))
152 (with-current-buffer (find-file-noselect f)
153 (save-window-excursion
157 (goto-char (point-min))
159 (remove-overlays (point) (point)); 'before-string prefix)
161 ;; Activate the new setting (save-restriction does not help).
162 (save-excursion (todos-category-select))))))))
164 ;; FIXME: Update when window-width changes. Add todos-reset-separator to
165 ;; window-configuration-change-hook in todos-mode? But this depends on the
166 ;; value being window-width instead of a constant length.
167 (defcustom todos-done-separator (make-string (window-width) ?_)
168 "String used to visually separate done from not done items.
169 Displayed as an overlay instead of `todos-done-separator' when
170 done items are shown."
172 :initialize 'custom-initialize-default
173 :set 'todos-reset-separator
176 ;; (defun todos-reset-done-separator (symbol value)
177 ;; "The :set function for `todos-done-separator'
178 ;; Also added to `window-configuration-change-hook' in Todos mode."
179 ;; (let ((oldvalue (symbol-value symbol)))
180 ;; (custom-set-default symbol value)
181 ;; (when (not (equal value oldvalue))
182 ;; (make-string (window-width) ?_)
183 ;; ;; (save-excursion (todos-category-select))
186 (defcustom todos-done-string "DONE "
187 "Identifying string appended to the front of done todos items."
189 :initialize 'custom-initialize-default
190 :set 'todos-reset-done-string
193 (defun todos-reset-done-string (symbol value)
194 "The :set function for user option `todos-done-string'."
195 (let ((oldvalue (symbol-value symbol))
196 (files (append todos-files todos-archives)))
197 (custom-set-default symbol value)
198 ;; Need to reset this to get font-locking right.
199 (setq todos-done-string-start
200 (concat "^\\[" (regexp-quote todos-done-string)))
201 (when (not (equal value oldvalue))
203 (with-current-buffer (find-file-noselect f)
204 (let (buffer-read-only)
206 (goto-char (point-min))
208 (if (re-search-forward
209 (concat "^" (regexp-quote todos-nondiary-start)
210 "\\(" (regexp-quote oldvalue) "\\)")
212 (replace-match value t t nil 1)
214 (todos-category-select)))))))
216 (defcustom todos-comment-string "COMMENT"
217 "String inserted before optional comment appended to done item."
219 :initialize 'custom-initialize-default
220 :set 'todos-reset-comment-string
223 (defun todos-reset-comment-string (symbol value)
224 "The :set function for user option `todos-comment-string'."
225 (let ((oldvalue (symbol-value symbol))
226 (files (append todos-files todos-archives)))
227 (custom-set-default symbol value)
228 (when (not (equal value oldvalue))
230 (with-current-buffer (find-file-noselect f)
231 (let (buffer-read-only)
234 (goto-char (point-min))
236 (if (re-search-forward
238 "\\[\\(" (regexp-quote oldvalue) "\\): [^]]*\\]")
240 (replace-match value t t nil 1)
242 (todos-category-select))))))))
244 (defcustom todos-show-with-done nil
245 "Non-nil to display done items in all categories."
249 (defun todos-mode-line-control (cat)
250 "Return a mode line control for Todos buffers.
251 Argument CAT is the name of the current Todos category.
252 This function is the value of the user variable
253 `todos-mode-line-function'."
254 (let ((file (todos-short-file-name todos-current-todos-file)))
255 (format "%s category %d: %s" file todos-category-number cat)))
257 (defcustom todos-mode-line-function 'todos-mode-line-control
258 "Function that returns a mode line control for Todos buffers.
259 The function expects one argument holding the name of the current
260 Todos category. The resulting control becomes the local value of
261 `mode-line-buffer-identification' in each Todos buffer."
265 (defun todos-special-buffer-name (buffer-type file-list)
266 "Rename Todos special buffer using BUFFER-TYPE and FILE-LIST.
268 The new name is constructed from the string BUFFER-TYPE, which
269 refers to one of the top priorities, diary or regexp item
270 filters, and the names of the filtered files in FILE-LIST. Used
271 in Todos Filter Items mode."
272 (let* ((flist (if (listp file-list) file-list (list file-list)))
273 (multi (> (length flist) 1))
274 (fnames (mapconcat (lambda (f) (todos-short-file-name f))
276 (rename-buffer (format (concat "%s for file" (if multi "s" "")
277 " \"%s\"") buffer-type fnames))))
279 (defcustom todos-filter-buffer "Todos filtered items"
280 "Initial name of buffer in Todos Filter Items mode."
284 (defcustom todos-top-priorities-buffer "Todos top priorities"
285 "Buffer type string for `todos-special-buffer-name'."
289 (defcustom todos-diary-items-buffer "Todos diary items"
290 "Buffer type string for `todos-special-buffer-name'."
294 (defcustom todos-regexp-items-buffer "Todos regexp items"
295 "Buffer type string for `todos-special-buffer-name'."
299 (defcustom todos-priorities-rules nil
300 "List of rules giving how many items `todos-top-priorities' shows.
301 This variable should be set interactively by
302 `\\[todos-set-top-priorities-in-file]' or
303 `\\[todos-set-top-priorities-in-category]'.
305 Each rule is a list of the form (FILE NUM ALIST), where FILE is a
306 member of `todos-files', NUM is a number specifying the default
307 number of top priority items for each category in that file, and
308 ALIST, when non-nil, consists of conses of a category name in
309 FILE and a number specifying the default number of top priority
310 items in that category, which overrides NUM."
314 (defcustom todos-show-priorities 1
315 "Default number of top priorities shown by `todos-top-priorities'."
319 (defcustom todos-filter-files nil
320 "List of default files for multifile item filtering."
321 :type `(set ,@(mapcar (lambda (f) (list 'const f))
322 (mapcar 'todos-short-file-name
323 (funcall todos-files-function))))
326 ;; FIXME: is there a better alternative to this?
327 (defun todos-reevaluate-filter-files-defcustom ()
328 "Reevaluate defcustom of `todos-filter-files'.
329 Called after adding or deleting a Todos file."
330 (eval (defcustom todos-filter-files nil
331 "List of files for multifile item filtering."
332 :type `(set ,@(mapcar (lambda (f) (list 'const f))
333 (mapcar 'todos-short-file-name
334 (funcall todos-files-function))))
337 (defcustom todos-filter-done-items nil
338 "Non-nil to include done items when processing regexp filters.
339 Done items from corresponding archive files are also included."
343 (defcustom todos-ignore-archived-categories nil
344 "Non-nil to ignore categories with only archived items.
345 When non-nil such categories are omitted from `todos-categories'
346 and hence from commands that use this variable. An exception is
347 \\[todos-display-categories], which displays all categories; but
348 those with only archived items are shown in `todos-archived-only'
349 face and clicking them in Todos Categories mode visits the
350 archived categories."
352 :initialize 'custom-initialize-default
353 :set 'todos-reset-categories
356 (defun todos-reset-categories (symbol value)
357 "The :set function for `todos-ignore-archived-categories'."
358 (custom-set-default symbol value)
359 (dolist (f (funcall todos-files-function))
360 (with-current-buffer (find-file-noselect f)
362 (setq todos-categories-full todos-categories
363 todos-categories (todos-truncate-categories-list))
364 (setq todos-categories todos-categories-full
365 todos-categories-full nil))
366 (todos-category-select))))
368 (defcustom todos-use-only-highlighted-region t
369 "Non-nil to enable inserting only highlighted region as new item."
373 (defcustom todos-include-in-diary nil
374 "Non-nil to allow new Todo items to be included in the diary."
378 (defcustom todos-diary-nonmarking nil
379 "Non-nil to insert new Todo diary items as nonmarking by default.
380 This appends `diary-nonmarking-symbol' to the front of an item on
381 insertion provided it doesn't begin with `todos-nondiary-marker'."
385 (defcustom todos-nondiary-marker '("[" "]")
386 "List of strings surrounding item date to block diary inclusion.
387 The first string is inserted before the item date and must be a
388 non-empty string that does not match a diary date in order to
389 have its intended effect. The second string is inserted after
391 :type '(list string string)
393 :initialize 'custom-initialize-default
394 :set 'todos-reset-nondiary-marker)
396 (defun todos-reset-nondiary-marker (symbol value)
397 "The :set function for user option `todos-nondiary-marker'."
398 (let ((oldvalue (symbol-value symbol))
399 (files (append todos-files todos-archives)))
400 (custom-set-default symbol value)
401 ;; Need to reset these to get font-locking right.
402 (setq todos-nondiary-start (nth 0 todos-nondiary-marker)
403 todos-nondiary-end (nth 1 todos-nondiary-marker)
404 todos-date-string-start
405 ;; See comment in defvar of `todos-date-string-start'.
406 (concat "^\\(" (regexp-quote todos-nondiary-start) "\\|"
407 (regexp-quote diary-nonmarking-symbol) "\\)?"))
408 (when (not (equal value oldvalue))
410 (with-current-buffer (find-file-noselect f)
411 (let (buffer-read-only)
413 (goto-char (point-min))
415 (if (re-search-forward
416 (concat "^\\(" todos-done-string-start "[^][]+] \\)?"
417 "\\(?1:" (regexp-quote (car oldvalue))
418 "\\)" todos-date-pattern "\\( "
419 diary-time-regexp "\\)?\\(?2:"
420 (regexp-quote (cadr oldvalue)) "\\)")
423 (replace-match (nth 0 value) t t nil 1)
424 (replace-match (nth 1 value) t t nil 2))
426 (todos-category-select)))))))
428 (defcustom todos-print-function 'ps-print-buffer-with-faces
429 "Function called to print buffer content; see `todos-print'."
433 (defcustom todos-completion-ignore-case nil
434 "Non-nil means case of user input in `todos-read-*' is ignored."
438 (defcustom todos-highlight-item nil
439 "Non-nil means highlight items at point."
441 :initialize 'custom-initialize-default
442 :set 'todos-reset-highlight-item
445 (defun todos-reset-highlight-item (symbol value)
446 "The :set function for `todos-highlight-item'."
447 (let ((oldvalue (symbol-value symbol))
448 (files (append todos-files todos-archives)))
449 (custom-set-default symbol value)
450 (when (not (equal value oldvalue))
452 (let ((buf (get-file-buffer f)))
454 (with-current-buffer buf
458 (hl-line-mode -1)))))))))
460 (defcustom todos-always-add-time-string nil
461 "Non-nil adds current time to a new item's date header by default.
462 When the Todos insertion commands have a non-nil \"maybe-notime\"
463 argument, this reverses the effect of
464 `todos-always-add-time-string': if t, these commands omit the
465 current time, if nil, they include it."
469 (defcustom todos-wrap-lines t
470 "Non-nil to wrap long lines via `todos-line-wrapping-function'."
474 (defcustom todos-line-wrapping-function 'todos-wrap-and-indent
475 "Line wrapping function used with non-nil `todos-wrap-lines'."
479 (defun todos-wrap-and-indent ()
480 "Use word wrapping on long lines and indent with a wrap prefix.
481 The amount of indentation is given by user option
482 `todos-indent-to-here'."
483 (set (make-local-variable 'word-wrap) t)
484 (set (make-local-variable 'wrap-prefix) (make-string todos-indent-to-here 32))
485 (unless (member '(continuation) fringe-indicator-alist)
486 (push '(continuation) fringe-indicator-alist)))
488 ;; FIXME: :set function (otherwise change takes effect only after killing and
490 (defcustom todos-indent-to-here 6
491 "Number of spaces `todos-line-wrapping-function' indents to."
492 :type '(integer :validate
494 (unless (> (widget-value widget) 0)
495 (widget-put widget :error
496 "Invalid value: must be a positive integer")
500 (defun todos-indent ()
501 "Indent from point to `todos-indent-to-here'."
502 (indent-to todos-indent-to-here todos-indent-to-here))
504 (defcustom todos-todo-mode-date-time-regexp
505 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
506 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
507 "Regexp matching legacy todo-mode.el item date-time strings.
508 In order for `todos-convert-legacy-files' to correctly convert this
509 string to the current Todos format, the regexp must contain four
510 explicitly numbered groups (see `(elisp) Regexp Backslash'),
511 where group 1 matches a string for the year, group 2 a string for
512 the month, group 3 a string for the day and group 4 a string for
513 the time. The default value converts date-time strings built
514 using the default value of `todo-time-string-format' from
519 (defgroup todos-categories nil
520 "Faces for Todos Categories mode."
524 (defcustom todos-categories-category-label "Category"
525 "Category button label in Todos Categories mode."
527 :group 'todos-categories)
529 (defcustom todos-categories-todo-label "Todo"
530 "Todo button label in Todos Categories mode."
532 :group 'todos-categories)
534 (defcustom todos-categories-diary-label "Diary"
535 "Diary button label in Todos Categories mode."
537 :group 'todos-categories)
539 (defcustom todos-categories-done-label "Done"
540 "Done button label in Todos Categories mode."
542 :group 'todos-categories)
544 (defcustom todos-categories-archived-label "Archived"
545 "Archived button label in Todos Categories mode."
547 :group 'todos-categories)
549 (defcustom todos-categories-totals-label "Totals"
550 "String to label total item counts in Todos Categories mode."
552 :group 'todos-categories)
554 (defcustom todos-categories-number-separator " | "
555 "String between number and category in Todos Categories mode.
556 This separates the number from the category name in the default
557 categories display according to priority."
559 :group 'todos-categories)
561 (defcustom todos-categories-align 'center
562 "Alignment of category names in Todos Categories mode."
563 :type '(radio (const left) (const center) (const right))
564 :group 'todos-categories)
566 ;; ---------------------------------------------------------------------------
569 (defgroup todos-faces nil
570 "Faces for the Todos modes."
574 (defface todos-prefix-string
575 ;; '((t :inherit font-lock-constant-face))
576 '((((class grayscale) (background light))
577 (:foreground "LightGray" :weight bold :underline t))
578 (((class grayscale) (background dark))
579 (:foreground "Gray50" :weight bold :underline t))
580 (((class color) (min-colors 88) (background light)) (:foreground "dark cyan"))
581 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
582 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
583 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
584 (((class color) (min-colors 8)) (:foreground "magenta"))
585 (t (:weight bold :underline t)))
586 "Face for Todos prefix string."
590 ;; '((t :inherit font-lock-warning-face))
594 (:weight bold :foreground "Red1"))
598 (:weight bold :foreground "Pink"))
602 (:weight bold :foreground "Red1"))
606 (:weight bold :foreground "Pink"))
611 (:weight bold :inverse-video t)))
612 "Face for marks on Todos items."
615 (defface todos-button
616 ;; '((t :inherit widget-field))
618 (:foreground "black" :background "yellow3"))
619 (((class grayscale color)
621 (:background "gray85"))
622 (((class grayscale color)
624 (:background "dim gray"))
627 "Face for buttons in todos-display-categories."
630 (defface todos-sorted-column
631 ;; '((t :inherit fringe))
634 (:foreground "grey95"))
637 (:foreground "grey10"))
639 (:foreground "gray")))
640 "Face for buttons in todos-display-categories."
643 (defface todos-archived-only
644 ;; '((t (:inherit (shadow))))
647 (:foreground "grey50"))
650 (:foreground "grey70"))
652 (:foreground "gray")))
653 "Face for archived-only categories in todos-display-categories."
656 (defface todos-search
657 ;; '((t :inherit match))
661 (:background "yellow1"))
665 (:background "RoyalBlue3"))
669 (:foreground "black" :background "yellow"))
673 (:foreground "white" :background "blue"))
678 (:background "gray")))
679 "Face for matches found by todos-search."
682 (defface todos-diary-expired
683 ;; '((t :inherit font-lock-warning-face))
686 (:weight bold :foreground "DarkOrange"))
688 (:weight bold :foreground "yellow"))
691 "Face for expired dates of diary items."
693 (defvar todos-diary-expired-face 'todos-diary-expired)
696 '((t :inherit diary))
697 "Face for the date string of a Todos item."
699 (defvar todos-date-face 'todos-date)
702 '((t :inherit diary-time))
703 "Face for the time string of a Todos item."
705 (defvar todos-time-face 'todos-time)
708 ;; '((t :inherit font-lock-comment-face))
709 '((((class grayscale)
711 (:slant italic :weight bold :foreground "DimGray"))
714 (:slant italic :weight bold :foreground "LightGray"))
718 (:foreground "Firebrick"))
722 (:foreground "chocolate1"))
730 (:foreground "red1"))
738 (:foreground "yellow"))
740 (:slant italic :weight bold)))
741 "Face for done Todos item header string."
743 (defvar todos-done-face 'todos-done)
745 (defface todos-comment
746 '((t :inherit todos-done))
747 "Face for comments appended to done Todos items."
749 (defvar todos-comment-face 'todos-comment)
751 (defface todos-done-sep
752 ;; '((t :inherit font-lock-type-face))
753 '((((class grayscale)
755 (:weight bold :foreground "Gray90"))
758 (:weight bold :foreground "DimGray"))
762 (:foreground "ForestGreen"))
766 (:foreground "PaleGreen"))
770 (:foreground "ForestGreen"))
774 (:foreground "PaleGreen"))
777 (:foreground "green"))
779 (:underline t :weight bold)))
780 "Face for separator string bewteen done and not done Todos items."
782 (defvar todos-done-sep-face 'todos-done-sep)
784 (defun todos-date-string-matcher (lim)
785 "Search for Todos date string within LIM for font-locking."
787 (concat todos-date-string-start "\\(?1:" todos-date-pattern "\\)") lim t))
789 (defun todos-time-string-matcher (lim)
790 "Search for Todos time string within LIM for font-locking."
791 (re-search-forward (concat todos-date-string-start todos-date-pattern
792 " \\(?1:" diary-time-regexp "\\)") lim t))
794 (defun todos-nondiary-marker-matcher (lim)
795 "Search for Todos nondiary markers within LIM for font-locking."
796 (re-search-forward (concat "^\\(?1:" (regexp-quote todos-nondiary-start) "\\)"
797 todos-date-pattern "\\(?: " diary-time-regexp
798 "\\)?\\(?2:" (regexp-quote todos-nondiary-end) "\\)")
801 (defun todos-diary-nonmarking-matcher (lim)
802 "Search for diary nonmarking symbol within LIM for font-locking."
803 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol)
804 "\\)" todos-date-pattern) lim t))
806 (defun todos-diary-expired-matcher (lim)
807 "Search for expired diary item date within LIM for font-locking."
808 (when (re-search-forward (concat "^\\(?:"
809 (regexp-quote diary-nonmarking-symbol)
810 "\\)?\\(?1:" todos-date-pattern "\\) \\(?2:"
811 diary-time-regexp "\\)?") lim t)
812 (let* ((date (match-string-no-properties 1))
813 (time (match-string-no-properties 2))
814 ;; days-between needs a non-empty time string.
815 (date-time (concat date " " (or time "00:00"))))
816 (or (and (not (string-match ".+day\\|\\*" date))
817 (< (days-between date-time (current-time-string)) 0))
818 (todos-diary-expired-matcher lim)))))
820 (defun todos-done-string-matcher (lim)
821 "Search for Todos done header within LIM for font-locking."
822 (re-search-forward (concat todos-done-string-start
826 (defun todos-comment-string-matcher (lim)
827 "Search for Todos done comment within LIM for font-locking."
828 (re-search-forward (concat "\\[\\(?1:" todos-comment-string "\\):")
831 ;; (defun todos-category-string-matcher (lim)
832 ;; "Search for Todos category name within LIM for font-locking.
833 ;; This is for fontifying category names appearing in Todos filter
835 ;; (if (eq major-mode 'todos-filter-items-mode)
836 ;; (re-search-forward
837 ;; (concat "^\\(?:" todos-date-string-start "\\)?" todos-date-pattern
838 ;; "\\(?: " diary-time-regexp "\\)?\\(?:"
839 ;; (regexp-quote todos-nondiary-end) "\\)? \\(?1:\\[.+\\]\\)")
842 (defun todos-category-string-matcher-1 (lim)
843 "Search for Todos category name within LIM for font-locking.
844 This is for fontifying category names appearing in Todos filter
845 mode following done items."
846 (if (eq major-mode 'todos-filter-items-mode)
847 (re-search-forward (concat todos-done-string-start todos-date-pattern
848 "\\(?: " diary-time-regexp
849 ;; Use non-greedy operator to prevent
850 ;; capturing possible following non-diary
852 "\\)?] \\(?1:\\[.+?\\]\\)")
855 (defun todos-category-string-matcher-2 (lim)
856 "Search for Todos category name within LIM for font-locking.
857 This is for fontifying category names appearing in Todos filter
858 mode following todo (not done) items."
859 (if (eq major-mode 'todos-filter-items-mode)
860 (re-search-forward (concat todos-date-string-start todos-date-pattern
861 "\\(?: " diary-time-regexp "\\)?\\(?:"
862 (regexp-quote todos-nondiary-end)
863 "\\)? \\(?1:\\[.+\\]\\)")
866 (defvar todos-font-lock-keywords
868 '(todos-nondiary-marker-matcher 1 todos-done-sep-face t)
869 '(todos-nondiary-marker-matcher 2 todos-done-sep-face t)
870 ;; This is the face used by diary-lib.el.
871 '(todos-diary-nonmarking-matcher 1 font-lock-constant-face t)
872 '(todos-date-string-matcher 1 todos-date-face t)
873 '(todos-time-string-matcher 1 todos-time-face t)
874 '(todos-done-string-matcher 0 todos-done-face t)
875 '(todos-comment-string-matcher 1 todos-done-face t)
876 ;; '(todos-category-string-matcher 1 todos-done-sep-face t)
877 '(todos-category-string-matcher-1 1 todos-done-sep-face t t)
878 '(todos-category-string-matcher-2 1 todos-done-sep-face t t)
879 '(todos-diary-expired-matcher 1 todos-diary-expired-face t)
880 '(todos-diary-expired-matcher 2 todos-diary-expired-face t t)
882 "Font-locking for Todos modes.")
884 ;; ---------------------------------------------------------------------------
885 ;;; Todos mode local variables and hook functions
887 (defvar todos-files (funcall todos-files-function)
888 "List of truenames of user's Todos files.")
890 (defvar todos-archives (funcall todos-files-function t)
891 "List of truenames of user's Todos archives.")
893 (defvar todos-current-todos-file nil
894 "Variable holding the name of the currently active Todos file.")
896 (defun todos-show-current-file ()
897 "Visit current instead of default Todos file with `todos-show'.
898 This function is added to `pre-command-hook' when user option
899 `todos-show-current-file' is set to non-nil."
900 (setq todos-global-current-todos-file todos-current-todos-file))
902 (defun todos-after-find-file ()
903 "Show Todos files correctly when visited from outside of Todos mode."
904 (and (member this-command todos-visit-files-commands)
905 (= (- (point-max) (point-min)) (buffer-size))
906 (member major-mode '(todos-mode todos-archive-mode))
907 (todos-category-select)))
909 (defun todos-reset-global-current-todos-file ()
910 "Update the value of `todos-global-current-todos-file'.
911 This becomes the latest existing Todos file or, if there is none,
912 the value of `todos-default-todos-file'.
913 This function is added to `kill-buffer-hook' in Todos mode."
914 (let ((buflist (copy-sequence (buffer-list)))
915 (cur todos-global-current-todos-file))
918 (let* ((buf (pop buflist))
919 (bufname (buffer-file-name buf)))
920 (when bufname (setq bufname (file-truename bufname)))
921 (when (and (member bufname todos-files)
922 (not (eq buf (current-buffer))))
923 (setq todos-global-current-todos-file bufname)
924 (throw 'done nil)))))
925 (if (equal cur todos-global-current-todos-file)
926 (setq todos-global-current-todos-file todos-default-todos-file))))
928 (defvar todos-categories nil
929 "Alist of categories in the current Todos file.
930 The elements are cons cells whose car is a category name and
931 whose cdr is a vector of the category's item counts. These are,
932 in order, the numbers of todo items, of todo items included in
933 the Diary, of done items and of archived items.")
935 (defvar todos-categories-full nil
936 "Variable holding non-truncated copy of `todos-categories'.
937 Set when `todos-ignore-archived-categories' is set to non-nil, to
938 restore full `todos-categories' list when
939 `todos-ignore-archived-categories' is reset to nil.")
941 (defvar todos-categories-with-marks nil
942 "Alist of categories and number of marked items they contain.")
944 (defvar todos-category-number 1
945 "Variable holding the number of the current Todos category.
946 Todos categories are numbered starting from 1.")
948 (defvar todos-first-visit t
949 "Non-nil if first display of this file in the current session.
950 See `todos-display-categories-first'.")
952 (defvar todos-show-done-only nil
953 "If non-nil display only done items in current category.
954 Set by the command `todos-show-done-only' and used by
955 `todos-category-select'.")
957 ;; ---------------------------------------------------------------------------
958 ;;; Global variables and helper functions
960 (defvar todos-global-current-todos-file nil
961 "Variable holding name of current Todos file.
962 Used by functions called from outside of Todos mode to visit the
963 current Todos file rather than the default Todos file (i.e. when
964 users option `todos-show-current-file' is non-nil).")
966 (defun todos-reevaluate-defcustoms ()
967 "Reevaluate defcustoms that provide choice list of Todos files."
968 (custom-set-default 'todos-default-todos-file
969 (symbol-value 'todos-default-todos-file))
970 (todos-reevaluate-default-file-defcustom)
971 (custom-set-default 'todos-filter-files (symbol-value 'todos-filter-files))
972 (todos-reevaluate-filter-files-defcustom))
974 (defvar todos-edit-buffer "*Todos Edit*"
975 "Name of current buffer in Todos Edit mode.")
977 (defvar todos-categories-buffer "*Todos Categories*"
978 "Name of buffer in Todos Categories mode.")
980 (defvar todos-print-buffer "*Todos Print*"
981 "Name of buffer containing printable Todos text.")
983 (defvar todos-date-pattern
984 (let ((dayname (diary-name-pattern calendar-day-name-array nil t)))
985 (concat "\\(?:" dayname "\\|"
987 ;; FIXME: how to choose between abbreviated and unabbreviated
989 (monthname (format "\\(?:%s\\|\\*\\)"
991 calendar-month-name-array
992 calendar-month-abbrev-array t)))
993 (month "\\(?:[0-9]+\\|\\*\\)")
994 (day "\\(?:[0-9]+\\|\\*\\)")
995 (year "-?\\(?:[0-9]+\\|\\*\\)"))
996 (mapconcat 'eval calendar-date-display-form ""))
998 "Regular expression matching a Todos date header.")
1000 (defvar todos-nondiary-start (nth 0 todos-nondiary-marker)
1001 "String inserted before item date to block diary inclusion.")
1003 (defvar todos-nondiary-end (nth 1 todos-nondiary-marker)
1004 "String inserted after item date matching `todos-nondiary-start'.")
1006 ;; By itself this matches anything, because of the `?'; however, it's only
1007 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
1009 (defvar todos-date-string-start
1010 (concat "^\\(" (regexp-quote todos-nondiary-start) "\\|"
1011 (regexp-quote diary-nonmarking-symbol) "\\)?")
1012 "Regular expression matching part of item header before the date.")
1014 (defvar todos-done-string-start
1015 (concat "^\\[" (regexp-quote todos-done-string))
1016 "Regular expression matching start of done item.")
1018 (defun todos-category-number (cat)
1019 "Return the number of category CAT in this Todos file.
1020 The buffer-local variable `todos-category-number' holds this
1021 number as its value."
1022 (let ((categories (mapcar 'car todos-categories)))
1023 (setq todos-category-number
1024 ;; Increment by one, so that the highest priority category in Todos
1025 ;; Categories mode is numbered one rather than zero.
1026 (1+ (- (length categories)
1027 (length (member cat categories)))))))
1029 (defun todos-current-category ()
1030 "Return the name of the current category."
1031 (car (nth (1- todos-category-number) todos-categories)))
1033 (defconst todos-category-beg "--==-- "
1034 "String marking beginning of category (inserted with its name).")
1036 (defconst todos-category-done "==--== DONE "
1037 "String marking beginning of category's done items.")
1039 (defun todos-category-select ()
1040 "Display the current category correctly."
1041 (let ((name (todos-current-category))
1042 cat-begin cat-end done-start done-sep-start done-end)
1044 (goto-char (point-min))
1046 (concat "^" (regexp-quote (concat todos-category-beg name)) "$") nil t)
1047 (setq cat-begin (1+ (line-end-position)))
1048 (setq cat-end (if (re-search-forward
1049 (concat "^" (regexp-quote todos-category-beg)) nil t)
1052 (setq mode-line-buffer-identification
1053 (funcall todos-mode-line-function name))
1054 (narrow-to-region cat-begin cat-end)
1055 (todos-prefix-overlays)
1056 (goto-char (point-min))
1057 (if (re-search-forward (concat "\n\\(" (regexp-quote todos-category-done)
1060 (setq done-start (match-beginning 0))
1061 (setq done-sep-start (match-beginning 1))
1062 (setq done-end (match-end 0)))
1063 (error "Category %s is missing todos-category-done string" name))
1064 (if todos-show-done-only
1065 (narrow-to-region (1+ done-end) (point-max))
1066 (when (and todos-show-with-done
1067 (re-search-forward todos-done-string-start nil t))
1068 ;; Now we want to see the done items, so reset displayed end to end of
1070 (setq done-start cat-end)
1071 ;; Make display overlay for done items separator string, unless there
1073 (let* ((done-sep todos-done-separator)
1074 (ovs (overlays-at done-sep-start))
1076 (unless (and ovs (string= (overlay-get (car ovs) 'display) done-sep))
1077 (setq ov-sep (make-overlay done-sep-start done-end))
1078 (overlay-put ov-sep 'display done-sep))))
1079 (narrow-to-region (point-min) done-start)
1080 ;; Loading this from todos-mode, or adding it to the mode hook, causes
1081 ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
1082 (when todos-highlight-item
1084 (hl-line-mode 1)))))
1086 (defun todos-get-count (type &optional category)
1087 "Return count of TYPE items in CATEGORY.
1088 If CATEGORY is nil, default to the current category."
1089 (let* ((cat (or category (todos-current-category)))
1090 (counts (cdr (assoc cat todos-categories)))
1091 (idx (cond ((eq type 'todo) 0)
1092 ((eq type 'diary) 1)
1094 ((eq type 'archived) 3))))
1097 (defun todos-update-count (type increment &optional category)
1098 "Change count of TYPE items in CATEGORY by integer INCREMENT.
1099 With nil or omitted CATEGORY, default to the current category."
1100 (let* ((cat (or category (todos-current-category)))
1101 (counts (cdr (assoc cat todos-categories)))
1102 (idx (cond ((eq type 'todo) 0)
1103 ((eq type 'diary) 1)
1105 ((eq type 'archived) 3))))
1106 (aset counts idx (+ increment (aref counts idx)))))
1108 (defun todos-set-categories ()
1109 "Set `todos-categories' from the sexp at the top of the file."
1110 ;; New archive files created by `todos-move-category' are empty, which would
1111 ;; make the sexp test fail and raise an error, so in this case we skip it.
1112 (unless (zerop (buffer-size))
1116 (goto-char (point-min))
1117 ;; todos-truncate-categories-list needs non-nil todos-categories.
1118 (setq todos-categories-full
1119 (if (looking-at "\(\(\"")
1120 (read (buffer-substring-no-properties
1121 (line-beginning-position)
1122 (line-end-position)))
1123 (error "Invalid or missing todos-categories sexp"))
1124 todos-categories todos-categories-full)))
1125 (if (and todos-ignore-archived-categories
1126 (eq major-mode 'todos-mode))
1127 (todos-truncate-categories-list)
1128 todos-categories-full)))
1130 (defun todos-update-categories-sexp ()
1131 "Update the `todos-categories' sexp at the top of the file."
1132 (let (buffer-read-only)
1136 (goto-char (point-min))
1137 (if (looking-at (concat "^" (regexp-quote todos-category-beg)))
1138 (progn (newline) (goto-char (point-min)))
1139 ;; With empty buffer (e.g. with new archive in
1140 ;; `todos-move-category') `kill-line' signals end of buffer.
1141 (kill-region (line-beginning-position) (line-end-position)))
1142 ;; todos-categories-full is nil on adding first category.
1143 (prin1 (or todos-categories-full todos-categories)
1144 (current-buffer))))))
1146 (defun todos-make-categories-list (&optional force)
1147 "Return an alist of Todos categories and their item counts.
1148 With non-nil argument FORCE parse the entire file to build the
1149 list; otherwise, get the value by reading the sexp at the top of
1151 (setq todos-categories nil)
1155 (goto-char (point-min))
1156 (let (counts cat archive)
1157 (when buffer-file-name ; Don't check with `todos-convert-legacy-files'.
1158 ;; FIXME: can todos-archives be too old here?
1159 (unless (member buffer-file-name (funcall todos-files-function t))
1160 (setq archive (concat (file-name-sans-extension
1161 todos-current-todos-file) ".toda"))))
1163 (cond ((looking-at (concat (regexp-quote todos-category-beg)
1165 (setq cat (match-string-no-properties 1))
1166 ;; Counts for each category: [todo diary done archive]
1167 (setq counts (make-vector 4 0))
1168 (setq todos-categories
1169 (append todos-categories (list (cons cat counts))))
1170 ;; todos-archives may be too old here (e.g. during
1171 ;; todos-move-category).
1172 (when (member archive (funcall todos-files-function t))
1173 (let ((archive-count 0))
1174 (with-current-buffer (find-file-noselect archive)
1176 (goto-char (point-min))
1177 (when (re-search-forward
1178 (concat (regexp-quote todos-category-beg) cat)
1181 (while (not (or (looking-at
1183 (regexp-quote todos-category-beg)
1186 (when (looking-at todos-done-string-start)
1187 (setq archive-count (1+ archive-count)))
1189 (todos-update-count 'archived archive-count cat))))
1190 ((looking-at todos-done-string-start)
1191 (todos-update-count 'done 1 cat))
1192 ((looking-at (concat "^\\("
1193 (regexp-quote diary-nonmarking-symbol)
1194 "\\)?" todos-date-pattern))
1195 (todos-update-count 'diary 1 cat)
1196 (todos-update-count 'todo 1 cat))
1197 ((looking-at (concat todos-date-string-start todos-date-pattern))
1198 (todos-update-count 'todo 1 cat))
1199 ;; If first line is todos-categories list, use it and end loop
1200 ;; -- unless FORCEd to scan whole file.
1203 (setq todos-categories (read (buffer-substring-no-properties
1204 (line-beginning-position)
1205 (line-end-position))))
1206 (goto-char (1- (point-max))))))
1210 (defun todos-truncate-categories-list ()
1211 "Return a truncated alist of Todos categories plus item counts.
1212 Categories containing only archived items are omitted. This list
1213 is used in Todos mode when `todos-ignore-archived-categories' is
1216 (dolist (catcons todos-categories-full cats)
1217 (let ((cat (car catcons)))
1220 (unless (and (zerop (todos-get-count 'todo cat))
1221 (zerop (todos-get-count 'done cat))
1222 (not (zerop (todos-get-count 'archived cat))))
1223 (list catcons))))))))
1225 (defun todos-check-format ()
1226 "Signal an error if the current Todos file is ill-formatted.
1227 Otherwise return t. The error message gives the line number
1228 where the invalid formatting was found."
1232 (goto-char (point-min))
1233 ;; Check for `todos-categories' sexp as the first line
1234 (let ((cats (prin1-to-string (or todos-categories-full todos-categories))))
1235 (unless (looking-at (regexp-quote cats))
1236 (error "Invalid or missing todos-categories sexp")))
1238 (let ((legit (concat "\\(^" (regexp-quote todos-category-beg) "\\)"
1239 "\\|\\(" todos-date-string-start todos-date-pattern "\\)"
1240 "\\|\\(^[ \t]+[^ \t]*\\)"
1242 "\\|\\(^" (regexp-quote todos-category-done) "\\)"
1243 "\\|\\(" todos-done-string-start "\\)")))
1245 (unless (looking-at legit)
1246 (error "Illegitimate Todos file format at line %d"
1247 (line-number-at-pos (point))))
1249 ;; (message "This Todos file is well-formatted.")
1252 (defun todos-repair-categories-sexp ()
1253 "Repair corrupt Todos categories sexp.
1254 This should only be needed as a consequence of careless manual
1255 editing or a bug in todos.el."
1257 (let ((todos-categories-full (todos-make-categories-list t)))
1258 (todos-update-categories-sexp)))
1260 (defvar todos-item-start (concat "\\(" todos-date-string-start "\\|"
1261 todos-done-string-start "\\)"
1263 "String identifying start of a Todos item.")
1265 (defun todos-item-start ()
1266 "Move to start of current Todos item and return its position."
1268 ;; Point is either on last item in this category or on the empty
1269 ;; line between done and not done items.
1271 ;; There are no done items in this category yet.
1272 (looking-at (regexp-quote todos-category-beg)))
1273 (goto-char (line-beginning-position))
1274 (while (not (looking-at todos-item-start))
1278 (defun todos-item-end ()
1279 "Move to end of current Todos item and return its position."
1280 ;; Items cannot end with a blank line.
1281 (unless (looking-at "^$")
1282 (let ((done (todos-done-item-p)))
1283 (todos-forward-item)
1284 ;; Adjust if item is last unfinished one before displayed done items.
1285 (when (and (not done) (todos-done-item-p))
1290 (defun todos-item-string ()
1291 "Return bare text of current item as a string."
1292 (let ((opoint (point))
1293 (start (todos-item-start))
1294 (end (todos-item-end)))
1296 (and start end (buffer-substring-no-properties start end))))
1298 (defun todos-remove-item ()
1299 "Internal function called in editing, deleting or moving items."
1300 (let* ((beg (todos-item-start))
1301 (end (progn (todos-item-end) (1+ (point))))
1302 (ovs (overlays-in beg beg)))
1303 ;; There can be both prefix/number and mark overlays.
1304 (while ovs (delete-overlay (car ovs)) (pop ovs))
1305 (delete-region beg end)))
1307 (defun todos-diary-item-p ()
1308 "Return non-nil if item at point has diary entry format."
1311 (not (looking-at (regexp-quote todos-nondiary-start)))))
1313 (defun todos-done-item-p ()
1314 "Return non-nil if item at point is a done item."
1317 (looking-at todos-done-string-start)))
1319 (defvar todos-item-mark (propertize (if (equal todos-prefix "*") "@" "*")
1321 "String used to mark items.")
1323 (defun todos-marked-item-p ()
1324 "If this item begins with `todos-item-mark', return mark overlay."
1325 (let ((ovs (overlays-in (line-beginning-position) (line-beginning-position)))
1326 (mark todos-item-mark)
1331 (and (equal (overlay-get ov 'before-string) mark)
1332 (throw 'stop (setq marked t)))))
1335 (defun todos-insert-with-overlays (item)
1336 "Insert ITEM at point and update prefix/priority number overlays."
1339 (todos-backward-item)
1340 (todos-prefix-overlays))
1342 (defun todos-prefix-overlays ()
1343 "Put before-string overlay in front of this category's items.
1344 The overlay's value is the string `todos-prefix' or with non-nil
1345 `todos-number-priorities' an integer in the sequence from 1 to
1346 the number of todo or done items in the category indicating the
1347 item's priority. Todo and done items are numbered independently
1349 (when (or todos-number-priorities
1350 (not (string-match "^[[:space:]]*$" todos-prefix)))
1351 (let ((prefix (propertize (concat todos-prefix " ")
1352 'face 'todos-prefix-string))
1355 (goto-char (point-min))
1357 (when (or (todos-date-string-matcher (line-end-position))
1358 (todos-done-string-matcher (line-end-position)))
1359 (goto-char (match-beginning 0))
1360 (when todos-number-priorities
1362 ;; Reset number to 1 for first done item.
1363 (when (and (looking-at todos-done-string-start)
1364 (looking-back (concat "^"
1365 (regexp-quote todos-category-done)
1368 (setq prefix (propertize (concat (number-to-string num) " ")
1369 'face 'todos-prefix-string)))
1370 (let ((ovs (overlays-in (point) (point)))
1374 (let ((val (overlay-get ov 'before-string)))
1377 (setq ov-pref val)))))
1378 (unless (equal ov-pref prefix)
1379 ;; Why doesn't this work?
1380 ;; (remove-overlays (point) (point) 'before-string)
1381 (remove-overlays (point) (point))
1382 (overlay-put (make-overlay (point) (point))
1383 'before-string prefix)
1384 (and marked (overlay-put (make-overlay (point) (point))
1385 'before-string todos-item-mark)))))
1388 (defun todos-read-file-name (prompt &optional archive mustmatch)
1389 "Choose and return the name of a Todos file, prompting with PROMPT.
1391 Show completions with TAB or SPC; the names are shown in short
1392 form but the absolute truename is returned. With non-nil ARCHIVE
1393 return the absolute truename of a Todos archive file. With non-nil
1394 MUSTMATCH the name of an existing file must be chosen;
1395 otherwise, a new file name is allowed."
1396 (unless (file-exists-p todos-files-directory)
1397 (make-directory todos-files-directory))
1398 (let ((completion-ignore-case todos-completion-ignore-case)
1399 (files (mapcar 'file-name-sans-extension
1400 (directory-files todos-files-directory nil
1401 (if archive "\.toda$" "\.todo$"))))
1403 (while (string= "" file)
1404 (setq file (completing-read prompt files nil mustmatch))
1405 (setq prompt "Enter a non-empty name (TAB for list of current files): "))
1406 (setq file (concat todos-files-directory file
1407 (if archive ".toda" ".todo")))
1409 (when (not (member file todos-files))
1410 (todos-validate-name file 'file)))
1411 (file-truename file)))
1413 (defun todos-read-category (prompt &optional mustmatch)
1414 "Choose and return a category name, prompting with PROMPT.
1415 Show completions with TAB or SPC. With non-nil MUSTMATCH the
1416 name must be that of an existing category; otherwise, a new
1417 category name is allowed, after checking its validity."
1418 ;; Allow SPC to insert spaces, for adding new category names.
1419 (let ((map minibuffer-local-completion-map))
1420 (define-key map " " nil)
1421 ;; Make a copy of todos-categories in case history-delete-duplicates is
1422 ;; non-nil, which makes completing-read alter todos-categories.
1423 (let* ((categories (copy-sequence todos-categories))
1424 (history (cons 'todos-categories (1+ todos-category-number)))
1425 (completion-ignore-case todos-completion-ignore-case)
1426 (cat (completing-read prompt todos-categories nil
1427 mustmatch nil history
1428 ;; Default for existing categories is the
1429 ;; current category.
1430 (if todos-categories
1431 (todos-current-category)
1432 ;; Trigger prompt for initial category
1435 (when (not (assoc cat categories))
1436 (todos-validate-name cat 'category)
1437 (if (y-or-n-p (format (concat "There is no category \"%s\" in "
1438 "this file; add it? ") cat))
1439 (todos-add-category cat)
1441 ;; Restore the original value of todos-categories.
1442 (setq todos-categories categories)
1445 (defun todos-validate-name (name type)
1446 "Prompt for new NAME for TYPE until it is valid, then return it.
1447 TYPE can be either a file or a category"
1448 (let (prompt file cat shortname)
1450 (and (cond ((string= "" name)
1452 (cond ((eq type 'file)
1453 ;; FIXME: just todos-files ?
1454 (if (funcall (todos-files))
1455 "Enter a non-empty file name: "
1456 ;; Empty string passed by todos-show to
1457 ;; prompt for initial Todos file.
1458 (concat "Initial file name ["
1459 todos-initial-file "]: ")))
1460 ((eq type 'category)
1461 (if todos-categories
1462 "Enter a non-empty category name: "
1463 ;; Empty string passed by todos-show to
1464 ;; prompt for initial category of a new
1466 (concat "Initial category name ["
1467 todos-initial-category "]: "))))))
1468 ((string-match "\\`\\s-+\\'" name)
1470 "Enter a name that does not contain only white space: "))
1471 ((and (eq type 'file) (member name todos-files))
1472 (setq prompt "Enter a non-existing file name: "))
1473 ((and (eq type 'category) (assoc name todos-categories))
1474 (setq prompt "Enter a non-existing category name: ")))
1475 (setq name (if (or (and (eq type 'file) todos-files)
1476 (and (eq type 'category) todos-categories))
1477 (read-from-minibuffer prompt)
1478 ;; Offer default initial name.
1479 (read-string prompt nil nil
1480 (cond ((eq type 'file)
1482 ((eq type 'category)
1483 todos-initial-category))))))))
1486 ;; Adapted from calendar-read-date and calendar-date-string.
1487 (defun todos-read-date ()
1488 "Prompt for Gregorian date and return it in the current format.
1489 Also accepts `*' as an unspecified month, day, or year."
1490 (let* ((year (calendar-read
1491 ;; FIXME: maybe better like monthname with RET for current month
1492 "Year (>0 or * for any year): "
1493 (lambda (x) (or (eq x '*) (> x 0)))
1494 (number-to-string (calendar-extract-year
1495 (calendar-current-date)))))
1496 (month-array (vconcat calendar-month-name-array (vector "*")))
1497 (abbrevs (vconcat calendar-month-abbrev-array (vector "*")))
1498 (completion-ignore-case todos-completion-ignore-case)
1499 (monthname (completing-read
1500 "Month name (RET for current month, * for any month): "
1501 (mapcar 'list (append month-array nil))
1503 (calendar-month-name (calendar-extract-month
1504 (calendar-current-date)) t)))
1505 (month (cdr (assoc-string
1506 monthname (calendar-make-alist month-array nil nil
1508 (last (if (= month 13)
1509 31 ; FIXME: what about shorter months?
1510 (let ((yr (if (eq year '*)
1511 1999 ; FIXME: no Feb. 29
1513 (calendar-last-day-of-month month yr))))
1515 (while (if (numberp day) (or (< day 0) (< last day)) (not (eq day '*)))
1516 (setq day (read-from-minibuffer
1517 (format "Day (1-%d or RET for today or * for any day): " last)
1520 (calendar-extract-day (calendar-current-date))))))
1521 (setq year (if (eq year '*) (symbol-name '*) (number-to-string year)))
1522 (setq day (if (eq day '*) (symbol-name '*) (number-to-string day)))
1523 ;; FIXME: make abbreviation customizable
1525 (or (and (= month 13) "*")
1526 (calendar-month-name (calendar-extract-month (list month day year))
1528 (mapconcat 'eval calendar-date-display-form "")))
1530 (defun todos-read-dayname ()
1531 "Choose name of a day of the week with completion and return it."
1532 (let ((completion-ignore-case todos-completion-ignore-case))
1533 (completing-read "Enter a day name: "
1534 (append calendar-day-name-array nil)
1537 (defun todos-read-time ()
1538 "Prompt for and return a valid clock time as a string.
1540 Valid time strings are those matching `diary-time-regexp'.
1541 Typing `<return>' at the prompt returns the current time, if the
1542 user option `todos-always-add-time-string' is non-nil, otherwise
1543 the empty string (i.e., no time string)."
1546 (setq answer (read-string "Enter a clock time: " nil nil
1547 (when todos-always-add-time-string
1548 (substring (current-time-string) 11 16))))
1549 (when (or (string= "" answer)
1550 (string-match diary-time-regexp answer))
1554 (defun todos-convert-legacy-date-time ()
1555 "Return converted date-time string.
1556 Helper function for `todos-convert-legacy-files'."
1557 (let* ((year (match-string 1))
1558 (month (match-string 2))
1559 (monthname (calendar-month-name (string-to-number month) t))
1560 (day (match-string 3))
1561 (time (match-string 4))
1564 (insert (mapconcat 'eval calendar-date-display-form "")
1565 (when time (concat " " time)))))
1567 ;; ---------------------------------------------------------------------------
1570 (defvar todos-multiple-files nil
1571 "List of files selected from `todos-multiple-files' widget.")
1573 (defvar todos-multiple-files-widget nil
1574 "Variable holding widget created by `todos-multiple-files'.")
1576 (defun todos-multiple-files ()
1577 "Pop to a buffer with a widget for choosing multiple filter files."
1580 (require 'wid-edit))
1581 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
1582 (pop-to-buffer (current-buffer))
1584 (kill-all-local-variables)
1585 (widget-insert "Select files for generating the top priorities list.\n\n")
1586 (setq todos-multiple-files-widget
1588 `(set ,@(mapcar (lambda (x) (list 'const x))
1589 (mapcar 'todos-short-file-name
1590 (funcall todos-files-function))))))
1591 (widget-insert "\n")
1592 (widget-create 'push-button
1593 :notify (lambda (widget &rest ignore)
1594 (setq todos-multiple-files 'quit)
1596 (exit-recursive-edit))
1599 (widget-create 'push-button
1600 :notify (lambda (&rest ignore)
1601 (setq todos-multiple-files
1603 (concat todos-files-directory
1606 todos-multiple-files-widget)))
1608 (exit-recursive-edit))
1610 (use-local-map widget-keymap)
1612 (message "Click \"Apply\" after selecting files.")
1615 (defun todos-filter-items (filter &optional multifile)
1616 "Build and display a list of items from different categories.
1618 The items are selected according to the value of FILTER, which
1619 can be `top' for top priority items, `diary' for diary items,
1620 `regexp' for items matching a regular expresion entered by the
1621 user, or a cons cell of one of these symbols and a number set by
1622 the calling command, which overrides `todos-show-priorities'.
1624 With non-nil argument MULTIFILE list top priorities of multiple
1625 Todos files, by default those in `todos-filter-files'."
1626 (let ((num (if (consp filter) (cdr filter) todos-show-priorities))
1627 (buf (get-buffer-create todos-filter-buffer))
1628 (files (list todos-current-todos-file))
1629 regexp fname bufstr cat beg end done)
1631 (setq files (or todos-multiple-files ; Passed from todos-*-multifile.
1632 (if (or (consp filter)
1633 (null todos-filter-files))
1634 (progn (todos-multiple-files) todos-multiple-files)
1635 todos-filter-files))
1636 todos-multiple-files nil))
1637 (if (eq files 'quit) (keyboard-quit))
1639 (error "No files have been chosen for filtering")
1640 (with-current-buffer buf
1642 (kill-all-local-variables)
1643 (todos-filter-items-mode))
1644 (when (eq filter 'regexp)
1645 (setq regexp (read-string "Enter a regular expression: ")))
1646 (save-current-buffer
1648 ;; Before inserting file contents into temp buffer, save a modified
1649 ;; buffer visiting it.
1650 (let ((bf (find-buffer-visiting f)))
1651 (when (buffer-modified-p bf)
1652 (with-current-buffer bf (save-buffer))))
1653 (setq fname (todos-short-file-name f))
1655 (when (and todos-filter-done-items (eq filter 'regexp))
1656 ;; If there is a corresponding archive file for the Todos file,
1657 ;; insert it first and add identifiers for todos-jump-to-item.
1658 (let ((arch (concat (file-name-sans-extension f) ".toda")))
1659 (when (file-exists-p arch)
1660 (insert-file-contents arch)
1661 ;; Delete Todos archive file categories sexp.
1662 (delete-region (line-beginning-position)
1663 (1+ (line-end-position)))
1666 (when (re-search-forward
1667 (concat (if todos-filter-done-items
1668 (concat "\\(?:" todos-done-string-start
1669 "\\|" todos-date-string-start
1671 todos-date-string-start)
1672 todos-date-pattern "\\(?: "
1673 diary-time-regexp "\\)?"
1674 (if todos-filter-done-items
1676 (regexp-quote todos-nondiary-end)) "?")
1678 (insert "(archive) "))
1680 (insert-file-contents f)
1681 ;; Delete Todos file categories sexp.
1682 (delete-region (line-beginning-position) (1+ (line-end-position)))
1684 ;; Unless the number of items to show was supplied by prefix
1685 ;; argument of caller, override `todos-show-priorities' with the
1686 ;; file-wide value from `todos-priorities-rules'.
1687 (unless (consp filter)
1688 (setq fnum (nth 1 (assoc f todos-priorities-rules))))
1689 (while (re-search-forward
1690 (concat "^" (regexp-quote todos-category-beg) "\\(.+\\)\n")
1692 (setq cat (match-string 1))
1694 ;; Unless the number of items to show was supplied by prefix
1695 ;; argument of caller, override the file-wide value from
1696 ;; `todos-priorities-rules' if set, else
1697 ;; `todos-show-priorities' with non-nil category-wide value
1698 ;; from `todos-priorities-rules'.
1699 (unless (consp filter)
1700 (let ((cats (nth 2 (assoc f todos-priorities-rules))))
1701 (setq cnum (or (cdr (assoc cat cats))
1703 ;; FIXME: need this?
1704 todos-show-priorities))))
1705 (delete-region (match-beginning 0) (match-end 0))
1706 (setq beg (point)) ; First item in the current category.
1707 (setq end (if (re-search-forward
1708 (concat "^" (regexp-quote todos-category-beg))
1714 (if (re-search-forward
1715 (concat "\n" (regexp-quote todos-category-done))
1719 (unless (and todos-filter-done-items (eq filter 'regexp))
1720 ;; Leave done items.
1721 (delete-region done end)
1723 (narrow-to-region beg end) ; Process only current category.
1724 (goto-char (point-min))
1725 ;; Apply the filter.
1726 (cond ((eq filter 'diary)
1728 (if (looking-at (regexp-quote todos-nondiary-start))
1730 (todos-forward-item))))
1731 ((eq filter 'regexp)
1733 (if (looking-at todos-item-start)
1734 (if (string-match regexp (todos-item-string))
1735 (todos-forward-item)
1736 (todos-remove-item))
1737 ;; Kill lines that aren't part of a todo or done
1738 ;; item (empty or todos-category-done).
1739 (delete-region (line-beginning-position)
1740 (1+ (line-end-position))))
1741 ;; If last todo item in file matches regexp and
1742 ;; there are no following done items,
1743 ;; todos-category-done string is left dangling,
1744 ;; because todos-forward-item jumps over it.
1747 (concat (regexp-quote todos-done-string)
1749 (delete-region (point) (progn
1752 (t ; Filter top priority items.
1753 (setq num (or cnum fnum num))
1755 (todos-forward-item num))))
1757 ;; Delete non-top-priority items.
1758 (unless (member filter '(diary regexp))
1759 (delete-region beg end))
1760 (goto-char (point-min))
1761 ;; Add file (if using multiple files) and category tags to
1764 (when (re-search-forward
1765 (concat (if todos-filter-done-items
1766 (concat "\\(?:" todos-done-string-start
1767 "\\|" todos-date-string-start
1769 todos-date-string-start)
1770 todos-date-pattern "\\(?: " diary-time-regexp
1771 "\\)?" (if todos-filter-done-items
1773 (regexp-quote todos-nondiary-end))
1777 (when (looking-at "(archive) ") (goto-char (match-end 0)))
1778 (insert (if multifile (concat fname ":") "") cat "]"))
1781 (setq bufstr (buffer-string))
1782 (with-current-buffer buf
1783 (let (buffer-read-only)
1784 (insert bufstr)))))))
1785 (set-window-buffer (selected-window) (set-buffer buf))
1786 (todos-prefix-overlays)
1787 (goto-char (point-min)))))
1789 (defun todos-set-top-priorities (&optional arg)
1790 "Set number of top priorities shown by `todos-top-priorities'.
1791 With non-nil ARG, set the number only for the current Todos
1792 category; otherwise, set the number for all categories in the
1795 Calling this function via either of the commands
1796 `todos-set-top-priorities-in-file' or
1797 `todos-set-top-priorities-in-category' is the recommended way to
1798 set the user customizable option `todos-priorities-rules'."
1799 (let* ((cat (todos-current-category))
1800 (file todos-current-todos-file)
1801 (rules todos-priorities-rules)
1802 (frule (assoc-string file rules))
1803 (crule (assoc-string cat (nth 2 frule)))
1804 (cur (or (if arg (cdr crule) (nth 1 frule))
1805 todos-show-priorities))
1806 (prompt (concat "Current number of top priorities in this "
1807 (if arg "category" "file") ": %d; "
1808 "enter new number: "))
1811 (while (or (not (string-match "[0-9]+" new)) ; Don't accept "" or "bla".
1812 (< (string-to-number new) 0))
1814 (setq new (read-string (format prompt cur0) nil nil cur0)
1815 prompt "Enter a non-negative number: "
1817 (setq new (string-to-number new))
1819 (append (nth 2 (delete crule frule)) (list (cons cat new)))
1820 (append (list file new) (list (nth 2 frule)))))
1821 (setq rules (cons (if arg
1822 (list file cur nrule)
1824 (delete frule rules)))
1825 (customize-save-variable 'todos-priorities-rules rules)))
1828 ;; ---------------------------------------------------------------------------
1829 ;;; Sorting and display routines for Todos Categories mode.
1831 (defun todos-longest-category-name-length (categories)
1832 "Return the length of the longest name in list CATEGORIES."
1834 (dolist (c categories longest)
1835 (setq longest (max longest (length c))))))
1837 (defun todos-padded-string (str)
1838 "Return string STR padded with spaces.
1839 The placement of the padding is determined by the value of user
1840 option `todos-categories-align'."
1841 (let* ((categories (mapcar 'car todos-categories))
1842 (len (max (todos-longest-category-name-length categories)
1843 (length todos-categories-category-label)))
1844 (strlen (length str))
1845 (strlen-odd (eq (logand strlen 1) 1)) ; oddp from cl.el
1846 (padding (max 0 (/ (- len strlen) 2)))
1847 (padding-left (cond ((eq todos-categories-align 'left) 0)
1848 ((eq todos-categories-align 'center) padding)
1849 ((eq todos-categories-align 'right)
1850 (if strlen-odd (1+ (* padding 2)) (* padding 2)))))
1851 (padding-right (cond ((eq todos-categories-align 'left)
1852 (if strlen-odd (1+ (* padding 2)) (* padding 2)))
1853 ((eq todos-categories-align 'center)
1854 (if strlen-odd (1+ padding) padding))
1855 ((eq todos-categories-align 'right) 0))))
1856 (concat (make-string padding-left 32) str (make-string padding-right 32))))
1858 (defvar todos-descending-counts nil
1859 "List of keys for category counts sorted in descending order.")
1861 (defun todos-sort (list &optional key)
1862 "Return a copy of LIST, possibly sorted according to KEY."
1863 (let* ((l (copy-sequence list))
1864 (fn (if (eq key 'alpha)
1865 (lambda (x) (upcase x)) ; Alphabetize case insensitively.
1866 (lambda (x) (todos-get-count key x))))
1867 (descending (member key todos-descending-counts))
1868 (cmp (if (eq key 'alpha)
1870 (if descending '< '>)))
1871 (pred (lambda (s1 s2) (let ((t1 (funcall fn (car s1)))
1872 (t2 (funcall fn (car s2))))
1873 (funcall cmp t1 t2)))))
1875 (setq l (sort l pred))
1877 (setq todos-descending-counts
1878 (delete key todos-descending-counts))
1879 (push key todos-descending-counts)))
1882 (defun todos-display-sorted (type)
1883 "Keep point on the TYPE count sorting button just clicked."
1884 (let ((opoint (point)))
1885 (todos-update-categories-display type)
1886 (goto-char opoint)))
1888 (defun todos-label-to-key (label)
1889 "Return symbol for sort key associated with LABEL."
1891 (cond ((string= label todos-categories-category-label)
1893 ((string= label todos-categories-todo-label)
1895 ((string= label todos-categories-diary-label)
1897 ((string= label todos-categories-done-label)
1899 ((string= label todos-categories-archived-label)
1900 (setq key 'archived)))
1903 (defun todos-insert-sort-button (label)
1904 "Insert button for displaying categories sorted by item counts.
1905 LABEL determines which type of count is sorted."
1906 (setq str (if (string= label todos-categories-category-label)
1907 (todos-padded-string label)
1910 (setq end (+ beg (length str)))
1911 (insert-button str 'face nil
1914 (let ((key (todos-label-to-key ,label)))
1915 (if (and (member key todos-descending-counts)
1918 ;; If display is alphabetical, switch back to
1920 (todos-display-sorted nil)
1921 (setq todos-descending-counts
1922 (delete key todos-descending-counts)))
1923 (todos-display-sorted key)))))
1924 (setq ovl (make-overlay beg end))
1925 (overlay-put ovl 'face 'todos-button))
1927 (defun todos-total-item-counts ()
1928 "Return a list of total item counts for the current file."
1929 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i))
1930 (mapcar 'cdr todos-categories))))
1933 (defun todos-insert-category-line (cat &optional nonum)
1934 "Insert button displaying category CAT's name and item counts.
1935 With non-nil argument NONUM show only these; otherwise, insert a
1936 number in front of the button indicating the category's priority.
1937 The number and the category name are separated by the string
1938 which is the value of the user option
1939 `todos-categories-number-separator'."
1940 (let* ((archive (member todos-current-todos-file todos-archives))
1941 (str (todos-padded-string cat))
1943 ;; num is declared in caller.
1947 (make-string (+ 4 (length todos-categories-number-separator))
1949 (format " %3d%s" num todos-categories-number-separator))
1951 (mapconcat (lambda (elt)
1953 (make-string (1+ (/ (length (car elt)) 2)) 32) ; label
1954 (format "%3d" (todos-get-count (cdr elt) cat)) ; count
1955 ;; Add an extra space if label length is odd
1956 ;; (using def of oddp from cl.el).
1957 (if (eq (logand (length (car elt)) 1) 1) " ")))
1959 (list (cons todos-categories-done-label 'done))
1960 (list (cons todos-categories-todo-label 'todo)
1961 (cons todos-categories-diary-label 'diary)
1962 (cons todos-categories-done-label 'done)
1963 (cons todos-categories-archived-label
1966 'face (if (and todos-ignore-archived-categories
1967 (zerop (todos-get-count 'todo cat))
1968 (zerop (todos-get-count 'done cat))
1969 (not (zerop (todos-get-count 'archived cat))))
1970 'todos-archived-only
1972 'action `(lambda (button) (let ((buf (current-buffer)))
1973 (todos-jump-to-category ,cat)
1974 (kill-buffer buf))))
1975 ;; Highlight the sorted count column.
1976 (let* ((beg (+ opoint 6 (length str)))
1978 (cond ((eq nonum 'todo)
1979 (setq beg (+ beg 1 (/ (length todos-categories-todo-label) 2))))
1981 (setq beg (+ beg 1 (length todos-categories-todo-label)
1982 2 (/ (length todos-categories-diary-label) 2))))
1984 (setq beg (+ beg 1 (length todos-categories-todo-label)
1985 2 (length todos-categories-diary-label)
1986 2 (/ (length todos-categories-done-label) 2))))
1987 ((eq nonum 'archived)
1988 (setq beg (+ beg 1 (length todos-categories-todo-label)
1989 2 (length todos-categories-diary-label)
1990 2 (length todos-categories-done-label)
1991 2 (/ (length todos-categories-archived-label) 2)))))
1992 (unless (= beg (+ opoint 6 (length str)))
1993 (setq end (+ beg 4))
1994 (setq ovl (make-overlay beg end))
1995 (overlay-put ovl 'face 'todos-sorted-column)))
1998 (defun todos-display-categories-1 ()
1999 "Prepare buffer for displaying table of categories and item counts."
2000 (unless (eq major-mode 'todos-categories-mode)
2001 (setq todos-global-current-todos-file (or todos-current-todos-file
2002 todos-default-todos-file))
2003 (set-window-buffer (selected-window)
2004 (set-buffer (get-buffer-create todos-categories-buffer)))
2005 (kill-all-local-variables)
2006 (todos-categories-mode)
2007 (let (buffer-read-only)
2009 ;; FIXME: add usage tips?
2010 (insert (format "Category counts for Todos file \"%s\"."
2011 (todos-short-file-name todos-current-todos-file)))
2013 ;; Make space for the column of category numbers.
2014 (insert (make-string (+ 4 (length todos-categories-number-separator)) 32))
2015 ;; Add the category and item count buttons (if this is the list of
2016 ;; categories in an archive, show only done item counts).
2017 (todos-insert-sort-button todos-categories-category-label)
2018 (if (member todos-current-todos-file todos-archives)
2019 (insert (concat (make-string 6 32)
2020 (format "%s" todos-categories-archived-label)))
2021 (insert (make-string 3 32))
2022 (todos-insert-sort-button todos-categories-todo-label)
2023 (insert (make-string 2 32))
2024 (todos-insert-sort-button todos-categories-diary-label)
2025 (insert (make-string 2 32))
2026 (todos-insert-sort-button todos-categories-done-label)
2027 (insert (make-string 2 32))
2028 (todos-insert-sort-button todos-categories-archived-label))
2031 (defun todos-update-categories-display (sortkey)
2033 (let* ((cats0 (if (and todos-ignore-archived-categories
2034 (not (eq major-mode 'todos-categories-mode)))
2035 todos-categories-full
2037 (cats (todos-sort cats0 sortkey))
2038 (archive (member todos-current-todos-file todos-archives))
2039 ;; `num' is used by todos-insert-category-line.
2041 ;; Find start of Category button if we just entered Todos Categories
2043 (pt (if (eq (point) (point-max))
2046 (goto-char (next-single-char-property-change
2047 (point) 'face nil (line-end-position))))))
2050 (delete-region (point) (point-max))
2051 ;; Fill in the table with buttonized lines, each showing a category and
2053 (mapc (lambda (cat) (todos-insert-category-line cat sortkey))
2056 ;; Add a line showing item count totals.
2057 (insert (make-string (+ 4 (length todos-categories-number-separator)) 32)
2058 (todos-padded-string todos-categories-totals-label)
2062 (make-string (1+ (/ (length (car elt)) 2)) 32)
2063 (format "%3d" (nth (cdr elt) (todos-total-item-counts)))
2064 ;; Add an extra space if label length is odd (using
2065 ;; definition of oddp from cl.el).
2066 (if (eq (logand (length (car elt)) 1) 1) " ")))
2068 (list (cons todos-categories-done-label 2))
2069 (list (cons todos-categories-todo-label 0)
2070 (cons todos-categories-diary-label 1)
2071 (cons todos-categories-done-label 2)
2072 (cons todos-categories-archived-label 3)))
2074 ;; Put cursor on Category button initially.
2075 (if pt (goto-char pt))
2076 (setq buffer-read-only t)))
2078 ;; ---------------------------------------------------------------------------
2079 ;;; Todos insertion commands, key bindings and keymap
2081 ;; Can either of these be included in Emacs? The originals are GFDL'd.
2082 ;; Slightly reformulated from
2083 ;; http://rosettacode.org/wiki/Power_set#Common_Lisp.
2084 (defun powerset-recursive (l)
2088 (let ((prev (powerset-recursive (cdr l))))
2089 (append (mapcar (lambda (elt) (cons (car l) elt))
2092 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C
2093 (defun powerset-bitwise (l)
2094 (let ((binnum (lsh 1 (length l)))
2099 (while (not (zerop bits))
2100 (let ((arg (pop ll)))
2101 (unless (zerop (logand bits 1))
2102 (setq elt (append elt (list arg))))
2103 (setq bits (lsh bits -1))))
2104 (setq pset (append pset (list elt)))
2108 ;; (defalias 'todos-powerset 'powerset-recursive)
2109 (defalias 'todos-powerset 'powerset-bitwise)
2111 ;; Return list of lists of non-nil atoms produced from ARGLIST. The elements
2112 ;; of ARGLIST may be atoms or lists.
2113 (defun todos-gen-arglists (arglist)
2116 (let ((arg (pop arglist)))
2117 (cond ((symbolp arg)
2118 (setq arglists (if arglists
2119 (mapcar (lambda (l) (push arg l)) arglists)
2120 (list (push arg arglists)))))
2124 (if (= 1 (length arglists))
2125 (apply (lambda (l) (push a l)) arglists)
2126 (mapcar (lambda (l) (push a l)) arglists)))
2128 (setq arglists (mapcar 'reverse (apply 'append (mapc 'car arglists))))))
2130 (defvar todos-insertion-commands-args-genlist
2131 '(diary nonmarking (calendar date dayname) time (here region))
2132 "Generator list for argument lists of Todos insertion commands.")
2134 (defvar todos-insertion-commands-args
2135 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist))
2137 (setq res (remove-duplicates
2138 (apply 'append (mapcar 'todos-powerset argslist)) :test 'equal))
2140 (unless (= 5 (length l))
2141 (let ((v (make-vector 5 nil)) elt)
2144 (cond ((eq elt 'diary)
2146 ((eq elt 'nonmarking)
2148 ((or (eq elt 'calendar)
2157 (setq l (append v nil))))
2158 (setq new (append new (list l))))
2160 "List of all argument lists for Todos insertion commands.")
2162 (defun todos-insertion-command-name (arglist)
2163 "Generate Todos insertion command name from ARGLIST."
2164 (replace-regexp-in-string
2166 (replace-regexp-in-string
2168 (concat "todos-item-insert-"
2169 (mapconcat (lambda (e) (if e (symbol-name e))) arglist "-")))))
2171 (defvar todos-insertion-commands-names
2173 (todos-insertion-command-name l))
2174 todos-insertion-commands-args)
2175 "List of names of Todos insertion commands.")
2177 (defmacro todos-define-insertion-command (&rest args)
2178 (let ((name (intern (todos-insertion-command-name args)))
2183 (arg4 (nth 4 args)))
2184 `(defun ,name (&optional arg)
2185 "Todos item insertion command generated from ARGS."
2187 (todos-insert-item arg ',arg0 ',arg1 ',arg2 ',arg3 ',arg4))))
2189 (defvar todos-insertion-commands
2191 (eval `(todos-define-insertion-command ,@c)))
2192 todos-insertion-commands-args)
2193 "List of Todos insertion commands.")
2195 (defvar todos-insertion-commands-arg-key-list
2196 '(("diary" "y" "yy")
2197 ("nonmarking" "k" "kk")
2198 ("calendar" "c" "cc")
2200 ("dayname" "n" "nn")
2206 (defun todos-insertion-key-bindings (map)
2208 (dolist (c todos-insertion-commands)
2210 (cname (symbol-name c)))
2212 (let ((arg (nth 0 l))
2215 (if (string-match (concat (regexp-quote arg) "\\_>") cname)
2216 (setq key (concat key key2)))
2217 (if (string-match (concat (regexp-quote arg) ".+") cname)
2218 (setq key (concat key key1)))))
2219 todos-insertion-commands-arg-key-list)
2220 (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname)
2221 (setq key (concat key "i")))
2222 (define-key map key c))))
2224 (defvar todos-insertion-map
2225 (let ((map (make-keymap)))
2226 (todos-insertion-key-bindings map)
2228 "Keymap for Todos mode insertion commands.")
2230 ;; ??FIXME: use easy-mmode-define-keymap and easy-mmode-defmap
2231 (defvar todos-key-bindings
2234 ("Cd" . todos-display-categories) ;FIXME: Cs todos-show-categories?
2235 ;("" . todos-display-categories-alphabetically)
2236 ("H" . todos-highlight-item)
2237 ("N" . todos-hide-show-item-numbering)
2238 ("D" . todos-hide-show-date-time)
2239 ("*" . todos-mark-unmark-item)
2240 ("C*" . todos-mark-category)
2241 ("Cu" . todos-unmark-category)
2242 ("PP" . todos-print)
2243 ("PF" . todos-print-to-file)
2244 ("v" . todos-hide-show-done-items)
2245 ("V" . todos-show-done-only)
2246 ("As" . todos-show-archive)
2247 ("Ac" . todos-choose-archive)
2248 ("Y" . todos-diary-items)
2249 ;;("" . todos-update-filter-files)
2250 ("Fe" . todos-edit-multiline)
2251 ("Fh" . todos-highlight-item)
2252 ("Fn" . todos-hide-show-item-numbering)
2253 ("Fd" . todos-hide-show-date-time)
2254 ("Ftt" . todos-top-priorities)
2255 ("Ftm" . todos-top-priorities-multifile)
2256 ("Fts" . todos-set-top-priorities-in-file)
2257 ("Cts" . todos-set-top-priorities-in-category)
2258 ("Fyy" . todos-diary-items)
2259 ("Fym" . todos-diary-items-multifile)
2260 ("Fxx" . todos-regexp-items)
2261 ("Fxm" . todos-regexp-items-multifile)
2262 ;;("" . todos-save-top-priorities)
2264 ("f" . todos-forward-category)
2265 ("b" . todos-backward-category)
2266 ("j" . todos-jump-to-category)
2267 ("J" . todos-jump-to-category-other-file)
2268 ("n" . todos-forward-item)
2269 ("p" . todos-backward-item)
2270 ("S" . todos-search)
2271 ("X" . todos-clear-matches)
2273 ("Fa" . todos-add-file)
2274 ("Ca" . todos-add-category)
2275 ("Cr" . todos-rename-category)
2276 ("Cg" . todos-merge-category)
2277 ;;("" . todos-merge-categories)
2278 ("Cm" . todos-move-category)
2279 ("Ck" . todos-delete-category)
2280 ("d" . todos-item-done)
2281 ("ee" . todos-edit-item)
2282 ("em" . todos-edit-multiline-item)
2283 ("eh" . todos-edit-item-header)
2284 ("edd" . todos-edit-item-date)
2285 ("edc" . todos-edit-item-date-from-calendar)
2286 ("edt" . todos-edit-item-date-is-today)
2287 ("et" . todos-edit-item-time)
2288 ("eyy" . todos-edit-item-diary-inclusion)
2289 ;; ("" . todos-edit-category-diary-inclusion)
2290 ("eyn" . todos-edit-item-diary-nonmarking)
2291 ;;("" . todos-edit-category-diary-nonmarking)
2292 ("ec" . todos-done-item-add-or-edit-comment) ;FIXME: or just "c"?
2293 ("i" . ,todos-insertion-map)
2294 ("k" . todos-delete-item)
2295 ("m" . todos-move-item)
2296 ("M" . todos-move-item-to-file)
2297 ;; FIXME: This binding prevents `-' from being used in a numerical prefix
2298 ;; argument without typing C-u
2299 ;; ("-" . todos-raise-item-priority)
2300 ("r" . todos-raise-item-priority)
2301 ;; ("+" . todos-lower-item-priority)
2302 ("l" . todos-lower-item-priority)
2303 ("#" . todos-set-item-priority)
2304 ("u" . todos-item-undo)
2305 ("Ad" . todos-archive-done-item-or-items) ;FIXME
2306 ("AD" . todos-archive-category-done-items) ;FIXME
2307 ("Au" . todos-unarchive-items)
2308 ("AU" . todos-unarchive-category)
2311 ([remap newline] . newline-and-indent)
2313 "Alist pairing keys defined in Todos modes and their bindings.")
2315 (defvar todos-mode-map
2316 (let ((map (make-keymap)))
2317 ;; Don't suppress digit keys, so they can supply prefix arguments.
2318 (suppress-keymap map)
2319 (dolist (ck todos-key-bindings)
2320 (define-key map (car ck) (cdr ck)))
2322 "Todos mode keymap.")
2326 todos-menu todos-mode-map "Todos Menu"
2329 ["Next Item" todos-forward-item t]
2330 ["Previous Item" todos-backward-item t]
2332 ["Next Category" todos-forward-category t]
2333 ["Previous Category" todos-backward-category t]
2334 ["Jump to Category" todos-jump-to-category t]
2335 ["Jump to Category in Other File" todos-jump-to-category-other-file t]
2337 ["Search Todos File" todos-search t]
2338 ["Clear Highlighting on Search Matches" todos-category-done t])
2340 ["List Current Categories" todos-display-categories t]
2341 ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
2342 ["Turn Item Highlighting on/off" todos-highlight-item t]
2343 ["Turn Item Numbering on/off" todos-hide-show-item-numbering t]
2344 ["Turn Item Time Stamp on/off" todos-hide-show-date-time t]
2345 ["View/Hide Done Items" todos-hide-show-done-items t]
2347 ["View Diary Items" todos-diary-items t]
2348 ["View Top Priority Items" todos-top-priorities t]
2349 ["View Multifile Top Priority Items" todos-top-priorities-multifile t]
2351 ["Print Category" todos-print t])
2353 ["Insert New Item" todos-insert-item t]
2354 ["Insert Item Here" todos-insert-item-here t]
2355 ("More Insertion Commands")
2356 ["Edit Item" todos-edit-item t]
2357 ["Edit Multiline Item" todos-edit-multiline t]
2358 ["Edit Item Header" todos-edit-item-header t]
2359 ["Edit Item Date" todos-edit-item-date t]
2360 ["Edit Item Time" todos-edit-item-time t]
2362 ["Lower Item Priority" todos-lower-item-priority t]
2363 ["Raise Item Priority" todos-raise-item-priority t]
2364 ["Set Item Priority" todos-set-item-priority t]
2365 ["Move (Recategorize) Item" todos-move-item t]
2366 ["Delete Item" todos-delete-item t]
2367 ["Undo Done Item" todos-item-undo t]
2368 ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t]
2369 ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t]
2370 ["Mark & Hide Done Item" todos-item-done t]
2371 ["Archive Done Items" todos-archive-category-done-items t]
2373 ["Add New Todos File" todos-add-file t]
2374 ["Add New Category" todos-add-category t]
2375 ["Delete Current Category" todos-delete-category t]
2376 ["Rename Current Category" todos-rename-category t]
2378 ["Save Todos File" todos-save t]
2379 ["Save Top Priorities" todos-save-top-priorities t])
2381 ["Quit" todos-quit t]
2384 (defvar todos-archive-mode-map
2385 (let ((map (make-sparse-keymap)))
2386 (suppress-keymap map t)
2387 ;; navigation commands
2388 (define-key map "f" 'todos-forward-category)
2389 (define-key map "b" 'todos-backward-category)
2390 (define-key map "j" 'todos-jump-to-category)
2391 (define-key map "n" 'todos-forward-item)
2392 (define-key map "p" 'todos-backward-item)
2394 (define-key map "C" 'todos-display-categories)
2395 (define-key map "H" 'todos-highlight-item)
2396 (define-key map "N" 'todos-hide-show-item-numbering)
2397 ;; (define-key map "" 'todos-hide-show-date-time)
2398 (define-key map "P" 'todos-print)
2399 (define-key map "q" 'todos-quit)
2400 (define-key map "s" 'todos-save)
2401 (define-key map "S" 'todos-search)
2402 (define-key map "t" 'todos-show) ;FIXME: should show same category
2403 (define-key map "u" 'todos-unarchive-item)
2404 (define-key map "U" 'todos-unarchive-category)
2406 "Todos Archive mode keymap.")
2408 (defvar todos-edit-mode-map
2409 (let ((map (make-sparse-keymap)))
2410 (define-key map "\C-x\C-q" 'todos-edit-quit)
2411 (define-key map [remap newline] 'newline-and-indent)
2413 "Todos Edit mode keymap.")
2415 (defvar todos-categories-mode-map
2416 (let ((map (make-sparse-keymap)))
2417 (suppress-keymap map t)
2418 ;; (define-key map "a" 'todos-display-categories-alphabetically)
2419 (define-key map "c" 'todos-display-categories)
2420 (define-key map "l" 'todos-lower-category)
2421 (define-key map "+" 'todos-lower-category)
2422 (define-key map "r" 'todos-raise-category)
2423 (define-key map "-" 'todos-raise-category)
2424 (define-key map "n" 'forward-button)
2425 (define-key map "p" 'backward-button)
2426 (define-key map [tab] 'forward-button)
2427 (define-key map [backtab] 'backward-button)
2428 (define-key map "q" 'todos-quit)
2429 ;; (define-key map "A" 'todos-add-category)
2430 ;; (define-key map "D" 'todos-delete-category)
2431 ;; (define-key map "R" 'todos-rename-category)
2433 "Todos Categories mode keymap.")
2435 (defvar todos-filter-items-mode-map
2436 (let ((map (make-keymap)))
2437 (suppress-keymap map t)
2438 ;; navigation commands
2439 (define-key map "j" 'todos-jump-to-item)
2440 (define-key map [remap newline] 'todos-jump-to-item)
2441 (define-key map "n" 'todos-forward-item)
2442 (define-key map "p" 'todos-backward-item)
2443 (define-key map "H" 'todos-highlight-item)
2444 (define-key map "N" 'todos-hide-show-item-numbering)
2445 (define-key map "D" 'todos-hide-show-date-time)
2446 (define-key map "P" 'todos-print)
2447 (define-key map "q" 'todos-quit)
2448 (define-key map "s" 'todos-save)
2449 ;; (define-key map "S" 'todos-save-top-priorities)
2451 (define-key map "l" 'todos-lower-item-priority)
2452 (define-key map "r" 'todos-raise-item-priority)
2453 (define-key map "#" 'todos-set-item-priority)
2455 "Todos Top Priorities mode keymap.")
2457 ;; FIXME: remove when part of Emacs
2458 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
2459 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
2461 (defun todos-modes-set-1 ()
2463 (set (make-local-variable 'font-lock-defaults) '(todos-font-lock-keywords t))
2464 (set (make-local-variable 'indent-line-function) 'todos-indent)
2465 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2467 (defun todos-modes-set-2 ()
2469 (add-to-invisibility-spec 'todos)
2470 (setq buffer-read-only t)
2471 (set (make-local-variable 'hl-line-range-function)
2472 (lambda() (when (todos-item-end)
2473 (cons (todos-item-start) (todos-item-end))))))
2475 (defun todos-modes-set-3 ()
2476 (set (make-local-variable 'todos-categories-full) nil)
2477 ;; todos-set-categories also sets todos-categories-full.
2478 (set (make-local-variable 'todos-categories) (todos-set-categories))
2479 (set (make-local-variable 'todos-category-number) 1)
2480 (set (make-local-variable 'todos-first-visit) t)
2481 (add-hook 'post-command-hook 'todos-after-find-file nil t))
2483 (put 'todos-mode 'mode-class 'special)
2485 ;; Autoloading isn't needed if files are identified by auto-mode-alist
2486 ;; ;; As calendar reads included Todos file before todos-mode is loaded.
2488 (define-derived-mode todos-mode special-mode "Todos" ()
2489 "Major mode for displaying, navigating and editing Todo lists.
2492 (easy-menu-add todos-menu)
2496 ;; Initialize todos-current-todos-file.
2497 (when (member (file-truename (buffer-file-name))
2498 (funcall todos-files-function))
2499 (set (make-local-variable 'todos-current-todos-file)
2500 (file-truename (buffer-file-name))))
2501 (set (make-local-variable 'todos-first-visit) t)
2502 (set (make-local-variable 'todos-show-done-only) nil)
2503 (set (make-local-variable 'todos-categoreis-with-marks) nil)
2504 (when todos-show-current-file
2505 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
2506 ;; FIXME: works more or less, but should be tied to the defcustom
2507 (add-hook 'window-configuration-change-hook
2509 (setq todos-done-separator (make-string (window-width) ?_)))
2511 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
2513 ;; FIXME: need this?
2514 (defun todos-unload-hook ()
2516 (remove-hook 'pre-command-hook 'todos-show-current-file t)
2517 (remove-hook 'post-command-hook 'todos-after-find-file t)
2518 (remove-hook 'window-configuration-change-hook
2520 (setq todos-done-separator
2521 (make-string (window-width) ?_))) t)
2522 (remove-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file t))
2524 (put 'todos-archive-mode 'mode-class 'special)
2526 (define-derived-mode todos-archive-mode todos-mode "Todos-Arch" ()
2527 "Major mode for archived Todos categories.
2529 \\{todos-archive-mode-map}"
2533 (set (make-local-variable 'todos-current-todos-file)
2534 (file-truename (buffer-file-name)))
2535 (set (make-local-variable 'todos-show-done-only) t))
2537 (defun todos-mode-external-set ()
2539 (set (make-local-variable 'todos-current-todos-file)
2540 todos-global-current-todos-file)
2541 (let ((cats (with-current-buffer (get-file-buffer todos-current-todos-file)
2542 (if todos-ignore-archived-categories
2543 todos-categories-full
2544 (todos-set-categories)))))
2545 (set (make-local-variable 'todos-categories) cats)))
2547 (define-derived-mode todos-edit-mode text-mode "Todos-Ed" ()
2548 "Major mode for editing multiline Todo items.
2550 \\{todos-edit-mode-map}"{
2552 (todos-mode-external-set))
2554 (put 'todos-categories-mode 'mode-class 'special)
2556 (define-derived-mode todos-categories-mode special-mode "Todos-Cats" ()
2557 "Major mode for displaying and editing Todos categories.
2559 \\{todos-categories-mode-map}"
2560 (todos-mode-external-set))
2562 (put 'todos-filter-mode 'mode-class 'special)
2564 (define-derived-mode todos-filter-items-mode special-mode "Todos-Fltr" ()
2565 "Mode for displaying and reprioritizing top priority Todos.
2567 \\{todos-filter-items-mode-map}"
2569 (todos-modes-set-2))
2571 ;; FIXME: need this?
2572 (defun todos-save ()
2573 "Save the current Todos file."
2576 ;; (if todos-save-top-priorities-too (todos-save-top-priorities))
2579 (defun todos-quit ()
2580 "Exit the current Todos-related buffer.
2581 Depending on the specific mode, this either kills the buffer or
2582 buries it and restores state as needed."
2584 (cond ((eq major-mode 'todos-categories-mode)
2586 (setq todos-descending-counts nil)
2588 ((eq major-mode 'todos-filter-items-mode)
2591 ((member major-mode (list 'todos-mode 'todos-archive-mode))
2592 ;; Have to write previously nonexistant archives to file.
2593 (unless (file-exists-p (buffer-file-name)) (todos-save))
2594 ;; FIXME: or should it save unconditionally?
2598 ;; ---------------------------------------------------------------------------
2599 ;;; Display Commands
2602 (defun todos-show (&optional solicit-file)
2603 "Visit the current Todos file and display one of its categories.
2605 With non-nil prefix argument SOLICIT-FILE ask for file to visit.
2606 Otherwise, the first invocation of this command in a session
2607 visits `todos-default-todos-file' (creating it if it does not yet
2608 exist); subsequent invocations from outside of Todos mode revisit
2609 this file or, if user option `todos-show-current-file' is
2610 non-nil, whichever Todos file was visited last.
2612 The category displayed on initial invocation is the first member
2613 of `todos-categories' for the current Todos file, on subsequent
2614 invocations whichever category was displayed last. If
2615 `todos-display-categories-first' is non-nil, then the first
2616 invocation of `todos-show' displays a clickable listing of the
2617 categories in the current Todos file.
2619 In Todos mode just the category's unfinished todo items are shown
2620 by default. The done items are hidden, but typing
2621 `\\[todos-hide-show-done-items]' displays them below the todo
2622 items. With non-nil user option `todos-show-with-done' both todo
2623 and done items are always shown on visiting a category."
2625 (let ((file (cond (solicit-file
2626 (if (funcall todos-files-function)
2627 (todos-read-file-name "Choose a Todos file to visit: "
2629 (error "There are no Todos files")))
2630 ((eq major-mode 'todos-archive-mode)
2631 ;; FIXME: should it visit same category?
2632 (concat (file-name-sans-extension todos-current-todos-file)
2635 ;; FIXME: If an archive is value of
2636 ;; todos-current-todos-file, todos-show will revisit
2637 ;; rather than the corresponding todo file -- ok or make
2639 (or todos-current-todos-file
2640 (and todos-show-current-file
2641 todos-global-current-todos-file)
2642 todos-default-todos-file
2643 (todos-add-file))))))
2644 (if (and todos-first-visit todos-display-categories-first)
2645 (todos-display-categories)
2646 (set-window-buffer (selected-window)
2647 (set-buffer (find-file-noselect file)))
2648 ;; If no Todos file exists, initialize one.
2649 (if (zerop (buffer-size))
2650 ;; Call with empty category name to get initial prompt.
2651 (setq todos-category-number (todos-add-category "")))
2652 (save-excursion (todos-category-select)))
2653 (setq todos-first-visit nil)))
2655 (defun todos-display-categories ()
2656 "Display a table of the current file's categories and item counts.
2658 In the initial display the categories are numbered, indicating
2659 their current order for navigating by \\[todos-forward-category]
2660 and \\[todos-backward-category]. You can persistantly change the
2661 order of the category at point by typing \\[todos-raise-category]
2662 or \\[todos-lower-category].
2664 The labels above the category names and item counts are buttons,
2665 and clicking these changes the display: sorted by category name
2666 or by the respective item counts (alternately descending or
2667 ascending). In these displays the categories are not numbered
2668 and \\[todos-raise-category] and \\[todos-lower-category] are
2669 disabled. (Programmatically, the sorting is triggered by passing
2670 a non-nil SORTKEY argument.)
2672 In addition, the lines with the category names and item counts
2673 are buttonized, and pressing one of these button jumps to the
2674 category in Todos mode (or Todos Archive mode, for categories
2675 containing only archived items, provided user option
2676 `todos-ignore-archived-categories' is non-nil. These categories
2677 are shown in `todos-archived-only' face."
2679 (todos-display-categories-1)
2681 (todos-update-categories-display sortkey)))
2683 ;; ;; FIXME: make this toggle with todos-display-categories
2684 ;; (defun todos-display-categories-alphabetically ()
2687 ;; (todos-display-sorted 'alpha))
2689 ;; ;; FIXME: provide key bindings for these or delete them
2690 ;; (defun todos-display-categories-sorted-by-todo ()
2693 ;; (todos-display-sorted 'todo))
2695 ;; (defun todos-display-categories-sorted-by-diary ()
2698 ;; (todos-display-sorted 'diary))
2700 ;; (defun todos-display-categories-sorted-by-done ()
2703 ;; (todos-display-sorted 'done))
2705 ;; (defun todos-display-categories-sorted-by-archived ()
2708 ;; (todos-display-sorted 'archived))
2710 (defun todos-hide-show-item-numbering ()
2713 (todos-reset-prefix 'todos-number-priorities (not todos-number-priorities)))
2715 (defun todos-hide-show-done-items ()
2716 "Show hidden or hide visible done items in current category."
2718 (if (zerop (todos-get-count 'done (todos-current-category)))
2719 (message "There are no done items in this category.")
2721 (goto-char (point-min))
2722 (let ((todos-show-with-done (not (re-search-forward
2723 todos-done-string-start nil t))))
2724 (todos-category-select)))))
2726 (defun todos-show-done-only ()
2727 "Switch between displaying only done or only todo items."
2729 (setq todos-show-done-only (not todos-show-done-only))
2730 (todos-category-select))
2732 (defun todos-show-archive (&optional ask)
2733 "Visit the archive of the current Todos category, if it exists.
2734 If the category has no archived items, prompt to visit the
2735 archive anyway. If there is no archive for this file or with
2736 non-nil argument ASK, prompt to visit another archive.
2738 With non-nil argument ASK prompt to choose an archive to visit;
2739 see `todos-choose-archive'. The buffer showing the archive is in
2740 Todos Archive mode. The first visit in a session displays the
2741 first category in the archive, subsequent visits return to the
2742 last category displayed." ;FIXME
2744 (let* ((cat (todos-current-category))
2745 (count (todos-get-count 'archived cat))
2746 (archive (concat (file-name-sans-extension todos-current-todos-file)
2749 (setq place (cond (ask 'other-archive)
2750 ((file-exists-p archive) 'this-archive)
2751 (t (when (y-or-n-p (concat "This file has no archive; "
2752 "visit another archive? "))
2754 (when (eq place 'other-archive)
2755 (setq archive (todos-read-file-name "Choose a Todos archive: " t t)))
2756 (when (and (eq place 'this-archive) (zerop count))
2757 (setq place (when (y-or-n-p
2758 (concat "This category has no archived items;"
2759 " visit archive anyway? "))
2762 (set-window-buffer (selected-window)
2763 (set-buffer (find-file-noselect archive)))
2764 (if (member place '(other-archive other-cat))
2765 (setq todos-category-number 1)
2766 (todos-category-number cat))
2767 (todos-category-select))))
2769 (defun todos-choose-archive ()
2770 "Choose an archive and visit it."
2772 (todos-show-archive t))
2774 (defun todos-highlight-item ()
2775 "Toggle highlighting the todo item the cursor is on."
2782 (defun todos-hide-show-date-time () ;(&optional all)
2783 "Hide or show date-time header of todo items.";; in current category.
2784 ;; With non-nil prefix argument ALL do this in the whole file."
2788 (goto-char (point-min))
2789 (let ((ovs (overlays-in (point) (1+ (point))))
2793 (if (equal (overlay-get ov 'display) "")
2794 (setq ovs nil hidden t)))
2797 (goto-char (point-min));)
2799 (remove-overlays (point-min) (point-max) 'display "")
2801 (when (re-search-forward
2802 (concat todos-date-string-start todos-date-pattern
2803 "\\( " diary-time-regexp "\\)?"
2804 (regexp-quote todos-nondiary-end) "? ")
2806 (unless (save-match-data (todos-done-item-p))
2807 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
2808 (overlay-put ov 'display "")))
2809 (todos-forward-item)))))))
2811 (defun todos-mark-unmark-item (&optional n all)
2812 "Mark item at point if unmarked, or unmark it if marked.
2814 With a positive numerical prefix argument N, change the
2815 markedness of the next N items. With non-nil argument ALL, mark
2816 all visible items in the category (depending on visibility, all
2817 todo and done items, or just todo or just done items).
2819 The mark is the character \"*\" inserted in front of the item's
2820 priority number or the `todos-prefix' string; if `todos-prefix'
2821 is \"*\", then the mark is \"@\"."
2823 (if all (goto-char (point-min)))
2824 (unless (> n 0) (setq n 1))
2826 (while (or (and all (not (eobp)))
2828 (let* ((cat (todos-current-category))
2829 (ov (todos-marked-item-p))
2830 (marked (assoc cat todos-categories-with-marks)))
2831 (if (and ov (not all))
2834 (if (= (cdr marked) 1) ; Deleted last mark in this category.
2835 (setq todos-categories-with-marks
2836 (assq-delete-all cat todos-categories-with-marks))
2837 (setcdr marked (1- (cdr marked)))))
2838 (when (todos-item-start)
2839 (unless (and all (todos-marked-item-p))
2840 (setq ov (make-overlay (point) (point)))
2841 (overlay-put ov 'before-string todos-item-mark)
2843 (setcdr marked (1+ (cdr marked)))
2844 (push (cons cat 1) todos-categories-with-marks))))))
2845 (todos-forward-item)
2848 (defun todos-mark-category ()
2849 "Put the \"*\" mark on all items in this category.
2850 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
2852 (todos-mark-unmark-item 0 t))
2854 (defun todos-unmark-category ()
2855 "Remove the \"*\" mark from all items in this category.
2856 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
2858 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
2859 (setq todos-categories-with-marks
2860 (delq (assoc (todos-current-category) todos-categories-with-marks)
2861 todos-categories-with-marks)))
2863 (defun todos-set-top-priorities-in-file ()
2864 "Set number of top priorities for this file.
2865 See `todos-set-top-priorities' for more details."
2867 (todos-set-top-priorities))
2869 (defun todos-set-top-priorities-in-category ()
2870 "Set number of top priorities for this category.
2871 See `todos-set-top-priorities' for more details."
2873 (todos-set-top-priorities t))
2875 (defun todos-top-priorities (&optional num)
2876 "List top priorities of each category in `todos-filter-files'.
2877 Number of entries for each category is given by NUM, which
2878 defaults to `todos-show-priorities'."
2880 (let ((arg (if num (cons 'top num) 'top))
2881 (buf todos-top-priorities-buffer)
2882 (file todos-current-todos-file))
2883 (todos-filter-items arg)
2884 (todos-special-buffer-name buf file)))
2886 (defun todos-top-priorities-multifile (&optional arg)
2887 "List top priorities of each category in `todos-filter-files'.
2889 If the prefix argument ARG is a number, this is the maximum
2890 number of top priorities to list in each category. If the prefix
2891 argument is `C-u', prompt for which files to filter and use
2892 `todos-show-priorities' as the number of top priorities to list
2893 in each category. If the prefix argument is `C-uC-u', prompt
2894 both for which files to filter and for how many top priorities to
2895 list in each category."
2897 (let* ((buf todos-top-priorities-buffer)
2899 (pref (if (numberp arg)
2901 (setq files (if (or (consp arg)
2902 (null todos-filter-files))
2903 (progn (todos-multiple-files)
2904 todos-multiple-files)
2905 todos-filter-files))
2906 (if (equal arg '(16))
2907 (cons 'top (read-number
2908 "Enter number of top priorities to show: "
2909 todos-show-priorities))
2911 (todos-filter-items pref t)
2912 (todos-special-buffer-name buf files)))
2914 (defun todos-diary-items ()
2915 "Display todo items for diary inclusion in this Todos file."
2917 (let ((buf todos-diary-items-buffer)
2918 (file todos-current-todos-file))
2919 (todos-filter-items 'diary)
2920 (todos-special-buffer-name buf file)))
2922 (defun todos-diary-items-multifile (&optional arg)
2923 "Display todo items for diary inclusion in one or more Todos file.
2924 The files are those listed in `todos-filter-files'."
2926 (let ((buf todos-diary-items-buffer)
2927 (files (if (or arg (null todos-filter-files))
2928 (progn (todos-multiple-files)
2929 todos-multiple-files)
2930 todos-filter-files)))
2931 (todos-filter-items 'diary t)
2932 (todos-special-buffer-name buf files)))
2934 (defun todos-regexp-items ()
2935 "Display todo items matching a user-entered regular expression.
2936 The items are those in the current Todos file."
2938 (let ((buf todos-regexp-items-buffer)
2939 (file todos-current-todos-file))
2940 (todos-filter-items 'regexp)
2941 (todos-special-buffer-name buf file)))
2943 (defun todos-regexp-items-multifile (&optional arg)
2944 "Display todo items matching a user-entered regular expression.
2945 The items are those in the files listed in `todos-filter-files'."
2947 (let ((buf todos-regexp-items-buffer)
2948 (files (if (or arg (null todos-filter-files))
2949 (progn (todos-multiple-files)
2950 todos-multiple-files)
2951 todos-filter-files)))
2952 (todos-filter-items 'regexp t)
2953 (todos-special-buffer-name buf files)))
2955 (defun todos-print (&optional to-file)
2956 "Produce a printable version of the current Todos buffer.
2957 This converts overlays and soft line wrapping and, depending on
2958 the value of `todos-print-function', includes faces. With
2959 non-nil argument TO-FILE write the printable version to a file;
2960 otherwise, send it to the default printer."
2962 (let ((buf todos-print-buffer)
2964 ((eq major-mode 'todos-mode)
2965 (concat "Todos File: "
2966 (todos-short-file-name todos-current-todos-file)
2967 "\nCategory: " (todos-current-category)))
2968 ((eq major-mode 'todos-filter-items-mode)
2969 "Todos Top Priorities")))
2970 (prefix (propertize (concat todos-prefix " ")
2971 'face 'todos-prefix-string))
2973 (fill-prefix (make-string todos-indent-to-here 32))
2974 (content (buffer-string))
2976 (with-current-buffer (get-buffer-create buf)
2978 (goto-char (point-min))
2981 (end (save-excursion (todos-item-end))))
2982 (when todos-number-priorities
2984 (setq prefix (propertize (concat (number-to-string num) " ")
2985 'face 'todos-prefix-string)))
2987 (fill-region beg end))
2988 ;; Calling todos-forward-item infloops at todos-item-start due to
2989 ;; non-overlay prefix, so search for item start instead.
2990 (if (re-search-forward todos-item-start nil t)
2992 (goto-char (point-max))))
2993 (if (re-search-backward (concat "^" (regexp-quote todos-category-done))
2995 (replace-match todos-done-separator))
2996 (goto-char (point-min))
3000 (let ((file (read-file-name "Print to file: ")))
3001 (funcall todos-print-function file))
3002 (funcall todos-print-function)))
3005 (defun todos-print-to-file ()
3006 "Save printable version of this Todos buffer to a file."
3010 (defun todos-convert-legacy-files ()
3011 "Convert legacy Todo files to the current Todos format.
3012 The files `todo-file-do' and `todo-file-done' are converted and
3013 saved (the latter as a Todos Archive file) with a new name in
3014 `todos-files-directory'. See also the documentation string of
3015 `todos-todo-mode-date-time-regexp' for further details."
3017 (if (fboundp 'todo-mode)
3018 (require 'todo-mode)
3019 (error "Void function `todo-mode'"))
3020 ;; Convert `todo-file-do'.
3021 (if (file-exists-p todo-file-do)
3022 (let ((default "todo-do-conv")
3025 (insert-file-contents todo-file-do)
3026 (let ((end (search-forward ")" (line-end-position) t))
3027 (beg (search-backward "(" (line-beginning-position) t)))
3028 (setq todo-categories
3029 (read (buffer-substring-no-properties beg end))))
3031 (delete-region (line-beginning-position) (1+ (line-end-position)))
3034 ((looking-at (regexp-quote (concat todo-prefix todo-category-beg)))
3035 (replace-match todos-category-beg))
3036 ((looking-at (regexp-quote todo-category-end))
3038 ((looking-at (regexp-quote (concat todo-prefix " "
3039 todo-category-sep)))
3040 (replace-match todos-category-done))
3041 ((looking-at (concat (regexp-quote todo-prefix) " "
3042 todos-todo-mode-date-time-regexp " "
3043 (regexp-quote todo-initials) ":"))
3044 (todos-convert-legacy-date-time)))
3046 (setq file (concat todos-files-directory
3048 (format "Save file as (default \"%s\"): " default)
3051 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3053 (insert-file-contents file)
3054 (let ((todos-categories (todos-make-categories-list t)))
3055 (todos-update-categories-sexp))
3056 (write-region (point-min) (point-max) file nil 'nomessage))
3057 ;; Convert `todo-file-done'.
3058 (when (file-exists-p todo-file-done)
3060 (insert-file-contents todo-file-done)
3061 (let ((beg (make-marker))
3063 cat cats comment item)
3065 (when (looking-at todos-todo-mode-date-time-regexp)
3066 (set-marker beg (point))
3067 (todos-convert-legacy-date-time)
3068 (set-marker end (point))
3070 (insert "[" todos-done-string)
3074 (when (looking-at todos-todo-mode-date-time-regexp)
3075 (todos-convert-legacy-date-time))
3076 (when (looking-at (concat " " (regexp-quote todo-initials) ":"))
3077 (replace-match "")))
3078 (if (re-search-forward
3079 (concat "^" todos-todo-mode-date-time-regexp) nil t)
3080 (goto-char (match-beginning 0))
3081 (goto-char (point-max)))
3083 (when (looking-back "\\[\\([^][]+\\)\\]")
3084 (setq cat (match-string 1))
3085 (goto-char (match-beginning 0))
3087 ;; If the item ends with a non-comment parenthesis not
3088 ;; followed by a period, we lose (but we inherit that problem
3089 ;; from todo-mode.el).
3090 (when (looking-back "(\\(.*\\)) ")
3091 (setq comment (match-string 1))
3093 (insert "[" todos-comment-string ": " comment "]"))
3094 (set-marker end (point))
3095 (if (member cat cats)
3096 ;; If item is already in its category, leave it there.
3097 (unless (save-excursion
3099 (concat "^" (regexp-quote todos-category-beg)
3101 (string= (match-string 1) cat))
3102 ;; Else move it to its category.
3103 (setq item (buffer-substring-no-properties beg end))
3104 (delete-region beg (1+ end))
3105 (set-marker beg (point))
3107 (concat "^" (regexp-quote (concat todos-category-beg cat)))
3110 (if (re-search-forward
3111 (concat "^" (regexp-quote todos-category-beg)
3113 (progn (goto-char (match-beginning 0))
3116 (goto-char (point-max)))
3121 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3123 (set-marker beg nil)
3124 (set-marker end nil))
3125 (setq file (concat (file-name-sans-extension file) ".toda"))
3126 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3128 (insert-file-contents file)
3129 (let ((todos-categories (todos-make-categories-list t)))
3130 (todos-update-categories-sexp))
3131 (write-region (point-min) (point-max) file nil 'nomessage)
3132 (setq archive-sexp (read (buffer-substring-no-properties
3133 (line-beginning-position)
3134 (line-end-position)))))
3135 (setq file (concat (file-name-sans-extension file) ".todo"))
3136 ;; Update categories sexp of converted Todos file again, adding
3137 ;; counts of archived items.
3139 (insert-file-contents file)
3140 (let ((sexp (read (buffer-substring-no-properties
3141 (line-beginning-position)
3142 (line-end-position)))))
3144 (let ((archive-cat (assoc (car cat) archive-sexp)))
3146 (aset (cdr cat) 3 (aref (cdr archive-cat) 2)))))
3147 (delete-region (line-beginning-position) (line-end-position))
3148 (prin1 sexp (current-buffer)))
3149 (write-region (point-min) (point-max) file nil 'nomessage)))
3150 (todos-reevaluate-defcustoms)
3151 (message "Format conversion done."))
3152 (error "No legacy Todo file exists")))
3154 ;; ---------------------------------------------------------------------------
3155 ;;; Navigation Commands
3157 (defun todos-forward-category (&optional back)
3158 "Visit the numerically next category in this Todos file.
3159 If the current category is the highest numbered, visit the first
3160 category. With non-nil argument BACK, visit the numerically
3161 previous category (the highest numbered one, if the current
3162 category is the first)."
3164 (setq todos-category-number
3165 (1+ (mod (- todos-category-number (if back 2 0))
3166 (length todos-categories))))
3167 (todos-category-select)
3168 (goto-char (point-min)))
3170 (defun todos-backward-category ()
3171 "Visit the numerically previous category in this Todos file.
3172 If the current category is the highest numbered, visit the first
3175 (todos-forward-category t))
3177 (defun todos-jump-to-category (&optional cat other-file)
3178 "Jump to a category in this or another Todos file.
3180 Programmatically, optional argument CAT provides the category
3181 name. When nil (as in interactive calls), prompt for the
3182 category, with TAB completion on existing categories. If a
3183 non-existing category name is entered, ask whether to add a new
3184 category with this name; if affirmed, add it, then jump to that
3185 category. With non-nil argument OTHER-FILE, prompt for a Todos
3186 file, otherwise jump within the current Todos file."
3188 (let ((file (or (and other-file
3189 (todos-read-file-name "Choose a Todos file: " nil t))
3190 ;; Jump to archived-only Categories from Todos Categories
3193 todos-ignore-archived-categories
3194 (zerop (todos-get-count 'todo cat))
3195 (zerop (todos-get-count 'done cat))
3196 (not (zerop (todos-get-count 'archived cat)))
3197 (concat (file-name-sans-extension
3198 todos-current-todos-file) ".toda"))
3199 todos-current-todos-file
3200 ;; If invoked from outside of Todos mode before
3202 todos-default-todos-file)))
3203 (with-current-buffer (find-file-noselect file)
3204 (and other-file (setq todos-current-todos-file file))
3205 (let ((category (or (and (assoc cat todos-categories) cat)
3206 (todos-read-category "Jump to category: "))))
3207 ;; Clean up after selecting category in Todos Categories mode.
3208 (if (string= (buffer-name) todos-categories-buffer)
3210 (if (or cat other-file)
3211 (set-window-buffer (selected-window)
3212 (set-buffer (get-file-buffer file))))
3213 (unless todos-global-current-todos-file
3214 (setq todos-global-current-todos-file todos-current-todos-file))
3215 (todos-category-number category)
3216 (if (> todos-category-number (length todos-categories))
3217 (setq todos-category-number (todos-add-category category)))
3218 (todos-category-select)
3219 (goto-char (point-min))))))
3221 (defun todos-jump-to-category-other-file ()
3222 "Jump to a category in another Todos file.
3223 The category is chosen by prompt, with TAB completion."
3225 (todos-jump-to-category nil t))
3227 (defun todos-jump-to-item ()
3228 "Jump to the file and category of the filtered item at point."
3230 (let ((str (todos-item-string))
3231 (buf (current-buffer))
3232 cat file archive beg)
3233 (string-match (concat (if todos-filter-done-items
3234 (concat "\\(?:" todos-done-string-start "\\|"
3235 todos-date-string-start "\\)")
3236 todos-date-string-start)
3237 todos-date-pattern "\\(?: " diary-time-regexp "\\)?"
3238 (if todos-filter-done-items
3240 (regexp-quote todos-nondiary-end)) "?"
3241 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
3242 "\\(?1:.*\\)\\]\\).*$") str)
3243 (setq cat (match-string 1 str))
3244 (setq file (match-string 2 str))
3245 (setq archive (string= (match-string 3 str) "(archive) "))
3246 (setq str (replace-match "" nil nil str 4))
3248 (concat todos-files-directory (substring file 0 -1)
3249 (if archive ".toda" ".todo"))
3251 (concat (file-name-sans-extension
3252 todos-global-current-todos-file) ".toda")
3253 todos-global-current-todos-file)))
3254 (find-file-noselect file)
3255 (with-current-buffer (get-file-buffer file)
3257 (goto-char (point-min))
3259 (concat "^" (regexp-quote (concat todos-category-beg cat))) nil t)
3260 (search-forward str)
3261 (setq beg (match-beginning 0)))
3263 (set-window-buffer (selected-window) (set-buffer (get-file-buffer file)))
3264 (setq todos-current-todos-file file)
3265 (setq todos-category-number (todos-category-number cat))
3266 (let ((todos-show-with-done (if todos-filter-done-items t
3267 todos-show-with-done)))
3268 (todos-category-select))
3271 ;; FIXME ? disallow prefix arg value < 1 (re-search-* allows these)
3272 (defun todos-forward-item (&optional count)
3273 "Move point down to start of item with next lower priority.
3274 With numerical prefix COUNT, move point COUNT items downward,"
3276 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
3277 (start (line-end-position)))
3279 (if (re-search-forward todos-item-start nil t (or count 1))
3280 (goto-char (match-beginning 0))
3281 (goto-char (point-max)))
3282 ;; If points advances by one from a todo to a done item, go back to the
3283 ;; space above todos-done-separator, since that is a legitimate place to
3284 ;; insert an item. But skip this space if count > 1, since that should
3285 ;; only stop on an item (FIXME: or not?)
3286 (when (and not-done (todos-done-item-p))
3287 (if (or (not count) (= count 1))
3288 (re-search-backward "^$" start t)))))
3290 (defun todos-backward-item (&optional count)
3291 "Move point up to start of item with next higher priority.
3292 With numerical prefix COUNT, move point COUNT items upward,"
3294 (let* ((done (todos-done-item-p)))
3295 ;; FIXME ? this moves to bob if on the first item (but so does previous-line)
3298 (re-search-backward todos-item-start nil t (or count 1)))
3299 ;; Unless this is a regexp filtered items buffer (which can contain
3300 ;; intermixed todo and done items), if points advances by one from a done
3301 ;; to a todo item, go back to the space above todos-done-separator, since
3302 ;; that is a legitimate place to insert an item. But skip this space if
3303 ;; count > 1, since that should only stop on an item (FIXME: or not?)
3304 (when (and done (not (todos-done-item-p)) (or (not count) (= count 1))
3305 (not (equal (buffer-name) todos-regexp-items-buffer)))
3306 (re-search-forward (concat "^" (regexp-quote todos-category-done)) nil t)
3307 (forward-line -1))))
3309 ;; FIXME: (i) Extend search to other Todos files. (ii) Allow navigating among
3311 (defun todos-search ()
3312 "Search for a regular expression in this Todos file.
3313 The search runs through the whole file and encompasses all and
3314 only todo and done items; it excludes category names. Multiple
3315 matches are shown sequentially, highlighted in `todos-search'
3318 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3320 matches match cat in-done ov mlen msg)
3322 (goto-char (point-min))
3324 (setq match (re-search-forward regex nil t))
3325 (goto-char (line-beginning-position))
3326 (unless (or (equal (point) 1)
3327 (looking-at (concat "^" (regexp-quote todos-category-beg))))
3328 (if match (push match matches)))
3330 (setq matches (reverse matches))
3334 (setq match (pop matches))
3337 (when (looking-at todos-done-string-start)
3339 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
3340 "\\(.*\\)\n") nil t)
3341 (setq cat (match-string-no-properties 1))
3342 (todos-category-number cat)
3343 (todos-category-select)
3345 (unless todos-show-with-done (todos-hide-show-done-items)))
3347 (setq ov (make-overlay (- (point) (length regex)) (point)))
3348 (overlay-put ov 'face 'todos-search)
3350 (setq mlen (length matches))
3353 (format "There are %d more matches; go to next match? "
3355 "There is one more match; go to it? "))
3357 (throw 'stop (setq msg (if (> mlen 1)
3358 (format "There are %d more matches."
3360 "There is one more match."))))))
3361 (setq msg "There are no more matches."))
3362 (todos-category-select)
3364 (message "No match for \"%s\"" regex))
3366 (if (y-or-n-p (concat msg "\nUnhighlight matches? "))
3367 (todos-clear-matches)
3368 (message "You can unhighlight the matches later by typing %s"
3369 (key-description (car (where-is-internal
3370 'todos-clear-matches))))))))
3372 (defun todos-clear-matches ()
3373 "Remove highlighting on matches found by todos-search."
3375 (remove-overlays 1 (1+ (buffer-size)) 'face 'todos-search))
3377 ;; ---------------------------------------------------------------------------
3378 ;;; Editing Commands
3381 ;; FIXME: should there also be command todos-delete-file or is it enough to
3382 ;; delete empty file on deleting last category with todos-delete-category?
3383 (defun todos-add-file ()
3384 "Name and add a new Todos file.
3385 Interactively, prompt for a category and display it.
3386 Noninteractively, return the name of the new file."
3388 (let ((default-file (if todos-default-todos-file
3389 (todos-short-file-name todos-default-todos-file)))
3390 (prompt (concat "Enter name of new Todos file "
3391 "(TAB or SPC to see current names): "))
3393 (setq file (todos-read-file-name prompt));))
3394 (setq shortname (todos-short-file-name file))
3395 (with-current-buffer (get-buffer-create file)
3397 (write-region (point-min) (point-max) file nil 'nomessage nil t)
3399 (todos-reevaluate-defcustoms)
3400 (if (called-interactively-p)
3402 (setq todos-current-todos-file file)
3406 (defun todos-add-category (&optional cat)
3407 "Add a new category to the current Todos file.
3408 Called interactively, prompt for category name, then visit the
3409 category in Todos mode. Non-interactively, argument CAT provides
3410 the category name, which is also the return value."
3412 (let* ((buffer-read-only)
3413 ;; FIXME: check against todos-archive-done-item-or-items with empty file
3414 (buf (find-file-noselect todos-current-todos-file t))
3415 ;; (buf (get-file-buffer todos-current-todos-file))
3416 (num (1+ (length todos-categories)))
3417 (counts (make-vector 4 0))) ; [todo diary done archived]
3418 (unless (zerop (buffer-size buf))
3419 (and (null todos-categories)
3420 (error "Error in %s: File is non-empty but contains no category"
3421 todos-current-todos-file)))
3422 (unless cat (setq cat (read-from-minibuffer "Enter new category name: ")))
3423 (with-current-buffer buf
3424 (setq cat (todos-validate-name cat 'category))
3425 (setq todos-categories (append todos-categories (list (cons cat counts))))
3426 (if todos-categories-full
3427 (setq todos-categories-full (append todos-categories-full
3428 (list (cons cat counts)))))
3430 (goto-char (point-max))
3431 (save-excursion ; Save point for todos-category-select.
3432 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3433 (todos-update-categories-sexp)
3434 ;; If called by command, display the newly added category, else return
3435 ;; the category number to the caller.
3436 (if (called-interactively-p 'any) ; FIXME?
3438 (setq todos-category-number num)
3439 (todos-category-select))
3442 (defun todos-rename-category ()
3443 "Rename current Todos category.
3444 If this file has an archive containing this category, rename the
3445 category there as well."
3447 (let* ((cat (todos-current-category))
3448 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat))))
3449 (setq new (todos-validate-name new 'category))
3450 (let* ((ofile todos-current-todos-file)
3451 (archive (concat (file-name-sans-extension ofile) ".toda"))
3452 (buffers (append (list ofile)
3453 (unless (zerop (todos-get-count 'archived cat))
3455 (dolist (buf buffers)
3456 (with-current-buffer (find-file-noselect buf)
3457 (let (buffer-read-only)
3458 (setq todos-categories (todos-set-categories))
3461 (setcar (assoc cat todos-categories) new)
3463 (goto-char (point-min))
3464 (todos-update-categories-sexp)
3465 (re-search-forward (concat (regexp-quote todos-category-beg)
3466 "\\(" (regexp-quote cat) "\\)\n")
3468 (replace-match new t t nil 1)))))))
3469 ;; FIXME: use force-mode-line-update instead?
3470 (setq mode-line-buffer-identification
3471 (funcall todos-mode-line-function new)))
3472 (save-excursion (todos-category-select)))
3474 (defun todos-delete-category (&optional arg)
3475 "Delete current Todos category provided it is empty.
3476 With ARG non-nil delete the category unconditionally,
3477 i.e. including all existing todo and done items."
3479 (let* ((cat (todos-current-category))
3480 (todo (todos-get-count 'todo cat))
3481 (done (todos-get-count 'done cat))
3482 (archived (todos-get-count 'archived cat)))
3483 (when (or (> (length todos-categories) 1)
3484 (y-or-n-p (concat "This is the only category in this file; "
3485 "deleting it will also delete the file.\n"
3486 "Do you want to proceed? ")))
3488 (or (> todo 0) (> done 0)))
3489 (message "To delete a non-empty category, type C-u %s."
3491 (car (where-is-internal 'todos-delete-category))))
3492 (when (yes-or-no-p (concat "Permanently remove category \"" cat
3493 "\"" (and arg " and all its entries") "? "))
3495 (y-or-n-p (concat "This category has archived items; "
3496 "the archived category will remain\n"
3497 "after deleting the todo category. "
3498 "Do you still want to delete it\n"
3499 "(see 'todos-ignore-archived-categories' "
3500 "for another option)? ")))
3502 (let ((buffer-read-only)
3503 (beg (re-search-backward
3504 (concat "^" (regexp-quote (concat todos-category-beg cat))
3506 (end (if (re-search-forward
3507 (concat "\n\\(" (regexp-quote todos-category-beg)
3511 (remove-overlays beg end)
3512 (delete-region beg end)
3513 (if (= (length todos-categories) 1)
3514 ;; If deleted category was the only one, delete the file.
3516 ;; FIXME: need this?
3517 (setq todos-categories nil)
3518 (todos-reevaluate-defcustoms)
3519 (delete-file todos-current-todos-file)
3521 (message "Deleted empty Todos file %s."
3522 todos-current-todos-file))
3523 ;; FIXME: what about todos-categories-full ?
3524 (setq todos-categories (delete (assoc cat todos-categories)
3526 (todos-update-categories-sexp)
3527 (setq todos-category-number
3528 (1+ (mod todos-category-number (length todos-categories))))
3529 (todos-category-select)
3530 (goto-char (point-min))
3531 (message "Deleted category %s." cat)))))))))
3533 (defun todos-raise-category (&optional lower)
3534 "Raise priority of category point is on in Todos Categories buffer.
3535 With non-nil argument LOWER, lower the category's priority."
3540 (skip-chars-forward " ")
3541 (setq num (number-at-point)))
3542 (when (and num (if lower
3543 (< num (length todos-categories))
3545 (let* ((col (current-column))
3546 (beg (progn (forward-line (if lower 0 -1)) (point)))
3547 (num1 (progn (skip-chars-forward " ") (1- (number-at-point))))
3549 (end (progn (forward-line 2) (point)))
3550 (catvec (vconcat todos-categories))
3551 (cat1-list (aref catvec num1))
3552 (cat2-list (aref catvec num2))
3553 (cat1 (car cat1-list))
3554 (cat2 (car cat2-list))
3555 buffer-read-only newcats)
3556 (delete-region beg end)
3557 (setq num1 (1+ num1))
3558 (setq num2 (1- num2))
3560 (todos-insert-category-line cat2)
3562 (todos-insert-category-line cat1)
3563 (aset catvec num2 (cons cat2 (cdr cat2-list)))
3564 (aset catvec num1 (cons cat1 (cdr cat1-list)))
3565 (setq todos-categories (append catvec nil))
3566 (setq newcats todos-categories)
3567 (with-current-buffer (get-file-buffer todos-current-todos-file)
3568 (setq todos-categories newcats)
3569 (todos-update-categories-sexp))
3570 (forward-line (if lower -1 -2))
3571 (forward-char col)))))
3573 (defun todos-lower-category ()
3574 "Lower priority of category point is on in Todos Categories buffer."
3576 (todos-raise-category t))
3578 (defun todos-move-category ()
3579 "Move current category to a different Todos file.
3580 If current category has archived items, also move those to the
3581 archive of the file moved to, creating it if it does not exist."
3583 (when (or (> (length todos-categories) 1)
3584 (y-or-n-p (concat "This is the only category in this file; "
3585 "moving it will also delete the file.\n"
3586 "Do you want to proceed? ")))
3587 (let* ((ofile todos-current-todos-file)
3588 (cat (todos-current-category))
3589 (nfile (todos-read-file-name "Choose a Todos file: " nil t))
3590 (archive (concat (file-name-sans-extension ofile) ".toda"))
3591 (buffers (append (list ofile)
3592 (unless (zerop (todos-get-count 'archived cat))
3595 (dolist (buf buffers)
3596 (with-current-buffer (find-file-noselect buf)
3598 (goto-char (point-max))
3599 (let* ((beg (re-search-backward
3601 (regexp-quote (concat todos-category-beg cat)))
3603 (end (if (re-search-forward
3604 (concat "^" (regexp-quote todos-category-beg))
3608 (content (buffer-substring-no-properties beg end))
3609 (counts (cdr (assoc cat todos-categories)))
3611 ;; Move the category to the new file. Also update or create
3612 ;; archive file if necessary.
3613 (with-current-buffer
3615 ;; Regenerate todos-archives in case there
3616 ;; is a newly created archive.
3617 (if (member buf (funcall todos-files-function t))
3618 (concat (file-name-sans-extension nfile) ".toda")
3620 (let* ((nfile-short (todos-short-file-name nfile))
3622 (format "Todos file \"%s\" already has "
3624 (format "the category \"%s\";\n" cat)
3625 "enter a new category name: "))
3628 (goto-char (point-max))
3630 ;; If the file moved to has a category with the same
3631 ;; name, rename the moved category.
3632 (when (assoc cat todos-categories)
3633 (unless (member (file-truename (buffer-file-name))
3634 (funcall todos-files-function t))
3635 (setq new (read-from-minibuffer prompt))
3636 (setq new (todos-validate-name new 'category))))
3637 ;; Replace old with new name in Todos and archive files.
3639 (goto-char (point-max))
3641 (concat "^" (regexp-quote todos-category-beg)
3642 "\\(" (regexp-quote cat) "\\)") nil t)
3643 (replace-match new nil nil nil 1)))
3644 (setq todos-categories
3645 (append todos-categories (list (cons new counts))))
3646 (todos-update-categories-sexp)
3647 ;; If archive was just created, save it to avoid "File <xyz> no
3648 ;; longer exists!" message on invoking
3649 ;; `todos-view-archived-items'. FIXME: maybe better to save
3651 (unless (file-exists-p (buffer-file-name))
3653 (todos-category-number (or new cat))
3654 (todos-category-select))
3655 ;; Delete the category from the old file, and if that was the
3656 ;; last category, delete the file. Also handle archive file
3658 (remove-overlays beg end)
3659 (delete-region beg end)
3660 (goto-char (point-min))
3661 ;; Put point after todos-categories sexp.
3663 (if (eobp) ; Aside from sexp, file is empty.
3665 ;; Skip confirming killing the archive buffer.
3666 (set-buffer-modified-p nil)
3667 (delete-file todos-current-todos-file)
3669 (when (member todos-current-todos-file todos-files)
3670 (todos-reevaluate-defcustoms)))
3671 (setq todos-categories (delete (assoc cat todos-categories)
3673 (todos-update-categories-sexp)
3674 (todos-category-select)))))
3675 (set-window-buffer (selected-window)
3676 (set-buffer (find-file-noselect nfile)))
3677 (todos-category-number (or new cat))
3678 (todos-category-select))))
3680 (defun todos-merge-category ()
3681 "Merge current category into another category in this file.
3682 The current category's todo and done items are appended to the
3683 chosen category's todo and done items, respectively, which
3684 becomes the current category, and the category moved from is
3687 (let ((buffer-read-only nil)
3688 (cat (todos-current-category))
3689 (goal (todos-read-category "Category to merge to: " t)))
3691 ;; FIXME: check if cat has archived items and merge those too
3694 (concat "^" (regexp-quote todos-category-beg)) nil t)
3696 (tbeg (progn (forward-line) (point)))
3699 (concat "^" (regexp-quote todos-category-done)) nil t)
3700 (forward-line) (point)))
3701 (tend (progn (forward-line -2) (point)))
3703 (if (re-search-forward
3704 (concat "^" (regexp-quote todos-category-beg)) nil t)
3707 (todo (buffer-substring-no-properties tbeg tend))
3708 (done (buffer-substring-no-properties dbeg cend))
3710 (goto-char (point-min))
3712 (concat "^" (regexp-quote (concat todos-category-beg goal))) nil t)
3714 (concat "^" (regexp-quote todos-category-done)) nil t)
3718 (goto-char (if (re-search-forward
3719 (concat "^" (regexp-quote todos-category-beg)) nil t)
3723 (remove-overlays cbeg cend)
3724 (delete-region cbeg cend)
3725 (todos-update-count 'todo (todos-get-count 'todo cat) goal)
3726 (todos-update-count 'done (todos-get-count 'done cat) goal)
3727 (setq todos-categories (delete (assoc cat todos-categories)
3729 (todos-update-categories-sexp)
3730 (todos-category-number goal)
3731 (todos-category-select)
3732 ;; Put point at the start of the merged todo items.
3733 ;; FIXME: what if there are no merged todo items but only done items?
3737 (defun todos-merge-categories ()
3740 (let* ((cats (mapcar 'car todos-categories))
3741 (goal (todos-read-category "Category to merge to: " t))
3742 (prompt (format "Merge to %s (type C-g to finish)? " goal))
3743 (source (let ((inhibit-quit t) l)
3744 (while (not (eq last-input-event 7))
3746 (when (y-or-n-p prompt)
3748 (setq cats (delete c cats))))))))
3752 ;; FIXME: make insertion options customizable per category?
3754 (defun todos-insert-item (&optional arg diary nonmarking date-type time
3756 "Add a new Todo item to a category.
3757 \(See the note at the end of this document string about key
3758 bindings and convenience commands derived from this command.)
3760 With no (or nil) prefix argument ARG, add the item to the current
3761 category; with one prefix argument (C-u), prompt for a category
3762 from the current Todos file; with two prefix arguments (C-u C-u),
3763 first prompt for a Todos file, then a category in that file. If
3764 a non-existing category is entered, ask whether to add it to the
3765 Todos file; if answered affirmatively, add the category and
3766 insert the item there.
3768 When argument DIARY is non-nil, this overrides the intent of the
3769 user option `todos-include-in-diary' for this item: if
3770 `todos-include-in-diary' is nil, include the item in the Fancy
3771 Diary display, and if it is non-nil, exclude the item from the
3772 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
3773 has its intended effect.
3775 When the item is included in the Fancy Diary display and the
3776 argument NONMARKING is non-nil, this overrides the intent of the
3777 user option `todos-diary-nonmarking' for this item: if
3778 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
3779 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
3781 The argument DATE-TYPE determines the content of the item's
3782 mandatory date header string and how it is added:
3783 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
3784 when the user puts the cursor on a date and hits RET, that
3785 date, in the format set by `calendar-date-display-form',
3786 becomes the date in the header.
3787 - If DATE-TYPE is the symbol `date', the header contains the date
3788 in the format set by `calendar-date-display-form', with year,
3789 month and day individually prompted for (month with tab
3791 - If DATE-TYPE is the symbol `dayname' the header contains a
3792 weekday name instead of a date, prompted for with tab
3794 - If DATE-TYPE has any other value (including nil or none) the
3795 header contains the current date (in the format set by
3796 `calendar-date-display-form').
3798 With non-nil argument TIME prompt for a time string, which must
3799 match `diary-time-regexp'. Typing `<return>' at the prompt
3800 returns the current time, if the user option
3801 `todos-always-add-time-string' is non-nil, otherwise the empty
3802 string (i.e., no time string). If TIME is absent or nil, add or
3803 omit the current time string according as
3804 `todos-always-add-time-string' is non-nil or nil, respectively.
3806 The argument REGION-OR-HERE determines the source and location of
3808 - If the REGION-OR-HERE is the symbol `here', prompt for the text
3809 of the new item and insert it directly above the todo item at
3810 point (hence lowering the priority of the remaining items), or
3811 if point is on the empty line below the last todo item, insert
3812 the new item there. An error is signalled if
3813 `todos-insert-item' is invoked with `here' outside of the
3815 - If REGION-OR-HERE is the symbol `region', use the region of the
3816 current buffer as the text of the new item, depending on the
3817 value of user option `todos-use-only-highlighted-region': if
3818 this is non-nil, then use the region only when it is
3819 highlighted; otherwise, use the region regardless of
3820 highlighting. An error is signalled if there is no region in
3821 the current buffer. Prompt for the item's priority in the
3822 category (an integer between 1 and one more than the number of
3823 items in the category), and insert the item accordingly.
3824 - If REGION-OR-HERE has any other value (in particular, nil or
3825 none), prompt for the text and the item's priority, and insert
3826 the item accordingly.
3828 To facilitate using these arguments when inserting a new todo
3829 item, convenience commands have been defined for all admissible
3830 combinations together with mnenomic key bindings based on on the
3831 name of the arguments and their order in the command's argument
3832 list: diar_y_ - nonmar_k_ing - _c_alendar or _d_ate or day_n_ame
3833 - _t_ime - _r_egion or _h_ere. These key combinations are
3834 appended to the basic insertion key (i) and keys that allow a
3835 following key must be doubled when used finally. For example,
3836 `iyh' will insert a new item with today's date, marked according
3837 to the DIARY argument described above, and with priority
3838 according to the HERE argument; while `iyy' does the same except
3839 the priority is not given by HERE but by prompting."
3840 ;; An alternative interface for customizing key
3841 ;; binding is also provided with the function
3842 ;; `todos-insertion-bindings'." ;FIXME
3844 (let ((region (eq region-or-here 'region))
3845 (here (eq region-or-here 'here)))
3847 ;; FIXME: better to use use-region-p or region-active-p?
3848 (unless (and (if todos-use-only-highlighted-region
3852 (error "The mark is not set now, so there is no region")))
3853 (let* ((buf (current-buffer))
3854 (new-item (if region
3855 ;; FIXME: or keep properties?
3856 (buffer-substring-no-properties
3857 (region-beginning) (region-end))
3858 (read-from-minibuffer "Todo item: ")))
3860 ((eq date-type 'date)
3862 ((eq date-type 'dayname)
3863 (todos-read-dayname))
3864 ((eq date-type 'calendar)
3865 (setq todos-date-from-calendar t)
3866 (todos-set-date-from-calendar))
3867 (t (calendar-date-string (calendar-current-date) t t))))
3868 (time-string (or (and time (todos-read-time))
3869 (and todos-always-add-time-string
3870 (substring (current-time-string) 11 16)))))
3871 (setq todos-date-from-calendar nil)
3872 (cond ((equal arg '(16)) ; FIXME: cf. set-mark-command
3873 (todos-jump-to-category nil t)
3876 (set-buffer (get-file-buffer todos-global-current-todos-file))))
3877 ((equal arg '(4)) ; FIXME: just arg?
3878 (todos-jump-to-category)
3881 (set-buffer (get-file-buffer todos-global-current-todos-file))))
3883 (when (not (derived-mode-p 'todos-mode)) (todos-show))))
3884 (let (buffer-read-only)
3886 ;; Add date, time and diary marking as required.
3887 (concat (if (not (and diary (not todos-include-in-diary)))
3888 todos-nondiary-start
3889 (when (and nonmarking (not todos-diary-nonmarking))
3890 diary-nonmarking-symbol))
3891 date-string (unless (and time-string
3892 (string= time-string ""))
3893 (concat " " time-string))
3894 (when (not (and diary (not todos-include-in-diary)))
3897 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
3898 (setq new-item (replace-regexp-in-string
3899 "\\(\n\\)[^[:blank:]]"
3900 (concat "\n" (make-string todos-indent-to-here 32))
3901 new-item nil nil 1))
3903 (cond ((not (eq major-mode 'todos-mode))
3904 (error "Cannot insert a todo item here outside of Todos mode"))
3905 ((not (eq buf (current-buffer)))
3906 (error "Cannot insert an item here after changing buffer"))
3907 ((or (todos-done-item-p)
3908 ;; Point on last blank line.
3909 (save-excursion (forward-line -1) (todos-done-item-p)))
3910 (error "Cannot insert a new item in the done item section"))
3912 (todos-insert-with-overlays new-item)))
3913 (todos-set-item-priority new-item (todos-current-category) t))
3914 (todos-update-count 'todo 1)
3915 (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
3916 (todos-update-categories-sexp)))))
3918 (defvar todos-date-from-calendar nil
3919 "Helper variable for setting item date from the Emacs Calendar.")
3921 (defun todos-set-date-from-calendar ()
3922 "Return string of date chosen from Calendar."
3923 (when todos-date-from-calendar
3924 (let (calendar-view-diary-initially-flag)
3926 ;; *Calendar* is now current buffer.
3927 (local-set-key (kbd "RET") 'exit-recursive-edit)
3928 (message "Put cursor on a date and type <return> to set it.")
3929 ;; FIXME: is there a better way than recursive-edit? Use unwind-protect?
3930 ;; Check recursive-depth?
3932 (setq todos-date-from-calendar
3933 (calendar-date-string (calendar-cursor-to-date t) t t))
3935 todos-date-from-calendar))
3937 (defun todos-delete-item ()
3938 "Delete at least one item in this category.
3940 If there are marked items, delete all of these; otherwise, delete
3943 (let* ((cat (todos-current-category))
3944 (marked (assoc cat todos-categories-with-marks))
3945 (item (unless marked (todos-item-string)))
3946 (ov (make-overlay (save-excursion (todos-item-start))
3947 (save-excursion (todos-item-end))))
3948 ;; FIXME: make confirmation an option
3950 (y-or-n-p "Permanently delete all marked items? ")
3952 (overlay-put ov 'face 'todos-search)
3953 (y-or-n-p (concat "Permanently delete this item? ")))))
3957 (and marked (goto-char (point-min)))
3960 (if (or (and marked (todos-marked-item-p)) item)
3962 (if (todos-done-item-p)
3963 (todos-update-count 'done -1)
3964 (todos-update-count 'todo -1 cat)
3965 (and (todos-diary-item-p) (todos-update-count 'diary -1)))
3968 ;; Don't leave point below last item.
3969 (and item (bolp) (eolp) (< (point-min) (point-max))
3970 (todos-backward-item))
3972 (throw 'done (setq item nil))))
3973 (todos-forward-item))))
3975 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
3976 (setq todos-categories-with-marks
3977 (assq-delete-all cat todos-categories-with-marks))
3979 (todos-update-categories-sexp)
3980 (todos-prefix-overlays))
3981 (if ov (delete-overlay ov))))
3983 (defun todos-edit-item ()
3984 "Edit the Todo item at point.
3985 If the item consists of only one logical line, edit it in the
3986 minibuffer; otherwise, edit it in Todos Edit mode."
3988 (when (todos-item-string)
3989 (let* ((buffer-read-only)
3990 (start (todos-item-start))
3993 (concat todos-date-string-start todos-date-pattern
3994 "\\( " diary-time-regexp "\\)?"
3995 (regexp-quote todos-nondiary-end) "?")
3996 (line-end-position) t)
3997 (1+ (- (point) start))))
3998 (item (todos-item-string))
3999 (multiline (> (length (split-string item "\n")) 1))
4002 (todos-edit-multiline t)
4003 (let ((new (read-string "Edit: " (cons item item-beg))))
4004 (while (not (string-match
4005 (concat todos-date-string-start todos-date-pattern) new))
4006 (setq new (read-from-minibuffer
4007 "Item must start with a date: " new)))
4008 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4009 (setq new (replace-regexp-in-string
4010 "\\(\n\\)[^[:blank:]]"
4011 (concat "\n" (make-string todos-indent-to-here 32)) new
4013 ;; If user moved point during editing, make sure it moves back.
4016 (todos-insert-with-overlays new)
4017 (move-to-column item-beg))))))
4019 (defun todos-edit-multiline-item ()
4020 "Edit current Todo item in Todos Edit mode.
4021 Use of newlines invokes `todos-indent' to insure compliance with
4022 the format of Diary entries."
4024 (todos-edit-multiline t))
4026 (defun todos-edit-multiline (&optional item)
4029 ;; FIXME: should there be only one live Todos Edit buffer?
4030 ;; (let ((buffer-name todos-edit-buffer))
4031 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
4034 (set-buffer (make-indirect-buffer
4035 (file-name-nondirectory todos-current-todos-file)
4038 (narrow-to-region (todos-item-start) (todos-item-end))
4041 ;; (message (concat "Type %s to check file format validity and "
4042 ;; "return to Todos mode.\n")
4043 ;; (key-description (car (where-is-internal 'todos-edit-quit))))
4044 (message "%s" (substitute-command-keys
4045 (concat "Type \\[todos-edit-quit] to check file format "
4046 "validity and return to Todos mode.\n")))))
4048 (defun todos-edit-quit ()
4049 "Return from Todos Edit mode to Todos mode.
4051 If the whole file was in Todos Edit mode, check before returning
4052 whether the file is still a valid Todos file and if so, also
4053 recalculate the Todos categories sexp, in case changes were made
4054 in the number or names of categories."
4056 ;; FIXME: worth doing this only if file was actually changed?
4057 (when (eq (buffer-size) (- (point-max) (point-min)))
4058 (when (todos-check-format)
4059 (todos-make-categories-list t)))
4061 ;; In case next buffer is not the one holding todos-current-todos-file.
4064 (defun todos-edit-item-header (&optional what)
4065 "Edit date/time header of at least one item.
4067 Interactively, ask whether to edit year, month and day or day of
4068 the week, as well as time. If there are marked items, apply the
4069 changes to all of these; otherwise, edit just the item at point.
4071 Non-interactively, argument WHAT specifies whether to set the
4072 date from the Calendar or to today, or whether to edit only the
4073 date or day, or only the time."
4075 (let* ((cat (todos-current-category))
4076 (marked (assoc cat todos-categories-with-marks))
4077 (first t) ; Match only first of marked items.
4078 (todos-date-from-calendar t)
4079 ndate ntime nheader)
4081 (or (and marked (goto-char (point-min))) (todos-item-start))
4085 (while (not (todos-marked-item-p))
4086 (todos-forward-item)
4087 (and (eobp) (throw 'stop nil))))
4088 (re-search-forward (concat todos-date-string-start "\\(?1:"
4090 "\\)\\(?2: " diary-time-regexp "\\)?")
4091 (line-end-position) t)
4092 (let* ((odate (match-string-no-properties 1))
4093 (otime (match-string-no-properties 2))
4095 (cond ((eq what 'today)
4097 (setq ndate (calendar-date-string
4098 (calendar-current-date) t t))
4099 (replace-match ndate nil nil nil 1)))
4100 ((eq what 'calendar)
4101 (setq ndate (save-match-data (todos-set-date-from-calendar)))
4102 (replace-match ndate nil nil nil 1))
4104 (unless (eq what 'timeonly)
4106 (setq ndate (if (save-match-data
4107 (string-match "[0-9]+" odate))
4108 (if (y-or-n-p "Change date? ")
4110 (todos-read-dayname))
4111 (if (y-or-n-p "Change day? ")
4112 (todos-read-dayname)
4113 (todos-read-date)))))
4114 (replace-match ndate nil nil nil 1))
4115 (unless (eq what 'dateonly)
4117 (setq ntime (save-match-data (todos-read-time)))
4118 (when (< 0 (length ntime))
4119 (setq ntime (concat " " ntime))))
4121 (replace-match ntime nil nil nil 2)
4122 (goto-char (match-end 1))
4124 (setq todos-date-from-calendar nil)
4127 (todos-forward-item)
4128 (goto-char (point-max))))))))
4130 (defun todos-edit-item-date ()
4131 "Prompt for and apply changes to current item's date."
4133 (todos-edit-item-header 'dateonly))
4135 (defun todos-edit-item-date-from-calendar ()
4136 "Prompt for changes to current item's date and apply from Calendar."
4138 (todos-edit-item-header 'calendar))
4140 (defun todos-edit-item-date-is-today ()
4141 "Set item date to today's date."
4143 (todos-edit-item-header 'today))
4145 (defun todos-edit-item-time ()
4146 "Prompt For and apply changes to current item's time."
4148 (todos-edit-item-header 'timeonly))
4150 (defun todos-edit-item-diary-inclusion ()
4151 "Change diary status of one or more todo items in this category.
4152 That is, insert `todos-nondiary-marker' if the candidate items
4153 lack this marking; otherwise, remove it.
4155 If there are marked todo items, change the diary status of all
4156 and only these, otherwise change the diary status of the item at
4159 (let ((buffer-read-only)
4160 (marked (assoc (todos-current-category)
4161 todos-categories-with-marks)))
4164 (when marked (goto-char (point-min)))
4166 (if (todos-done-item-p)
4167 (throw 'stop (message "Done items cannot be edited"))
4168 (unless (and marked (not (todos-marked-item-p)))
4169 (let* ((beg (todos-item-start))
4170 (lim (save-excursion (todos-item-end)))
4171 (end (save-excursion
4172 (or (todos-time-string-matcher lim)
4173 (todos-date-string-matcher lim)))))
4174 (if (looking-at (regexp-quote todos-nondiary-start))
4177 (search-forward todos-nondiary-end (1+ end) t)
4179 (todos-update-count 'diary 1))
4181 (insert todos-nondiary-start)
4182 (goto-char (1+ end))
4183 (insert todos-nondiary-end)
4184 (todos-update-count 'diary -1)))))
4185 (unless marked (throw 'stop nil))
4186 (todos-forward-item)))))
4187 (todos-update-categories-sexp)))
4189 (defun todos-edit-category-diary-inclusion (arg)
4190 "Make all items in this category diary items.
4191 With prefix ARG, make all items in this category non-diary
4195 (goto-char (point-min))
4196 (let ((todo-count (todos-get-count 'todo))
4197 (diary-count (todos-get-count 'diary))
4201 (if (todos-done-item-p) ; We've gone too far.
4203 (let* ((beg (todos-item-start))
4204 (lim (save-excursion (todos-item-end)))
4205 (end (save-excursion
4206 (or (todos-time-string-matcher lim)
4207 (todos-date-string-matcher lim)))))
4209 (unless (looking-at (regexp-quote todos-nondiary-start))
4210 (insert todos-nondiary-start)
4211 (goto-char (1+ end))
4212 (insert todos-nondiary-end))
4213 (when (looking-at (regexp-quote todos-nondiary-start))
4215 (search-forward todos-nondiary-end (1+ end) t)
4216 (replace-match "")))))
4217 (todos-forward-item))
4218 (unless (if arg (zerop diary-count) (= diary-count todo-count))
4219 (todos-update-count 'diary (if arg
4221 (- todo-count diary-count))))
4222 (todos-update-categories-sexp)))))
4224 (defun todos-edit-item-diary-nonmarking ()
4225 "Change non-marking of one or more diary items in this category.
4226 That is, insert `diary-nonmarking-symbol' if the candidate items
4227 lack this marking; otherwise, remove it.
4229 If there are marked todo items, change the non-marking status of
4230 all and only these, otherwise change the non-marking status of
4233 (let ((buffer-read-only)
4234 (marked (assoc (todos-current-category)
4235 todos-categories-with-marks)))
4238 (when marked (goto-char (point-min)))
4240 (if (todos-done-item-p)
4241 (throw 'stop (message "Done items cannot be edited"))
4242 (unless (and marked (not (todos-marked-item-p)))
4244 (unless (looking-at (regexp-quote todos-nondiary-start))
4245 (if (looking-at (regexp-quote diary-nonmarking-symbol))
4247 (insert diary-nonmarking-symbol))))
4248 (unless marked (throw 'stop nil))
4249 (todos-forward-item)))))))
4251 (defun todos-edit-category-diary-nonmarking (arg)
4252 "Add `diary-nonmarking-symbol' to all diary items in this category.
4253 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
4254 items in this category."
4257 (goto-char (point-min))
4258 (let (buffer-read-only)
4261 (if (todos-done-item-p) ; We've gone too far.
4263 (unless (looking-at (regexp-quote todos-nondiary-start))
4265 (when (looking-at (regexp-quote diary-nonmarking-symbol))
4267 (unless (looking-at (regexp-quote diary-nonmarking-symbol))
4268 (insert diary-nonmarking-symbol))))
4269 (todos-forward-item)))))))
4271 (defun todos-raise-item-priority (&optional lower)
4272 "Raise priority of current item by moving it up by one item.
4273 With non-nil argument LOWER lower item's priority."
4275 (unless (or (todos-done-item-p)
4276 (and (eq major-mode 'todos-filter-items-mode)
4277 ;; Items in Top Priorities buffer can be reprioritized.
4278 (not (string-match (regexp-quote todos-top-priorities-buffer)
4280 ;; Point is between todo and done items.
4282 (let (buffer-read-only)
4285 ;; Can't lower final todo item.
4286 (todos-forward-item)
4287 (and (looking-at todos-item-start)
4288 (not (todos-done-item-p)))))
4289 ;; Can't raise or lower todo item when it's the only one.
4290 (> (count-lines (point-min) (point)) 0))
4291 (let ((item (todos-item-string))
4292 (marked (todos-marked-item-p)))
4293 ;; In Top Priorities buffer, an item's priority can be changed
4294 ;; wrt items in another category, but not wrt items in the same
4296 (when (eq major-mode 'todos-filter-items-mode)
4297 (let* ((regexp (concat todos-date-string-start todos-date-pattern
4298 "\\( " diary-time-regexp "\\)?"
4299 (regexp-quote todos-nondiary-end)
4300 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)"))
4301 (cat1 (save-excursion
4302 (re-search-forward regexp nil t)
4304 (cat2 (save-excursion
4306 (todos-forward-item)
4307 (todos-backward-item))
4308 (re-search-forward regexp nil t)
4310 (if (string= cat1 cat2)
4311 ;; FIXME: better message
4312 (error (concat "Cannot change item's priority in its "
4313 "category; do this in Todos mode")))))
4315 (if lower (todos-forward-item) (todos-backward-item))
4316 (todos-insert-with-overlays item)
4317 ;; If item was marked, retore the mark.
4318 (and marked (overlay-put (make-overlay (point) (point))
4319 'before-string todos-item-mark)))
4320 (message ""))))) ;FIXME: no message ?
4322 (defun todos-lower-item-priority ()
4323 "Lower priority of current item by moving it down by one item."
4325 (todos-raise-item-priority t))
4327 ;; FIXME: incorporate todos-(raise|lower)-item-priority ?
4328 ;; FIXME: this does not DTRT in todos-categories-mode
4329 (defun todos-set-item-priority (item cat &optional new)
4330 "Set todo ITEM's priority in category CAT, moving item as needed.
4331 Interactively, the item and the category are the current ones,
4332 and the priority is a number between 1 and the number of items in
4333 the category. Non-interactively with argument NEW, the lowest
4334 priority is one more than the number of items in CAT."
4335 (interactive (list (todos-item-string) (todos-current-category)))
4336 (unless (called-interactively-p t)
4337 (todos-category-number cat)
4338 (todos-category-select))
4339 (let* ((todo (todos-get-count 'todo cat))
4340 (maxnum (if new (1+ todo) todo))
4342 priority candidate prompt)
4343 (unless (zerop todo)
4344 (while (not priority)
4346 (string-to-number (read-from-minibuffer
4348 (format "Set item priority (1-%d): "
4351 (when (or (< candidate 1) (> candidate maxnum))
4352 (format "Priority must be an integer between 1 and %d.\n"
4354 (unless prompt (setq priority candidate)))
4355 ;; Interactively, just relocate the item within its category.
4356 (when (called-interactively-p) (todos-remove-item))
4357 (goto-char (point-min))
4358 (unless (= priority 1) (todos-forward-item (1- priority))))
4359 (todos-insert-with-overlays item)))
4361 (defun todos-move-item (&optional file)
4362 "Move at least one todo item to another category.
4364 If there are marked items, move all of these; otherwise, move
4367 With non-nil argument FILE, first prompt for another Todos file and
4368 then a category in that file to move the item or items to.
4370 If the chosen category is not one of the existing categories,
4371 then it is created and the item(s) become(s) the first
4372 entry/entries in that category."
4374 (unless (or (todos-done-item-p)
4375 ;; Point is between todo and done items.
4377 (let* ((buffer-read-only)
4378 (file1 todos-current-todos-file)
4379 (cat1 (todos-current-category))
4380 (marked (assoc cat1 todos-categories-with-marks))
4381 (num todos-category-number)
4382 (item (todos-item-string))
4383 (diary-item (todos-diary-item-p))
4384 (omark (save-excursion (todos-item-start) (point-marker)))
4386 (todos-read-file-name "Choose a Todos file: " nil t)
4391 (set-buffer (find-file-noselect file2))
4392 (setq cat2 (let* ((pl (if (and marked (> (cdr marked) 1)) "s" ""))
4393 (name (todos-read-category
4394 (concat "Move item" pl " to category: ")))
4395 (prompt (concat "Choose a different category than "
4396 "the current one\n(type `"
4398 (car (where-is-internal
4399 'todos-set-item-priority)))
4400 "' to reprioritize item "
4401 "within the same category): ")))
4402 (while (equal name cat1)
4403 (setq name (todos-read-category prompt)))
4405 (set-buffer (get-file-buffer file1))
4409 (goto-char (point-min))
4411 (when (todos-marked-item-p)
4412 (setq item (concat item (todos-item-string) "\n"))
4413 (setq count (1+ count))
4414 (when (todos-diary-item-p)
4415 (setq count-diary (1+ count-diary))))
4416 (todos-forward-item))
4417 ;; Chop off last newline.
4418 (setq item (substring item 0 -1)))
4420 (when (todos-diary-item-p) (setq count-diary 1)))
4421 (set-window-buffer (selected-window)
4422 (set-buffer (find-file-noselect file2)))
4423 (unless (assoc cat2 todos-categories) (todos-add-category cat2))
4424 (todos-set-item-priority item cat2 t)
4425 (setq nmark (point-marker))
4426 (todos-update-count 'todo count)
4427 (todos-update-count 'diary count-diary)
4428 (todos-update-categories-sexp)
4429 (with-current-buffer (get-file-buffer file1)
4438 (concat "^" (regexp-quote todos-category-beg)) nil t)
4442 (concat "^" (regexp-quote todos-category-done)) nil t)
4443 (setq end (match-beginning 0))
4445 (while (< (point) end)
4446 (if (todos-marked-item-p)
4448 (todos-forward-item))))
4449 (todos-remove-item))))
4450 (todos-update-count 'todo (- count) cat1)
4451 (todos-update-count 'diary (- count-diary) cat1)
4452 (todos-update-categories-sexp))
4453 (set-window-buffer (selected-window)
4454 (set-buffer (find-file-noselect file2)))
4455 (setq todos-category-number (todos-category-number cat2))
4456 (todos-category-select)
4457 (goto-char nmark))))
4459 (defun todos-move-item-to-file ()
4460 "Move the current todo item to a category in another Todos file."
4462 (todos-move-item t))
4464 (defun todos-move-item-to-diary ()
4465 "Move one or more items in current category to the diary file.
4467 If there are marked items, move all of these; otherwise, move
4473 ;; FIXME: make adding date customizable, and make this and time customization
4474 ;; overridable via double prefix arg ??
4475 (defun todos-item-done (&optional arg)
4476 "Tag at least one item in this category as done and hide it.
4478 With prefix argument ARG prompt for a comment and append it to
4479 the done item; this is only possible if there are no marked
4480 items. If there are marked items, tag all of these with
4481 `todos-done-string' plus the current date and, if
4482 `todos-always-add-time-string' is non-nil, the current time;
4483 otherwise, just tag the item at point. Items tagged as done are
4484 relocated to the category's (by default hidden) done section."
4486 (let* ((cat (todos-current-category))
4487 (marked (assoc cat todos-categories-with-marks)))
4488 (unless (or (todos-done-item-p)
4489 (and (looking-at "^$") (not marked)))
4490 (let* ((date-string (calendar-date-string (calendar-current-date) t t))
4491 (time-string (if todos-always-add-time-string
4492 (concat " " (substring (current-time-string) 11 16))
4494 (done-prefix (concat "[" todos-done-string date-string time-string
4496 (comment (and arg (not marked) (read-string "Enter a comment: ")))
4501 (and marked (goto-char (point-min)))
4504 (if (or (not marked) (and marked (todos-marked-item-p)))
4506 (setq item (todos-item-string))
4507 (setq done-item (cond (marked
4508 (concat done-item done-prefix item "\n"))
4510 (concat done-prefix item " ["
4511 todos-comment-string
4514 (concat done-prefix item))))
4515 (setq item-count (1+ item-count))
4516 (when (todos-diary-item-p)
4517 (setq diary-count (1+ diary-count)))
4519 (unless marked (throw 'done nil)))
4520 (todos-forward-item))))
4522 ;; Chop off last newline of done item string.
4523 (setq done-item (substring done-item 0 -1))
4524 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
4525 (setq todos-categories-with-marks
4526 (assq-delete-all cat todos-categories-with-marks)))
4530 (concat "^" (regexp-quote todos-category-done)) nil t)
4532 (insert done-item "\n"))
4533 (todos-update-count 'todo (- item-count))
4534 (todos-update-count 'done item-count)
4535 (todos-update-count 'diary (- diary-count))
4536 (todos-update-categories-sexp)
4537 (save-excursion (todos-category-select))))))
4539 ;; FIXME: only if there's no comment, or edit an existing comment?
4540 (defun todos-done-item-add-or-edit-comment ()
4541 "Add a comment to this done item or edit an existing comment."
4543 (when (todos-done-item-p)
4544 (let ((item (todos-item-string))
4545 (end (save-excursion (todos-item-end)))
4546 comment buffer-read-only)
4549 (if (re-search-forward (concat " \\["
4550 (regexp-quote todos-comment-string)
4551 ": \\([^]]+\\)\\]") end t)
4553 (setq comment (read-string "Edit comment: "
4554 (cons (match-string 1) 1)))
4555 (replace-match comment nil nil nil 1))
4556 (setq comment (read-string "Enter a comment: "))
4558 (insert " [" todos-comment-string ": " comment "]"))))))
4560 ;; FIXME: implement this or done item editing?
4561 (defun todos-uncomment-done-item ()
4565 ;; FIXME: delete comment from restored item or just leave it up to user?
4566 (defun todos-item-undo ()
4567 "Restore this done item to the todo section of this category."
4569 (when (todos-done-item-p)
4570 (let* ((buffer-read-only)
4571 (done-item (todos-item-string))
4573 (orig-mrk (progn (todos-item-start) (point-marker)))
4574 ;; Find the end of the date string added upon making item done.
4575 (start (search-forward "] "))
4576 (item (buffer-substring start (todos-item-end)))
4579 ;; If user cancels before setting new priority, then restore everything.
4582 (todos-set-item-priority item (todos-current-category) t)
4584 (todos-update-count 'todo 1)
4585 (todos-update-count 'done -1)
4586 (and (todos-diary-item-p) (todos-update-count 'diary 1))
4587 (todos-update-categories-sexp))
4590 (goto-char orig-mrk)
4591 (todos-insert-with-overlays done-item)
4592 (let ((todos-show-with-done t))
4593 (todos-category-select)
4594 (goto-char opoint)))
4595 (set-marker orig-mrk nil)))))
4597 (defun todos-archive-done-item-or-items (&optional all)
4598 "Archive at least one done item in this category.
4600 If there are marked done items (and no marked todo items),
4601 archive all of these; otherwise, with non-nil argument ALL,
4602 archive all done items in this category; otherwise, archive the
4605 If the archive of this file does not exist, it is created. If
4606 this category does not exist in the archive, it is created."
4608 (when (not (member (buffer-file-name) (funcall todos-files-function t)))
4609 (if (and all (zerop (todos-get-count 'done)))
4610 (message "No done items in this category")
4612 (let* ((cat (todos-current-category))
4613 (tbuf (current-buffer))
4614 (marked (assoc cat todos-categories-with-marks))
4615 (afile (concat (file-name-sans-extension
4616 todos-current-todos-file) ".toda"))
4617 (archive (if (file-exists-p afile)
4618 (find-file-noselect afile t)
4620 ;; todos-add-category requires an exisiting file...
4621 (with-current-buffer (get-buffer-create afile)
4623 (write-region (point-min) (point-max) afile
4624 nil 'nomessage nil t)))
4625 ;; ...but the file still lacks a categories sexp, so
4626 ;; visiting the file would barf on todos-set-categories,
4627 ;; hence we just return the buffer.
4628 (get-buffer afile)))
4629 (item (and (todos-done-item-p) (concat (todos-item-string) "\n")))
4631 marked-items beg end all-done
4636 (goto-char (point-min))
4638 (if (todos-marked-item-p)
4639 (if (not (todos-done-item-p))
4640 (throw 'end (message "Only done items can be archived"))
4641 (concat marked-items (todos-item-string) "\n")
4642 (setq count (1+ count)))
4643 (todos-forward-item)))))
4645 (if (y-or-n-p "Archive all done items in this category? ")
4648 (goto-char (point-min))
4651 (re-search-forward todos-done-string-start nil t)
4652 (match-beginning 0))
4653 end (if (re-search-forward
4654 (concat "^" (regexp-quote todos-category-beg))
4658 all-done (buffer-substring beg end)
4659 count (todos-get-count 'done))))
4661 (when (or marked all item)
4662 (with-current-buffer archive
4663 (let ((current todos-global-current-todos-file)
4666 (goto-char (point-min))
4669 (concat "^" (regexp-quote (concat todos-category-beg cat)))
4671 (re-search-forward (regexp-quote todos-category-done) nil t))
4673 ;; todos-add-category uses t-c-t-f, so temporarily set it.
4674 (setq todos-current-todos-file afile)
4675 (todos-add-category cat)
4676 (goto-char (point-max)))
4677 (insert (cond (marked marked-items)
4680 (todos-update-count 'done (if (or marked all) count 1))
4681 (todos-update-categories-sexp)
4682 ;; Save to file now (using write-region in order not to visit
4683 ;; afile) so we can visit it later with todos-show-archive.
4684 (write-region nil nil afile)
4685 (setq todos-current-todos-file current)))
4686 (with-current-buffer tbuf
4687 (cond ((or marked item)
4688 (and marked (goto-char (point-min)))
4691 (if (or (and marked (todos-marked-item-p)) item)
4694 (todos-update-count 'done -1)
4695 (todos-update-count 'archived 1)
4696 ;; Don't leave point below last item.
4697 (and item (bolp) (eolp) (< (point-min) (point-max))
4698 (todos-backward-item))
4700 (throw 'done (setq item nil))))
4701 (todos-forward-item)))))
4703 (remove-overlays beg end)
4704 (delete-region beg end)
4705 (todos-update-count 'done (- count))
4706 (todos-update-count 'archived count)))
4708 (remove-overlays (point-min) (point-max)
4709 'before-string todos-item-mark)
4710 (setq todos-categories-with-marks
4711 (assq-delete-all cat todos-categories-with-marks))
4713 (todos-update-categories-sexp)
4714 (todos-prefix-overlays)
4715 ;; FIXME: Heisenbug: item displays mark -- but not when edebugging
4716 (remove-overlays (point-min) (point-max)
4717 'before-string todos-item-mark)))
4718 (display-buffer (find-file-noselect afile) t)
4719 ;; FIXME: how to avoid switch-to-buffer and still get tbuf above
4720 ;; afile? What about pop-to-buffer-same-window in recent trunk?
4721 (switch-to-buffer tbuf))))))
4723 (defun todos-archive-category-done-items ()
4724 "Move all done items in this category to its archive."
4726 (todos-archive-done-item-or-items t))
4728 (defun todos-unarchive-items (&optional all)
4729 "Unarchive at least one item in this archive category.
4731 If there are marked items, unarchive all of these; otherwise,
4732 with non-nil argument ALL, unarchive all items in this category;
4733 otherwise, unarchive the item at point.
4735 Unarchived items are restored as done items to the corresponding
4736 category in the Todos file, inserted at the end of done section.
4737 If all items in the archive category were restored, the category
4738 is deleted from the archive. If this was the only category in the
4739 archive, the archive file is deleted."
4741 (when (member (buffer-file-name) (funcall todos-files-function t))
4743 (let* ((buffer-read-only nil)
4744 (tbuf (find-file-noselect
4745 (concat (file-name-sans-extension todos-current-todos-file)
4747 (cat (todos-current-category))
4748 (marked (assoc cat todos-categories-with-marks))
4749 (item (concat (todos-item-string) "\n"))
4750 (all-items (buffer-substring (point-min) (point-max)))
4751 (all-count (todos-get-count 'done))
4752 marked-items marked-count)
4754 (goto-char (point-min))
4756 (when (todos-marked-item-p)
4757 (concat marked-items (todos-item-string) "\n")
4758 (setq marked-count (1+ marked-count)))
4759 (todos-forward-item)))
4760 ;; Restore items to end of category's done section and update counts.
4761 (with-current-buffer tbuf
4762 (let (buffer-read-only)
4764 (goto-char (point-min))
4765 (re-search-forward (concat "^" (regexp-quote
4766 (concat todos-category-beg cat)))
4768 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
4770 (goto-char (match-beginning 0))
4771 (goto-char (point-max)))
4773 (insert marked-items)
4774 (todos-update-count 'done marked-count)
4775 (todos-update-count 'archived (- marked-count)))
4777 (if (y-or-n-p (concat "Restore this category's items "
4778 "to Todos file as done items "
4779 "and delete this category? "))
4780 (progn (insert all-items)
4781 (todos-update-count 'done all-count)
4782 (todos-update-count 'archived (- all-count)))
4786 (todos-update-count 'done 1)
4787 (todos-update-count 'archived -1)))
4788 (todos-update-categories-sexp)))
4789 ;; Delete restored items from archive.
4790 (cond ((or marked item)
4791 (and marked (goto-char (point-min)))
4794 (if (or (and marked (todos-marked-item-p)) item)
4797 (todos-update-count 'done -1)
4798 ;; Don't leave point below last item.
4799 (and item (bolp) (eolp) (< (point-min) (point-max))
4800 (todos-backward-item))
4802 (throw 'done (setq item nil))))
4803 (todos-forward-item)))))
4805 (remove-overlays (point-min) (point-max))
4806 (delete-region (point-min) (point-max))
4807 (todos-update-count 'done (- all-count))))
4808 ;; If that was the last category in the archive, delete the whole file.
4809 (if (= (length todos-categories) 1)
4811 (delete-file todos-current-todos-file)
4812 ;; Don't bother confirming killing the archive buffer.
4813 (set-buffer-modified-p nil)
4815 ;; Otherwise, if the archive category is now empty, delete it.
4816 (when (eq (point-min) (point-max))
4818 (let ((beg (re-search-backward
4819 (concat "^" (regexp-quote todos-category-beg) cat)
4821 (end (if (re-search-forward
4822 (concat "^" (regexp-quote todos-category-beg))
4826 (remove-overlays beg end)
4827 (delete-region beg end)
4828 (setq todos-categories (delete (assoc cat todos-categories)
4830 (todos-update-categories-sexp))))
4831 ;; Visit category in Todos file and show restored done items.
4832 (let ((tfile (buffer-file-name tbuf))
4833 (todos-show-with-done t))
4834 (set-window-buffer (selected-window)
4835 (set-buffer (find-file-noselect tfile)))
4836 (todos-category-number cat)
4838 (message "Items unarchived."))))))
4840 (defun todos-unarchive-category ()
4841 "Unarchive all items in this category. See `todos-unarchive-items'."
4843 (todos-unarchive-items t))
4847 ;;; todos.el ends here
4850 ;; ---------------------------------------------------------------------------
4851 ;;; Addition to calendar.el
4853 ;; FIXME: autoload when key-binding is defined in calendar.el
4854 (defun todos-insert-item-from-calendar ()
4857 ;; FIXME: todos-current-todos-file is nil here, better to solicit Todos
4858 ;; file? todos-global-current-todos-file is nil if no Todos file has been
4860 (pop-to-buffer (file-name-nondirectory todos-global-current-todos-file))
4862 ;; FIXME: this now calls todos-set-date-from-calendar
4863 (todos-insert-item t 'calendar))
4865 ;; FIXME: calendar is loaded before todos
4866 ;; (add-hook 'calendar-load-hook
4868 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar);))
4870 ;; ---------------------------------------------------------------------------
4871 ;;; necessitated adaptations to diary-lib.el
4873 ;; (defun diary-goto-entry (button)
4874 ;; "Jump to the diary entry for the BUTTON at point."
4875 ;; (let* ((locator (button-get button 'locator))
4876 ;; (marker (car locator))
4877 ;; markbuf file opoint)
4878 ;; ;; If marker pointing to diary location is valid, use that.
4879 ;; (if (and marker (setq markbuf (marker-buffer marker)))
4881 ;; (pop-to-buffer markbuf)
4882 ;; (goto-char (marker-position marker)))
4883 ;; ;; Marker is invalid (eg buffer has been killed, as is the case with
4884 ;; ;; included diary files).
4885 ;; (or (and (setq file (cadr locator))
4886 ;; (file-exists-p file)
4887 ;; (find-file-other-window file)
4889 ;; (when (eq major-mode (default-value 'major-mode)) (diary-mode))
4890 ;; (when (eq major-mode 'todos-mode) (widen))
4891 ;; (goto-char (point-min))
4892 ;; (when (re-search-forward (format "%s.*\\(%s\\)"
4893 ;; (regexp-quote (nth 2 locator))
4894 ;; (regexp-quote (nth 3 locator)))
4896 ;; (goto-char (match-beginning 1))
4897 ;; (when (eq major-mode 'todos-mode)
4898 ;; (setq opoint (point))
4899 ;; (re-search-backward (concat "^"
4900 ;; (regexp-quote todos-category-beg)
4903 ;; (todos-category-number (match-string 1))
4904 ;; (todos-category-select)
4905 ;; (goto-char opoint)))))
4906 ;; (message "Unable to locate this diary entry")))))