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-if-not and find-if-not in todos-reset-global-current-todos-file
32 ;; and for remove-duplicates in todos-insertion-commands-args.
33 (eval-when-compile (require 'cl
))
35 ;; ---------------------------------------------------------------------------
39 "Create and maintain categorized lists of todo items."
40 :link
'(emacs-commentary-link "todos")
44 (defcustom todos-files-directory
(locate-user-emacs-file "todos/")
45 "Directory where user's Todos files are saved."
49 (defun todos-files (&optional archives
)
50 "Default value of `todos-files-function'.
51 This returns the case-insensitive alphabetically sorted list of
52 file truenames in `todos-files-directory' with the extension
53 \".todo\". With non-nil ARCHIVES return the list of archive file
54 truenames (those with the extension \".toda\")."
55 (let ((files (if (file-exists-p todos-files-directory
)
56 (mapcar 'file-truename
57 (directory-files todos-files-directory t
58 (if archives
"\.toda$" "\.todo$") t
)))))
59 (sort files
(lambda (s1 s2
) (let ((cis1 (upcase s1
))
61 (string< cis1 cis2
))))))
63 (defcustom todos-files-function
'todos-files
64 "Function returning the value of the variable `todos-files'.
65 This function should take an optional argument that, if non-nil,
66 makes it return the value of the variable `todos-archives'."
70 (defun todos-short-file-name (file)
71 "Return short form of Todos FILE.
72 This lacks the extension and directory components."
73 (file-name-sans-extension (file-name-nondirectory file
)))
75 (defcustom todos-default-todos-file
(car (funcall todos-files-function
))
76 "Todos file visited by first session invocation of `todos-show'."
77 :type
`(radio ,@(mapcar (lambda (f) (list 'const f
))
78 (mapcar 'todos-short-file-name
79 (funcall todos-files-function
))))
82 ;; FIXME: is there a better alternative to this?
83 (defun todos-reevaluate-default-file-defcustom ()
84 "Reevaluate defcustom of `todos-default-todos-file'.
85 Called after adding or deleting a Todos file."
86 (eval (defcustom todos-default-todos-file
(car (funcall todos-files-function
))
87 "Todos file visited by first session invocation of `todos-show'."
88 :type
`(radio ,@(mapcar (lambda (f) (list 'const f
))
89 (mapcar 'todos-short-file-name
90 (funcall todos-files-function
))))
93 (defcustom todos-show-current-file t
94 "Non-nil to make `todos-show' visit the current Todos file.
95 Otherwise, `todos-show' always visits `todos-default-todos-file'."
97 :initialize
'custom-initialize-default
98 :set
'todos-set-show-current-file
101 (defun todos-set-show-current-file (symbol value
)
102 "The :set function for user option `todos-show-current-file'."
103 (custom-set-default symbol value
)
105 (add-hook 'pre-command-hook
'todos-show-current-file nil t
)
106 (remove-hook 'pre-command-hook
'todos-show-current-file t
)))
108 (defcustom todos-visit-files-commands
(list 'find-file
'dired-find-file
)
109 "List of commands to visit files for `todos-after-find-file'.
110 Invoking these commands to visit a Todos or Todos Archive file
111 calls `todos-show' or `todos-show-archive', so that the file is
112 displayed correctly."
113 :type
'(repeat function
)
116 (defcustom todos-initial-file
"Todo"
117 "Default file name offered on adding first Todos file."
121 (defcustom todos-initial-category
"Todo"
122 "Default category name offered on initializing a new Todos file."
126 (defcustom todos-display-categories-first nil
127 "Non-nil to display category list on first visit to a Todos file."
131 (defcustom todos-prefix
""
132 "String prefixed to todo items for visual distinction."
134 :initialize
'custom-initialize-default
135 :set
'todos-reset-prefix
138 (defcustom todos-number-priorities t
139 "Non-nil to prefix items with consecutively increasing integers.
140 These reflect the priorities of the items in each category."
142 :initialize
'custom-initialize-default
143 :set
'todos-reset-prefix
146 (defun todos-reset-prefix (symbol value
)
147 "The :set function for `todos-prefix' and `todos-number-priorities'."
148 (let ((oldvalue (symbol-value symbol
))
149 (files (append todos-files todos-archives
)))
150 (custom-set-default symbol value
)
151 (when (not (equal value oldvalue
))
153 (with-current-buffer (find-file-noselect f
)
154 (save-window-excursion
158 (goto-char (point-min))
160 (remove-overlays (point) (point)); 'before-string prefix)
162 ;; Activate the new setting (save-restriction does not help).
163 (save-excursion (todos-category-select))))))))
165 ;; FIXME: Update when window-width changes. Add todos-reset-separator to
166 ;; window-configuration-change-hook in todos-mode? But this depends on the
167 ;; value being window-width instead of a constant length.
168 (defcustom todos-done-separator
(make-string (window-width) ?_
)
169 "String used to visually separate done from not done items.
170 Displayed as an overlay instead of `todos-done-separator' when
171 done items are shown."
173 :initialize
'custom-initialize-default
174 :set
'todos-reset-separator
177 ;; (defun todos-reset-done-separator (symbol value)
178 ;; "The :set function for `todos-done-separator'
179 ;; Also added to `window-configuration-change-hook' in Todos mode."
180 ;; (let ((oldvalue (symbol-value symbol)))
181 ;; (custom-set-default symbol value)
182 ;; (when (not (equal value oldvalue))
183 ;; (make-string (window-width) ?_)
184 ;; ;; (save-excursion (todos-category-select))
187 (defcustom todos-done-string
"DONE "
188 "Identifying string appended to the front of done todos items."
190 :initialize
'custom-initialize-default
191 :set
'todos-reset-done-string
194 (defun todos-reset-done-string (symbol value
)
195 "The :set function for user option `todos-done-string'."
196 (let ((oldvalue (symbol-value symbol
))
197 (files (append todos-files todos-archives
)))
198 (custom-set-default symbol value
)
199 ;; Need to reset this to get font-locking right.
200 (setq todos-done-string-start
201 (concat "^\\[" (regexp-quote todos-done-string
)))
202 (when (not (equal value oldvalue
))
204 (with-current-buffer (find-file-noselect f
)
205 (let (buffer-read-only)
207 (goto-char (point-min))
209 (if (re-search-forward
210 (concat "^" (regexp-quote todos-nondiary-start
)
211 "\\(" (regexp-quote oldvalue
) "\\)")
213 (replace-match value t t nil
1)
215 (todos-category-select)))))))
217 (defcustom todos-comment-string
"COMMENT"
218 "String inserted before optional comment appended to done item."
220 :initialize
'custom-initialize-default
221 :set
'todos-reset-comment-string
224 (defun todos-reset-comment-string (symbol value
)
225 "The :set function for user option `todos-comment-string'."
226 (let ((oldvalue (symbol-value symbol
))
227 (files (append todos-files todos-archives
)))
228 (custom-set-default symbol value
)
229 (when (not (equal value oldvalue
))
231 (with-current-buffer (find-file-noselect f
)
232 (let (buffer-read-only)
235 (goto-char (point-min))
237 (if (re-search-forward
239 "\\[\\(" (regexp-quote oldvalue
) "\\): [^]]*\\]")
241 (replace-match value t t nil
1)
243 (todos-category-select))))))))
245 (defcustom todos-show-with-done nil
246 "Non-nil to display done items in all categories."
250 (defun todos-mode-line-control (cat)
251 "Return a mode line control for Todos buffers.
252 Argument CAT is the name of the current Todos category.
253 This function is the value of the user variable
254 `todos-mode-line-function'."
255 (let ((file (todos-short-file-name todos-current-todos-file
)))
256 (format "%s category %d: %s" file todos-category-number cat
)))
258 (defcustom todos-mode-line-function
'todos-mode-line-control
259 "Function that returns a mode line control for Todos buffers.
260 The function expects one argument holding the name of the current
261 Todos category. The resulting control becomes the local value of
262 `mode-line-buffer-identification' in each Todos buffer."
266 (defun todos-special-buffer-name (buffer-type file-list
)
267 "Rename Todos special buffer using BUFFER-TYPE and FILE-LIST.
269 The new name is constructed from the string BUFFER-TYPE, which
270 refers to one of the top priorities, diary or regexp item
271 filters, and the names of the filtered files in FILE-LIST. Used
272 in Todos Filter Items mode."
273 (let* ((flist (if (listp file-list
) file-list
(list file-list
)))
274 (multi (> (length flist
) 1))
275 (fnames (mapconcat (lambda (f) (todos-short-file-name f
))
277 (rename-buffer (format (concat "%s for file" (if multi
"s" "")
278 " \"%s\"") buffer-type fnames
))))
280 (defcustom todos-filter-buffer
"Todos filtered items"
281 "Initial name of buffer in Todos Filter Items mode."
285 (defcustom todos-top-priorities-buffer
"Todos top priorities"
286 "Buffer type string for `todos-special-buffer-name'."
290 (defcustom todos-diary-items-buffer
"Todos diary items"
291 "Buffer type string for `todos-special-buffer-name'."
295 (defcustom todos-regexp-items-buffer
"Todos regexp items"
296 "Buffer type string for `todos-special-buffer-name'."
300 (defcustom todos-priorities-rules nil
301 "List of rules giving how many items `todos-top-priorities' shows.
302 This variable should be set interactively by
303 `\\[todos-set-top-priorities-in-file]' or
304 `\\[todos-set-top-priorities-in-category]'.
306 Each rule is a list of the form (FILE NUM ALIST), where FILE is a
307 member of `todos-files', NUM is a number specifying the default
308 number of top priority items for each category in that file, and
309 ALIST, when non-nil, consists of conses of a category name in
310 FILE and a number specifying the default number of top priority
311 items in that category, which overrides NUM."
315 (defcustom todos-show-priorities
1
316 "Default number of top priorities shown by `todos-top-priorities'."
320 (defcustom todos-filter-files nil
321 "List of default files for multifile item filtering."
322 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
323 (mapcar 'todos-short-file-name
324 (funcall todos-files-function
))))
327 ;; FIXME: is there a better alternative to this?
328 (defun todos-reevaluate-filter-files-defcustom ()
329 "Reevaluate defcustom of `todos-filter-files'.
330 Called after adding or deleting a Todos file."
331 (eval (defcustom todos-filter-files nil
332 "List of files for multifile item filtering."
333 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
334 (mapcar 'todos-short-file-name
335 (funcall todos-files-function
))))
338 (defcustom todos-filter-done-items nil
339 "Non-nil to include done items when processing regexp filters.
340 Done items from corresponding archive files are also included."
344 (defcustom todos-ignore-archived-categories nil
345 "Non-nil to skip categories with only archived items when browsing.
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." ;FIXME
354 (defcustom todos-use-only-highlighted-region t
355 "Non-nil to enable inserting only highlighted region as new item."
359 (defcustom todos-include-in-diary nil
360 "Non-nil to allow new Todo items to be included in the diary."
364 (defcustom todos-diary-nonmarking nil
365 "Non-nil to insert new Todo diary items as nonmarking by default.
366 This appends `diary-nonmarking-symbol' to the front of an item on
367 insertion provided it doesn't begin with `todos-nondiary-marker'."
371 (defcustom todos-nondiary-marker
'("[" "]")
372 "List of strings surrounding item date to block diary inclusion.
373 The first string is inserted before the item date and must be a
374 non-empty string that does not match a diary date in order to
375 have its intended effect. The second string is inserted after
377 :type
'(list string string
)
379 :initialize
'custom-initialize-default
380 :set
'todos-reset-nondiary-marker
)
382 (defun todos-reset-nondiary-marker (symbol value
)
383 "The :set function for user option `todos-nondiary-marker'."
384 (let ((oldvalue (symbol-value symbol
))
385 (files (append todos-files todos-archives
)))
386 (custom-set-default symbol value
)
387 ;; Need to reset these to get font-locking right.
388 (setq todos-nondiary-start
(nth 0 todos-nondiary-marker
)
389 todos-nondiary-end
(nth 1 todos-nondiary-marker
)
390 todos-date-string-start
391 ;; See comment in defvar of `todos-date-string-start'.
392 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
393 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
394 (when (not (equal value oldvalue
))
396 (with-current-buffer (find-file-noselect f
)
397 (let (buffer-read-only)
399 (goto-char (point-min))
401 (if (re-search-forward
402 (concat "^\\(" todos-done-string-start
"[^][]+] \\)?"
403 "\\(?1:" (regexp-quote (car oldvalue
))
404 "\\)" todos-date-pattern
"\\( "
405 diary-time-regexp
"\\)?\\(?2:"
406 (regexp-quote (cadr oldvalue
)) "\\)")
409 (replace-match (nth 0 value
) t t nil
1)
410 (replace-match (nth 1 value
) t t nil
2))
412 (todos-category-select)))))))
414 (defcustom todos-print-function
'ps-print-buffer-with-faces
415 "Function called to print buffer content; see `todos-print'."
419 (defcustom todos-completion-ignore-case nil
420 "Non-nil means case of user input in `todos-read-*' is ignored."
424 (defcustom todos-highlight-item nil
425 "Non-nil means highlight items at point."
427 :initialize
'custom-initialize-default
428 :set
'todos-reset-highlight-item
431 (defun todos-reset-highlight-item (symbol value
)
432 "The :set function for `todos-highlight-item'."
433 (let ((oldvalue (symbol-value symbol
))
434 (files (append todos-files todos-archives
)))
435 (custom-set-default symbol value
)
436 (when (not (equal value oldvalue
))
438 (let ((buf (get-file-buffer f
)))
440 (with-current-buffer buf
444 (hl-line-mode -
1)))))))))
446 (defcustom todos-always-add-time-string nil
447 "Non-nil adds current time to a new item's date header by default.
448 When the Todos insertion commands have a non-nil \"maybe-notime\"
449 argument, this reverses the effect of
450 `todos-always-add-time-string': if t, these commands omit the
451 current time, if nil, they include it."
455 (defcustom todos-wrap-lines t
456 "Non-nil to wrap long lines via `todos-line-wrapping-function'."
460 (defcustom todos-line-wrapping-function
'todos-wrap-and-indent
461 "Line wrapping function used with non-nil `todos-wrap-lines'."
465 (defun todos-wrap-and-indent ()
466 "Use word wrapping on long lines and indent with a wrap prefix.
467 The amount of indentation is given by user option
468 `todos-indent-to-here'."
469 (set (make-local-variable 'word-wrap
) t
)
470 (set (make-local-variable 'wrap-prefix
) (make-string todos-indent-to-here
32))
471 (unless (member '(continuation) fringe-indicator-alist
)
472 (push '(continuation) fringe-indicator-alist
)))
474 ;; FIXME: :set function (otherwise change takes effect only after killing and
476 (defcustom todos-indent-to-here
6
477 "Number of spaces `todos-line-wrapping-function' indents to."
478 :type
'(integer :validate
480 (unless (> (widget-value widget
) 0)
481 (widget-put widget
:error
482 "Invalid value: must be a positive integer")
486 (defun todos-indent ()
487 "Indent from point to `todos-indent-to-here'."
488 (indent-to todos-indent-to-here todos-indent-to-here
))
490 (defcustom todos-todo-mode-date-time-regexp
491 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
492 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
493 "Regexp matching legacy todo-mode.el item date-time strings.
494 In order for `todos-convert-legacy-files' to correctly convert this
495 string to the current Todos format, the regexp must contain four
496 explicitly numbered groups (see `(elisp) Regexp Backslash'),
497 where group 1 matches a string for the year, group 2 a string for
498 the month, group 3 a string for the day and group 4 a string for
499 the time. The default value converts date-time strings built
500 using the default value of `todo-time-string-format' from
505 (defgroup todos-categories nil
506 "Faces for Todos Categories mode."
510 (defcustom todos-categories-category-label
"Category"
511 "Category button label in Todos Categories mode."
513 :group
'todos-categories
)
515 (defcustom todos-categories-todo-label
"Todo"
516 "Todo button label in Todos Categories mode."
518 :group
'todos-categories
)
520 (defcustom todos-categories-diary-label
"Diary"
521 "Diary button label in Todos Categories mode."
523 :group
'todos-categories
)
525 (defcustom todos-categories-done-label
"Done"
526 "Done button label in Todos Categories mode."
528 :group
'todos-categories
)
530 (defcustom todos-categories-archived-label
"Archived"
531 "Archived button label in Todos Categories mode."
533 :group
'todos-categories
)
535 (defcustom todos-categories-totals-label
"Totals"
536 "String to label total item counts in Todos Categories mode."
538 :group
'todos-categories
)
540 (defcustom todos-categories-number-separator
" | "
541 "String between number and category in Todos Categories mode.
542 This separates the number from the category name in the default
543 categories display according to priority."
545 :group
'todos-categories
)
547 (defcustom todos-categories-align
'center
548 "Alignment of category names in Todos Categories mode."
549 :type
'(radio (const left
) (const center
) (const right
))
550 :group
'todos-categories
)
552 ;; ---------------------------------------------------------------------------
555 (defgroup todos-faces nil
556 "Faces for the Todos modes."
560 (defface todos-prefix-string
561 ;; '((t :inherit font-lock-constant-face))
562 '((((class grayscale
) (background light
))
563 (:foreground
"LightGray" :weight bold
:underline t
))
564 (((class grayscale
) (background dark
))
565 (:foreground
"Gray50" :weight bold
:underline t
))
566 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
567 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
568 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
569 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
570 (((class color
) (min-colors 8)) (:foreground
"magenta"))
571 (t (:weight bold
:underline t
)))
572 "Face for Todos prefix string."
576 ;; '((t :inherit font-lock-warning-face))
580 (:weight bold
:foreground
"Red1"))
584 (:weight bold
:foreground
"Pink"))
588 (:weight bold
:foreground
"Red1"))
592 (:weight bold
:foreground
"Pink"))
597 (:weight bold
:inverse-video t
)))
598 "Face for marks on Todos items."
601 (defface todos-button
602 ;; '((t :inherit widget-field))
604 (:foreground
"black" :background
"yellow3"))
605 (((class grayscale color
)
607 (:background
"gray85"))
608 (((class grayscale color
)
610 (:background
"dim gray"))
613 "Face for buttons in todos-display-categories."
616 (defface todos-sorted-column
617 ;; '((t :inherit fringe))
620 (:foreground
"grey95"))
623 (:foreground
"grey10"))
625 (:foreground
"gray")))
626 "Face for buttons in todos-display-categories."
629 (defface todos-archived-only
630 ;; '((t (:inherit (shadow))))
633 (:foreground
"grey50"))
636 (:foreground
"grey70"))
638 (:foreground
"gray")))
639 "Face for archived-only categories in todos-display-categories."
642 (defface todos-search
643 ;; '((t :inherit match))
647 (:background
"yellow1"))
651 (:background
"RoyalBlue3"))
655 (:foreground
"black" :background
"yellow"))
659 (:foreground
"white" :background
"blue"))
664 (:background
"gray")))
665 "Face for matches found by todos-search."
668 (defface todos-diary-expired
669 ;; '((t :inherit font-lock-warning-face))
672 (:weight bold
:foreground
"DarkOrange"))
674 (:weight bold
:foreground
"yellow"))
677 "Face for expired dates of diary items."
679 (defvar todos-diary-expired-face
'todos-diary-expired
)
682 '((t :inherit diary
))
683 "Face for the date string of a Todos item."
685 (defvar todos-date-face
'todos-date
)
688 '((t :inherit diary-time
))
689 "Face for the time string of a Todos item."
691 (defvar todos-time-face
'todos-time
)
694 ;; '((t :inherit font-lock-comment-face))
695 '((((class grayscale
)
697 (:slant italic
:weight bold
:foreground
"DimGray"))
700 (:slant italic
:weight bold
:foreground
"LightGray"))
704 (:foreground
"Firebrick"))
708 (:foreground
"chocolate1"))
716 (:foreground
"red1"))
724 (:foreground
"yellow"))
726 (:slant italic
:weight bold
)))
727 "Face for done Todos item header string."
729 (defvar todos-done-face
'todos-done
)
731 (defface todos-comment
732 '((t :inherit todos-done
))
733 "Face for comments appended to done Todos items."
735 (defvar todos-comment-face
'todos-comment
)
737 (defface todos-done-sep
738 ;; '((t :inherit font-lock-type-face))
739 '((((class grayscale
)
741 (:weight bold
:foreground
"Gray90"))
744 (:weight bold
:foreground
"DimGray"))
748 (:foreground
"ForestGreen"))
752 (:foreground
"PaleGreen"))
756 (:foreground
"ForestGreen"))
760 (:foreground
"PaleGreen"))
763 (:foreground
"green"))
765 (:underline t
:weight bold
)))
766 "Face for separator string bewteen done and not done Todos items."
768 (defvar todos-done-sep-face
'todos-done-sep
)
770 (defun todos-date-string-matcher (lim)
771 "Search for Todos date string within LIM for font-locking."
773 (concat todos-date-string-start
"\\(?1:" todos-date-pattern
"\\)") lim t
))
775 (defun todos-time-string-matcher (lim)
776 "Search for Todos time string within LIM for font-locking."
777 (re-search-forward (concat todos-date-string-start todos-date-pattern
778 " \\(?1:" diary-time-regexp
"\\)") lim t
))
780 (defun todos-nondiary-marker-matcher (lim)
781 "Search for Todos nondiary markers within LIM for font-locking."
782 (re-search-forward (concat "^\\(?1:" (regexp-quote todos-nondiary-start
) "\\)"
783 todos-date-pattern
"\\(?: " diary-time-regexp
784 "\\)?\\(?2:" (regexp-quote todos-nondiary-end
) "\\)")
787 (defun todos-diary-nonmarking-matcher (lim)
788 "Search for diary nonmarking symbol within LIM for font-locking."
789 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
790 "\\)" todos-date-pattern
) lim t
))
792 (defun todos-diary-expired-matcher (lim)
793 "Search for expired diary item date within LIM for font-locking."
794 (when (re-search-forward (concat "^\\(?:"
795 (regexp-quote diary-nonmarking-symbol
)
796 "\\)?\\(?1:" todos-date-pattern
"\\) \\(?2:"
797 diary-time-regexp
"\\)?") lim t
)
798 (let* ((date (match-string-no-properties 1))
799 (time (match-string-no-properties 2))
800 ;; days-between needs a non-empty time string.
801 (date-time (concat date
" " (or time
"00:00"))))
802 (or (and (not (string-match ".+day\\|\\*" date
))
803 (< (days-between date-time
(current-time-string)) 0))
804 (todos-diary-expired-matcher lim
)))))
806 (defun todos-done-string-matcher (lim)
807 "Search for Todos done header within LIM for font-locking."
808 (re-search-forward (concat todos-done-string-start
812 (defun todos-comment-string-matcher (lim)
813 "Search for Todos done comment within LIM for font-locking."
814 (re-search-forward (concat "\\[\\(?1:" todos-comment-string
"\\):")
817 ;; (defun todos-category-string-matcher (lim)
818 ;; "Search for Todos category name within LIM for font-locking.
819 ;; This is for fontifying category names appearing in Todos filter
821 ;; (if (eq major-mode 'todos-filter-items-mode)
822 ;; (re-search-forward
823 ;; (concat "^\\(?:" todos-date-string-start "\\)?" todos-date-pattern
824 ;; "\\(?: " diary-time-regexp "\\)?\\(?:"
825 ;; (regexp-quote todos-nondiary-end) "\\)? \\(?1:\\[.+\\]\\)")
828 (defun todos-category-string-matcher-1 (lim)
829 "Search for Todos category name within LIM for font-locking.
830 This is for fontifying category names appearing in Todos filter
831 mode following done items."
832 (if (eq major-mode
'todos-filter-items-mode
)
833 (re-search-forward (concat todos-done-string-start todos-date-pattern
834 "\\(?: " diary-time-regexp
835 ;; Use non-greedy operator to prevent
836 ;; capturing possible following non-diary
838 "\\)?] \\(?1:\\[.+?\\]\\)")
841 (defun todos-category-string-matcher-2 (lim)
842 "Search for Todos category name within LIM for font-locking.
843 This is for fontifying category names appearing in Todos filter
844 mode following todo (not done) items."
845 (if (eq major-mode
'todos-filter-items-mode
)
846 (re-search-forward (concat todos-date-string-start todos-date-pattern
847 "\\(?: " diary-time-regexp
"\\)?\\(?:"
848 (regexp-quote todos-nondiary-end
)
849 "\\)? \\(?1:\\[.+\\]\\)")
852 (defvar todos-font-lock-keywords
854 '(todos-nondiary-marker-matcher 1 todos-done-sep-face t
)
855 '(todos-nondiary-marker-matcher 2 todos-done-sep-face t
)
856 ;; This is the face used by diary-lib.el.
857 '(todos-diary-nonmarking-matcher 1 font-lock-constant-face t
)
858 '(todos-date-string-matcher 1 todos-date-face t
)
859 '(todos-time-string-matcher 1 todos-time-face t
)
860 '(todos-done-string-matcher 0 todos-done-face t
)
861 '(todos-comment-string-matcher 1 todos-done-face t
)
862 ;; '(todos-category-string-matcher 1 todos-done-sep-face t)
863 '(todos-category-string-matcher-1 1 todos-done-sep-face t t
)
864 '(todos-category-string-matcher-2 1 todos-done-sep-face t t
)
865 '(todos-diary-expired-matcher 1 todos-diary-expired-face t
)
866 '(todos-diary-expired-matcher 2 todos-diary-expired-face t t
)
868 "Font-locking for Todos modes.")
870 ;; ---------------------------------------------------------------------------
871 ;;; Todos mode local variables and hook functions
873 (defvar todos-files
(funcall todos-files-function
)
874 "List of truenames of user's Todos files.")
876 (defvar todos-archives
(funcall todos-files-function t
)
877 "List of truenames of user's Todos archives.")
879 (defvar todos-current-todos-file nil
880 "Variable holding the name of the currently active Todos file.")
882 (defun todos-show-current-file ()
883 "Visit current instead of default Todos file with `todos-show'.
884 This function is added to `pre-command-hook' when user option
885 `todos-show-current-file' is set to non-nil."
886 (setq todos-global-current-todos-file todos-current-todos-file
))
888 (defun todos-after-find-file ()
889 "Show Todos files correctly when visited from outside of Todos mode."
890 (and (member this-command todos-visit-files-commands
)
891 (= (- (point-max) (point-min)) (buffer-size))
892 (member major-mode
'(todos-mode todos-archive-mode
))
893 (todos-category-select)))
895 ;; FIXME: this slows down killing Todos buffer noticeably
896 (defun todos-reset-global-current-todos-file ()
897 "Update the value of `todos-global-current-todos-file'.
898 This becomes the latest existing Todos file or, if there is none,
899 the value of `todos-default-todos-file'.
900 This function is added to `kill-buffer-hook' in Todos mode."
901 (let ((todos-buffer-list (nreverse
905 'file-name-nondirectory
906 (funcall todos-files-function
))))
907 (mapcar 'buffer-name
(buffer-list)))))
909 (setq latest
(find-if-not (lambda (f) (string= f
(buffer-name)))
911 (setq todos-global-current-todos-file
(or latest todos-default-todos-file
))))
913 (defvar todos-categories nil
914 "Alist of categories in the current Todos file.
915 The elements are cons cells whose car is a category name and
916 whose cdr is a vector of the category's item counts. These are,
917 in order, the numbers of todo items, of todo items included in
918 the Diary, of done items and of archived items.")
920 (defvar todos-categories-with-marks nil
921 "Alist of categories and number of marked items they contain.")
923 (defvar todos-category-number
1
924 "Variable holding the number of the current Todos category.
925 Todos categories are numbered starting from 1.")
927 (defvar todos-first-visit t
928 "Non-nil if first display of this file in the current session.
929 See `todos-display-categories-first'.")
931 (defvar todos-show-done-only nil
932 "If non-nil display only done items in current category.
933 Set by the command `todos-show-done-only' and used by
934 `todos-category-select'.")
936 ;; ---------------------------------------------------------------------------
937 ;;; Global variables and helper functions
939 (defvar todos-global-current-todos-file nil
940 "Variable holding name of current Todos file.
941 Used by functions called from outside of Todos mode to visit the
942 current Todos file rather than the default Todos file (i.e. when
943 users option `todos-show-current-file' is non-nil).")
945 (defun todos-reevaluate-defcustoms ()
946 "Reevaluate defcustoms that provide choice list of Todos files."
947 (custom-set-default 'todos-default-todos-file
948 (symbol-value 'todos-default-todos-file
))
949 (todos-reevaluate-default-file-defcustom)
950 (custom-set-default 'todos-filter-files
(symbol-value 'todos-filter-files
))
951 (todos-reevaluate-filter-files-defcustom))
953 (defvar todos-edit-buffer
"*Todos Edit*"
954 "Name of current buffer in Todos Edit mode.")
956 (defvar todos-categories-buffer
"*Todos Categories*"
957 "Name of buffer in Todos Categories mode.")
959 (defvar todos-print-buffer
"*Todos Print*"
960 "Name of buffer containing printable Todos text.")
962 (defvar todos-date-pattern
963 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
964 (concat "\\(?:" dayname
"\\|"
966 ;; FIXME: how to choose between abbreviated and unabbreviated
968 (monthname (format "\\(?:%s\\|\\*\\)"
970 calendar-month-name-array
971 calendar-month-abbrev-array t
)))
972 (month "\\(?:[0-9]+\\|\\*\\)")
973 (day "\\(?:[0-9]+\\|\\*\\)")
974 (year "-?\\(?:[0-9]+\\|\\*\\)"))
975 (mapconcat 'eval calendar-date-display-form
""))
977 "Regular expression matching a Todos date header.")
979 (defvar todos-nondiary-start
(nth 0 todos-nondiary-marker
)
980 "String inserted before item date to block diary inclusion.")
982 (defvar todos-nondiary-end
(nth 1 todos-nondiary-marker
)
983 "String inserted after item date matching `todos-nondiary-start'.")
985 ;; By itself this matches anything, because of the `?'; however, it's only
986 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
988 (defvar todos-date-string-start
989 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
990 (regexp-quote diary-nonmarking-symbol
) "\\)?")
991 "Regular expression matching part of item header before the date.")
993 (defvar todos-done-string-start
994 (concat "^\\[" (regexp-quote todos-done-string
))
995 "Regular expression matching start of done item.")
997 (defun todos-category-number (cat)
998 "Return the number of category CAT in this Todos file.
999 The buffer-local variable `todos-category-number' holds this
1000 number as its value."
1001 (let ((categories (mapcar 'car todos-categories
)))
1002 (setq todos-category-number
1003 ;; Increment by one, so that the highest priority category in Todos
1004 ;; Categories mode is numbered one rather than zero.
1005 (1+ (- (length categories
)
1006 (length (member cat categories
)))))))
1008 (defun todos-current-category ()
1009 "Return the name of the current category."
1010 (car (nth (1- todos-category-number
) todos-categories
)))
1012 (defconst todos-category-beg
"--==-- "
1013 "String marking beginning of category (inserted with its name).")
1015 (defconst todos-category-done
"==--== DONE "
1016 "String marking beginning of category's done items.")
1018 (defun todos-category-select ()
1019 "Display the current category correctly."
1020 (let ((name (todos-current-category))
1021 cat-begin cat-end done-start done-sep-start done-end
)
1023 (goto-char (point-min))
1025 (concat "^" (regexp-quote (concat todos-category-beg name
)) "$") nil t
)
1026 (setq cat-begin
(1+ (line-end-position)))
1027 (setq cat-end
(if (re-search-forward
1028 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
1031 (setq mode-line-buffer-identification
1032 (funcall todos-mode-line-function name
))
1033 (narrow-to-region cat-begin cat-end
)
1034 (todos-prefix-overlays)
1035 (goto-char (point-min))
1036 (if (re-search-forward (concat "\n\\(" (regexp-quote todos-category-done
)
1039 (setq done-start
(match-beginning 0))
1040 (setq done-sep-start
(match-beginning 1))
1041 (setq done-end
(match-end 0)))
1042 (error "Category %s is missing todos-category-done string" name
))
1043 (if todos-show-done-only
1044 (narrow-to-region (1+ done-end
) (point-max))
1045 (when (and todos-show-with-done
1046 (re-search-forward todos-done-string-start nil t
))
1047 ;; Now we want to see the done items, so reset displayed end to end of
1049 (setq done-start cat-end
)
1050 ;; Make display overlay for done items separator string, unless there
1052 (let* ((done-sep todos-done-separator
)
1053 (ovs (overlays-at done-sep-start
))
1055 (unless (and ovs
(string= (overlay-get (car ovs
) 'display
) done-sep
))
1056 (setq ov-sep
(make-overlay done-sep-start done-end
))
1057 (overlay-put ov-sep
'display done-sep
))))
1058 (narrow-to-region (point-min) done-start
)
1059 ;; Loading this from todos-mode, or adding it to the mode hook, causes
1060 ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
1061 (when todos-highlight-item
1063 (hl-line-mode 1)))))
1065 (defun todos-get-count (type &optional category
)
1066 "Return count of TYPE items in CATEGORY.
1067 If CATEGORY is nil, default to the current category."
1068 (let* ((cat (or category
(todos-current-category)))
1069 (counts (cdr (assoc cat todos-categories
)))
1070 (idx (cond ((eq type
'todo
) 0)
1071 ((eq type
'diary
) 1)
1073 ((eq type
'archived
) 3))))
1076 (defun todos-update-count (type increment
&optional category
)
1077 "Change count of TYPE items in CATEGORY by integer INCREMENT.
1078 With nil or omitted CATEGORY, default to the current category."
1079 (let* ((cat (or category
(todos-current-category)))
1080 (counts (cdr (assoc cat todos-categories
)))
1081 (idx (cond ((eq type
'todo
) 0)
1082 ((eq type
'diary
) 1)
1084 ((eq type
'archived
) 3))))
1085 (aset counts idx
(+ increment
(aref counts idx
)))))
1087 (defun todos-set-categories () ;FIXME: need this?
1088 "Set `todos-categories' from the sexp at the top of the file."
1089 ;; New archive files created by `todos-move-category' are empty, which would
1090 ;; make the sexp test fail and raise an error, so in this case we skip it.
1091 (unless (zerop (buffer-size))
1095 (goto-char (point-min))
1096 (setq todos-categories
1097 (if (looking-at "\(\(\"")
1098 (read (buffer-substring-no-properties
1099 (line-beginning-position)
1100 (line-end-position)))
1101 (error "Invalid or missing todos-categories sexp")))))))
1103 (defun todos-update-categories-sexp ()
1104 "Update the `todos-categories' sexp at the top of the file."
1105 (let (buffer-read-only)
1109 (goto-char (point-min))
1110 (if (looking-at (concat "^" (regexp-quote todos-category-beg
)))
1111 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
1112 ;; No categories sexp means the first item was just added
1113 ;; to this file, so have to initialize Todos file and
1114 ;; categories variables in order e.g. to enable categories
1116 (setq todos-default-todos-file
(buffer-file-name))
1117 (setq todos-categories
(todos-make-categories-list t
)))
1118 ;; With empty buffer (e.g. with new archive in
1119 ;; `todos-move-category') `kill-line' signals end of buffer.
1120 (kill-region (line-beginning-position) (line-end-position)))
1121 (prin1 todos-categories
(current-buffer))))))
1123 (defun todos-make-categories-list (&optional force
)
1124 "Return an alist of Todos categories and their item counts.
1125 With non-nil argument FORCE parse the entire file to build the
1126 list; otherwise, get the value by reading the sexp at the top of
1128 (setq todos-categories nil
)
1132 (goto-char (point-min))
1133 (let (counts cat archive
)
1134 (when buffer-file-name
; Don't check with `todos-convert-legacy-files'.
1135 ;; FIXME: can todos-archives be too old here?
1136 (unless (member buffer-file-name
(funcall todos-files-function t
))
1137 (setq archive
(concat (file-name-sans-extension
1138 todos-current-todos-file
) ".toda"))))
1140 (cond ((looking-at (concat (regexp-quote todos-category-beg
)
1142 (setq cat
(match-string-no-properties 1))
1143 ;; Counts for each category: [todo diary done archive]
1144 (setq counts
(make-vector 4 0))
1145 (setq todos-categories
1146 (append todos-categories
(list (cons cat counts
))))
1147 ;; todos-archives may be too old here (e.g. during
1148 ;; todos-move-category).
1149 (when (member archive
(funcall todos-files-function t
))
1150 (let ((archive-count 0))
1151 (with-current-buffer (find-file-noselect archive
)
1153 (goto-char (point-min))
1154 (when (re-search-forward
1155 (concat (regexp-quote todos-category-beg
) cat
)
1158 (while (not (or (looking-at
1160 (regexp-quote todos-category-beg
)
1163 (when (looking-at todos-done-string-start
)
1164 (setq archive-count
(1+ archive-count
)))
1166 (todos-update-count 'archived archive-count cat
))))
1167 ((looking-at todos-done-string-start
)
1168 (todos-update-count 'done
1 cat
))
1169 ((looking-at (concat "^\\("
1170 (regexp-quote diary-nonmarking-symbol
)
1171 "\\)?" todos-date-pattern
))
1172 (todos-update-count 'diary
1 cat
)
1173 (todos-update-count 'todo
1 cat
))
1174 ((looking-at (concat todos-date-string-start todos-date-pattern
))
1175 (todos-update-count 'todo
1 cat
))
1176 ;; If first line is todos-categories list, use it and end loop
1177 ;; -- unless FORCEd to scan whole file.
1180 (setq todos-categories
(read (buffer-substring-no-properties
1181 (line-beginning-position)
1182 (line-end-position))))
1183 (goto-char (1- (point-max))))))
1187 (defun todos-check-format ()
1188 "Signal an error if the current Todos file is ill-formatted.
1189 Otherwise return t. The error message gives the line number
1190 where the invalid formatting was found."
1194 (goto-char (point-min))
1195 ;; Check for `todos-categories' sexp as the first line
1196 (let ((cats (prin1-to-string todos-categories
)))
1197 (unless (looking-at (regexp-quote cats
))
1198 (error "Invalid or missing todos-categories sexp")))
1200 (let ((legit (concat "\\(^" (regexp-quote todos-category-beg
) "\\)"
1201 "\\|\\(" todos-date-string-start todos-date-pattern
"\\)"
1202 "\\|\\(^[ \t]+[^ \t]*\\)"
1204 "\\|\\(^" (regexp-quote todos-category-done
) "\\)"
1205 "\\|\\(" todos-done-string-start
"\\)")))
1207 (unless (looking-at legit
)
1208 (error "Illegitimate Todos file format at line %d"
1209 (line-number-at-pos (point))))
1211 ;; (message "This Todos file is well-formatted.")
1214 (defun todos-repair-categories-sexp ()
1215 "Repair corrupt Todos categories sexp.
1216 This should only be needed as a consequence of careless manual
1217 editing or a bug in todos.el."
1219 (let ((todos-categories (todos-make-categories-list t
)))
1220 (todos-update-categories-sexp)))
1222 (defvar todos-item-start
(concat "\\(" todos-date-string-start
"\\|"
1223 todos-done-string-start
"\\)"
1225 "String identifying start of a Todos item.")
1227 (defun todos-item-start ()
1228 "Move to start of current Todos item and return its position."
1230 ;; Point is either on last item in this category or on the empty
1231 ;; line between done and not done items.
1233 ;; There are no done items in this category yet.
1234 (looking-at (regexp-quote todos-category-beg
)))
1235 (goto-char (line-beginning-position))
1236 (while (not (looking-at todos-item-start
))
1240 (defun todos-item-end ()
1241 "Move to end of current Todos item and return its position."
1242 ;; Items cannot end with a blank line.
1243 (unless (looking-at "^$")
1244 (let ((done (todos-done-item-p)))
1245 ;; FIXME: don't use a command to define this function!
1246 (todos-forward-item)
1247 ;; Adjust if item is last unfinished one before displayed done items.
1248 (when (and (not done
) (todos-done-item-p))
1253 (defun todos-item-string ()
1254 "Return bare text of current item as a string."
1255 (let ((opoint (point))
1256 (start (todos-item-start))
1257 (end (todos-item-end)))
1259 (and start end
(buffer-substring-no-properties start end
))))
1261 (defun todos-remove-item ()
1262 "Internal function called in editing, deleting or moving items."
1263 (let* ((beg (todos-item-start))
1264 (end (progn (todos-item-end) (1+ (point))))
1265 (ovs (overlays-in beg beg
)))
1266 ;; There can be both prefix/number and mark overlays.
1267 (while ovs
(delete-overlay (car ovs
)) (pop ovs
))
1268 (delete-region beg end
)))
1270 (defun todos-diary-item-p ()
1271 "Return non-nil if item at point has diary entry format."
1274 (not (looking-at (regexp-quote todos-nondiary-start
)))))
1276 (defun todos-done-item-p ()
1277 "Return non-nil if item at point is a done item."
1280 (looking-at todos-done-string-start
)))
1282 (defvar todos-item-mark
(propertize (if (equal todos-prefix
"*") "@" "*")
1284 "String used to mark items.")
1286 (defun todos-marked-item-p ()
1287 "If this item begins with `todos-item-mark', return mark overlay."
1288 (let ((ovs (overlays-in (line-beginning-position) (line-beginning-position)))
1289 (mark todos-item-mark
)
1294 (and (equal (overlay-get ov
'before-string
) mark
)
1295 (throw 'stop
(setq marked t
)))))
1298 (defun todos-insert-with-overlays (item)
1299 "Insert ITEM at point and update prefix/priority number overlays."
1302 (todos-backward-item)
1303 (todos-prefix-overlays))
1305 (defun todos-prefix-overlays ()
1306 "Put before-string overlay in front of this category's items.
1307 The overlay's value is the string `todos-prefix' or with non-nil
1308 `todos-number-priorities' an integer in the sequence from 1 to
1309 the number of todo or done items in the category indicating the
1310 item's priority. Todo and done items are numbered independently
1312 (when (or todos-number-priorities
1313 (not (string-match "^[[:space:]]*$" todos-prefix
)))
1314 (let ((prefix (propertize (concat todos-prefix
" ")
1315 'face
'todos-prefix-string
))
1318 (goto-char (point-min))
1320 (when (or (todos-date-string-matcher (line-end-position))
1321 (todos-done-string-matcher (line-end-position)))
1322 (goto-char (match-beginning 0))
1323 (when todos-number-priorities
1325 ;; Reset number to 1 for first done item.
1326 (when (and (looking-at todos-done-string-start
)
1327 (looking-back (concat "^"
1328 (regexp-quote todos-category-done
)
1331 (setq prefix
(propertize (concat (number-to-string num
) " ")
1332 'face
'todos-prefix-string
)))
1333 (let ((ovs (overlays-in (point) (point)))
1337 (let ((val (overlay-get ov
'before-string
)))
1340 (setq ov-pref val
)))))
1341 (unless (equal ov-pref prefix
)
1342 ;; Why doesn't this work?
1343 ;; (remove-overlays (point) (point) 'before-string)
1344 (remove-overlays (point) (point))
1345 (overlay-put (make-overlay (point) (point))
1346 'before-string prefix
)
1347 (and marked
(overlay-put (make-overlay (point) (point))
1348 'before-string todos-item-mark
)))))
1351 (defun todos-read-file-name (prompt &optional archive mustmatch
)
1352 "Choose and return the name of a Todos file, prompting with PROMPT.
1354 Show completions with TAB or SPC; the names are shown in short
1355 form but the absolute truename is returned. With non-nil ARCHIVE
1356 return the absolute truename of a Todos archive file. With non-nil
1357 MUSTMATCH the name of an existing file must be chosen;
1358 otherwise, a new file name is allowed."
1359 (let* ((completion-ignore-case todos-completion-ignore-case
)
1360 (files (mapcar 'todos-short-file-name
1361 (if archive todos-archives todos-files
)))
1362 (file (completing-read prompt files nil mustmatch nil nil
1364 ;; Trigger prompt for initial file.
1366 (unless (file-exists-p todos-files-directory
)
1367 (make-directory todos-files-directory
))
1369 (setq file
(todos-validate-name file
'file
)))
1370 (setq file
(file-truename (concat todos-files-directory file
1371 (if archive
".toda" ".todo"))))))
1373 (defun todos-read-category (prompt &optional mustmatch added
)
1374 "Choose and return a category name, prompting with PROMPT.
1375 Show completions with TAB or SPC. With non-nil MUSTMATCH the
1376 name must be that of an existing category; otherwise, a new
1377 category name is allowed, after checking its validity. Non-nil
1378 argument ADDED means the caller is todos-add-category, so don't
1379 ask whether to add the category."
1380 ;; Allow SPC to insert spaces, for adding new category names.
1381 (let ((map minibuffer-local-completion-map
))
1382 (define-key map
" " nil
)
1383 ;; Make a copy of todos-categories in case history-delete-duplicates is
1384 ;; non-nil, which makes completing-read alter todos-categories.
1385 (let* ((categories (copy-sequence todos-categories
))
1386 (history (cons 'todos-categories
(1+ todos-category-number
)))
1387 (completion-ignore-case todos-completion-ignore-case
)
1388 (cat (completing-read prompt todos-categories nil
1389 mustmatch nil history
1390 ;; Default for existing categories is the
1391 ;; current category.
1392 (if todos-categories
1393 (todos-current-category)
1394 ;; Trigger prompt for initial category.
1396 (unless (or mustmatch
(assoc cat todos-categories
))
1397 (todos-validate-name cat
'category
)
1399 (if (y-or-n-p (format (concat "There is no category \"%s\" in "
1400 "this file; add it? ") cat
))
1401 (todos-add-category cat
)
1403 ;; Restore the original value of todos-categories unless a new category
1404 ;; was added (since todos-add-category changes todos-categories).
1405 (unless added
(setq todos-categories categories
))
1408 (defun todos-validate-name (name type
)
1409 "Prompt for new NAME for TYPE until it is valid, then return it.
1410 TYPE can be either a file or a category"
1411 (let ((categories todos-categories
)
1412 (files (mapcar 'todos-short-file-name todos-files
))
1415 (and (cond ((string= "" name
)
1417 (cond ((eq type
'file
)
1419 "Enter a non-empty file name: "
1420 ;; Empty string passed by todos-show to
1421 ;; prompt for initial Todos file.
1422 (concat "Initial file name ["
1423 todos-initial-file
"]: ")))
1424 ((eq type
'category
)
1425 (if todos-categories
1426 "Enter a non-empty category name: "
1427 ;; Empty string passed by todos-show to
1428 ;; prompt for initial category of a new
1430 (concat "Initial category name ["
1431 todos-initial-category
"]: "))))))
1432 ((string-match "\\`\\s-+\\'" name
)
1434 "Enter a name that does not contain only white space: "))
1435 ((and (eq type
'file
) (member name todos-files
))
1436 (setq prompt
"Enter a non-existing file name: "))
1437 ((and (eq type
'category
) (assoc name todos-categories
))
1438 (setq prompt
"Enter a non-existing category name: ")))
1439 (setq name
(if (or (and (eq type
'file
) todos-files
)
1440 (and (eq type
'category
) todos-categories
))
1441 (completing-read prompt
(cond ((eq type
'file
)
1443 ((eq type
'category
)
1445 ;; Offer default initial name.
1446 (completing-read prompt
(if (eq type
'file
)
1449 nil nil
(if (eq type
'file
)
1451 todos-initial-category
))))))
1454 ;; Adapted from calendar-read-date and calendar-date-string.
1455 (defun todos-read-date ()
1456 "Prompt for Gregorian date and return it in the current format.
1457 Also accepts `*' as an unspecified month, day, or year."
1458 (let* ((year (calendar-read
1459 ;; FIXME: maybe better like monthname with RET for current month
1460 "Year (>0 or * for any year): "
1461 (lambda (x) (or (eq x
'*) (> x
0)))
1462 (number-to-string (calendar-extract-year
1463 (calendar-current-date)))))
1464 (month-array (vconcat calendar-month-name-array
(vector "*")))
1465 (abbrevs (vconcat calendar-month-abbrev-array
(vector "*")))
1466 (completion-ignore-case todos-completion-ignore-case
)
1467 (monthname (completing-read
1468 "Month name (RET for current month, * for any month): "
1469 (mapcar 'list
(append month-array nil
))
1471 (calendar-month-name (calendar-extract-month
1472 (calendar-current-date)) t
)))
1473 (month (cdr (assoc-string
1474 monthname
(calendar-make-alist month-array nil nil
1476 (last (if (= month
13)
1477 31 ; FIXME: what about shorter months?
1478 (let ((yr (if (eq year
'*)
1479 1999 ; FIXME: no Feb. 29
1481 (calendar-last-day-of-month month yr
))))
1483 (while (if (numberp day
) (or (< day
0) (< last day
)) (not (eq day
'*)))
1484 (setq day
(read-from-minibuffer
1485 (format "Day (1-%d or RET for today or * for any day): " last
)
1488 (calendar-extract-day (calendar-current-date))))))
1489 (setq year
(if (eq year
'*) (symbol-name '*) (number-to-string year
)))
1490 (setq day
(if (eq day
'*) (symbol-name '*) (number-to-string day
)))
1491 ;; FIXME: make abbreviation customizable
1493 (or (and (= month
13) "*")
1494 (calendar-month-name (calendar-extract-month (list month day year
))
1496 (mapconcat 'eval calendar-date-display-form
"")))
1498 (defun todos-read-dayname ()
1499 "Choose name of a day of the week with completion and return it."
1500 (let ((completion-ignore-case todos-completion-ignore-case
))
1501 (completing-read "Enter a day name: "
1502 (append calendar-day-name-array nil
)
1505 (defun todos-read-time ()
1506 "Prompt for and return a valid clock time as a string.
1508 Valid time strings are those matching `diary-time-regexp'.
1509 Typing `<return>' at the prompt returns the current time, if the
1510 user option `todos-always-add-time-string' is non-nil, otherwise
1511 the empty string (i.e., no time string)."
1514 (setq answer
(read-string "Enter a clock time: " nil nil
1515 (when todos-always-add-time-string
1516 (substring (current-time-string) 11 16))))
1517 (when (or (string= "" answer
)
1518 (string-match diary-time-regexp answer
))
1522 (defun todos-convert-legacy-date-time ()
1523 "Return converted date-time string.
1524 Helper function for `todos-convert-legacy-files'."
1525 (let* ((year (match-string 1))
1526 (month (match-string 2))
1527 (monthname (calendar-month-name (string-to-number month
) t
))
1528 (day (match-string 3))
1529 (time (match-string 4))
1532 (insert (mapconcat 'eval calendar-date-display-form
"")
1533 (when time
(concat " " time
)))))
1535 ;; ---------------------------------------------------------------------------
1538 (defvar todos-multiple-files nil
1539 "List of files selected from `todos-multiple-files' widget.")
1541 (defvar todos-multiple-files-widget nil
1542 "Variable holding widget created by `todos-multiple-files'.")
1544 (defun todos-multiple-files ()
1545 "Pop to a buffer with a widget for choosing multiple filter files."
1548 (require 'wid-edit
))
1549 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
1550 (pop-to-buffer (current-buffer))
1552 (kill-all-local-variables)
1553 (widget-insert "Select files for generating the top priorities list.\n\n")
1554 (setq todos-multiple-files-widget
1556 `(set ,@(mapcar (lambda (x) (list 'const x
))
1557 (mapcar 'todos-short-file-name
1558 (funcall todos-files-function
))))))
1559 (widget-insert "\n")
1560 (widget-create 'push-button
1561 :notify
(lambda (widget &rest ignore
)
1562 (setq todos-multiple-files
'quit
)
1564 (exit-recursive-edit))
1567 (widget-create 'push-button
1568 :notify
(lambda (&rest ignore
)
1569 (setq todos-multiple-files
1571 (concat todos-files-directory
1574 todos-multiple-files-widget
)))
1576 (exit-recursive-edit))
1578 (use-local-map widget-keymap
)
1580 (message "Click \"Apply\" after selecting files.")
1583 (defun todos-filter-items (filter &optional multifile
)
1584 "Build and display a list of items from different categories.
1586 The items are selected according to the value of FILTER, which
1587 can be `top' for top priority items, `diary' for diary items,
1588 `regexp' for items matching a regular expresion entered by the
1589 user, or a cons cell of one of these symbols and a number set by
1590 the calling command, which overrides `todos-show-priorities'.
1592 With non-nil argument MULTIFILE list top priorities of multiple
1593 Todos files, by default those in `todos-filter-files'."
1594 (let ((num (if (consp filter
) (cdr filter
) todos-show-priorities
))
1595 (buf (get-buffer-create todos-filter-buffer
))
1596 (files (list todos-current-todos-file
))
1597 regexp fname bufstr cat beg end done
)
1599 (setq files
(or todos-multiple-files
; Passed from todos-*-multifile.
1600 (if (or (consp filter
)
1601 (null todos-filter-files
))
1602 (progn (todos-multiple-files) todos-multiple-files
)
1603 todos-filter-files
))
1604 todos-multiple-files nil
))
1605 (if (eq files
'quit
) (keyboard-quit))
1607 (error "No files have been chosen for filtering")
1608 (with-current-buffer buf
1610 (kill-all-local-variables)
1611 (todos-filter-items-mode))
1612 (when (eq filter
'regexp
)
1613 (setq regexp
(read-string "Enter a regular expression: ")))
1614 (save-current-buffer
1616 ;; Before inserting file contents into temp buffer, save a modified
1617 ;; buffer visiting it.
1618 (let ((bf (find-buffer-visiting f
)))
1619 (when (buffer-modified-p bf
)
1620 (with-current-buffer bf
(save-buffer))))
1621 (setq fname
(todos-short-file-name f
))
1623 (when (and todos-filter-done-items
(eq filter
'regexp
))
1624 ;; If there is a corresponding archive file for the Todos file,
1625 ;; insert it first and add identifiers for todos-jump-to-item.
1626 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
1627 (when (file-exists-p arch
)
1628 (insert-file-contents arch
)
1629 ;; Delete Todos archive file categories sexp.
1630 (delete-region (line-beginning-position)
1631 (1+ (line-end-position)))
1634 (when (re-search-forward
1635 (concat (if todos-filter-done-items
1636 (concat "\\(?:" todos-done-string-start
1637 "\\|" todos-date-string-start
1639 todos-date-string-start
)
1640 todos-date-pattern
"\\(?: "
1641 diary-time-regexp
"\\)?"
1642 (if todos-filter-done-items
1644 (regexp-quote todos-nondiary-end
)) "?")
1646 (insert "(archive) "))
1648 (insert-file-contents f
)
1649 ;; Delete Todos file categories sexp.
1650 (delete-region (line-beginning-position) (1+ (line-end-position)))
1652 ;; Unless the number of items to show was supplied by prefix
1653 ;; argument of caller, override `todos-show-priorities' with the
1654 ;; file-wide value from `todos-priorities-rules'.
1655 (unless (consp filter
)
1656 (setq fnum
(nth 1 (assoc f todos-priorities-rules
))))
1657 (while (re-search-forward
1658 (concat "^" (regexp-quote todos-category-beg
) "\\(.+\\)\n")
1660 (setq cat
(match-string 1))
1662 ;; Unless the number of items to show was supplied by prefix
1663 ;; argument of caller, override the file-wide value from
1664 ;; `todos-priorities-rules' if set, else
1665 ;; `todos-show-priorities' with non-nil category-wide value
1666 ;; from `todos-priorities-rules'.
1667 (unless (consp filter
)
1668 (let ((cats (nth 2 (assoc f todos-priorities-rules
))))
1669 (setq cnum
(or (cdr (assoc cat cats
))
1671 ;; FIXME: need this?
1672 todos-show-priorities
))))
1673 (delete-region (match-beginning 0) (match-end 0))
1674 (setq beg
(point)) ; First item in the current category.
1675 (setq end
(if (re-search-forward
1676 (concat "^" (regexp-quote todos-category-beg
))
1682 (if (re-search-forward
1683 (concat "\n" (regexp-quote todos-category-done
))
1687 (unless (and todos-filter-done-items
(eq filter
'regexp
))
1688 ;; Leave done items.
1689 (delete-region done end
)
1691 (narrow-to-region beg end
) ; Process only current category.
1692 (goto-char (point-min))
1693 ;; Apply the filter.
1694 (cond ((eq filter
'diary
)
1696 (if (looking-at (regexp-quote todos-nondiary-start
))
1698 (todos-forward-item))))
1699 ((eq filter
'regexp
)
1701 (if (looking-at todos-item-start
)
1702 (if (string-match regexp
(todos-item-string))
1703 (todos-forward-item)
1704 (todos-remove-item))
1705 ;; Kill lines that aren't part of a todo or done
1706 ;; item (empty or todos-category-done).
1707 (delete-region (line-beginning-position)
1708 (1+ (line-end-position))))
1709 ;; If last todo item in file matches regexp and
1710 ;; there are no following done items,
1711 ;; todos-category-done string is left dangling,
1712 ;; because todos-forward-item jumps over it.
1715 (concat (regexp-quote todos-done-string
)
1717 (delete-region (point) (progn
1720 (t ; Filter top priority items.
1721 (setq num
(or cnum fnum num
))
1723 (todos-forward-item num
))))
1725 ;; Delete non-top-priority items.
1726 (unless (member filter
'(diary regexp
))
1727 (delete-region beg end
))
1728 (goto-char (point-min))
1729 ;; Add file (if using multiple files) and category tags to
1732 (when (re-search-forward
1733 (concat (if todos-filter-done-items
1734 (concat "\\(?:" todos-done-string-start
1735 "\\|" todos-date-string-start
1737 todos-date-string-start
)
1738 todos-date-pattern
"\\(?: " diary-time-regexp
1739 "\\)?" (if todos-filter-done-items
1741 (regexp-quote todos-nondiary-end
))
1745 (when (looking-at "(archive) ") (goto-char (match-end 0)))
1746 (insert (if multifile
(concat fname
":") "") cat
"]"))
1749 (setq bufstr
(buffer-string))
1750 (with-current-buffer buf
1751 (let (buffer-read-only)
1752 (insert bufstr
)))))))
1753 (set-window-buffer (selected-window) (set-buffer buf
))
1754 (todos-prefix-overlays)
1755 (goto-char (point-min)))))
1757 (defun todos-set-top-priorities (&optional arg
)
1758 "Set number of top priorities shown by `todos-top-priorities'.
1759 With non-nil ARG, set the number only for the current Todos
1760 category; otherwise, set the number for all categories in the
1763 Calling this function via either of the commands
1764 `todos-set-top-priorities-in-file' or
1765 `todos-set-top-priorities-in-category' is the recommended way to
1766 set the user customizable option `todos-priorities-rules'."
1767 (let* ((cat (todos-current-category))
1768 (file todos-current-todos-file
)
1769 (rules todos-priorities-rules
)
1770 (frule (assoc-string file rules
))
1771 (crule (assoc-string cat
(nth 2 frule
)))
1772 (cur (or (if arg
(cdr crule
) (nth 1 frule
))
1773 todos-show-priorities
))
1774 (prompt (concat "Current number of top priorities in this "
1775 (if arg
"category" "file") ": %d; "
1776 "enter new number: "))
1779 (while (or (not (string-match "[0-9]+" new
)) ; Don't accept "" or "bla".
1780 (< (string-to-number new
) 0))
1782 (setq new
(read-string (format prompt cur0
) nil nil cur0
)
1783 prompt
"Enter a non-negative number: "
1785 (setq new
(string-to-number new
))
1787 (append (nth 2 (delete crule frule
)) (list (cons cat new
)))
1788 (append (list file new
) (list (nth 2 frule
)))))
1789 (setq rules
(cons (if arg
1790 (list file cur nrule
)
1792 (delete frule rules
)))
1793 (customize-save-variable 'todos-priorities-rules rules
)))
1796 ;; ---------------------------------------------------------------------------
1797 ;;; Sorting and display routines for Todos Categories mode.
1799 (defun todos-longest-category-name-length (categories)
1800 "Return the length of the longest name in list CATEGORIES."
1802 (dolist (c categories longest
)
1803 (setq longest
(max longest
(length c
))))))
1805 (defun todos-padded-string (str)
1806 "Return string STR padded with spaces.
1807 The placement of the padding is determined by the value of user
1808 option `todos-categories-align'."
1809 (let* ((categories (mapcar 'car todos-categories
))
1810 (len (max (todos-longest-category-name-length categories
)
1811 (length todos-categories-category-label
)))
1812 (strlen (length str
))
1813 (strlen-odd (eq (logand strlen
1) 1)) ; oddp from cl.el
1814 (padding (max 0 (/ (- len strlen
) 2)))
1815 (padding-left (cond ((eq todos-categories-align
'left
) 0)
1816 ((eq todos-categories-align
'center
) padding
)
1817 ((eq todos-categories-align
'right
)
1818 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
1819 (padding-right (cond ((eq todos-categories-align
'left
)
1820 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
1821 ((eq todos-categories-align
'center
)
1822 (if strlen-odd
(1+ padding
) padding
))
1823 ((eq todos-categories-align
'right
) 0))))
1824 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
1826 (defvar todos-descending-counts nil
1827 "List of keys for category counts sorted in descending order.")
1829 (defun todos-sort (list &optional key
)
1830 "Return a copy of LIST, possibly sorted according to KEY."
1831 (let* ((l (copy-sequence list
))
1832 (fn (if (eq key
'alpha
)
1833 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
1834 (lambda (x) (todos-get-count key x
))))
1835 (descending (member key todos-descending-counts
))
1836 (cmp (if (eq key
'alpha
)
1838 (if descending
'< '>)))
1839 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
1840 (t2 (funcall fn
(car s2
))))
1841 (funcall cmp t1 t2
)))))
1843 (setq l
(sort l pred
))
1845 (setq todos-descending-counts
1846 (delete key todos-descending-counts
))
1847 (push key todos-descending-counts
)))
1850 (defun todos-display-sorted (type)
1851 "Keep point on the TYPE count sorting button just clicked."
1852 (let ((opoint (point)))
1853 (todos-update-categories-display type
)
1854 (goto-char opoint
)))
1856 (defun todos-label-to-key (label)
1857 "Return symbol for sort key associated with LABEL."
1859 (cond ((string= label todos-categories-category-label
)
1861 ((string= label todos-categories-todo-label
)
1863 ((string= label todos-categories-diary-label
)
1865 ((string= label todos-categories-done-label
)
1867 ((string= label todos-categories-archived-label
)
1868 (setq key
'archived
)))
1871 (defun todos-insert-sort-button (label)
1872 "Insert button for displaying categories sorted by item counts.
1873 LABEL determines which type of count is sorted."
1874 (setq str
(if (string= label todos-categories-category-label
)
1875 (todos-padded-string label
)
1878 (setq end
(+ beg
(length str
)))
1879 (insert-button str
'face nil
1882 (let ((key (todos-label-to-key ,label
)))
1883 (if (and (member key todos-descending-counts
)
1886 ;; If display is alphabetical, switch back to
1888 (todos-display-sorted nil
)
1889 (setq todos-descending-counts
1890 (delete key todos-descending-counts
)))
1891 (todos-display-sorted key
)))))
1892 (setq ovl
(make-overlay beg end
))
1893 (overlay-put ovl
'face
'todos-button
))
1895 (defun todos-total-item-counts ()
1896 "Return a list of total item counts for the current file."
1897 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i
))
1898 (mapcar 'cdr todos-categories
))))
1901 (defvar todos-categories-category-number
0
1902 "Variable for numbering categories in Todos Categories mode.")
1904 (defun todos-insert-category-line (cat &optional nonum
)
1905 "Insert button with category CAT's name and item counts.
1906 With non-nil argument NONUM show only these; otherwise, insert a
1907 number in front of the button indicating the category's priority.
1908 The number and the category name are separated by the string
1909 which is the value of the user option
1910 `todos-categories-number-separator'."
1911 (let ((archive (member todos-current-todos-file todos-archives
))
1912 (num todos-categories-category-number
)
1913 (str (todos-padded-string cat
))
1915 (setq num
(1+ num
) todos-categories-category-number num
)
1918 (make-string (+ 4 (length todos-categories-number-separator
))
1920 (format " %3d%s" num todos-categories-number-separator
))
1922 (mapconcat (lambda (elt)
1924 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
1925 (format "%3d" (todos-get-count (cdr elt
) cat
)) ; count
1926 ;; Add an extra space if label length is odd
1927 ;; (using def of oddp from cl.el).
1928 (if (eq (logand (length (car elt
)) 1) 1) " ")))
1930 (list (cons todos-categories-done-label
'done
))
1931 (list (cons todos-categories-todo-label
'todo
)
1932 (cons todos-categories-diary-label
'diary
)
1933 (cons todos-categories-done-label
'done
)
1934 (cons todos-categories-archived-label
1937 'face
(if (and todos-ignore-archived-categories
1938 (zerop (todos-get-count 'todo cat
))
1939 (zerop (todos-get-count 'done cat
))
1940 (not (zerop (todos-get-count 'archived cat
))))
1941 'todos-archived-only
1943 'action
`(lambda (button) (let ((buf (current-buffer)))
1944 (todos-jump-to-category ,cat
)
1945 (kill-buffer buf
))))
1946 ;; Highlight the sorted count column.
1947 (let* ((beg (+ opoint
6 (length str
)))
1949 (cond ((eq nonum
'todo
)
1950 (setq beg
(+ beg
1 (/ (length todos-categories-todo-label
) 2))))
1952 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
1953 2 (/ (length todos-categories-diary-label
) 2))))
1955 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
1956 2 (length todos-categories-diary-label
)
1957 2 (/ (length todos-categories-done-label
) 2))))
1958 ((eq nonum
'archived
)
1959 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
1960 2 (length todos-categories-diary-label
)
1961 2 (length todos-categories-done-label
)
1962 2 (/ (length todos-categories-archived-label
) 2)))))
1963 (unless (= beg
(+ opoint
6 (length str
)))
1964 (setq end
(+ beg
4))
1965 (setq ovl
(make-overlay beg end
))
1966 (overlay-put ovl
'face
'todos-sorted-column
)))
1969 (defun todos-display-categories-1 ()
1970 "Prepare buffer for displaying table of categories and item counts."
1971 (unless (eq major-mode
'todos-categories-mode
)
1972 (setq todos-global-current-todos-file
(or todos-current-todos-file
1973 todos-default-todos-file
))
1974 (set-window-buffer (selected-window)
1975 (set-buffer (get-buffer-create todos-categories-buffer
)))
1976 (kill-all-local-variables)
1977 (todos-categories-mode)
1978 (let (buffer-read-only)
1980 ;; FIXME: add usage tips?
1981 (insert (format "Category counts for Todos file \"%s\"."
1982 (todos-short-file-name todos-current-todos-file
)))
1984 ;; Make space for the column of category numbers.
1985 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32))
1986 ;; Add the category and item count buttons (if this is the list of
1987 ;; categories in an archive, show only done item counts).
1988 (todos-insert-sort-button todos-categories-category-label
)
1989 (if (member todos-current-todos-file todos-archives
)
1990 (insert (concat (make-string 6 32)
1991 (format "%s" todos-categories-archived-label
)))
1992 (insert (make-string 3 32))
1993 (todos-insert-sort-button todos-categories-todo-label
)
1994 (insert (make-string 2 32))
1995 (todos-insert-sort-button todos-categories-diary-label
)
1996 (insert (make-string 2 32))
1997 (todos-insert-sort-button todos-categories-done-label
)
1998 (insert (make-string 2 32))
1999 (todos-insert-sort-button todos-categories-archived-label
))
2002 (defun todos-update-categories-display (sortkey)
2004 (let* ((cats0 todos-categories
)
2005 (cats (todos-sort cats0 sortkey
))
2006 (archive (member todos-current-todos-file todos-archives
))
2007 (todos-categories-category-number 0)
2008 ;; Find start of Category button if we just entered Todos Categories
2010 (pt (if (eq (point) (point-max))
2013 (goto-char (next-single-char-property-change
2014 (point) 'face nil
(line-end-position))))))
2017 (delete-region (point) (point-max))
2018 ;; Fill in the table with buttonized lines, each showing a category and
2020 (mapc (lambda (cat) (todos-insert-category-line cat sortkey
))
2023 ;; Add a line showing item count totals.
2024 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32)
2025 (todos-padded-string todos-categories-totals-label
)
2029 (make-string (1+ (/ (length (car elt
)) 2)) 32)
2030 (format "%3d" (nth (cdr elt
) (todos-total-item-counts)))
2031 ;; Add an extra space if label length is odd (using
2032 ;; definition of oddp from cl.el).
2033 (if (eq (logand (length (car elt
)) 1) 1) " ")))
2035 (list (cons todos-categories-done-label
2))
2036 (list (cons todos-categories-todo-label
0)
2037 (cons todos-categories-diary-label
1)
2038 (cons todos-categories-done-label
2)
2039 (cons todos-categories-archived-label
3)))
2041 ;; Put cursor on Category button initially.
2042 (if pt
(goto-char pt
))
2043 (setq buffer-read-only t
)))
2045 ;; ---------------------------------------------------------------------------
2046 ;;; Todos insertion commands, key bindings and keymap
2048 ;; Can either of these be included in Emacs? The originals are GFDL'd.
2050 ;; Slightly reformulated from
2051 ;; http://rosettacode.org/wiki/Power_set#Common_Lisp.
2052 (defun powerset-recursive (l)
2056 (let ((prev (powerset-recursive (cdr l
))))
2057 (append (mapcar (lambda (elt) (cons (car l
) elt
))
2061 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C
2062 (defun powerset-bitwise (l)
2063 (let ((binnum (lsh 1 (length l
)))
2068 (while (not (zerop bits
))
2069 (let ((arg (pop ll
)))
2070 (unless (zerop (logand bits
1))
2071 (setq elt
(append elt
(list arg
))))
2072 (setq bits
(lsh bits -
1))))
2073 (setq pset
(append pset
(list elt
)))
2077 ;; (defalias 'todos-powerset 'powerset-recursive)
2078 (defalias 'todos-powerset
'powerset-bitwise
)
2080 ;; Return list of lists of non-nil atoms produced from ARGLIST. The elements
2081 ;; of ARGLIST may be atoms or lists.
2082 (defun todos-gen-arglists (arglist)
2085 (let ((arg (pop arglist
)))
2086 (cond ((symbolp arg
)
2087 (setq arglists
(if arglists
2088 (mapcar (lambda (l) (push arg l
)) arglists
)
2089 (list (push arg arglists
)))))
2093 (if (= 1 (length arglists
))
2094 (apply (lambda (l) (push a l
)) arglists
)
2095 (mapcar (lambda (l) (push a l
)) arglists
)))
2097 (setq arglists
(mapcar 'reverse
(apply 'append
(mapc 'car arglists
))))))
2099 (defvar todos-insertion-commands-args-genlist
2100 '(diary nonmarking
(calendar date dayname
) time
(here region
))
2101 "Generator list for argument lists of Todos insertion commands.")
2103 (defvar todos-insertion-commands-args
2104 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist
))
2106 (setq res
(remove-duplicates
2107 (apply 'append
(mapcar 'todos-powerset argslist
)) :test
'equal
))
2109 (unless (= 5 (length l
))
2110 (let ((v (make-vector 5 nil
)) elt
)
2113 (cond ((eq elt
'diary
)
2115 ((eq elt
'nonmarking
)
2117 ((or (eq elt
'calendar
)
2126 (setq l
(append v nil
))))
2127 (setq new
(append new
(list l
))))
2129 "List of all argument lists for Todos insertion commands.")
2131 (defun todos-insertion-command-name (arglist)
2132 "Generate Todos insertion command name from ARGLIST."
2133 (replace-regexp-in-string
2135 (replace-regexp-in-string
2137 (concat "todos-item-insert-"
2138 (mapconcat (lambda (e) (if e
(symbol-name e
))) arglist
"-")))))
2140 (defvar todos-insertion-commands-names
2142 (todos-insertion-command-name l
))
2143 todos-insertion-commands-args
)
2144 "List of names of Todos insertion commands.")
2146 (defmacro todos-define-insertion-command
(&rest args
)
2147 (let ((name (intern (todos-insertion-command-name args
)))
2152 (arg4 (nth 4 args
)))
2153 `(defun ,name
(&optional arg
)
2154 "Todos item insertion command generated from ARGS."
2156 (todos-insert-item arg
',arg0
',arg1
',arg2
',arg3
',arg4
))))
2158 (defvar todos-insertion-commands
2160 (eval `(todos-define-insertion-command ,@c
)))
2161 todos-insertion-commands-args
)
2162 "List of Todos insertion commands.")
2164 (defvar todos-insertion-commands-arg-key-list
2165 '(("diary" "y" "yy")
2166 ("nonmarking" "k" "kk")
2167 ("calendar" "c" "cc")
2169 ("dayname" "n" "nn")
2175 (defun todos-insertion-key-bindings (map)
2177 (dolist (c todos-insertion-commands
)
2179 (cname (symbol-name c
)))
2181 (let ((arg (nth 0 l
))
2184 (if (string-match (concat (regexp-quote arg
) "\\_>") cname
)
2185 (setq key
(concat key key2
)))
2186 (if (string-match (concat (regexp-quote arg
) ".+") cname
)
2187 (setq key
(concat key key1
)))))
2188 todos-insertion-commands-arg-key-list
)
2189 (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname
)
2190 (setq key
(concat key
"i")))
2191 (define-key map key c
))))
2193 (defvar todos-insertion-map
2194 (let ((map (make-keymap)))
2195 (todos-insertion-key-bindings map
)
2197 "Keymap for Todos mode insertion commands.")
2199 ;; ??FIXME: use easy-mmode-define-keymap and easy-mmode-defmap
2200 (defvar todos-key-bindings
2203 ("Cd" . todos-display-categories
) ;FIXME: Cs todos-show-categories?
2204 ;("" . todos-display-categories-alphabetically)
2205 ("H" . todos-highlight-item
)
2206 ("N" . todos-hide-show-item-numbering
)
2207 ("D" . todos-hide-show-date-time
)
2208 ("*" . todos-mark-unmark-item
)
2209 ("C*" . todos-mark-category
)
2210 ("Cu" . todos-unmark-category
)
2211 ("PP" . todos-print
)
2212 ("PF" . todos-print-to-file
)
2213 ("v" . todos-hide-show-done-items
)
2214 ("V" . todos-show-done-only
)
2215 ("As" . todos-show-archive
)
2216 ("Ac" . todos-choose-archive
)
2217 ("Y" . todos-diary-items
)
2218 ;;("" . todos-update-filter-files)
2219 ("Fe" . todos-edit-multiline
)
2220 ("Fh" . todos-highlight-item
)
2221 ("Fn" . todos-hide-show-item-numbering
)
2222 ("Fd" . todos-hide-show-date-time
)
2223 ("Ftt" . todos-top-priorities
)
2224 ("Ftm" . todos-top-priorities-multifile
)
2225 ("Fts" . todos-set-top-priorities-in-file
)
2226 ("Cts" . todos-set-top-priorities-in-category
)
2227 ("Fyy" . todos-diary-items
)
2228 ("Fym" . todos-diary-items-multifile
)
2229 ("Fxx" . todos-regexp-items
)
2230 ("Fxm" . todos-regexp-items-multifile
)
2231 ;;("" . todos-save-top-priorities)
2233 ("f" . todos-forward-category
)
2234 ("b" . todos-backward-category
)
2235 ("j" . todos-jump-to-category
)
2236 ("J" . todos-jump-to-category-other-file
)
2237 ("n" . todos-forward-item
)
2238 ("p" . todos-backward-item
)
2239 ("S" . todos-search
)
2240 ("X" . todos-clear-matches
)
2242 ("Fa" . todos-add-file
)
2243 ("Ca" . todos-add-category
)
2244 ("Cr" . todos-rename-category
)
2245 ("Cg" . todos-merge-category
)
2246 ;;("" . todos-merge-categories)
2247 ("Cm" . todos-move-category
)
2248 ("Ck" . todos-delete-category
)
2249 ("d" . todos-item-done
)
2250 ("ee" . todos-edit-item
)
2251 ("em" . todos-edit-multiline-item
)
2252 ("eh" . todos-edit-item-header
)
2253 ("edd" . todos-edit-item-date
)
2254 ("edc" . todos-edit-item-date-from-calendar
)
2255 ("edt" . todos-edit-item-date-is-today
)
2256 ("et" . todos-edit-item-time
)
2257 ("eyy" . todos-edit-item-diary-inclusion
)
2258 ;; ("" . todos-edit-category-diary-inclusion)
2259 ("eyn" . todos-edit-item-diary-nonmarking
)
2260 ;;("" . todos-edit-category-diary-nonmarking)
2261 ("ec" . todos-done-item-add-or-edit-comment
) ;FIXME: or just "c"?
2262 ("i" .
,todos-insertion-map
)
2263 ("k" . todos-delete-item
) ;FIXME: not single letter?
2264 ("m" . todos-move-item
)
2265 ("M" . todos-move-item-to-file
)
2266 ;; FIXME: This binding prevents `-' from being used in a numerical prefix
2267 ;; argument without typing C-u
2268 ;; ("-" . todos-raise-item-priority)
2269 ("r" . todos-raise-item-priority
)
2270 ;; ("+" . todos-lower-item-priority)
2271 ("l" . todos-lower-item-priority
)
2272 ("#" . todos-set-item-priority
)
2273 ("u" . todos-item-undo
)
2274 ("Ad" . todos-archive-done-item
) ;FIXME
2275 ("AD" . todos-archive-category-done-items
) ;FIXME
2276 ("Au" . todos-unarchive-items
)
2277 ("AU" . todos-unarchive-category
)
2280 ([remap newline
] . newline-and-indent
)
2282 "Alist pairing keys defined in Todos modes and their bindings.")
2284 (defvar todos-mode-map
2285 (let ((map (make-keymap)))
2286 ;; Don't suppress digit keys, so they can supply prefix arguments.
2287 (suppress-keymap map
)
2288 (dolist (ck todos-key-bindings
)
2289 (define-key map
(car ck
) (cdr ck
)))
2291 "Todos mode keymap.")
2295 todos-menu todos-mode-map
"Todos Menu"
2298 ["Next Item" todos-forward-item t
]
2299 ["Previous Item" todos-backward-item t
]
2301 ["Next Category" todos-forward-category t
]
2302 ["Previous Category" todos-backward-category t
]
2303 ["Jump to Category" todos-jump-to-category t
]
2304 ["Jump to Category in Other File" todos-jump-to-category-other-file t
]
2306 ["Search Todos File" todos-search t
]
2307 ["Clear Highlighting on Search Matches" todos-category-done t
])
2309 ["List Current Categories" todos-display-categories t
]
2310 ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
2311 ["Turn Item Highlighting on/off" todos-highlight-item t
]
2312 ["Turn Item Numbering on/off" todos-hide-show-item-numbering t
]
2313 ["Turn Item Time Stamp on/off" todos-hide-show-date-time t
]
2314 ["View/Hide Done Items" todos-hide-show-done-items t
]
2316 ["View Diary Items" todos-diary-items t
]
2317 ["View Top Priority Items" todos-top-priorities t
]
2318 ["View Multifile Top Priority Items" todos-top-priorities-multifile t
]
2320 ["Print Category" todos-print t
])
2322 ["Insert New Item" todos-insert-item t
]
2323 ["Insert Item Here" todos-insert-item-here t
]
2324 ("More Insertion Commands")
2325 ["Edit Item" todos-edit-item t
]
2326 ["Edit Multiline Item" todos-edit-multiline t
]
2327 ["Edit Item Header" todos-edit-item-header t
]
2328 ["Edit Item Date" todos-edit-item-date t
]
2329 ["Edit Item Time" todos-edit-item-time t
]
2331 ["Lower Item Priority" todos-lower-item-priority t
]
2332 ["Raise Item Priority" todos-raise-item-priority t
]
2333 ["Set Item Priority" todos-set-item-priority t
]
2334 ["Move (Recategorize) Item" todos-move-item t
]
2335 ["Delete Item" todos-delete-item t
]
2336 ["Undo Done Item" todos-item-undo t
]
2337 ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t
]
2338 ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t
]
2339 ["Mark & Hide Done Item" todos-item-done t
]
2340 ["Archive Done Items" todos-archive-category-done-items t
]
2342 ["Add New Todos File" todos-add-file t
]
2343 ["Add New Category" todos-add-category t
]
2344 ["Delete Current Category" todos-delete-category t
]
2345 ["Rename Current Category" todos-rename-category t
]
2347 ["Save Todos File" todos-save t
]
2348 ["Save Top Priorities" todos-save-top-priorities t
])
2350 ["Quit" todos-quit t
]
2353 (defvar todos-archive-mode-map
2354 (let ((map (make-sparse-keymap)))
2355 (suppress-keymap map t
)
2356 ;; navigation commands
2357 (define-key map
"f" 'todos-forward-category
)
2358 (define-key map
"b" 'todos-backward-category
)
2359 (define-key map
"j" 'todos-jump-to-category
)
2360 (define-key map
"n" 'todos-forward-item
)
2361 (define-key map
"p" 'todos-backward-item
)
2363 (define-key map
"C" 'todos-display-categories
)
2364 (define-key map
"H" 'todos-highlight-item
)
2365 (define-key map
"N" 'todos-hide-show-item-numbering
)
2366 ;; (define-key map "" 'todos-hide-show-date-time)
2367 (define-key map
"P" 'todos-print
)
2368 (define-key map
"q" 'todos-quit
)
2369 (define-key map
"s" 'todos-save
)
2370 (define-key map
"S" 'todos-search
)
2371 (define-key map
"t" 'todos-show
)
2372 (define-key map
"u" 'todos-unarchive-item
)
2373 (define-key map
"U" 'todos-unarchive-category
)
2375 "Todos Archive mode keymap.")
2377 (defvar todos-edit-mode-map
2378 (let ((map (make-sparse-keymap)))
2379 (define-key map
"\C-x\C-q" 'todos-edit-quit
)
2380 (define-key map
[remap newline
] 'newline-and-indent
)
2382 "Todos Edit mode keymap.")
2384 (defvar todos-categories-mode-map
2385 (let ((map (make-sparse-keymap)))
2386 (suppress-keymap map t
)
2387 ;; (define-key map "a" 'todos-display-categories-alphabetically)
2388 (define-key map
"c" 'todos-display-categories
)
2389 (define-key map
"l" 'todos-lower-category-priority
)
2390 (define-key map
"+" 'todos-lower-category-priority
)
2391 (define-key map
"r" 'todos-raise-category-priority
)
2392 (define-key map
"-" 'todos-raise-category-priority
)
2393 (define-key map
"n" 'forward-button
)
2394 (define-key map
"p" 'backward-button
)
2395 (define-key map
[tab] 'forward-button)
2396 (define-key map [backtab] 'backward-button)
2397 (define-key map "q" 'todos-quit)
2398 ;; (define-key map "A" 'todos-add-category)
2399 ;; (define-key map "D" 'todos-delete-category)
2400 ;; (define-key map "R" 'todos-rename-category)
2402 "Todos Categories mode keymap.")
2404 (defvar todos-filter-items-mode-map
2405 (let ((map (make-keymap)))
2406 (suppress-keymap map t)
2407 ;; navigation commands
2408 (define-key map "j" 'todos-jump-to-item)
2409 (define-key map [remap newline] 'todos-jump-to-item)
2410 (define-key map "n" 'todos-forward-item)
2411 (define-key map "p" 'todos-backward-item)
2412 (define-key map "H" 'todos-highlight-item)
2413 (define-key map "N" 'todos-hide-show-item-numbering)
2414 (define-key map "D" 'todos-hide-show-date-time)
2415 (define-key map "P" 'todos-print)
2416 (define-key map "q" 'todos-quit)
2417 (define-key map "s" 'todos-save)
2418 ;; (define-key map "S" 'todos-save-top-priorities)
2420 (define-key map "l" 'todos-lower-item-priority)
2421 (define-key map "r" 'todos-raise-item-priority)
2422 (define-key map "#" 'todos-set-item-top-priority)
2424 "Todos Top Priorities mode keymap.")
2426 (defun todos-modes-set-1 ()
2428 (set (make-local-variable 'font-lock-defaults) '(todos-font-lock-keywords t))
2429 (set (make-local-variable 'indent-line-function) 'todos-indent)
2430 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2432 (defun todos-modes-set-2 ()
2434 (add-to-invisibility-spec 'todos)
2435 (setq buffer-read-only t)
2436 (set (make-local-variable 'hl-line-range-function)
2437 (lambda() (when (todos-item-end)
2438 (cons (todos-item-start) (todos-item-end))))))
2440 (defun todos-modes-set-3 ()
2441 ;; FIXME: is this right?
2442 (set (make-local-variable 'todos-categories) (todos-set-categories))
2443 (set (make-local-variable 'todos-category-number) 1)
2444 (set (make-local-variable 'todos-first-visit) t)
2445 (add-hook 'post-command-hook 'todos-after-find-file nil t))
2447 (put 'todos-mode 'mode-class 'special)
2449 ;; Autoloading isn't needed if files are identified by auto-mode-alist
2450 ;; ;; As calendar reads included Todos file before todos-mode is loaded.
2452 (define-derived-mode todos-mode special-mode "Todos" ()
2453 "Major mode for displaying, navigating and editing Todo lists.
2456 (easy-menu-add todos-menu)
2460 ;; Initialize todos-current-todos-file.
2461 (when (member (file-truename (buffer-file-name))
2462 (funcall todos-files-function))
2463 (set (make-local-variable 'todos-current-todos-file)
2464 (file-truename (buffer-file-name))))
2465 (set (make-local-variable 'todos-first-visit) t)
2466 (set (make-local-variable 'todos-show-done-only) nil)
2467 (set (make-local-variable 'todos-categories-with-marks) nil)
2468 (when todos-show-current-file
2469 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
2470 ;; FIXME: works more or less, but should be tied to the defcustom
2471 (add-hook 'window-configuration-change-hook
2473 (setq todos-done-separator (make-string (window-width) ?_)))
2475 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
2477 ;; FIXME: need this?
2478 (defun todos-unload-hook ()
2480 (remove-hook 'pre-command-hook 'todos-show-current-file t)
2481 (remove-hook 'post-command-hook 'todos-after-find-file t)
2482 (remove-hook 'window-configuration-change-hook
2484 (setq todos-done-separator
2485 (make-string (window-width) ?_))) t)
2486 (remove-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file t))
2488 (put 'todos-archive-mode 'mode-class 'special)
2490 (define-derived-mode todos-archive-mode todos-mode "Todos-Arch" ()
2491 "Major mode for archived Todos categories.
2493 \\{todos-archive-mode-map}"
2497 (set (make-local-variable 'todos-current-todos-file)
2498 (file-truename (buffer-file-name)))
2499 (set (make-local-variable 'todos-show-done-only) t))
2501 (defun todos-mode-external-set ()
2503 (set (make-local-variable 'todos-current-todos-file)
2504 todos-global-current-todos-file)
2505 (let ((cats (with-current-buffer (get-file-buffer todos-current-todos-file)
2506 ;; FIXME: or just todos-categories?
2507 (todos-set-categories))))
2508 (set (make-local-variable 'todos-categories) cats)))
2510 (define-derived-mode todos-edit-mode text-mode "Todos-Ed" ()
2511 "Major mode for editing multiline Todo items.
2513 \\{todos-edit-mode-map}"
2515 (todos-mode-external-set))
2517 (put 'todos-categories-mode 'mode-class 'special)
2519 (define-derived-mode todos-categories-mode special-mode "Todos-Cats" ()
2520 "Major mode for displaying and editing Todos categories.
2522 \\{todos-categories-mode-map}"
2523 (todos-mode-external-set))
2525 (put 'todos-filter-mode 'mode-class 'special)
2527 (define-derived-mode todos-filter-items-mode special-mode "Todos-Fltr" ()
2528 "Mode for displaying and reprioritizing top priority Todos.
2530 \\{todos-filter-items-mode-map}"
2532 (todos-modes-set-2))
2534 ;; FIXME: need this?
2535 (defun todos-save ()
2536 "Save the current Todos file."
2539 ;; (if todos-save-top-priorities-too (todos-save-top-priorities))
2542 (defun todos-quit ()
2543 "Exit the current Todos-related buffer.
2544 Depending on the specific mode, this either kills the buffer or
2545 buries it and restores state as needed."
2547 (cond ((eq major-mode 'todos-categories-mode)
2549 (setq todos-descending-counts nil)
2551 ((eq major-mode 'todos-filter-items-mode)
2554 ((member major-mode (list 'todos-mode 'todos-archive-mode))
2555 ;; Have to write previously nonexistant archives to file.
2556 (unless (file-exists-p (buffer-file-name)) (todos-save))
2557 ;; FIXME: make this customizable?
2561 ;; ---------------------------------------------------------------------------
2562 ;;; Display Commands
2565 (defun todos-show (&optional solicit-file)
2566 "Visit the current Todos file and display one of its categories.
2568 With non-nil prefix argument SOLICIT-FILE ask for file to visit.
2569 Otherwise, the first invocation of this command in a session
2570 visits `todos-default-todos-file' (creating it if it does not yet
2571 exist); subsequent invocations from outside of Todos mode revisit
2572 this file or, if user option `todos-show-current-file' is
2573 non-nil, whichever Todos file was visited last.
2575 The category displayed on initial invocation is the first member
2576 of `todos-categories' for the current Todos file, on subsequent
2577 invocations whichever category was displayed last. If
2578 `todos-display-categories-first' is non-nil, then the first
2579 invocation of `todos-show' displays a clickable listing of the
2580 categories in the current Todos file.
2582 In Todos mode just the category's unfinished todo items are shown
2583 by default. The done items are hidden, but typing
2584 `\\[todos-hide-show-done-items]' displays them below the todo
2585 items. With non-nil user option `todos-show-with-done' both todo
2586 and done items are always shown on visiting a category.
2588 If this command is invoked in Todos Archive mode, it visits the
2589 corresponding Todos file, displaying the corresponding category."
2592 (file (cond (solicit-file
2593 (if (funcall todos-files-function)
2594 (todos-read-file-name "Choose a Todos file to visit: "
2596 (error "There are no Todos files")))
2597 ((eq major-mode 'todos-archive-mode)
2598 (setq cat (todos-current-category))
2599 (concat (file-name-sans-extension todos-current-todos-file)
2602 ;; FIXME: If an archive is value of
2603 ;; todos-current-todos-file, todos-show will revisit it
2604 ;; rather than the corresponding todo file -- ok or make
2606 (or todos-current-todos-file
2607 (and todos-show-current-file
2608 todos-global-current-todos-file)
2609 todos-default-todos-file
2610 (todos-add-file))))))
2611 (if (and todos-first-visit todos-display-categories-first)
2612 (todos-display-categories)
2613 (set-window-buffer (selected-window)
2614 (set-buffer (find-file-noselect file)))
2615 ;; If called from archive file, show corresponding category in Todos
2616 ;; file, if it exists.
2617 (when (assoc cat todos-categories)
2618 (setq todos-category-number (todos-category-number cat)))
2619 ;; If no Todos file exists, initialize one.
2620 (when (zerop (buffer-size))
2621 ;; Call with empty category name to get initial prompt.
2622 (setq todos-category-number (todos-add-category "")))
2623 (save-excursion (todos-category-select)))
2624 (setq todos-first-visit nil)))
2626 (defun todos-display-categories ()
2627 "Display a table of the current file's categories and item counts.
2629 In the initial display the categories are numbered, indicating
2630 their current order for navigating by \\[todos-forward-category]
2631 and \\[todos-backward-category]. You can persistantly change the
2632 order of the category at point by typing
2633 \\[todos-raise-category-priority] or
2634 \\[todos-lower-category-priority].
2636 The labels above the category names and item counts are buttons,
2637 and clicking these changes the display: sorted by category name
2638 or by the respective item counts (alternately descending or
2639 ascending). In these displays the categories are not numbered
2640 and \\[todos-raise-category-priority] and
2641 \\[todos-lower-category-priority] are
2642 disabled. (Programmatically, the sorting is triggered by passing
2643 a non-nil SORTKEY argument.)
2645 In addition, the lines with the category names and item counts
2646 are buttonized, and pressing one of these button jumps to the
2647 category in Todos mode (or Todos Archive mode, for categories
2648 containing only archived items, provided user option
2649 `todos-ignore-archived-categories' is non-nil. These categories
2650 are shown in `todos-archived-only' face."
2652 (todos-display-categories-1)
2654 (todos-update-categories-display sortkey)))
2656 ;; ;; FIXME: make this toggle with todos-display-categories
2657 ;; (defun todos-display-categories-alphabetically ()
2660 ;; (todos-display-sorted 'alpha))
2662 ;; ;; FIXME: provide key bindings for these or delete them
2663 ;; (defun todos-display-categories-sorted-by-todo ()
2666 ;; (todos-display-sorted 'todo))
2668 ;; (defun todos-display-categories-sorted-by-diary ()
2671 ;; (todos-display-sorted 'diary))
2673 ;; (defun todos-display-categories-sorted-by-done ()
2676 ;; (todos-display-sorted 'done))
2678 ;; (defun todos-display-categories-sorted-by-archived ()
2681 ;; (todos-display-sorted 'archived))
2683 (defun todos-hide-show-item-numbering ()
2686 (todos-reset-prefix 'todos-number-priorities (not todos-number-priorities)))
2688 (defun todos-hide-show-done-items ()
2689 "Show hidden or hide visible done items in current category."
2691 (if (zerop (todos-get-count 'done (todos-current-category)))
2692 (message "There are no done items in this category.")
2694 (goto-char (point-min))
2695 (let ((todos-show-with-done (not (re-search-forward
2696 todos-done-string-start nil t))))
2697 (todos-category-select)))))
2699 (defun todos-show-done-only ()
2700 "Switch between displaying only done or only todo items."
2702 (setq todos-show-done-only (not todos-show-done-only))
2703 (todos-category-select))
2705 (defun todos-show-archive (&optional ask)
2706 "Visit the archive of the current Todos category, if it exists.
2707 If the category has no archived items, prompt to visit the
2708 archive anyway. If there is no archive for this file or with
2709 non-nil argument ASK, prompt to visit another archive.
2711 With non-nil argument ASK prompt to choose an archive to visit;
2712 see `todos-choose-archive'. The buffer showing the archive is in
2713 Todos Archive mode. The first visit in a session displays the
2714 first category in the archive, subsequent visits return to the
2715 last category displayed." ;FIXME
2717 (let* ((cat (todos-current-category))
2718 (count (todos-get-count 'archived cat))
2719 (archive (concat (file-name-sans-extension todos-current-todos-file)
2722 (setq place (cond (ask 'other-archive)
2723 ((file-exists-p archive) 'this-archive)
2724 (t (when (y-or-n-p (concat "This file has no archive; "
2725 "visit another archive? "))
2727 (when (eq place 'other-archive)
2728 (setq archive (todos-read-file-name "Choose a Todos archive: " t t)))
2729 (when (and (eq place 'this-archive) (zerop count))
2730 (setq place (when (y-or-n-p
2731 (concat "This category has no archived items;"
2732 " visit archive anyway? "))
2735 (set-window-buffer (selected-window)
2736 (set-buffer (find-file-noselect archive)))
2737 (if (member place '(other-archive other-cat))
2738 (setq todos-category-number 1)
2739 (todos-category-number cat))
2740 (todos-category-select))))
2742 (defun todos-choose-archive ()
2743 "Choose an archive and visit it."
2745 (todos-show-archive t))
2747 (defun todos-highlight-item ()
2748 "Toggle highlighting the todo item the cursor is on."
2755 (defun todos-hide-show-date-time () ;(&optional all)
2756 "Hide or show date-time header of todo items.";; in current category.
2757 ;; With non-nil prefix argument ALL do this in the whole file."
2761 (goto-char (point-min))
2762 (let ((ovs (overlays-in (point) (1+ (point))))
2766 (if (equal (overlay-get ov 'display) "")
2767 (setq ovs nil hidden t)))
2770 (goto-char (point-min));)
2772 (remove-overlays (point-min) (point-max) 'display "")
2774 (when (re-search-forward
2775 (concat todos-date-string-start todos-date-pattern
2776 "\\( " diary-time-regexp "\\)?"
2777 (regexp-quote todos-nondiary-end) "? ")
2779 (unless (save-match-data (todos-done-item-p))
2780 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
2781 (overlay-put ov 'display "")))
2782 (todos-forward-item)))))))
2784 (defun todos-mark-unmark-item (&optional n all)
2785 "Mark item at point if unmarked, or unmark it if marked.
2787 With a positive numerical prefix argument N, change the
2788 markedness of the next N items. With non-nil argument ALL, mark
2789 all visible items in the category (depending on visibility, all
2790 todo and done items, or just todo or just done items).
2792 The mark is the character \"*\" inserted in front of the item's
2793 priority number or the `todos-prefix' string; if `todos-prefix'
2794 is \"*\", then the mark is \"@\"."
2796 (if all (goto-char (point-min)))
2797 (unless (> n 0) (setq n 1))
2799 (while (or (and all (not (eobp)))
2801 (let* ((cat (todos-current-category))
2802 (ov (todos-marked-item-p))
2803 (marked (assoc cat todos-categories-with-marks)))
2804 (if (and ov (not all))
2807 (if (= (cdr marked) 1) ; Deleted last mark in this category.
2808 (setq todos-categories-with-marks
2809 (assq-delete-all cat todos-categories-with-marks))
2810 (setcdr marked (1- (cdr marked)))))
2811 (when (todos-item-start)
2812 (unless (and all (todos-marked-item-p))
2813 (setq ov (make-overlay (point) (point)))
2814 (overlay-put ov 'before-string todos-item-mark)
2816 (setcdr marked (1+ (cdr marked)))
2817 (push (cons cat 1) todos-categories-with-marks))))))
2818 (todos-forward-item)
2821 (defun todos-mark-category ()
2822 "Put the \"*\" mark on all items in this category.
2823 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
2825 (todos-mark-unmark-item 0 t))
2827 (defun todos-unmark-category ()
2828 "Remove the \"*\" mark from all items in this category.
2829 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
2831 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
2832 (setq todos-categories-with-marks
2833 (delq (assoc (todos-current-category) todos-categories-with-marks)
2834 todos-categories-with-marks)))
2836 (defun todos-set-top-priorities-in-file ()
2837 "Set number of top priorities for this file.
2838 See `todos-set-top-priorities' for more details."
2840 (todos-set-top-priorities))
2842 (defun todos-set-top-priorities-in-category ()
2843 "Set number of top priorities for this category.
2844 See `todos-set-top-priorities' for more details."
2846 (todos-set-top-priorities t))
2848 (defun todos-top-priorities (&optional num)
2849 "List top priorities of each category in `todos-filter-files'.
2850 Number of entries for each category is given by NUM, which
2851 defaults to `todos-show-priorities'."
2853 (let ((arg (if num (cons 'top num) 'top))
2854 (buf todos-top-priorities-buffer)
2855 (file todos-current-todos-file))
2856 (todos-filter-items arg)
2857 (todos-special-buffer-name buf file)))
2859 (defun todos-top-priorities-multifile (&optional arg)
2860 "List top priorities of each category in `todos-filter-files'.
2862 If the prefix argument ARG is a number, this is the maximum
2863 number of top priorities to list in each category. If the prefix
2864 argument is `C-u', prompt for which files to filter and use
2865 `todos-show-priorities' as the number of top priorities to list
2866 in each category. If the prefix argument is `C-uC-u', prompt
2867 both for which files to filter and for how many top priorities to
2868 list in each category."
2870 (let* ((buf todos-top-priorities-buffer)
2872 (pref (if (numberp arg)
2874 (setq files (if (or (consp arg)
2875 (null todos-filter-files))
2876 (progn (todos-multiple-files)
2877 todos-multiple-files)
2878 todos-filter-files))
2879 (if (equal arg '(16))
2880 (cons 'top (read-number
2881 "Enter number of top priorities to show: "
2882 todos-show-priorities))
2884 (todos-filter-items pref t)
2885 (todos-special-buffer-name buf files)))
2887 (defun todos-diary-items ()
2888 "Display todo items for diary inclusion in this Todos file."
2890 (let ((buf todos-diary-items-buffer)
2891 (file todos-current-todos-file))
2892 (todos-filter-items 'diary)
2893 (todos-special-buffer-name buf file)))
2895 (defun todos-diary-items-multifile (&optional arg)
2896 "Display todo items for diary inclusion in one or more Todos file.
2897 The files are those listed in `todos-filter-files'."
2899 (let ((buf todos-diary-items-buffer)
2900 (files (if (or arg (null todos-filter-files))
2901 (progn (todos-multiple-files)
2902 todos-multiple-files)
2903 todos-filter-files)))
2904 (todos-filter-items 'diary t)
2905 (todos-special-buffer-name buf files)))
2907 (defun todos-regexp-items ()
2908 "Display todo items matching a user-entered regular expression.
2909 The items are those in the current Todos file."
2911 (let ((buf todos-regexp-items-buffer)
2912 (file todos-current-todos-file))
2913 (todos-filter-items 'regexp)
2914 (todos-special-buffer-name buf file)))
2916 (defun todos-regexp-items-multifile (&optional arg)
2917 "Display todo items matching a user-entered regular expression.
2918 The items are those in the files listed in `todos-filter-files'."
2920 (let ((buf todos-regexp-items-buffer)
2921 (files (if (or arg (null todos-filter-files))
2922 (progn (todos-multiple-files)
2923 todos-multiple-files)
2924 todos-filter-files)))
2925 (todos-filter-items 'regexp t)
2926 (todos-special-buffer-name buf files)))
2928 (defun todos-print (&optional to-file)
2929 "Produce a printable version of the current Todos buffer.
2930 This converts overlays and soft line wrapping and, depending on
2931 the value of `todos-print-function', includes faces. With
2932 non-nil argument TO-FILE write the printable version to a file;
2933 otherwise, send it to the default printer."
2935 (let ((buf todos-print-buffer)
2937 ((eq major-mode 'todos-mode)
2938 (concat "Todos File: "
2939 (todos-short-file-name todos-current-todos-file)
2940 "\nCategory: " (todos-current-category)))
2941 ((eq major-mode 'todos-filter-items-mode)
2942 "Todos Top Priorities")))
2943 (prefix (propertize (concat todos-prefix " ")
2944 'face 'todos-prefix-string))
2946 (fill-prefix (make-string todos-indent-to-here 32))
2947 (content (buffer-string))
2949 (with-current-buffer (get-buffer-create buf)
2951 (goto-char (point-min))
2954 (end (save-excursion (todos-item-end))))
2955 (when todos-number-priorities
2957 (setq prefix (propertize (concat (number-to-string num) " ")
2958 'face 'todos-prefix-string)))
2960 (fill-region beg end))
2961 ;; Calling todos-forward-item infloops at todos-item-start due to
2962 ;; non-overlay prefix, so search for item start instead.
2963 (if (re-search-forward todos-item-start nil t)
2965 (goto-char (point-max))))
2966 (if (re-search-backward (concat "^" (regexp-quote todos-category-done))
2968 (replace-match todos-done-separator))
2969 (goto-char (point-min))
2973 (let ((file (read-file-name "Print to file: ")))
2974 (funcall todos-print-function file))
2975 (funcall todos-print-function)))
2978 (defun todos-print-to-file ()
2979 "Save printable version of this Todos buffer to a file."
2983 (defun todos-convert-legacy-files ()
2984 "Convert legacy Todo files to the current Todos format.
2985 The files `todo-file-do' and `todo-file-done' are converted and
2986 saved (the latter as a Todos Archive file) with a new name in
2987 `todos-files-directory'. See also the documentation string of
2988 `todos-todo-mode-date-time-regexp' for further details."
2990 (if (fboundp 'todo-mode)
2991 (require 'todo-mode)
2992 (error "Void function `todo-mode'"))
2993 ;; Convert `todo-file-do'.
2994 (if (file-exists-p todo-file-do)
2995 (let ((default "todo-do-conv")
2998 (insert-file-contents todo-file-do)
2999 (let ((end (search-forward ")" (line-end-position) t))
3000 (beg (search-backward "(" (line-beginning-position) t)))
3001 (setq todo-categories
3002 (read (buffer-substring-no-properties beg end))))
3004 (delete-region (line-beginning-position) (1+ (line-end-position)))
3007 ((looking-at (regexp-quote (concat todo-prefix todo-category-beg)))
3008 (replace-match todos-category-beg))
3009 ((looking-at (regexp-quote todo-category-end))
3011 ((looking-at (regexp-quote (concat todo-prefix " "
3012 todo-category-sep)))
3013 (replace-match todos-category-done))
3014 ((looking-at (concat (regexp-quote todo-prefix) " "
3015 todos-todo-mode-date-time-regexp " "
3016 (regexp-quote todo-initials) ":"))
3017 (todos-convert-legacy-date-time)))
3019 (setq file (concat todos-files-directory
3021 (format "Save file as (default \"%s\"): " default)
3024 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3026 (insert-file-contents file)
3027 (let ((todos-categories (todos-make-categories-list t)))
3028 (todos-update-categories-sexp))
3029 (write-region (point-min) (point-max) file nil 'nomessage))
3030 ;; Convert `todo-file-done'.
3031 (when (file-exists-p todo-file-done)
3033 (insert-file-contents todo-file-done)
3034 (let ((beg (make-marker))
3036 cat cats comment item)
3038 (when (looking-at todos-todo-mode-date-time-regexp)
3039 (set-marker beg (point))
3040 (todos-convert-legacy-date-time)
3041 (set-marker end (point))
3043 (insert "[" todos-done-string)
3047 (when (looking-at todos-todo-mode-date-time-regexp)
3048 (todos-convert-legacy-date-time))
3049 (when (looking-at (concat " " (regexp-quote todo-initials) ":"))
3050 (replace-match "")))
3051 (if (re-search-forward
3052 (concat "^" todos-todo-mode-date-time-regexp) nil t)
3053 (goto-char (match-beginning 0))
3054 (goto-char (point-max)))
3056 (when (looking-back "\\[\\([^][]+\\)\\]")
3057 (setq cat (match-string 1))
3058 (goto-char (match-beginning 0))
3060 ;; If the item ends with a non-comment parenthesis not
3061 ;; followed by a period, we lose (but we inherit that problem
3062 ;; from todo-mode.el).
3063 (when (looking-back "(\\(.*\\)) ")
3064 (setq comment (match-string 1))
3066 (insert "[" todos-comment-string ": " comment "]"))
3067 (set-marker end (point))
3068 (if (member cat cats)
3069 ;; If item is already in its category, leave it there.
3070 (unless (save-excursion
3072 (concat "^" (regexp-quote todos-category-beg)
3074 (string= (match-string 1) cat))
3075 ;; Else move it to its category.
3076 (setq item (buffer-substring-no-properties beg end))
3077 (delete-region beg (1+ end))
3078 (set-marker beg (point))
3080 (concat "^" (regexp-quote (concat todos-category-beg cat)))
3083 (if (re-search-forward
3084 (concat "^" (regexp-quote todos-category-beg)
3086 (progn (goto-char (match-beginning 0))
3089 (goto-char (point-max)))
3094 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3096 (set-marker beg nil)
3097 (set-marker end nil))
3098 (setq file (concat (file-name-sans-extension file) ".toda"))
3099 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3101 (insert-file-contents file)
3102 (let ((todos-categories (todos-make-categories-list t)))
3103 (todos-update-categories-sexp))
3104 (write-region (point-min) (point-max) file nil 'nomessage)
3105 (setq archive-sexp (read (buffer-substring-no-properties
3106 (line-beginning-position)
3107 (line-end-position)))))
3108 (setq file (concat (file-name-sans-extension file) ".todo"))
3109 ;; Update categories sexp of converted Todos file again, adding
3110 ;; counts of archived items.
3112 (insert-file-contents file)
3113 (let ((sexp (read (buffer-substring-no-properties
3114 (line-beginning-position)
3115 (line-end-position)))))
3117 (let ((archive-cat (assoc (car cat) archive-sexp)))
3119 (aset (cdr cat) 3 (aref (cdr archive-cat) 2)))))
3120 (delete-region (line-beginning-position) (line-end-position))
3121 (prin1 sexp (current-buffer)))
3122 (write-region (point-min) (point-max) file nil 'nomessage)))
3123 (todos-reevaluate-defcustoms)
3124 (message "Format conversion done."))
3125 (error "No legacy Todo file exists")))
3127 ;; ---------------------------------------------------------------------------
3128 ;;; Navigation Commands
3130 (defun todos-forward-category (&optional back)
3131 "Visit the numerically next category in this Todos file.
3132 If the current category is the highest numbered, visit the first
3133 category. With non-nil argument BACK, visit the numerically
3134 previous category (the highest numbered one, if the current
3135 category is the first)."
3137 (setq todos-category-number
3138 (1+ (mod (- todos-category-number (if back 2 0))
3139 (length todos-categories))))
3140 (when todos-ignore-archived-categories
3141 (while (and (zerop (todos-get-count 'todo))
3142 (zerop (todos-get-count 'done))
3143 (not (zerop (todos-get-count 'archive))))
3144 (setq todos-category-number
3145 (apply (if back '1- '1+) (list todos-category-number)))))
3146 (todos-category-select)
3147 (goto-char (point-min)))
3149 (defun todos-backward-category ()
3150 "Visit the numerically previous category in this Todos file.
3151 If the current category is the highest numbered, visit the first
3154 (todos-forward-category t))
3156 (defun todos-jump-to-category (&optional cat other-file)
3157 "Jump to a category in this or another Todos file.
3159 Programmatically, optional argument CAT provides the category
3160 name. When nil (as in interactive calls), prompt for the
3161 category, with TAB completion on existing categories. If a
3162 non-existing category name is entered, ask whether to add a new
3163 category with this name; if affirmed, add it, then jump to that
3164 category. With non-nil argument OTHER-FILE, prompt for a Todos
3165 file, otherwise jump within the current Todos file."
3167 (let ((file (or (and other-file
3168 (todos-read-file-name "Choose a Todos file: " nil t))
3169 ;; Jump to archived-only Categories from Todos Categories
3172 todos-ignore-archived-categories
3173 (zerop (todos-get-count 'todo cat))
3174 (zerop (todos-get-count 'done cat))
3175 (not (zerop (todos-get-count 'archived cat)))
3176 (concat (file-name-sans-extension
3177 todos-current-todos-file) ".toda"))
3178 todos-current-todos-file
3179 ;; If invoked from outside of Todos mode before
3181 todos-default-todos-file)))
3182 (with-current-buffer (find-file-noselect file)
3183 (and other-file (setq todos-current-todos-file file))
3184 (let ((category (or (and (assoc cat todos-categories) cat)
3185 (todos-read-category "Jump to category: "))))
3186 ;; Clean up after selecting category in Todos Categories mode.
3187 (if (string= (buffer-name) todos-categories-buffer)
3189 (if (or cat other-file)
3190 (set-window-buffer (selected-window)
3191 (set-buffer (get-file-buffer file))))
3192 (unless todos-global-current-todos-file
3193 (setq todos-global-current-todos-file todos-current-todos-file))
3194 (todos-category-number category) ; (1+ (length t-c)) if new category.
3195 ;; (if (> todos-category-number (length todos-categories))
3196 ;; (setq todos-category-number (todos-add-category category)))
3197 (todos-category-select)
3198 (goto-char (point-min))))))
3200 (defun todos-jump-to-category-other-file ()
3201 "Jump to a category in another Todos file.
3202 The category is chosen by prompt, with TAB completion."
3204 (todos-jump-to-category nil t))
3206 (defun todos-jump-to-item ()
3207 "Jump to the file and category of the filtered item at point."
3209 (let ((str (todos-item-string))
3210 (buf (current-buffer))
3211 cat file archive beg)
3212 (string-match (concat (if todos-filter-done-items
3213 (concat "\\(?:" todos-done-string-start "\\|"
3214 todos-date-string-start "\\)")
3215 todos-date-string-start)
3216 todos-date-pattern "\\(?: " diary-time-regexp "\\)?"
3217 (if todos-filter-done-items
3219 (regexp-quote todos-nondiary-end)) "?"
3220 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
3221 "\\(?1:.*\\)\\]\\).*$") str)
3222 (setq cat (match-string 1 str))
3223 (setq file (match-string 2 str))
3224 (setq archive (string= (match-string 3 str) "(archive) "))
3225 (setq str (replace-match "" nil nil str 4))
3227 (concat todos-files-directory (substring file 0 -1)
3228 (if archive ".toda" ".todo"))
3230 (concat (file-name-sans-extension
3231 todos-global-current-todos-file) ".toda")
3232 todos-global-current-todos-file)))
3233 (find-file-noselect file)
3234 (with-current-buffer (get-file-buffer file)
3236 (goto-char (point-min))
3238 (concat "^" (regexp-quote (concat todos-category-beg cat))) nil t)
3239 (search-forward str)
3240 (setq beg (match-beginning 0)))
3242 (set-window-buffer (selected-window) (set-buffer (get-file-buffer file)))
3243 (setq todos-current-todos-file file)
3244 (setq todos-category-number (todos-category-number cat))
3245 (let ((todos-show-with-done (if todos-filter-done-items t
3246 todos-show-with-done)))
3247 (todos-category-select))
3250 ;; FIXME ? disallow prefix arg value < 1 (re-search-* allows these)
3251 (defun todos-forward-item (&optional count)
3252 "Move point down to start of item with next lower priority.
3253 With numerical prefix COUNT, move point COUNT items downward,"
3255 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
3256 (start (line-end-position)))
3258 (if (re-search-forward todos-item-start nil t (or count 1))
3259 (goto-char (match-beginning 0))
3260 (goto-char (point-max)))
3261 ;; FIXME: this is insufficient, since there could be no done items in this
3262 ;; category, so the search puts us on a todo item. Have to stop before
3263 ;; todos-done-separator when buffer is not narrowed.
3264 ;; If points advances by one from a todo to a done item, go back to the
3265 ;; space above todos-done-separator, since that is a legitimate place to
3266 ;; insert an item. But skip this space if count > 1, since that should
3267 ;; only stop on an item (FIXME: or not?)
3268 (when (and not-done (todos-done-item-p))
3269 (if (or (not count) (= count 1))
3270 (re-search-backward "^$" start t)))))
3272 (defun todos-backward-item (&optional count)
3273 "Move point up to start of item with next higher priority.
3274 With numerical prefix COUNT, move point COUNT items upward,"
3276 (let* ((done (todos-done-item-p)))
3277 ;; FIXME ? this moves to bob if on the first item (but so does previous-line)
3280 (re-search-backward todos-item-start nil t (or count 1)))
3281 ;; Unless this is a regexp filtered items buffer (which can contain
3282 ;; intermixed todo and done items), if points advances by one from a done
3283 ;; to a todo item, go back to the space above todos-done-separator, since
3284 ;; that is a legitimate place to insert an item. But skip this space if
3285 ;; count > 1, since that should only stop on an item (FIXME: or not?)
3286 (when (and done (not (todos-done-item-p)) (or (not count) (= count 1))
3287 (not (equal (buffer-name) todos-regexp-items-buffer)))
3288 (re-search-forward (concat "^" (regexp-quote todos-category-done)) nil t)
3289 (forward-line -1))))
3291 ;; FIXME: (i) Extend search to other Todos files. (ii) Allow navigating among
3292 ;; hits. (But these are available in another form with
3293 ;; todos-regexp-items-multifile.)
3294 (defun todos-search ()
3295 "Search for a regular expression in this Todos file.
3296 The search runs through the whole file and encompasses all and
3297 only todo and done items; it excludes category names. Multiple
3298 matches are shown sequentially, highlighted in `todos-search'
3301 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3303 matches match cat in-done ov mlen msg)
3305 (goto-char (point-min))
3307 (setq match (re-search-forward regex nil t))
3308 (goto-char (line-beginning-position))
3309 (unless (or (equal (point) 1)
3310 (looking-at (concat "^" (regexp-quote todos-category-beg))))
3311 (if match (push match matches)))
3313 (setq matches (reverse matches))
3317 (setq match (pop matches))
3320 (when (looking-at todos-done-string-start)
3322 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
3323 "\\(.*\\)\n") nil t)
3324 (setq cat (match-string-no-properties 1))
3325 (todos-category-number cat)
3326 (todos-category-select)
3328 (unless todos-show-with-done (todos-hide-show-done-items)))
3330 (setq ov (make-overlay (- (point) (length regex)) (point)))
3331 (overlay-put ov 'face 'todos-search)
3333 (setq mlen (length matches))
3336 (format "There are %d more matches; go to next match? "
3338 "There is one more match; go to it? "))
3340 (throw 'stop (setq msg (if (> mlen 1)
3341 (format "There are %d more matches."
3343 "There is one more match."))))))
3344 (setq msg "There are no more matches."))
3345 (todos-category-select)
3347 (message "No match for \"%s\"" regex))
3349 (if (y-or-n-p (concat msg "\nUnhighlight matches? "))
3350 (todos-clear-matches)
3351 (message "You can unhighlight the matches later by typing %s"
3352 (key-description (car (where-is-internal
3353 'todos-clear-matches))))))))
3355 (defun todos-clear-matches ()
3356 "Remove highlighting on matches found by todos-search."
3358 (remove-overlays 1 (1+ (buffer-size)) 'face 'todos-search))
3360 ;; ---------------------------------------------------------------------------
3361 ;;; Editing Commands
3363 (defun todos-add-file ()
3364 "Name and add a new Todos file.
3365 Interactively, prompt for a category and display it.
3366 Noninteractively, return the name of the new file."
3368 (let ((prompt (concat "Enter name of new Todos file "
3369 "(TAB or SPC to see current names): "))
3371 (setq file (todos-read-file-name prompt))
3372 (with-current-buffer (get-buffer-create file)
3374 (write-region (point-min) (point-max) file nil 'nomessage nil t)
3376 (todos-reevaluate-defcustoms)
3377 (if (called-interactively-p)
3379 (set-window-buffer (selected-window)
3380 (set-buffer (find-file-noselect file)))
3381 (setq todos-current-todos-file file)
3385 (defun todos-add-category (&optional cat)
3386 "Add a new category to the current Todos file.
3387 Called interactively, prompts for category name, then visits the
3388 category in Todos mode. Non-interactively, argument CAT provides
3389 the category name and the return value is the category number."
3391 (let* ((buffer-read-only)
3392 (num (1+ (length todos-categories)))
3393 (counts (make-vector 4 0))) ; [todo diary done archived]
3395 (setq cat (todos-validate-name cat 'category)) ;FIXME: need this?
3396 (setq cat (todos-read-category "Enter new category name: " nil t)))
3397 (setq todos-categories (append todos-categories (list (cons cat counts))))
3399 (goto-char (point-max))
3400 (save-excursion ; Save point for todos-category-select.
3401 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3402 (todos-update-categories-sexp)
3403 ;; If called by command, display the newly added category, else return
3404 ;; the category number to the caller.
3405 (if (called-interactively-p 'any) ; FIXME?
3407 (setq todos-category-number num)
3408 (todos-category-select))
3411 (defun todos-rename-category ()
3412 "Rename current Todos category.
3413 If this file has an archive containing this category, rename the
3414 category there as well."
3416 (let* ((cat (todos-current-category))
3417 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat))))
3418 (setq new (todos-validate-name new 'category))
3419 (let* ((ofile todos-current-todos-file)
3420 (archive (concat (file-name-sans-extension ofile) ".toda"))
3421 (buffers (append (list ofile)
3422 (unless (zerop (todos-get-count 'archived cat))
3424 (dolist (buf buffers)
3425 (with-current-buffer (find-file-noselect buf)
3426 (let (buffer-read-only)
3427 (setq todos-categories (todos-set-categories))
3430 (setcar (assoc cat todos-categories) new)
3432 (goto-char (point-min))
3433 (todos-update-categories-sexp)
3434 (re-search-forward (concat (regexp-quote todos-category-beg)
3435 "\\(" (regexp-quote cat) "\\)\n")
3437 (replace-match new t t nil 1)))))))
3438 (force-mode-line-update))
3439 (save-excursion (todos-category-select)))
3441 (defun todos-delete-category (&optional arg)
3442 "Delete current Todos category provided it is empty.
3443 With ARG non-nil delete the category unconditionally,
3444 i.e. including all existing todo and done items."
3446 (let* ((file todos-current-todos-file)
3447 (cat (todos-current-category))
3448 (todo (todos-get-count 'todo cat))
3449 (done (todos-get-count 'done cat))
3450 (archived (todos-get-count 'archived cat)))
3452 (or (> todo 0) (> done 0)))
3453 (message "%s" (substitute-command-keys
3454 (concat "To delete a non-empty category, "
3455 "type C-u \\[todos-delete-category].")))
3456 (when (cond ((= (length todos-categories) 1)
3457 (y-or-n-p (concat "This is the only category in this file; "
3458 "deleting it will also delete the file.\n"
3459 "Do you want to proceed? ")))
3461 (y-or-n-p (concat "This category has archived items; "
3462 "the archived category will remain\n"
3463 "after deleting the todo category. "
3464 "Do you still want to delete it\n"
3465 "(see 'todos-ignore-archived-categories' "
3466 "for another option)? ")))
3468 (y-or-n-p (concat "Permanently remove category \"" cat
3469 "\"" (and arg " and all its entries")
3472 (let ((buffer-read-only)
3473 (beg (re-search-backward
3474 (concat "^" (regexp-quote (concat todos-category-beg cat))
3476 (end (if (re-search-forward
3477 (concat "\n\\(" (regexp-quote todos-category-beg)
3481 (remove-overlays beg end)
3482 (delete-region beg end)
3483 (if (= (length todos-categories) 1)
3484 ;; If deleted category was the only one, delete the file.
3486 (todos-reevaluate-defcustoms)
3487 ;; Skip confirming killing the archive buffer if it has been
3488 ;; modified and not saved.
3489 (set-buffer-modified-p nil)
3492 (message "Deleted Todos file %s." file))
3493 (setq todos-categories (delete (assoc cat todos-categories)
3495 (todos-update-categories-sexp)
3496 (setq todos-category-number
3497 (1+ (mod todos-category-number (length todos-categories))))
3498 (todos-category-select)
3499 (goto-char (point-min))
3500 (message "Deleted category %s." cat)))))))
3502 (defun todos-raise-category-priority (&optional lower)
3503 "Raise priority of category point is on in Todos Categories buffer.
3504 With non-nil argument LOWER, lower the category's priority."
3506 (let ((num todos-categories-category-number))
3509 (skip-chars-forward " ")
3510 (setq num (number-at-point)))
3511 (when (and num (if lower
3512 (< num (length todos-categories))
3514 (let* ((col (current-column))
3515 (beg (progn (forward-line (if lower 0 -1)) (point)))
3516 (num1 (progn (skip-chars-forward " ") (1- (number-at-point))))
3518 (end (progn (forward-line 2) (point)))
3519 (catvec (vconcat todos-categories))
3520 (cat1-list (aref catvec num1))
3521 (cat2-list (aref catvec num2))
3522 (cat1 (car cat1-list))
3523 (cat2 (car cat2-list))
3524 buffer-read-only newcats)
3525 (delete-region beg end)
3526 (setq num1 (1+ num1))
3527 (setq num2 (1- num2))
3529 (todos-insert-category-line cat2)
3531 (todos-insert-category-line cat1)
3532 (aset catvec num2 (cons cat2 (cdr cat2-list)))
3533 (aset catvec num1 (cons cat1 (cdr cat1-list)))
3534 (setq todos-categories (append catvec nil))
3535 (setq newcats todos-categories)
3536 (with-current-buffer (get-file-buffer todos-current-todos-file)
3537 (setq todos-categories newcats)
3538 (todos-update-categories-sexp))
3539 (forward-line (if lower -1 -2))
3540 (forward-char col)))))
3542 (defun todos-lower-category-priority ()
3543 "Lower priority of category point is on in Todos Categories buffer."
3545 (todos-raise-category-priority t))
3547 (defun todos-set-category-priority ()
3553 (defun todos-move-category ()
3554 "Move current category to a different Todos file.
3555 If current category has archived items, also move those to the
3556 archive of the file moved to, creating it if it does not exist."
3558 (when (or (> (length todos-categories) 1)
3559 (y-or-n-p (concat "This is the only category in this file; "
3560 "moving it will also delete the file.\n"
3561 "Do you want to proceed? ")))
3562 (let* ((ofile todos-current-todos-file)
3563 (cat (todos-current-category))
3564 (nfile (todos-read-file-name "Choose a Todos file: " nil t))
3565 (archive (concat (file-name-sans-extension ofile) ".toda"))
3566 (buffers (append (list ofile)
3567 (unless (zerop (todos-get-count 'archived cat))
3570 (dolist (buf buffers)
3571 (with-current-buffer (find-file-noselect buf)
3573 (goto-char (point-max))
3574 (let* ((beg (re-search-backward
3576 (regexp-quote (concat todos-category-beg cat)))
3578 (end (if (re-search-forward
3579 (concat "^" (regexp-quote todos-category-beg))
3583 (content (buffer-substring-no-properties beg end))
3584 (counts (cdr (assoc cat todos-categories)))
3586 ;; Move the category to the new file. Also update or create
3587 ;; archive file if necessary.
3588 (with-current-buffer
3590 ;; Regenerate todos-archives in case there
3591 ;; is a newly created archive.
3592 (if (member buf (funcall todos-files-function t))
3593 (concat (file-name-sans-extension nfile) ".toda")
3595 (let* ((nfile-short (todos-short-file-name nfile))
3597 (format "Todos file \"%s\" already has "
3599 (format "the category \"%s\";\n" cat)
3600 "enter a new category name: "))
3603 (goto-char (point-max))
3605 ;; If the file moved to has a category with the same
3606 ;; name, rename the moved category.
3607 (when (assoc cat todos-categories)
3608 (unless (member (file-truename (buffer-file-name))
3609 (funcall todos-files-function t))
3610 (setq new (read-from-minibuffer prompt))
3611 (setq new (todos-validate-name new 'category))))
3612 ;; Replace old with new name in Todos and archive files.
3614 (goto-char (point-max))
3616 (concat "^" (regexp-quote todos-category-beg)
3617 "\\(" (regexp-quote cat) "\\)") nil t)
3618 (replace-match new nil nil nil 1)))
3619 (setq todos-categories
3620 (append todos-categories (list (cons new counts))))
3621 (todos-update-categories-sexp)
3622 ;; If archive was just created, save it to avoid "File <xyz> no
3623 ;; longer exists!" message on invoking
3624 ;; `todos-view-archived-items'. FIXME: maybe better to save
3626 (unless (file-exists-p (buffer-file-name))
3628 (todos-category-number (or new cat))
3629 (todos-category-select))
3630 ;; Delete the category from the old file, and if that was the
3631 ;; last category, delete the file. Also handle archive file
3633 (remove-overlays beg end)
3634 (delete-region beg end)
3635 (goto-char (point-min))
3636 ;; Put point after todos-categories sexp.
3638 (if (eobp) ; Aside from sexp, file is empty.
3640 ;; Skip confirming killing the archive buffer.
3641 (set-buffer-modified-p nil)
3642 (delete-file todos-current-todos-file)
3644 (when (member todos-current-todos-file todos-files)
3645 (todos-reevaluate-defcustoms)))
3646 (setq todos-categories (delete (assoc cat todos-categories)
3648 (todos-update-categories-sexp)
3649 (todos-category-select)))))
3650 (set-window-buffer (selected-window)
3651 (set-buffer (find-file-noselect nfile)))
3652 (todos-category-number (or new cat))
3653 (todos-category-select))))
3655 (defun todos-merge-category ()
3656 "Merge current category into another category in this file.
3657 The current category's todo and done items are appended to the
3658 chosen category's todo and done items, respectively, which
3659 becomes the current category, and the category moved from is
3662 (let ((buffer-read-only nil)
3663 (cat (todos-current-category))
3664 (goal (todos-read-category "Category to merge to: " t)))
3666 ;; FIXME: check if cat has archived items and merge those too
3669 (concat "^" (regexp-quote todos-category-beg)) nil t)
3671 (tbeg (progn (forward-line) (point)))
3674 (concat "^" (regexp-quote todos-category-done)) nil t)
3675 (forward-line) (point)))
3676 (tend (progn (forward-line -2) (point)))
3678 (if (re-search-forward
3679 (concat "^" (regexp-quote todos-category-beg)) nil t)
3682 (todo (buffer-substring-no-properties tbeg tend))
3683 (done (buffer-substring-no-properties dbeg cend))
3685 (goto-char (point-min))
3687 (concat "^" (regexp-quote (concat todos-category-beg goal))) nil t)
3689 (concat "^" (regexp-quote todos-category-done)) nil t)
3693 (goto-char (if (re-search-forward
3694 (concat "^" (regexp-quote todos-category-beg)) nil t)
3698 (remove-overlays cbeg cend)
3699 (delete-region cbeg cend)
3700 (todos-update-count 'todo (todos-get-count 'todo cat) goal)
3701 (todos-update-count 'done (todos-get-count 'done cat) goal)
3702 (setq todos-categories (delete (assoc cat todos-categories)
3704 (todos-update-categories-sexp)
3705 (todos-category-number goal)
3706 (todos-category-select)
3707 ;; Put point at the start of the merged todo items.
3708 ;; FIXME: what if there are no merged todo items but only done items?
3712 (defun todos-merge-categories ()
3715 (let* ((cats (mapcar 'car todos-categories))
3716 (goal (todos-read-category "Category to merge to: " t))
3717 (prompt (format "Merge to %s (type C-g to finish)? " goal))
3718 (source (let ((inhibit-quit t) l)
3719 (while (not (eq last-input-event 7))
3721 (when (y-or-n-p prompt)
3723 (setq cats (delete c cats))))))))
3727 ;; FIXME: make insertion options customizable per category?
3729 (defun todos-insert-item (&optional arg diary nonmarking date-type time
3731 "Add a new Todo item to a category.
3732 \(See the note at the end of this document string about key
3733 bindings and convenience commands derived from this command.)
3735 With no (or nil) prefix argument ARG, add the item to the current
3736 category; with one prefix argument (C-u), prompt for a category
3737 from the current Todos file; with two prefix arguments (C-u C-u),
3738 first prompt for a Todos file, then a category in that file. If
3739 a non-existing category is entered, ask whether to add it to the
3740 Todos file; if answered affirmatively, add the category and
3741 insert the item there.
3743 When argument DIARY is non-nil, this overrides the intent of the
3744 user option `todos-include-in-diary' for this item: if
3745 `todos-include-in-diary' is nil, include the item in the Fancy
3746 Diary display, and if it is non-nil, exclude the item from the
3747 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
3748 has its intended effect.
3750 When the item is included in the Fancy Diary display and the
3751 argument NONMARKING is non-nil, this overrides the intent of the
3752 user option `todos-diary-nonmarking' for this item: if
3753 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
3754 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
3756 The argument DATE-TYPE determines the content of the item's
3757 mandatory date header string and how it is added:
3758 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
3759 when the user puts the cursor on a date and hits RET, that
3760 date, in the format set by `calendar-date-display-form',
3761 becomes the date in the header.
3762 - If DATE-TYPE is the symbol `date', the header contains the date
3763 in the format set by `calendar-date-display-form', with year,
3764 month and day individually prompted for (month with tab
3766 - If DATE-TYPE is the symbol `dayname' the header contains a
3767 weekday name instead of a date, prompted for with tab
3769 - If DATE-TYPE has any other value (including nil or none) the
3770 header contains the current date (in the format set by
3771 `calendar-date-display-form').
3773 With non-nil argument TIME prompt for a time string, which must
3774 match `diary-time-regexp'. Typing `<return>' at the prompt
3775 returns the current time, if the user option
3776 `todos-always-add-time-string' is non-nil, otherwise the empty
3777 string (i.e., no time string). If TIME is absent or nil, add or
3778 omit the current time string according as
3779 `todos-always-add-time-string' is non-nil or nil, respectively.
3781 The argument REGION-OR-HERE determines the source and location of
3783 - If the REGION-OR-HERE is the symbol `here', prompt for the text
3784 of the new item and insert it directly above the todo item at
3785 point (hence lowering the priority of the remaining items), or
3786 if point is on the empty line below the last todo item, insert
3787 the new item there. An error is signalled if
3788 `todos-insert-item' is invoked with `here' outside of the
3790 - If REGION-OR-HERE is the symbol `region', use the region of the
3791 current buffer as the text of the new item, depending on the
3792 value of user option `todos-use-only-highlighted-region': if
3793 this is non-nil, then use the region only when it is
3794 highlighted; otherwise, use the region regardless of
3795 highlighting. An error is signalled if there is no region in
3796 the current buffer. Prompt for the item's priority in the
3797 category (an integer between 1 and one more than the number of
3798 items in the category), and insert the item accordingly.
3799 - If REGION-OR-HERE has any other value (in particular, nil or
3800 none), prompt for the text and the item's priority, and insert
3801 the item accordingly.
3803 To facilitate using these arguments when inserting a new todo
3804 item, convenience commands have been defined for all admissible
3805 combinations together with mnenomic key bindings based on on the
3806 name of the arguments and their order in the command's argument
3807 list: diar_y_ - nonmar_k_ing - _c_alendar or _d_ate or day_n_ame
3808 - _t_ime - _r_egion or _h_ere. These key combinations are
3809 appended to the basic insertion key (i) and keys that allow a
3810 following key must be doubled when used finally. For example,
3811 `iyh' will insert a new item with today's date, marked according
3812 to the DIARY argument described above, and with priority
3813 according to the HERE argument; while `iyy' does the same except
3814 the priority is not given by HERE but by prompting."
3815 ;; An alternative interface for customizing key
3816 ;; binding is also provided with the function
3817 ;; `todos-insertion-bindings'." ;FIXME
3819 (let ((region (eq region-or-here 'region))
3820 (here (eq region-or-here 'here)))
3822 (let (use-empty-active-region)
3823 (unless (and todos-use-only-highlighted-region (use-region-p))
3824 (error "There is no active region"))))
3825 (let* ((buf (current-buffer))
3826 (new-item (if region
3827 ;; FIXME: or keep properties?
3828 (buffer-substring-no-properties
3829 (region-beginning) (region-end))
3830 (read-from-minibuffer "Todo item: ")))
3832 ((eq date-type 'date)
3834 ((eq date-type 'dayname)
3835 (todos-read-dayname))
3836 ((eq date-type 'calendar)
3837 (setq todos-date-from-calendar t)
3838 (todos-set-date-from-calendar))
3839 (t (calendar-date-string (calendar-current-date) t t))))
3840 (time-string (or (and time (todos-read-time))
3841 (and todos-always-add-time-string
3842 (substring (current-time-string) 11 16)))))
3843 (setq todos-date-from-calendar nil)
3844 (cond ((equal arg '(16)) ; FIXME: cf. set-mark-command
3845 (todos-jump-to-category nil t)
3848 (set-buffer (get-file-buffer todos-global-current-todos-file))))
3849 ((equal arg '(4)) ; FIXME: just arg?
3850 (todos-jump-to-category)
3853 (set-buffer (get-file-buffer todos-global-current-todos-file))))
3855 (when (not (derived-mode-p 'todos-mode)) (todos-show))))
3856 (let (buffer-read-only)
3858 ;; Add date, time and diary marking as required.
3859 (concat (if (not (and diary (not todos-include-in-diary)))
3860 todos-nondiary-start
3861 (when (and nonmarking (not todos-diary-nonmarking))
3862 diary-nonmarking-symbol))
3863 date-string (unless (and time-string
3864 (string= time-string ""))
3865 (concat " " time-string))
3866 (when (not (and diary (not todos-include-in-diary)))
3869 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
3870 (setq new-item (replace-regexp-in-string
3871 "\\(\n\\)[^[:blank:]]"
3872 (concat "\n" (make-string todos-indent-to-here 32))
3873 new-item nil nil 1))
3875 (cond ((not (eq major-mode 'todos-mode))
3876 (error "Cannot insert a todo item here outside of Todos mode"))
3877 ((not (eq buf (current-buffer)))
3878 (error "Cannot insert an item here after changing buffer"))
3879 ((or (todos-done-item-p)
3880 ;; Point on last blank line.
3881 (save-excursion (forward-line -1) (todos-done-item-p)))
3882 (error "Cannot insert a new item in the done item section"))
3884 (todos-insert-with-overlays new-item)))
3885 (todos-set-item-priority new-item (todos-current-category) t))
3886 (todos-update-count 'todo 1)
3887 (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
3888 (todos-update-categories-sexp)))))
3890 (defvar todos-date-from-calendar nil
3891 "Helper variable for setting item date from the Emacs Calendar.")
3893 (defun todos-set-date-from-calendar ()
3894 "Return string of date chosen from Calendar."
3895 (when todos-date-from-calendar
3896 (let (calendar-view-diary-initially-flag)
3898 ;; *Calendar* is now current buffer.
3899 (local-set-key (kbd "RET") 'exit-recursive-edit)
3900 (message "Put cursor on a date and type <return> to set it.")
3901 ;; FIXME: is there a better way than recursive-edit? Use unwind-protect?
3902 ;; Check recursive-depth?
3904 (setq todos-date-from-calendar
3905 (calendar-date-string (calendar-cursor-to-date t) t t))
3907 todos-date-from-calendar))
3909 (defun todos-delete-item ()
3910 "Delete at least one item in this category.
3912 If there are marked items, delete all of these; otherwise, delete
3915 (let* ((cat (todos-current-category))
3916 (marked (assoc cat todos-categories-with-marks))
3917 (item (unless marked (todos-item-string)))
3918 (ov (make-overlay (save-excursion (todos-item-start))
3919 (save-excursion (todos-item-end))))
3920 ;; FIXME: make confirmation an option?
3922 (y-or-n-p "Permanently delete all marked items? ")
3924 (overlay-put ov 'face 'todos-search)
3925 (y-or-n-p (concat "Permanently delete this item? ")))))
3929 (and marked (goto-char (point-min)))
3932 (if (or (and marked (todos-marked-item-p)) item)
3934 (if (todos-done-item-p)
3935 (todos-update-count 'done -1)
3936 (todos-update-count 'todo -1 cat)
3937 (and (todos-diary-item-p) (todos-update-count 'diary -1)))
3940 ;; Don't leave point below last item.
3941 (and item (bolp) (eolp) (< (point-min) (point-max))
3942 (todos-backward-item))
3944 (throw 'done (setq item nil))))
3945 (todos-forward-item))))
3947 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
3948 (setq todos-categories-with-marks
3949 (assq-delete-all cat todos-categories-with-marks))
3951 (todos-update-categories-sexp)
3952 (todos-prefix-overlays))
3953 (if ov (delete-overlay ov))))
3955 (defun todos-edit-item ()
3956 "Edit the Todo item at point.
3957 If the item consists of only one logical line, edit it in the
3958 minibuffer; otherwise, edit it in Todos Edit mode."
3960 (when (todos-item-string)
3961 (let* ((buffer-read-only)
3962 (start (todos-item-start))
3965 (concat todos-date-string-start todos-date-pattern
3966 "\\( " diary-time-regexp "\\)?"
3967 (regexp-quote todos-nondiary-end) "?")
3968 (line-end-position) t)
3969 (1+ (- (point) start))))
3970 (item (todos-item-string))
3971 (multiline (> (length (split-string item "\n")) 1))
3974 (todos-edit-multiline t)
3975 (let ((new (read-string "Edit: " (cons item item-beg))))
3976 (while (not (string-match
3977 (concat todos-date-string-start todos-date-pattern) new))
3978 (setq new (read-from-minibuffer
3979 "Item must start with a date: " new)))
3980 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
3981 (setq new (replace-regexp-in-string
3982 "\\(\n\\)[^[:blank:]]"
3983 (concat "\n" (make-string todos-indent-to-here 32)) new
3985 ;; If user moved point during editing, make sure it moves back.
3988 (todos-insert-with-overlays new)
3989 (move-to-column item-beg))))))
3991 (defun todos-edit-multiline-item ()
3992 "Edit current Todo item in Todos Edit mode.
3993 Use of newlines invokes `todos-indent' to insure compliance with
3994 the format of Diary entries."
3996 (todos-edit-multiline t))
3998 (defun todos-edit-multiline (&optional item)
4001 ;; FIXME: should there be only one live Todos Edit buffer?
4002 ;; (let ((buffer-name todos-edit-buffer))
4003 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
4006 (set-buffer (make-indirect-buffer
4007 (file-name-nondirectory todos-current-todos-file)
4010 (narrow-to-region (todos-item-start) (todos-item-end))
4013 ;; (message (concat "Type %s to check file format validity and "
4014 ;; "return to Todos mode.\n")
4015 ;; (key-description (car (where-is-internal 'todos-edit-quit))))
4016 (message "%s" (substitute-command-keys
4017 (concat "Type \\[todos-edit-quit] to check file format "
4018 "validity and return to Todos mode.\n")))))
4020 (defun todos-edit-quit ()
4021 "Return from Todos Edit mode to Todos mode.
4023 If the whole file was in Todos Edit mode, check before returning
4024 whether the file is still a valid Todos file and if so, also
4025 recalculate the Todos categories sexp, in case changes were made
4026 in the number or names of categories."
4028 ;; FIXME: worth doing this only if file was actually changed?
4029 (when (eq (buffer-size) (- (point-max) (point-min)))
4030 (when (todos-check-format)
4031 (todos-make-categories-list t)))
4033 ;; In case next buffer is not the one holding todos-current-todos-file.
4036 (defun todos-edit-item-header (&optional what)
4037 "Edit date/time header of at least one item.
4039 Interactively, ask whether to edit year, month and day or day of
4040 the week, as well as time. If there are marked items, apply the
4041 changes to all of these; otherwise, edit just the item at point.
4043 Non-interactively, argument WHAT specifies whether to set the
4044 date from the Calendar or to today, or whether to edit only the
4045 date or day, or only the time."
4047 (let* ((cat (todos-current-category))
4048 (marked (assoc cat todos-categories-with-marks))
4049 (first t) ; Match only first of marked items.
4050 (todos-date-from-calendar t)
4051 ndate ntime nheader)
4053 (or (and marked (goto-char (point-min))) (todos-item-start))
4057 (while (not (todos-marked-item-p))
4058 (todos-forward-item)
4059 (and (eobp) (throw 'stop nil))))
4060 (re-search-forward (concat todos-date-string-start "\\(?1:"
4062 "\\)\\(?2: " diary-time-regexp "\\)?")
4063 (line-end-position) t)
4064 (let* ((odate (match-string-no-properties 1))
4065 (otime (match-string-no-properties 2))
4067 (cond ((eq what 'today)
4069 (setq ndate (calendar-date-string
4070 (calendar-current-date) t t))
4071 (replace-match ndate nil nil nil 1)))
4072 ((eq what 'calendar)
4073 (setq ndate (save-match-data (todos-set-date-from-calendar)))
4074 (replace-match ndate nil nil nil 1))
4076 (unless (eq what 'timeonly)
4078 (setq ndate (if (save-match-data
4079 (string-match "[0-9]+" odate))
4080 (if (y-or-n-p "Change date? ")
4082 (todos-read-dayname))
4083 (if (y-or-n-p "Change day? ")
4084 (todos-read-dayname)
4085 (todos-read-date)))))
4086 (replace-match ndate nil nil nil 1))
4087 (unless (eq what 'dateonly)
4089 (setq ntime (save-match-data (todos-read-time)))
4090 (when (< 0 (length ntime))
4091 (setq ntime (concat " " ntime))))
4093 (replace-match ntime nil nil nil 2)
4094 (goto-char (match-end 1))
4096 (setq todos-date-from-calendar nil)
4099 (todos-forward-item)
4100 (goto-char (point-max))))))))
4102 (defun todos-edit-item-date ()
4103 "Prompt for and apply changes to current item's date."
4105 (todos-edit-item-header 'dateonly))
4107 (defun todos-edit-item-date-from-calendar ()
4108 "Prompt for changes to current item's date and apply from Calendar."
4110 (todos-edit-item-header 'calendar))
4112 (defun todos-edit-item-date-is-today ()
4113 "Set item date to today's date."
4115 (todos-edit-item-header 'today))
4117 (defun todos-edit-item-time ()
4118 "Prompt For and apply changes to current item's time."
4120 (todos-edit-item-header 'timeonly))
4122 (defun todos-edit-item-diary-inclusion ()
4123 "Change diary status of one or more todo items in this category.
4124 That is, insert `todos-nondiary-marker' if the candidate items
4125 lack this marking; otherwise, remove it.
4127 If there are marked todo items, change the diary status of all
4128 and only these, otherwise change the diary status of the item at
4131 (let ((buffer-read-only)
4132 (marked (assoc (todos-current-category)
4133 todos-categories-with-marks)))
4136 (when marked (goto-char (point-min)))
4138 (if (todos-done-item-p)
4139 (throw 'stop (message "Done items cannot be edited"))
4140 (unless (and marked (not (todos-marked-item-p)))
4141 (let* ((beg (todos-item-start))
4142 (lim (save-excursion (todos-item-end)))
4143 (end (save-excursion
4144 (or (todos-time-string-matcher lim)
4145 (todos-date-string-matcher lim)))))
4146 (if (looking-at (regexp-quote todos-nondiary-start))
4149 (search-forward todos-nondiary-end (1+ end) t)
4151 (todos-update-count 'diary 1))
4153 (insert todos-nondiary-start)
4154 (goto-char (1+ end))
4155 (insert todos-nondiary-end)
4156 (todos-update-count 'diary -1)))))
4157 (unless marked (throw 'stop nil))
4158 (todos-forward-item)))))
4159 (todos-update-categories-sexp)))
4161 (defun todos-edit-category-diary-inclusion (arg)
4162 "Make all items in this category diary items.
4163 With prefix ARG, make all items in this category non-diary
4167 (goto-char (point-min))
4168 (let ((todo-count (todos-get-count 'todo))
4169 (diary-count (todos-get-count 'diary))
4173 (if (todos-done-item-p) ; We've gone too far.
4175 (let* ((beg (todos-item-start))
4176 (lim (save-excursion (todos-item-end)))
4177 (end (save-excursion
4178 (or (todos-time-string-matcher lim)
4179 (todos-date-string-matcher lim)))))
4181 (unless (looking-at (regexp-quote todos-nondiary-start))
4182 (insert todos-nondiary-start)
4183 (goto-char (1+ end))
4184 (insert todos-nondiary-end))
4185 (when (looking-at (regexp-quote todos-nondiary-start))
4187 (search-forward todos-nondiary-end (1+ end) t)
4188 (replace-match "")))))
4189 (todos-forward-item))
4190 (unless (if arg (zerop diary-count) (= diary-count todo-count))
4191 (todos-update-count 'diary (if arg
4193 (- todo-count diary-count))))
4194 (todos-update-categories-sexp)))))
4196 (defun todos-edit-item-diary-nonmarking ()
4197 "Change non-marking of one or more diary items in this category.
4198 That is, insert `diary-nonmarking-symbol' if the candidate items
4199 lack this marking; otherwise, remove it.
4201 If there are marked todo items, change the non-marking status of
4202 all and only these, otherwise change the non-marking status of
4205 (let ((buffer-read-only)
4206 (marked (assoc (todos-current-category)
4207 todos-categories-with-marks)))
4210 (when marked (goto-char (point-min)))
4212 (if (todos-done-item-p)
4213 (throw 'stop (message "Done items cannot be edited"))
4214 (unless (and marked (not (todos-marked-item-p)))
4216 (unless (looking-at (regexp-quote todos-nondiary-start))
4217 (if (looking-at (regexp-quote diary-nonmarking-symbol))
4219 (insert diary-nonmarking-symbol))))
4220 (unless marked (throw 'stop nil))
4221 (todos-forward-item)))))))
4223 (defun todos-edit-category-diary-nonmarking (arg)
4224 "Add `diary-nonmarking-symbol' to all diary items in this category.
4225 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
4226 items in this category."
4229 (goto-char (point-min))
4230 (let (buffer-read-only)
4233 (if (todos-done-item-p) ; We've gone too far.
4235 (unless (looking-at (regexp-quote todos-nondiary-start))
4237 (when (looking-at (regexp-quote diary-nonmarking-symbol))
4239 (unless (looking-at (regexp-quote diary-nonmarking-symbol))
4240 (insert diary-nonmarking-symbol))))
4241 (todos-forward-item)))))))
4243 (defun todos-raise-item-priority (&optional lower)
4244 "Raise priority of current item by moving it up by one item.
4245 With non-nil argument LOWER lower item's priority."
4247 (unless (or (todos-done-item-p) ; Can't reprioritize done items.
4248 ;; Can't raise or lower todo item when it's the only one.
4249 (< (todos-get-count 'todo) 2)
4250 ;; Point is between todo and done items.
4252 ;; Can't lower final todo item.
4255 (todos-forward-item)
4257 ;; Can't reprioritize filtered items other than Top Priorities.
4258 (and (eq major-mode 'todos-filter-items-mode)
4259 (not (string-match (regexp-quote todos-top-priorities-buffer)
4261 (let ((item (todos-item-string))
4262 (marked (todos-marked-item-p))
4264 ;; In Top Priorities buffer, an item's priority can be changed
4265 ;; wrt items in another category, but not wrt items in the same
4267 (when (eq major-mode 'todos-filter-items-mode)
4268 (let* ((regexp (concat todos-date-string-start todos-date-pattern
4269 "\\( " diary-time-regexp "\\)?"
4270 (regexp-quote todos-nondiary-end)
4271 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)"))
4272 (cat1 (save-excursion
4273 (re-search-forward regexp nil t)
4275 (cat2 (save-excursion
4277 (todos-forward-item)
4278 (todos-backward-item))
4279 (re-search-forward regexp nil t)
4281 (if (string= cat1 cat2)
4283 (concat "Cannot reprioritize items in the same "
4284 "category in this mode, only in Todos mode")))))
4286 (if lower (todos-forward-item) (todos-backward-item))
4287 (todos-insert-with-overlays item)
4288 ;; If item was marked, retore the mark.
4289 (and marked (overlay-put (make-overlay (point) (point))
4290 'before-string todos-item-mark)))))
4292 (defun todos-lower-item-priority ()
4293 "Lower priority of current item by moving it down by one item."
4295 (todos-raise-item-priority t))
4297 ;; FIXME: incorporate todos-(raise|lower)-item-priority ?
4298 (defun todos-set-item-priority (item cat &optional new)
4299 "Set todo ITEM's priority in category CAT, moving item as needed.
4300 Interactively, the item and the category are the current ones,
4301 and the priority is a number between 1 and the number of items in
4302 the category. Non-interactively with argument NEW, the lowest
4303 priority is one more than the number of items in CAT."
4304 (interactive (list (todos-item-string) (todos-current-category)))
4305 (unless (called-interactively-p t)
4306 (todos-category-number cat)
4307 (todos-category-select))
4308 (let* ((todo (todos-get-count 'todo cat))
4309 (maxnum (if new (1+ todo) todo))
4311 priority candidate prompt)
4312 (unless (zerop todo)
4313 (while (not priority)
4315 (string-to-number (read-from-minibuffer
4317 (format "Set item priority (1-%d): "
4320 (when (or (< candidate 1) (> candidate maxnum))
4321 (format "Priority must be an integer between 1 and %d.\n"
4323 (unless prompt (setq priority candidate)))
4324 ;; Interactively, just relocate the item within its category.
4325 (when (called-interactively-p) (todos-remove-item))
4326 (goto-char (point-min))
4327 (unless (= priority 1) (todos-forward-item (1- priority))))
4328 (todos-insert-with-overlays item)))
4330 (defun todos-set-item-top-priority ()
4331 "Set this item's priority in the Top Priorities display.
4332 Reprioritizing items that belong to the same category is not
4333 allowed; this is reserved for Todos mode."
4335 (when (string-match (regexp-quote todos-top-priorities-buffer) (buffer-name))
4337 (item (todos-item-string))
4338 (end (todos-item-end))
4339 (beg (todos-item-start))
4340 (regexp (concat todos-date-string-start todos-date-pattern
4341 "\\(?: " diary-time-regexp "\\)?"
4342 (regexp-quote todos-nondiary-end)
4343 "?\\(?1: \\[\\(?:.+:\\)?.+\\]\\)"))
4344 (cat (when (looking-at regexp) (match-string 1)))
4345 buffer-read-only current priority candidate prompt new)
4347 (goto-char (point-min))
4349 (setq count (1+ count))
4350 (when (string= item (todos-item-string))
4351 (setq current count))
4352 (todos-forward-item)))
4353 (unless (zerop count)
4354 (while (not priority)
4356 (string-to-number (read-from-minibuffer
4358 (format "Set item priority (1-%d): "
4361 (when (or (< candidate 1) (> candidate count))
4362 (format "Priority must be an integer between 1 and %d.\n"
4364 (unless prompt (setq priority candidate)))
4365 (goto-char (point-min))
4366 (unless (= priority 1) (todos-forward-item (1- priority)))
4367 (setq new (point-marker))
4368 (if (or (and (< priority current)
4370 (save-excursion (search-forward cat beg t)))
4371 (and (> priority current)
4372 (save-excursion (search-backward cat end t))))
4374 (set-marker new nil)
4376 (error (concat "Cannot reprioritize items in the same category "
4377 "in this mode, only in Todos mode")))
4381 (todos-insert-with-overlays item)
4382 (set-marker new nil))))))
4384 (defun todos-move-item (&optional file)
4385 "Move at least one todo item to another category.
4387 If there are marked items, move all of these; otherwise, move
4390 With non-nil argument FILE, first prompt for another Todos file and
4391 then a category in that file to move the item or items to.
4393 If the chosen category is not one of the existing categories,
4394 then it is created and the item(s) become(s) the first
4395 entry/entries in that category."
4397 (unless (or (todos-done-item-p)
4398 ;; Point is between todo and done items.
4400 (let* ((buffer-read-only)
4401 (file1 todos-current-todos-file)
4402 (cat1 (todos-current-category))
4403 (marked (assoc cat1 todos-categories-with-marks))
4404 (num todos-category-number)
4405 (item (todos-item-string))
4406 (diary-item (todos-diary-item-p))
4407 (omark (save-excursion (todos-item-start) (point-marker)))
4409 (todos-read-file-name "Choose a Todos file: " nil t)
4414 (set-buffer (find-file-noselect file2))
4415 (setq cat2 (let* ((pl (if (and marked (> (cdr marked) 1)) "s" ""))
4416 (name (todos-read-category
4417 (concat "Move item" pl " to category: ")))
4418 (prompt (concat "Choose a different category than "
4419 "the current one\n(type `"
4421 (car (where-is-internal
4422 'todos-set-item-priority)))
4423 "' to reprioritize item "
4424 "within the same category): ")))
4425 (while (equal name cat1)
4426 (setq name (todos-read-category prompt)))
4428 (set-buffer (get-file-buffer file1))
4432 (goto-char (point-min))
4434 (when (todos-marked-item-p)
4435 (setq item (concat item (todos-item-string) "\n"))
4436 (setq count (1+ count))
4437 (when (todos-diary-item-p)
4438 (setq count-diary (1+ count-diary))))
4439 (todos-forward-item))
4440 ;; Chop off last newline.
4441 (setq item (substring item 0 -1)))
4443 (when (todos-diary-item-p) (setq count-diary 1)))
4444 (set-window-buffer (selected-window)
4445 (set-buffer (find-file-noselect file2)))
4446 (unless (assoc cat2 todos-categories) (todos-add-category cat2))
4447 (todos-set-item-priority item cat2 t)
4448 (setq nmark (point-marker))
4449 (todos-update-count 'todo count)
4450 (todos-update-count 'diary count-diary)
4451 (todos-update-categories-sexp)
4452 (with-current-buffer (get-file-buffer file1)
4461 (concat "^" (regexp-quote todos-category-beg)) nil t)
4465 (concat "^" (regexp-quote todos-category-done)) nil t)
4466 (setq end (match-beginning 0))
4468 (while (< (point) end)
4469 (if (todos-marked-item-p)
4471 (todos-forward-item))))
4472 (todos-remove-item))))
4473 (todos-update-count 'todo (- count) cat1)
4474 (todos-update-count 'diary (- count-diary) cat1)
4475 (todos-update-categories-sexp))
4476 (set-window-buffer (selected-window)
4477 (set-buffer (find-file-noselect file2)))
4478 (setq todos-category-number (todos-category-number cat2))
4479 (todos-category-select)
4480 (goto-char nmark))))
4482 (defun todos-move-item-to-file ()
4483 "Move the current todo item to a category in another Todos file."
4485 (todos-move-item t))
4487 (defun todos-move-item-to-diary ()
4488 "Move one or more items in current category to the diary file.
4490 If there are marked items, move all of these; otherwise, move
4496 ;; FIXME: make adding date customizable, and make this and time customization
4497 ;; overridable via double prefix arg ??
4498 (defun todos-item-done (&optional arg)
4499 "Tag at least one item in this category as done and hide it.
4501 With prefix argument ARG prompt for a comment and append it to
4502 the done item; this is only possible if there are no marked
4503 items. If there are marked items, tag all of these with
4504 `todos-done-string' plus the current date and, if
4505 `todos-always-add-time-string' is non-nil, the current time;
4506 otherwise, just tag the item at point. Items tagged as done are
4507 relocated to the category's (by default hidden) done section."
4509 (let* ((cat (todos-current-category))
4510 (marked (assoc cat todos-categories-with-marks)))
4511 (unless (or (todos-done-item-p)
4512 (and (looking-at "^$") (not marked)))
4513 (let* ((date-string (calendar-date-string (calendar-current-date) t t))
4514 (time-string (if todos-always-add-time-string
4515 (concat " " (substring (current-time-string) 11 16))
4517 (done-prefix (concat "[" todos-done-string date-string time-string
4519 (comment (and arg (not marked) (read-string "Enter a comment: ")))
4524 (and marked (goto-char (point-min)))
4527 (if (or (not marked) (and marked (todos-marked-item-p)))
4529 (setq item (todos-item-string))
4530 (setq done-item (cond (marked
4531 (concat done-item done-prefix item "\n"))
4533 (concat done-prefix item " ["
4534 todos-comment-string
4537 (concat done-prefix item))))
4538 (setq item-count (1+ item-count))
4539 (when (todos-diary-item-p)
4540 (setq diary-count (1+ diary-count)))
4542 (unless marked (throw 'done nil)))
4543 (todos-forward-item))))
4545 ;; Chop off last newline of done item string.
4546 (setq done-item (substring done-item 0 -1))
4547 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
4548 (setq todos-categories-with-marks
4549 (assq-delete-all cat todos-categories-with-marks)))
4553 (concat "^" (regexp-quote todos-category-done)) nil t)
4555 (insert done-item "\n"))
4556 (todos-update-count 'todo (- item-count))
4557 (todos-update-count 'done item-count)
4558 (todos-update-count 'diary (- diary-count))
4559 (todos-update-categories-sexp)
4560 (save-excursion (todos-category-select))))))
4562 (defun todos-done-item-add-or-edit-comment ()
4563 "Add a comment to this done item or edit an existing comment."
4565 (when (todos-done-item-p)
4566 (let ((item (todos-item-string))
4567 (end (save-excursion (todos-item-end)))
4568 comment buffer-read-only)
4571 (if (re-search-forward (concat " \\["
4572 (regexp-quote todos-comment-string)
4573 ": \\([^]]+\\)\\]") end t)
4575 (setq comment (read-string "Edit comment: "
4576 (cons (match-string 1) 1)))
4577 (replace-match comment nil nil nil 1))
4578 (setq comment (read-string "Enter a comment: "))
4580 (insert " [" todos-comment-string ": " comment "]"))))))
4582 ;; FIXME: implement this or done item editing?
4583 (defun todos-uncomment-done-item ()
4587 ;; FIXME: delete comment from restored item or just leave it up to user?
4588 (defun todos-item-undo ()
4589 "Restore this done item to the todo section of this category."
4591 (when (todos-done-item-p)
4592 (let* ((buffer-read-only)
4593 (done-item (todos-item-string))
4595 (orig-mrk (progn (todos-item-start) (point-marker)))
4596 ;; Find the end of the date string added upon making item done.
4597 (start (search-forward "] "))
4598 (item (buffer-substring start (todos-item-end)))
4601 ;; If user cancels before setting new priority, then restore everything.
4604 (todos-set-item-priority item (todos-current-category) t)
4606 (todos-update-count 'todo 1)
4607 (todos-update-count 'done -1)
4608 (and (todos-diary-item-p) (todos-update-count 'diary 1))
4609 (todos-update-categories-sexp))
4612 (goto-char orig-mrk)
4613 (todos-insert-with-overlays done-item)
4614 (let ((todos-show-with-done t))
4615 (todos-category-select)
4616 (goto-char opoint)))
4617 (set-marker orig-mrk nil)))))
4619 (defun todos-archive-done-item (&optional all)
4620 "Archive at least one done item in this category.
4622 If there are marked done items (and no marked todo items),
4623 archive all of these; otherwise, with non-nil argument ALL,
4624 archive all done items in this category; otherwise, archive the
4627 If the archive of this file does not exist, it is created. If
4628 this category does not exist in the archive, it is created."
4630 ;; (when (not (member (buffer-file-name) (funcall todos-files-function t)))
4631 (when (eq major-mode 'todos-mode)
4632 (if (and all (zerop (todos-get-count 'done)))
4633 (message "No done items in this category")
4635 (let* ((cat (todos-current-category))
4636 (tbuf (current-buffer))
4637 (marked (assoc cat todos-categories-with-marks))
4638 (afile (concat (file-name-sans-extension
4639 todos-current-todos-file) ".toda"))
4640 (archive (if (file-exists-p afile)
4641 (find-file-noselect afile t)
4643 ;; todos-add-category requires an exisiting file...
4644 (with-current-buffer (get-buffer-create afile)
4646 (write-region (point-min) (point-max) afile
4647 nil 'nomessage nil t)))
4648 ;; ...but the file still lacks a categories sexp, so
4649 ;; visiting the file would barf on todos-set-categories,
4650 ;; hence we just return the buffer.
4651 (get-buffer afile)))
4652 (item (and (todos-done-item-p) (concat (todos-item-string) "\n")))
4654 marked-items beg end all-done
4659 (goto-char (point-min))
4661 (if (todos-marked-item-p)
4662 (if (not (todos-done-item-p))
4663 (throw 'end (message "Only done items can be archived"))
4664 (concat marked-items (todos-item-string) "\n")
4665 (setq count (1+ count)))
4666 (todos-forward-item)))))
4668 (if (y-or-n-p "Archive all done items in this category? ")
4671 (goto-char (point-min))
4674 (re-search-forward todos-done-string-start nil t)
4675 (match-beginning 0))
4676 end (if (re-search-forward
4677 (concat "^" (regexp-quote todos-category-beg))
4681 all-done (buffer-substring beg end)
4682 count (todos-get-count 'done))))
4684 (when (or marked all item)
4685 (with-current-buffer archive
4686 (let ((current todos-global-current-todos-file)
4689 (goto-char (point-min))
4692 (concat "^" (regexp-quote (concat todos-category-beg cat)))
4694 (re-search-forward (regexp-quote todos-category-done) nil t))
4696 ;; todos-add-category uses t-c-t-f, so temporarily set it.
4697 (setq todos-current-todos-file afile)
4698 (todos-add-category cat)
4699 (goto-char (point-max)))
4700 (insert (cond (marked marked-items)
4703 (todos-update-count 'done (if (or marked all) count 1))
4704 (todos-update-categories-sexp)
4705 ;; Save to file now (using write-region in order not to visit
4706 ;; afile) so we can visit it later with todos-show-archive.
4707 (write-region nil nil afile)
4708 (setq todos-current-todos-file current)))
4709 (with-current-buffer tbuf
4710 (cond ((or marked item)
4711 (and marked (goto-char (point-min)))
4714 (if (or (and marked (todos-marked-item-p)) item)
4717 (todos-update-count 'done -1)
4718 (todos-update-count 'archived 1)
4719 ;; Don't leave point below last item.
4720 (and item (bolp) (eolp) (< (point-min) (point-max))
4721 (todos-backward-item))
4723 (throw 'done (setq item nil))))
4724 (todos-forward-item)))))
4726 (remove-overlays beg end)
4727 (delete-region beg end)
4728 (todos-update-count 'done (- count))
4729 (todos-update-count 'archived count)))
4731 (remove-overlays (point-min) (point-max)
4732 'before-string todos-item-mark)
4733 (setq todos-categories-with-marks
4734 (assq-delete-all cat todos-categories-with-marks))
4736 (todos-update-categories-sexp)
4737 (todos-prefix-overlays)
4738 ;; FIXME: Heisenbug: item displays mark -- but not when edebugging
4739 (remove-overlays (point-min) (point-max)
4740 'before-string todos-item-mark)))
4741 (display-buffer (find-file-noselect afile) t)
4742 ;; FIXME: how to avoid switch-to-buffer and still get tbuf above
4743 ;; afile? What about pop-to-buffer-same-window in recent trunk?
4744 (switch-to-buffer tbuf))))))
4746 (defun todos-archive-category-done-items ()
4747 "Move all done items in this category to its archive."
4749 (todos-archive-done-item t))
4751 (defun todos-unarchive-items (&optional all)
4752 "Unarchive at least one item in this archive category.
4754 If there are marked items, unarchive all of these; otherwise,
4755 with non-nil argument ALL, unarchive all items in this category;
4756 otherwise, unarchive the item at point.
4758 Unarchived items are restored as done items to the corresponding
4759 category in the Todos file, inserted at the end of done section.
4760 If all items in the archive category were restored, the category
4761 is deleted from the archive. If this was the only category in the
4762 archive, the archive file is deleted."
4764 (when (member (buffer-file-name) (funcall todos-files-function t))
4766 (let* ((buffer-read-only nil)
4767 (tbuf (find-file-noselect
4768 (concat (file-name-sans-extension todos-current-todos-file)
4770 (cat (todos-current-category))
4771 (marked (assoc cat todos-categories-with-marks))
4772 (item (concat (todos-item-string) "\n"))
4773 (all-items (buffer-substring (point-min) (point-max)))
4774 (all-count (todos-get-count 'done))
4775 marked-items marked-count)
4777 (goto-char (point-min))
4779 (when (todos-marked-item-p)
4780 (concat marked-items (todos-item-string) "\n")
4781 (setq marked-count (1+ marked-count)))
4782 (todos-forward-item)))
4783 ;; Restore items to end of category's done section and update counts.
4784 (with-current-buffer tbuf
4785 (let (buffer-read-only)
4787 (goto-char (point-min))
4788 (re-search-forward (concat "^" (regexp-quote
4789 (concat todos-category-beg cat)))
4791 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
4793 (goto-char (match-beginning 0))
4794 (goto-char (point-max)))
4796 (insert marked-items)
4797 (todos-update-count 'done marked-count)
4798 (todos-update-count 'archived (- marked-count)))
4800 (if (y-or-n-p (concat "Restore this category's items "
4801 "to Todos file as done items "
4802 "and delete this category? "))
4803 (progn (insert all-items)
4804 (todos-update-count 'done all-count)
4805 (todos-update-count 'archived (- all-count)))
4809 (todos-update-count 'done 1)
4810 (todos-update-count 'archived -1)))
4811 (todos-update-categories-sexp)))
4812 ;; Delete restored items from archive.
4813 (cond ((or marked item)
4814 (and marked (goto-char (point-min)))
4817 (if (or (and marked (todos-marked-item-p)) item)
4820 (todos-update-count 'done -1)
4821 ;; Don't leave point below last item.
4822 (and item (bolp) (eolp) (< (point-min) (point-max))
4823 (todos-backward-item))
4825 (throw 'done (setq item nil))))
4826 (todos-forward-item)))))
4828 (remove-overlays (point-min) (point-max))
4829 (delete-region (point-min) (point-max))
4830 (todos-update-count 'done (- all-count))))
4831 ;; If that was the last category in the archive, delete the whole file.
4832 (if (= (length todos-categories) 1)
4834 (delete-file todos-current-todos-file)
4835 ;; Don't bother confirming killing the archive buffer.
4836 (set-buffer-modified-p nil)
4838 ;; Otherwise, if the archive category is now empty, delete it.
4839 (when (eq (point-min) (point-max))
4841 (let ((beg (re-search-backward
4842 (concat "^" (regexp-quote todos-category-beg) cat)
4844 (end (if (re-search-forward
4845 (concat "^" (regexp-quote todos-category-beg))
4849 (remove-overlays beg end)
4850 (delete-region beg end)
4851 (setq todos-categories (delete (assoc cat todos-categories)
4853 (setq todos-categories (delete (assoc cat todos-categories)
4855 (todos-update-categories-sexp))))
4856 ;; Visit category in Todos file and show restored done items.
4857 (let ((tfile (buffer-file-name tbuf))
4858 (todos-show-with-done t))
4859 (set-window-buffer (selected-window)
4860 (set-buffer (find-file-noselect tfile)))
4861 (todos-category-number cat)
4863 (message "Items unarchived."))))))
4865 (defun todos-unarchive-category ()
4866 "Unarchive all items in this category. See `todos-unarchive-items'."
4868 (todos-unarchive-items t))
4872 ;;; todos.el ends here
4874 ;; ---------------------------------------------------------------------------
4876 ;; FIXME: remove when part of Emacs
4877 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
4878 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
4880 ;;; Addition to calendar.el
4881 ;; FIXME: autoload when key-binding is defined in calendar.el
4882 (defun todos-insert-item-from-calendar ()
4885 ;; FIXME: todos-current-todos-file is nil here, better to solicit Todos
4886 ;; file? todos-global-current-todos-file is nil if no Todos file has been
4888 (pop-to-buffer (file-name-nondirectory todos-global-current-todos-file))
4890 ;; FIXME: this now calls todos-set-date-from-calendar
4891 (todos-insert-item t 'calendar))
4893 ;; FIXME: calendar is loaded before todos
4894 ;; (add-hook 'calendar-load-hook
4896 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar);))
4898 ;; ---------------------------------------------------------------------------
4899 ;;; necessitated adaptations to diary-lib.el
4901 ;; (defun diary-goto-entry (button)
4902 ;; "Jump to the diary entry for the BUTTON at point."
4903 ;; (let* ((locator (button-get button 'locator))
4904 ;; (marker (car locator))
4905 ;; markbuf file opoint)
4906 ;; ;; If marker pointing to diary location is valid, use that.
4907 ;; (if (and marker (setq markbuf (marker-buffer marker)))
4909 ;; (pop-to-buffer markbuf)
4910 ;; (goto-char (marker-position marker)))
4911 ;; ;; Marker is invalid (eg buffer has been killed, as is the case with
4912 ;; ;; included diary files).
4913 ;; (or (and (setq file (cadr locator))
4914 ;; (file-exists-p file)
4915 ;; (find-file-other-window file)
4917 ;; (when (eq major-mode (default-value 'major-mode)) (diary-mode))
4918 ;; (when (eq major-mode 'todos-mode) (widen))
4919 ;; (goto-char (point-min))
4920 ;; (when (re-search-forward (format "%s.*\\(%s\\)"
4921 ;; (regexp-quote (nth 2 locator))
4922 ;; (regexp-quote (nth 3 locator)))
4924 ;; (goto-char (match-beginning 1))
4925 ;; (when (eq major-mode 'todos-mode)
4926 ;; (setq opoint (point))
4927 ;; (re-search-backward (concat "^"
4928 ;; (regexp-quote todos-category-beg)
4931 ;; (todos-category-number (match-string 1))
4932 ;; (todos-category-select)
4933 ;; (goto-char opoint)))))
4934 ;; (message "Unable to locate this diary entry")))))