1 ;;; Todos.el --- facilities for making and maintaining Todo lists
3 ;; Copyright (C) 1997, 1999, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Oliver Seidel <privat@os10000.net>
6 ;; Stephen Berman <stephen.berman@gmx.net>
7 ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
9 ;; Keywords: calendar, todo
11 ;; This file is [not yet] part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; For remove-duplicates in todos-insertion-commands-args.
32 (eval-when-compile (require 'cl
))
34 ;; ---------------------------------------------------------------------------
38 "Create and maintain categorized lists of todo items."
39 :link
'(emacs-commentary-link "todos")
43 (defcustom todos-files-directory
(locate-user-emacs-file "todos/")
44 "Directory where user's Todos files are saved."
48 (defun todos-files (&optional archives
)
49 "Default value of `todos-files-function'.
50 This returns the case-insensitive alphabetically sorted list of
51 file truenames in `todos-files-directory' with the extension
52 \".todo\". With non-nil ARCHIVES return the list of archive file
53 truenames (those with the extension \".toda\")."
54 (let ((files (if (file-exists-p todos-files-directory
)
55 (mapcar 'file-truename
56 (directory-files todos-files-directory t
57 (if archives
"\.toda$" "\.todo$") t
)))))
58 (sort files
(lambda (s1 s2
) (let ((cis1 (upcase s1
))
60 (string< cis1 cis2
))))))
62 (defcustom todos-files-function
'todos-files
63 "Function returning the value of the variable `todos-files'.
64 This function should take an optional argument that, if non-nil,
65 makes it return the value of the variable `todos-archives'."
69 (defun todos-short-file-name (file)
70 "Return short form of Todos FILE.
71 This lacks the extension and directory components."
72 (file-name-sans-extension (file-name-nondirectory file
)))
74 (defcustom todos-default-todos-file
(todos-short-file-name
75 (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 (defun todos-reevaluate-default-file-defcustom ()
83 "Reevaluate defcustom of `todos-default-todos-file'.
84 Called after adding or deleting a Todos file."
85 (eval (defcustom todos-default-todos-file
(car (funcall todos-files-function
))
86 "Todos file visited by first session invocation of `todos-show'."
87 :type
`(radio ,@(mapcar (lambda (f) (list 'const f
))
88 (mapcar 'todos-short-file-name
89 (funcall todos-files-function
))))
92 (defcustom todos-show-current-file t
93 "Non-nil to make `todos-show' visit the current Todos file.
94 Otherwise, `todos-show' always visits `todos-default-todos-file'."
96 :initialize
'custom-initialize-default
97 :set
'todos-set-show-current-file
100 (defun todos-set-show-current-file (symbol value
)
101 "The :set function for user option `todos-show-current-file'."
102 (custom-set-default symbol value
)
104 (add-hook 'pre-command-hook
'todos-show-current-file nil t
)
105 (remove-hook 'pre-command-hook
'todos-show-current-file t
)))
107 (defcustom todos-category-completions-files nil
108 "List of files for building `todos-read-category' completions."
109 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
110 (mapcar 'todos-short-file-name
111 (funcall todos-files-function
))))
114 (defun todos-reevaluate-category-completions-files-defcustom ()
115 "Reevaluate defcustom of `todos-category-completions-files'.
116 Called after adding or deleting a Todos file."
117 (eval (defcustom todos-category-completions-files nil
118 "List of files for building `todos-read-category' completions."
119 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
120 (mapcar 'todos-short-file-name
121 (funcall todos-files-function
))))
124 (defcustom todos-visit-files-commands
(list 'find-file
'dired-find-file
)
125 "List of file finding commands for `todos-display-as-todos-file'.
126 Invoking these commands to visit a Todos or Todos Archive file
127 calls `todos-show' or `todos-show-archive', so that the file is
128 displayed correctly."
129 :type
'(repeat function
)
132 (defcustom todos-initial-file
"Todo"
133 "Default file name offered on adding first Todos file."
137 (defcustom todos-initial-category
"Todo"
138 "Default category name offered on initializing a new Todos file."
142 (defcustom todos-show-first
'first
143 "What action to take on first use of `todos-show' on a file."
144 :type
'(choice (const :tag
"Show first category" first
)
145 (const :tag
"Show table of categories" table
)
146 (const :tag
"Show top priorities" top
))
149 (defcustom todos-completion-ignore-case nil
150 "Non-nil means case is ignored by `todos-read-*' functions."
154 (defcustom todos-undo-item-omit-comment
'ask
155 "Whether to omit done item comment on undoing the item.
156 Nil means never omit the comment, t means always omit it, `ask'
157 means prompt user and omit comment only on confirmation."
158 :type
'(choice (const :tag
"Never" nil
)
159 (const :tag
"Always" t
)
160 (const :tag
"Ask" ask
))
163 (defcustom todos-print-function
'ps-print-buffer-with-faces
164 "Function called to print buffer content; see `todos-print'."
168 (defcustom todos-todo-mode-date-time-regexp
169 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
170 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
171 "Regexp matching legacy todo-mode.el item date-time strings.
172 In order for `todos-convert-legacy-files' to correctly convert this
173 string to the current Todos format, the regexp must contain four
174 explicitly numbered groups (see `(elisp) Regexp Backslash'),
175 where group 1 matches a string for the year, group 2 a string for
176 the month, group 3 a string for the day and group 4 a string for
177 the time. The default value converts date-time strings built
178 using the default value of `todo-time-string-format' from
183 ;; ---------------------------------------------------------------------------
184 ;;; Todos mode display options
186 (defgroup todos-mode-display nil
187 "User display options for Todos mode."
191 (defcustom todos-prefix
""
192 "String prefixed to todo items for visual distinction."
193 :type
'(string :validate
195 (when (string= (widget-value widget
) todos-item-mark
)
198 "Invalid value: must be distinct from `todos-item-mark'")
200 :initialize
'custom-initialize-default
201 :set
'todos-reset-prefix
202 :group
'todos-mode-display
)
204 (defcustom todos-number-priorities t
205 "Non-nil to prefix items with consecutively increasing integers.
206 These reflect the priorities of the items in each category."
208 :initialize
'custom-initialize-default
209 :set
'todos-reset-prefix
210 :group
'todos-mode-display
)
212 (defun todos-reset-prefix (symbol value
)
213 "The :set function for `todos-prefix' and `todos-number-priorities'."
214 (let ((oldvalue (symbol-value symbol
))
215 (files (append todos-files todos-archives
)))
216 (custom-set-default symbol value
)
217 (when (not (equal value oldvalue
))
219 (with-current-buffer (find-file-noselect f
)
220 (save-window-excursion
224 (goto-char (point-min))
226 (remove-overlays (point) (point)); 'before-string prefix)
228 ;; Activate the new setting (save-restriction does not help).
229 (save-excursion (todos-category-select))))))))
231 (defcustom todos-item-mark
"*"
232 "String used to mark items.
233 To ensure item marking works, change the value of this option
234 only when no items are marked."
235 :type
'(string :validate
237 (when (string= (widget-value widget
) todos-prefix
)
240 "Invalid value: must be distinct from `todos-prefix'")
242 :set
(lambda (symbol value
)
243 (custom-set-default symbol
(propertize value
'face
'todos-mark
)))
244 :group
'todos-mode-display
)
246 (defcustom todos-done-separator-string
"="
247 "String for generating `todos-done-separator'.
249 If the string consists of a single character,
250 `todos-done-separator' will be the string made by repeating this
251 character for the width of the window, and the length is
252 automatically recalculated when the window width changes. If the
253 string consists of more (or less) than one character, it will be
254 the value of `todos-done-separator'."
256 :initialize
'custom-initialize-default
257 :set
'todos-reset-done-separator-string
258 :group
'todos-mode-display
)
260 (defun todos-reset-done-separator-string (symbol value
)
261 "The :set function for `todos-done-separator-string'."
262 (let ((oldvalue (symbol-value symbol
))
263 (files todos-file-buffers
)
264 (sep todos-done-separator
))
265 (custom-set-default symbol value
)
266 (when (not (equal value oldvalue
))
268 (with-current-buffer (find-file-noselect f
)
269 (let (buffer-read-only)
270 (setq todos-done-separator
(todos-done-separator))
271 (when (= 1 (length value
))
272 (todos-reset-done-separator sep
)))
273 (todos-category-select))))))
275 (defcustom todos-done-string
"DONE "
276 "Identifying string appended to the front of done todos items."
278 :initialize
'custom-initialize-default
279 :set
'todos-reset-done-string
280 :group
'todos-mode-display
)
282 (defun todos-reset-done-string (symbol value
)
283 "The :set function for user option `todos-done-string'."
284 (let ((oldvalue (symbol-value symbol
))
285 (files (append todos-files todos-archives
)))
286 (custom-set-default symbol value
)
287 ;; Need to reset this to get font-locking right.
288 (setq todos-done-string-start
289 (concat "^\\[" (regexp-quote todos-done-string
)))
290 (when (not (equal value oldvalue
))
292 (with-current-buffer (find-file-noselect f
)
293 (let (buffer-read-only)
295 (goto-char (point-min))
297 (if (re-search-forward
298 (concat "^" (regexp-quote todos-nondiary-start
)
299 "\\(" (regexp-quote oldvalue
) "\\)")
301 (replace-match value t t nil
1)
303 (todos-category-select)))))))
305 (defcustom todos-comment-string
"COMMENT"
306 "String inserted before optional comment appended to done item."
308 :initialize
'custom-initialize-default
309 :set
'todos-reset-comment-string
310 :group
'todos-mode-display
)
312 (defun todos-reset-comment-string (symbol value
)
313 "The :set function for user option `todos-comment-string'."
314 (let ((oldvalue (symbol-value symbol
))
315 (files (append todos-files todos-archives
)))
316 (custom-set-default symbol value
)
317 (when (not (equal value oldvalue
))
319 (with-current-buffer (find-file-noselect f
)
320 (let (buffer-read-only)
323 (goto-char (point-min))
325 (if (re-search-forward
327 "\\[\\(" (regexp-quote oldvalue
) "\\): [^]]*\\]")
329 (replace-match value t t nil
1)
331 (todos-category-select))))))))
333 (defcustom todos-show-with-done nil
334 "Non-nil to display done items in all categories."
336 :group
'todos-mode-display
)
338 (defun todos-mode-line-control (cat)
339 "Return a mode line control for Todos buffers.
340 Argument CAT is the name of the current Todos category.
341 This function is the value of the user variable
342 `todos-mode-line-function'."
343 (let ((file (todos-short-file-name todos-current-todos-file
)))
344 (format "%s category %d: %s" file todos-category-number cat
)))
346 (defcustom todos-mode-line-function
'todos-mode-line-control
347 "Function that returns a mode line control for Todos buffers.
348 The function expects one argument holding the name of the current
349 Todos category. The resulting control becomes the local value of
350 `mode-line-buffer-identification' in each Todos buffer."
352 :group
'todos-mode-display
)
354 (defcustom todos-skip-archived-categories nil
355 "Non-nil to skip categories with only archived items when browsing.
357 Moving by category todos or archive file (with
358 \\[todos-forward-category] and \\[todos-backward-category]) skips
359 categories that contain only archived items. Other commands
360 still recognize these categories. In Todos Categories
361 mode (reached with \\[todos-display-categories]) these categories
362 shown in `todos-archived-only' face and clicking them in Todos
363 Categories mode visits the archived categories."
365 :group
'todos-mode-display
)
367 (defcustom todos-highlight-item nil
368 "Non-nil means highlight items at point."
370 :initialize
'custom-initialize-default
371 :set
'todos-reset-highlight-item
372 :group
'todos-mode-display
)
374 (defun todos-reset-highlight-item (symbol value
)
375 "The :set function for `todos-highlight-item'."
376 (let ((oldvalue (symbol-value symbol
))
377 (files (append todos-files todos-archives
)))
378 (custom-set-default symbol value
)
379 (when (not (equal value oldvalue
))
381 (let ((buf (find-buffer-visiting f
)))
383 (with-current-buffer buf
387 (hl-line-mode -
1)))))))))
389 (defcustom todos-wrap-lines t
390 "Non-nil to wrap long lines via `todos-line-wrapping-function'."
391 :group
'todos-mode-display
394 (defcustom todos-line-wrapping-function
'todos-wrap-and-indent
395 "Line wrapping function used with non-nil `todos-wrap-lines'."
396 :group
'todos-mode-display
399 (defun todos-wrap-and-indent ()
400 "Use word wrapping on long lines and indent with a wrap prefix.
401 The amount of indentation is given by user option
402 `todos-indent-to-here'."
403 (set (make-local-variable 'word-wrap
) t
)
404 (set (make-local-variable 'wrap-prefix
) (make-string todos-indent-to-here
32))
405 (unless (member '(continuation) fringe-indicator-alist
)
406 (push '(continuation) fringe-indicator-alist
)))
408 (defcustom todos-indent-to-here
6
409 "Number of spaces `todos-line-wrapping-function' indents to."
410 :type
'(integer :validate
412 (unless (> (widget-value widget
) 0)
413 (widget-put widget
:error
414 "Invalid value: must be a positive integer")
418 (defun todos-indent ()
419 "Indent from point to `todos-indent-to-here'."
420 (indent-to todos-indent-to-here todos-indent-to-here
))
422 ;; ---------------------------------------------------------------------------
423 ;;; Item insertion options
425 (defgroup todos-item-insertion nil
426 "User options for adding new todo items."
430 (defcustom todos-include-in-diary nil
431 "Non-nil to allow new Todo items to be included in the diary."
433 :group
'todos-item-insertion
)
435 (defcustom todos-diary-nonmarking nil
436 "Non-nil to insert new Todo diary items as nonmarking by default.
437 This appends `diary-nonmarking-symbol' to the front of an item on
438 insertion provided it doesn't begin with `todos-nondiary-marker'."
440 :group
'todos-item-insertion
)
442 (defcustom todos-nondiary-marker
'("[" "]")
443 "List of strings surrounding item date to block diary inclusion.
444 The first string is inserted before the item date and must be a
445 non-empty string that does not match a diary date in order to
446 have its intended effect. The second string is inserted after
448 :type
'(list string string
)
449 :group
'todos-item-insertion
450 :initialize
'custom-initialize-default
451 :set
'todos-reset-nondiary-marker
)
453 (defun todos-reset-nondiary-marker (symbol value
)
454 "The :set function for user option `todos-nondiary-marker'."
455 (let ((oldvalue (symbol-value symbol
))
456 (files (append todos-files todos-archives
)))
457 (custom-set-default symbol value
)
458 ;; Need to reset these to get font-locking right.
459 (setq todos-nondiary-start
(nth 0 todos-nondiary-marker
)
460 todos-nondiary-end
(nth 1 todos-nondiary-marker
)
461 todos-date-string-start
462 ;; See comment in defvar of `todos-date-string-start'.
463 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
464 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
465 (when (not (equal value oldvalue
))
467 (with-current-buffer (find-file-noselect f
)
468 (let (buffer-read-only)
470 (goto-char (point-min))
472 (if (re-search-forward
473 (concat "^\\(" todos-done-string-start
"[^][]+] \\)?"
474 "\\(?1:" (regexp-quote (car oldvalue
))
475 "\\)" todos-date-pattern
"\\( "
476 diary-time-regexp
"\\)?\\(?2:"
477 (regexp-quote (cadr oldvalue
)) "\\)")
480 (replace-match (nth 0 value
) t t nil
1)
481 (replace-match (nth 1 value
) t t nil
2))
483 (todos-category-select)))))))
485 (defcustom todos-always-add-time-string nil
486 "Non-nil adds current time to a new item's date header by default.
487 When the Todos insertion commands have a non-nil \"maybe-notime\"
488 argument, this reverses the effect of
489 `todos-always-add-time-string': if t, these commands omit the
490 current time, if nil, they include it."
492 :group
'todos-item-insertion
)
494 (defcustom todos-use-only-highlighted-region t
495 "Non-nil to enable inserting only highlighted region as new item."
497 :group
'todos-item-insertion
)
499 ;; ---------------------------------------------------------------------------
500 ;;; Todos Filter Items mode options
502 (defgroup todos-filtered nil
503 "User options for Todos Filter Items mode."
507 (defcustom todos-filtered-items-buffer
"Todos filtered items"
508 "Initial name of buffer in Todos Filter Items mode."
510 :group
'todos-filtered
)
512 (defcustom todos-top-priorities-buffer
"Todos top priorities"
513 "Buffer type string for `todos-filtered-buffer-name'."
515 :group
'todos-filtered
)
517 (defcustom todos-diary-items-buffer
"Todos diary items"
518 "Buffer type string for `todos-filtered-buffer-name'."
520 :group
'todos-filtered
)
522 (defcustom todos-regexp-items-buffer
"Todos regexp items"
523 "Buffer type string for `todos-filtered-buffer-name'."
525 :group
'todos-filtered
)
527 (defcustom todos-priorities-rules nil
528 "List of rules giving how many items `todos-top-priorities' shows.
529 This variable should be set interactively by
530 `\\[todos-set-top-priorities-in-file]' or
531 `\\[todos-set-top-priorities-in-category]'.
533 Each rule is a list of the form (FILE NUM ALIST), where FILE is a
534 member of `todos-files', NUM is a number specifying the default
535 number of top priority items for each category in that file, and
536 ALIST, when non-nil, consists of conses of a category name in
537 FILE and a number specifying the default number of top priority
538 items in that category, which overrides NUM."
540 :group
'todos-filtered
)
542 (defcustom todos-show-priorities
1
543 "Default number of top priorities shown by `todos-top-priorities'."
545 :group
'todos-filtered
)
547 (defcustom todos-filter-files nil
548 "List of default files for multifile item filtering."
549 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
550 (mapcar 'todos-short-file-name
551 (funcall todos-files-function
))))
552 :group
'todos-filtered
)
554 (defun todos-reevaluate-filter-files-defcustom ()
555 "Reevaluate defcustom of `todos-filter-files'.
556 Called after adding or deleting a Todos file."
557 (eval (defcustom todos-filter-files nil
558 "List of files for multifile item filtering."
559 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
560 (mapcar 'todos-short-file-name
561 (funcall todos-files-function
))))
564 (defcustom todos-filter-done-items nil
565 "Non-nil to include done items when processing regexp filters.
566 Done items from corresponding archive files are also included."
568 :group
'todos-filtered
)
570 ;; ---------------------------------------------------------------------------
571 ;;; Todos Categories mode options
573 (defgroup todos-categories nil
574 "User options for Todos Categories mode."
578 (defcustom todos-categories-category-label
"Category"
579 "Category button label in Todos Categories mode."
581 :group
'todos-categories
)
583 (defcustom todos-categories-todo-label
"Todo"
584 "Todo button label in Todos Categories mode."
586 :group
'todos-categories
)
588 (defcustom todos-categories-diary-label
"Diary"
589 "Diary button label in Todos Categories mode."
591 :group
'todos-categories
)
593 (defcustom todos-categories-done-label
"Done"
594 "Done button label in Todos Categories mode."
596 :group
'todos-categories
)
598 (defcustom todos-categories-archived-label
"Archived"
599 "Archived button label in Todos Categories mode."
601 :group
'todos-categories
)
603 (defcustom todos-categories-totals-label
"Totals"
604 "String to label total item counts in Todos Categories mode."
606 :group
'todos-categories
)
608 (defcustom todos-categories-number-separator
" | "
609 "String between number and category in Todos Categories mode.
610 This separates the number from the category name in the default
611 categories display according to priority."
613 :group
'todos-categories
)
615 (defcustom todos-categories-align
'center
616 "Alignment of category names in Todos Categories mode."
617 :type
'(radio (const left
) (const center
) (const right
))
618 :group
'todos-categories
)
620 ;; ---------------------------------------------------------------------------
621 ;;; Faces and font-lock matcher functions
623 (defgroup todos-faces nil
624 "Faces for the Todos modes."
628 (defface todos-prefix-string
629 ;; '((t :inherit font-lock-constant-face))
630 '((((class grayscale
) (background light
))
631 (:foreground
"LightGray" :weight bold
:underline t
))
632 (((class grayscale
) (background dark
))
633 (:foreground
"Gray50" :weight bold
:underline t
))
634 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
635 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
636 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
637 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
638 (((class color
) (min-colors 8)) (:foreground
"magenta"))
639 (t (:weight bold
:underline t
)))
640 "Face for Todos prefix or numerical priority string."
643 (defface todos-top-priority
644 ;; bold font-lock-comment-face
645 '((default :weight bold
)
646 (((class grayscale
) (background light
)) :foreground
"DimGray" :slant italic
)
647 (((class grayscale
) (background dark
)) :foreground
"LightGray" :slant italic
)
648 (((class color
) (min-colors 88) (background light
)) :foreground
"Firebrick")
649 (((class color
) (min-colors 88) (background dark
)) :foreground
"chocolate1")
650 (((class color
) (min-colors 16) (background light
)) :foreground
"red")
651 (((class color
) (min-colors 16) (background dark
)) :foreground
"red1")
652 (((class color
) (min-colors 8) (background light
)) :foreground
"red")
653 (((class color
) (min-colors 8) (background dark
)) :foreground
"yellow")
655 "Face for top priority Todos item numerical priority string.
656 The item's priority number string has this face if the number is
657 less than or equal the category's top priority setting."
661 ;; '((t :inherit font-lock-warning-face))
665 (:weight bold
:foreground
"Red1"))
669 (:weight bold
:foreground
"Pink"))
673 (:weight bold
:foreground
"Red1"))
677 (:weight bold
:foreground
"Pink"))
682 (:weight bold
:inverse-video t
)))
683 "Face for marks on Todos items."
686 (defface todos-button
687 ;; '((t :inherit widget-field))
689 (:foreground
"black" :background
"yellow3"))
690 (((class grayscale color
)
692 (:background
"gray85"))
693 (((class grayscale color
)
695 (:background
"dim gray"))
698 "Face for buttons in todos-display-categories."
701 (defface todos-sorted-column
706 (:background
"grey85"))
709 (:background
"grey85" :foreground
"grey10"))
711 (:background
"gray")))
712 "Face for buttons in todos-display-categories."
715 (defface todos-archived-only
716 ;; '((t (:inherit (shadow))))
719 (:foreground
"grey50"))
722 (:foreground
"grey70"))
724 (:foreground
"gray")))
725 "Face for archived-only categories in todos-display-categories."
728 (defface todos-search
729 ;; '((t :inherit match))
733 (:background
"yellow1"))
737 (:background
"RoyalBlue3"))
741 (:foreground
"black" :background
"yellow"))
745 (:foreground
"white" :background
"blue"))
750 (:background
"gray")))
751 "Face for matches found by todos-search."
754 (defface todos-diary-expired
755 ;; Doesn't contrast enough with todos-date (= diary) face.
756 ;; ;; '((t :inherit warning))
757 ;; '((default :weight bold)
758 ;; (((class color) (min-colors 16)) :foreground "DarkOrange")
759 ;; (((class color)) :foreground "yellow"))
760 ;; bold font-lock-function-name-face
761 '((default :weight bold
)
762 (((class color
) (min-colors 88) (background light
)) :foreground
"Blue1")
763 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSkyBlue")
764 (((class color
) (min-colors 16) (background light
)) :foreground
"Blue")
765 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSkyBlue")
766 (((class color
) (min-colors 8)) :foreground
"blue")
767 (t :inverse-video t
))
768 "Face for expired dates of diary items."
770 (defvar todos-diary-expired-face
'todos-diary-expired
)
773 '((t :inherit diary
))
774 "Face for the date string of a Todos item."
776 (defvar todos-date-face
'todos-date
)
779 '((t :inherit diary-time
))
780 "Face for the time string of a Todos item."
782 (defvar todos-time-face
'todos-time
)
784 (defface todos-nondiary
785 ;; '((t :inherit font-lock-type-face))
786 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
787 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
788 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
789 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
790 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
791 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
792 (((class color
) (min-colors 8)) :foreground
"green")
793 (t :weight bold
:underline t
))
794 "Face for non-diary markers around todo item date/time header."
796 (defvar todos-nondiary-face
'todos-nondiary
)
798 (defface todos-category-string
799 ;; '((t :inherit font-lock-type-face))
800 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
801 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
802 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
803 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
804 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
805 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
806 (((class color
) (min-colors 8)) :foreground
"green")
807 (t :weight bold
:underline t
))
808 "Face for category file names in Todos Filtered Item."
810 (defvar todos-category-string-face
'todos-category-string
)
813 ;; '((t :inherit font-lock-keyword-face))
814 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
815 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
816 (((class color
) (min-colors 88) (background light
)) :foreground
"Purple")
817 (((class color
) (min-colors 88) (background dark
)) :foreground
"Cyan1")
818 (((class color
) (min-colors 16) (background light
)) :foreground
"Purple")
819 (((class color
) (min-colors 16) (background dark
)) :foreground
"Cyan")
820 (((class color
) (min-colors 8)) :foreground
"cyan" :weight bold
)
822 "Face for done Todos item header string."
824 (defvar todos-done-face
'todos-done
)
826 (defface todos-comment
827 ;; '((t :inherit font-lock-comment-face))
828 '((((class grayscale
) (background light
))
829 :foreground
"DimGray" :weight bold
:slant italic
)
830 (((class grayscale
) (background dark
))
831 :foreground
"LightGray" :weight bold
:slant italic
)
832 (((class color
) (min-colors 88) (background light
))
833 :foreground
"Firebrick")
834 (((class color
) (min-colors 88) (background dark
))
835 :foreground
"chocolate1")
836 (((class color
) (min-colors 16) (background light
))
838 (((class color
) (min-colors 16) (background dark
))
840 (((class color
) (min-colors 8) (background light
))
842 (((class color
) (min-colors 8) (background dark
))
843 :foreground
"yellow")
844 (t :weight bold
:slant italic
))
845 "Face for comments appended to done Todos items."
847 (defvar todos-comment-face
'todos-comment
)
849 (defface todos-done-sep
850 ;; '((t :inherit font-lock-builtin-face))
851 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
852 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
853 (((class color
) (min-colors 88) (background light
)) :foreground
"dark slate blue")
854 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSteelBlue")
855 (((class color
) (min-colors 16) (background light
)) :foreground
"Orchid")
856 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSteelBlue")
857 (((class color
) (min-colors 8)) :foreground
"blue" :weight bold
)
859 "Face for separator string bewteen done and not done Todos items."
861 (defvar todos-done-sep-face
'todos-done-sep
)
863 (defun todos-date-string-matcher (lim)
864 "Search for Todos date string within LIM for font-locking."
866 (concat todos-date-string-start
"\\(?1:" todos-date-pattern
"\\)") lim t
))
868 (defun todos-time-string-matcher (lim)
869 "Search for Todos time string within LIM for font-locking."
870 (re-search-forward (concat todos-date-string-start todos-date-pattern
871 " \\(?1:" diary-time-regexp
"\\)") lim t
))
873 (defun todos-nondiary-marker-matcher (lim)
874 "Search for Todos nondiary markers within LIM for font-locking."
875 (re-search-forward (concat "^\\(?1:" (regexp-quote todos-nondiary-start
) "\\)"
876 todos-date-pattern
"\\(?: " diary-time-regexp
877 "\\)?\\(?2:" (regexp-quote todos-nondiary-end
) "\\)")
880 (defun todos-diary-nonmarking-matcher (lim)
881 "Search for diary nonmarking symbol within LIM for font-locking."
882 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
883 "\\)" todos-date-pattern
) lim t
))
885 (defun todos-diary-expired-matcher (lim)
886 "Search for expired diary item date within LIM for font-locking."
887 (when (re-search-forward (concat "^\\(?:"
888 (regexp-quote diary-nonmarking-symbol
)
889 "\\)?\\(?1:" todos-date-pattern
"\\) \\(?2:"
890 diary-time-regexp
"\\)?") lim t
)
891 (let* ((date (match-string-no-properties 1))
892 (time (match-string-no-properties 2))
893 ;; Function days-between requires a non-empty time string.
894 (date-time (concat date
" " (or time
"00:00"))))
895 (or (and (not (string-match ".+day\\|\\*" date
))
896 (< (days-between date-time
(current-time-string)) 0))
897 (todos-diary-expired-matcher lim
)))))
899 (defun todos-done-string-matcher (lim)
900 "Search for Todos done header within LIM for font-locking."
901 (re-search-forward (concat todos-done-string-start
905 (defun todos-comment-string-matcher (lim)
906 "Search for Todos done comment within LIM for font-locking."
907 (re-search-forward (concat "\\[\\(?1:" todos-comment-string
"\\):")
910 ;; (defun todos-category-string-matcher (lim)
911 ;; "Search for Todos category name within LIM for font-locking.
912 ;; This is for fontifying category names appearing in Todos filter
914 ;; (if (eq major-mode 'todos-filtered-items-mode)
915 ;; (re-search-forward
916 ;; (concat "^\\(?:" todos-date-string-start "\\)?" todos-date-pattern
917 ;; "\\(?: " diary-time-regexp "\\)?\\(?:"
918 ;; (regexp-quote todos-nondiary-end) "\\)? \\(?1:\\[.+\\]\\)")
921 (defun todos-category-string-matcher-1 (lim)
922 "Search for Todos category name within LIM for font-locking.
923 This is for fontifying category and file names appearing in Todos
924 Filtered Items mode following done items."
925 (if (eq major-mode
'todos-filtered-items-mode
)
926 (re-search-forward (concat todos-done-string-start todos-date-pattern
927 "\\(?: " diary-time-regexp
928 ;; Use non-greedy operator to prevent
929 ;; capturing possible following non-diary
931 "\\)?] \\(?1:\\[.+?\\]\\)")
934 (defun todos-category-string-matcher-2 (lim)
935 "Search for Todos category name within LIM for font-locking.
936 This is for fontifying category and file names appearing in Todos
937 Filtered Items mode following todo (not done) items."
938 (if (eq major-mode
'todos-filtered-items-mode
)
939 (re-search-forward (concat todos-date-string-start todos-date-pattern
940 "\\(?: " diary-time-regexp
"\\)?\\(?:"
941 (regexp-quote todos-nondiary-end
)
942 "\\)? \\(?1:\\[.+\\]\\)")
945 (defvar todos-font-lock-keywords
947 '(todos-nondiary-marker-matcher 1 todos-nondiary-face t
)
948 '(todos-nondiary-marker-matcher 2 todos-nondiary-face t
)
949 ;; diary-lib.el uses font-lock-constant-face for diary-nonmarking-symbol.
950 '(todos-diary-nonmarking-matcher 1 font-lock-constant-face t
)
951 '(todos-date-string-matcher 1 todos-date-face t
)
952 '(todos-time-string-matcher 1 todos-time-face t
)
953 '(todos-done-string-matcher 0 todos-done-face t
)
954 '(todos-comment-string-matcher 1 todos-comment-face t
)
955 '(todos-category-string-matcher-1 1 todos-category-string-face t t
)
956 '(todos-category-string-matcher-2 1 todos-category-string-face t t
)
957 '(todos-diary-expired-matcher 1 todos-diary-expired-face t
)
958 '(todos-diary-expired-matcher 2 todos-diary-expired-face t t
)
960 "Font-locking for Todos modes.")
962 ;; ---------------------------------------------------------------------------
963 ;;; Todos mode local variables and hook functions
965 (defvar todos-current-todos-file nil
966 "Variable holding the name of the currently active Todos file.")
968 (defun todos-show-current-file ()
969 "Visit current instead of default Todos file with `todos-show'.
970 This function is added to `pre-command-hook' when user option
971 `todos-show-current-file' is set to non-nil."
972 (setq todos-global-current-todos-file todos-current-todos-file
))
974 (defun todos-display-as-todos-file ()
975 "Show Todos files correctly when visited from outside of Todos mode."
976 (and (member this-command todos-visit-files-commands
)
977 (= (- (point-max) (point-min)) (buffer-size))
978 (member major-mode
'(todos-mode todos-archive-mode
))
979 (todos-category-select)))
981 (defun todos-add-to-buffer-list ()
982 "Add name of just visited Todos file to `todos-file-buffers'.
983 This function is added to `find-file-hook' in Todos mode."
984 (let ((filename (file-truename (buffer-file-name))))
985 (when (member filename todos-files
)
986 (add-to-list 'todos-file-buffers filename
))))
988 (defun todos-update-buffer-list ()
989 "Make current Todos mode buffer file car of `todos-file-buffers'.
990 This function is added to `post-command-hook' in Todos mode."
991 (let ((filename (file-truename (buffer-file-name))))
992 (unless (eq (car todos-file-buffers
) filename
)
993 (setq todos-file-buffers
994 (cons filename
(delete filename todos-file-buffers
))))))
996 (defun todos-reset-global-current-todos-file ()
997 "Update the value of `todos-global-current-todos-file'.
998 This becomes the latest existing Todos file or, if there is none,
999 the value of `todos-default-todos-file'.
1000 This function is added to `kill-buffer-hook' in Todos mode."
1001 (let ((filename (file-truename (buffer-file-name))))
1002 (setq todos-file-buffers
(delete filename todos-file-buffers
))
1003 (setq todos-global-current-todos-file
1004 (or (car todos-file-buffers
)
1005 (todos-absolute-file-name todos-default-todos-file
)))))
1007 (defvar todos-categories nil
1008 "Alist of categories in the current Todos file.
1009 The elements are cons cells whose car is a category name and
1010 whose cdr is a vector of the category's item counts. These are,
1011 in order, the numbers of todo items, of todo items included in
1012 the Diary, of done items and of archived items.")
1014 (defvar todos-categories-with-marks nil
1015 "Alist of categories and number of marked items they contain.")
1017 (defvar todos-category-number
1
1018 "Variable holding the number of the current Todos category.
1019 Todos categories are numbered starting from 1.")
1021 (defvar todos-show-done-only nil
1022 "If non-nil display only done items in current category.
1023 Set by the command `todos-show-done-only' and used by
1024 `todos-category-select'.")
1026 (defun todos-reset-and-enable-done-separator ()
1027 "Show resized done items separator overlay after window change.
1028 Added to `window-configuration-change-hook' in `todos-mode'."
1029 (when (= 1 (length todos-done-separator-string
))
1030 (let ((sep todos-done-separator
))
1031 (setq todos-done-separator
(todos-done-separator))
1032 (save-match-data (todos-reset-done-separator sep
)))))
1034 ;; ---------------------------------------------------------------------------
1035 ;;; Global variables and helper functions for files and buffers
1037 (defvar todos-files
(funcall todos-files-function
)
1038 "List of truenames of user's Todos files.")
1040 (defvar todos-archives
(funcall todos-files-function t
)
1041 "List of truenames of user's Todos archives.")
1043 (defvar todos-visited nil
1044 "List of Todos files visited in this session by `todos-show'.
1045 Used to determine initial display according to the value of
1046 `todos-show-first'.")
1048 (defvar todos-file-buffers nil
1049 "List of file names of live Todos mode buffers.")
1051 (defvar todos-global-current-todos-file nil
1052 "Variable holding name of current Todos file.
1053 Used by functions called from outside of Todos mode to visit the
1054 current Todos file rather than the default Todos file (i.e. when
1055 users option `todos-show-current-file' is non-nil).")
1057 (defun todos-reevaluate-filelist-defcustoms ()
1058 "Reevaluate defcustoms that provide choice list of Todos files."
1059 (custom-set-default 'todos-default-todos-file
1060 (symbol-value 'todos-default-todos-file
))
1061 (todos-reevaluate-default-file-defcustom)
1062 (custom-set-default 'todos-filter-files
(symbol-value 'todos-filter-files
))
1063 (todos-reevaluate-filter-files-defcustom)
1064 (custom-set-default 'todos-category-completions-files
1065 (symbol-value 'todos-category-completions-files
))
1066 (todos-reevaluate-category-completions-files-defcustom))
1068 (defvar todos-edit-buffer
"*Todos Edit*"
1069 "Name of current buffer in Todos Edit mode.")
1071 (defvar todos-categories-buffer
"*Todos Categories*"
1072 "Name of buffer in Todos Categories mode.")
1074 (defvar todos-print-buffer
"*Todos Print*"
1075 "Name of buffer containing printable Todos text.")
1077 (defun todos-absolute-file-name (name &optional type
)
1078 "Return the absolute file name of short Todos file NAME.
1079 With TYPE `archive' or `top' return the absolute file name of the
1080 short Todos Archive or Top Priorities file name, respectively."
1081 ;; NOP if there is no Todos file yet (i.e. don't concatenate nil).
1084 (concat todos-files-directory name
1085 (cond ((eq type
'archive
) ".toda")
1086 ((eq type
'top
) ".todt")
1089 (defun todos-check-format ()
1090 "Signal an error if the current Todos file is ill-formatted.
1091 Otherwise return t. The error message gives the line number
1092 where the invalid formatting was found."
1096 (goto-char (point-min))
1097 ;; Check for `todos-categories' sexp as the first line
1098 (let ((cats (prin1-to-string todos-categories
)))
1099 (unless (looking-at (regexp-quote cats
))
1100 (error "Invalid or missing todos-categories sexp")))
1102 (let ((legit (concat "\\(^" (regexp-quote todos-category-beg
) "\\)"
1103 "\\|\\(" todos-date-string-start todos-date-pattern
"\\)"
1104 "\\|\\(^[ \t]+[^ \t]*\\)"
1106 "\\|\\(^" (regexp-quote todos-category-done
) "\\)"
1107 "\\|\\(" todos-done-string-start
"\\)")))
1109 (unless (looking-at legit
)
1110 (error "Illegitimate Todos file format at line %d"
1111 (line-number-at-pos (point))))
1113 ;; (message "This Todos file is well-formatted.")
1116 ;; ---------------------------------------------------------------------------
1117 (defun todos-convert-legacy-date-time ()
1118 "Return converted date-time string.
1119 Helper function for `todos-convert-legacy-files'."
1120 (let* ((year (match-string 1))
1121 (month (match-string 2))
1122 (monthname (calendar-month-name (string-to-number month
) t
))
1123 (day (match-string 3))
1124 (time (match-string 4))
1127 (insert (mapconcat 'eval calendar-date-display-form
"")
1128 (when time
(concat " " time
)))))
1130 ;; ---------------------------------------------------------------------------
1131 ;;; Global variables and helper functions for categories
1133 (defun todos-category-number (cat)
1134 "Return the number of category CAT in this Todos file.
1135 The buffer-local variable `todos-category-number' holds this
1136 number as its value."
1137 (let ((categories (mapcar 'car todos-categories
)))
1138 (setq todos-category-number
1139 ;; Increment by one, so that the highest priority category in Todos
1140 ;; Categories mode is numbered one rather than zero.
1141 (1+ (- (length categories
)
1142 (length (member cat categories
)))))))
1144 (defun todos-current-category ()
1145 "Return the name of the current category."
1146 (car (nth (1- todos-category-number
) todos-categories
)))
1148 (defconst todos-category-beg
"--==-- "
1149 "String marking beginning of category (inserted with its name).")
1151 (defconst todos-category-done
"==--== DONE "
1152 "String marking beginning of category's done items.")
1154 (defun todos-done-separator ()
1155 "Return string used as value of variable `todos-done-separator'."
1156 (let ((sep todos-done-separator-string
))
1157 (propertize (if (= 1 (length sep
))
1158 ;; If separator's length is window-width, an
1159 ;; indented empty line appears between the
1160 ;; separator and the first done item.
1161 ;; FIXME: should this be customizable?
1162 (make-string (1- (window-width)) (string-to-char sep
))
1163 todos-done-separator-string
)
1164 'face
'todos-done-sep
)))
1166 (defvar todos-done-separator
(todos-done-separator)
1167 "String used to visually separate done from not done items.
1168 Displayed as an overlay instead of `todos-category-done' when
1169 done items are shown. Its value is determined by user option
1170 `todos-done-separator-string'.")
1172 (defun todos-reset-done-separator (sep)
1173 "Replace existing overlays of done items separator string SEP."
1177 (goto-char (point-min))
1178 (while (re-search-forward
1179 (concat "\n\\(" (regexp-quote todos-category-done
) "\\)") nil t
)
1180 (let* ((beg (match-beginning 1))
1182 (ovs (overlays-at beg
))
1183 (ov (when ovs
(car ovs
)))
1184 (old-sep (when ov
(overlay-get ov
'display
)))
1186 (when (string= old-sep sep
)
1187 (setq new-ov
(make-overlay beg end
))
1188 (overlay-put new-ov
'display todos-done-separator
)
1189 (delete-overlay ov
)))))))
1191 (defun todos-category-completions ()
1192 "Return a list of completions for `todos-read-category'.
1193 Each element of the list is a cons of a category name and the
1194 file or list of files (as short file names) it is in. The files
1195 are the current (or else the default) Todos file plus all other
1196 Todos files named in `todos-category-completions-files'."
1197 (let* ((curfile (or todos-current-todos-file
1198 (and todos-show-current-file
1199 todos-global-current-todos-file
)
1200 (todos-absolute-file-name todos-default-todos-file
)))
1201 (files (or (mapcar 'todos-absolute-file-name
1202 todos-category-completions-files
)
1205 ;; If file was just added, it has no category completions.
1206 (unless (zerop (buffer-size (find-buffer-visiting curfile
)))
1207 (add-to-list 'files curfile
)
1208 (dolist (f files listall
)
1209 (with-current-buffer (find-file-noselect f
'nowarn
)
1213 (goto-char (point-min))
1214 (setq listf
(read (buffer-substring-no-properties
1215 (line-beginning-position)
1216 (line-end-position)))))))
1217 (mapc (lambda (elt) (let* ((cat (car elt
))
1218 (la-elt (assoc cat listall
)))
1220 (setcdr la-elt
(append (list (cdr la-elt
))
1222 (push (cons cat f
) listall
))))
1225 (defun todos-category-select ()
1226 "Display the current category correctly."
1227 (let ((name (todos-current-category))
1228 cat-begin cat-end done-start done-sep-start done-end
)
1230 (goto-char (point-min))
1232 (concat "^" (regexp-quote (concat todos-category-beg name
)) "$") nil t
)
1233 (setq cat-begin
(1+ (line-end-position)))
1234 (setq cat-end
(if (re-search-forward
1235 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
1238 (setq mode-line-buffer-identification
1239 (funcall todos-mode-line-function name
))
1240 (narrow-to-region cat-begin cat-end
)
1241 (todos-prefix-overlays)
1242 (goto-char (point-min))
1243 (if (re-search-forward (concat "\n\\(" (regexp-quote todos-category-done
)
1246 (setq done-start
(match-beginning 0))
1247 (setq done-sep-start
(match-beginning 1))
1248 (setq done-end
(match-end 0)))
1249 (error "Category %s is missing todos-category-done string" name
))
1250 (if todos-show-done-only
1251 (narrow-to-region (1+ done-end
) (point-max))
1252 (when (and todos-show-with-done
1253 (re-search-forward todos-done-string-start nil t
))
1254 ;; Now we want to see the done items, so reset displayed end to end of
1256 (setq done-start cat-end
)
1257 ;; Make display overlay for done items separator string, unless there
1259 (let* ((done-sep todos-done-separator
)
1260 (ovs (overlays-at done-sep-start
))
1263 ;; There should never be more than one overlay here, so car suffices.
1264 (unless (and ovs
(string= (overlay-get (car ovs
) 'display
) done-sep
))
1265 (setq ov-sep
(make-overlay done-sep-start done-end
))
1266 (overlay-put ov-sep
'display done-sep
))))
1267 ;; (setq ov-sep0 (make-overlay done-sep-start done-end))
1268 ;; (setq ov-sep1 (make-overlay done-end done-end))
1269 ;; (overlay-put ov-sep0 'invisible t)
1270 ;; (overlay-put ov-sep1 'after-string done-sep)))
1271 (narrow-to-region (point-min) done-start
)
1272 ;; Loading this from todos-mode, or adding it to the mode hook, causes
1273 ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
1274 (when todos-highlight-item
1276 (hl-line-mode 1)))))
1278 (defun todos-get-count (type &optional category
)
1279 "Return count of TYPE items in CATEGORY.
1280 If CATEGORY is nil, default to the current category."
1281 (let* ((cat (or category
(todos-current-category)))
1282 (counts (cdr (assoc cat todos-categories
)))
1283 (idx (cond ((eq type
'todo
) 0)
1284 ((eq type
'diary
) 1)
1286 ((eq type
'archived
) 3))))
1289 (defun todos-update-count (type increment
&optional category
)
1290 "Change count of TYPE items in CATEGORY by integer INCREMENT.
1291 With nil or omitted CATEGORY, default to the current category."
1292 (let* ((cat (or category
(todos-current-category)))
1293 (counts (cdr (assoc cat todos-categories
)))
1294 (idx (cond ((eq type
'todo
) 0)
1295 ((eq type
'diary
) 1)
1297 ((eq type
'archived
) 3))))
1298 (aset counts idx
(+ increment
(aref counts idx
)))))
1300 (defun todos-set-categories ()
1301 "Set `todos-categories' from the sexp at the top of the file."
1302 ;; New archive files created by `todos-move-category' are empty, which would
1303 ;; make the sexp test fail and raise an error, so in this case we skip it.
1304 (unless (zerop (buffer-size))
1308 (goto-char (point-min))
1309 (setq todos-categories
1310 (if (looking-at "\(\(\"")
1311 (read (buffer-substring-no-properties
1312 (line-beginning-position)
1313 (line-end-position)))
1314 (error "Invalid or missing todos-categories sexp")))))))
1316 (defun todos-update-categories-sexp ()
1317 "Update the `todos-categories' sexp at the top of the file."
1318 (let (buffer-read-only)
1322 (goto-char (point-min))
1323 (if (looking-at (concat "^" (regexp-quote todos-category-beg
)))
1324 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
1325 (setq todos-categories
(todos-make-categories-list t
)))
1326 (delete-region (line-beginning-position) (line-end-position)))
1327 (prin1 todos-categories
(current-buffer))))))
1329 (defun todos-make-categories-list (&optional force
)
1330 "Return an alist of Todos categories and their item counts.
1331 With non-nil argument FORCE parse the entire file to build the
1332 list; otherwise, get the value by reading the sexp at the top of
1334 (setq todos-categories nil
)
1338 (goto-char (point-min))
1339 (let (counts cat archive
)
1340 ;; If the file is a todo file and has archived items, identify the
1341 ;; archive, in order to count its items. But skip this with
1342 ;; `todos-convert-legacy-files', since that converts filed items to
1344 (when buffer-file-name
; During conversion there is no file yet.
1345 ;; If the file is an archive, it doesn't have an archive.
1346 (unless (member (file-truename buffer-file-name
)
1347 (funcall todos-files-function t
))
1348 (setq archive
(concat (file-name-sans-extension
1349 todos-current-todos-file
) ".toda"))))
1351 (cond ((looking-at (concat (regexp-quote todos-category-beg
)
1353 (setq cat
(match-string-no-properties 1))
1354 ;; Counts for each category: [todo diary done archive]
1355 (setq counts
(make-vector 4 0))
1356 (setq todos-categories
1357 (append todos-categories
(list (cons cat counts
))))
1358 ;; Add archived item count to the todo file item counts.
1359 ;; Make sure to include newly created archives, e.g. due to
1360 ;; todos-move-category.
1361 (when (member archive
(funcall todos-files-function t
))
1362 (let ((archive-count 0))
1363 (with-current-buffer (find-file-noselect archive
)
1365 (goto-char (point-min))
1366 (when (re-search-forward
1367 (concat "^" (regexp-quote todos-category-beg
)
1371 (while (not (or (looking-at
1373 (regexp-quote todos-category-beg
)
1376 (when (looking-at todos-done-string-start
)
1377 (setq archive-count
(1+ archive-count
)))
1379 (todos-update-count 'archived archive-count cat
))))
1380 ((looking-at todos-done-string-start
)
1381 (todos-update-count 'done
1 cat
))
1382 ((looking-at (concat "^\\("
1383 (regexp-quote diary-nonmarking-symbol
)
1384 "\\)?" todos-date-pattern
))
1385 (todos-update-count 'diary
1 cat
)
1386 (todos-update-count 'todo
1 cat
))
1387 ((looking-at (concat todos-date-string-start todos-date-pattern
))
1388 (todos-update-count 'todo
1 cat
))
1389 ;; If first line is todos-categories list, use it and end loop
1390 ;; -- unless FORCEd to scan whole file.
1393 (setq todos-categories
(read (buffer-substring-no-properties
1394 (line-beginning-position)
1395 (line-end-position))))
1396 (goto-char (1- (point-max))))))
1400 (defun todos-repair-categories-sexp ()
1401 "Repair corrupt Todos categories sexp.
1402 This should only be needed as a consequence of careless manual
1403 editing or a bug in todos.el."
1405 (let ((todos-categories (todos-make-categories-list t
)))
1406 (todos-update-categories-sexp)))
1408 ;;; Global variables and helper functions for items
1410 (defconst todos-month-name-array
1411 (vconcat calendar-month-name-array
(vector "*"))
1412 "Array of month names, in order.
1413 The final element is \"*\", indicating an unspecified month.")
1415 (defconst todos-month-abbrev-array
1416 (vconcat calendar-month-abbrev-array
(vector "*"))
1417 "Array of abbreviated month names, in order.
1418 The final element is \"*\", indicating an unspecified month.")
1420 (defconst todos-date-pattern
1421 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
1422 (concat "\\(?5:" dayname
"\\|"
1424 (monthname (format "\\(?6:%s\\)" (diary-name-pattern
1425 todos-month-name-array
1426 todos-month-abbrev-array
)))
1427 (month "\\(?7:[0-9]+\\|\\*\\)")
1428 (day "\\(?8:[0-9]+\\|\\*\\)")
1429 (year "-?\\(?9:[0-9]+\\|\\*\\)"))
1430 (mapconcat 'eval calendar-date-display-form
""))
1432 "Regular expression matching a Todos date header.")
1434 (defconst todos-nondiary-start
(nth 0 todos-nondiary-marker
)
1435 "String inserted before item date to block diary inclusion.")
1437 (defconst todos-nondiary-end
(nth 1 todos-nondiary-marker
)
1438 "String inserted after item date matching `todos-nondiary-start'.")
1440 ;; By itself this matches anything, because of the `?'; however, it's only
1441 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
1443 (defconst todos-date-string-start
1444 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
1445 (regexp-quote diary-nonmarking-symbol
) "\\)?")
1446 "Regular expression matching part of item header before the date.")
1448 (defconst todos-done-string-start
1449 (concat "^\\[" (regexp-quote todos-done-string
))
1450 "Regular expression matching start of done item.")
1452 (defconst todos-item-start
(concat "\\(" todos-date-string-start
"\\|"
1453 todos-done-string-start
"\\)"
1455 "String identifying start of a Todos item.")
1457 (defun todos-item-start ()
1458 "Move to start of current Todos item and return its position."
1460 ;; Buffer is empty (invocation possible e.g. via todos-forward-item
1461 ;; from todos-filter-items when processing category with no todo
1463 (eq (point-min) (point-max))
1464 ;; Point is on the empty line below category's last todo item...
1465 (and (looking-at "^$")
1466 (or (eobp) ; ...and done items are hidden...
1467 (save-excursion ; ...or done items are visible.
1469 (looking-at (concat "^"
1470 (regexp-quote todos-category-done
))))))
1471 ;; Buffer is widened.
1472 (looking-at (regexp-quote todos-category-beg
)))
1473 (goto-char (line-beginning-position))
1474 (while (not (looking-at todos-item-start
))
1478 (defun todos-item-end ()
1479 "Move to end of current Todos item and return its position."
1480 ;; Items cannot end with a blank line.
1481 (unless (looking-at "^$")
1482 (let* ((done (todos-done-item-p))
1484 ;; For todo items, end is before the done items section, for done
1485 ;; items, end is before the next category. If these limits are
1486 ;; missing or inaccessible, end it before the end of the buffer.
1487 (lim (if (save-excursion
1489 (concat "^" (regexp-quote (if done
1491 todos-category-done
)))
1493 (progn (setq to-lim t
) (match-beginning 0))
1495 (when (bolp) (forward-char)) ; Find start of next item.
1496 (goto-char (if (re-search-forward todos-item-start lim t
)
1498 (if to-lim lim
(point-max))))
1499 ;; For last todo item, skip back over the empty line before the done
1500 ;; items section, else just back to the end of the previous line.
1501 (backward-char (when (and to-lim
(not done
) (eq (point) lim
)) 2))
1504 (defun todos-item-string ()
1505 "Return bare text of current item as a string."
1506 (let ((opoint (point))
1507 (start (todos-item-start))
1508 (end (todos-item-end)))
1510 (and start end
(buffer-substring-no-properties start end
))))
1512 (defun todos-remove-item ()
1513 "Internal function called in editing, deleting or moving items."
1514 (let* ((beg (todos-item-start))
1515 (end (progn (todos-item-end) (1+ (point))))
1516 (ovs (overlays-in beg beg
)))
1517 ;; There can be both prefix/number and mark overlays.
1518 (while ovs
(delete-overlay (car ovs
)) (pop ovs
))
1519 (delete-region beg end
)))
1521 (defun todos-diary-item-p ()
1522 "Return non-nil if item at point has diary entry format."
1524 (when (todos-item-string) ; Exclude empty lines.
1526 (not (looking-at (regexp-quote todos-nondiary-start
))))))
1528 (defun todos-done-item-p ()
1529 "Return non-nil if item at point is a done item."
1532 (looking-at todos-done-string-start
)))
1534 ;; (defun todos-done-item-section-p ()
1535 ;; "Return non-nil if point is in category's done items section."
1537 ;; (or (re-search-backward (concat "^" (regexp-quote todos-category-done))
1539 ;; (progn (goto-char (point-min))
1540 ;; (looking-at todos-done-string-start)))))
1542 (defun todos-prefix-overlay ()
1543 "Return this item's prefix overlay."
1544 (let* ((lbp (line-beginning-position))
1545 (ovs (overlays-in lbp lbp
)))
1548 (defun todos-marked-item-p ()
1549 "Non-nil if this item begins with `todos-item-mark'.
1550 In that case, return the item's prefix overlay."
1551 ;; If a todos-item-insert command is called on a Todos file before
1552 ;; it is visited, it has no prefix overlays, so conditionalize:
1553 (let* ((ov (todos-prefix-overlay))
1554 (pref (when ov
(overlay-get ov
'before-string
)))
1556 (string-match (concat "^" (regexp-quote todos-item-mark
))
1560 (defun todos-insert-with-overlays (item)
1561 "Insert ITEM at point and update prefix/priority number overlays."
1563 ;; Insertion pushes item down but not its prefix overlay. When the
1564 ;; overlay includes a mark, this would now mark the inserted ITEM,
1565 ;; so move it to the pushed down item.
1566 (let ((ov (todos-prefix-overlay))
1567 (marked (todos-marked-item-p)))
1569 (when marked
(move-overlay ov
(point) (point))))
1570 (todos-backward-item)
1571 (todos-prefix-overlays))
1573 (defun todos-prefix-overlays ()
1574 "Update the prefix overlays of the current category's items.
1575 The overlay's value is the string `todos-prefix' or with non-nil
1576 `todos-number-priorities' an integer in the sequence from 1 to
1577 the number of todo or done items in the category indicating the
1578 item's priority. Todo and done items are numbered independently
1580 (let ((prefix (propertize (concat todos-prefix
" ")
1581 'face
'todos-prefix-string
))
1583 (cat-tp (or (cdr (assoc-string
1584 (todos-current-category)
1585 (nth 2 (assoc-string todos-current-todos-file
1586 todos-priorities-rules
))))
1587 todos-show-priorities
))
1590 (goto-char (point-min))
1592 (when (or (todos-date-string-matcher (line-end-position))
1593 (todos-done-string-matcher (line-end-position)))
1594 (goto-char (match-beginning 0))
1595 (when todos-number-priorities
1597 ;; Reset number to 1 for first done item.
1598 (when (and (looking-at todos-done-string-start
)
1599 (looking-back (concat "^"
1600 (regexp-quote todos-category-done
)
1604 (setq prefix
(propertize (concat (number-to-string num
) " ")
1606 ;; Numbers of top priorities have
1607 ;; a distinct face in Todos mode.
1608 (if (and (not done
) (<= num cat-tp
)
1609 (eq major-mode
'todos-mode
))
1611 'todos-prefix-string
))))
1612 (let ((ov (todos-prefix-overlay))
1613 (marked (todos-marked-item-p)))
1614 (unless ov
(setq ov
(make-overlay (point) (point))))
1615 (overlay-put ov
'before-string
(if marked
1616 (concat todos-item-mark prefix
)
1620 ;; ---------------------------------------------------------------------------
1621 ;;; Helper functions for user input with prompting and completion
1623 (defun todos-read-file-name (prompt &optional archive mustmatch
)
1624 "Choose and return the name of a Todos file, prompting with PROMPT.
1626 Show completions with TAB or SPC; the names are shown in short
1627 form but the absolute truename is returned. With non-nil ARCHIVE
1628 return the absolute truename of a Todos archive file. With non-nil
1629 MUSTMATCH the name of an existing file must be chosen;
1630 otherwise, a new file name is allowed."
1631 (let* ((completion-ignore-case todos-completion-ignore-case
)
1632 (files (mapcar 'todos-short-file-name
1633 (if archive todos-archives todos-files
)))
1634 (file (completing-read prompt files nil mustmatch nil nil
1636 ;; Trigger prompt for initial file.
1638 (unless (file-exists-p todos-files-directory
)
1639 (make-directory todos-files-directory
))
1641 (setq file
(todos-validate-name file
'file
)))
1642 (setq file
(file-truename (concat todos-files-directory file
1643 (if archive
".toda" ".todo"))))))
1645 (defun todos-read-category (prompt &optional match-type file
)
1646 "Choose and return a category name, prompting with PROMPT.
1647 Show completions for existing categories with TAB or SPC.
1649 The argument MATCH-TYPE specifies the matching requirements on
1650 the category name: with the value `merge' the name must complete
1651 to that of an existing category; with the value `add' the name
1652 must not be that of an existing category; with all other values
1653 both existing and new valid category names are accepted.
1655 With non-nil argument FILE prompt for a file and complete only
1656 against categories in that file; otherwise complete against all
1657 categories from `todos-category-completions-files'."
1658 ;; Allow SPC to insert spaces, for adding new category names.
1659 (let ((map minibuffer-local-completion-map
))
1660 (define-key map
" " nil
)
1661 (let* ((add (eq match-type
'add
))
1662 (file0 (when (and file
(> (length todos-files
) 1))
1663 (todos-read-file-name "Choose a Todos file: " nil t
)))
1664 (completions (unless file0
(todos-category-completions)))
1665 (categories (cond (file0
1666 (with-current-buffer
1667 (find-file-noselect file0
'nowarn
)
1668 (let ((todos-current-todos-file file0
))
1670 ((and add
(not file
))
1671 (with-current-buffer
1672 (find-file-noselect todos-current-todos-file
)
1676 (completion-ignore-case todos-completion-ignore-case
)
1677 (cat (completing-read prompt categories nil
1678 (eq match-type
'merge
) nil nil
1679 ;; Unless we're adding a category via
1680 ;; todos-add-category, set default
1681 ;; for existing categories to the
1682 ;; current category of the chosen
1683 ;; file or else of the current file.
1684 (if (and categories
(not add
))
1685 (with-current-buffer
1688 todos-current-todos-file
1689 (todos-absolute-file-name
1690 todos-default-todos-file
)))
1691 (todos-current-category))
1692 ;; Trigger prompt for initial category.
1694 (catfil (cdr (assoc cat completions
)))
1695 (str "Category \"%s\" from which file (TAB for choices)? "))
1696 ;; If we do category completion and the chosen category name
1697 ;; occurs in more than one file, prompt to choose one file.
1698 (unless (or file0 add
(not catfil
))
1699 (setq file0
(file-truename
1702 (todos-absolute-file-name
1703 (completing-read (format str cat
)
1704 todos-category-completions-files
))))))
1705 ;; Default to the current file.
1706 (unless file0
(setq file0 todos-current-todos-file
))
1707 ;; First validate only a name passed interactively from
1708 ;; todos-add-category, which must be of a nonexisting category.
1709 (unless (and (assoc cat categories
) (not add
))
1710 ;; Validate only against completion categories.
1711 (let ((todos-categories categories
))
1712 (setq cat
(todos-validate-name cat
'category
)))
1713 ;; When user enters a nonexisting category name by jumping or
1714 ;; moving, confirm that it should be added, then validate.
1716 (if (y-or-n-p (format "Add new category \"%s\" to file \"%s\"? "
1717 cat
(todos-short-file-name file0
)))
1719 (when (assoc cat categories
)
1720 (let ((todos-categories categories
))
1721 (setq cat
(todos-validate-name cat
'category
))))
1722 ;; Restore point and narrowing after adding new
1723 ;; category, to avoid moving to beginning of file when
1724 ;; moving marked items to a new category
1725 ;; (todos-move-item).
1728 (todos-add-category file0 cat
))))
1729 ;; If we decide not to add a category, exit without returning.
1733 (defun todos-validate-name (name type
)
1734 "Prompt for new NAME for TYPE until it is valid, then return it.
1735 TYPE can be either of the symbols `file' or `category'."
1736 (let ((categories todos-categories
)
1737 (files (mapcar 'todos-short-file-name todos-files
))
1740 (and (cond ((string= "" name
)
1742 (cond ((eq type
'file
)
1744 "Enter a non-empty file name: "
1745 ;; Empty string passed by todos-show to
1746 ;; prompt for initial Todos file.
1747 (concat "Initial file name ["
1748 todos-initial-file
"]: ")))
1749 ((eq type
'category
)
1751 "Enter a non-empty category name: "
1752 ;; Empty string passed by todos-show to
1753 ;; prompt for initial category of a new
1755 (concat "Initial category name ["
1756 todos-initial-category
"]: "))))))
1757 ((string-match "\\`\\s-+\\'" name
)
1759 "Enter a name that does not contain only white space: "))
1760 ((and (eq type
'file
) (member name files
))
1761 (setq prompt
"Enter a non-existing file name: "))
1762 ((and (eq type
'category
) (assoc name categories
))
1763 (setq prompt
"Enter a non-existing category name: ")))
1764 (setq name
(if (or (and (eq type
'file
) files
)
1765 (and (eq type
'category
) categories
))
1766 (completing-read prompt
(cond ((eq type
'file
)
1768 ((eq type
'category
)
1770 ;; Offer default initial name.
1771 (completing-read prompt
(if (eq type
'file
)
1774 nil nil
(if (eq type
'file
)
1776 todos-initial-category
))))))
1779 ;; Adapted from calendar-read-date and calendar-date-string.
1780 (defun todos-read-date (&optional arg mo yr
)
1781 "Prompt for Gregorian date and return it in the current format.
1783 With non-nil ARG, prompt for and return only the date component
1784 specified by ARG, which can be one of these symbols:
1785 `month' (prompt for name, return name or number according to
1786 value of `calendar-date-display-form'), `day' of month, or
1787 `year'. The value of each of these components can be `*',
1788 indicating an unspecified month, day, or year.
1790 When ARG is `day', non-nil arguments MO and YR determine the
1791 number of the last the day of the month."
1792 (let (year monthname month day
1793 dayname
) ; Needed by calendar-date-display-form.
1794 (when (or (not arg
) (eq arg
'year
))
1795 (while (if (natnump year
) (< year
1) (not (eq year
'*)))
1796 (setq year
(read-from-minibuffer
1797 "Year (>0 or RET for this year or * for any year): "
1798 nil nil t nil
(number-to-string
1799 (calendar-extract-year
1800 (calendar-current-date)))))))
1801 (when (or (not arg
) (eq arg
'month
))
1802 (let* ((marray todos-month-name-array
)
1803 (mlist (append marray nil
))
1804 (mabarray todos-month-abbrev-array
)
1805 (mablist (append mabarray nil
))
1806 (completion-ignore-case todos-completion-ignore-case
))
1807 (setq monthname
(completing-read
1808 "Month name (RET for current month, * for any month): "
1809 ;; (mapcar 'list (append marray nil))
1811 (calendar-month-name (calendar-extract-month
1812 (calendar-current-date)) t
))
1813 ;; month (cdr (assoc-string
1814 ;; monthname (calendar-make-alist marray nil nil
1816 month
(1+ (- (length mlist
)
1817 (length (or (member monthname mlist
)
1818 (member monthname mablist
))))))
1819 (setq monthname
(aref mabarray
(1- month
)))))
1820 (when (or (not arg
) (eq arg
'day
))
1821 (let ((last (let ((mm (or month mo
))
1823 ;; If month is unspecified, use a month with 31
1824 ;; days for checking day of month input. Does
1825 ;; Calendar do anything special when * is
1826 ;; currently a shorter month?
1827 (if (= mm
13) (setq mm
1))
1828 ;; If year is unspecified, use a leap year to
1830 (if (eq year
'*) (setq yy
2012))
1831 (calendar-last-day-of-month mm yy
))))
1832 (while (if (natnump day
) (or (< day
1) (> day last
)) (not (eq day
'*)))
1833 (setq day
(read-from-minibuffer
1834 (format "Day (1-%d or RET for today or * for any day): "
1836 nil nil t nil
(number-to-string
1837 (calendar-extract-day
1838 (calendar-current-date))))))))
1839 ;; Stringify read values (monthname is already a string).
1840 (and year
(setq year
(if (eq year
'*)
1842 (number-to-string year
))))
1843 (and day
(setq day
(if (eq day
'*)
1845 (number-to-string day
))))
1846 (and month
(setq month
(if (eq month
'*)
1848 (number-to-string month
))))
1850 (cond ((eq arg
'year
) year
)
1853 (if (memq 'month calendar-date-display-form
)
1856 (mapconcat 'eval calendar-date-display-form
""))))
1858 (defun todos-read-dayname ()
1859 "Choose name of a day of the week with completion and return it."
1860 (let ((completion-ignore-case todos-completion-ignore-case
))
1861 (completing-read "Enter a day name: "
1862 (append calendar-day-name-array nil
)
1865 (defun todos-read-time ()
1866 "Prompt for and return a valid clock time as a string.
1868 Valid time strings are those matching `diary-time-regexp'.
1869 Typing `<return>' at the prompt returns the current time, if the
1870 user option `todos-always-add-time-string' is non-nil, otherwise
1871 the empty string (i.e., no time string)."
1874 (setq answer
(read-string "Enter a clock time: " nil nil
1875 (when todos-always-add-time-string
1876 (substring (current-time-string) 11 16))))
1877 (when (or (string= "" answer
)
1878 (string-match diary-time-regexp answer
))
1882 ;; ---------------------------------------------------------------------------
1883 ;;; Item filtering infrastructure
1885 (defvar todos-multiple-filter-files nil
1886 "List of files selected from `todos-multiple-filter-files' widget.")
1888 (defvar todos-multiple-filter-files-widget nil
1889 "Variable holding widget created by `todos-multiple-filter-files'.")
1891 (defun todos-multiple-filter-files ()
1892 "Pop to a buffer with a widget for choosing multiple filter files."
1895 (require 'wid-edit
))
1896 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
1897 (pop-to-buffer (current-buffer))
1899 (kill-all-local-variables)
1900 (widget-insert "Select files for generating the top priorities list.\n\n")
1901 (setq todos-multiple-filter-files-widget
1903 `(set ,@(mapcar (lambda (x) (list 'const x
))
1904 (mapcar 'todos-short-file-name
1905 (funcall todos-files-function
))))))
1906 (widget-insert "\n")
1907 (widget-create 'push-button
1908 :notify
(lambda (widget &rest ignore
)
1909 (setq todos-multiple-filter-files
'quit
)
1911 (exit-recursive-edit))
1914 (widget-create 'push-button
1915 :notify
(lambda (&rest ignore
)
1916 (setq todos-multiple-filter-files
1919 (concat todos-files-directory
1922 todos-multiple-filter-files-widget
)))
1924 (exit-recursive-edit))
1926 (use-local-map widget-keymap
)
1928 (message "Click \"Apply\" after selecting files.")
1931 (defun todos-filter-items (filter file-list
)
1932 "Display a list of items from FILE-LIST that satisfy FILTER.
1933 The values of FILE-LIST and FILTER are passed from the calling
1934 commands. The files in FILE-LIST are either the current Todos
1935 file or those listed in `todos-filter-files' or chosen
1936 interactively. The values of FILTER can be `top' for top
1937 priority items, a cons of `top' and a number passed by the
1938 caller, `diary' for diary items, or `regexp' for items matching a
1939 regular expresion entered by the user."
1940 (let ((num (if (consp filter
) (cdr filter
) todos-show-priorities
))
1941 (buf (get-buffer-create todos-filtered-items-buffer
))
1942 (multifile (> (length file-list
) 1))
1943 regexp fname bufstr cat beg end done
)
1944 (if (null file-list
)
1945 (error "No files have been chosen for filtering")
1946 (with-current-buffer buf
1948 (kill-all-local-variables)
1949 (todos-filtered-items-mode))
1950 (when (eq filter
'regexp
)
1951 (setq regexp
(read-string "Enter a regular expression: ")))
1952 (save-current-buffer
1953 (dolist (f file-list
)
1954 ;; Before inserting file contents into temp buffer, save a modified
1955 ;; buffer visiting it.
1956 (let ((bf (find-buffer-visiting f
)))
1957 (when (buffer-modified-p bf
)
1958 (with-current-buffer bf
(save-buffer))))
1959 (setq fname
(todos-short-file-name f
))
1961 (when (and todos-filter-done-items
(eq filter
'regexp
))
1962 ;; If there is a corresponding archive file for the Todos file,
1963 ;; insert it first and add identifiers for todos-jump-to-item.
1964 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
1965 (when (file-exists-p arch
)
1966 (insert-file-contents arch
)
1967 ;; Delete Todos archive file categories sexp.
1968 (delete-region (line-beginning-position)
1969 (1+ (line-end-position)))
1972 (when (re-search-forward
1973 (concat (if todos-filter-done-items
1974 (concat "\\(?:" todos-done-string-start
1975 "\\|" todos-date-string-start
1977 todos-date-string-start
)
1978 todos-date-pattern
"\\(?: "
1979 diary-time-regexp
"\\)?"
1980 (if todos-filter-done-items
1982 (regexp-quote todos-nondiary-end
)) "?")
1984 (insert "(archive) "))
1986 (insert-file-contents f
)
1987 ;; Delete Todos file categories sexp.
1988 (delete-region (line-beginning-position) (1+ (line-end-position)))
1990 ;; Unless the number of top priorities to show was
1991 ;; passed by the caller, the file-wide value from
1992 ;; `todos-priorities-rules', if non-nil, overrides
1993 ;; `todos-show-priorities'.
1994 (unless (consp filter
)
1995 (setq fnum
(or (nth 1 (assoc f todos-priorities-rules
))
1996 todos-show-priorities
)))
1997 (while (re-search-forward
1998 (concat "^" (regexp-quote todos-category-beg
) "\\(.+\\)\n")
2000 (setq cat
(match-string 1))
2002 ;; Unless the number of top priorities to show was
2003 ;; passed by the caller, the category-wide value
2004 ;; from `todos-priorities-rules', if non-nil,
2005 ;; overrides a non-nil file-wide value from
2006 ;; `todos-priorities-rules' as well as
2007 ;; `todos-show-priorities'.
2008 (unless (consp filter
)
2009 (let ((cats (nth 2 (assoc f todos-priorities-rules
))))
2010 (setq cnum
(or (cdr (assoc cat cats
)) fnum
))))
2011 (delete-region (match-beginning 0) (match-end 0))
2012 (setq beg
(point)) ; First item in the current category.
2013 (setq end
(if (re-search-forward
2014 (concat "^" (regexp-quote todos-category-beg
))
2020 (if (re-search-forward
2021 (concat "\n" (regexp-quote todos-category-done
))
2025 (unless (and todos-filter-done-items
(eq filter
'regexp
))
2026 ;; Leave done items.
2027 (delete-region done end
)
2029 (narrow-to-region beg end
) ; Process only current category.
2030 (goto-char (point-min))
2031 ;; Apply the filter.
2032 (cond ((eq filter
'diary
)
2034 (if (looking-at (regexp-quote todos-nondiary-start
))
2036 (todos-forward-item))))
2037 ((eq filter
'regexp
)
2039 (if (looking-at todos-item-start
)
2040 (if (string-match regexp
(todos-item-string))
2041 (todos-forward-item)
2042 (todos-remove-item))
2043 ;; Kill lines that aren't part of a todo or done
2044 ;; item (empty or todos-category-done).
2045 (delete-region (line-beginning-position)
2046 (1+ (line-end-position))))
2047 ;; If last todo item in file matches regexp and
2048 ;; there are no following done items,
2049 ;; todos-category-done string is left dangling,
2050 ;; because todos-forward-item jumps over it.
2053 (concat (regexp-quote todos-done-string
)
2055 (delete-region (point) (progn
2058 (t ; Filter top priority items.
2059 (setq num
(or cnum fnum num
))
2061 (todos-forward-item num
))))
2063 ;; Delete non-top-priority items.
2064 (unless (member filter
'(diary regexp
))
2065 (delete-region beg end
))
2066 (goto-char (point-min))
2067 ;; Add file (if using multiple files) and category tags to
2070 (when (re-search-forward
2071 (concat (if todos-filter-done-items
2072 (concat "\\(?:" todos-done-string-start
2073 "\\|" todos-date-string-start
2075 todos-date-string-start
)
2076 todos-date-pattern
"\\(?: " diary-time-regexp
2077 "\\)?" (if todos-filter-done-items
2079 (regexp-quote todos-nondiary-end
))
2083 (when (looking-at "(archive) ") (goto-char (match-end 0)))
2084 (insert (if multifile
(concat fname
":") "") cat
"]"))
2087 (setq bufstr
(buffer-string))
2088 (with-current-buffer buf
2089 (let (buffer-read-only)
2090 (insert bufstr
)))))))
2091 (set-window-buffer (selected-window) (set-buffer buf
))
2092 (todos-prefix-overlays)
2093 (goto-char (point-min)))))
2095 (defun todos-set-top-priorities (&optional arg
)
2096 "Set number of top priorities shown by `todos-top-priorities'.
2097 With non-nil ARG, set the number only for the current Todos
2098 category; otherwise, set the number for all categories in the
2101 Calling this function via either of the commands
2102 `todos-set-top-priorities-in-file' or
2103 `todos-set-top-priorities-in-category' is the recommended way to
2104 set the user customizable option `todos-priorities-rules'."
2105 (let* ((cat (todos-current-category))
2106 (file todos-current-todos-file
)
2107 (rules todos-priorities-rules
)
2108 (frule (assoc-string file rules
))
2109 (crule (assoc-string cat
(nth 2 frule
)))
2110 (crules (nth 2 frule
))
2111 (cur (or (if arg
(cdr crule
) (nth 1 frule
))
2112 todos-show-priorities
))
2113 (prompt (if arg
(concat "Number of top priorities in this category"
2114 " (currently %d): ")
2115 (concat "Default number of top priorities per category"
2116 " in this file (currently %d): ")))
2121 (setq new
(read-number (format prompt cur0
))
2122 prompt
"Enter a non-negative number: "
2125 (append (delete crule crules
) (list (cons cat new
)))
2126 (append (list file new
) (list crules
))))
2127 (setq rules
(cons (if arg
2128 (list file cur nrule
)
2130 (delete frule rules
)))
2131 (customize-save-variable 'todos-priorities-rules rules
)
2132 (todos-prefix-overlays)))
2134 (defun todos-filtered-buffer-name (buffer-type file-list
)
2135 "Rename Todos filtered buffer using BUFFER-TYPE and FILE-LIST.
2137 The new name is constructed from the string BUFFER-TYPE, which
2138 refers to one of the top priorities, diary or regexp item
2139 filters, and the names of the filtered files in FILE-LIST. Used
2140 in Todos Filter Items mode."
2141 (let* ((multi (> (length file-list
) 1))
2142 (fnames (mapconcat (lambda (f) (todos-short-file-name f
))
2144 (rename-buffer (format (concat "%s for file" (if multi
"s" "")
2145 " \"%s\"") buffer-type fnames
))))
2147 (defun todos-find-item (str)
2148 "Search for saved top priority item STR in its Todos file.
2149 Return the list (FOUND FILE CAT), where CAT and FILE are the
2150 item's category and file, and FOUND is a cons cell if the search
2151 succeeds, whose car is the start of the item in FILE and whose
2152 cdr is `done' if the item is now a done item, `changed' if its
2153 priority has changed or its text was truncated or augmented, and
2155 (string-match (concat (if todos-filter-done-items
2156 (concat "\\(?:" todos-done-string-start
"\\|"
2157 todos-date-string-start
"\\)")
2158 todos-date-string-start
)
2159 todos-date-pattern
"\\(?: " diary-time-regexp
"\\)?"
2160 (if todos-filter-done-items
2162 (regexp-quote todos-nondiary-end
)) "?"
2163 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
2164 "\\(?1:.*\\)\\]\\).*$") str
)
2165 (let ((cat (match-string 1 str
))
2166 (file (match-string 2 str
))
2167 (archive (string= (match-string 3 str
) "(archive) "))
2168 (filcat (match-string 4 str
))
2171 (setq str
(replace-match "" nil nil str
4))
2173 (while (search-backward filcat nil t
)
2174 (setq tpriority
(1+ tpriority
))))
2176 (concat todos-files-directory
(substring file
0 -
1)
2177 (if archive
".toda" ".todo"))
2179 (concat (file-name-sans-extension
2180 todos-global-current-todos-file
) ".toda")
2181 todos-global-current-todos-file
)))
2182 (find-file-noselect file
)
2183 (with-current-buffer (find-buffer-visiting file
)
2185 (goto-char (point-min))
2186 (let ((beg (re-search-forward
2187 (concat "^" (regexp-quote (concat todos-category-beg cat
)) "$")
2189 (done (save-excursion
2191 (concat "^" (regexp-quote todos-category-done
)) nil t
)))
2192 (end (save-excursion
2193 (or (re-search-forward
2194 (concat "^" (regexp-quote todos-category-beg
))
2197 (setq found
(when (search-forward str end t
)
2198 (goto-char (match-beginning 0))))
2201 (cons found
(if (> (point) done
)
2203 (let ((cpriority 1))
2205 ;; Not top item in category.
2206 (while (> (point) (1+ beg
))
2207 (let ((opoint (point)))
2208 (todos-backward-item)
2209 ;; Can't move backward beyond
2210 ;; first item in file.
2211 (unless (= (point) opoint
)
2212 (setq cpriority
(1+ cpriority
))))))
2213 (if (and (= tpriority cpriority
)
2214 ;; Proper substring is not the same.
2215 (string= (todos-item-string)
2219 (list found file cat
)))
2221 (defun todos-check-top-priorities ()
2222 "Return a message saying whether top priorities file is up to date."
2226 (let* ((item (todos-item-string))
2227 (found (car (todos-find-item item
))))
2228 (unless (eq (cdr found
) 'same
)
2230 (overlay-put (make-overlay (todos-item-start) (todos-item-end))
2231 'face
'todos-search
))
2232 (setq count
(1+ count
))))
2233 ;; (throw 'old (message "The marked item is not up to date.")))
2234 (todos-forward-item))
2236 (message "Top priorities file is up to date.")
2237 (message (concat "The highlighted item" (if (= count
1) " is " "s are ")
2239 ;; "\nType <return> on item for details."
2242 (defun todos-top-priorities-filename ()
2244 (let ((bufname (buffer-name)))
2245 (string-match "\"\\([^\"]+\\)\"" bufname
)
2246 (let* ((filename-str (substring bufname
(match-beginning 1) (match-end 1)))
2247 (filename-base (replace-regexp-in-string ", " "-" filename-str
)))
2248 (concat todos-files-directory filename-base
".todt"))))
2250 (defun todos-save-top-priorities-buffer ()
2252 (let ((filename (todos-top-priorities-filename)))
2253 (if (file-exists-p filename
)
2255 (write-region nil nil filename nil t nil t
))))
2257 ;; ---------------------------------------------------------------------------
2258 ;;; Sorting and display routines for Todos Categories mode.
2260 (defun todos-longest-category-name-length (categories)
2261 "Return the length of the longest name in list CATEGORIES."
2263 (dolist (c categories longest
)
2264 (setq longest
(max longest
(length c
))))))
2266 (defun todos-adjusted-category-label-length ()
2267 "Return adjusted length of category label button.
2268 The adjustment ensures proper tabular alignment in Todos
2270 (let* ((categories (mapcar 'car todos-categories
))
2271 (longest (todos-longest-category-name-length categories
))
2272 (catlablen (length todos-categories-category-label
))
2273 (lc-diff (- longest catlablen
)))
2274 (if (and (natnump lc-diff
)
2275 (eq (logand lc-diff
1) 1)) ; oddp from cl.el
2277 (max longest catlablen
))))
2279 (defun todos-padded-string (str)
2280 "Return category name or label string STR padded with spaces.
2281 The placement of the padding is determined by the value of user
2282 option `todos-categories-align'."
2283 (let* ((len (todos-adjusted-category-label-length))
2284 (strlen (length str
))
2285 (strlen-odd (eq (logand strlen
1) 1))
2286 (padding (max 0 (/ (- len strlen
) 2)))
2287 (padding-left (cond ((eq todos-categories-align
'left
) 0)
2288 ((eq todos-categories-align
'center
) padding
)
2289 ((eq todos-categories-align
'right
)
2290 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
2291 (padding-right (cond ((eq todos-categories-align
'left
)
2292 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
2293 ((eq todos-categories-align
'center
)
2294 (if strlen-odd
(1+ padding
) padding
))
2295 ((eq todos-categories-align
'right
) 0))))
2296 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
2298 (defvar todos-descending-counts nil
2299 "List of keys for category counts sorted in descending order.")
2301 (defun todos-sort (list &optional key
)
2302 "Return a copy of LIST, possibly sorted according to KEY."
2303 (let* ((l (copy-sequence list
))
2304 (fn (if (eq key
'alpha
)
2305 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
2306 (lambda (x) (todos-get-count key x
))))
2307 ;; Keep track of whether the last sort by key was descending or
2309 (descending (member key todos-descending-counts
))
2310 (cmp (if (eq key
'alpha
)
2312 (if descending
'< '>)))
2313 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
2314 (t2 (funcall fn
(car s2
))))
2315 (funcall cmp t1 t2
)))))
2317 (setq l
(sort l pred
))
2318 ;; Switch between descending and ascending sort order.
2320 (setq todos-descending-counts
2321 (delete key todos-descending-counts
))
2322 (push key todos-descending-counts
)))
2325 (defun todos-display-sorted (type)
2326 "Keep point on the TYPE count sorting button just clicked."
2327 (let ((opoint (point)))
2328 (todos-update-categories-display type
)
2329 (goto-char opoint
)))
2331 (defun todos-label-to-key (label)
2332 "Return symbol for sort key associated with LABEL."
2334 (cond ((string= label todos-categories-category-label
)
2336 ((string= label todos-categories-todo-label
)
2338 ((string= label todos-categories-diary-label
)
2340 ((string= label todos-categories-done-label
)
2342 ((string= label todos-categories-archived-label
)
2343 (setq key
'archived
)))
2346 (defun todos-insert-sort-button (label)
2347 "Insert button for displaying categories sorted by item counts.
2348 LABEL determines which type of count is sorted."
2349 (setq str
(if (string= label todos-categories-category-label
)
2350 (todos-padded-string label
)
2353 (setq end
(+ beg
(length str
)))
2354 (insert-button str
'face nil
2357 (let ((key (todos-label-to-key ,label
)))
2358 (if (and (member key todos-descending-counts
)
2361 ;; If display is alphabetical, switch back to
2362 ;; category priority order.
2363 (todos-display-sorted nil
)
2364 (setq todos-descending-counts
2365 (delete key todos-descending-counts
)))
2366 (todos-display-sorted key
)))))
2367 (setq ovl
(make-overlay beg end
))
2368 (overlay-put ovl
'face
'todos-button
))
2370 (defun todos-total-item-counts ()
2371 "Return a list of total item counts for the current file."
2372 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i
))
2373 (mapcar 'cdr todos-categories
))))
2376 (defvar todos-categories-category-number
0
2377 "Variable for numbering categories in Todos Categories mode.")
2379 (defun todos-insert-category-line (cat &optional nonum
)
2380 "Insert button with category CAT's name and item counts.
2381 With non-nil argument NONUM show only these; otherwise, insert a
2382 number in front of the button indicating the category's priority.
2383 The number and the category name are separated by the string
2384 which is the value of the user option
2385 `todos-categories-number-separator'."
2386 (let ((archive (member todos-current-todos-file todos-archives
))
2387 (num todos-categories-category-number
)
2388 (str (todos-padded-string cat
))
2390 (setq num
(1+ num
) todos-categories-category-number num
)
2393 (make-string (+ 4 (length todos-categories-number-separator
))
2395 (format " %3d%s" num todos-categories-number-separator
))
2397 (mapconcat (lambda (elt)
2399 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
2400 (format "%3d" (todos-get-count (cdr elt
) cat
)) ; count
2401 ;; Add an extra space if label length is odd
2402 ;; (using def of oddp from cl.el).
2403 (if (eq (logand (length (car elt
)) 1) 1) " ")))
2405 (list (cons todos-categories-done-label
'done
))
2406 (list (cons todos-categories-todo-label
'todo
)
2407 (cons todos-categories-diary-label
'diary
)
2408 (cons todos-categories-done-label
'done
)
2409 (cons todos-categories-archived-label
2412 " ") ; So highlighting of last column is consistent with the others.
2413 'face
(if (and todos-skip-archived-categories
2414 (zerop (todos-get-count 'todo cat
))
2415 (zerop (todos-get-count 'done cat
))
2416 (not (zerop (todos-get-count 'archived cat
))))
2417 'todos-archived-only
2419 'action
`(lambda (button) (let ((buf (current-buffer)))
2420 (todos-jump-to-category nil
,cat
)
2421 (kill-buffer buf
))))
2422 ;; Highlight the sorted count column.
2423 (let* ((beg (+ opoint
7 (length str
)))
2425 (cond ((eq nonum
'todo
)
2426 (setq beg
(+ beg
1 (/ (length todos-categories-todo-label
) 2))))
2428 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
2429 2 (/ (length todos-categories-diary-label
) 2))))
2431 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
2432 2 (length todos-categories-diary-label
)
2433 2 (/ (length todos-categories-done-label
) 2))))
2434 ((eq nonum
'archived
)
2435 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
2436 2 (length todos-categories-diary-label
)
2437 2 (length todos-categories-done-label
)
2438 2 (/ (length todos-categories-archived-label
) 2)))))
2439 (unless (= beg
(+ opoint
7 (length str
))) ; Don't highlight categories.
2440 (setq end
(+ beg
4))
2441 (setq ovl
(make-overlay beg end
))
2442 (overlay-put ovl
'face
'todos-sorted-column
)))
2445 (defun todos-display-categories-1 ()
2446 "Prepare buffer for displaying table of categories and item counts."
2447 (unless (eq major-mode
'todos-categories-mode
)
2448 (setq todos-global-current-todos-file
2449 (or todos-current-todos-file
2450 (todos-absolute-file-name todos-default-todos-file
)))
2451 (set-window-buffer (selected-window)
2452 (set-buffer (get-buffer-create todos-categories-buffer
)))
2453 (kill-all-local-variables)
2454 (todos-categories-mode)
2455 (let ((archive (member todos-current-todos-file todos-archives
))
2458 (insert (format (concat "Category counts for Todos "
2459 (if archive
"archive" "file")
2461 (todos-short-file-name todos-current-todos-file
)))
2463 ;; Make space for the column of category numbers.
2464 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32))
2465 ;; Add the category and item count buttons (if this is the list of
2466 ;; categories in an archive, show only done item counts).
2467 (todos-insert-sort-button todos-categories-category-label
)
2470 (insert (make-string 3 32))
2471 (todos-insert-sort-button todos-categories-done-label
))
2472 (insert (make-string 3 32))
2473 (todos-insert-sort-button todos-categories-todo-label
)
2474 (insert (make-string 2 32))
2475 (todos-insert-sort-button todos-categories-diary-label
)
2476 (insert (make-string 2 32))
2477 (todos-insert-sort-button todos-categories-done-label
)
2478 (insert (make-string 2 32))
2479 (todos-insert-sort-button todos-categories-archived-label
))
2482 (defun todos-update-categories-display (sortkey)
2484 (let* ((cats0 todos-categories
)
2485 (cats (todos-sort cats0 sortkey
))
2486 (archive (member todos-current-todos-file todos-archives
))
2487 (todos-categories-category-number 0)
2488 ;; Find start of Category button if we just entered Todos Categories
2490 (pt (if (eq (point) (point-max))
2493 (goto-char (next-single-char-property-change
2494 (point) 'face nil
(line-end-position))))))
2497 (delete-region (point) (point-max))
2498 ;; Fill in the table with buttonized lines, each showing a category and
2500 (mapc (lambda (cat) (todos-insert-category-line cat sortkey
))
2503 ;; Add a line showing item count totals.
2504 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32)
2505 (todos-padded-string todos-categories-totals-label
)
2509 (make-string (1+ (/ (length (car elt
)) 2)) 32)
2510 (format "%3d" (nth (cdr elt
) (todos-total-item-counts)))
2511 ;; Add an extra space if label length is odd (using
2512 ;; definition of oddp from cl.el).
2513 (if (eq (logand (length (car elt
)) 1) 1) " ")))
2515 (list (cons todos-categories-done-label
2))
2516 (list (cons todos-categories-todo-label
0)
2517 (cons todos-categories-diary-label
1)
2518 (cons todos-categories-done-label
2)
2519 (cons todos-categories-archived-label
3)))
2521 ;; Put cursor on Category button initially.
2522 (if pt
(goto-char pt
))
2523 (setq buffer-read-only t
)))
2525 ;; ---------------------------------------------------------------------------
2526 ;;; Routines for generating Todos insertion commands and key bindings
2528 ;; Can either of these be included in Emacs? The originals are GFDL'd.
2530 ;; Slightly reformulated from
2531 ;; http://rosettacode.org/wiki/Power_set#Common_Lisp.
2532 (defun powerset-recursive (l)
2536 (let ((prev (powerset-recursive (cdr l
))))
2537 (append (mapcar (lambda (elt) (cons (car l
) elt
))
2541 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C
2542 (defun powerset-bitwise (l)
2543 (let ((binnum (lsh 1 (length l
)))
2548 (while (not (zerop bits
))
2549 (let ((arg (pop ll
)))
2550 (unless (zerop (logand bits
1))
2551 (setq elt
(append elt
(list arg
))))
2552 (setq bits
(lsh bits -
1))))
2553 (setq pset
(append pset
(list elt
)))
2557 ;; (defalias 'todos-powerset 'powerset-recursive)
2558 (defalias 'todos-powerset
'powerset-bitwise
)
2560 ;; Return list of lists of non-nil atoms produced from ARGLIST. The elements
2561 ;; of ARGLIST may be atoms or lists.
2562 (defun todos-gen-arglists (arglist)
2565 (let ((arg (pop arglist
)))
2566 (cond ((symbolp arg
)
2567 (setq arglists
(if arglists
2568 (mapcar (lambda (l) (push arg l
)) arglists
)
2569 (list (push arg arglists
)))))
2573 (if (= 1 (length arglists
))
2574 (apply (lambda (l) (push a l
)) arglists
)
2575 (mapcar (lambda (l) (push a l
)) arglists
)))
2577 (setq arglists
(mapcar 'reverse
(apply 'append
(mapc 'car arglists
))))))
2579 (defvar todos-insertion-commands-args-genlist
2580 '(diary nonmarking
(calendar date dayname
) time
(here region
))
2581 "Generator list for argument lists of Todos insertion commands.")
2583 (defvar todos-insertion-commands-args
2584 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist
))
2586 (setq res
(remove-duplicates
2587 (apply 'append
(mapcar 'todos-powerset argslist
)) :test
'equal
))
2589 (unless (= 5 (length l
))
2590 (let ((v (make-vector 5 nil
)) elt
)
2593 (cond ((eq elt
'diary
)
2595 ((eq elt
'nonmarking
)
2597 ((or (eq elt
'calendar
)
2606 (setq l
(append v nil
))))
2607 (setq new
(append new
(list l
))))
2609 "List of all argument lists for Todos insertion commands.")
2611 (defun todos-insertion-command-name (arglist)
2612 "Generate Todos insertion command name from ARGLIST."
2613 (replace-regexp-in-string
2615 (replace-regexp-in-string
2617 (concat "todos-item-insert-"
2618 (mapconcat (lambda (e) (if e
(symbol-name e
))) arglist
"-")))))
2620 (defvar todos-insertion-commands-names
2622 (todos-insertion-command-name l
))
2623 todos-insertion-commands-args
)
2624 "List of names of Todos insertion commands.")
2626 (defmacro todos-define-insertion-command
(&rest args
)
2627 (let ((name (intern (todos-insertion-command-name args
)))
2632 (arg4 (nth 4 args
)))
2633 `(defun ,name
(&optional arg
&rest args
)
2634 "Todos item insertion command generated from ARGS."
2635 (interactive (list current-prefix-arg
))
2636 (todos-insert-item arg
',arg0
',arg1
',arg2
',arg3
',arg4
))))
2638 (defvar todos-insertion-commands
2640 (eval `(todos-define-insertion-command ,@c
)))
2641 todos-insertion-commands-args
)
2642 "List of Todos insertion commands.")
2644 (defvar todos-insertion-commands-arg-key-list
2645 '(("diary" "y" "yy")
2646 ("nonmarking" "k" "kk")
2647 ("calendar" "c" "cc")
2649 ("dayname" "n" "nn")
2655 (defun todos-insertion-key-bindings (map)
2657 (dolist (c todos-insertion-commands
)
2659 (cname (symbol-name c
)))
2661 (let ((arg (nth 0 l
))
2664 (if (string-match (concat (regexp-quote arg
) "\\_>") cname
)
2665 (setq key
(concat key key2
)))
2666 (if (string-match (concat (regexp-quote arg
) ".+") cname
)
2667 (setq key
(concat key key1
)))))
2668 todos-insertion-commands-arg-key-list
)
2669 (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname
)
2670 (setq key
(concat key
"i")))
2671 (define-key map key c
))))
2673 (defvar todos-insertion-map
2674 (let ((map (make-keymap)))
2675 (todos-insertion-key-bindings map
)
2676 (define-key map
"p" 'todos-copy-item
)
2678 "Keymap for Todos mode insertion commands.")
2680 ;; ---------------------------------------------------------------------------
2681 ;;; Key maps and menus
2683 (defvar todos-key-bindings
2686 ("Cd" . todos-display-categories
) ;FIXME: Fc todos-file-categories?
2687 ("H" . todos-highlight-item
)
2688 ("N" . todos-hide-show-item-numbering
)
2689 ("D" . todos-hide-show-date-time
)
2690 ("*" . todos-mark-unmark-item
)
2691 ("C*" . todos-mark-category
)
2692 ("Cu" . todos-unmark-category
)
2693 ("PP" . todos-print
)
2694 ("PF" . todos-print-to-file
)
2695 ("v" . todos-hide-show-done-items
)
2696 ("V" . todos-show-done-only
)
2697 ("As" . todos-show-archive
)
2698 ("Ac" . todos-choose-archive
)
2699 ;; ("Y" . todos-diary-items)
2700 ("Fe" . todos-edit-multiline
)
2701 ("Fh" . todos-highlight-item
)
2702 ("Fn" . todos-hide-show-item-numbering
)
2703 ("Fd" . todos-hide-show-date-time
)
2704 ("Ftt" . todos-top-priorities
)
2705 ("Ftm" . todos-top-priorities-multifile
)
2706 ("Fts" . todos-set-top-priorities-in-file
)
2707 ("Cts" . todos-set-top-priorities-in-category
)
2708 ("Fyy" . todos-diary-items
)
2709 ("Fym" . todos-diary-items-multifile
)
2710 ("Fxx" . todos-regexp-items
)
2711 ("Fxm" . todos-regexp-items-multifile
)
2713 ("f" . todos-forward-category
)
2714 ("b" . todos-backward-category
)
2715 ("j" . todos-jump-to-category
)
2716 ("n" . todos-forward-item
)
2717 ("p" . todos-backward-item
)
2718 ("S" . todos-search
)
2719 ("X" . todos-clear-matches
)
2721 ("Fa" . todos-add-file
)
2722 ("Ca" . todos-add-category
)
2723 ("Cr" . todos-rename-category
)
2724 ("Cg" . todos-merge-category
)
2725 ("Cm" . todos-move-category
)
2726 ("Ck" . todos-delete-category
)
2727 ("d" . todos-item-done
)
2728 ("ee" . todos-edit-item
)
2729 ("em" . todos-edit-multiline-item
)
2730 ("eh" . todos-edit-item-header
)
2731 ("edc" . todos-edit-item-date-from-calendar
)
2732 ("edt" . todos-edit-item-date-to-today
)
2733 ("edn" . todos-edit-item-date-day-name
)
2734 ("edy" . todos-edit-item-date-year
)
2735 ("edm" . todos-edit-item-date-month
)
2736 ("edd" . todos-edit-item-date-day
)
2737 ("et" . todos-edit-item-time
)
2738 ("eyy" . todos-edit-item-diary-inclusion
)
2739 ;; ("" . todos-edit-category-diary-inclusion)
2740 ("eyn" . todos-edit-item-diary-nonmarking
)
2741 ;;("" . todos-edit-category-diary-nonmarking)
2742 ("ec" . todos-done-item-add-edit-or-delete-comment
)
2743 ("i" .
,todos-insertion-map
)
2744 ("k" . todos-delete-item
) ;FIXME: not single letter?
2745 ("m" . todos-move-item
)
2746 ("r" . todos-raise-item-priority
)
2747 ("l" . todos-lower-item-priority
)
2748 ("#" . todos-set-item-priority
)
2749 ("u" . todos-item-undo
)
2750 ("Ad" . todos-archive-done-item
) ;FIXME: ad
2751 ("AD" . todos-archive-category-done-items
) ;FIXME: aD or C-u ad ?
2754 ([remap newline
] . newline-and-indent
)
2756 "Alist pairing keys defined in Todos modes and their bindings.")
2758 (defvar todos-mode-map
2759 (let ((map (make-keymap)))
2760 ;; Don't suppress digit keys, so they can supply prefix arguments.
2761 (suppress-keymap map
)
2762 (dolist (ck todos-key-bindings
)
2763 (define-key map
(car ck
) (cdr ck
)))
2765 "Todos mode keymap.")
2768 todos-menu todos-mode-map
"Todos Menu"
2771 ["Next Item" todos-forward-item t
]
2772 ["Previous Item" todos-backward-item t
]
2774 ["Next Category" todos-forward-category t
]
2775 ["Previous Category" todos-backward-category t
]
2776 ["Jump to Category" todos-jump-to-category t
]
2778 ["Search Todos File" todos-search t
]
2779 ["Clear Highlighting on Search Matches" todos-category-done t
])
2781 ["List Current Categories" todos-display-categories t
]
2782 ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
2783 ["Turn Item Highlighting on/off" todos-highlight-item t
]
2784 ["Turn Item Numbering on/off" todos-hide-show-item-numbering t
]
2785 ["Turn Item Time Stamp on/off" todos-hide-show-date-time t
]
2786 ["View/Hide Done Items" todos-hide-show-done-items t
]
2788 ["View Diary Items" todos-diary-items t
]
2789 ["View Top Priority Items" todos-top-priorities t
]
2790 ["View Multifile Top Priority Items" todos-top-priorities-multifile t
]
2792 ["Print Category" todos-print t
])
2794 ["Insert New Item" todos-insert-item t
]
2795 ["Insert Item Here" todos-insert-item-here t
]
2796 ("More Insertion Commands")
2797 ["Edit Item" todos-edit-item t
]
2798 ["Edit Multiline Item" todos-edit-multiline t
]
2799 ["Edit Item Header" todos-edit-item-header t
]
2800 ["Edit Item Date" todos-edit-item-date t
]
2801 ["Edit Item Time" todos-edit-item-time t
]
2803 ["Lower Item Priority" todos-lower-item-priority t
]
2804 ["Raise Item Priority" todos-raise-item-priority t
]
2805 ["Set Item Priority" todos-set-item-priority t
]
2806 ["Move (Recategorize) Item" todos-move-item t
]
2807 ["Delete Item" todos-delete-item t
]
2808 ["Undo Done Item" todos-item-undo t
]
2809 ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t
]
2810 ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t
]
2811 ["Mark & Hide Done Item" todos-item-done t
]
2812 ["Archive Done Items" todos-archive-category-done-items t
]
2814 ["Add New Todos File" todos-add-file t
]
2815 ["Add New Category" todos-add-category t
]
2816 ["Delete Current Category" todos-delete-category t
]
2817 ["Rename Current Category" todos-rename-category t
]
2819 ["Save Todos File" todos-save t
]
2822 ["Quit" todos-quit t
]
2825 (defvar todos-archive-mode-map
2826 (let ((map (make-sparse-keymap)))
2827 (suppress-keymap map t
)
2828 ;; navigation commands
2829 (define-key map
"f" 'todos-forward-category
)
2830 (define-key map
"b" 'todos-backward-category
)
2831 (define-key map
"j" 'todos-jump-to-category
)
2832 (define-key map
"n" 'todos-forward-item
)
2833 (define-key map
"p" 'todos-backward-item
)
2835 (define-key map
"C" 'todos-display-categories
)
2836 (define-key map
"H" 'todos-highlight-item
)
2837 (define-key map
"N" 'todos-hide-show-item-numbering
)
2838 ;; (define-key map "" 'todos-hide-show-date-time)
2839 (define-key map
"P" 'todos-print
)
2840 (define-key map
"q" 'todos-quit
)
2841 (define-key map
"s" 'todos-save
)
2842 (define-key map
"S" 'todos-search
)
2843 (define-key map
"t" 'todos-show
)
2844 (define-key map
"u" 'todos-unarchive-items
)
2845 (define-key map
"U" 'todos-unarchive-category
)
2847 "Todos Archive mode keymap.")
2849 (defvar todos-edit-mode-map
2850 (let ((map (make-sparse-keymap)))
2851 (define-key map
"\C-x\C-q" 'todos-edit-quit
)
2852 (define-key map
[remap newline
] 'newline-and-indent
)
2854 "Todos Edit mode keymap.")
2856 (defvar todos-categories-mode-map
2857 (let ((map (make-sparse-keymap)))
2858 (suppress-keymap map t
)
2859 (define-key map
"c" 'todos-display-categories-alphabetically-or-by-priority
)
2860 (define-key map
"t" 'todos-display-categories-sorted-by-todo
)
2861 (define-key map
"y" 'todos-display-categories-sorted-by-diary
)
2862 (define-key map
"d" 'todos-display-categories-sorted-by-done
)
2863 (define-key map
"a" 'todos-display-categories-sorted-by-archived
)
2864 (define-key map
"l" 'todos-lower-category-priority
)
2865 (define-key map
"+" 'todos-lower-category-priority
)
2866 (define-key map
"r" 'todos-raise-category-priority
)
2867 (define-key map
"-" 'todos-raise-category-priority
)
2868 (define-key map
"n" 'todos-forward-button
)
2869 (define-key map
"p" 'todos-backward-button
)
2870 (define-key map
[tab] 'todos-forward-button)
2871 (define-key map [backtab] 'todos-backward-button)
2872 (define-key map "q" 'todos-quit)
2873 ;; (define-key map "A" 'todos-add-category)
2874 ;; (define-key map "D" 'todos-delete-category)
2875 ;; (define-key map "R" 'todos-rename-category)
2877 "Todos Categories mode keymap.")
2879 (defvar todos-filtered-items-mode-map
2880 (let ((map (make-keymap)))
2881 (suppress-keymap map t)
2882 ;; navigation commands
2883 (define-key map "j" 'todos-jump-to-item)
2884 (define-key map [remap newline] 'todos-jump-to-item)
2885 (define-key map "n" 'todos-forward-item)
2886 (define-key map "p" 'todos-backward-item)
2887 (define-key map "H" 'todos-highlight-item)
2888 (define-key map "N" 'todos-hide-show-item-numbering)
2889 (define-key map "D" 'todos-hide-show-date-time)
2890 (define-key map "P" 'todos-print)
2891 (define-key map "q" 'todos-quit)
2892 (define-key map "s" 'todos-save)
2894 (define-key map "l" 'todos-lower-item-priority)
2895 (define-key map "r" 'todos-raise-item-priority)
2896 (define-key map "#" 'todos-set-item-priority)
2898 "Todos Top Priorities mode keymap.")
2900 ;; ---------------------------------------------------------------------------
2901 ;;; Mode definitions
2903 (defun todos-modes-set-1 ()
2905 (set (make-local-variable 'font-lock-defaults) '(todos-font-lock-keywords t))
2906 (set (make-local-variable 'indent-line-function) 'todos-indent)
2907 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2909 (defun todos-modes-set-2 ()
2911 (add-to-invisibility-spec 'todos)
2912 (setq buffer-read-only t)
2913 (set (make-local-variable 'hl-line-range-function)
2914 (lambda() (when (todos-item-end)
2915 (cons (todos-item-start) (todos-item-end))))))
2917 (defun todos-modes-set-3 ()
2919 (set (make-local-variable 'todos-categories) (todos-set-categories))
2920 (set (make-local-variable 'todos-category-number) 1)
2921 (add-hook 'find-file-hook 'todos-display-as-todos-file nil t))
2923 (put 'todos-mode 'mode-class 'special)
2925 (define-derived-mode todos-mode special-mode "Todos"
2926 "Major mode for displaying, navigating and editing Todo lists.
2929 (easy-menu-add todos-menu)
2933 ;; Initialize todos-current-todos-file.
2934 (when (member (file-truename (buffer-file-name))
2935 (funcall todos-files-function))
2936 (set (make-local-variable 'todos-current-todos-file)
2937 (file-truename (buffer-file-name))))
2938 (set (make-local-variable 'todos-show-done-only) nil)
2939 (set (make-local-variable 'todos-categories-with-marks) nil)
2940 (add-hook 'find-file-hook 'todos-add-to-buffer-list nil t)
2941 (add-hook 'post-command-hook 'todos-update-buffer-list nil t)
2942 (when todos-show-current-file
2943 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
2944 (add-hook 'window-configuration-change-hook
2945 'todos-reset-and-enable-done-separator nil t)
2946 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
2948 (put 'todos-archive-mode 'mode-class 'special)
2950 ;; If todos-mode is parent, all todos-mode key bindings appear to be
2951 ;; available in todos-archive-mode (e.g. shown by C-h m).
2952 (define-derived-mode todos-archive-mode special-mode "Todos-Arch"
2953 "Major mode for archived Todos categories.
2955 \\{todos-archive-mode-map}"
2959 (set (make-local-variable 'todos-current-todos-file)
2960 (file-truename (buffer-file-name)))
2961 (set (make-local-variable 'todos-show-done-only) t))
2963 (defun todos-mode-external-set ()
2965 (set (make-local-variable 'todos-current-todos-file)
2966 todos-global-current-todos-file)
2967 (let ((cats (with-current-buffer
2968 ;; Can't use find-buffer-visiting when
2969 ;; `todos-display-categories' is called on first
2970 ;; invocation of `todos-show', since there is then
2971 ;; no buffer visiting the current file.
2972 (find-file-noselect todos-current-todos-file 'nowarn)
2974 (set (make-local-variable 'todos-categories) cats)))
2976 (define-derived-mode todos-edit-mode text-mode "Todos-Ed"
2977 "Major mode for editing multiline Todo items.
2979 \\{todos-edit-mode-map}"
2981 (todos-mode-external-set))
2983 (put 'todos-categories-mode 'mode-class 'special)
2985 (define-derived-mode todos-categories-mode special-mode "Todos-Cats"
2986 "Major mode for displaying and editing Todos categories.
2988 \\{todos-categories-mode-map}"
2989 (todos-mode-external-set))
2991 (put 'todos-filtered-items-mode 'mode-class 'special)
2993 (define-derived-mode todos-filtered-items-mode special-mode "Todos-Fltr"
2994 "Mode for displaying and reprioritizing top priority Todos.
2996 \\{todos-filtered-items-mode-map}"
2998 (todos-modes-set-2))
3000 ;; ---------------------------------------------------------------------------
3003 ;; ---------------------------------------------------------------------------
3004 ;;; Entering and Exiting
3007 (defun todos-show (&optional solicit-file)
3008 "Visit a Todos file and display one of its categories.
3009 With non-nil prefix argument SOLICIT-FILE prompt for which todo
3010 file to visit; otherwise visit `todos-default-todos-file'.
3011 Subsequent invocations from outside of Todos mode revisit this
3012 file or, with option `todos-show-current-file' non-nil (the
3013 default), whichever Todos file was last visited.
3015 Calling this command before any Todos file exists prompts for a
3016 file name and an initial category (defaulting to
3017 `todos-initial-file' and `todos-initial-category'), creates both
3018 of these, visits the file and displays the category.
3020 The first invocation of this command on an existing Todos file
3021 interacts with the option `todos-show-first': if `table', show
3022 the table of categories in the file; if `top', show the
3023 corresponding top priorities file, if any; if `first' (the
3024 default value), show the first category in the file. Subsequent
3025 invocations always show the file's current (i.e., last displayed)
3028 In Todos mode just the category's unfinished todo items are shown
3029 by default. The done items are hidden, but typing
3030 `\\[todos-hide-show-done-items]' displays them below the todo
3031 items. With non-nil user option `todos-show-with-done' both todo
3032 and done items are always shown on visiting a category.
3034 Invoking this command in Todos Archive mode visits the
3035 corresponding Todos file, displaying the corresponding category."
3038 (show-first todos-show-first)
3039 (file (cond (solicit-file
3040 (if (funcall todos-files-function)
3041 (todos-read-file-name "Choose a Todos file to visit: "
3043 (error "There are no Todos files")))
3044 ((and (eq major-mode 'todos-archive-mode)
3045 ;; Called noninteractively via todos-quit from
3046 ;; Todos Categories mode to return to archive file.
3047 (called-interactively-p 'any))
3048 (setq cat (todos-current-category))
3049 (concat (file-name-sans-extension todos-current-todos-file)
3052 (or todos-current-todos-file
3053 (and todos-show-current-file
3054 todos-global-current-todos-file)
3055 (todos-absolute-file-name todos-default-todos-file)
3056 (todos-add-file))))))
3057 (unless (member file todos-visited)
3058 ;; Can't setq t-c-t-f here, otherwise wrong file shown when
3059 ;; called again from todos-display-categories.
3060 (let ((todos-current-todos-file file))
3061 (cond ((eq todos-show-first 'table)
3062 (todos-display-categories))
3063 ((eq todos-show-first 'top)
3064 (let* ((shortf (todos-short-file-name file))
3065 (tp-file (todos-absolute-file-name shortf 'top)))
3066 (if (file-exists-p tp-file)
3069 (set-buffer (find-file-noselect tp-file 'nowarn)))
3070 (message "There is no top priorities file for %s" shortf)
3071 (setq todos-show-first 'first)))))))
3072 (when (or (member file todos-visited)
3073 (eq todos-show-first 'first))
3074 (set-window-buffer (selected-window)
3075 (set-buffer (find-file-noselect file 'nowarn)))
3076 ;; If called from archive file, show corresponding
3077 ;; category in Todos file, if it exists.
3078 (when (assoc cat todos-categories)
3079 (setq todos-category-number (todos-category-number cat)))
3080 ;; If this is a new Todos file, add its first category.
3081 (when (zerop (buffer-size))
3082 (setq todos-category-number
3083 (todos-add-category todos-current-todos-file "")))
3084 (save-excursion (todos-category-select)))
3085 (setq todos-show-first show-first)
3086 (add-to-list 'todos-visited file)))
3088 (defun todos-display-categories ()
3089 "Display a table of the current file's categories and item counts.
3091 In the initial display the categories are numbered, indicating
3092 their current order for navigating by \\[todos-forward-category]
3093 and \\[todos-backward-category]. You can persistantly change the
3094 order of the category at point by typing
3095 \\[todos-raise-category-priority] or
3096 \\[todos-lower-category-priority].
3098 The labels above the category names and item counts are buttons,
3099 and clicking these changes the display: sorted by category name
3100 or by the respective item counts (alternately descending or
3101 ascending). In these displays the categories are not numbered
3102 and \\[todos-raise-category-priority] and
3103 \\[todos-lower-category-priority] are
3104 disabled. (Programmatically, the sorting is triggered by passing
3105 a non-nil SORTKEY argument.)
3107 In addition, the lines with the category names and item counts
3108 are buttonized, and pressing one of these button jumps to the
3109 category in Todos mode (or Todos Archive mode, for categories
3110 containing only archived items, provided user option
3111 `todos-skip-archived-categories' is non-nil. These categories
3112 are shown in `todos-archived-only' face."
3114 (todos-display-categories-1)
3116 (todos-update-categories-display sortkey)))
3118 (defun todos-display-categories-alphabetically-or-by-priority ()
3122 (goto-char (point-min))
3124 (if (member 'alpha todos-descending-counts)
3126 (todos-update-categories-display nil)
3127 (setq todos-descending-counts
3128 (delete 'alpha todos-descending-counts)))
3129 (todos-update-categories-display 'alpha))))
3131 (defun todos-display-categories-sorted-by-todo ()
3135 (goto-char (point-min))
3137 (todos-update-categories-display 'todo)))
3139 (defun todos-display-categories-sorted-by-diary ()
3143 (goto-char (point-min))
3145 (todos-update-categories-display 'diary)))
3147 (defun todos-display-categories-sorted-by-done ()
3151 (goto-char (point-min))
3153 (todos-update-categories-display 'done)))
3155 (defun todos-display-categories-sorted-by-archived ()
3159 (goto-char (point-min))
3161 (todos-update-categories-display 'archived)))
3163 (defun todos-show-archive (&optional ask)
3164 "Visit the archive of the current Todos category, if it exists.
3165 If the category has no archived items, prompt to visit the
3166 archive anyway. If there is no archive for this file or with
3167 non-nil argument ASK, prompt to visit another archive.
3169 The buffer showing the archive is in Todos Archive mode. The
3170 first visit in a session displays the first category in the
3171 archive, subsequent visits return to the last category
3174 (let* ((cat (todos-current-category))
3175 (count (todos-get-count 'archived cat))
3176 (archive (concat (file-name-sans-extension todos-current-todos-file)
3179 (setq place (cond (ask 'other-archive)
3180 ((file-exists-p archive) 'this-archive)
3181 (t (when (y-or-n-p (concat "This file has no archive; "
3182 "visit another archive? "))
3184 (when (eq place 'other-archive)
3185 (setq archive (todos-read-file-name "Choose a Todos archive: " t t)))
3186 (when (and (eq place 'this-archive) (zerop count))
3187 (setq place (when (y-or-n-p
3188 (concat "This category has no archived items;"
3189 " visit archive anyway? "))
3192 (set-window-buffer (selected-window)
3193 (set-buffer (find-file-noselect archive)))
3194 (if (member place '(other-archive other-cat))
3195 (setq todos-category-number 1)
3196 (todos-category-number cat))
3197 (todos-category-select))))
3199 (defun todos-choose-archive ()
3200 "Choose an archive and visit it."
3202 (todos-show-archive t))
3204 (defun todos-save ()
3205 "Save the current Todos file."
3207 (cond ((eq major-mode 'todos-filtered-items-mode)
3208 (todos-check-top-priorities)
3209 (todos-save-top-priorities-buffer))
3213 (defun todos-quit ()
3214 "Exit the current Todos-related buffer.
3215 Depending on the specific mode, this either kills the buffer or
3216 buries it and restores state as needed."
3218 (cond ((eq major-mode 'todos-categories-mode)
3219 ;; Postpone killing buffer till after calling todos-show, to
3220 ;; prevent killing todos-mode buffer.
3221 (let ((buf (current-buffer)))
3222 (setq todos-descending-counts nil)
3223 ;; Ensure todos-show calls todos-display-categories only on
3224 ;; first invocation per file.
3225 (when (eq todos-show-first 'table)
3226 (add-to-list 'todos-visited todos-current-todos-file))
3229 ((eq major-mode 'todos-filtered-items-mode)
3232 ((member major-mode (list 'todos-mode 'todos-archive-mode))
3233 ;; Have to write previously nonexistant archives to file, and might
3234 ;; as well save Todos file also.
3238 (defun todos-print (&optional to-file)
3239 "Produce a printable version of the current Todos buffer.
3240 This converts overlays and soft line wrapping and, depending on
3241 the value of `todos-print-function', includes faces. With
3242 non-nil argument TO-FILE write the printable version to a file;
3243 otherwise, send it to the default printer."
3245 (let ((buf todos-print-buffer)
3247 ((eq major-mode 'todos-mode)
3248 (concat "Todos File: "
3249 (todos-short-file-name todos-current-todos-file)
3250 "\nCategory: " (todos-current-category)))
3251 ((eq major-mode 'todos-filtered-items-mode)
3252 "Todos Top Priorities")))
3253 (prefix (propertize (concat todos-prefix " ")
3254 'face 'todos-prefix-string))
3256 (fill-prefix (make-string todos-indent-to-here 32))
3257 (content (buffer-string))
3259 (with-current-buffer (get-buffer-create buf)
3261 (goto-char (point-min))
3264 (end (save-excursion (todos-item-end))))
3265 (when todos-number-priorities
3267 (setq prefix (propertize (concat (number-to-string num) " ")
3268 'face 'todos-prefix-string)))
3270 (fill-region beg end))
3271 ;; Calling todos-forward-item infloops at todos-item-start due to
3272 ;; non-overlay prefix, so search for item start instead.
3273 (if (re-search-forward todos-item-start nil t)
3275 (goto-char (point-max))))
3276 (if (re-search-backward (concat "^" (regexp-quote todos-category-done))
3278 (replace-match todos-done-separator))
3279 (goto-char (point-min))
3283 (let ((file (read-file-name "Print to file: ")))
3284 (funcall todos-print-function file))
3285 (funcall todos-print-function)))
3288 (defun todos-print-to-file ()
3289 "Save printable version of this Todos buffer to a file."
3293 (defun todos-convert-legacy-files ()
3294 "Convert legacy Todo files to the current Todos format.
3295 The files `todo-file-do' and `todo-file-done' are converted and
3296 saved (the latter as a Todos Archive file) with a new name in
3297 `todos-files-directory'. See also the documentation string of
3298 `todos-todo-mode-date-time-regexp' for further details."
3300 (if (fboundp 'todo-mode)
3301 (require 'todo-mode)
3302 (error "Void function `todo-mode'"))
3303 ;; Convert `todo-file-do'.
3304 (if (file-exists-p todo-file-do)
3305 (let ((default "todo-do-conv")
3308 (insert-file-contents todo-file-do)
3309 (let ((end (search-forward ")" (line-end-position) t))
3310 (beg (search-backward "(" (line-beginning-position) t)))
3311 (setq todo-categories
3312 (read (buffer-substring-no-properties beg end))))
3314 (delete-region (line-beginning-position) (1+ (line-end-position)))
3317 ((looking-at (regexp-quote (concat todo-prefix todo-category-beg)))
3318 (replace-match todos-category-beg))
3319 ((looking-at (regexp-quote todo-category-end))
3321 ((looking-at (regexp-quote (concat todo-prefix " "
3322 todo-category-sep)))
3323 (replace-match todos-category-done))
3324 ((looking-at (concat (regexp-quote todo-prefix) " "
3325 todos-todo-mode-date-time-regexp " "
3326 (regexp-quote todo-initials) ":"))
3327 (todos-convert-legacy-date-time)))
3329 (setq file (concat todos-files-directory
3331 (format "Save file as (default \"%s\"): " default)
3334 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3336 (insert-file-contents file)
3337 (let ((todos-categories (todos-make-categories-list t)))
3338 (todos-update-categories-sexp))
3339 (write-region (point-min) (point-max) file nil 'nomessage))
3340 ;; Convert `todo-file-done'.
3341 (when (file-exists-p todo-file-done)
3343 (insert-file-contents todo-file-done)
3344 (let ((beg (make-marker))
3346 cat cats comment item)
3348 (when (looking-at todos-todo-mode-date-time-regexp)
3349 (set-marker beg (point))
3350 (todos-convert-legacy-date-time)
3351 (set-marker end (point))
3353 (insert "[" todos-done-string)
3357 (when (looking-at todos-todo-mode-date-time-regexp)
3358 (todos-convert-legacy-date-time))
3359 (when (looking-at (concat " " (regexp-quote todo-initials) ":"))
3360 (replace-match "")))
3361 (if (re-search-forward
3362 (concat "^" todos-todo-mode-date-time-regexp) nil t)
3363 (goto-char (match-beginning 0))
3364 (goto-char (point-max)))
3366 (when (looking-back "\\[\\([^][]+\\)\\]")
3367 (setq cat (match-string 1))
3368 (goto-char (match-beginning 0))
3370 ;; If the item ends with a non-comment parenthesis not
3371 ;; followed by a period, we lose (but we inherit that problem
3372 ;; from todo-mode.el).
3373 (when (looking-back "(\\(.*\\)) ")
3374 (setq comment (match-string 1))
3376 (insert "[" todos-comment-string ": " comment "]"))
3377 (set-marker end (point))
3378 (if (member cat cats)
3379 ;; If item is already in its category, leave it there.
3380 (unless (save-excursion
3382 (concat "^" (regexp-quote todos-category-beg)
3384 (string= (match-string 1) cat))
3385 ;; Else move it to its category.
3386 (setq item (buffer-substring-no-properties beg end))
3387 (delete-region beg (1+ end))
3388 (set-marker beg (point))
3390 (concat "^" (regexp-quote (concat todos-category-beg cat))
3394 (if (re-search-forward
3395 (concat "^" (regexp-quote todos-category-beg)
3397 (progn (goto-char (match-beginning 0))
3400 (goto-char (point-max)))
3405 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3407 (set-marker beg nil)
3408 (set-marker end nil))
3409 (setq file (concat (file-name-sans-extension file) ".toda"))
3410 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3412 (insert-file-contents file)
3413 (let ((todos-categories (todos-make-categories-list t)))
3414 (todos-update-categories-sexp))
3415 (write-region (point-min) (point-max) file nil 'nomessage)
3416 (setq archive-sexp (read (buffer-substring-no-properties
3417 (line-beginning-position)
3418 (line-end-position)))))
3419 (setq file (concat (file-name-sans-extension file) ".todo"))
3420 ;; Update categories sexp of converted Todos file again, adding
3421 ;; counts of archived items.
3423 (insert-file-contents file)
3424 (let ((sexp (read (buffer-substring-no-properties
3425 (line-beginning-position)
3426 (line-end-position)))))
3428 (let ((archive-cat (assoc (car cat) archive-sexp)))
3430 (aset (cdr cat) 3 (aref (cdr archive-cat) 2)))))
3431 (delete-region (line-beginning-position) (line-end-position))
3432 (prin1 sexp (current-buffer)))
3433 (write-region (point-min) (point-max) file nil 'nomessage)))
3434 (todos-reevaluate-filelist-defcustoms)
3435 (message "Format conversion done."))
3436 (error "No legacy Todo file exists")))
3438 ;; ---------------------------------------------------------------------------
3439 ;;; Navigation Commands
3441 (defun todos-forward-category (&optional back)
3442 "Visit the numerically next category in this Todos file.
3443 If the current category is the highest numbered, visit the first
3444 category. With non-nil argument BACK, visit the numerically
3445 previous category (the highest numbered one, if the current
3446 category is the first)."
3448 (setq todos-category-number
3449 (1+ (mod (- todos-category-number (if back 2 0))
3450 (length todos-categories))))
3451 (when todos-skip-archived-categories
3452 (while (and (zerop (todos-get-count 'todo))
3453 (zerop (todos-get-count 'done))
3454 (not (zerop (todos-get-count 'archived))))
3455 (setq todos-category-number
3456 (apply (if back '1- '1+) (list todos-category-number)))))
3457 (todos-category-select)
3458 (goto-char (point-min)))
3460 (defun todos-backward-category ()
3461 "Visit the numerically previous category in this Todos file.
3462 If the current category is the highest numbered, visit the first
3465 (todos-forward-category t))
3468 (defun todos-jump-to-category (&optional file cat)
3469 "Prompt for a category in a Todos file and jump to it.
3471 With prefix argument FILE, prompt for a specific Todos file and
3472 choose (with TAB completion) a category in it to jump to;
3473 otherwise, choose and jump to any category in either the current
3474 Todos file or a file in `todos-category-completions-files'.
3476 You can also enter a non-existing category name, triggering a
3477 prompt whether to add a new category by that name; on
3478 confirmation it is added and jumped to.
3480 Noninteractively, jump directly to the category named by argument
3481 CAT; this is used in Todos Categories mode."
3483 ;; If invoked outside of Todos mode and there is not yet any Todos
3484 ;; file, initialize one.
3485 (if (null todos-files)
3487 (let ((file0 (when cat ; We're in Todos Categories mode.
3488 ;; With non-nil `todos-skip-archived-categories'
3489 ;; jump to archive file of a category with only
3491 (if (and todos-skip-archived-categories
3492 (zerop (todos-get-count 'todo cat))
3493 (zerop (todos-get-count 'done cat))
3494 (not (zerop (todos-get-count 'archived cat))))
3495 (concat (file-name-sans-extension
3496 todos-current-todos-file) ".toda")
3497 ;; Otherwise, jump to current todos file.
3498 todos-current-todos-file)))
3499 (cat+file (unless cat
3500 (todos-read-category "Jump to category: " nil file))))
3501 (setq category (or cat (car cat+file)))
3502 (unless cat (setq file0 (cdr cat+file)))
3503 (with-current-buffer (find-file-noselect file0 'nowarn)
3504 (setq todos-current-todos-file file0)
3505 ;; If called from Todos Categories mode, clean up before jumping.
3506 (if (string= (buffer-name) todos-categories-buffer)
3508 (set-window-buffer (selected-window)
3509 (set-buffer (find-buffer-visiting file0)))
3510 (unless todos-global-current-todos-file
3511 (setq todos-global-current-todos-file todos-current-todos-file))
3512 (todos-category-number category)
3513 (todos-category-select)
3514 (goto-char (point-min))))))
3516 (defun todos-jump-to-item ()
3517 "Jump to the file and category of the filtered item at point."
3519 (let* ((str (todos-item-string))
3520 (buf (current-buffer))
3521 (res (todos-find-item str))
3526 (message "Category %s does not contain this item." cat)
3528 (set-window-buffer (selected-window)
3529 (set-buffer (find-buffer-visiting file)))
3530 (setq todos-current-todos-file file)
3531 (setq todos-category-number (todos-category-number cat))
3532 (let ((todos-show-with-done (if (or todos-filter-done-items
3533 (eq (cdr found) 'done))
3535 todos-show-with-done)))
3536 (todos-category-select))
3537 (goto-char (car found)))))
3539 (defun todos-forward-item (&optional count)
3540 "Move point down to start of item with next lower priority.
3541 With positive numerical prefix COUNT, move point COUNT items
3544 If the category's done items are hidden, this command also moves
3545 point to the empty line below the last todo item from any higher
3546 item in the category, i.e., when invoked with or without a prefix
3547 argument. If the category's done items are visible, this command
3548 called with a prefix argument only moves point to a lower item,
3549 e.g., with point on the last todo item and called with prefix 1,
3550 it moves point to the first done item; but if called with point
3551 on the last todo item without a prefix argument, it moves point
3552 the the empty line above the done items separator."
3554 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3555 ;; the corresponding command.
3556 (if (and count (> 1 count))
3557 (error "This command only accepts a positive numerical prefix argument")
3558 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
3559 (start (line-end-position)))
3561 (if (re-search-forward todos-item-start nil t (or count 1))
3562 (goto-char (match-beginning 0))
3563 (goto-char (point-max)))
3564 ;; If points advances by one from a todo to a done item, go back to the
3565 ;; space above todos-done-separator, since that is a legitimate place to
3566 ;; insert an item. But skip this space if count > 1, since that should
3567 ;; only stop on an item.
3568 (when (and not-done (todos-done-item-p) (not count))
3569 ;; (if (or (not count) (= count 1))
3570 (re-search-backward "^$" start t)))));)
3571 ;; FIXME: The preceding sexp is insufficient when buffer is not narrowed,
3572 ;; since there could be no done items in this category, so the search puts
3573 ;; us on first todo item of next category. Does this ever happen? If so:
3574 ;; (let ((opoint) (point))
3575 ;; (forward-line -1)
3576 ;; (when (or (not count) (= count 1))
3577 ;; (cond ((looking-at (concat "^" (regexp-quote todos-category-beg)))
3578 ;; (forward-line -2))
3579 ;; ((looking-at (concat "^" (regexp-quote todos-category-done)))
3580 ;; (forward-line -1))
3582 ;; (goto-char opoint)))))))
3584 (defun todos-backward-item (&optional count)
3585 "Move point up to start of item with next higher priority.
3586 With positive numerical prefix COUNT, move point COUNT items
3589 If the category's done items are visible, this command called
3590 with a prefix argument only moves point to a higher item, e.g.,
3591 with point on the first done item and called with prefix 1, it
3592 moves to the last todo item; but if called with point on the
3593 first done item without a prefix argument, it moves point the the
3594 empty line above the done items separator."
3596 ;; Avoid moving to bob if on the first item but not at bob.
3597 (when (> (line-number-at-pos) 1)
3598 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3599 ;; the corresponding command.
3600 (if (and count (> 1 count))
3601 (error "This command only accepts a positive numerical prefix argument")
3602 (let* ((done (todos-done-item-p)))
3605 (re-search-backward todos-item-start nil t (or count 1)))
3606 ;; Unless this is a regexp filtered items buffer (which can contain
3607 ;; intermixed todo and done items), if points advances by one from a
3608 ;; done to a todo item, go back to the space above
3609 ;; todos-done-separator, since that is a legitimate place to insert an
3610 ;; item. But skip this space if count > 1, since that should only
3612 (when (and done (not (todos-done-item-p)) (not count)
3613 ;(or (not count) (= count 1))
3614 (not (equal (buffer-name) todos-regexp-items-buffer)))
3615 (re-search-forward (concat "^" (regexp-quote todos-category-done))
3617 (forward-line -1))))))
3619 (defun todos-forward-button (n &optional wrap display-message)
3621 (interactive "p\nd\nd")
3622 (forward-button n wrap display-message)
3623 (and (bolp) (button-at (point))
3624 ;; Align with beginning of category label.
3625 (forward-char (+ 4 (length todos-categories-number-separator)))))
3627 (defun todos-backward-button (n &optional wrap display-message)
3629 (interactive "p\nd\nd")
3630 (backward-button n wrap display-message)
3631 (and (bolp) (button-at (point))
3632 ;; Align with beginning of category label.
3633 (forward-char (+ 4 (length todos-categories-number-separator)))))
3635 (defun todos-search ()
3636 "Search for a regular expression in this Todos file.
3637 The search runs through the whole file and encompasses all and
3638 only todo and done items; it excludes category names. Multiple
3639 matches are shown sequentially, highlighted in `todos-search'
3642 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3644 matches match cat in-done ov mlen msg)
3646 (goto-char (point-min))
3648 (setq match (re-search-forward regex nil t))
3649 (goto-char (line-beginning-position))
3650 (unless (or (equal (point) 1)
3651 (looking-at (concat "^" (regexp-quote todos-category-beg))))
3652 (if match (push match matches)))
3654 (setq matches (reverse matches))
3658 (setq match (pop matches))
3661 (when (looking-at todos-done-string-start)
3663 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
3664 "\\(.*\\)\n") nil t)
3665 (setq cat (match-string-no-properties 1))
3666 (todos-category-number cat)
3667 (todos-category-select)
3669 (unless todos-show-with-done (todos-hide-show-done-items)))
3671 (setq ov (make-overlay (- (point) (length regex)) (point)))
3672 (overlay-put ov 'face 'todos-search)
3674 (setq mlen (length matches))
3677 (format "There are %d more matches; go to next match? "
3679 "There is one more match; go to it? "))
3681 (throw 'stop (setq msg (if (> mlen 1)
3682 (format "There are %d more matches."
3684 "There is one more match."))))))
3685 (setq msg "There are no more matches."))
3686 (todos-category-select)
3688 (message "No match for \"%s\"" regex))
3690 (if (y-or-n-p (concat msg "\nUnhighlight matches? "))
3691 (todos-clear-matches)
3692 (message "You can unhighlight the matches later by typing %s"
3693 (key-description (car (where-is-internal
3694 'todos-clear-matches))))))))
3696 (defun todos-clear-matches ()
3697 "Remove highlighting on matches found by todos-search."
3699 (remove-overlays 1 (1+ (buffer-size)) 'face 'todos-search))
3701 ;; ---------------------------------------------------------------------------
3702 ;;; Display Commands
3704 (defun todos-hide-show-item-numbering ()
3707 (todos-reset-prefix 'todos-number-priorities (not todos-number-priorities)))
3709 (defun todos-hide-show-done-items ()
3710 "Show hidden or hide visible done items in current category."
3712 (if (zerop (todos-get-count 'done (todos-current-category)))
3713 (message "There are no done items in this category.")
3714 (let ((opoint (point)))
3715 (goto-char (point-min))
3716 (let* ((shown (re-search-forward todos-done-string-start nil t))
3717 (todos-show-with-done (not shown)))
3718 (todos-category-select)
3720 ;; If start of done items sections is below the bottom of the
3721 ;; window, make it visible.
3724 (goto-char (point-min))
3725 (re-search-forward todos-done-string-start nil t)))
3726 (if (not (pos-visible-in-window-p shown))
3728 (goto-char opoint)))))))
3730 (defun todos-show-done-only ()
3731 "Switch between displaying only done or only todo items."
3733 (setq todos-show-done-only (not todos-show-done-only))
3734 (todos-category-select))
3736 (defun todos-highlight-item ()
3737 "Highlight or unhighlight the todo item the cursor is on."
3744 (defun todos-hide-show-date-time ()
3745 "Hide or show date-time header of todo items in the current file."
3749 (goto-char (point-min))
3750 (let ((ovs (overlays-in (point) (1+ (point))))
3754 (if (equal (overlay-get ov 'display) "")
3755 (setq ovs nil hidden t)))
3757 (goto-char (point-min))
3759 (remove-overlays (point-min) (point-max) 'display "")
3761 (when (re-search-forward
3762 (concat todos-date-string-start todos-date-pattern
3763 "\\( " diary-time-regexp "\\)?"
3764 (regexp-quote todos-nondiary-end) "? ")
3766 (unless (save-match-data (todos-done-item-p))
3767 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
3768 (overlay-put ov 'display "")))
3769 (todos-forward-item)))))))
3771 (defun todos-mark-unmark-item (&optional n)
3772 "Mark item with `todos-item-mark' if unmarked, otherwise unmark it.
3773 With a positive numerical prefix argument N, change the
3774 marking of the next N items."
3776 (unless (> n 1) (setq n 1))
3778 (let* ((cat (todos-current-category))
3779 (marks (assoc cat todos-categories-with-marks))
3780 (ov (todos-prefix-overlay))
3781 (pref (overlay-get ov 'before-string)))
3782 (if (todos-marked-item-p)
3784 (overlay-put ov 'before-string (substring pref 1))
3785 (if (= (cdr marks) 1) ; Deleted last mark in this category.
3786 (setq todos-categories-with-marks
3787 (assq-delete-all cat todos-categories-with-marks))
3788 (setcdr marks (1- (cdr marks)))))
3789 (overlay-put ov 'before-string (concat todos-item-mark pref))
3791 (setcdr marks (1+ (cdr marks)))
3792 (push (cons cat 1) todos-categories-with-marks))))
3793 (todos-forward-item)))
3795 (defun todos-mark-category ()
3796 "Mark all visiblw items in this category with `todos-item-mark'."
3799 (goto-char (point-min))
3801 (let* ((cat (todos-current-category))
3802 (marks (assoc cat todos-categories-with-marks))
3803 (ov (todos-prefix-overlay))
3804 (pref (overlay-get ov 'before-string)))
3805 (unless (todos-marked-item-p)
3806 (overlay-put ov 'before-string (concat todos-item-mark pref))
3808 (setcdr marks (1+ (cdr marks)))
3809 (push (cons cat 1) todos-categories-with-marks))))
3810 (todos-forward-item))))
3812 (defun todos-unmark-category ()
3813 "Remove `todos-item-mark' from all visible items in this category."
3816 (goto-char (point-min))
3818 (let* ((cat (todos-current-category))
3819 (marks (assoc cat todos-categories-with-marks))
3820 (ov (todos-prefix-overlay))
3821 (pref (overlay-get ov 'before-string)))
3822 (when (todos-marked-item-p)
3823 (overlay-put ov 'before-string (substring pref 1))
3824 (setq todos-categories-with-marks
3825 (delq (assoc (todos-current-category)
3826 todos-categories-with-marks)
3827 todos-categories-with-marks))))
3828 (todos-forward-item))))
3830 ;; ---------------------------------------------------------------------------
3831 ;;; Item filtering commands
3833 (defun todos-set-top-priorities-in-file ()
3834 "Set number of top priorities for this file.
3835 See `todos-set-top-priorities' for more details."
3837 (todos-set-top-priorities))
3839 (defun todos-set-top-priorities-in-category ()
3840 "Set number of top priorities for this category.
3841 See `todos-set-top-priorities' for more details."
3843 (todos-set-top-priorities t))
3845 (defun todos-top-priorities (&optional arg multifile)
3846 "Display a list of top priority items from different categories.
3847 The categories are either a subset of those in the current Todos
3848 file, or else, with non-nil argument MULTIFILE, a subset of the
3849 categories in the files listed in `todos-filter-files', or if
3850 this nil, in the files chosen from a file selection dialog that
3851 pops up in this case.
3853 With numerical prefix ARG show at most ARG top priority items
3854 from each category. With `C-u' as prefix argument show the
3855 numbers of top priority items specified by category in
3856 `todos-priorities-rules', if this has an entry for the file(s);
3857 otherwise show `todos-show-priorities' items per category in the
3858 file(s). With no prefix argument, if a top priorities file for
3859 the current Todos file has previously been saved (see
3860 `todos-save-top-priorities-buffer'), visit this file; if there is
3861 no such file, build the list as with prefix argument `C-u'.
3863 The prefix ARG regulates how many top priorities from
3864 each category to show, as described above."
3866 (let* ((flist (if multifile
3867 (or todos-filter-files
3868 (progn (todos-multiple-filter-files)
3869 todos-multiple-filter-files))
3870 (list todos-current-todos-file)))
3871 (tp-file (if (equal flist 'quit)
3872 ;; Pressed `cancel' in file selection dialog.
3874 (concat todos-files-directory
3875 (mapconcat 'identity
3876 (mapcar 'todos-short-file-name flist)
3879 (tp-file-exists (file-exists-p tp-file))
3880 (buf todos-top-priorities-buffer))
3881 (cond ((and arg (natnump arg))
3882 (todos-filter-items (cons 'top arg) flist))
3883 ((and (not arg) tp-file-exists)
3885 (todos-prefix-overlays)
3886 (todos-check-top-priorities))
3888 (todos-filter-items 'top flist)))
3889 (unless tp-file-exists
3890 (todos-filtered-buffer-name buf flist))))
3892 (defun todos-top-priorities-multifile (&optional arg)
3893 "Display a list of top priority items from different categories.
3894 The categories are a subset of the categories in the files listed
3895 in `todos-filter-files', or if this nil, in the files chosen from
3896 a file selection dialog that pops up in this case.
3898 With numerical prefix ARG show at most ARG top priority items
3899 from each category in each file. With `C-u' as prefix argument
3900 show the numbers of top priority items specified in
3901 `todos-priorities-rules', if this is non-nil; otherwise show
3902 `todos-show-priorities' items per category. With no prefix
3903 argument, if a top priorities file for the chosen Todos files
3904 exists (see `todos-save-top-priorities-buffer'), visit this file;
3905 if there is no such file, do the same as with prefix argument
3908 (todos-top-priorities arg t))
3910 (defun todos-diary-items (&optional multifile)
3911 "Display a list of todo diary items from different categories.
3912 The categories are either a subset of those in the current Todos
3913 file, or else, with non-nil argument MULTIFILE, a subset of the
3914 categories in the files listed in `todos-filter-files', or if
3915 this nil, in the files chosen from a file selection dialog that
3916 pops up in this case."
3918 (let ((flist (if multifile
3919 (or todos-filter-files
3920 (progn (todos-multiple-filter-files)
3921 todos-multiple-filter-files))
3922 (list todos-current-todos-file)))
3923 (buf todos-diary-items-buffer))
3924 (if (equal flist 'quit)
3925 ;; Pressed `cancel' in file selection dialog.
3927 (todos-filter-items 'diary flist)
3928 (todos-filtered-buffer-name buf flist))))
3930 (defun todos-diary-items-multifile ()
3931 "Display a list of todo diary items from one or more Todos files.
3932 The categories are a subset of the categories in the files listed
3933 in `todos-filter-files', or if this nil, in the files chosen from
3934 a file selection dialog that pops up in this case."
3936 (todos-diary-items t))
3938 (defun todos-regexp-items (&optional multifile)
3939 "Prompt for a regular expression and display items that match it.
3940 The matches may be from different categories and with non-nil
3941 option `todos-filter-done-items', can include not only todo items
3942 but also done items, including those in Archive files.
3944 The categories are either a subset of those in the current Todos
3945 file (and possibly in the corresponding Archive file), or else,
3946 with non-nil argument MULTIFILE, a subset of the categories in
3947 the files listed in `todos-filter-files', or if this nil, in the
3948 files chosen from a file selection dialog that pops up in this
3949 case (and possibly in the corresponding Archive files)."
3951 (let ((flist (if multifile
3952 (or todos-filter-files
3953 (progn (todos-multiple-filter-files)
3954 todos-multiple-filter-files))
3955 (list todos-current-todos-file)))
3956 (buf todos-regexp-items-buffer))
3957 (if (equal flist 'quit)
3958 ;; Pressed `cancel' in file selection dialog.
3960 (todos-filter-items 'regexp flist)
3961 (todos-filtered-buffer-name buf flist))))
3963 (defun todos-regexp-items-multifile ()
3964 "Prompt for a regular expression and display items that match it.
3965 The matches may be from different categories and with non-nil
3966 option `todos-filter-done-items', can include not only todo items
3967 but also done items, including those in Archive files.
3969 The categories are a subset of the categories in the files listed
3970 in `todos-filter-files', or if this nil, in the files chosen from
3971 a file selection dialog that pops up in this case (and possibly
3972 in the corresponding Archive files)."
3974 (todos-regexp-items t))
3976 ;; ---------------------------------------------------------------------------
3977 ;;; Editing Commands
3979 (defun todos-add-file ()
3980 "Name and add a new Todos file.
3981 Interactively, prompt for a category and display it.
3982 Noninteractively, return the name of the new file."
3984 (let ((prompt (concat "Enter name of new Todos file "
3985 "(TAB or SPC to see current names): "))
3987 (setq file (todos-read-file-name prompt))
3988 (with-current-buffer (get-buffer-create file)
3990 (write-region (point-min) (point-max) file nil 'nomessage nil t)
3992 (todos-reevaluate-filelist-defcustoms)
3993 (if (called-interactively-p)
3995 (set-window-buffer (selected-window)
3996 (set-buffer (find-file-noselect file)))
3997 (setq todos-current-todos-file file)
4001 ;;; Category editing commands
4003 (defun todos-add-category (&optional file cat)
4004 "Add a new category to a Todos file.
4006 Called interactively with prefix argument FILE, prompt for a file
4007 and then for a new category to add to that file, otherwise prompt
4008 just for a category to add to the current Todos file. After adding
4009 the category, visit it in Todos mode.
4011 Non-interactively, add category CAT to file FILE; if FILE is nil,
4012 add CAT to the current Todos file. After adding the category,
4013 return the new category number."
4016 ;; If cat is passed from caller, don't prompt, unless it is "",
4017 ;; which means the file was just added and has no category yet.
4018 (if (and cat (> (length cat) 0))
4019 (setq file0 (or (and (stringp file) file)
4020 todos-current-todos-file))
4021 (setq catfil (todos-read-category "Enter a new category name: "
4022 'add (when (called-interactively-p 'any)
4025 file0 (if (called-interactively-p 'any)
4029 (let ((counts (make-vector 4 0)) ; [todo diary done archived]
4030 (num (1+ (length todos-categories)))
4031 (buffer-read-only nil))
4032 (setq todos-current-todos-file file0)
4033 (setq todos-categories (append todos-categories
4034 (list (cons cat counts))))
4036 (goto-char (point-max))
4037 (save-excursion ; Save point for todos-category-select.
4038 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
4039 (todos-update-categories-sexp)
4040 ;; If invoked by user, display the newly added category, if
4041 ;; called programmatically return the category number to the
4043 (if (called-interactively-p 'any)
4045 (setq todos-category-number num)
4046 (todos-category-select))
4049 (defun todos-rename-category ()
4050 "Rename current Todos category.
4051 If this file has an archive containing this category, rename the
4052 category there as well."
4054 (let* ((cat (todos-current-category))
4055 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat))))
4056 (setq new (todos-validate-name new 'category))
4057 (let* ((ofile todos-current-todos-file)
4058 (archive (concat (file-name-sans-extension ofile) ".toda"))
4059 (buffers (append (list ofile)
4060 (unless (zerop (todos-get-count 'archived cat))
4062 (dolist (buf buffers)
4063 (with-current-buffer (find-file-noselect buf)
4064 (let (buffer-read-only)
4065 (setq todos-categories (todos-set-categories))
4068 (setcar (assoc cat todos-categories) new)
4070 (goto-char (point-min))
4071 (todos-update-categories-sexp)
4072 (re-search-forward (concat (regexp-quote todos-category-beg)
4073 "\\(" (regexp-quote cat) "\\)\n")
4075 (replace-match new t t nil 1)))))))
4076 (force-mode-line-update))
4077 (save-excursion (todos-category-select)))
4079 (defun todos-delete-category (&optional arg)
4080 "Delete current Todos category provided it is empty.
4081 With ARG non-nil delete the category unconditionally,
4082 i.e. including all existing todo and done items."
4084 (let* ((file todos-current-todos-file)
4085 (cat (todos-current-category))
4086 (todo (todos-get-count 'todo cat))
4087 (done (todos-get-count 'done cat))
4088 (archived (todos-get-count 'archived cat)))
4090 (or (> todo 0) (> done 0)))
4091 (message "%s" (substitute-command-keys
4092 (concat "To delete a non-empty category, "
4093 "type C-u \\[todos-delete-category].")))
4094 (when (cond ((= (length todos-categories) 1)
4095 (y-or-n-p (concat "This is the only category in this file; "
4096 "deleting it will also delete the file.\n"
4097 "Do you want to proceed? ")))
4099 (y-or-n-p (concat "This category has archived items; "
4100 "the archived category will remain\n"
4101 "after deleting the todo category. "
4102 "Do you still want to delete it\n"
4103 "(see 'todos-skip-archived-categories' "
4104 "for another option)? ")))
4106 (y-or-n-p (concat "Permanently remove category \"" cat
4107 "\"" (and arg " and all its entries")
4110 (let ((buffer-read-only)
4111 (beg (re-search-backward
4112 (concat "^" (regexp-quote (concat todos-category-beg cat))
4114 (end (if (re-search-forward
4115 (concat "\n\\(" (regexp-quote todos-category-beg)
4119 (remove-overlays beg end)
4120 (delete-region beg end)
4121 (if (= (length todos-categories) 1)
4122 ;; If deleted category was the only one, delete the file.
4124 (todos-reevaluate-filelist-defcustoms)
4125 ;; Skip confirming killing the archive buffer if it has been
4126 ;; modified and not saved.
4127 (set-buffer-modified-p nil)
4130 (message "Deleted Todos file %s." file))
4131 (setq todos-categories (delete (assoc cat todos-categories)
4133 (todos-update-categories-sexp)
4134 (setq todos-category-number
4135 (1+ (mod todos-category-number (length todos-categories))))
4136 (todos-category-select)
4137 (goto-char (point-min))
4138 (message "Deleted category %s." cat)))))))
4140 (defun todos-move-category ()
4141 "Move current category to a different Todos file.
4142 If current category has archived items, also move those to the
4143 archive of the file moved to, creating it if it does not exist."
4145 (when (or (> (length todos-categories) 1)
4146 (y-or-n-p (concat "This is the only category in this file; "
4147 "moving it will also delete the file.\n"
4148 "Do you want to proceed? ")))
4149 (let* ((ofile todos-current-todos-file)
4150 (cat (todos-current-category))
4151 (nfile (todos-read-file-name
4152 "Choose a Todos file to move this category to: " nil t))
4153 (archive (concat (file-name-sans-extension ofile) ".toda"))
4154 (buffers (append (list ofile)
4155 (unless (zerop (todos-get-count 'archived cat))
4158 (while (equal (file-truename nfile) (file-truename ofile))
4159 (setq nfile (todos-read-file-name
4160 "Choose a file distinct from this file: " nil t)))
4161 (dolist (buf buffers)
4162 (with-current-buffer (find-file-noselect buf)
4164 (goto-char (point-max))
4165 (let* ((beg (re-search-backward
4166 (concat "^" (regexp-quote (concat todos-category-beg cat))
4169 (end (if (re-search-forward
4170 (concat "^" (regexp-quote todos-category-beg))
4174 (content (buffer-substring-no-properties beg end))
4175 (counts (cdr (assoc cat todos-categories)))
4177 ;; Move the category to the new file. Also update or create
4178 ;; archive file if necessary.
4179 (with-current-buffer
4181 ;; Regenerate todos-archives in case there
4182 ;; is a newly created archive.
4183 (if (member buf (funcall todos-files-function t))
4184 (concat (file-name-sans-extension nfile) ".toda")
4186 (let* ((nfile-short (todos-short-file-name nfile))
4188 (format "Todos file \"%s\" already has "
4190 (format "the category \"%s\";\n" cat)
4191 "enter a new category name: "))
4194 (goto-char (point-max))
4196 ;; If the file moved to has a category with the same
4197 ;; name, rename the moved category.
4198 (when (assoc cat todos-categories)
4199 (unless (member (file-truename (buffer-file-name))
4200 (funcall todos-files-function t))
4201 (setq new (read-from-minibuffer prompt))
4202 (setq new (todos-validate-name new 'category))))
4203 ;; Replace old with new name in Todos and archive files.
4205 (goto-char (point-max))
4207 (concat "^" (regexp-quote todos-category-beg)
4208 "\\(" (regexp-quote cat) "\\)$") nil t)
4209 (replace-match new nil nil nil 1)))
4210 (setq todos-categories
4211 (append todos-categories (list (cons new counts))))
4212 (todos-update-categories-sexp)
4213 ;; If archive was just created, save it to avoid "File
4214 ;; <xyz> no longer exists!" message on invoking
4215 ;; `todos-view-archived-items'.
4216 (unless (file-exists-p (buffer-file-name))
4218 (todos-category-number (or new cat))
4219 (todos-category-select))
4220 ;; Delete the category from the old file, and if that was the
4221 ;; last category, delete the file. Also handle archive file
4223 (remove-overlays beg end)
4224 (delete-region beg end)
4225 (goto-char (point-min))
4226 ;; Put point after todos-categories sexp.
4228 (if (eobp) ; Aside from sexp, file is empty.
4230 ;; Skip confirming killing the archive buffer.
4231 (set-buffer-modified-p nil)
4232 (delete-file todos-current-todos-file)
4234 (when (member todos-current-todos-file todos-files)
4235 (todos-reevaluate-filelist-defcustoms)))
4236 (setq todos-categories (delete (assoc cat todos-categories)
4238 (todos-update-categories-sexp)
4239 (todos-category-select)))))
4240 (set-window-buffer (selected-window)
4241 (set-buffer (find-file-noselect nfile)))
4242 (todos-category-number (or new cat))
4243 (todos-category-select))))
4245 (defun todos-merge-category (&optional file)
4246 "Merge current category into another existing category.
4248 With prefix argument FILE, prompt for a specific Todos file and
4249 choose (with TAB completion) a category in it to merge into;
4250 otherwise, choose and merge into a category in either the
4251 current Todos file or a file in `todos-category-completions-files'.
4253 After merging, the current category's todo and done items are
4254 appended to the chosen goal category's todo and done items,
4255 respectively. The goal category becomes the current category,
4256 and the previous current category is deleted.
4258 If both the first and goal categories also have archived items,
4259 the former are merged to the latter. If only the first category
4260 has archived items, the archived category is renamed to the goal
4263 (let* ((tfile todos-current-todos-file)
4264 (archive (concat (file-name-sans-extension (if file gfile tfile))
4266 (cat (todos-current-category))
4267 (cat+file (todos-read-category "Merge into category: " 'merge file))
4268 (goal (car cat+file))
4269 (gfile (cdr cat+file))
4270 archived-count here)
4271 ;; Merge in todo file.
4272 (with-current-buffer (get-buffer (find-file-noselect tfile))
4274 (let* ((buffer-read-only nil)
4277 (concat "^" (regexp-quote todos-category-beg)) nil t)
4279 (tbeg (progn (forward-line) (point-marker)))
4282 (concat "^" (regexp-quote todos-category-done)) nil t)
4283 (forward-line) (point-marker)))
4284 ;; Omit empty line between todo and done items.
4285 (tend (progn (forward-line -2) (point-marker)))
4287 (if (re-search-forward
4288 (concat "^" (regexp-quote todos-category-beg)) nil t)
4290 (goto-char (match-beginning 0))
4292 (point-max-marker))))
4293 (todo (buffer-substring-no-properties tbeg tend))
4294 (done (buffer-substring-no-properties dbeg cend)))
4295 (goto-char (point-min))
4296 ;; Merge any todo items.
4297 (unless (zerop (length todo))
4299 (concat "^" (regexp-quote (concat todos-category-beg goal)) "$")
4302 (concat "^" (regexp-quote todos-category-done)) nil t)
4304 (setq here (point-marker))
4306 (todos-update-count 'todo (todos-get-count 'todo cat) goal))
4307 ;; Merge any done items.
4308 (unless (zerop (length done))
4309 (goto-char (if (re-search-forward
4310 (concat "^" (regexp-quote todos-category-beg)) nil t)
4313 (when (zerop (length todo)) (setq here (point-marker)))
4315 (todos-update-count 'done (todos-get-count 'done cat) goal))
4316 (remove-overlays cbeg cend)
4317 (delete-region cbeg cend)
4318 (setq todos-categories (delete (assoc cat todos-categories)
4320 (todos-update-categories-sexp)
4321 (mapc (lambda (m) (set-marker m nil)) (list cbeg tbeg dbeg tend cend))))
4322 (when (file-exists-p archive)
4323 ;; Merge in archive file.
4324 (with-current-buffer (get-buffer (find-file-noselect archive))
4326 (goto-char (point-min))
4327 (let ((buffer-read-only nil)
4328 (cbeg (save-excursion
4329 (when (re-search-forward
4330 (concat "^" (regexp-quote
4331 (concat todos-category-beg cat)) "$")
4333 (goto-char (match-beginning 0))
4335 (gbeg (save-excursion
4336 (when (re-search-forward
4337 (concat "^" (regexp-quote
4338 (concat todos-category-beg goal)) "$")
4340 (goto-char (match-beginning 0))
4344 (setq archived-count (todos-get-count 'done cat))
4345 (setq cend (save-excursion
4346 (if (re-search-forward
4347 (concat "^" (regexp-quote todos-category-beg))
4351 (setq carch (save-excursion (goto-char cbeg) (forward-line)
4352 (buffer-substring-no-properties (point) cend)))
4353 ;; If both categories of the merge have archived items, merge the
4354 ;; source items to the goal items, else "merge" by renaming the
4355 ;; source category to goal.
4358 (goto-char (if (re-search-forward
4359 (concat "^" (regexp-quote todos-category-beg))
4364 (remove-overlays cbeg cend)
4365 (delete-region cbeg cend))
4367 (search-forward cat)
4368 (replace-match goal))
4369 (setq todos-categories (todos-make-categories-list t))
4370 (todos-update-categories-sexp)))))
4371 (with-current-buffer (get-file-buffer tfile)
4372 (when archived-count
4373 (unless (zerop archived-count)
4374 (todos-update-count 'archived archived-count goal)
4375 (todos-update-categories-sexp)))
4376 (todos-category-number goal)
4377 ;; If there are only merged done items, show them.
4378 (let ((todos-show-with-done (zerop (todos-get-count 'todo goal))))
4379 (todos-category-select)
4380 ;; Put point on the first merged item.
4382 (set-marker here nil)))
4384 (defun todos-set-category-priority (&optional arg)
4385 "Change priority of category at point in Todos Categories buffer.
4387 With ARG nil, prompt for the new priority number. Alternatively,
4388 the new priority can be provided by a numerical prefix ARG.
4389 Otherwise, if ARG is either of the symbols `raise' or `lower',
4390 raise or lower the category's priority by one."
4392 (let ((curnum (save-excursion
4393 ;; Get the number representing the priority of the category
4394 ;; on the current line.
4395 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
4396 (when curnum ; Do nothing if we're not on a category line.
4397 (let* ((maxnum (length todos-categories))
4398 (prompt (format "Set category priority (1-%d): " maxnum))
4399 (col (current-column))
4400 (buffer-read-only nil)
4401 (priority (cond ((and (eq arg 'raise) (> curnum 1))
4403 ((and (eq arg 'lower) (< curnum maxnum))
4406 (while (not priority)
4407 (setq candidate (or arg (read-number prompt)))
4410 (cond ((or (< candidate 1) (> candidate maxnum))
4411 (format "Priority must be an integer between 1 and %d: "
4413 ((= candidate curnum)
4414 "Choose a different priority than the current one: ")))
4415 (unless prompt (setq priority candidate)))
4416 (let* ((lower (< curnum priority)) ; Priority is being lowered.
4417 (head (butlast todos-categories
4418 (apply (if lower 'identity '1+)
4419 (list (- maxnum priority)))))
4420 (tail (nthcdr (apply (if lower 'identity '1-) (list priority))
4422 ;; Category's name and items counts list.
4423 (catcons (nth (1- curnum) todos-categories))
4424 (todos-categories (nconc head (list catcons) tail))
4426 (when lower (setq todos-categories (nreverse todos-categories)))
4427 (setq todos-categories (delete-dups todos-categories))
4428 (when lower (setq todos-categories (nreverse todos-categories)))
4429 (setq newcats todos-categories)
4431 (with-current-buffer (find-buffer-visiting todos-current-todos-file)
4432 (setq todos-categories newcats)
4433 (todos-update-categories-sexp))
4434 (todos-display-categories)
4435 (forward-line (1+ priority))
4436 (forward-char col))))))
4438 (defun todos-raise-category-priority ()
4439 "Raise priority of category at point in Todos Categories buffer."
4441 (todos-set-category-priority 'raise))
4443 (defun todos-lower-category-priority ()
4444 "Lower priority of category at point in Todos Categories buffer."
4446 (todos-set-category-priority 'lower))
4448 ;; ---------------------------------------------------------------------------
4449 ;;; Item editing commands
4452 (defun todos-insert-item (&optional arg diary nonmarking date-type time
4454 "Add a new Todo item to a category.
4455 \(See the note at the end of this document string about key
4456 bindings and convenience commands derived from this command.)
4458 With no (or nil) prefix argument ARG, add the item to the current
4459 category; with one prefix argument (C-u), prompt for a category
4460 from the current Todos file; with two prefix arguments (C-u C-u),
4461 first prompt for a Todos file, then a category in that file. If
4462 a non-existing category is entered, ask whether to add it to the
4463 Todos file; if answered affirmatively, add the category and
4464 insert the item there.
4466 When argument DIARY is non-nil, this overrides the intent of the
4467 user option `todos-include-in-diary' for this item: if
4468 `todos-include-in-diary' is nil, include the item in the Fancy
4469 Diary display, and if it is non-nil, exclude the item from the
4470 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
4471 has its intended effect.
4473 When the item is included in the Fancy Diary display and the
4474 argument NONMARKING is non-nil, this overrides the intent of the
4475 user option `todos-diary-nonmarking' for this item: if
4476 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
4477 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
4479 The argument DATE-TYPE determines the content of the item's
4480 mandatory date header string and how it is added:
4481 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
4482 when the user puts the cursor on a date and hits RET, that
4483 date, in the format set by `calendar-date-display-form',
4484 becomes the date in the header.
4485 - If DATE-TYPE is a string matching the regexp
4486 `todos-date-pattern', that string becomes the date in the
4487 header. This case is for the command
4488 `todos-insert-item-from-calendar' which is called from the
4490 - If DATE-TYPE is the symbol `date', the header contains the date
4491 in the format set by `calendar-date-display-form', with year,
4492 month and day individually prompted for (month with tab
4494 - If DATE-TYPE is the symbol `dayname' the header contains a
4495 weekday name instead of a date, prompted for with tab
4497 - If DATE-TYPE has any other value (including nil or none) the
4498 header contains the current date (in the format set by
4499 `calendar-date-display-form').
4501 With non-nil argument TIME prompt for a time string, which must
4502 match `diary-time-regexp'. Typing `<return>' at the prompt
4503 returns the current time, if the user option
4504 `todos-always-add-time-string' is non-nil, otherwise the empty
4505 string (i.e., no time string). If TIME is absent or nil, add or
4506 omit the current time string according as
4507 `todos-always-add-time-string' is non-nil or nil, respectively.
4509 The argument REGION-OR-HERE determines the source and location of
4511 - If the REGION-OR-HERE is the symbol `here', prompt for the text
4512 of the new item and, if the command was invoked in the current
4513 category, insert it directly above the todo item at
4514 point (hence lowering the priority of the remaining items), or
4515 if point is on the empty line below the last todo item, insert
4516 the new item there. If the command with `here' is invoked
4517 outside of the current category, jump to the chosen category
4518 and insert the new item as the first item in the category.
4519 - If REGION-OR-HERE is the symbol `region', use the region of the
4520 current buffer as the text of the new item, depending on the
4521 value of user option `todos-use-only-highlighted-region': if
4522 this is non-nil, then use the region only when it is
4523 highlighted; otherwise, use the region regardless of
4524 highlighting. An error is signalled if there is no region in
4525 the current buffer. Prompt for the item's priority in the
4526 category (an integer between 1 and one more than the number of
4527 items in the category), and insert the item accordingly.
4528 - If REGION-OR-HERE has any other value (in particular, nil or
4529 none), prompt for the text and the item's priority, and insert
4530 the item accordingly.
4532 To facilitate using these arguments when inserting a new todo
4533 item, convenience commands have been defined for all admissible
4534 combinations together with mnenomic key bindings based on on the
4535 name of the arguments and their order in the command's argument
4536 list: diar_y_ - nonmar_k_ing - _c_alendar or _d_ate or day_n_ame
4537 - _t_ime - _r_egion or _h_ere. These key combinations are
4538 appended to the basic insertion key (i) and keys that allow a
4539 following key must be doubled when used finally. For example,
4540 `iyh' will insert a new item with today's date, marked according
4541 to the DIARY argument described above, and with priority
4542 according to the HERE argument; while `iyy' does the same except
4543 the priority is not given by HERE but by prompting."
4544 ;; An alternative interface for customizing key
4545 ;; binding is also provided with the function
4546 ;; `todos-insertion-bindings'." ;FIXME
4548 ;; If invoked outside of Todos mode and there is not yet any Todos
4549 ;; file, initialize one.
4550 (if (null todos-files)
4552 (let ((region (eq region-or-here 'region))
4553 (here (eq region-or-here 'here)))
4555 (let (use-empty-active-region)
4556 (unless (and todos-use-only-highlighted-region (use-region-p))
4557 (error "There is no active region"))))
4558 (let* ((buf (current-buffer))
4559 (ocat (todos-current-category))
4560 (todos-mm (eq major-mode 'todos-mode))
4561 (cat+file (cond ((equal arg '(4))
4562 (todos-read-category "Insert in category: "))
4564 (todos-read-category "Insert in category: "
4567 (cons (todos-current-category)
4568 (or todos-current-todos-file
4569 (todos-absolute-file-name
4570 todos-default-todos-file))))))
4571 (cat (car cat+file))
4572 (file (cdr cat+file))
4573 (new-item (if region
4574 (buffer-substring-no-properties
4575 (region-beginning) (region-end))
4576 (read-from-minibuffer "Todo item: ")))
4578 ((eq date-type 'date)
4580 ((eq date-type 'dayname)
4581 (todos-read-dayname))
4582 ((eq date-type 'calendar)
4583 (setq todos-date-from-calendar t)
4584 (or (todos-set-date-from-calendar)
4585 ;; If user exits Calendar before choosing
4586 ;; a date, cancel item insertion.
4588 ((and (stringp date-type)
4589 (string-match todos-date-pattern date-type))
4590 (setq todos-date-from-calendar date-type)
4591 (todos-set-date-from-calendar))
4593 (calendar-date-string (calendar-current-date) t t))))
4594 (time-string (or (and time (todos-read-time))
4595 (and todos-always-add-time-string
4596 (substring (current-time-string) 11 16)))))
4597 (setq todos-date-from-calendar nil)
4598 (find-file-noselect file 'nowarn)
4599 (set-window-buffer (selected-window)
4600 ;; If current category was nil till now, on
4601 ;; entering Todos mode here it will be set to
4602 ;; file's first category.
4603 (set-buffer (find-buffer-visiting file)))
4604 (setq todos-current-todos-file file)
4605 ;; If only done items are displayed in category, toggle to
4608 (when (and (goto-char (point-min))
4609 (looking-at todos-done-string-start))
4610 (todos-show-done-only)))
4611 (unless todos-global-current-todos-file
4612 (setq todos-global-current-todos-file todos-current-todos-file))
4613 ;; These are not needed here, since they are called in
4614 ;; todos-set-item-priority.
4615 ;; (todos-category-number cat)
4616 ;; (todos-category-select)
4617 (let (buffer-read-only)
4619 ;; Add date, time and diary marking as required.
4620 (concat (if (not (and diary (not todos-include-in-diary)))
4621 todos-nondiary-start
4622 (when (and nonmarking (not todos-diary-nonmarking))
4623 diary-nonmarking-symbol))
4624 date-string (when (and time-string ; Can be empty string.
4625 (not (zerop (length time-string))))
4626 (concat " " time-string))
4627 (when (not (and diary (not todos-include-in-diary)))
4630 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4631 (setq new-item (replace-regexp-in-string
4632 "\\(\n\\)[^[:blank:]]"
4633 (concat "\n" (make-string todos-indent-to-here 32))
4634 new-item nil nil 1))
4637 (unless (and todos-mm (equal cat ocat))
4638 (todos-category-number cat)
4639 (todos-category-select)
4640 (goto-char (point-min)))
4641 (todos-insert-with-overlays new-item))
4643 (todos-set-item-priority new-item cat t)
4644 ;; In (at least) two circumstances, point may be at eob
4645 ;; and eob at window-start, so that that the todo items
4646 ;; are out of view: (i) if item is inserted at end of
4647 ;; category, (ii) if only done items are shown, this is
4648 ;; (above) programmatically toggled to show todo items,
4649 ;; and user cancels before setting new item's
4650 ;; priority. To make sure the todo items are displayed
4651 ;; in the window, force recentering.
4653 (todos-update-count 'todo 1)
4654 (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
4655 (todos-update-categories-sexp))))))
4657 (defun todos-copy-item ()
4658 "Copy item at point and insert the copy as a new item."
4660 (unless (or (todos-done-item-p) (looking-at "^$"))
4661 (let ((copy (todos-item-string))
4662 (diary-item (todos-diary-item-p)))
4663 (todos-set-item-priority copy (todos-current-category) t)
4664 (todos-update-count 'todo 1)
4665 (when diary-item (todos-update-count 'diary 1))
4666 (todos-update-categories-sexp))))
4668 (defvar todos-date-from-calendar nil
4669 "Helper variable for setting item date from the Emacs Calendar.")
4671 (defun todos-set-date-from-calendar ()
4672 "Return string of date chosen from Calendar."
4673 (cond ((and (stringp todos-date-from-calendar)
4674 (string-match todos-date-pattern todos-date-from-calendar))
4675 todos-date-from-calendar)
4676 (todos-date-from-calendar
4677 (let (calendar-view-diary-initially-flag)
4678 (calendar)) ; *Calendar* is now current buffer.
4679 (define-key calendar-mode-map [remap newline] 'exit-recursive-edit)
4680 ;; If user exits Calendar before choosing a date, clean up properly.
4681 (define-key calendar-mode-map
4682 [remap calendar-exit] (lambda ()
4686 (exit-recursive-edit))))
4687 (message "Put cursor on a date and type <return> to set it.")
4690 (when (equal (buffer-name) calendar-buffer)
4691 (setq todos-date-from-calendar
4692 (calendar-date-string (calendar-cursor-to-date t) t t))
4694 todos-date-from-calendar)
4695 (define-key calendar-mode-map [remap newline] nil)
4696 (define-key calendar-mode-map [remap calendar-exit] nil)
4697 (unless (zerop (recursion-depth)) (exit-recursive-edit))
4698 (when (stringp todos-date-from-calendar)
4699 todos-date-from-calendar)))))
4701 (defun todos-delete-item ()
4702 "Delete at least one item in this category.
4704 If there are marked items, delete all of these; otherwise, delete
4709 (let* ((cat (todos-current-category))
4710 (marked (assoc cat todos-categories-with-marks))
4711 (item (unless marked (todos-item-string)))
4713 (y-or-n-p "Permanently delete all marked items? ")
4715 (setq ov (make-overlay
4716 (save-excursion (todos-item-start))
4717 (save-excursion (todos-item-end))))
4718 (overlay-put ov 'face 'todos-search)
4719 (y-or-n-p (concat "Permanently delete this item? ")))))
4722 (and marked (goto-char (point-min)))
4725 (if (or (and marked (todos-marked-item-p)) item)
4727 (if (todos-done-item-p)
4728 (todos-update-count 'done -1)
4729 (todos-update-count 'todo -1 cat)
4730 (and (todos-diary-item-p) (todos-update-count 'diary -1)))
4731 (if ov (delete-overlay ov))
4733 ;; Don't leave point below last item.
4734 (and item (bolp) (eolp) (< (point-min) (point-max))
4735 (todos-backward-item))
4737 (throw 'done (setq item nil))))
4738 (todos-forward-item))))
4740 (setq todos-categories-with-marks
4741 (assq-delete-all cat todos-categories-with-marks)))
4742 (todos-update-categories-sexp)
4743 (todos-prefix-overlays)))
4744 (if ov (delete-overlay ov)))))
4746 (defun todos-edit-item (&optional arg)
4747 "Edit the Todo item at point.
4749 With non-nil prefix argument ARG, include the item's date/time
4750 header, making it also editable; otherwise, include only the item
4753 If the item consists of only one logical line, edit it in the
4754 minibuffer; otherwise, edit it in Todos Edit mode."
4756 (when (todos-item-string)
4757 (let* ((opoint (point))
4758 (start (todos-item-start))
4761 (concat todos-date-string-start todos-date-pattern
4762 "\\( " diary-time-regexp "\\)?"
4763 (regexp-quote todos-nondiary-end) "?")
4764 (line-end-position) t)
4765 (1+ (- (point) start))))
4766 (header (substring (todos-item-string) 0 item-beg))
4767 (item (if arg (todos-item-string)
4768 (substring (todos-item-string) item-beg)))
4769 (multiline (> (length (split-string item "\n")) 1))
4770 (buffer-read-only nil))
4772 (todos-edit-multiline-item)
4773 (let ((new (concat (if arg "" header)
4774 (read-string "Edit: " (if arg
4775 (cons item item-beg)
4778 (while (not (string-match (concat todos-date-string-start
4779 todos-date-pattern) new))
4780 (setq new (read-from-minibuffer
4781 "Item must start with a date: " new))))
4782 ;; Ensure lines following hard newlines are indented.
4783 (setq new (replace-regexp-in-string
4784 "\\(\n\\)[^[:blank:]]"
4785 (concat "\n" (make-string todos-indent-to-here 32)) new
4787 ;; If user moved point during editing, make sure it moves back.
4790 (todos-insert-with-overlays new)
4791 (move-to-column item-beg))))))
4793 (defun todos-edit-multiline-item ()
4794 "Edit current Todo item in Todos Edit mode.
4795 Use of newlines invokes `todos-indent' to insure compliance with
4796 the format of Diary entries."
4798 (todos-edit-multiline t))
4800 (defun todos-edit-multiline (&optional item)
4803 (let ((buffer-name todos-edit-buffer))
4806 (set-buffer (make-indirect-buffer
4807 (file-name-nondirectory todos-current-todos-file)
4810 (narrow-to-region (todos-item-start) (todos-item-end))
4813 (message "%s" (substitute-command-keys
4814 (concat "Type \\[todos-edit-quit] to check file format "
4815 "validity and return to Todos mode.\n")))))
4817 (defun todos-edit-quit ()
4818 "Return from Todos Edit mode to Todos mode.
4819 If the item contains hard line breaks, make sure the following
4820 lines are indented by `todos-indent-to-here' to conform to diary
4823 If the whole file was in Todos Edit mode, check before returning
4824 whether the file is still a valid Todos file and if so, also
4825 recalculate the Todos categories sexp, in case changes were made
4826 in the number or names of categories."
4828 (if (eq (buffer-size) (- (point-max) (point-min)))
4829 (when (todos-check-format)
4830 (todos-repair-categories-sexp))
4831 ;; Ensure lines following hard newlines are indented.
4832 (let ((item (replace-regexp-in-string
4833 "\\(\n\\)[^[:blank:]]"
4834 (concat "\n" (make-string todos-indent-to-here 32))
4835 (buffer-string) nil nil 1)))
4836 (delete-region (point-min) (point-max))
4839 ;; In case next buffer is not the one holding todos-current-todos-file.
4842 (defun todos-edit-item-header-1 (what &optional inc)
4843 "Function underlying commands to edit item date/time header.
4845 The argument WHAT (passed by invoking commands) specifies what
4846 part of the header to edit; possible values are these symbols:
4847 `date', to edit the year, month, and day of the date string;
4848 `time', to edit just the time string; `calendar', to select the
4849 date from the Calendar; `today', to set the date to today's date;
4850 `dayname', to set the date string to the name of a day or to
4851 change the day name; and `year', `month' or `day', to edit only
4852 these respective parts of the date string (`day' is the number of
4853 the given day of the month, and `month' is either the name of the
4854 given month or its number, depending on the value of
4855 `calendar-date-display-form').
4857 The optional argument INC is a positive or negative integer
4858 \(passed by invoking commands as a numerical prefix argument)
4859 that in conjunction with the WHAT values `year', `month' or
4860 `day', increments or decrements the specified date string
4861 component by the specified number of suitable units, i.e., years,
4862 months, or days, with automatic adjustment of the other date
4863 string components as necessary.
4865 If there are marked items, apply the same edit to all of these;
4866 otherwise, edit just the item at point."
4867 (let* ((cat (todos-current-category))
4868 (marked (assoc cat todos-categories-with-marks))
4870 (todos-date-from-calendar t)
4871 (buffer-read-only nil)
4872 ndate ntime year monthname month day
4873 dayname) ; Needed by calendar-date-display-form.
4875 (or (and marked (goto-char (point-min))) (todos-item-start))
4879 (while (not (todos-marked-item-p))
4880 (todos-forward-item)
4881 (and (eobp) (throw 'end nil))))
4882 (re-search-forward (concat todos-date-string-start "\\(?1:"
4884 "\\)\\(?2: " diary-time-regexp "\\)?"
4885 (regexp-quote todos-nondiary-end) "?")
4886 (line-end-position) t)
4887 (let* ((odate (match-string-no-properties 1))
4888 (otime (match-string-no-properties 2))
4889 (omonthname (match-string-no-properties 6))
4890 (omonth (match-string-no-properties 7))
4891 (oday (match-string-no-properties 8))
4892 (oyear (match-string-no-properties 9))
4893 (tmn-array todos-month-name-array)
4894 (mlist (append tmn-array nil))
4895 (tma-array todos-month-abbrev-array)
4896 (mablist (append tma-array nil))
4897 (yy (and oyear (unless (string= oyear "*")
4898 (string-to-number oyear))))
4899 (mm (or (and omonth (unless (string= omonth "*")
4900 (string-to-number omonth)))
4901 (1+ (- (length mlist)
4902 (length (or (member omonthname mlist)
4903 (member omonthname mablist)))))))
4904 (dd (and oday (unless (string= oday "*")
4905 (string-to-number oday)))))
4906 ;; If there are marked items, use only the first to set
4907 ;; header changes, and apply these to all marked items.
4911 (setq ndate (todos-read-date)))
4912 ((eq what 'calendar)
4913 (setq ndate (save-match-data (todos-set-date-from-calendar))))
4915 (setq ndate (calendar-date-string (calendar-current-date) t t)))
4917 (setq ndate (todos-read-dayname)))
4919 (setq ntime (save-match-data (todos-read-time)))
4920 (when (> (length ntime) 0)
4921 (setq ntime (concat " " ntime))))
4922 ;; When date string consists only of a day name,
4923 ;; passing other date components is a NOP.
4924 ((and (memq what '(year month day))
4925 (not (or oyear omonth oday))))
4928 monthname omonthname
4930 year (cond ((not current-prefix-arg)
4931 (todos-read-date 'year))
4932 ((string= oyear "*")
4933 (error "Cannot increment *"))
4935 (number-to-string (+ yy inc))))))
4939 (if (memq 'month calendar-date-display-form)
4942 (cond ((not current-prefix-arg)
4943 (todos-read-date 'month))
4944 ((or (string= omonth "*") (= mm 13))
4945 (error "Cannot increment *"))
4947 (let ((mminc (+ mm inc)))
4948 ;; Increment or decrement month by INC
4950 (setq mm (% mminc 12))
4951 ;; If result is 0, make month December.
4952 (setq mm (if (= mm 0) 12 (abs mm)))
4953 ;; Adjust year if necessary.
4954 (setq year (or (and (cond ((> mminc 12)
4955 (+ yy (/ mminc 12)))
4957 (- yy (/ mminc 12) 1))
4959 (number-to-string yy))
4961 ;; Return the changed numerical month as
4962 ;; a string or the corresponding month name.
4964 (number-to-string mm)
4965 (aref tma-array (1- mm))))))
4966 (let ((yy (string-to-number year)) ; 0 if year is "*".
4967 ;; When mm is 13 (corresponding to "*" as value
4968 ;; of month), this raises an args-out-of-range
4969 ;; error in calendar-last-day-of-month, so use 1
4970 ;; (corresponding to January) to get 31 days.
4971 (mm (if (= mm 13) 1 mm)))
4972 (if (> (string-to-number day)
4973 (calendar-last-day-of-month mm yy))
4974 (error "%s %s does not have %s days"
4975 (aref tmn-array (1- mm))
4976 (if (= mm 2) yy "") day))))
4980 monthname omonthname
4982 ((not current-prefix-arg)
4983 (todos-read-date 'day mm oyear))
4985 (error "Cannot increment *"))
4986 ((or (string= omonth "*") (string= omonthname "*"))
4987 (setq dd (+ dd inc))
4989 (error "A month cannot have more than 31 days")
4990 (number-to-string dd)))
4991 ;; Increment or decrement day by INC,
4992 ;; adjusting month and year if necessary
4993 ;; (if year is "*" assume current year to
4994 ;; calculate adjustment).
4996 (let* ((yy (or yy (calendar-extract-year
4997 (calendar-current-date))))
4998 (date (calendar-gregorian-from-absolute
4999 (+ (calendar-absolute-from-gregorian
5000 (list mm dd yy)) inc)))
5001 (adjmm (nth 0 date)))
5002 ;; Set year and month(name) to adjusted values.
5003 (unless (string= year "*")
5004 (setq year (number-to-string (nth 2 date))))
5006 (setq month (number-to-string adjmm))
5007 (setq monthname (aref tma-array (1- adjmm))))
5008 ;; Return changed numerical day as a string.
5009 (number-to-string (nth 1 date)))))))))
5010 ;; If new year, month or day date string components were
5011 ;; calculated, rebuild the whole date string from them.
5012 (when (memq what '(year month day))
5013 (if (or oyear omonth omonthname oday)
5014 (setq ndate (mapconcat 'eval calendar-date-display-form ""))
5015 (message "Cannot edit date component of empty date string")))
5016 (when ndate (replace-match ndate nil nil nil 1))
5017 ;; Add new time string to the header, if it was supplied.
5020 (replace-match ntime nil nil nil 2)
5021 (goto-char (match-end 1))
5023 (setq todos-date-from-calendar nil)
5025 ;; Apply the changes to the first marked item header to the
5026 ;; remaining marked items. If there are no marked items,
5029 (todos-forward-item)
5030 (goto-char (point-max))))))))
5032 (defun todos-edit-item-header ()
5033 "Interactively edit at least the date of item's date/time header.
5034 If user option `todos-always-add-time-string' is non-nil, also
5035 edit item's time string."
5037 (todos-edit-item-header-1 'date)
5038 (when todos-always-add-time-string
5039 (todos-edit-item-time)))
5041 (defun todos-edit-item-time ()
5042 "Interactively edit the time string of item's date/time header."
5044 (todos-edit-item-header-1 'time))
5046 (defun todos-edit-item-date-from-calendar ()
5047 "Interactively edit item's date using the Calendar."
5049 (todos-edit-item-header-1 'calendar))
5051 (defun todos-edit-item-date-to-today ()
5052 "Set item's date to today's date."
5054 (todos-edit-item-header-1 'today))
5056 (defun todos-edit-item-date-day-name ()
5057 "Replace item's date with the name of a day of the week."
5059 (todos-edit-item-header-1 'dayname))
5061 (defun todos-edit-item-date-year (&optional inc)
5062 "Interactively edit the year of item's date string.
5063 With prefix argument INC a positive or negative integer,
5064 increment or decrement the year by INC."
5066 (todos-edit-item-header-1 'year inc))
5068 (defun todos-edit-item-date-month (&optional inc)
5069 "Interactively edit the month of item's date string.
5070 With prefix argument INC a positive or negative integer,
5071 increment or decrement the month by INC."
5073 (todos-edit-item-header-1 'month inc))
5075 (defun todos-edit-item-date-day (&optional inc)
5076 "Interactively edit the day of the month of item's date string.
5077 With prefix argument INC a positive or negative integer,
5078 increment or decrement the day by INC."
5080 (todos-edit-item-header-1 'day inc))
5082 (defun todos-edit-item-diary-inclusion ()
5083 "Change diary status of one or more todo items in this category.
5084 That is, insert `todos-nondiary-marker' if the candidate items
5085 lack this marking; otherwise, remove it.
5087 If there are marked todo items, change the diary status of all
5088 and only these, otherwise change the diary status of the item at
5091 (let ((buffer-read-only)
5092 (marked (assoc (todos-current-category)
5093 todos-categories-with-marks)))
5096 (when marked (goto-char (point-min)))
5098 (if (todos-done-item-p)
5099 (throw 'stop (message "Done items cannot be edited"))
5100 (unless (and marked (not (todos-marked-item-p)))
5101 (let* ((beg (todos-item-start))
5102 (lim (save-excursion (todos-item-end)))
5103 (end (save-excursion
5104 (or (todos-time-string-matcher lim)
5105 (todos-date-string-matcher lim)))))
5106 (if (looking-at (regexp-quote todos-nondiary-start))
5109 (search-forward todos-nondiary-end (1+ end) t)
5111 (todos-update-count 'diary 1))
5113 (insert todos-nondiary-start)
5114 (goto-char (1+ end))
5115 (insert todos-nondiary-end)
5116 (todos-update-count 'diary -1)))))
5117 (unless marked (throw 'stop nil))
5118 (todos-forward-item)))))
5119 (todos-update-categories-sexp)))
5121 (defun todos-edit-category-diary-inclusion (arg)
5122 "Make all items in this category diary items.
5123 With prefix ARG, make all items in this category non-diary
5127 (goto-char (point-min))
5128 (let ((todo-count (todos-get-count 'todo))
5129 (diary-count (todos-get-count 'diary))
5133 (if (todos-done-item-p) ; We've gone too far.
5135 (let* ((beg (todos-item-start))
5136 (lim (save-excursion (todos-item-end)))
5137 (end (save-excursion
5138 (or (todos-time-string-matcher lim)
5139 (todos-date-string-matcher lim)))))
5141 (unless (looking-at (regexp-quote todos-nondiary-start))
5142 (insert todos-nondiary-start)
5143 (goto-char (1+ end))
5144 (insert todos-nondiary-end))
5145 (when (looking-at (regexp-quote todos-nondiary-start))
5147 (search-forward todos-nondiary-end (1+ end) t)
5148 (replace-match "")))))
5149 (todos-forward-item))
5150 (unless (if arg (zerop diary-count) (= diary-count todo-count))
5151 (todos-update-count 'diary (if arg
5153 (- todo-count diary-count))))
5154 (todos-update-categories-sexp)))))
5156 (defun todos-edit-item-diary-nonmarking ()
5157 "Change non-marking of one or more diary items in this category.
5158 That is, insert `diary-nonmarking-symbol' if the candidate items
5159 lack this marking; otherwise, remove it.
5161 If there are marked todo items, change the non-marking status of
5162 all and only these, otherwise change the non-marking status of
5165 (let ((buffer-read-only)
5166 (marked (assoc (todos-current-category)
5167 todos-categories-with-marks)))
5170 (when marked (goto-char (point-min)))
5172 (if (todos-done-item-p)
5173 (throw 'stop (message "Done items cannot be edited"))
5174 (unless (and marked (not (todos-marked-item-p)))
5176 (unless (looking-at (regexp-quote todos-nondiary-start))
5177 (if (looking-at (regexp-quote diary-nonmarking-symbol))
5179 (insert diary-nonmarking-symbol))))
5180 (unless marked (throw 'stop nil))
5181 (todos-forward-item)))))))
5183 (defun todos-edit-category-diary-nonmarking (arg)
5184 "Add `diary-nonmarking-symbol' to all diary items in this category.
5185 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
5186 items in this category."
5189 (goto-char (point-min))
5190 (let (buffer-read-only)
5193 (if (todos-done-item-p) ; We've gone too far.
5195 (unless (looking-at (regexp-quote todos-nondiary-start))
5197 (when (looking-at (regexp-quote diary-nonmarking-symbol))
5199 (unless (looking-at (regexp-quote diary-nonmarking-symbol))
5200 (insert diary-nonmarking-symbol))))
5201 (todos-forward-item)))))))
5203 (defun todos-set-item-priority (&optional item cat new arg)
5204 "Prompt for and set ITEM's priority in CATegory.
5206 Interactively, ITEM is the todo item at point, CAT is the current
5207 category, and the priority is a number between 1 and the number
5208 of items in the category. Non-interactively, non-nil NEW means
5209 ITEM is a new item and the lowest priority is one more than the
5210 number of items in CAT.
5212 The new priority is set either interactively by prompt or by a
5213 numerical prefix argument, or noninteractively by argument ARG,
5214 whose value can be either of the symbols `raise' or `lower',
5215 meaning to raise or lower the item's priority by one."
5216 (interactive) ;FIXME: Prefix arg?
5217 (unless (and (called-interactively-p 'any)
5218 (or (todos-done-item-p) (looking-at "^$")))
5219 (let* ((item (or item (todos-item-string)))
5220 (marked (todos-marked-item-p))
5221 (cat (or cat (cond ((eq major-mode 'todos-mode)
5222 (todos-current-category))
5223 ((eq major-mode 'todos-filtered-items-mode)
5225 (concat todos-date-string-start
5227 "\\( " diary-time-regexp "\\)?"
5228 (regexp-quote todos-nondiary-end)
5229 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
5231 (re-search-forward regexp1 nil t)
5232 (match-string-no-properties 1)))))))
5234 (todo (cond ((or (eq arg 'raise) (eq arg 'lower)
5235 (eq major-mode 'todos-filtered-items-mode))
5237 (let ((curstart (todos-item-start))
5239 (goto-char (point-min))
5240 (while (looking-at todos-item-start)
5241 (setq count (1+ count))
5242 (when (= (point) curstart) (setq curnum count))
5243 (todos-forward-item))
5245 ((eq major-mode 'todos-mode)
5246 (todos-get-count 'todo cat))))
5247 (maxnum (if new (1+ todo) todo))
5248 (prompt (format "Set item priority (1-%d): " maxnum))
5249 (priority (cond ((numberp current-prefix-arg)
5251 ((and (eq arg 'raise) (>= curnum 1))
5253 ((and (eq arg 'lower) (<= curnum maxnum))
5257 (unless (and priority
5258 (or (and (eq arg 'raise) (zerop priority))
5259 (and (eq arg 'lower) (> priority maxnum))))
5260 ;; When moving item to another category, show the category before
5261 ;; prompting for its priority.
5262 (unless (or arg (called-interactively-p 'any))
5263 (todos-category-number cat)
5264 ;; If done items in category are visible, keep them visible.
5265 (let ((done todos-show-with-done))
5266 (when (> (buffer-size) (- (point-max) (point-min)))
5268 (goto-char (point-min))
5269 (setq done (re-search-forward todos-done-string-start nil t))))
5270 (let ((todos-show-with-done done))
5271 (todos-category-select)))))
5272 ;; Prompt for priority only when the category has at least one todo item.
5274 (while (not priority)
5275 (setq candidate (read-number prompt))
5276 (setq prompt (when (or (< candidate 1) (> candidate maxnum))
5277 (format "Priority must be an integer between 1 and %d.\n"
5279 (unless prompt (setq priority candidate))))
5280 ;; In Top Priorities buffer, an item's priority can be changed
5281 ;; wrt items in another category, but not wrt items in the same
5283 (when (eq major-mode 'todos-filtered-items-mode)
5284 (let* ((regexp2 (concat todos-date-string-start todos-date-pattern
5285 "\\( " diary-time-regexp "\\)?"
5286 (regexp-quote todos-nondiary-end)
5287 "?\\(?1:" (regexp-quote cat) "\\)"))
5288 (end (cond ((< curnum priority)
5289 (save-excursion (todos-item-end)))
5290 ((> curnum priority)
5291 (save-excursion (todos-item-start)))))
5292 (match (save-excursion
5293 (cond ((< curnum priority)
5294 (todos-forward-item (1+ (- priority curnum)))
5295 (when (re-search-backward regexp2 end t)
5296 (match-string-no-properties 1)))
5297 ((> curnum priority)
5298 (todos-backward-item (- curnum priority))
5299 (when (re-search-forward regexp2 end t)
5300 (match-string-no-properties 1)))))))
5302 (error (concat "Cannot reprioritize items from the same "
5303 "category in this mode, only in Todos mode")))))
5304 ;; Interactively or with non-nil ARG, relocate the item within its
5306 (when (or arg (called-interactively-p 'any))
5307 (todos-remove-item))
5308 (goto-char (point-min))
5310 (unless (= priority 1)
5311 (todos-forward-item (1- priority))
5312 ;; When called from todos-item-undo and the highest priority
5313 ;; is chosen, this advances point to the first done item, so
5314 ;; move it up to the empty line above the done items
5316 (when (looking-back (concat "^"
5317 (regexp-quote todos-category-done) "\n"))
5318 (todos-backward-item))))
5319 (todos-insert-with-overlays item)
5320 ;; If item was marked, restore the mark.
5322 (let* ((ov (todos-prefix-overlay))
5323 (pref (overlay-get ov 'before-string)))
5324 (overlay-put ov 'before-string (concat todos-item-mark pref)))))))
5326 (defun todos-raise-item-priority ()
5327 "Raise priority of current item by moving it up by one item."
5329 (todos-set-item-priority nil nil nil 'raise))
5331 (defun todos-lower-item-priority ()
5332 "Lower priority of current item by moving it down by one item."
5334 (todos-set-item-priority nil nil nil 'lower))
5336 (defun todos-move-item (&optional file)
5337 "Move at least one todo or done item to another category.
5338 If there are marked items, move all of these; otherwise, move
5341 With prefix argument FILE, prompt for a specific Todos file and
5342 choose (with TAB completion) a category in it to move the item or
5343 items to; otherwise, choose and move to any category in either
5344 the current Todos file or one of the files in
5345 `todos-category-completions-files'. If the chosen category is
5346 not an existing categories, then it is created and the item(s)
5347 become(s) the first entry/entries in that category.
5349 With moved Todo items, prompt to set the priority in the category
5350 moved to (with multiple todos items, the one that had the highest
5351 priority in the category moved from gets the new priority and the
5352 rest of the moved todo items are inserted in sequence below it).
5353 Moved done items are appended to the end of the done items
5354 section in the category moved to."
5356 (let* ((cat1 (todos-current-category))
5357 (marked (assoc cat1 todos-categories-with-marks)))
5358 ;; NOP if point is not on an item and there are no marked items.
5359 (unless (and (looking-at "^$")
5361 (let* ((buffer-read-only)
5362 (file1 todos-current-todos-file)
5363 (num todos-category-number)
5364 (item (todos-item-string))
5365 (diary-item (todos-diary-item-p))
5366 (done-item (and (todos-done-item-p) (concat item "\n")))
5367 (omark (save-excursion (todos-item-start) (point-marker)))
5371 ov cat+file cat2 file2 moved nmark todo-items done-items)
5375 (setq ov (make-overlay (save-excursion (todos-item-start))
5376 (save-excursion (todos-item-end))))
5377 (overlay-put ov 'face 'todos-search))
5378 (setq cat+file (let ((pl (if (and marked (> (cdr marked) 1))
5380 (todos-read-category (concat "Move item" pl
5384 file2 (cdr cat+file)))
5385 (if ov (delete-overlay ov)))
5386 (set-buffer (find-buffer-visiting file1))
5389 (goto-char (point-min))
5391 (when (todos-marked-item-p)
5392 (if (todos-done-item-p)
5393 (setq done-items (concat done-items
5394 (todos-item-string) "\n")
5396 (setq todo-items (concat todo-items
5397 (todos-item-string) "\n")
5399 (when (todos-diary-item-p)
5400 (setq diary (1+ diary)))))
5401 (todos-forward-item))
5402 ;; Chop off last newline of multiple todo item string,
5403 ;; since it will be reinserted when setting priority
5404 ;; (but with done items priority is not set, so keep
5407 (setq todo-items (substring todo-items 0 -1))))
5408 (if (todos-done-item-p)
5411 (when (todos-diary-item-p) (setq diary 1))))
5412 (set-window-buffer (selected-window)
5413 (set-buffer (find-file-noselect file2 'nowarn)))
5416 (when (or todo-items (and item (not done-item)))
5417 (todos-set-item-priority (or todo-items item) cat2 t))
5418 ;; Move done items en bloc to end of done item section.
5419 (when (or done-items done-item)
5420 (todos-category-number cat2)
5422 (goto-char (point-min))
5423 (re-search-forward (concat "^" (regexp-quote
5424 (concat todos-category-beg cat2))
5427 (goto-char (if (re-search-forward
5428 (concat "^" (regexp-quote todos-category-beg))
5432 (insert (or done-items done-item)))
5435 ;; Move succeeded, so remove item from starting category,
5436 ;; update item counts and display the category containing
5439 (setq nmark (point-marker))
5440 (when todo (todos-update-count 'todo todo))
5441 (when diary (todos-update-count 'diary diary))
5442 (when done (todos-update-count 'done done))
5443 (todos-update-categories-sexp)
5444 (with-current-buffer (find-buffer-visiting file1)
5453 (concat "^" (regexp-quote todos-category-beg)) nil t)
5456 (setq end (if (re-search-forward
5457 (concat "^" (regexp-quote
5458 todos-category-beg)) nil t)
5462 (while (< (point) end)
5463 (if (todos-marked-item-p)
5465 (todos-forward-item)))
5466 (setq todos-categories-with-marks
5467 (assq-delete-all cat1 todos-categories-with-marks)))
5468 (if ov (delete-overlay ov))
5469 (todos-remove-item))))
5470 (when todo (todos-update-count 'todo (- todo) cat1))
5471 (when diary (todos-update-count 'diary (- diary) cat1))
5472 (when done (todos-update-count 'done (- done) cat1))
5473 (todos-update-categories-sexp))
5474 (set-window-buffer (selected-window)
5475 (set-buffer (find-file-noselect file2 'nowarn)))
5476 (setq todos-category-number (todos-category-number cat2))
5477 (let ((todos-show-with-done (or done-items done-item)))
5478 (todos-category-select))
5480 ;; If item is moved to end of category, make sure the
5481 ;; items above it are displayed in the window.
5483 ;; User quit before setting priority of todo item(s), so
5484 ;; return to starting category.
5486 (todos-category-number cat1)
5487 (todos-category-select)
5488 (goto-char omark))))))))
5490 (defun todos-item-done (&optional arg)
5491 "Tag a todo item in this category as done and relocate it.
5493 With prefix argument ARG prompt for a comment and append it to
5494 the done item; this is only possible if there are no marked
5495 items. If there are marked items, tag all of these with
5496 `todos-done-string' plus the current date and, if
5497 `todos-always-add-time-string' is non-nil, the current time;
5498 otherwise, just tag the item at point. Items tagged as done are
5499 relocated to the category's (by default hidden) done section. If
5500 done items are visible on invoking this command, they remain
5503 (let* ((cat (todos-current-category))
5504 (marked (assoc cat todos-categories-with-marks)))
5505 (unless (or (todos-done-item-p)
5506 ;; Point is between todo and done items.
5507 (and (looking-at "^$") (not marked)))
5508 (let* ((date-string (calendar-date-string (calendar-current-date) t t))
5509 (time-string (if todos-always-add-time-string
5510 (concat " " (substring (current-time-string) 11 16))
5512 (done-prefix (concat "[" todos-done-string date-string time-string
5514 (comment (and arg (not marked) (read-string "Enter a comment: ")))
5517 (show-done (save-excursion
5518 (goto-char (point-min))
5519 (re-search-forward todos-done-string-start nil t)))
5522 (and marked (goto-char (point-min)))
5524 ;; Stop looping when we hit the empty line below the last
5525 ;; todo item (this is eobp if only done items are hidden).
5526 (while (not (looking-at "^$")) ;(not (eobp))
5527 (if (or (not marked) (and marked (todos-marked-item-p)))
5529 (setq item (todos-item-string))
5530 (setq done-item (cond (marked
5531 (concat done-item done-prefix item "\n"))
5533 (concat done-prefix item " ["
5534 todos-comment-string
5537 (concat done-prefix item))))
5538 (setq item-count (1+ item-count))
5539 (when (todos-diary-item-p)
5540 (setq diary-count (1+ diary-count)))
5542 (unless marked (throw 'done nil)))
5543 (todos-forward-item))))
5545 ;; Chop off last newline of done item string.
5546 (setq done-item (substring done-item 0 -1))
5547 (setq todos-categories-with-marks
5548 (assq-delete-all cat todos-categories-with-marks)))
5552 (concat "^" (regexp-quote todos-category-done)) nil t)
5554 (insert done-item "\n"))
5555 (todos-update-count 'todo (- item-count))
5556 (todos-update-count 'done item-count)
5557 (todos-update-count 'diary (- diary-count))
5558 (todos-update-categories-sexp)
5559 (let ((todos-show-with-done show-done))
5560 (todos-category-select))))))
5562 (defun todos-done-item-add-edit-or-delete-comment (&optional arg)
5563 "Add a comment to this done item or edit an existing comment.
5564 With prefix ARG delete an existing comment."
5566 (when (todos-done-item-p)
5567 (let ((item (todos-item-string))
5568 (end (save-excursion (todos-item-end)))
5569 comment buffer-read-only)
5572 (if (re-search-forward (concat " \\["
5573 (regexp-quote todos-comment-string)
5574 ": \\([^]]+\\)\\]") end t)
5576 (when (y-or-n-p "Delete comment? ")
5577 (delete-region (match-beginning 0) (match-end 0)))
5578 (setq comment (read-string "Edit comment: "
5579 (cons (match-string 1) 1)))
5580 (replace-match comment nil nil nil 1))
5581 (setq comment (read-string "Enter a comment: "))
5583 (insert " [" todos-comment-string ": " comment "]"))))))
5585 (defun todos-item-undo ()
5586 "Restore this done item to the todo section of this category.
5587 If done item has a comment, ask whether to omit the comment from
5590 (let* ((cat (todos-current-category))
5591 (marked (assoc cat todos-categories-with-marks)))
5592 (when (or marked (todos-done-item-p))
5593 (let ((buffer-read-only)
5594 (bufmod (buffer-modified-p))
5596 (orig-mrk (progn (todos-item-start) (point-marker)))
5597 (orig-item (todos-item-string))
5601 start end item undone)
5602 (and marked (goto-char (point-min)))
5605 (if (or (not marked) (and marked (todos-marked-item-p)))
5606 (if (not (todos-done-item-p))
5607 (error "Only done items can be undone")
5609 ;; Find the end of the date string added upon tagging item as
5611 (setq start (search-forward "] "))
5612 (setq item-count (1+ item-count))
5613 (unless (looking-at (regexp-quote todos-nondiary-start))
5614 (setq diary-count (1+ diary-count)))
5615 (setq end (save-excursion (todos-item-end)))
5616 ;; Ask (once) whether to omit done item's comment. If
5617 ;; affirmed, omit subsequent comments without asking.
5618 (when (re-search-forward
5619 (concat " \\[" (regexp-quote todos-comment-string)
5620 ": [^]]+\\]") end t)
5621 (if (eq first 'first)
5623 (if (eq todos-undo-item-omit-comment 'ask)
5625 "Omit comment from restored item? ")
5627 (when todos-undo-item-omit-comment 'omit)))
5629 (when (eq first 'omit)
5630 (delete-region (match-beginning 0) (match-end 0))
5631 (setq end (point))))
5632 (setq item (concat item
5633 (buffer-substring-no-properties start end)
5634 (when marked "\n")))
5636 (unless marked (throw 'done nil)))
5637 (todos-forward-item))))
5640 (setq todos-categories-with-marks
5641 (assq-delete-all cat todos-categories-with-marks))
5642 ;; Insert undone items that were marked at end of todo item list.
5643 (goto-char (point-min))
5644 (re-search-forward (concat "^" (regexp-quote todos-category-done))
5648 (todos-update-count 'todo item-count)
5649 (todos-update-count 'done (- item-count))
5650 (when diary-count (todos-update-count 'diary diary-count))
5651 (todos-update-categories-sexp)
5652 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5653 (todos-category-select)))
5654 ;; With an unmarked undone item, prompt for its priority. If user
5655 ;; cancels before setting new priority, then leave the done item
5659 (todos-set-item-priority item (todos-current-category) t)
5661 (todos-update-count 'todo 1)
5662 (todos-update-count 'done -1)
5663 (and (todos-diary-item-p) (todos-update-count 'diary 1))
5664 (todos-update-categories-sexp)
5665 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5666 (todos-category-select)))
5668 (let ((todos-show-with-done t))
5670 (goto-char orig-mrk)
5671 (todos-insert-with-overlays orig-item)
5672 (set-buffer-modified-p bufmod)
5673 (todos-category-select))
5674 (goto-char opoint))))
5675 (set-marker orig-mrk nil)))))
5677 (defun todos-archive-done-item (&optional all)
5678 "Archive at least one done item in this category.
5680 If there are marked done items (and no marked todo items),
5681 archive all of these; otherwise, with non-nil argument ALL,
5682 archive all done items in this category; otherwise, archive the
5685 If the archive of this file does not exist, it is created. If
5686 this category does not exist in the archive, it is created."
5688 (when (eq major-mode 'todos-mode)
5689 (if (and all (zerop (todos-get-count 'done)))
5690 (message "No done items in this category")
5692 (let* ((cat (todos-current-category))
5693 (tbuf (current-buffer))
5694 (marked (assoc cat todos-categories-with-marks))
5695 (afile (concat (file-name-sans-extension
5696 todos-current-todos-file) ".toda"))
5697 (archive (if (file-exists-p afile)
5698 (find-file-noselect afile t)
5699 (get-buffer-create afile)))
5700 (item (and (todos-done-item-p) (concat (todos-item-string) "\n")))
5702 (opoint (unless (todos-done-item-p) (point)))
5703 marked-items beg end all-done
5708 (goto-char (point-min))
5710 (when (todos-marked-item-p)
5711 (if (not (todos-done-item-p))
5712 (throw 'end (message "Only done items can be archived"))
5714 (concat marked-items (todos-item-string) "\n"))
5715 (setq count (1+ count))))
5716 (todos-forward-item))))
5718 (if (y-or-n-p "Archive all done items in this category? ")
5721 (goto-char (point-min))
5724 (re-search-forward todos-done-string-start nil t)
5725 (match-beginning 0))
5726 end (if (re-search-forward
5727 (concat "^" (regexp-quote todos-category-beg))
5731 all-done (buffer-substring-no-properties beg end)
5732 count (todos-get-count 'done))
5733 ;; Restore starting point, unless it was on a done
5734 ;; item, since they will all be deleted.
5735 (when opoint (goto-char opoint))))
5737 (if (not (or marked all item))
5738 (throw 'end (message "Only done items can be archived"))
5739 (with-current-buffer archive
5740 (unless buffer-file-name (erase-buffer))
5741 (let (buffer-read-only)
5743 (goto-char (point-min))
5744 (if (and (re-search-forward
5745 (concat "^" (regexp-quote
5746 (concat todos-category-beg cat)) "$")
5748 (re-search-forward (regexp-quote todos-category-done)
5750 ;; Start of done items section in existing category.
5752 (todos-add-category nil cat)
5753 ;; Start of done items section in new category.
5754 (goto-char (point-max)))
5755 (insert (cond (marked marked-items)
5758 (todos-update-count 'done (if (or marked all) count 1) cat)
5759 (todos-update-categories-sexp)
5760 ;; If archive is new, save to file now (using write-region in
5761 ;; order not to get prompted for file to save to), to let
5762 ;; auto-mode-alist take effect below.
5763 (unless buffer-file-name
5764 (write-region nil nil afile)
5766 (with-current-buffer tbuf
5768 ;; If we're archiving all done items, can't
5769 ;; first archive item point was on, since
5770 ;; that will short-circuit the rest.
5771 (and item (not all)))
5772 (and marked (goto-char (point-min)))
5775 (if (or (and marked (todos-marked-item-p)) item)
5778 (todos-update-count 'done -1)
5779 (todos-update-count 'archived 1)
5780 ;; Don't leave point below last item.
5781 (and item (bolp) (eolp) (< (point-min) (point-max))
5782 (todos-backward-item))
5784 (throw 'done (setq item nil))))
5785 (todos-forward-item)))))
5789 ;; Make sure done items are accessible.
5791 (remove-overlays beg end)
5792 (delete-region beg end)
5793 (todos-update-count 'done (- count))
5794 (todos-update-count 'archived count)))))
5796 (setq todos-categories-with-marks
5797 (assq-delete-all cat todos-categories-with-marks)))
5798 (todos-update-categories-sexp)
5799 (todos-prefix-overlays)))
5801 (todos-category-number cat)
5802 (todos-category-select)
5803 (split-window-below)
5804 (set-window-buffer (selected-window) tbuf)
5805 ;; Make todo file current to select category.
5806 (find-file (buffer-file-name tbuf))
5807 ;; Make sure done item separator is hidden (if done items
5808 ;; were initially visible).
5809 (let (todos-show-with-done) (todos-category-select)))))))
5811 (defun todos-archive-category-done-items ()
5812 "Move all done items in this category to its archive."
5814 (todos-archive-done-item t))
5816 (defun todos-unarchive-items (&optional all)
5817 "Unarchive at least one item in this archive category.
5819 If there are marked items, unarchive all of these; otherwise,
5820 with non-nil argument ALL, unarchive all items in this category;
5821 otherwise, unarchive the item at point.
5823 Unarchived items are restored as done items to the corresponding
5824 category in the Todos file, inserted at the end of done section.
5825 If all items in the archive category were restored, the category
5826 is deleted from the archive. If this was the only category in the
5827 archive, the archive file is deleted."
5829 (when (eq major-mode 'todos-archive-mode)
5831 (let* ((cat (todos-current-category))
5832 (tbuf (find-file-noselect
5833 (concat (file-name-sans-extension todos-current-todos-file)
5835 (marked (assoc cat todos-categories-with-marks))
5836 (item (concat (todos-item-string) "\n"))
5837 (all-items (when all (buffer-substring-no-properties
5838 (point-min) (point-max))))
5839 (all-count (when all (todos-get-count 'done)))
5840 marked-items marked-count
5844 (goto-char (point-min))
5846 (when (todos-marked-item-p)
5847 (concat marked-items (todos-item-string) "\n")
5848 (setq marked-count (1+ marked-count)))
5849 (todos-forward-item))))
5850 ;; Restore items to end of category's done section and update counts.
5851 (with-current-buffer tbuf
5852 (let (buffer-read-only)
5854 (goto-char (point-min))
5855 (re-search-forward (concat "^" (regexp-quote
5856 (concat todos-category-beg cat)) "$")
5858 ;; Go to end of category's done section.
5859 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
5861 (goto-char (match-beginning 0))
5862 (goto-char (point-max)))
5864 (insert marked-items)
5865 (todos-update-count 'done marked-count cat)
5866 (todos-update-count 'archived (- marked-count) cat))
5869 (todos-update-count 'done all-count cat)
5870 (todos-update-count 'archived (- all-count) cat))
5873 (todos-update-count 'done 1 cat)
5874 (todos-update-count 'archived -1 cat)))
5875 (todos-update-categories-sexp)))
5876 ;; Delete restored items from archive.
5877 (cond ((or marked item)
5878 (and marked (goto-char (point-min)))
5881 (if (or (and marked (todos-marked-item-p)) item)
5884 ;; Don't leave point below last item.
5885 (and item (bolp) (eolp) (< (point-min) (point-max))
5886 (todos-backward-item))
5888 (throw 'done (setq item nil))))
5889 (todos-forward-item))))
5890 (todos-update-count 'done (if marked (- marked-count) -1) cat))
5892 (remove-overlays (point-min) (point-max))
5893 (delete-region (point-min) (point-max))))
5894 ;; If that was the last category in the archive, delete the whole file.
5895 (if (= (length todos-categories) 1)
5897 (delete-file todos-current-todos-file)
5898 ;; Don't bother confirming killing the archive buffer.
5899 (set-buffer-modified-p nil)
5901 ;; Otherwise, if the archive category is now empty, delete it.
5902 (when (eq (point-min) (point-max))
5904 (let ((beg (re-search-backward
5905 (concat "^" (regexp-quote todos-category-beg) cat "$")
5907 (end (if (re-search-forward
5908 (concat "^" (regexp-quote todos-category-beg))
5912 (remove-overlays beg end)
5913 (delete-region beg end)
5914 (setq todos-categories (delete (assoc cat todos-categories)
5916 (todos-update-categories-sexp))))
5917 ;; Visit category in Todos file and show restored done items.
5918 (let ((tfile (buffer-file-name tbuf))
5919 (todos-show-with-done t))
5920 (set-window-buffer (selected-window)
5921 (set-buffer (find-file-noselect tfile)))
5922 (todos-category-number cat)
5924 (message "Items unarchived."))))))
5926 (defun todos-unarchive-category ()
5927 "Unarchive all items in this category. See `todos-unarchive-items'."
5929 (todos-unarchive-items t))
5933 ;;; todos.el ends here
5935 ;; FIXME: remove when part of Emacs
5936 ;; ---------------------------------------------------------------------------
5937 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
5938 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
5939 (add-to-list 'auto-mode-alist '("\\.todt\\'" . todos-filtered-items-mode))
5941 ;;; Addition to calendar.el
5942 ;; FIXME: autoload when key-binding is defined in calendar.el
5943 (defun todos-insert-item-from-calendar (&optional arg)
5946 (setq todos-date-from-calendar
5947 (calendar-date-string (calendar-cursor-to-date t) t t))
5950 (todos-insert-item arg nil nil todos-date-from-calendar))
5952 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar)
5954 ;;; necessitated adaptations to diary-lib.el
5956 ;; (defun diary-goto-entry (button)
5957 ;; "Jump to the diary entry for the BUTTON at point."
5958 ;; (let* ((locator (button-get button 'locator))
5959 ;; (marker (car locator))
5960 ;; markbuf file opoint)
5961 ;; ;; If marker pointing to diary location is valid, use that.
5962 ;; (if (and marker (setq markbuf (marker-buffer marker)))
5964 ;; (pop-to-buffer markbuf)
5965 ;; (goto-char (marker-position marker)))
5966 ;; ;; Marker is invalid (eg buffer has been killed, as is the case with
5967 ;; ;; included diary files).
5968 ;; (or (and (setq file (cadr locator))
5969 ;; (file-exists-p file)
5970 ;; (find-file-other-window file)
5972 ;; (when (eq major-mode (default-value 'major-mode)) (diary-mode))
5973 ;; (when (eq major-mode 'todos-mode) (widen))
5974 ;; (goto-char (point-min))
5975 ;; (when (re-search-forward (format "%s.*\\(%s\\)"
5976 ;; (regexp-quote (nth 2 locator))
5977 ;; (regexp-quote (nth 3 locator)))
5979 ;; (goto-char (match-beginning 1))
5980 ;; (when (eq major-mode 'todos-mode)
5981 ;; (setq opoint (point))
5982 ;; (re-search-backward (concat "^"
5983 ;; (regexp-quote todos-category-beg)
5986 ;; (todos-category-number (match-string 1))
5987 ;; (todos-category-select)
5988 ;; (goto-char opoint)))))
5989 ;; (message "Unable to locate this diary entry")))))