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 (setq todos-done-separator
(todos-done-separator))
267 (when (= 1 (length value
))
268 (todos-reset-done-separator sep
))))
270 (defcustom todos-done-string
"DONE "
271 "Identifying string appended to the front of done todos items."
273 :initialize
'custom-initialize-default
274 :set
'todos-reset-done-string
275 :group
'todos-mode-display
)
277 (defun todos-reset-done-string (symbol value
)
278 "The :set function for user option `todos-done-string'."
279 (let ((oldvalue (symbol-value symbol
))
280 (files (append todos-files todos-archives
)))
281 (custom-set-default symbol value
)
282 ;; Need to reset this to get font-locking right.
283 (setq todos-done-string-start
284 (concat "^\\[" (regexp-quote todos-done-string
)))
285 (when (not (equal value oldvalue
))
287 (with-current-buffer (find-file-noselect f
)
288 (let (buffer-read-only)
290 (goto-char (point-min))
292 (if (re-search-forward
293 (concat "^" (regexp-quote todos-nondiary-start
)
294 "\\(" (regexp-quote oldvalue
) "\\)")
296 (replace-match value t t nil
1)
298 (todos-category-select)))))))
300 (defcustom todos-comment-string
"COMMENT"
301 "String inserted before optional comment appended to done item."
303 :initialize
'custom-initialize-default
304 :set
'todos-reset-comment-string
305 :group
'todos-mode-display
)
307 (defun todos-reset-comment-string (symbol value
)
308 "The :set function for user option `todos-comment-string'."
309 (let ((oldvalue (symbol-value symbol
))
310 (files (append todos-files todos-archives
)))
311 (custom-set-default symbol value
)
312 (when (not (equal value oldvalue
))
314 (with-current-buffer (find-file-noselect f
)
315 (let (buffer-read-only)
318 (goto-char (point-min))
320 (if (re-search-forward
322 "\\[\\(" (regexp-quote oldvalue
) "\\): [^]]*\\]")
324 (replace-match value t t nil
1)
326 (todos-category-select))))))))
328 (defcustom todos-show-with-done nil
329 "Non-nil to display done items in all categories."
331 :group
'todos-mode-display
)
333 (defun todos-mode-line-control (cat)
334 "Return a mode line control for Todos buffers.
335 Argument CAT is the name of the current Todos category.
336 This function is the value of the user variable
337 `todos-mode-line-function'."
338 (let ((file (todos-short-file-name todos-current-todos-file
)))
339 (format "%s category %d: %s" file todos-category-number cat
)))
341 (defcustom todos-mode-line-function
'todos-mode-line-control
342 "Function that returns a mode line control for Todos buffers.
343 The function expects one argument holding the name of the current
344 Todos category. The resulting control becomes the local value of
345 `mode-line-buffer-identification' in each Todos buffer."
347 :group
'todos-mode-display
)
349 (defcustom todos-skip-archived-categories nil
350 "Non-nil to skip categories with only archived items when browsing.
352 Moving by category todos or archive file (with
353 \\[todos-forward-category] and \\[todos-backward-category]) skips
354 categories that contain only archived items. Other commands
355 still recognize these categories. In Todos Categories
356 mode (reached with \\[todos-display-categories]) these categories
357 shown in `todos-archived-only' face and clicking them in Todos
358 Categories mode visits the archived categories."
360 :group
'todos-mode-display
)
362 (defcustom todos-highlight-item nil
363 "Non-nil means highlight items at point."
365 :initialize
'custom-initialize-default
366 :set
'todos-reset-highlight-item
367 :group
'todos-mode-display
)
369 (defun todos-reset-highlight-item (symbol value
)
370 "The :set function for `todos-highlight-item'."
371 (let ((oldvalue (symbol-value symbol
))
372 (files (append todos-files todos-archives
)))
373 (custom-set-default symbol value
)
374 (when (not (equal value oldvalue
))
376 (let ((buf (find-buffer-visiting f
)))
378 (with-current-buffer buf
382 (hl-line-mode -
1)))))))))
384 (defcustom todos-wrap-lines t
385 "Non-nil to wrap long lines via `todos-line-wrapping-function'."
386 :group
'todos-mode-display
389 (defcustom todos-line-wrapping-function
'todos-wrap-and-indent
390 "Line wrapping function used with non-nil `todos-wrap-lines'."
391 :group
'todos-mode-display
394 (defun todos-wrap-and-indent ()
395 "Use word wrapping on long lines and indent with a wrap prefix.
396 The amount of indentation is given by user option
397 `todos-indent-to-here'."
398 (set (make-local-variable 'word-wrap
) t
)
399 (set (make-local-variable 'wrap-prefix
) (make-string todos-indent-to-here
32))
400 (unless (member '(continuation) fringe-indicator-alist
)
401 (push '(continuation) fringe-indicator-alist
)))
403 (defcustom todos-indent-to-here
6
404 "Number of spaces `todos-line-wrapping-function' indents to."
405 :type
'(integer :validate
407 (unless (> (widget-value widget
) 0)
408 (widget-put widget
:error
409 "Invalid value: must be a positive integer")
413 (defun todos-indent ()
414 "Indent from point to `todos-indent-to-here'."
415 (indent-to todos-indent-to-here todos-indent-to-here
))
417 ;; ---------------------------------------------------------------------------
418 ;;; Item insertion options
420 (defgroup todos-item-insertion nil
421 "User options for adding new todo items."
425 (defcustom todos-include-in-diary nil
426 "Non-nil to allow new Todo items to be included in the diary."
428 :group
'todos-item-insertion
)
430 (defcustom todos-diary-nonmarking nil
431 "Non-nil to insert new Todo diary items as nonmarking by default.
432 This appends `diary-nonmarking-symbol' to the front of an item on
433 insertion provided it doesn't begin with `todos-nondiary-marker'."
435 :group
'todos-item-insertion
)
437 (defcustom todos-nondiary-marker
'("[" "]")
438 "List of strings surrounding item date to block diary inclusion.
439 The first string is inserted before the item date and must be a
440 non-empty string that does not match a diary date in order to
441 have its intended effect. The second string is inserted after
443 :type
'(list string string
)
444 :group
'todos-item-insertion
445 :initialize
'custom-initialize-default
446 :set
'todos-reset-nondiary-marker
)
448 (defun todos-reset-nondiary-marker (symbol value
)
449 "The :set function for user option `todos-nondiary-marker'."
450 (let ((oldvalue (symbol-value symbol
))
451 (files (append todos-files todos-archives
)))
452 (custom-set-default symbol value
)
453 ;; Need to reset these to get font-locking right.
454 (setq todos-nondiary-start
(nth 0 todos-nondiary-marker
)
455 todos-nondiary-end
(nth 1 todos-nondiary-marker
)
456 todos-date-string-start
457 ;; See comment in defvar of `todos-date-string-start'.
458 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
459 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
460 (when (not (equal value oldvalue
))
462 (with-current-buffer (find-file-noselect f
)
463 (let (buffer-read-only)
465 (goto-char (point-min))
467 (if (re-search-forward
468 (concat "^\\(" todos-done-string-start
"[^][]+] \\)?"
469 "\\(?1:" (regexp-quote (car oldvalue
))
470 "\\)" todos-date-pattern
"\\( "
471 diary-time-regexp
"\\)?\\(?2:"
472 (regexp-quote (cadr oldvalue
)) "\\)")
475 (replace-match (nth 0 value
) t t nil
1)
476 (replace-match (nth 1 value
) t t nil
2))
478 (todos-category-select)))))))
480 (defcustom todos-always-add-time-string nil
481 "Non-nil adds current time to a new item's date header by default.
482 When the Todos insertion commands have a non-nil \"maybe-notime\"
483 argument, this reverses the effect of
484 `todos-always-add-time-string': if t, these commands omit the
485 current time, if nil, they include it."
487 :group
'todos-item-insertion
)
489 (defcustom todos-use-only-highlighted-region t
490 "Non-nil to enable inserting only highlighted region as new item."
492 :group
'todos-item-insertion
)
494 ;; ---------------------------------------------------------------------------
495 ;;; Todos Filter Items mode options
497 (defgroup todos-filtered nil
498 "User options for Todos Filter Items mode."
502 (defcustom todos-filtered-items-buffer
"Todos filtered items"
503 "Initial name of buffer in Todos Filter Items mode."
505 :group
'todos-filtered
)
507 (defcustom todos-top-priorities-buffer
"Todos top priorities"
508 "Buffer type string for `todos-filtered-buffer-name'."
510 :group
'todos-filtered
)
512 (defcustom todos-diary-items-buffer
"Todos diary items"
513 "Buffer type string for `todos-filtered-buffer-name'."
515 :group
'todos-filtered
)
517 (defcustom todos-regexp-items-buffer
"Todos regexp items"
518 "Buffer type string for `todos-filtered-buffer-name'."
520 :group
'todos-filtered
)
522 (defcustom todos-priorities-rules nil
523 "List of rules giving how many items `todos-top-priorities' shows.
524 This variable should be set interactively by
525 `\\[todos-set-top-priorities-in-file]' or
526 `\\[todos-set-top-priorities-in-category]'.
528 Each rule is a list of the form (FILE NUM ALIST), where FILE is a
529 member of `todos-files', NUM is a number specifying the default
530 number of top priority items for each category in that file, and
531 ALIST, when non-nil, consists of conses of a category name in
532 FILE and a number specifying the default number of top priority
533 items in that category, which overrides NUM."
535 :group
'todos-filtered
)
537 (defcustom todos-show-priorities
1
538 "Default number of top priorities shown by `todos-top-priorities'."
540 :group
'todos-filtered
)
542 (defcustom todos-filter-files nil
543 "List of default files for multifile item filtering."
544 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
545 (mapcar 'todos-short-file-name
546 (funcall todos-files-function
))))
547 :group
'todos-filtered
)
549 (defun todos-reevaluate-filter-files-defcustom ()
550 "Reevaluate defcustom of `todos-filter-files'.
551 Called after adding or deleting a Todos file."
552 (eval (defcustom todos-filter-files nil
553 "List of files for multifile item filtering."
554 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
555 (mapcar 'todos-short-file-name
556 (funcall todos-files-function
))))
559 (defcustom todos-filter-done-items nil
560 "Non-nil to include done items when processing regexp filters.
561 Done items from corresponding archive files are also included."
563 :group
'todos-filtered
)
565 ;; ---------------------------------------------------------------------------
566 ;;; Todos Categories mode options
568 (defgroup todos-categories nil
569 "User options for Todos Categories mode."
573 (defcustom todos-categories-category-label
"Category"
574 "Category button label in Todos Categories mode."
576 :group
'todos-categories
)
578 (defcustom todos-categories-todo-label
"Todo"
579 "Todo button label in Todos Categories mode."
581 :group
'todos-categories
)
583 (defcustom todos-categories-diary-label
"Diary"
584 "Diary button label in Todos Categories mode."
586 :group
'todos-categories
)
588 (defcustom todos-categories-done-label
"Done"
589 "Done button label in Todos Categories mode."
591 :group
'todos-categories
)
593 (defcustom todos-categories-archived-label
"Archived"
594 "Archived button label in Todos Categories mode."
596 :group
'todos-categories
)
598 (defcustom todos-categories-totals-label
"Totals"
599 "String to label total item counts in Todos Categories mode."
601 :group
'todos-categories
)
603 (defcustom todos-categories-number-separator
" | "
604 "String between number and category in Todos Categories mode.
605 This separates the number from the category name in the default
606 categories display according to priority."
608 :group
'todos-categories
)
610 (defcustom todos-categories-align
'center
611 "Alignment of category names in Todos Categories mode."
612 :type
'(radio (const left
) (const center
) (const right
))
613 :group
'todos-categories
)
615 ;; ---------------------------------------------------------------------------
616 ;;; Faces and font-lock matcher functions
618 (defgroup todos-faces nil
619 "Faces for the Todos modes."
623 (defface todos-prefix-string
624 ;; '((t :inherit font-lock-constant-face))
625 '((((class grayscale
) (background light
))
626 (:foreground
"LightGray" :weight bold
:underline t
))
627 (((class grayscale
) (background dark
))
628 (:foreground
"Gray50" :weight bold
:underline t
))
629 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
630 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
631 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
632 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
633 (((class color
) (min-colors 8)) (:foreground
"magenta"))
634 (t (:weight bold
:underline t
)))
635 "Face for Todos prefix or numerical priority string."
638 (defface todos-top-priority
639 ;; bold font-lock-comment-face
640 '((default :weight bold
)
641 (((class grayscale
) (background light
)) :foreground
"DimGray" :slant italic
)
642 (((class grayscale
) (background dark
)) :foreground
"LightGray" :slant italic
)
643 (((class color
) (min-colors 88) (background light
)) :foreground
"Firebrick")
644 (((class color
) (min-colors 88) (background dark
)) :foreground
"chocolate1")
645 (((class color
) (min-colors 16) (background light
)) :foreground
"red")
646 (((class color
) (min-colors 16) (background dark
)) :foreground
"red1")
647 (((class color
) (min-colors 8) (background light
)) :foreground
"red")
648 (((class color
) (min-colors 8) (background dark
)) :foreground
"yellow")
650 "Face for top priority Todos item numerical priority string.
651 The item's priority number string has this face if the number is
652 less than or equal the category's top priority setting."
656 ;; '((t :inherit font-lock-warning-face))
660 (:weight bold
:foreground
"Red1"))
664 (:weight bold
:foreground
"Pink"))
668 (:weight bold
:foreground
"Red1"))
672 (:weight bold
:foreground
"Pink"))
677 (:weight bold
:inverse-video t
)))
678 "Face for marks on Todos items."
681 (defface todos-button
682 ;; '((t :inherit widget-field))
684 (:foreground
"black" :background
"yellow3"))
685 (((class grayscale color
)
687 (:background
"gray85"))
688 (((class grayscale color
)
690 (:background
"dim gray"))
693 "Face for buttons in todos-display-categories."
696 (defface todos-sorted-column
701 (:background
"grey85"))
704 (:background
"grey85" :foreground
"grey10"))
706 (:background
"gray")))
707 "Face for buttons in todos-display-categories."
710 (defface todos-archived-only
711 ;; '((t (:inherit (shadow))))
714 (:foreground
"grey50"))
717 (:foreground
"grey70"))
719 (:foreground
"gray")))
720 "Face for archived-only categories in todos-display-categories."
723 (defface todos-search
724 ;; '((t :inherit match))
728 (:background
"yellow1"))
732 (:background
"RoyalBlue3"))
736 (:foreground
"black" :background
"yellow"))
740 (:foreground
"white" :background
"blue"))
745 (:background
"gray")))
746 "Face for matches found by todos-search."
749 (defface todos-diary-expired
750 ;; Doesn't contrast enough with todos-date (= diary) face.
751 ;; ;; '((t :inherit warning))
752 ;; '((default :weight bold)
753 ;; (((class color) (min-colors 16)) :foreground "DarkOrange")
754 ;; (((class color)) :foreground "yellow"))
755 ;; bold font-lock-function-name-face
756 '((default :weight bold
)
757 (((class color
) (min-colors 88) (background light
)) :foreground
"Blue1")
758 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSkyBlue")
759 (((class color
) (min-colors 16) (background light
)) :foreground
"Blue")
760 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSkyBlue")
761 (((class color
) (min-colors 8)) :foreground
"blue")
762 (t :inverse-video t
))
763 "Face for expired dates of diary items."
765 (defvar todos-diary-expired-face
'todos-diary-expired
)
768 '((t :inherit diary
))
769 "Face for the date string of a Todos item."
771 (defvar todos-date-face
'todos-date
)
774 '((t :inherit diary-time
))
775 "Face for the time string of a Todos item."
777 (defvar todos-time-face
'todos-time
)
779 (defface todos-nondiary
780 ;; '((t :inherit font-lock-type-face))
781 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
782 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
783 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
784 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
785 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
786 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
787 (((class color
) (min-colors 8)) :foreground
"green")
788 (t :weight bold
:underline t
))
789 "Face for non-diary markers around todo item date/time header."
791 (defvar todos-nondiary-face
'todos-nondiary
)
793 (defface todos-category-string
794 ;; '((t :inherit font-lock-type-face))
795 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
796 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
797 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
798 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
799 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
800 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
801 (((class color
) (min-colors 8)) :foreground
"green")
802 (t :weight bold
:underline t
))
803 "Face for category file names in Todos Filtered Item."
805 (defvar todos-category-string-face
'todos-category-string
)
808 ;; '((t :inherit font-lock-keyword-face))
809 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
810 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
811 (((class color
) (min-colors 88) (background light
)) :foreground
"Purple")
812 (((class color
) (min-colors 88) (background dark
)) :foreground
"Cyan1")
813 (((class color
) (min-colors 16) (background light
)) :foreground
"Purple")
814 (((class color
) (min-colors 16) (background dark
)) :foreground
"Cyan")
815 (((class color
) (min-colors 8)) :foreground
"cyan" :weight bold
)
817 "Face for done Todos item header string."
819 (defvar todos-done-face
'todos-done
)
821 (defface todos-comment
822 ;; '((t :inherit font-lock-comment-face))
823 '((((class grayscale
) (background light
))
824 :foreground
"DimGray" :weight bold
:slant italic
)
825 (((class grayscale
) (background dark
))
826 :foreground
"LightGray" :weight bold
:slant italic
)
827 (((class color
) (min-colors 88) (background light
))
828 :foreground
"Firebrick")
829 (((class color
) (min-colors 88) (background dark
))
830 :foreground
"chocolate1")
831 (((class color
) (min-colors 16) (background light
))
833 (((class color
) (min-colors 16) (background dark
))
835 (((class color
) (min-colors 8) (background light
))
837 (((class color
) (min-colors 8) (background dark
))
838 :foreground
"yellow")
839 (t :weight bold
:slant italic
))
840 "Face for comments appended to done Todos items."
842 (defvar todos-comment-face
'todos-comment
)
844 (defface todos-done-sep
845 ;; '((t :inherit font-lock-builtin-face))
846 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
847 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
848 (((class color
) (min-colors 88) (background light
)) :foreground
"dark slate blue")
849 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSteelBlue")
850 (((class color
) (min-colors 16) (background light
)) :foreground
"Orchid")
851 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSteelBlue")
852 (((class color
) (min-colors 8)) :foreground
"blue" :weight bold
)
854 "Face for separator string bewteen done and not done Todos items."
856 (defvar todos-done-sep-face
'todos-done-sep
)
858 (defun todos-date-string-matcher (lim)
859 "Search for Todos date string within LIM for font-locking."
861 (concat todos-date-string-start
"\\(?1:" todos-date-pattern
"\\)") lim t
))
863 (defun todos-time-string-matcher (lim)
864 "Search for Todos time string within LIM for font-locking."
865 (re-search-forward (concat todos-date-string-start todos-date-pattern
866 " \\(?1:" diary-time-regexp
"\\)") lim t
))
868 (defun todos-nondiary-marker-matcher (lim)
869 "Search for Todos nondiary markers within LIM for font-locking."
870 (re-search-forward (concat "^\\(?1:" (regexp-quote todos-nondiary-start
) "\\)"
871 todos-date-pattern
"\\(?: " diary-time-regexp
872 "\\)?\\(?2:" (regexp-quote todos-nondiary-end
) "\\)")
875 (defun todos-diary-nonmarking-matcher (lim)
876 "Search for diary nonmarking symbol within LIM for font-locking."
877 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
878 "\\)" todos-date-pattern
) lim t
))
880 (defun todos-diary-expired-matcher (lim)
881 "Search for expired diary item date within LIM for font-locking."
882 (when (re-search-forward (concat "^\\(?:"
883 (regexp-quote diary-nonmarking-symbol
)
884 "\\)?\\(?1:" todos-date-pattern
"\\) \\(?2:"
885 diary-time-regexp
"\\)?") lim t
)
886 (let* ((date (match-string-no-properties 1))
887 (time (match-string-no-properties 2))
888 ;; Function days-between requires a non-empty time string.
889 (date-time (concat date
" " (or time
"00:00"))))
890 (or (and (not (string-match ".+day\\|\\*" date
))
891 (< (days-between date-time
(current-time-string)) 0))
892 (todos-diary-expired-matcher lim
)))))
894 (defun todos-done-string-matcher (lim)
895 "Search for Todos done header within LIM for font-locking."
896 (re-search-forward (concat todos-done-string-start
900 (defun todos-comment-string-matcher (lim)
901 "Search for Todos done comment within LIM for font-locking."
902 (re-search-forward (concat "\\[\\(?1:" todos-comment-string
"\\):")
905 ;; (defun todos-category-string-matcher (lim)
906 ;; "Search for Todos category name within LIM for font-locking.
907 ;; This is for fontifying category names appearing in Todos filter
909 ;; (if (eq major-mode 'todos-filtered-items-mode)
910 ;; (re-search-forward
911 ;; (concat "^\\(?:" todos-date-string-start "\\)?" todos-date-pattern
912 ;; "\\(?: " diary-time-regexp "\\)?\\(?:"
913 ;; (regexp-quote todos-nondiary-end) "\\)? \\(?1:\\[.+\\]\\)")
916 (defun todos-category-string-matcher-1 (lim)
917 "Search for Todos category name within LIM for font-locking.
918 This is for fontifying category and file names appearing in Todos
919 Filtered Items mode following done items."
920 (if (eq major-mode
'todos-filtered-items-mode
)
921 (re-search-forward (concat todos-done-string-start todos-date-pattern
922 "\\(?: " diary-time-regexp
923 ;; Use non-greedy operator to prevent
924 ;; capturing possible following non-diary
926 "\\)?] \\(?1:\\[.+?\\]\\)")
929 (defun todos-category-string-matcher-2 (lim)
930 "Search for Todos category name within LIM for font-locking.
931 This is for fontifying category and file names appearing in Todos
932 Filtered Items mode following todo (not done) items."
933 (if (eq major-mode
'todos-filtered-items-mode
)
934 (re-search-forward (concat todos-date-string-start todos-date-pattern
935 "\\(?: " diary-time-regexp
"\\)?\\(?:"
936 (regexp-quote todos-nondiary-end
)
937 "\\)? \\(?1:\\[.+\\]\\)")
940 (defvar todos-font-lock-keywords
942 '(todos-nondiary-marker-matcher 1 todos-nondiary-face t
)
943 '(todos-nondiary-marker-matcher 2 todos-nondiary-face t
)
944 ;; diary-lib.el uses font-lock-constant-face for diary-nonmarking-symbol.
945 '(todos-diary-nonmarking-matcher 1 font-lock-constant-face t
)
946 '(todos-date-string-matcher 1 todos-date-face t
)
947 '(todos-time-string-matcher 1 todos-time-face t
)
948 '(todos-done-string-matcher 0 todos-done-face t
)
949 '(todos-comment-string-matcher 1 todos-comment-face t
)
950 '(todos-category-string-matcher-1 1 todos-category-string-face t t
)
951 '(todos-category-string-matcher-2 1 todos-category-string-face t t
)
952 '(todos-diary-expired-matcher 1 todos-diary-expired-face t
)
953 '(todos-diary-expired-matcher 2 todos-diary-expired-face t t
)
955 "Font-locking for Todos modes.")
957 ;; ---------------------------------------------------------------------------
958 ;;; Todos mode local variables and hook functions
960 (defvar todos-current-todos-file nil
961 "Variable holding the name of the currently active Todos file.")
963 (defun todos-show-current-file ()
964 "Visit current instead of default Todos file with `todos-show'.
965 This function is added to `pre-command-hook' when user option
966 `todos-show-current-file' is set to non-nil."
967 (setq todos-global-current-todos-file todos-current-todos-file
))
969 (defun todos-display-as-todos-file ()
970 "Show Todos files correctly when visited from outside of Todos mode."
971 (and (member this-command todos-visit-files-commands
)
972 (= (- (point-max) (point-min)) (buffer-size))
973 (member major-mode
'(todos-mode todos-archive-mode
))
974 (todos-category-select)))
976 (defun todos-add-to-buffer-list ()
977 "Add name of just visited Todos file to `todos-file-buffers'.
978 This function is added to `find-file-hook' in Todos mode."
979 (let ((filename (file-truename (buffer-file-name))))
980 (when (member filename todos-files
)
981 (add-to-list 'todos-file-buffers filename
))))
983 (defun todos-update-buffer-list ()
984 "Make current Todos mode buffer file car of `todos-file-buffers'.
985 This function is added to `post-command-hook' in Todos mode."
986 (let ((filename (file-truename (buffer-file-name))))
987 (unless (eq (car todos-file-buffers
) filename
)
988 (setq todos-file-buffers
989 (cons filename
(delete filename todos-file-buffers
))))))
991 (defun todos-reset-global-current-todos-file ()
992 "Update the value of `todos-global-current-todos-file'.
993 This becomes the latest existing Todos file or, if there is none,
994 the value of `todos-default-todos-file'.
995 This function is added to `kill-buffer-hook' in Todos mode."
996 (let ((filename (file-truename (buffer-file-name))))
997 (setq todos-file-buffers
(delete filename todos-file-buffers
))
998 (setq todos-global-current-todos-file
999 (or (car todos-file-buffers
)
1000 (todos-absolute-file-name todos-default-todos-file
)))))
1002 (defvar todos-categories nil
1003 "Alist of categories in the current Todos file.
1004 The elements are cons cells whose car is a category name and
1005 whose cdr is a vector of the category's item counts. These are,
1006 in order, the numbers of todo items, of todo items included in
1007 the Diary, of done items and of archived items.")
1009 (defvar todos-categories-with-marks nil
1010 "Alist of categories and number of marked items they contain.")
1012 (defvar todos-category-number
1
1013 "Variable holding the number of the current Todos category.
1014 Todos categories are numbered starting from 1.")
1016 (defvar todos-show-done-only nil
1017 "If non-nil display only done items in current category.
1018 Set by the command `todos-show-done-only' and used by
1019 `todos-category-select'.")
1021 (defun todos-reset-and-enable-done-separator ()
1022 "Show resized done items separator overlay after window change.
1023 Added to `window-configuration-change-hook' in `todos-mode'."
1024 (when (= 1 (length todos-done-separator-string
))
1025 (let ((sep todos-done-separator
))
1026 (setq todos-done-separator
(todos-done-separator))
1027 (save-match-data (todos-reset-done-separator sep
)))))
1029 ;; ---------------------------------------------------------------------------
1030 ;;; Global variables and helper functions for files and buffers
1032 (defvar todos-files
(funcall todos-files-function
)
1033 "List of truenames of user's Todos files.")
1035 (defvar todos-archives
(funcall todos-files-function t
)
1036 "List of truenames of user's Todos archives.")
1038 (defvar todos-visited nil
1039 "List of Todos files visited in this session by `todos-show'.
1040 Used to determine initial display according to the value of
1041 `todos-show-first'.")
1043 (defvar todos-file-buffers nil
1044 "List of file names of live Todos mode buffers.")
1046 (defvar todos-global-current-todos-file nil
1047 "Variable holding name of current Todos file.
1048 Used by functions called from outside of Todos mode to visit the
1049 current Todos file rather than the default Todos file (i.e. when
1050 users option `todos-show-current-file' is non-nil).")
1052 (defun todos-reevaluate-filelist-defcustoms ()
1053 "Reevaluate defcustoms that provide choice list of Todos files."
1054 (custom-set-default 'todos-default-todos-file
1055 (symbol-value 'todos-default-todos-file
))
1056 (todos-reevaluate-default-file-defcustom)
1057 (custom-set-default 'todos-filter-files
(symbol-value 'todos-filter-files
))
1058 (todos-reevaluate-filter-files-defcustom)
1059 (custom-set-default 'todos-category-completions-files
1060 (symbol-value 'todos-category-completions-files
))
1061 (todos-reevaluate-category-completions-files-defcustom))
1063 (defvar todos-edit-buffer
"*Todos Edit*"
1064 "Name of current buffer in Todos Edit mode.")
1066 (defvar todos-categories-buffer
"*Todos Categories*"
1067 "Name of buffer in Todos Categories mode.")
1069 (defvar todos-print-buffer
"*Todos Print*"
1070 "Name of buffer containing printable Todos text.")
1072 (defun todos-absolute-file-name (name &optional type
)
1073 "Return the absolute file name of short Todos file NAME.
1074 With TYPE `archive' or `top' return the absolute file name of the
1075 short Todos Archive or Top Priorities file name, respectively."
1076 ;; NOP if there is no Todos file yet (i.e. don't concatenate nil).
1079 (concat todos-files-directory name
1080 (cond ((eq type
'archive
) ".toda")
1081 ((eq type
'top
) ".todt")
1084 (defun todos-check-format ()
1085 "Signal an error if the current Todos file is ill-formatted.
1086 Otherwise return t. The error message gives the line number
1087 where the invalid formatting was found."
1091 (goto-char (point-min))
1092 ;; Check for `todos-categories' sexp as the first line
1093 (let ((cats (prin1-to-string todos-categories
)))
1094 (unless (looking-at (regexp-quote cats
))
1095 (error "Invalid or missing todos-categories sexp")))
1097 (let ((legit (concat "\\(^" (regexp-quote todos-category-beg
) "\\)"
1098 "\\|\\(" todos-date-string-start todos-date-pattern
"\\)"
1099 "\\|\\(^[ \t]+[^ \t]*\\)"
1101 "\\|\\(^" (regexp-quote todos-category-done
) "\\)"
1102 "\\|\\(" todos-done-string-start
"\\)")))
1104 (unless (looking-at legit
)
1105 (error "Illegitimate Todos file format at line %d"
1106 (line-number-at-pos (point))))
1108 ;; (message "This Todos file is well-formatted.")
1111 ;; ---------------------------------------------------------------------------
1112 (defun todos-convert-legacy-date-time ()
1113 "Return converted date-time string.
1114 Helper function for `todos-convert-legacy-files'."
1115 (let* ((year (match-string 1))
1116 (month (match-string 2))
1117 (monthname (calendar-month-name (string-to-number month
) t
))
1118 (day (match-string 3))
1119 (time (match-string 4))
1122 (insert (mapconcat 'eval calendar-date-display-form
"")
1123 (when time
(concat " " time
)))))
1125 ;; ---------------------------------------------------------------------------
1126 ;;; Global variables and helper functions for categories
1128 (defun todos-category-number (cat)
1129 "Return the number of category CAT in this Todos file.
1130 The buffer-local variable `todos-category-number' holds this
1131 number as its value."
1132 (let ((categories (mapcar 'car todos-categories
)))
1133 (setq todos-category-number
1134 ;; Increment by one, so that the highest priority category in Todos
1135 ;; Categories mode is numbered one rather than zero.
1136 (1+ (- (length categories
)
1137 (length (member cat categories
)))))))
1139 (defun todos-current-category ()
1140 "Return the name of the current category."
1141 (car (nth (1- todos-category-number
) todos-categories
)))
1143 (defconst todos-category-beg
"--==-- "
1144 "String marking beginning of category (inserted with its name).")
1146 (defconst todos-category-done
"==--== DONE "
1147 "String marking beginning of category's done items.")
1149 (defun todos-done-separator ()
1150 "Return string used as value of variable `todos-done-separator'."
1151 (let ((sep todos-done-separator-string
))
1152 (propertize (if (= 1 (length sep
))
1153 ;; If separator's length is window-width, an
1154 ;; indented empty line appears between the
1155 ;; separator and the first done item.
1156 ;; FIXME: should this be customizable?
1157 (make-string (1- (window-width)) (string-to-char sep
))
1158 todos-done-separator-string
)
1159 'face
'todos-done-sep
)))
1161 (defvar todos-done-separator
(todos-done-separator)
1162 "String used to visually separate done from not done items.
1163 Displayed as an overlay instead of `todos-category-done' when
1164 done items are shown. Its value is determined by user option
1165 `todos-done-separator-string'.")
1167 (defun todos-reset-done-separator (sep)
1168 "Replace existing overlays of done items separator string SEP."
1172 (goto-char (point-min))
1173 (while (re-search-forward
1174 (concat "\n\\(" (regexp-quote todos-category-done
) "\\)") nil t
)
1175 (let* ((beg (match-beginning 1))
1177 (ovs (overlays-at beg
))
1178 (ov (when ovs
(car ovs
)))
1179 (old-sep (when ov
(overlay-get ov
'display
)))
1181 (when (string= old-sep sep
)
1182 (setq new-ov
(make-overlay beg end
))
1183 (overlay-put new-ov
'display todos-done-separator
)
1184 (delete-overlay ov
)))))))
1186 (defun todos-category-completions ()
1187 "Return a list of completions for `todos-read-category'.
1188 Each element of the list is a cons of a category name and the
1189 file or list of files (as short file names) it is in. The files
1190 are the current (or else the default) Todos file plus all other
1191 Todos files named in `todos-category-completions-files'."
1192 (let* ((curfile (or todos-current-todos-file
1193 (and todos-show-current-file
1194 todos-global-current-todos-file
)
1195 (todos-absolute-file-name todos-default-todos-file
)))
1196 (files (or (mapcar 'todos-absolute-file-name
1197 todos-category-completions-files
)
1200 ;; If file was just added, it has no category completions.
1201 (unless (zerop (buffer-size (find-buffer-visiting curfile
)))
1202 (add-to-list 'files curfile
)
1203 (dolist (f files listall
)
1204 (with-current-buffer (find-file-noselect f
'nowarn
)
1208 (goto-char (point-min))
1209 (setq listf
(read (buffer-substring-no-properties
1210 (line-beginning-position)
1211 (line-end-position)))))))
1212 (mapc (lambda (elt) (let* ((cat (car elt
))
1213 (la-elt (assoc cat listall
)))
1215 (setcdr la-elt
(append (list (cdr la-elt
))
1217 (push (cons cat f
) listall
))))
1220 (defun todos-category-select ()
1221 "Display the current category correctly."
1222 (let ((name (todos-current-category))
1223 cat-begin cat-end done-start done-sep-start done-end
)
1225 (goto-char (point-min))
1227 (concat "^" (regexp-quote (concat todos-category-beg name
)) "$") nil t
)
1228 (setq cat-begin
(1+ (line-end-position)))
1229 (setq cat-end
(if (re-search-forward
1230 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
1233 (setq mode-line-buffer-identification
1234 (funcall todos-mode-line-function name
))
1235 (narrow-to-region cat-begin cat-end
)
1236 (todos-prefix-overlays)
1237 (goto-char (point-min))
1238 (if (re-search-forward (concat "\n\\(" (regexp-quote todos-category-done
)
1241 (setq done-start
(match-beginning 0))
1242 (setq done-sep-start
(match-beginning 1))
1243 (setq done-end
(match-end 0)))
1244 (error "Category %s is missing todos-category-done string" name
))
1245 (if todos-show-done-only
1246 (narrow-to-region (1+ done-end
) (point-max))
1247 (when (and todos-show-with-done
1248 (re-search-forward todos-done-string-start nil t
))
1249 ;; Now we want to see the done items, so reset displayed end to end of
1251 (setq done-start cat-end
)
1252 ;; Make display overlay for done items separator string, unless there
1254 (let* ((done-sep todos-done-separator
)
1255 (ovs (overlays-at done-sep-start
))
1258 ;; There should never be more than one overlay here, so car suffices.
1259 (unless (and ovs
(string= (overlay-get (car ovs
) 'display
) done-sep
))
1260 (setq ov-sep
(make-overlay done-sep-start done-end
))
1261 (overlay-put ov-sep
'display done-sep
))))
1262 ;; (setq ov-sep0 (make-overlay done-sep-start done-end))
1263 ;; (setq ov-sep1 (make-overlay done-end done-end))
1264 ;; (overlay-put ov-sep0 'invisible t)
1265 ;; (overlay-put ov-sep1 'after-string done-sep)))
1266 (narrow-to-region (point-min) done-start
)
1267 ;; Loading this from todos-mode, or adding it to the mode hook, causes
1268 ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
1269 (when todos-highlight-item
1271 (hl-line-mode 1)))))
1273 (defun todos-get-count (type &optional category
)
1274 "Return count of TYPE items in CATEGORY.
1275 If CATEGORY is nil, default to the current category."
1276 (let* ((cat (or category
(todos-current-category)))
1277 (counts (cdr (assoc cat todos-categories
)))
1278 (idx (cond ((eq type
'todo
) 0)
1279 ((eq type
'diary
) 1)
1281 ((eq type
'archived
) 3))))
1284 (defun todos-update-count (type increment
&optional category
)
1285 "Change count of TYPE items in CATEGORY by integer INCREMENT.
1286 With nil or omitted CATEGORY, default to the current category."
1287 (let* ((cat (or category
(todos-current-category)))
1288 (counts (cdr (assoc cat todos-categories
)))
1289 (idx (cond ((eq type
'todo
) 0)
1290 ((eq type
'diary
) 1)
1292 ((eq type
'archived
) 3))))
1293 (aset counts idx
(+ increment
(aref counts idx
)))))
1295 (defun todos-set-categories ()
1296 "Set `todos-categories' from the sexp at the top of the file."
1297 ;; New archive files created by `todos-move-category' are empty, which would
1298 ;; make the sexp test fail and raise an error, so in this case we skip it.
1299 (unless (zerop (buffer-size))
1303 (goto-char (point-min))
1304 (setq todos-categories
1305 (if (looking-at "\(\(\"")
1306 (read (buffer-substring-no-properties
1307 (line-beginning-position)
1308 (line-end-position)))
1309 (error "Invalid or missing todos-categories sexp")))))))
1311 (defun todos-update-categories-sexp ()
1312 "Update the `todos-categories' sexp at the top of the file."
1313 (let (buffer-read-only)
1317 (goto-char (point-min))
1318 (if (looking-at (concat "^" (regexp-quote todos-category-beg
)))
1319 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
1320 (setq todos-categories
(todos-make-categories-list t
)))
1321 (delete-region (line-beginning-position) (line-end-position)))
1322 (prin1 todos-categories
(current-buffer))))))
1324 (defun todos-make-categories-list (&optional force
)
1325 "Return an alist of Todos categories and their item counts.
1326 With non-nil argument FORCE parse the entire file to build the
1327 list; otherwise, get the value by reading the sexp at the top of
1329 (setq todos-categories nil
)
1333 (goto-char (point-min))
1334 (let (counts cat archive
)
1335 ;; If the file is a todo file and has archived items, identify the
1336 ;; archive, in order to count its items. But skip this with
1337 ;; `todos-convert-legacy-files', since that converts filed items to
1339 (when buffer-file-name
; During conversion there is no file yet.
1340 ;; If the file is an archive, it doesn't have an archive.
1341 (unless (member (file-truename buffer-file-name
)
1342 (funcall todos-files-function t
))
1343 (setq archive
(concat (file-name-sans-extension
1344 todos-current-todos-file
) ".toda"))))
1346 (cond ((looking-at (concat (regexp-quote todos-category-beg
)
1348 (setq cat
(match-string-no-properties 1))
1349 ;; Counts for each category: [todo diary done archive]
1350 (setq counts
(make-vector 4 0))
1351 (setq todos-categories
1352 (append todos-categories
(list (cons cat counts
))))
1353 ;; Add archived item count to the todo file item counts.
1354 ;; Make sure to include newly created archives, e.g. due to
1355 ;; todos-move-category.
1356 (when (member archive
(funcall todos-files-function t
))
1357 (let ((archive-count 0))
1358 (with-current-buffer (find-file-noselect archive
)
1360 (goto-char (point-min))
1361 (when (re-search-forward
1362 (concat "^" (regexp-quote todos-category-beg
)
1366 (while (not (or (looking-at
1368 (regexp-quote todos-category-beg
)
1371 (when (looking-at todos-done-string-start
)
1372 (setq archive-count
(1+ archive-count
)))
1374 (todos-update-count 'archived archive-count cat
))))
1375 ((looking-at todos-done-string-start
)
1376 (todos-update-count 'done
1 cat
))
1377 ((looking-at (concat "^\\("
1378 (regexp-quote diary-nonmarking-symbol
)
1379 "\\)?" todos-date-pattern
))
1380 (todos-update-count 'diary
1 cat
)
1381 (todos-update-count 'todo
1 cat
))
1382 ((looking-at (concat todos-date-string-start todos-date-pattern
))
1383 (todos-update-count 'todo
1 cat
))
1384 ;; If first line is todos-categories list, use it and end loop
1385 ;; -- unless FORCEd to scan whole file.
1388 (setq todos-categories
(read (buffer-substring-no-properties
1389 (line-beginning-position)
1390 (line-end-position))))
1391 (goto-char (1- (point-max))))))
1395 (defun todos-repair-categories-sexp ()
1396 "Repair corrupt Todos categories sexp.
1397 This should only be needed as a consequence of careless manual
1398 editing or a bug in todos.el."
1400 (let ((todos-categories (todos-make-categories-list t
)))
1401 (todos-update-categories-sexp)))
1403 ;;; Global variables and helper functions for items
1405 (defconst todos-month-name-array
1406 (vconcat calendar-month-name-array
(vector "*"))
1407 "Array of month names, in order.
1408 The final element is \"*\", indicating an unspecified month.")
1410 (defconst todos-month-abbrev-array
1411 (vconcat calendar-month-abbrev-array
(vector "*"))
1412 "Array of abbreviated month names, in order.
1413 The final element is \"*\", indicating an unspecified month.")
1415 (defconst todos-date-pattern
1416 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
1417 (concat "\\(?5:" dayname
"\\|"
1419 (monthname (format "\\(?6:%s\\)" (diary-name-pattern
1420 todos-month-name-array
1421 todos-month-abbrev-array
)))
1422 (month "\\(?7:[0-9]+\\|\\*\\)")
1423 (day "\\(?8:[0-9]+\\|\\*\\)")
1424 (year "-?\\(?9:[0-9]+\\|\\*\\)"))
1425 (mapconcat 'eval calendar-date-display-form
""))
1427 "Regular expression matching a Todos date header.")
1429 (defconst todos-nondiary-start
(nth 0 todos-nondiary-marker
)
1430 "String inserted before item date to block diary inclusion.")
1432 (defconst todos-nondiary-end
(nth 1 todos-nondiary-marker
)
1433 "String inserted after item date matching `todos-nondiary-start'.")
1435 ;; By itself this matches anything, because of the `?'; however, it's only
1436 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
1438 (defconst todos-date-string-start
1439 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
1440 (regexp-quote diary-nonmarking-symbol
) "\\)?")
1441 "Regular expression matching part of item header before the date.")
1443 (defconst todos-done-string-start
1444 (concat "^\\[" (regexp-quote todos-done-string
))
1445 "Regular expression matching start of done item.")
1447 (defconst todos-item-start
(concat "\\(" todos-date-string-start
"\\|"
1448 todos-done-string-start
"\\)"
1450 "String identifying start of a Todos item.")
1452 (defun todos-item-start ()
1453 "Move to start of current Todos item and return its position."
1455 ;; Buffer is empty (invocation possible e.g. via todos-forward-item
1456 ;; from todos-filter-items when processing category with no todo
1458 (eq (point-min) (point-max))
1459 ;; Point is on the empty line below category's last todo item...
1460 (and (looking-at "^$")
1461 (or (eobp) ; ...and done items are hidden...
1462 (save-excursion ; ...or done items are visible.
1464 (looking-at (concat "^"
1465 (regexp-quote todos-category-done
))))))
1466 ;; Buffer is widened.
1467 (looking-at (regexp-quote todos-category-beg
)))
1468 (goto-char (line-beginning-position))
1469 (while (not (looking-at todos-item-start
))
1473 (defun todos-item-end ()
1474 "Move to end of current Todos item and return its position."
1475 ;; Items cannot end with a blank line.
1476 (unless (looking-at "^$")
1477 (let* ((done (todos-done-item-p))
1479 ;; For todo items, end is before the done items section, for done
1480 ;; items, end is before the next category. If these limits are
1481 ;; missing or inaccessible, end it before the end of the buffer.
1482 (lim (if (save-excursion
1484 (concat "^" (regexp-quote (if done
1486 todos-category-done
)))
1488 (progn (setq to-lim t
) (match-beginning 0))
1490 (when (bolp) (forward-char)) ; Find start of next item.
1491 (goto-char (if (re-search-forward todos-item-start lim t
)
1493 (if to-lim lim
(point-max))))
1494 ;; For last todo item, skip back over the empty line before the done
1495 ;; items section, else just back to the end of the previous line.
1496 (backward-char (when (and to-lim
(not done
) (eq (point) lim
)) 2))
1499 (defun todos-item-string ()
1500 "Return bare text of current item as a string."
1501 (let ((opoint (point))
1502 (start (todos-item-start))
1503 (end (todos-item-end)))
1505 (and start end
(buffer-substring-no-properties start end
))))
1507 (defun todos-remove-item ()
1508 "Internal function called in editing, deleting or moving items."
1509 (let* ((beg (todos-item-start))
1510 (end (progn (todos-item-end) (1+ (point))))
1511 (ovs (overlays-in beg beg
)))
1512 ;; There can be both prefix/number and mark overlays.
1513 (while ovs
(delete-overlay (car ovs
)) (pop ovs
))
1514 (delete-region beg end
)))
1516 (defun todos-diary-item-p ()
1517 "Return non-nil if item at point has diary entry format."
1519 (when (todos-item-string) ; Exclude empty lines.
1521 (not (looking-at (regexp-quote todos-nondiary-start
))))))
1523 (defun todos-done-item-p ()
1524 "Return non-nil if item at point is a done item."
1527 (looking-at todos-done-string-start
)))
1529 ;; (defun todos-done-item-section-p ()
1530 ;; "Return non-nil if point is in category's done items section."
1532 ;; (or (re-search-backward (concat "^" (regexp-quote todos-category-done))
1534 ;; (progn (goto-char (point-min))
1535 ;; (looking-at todos-done-string-start)))))
1537 (defun todos-prefix-overlay ()
1538 "Return this item's prefix overlay."
1539 (let* ((lbp (line-beginning-position))
1540 (ovs (overlays-in lbp lbp
)))
1543 (defun todos-marked-item-p ()
1544 "Non-nil if this item begins with `todos-item-mark'.
1545 In that case, return the item's prefix overlay."
1546 ;; If a todos-item-insert command is called on a Todos file before
1547 ;; it is visited, it has no prefix overlays, so conditionalize:
1548 (let* ((ov (todos-prefix-overlay))
1549 (pref (when ov
(overlay-get ov
'before-string
)))
1551 (string-match (concat "^" (regexp-quote todos-item-mark
))
1555 (defun todos-insert-with-overlays (item)
1556 "Insert ITEM at point and update prefix/priority number overlays."
1558 ;; Insertion pushes item down but not its prefix overlay. When the
1559 ;; overlay includes a mark, this would now mark the inserted ITEM,
1560 ;; so move it to the pushed down item.
1561 (let ((ov (todos-prefix-overlay))
1562 (marked (todos-marked-item-p)))
1564 (when marked
(move-overlay ov
(point) (point))))
1565 (todos-backward-item)
1566 (todos-prefix-overlays))
1568 (defun todos-prefix-overlays ()
1569 "Update the prefix overlays of the current category's items.
1570 The overlay's value is the string `todos-prefix' or with non-nil
1571 `todos-number-priorities' an integer in the sequence from 1 to
1572 the number of todo or done items in the category indicating the
1573 item's priority. Todo and done items are numbered independently
1575 (let ((prefix (propertize (concat todos-prefix
" ")
1576 'face
'todos-prefix-string
))
1578 (cat-tp (or (cdr (assoc-string
1579 (todos-current-category)
1580 (nth 2 (assoc-string todos-current-todos-file
1581 todos-priorities-rules
))))
1582 todos-show-priorities
))
1585 (goto-char (point-min))
1587 (when (or (todos-date-string-matcher (line-end-position))
1588 (todos-done-string-matcher (line-end-position)))
1589 (goto-char (match-beginning 0))
1590 (when todos-number-priorities
1592 ;; Reset number to 1 for first done item.
1593 (when (and (looking-at todos-done-string-start
)
1594 (looking-back (concat "^"
1595 (regexp-quote todos-category-done
)
1599 (setq prefix
(propertize (concat (number-to-string num
) " ")
1601 ;; Numbers of top priorities have
1602 ;; a distinct face in Todos mode.
1603 (if (and (not done
) (<= num cat-tp
)
1604 (eq major-mode
'todos-mode
))
1606 'todos-prefix-string
))))
1607 (let ((ov (todos-prefix-overlay))
1608 (marked (todos-marked-item-p)))
1609 (unless ov
(setq ov
(make-overlay (point) (point))))
1610 (overlay-put ov
'before-string
(if marked
1611 (concat todos-item-mark prefix
)
1615 ;; ---------------------------------------------------------------------------
1616 ;;; Helper functions for user input with prompting and completion
1618 (defun todos-read-file-name (prompt &optional archive mustmatch
)
1619 "Choose and return the name of a Todos file, prompting with PROMPT.
1621 Show completions with TAB or SPC; the names are shown in short
1622 form but the absolute truename is returned. With non-nil ARCHIVE
1623 return the absolute truename of a Todos archive file. With non-nil
1624 MUSTMATCH the name of an existing file must be chosen;
1625 otherwise, a new file name is allowed."
1626 (let* ((completion-ignore-case todos-completion-ignore-case
)
1627 (files (mapcar 'todos-short-file-name
1628 (if archive todos-archives todos-files
)))
1629 (file (completing-read prompt files nil mustmatch nil nil
1631 ;; Trigger prompt for initial file.
1633 (unless (file-exists-p todos-files-directory
)
1634 (make-directory todos-files-directory
))
1636 (setq file
(todos-validate-name file
'file
)))
1637 (setq file
(file-truename (concat todos-files-directory file
1638 (if archive
".toda" ".todo"))))))
1640 (defun todos-read-category (prompt &optional match-type file
)
1641 "Choose and return a category name, prompting with PROMPT.
1642 Show completions for existing categories with TAB or SPC.
1644 The argument MATCH-TYPE specifies the matching requirements on
1645 the category name: with the value `merge' the name must complete
1646 to that of an existing category; with the value `add' the name
1647 must not be that of an existing category; with all other values
1648 both existing and new valid category names are accepted.
1650 With non-nil argument FILE prompt for a file and complete only
1651 against categories in that file; otherwise complete against all
1652 categories from `todos-category-completions-files'."
1653 ;; Allow SPC to insert spaces, for adding new category names.
1654 (let ((map minibuffer-local-completion-map
))
1655 (define-key map
" " nil
)
1656 (let* ((add (eq match-type
'add
))
1657 (file0 (when (and file
(> (length todos-files
) 1))
1658 (todos-read-file-name "Choose a Todos file: " nil t
)))
1659 (completions (unless file0
(todos-category-completions)))
1660 (categories (cond (file0
1661 (with-current-buffer
1662 (find-file-noselect file0
'nowarn
)
1663 (let ((todos-current-todos-file file0
))
1665 ((and add
(not file
))
1666 (with-current-buffer
1667 (find-file-noselect todos-current-todos-file
)
1671 (completion-ignore-case todos-completion-ignore-case
)
1672 (cat (completing-read prompt categories nil
1673 (eq match-type
'merge
) nil nil
1674 ;; Unless we're adding a category via
1675 ;; todos-add-category, set default
1676 ;; for existing categories to the
1677 ;; current category of the chosen
1678 ;; file or else of the current file.
1679 (if (and categories
(not add
))
1680 (with-current-buffer
1683 todos-current-todos-file
1684 (todos-absolute-file-name
1685 todos-default-todos-file
)))
1686 (todos-current-category))
1687 ;; Trigger prompt for initial category.
1689 (catfil (cdr (assoc cat completions
)))
1690 (str "Category \"%s\" from which file (TAB for choices)? "))
1691 ;; If we do category completion and the chosen category name
1692 ;; occurs in more than one file, prompt to choose one file.
1693 (unless (or file0 add
(not catfil
))
1694 (setq file0
(file-truename
1697 (todos-absolute-file-name
1698 (completing-read (format str cat
)
1699 todos-category-completions-files
))))))
1700 ;; Default to the current file.
1701 (unless file0
(setq file0 todos-current-todos-file
))
1702 ;; First validate only a name passed interactively from
1703 ;; todos-add-category, which must be of a nonexisting category.
1704 (unless (and (assoc cat categories
) (not add
))
1705 ;; Validate only against completion categories.
1706 (let ((todos-categories categories
))
1707 (setq cat
(todos-validate-name cat
'category
)))
1708 ;; When user enters a nonexisting category name by jumping or
1709 ;; moving, confirm that it should be added, then validate.
1711 (if (y-or-n-p (format "Add new category \"%s\" to file \"%s\"? "
1712 cat
(todos-short-file-name file0
)))
1714 (when (assoc cat categories
)
1715 (let ((todos-categories categories
))
1716 (setq cat
(todos-validate-name cat
'category
))))
1717 ;; Restore point and narrowing after adding new
1718 ;; category, to avoid moving to beginning of file when
1719 ;; moving marked items to a new category
1720 ;; (todos-move-item).
1723 (todos-add-category file0 cat
))))
1724 ;; If we decide not to add a category, exit without returning.
1728 (defun todos-validate-name (name type
)
1729 "Prompt for new NAME for TYPE until it is valid, then return it.
1730 TYPE can be either of the symbols `file' or `category'."
1731 (let ((categories todos-categories
)
1732 (files (mapcar 'todos-short-file-name todos-files
))
1735 (and (cond ((string= "" name
)
1737 (cond ((eq type
'file
)
1739 "Enter a non-empty file name: "
1740 ;; Empty string passed by todos-show to
1741 ;; prompt for initial Todos file.
1742 (concat "Initial file name ["
1743 todos-initial-file
"]: ")))
1744 ((eq type
'category
)
1746 "Enter a non-empty category name: "
1747 ;; Empty string passed by todos-show to
1748 ;; prompt for initial category of a new
1750 (concat "Initial category name ["
1751 todos-initial-category
"]: "))))))
1752 ((string-match "\\`\\s-+\\'" name
)
1754 "Enter a name that does not contain only white space: "))
1755 ((and (eq type
'file
) (member name files
))
1756 (setq prompt
"Enter a non-existing file name: "))
1757 ((and (eq type
'category
) (assoc name categories
))
1758 (setq prompt
"Enter a non-existing category name: ")))
1759 (setq name
(if (or (and (eq type
'file
) files
)
1760 (and (eq type
'category
) categories
))
1761 (completing-read prompt
(cond ((eq type
'file
)
1763 ((eq type
'category
)
1765 ;; Offer default initial name.
1766 (completing-read prompt
(if (eq type
'file
)
1769 nil nil
(if (eq type
'file
)
1771 todos-initial-category
))))))
1774 ;; Adapted from calendar-read-date and calendar-date-string.
1775 (defun todos-read-date (&optional arg mo yr
)
1776 "Prompt for Gregorian date and return it in the current format.
1778 With non-nil ARG, prompt for and return only the date component
1779 specified by ARG, which can be one of these symbols:
1780 `month' (prompt for name, return name or number according to
1781 value of `calendar-date-display-form'), `day' of month, or
1782 `year'. The value of each of these components can be `*',
1783 indicating an unspecified month, day, or year.
1785 When ARG is `day', non-nil arguments MO and YR determine the
1786 number of the last the day of the month."
1787 (let (year monthname month day
1788 dayname
) ; Needed by calendar-date-display-form.
1789 (when (or (not arg
) (eq arg
'year
))
1790 (while (if (natnump year
) (< year
1) (not (eq year
'*)))
1791 (setq year
(read-from-minibuffer
1792 "Year (>0 or RET for this year or * for any year): "
1793 nil nil t nil
(number-to-string
1794 (calendar-extract-year
1795 (calendar-current-date)))))))
1796 (when (or (not arg
) (eq arg
'month
))
1797 (let* ((marray todos-month-name-array
)
1798 (mlist (append marray nil
))
1799 (mabarray todos-month-abbrev-array
)
1800 (mablist (append mabarray nil
))
1801 (completion-ignore-case todos-completion-ignore-case
))
1802 (setq monthname
(completing-read
1803 "Month name (RET for current month, * for any month): "
1804 ;; (mapcar 'list (append marray nil))
1806 (calendar-month-name (calendar-extract-month
1807 (calendar-current-date)) t
))
1808 ;; month (cdr (assoc-string
1809 ;; monthname (calendar-make-alist marray nil nil
1811 month
(1+ (- (length mlist
)
1812 (length (or (member monthname mlist
)
1813 (member monthname mablist
))))))
1814 (setq monthname
(aref mabarray
(1- month
)))))
1815 (when (or (not arg
) (eq arg
'day
))
1816 (let ((last (let ((mm (or month mo
))
1818 ;; If month is unspecified, use a month with 31
1819 ;; days for checking day of month input. Does
1820 ;; Calendar do anything special when * is
1821 ;; currently a shorter month?
1822 (if (= mm
13) (setq mm
1))
1823 ;; If year is unspecified, use a leap year to
1825 (if (eq year
'*) (setq yy
2012))
1826 (calendar-last-day-of-month mm yy
))))
1827 (while (if (natnump day
) (or (< day
1) (> day last
)) (not (eq day
'*)))
1828 (setq day
(read-from-minibuffer
1829 (format "Day (1-%d or RET for today or * for any day): "
1831 nil nil t nil
(number-to-string
1832 (calendar-extract-day
1833 (calendar-current-date))))))))
1834 ;; Stringify read values (monthname is already a string).
1835 (and year
(setq year
(if (eq year
'*)
1837 (number-to-string year
))))
1838 (and day
(setq day
(if (eq day
'*)
1840 (number-to-string day
))))
1841 (and month
(setq month
(if (eq month
'*)
1843 (number-to-string month
))))
1845 (cond ((eq arg
'year
) year
)
1848 (if (memq 'month calendar-date-display-form
)
1851 (mapconcat 'eval calendar-date-display-form
""))))
1853 (defun todos-read-dayname ()
1854 "Choose name of a day of the week with completion and return it."
1855 (let ((completion-ignore-case todos-completion-ignore-case
))
1856 (completing-read "Enter a day name: "
1857 (append calendar-day-name-array nil
)
1860 (defun todos-read-time ()
1861 "Prompt for and return a valid clock time as a string.
1863 Valid time strings are those matching `diary-time-regexp'.
1864 Typing `<return>' at the prompt returns the current time, if the
1865 user option `todos-always-add-time-string' is non-nil, otherwise
1866 the empty string (i.e., no time string)."
1869 (setq answer
(read-string "Enter a clock time: " nil nil
1870 (when todos-always-add-time-string
1871 (substring (current-time-string) 11 16))))
1872 (when (or (string= "" answer
)
1873 (string-match diary-time-regexp answer
))
1877 ;; ---------------------------------------------------------------------------
1878 ;;; Item filtering infrastructure
1880 (defvar todos-multiple-filter-files nil
1881 "List of files selected from `todos-multiple-filter-files' widget.")
1883 (defvar todos-multiple-filter-files-widget nil
1884 "Variable holding widget created by `todos-multiple-filter-files'.")
1886 (defun todos-multiple-filter-files ()
1887 "Pop to a buffer with a widget for choosing multiple filter files."
1890 (require 'wid-edit
))
1891 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
1892 (pop-to-buffer (current-buffer))
1894 (kill-all-local-variables)
1895 (widget-insert "Select files for generating the top priorities list.\n\n")
1896 (setq todos-multiple-filter-files-widget
1898 `(set ,@(mapcar (lambda (x) (list 'const x
))
1899 (mapcar 'todos-short-file-name
1900 (funcall todos-files-function
))))))
1901 (widget-insert "\n")
1902 (widget-create 'push-button
1903 :notify
(lambda (widget &rest ignore
)
1904 (setq todos-multiple-filter-files
'quit
)
1906 (exit-recursive-edit))
1909 (widget-create 'push-button
1910 :notify
(lambda (&rest ignore
)
1911 (setq todos-multiple-filter-files
1914 (concat todos-files-directory
1917 todos-multiple-filter-files-widget
)))
1919 (exit-recursive-edit))
1921 (use-local-map widget-keymap
)
1923 (message "Click \"Apply\" after selecting files.")
1926 (defun todos-filter-items (filter file-list
)
1927 "Display a list of items from FILE-LIST that satisfy FILTER.
1928 The values of FILE-LIST and FILTER are passed from the calling
1929 commands. The files in FILE-LIST are either the current Todos
1930 file or those listed in `todos-filter-files' or chosen
1931 interactively. The values of FILTER can be `top' for top
1932 priority items, a cons of `top' and a number passed by the
1933 caller, `diary' for diary items, or `regexp' for items matching a
1934 regular expresion entered by the user."
1935 (let ((num (if (consp filter
) (cdr filter
) todos-show-priorities
))
1936 (buf (get-buffer-create todos-filtered-items-buffer
))
1937 (multifile (> (length file-list
) 1))
1938 regexp fname bufstr cat beg end done
)
1939 (if (null file-list
)
1940 (error "No files have been chosen for filtering")
1941 (with-current-buffer buf
1943 (kill-all-local-variables)
1944 (todos-filtered-items-mode))
1945 (when (eq filter
'regexp
)
1946 (setq regexp
(read-string "Enter a regular expression: ")))
1947 (save-current-buffer
1948 (dolist (f file-list
)
1949 ;; Before inserting file contents into temp buffer, save a modified
1950 ;; buffer visiting it.
1951 (let ((bf (find-buffer-visiting f
)))
1952 (when (buffer-modified-p bf
)
1953 (with-current-buffer bf
(save-buffer))))
1954 (setq fname
(todos-short-file-name f
))
1956 (when (and todos-filter-done-items
(eq filter
'regexp
))
1957 ;; If there is a corresponding archive file for the Todos file,
1958 ;; insert it first and add identifiers for todos-jump-to-item.
1959 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
1960 (when (file-exists-p arch
)
1961 (insert-file-contents arch
)
1962 ;; Delete Todos archive file categories sexp.
1963 (delete-region (line-beginning-position)
1964 (1+ (line-end-position)))
1967 (when (re-search-forward
1968 (concat (if todos-filter-done-items
1969 (concat "\\(?:" todos-done-string-start
1970 "\\|" todos-date-string-start
1972 todos-date-string-start
)
1973 todos-date-pattern
"\\(?: "
1974 diary-time-regexp
"\\)?"
1975 (if todos-filter-done-items
1977 (regexp-quote todos-nondiary-end
)) "?")
1979 (insert "(archive) "))
1981 (insert-file-contents f
)
1982 ;; Delete Todos file categories sexp.
1983 (delete-region (line-beginning-position) (1+ (line-end-position)))
1985 ;; Unless the number of top priorities to show was
1986 ;; passed by the caller, the file-wide value from
1987 ;; `todos-priorities-rules', if non-nil, overrides
1988 ;; `todos-show-priorities'.
1989 (unless (consp filter
)
1990 (setq fnum
(or (nth 1 (assoc f todos-priorities-rules
))
1991 todos-show-priorities
)))
1992 (while (re-search-forward
1993 (concat "^" (regexp-quote todos-category-beg
) "\\(.+\\)\n")
1995 (setq cat
(match-string 1))
1997 ;; Unless the number of top priorities to show was
1998 ;; passed by the caller, the category-wide value
1999 ;; from `todos-priorities-rules', if non-nil,
2000 ;; overrides a non-nil file-wide value from
2001 ;; `todos-priorities-rules' as well as
2002 ;; `todos-show-priorities'.
2003 (unless (consp filter
)
2004 (let ((cats (nth 2 (assoc f todos-priorities-rules
))))
2005 (setq cnum
(or (cdr (assoc cat cats
)) fnum
))))
2006 (delete-region (match-beginning 0) (match-end 0))
2007 (setq beg
(point)) ; First item in the current category.
2008 (setq end
(if (re-search-forward
2009 (concat "^" (regexp-quote todos-category-beg
))
2015 (if (re-search-forward
2016 (concat "\n" (regexp-quote todos-category-done
))
2020 (unless (and todos-filter-done-items
(eq filter
'regexp
))
2021 ;; Leave done items.
2022 (delete-region done end
)
2024 (narrow-to-region beg end
) ; Process only current category.
2025 (goto-char (point-min))
2026 ;; Apply the filter.
2027 (cond ((eq filter
'diary
)
2029 (if (looking-at (regexp-quote todos-nondiary-start
))
2031 (todos-forward-item))))
2032 ((eq filter
'regexp
)
2034 (if (looking-at todos-item-start
)
2035 (if (string-match regexp
(todos-item-string))
2036 (todos-forward-item)
2037 (todos-remove-item))
2038 ;; Kill lines that aren't part of a todo or done
2039 ;; item (empty or todos-category-done).
2040 (delete-region (line-beginning-position)
2041 (1+ (line-end-position))))
2042 ;; If last todo item in file matches regexp and
2043 ;; there are no following done items,
2044 ;; todos-category-done string is left dangling,
2045 ;; because todos-forward-item jumps over it.
2048 (concat (regexp-quote todos-done-string
)
2050 (delete-region (point) (progn
2053 (t ; Filter top priority items.
2054 (setq num
(or cnum fnum num
))
2056 (todos-forward-item num
))))
2058 ;; Delete non-top-priority items.
2059 (unless (member filter
'(diary regexp
))
2060 (delete-region beg end
))
2061 (goto-char (point-min))
2062 ;; Add file (if using multiple files) and category tags to
2065 (when (re-search-forward
2066 (concat (if todos-filter-done-items
2067 (concat "\\(?:" todos-done-string-start
2068 "\\|" todos-date-string-start
2070 todos-date-string-start
)
2071 todos-date-pattern
"\\(?: " diary-time-regexp
2072 "\\)?" (if todos-filter-done-items
2074 (regexp-quote todos-nondiary-end
))
2078 (when (looking-at "(archive) ") (goto-char (match-end 0)))
2079 (insert (if multifile
(concat fname
":") "") cat
"]"))
2082 (setq bufstr
(buffer-string))
2083 (with-current-buffer buf
2084 (let (buffer-read-only)
2085 (insert bufstr
)))))))
2086 (set-window-buffer (selected-window) (set-buffer buf
))
2087 (todos-prefix-overlays)
2088 (goto-char (point-min)))))
2090 (defun todos-set-top-priorities (&optional arg
)
2091 "Set number of top priorities shown by `todos-top-priorities'.
2092 With non-nil ARG, set the number only for the current Todos
2093 category; otherwise, set the number for all categories in the
2096 Calling this function via either of the commands
2097 `todos-set-top-priorities-in-file' or
2098 `todos-set-top-priorities-in-category' is the recommended way to
2099 set the user customizable option `todos-priorities-rules'."
2100 (let* ((cat (todos-current-category))
2101 (file todos-current-todos-file
)
2102 (rules todos-priorities-rules
)
2103 (frule (assoc-string file rules
))
2104 (crule (assoc-string cat
(nth 2 frule
)))
2105 (crules (nth 2 frule
))
2106 (cur (or (if arg
(cdr crule
) (nth 1 frule
))
2107 todos-show-priorities
))
2108 (prompt (if arg
(concat "Number of top priorities in this category"
2109 " (currently %d): ")
2110 (concat "Default number of top priorities per category"
2111 " in this file (currently %d): ")))
2116 (setq new
(read-number (format prompt cur0
))
2117 prompt
"Enter a non-negative number: "
2120 (append (delete crule crules
) (list (cons cat new
)))
2121 (append (list file new
) (list crules
))))
2122 (setq rules
(cons (if arg
2123 (list file cur nrule
)
2125 (delete frule rules
)))
2126 (customize-save-variable 'todos-priorities-rules rules
)
2127 (todos-prefix-overlays)))
2129 (defun todos-filtered-buffer-name (buffer-type file-list
)
2130 "Rename Todos filtered buffer using BUFFER-TYPE and FILE-LIST.
2132 The new name is constructed from the string BUFFER-TYPE, which
2133 refers to one of the top priorities, diary or regexp item
2134 filters, and the names of the filtered files in FILE-LIST. Used
2135 in Todos Filter Items mode."
2136 (let* ((multi (> (length file-list
) 1))
2137 (fnames (mapconcat (lambda (f) (todos-short-file-name f
))
2139 (rename-buffer (format (concat "%s for file" (if multi
"s" "")
2140 " \"%s\"") buffer-type fnames
))))
2142 (defun todos-find-item (str)
2143 "Search for saved top priority item STR in its Todos file.
2144 Return the list (FOUND FILE CAT), where CAT and FILE are the
2145 item's category and file, and FOUND is a cons cell if the search
2146 succeeds, whose car is the start of the item in FILE and whose
2147 cdr is `done' if the item is now a done item, `changed' if its
2148 priority has changed or its text was truncated or augmented, and
2150 (string-match (concat (if todos-filter-done-items
2151 (concat "\\(?:" todos-done-string-start
"\\|"
2152 todos-date-string-start
"\\)")
2153 todos-date-string-start
)
2154 todos-date-pattern
"\\(?: " diary-time-regexp
"\\)?"
2155 (if todos-filter-done-items
2157 (regexp-quote todos-nondiary-end
)) "?"
2158 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
2159 "\\(?1:.*\\)\\]\\).*$") str
)
2160 (let ((cat (match-string 1 str
))
2161 (file (match-string 2 str
))
2162 (archive (string= (match-string 3 str
) "(archive) "))
2163 (filcat (match-string 4 str
))
2166 (setq str
(replace-match "" nil nil str
4))
2168 (while (search-backward filcat nil t
)
2169 (setq tpriority
(1+ tpriority
))))
2171 (concat todos-files-directory
(substring file
0 -
1)
2172 (if archive
".toda" ".todo"))
2174 (concat (file-name-sans-extension
2175 todos-global-current-todos-file
) ".toda")
2176 todos-global-current-todos-file
)))
2177 (find-file-noselect file
)
2178 (with-current-buffer (find-buffer-visiting file
)
2180 (goto-char (point-min))
2181 (let ((beg (re-search-forward
2182 (concat "^" (regexp-quote (concat todos-category-beg cat
)) "$")
2184 (done (save-excursion
2186 (concat "^" (regexp-quote todos-category-done
)) nil t
)))
2187 (end (save-excursion
2188 (or (re-search-forward
2189 (concat "^" (regexp-quote todos-category-beg
))
2192 (setq found
(when (search-forward str end t
)
2193 (goto-char (match-beginning 0))))
2196 (cons found
(if (> (point) done
)
2198 (let ((cpriority 1))
2200 ;; Not top item in category.
2201 (while (> (point) (1+ beg
))
2202 (let ((opoint (point)))
2203 (todos-backward-item)
2204 ;; Can't move backward beyond
2205 ;; first item in file.
2206 (unless (= (point) opoint
)
2207 (setq cpriority
(1+ cpriority
))))))
2208 (if (and (= tpriority cpriority
)
2209 ;; Proper substring is not the same.
2210 (string= (todos-item-string)
2214 (list found file cat
)))
2216 (defun todos-check-top-priorities ()
2217 "Return a message saying whether top priorities file is up to date."
2221 (let* ((item (todos-item-string))
2222 (found (car (todos-find-item item
))))
2223 (unless (eq (cdr found
) 'same
)
2225 (overlay-put (make-overlay (todos-item-start) (todos-item-end))
2226 'face
'todos-search
))
2227 (setq count
(1+ count
))))
2228 ;; (throw 'old (message "The marked item is not up to date.")))
2229 (todos-forward-item))
2231 (message "Top priorities file is up to date.")
2232 (message (concat "The highlighted item" (if (= count
1) " is " "s are ")
2234 ;; "\nType <return> on item for details."
2237 (defun todos-top-priorities-filename ()
2239 (let ((bufname (buffer-name)))
2240 (string-match "\"\\([^\"]+\\)\"" bufname
)
2241 (let* ((filename-str (substring bufname
(match-beginning 1) (match-end 1)))
2242 (filename-base (replace-regexp-in-string ", " "-" filename-str
)))
2243 (concat todos-files-directory filename-base
".todt"))))
2245 (defun todos-save-top-priorities-buffer ()
2247 (let ((filename (todos-top-priorities-filename)))
2248 (if (file-exists-p filename
)
2250 (write-region nil nil filename nil t nil t
))))
2252 ;; ---------------------------------------------------------------------------
2253 ;;; Sorting and display routines for Todos Categories mode.
2255 (defun todos-longest-category-name-length (categories)
2256 "Return the length of the longest name in list CATEGORIES."
2258 (dolist (c categories longest
)
2259 (setq longest
(max longest
(length c
))))))
2261 (defun todos-adjusted-category-label-length ()
2262 "Return adjusted length of category label button.
2263 The adjustment ensures proper tabular alignment in Todos
2265 (let* ((categories (mapcar 'car todos-categories
))
2266 (longest (todos-longest-category-name-length categories
))
2267 (catlablen (length todos-categories-category-label
))
2268 (lc-diff (- longest catlablen
)))
2269 (if (and (natnump lc-diff
)
2270 (eq (logand lc-diff
1) 1)) ; oddp from cl.el
2272 (max longest catlablen
))))
2274 (defun todos-padded-string (str)
2275 "Return category name or label string STR padded with spaces.
2276 The placement of the padding is determined by the value of user
2277 option `todos-categories-align'."
2278 (let* ((len (todos-adjusted-category-label-length))
2279 (strlen (length str
))
2280 (strlen-odd (eq (logand strlen
1) 1))
2281 (padding (max 0 (/ (- len strlen
) 2)))
2282 (padding-left (cond ((eq todos-categories-align
'left
) 0)
2283 ((eq todos-categories-align
'center
) padding
)
2284 ((eq todos-categories-align
'right
)
2285 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
2286 (padding-right (cond ((eq todos-categories-align
'left
)
2287 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
2288 ((eq todos-categories-align
'center
)
2289 (if strlen-odd
(1+ padding
) padding
))
2290 ((eq todos-categories-align
'right
) 0))))
2291 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
2293 (defvar todos-descending-counts nil
2294 "List of keys for category counts sorted in descending order.")
2296 (defun todos-sort (list &optional key
)
2297 "Return a copy of LIST, possibly sorted according to KEY."
2298 (let* ((l (copy-sequence list
))
2299 (fn (if (eq key
'alpha
)
2300 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
2301 (lambda (x) (todos-get-count key x
))))
2302 ;; Keep track of whether the last sort by key was descending or
2304 (descending (member key todos-descending-counts
))
2305 (cmp (if (eq key
'alpha
)
2307 (if descending
'< '>)))
2308 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
2309 (t2 (funcall fn
(car s2
))))
2310 (funcall cmp t1 t2
)))))
2312 (setq l
(sort l pred
))
2313 ;; Switch between descending and ascending sort order.
2315 (setq todos-descending-counts
2316 (delete key todos-descending-counts
))
2317 (push key todos-descending-counts
)))
2320 (defun todos-display-sorted (type)
2321 "Keep point on the TYPE count sorting button just clicked."
2322 (let ((opoint (point)))
2323 (todos-update-categories-display type
)
2324 (goto-char opoint
)))
2326 (defun todos-label-to-key (label)
2327 "Return symbol for sort key associated with LABEL."
2329 (cond ((string= label todos-categories-category-label
)
2331 ((string= label todos-categories-todo-label
)
2333 ((string= label todos-categories-diary-label
)
2335 ((string= label todos-categories-done-label
)
2337 ((string= label todos-categories-archived-label
)
2338 (setq key
'archived
)))
2341 (defun todos-insert-sort-button (label)
2342 "Insert button for displaying categories sorted by item counts.
2343 LABEL determines which type of count is sorted."
2344 (setq str
(if (string= label todos-categories-category-label
)
2345 (todos-padded-string label
)
2348 (setq end
(+ beg
(length str
)))
2349 (insert-button str
'face nil
2352 (let ((key (todos-label-to-key ,label
)))
2353 (if (and (member key todos-descending-counts
)
2356 ;; If display is alphabetical, switch back to
2357 ;; category priority order.
2358 (todos-display-sorted nil
)
2359 (setq todos-descending-counts
2360 (delete key todos-descending-counts
)))
2361 (todos-display-sorted key
)))))
2362 (setq ovl
(make-overlay beg end
))
2363 (overlay-put ovl
'face
'todos-button
))
2365 (defun todos-total-item-counts ()
2366 "Return a list of total item counts for the current file."
2367 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i
))
2368 (mapcar 'cdr todos-categories
))))
2371 (defvar todos-categories-category-number
0
2372 "Variable for numbering categories in Todos Categories mode.")
2374 (defun todos-insert-category-line (cat &optional nonum
)
2375 "Insert button with category CAT's name and item counts.
2376 With non-nil argument NONUM show only these; otherwise, insert a
2377 number in front of the button indicating the category's priority.
2378 The number and the category name are separated by the string
2379 which is the value of the user option
2380 `todos-categories-number-separator'."
2381 (let ((archive (member todos-current-todos-file todos-archives
))
2382 (num todos-categories-category-number
)
2383 (str (todos-padded-string cat
))
2385 (setq num
(1+ num
) todos-categories-category-number num
)
2388 (make-string (+ 4 (length todos-categories-number-separator
))
2390 (format " %3d%s" num todos-categories-number-separator
))
2392 (mapconcat (lambda (elt)
2394 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
2395 (format "%3d" (todos-get-count (cdr elt
) cat
)) ; count
2396 ;; Add an extra space if label length is odd
2397 ;; (using def of oddp from cl.el).
2398 (if (eq (logand (length (car elt
)) 1) 1) " ")))
2400 (list (cons todos-categories-done-label
'done
))
2401 (list (cons todos-categories-todo-label
'todo
)
2402 (cons todos-categories-diary-label
'diary
)
2403 (cons todos-categories-done-label
'done
)
2404 (cons todos-categories-archived-label
2407 " ") ; So highlighting of last column is consistent with the others.
2408 'face
(if (and todos-skip-archived-categories
2409 (zerop (todos-get-count 'todo cat
))
2410 (zerop (todos-get-count 'done cat
))
2411 (not (zerop (todos-get-count 'archived cat
))))
2412 'todos-archived-only
2414 'action
`(lambda (button) (let ((buf (current-buffer)))
2415 (todos-jump-to-category nil
,cat
)
2416 (kill-buffer buf
))))
2417 ;; Highlight the sorted count column.
2418 (let* ((beg (+ opoint
7 (length str
)))
2420 (cond ((eq nonum
'todo
)
2421 (setq beg
(+ beg
1 (/ (length todos-categories-todo-label
) 2))))
2423 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
2424 2 (/ (length todos-categories-diary-label
) 2))))
2426 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
2427 2 (length todos-categories-diary-label
)
2428 2 (/ (length todos-categories-done-label
) 2))))
2429 ((eq nonum
'archived
)
2430 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
2431 2 (length todos-categories-diary-label
)
2432 2 (length todos-categories-done-label
)
2433 2 (/ (length todos-categories-archived-label
) 2)))))
2434 (unless (= beg
(+ opoint
7 (length str
))) ; Don't highlight categories.
2435 (setq end
(+ beg
4))
2436 (setq ovl
(make-overlay beg end
))
2437 (overlay-put ovl
'face
'todos-sorted-column
)))
2440 (defun todos-display-categories-1 ()
2441 "Prepare buffer for displaying table of categories and item counts."
2442 (unless (eq major-mode
'todos-categories-mode
)
2443 (setq todos-global-current-todos-file
2444 (or todos-current-todos-file
2445 (todos-absolute-file-name todos-default-todos-file
)))
2446 (set-window-buffer (selected-window)
2447 (set-buffer (get-buffer-create todos-categories-buffer
)))
2448 (kill-all-local-variables)
2449 (todos-categories-mode)
2450 (let ((archive (member todos-current-todos-file todos-archives
))
2453 (insert (format (concat "Category counts for Todos "
2454 (if archive
"archive" "file")
2456 (todos-short-file-name todos-current-todos-file
)))
2458 ;; Make space for the column of category numbers.
2459 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32))
2460 ;; Add the category and item count buttons (if this is the list of
2461 ;; categories in an archive, show only done item counts).
2462 (todos-insert-sort-button todos-categories-category-label
)
2465 (insert (make-string 3 32))
2466 (todos-insert-sort-button todos-categories-done-label
))
2467 (insert (make-string 3 32))
2468 (todos-insert-sort-button todos-categories-todo-label
)
2469 (insert (make-string 2 32))
2470 (todos-insert-sort-button todos-categories-diary-label
)
2471 (insert (make-string 2 32))
2472 (todos-insert-sort-button todos-categories-done-label
)
2473 (insert (make-string 2 32))
2474 (todos-insert-sort-button todos-categories-archived-label
))
2477 (defun todos-update-categories-display (sortkey)
2479 (let* ((cats0 todos-categories
)
2480 (cats (todos-sort cats0 sortkey
))
2481 (archive (member todos-current-todos-file todos-archives
))
2482 (todos-categories-category-number 0)
2483 ;; Find start of Category button if we just entered Todos Categories
2485 (pt (if (eq (point) (point-max))
2488 (goto-char (next-single-char-property-change
2489 (point) 'face nil
(line-end-position))))))
2492 (delete-region (point) (point-max))
2493 ;; Fill in the table with buttonized lines, each showing a category and
2495 (mapc (lambda (cat) (todos-insert-category-line cat sortkey
))
2498 ;; Add a line showing item count totals.
2499 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32)
2500 (todos-padded-string todos-categories-totals-label
)
2504 (make-string (1+ (/ (length (car elt
)) 2)) 32)
2505 (format "%3d" (nth (cdr elt
) (todos-total-item-counts)))
2506 ;; Add an extra space if label length is odd (using
2507 ;; definition of oddp from cl.el).
2508 (if (eq (logand (length (car elt
)) 1) 1) " ")))
2510 (list (cons todos-categories-done-label
2))
2511 (list (cons todos-categories-todo-label
0)
2512 (cons todos-categories-diary-label
1)
2513 (cons todos-categories-done-label
2)
2514 (cons todos-categories-archived-label
3)))
2516 ;; Put cursor on Category button initially.
2517 (if pt
(goto-char pt
))
2518 (setq buffer-read-only t
)))
2520 ;; ---------------------------------------------------------------------------
2521 ;;; Routines for generating Todos insertion commands and key bindings
2523 ;; Can either of these be included in Emacs? The originals are GFDL'd.
2525 ;; Slightly reformulated from
2526 ;; http://rosettacode.org/wiki/Power_set#Common_Lisp.
2527 (defun powerset-recursive (l)
2531 (let ((prev (powerset-recursive (cdr l
))))
2532 (append (mapcar (lambda (elt) (cons (car l
) elt
))
2536 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C
2537 (defun powerset-bitwise (l)
2538 (let ((binnum (lsh 1 (length l
)))
2543 (while (not (zerop bits
))
2544 (let ((arg (pop ll
)))
2545 (unless (zerop (logand bits
1))
2546 (setq elt
(append elt
(list arg
))))
2547 (setq bits
(lsh bits -
1))))
2548 (setq pset
(append pset
(list elt
)))
2552 ;; (defalias 'todos-powerset 'powerset-recursive)
2553 (defalias 'todos-powerset
'powerset-bitwise
)
2555 ;; Return list of lists of non-nil atoms produced from ARGLIST. The elements
2556 ;; of ARGLIST may be atoms or lists.
2557 (defun todos-gen-arglists (arglist)
2560 (let ((arg (pop arglist
)))
2561 (cond ((symbolp arg
)
2562 (setq arglists
(if arglists
2563 (mapcar (lambda (l) (push arg l
)) arglists
)
2564 (list (push arg arglists
)))))
2568 (if (= 1 (length arglists
))
2569 (apply (lambda (l) (push a l
)) arglists
)
2570 (mapcar (lambda (l) (push a l
)) arglists
)))
2572 (setq arglists
(mapcar 'reverse
(apply 'append
(mapc 'car arglists
))))))
2574 (defvar todos-insertion-commands-args-genlist
2575 '(diary nonmarking
(calendar date dayname
) time
(here region
))
2576 "Generator list for argument lists of Todos insertion commands.")
2578 (defvar todos-insertion-commands-args
2579 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist
))
2581 (setq res
(remove-duplicates
2582 (apply 'append
(mapcar 'todos-powerset argslist
)) :test
'equal
))
2584 (unless (= 5 (length l
))
2585 (let ((v (make-vector 5 nil
)) elt
)
2588 (cond ((eq elt
'diary
)
2590 ((eq elt
'nonmarking
)
2592 ((or (eq elt
'calendar
)
2601 (setq l
(append v nil
))))
2602 (setq new
(append new
(list l
))))
2604 "List of all argument lists for Todos insertion commands.")
2606 (defun todos-insertion-command-name (arglist)
2607 "Generate Todos insertion command name from ARGLIST."
2608 (replace-regexp-in-string
2610 (replace-regexp-in-string
2612 (concat "todos-item-insert-"
2613 (mapconcat (lambda (e) (if e
(symbol-name e
))) arglist
"-")))))
2615 (defvar todos-insertion-commands-names
2617 (todos-insertion-command-name l
))
2618 todos-insertion-commands-args
)
2619 "List of names of Todos insertion commands.")
2621 (defmacro todos-define-insertion-command
(&rest args
)
2622 (let ((name (intern (todos-insertion-command-name args
)))
2627 (arg4 (nth 4 args
)))
2628 `(defun ,name
(&optional arg
&rest args
)
2629 "Todos item insertion command generated from ARGS."
2630 (interactive (list current-prefix-arg
))
2631 (todos-insert-item arg
',arg0
',arg1
',arg2
',arg3
',arg4
))))
2633 (defvar todos-insertion-commands
2635 (eval `(todos-define-insertion-command ,@c
)))
2636 todos-insertion-commands-args
)
2637 "List of Todos insertion commands.")
2639 (defvar todos-insertion-commands-arg-key-list
2640 '(("diary" "y" "yy")
2641 ("nonmarking" "k" "kk")
2642 ("calendar" "c" "cc")
2644 ("dayname" "n" "nn")
2650 (defun todos-insertion-key-bindings (map)
2652 (dolist (c todos-insertion-commands
)
2654 (cname (symbol-name c
)))
2656 (let ((arg (nth 0 l
))
2659 (if (string-match (concat (regexp-quote arg
) "\\_>") cname
)
2660 (setq key
(concat key key2
)))
2661 (if (string-match (concat (regexp-quote arg
) ".+") cname
)
2662 (setq key
(concat key key1
)))))
2663 todos-insertion-commands-arg-key-list
)
2664 (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname
)
2665 (setq key
(concat key
"i")))
2666 (define-key map key c
))))
2668 (defvar todos-insertion-map
2669 (let ((map (make-keymap)))
2670 (todos-insertion-key-bindings map
)
2671 (define-key map
"p" 'todos-copy-item
)
2673 "Keymap for Todos mode insertion commands.")
2675 ;; ---------------------------------------------------------------------------
2676 ;;; Key maps and menus
2678 (defvar todos-key-bindings
2681 ("Cd" . todos-display-categories
) ;FIXME: Fc todos-file-categories?
2682 ("H" . todos-highlight-item
)
2683 ("N" . todos-hide-show-item-numbering
)
2684 ("D" . todos-hide-show-date-time
)
2685 ("*" . todos-mark-unmark-item
)
2686 ("C*" . todos-mark-category
)
2687 ("Cu" . todos-unmark-category
)
2688 ("PP" . todos-print
)
2689 ("PF" . todos-print-to-file
)
2690 ("v" . todos-hide-show-done-items
)
2691 ("V" . todos-show-done-only
)
2692 ("As" . todos-show-archive
)
2693 ("Ac" . todos-choose-archive
)
2694 ;; ("Y" . todos-diary-items)
2695 ("Fe" . todos-edit-multiline
)
2696 ("Fh" . todos-highlight-item
)
2697 ("Fn" . todos-hide-show-item-numbering
)
2698 ("Fd" . todos-hide-show-date-time
)
2699 ("Ftt" . todos-top-priorities
)
2700 ("Ftm" . todos-top-priorities-multifile
)
2701 ("Fts" . todos-set-top-priorities-in-file
)
2702 ("Cts" . todos-set-top-priorities-in-category
)
2703 ("Fyy" . todos-diary-items
)
2704 ("Fym" . todos-diary-items-multifile
)
2705 ("Fxx" . todos-regexp-items
)
2706 ("Fxm" . todos-regexp-items-multifile
)
2708 ("f" . todos-forward-category
)
2709 ("b" . todos-backward-category
)
2710 ("j" . todos-jump-to-category
)
2711 ("n" . todos-forward-item
)
2712 ("p" . todos-backward-item
)
2713 ("S" . todos-search
)
2714 ("X" . todos-clear-matches
)
2716 ("Fa" . todos-add-file
)
2717 ("Ca" . todos-add-category
)
2718 ("Cr" . todos-rename-category
)
2719 ("Cg" . todos-merge-category
)
2720 ("Cm" . todos-move-category
)
2721 ("Ck" . todos-delete-category
)
2722 ("d" . todos-item-done
)
2723 ("ee" . todos-edit-item
)
2724 ("em" . todos-edit-multiline-item
)
2725 ("eh" . todos-edit-item-header
)
2726 ("edc" . todos-edit-item-date-from-calendar
)
2727 ("edt" . todos-edit-item-date-to-today
)
2728 ("edn" . todos-edit-item-date-day-name
)
2729 ("edy" . todos-edit-item-date-year
)
2730 ("edm" . todos-edit-item-date-month
)
2731 ("edd" . todos-edit-item-date-day
)
2732 ("et" . todos-edit-item-time
)
2733 ("eyy" . todos-edit-item-diary-inclusion
)
2734 ;; ("" . todos-edit-category-diary-inclusion)
2735 ("eyn" . todos-edit-item-diary-nonmarking
)
2736 ;;("" . todos-edit-category-diary-nonmarking)
2737 ("ec" . todos-done-item-add-edit-or-delete-comment
)
2738 ("i" .
,todos-insertion-map
)
2739 ("k" . todos-delete-item
) ;FIXME: not single letter?
2740 ("m" . todos-move-item
)
2741 ("r" . todos-raise-item-priority
)
2742 ("l" . todos-lower-item-priority
)
2743 ("#" . todos-set-item-priority
)
2744 ("u" . todos-item-undo
)
2745 ("Ad" . todos-archive-done-item
) ;FIXME: ad
2746 ("AD" . todos-archive-category-done-items
) ;FIXME: aD or C-u ad ?
2749 ([remap newline
] . newline-and-indent
)
2751 "Alist pairing keys defined in Todos modes and their bindings.")
2753 (defvar todos-mode-map
2754 (let ((map (make-keymap)))
2755 ;; Don't suppress digit keys, so they can supply prefix arguments.
2756 (suppress-keymap map
)
2757 (dolist (ck todos-key-bindings
)
2758 (define-key map
(car ck
) (cdr ck
)))
2760 "Todos mode keymap.")
2763 todos-menu todos-mode-map
"Todos Menu"
2766 ["Next Item" todos-forward-item t
]
2767 ["Previous Item" todos-backward-item t
]
2769 ["Next Category" todos-forward-category t
]
2770 ["Previous Category" todos-backward-category t
]
2771 ["Jump to Category" todos-jump-to-category t
]
2773 ["Search Todos File" todos-search t
]
2774 ["Clear Highlighting on Search Matches" todos-category-done t
])
2776 ["List Current Categories" todos-display-categories t
]
2777 ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
2778 ["Turn Item Highlighting on/off" todos-highlight-item t
]
2779 ["Turn Item Numbering on/off" todos-hide-show-item-numbering t
]
2780 ["Turn Item Time Stamp on/off" todos-hide-show-date-time t
]
2781 ["View/Hide Done Items" todos-hide-show-done-items t
]
2783 ["View Diary Items" todos-diary-items t
]
2784 ["View Top Priority Items" todos-top-priorities t
]
2785 ["View Multifile Top Priority Items" todos-top-priorities-multifile t
]
2787 ["Print Category" todos-print t
])
2789 ["Insert New Item" todos-insert-item t
]
2790 ["Insert Item Here" todos-insert-item-here t
]
2791 ("More Insertion Commands")
2792 ["Edit Item" todos-edit-item t
]
2793 ["Edit Multiline Item" todos-edit-multiline t
]
2794 ["Edit Item Header" todos-edit-item-header t
]
2795 ["Edit Item Date" todos-edit-item-date t
]
2796 ["Edit Item Time" todos-edit-item-time t
]
2798 ["Lower Item Priority" todos-lower-item-priority t
]
2799 ["Raise Item Priority" todos-raise-item-priority t
]
2800 ["Set Item Priority" todos-set-item-priority t
]
2801 ["Move (Recategorize) Item" todos-move-item t
]
2802 ["Delete Item" todos-delete-item t
]
2803 ["Undo Done Item" todos-item-undo t
]
2804 ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t
]
2805 ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t
]
2806 ["Mark & Hide Done Item" todos-item-done t
]
2807 ["Archive Done Items" todos-archive-category-done-items t
]
2809 ["Add New Todos File" todos-add-file t
]
2810 ["Add New Category" todos-add-category t
]
2811 ["Delete Current Category" todos-delete-category t
]
2812 ["Rename Current Category" todos-rename-category t
]
2814 ["Save Todos File" todos-save t
]
2817 ["Quit" todos-quit t
]
2820 (defvar todos-archive-mode-map
2821 (let ((map (make-sparse-keymap)))
2822 (suppress-keymap map t
)
2823 ;; navigation commands
2824 (define-key map
"f" 'todos-forward-category
)
2825 (define-key map
"b" 'todos-backward-category
)
2826 (define-key map
"j" 'todos-jump-to-category
)
2827 (define-key map
"n" 'todos-forward-item
)
2828 (define-key map
"p" 'todos-backward-item
)
2830 (define-key map
"C" 'todos-display-categories
)
2831 (define-key map
"H" 'todos-highlight-item
)
2832 (define-key map
"N" 'todos-hide-show-item-numbering
)
2833 ;; (define-key map "" 'todos-hide-show-date-time)
2834 (define-key map
"P" 'todos-print
)
2835 (define-key map
"q" 'todos-quit
)
2836 (define-key map
"s" 'todos-save
)
2837 (define-key map
"S" 'todos-search
)
2838 (define-key map
"t" 'todos-show
)
2839 (define-key map
"u" 'todos-unarchive-items
)
2840 (define-key map
"U" 'todos-unarchive-category
)
2842 "Todos Archive mode keymap.")
2844 (defvar todos-edit-mode-map
2845 (let ((map (make-sparse-keymap)))
2846 (define-key map
"\C-x\C-q" 'todos-edit-quit
)
2847 (define-key map
[remap newline
] 'newline-and-indent
)
2849 "Todos Edit mode keymap.")
2851 (defvar todos-categories-mode-map
2852 (let ((map (make-sparse-keymap)))
2853 (suppress-keymap map t
)
2854 (define-key map
"c" 'todos-display-categories-alphabetically-or-by-priority
)
2855 (define-key map
"t" 'todos-display-categories-sorted-by-todo
)
2856 (define-key map
"y" 'todos-display-categories-sorted-by-diary
)
2857 (define-key map
"d" 'todos-display-categories-sorted-by-done
)
2858 (define-key map
"a" 'todos-display-categories-sorted-by-archived
)
2859 (define-key map
"l" 'todos-lower-category-priority
)
2860 (define-key map
"+" 'todos-lower-category-priority
)
2861 (define-key map
"r" 'todos-raise-category-priority
)
2862 (define-key map
"-" 'todos-raise-category-priority
)
2863 (define-key map
"n" 'todos-forward-button
)
2864 (define-key map
"p" 'todos-backward-button
)
2865 (define-key map
[tab] 'todos-forward-button)
2866 (define-key map [backtab] 'todos-backward-button)
2867 (define-key map "q" 'todos-quit)
2868 ;; (define-key map "A" 'todos-add-category)
2869 ;; (define-key map "D" 'todos-delete-category)
2870 ;; (define-key map "R" 'todos-rename-category)
2872 "Todos Categories mode keymap.")
2874 (defvar todos-filtered-items-mode-map
2875 (let ((map (make-keymap)))
2876 (suppress-keymap map t)
2877 ;; navigation commands
2878 (define-key map "j" 'todos-jump-to-item)
2879 (define-key map [remap newline] 'todos-jump-to-item)
2880 (define-key map "n" 'todos-forward-item)
2881 (define-key map "p" 'todos-backward-item)
2882 (define-key map "H" 'todos-highlight-item)
2883 (define-key map "N" 'todos-hide-show-item-numbering)
2884 (define-key map "D" 'todos-hide-show-date-time)
2885 (define-key map "P" 'todos-print)
2886 (define-key map "q" 'todos-quit)
2887 (define-key map "s" 'todos-save)
2889 (define-key map "l" 'todos-lower-item-priority)
2890 (define-key map "r" 'todos-raise-item-priority)
2891 (define-key map "#" 'todos-set-item-priority)
2893 "Todos Top Priorities mode keymap.")
2895 ;; ---------------------------------------------------------------------------
2896 ;;; Mode definitions
2898 (defun todos-modes-set-1 ()
2900 (set (make-local-variable 'font-lock-defaults) '(todos-font-lock-keywords t))
2901 (set (make-local-variable 'indent-line-function) 'todos-indent)
2902 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2904 (defun todos-modes-set-2 ()
2906 (add-to-invisibility-spec 'todos)
2907 (setq buffer-read-only t)
2908 (set (make-local-variable 'hl-line-range-function)
2909 (lambda() (when (todos-item-end)
2910 (cons (todos-item-start) (todos-item-end))))))
2912 (defun todos-modes-set-3 ()
2914 (set (make-local-variable 'todos-categories) (todos-set-categories))
2915 (set (make-local-variable 'todos-category-number) 1)
2916 (add-hook 'find-file-hook 'todos-display-as-todos-file nil t))
2918 (put 'todos-mode 'mode-class 'special)
2920 (define-derived-mode todos-mode special-mode "Todos"
2921 "Major mode for displaying, navigating and editing Todo lists.
2924 (easy-menu-add todos-menu)
2928 ;; Initialize todos-current-todos-file.
2929 (when (member (file-truename (buffer-file-name))
2930 (funcall todos-files-function))
2931 (set (make-local-variable 'todos-current-todos-file)
2932 (file-truename (buffer-file-name))))
2933 (set (make-local-variable 'todos-show-done-only) nil)
2934 (set (make-local-variable 'todos-categories-with-marks) nil)
2935 (add-hook 'find-file-hook 'todos-add-to-buffer-list nil t)
2936 (add-hook 'post-command-hook 'todos-update-buffer-list nil t)
2937 (when todos-show-current-file
2938 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
2939 (add-hook 'window-configuration-change-hook
2940 'todos-reset-and-enable-done-separator nil t)
2941 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
2943 (put 'todos-archive-mode 'mode-class 'special)
2945 ;; If todos-mode is parent, all todos-mode key bindings appear to be
2946 ;; available in todos-archive-mode (e.g. shown by C-h m).
2947 (define-derived-mode todos-archive-mode special-mode "Todos-Arch"
2948 "Major mode for archived Todos categories.
2950 \\{todos-archive-mode-map}"
2954 (set (make-local-variable 'todos-current-todos-file)
2955 (file-truename (buffer-file-name)))
2956 (set (make-local-variable 'todos-show-done-only) t))
2958 (defun todos-mode-external-set ()
2960 (set (make-local-variable 'todos-current-todos-file)
2961 todos-global-current-todos-file)
2962 (let ((cats (with-current-buffer
2963 ;; Can't use find-buffer-visiting when
2964 ;; `todos-display-categories' is called on first
2965 ;; invocation of `todos-show', since there is then
2966 ;; no buffer visiting the current file.
2967 (find-file-noselect todos-current-todos-file 'nowarn)
2969 (set (make-local-variable 'todos-categories) cats)))
2971 (define-derived-mode todos-edit-mode text-mode "Todos-Ed"
2972 "Major mode for editing multiline Todo items.
2974 \\{todos-edit-mode-map}"
2976 (todos-mode-external-set))
2978 (put 'todos-categories-mode 'mode-class 'special)
2980 (define-derived-mode todos-categories-mode special-mode "Todos-Cats"
2981 "Major mode for displaying and editing Todos categories.
2983 \\{todos-categories-mode-map}"
2984 (todos-mode-external-set))
2986 (put 'todos-filtered-items-mode 'mode-class 'special)
2988 (define-derived-mode todos-filtered-items-mode special-mode "Todos-Fltr"
2989 "Mode for displaying and reprioritizing top priority Todos.
2991 \\{todos-filtered-items-mode-map}"
2993 (todos-modes-set-2))
2995 ;; ---------------------------------------------------------------------------
2998 ;; ---------------------------------------------------------------------------
2999 ;;; Entering and Exiting
3002 (defun todos-show (&optional solicit-file)
3003 "Visit a Todos file and display one of its categories.
3004 With non-nil prefix argument SOLICIT-FILE prompt for which todo
3005 file to visit; otherwise visit `todos-default-todos-file'.
3006 Subsequent invocations from outside of Todos mode revisit this
3007 file or, with option `todos-show-current-file' non-nil (the
3008 default), whichever Todos file was last visited.
3010 Calling this command before any Todos file exists prompts for a
3011 file name and an initial category (defaulting to
3012 `todos-initial-file' and `todos-initial-category'), creates both
3013 of these, visits the file and displays the category.
3015 The first invocation of this command on an existing Todos file
3016 interacts with the option `todos-show-first': if `table', show
3017 the table of categories in the file; if `top', show the
3018 corresponding top priorities file, if any; if `first' (the
3019 default value), show the first category in the file. Subsequent
3020 invocations always show the file's current (i.e., last displayed)
3023 In Todos mode just the category's unfinished todo items are shown
3024 by default. The done items are hidden, but typing
3025 `\\[todos-hide-show-done-items]' displays them below the todo
3026 items. With non-nil user option `todos-show-with-done' both todo
3027 and done items are always shown on visiting a category.
3029 Invoking this command in Todos Archive mode visits the
3030 corresponding Todos file, displaying the corresponding category."
3033 (show-first todos-show-first)
3034 (file (cond (solicit-file
3035 (if (funcall todos-files-function)
3036 (todos-read-file-name "Choose a Todos file to visit: "
3038 (error "There are no Todos files")))
3039 ((and (eq major-mode 'todos-archive-mode)
3040 ;; Called noninteractively via todos-quit from
3041 ;; Todos Categories mode to return to archive file.
3042 (called-interactively-p 'any))
3043 (setq cat (todos-current-category))
3044 (concat (file-name-sans-extension todos-current-todos-file)
3047 (or todos-current-todos-file
3048 (and todos-show-current-file
3049 todos-global-current-todos-file)
3050 (todos-absolute-file-name todos-default-todos-file)
3051 (todos-add-file))))))
3052 (unless (member file todos-visited)
3053 ;; Can't setq t-c-t-f here, otherwise wrong file shown when
3054 ;; called again from todos-display-categories.
3055 (let ((todos-current-todos-file file))
3056 (cond ((eq todos-show-first 'table)
3057 (todos-display-categories))
3058 ((eq todos-show-first 'top)
3059 (let* ((shortf (todos-short-file-name file))
3060 (tp-file (todos-absolute-file-name shortf 'top)))
3061 (if (file-exists-p tp-file)
3064 (set-buffer (find-file-noselect tp-file 'nowarn)))
3065 (message "There is no top priorities file for %s" shortf)
3066 (setq todos-show-first 'first)))))))
3067 (when (or (member file todos-visited)
3068 (eq todos-show-first 'first))
3069 (set-window-buffer (selected-window)
3070 (set-buffer (find-file-noselect file 'nowarn)))
3071 ;; If called from archive file, show corresponding
3072 ;; category in Todos file, if it exists.
3073 (when (assoc cat todos-categories)
3074 (setq todos-category-number (todos-category-number cat)))
3075 ;; If this is a new Todos file, add its first category.
3076 (when (zerop (buffer-size))
3077 (setq todos-category-number
3078 (todos-add-category todos-current-todos-file "")))
3079 (save-excursion (todos-category-select)))
3080 (setq todos-show-first show-first)
3081 (add-to-list 'todos-visited file)))
3083 (defun todos-display-categories ()
3084 "Display a table of the current file's categories and item counts.
3086 In the initial display the categories are numbered, indicating
3087 their current order for navigating by \\[todos-forward-category]
3088 and \\[todos-backward-category]. You can persistantly change the
3089 order of the category at point by typing
3090 \\[todos-raise-category-priority] or
3091 \\[todos-lower-category-priority].
3093 The labels above the category names and item counts are buttons,
3094 and clicking these changes the display: sorted by category name
3095 or by the respective item counts (alternately descending or
3096 ascending). In these displays the categories are not numbered
3097 and \\[todos-raise-category-priority] and
3098 \\[todos-lower-category-priority] are
3099 disabled. (Programmatically, the sorting is triggered by passing
3100 a non-nil SORTKEY argument.)
3102 In addition, the lines with the category names and item counts
3103 are buttonized, and pressing one of these button jumps to the
3104 category in Todos mode (or Todos Archive mode, for categories
3105 containing only archived items, provided user option
3106 `todos-skip-archived-categories' is non-nil. These categories
3107 are shown in `todos-archived-only' face."
3109 (todos-display-categories-1)
3111 (todos-update-categories-display sortkey)))
3113 (defun todos-display-categories-alphabetically-or-by-priority ()
3117 (goto-char (point-min))
3119 (if (member 'alpha todos-descending-counts)
3121 (todos-update-categories-display nil)
3122 (setq todos-descending-counts
3123 (delete 'alpha todos-descending-counts)))
3124 (todos-update-categories-display 'alpha))))
3126 (defun todos-display-categories-sorted-by-todo ()
3130 (goto-char (point-min))
3132 (todos-update-categories-display 'todo)))
3134 (defun todos-display-categories-sorted-by-diary ()
3138 (goto-char (point-min))
3140 (todos-update-categories-display 'diary)))
3142 (defun todos-display-categories-sorted-by-done ()
3146 (goto-char (point-min))
3148 (todos-update-categories-display 'done)))
3150 (defun todos-display-categories-sorted-by-archived ()
3154 (goto-char (point-min))
3156 (todos-update-categories-display 'archived)))
3158 (defun todos-show-archive (&optional ask)
3159 "Visit the archive of the current Todos category, if it exists.
3160 If the category has no archived items, prompt to visit the
3161 archive anyway. If there is no archive for this file or with
3162 non-nil argument ASK, prompt to visit another archive.
3164 The buffer showing the archive is in Todos Archive mode. The
3165 first visit in a session displays the first category in the
3166 archive, subsequent visits return to the last category
3169 (let* ((cat (todos-current-category))
3170 (count (todos-get-count 'archived cat))
3171 (archive (concat (file-name-sans-extension todos-current-todos-file)
3174 (setq place (cond (ask 'other-archive)
3175 ((file-exists-p archive) 'this-archive)
3176 (t (when (y-or-n-p (concat "This file has no archive; "
3177 "visit another archive? "))
3179 (when (eq place 'other-archive)
3180 (setq archive (todos-read-file-name "Choose a Todos archive: " t t)))
3181 (when (and (eq place 'this-archive) (zerop count))
3182 (setq place (when (y-or-n-p
3183 (concat "This category has no archived items;"
3184 " visit archive anyway? "))
3187 (set-window-buffer (selected-window)
3188 (set-buffer (find-file-noselect archive)))
3189 (if (member place '(other-archive other-cat))
3190 (setq todos-category-number 1)
3191 (todos-category-number cat))
3192 (todos-category-select))))
3194 (defun todos-choose-archive ()
3195 "Choose an archive and visit it."
3197 (todos-show-archive t))
3199 (defun todos-save ()
3200 "Save the current Todos file."
3202 (cond ((eq major-mode 'todos-filtered-items-mode)
3203 (todos-check-top-priorities)
3204 (todos-save-top-priorities-buffer))
3208 (defun todos-quit ()
3209 "Exit the current Todos-related buffer.
3210 Depending on the specific mode, this either kills the buffer or
3211 buries it and restores state as needed."
3213 (cond ((eq major-mode 'todos-categories-mode)
3214 ;; Postpone killing buffer till after calling todos-show, to
3215 ;; prevent killing todos-mode buffer.
3216 (let ((buf (current-buffer)))
3217 (setq todos-descending-counts nil)
3218 ;; Ensure todos-show calls todos-display-categories only on
3219 ;; first invocation per file.
3220 (when (eq todos-show-first 'table)
3221 (add-to-list 'todos-visited todos-current-todos-file))
3224 ((eq major-mode 'todos-filtered-items-mode)
3227 ((member major-mode (list 'todos-mode 'todos-archive-mode))
3228 ;; Have to write previously nonexistant archives to file, and might
3229 ;; as well save Todos file also.
3233 (defun todos-print (&optional to-file)
3234 "Produce a printable version of the current Todos buffer.
3235 This converts overlays and soft line wrapping and, depending on
3236 the value of `todos-print-function', includes faces. With
3237 non-nil argument TO-FILE write the printable version to a file;
3238 otherwise, send it to the default printer."
3240 (let ((buf todos-print-buffer)
3242 ((eq major-mode 'todos-mode)
3243 (concat "Todos File: "
3244 (todos-short-file-name todos-current-todos-file)
3245 "\nCategory: " (todos-current-category)))
3246 ((eq major-mode 'todos-filtered-items-mode)
3247 "Todos Top Priorities")))
3248 (prefix (propertize (concat todos-prefix " ")
3249 'face 'todos-prefix-string))
3251 (fill-prefix (make-string todos-indent-to-here 32))
3252 (content (buffer-string))
3254 (with-current-buffer (get-buffer-create buf)
3256 (goto-char (point-min))
3259 (end (save-excursion (todos-item-end))))
3260 (when todos-number-priorities
3262 (setq prefix (propertize (concat (number-to-string num) " ")
3263 'face 'todos-prefix-string)))
3265 (fill-region beg end))
3266 ;; Calling todos-forward-item infloops at todos-item-start due to
3267 ;; non-overlay prefix, so search for item start instead.
3268 (if (re-search-forward todos-item-start nil t)
3270 (goto-char (point-max))))
3271 (if (re-search-backward (concat "^" (regexp-quote todos-category-done))
3273 (replace-match todos-done-separator))
3274 (goto-char (point-min))
3278 (let ((file (read-file-name "Print to file: ")))
3279 (funcall todos-print-function file))
3280 (funcall todos-print-function)))
3283 (defun todos-print-to-file ()
3284 "Save printable version of this Todos buffer to a file."
3288 (defun todos-convert-legacy-files ()
3289 "Convert legacy Todo files to the current Todos format.
3290 The files `todo-file-do' and `todo-file-done' are converted and
3291 saved (the latter as a Todos Archive file) with a new name in
3292 `todos-files-directory'. See also the documentation string of
3293 `todos-todo-mode-date-time-regexp' for further details."
3295 (if (fboundp 'todo-mode)
3296 (require 'todo-mode)
3297 (error "Void function `todo-mode'"))
3298 ;; Convert `todo-file-do'.
3299 (if (file-exists-p todo-file-do)
3300 (let ((default "todo-do-conv")
3303 (insert-file-contents todo-file-do)
3304 (let ((end (search-forward ")" (line-end-position) t))
3305 (beg (search-backward "(" (line-beginning-position) t)))
3306 (setq todo-categories
3307 (read (buffer-substring-no-properties beg end))))
3309 (delete-region (line-beginning-position) (1+ (line-end-position)))
3312 ((looking-at (regexp-quote (concat todo-prefix todo-category-beg)))
3313 (replace-match todos-category-beg))
3314 ((looking-at (regexp-quote todo-category-end))
3316 ((looking-at (regexp-quote (concat todo-prefix " "
3317 todo-category-sep)))
3318 (replace-match todos-category-done))
3319 ((looking-at (concat (regexp-quote todo-prefix) " "
3320 todos-todo-mode-date-time-regexp " "
3321 (regexp-quote todo-initials) ":"))
3322 (todos-convert-legacy-date-time)))
3324 (setq file (concat todos-files-directory
3326 (format "Save file as (default \"%s\"): " default)
3329 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3331 (insert-file-contents file)
3332 (let ((todos-categories (todos-make-categories-list t)))
3333 (todos-update-categories-sexp))
3334 (write-region (point-min) (point-max) file nil 'nomessage))
3335 ;; Convert `todo-file-done'.
3336 (when (file-exists-p todo-file-done)
3338 (insert-file-contents todo-file-done)
3339 (let ((beg (make-marker))
3341 cat cats comment item)
3343 (when (looking-at todos-todo-mode-date-time-regexp)
3344 (set-marker beg (point))
3345 (todos-convert-legacy-date-time)
3346 (set-marker end (point))
3348 (insert "[" todos-done-string)
3352 (when (looking-at todos-todo-mode-date-time-regexp)
3353 (todos-convert-legacy-date-time))
3354 (when (looking-at (concat " " (regexp-quote todo-initials) ":"))
3355 (replace-match "")))
3356 (if (re-search-forward
3357 (concat "^" todos-todo-mode-date-time-regexp) nil t)
3358 (goto-char (match-beginning 0))
3359 (goto-char (point-max)))
3361 (when (looking-back "\\[\\([^][]+\\)\\]")
3362 (setq cat (match-string 1))
3363 (goto-char (match-beginning 0))
3365 ;; If the item ends with a non-comment parenthesis not
3366 ;; followed by a period, we lose (but we inherit that problem
3367 ;; from todo-mode.el).
3368 (when (looking-back "(\\(.*\\)) ")
3369 (setq comment (match-string 1))
3371 (insert "[" todos-comment-string ": " comment "]"))
3372 (set-marker end (point))
3373 (if (member cat cats)
3374 ;; If item is already in its category, leave it there.
3375 (unless (save-excursion
3377 (concat "^" (regexp-quote todos-category-beg)
3379 (string= (match-string 1) cat))
3380 ;; Else move it to its category.
3381 (setq item (buffer-substring-no-properties beg end))
3382 (delete-region beg (1+ end))
3383 (set-marker beg (point))
3385 (concat "^" (regexp-quote (concat todos-category-beg cat))
3389 (if (re-search-forward
3390 (concat "^" (regexp-quote todos-category-beg)
3392 (progn (goto-char (match-beginning 0))
3395 (goto-char (point-max)))
3400 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3402 (set-marker beg nil)
3403 (set-marker end nil))
3404 (setq file (concat (file-name-sans-extension file) ".toda"))
3405 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3407 (insert-file-contents file)
3408 (let ((todos-categories (todos-make-categories-list t)))
3409 (todos-update-categories-sexp))
3410 (write-region (point-min) (point-max) file nil 'nomessage)
3411 (setq archive-sexp (read (buffer-substring-no-properties
3412 (line-beginning-position)
3413 (line-end-position)))))
3414 (setq file (concat (file-name-sans-extension file) ".todo"))
3415 ;; Update categories sexp of converted Todos file again, adding
3416 ;; counts of archived items.
3418 (insert-file-contents file)
3419 (let ((sexp (read (buffer-substring-no-properties
3420 (line-beginning-position)
3421 (line-end-position)))))
3423 (let ((archive-cat (assoc (car cat) archive-sexp)))
3425 (aset (cdr cat) 3 (aref (cdr archive-cat) 2)))))
3426 (delete-region (line-beginning-position) (line-end-position))
3427 (prin1 sexp (current-buffer)))
3428 (write-region (point-min) (point-max) file nil 'nomessage)))
3429 (todos-reevaluate-filelist-defcustoms)
3430 (message "Format conversion done."))
3431 (error "No legacy Todo file exists")))
3433 ;; ---------------------------------------------------------------------------
3434 ;;; Navigation Commands
3436 (defun todos-forward-category (&optional back)
3437 "Visit the numerically next category in this Todos file.
3438 If the current category is the highest numbered, visit the first
3439 category. With non-nil argument BACK, visit the numerically
3440 previous category (the highest numbered one, if the current
3441 category is the first)."
3443 (setq todos-category-number
3444 (1+ (mod (- todos-category-number (if back 2 0))
3445 (length todos-categories))))
3446 (when todos-skip-archived-categories
3447 (while (and (zerop (todos-get-count 'todo))
3448 (zerop (todos-get-count 'done))
3449 (not (zerop (todos-get-count 'archived))))
3450 (setq todos-category-number
3451 (apply (if back '1- '1+) (list todos-category-number)))))
3452 (todos-category-select)
3453 (goto-char (point-min)))
3455 (defun todos-backward-category ()
3456 "Visit the numerically previous category in this Todos file.
3457 If the current category is the highest numbered, visit the first
3460 (todos-forward-category t))
3463 (defun todos-jump-to-category (&optional file cat)
3464 "Prompt for a category in a Todos file and jump to it.
3466 With prefix argument FILE, prompt for a specific Todos file and
3467 choose (with TAB completion) a category in it to jump to;
3468 otherwise, choose and jump to any category in either the current
3469 Todos file or a file in `todos-category-completions-files'.
3471 You can also enter a non-existing category name, triggering a
3472 prompt whether to add a new category by that name; on
3473 confirmation it is added and jumped to.
3475 Noninteractively, jump directly to the category named by argument
3476 CAT; this is used in Todos Categories mode."
3478 ;; If invoked outside of Todos mode and there is not yet any Todos
3479 ;; file, initialize one.
3480 (if (null todos-files)
3482 (let ((file0 (when cat ; We're in Todos Categories mode.
3483 ;; With non-nil `todos-skip-archived-categories'
3484 ;; jump to archive file of a category with only
3486 (if (and todos-skip-archived-categories
3487 (zerop (todos-get-count 'todo cat))
3488 (zerop (todos-get-count 'done cat))
3489 (not (zerop (todos-get-count 'archived cat))))
3490 (concat (file-name-sans-extension
3491 todos-current-todos-file) ".toda")
3492 ;; Otherwise, jump to current todos file.
3493 todos-current-todos-file)))
3494 (cat+file (unless cat
3495 (todos-read-category "Jump to category: " nil file))))
3496 (setq category (or cat (car cat+file)))
3497 (unless cat (setq file0 (cdr cat+file)))
3498 (with-current-buffer (find-file-noselect file0 'nowarn)
3499 (setq todos-current-todos-file file0)
3500 ;; If called from Todos Categories mode, clean up before jumping.
3501 (if (string= (buffer-name) todos-categories-buffer)
3503 (set-window-buffer (selected-window)
3504 (set-buffer (find-buffer-visiting file0)))
3505 (unless todos-global-current-todos-file
3506 (setq todos-global-current-todos-file todos-current-todos-file))
3507 (todos-category-number category)
3508 (todos-category-select)
3509 (goto-char (point-min))))))
3511 (defun todos-jump-to-item ()
3512 "Jump to the file and category of the filtered item at point."
3514 (let* ((str (todos-item-string))
3515 (buf (current-buffer))
3516 (res (todos-find-item str))
3521 (message "Category %s does not contain this item." cat)
3523 (set-window-buffer (selected-window)
3524 (set-buffer (find-buffer-visiting file)))
3525 (setq todos-current-todos-file file)
3526 (setq todos-category-number (todos-category-number cat))
3527 (let ((todos-show-with-done (if (or todos-filter-done-items
3528 (eq (cdr found) 'done))
3530 todos-show-with-done)))
3531 (todos-category-select))
3532 (goto-char (car found)))))
3534 (defun todos-forward-item (&optional count)
3535 "Move point down to start of item with next lower priority.
3536 With positive numerical prefix COUNT, move point COUNT items
3539 If the category's done items are hidden, this command also moves
3540 point to the empty line below the last todo item from any higher
3541 item in the category, i.e., when invoked with or without a prefix
3542 argument. If the category's done items are visible, this command
3543 called with a prefix argument only moves point to a lower item,
3544 e.g., with point on the last todo item and called with prefix 1,
3545 it moves point to the first done item; but if called with point
3546 on the last todo item without a prefix argument, it moves point
3547 the the empty line above the done items separator."
3549 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3550 ;; the corresponding command.
3551 (if (and count (> 1 count))
3552 (error "This command only accepts a positive numerical prefix argument")
3553 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
3554 (start (line-end-position)))
3556 (if (re-search-forward todos-item-start nil t (or count 1))
3557 (goto-char (match-beginning 0))
3558 (goto-char (point-max)))
3559 ;; If points advances by one from a todo to a done item, go back to the
3560 ;; space above todos-done-separator, since that is a legitimate place to
3561 ;; insert an item. But skip this space if count > 1, since that should
3562 ;; only stop on an item.
3563 (when (and not-done (todos-done-item-p) (not count))
3564 ;; (if (or (not count) (= count 1))
3565 (re-search-backward "^$" start t)))));)
3566 ;; FIXME: The preceding sexp is insufficient when buffer is not narrowed,
3567 ;; since there could be no done items in this category, so the search puts
3568 ;; us on first todo item of next category. Does this ever happen? If so:
3569 ;; (let ((opoint) (point))
3570 ;; (forward-line -1)
3571 ;; (when (or (not count) (= count 1))
3572 ;; (cond ((looking-at (concat "^" (regexp-quote todos-category-beg)))
3573 ;; (forward-line -2))
3574 ;; ((looking-at (concat "^" (regexp-quote todos-category-done)))
3575 ;; (forward-line -1))
3577 ;; (goto-char opoint)))))))
3579 (defun todos-backward-item (&optional count)
3580 "Move point up to start of item with next higher priority.
3581 With positive numerical prefix COUNT, move point COUNT items
3584 If the category's done items are visible, this command called
3585 with a prefix argument only moves point to a higher item, e.g.,
3586 with point on the first done item and called with prefix 1, it
3587 moves to the last todo item; but if called with point on the
3588 first done item without a prefix argument, it moves point the the
3589 empty line above the done items separator."
3591 ;; Avoid moving to bob if on the first item but not at bob.
3592 (when (> (line-number-at-pos) 1)
3593 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3594 ;; the corresponding command.
3595 (if (and count (> 1 count))
3596 (error "This command only accepts a positive numerical prefix argument")
3597 (let* ((done (todos-done-item-p)))
3600 (re-search-backward todos-item-start nil t (or count 1)))
3601 ;; Unless this is a regexp filtered items buffer (which can contain
3602 ;; intermixed todo and done items), if points advances by one from a
3603 ;; done to a todo item, go back to the space above
3604 ;; todos-done-separator, since that is a legitimate place to insert an
3605 ;; item. But skip this space if count > 1, since that should only
3607 (when (and done (not (todos-done-item-p)) (not count)
3608 ;(or (not count) (= count 1))
3609 (not (equal (buffer-name) todos-regexp-items-buffer)))
3610 (re-search-forward (concat "^" (regexp-quote todos-category-done))
3612 (forward-line -1))))))
3614 (defun todos-forward-button (n &optional wrap display-message)
3616 (interactive "p\nd\nd")
3617 (forward-button n wrap display-message)
3618 (and (bolp) (button-at (point))
3619 ;; Align with beginning of category label.
3620 (forward-char (+ 4 (length todos-categories-number-separator)))))
3622 (defun todos-backward-button (n &optional wrap display-message)
3624 (interactive "p\nd\nd")
3625 (backward-button n wrap display-message)
3626 (and (bolp) (button-at (point))
3627 ;; Align with beginning of category label.
3628 (forward-char (+ 4 (length todos-categories-number-separator)))))
3630 (defun todos-search ()
3631 "Search for a regular expression in this Todos file.
3632 The search runs through the whole file and encompasses all and
3633 only todo and done items; it excludes category names. Multiple
3634 matches are shown sequentially, highlighted in `todos-search'
3637 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3639 matches match cat in-done ov mlen msg)
3641 (goto-char (point-min))
3643 (setq match (re-search-forward regex nil t))
3644 (goto-char (line-beginning-position))
3645 (unless (or (equal (point) 1)
3646 (looking-at (concat "^" (regexp-quote todos-category-beg))))
3647 (if match (push match matches)))
3649 (setq matches (reverse matches))
3653 (setq match (pop matches))
3656 (when (looking-at todos-done-string-start)
3658 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
3659 "\\(.*\\)\n") nil t)
3660 (setq cat (match-string-no-properties 1))
3661 (todos-category-number cat)
3662 (todos-category-select)
3664 (unless todos-show-with-done (todos-hide-show-done-items)))
3666 (setq ov (make-overlay (- (point) (length regex)) (point)))
3667 (overlay-put ov 'face 'todos-search)
3669 (setq mlen (length matches))
3672 (format "There are %d more matches; go to next match? "
3674 "There is one more match; go to it? "))
3676 (throw 'stop (setq msg (if (> mlen 1)
3677 (format "There are %d more matches."
3679 "There is one more match."))))))
3680 (setq msg "There are no more matches."))
3681 (todos-category-select)
3683 (message "No match for \"%s\"" regex))
3685 (if (y-or-n-p (concat msg "\nUnhighlight matches? "))
3686 (todos-clear-matches)
3687 (message "You can unhighlight the matches later by typing %s"
3688 (key-description (car (where-is-internal
3689 'todos-clear-matches))))))))
3691 (defun todos-clear-matches ()
3692 "Remove highlighting on matches found by todos-search."
3694 (remove-overlays 1 (1+ (buffer-size)) 'face 'todos-search))
3696 ;; ---------------------------------------------------------------------------
3697 ;;; Display Commands
3699 (defun todos-hide-show-item-numbering ()
3702 (todos-reset-prefix 'todos-number-priorities (not todos-number-priorities)))
3704 (defun todos-hide-show-done-items ()
3705 "Show hidden or hide visible done items in current category."
3707 (if (zerop (todos-get-count 'done (todos-current-category)))
3708 (message "There are no done items in this category.")
3710 (goto-char (point-min))
3711 (let ((todos-show-with-done (not (re-search-forward
3712 todos-done-string-start nil t))))
3713 (todos-category-select)))))
3715 (defun todos-show-done-only ()
3716 "Switch between displaying only done or only todo items."
3718 (setq todos-show-done-only (not todos-show-done-only))
3719 (todos-category-select))
3721 (defun todos-highlight-item ()
3722 "Highlight or unhighlight the todo item the cursor is on."
3729 (defun todos-hide-show-date-time ()
3730 "Hide or show date-time header of todo items in the current file."
3734 (goto-char (point-min))
3735 (let ((ovs (overlays-in (point) (1+ (point))))
3739 (if (equal (overlay-get ov 'display) "")
3740 (setq ovs nil hidden t)))
3742 (goto-char (point-min))
3744 (remove-overlays (point-min) (point-max) 'display "")
3746 (when (re-search-forward
3747 (concat todos-date-string-start todos-date-pattern
3748 "\\( " diary-time-regexp "\\)?"
3749 (regexp-quote todos-nondiary-end) "? ")
3751 (unless (save-match-data (todos-done-item-p))
3752 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
3753 (overlay-put ov 'display "")))
3754 (todos-forward-item)))))))
3756 (defun todos-mark-unmark-item (&optional n)
3757 "Mark item with `todos-item-mark' if unmarked, otherwise unmark it.
3758 With a positive numerical prefix argument N, change the
3759 marking of the next N items."
3761 (unless (> n 1) (setq n 1))
3763 (let* ((cat (todos-current-category))
3764 (marks (assoc cat todos-categories-with-marks))
3765 (ov (todos-prefix-overlay))
3766 (pref (overlay-get ov 'before-string)))
3767 (if (todos-marked-item-p)
3769 (overlay-put ov 'before-string (substring pref 1))
3770 (if (= (cdr marks) 1) ; Deleted last mark in this category.
3771 (setq todos-categories-with-marks
3772 (assq-delete-all cat todos-categories-with-marks))
3773 (setcdr marks (1- (cdr marks)))))
3774 (overlay-put ov 'before-string (concat todos-item-mark pref))
3776 (setcdr marks (1+ (cdr marks)))
3777 (push (cons cat 1) todos-categories-with-marks))))
3778 (todos-forward-item)))
3780 (defun todos-mark-category ()
3781 "Mark all visiblw items in this category with `todos-item-mark'."
3784 (goto-char (point-min))
3786 (let* ((cat (todos-current-category))
3787 (marks (assoc cat todos-categories-with-marks))
3788 (ov (todos-prefix-overlay))
3789 (pref (overlay-get ov 'before-string)))
3790 (unless (todos-marked-item-p)
3791 (overlay-put ov 'before-string (concat todos-item-mark pref))
3793 (setcdr marks (1+ (cdr marks)))
3794 (push (cons cat 1) todos-categories-with-marks))))
3795 (todos-forward-item))))
3797 (defun todos-unmark-category ()
3798 "Remove `todos-item-mark' from all visible items in this category."
3801 (goto-char (point-min))
3803 (let* ((cat (todos-current-category))
3804 (marks (assoc cat todos-categories-with-marks))
3805 (ov (todos-prefix-overlay))
3806 (pref (overlay-get ov 'before-string)))
3807 (when (todos-marked-item-p)
3808 (overlay-put ov 'before-string (substring pref 1))
3809 (setq todos-categories-with-marks
3810 (delq (assoc (todos-current-category)
3811 todos-categories-with-marks)
3812 todos-categories-with-marks))))
3813 (todos-forward-item))))
3815 ;; ---------------------------------------------------------------------------
3816 ;;; Item filtering commands
3818 (defun todos-set-top-priorities-in-file ()
3819 "Set number of top priorities for this file.
3820 See `todos-set-top-priorities' for more details."
3822 (todos-set-top-priorities))
3824 (defun todos-set-top-priorities-in-category ()
3825 "Set number of top priorities for this category.
3826 See `todos-set-top-priorities' for more details."
3828 (todos-set-top-priorities t))
3830 (defun todos-top-priorities (&optional arg multifile)
3831 "Display a list of top priority items from different categories.
3832 The categories are either a subset of those in the current Todos
3833 file, or else, with non-nil argument MULTIFILE, a subset of the
3834 categories in the files listed in `todos-filter-files', or if
3835 this nil, in the files chosen from a file selection dialog that
3836 pops up in this case.
3838 With numerical prefix ARG show at most ARG top priority items
3839 from each category. With `C-u' as prefix argument show the
3840 numbers of top priority items specified by category in
3841 `todos-priorities-rules', if this has an entry for the file(s);
3842 otherwise show `todos-show-priorities' items per category in the
3843 file(s). With no prefix argument, if a top priorities file for
3844 the current Todos file has previously been saved (see
3845 `todos-save-top-priorities-buffer'), visit this file; if there is
3846 no such file, build the list as with prefix argument `C-u'.
3848 The prefix ARG regulates how many top priorities from
3849 each category to show, as described above."
3851 (let* ((flist (if multifile
3852 (or todos-filter-files
3853 (progn (todos-multiple-filter-files)
3854 todos-multiple-filter-files))
3855 (list todos-current-todos-file)))
3856 (tp-file (if (equal flist 'quit)
3857 ;; Pressed `cancel' in file selection dialog.
3859 (concat todos-files-directory
3860 (mapconcat 'identity
3861 (mapcar 'todos-short-file-name flist)
3864 (tp-file-exists (file-exists-p tp-file))
3865 (buf todos-top-priorities-buffer))
3866 (cond ((and arg (natnump arg))
3867 (todos-filter-items (cons 'top arg) flist))
3868 ((and (not arg) tp-file-exists)
3870 (todos-prefix-overlays)
3871 (todos-check-top-priorities))
3873 (todos-filter-items 'top flist)))
3874 (unless tp-file-exists
3875 (todos-filtered-buffer-name buf flist))))
3877 (defun todos-top-priorities-multifile (&optional arg)
3878 "Display a list of top priority items from different categories.
3879 The categories are a subset of the categories in the files listed
3880 in `todos-filter-files', or if this nil, in the files chosen from
3881 a file selection dialog that pops up in this case.
3883 With numerical prefix ARG show at most ARG top priority items
3884 from each category in each file. With `C-u' as prefix argument
3885 show the numbers of top priority items specified in
3886 `todos-priorities-rules', if this is non-nil; otherwise show
3887 `todos-show-priorities' items per category. With no prefix
3888 argument, if a top priorities file for the chosen Todos files
3889 exists (see `todos-save-top-priorities-buffer'), visit this file;
3890 if there is no such file, do the same as with prefix argument
3893 (todos-top-priorities arg t))
3895 (defun todos-diary-items (&optional multifile)
3896 "Display a list of todo diary items from different categories.
3897 The categories are either a subset of those in the current Todos
3898 file, or else, with non-nil argument MULTIFILE, a subset of the
3899 categories in the files listed in `todos-filter-files', or if
3900 this nil, in the files chosen from a file selection dialog that
3901 pops up in this case."
3903 (let ((flist (if multifile
3904 (or todos-filter-files
3905 (progn (todos-multiple-filter-files)
3906 todos-multiple-filter-files))
3907 (list todos-current-todos-file)))
3908 (buf todos-diary-items-buffer))
3909 (if (equal flist 'quit)
3910 ;; Pressed `cancel' in file selection dialog.
3912 (todos-filter-items 'diary flist)
3913 (todos-filtered-buffer-name buf flist))))
3915 (defun todos-diary-items-multifile ()
3916 "Display a list of todo diary items from one or more Todos files.
3917 The categories are a subset of the categories in the files listed
3918 in `todos-filter-files', or if this nil, in the files chosen from
3919 a file selection dialog that pops up in this case."
3921 (todos-diary-items t))
3923 (defun todos-regexp-items (&optional multifile)
3924 "Prompt for a regular expression and display items that match it.
3925 The matches may be from different categories and with non-nil
3926 option `todos-filter-done-items', can include not only todo items
3927 but also done items, including those in Archive files.
3929 The categories are either a subset of those in the current Todos
3930 file (and possibly in the corresponding Archive file), or else,
3931 with non-nil argument MULTIFILE, a subset of the categories in
3932 the files listed in `todos-filter-files', or if this nil, in the
3933 files chosen from a file selection dialog that pops up in this
3934 case (and possibly in the corresponding Archive files)."
3936 (let ((flist (if multifile
3937 (or todos-filter-files
3938 (progn (todos-multiple-filter-files)
3939 todos-multiple-filter-files))
3940 (list todos-current-todos-file)))
3941 (buf todos-regexp-items-buffer))
3942 (if (equal flist 'quit)
3943 ;; Pressed `cancel' in file selection dialog.
3945 (todos-filter-items 'regexp flist)
3946 (todos-filtered-buffer-name buf flist))))
3948 (defun todos-regexp-items-multifile ()
3949 "Prompt for a regular expression and display items that match it.
3950 The matches may be from different categories and with non-nil
3951 option `todos-filter-done-items', can include not only todo items
3952 but also done items, including those in Archive files.
3954 The categories are a subset of the categories in the files listed
3955 in `todos-filter-files', or if this nil, in the files chosen from
3956 a file selection dialog that pops up in this case (and possibly
3957 in the corresponding Archive files)."
3959 (todos-regexp-items t))
3961 ;; ---------------------------------------------------------------------------
3962 ;;; Editing Commands
3964 (defun todos-add-file ()
3965 "Name and add a new Todos file.
3966 Interactively, prompt for a category and display it.
3967 Noninteractively, return the name of the new file."
3969 (let ((prompt (concat "Enter name of new Todos file "
3970 "(TAB or SPC to see current names): "))
3972 (setq file (todos-read-file-name prompt))
3973 (with-current-buffer (get-buffer-create file)
3975 (write-region (point-min) (point-max) file nil 'nomessage nil t)
3977 (todos-reevaluate-filelist-defcustoms)
3978 (if (called-interactively-p)
3980 (set-window-buffer (selected-window)
3981 (set-buffer (find-file-noselect file)))
3982 (setq todos-current-todos-file file)
3986 ;;; Category editing commands
3988 (defun todos-add-category (&optional file cat)
3989 "Add a new category to a Todos file.
3991 Called interactively with prefix argument FILE, prompt for a file
3992 and then for a new category to add to that file, otherwise prompt
3993 just for a category to add to the current Todos file. After adding
3994 the category, visit it in Todos mode.
3996 Non-interactively, add category CAT to file FILE; if FILE is nil,
3997 add CAT to the current Todos file. After adding the category,
3998 return the new category number."
4001 ;; If cat is passed from caller, don't prompt, unless it is "",
4002 ;; which means the file was just added and has no category yet.
4003 (if (and cat (> (length cat) 0))
4004 (setq file0 (or (and (stringp file) file)
4005 todos-current-todos-file))
4006 (setq catfil (todos-read-category "Enter a new category name: "
4007 'add (when (called-interactively-p 'any)
4010 file0 (if (called-interactively-p 'any)
4014 (let ((counts (make-vector 4 0)) ; [todo diary done archived]
4015 (num (1+ (length todos-categories)))
4016 (buffer-read-only nil))
4017 (setq todos-current-todos-file file0)
4018 (setq todos-categories (append todos-categories
4019 (list (cons cat counts))))
4021 (goto-char (point-max))
4022 (save-excursion ; Save point for todos-category-select.
4023 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
4024 (todos-update-categories-sexp)
4025 ;; If invoked by user, display the newly added category, if
4026 ;; called programmatically return the category number to the
4028 (if (called-interactively-p 'any)
4030 (setq todos-category-number num)
4031 (todos-category-select))
4034 (defun todos-rename-category ()
4035 "Rename current Todos category.
4036 If this file has an archive containing this category, rename the
4037 category there as well."
4039 (let* ((cat (todos-current-category))
4040 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat))))
4041 (setq new (todos-validate-name new 'category))
4042 (let* ((ofile todos-current-todos-file)
4043 (archive (concat (file-name-sans-extension ofile) ".toda"))
4044 (buffers (append (list ofile)
4045 (unless (zerop (todos-get-count 'archived cat))
4047 (dolist (buf buffers)
4048 (with-current-buffer (find-file-noselect buf)
4049 (let (buffer-read-only)
4050 (setq todos-categories (todos-set-categories))
4053 (setcar (assoc cat todos-categories) new)
4055 (goto-char (point-min))
4056 (todos-update-categories-sexp)
4057 (re-search-forward (concat (regexp-quote todos-category-beg)
4058 "\\(" (regexp-quote cat) "\\)\n")
4060 (replace-match new t t nil 1)))))))
4061 (force-mode-line-update))
4062 (save-excursion (todos-category-select)))
4064 (defun todos-delete-category (&optional arg)
4065 "Delete current Todos category provided it is empty.
4066 With ARG non-nil delete the category unconditionally,
4067 i.e. including all existing todo and done items."
4069 (let* ((file todos-current-todos-file)
4070 (cat (todos-current-category))
4071 (todo (todos-get-count 'todo cat))
4072 (done (todos-get-count 'done cat))
4073 (archived (todos-get-count 'archived cat)))
4075 (or (> todo 0) (> done 0)))
4076 (message "%s" (substitute-command-keys
4077 (concat "To delete a non-empty category, "
4078 "type C-u \\[todos-delete-category].")))
4079 (when (cond ((= (length todos-categories) 1)
4080 (y-or-n-p (concat "This is the only category in this file; "
4081 "deleting it will also delete the file.\n"
4082 "Do you want to proceed? ")))
4084 (y-or-n-p (concat "This category has archived items; "
4085 "the archived category will remain\n"
4086 "after deleting the todo category. "
4087 "Do you still want to delete it\n"
4088 "(see 'todos-skip-archived-categories' "
4089 "for another option)? ")))
4091 (y-or-n-p (concat "Permanently remove category \"" cat
4092 "\"" (and arg " and all its entries")
4095 (let ((buffer-read-only)
4096 (beg (re-search-backward
4097 (concat "^" (regexp-quote (concat todos-category-beg cat))
4099 (end (if (re-search-forward
4100 (concat "\n\\(" (regexp-quote todos-category-beg)
4104 (remove-overlays beg end)
4105 (delete-region beg end)
4106 (if (= (length todos-categories) 1)
4107 ;; If deleted category was the only one, delete the file.
4109 (todos-reevaluate-filelist-defcustoms)
4110 ;; Skip confirming killing the archive buffer if it has been
4111 ;; modified and not saved.
4112 (set-buffer-modified-p nil)
4115 (message "Deleted Todos file %s." file))
4116 (setq todos-categories (delete (assoc cat todos-categories)
4118 (todos-update-categories-sexp)
4119 (setq todos-category-number
4120 (1+ (mod todos-category-number (length todos-categories))))
4121 (todos-category-select)
4122 (goto-char (point-min))
4123 (message "Deleted category %s." cat)))))))
4125 (defun todos-move-category ()
4126 "Move current category to a different Todos file.
4127 If current category has archived items, also move those to the
4128 archive of the file moved to, creating it if it does not exist."
4130 (when (or (> (length todos-categories) 1)
4131 (y-or-n-p (concat "This is the only category in this file; "
4132 "moving it will also delete the file.\n"
4133 "Do you want to proceed? ")))
4134 (let* ((ofile todos-current-todos-file)
4135 (cat (todos-current-category))
4136 (nfile (todos-read-file-name
4137 "Choose a Todos file to move this category to: " nil t))
4138 (archive (concat (file-name-sans-extension ofile) ".toda"))
4139 (buffers (append (list ofile)
4140 (unless (zerop (todos-get-count 'archived cat))
4143 (while (equal (file-truename nfile) (file-truename ofile))
4144 (setq nfile (todos-read-file-name
4145 "Choose a file distinct from this file: " nil t)))
4146 (dolist (buf buffers)
4147 (with-current-buffer (find-file-noselect buf)
4149 (goto-char (point-max))
4150 (let* ((beg (re-search-backward
4151 (concat "^" (regexp-quote (concat todos-category-beg cat))
4154 (end (if (re-search-forward
4155 (concat "^" (regexp-quote todos-category-beg))
4159 (content (buffer-substring-no-properties beg end))
4160 (counts (cdr (assoc cat todos-categories)))
4162 ;; Move the category to the new file. Also update or create
4163 ;; archive file if necessary.
4164 (with-current-buffer
4166 ;; Regenerate todos-archives in case there
4167 ;; is a newly created archive.
4168 (if (member buf (funcall todos-files-function t))
4169 (concat (file-name-sans-extension nfile) ".toda")
4171 (let* ((nfile-short (todos-short-file-name nfile))
4173 (format "Todos file \"%s\" already has "
4175 (format "the category \"%s\";\n" cat)
4176 "enter a new category name: "))
4179 (goto-char (point-max))
4181 ;; If the file moved to has a category with the same
4182 ;; name, rename the moved category.
4183 (when (assoc cat todos-categories)
4184 (unless (member (file-truename (buffer-file-name))
4185 (funcall todos-files-function t))
4186 (setq new (read-from-minibuffer prompt))
4187 (setq new (todos-validate-name new 'category))))
4188 ;; Replace old with new name in Todos and archive files.
4190 (goto-char (point-max))
4192 (concat "^" (regexp-quote todos-category-beg)
4193 "\\(" (regexp-quote cat) "\\)$") nil t)
4194 (replace-match new nil nil nil 1)))
4195 (setq todos-categories
4196 (append todos-categories (list (cons new counts))))
4197 (todos-update-categories-sexp)
4198 ;; If archive was just created, save it to avoid "File
4199 ;; <xyz> no longer exists!" message on invoking
4200 ;; `todos-view-archived-items'.
4201 (unless (file-exists-p (buffer-file-name))
4203 (todos-category-number (or new cat))
4204 (todos-category-select))
4205 ;; Delete the category from the old file, and if that was the
4206 ;; last category, delete the file. Also handle archive file
4208 (remove-overlays beg end)
4209 (delete-region beg end)
4210 (goto-char (point-min))
4211 ;; Put point after todos-categories sexp.
4213 (if (eobp) ; Aside from sexp, file is empty.
4215 ;; Skip confirming killing the archive buffer.
4216 (set-buffer-modified-p nil)
4217 (delete-file todos-current-todos-file)
4219 (when (member todos-current-todos-file todos-files)
4220 (todos-reevaluate-filelist-defcustoms)))
4221 (setq todos-categories (delete (assoc cat todos-categories)
4223 (todos-update-categories-sexp)
4224 (todos-category-select)))))
4225 (set-window-buffer (selected-window)
4226 (set-buffer (find-file-noselect nfile)))
4227 (todos-category-number (or new cat))
4228 (todos-category-select))))
4230 (defun todos-merge-category (&optional file)
4231 "Merge current category into another existing category.
4233 With prefix argument FILE, prompt for a specific Todos file and
4234 choose (with TAB completion) a category in it to merge into;
4235 otherwise, choose and merge into a category in either the
4236 current Todos file or a file in `todos-category-completions-files'.
4238 After merging, the current category's todo and done items are
4239 appended to the chosen goal category's todo and done items,
4240 respectively. The goal category becomes the current category,
4241 and the previous current category is deleted.
4243 If both the first and goal categories also have archived items,
4244 the former are merged to the latter. If only the first category
4245 has archived items, the archived category is renamed to the goal
4248 (let* ((tfile todos-current-todos-file)
4249 (archive (concat (file-name-sans-extension (if file gfile tfile))
4251 (cat (todos-current-category))
4252 (cat+file (todos-read-category "Merge into category: " 'merge file))
4253 (goal (car cat+file))
4254 (gfile (cdr cat+file))
4255 archived-count here)
4256 ;; Merge in todo file.
4257 (with-current-buffer (get-buffer (find-file-noselect tfile))
4259 (let* ((buffer-read-only nil)
4262 (concat "^" (regexp-quote todos-category-beg)) nil t)
4264 (tbeg (progn (forward-line) (point-marker)))
4267 (concat "^" (regexp-quote todos-category-done)) nil t)
4268 (forward-line) (point-marker)))
4269 ;; Omit empty line between todo and done items.
4270 (tend (progn (forward-line -2) (point-marker)))
4272 (if (re-search-forward
4273 (concat "^" (regexp-quote todos-category-beg)) nil t)
4275 (goto-char (match-beginning 0))
4277 (point-max-marker))))
4278 (todo (buffer-substring-no-properties tbeg tend))
4279 (done (buffer-substring-no-properties dbeg cend)))
4280 (goto-char (point-min))
4281 ;; Merge any todo items.
4282 (unless (zerop (length todo))
4284 (concat "^" (regexp-quote (concat todos-category-beg goal)) "$")
4287 (concat "^" (regexp-quote todos-category-done)) nil t)
4289 (setq here (point-marker))
4291 (todos-update-count 'todo (todos-get-count 'todo cat) goal))
4292 ;; Merge any done items.
4293 (unless (zerop (length done))
4294 (goto-char (if (re-search-forward
4295 (concat "^" (regexp-quote todos-category-beg)) nil t)
4298 (when (zerop (length todo)) (setq here (point-marker)))
4300 (todos-update-count 'done (todos-get-count 'done cat) goal))
4301 (remove-overlays cbeg cend)
4302 (delete-region cbeg cend)
4303 (setq todos-categories (delete (assoc cat todos-categories)
4305 (todos-update-categories-sexp)
4306 (mapc (lambda (m) (set-marker m nil)) (list cbeg tbeg dbeg tend cend))))
4307 (when (file-exists-p archive)
4308 ;; Merge in archive file.
4309 (with-current-buffer (get-buffer (find-file-noselect archive))
4311 (goto-char (point-min))
4312 (let ((buffer-read-only nil)
4313 (cbeg (save-excursion
4314 (when (re-search-forward
4315 (concat "^" (regexp-quote
4316 (concat todos-category-beg cat)) "$")
4318 (goto-char (match-beginning 0))
4320 (gbeg (save-excursion
4321 (when (re-search-forward
4322 (concat "^" (regexp-quote
4323 (concat todos-category-beg goal)) "$")
4325 (goto-char (match-beginning 0))
4329 (setq archived-count (todos-get-count 'done cat))
4330 (setq cend (save-excursion
4331 (if (re-search-forward
4332 (concat "^" (regexp-quote todos-category-beg))
4336 (setq carch (save-excursion (goto-char cbeg) (forward-line)
4337 (buffer-substring-no-properties (point) cend)))
4338 ;; If both categories of the merge have archived items, merge the
4339 ;; source items to the goal items, else "merge" by renaming the
4340 ;; source category to goal.
4343 (goto-char (if (re-search-forward
4344 (concat "^" (regexp-quote todos-category-beg))
4349 (remove-overlays cbeg cend)
4350 (delete-region cbeg cend))
4352 (search-forward cat)
4353 (replace-match goal))
4354 (setq todos-categories (todos-make-categories-list t))
4355 (todos-update-categories-sexp)))))
4356 (with-current-buffer (get-file-buffer tfile)
4357 (when archived-count
4358 (unless (zerop archived-count)
4359 (todos-update-count 'archived archived-count goal)
4360 (todos-update-categories-sexp)))
4361 (todos-category-number goal)
4362 ;; If there are only merged done items, show them.
4363 (let ((todos-show-with-done (zerop (todos-get-count 'todo goal))))
4364 (todos-category-select)
4365 ;; Put point on the first merged item.
4367 (set-marker here nil)))
4369 (defun todos-set-category-priority (&optional arg)
4370 "Change priority of category at point in Todos Categories buffer.
4372 With ARG nil, prompt for the new priority number. Alternatively,
4373 the new priority can be provided by a numerical prefix ARG.
4374 Otherwise, if ARG is either of the symbols `raise' or `lower',
4375 raise or lower the category's priority by one."
4377 (let ((curnum (save-excursion
4378 ;; Get the number representing the priority of the category
4379 ;; on the current line.
4380 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
4381 (when curnum ; Do nothing if we're not on a category line.
4382 (let* ((maxnum (length todos-categories))
4383 (prompt (format "Set category priority (1-%d): " maxnum))
4384 (col (current-column))
4385 (buffer-read-only nil)
4386 (priority (cond ((and (eq arg 'raise) (> curnum 1))
4388 ((and (eq arg 'lower) (< curnum maxnum))
4391 (while (not priority)
4392 (setq candidate (or arg (read-number prompt)))
4395 (cond ((or (< candidate 1) (> candidate maxnum))
4396 (format "Priority must be an integer between 1 and %d: "
4398 ((= candidate curnum)
4399 "Choose a different priority than the current one: ")))
4400 (unless prompt (setq priority candidate)))
4401 (let* ((lower (< curnum priority)) ; Priority is being lowered.
4402 (head (butlast todos-categories
4403 (apply (if lower 'identity '1+)
4404 (list (- maxnum priority)))))
4405 (tail (nthcdr (apply (if lower 'identity '1-) (list priority))
4407 ;; Category's name and items counts list.
4408 (catcons (nth (1- curnum) todos-categories))
4409 (todos-categories (nconc head (list catcons) tail))
4411 (when lower (setq todos-categories (nreverse todos-categories)))
4412 (setq todos-categories (delete-dups todos-categories))
4413 (when lower (setq todos-categories (nreverse todos-categories)))
4414 (setq newcats todos-categories)
4416 (with-current-buffer (find-buffer-visiting todos-current-todos-file)
4417 (setq todos-categories newcats)
4418 (todos-update-categories-sexp))
4419 (todos-display-categories)
4420 (forward-line (1+ priority))
4421 (forward-char col))))))
4423 (defun todos-raise-category-priority ()
4424 "Raise priority of category at point in Todos Categories buffer."
4426 (todos-set-category-priority 'raise))
4428 (defun todos-lower-category-priority ()
4429 "Lower priority of category at point in Todos Categories buffer."
4431 (todos-set-category-priority 'lower))
4433 ;; ---------------------------------------------------------------------------
4434 ;;; Item editing commands
4437 (defun todos-insert-item (&optional arg diary nonmarking date-type time
4439 "Add a new Todo item to a category.
4440 \(See the note at the end of this document string about key
4441 bindings and convenience commands derived from this command.)
4443 With no (or nil) prefix argument ARG, add the item to the current
4444 category; with one prefix argument (C-u), prompt for a category
4445 from the current Todos file; with two prefix arguments (C-u C-u),
4446 first prompt for a Todos file, then a category in that file. If
4447 a non-existing category is entered, ask whether to add it to the
4448 Todos file; if answered affirmatively, add the category and
4449 insert the item there.
4451 When argument DIARY is non-nil, this overrides the intent of the
4452 user option `todos-include-in-diary' for this item: if
4453 `todos-include-in-diary' is nil, include the item in the Fancy
4454 Diary display, and if it is non-nil, exclude the item from the
4455 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
4456 has its intended effect.
4458 When the item is included in the Fancy Diary display and the
4459 argument NONMARKING is non-nil, this overrides the intent of the
4460 user option `todos-diary-nonmarking' for this item: if
4461 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
4462 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
4464 The argument DATE-TYPE determines the content of the item's
4465 mandatory date header string and how it is added:
4466 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
4467 when the user puts the cursor on a date and hits RET, that
4468 date, in the format set by `calendar-date-display-form',
4469 becomes the date in the header.
4470 - If DATE-TYPE is a string matching the regexp
4471 `todos-date-pattern', that string becomes the date in the
4472 header. This case is for the command
4473 `todos-insert-item-from-calendar' which is called from the
4475 - If DATE-TYPE is the symbol `date', the header contains the date
4476 in the format set by `calendar-date-display-form', with year,
4477 month and day individually prompted for (month with tab
4479 - If DATE-TYPE is the symbol `dayname' the header contains a
4480 weekday name instead of a date, prompted for with tab
4482 - If DATE-TYPE has any other value (including nil or none) the
4483 header contains the current date (in the format set by
4484 `calendar-date-display-form').
4486 With non-nil argument TIME prompt for a time string, which must
4487 match `diary-time-regexp'. Typing `<return>' at the prompt
4488 returns the current time, if the user option
4489 `todos-always-add-time-string' is non-nil, otherwise the empty
4490 string (i.e., no time string). If TIME is absent or nil, add or
4491 omit the current time string according as
4492 `todos-always-add-time-string' is non-nil or nil, respectively.
4494 The argument REGION-OR-HERE determines the source and location of
4496 - If the REGION-OR-HERE is the symbol `here', prompt for the text
4497 of the new item and insert it directly above the todo item at
4498 point (hence lowering the priority of the remaining items), or
4499 if point is on the empty line below the last todo item, insert
4500 the new item there. An error is signalled if
4501 `todos-insert-item' is invoked with `here' outside of the
4503 - If REGION-OR-HERE is the symbol `region', use the region of the
4504 current buffer as the text of the new item, depending on the
4505 value of user option `todos-use-only-highlighted-region': if
4506 this is non-nil, then use the region only when it is
4507 highlighted; otherwise, use the region regardless of
4508 highlighting. An error is signalled if there is no region in
4509 the current buffer. Prompt for the item's priority in the
4510 category (an integer between 1 and one more than the number of
4511 items in the category), and insert the item accordingly.
4512 - If REGION-OR-HERE has any other value (in particular, nil or
4513 none), prompt for the text and the item's priority, and insert
4514 the item accordingly.
4516 To facilitate using these arguments when inserting a new todo
4517 item, convenience commands have been defined for all admissible
4518 combinations together with mnenomic key bindings based on on the
4519 name of the arguments and their order in the command's argument
4520 list: diar_y_ - nonmar_k_ing - _c_alendar or _d_ate or day_n_ame
4521 - _t_ime - _r_egion or _h_ere. These key combinations are
4522 appended to the basic insertion key (i) and keys that allow a
4523 following key must be doubled when used finally. For example,
4524 `iyh' will insert a new item with today's date, marked according
4525 to the DIARY argument described above, and with priority
4526 according to the HERE argument; while `iyy' does the same except
4527 the priority is not given by HERE but by prompting."
4528 ;; An alternative interface for customizing key
4529 ;; binding is also provided with the function
4530 ;; `todos-insertion-bindings'." ;FIXME
4532 ;; If invoked outside of Todos mode and there is not yet any Todos
4533 ;; file, initialize one.
4534 (if (null todos-files)
4536 (let ((region (eq region-or-here 'region))
4537 (here (eq region-or-here 'here)))
4539 (let (use-empty-active-region)
4540 (unless (and todos-use-only-highlighted-region (use-region-p))
4541 (error "There is no active region"))))
4542 (let* ((buf (current-buffer))
4543 ;; (ocat (todos-current-category))
4544 ;; (todos-mm (eq major-mode 'todos-mode))
4545 (cat+file (cond ((equal arg '(4))
4546 (todos-read-category "Insert in category: "))
4548 (todos-read-category "Insert in category: "
4551 (cons (todos-current-category)
4552 (or todos-current-todos-file
4553 (todos-absolute-file-name
4554 todos-default-todos-file))))))
4555 (cat (car cat+file))
4556 (file (cdr cat+file))
4557 (new-item (if region
4558 (buffer-substring-no-properties
4559 (region-beginning) (region-end))
4560 (read-from-minibuffer "Todo item: ")))
4562 ((eq date-type 'date)
4564 ((eq date-type 'dayname)
4565 (todos-read-dayname))
4566 ((eq date-type 'calendar)
4567 (setq todos-date-from-calendar t)
4568 (or (todos-set-date-from-calendar)
4569 ;; If user exits Calendar before choosing
4570 ;; a date, cancel item insertion.
4572 ((and (stringp date-type)
4573 (string-match todos-date-pattern date-type))
4574 (setq todos-date-from-calendar date-type)
4575 (todos-set-date-from-calendar))
4577 (calendar-date-string (calendar-current-date) t t))))
4578 (time-string (or (and time (todos-read-time))
4579 (and todos-always-add-time-string
4580 (substring (current-time-string) 11 16)))))
4581 (setq todos-date-from-calendar nil)
4582 (find-file-noselect file 'nowarn)
4583 (set-window-buffer (selected-window)
4584 ;; If current category was nil till now, on
4585 ;; entering Todos mode here it will be set to
4586 ;; file's first category.
4587 (set-buffer (find-buffer-visiting file)))
4588 (setq todos-current-todos-file file)
4589 ;; If only done items are displayed in category, toggle to
4591 (when (and (goto-char (point-min))
4592 (looking-at todos-done-string-start))
4593 (todos-show-done-only))
4594 (unless todos-global-current-todos-file
4595 (setq todos-global-current-todos-file todos-current-todos-file))
4596 ;; These are not needed here, since they are called in
4597 ;; todos-set-item-priority.
4598 ;; (todos-category-number cat)
4599 ;; (todos-category-select)
4600 (let (buffer-read-only)
4602 ;; Add date, time and diary marking as required.
4603 (concat (if (not (and diary (not todos-include-in-diary)))
4604 todos-nondiary-start
4605 (when (and nonmarking (not todos-diary-nonmarking))
4606 diary-nonmarking-symbol))
4607 date-string (when (and time-string ; Can be empty string.
4608 (not (zerop (length time-string))))
4609 (concat " " time-string))
4610 (when (not (and diary (not todos-include-in-diary)))
4613 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4614 (setq new-item (replace-regexp-in-string
4615 "\\(\n\\)[^[:blank:]]"
4616 (concat "\n" (make-string todos-indent-to-here 32))
4617 new-item nil nil 1))
4619 (cond ((not (eq major-mode 'todos-mode))
4620 (error "Cannot insert a todo item here outside of Todos mode"))
4621 ((not (eq buf (current-buffer)))
4622 (error "Cannot insert an item here after changing buffer"))
4623 ((or (todos-done-item-p)
4624 ;; Point on last blank line.
4625 (save-excursion (forward-line -1) (todos-done-item-p)))
4626 (error "Cannot insert a new item in the done item section"))
4628 (todos-insert-with-overlays new-item)))
4629 ;; (if (and todos-mm (equal cat ocat))
4630 ;; (todos-insert-with-overlays new-item)
4632 ;; (todos-set-item-priority new-item (todos-current-category) t))
4634 (todos-set-item-priority new-item cat t)
4635 ;; In (at least) two circumstances, point may be at eob
4636 ;; and eob at window-start, so that that the todo items
4637 ;; are out of view: (i) if item is inserted at end of
4638 ;; category, (ii) if only done items are shown, this is
4639 ;; (above) programmatically toggled to show todo items,
4640 ;; and user cancels before setting new item's
4641 ;; priority. To make sure the todo items are displayed
4642 ;; in the window, force recentering.
4644 (todos-update-count 'todo 1)
4645 (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
4646 (todos-update-categories-sexp))))))
4648 (defun todos-copy-item ()
4649 "Copy item at point and insert the copy as a new item."
4651 (unless (or (todos-done-item-p) (looking-at "^$"))
4652 (let ((copy (todos-item-string))
4653 (diary-item (todos-diary-item-p)))
4654 (todos-set-item-priority copy (todos-current-category) t)
4655 (todos-update-count 'todo 1)
4656 (when diary-item (todos-update-count 'diary 1))
4657 (todos-update-categories-sexp))))
4659 (defvar todos-date-from-calendar nil
4660 "Helper variable for setting item date from the Emacs Calendar.")
4662 (defun todos-set-date-from-calendar ()
4663 "Return string of date chosen from Calendar."
4664 (cond ((and (stringp todos-date-from-calendar)
4665 (string-match todos-date-pattern todos-date-from-calendar))
4666 todos-date-from-calendar)
4667 (todos-date-from-calendar
4668 (let (calendar-view-diary-initially-flag)
4669 (calendar)) ; *Calendar* is now current buffer.
4670 (define-key calendar-mode-map [remap newline] 'exit-recursive-edit)
4671 ;; If user exits Calendar before choosing a date, clean up properly.
4672 (define-key calendar-mode-map
4673 [remap calendar-exit] (lambda ()
4677 (exit-recursive-edit))))
4678 (message "Put cursor on a date and type <return> to set it.")
4681 (when (equal (buffer-name) calendar-buffer)
4682 (setq todos-date-from-calendar
4683 (calendar-date-string (calendar-cursor-to-date t) t t))
4685 todos-date-from-calendar)
4686 (define-key calendar-mode-map [remap newline] nil)
4687 (define-key calendar-mode-map [remap calendar-exit] nil)
4688 (unless (zerop (recursion-depth)) (exit-recursive-edit))
4689 (when (stringp todos-date-from-calendar)
4690 todos-date-from-calendar)))))
4692 (defun todos-delete-item ()
4693 "Delete at least one item in this category.
4695 If there are marked items, delete all of these; otherwise, delete
4700 (let* ((cat (todos-current-category))
4701 (marked (assoc cat todos-categories-with-marks))
4702 (item (unless marked (todos-item-string)))
4704 (y-or-n-p "Permanently delete all marked items? ")
4706 (setq ov (make-overlay
4707 (save-excursion (todos-item-start))
4708 (save-excursion (todos-item-end))))
4709 (overlay-put ov 'face 'todos-search)
4710 (y-or-n-p (concat "Permanently delete this item? ")))))
4713 (and marked (goto-char (point-min)))
4716 (if (or (and marked (todos-marked-item-p)) item)
4718 (if (todos-done-item-p)
4719 (todos-update-count 'done -1)
4720 (todos-update-count 'todo -1 cat)
4721 (and (todos-diary-item-p) (todos-update-count 'diary -1)))
4722 (if ov (delete-overlay ov))
4724 ;; Don't leave point below last item.
4725 (and item (bolp) (eolp) (< (point-min) (point-max))
4726 (todos-backward-item))
4728 (throw 'done (setq item nil))))
4729 (todos-forward-item))))
4731 (setq todos-categories-with-marks
4732 (assq-delete-all cat todos-categories-with-marks)))
4733 (todos-update-categories-sexp)
4734 (todos-prefix-overlays)))
4735 (if ov (delete-overlay ov)))))
4737 (defun todos-edit-item (&optional arg)
4738 "Edit the Todo item at point.
4740 With non-nil prefix argument ARG, include the item's date/time
4741 header, making it also editable; otherwise, include only the item
4744 If the item consists of only one logical line, edit it in the
4745 minibuffer; otherwise, edit it in Todos Edit mode."
4747 (when (todos-item-string)
4748 (let* ((opoint (point))
4749 (start (todos-item-start))
4752 (concat todos-date-string-start todos-date-pattern
4753 "\\( " diary-time-regexp "\\)?"
4754 (regexp-quote todos-nondiary-end) "?")
4755 (line-end-position) t)
4756 (1+ (- (point) start))))
4757 (header (substring (todos-item-string) 0 item-beg))
4758 (item (if arg (todos-item-string)
4759 (substring (todos-item-string) item-beg)))
4760 (multiline (> (length (split-string item "\n")) 1))
4761 (buffer-read-only nil))
4763 (todos-edit-multiline-item)
4764 (let ((new (concat (if arg "" header)
4765 (read-string "Edit: " (if arg
4766 (cons item item-beg)
4769 (while (not (string-match (concat todos-date-string-start
4770 todos-date-pattern) new))
4771 (setq new (read-from-minibuffer
4772 "Item must start with a date: " new))))
4773 ;; Ensure lines following hard newlines are indented.
4774 (setq new (replace-regexp-in-string
4775 "\\(\n\\)[^[:blank:]]"
4776 (concat "\n" (make-string todos-indent-to-here 32)) new
4778 ;; If user moved point during editing, make sure it moves back.
4781 (todos-insert-with-overlays new)
4782 (move-to-column item-beg))))))
4784 (defun todos-edit-multiline-item ()
4785 "Edit current Todo item in Todos Edit mode.
4786 Use of newlines invokes `todos-indent' to insure compliance with
4787 the format of Diary entries."
4789 (todos-edit-multiline t))
4791 (defun todos-edit-multiline (&optional item)
4794 (let ((buffer-name todos-edit-buffer))
4797 (set-buffer (make-indirect-buffer
4798 (file-name-nondirectory todos-current-todos-file)
4801 (narrow-to-region (todos-item-start) (todos-item-end))
4804 (message "%s" (substitute-command-keys
4805 (concat "Type \\[todos-edit-quit] to check file format "
4806 "validity and return to Todos mode.\n")))))
4808 (defun todos-edit-quit ()
4809 "Return from Todos Edit mode to Todos mode.
4810 If the item contains hard line breaks, make sure the following
4811 lines are indented by `todos-indent-to-here' to conform to diary
4814 If the whole file was in Todos Edit mode, check before returning
4815 whether the file is still a valid Todos file and if so, also
4816 recalculate the Todos categories sexp, in case changes were made
4817 in the number or names of categories."
4819 (if (eq (buffer-size) (- (point-max) (point-min)))
4820 (when (todos-check-format)
4821 (todos-repair-categories-sexp))
4822 ;; Ensure lines following hard newlines are indented.
4823 (let ((item (replace-regexp-in-string
4824 "\\(\n\\)[^[:blank:]]"
4825 (concat "\n" (make-string todos-indent-to-here 32))
4826 (buffer-string) nil nil 1)))
4827 (delete-region (point-min) (point-max))
4830 ;; In case next buffer is not the one holding todos-current-todos-file.
4833 (defun todos-edit-item-header-1 (what &optional inc)
4834 "Function underlying commands to edit item date/time header.
4836 The argument WHAT (passed by invoking commands) specifies what
4837 part of the header to edit; possible values are these symbols:
4838 `date', to edit the year, month, and day of the date string;
4839 `time', to edit just the time string; `calendar', to select the
4840 date from the Calendar; `today', to set the date to today's date;
4841 `dayname', to set the date string to the name of a day or to
4842 change the day name; and `year', `month' or `day', to edit only
4843 these respective parts of the date string (`day' is the number of
4844 the given day of the month, and `month' is either the name of the
4845 given month or its number, depending on the value of
4846 `calendar-date-display-form').
4848 The optional argument INC is a positive or negative integer
4849 \(passed by invoking commands as a numerical prefix argument)
4850 that in conjunction with the WHAT values `year', `month' or
4851 `day', increments or decrements the specified date string
4852 component by the specified number of suitable units, i.e., years,
4853 months, or days, with automatic adjustment of the other date
4854 string components as necessary.
4856 If there are marked items, apply the same edit to all of these;
4857 otherwise, edit just the item at point."
4858 (let* ((cat (todos-current-category))
4859 (marked (assoc cat todos-categories-with-marks))
4861 (todos-date-from-calendar t)
4862 (buffer-read-only nil)
4863 ndate ntime year monthname month day
4864 dayname) ; Needed by calendar-date-display-form.
4866 (or (and marked (goto-char (point-min))) (todos-item-start))
4870 (while (not (todos-marked-item-p))
4871 (todos-forward-item)
4872 (and (eobp) (throw 'end nil))))
4873 (re-search-forward (concat todos-date-string-start "\\(?1:"
4875 "\\)\\(?2: " diary-time-regexp "\\)?"
4876 (regexp-quote todos-nondiary-end) "?")
4877 (line-end-position) t)
4878 (let* ((odate (match-string-no-properties 1))
4879 (otime (match-string-no-properties 2))
4880 (omonthname (match-string-no-properties 6))
4881 (omonth (match-string-no-properties 7))
4882 (oday (match-string-no-properties 8))
4883 (oyear (match-string-no-properties 9))
4884 (tmn-array todos-month-name-array)
4885 (mlist (append tmn-array nil))
4886 (tma-array todos-month-abbrev-array)
4887 (mablist (append tma-array nil))
4888 (yy (and oyear (unless (string= oyear "*")
4889 (string-to-number oyear))))
4890 (mm (or (and omonth (unless (string= omonth "*")
4891 (string-to-number omonth)))
4892 (1+ (- (length mlist)
4893 (length (or (member omonthname mlist)
4894 (member omonthname mablist)))))))
4895 (dd (and oday (unless (string= oday "*")
4896 (string-to-number oday)))))
4897 ;; If there are marked items, use only the first to set
4898 ;; header changes, and apply these to all marked items.
4902 (setq ndate (todos-read-date)))
4903 ((eq what 'calendar)
4904 (setq ndate (save-match-data (todos-set-date-from-calendar))))
4906 (setq ndate (calendar-date-string (calendar-current-date) t t)))
4908 (setq ndate (todos-read-dayname)))
4910 (setq ntime (save-match-data (todos-read-time)))
4911 (when (> (length ntime) 0)
4912 (setq ntime (concat " " ntime))))
4913 ;; When date string consists only of a day name,
4914 ;; passing other date components is a NOP.
4915 ((and (memq what '(year month day))
4916 (not (or oyear omonth oday))))
4919 monthname omonthname
4921 year (cond ((not current-prefix-arg)
4922 (todos-read-date 'year))
4923 ((string= oyear "*")
4924 (error "Cannot increment *"))
4926 (number-to-string (+ yy inc))))))
4930 (if (memq 'month calendar-date-display-form)
4933 (cond ((not current-prefix-arg)
4934 (todos-read-date 'month))
4935 ((or (string= omonth "*") (= mm 13))
4936 (error "Cannot increment *"))
4938 (let ((mminc (+ mm inc)))
4939 ;; Increment or decrement month by INC
4941 (setq mm (% mminc 12))
4942 ;; If result is 0, make month December.
4943 (setq mm (if (= mm 0) 12 (abs mm)))
4944 ;; Adjust year if necessary.
4945 (setq year (or (and (cond ((> mminc 12)
4946 (+ yy (/ mminc 12)))
4948 (- yy (/ mminc 12) 1))
4950 (number-to-string yy))
4952 ;; Return the changed numerical month as
4953 ;; a string or the corresponding month name.
4955 (number-to-string mm)
4956 (aref tma-array (1- mm))))))
4957 (let ((yy (string-to-number year)) ; 0 if year is "*".
4958 ;; When mm is 13 (corresponding to "*" as value
4959 ;; of month), this raises an args-out-of-range
4960 ;; error in calendar-last-day-of-month, so use 1
4961 ;; (corresponding to January) to get 31 days.
4962 (mm (if (= mm 13) 1 mm)))
4963 (if (> (string-to-number day)
4964 (calendar-last-day-of-month mm yy))
4965 (error "%s %s does not have %s days"
4966 (aref tmn-array (1- mm))
4967 (if (= mm 2) yy "") day))))
4971 monthname omonthname
4973 ((not current-prefix-arg)
4974 (todos-read-date 'day mm oyear))
4976 (error "Cannot increment *"))
4977 ((or (string= omonth "*") (string= omonthname "*"))
4978 (setq dd (+ dd inc))
4980 (error "A month cannot have more than 31 days")
4981 (number-to-string dd)))
4982 ;; Increment or decrement day by INC,
4983 ;; adjusting month and year if necessary
4984 ;; (if year is "*" assume current year to
4985 ;; calculate adjustment).
4987 (let* ((yy (or yy (calendar-extract-year
4988 (calendar-current-date))))
4989 (date (calendar-gregorian-from-absolute
4990 (+ (calendar-absolute-from-gregorian
4991 (list mm dd yy)) inc)))
4992 (adjmm (nth 0 date)))
4993 ;; Set year and month(name) to adjusted values.
4994 (unless (string= year "*")
4995 (setq year (number-to-string (nth 2 date))))
4997 (setq month (number-to-string adjmm))
4998 (setq monthname (aref tma-array (1- adjmm))))
4999 ;; Return changed numerical day as a string.
5000 (number-to-string (nth 1 date)))))))))
5001 ;; If new year, month or day date string components were
5002 ;; calculated, rebuild the whole date string from them.
5003 (when (memq what '(year month day))
5004 (if (or oyear omonth omonthname oday)
5005 (setq ndate (mapconcat 'eval calendar-date-display-form ""))
5006 (message "Cannot edit date component of empty date string")))
5007 (when ndate (replace-match ndate nil nil nil 1))
5008 ;; Add new time string to the header, if it was supplied.
5011 (replace-match ntime nil nil nil 2)
5012 (goto-char (match-end 1))
5014 (setq todos-date-from-calendar nil)
5016 ;; Apply the changes to the first marked item header to the
5017 ;; remaining marked items. If there are no marked items,
5020 (todos-forward-item)
5021 (goto-char (point-max))))))))
5023 (defun todos-edit-item-header ()
5024 "Interactively edit at least the date of item's date/time header.
5025 If user option `todos-always-add-time-string' is non-nil, also
5026 edit item's time string."
5028 (todos-edit-item-header-1 'date)
5029 (when todos-always-add-time-string
5030 (todos-edit-item-time)))
5032 (defun todos-edit-item-time ()
5033 "Interactively edit the time string of item's date/time header."
5035 (todos-edit-item-header-1 'time))
5037 (defun todos-edit-item-date-from-calendar ()
5038 "Interactively edit item's date using the Calendar."
5040 (todos-edit-item-header-1 'calendar))
5042 (defun todos-edit-item-date-to-today ()
5043 "Set item's date to today's date."
5045 (todos-edit-item-header-1 'today))
5047 (defun todos-edit-item-date-day-name ()
5048 "Replace item's date with the name of a day of the week."
5050 (todos-edit-item-header-1 'dayname))
5052 (defun todos-edit-item-date-year (&optional inc)
5053 "Interactively edit the year of item's date string.
5054 With prefix argument INC a positive or negative integer,
5055 increment or decrement the year by INC."
5057 (todos-edit-item-header-1 'year inc))
5059 (defun todos-edit-item-date-month (&optional inc)
5060 "Interactively edit the month of item's date string.
5061 With prefix argument INC a positive or negative integer,
5062 increment or decrement the month by INC."
5064 (todos-edit-item-header-1 'month inc))
5066 (defun todos-edit-item-date-day (&optional inc)
5067 "Interactively edit the day of the month of item's date string.
5068 With prefix argument INC a positive or negative integer,
5069 increment or decrement the day by INC."
5071 (todos-edit-item-header-1 'day inc))
5073 (defun todos-edit-item-diary-inclusion ()
5074 "Change diary status of one or more todo items in this category.
5075 That is, insert `todos-nondiary-marker' if the candidate items
5076 lack this marking; otherwise, remove it.
5078 If there are marked todo items, change the diary status of all
5079 and only these, otherwise change the diary status of the item at
5082 (let ((buffer-read-only)
5083 (marked (assoc (todos-current-category)
5084 todos-categories-with-marks)))
5087 (when marked (goto-char (point-min)))
5089 (if (todos-done-item-p)
5090 (throw 'stop (message "Done items cannot be edited"))
5091 (unless (and marked (not (todos-marked-item-p)))
5092 (let* ((beg (todos-item-start))
5093 (lim (save-excursion (todos-item-end)))
5094 (end (save-excursion
5095 (or (todos-time-string-matcher lim)
5096 (todos-date-string-matcher lim)))))
5097 (if (looking-at (regexp-quote todos-nondiary-start))
5100 (search-forward todos-nondiary-end (1+ end) t)
5102 (todos-update-count 'diary 1))
5104 (insert todos-nondiary-start)
5105 (goto-char (1+ end))
5106 (insert todos-nondiary-end)
5107 (todos-update-count 'diary -1)))))
5108 (unless marked (throw 'stop nil))
5109 (todos-forward-item)))))
5110 (todos-update-categories-sexp)))
5112 (defun todos-edit-category-diary-inclusion (arg)
5113 "Make all items in this category diary items.
5114 With prefix ARG, make all items in this category non-diary
5118 (goto-char (point-min))
5119 (let ((todo-count (todos-get-count 'todo))
5120 (diary-count (todos-get-count 'diary))
5124 (if (todos-done-item-p) ; We've gone too far.
5126 (let* ((beg (todos-item-start))
5127 (lim (save-excursion (todos-item-end)))
5128 (end (save-excursion
5129 (or (todos-time-string-matcher lim)
5130 (todos-date-string-matcher lim)))))
5132 (unless (looking-at (regexp-quote todos-nondiary-start))
5133 (insert todos-nondiary-start)
5134 (goto-char (1+ end))
5135 (insert todos-nondiary-end))
5136 (when (looking-at (regexp-quote todos-nondiary-start))
5138 (search-forward todos-nondiary-end (1+ end) t)
5139 (replace-match "")))))
5140 (todos-forward-item))
5141 (unless (if arg (zerop diary-count) (= diary-count todo-count))
5142 (todos-update-count 'diary (if arg
5144 (- todo-count diary-count))))
5145 (todos-update-categories-sexp)))))
5147 (defun todos-edit-item-diary-nonmarking ()
5148 "Change non-marking of one or more diary items in this category.
5149 That is, insert `diary-nonmarking-symbol' if the candidate items
5150 lack this marking; otherwise, remove it.
5152 If there are marked todo items, change the non-marking status of
5153 all and only these, otherwise change the non-marking status of
5156 (let ((buffer-read-only)
5157 (marked (assoc (todos-current-category)
5158 todos-categories-with-marks)))
5161 (when marked (goto-char (point-min)))
5163 (if (todos-done-item-p)
5164 (throw 'stop (message "Done items cannot be edited"))
5165 (unless (and marked (not (todos-marked-item-p)))
5167 (unless (looking-at (regexp-quote todos-nondiary-start))
5168 (if (looking-at (regexp-quote diary-nonmarking-symbol))
5170 (insert diary-nonmarking-symbol))))
5171 (unless marked (throw 'stop nil))
5172 (todos-forward-item)))))))
5174 (defun todos-edit-category-diary-nonmarking (arg)
5175 "Add `diary-nonmarking-symbol' to all diary items in this category.
5176 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
5177 items in this category."
5180 (goto-char (point-min))
5181 (let (buffer-read-only)
5184 (if (todos-done-item-p) ; We've gone too far.
5186 (unless (looking-at (regexp-quote todos-nondiary-start))
5188 (when (looking-at (regexp-quote diary-nonmarking-symbol))
5190 (unless (looking-at (regexp-quote diary-nonmarking-symbol))
5191 (insert diary-nonmarking-symbol))))
5192 (todos-forward-item)))))))
5194 (defun todos-set-item-priority (&optional item cat new arg)
5195 "Prompt for and set ITEM's priority in CATegory.
5197 Interactively, ITEM is the todo item at point, CAT is the current
5198 category, and the priority is a number between 1 and the number
5199 of items in the category. Non-interactively, non-nil NEW means
5200 ITEM is a new item and the lowest priority is one more than the
5201 number of items in CAT.
5203 The new priority is set either interactively by prompt or by a
5204 numerical prefix argument, or noninteractively by argument ARG,
5205 whose value can be either of the symbols `raise' or `lower',
5206 meaning to raise or lower the item's priority by one."
5207 (interactive) ;FIXME: Prefix arg?
5208 (unless (and (called-interactively-p 'any)
5209 (or (todos-done-item-p) (looking-at "^$")))
5210 (let* ((item (or item (todos-item-string)))
5211 (marked (todos-marked-item-p))
5212 (cat (or cat (cond ((eq major-mode 'todos-mode)
5213 (todos-current-category))
5214 ((eq major-mode 'todos-filtered-items-mode)
5216 (concat todos-date-string-start
5218 "\\( " diary-time-regexp "\\)?"
5219 (regexp-quote todos-nondiary-end)
5220 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
5222 (re-search-forward regexp1 nil t)
5223 (match-string-no-properties 1)))))))
5225 (todo (cond ((or (eq arg 'raise) (eq arg 'lower)
5226 (eq major-mode 'todos-filtered-items-mode))
5228 (let ((curstart (todos-item-start))
5230 (goto-char (point-min))
5231 (while (looking-at todos-item-start)
5232 (setq count (1+ count))
5233 (when (= (point) curstart) (setq curnum count))
5234 (todos-forward-item))
5236 ((eq major-mode 'todos-mode)
5237 (todos-get-count 'todo cat))))
5238 (maxnum (if new (1+ todo) todo))
5239 (prompt (format "Set item priority (1-%d): " maxnum))
5240 (priority (cond ((numberp current-prefix-arg)
5242 ((and (eq arg 'raise) (>= curnum 1))
5244 ((and (eq arg 'lower) (<= curnum maxnum))
5248 (unless (and priority
5249 (or (and (eq arg 'raise) (zerop priority))
5250 (and (eq arg 'lower) (> priority maxnum))))
5251 ;; When moving item to another category, show the category before
5252 ;; prompting for its priority.
5253 (unless (or arg (called-interactively-p 'any))
5254 (todos-category-number cat)
5255 ;; If done items in category are visible, keep them visible.
5256 (let ((done todos-show-with-done))
5257 (when (> (buffer-size) (- (point-max) (point-min)))
5259 (goto-char (point-min))
5260 (setq done (re-search-forward todos-done-string-start nil t))))
5261 (let ((todos-show-with-done done))
5262 (todos-category-select)))))
5263 ;; Prompt for priority only when the category has at least one todo item.
5265 (while (not priority)
5266 (setq candidate (read-number prompt))
5267 (setq prompt (when (or (< candidate 1) (> candidate maxnum))
5268 (format "Priority must be an integer between 1 and %d.\n"
5270 (unless prompt (setq priority candidate))))
5271 ;; In Top Priorities buffer, an item's priority can be changed
5272 ;; wrt items in another category, but not wrt items in the same
5274 (when (eq major-mode 'todos-filtered-items-mode)
5275 (let* ((regexp2 (concat todos-date-string-start todos-date-pattern
5276 "\\( " diary-time-regexp "\\)?"
5277 (regexp-quote todos-nondiary-end)
5278 "?\\(?1:" (regexp-quote cat) "\\)"))
5279 (end (cond ((< curnum priority)
5280 (save-excursion (todos-item-end)))
5281 ((> curnum priority)
5282 (save-excursion (todos-item-start)))))
5283 (match (save-excursion
5284 (cond ((< curnum priority)
5285 (todos-forward-item (1+ (- priority curnum)))
5286 (when (re-search-backward regexp2 end t)
5287 (match-string-no-properties 1)))
5288 ((> curnum priority)
5289 (todos-backward-item (- curnum priority))
5290 (when (re-search-forward regexp2 end t)
5291 (match-string-no-properties 1)))))))
5293 (error (concat "Cannot reprioritize items from the same "
5294 "category in this mode, only in Todos mode")))))
5295 ;; Interactively or with non-nil ARG, relocate the item within its
5297 (when (or arg (called-interactively-p 'any))
5298 (todos-remove-item))
5299 (goto-char (point-min))
5301 (unless (= priority 1)
5302 (todos-forward-item (1- priority))
5303 ;; When called from todos-item-undo and the highest priority
5304 ;; is chosen, this advances point to the first done item, so
5305 ;; move it up to the empty line above the done items
5307 (when (looking-back (concat "^"
5308 (regexp-quote todos-category-done) "\n"))
5309 (todos-backward-item))))
5310 (todos-insert-with-overlays item)
5311 ;; If item was marked, restore the mark.
5313 (let* ((ov (todos-prefix-overlay))
5314 (pref (overlay-get ov 'before-string)))
5315 (overlay-put ov 'before-string (concat todos-item-mark pref))))))))
5317 (defun todos-raise-item-priority ()
5318 "Raise priority of current item by moving it up by one item."
5320 (todos-set-item-priority nil nil nil 'raise))
5322 (defun todos-lower-item-priority ()
5323 "Lower priority of current item by moving it down by one item."
5325 (todos-set-item-priority nil nil nil 'lower))
5327 (defun todos-move-item (&optional file)
5328 "Move at least one todo or done item to another category.
5329 If there are marked items, move all of these; otherwise, move
5332 With prefix argument FILE, prompt for a specific Todos file and
5333 choose (with TAB completion) a category in it to move the item or
5334 items to; otherwise, choose and move to any category in either
5335 the current Todos file or one of the files in
5336 `todos-category-completions-files'. If the chosen category is
5337 not an existing categories, then it is created and the item(s)
5338 become(s) the first entry/entries in that category.
5340 With moved Todo items, prompt to set the priority in the category
5341 moved to (with multiple todos items, the one that had the highest
5342 priority in the category moved from gets the new priority and the
5343 rest of the moved todo items are inserted in sequence below it).
5344 Moved done items are appended to the end of the done items
5345 section in the category moved to."
5347 (let* ((cat1 (todos-current-category))
5348 (marked (assoc cat1 todos-categories-with-marks)))
5349 ;; NOP if point is not on an item and there are no marked items.
5350 (unless (and (looking-at "^$")
5352 (let* ((buffer-read-only)
5353 (file1 todos-current-todos-file)
5354 (num todos-category-number)
5355 (item (todos-item-string))
5356 (diary-item (todos-diary-item-p))
5357 (done-item (and (todos-done-item-p) (concat item "\n")))
5358 (omark (save-excursion (todos-item-start) (point-marker)))
5362 ov cat+file cat2 file2 moved nmark todo-items done-items)
5366 (setq ov (make-overlay (save-excursion (todos-item-start))
5367 (save-excursion (todos-item-end))))
5368 (overlay-put ov 'face 'todos-search))
5369 (setq cat+file (let ((pl (if (and marked (> (cdr marked) 1))
5371 (todos-read-category (concat "Move item" pl
5375 file2 (cdr cat+file)))
5376 (if ov (delete-overlay ov)))
5377 (set-buffer (find-buffer-visiting file1))
5380 (goto-char (point-min))
5382 (when (todos-marked-item-p)
5383 (if (todos-done-item-p)
5384 (setq done-items (concat done-items
5385 (todos-item-string) "\n")
5387 (setq todo-items (concat todo-items
5388 (todos-item-string) "\n")
5390 (when (todos-diary-item-p)
5391 (setq diary (1+ diary)))))
5392 (todos-forward-item))
5393 ;; Chop off last newline of multiple todo item string,
5394 ;; since it will be reinserted when setting priority
5395 ;; (but with done items priority is not set, so keep
5398 (setq todo-items (substring todo-items 0 -1))))
5399 (if (todos-done-item-p)
5402 (when (todos-diary-item-p) (setq diary 1))))
5403 (set-window-buffer (selected-window)
5404 (set-buffer (find-file-noselect file2 'nowarn)))
5407 (when (or todo-items (and item (not done-item)))
5408 (todos-set-item-priority (or todo-items item) cat2 t))
5409 ;; Move done items en bloc to end of done item section.
5410 (when (or done-items done-item)
5411 (todos-category-number cat2)
5413 (goto-char (point-min))
5414 (re-search-forward (concat "^" (regexp-quote
5415 (concat todos-category-beg cat2))
5418 (goto-char (if (re-search-forward
5419 (concat "^" (regexp-quote todos-category-beg))
5423 (insert (or done-items done-item)))
5426 ;; Move succeeded, so remove item from starting category,
5427 ;; update item counts and display the category containing
5430 (setq nmark (point-marker))
5431 (when todo (todos-update-count 'todo todo))
5432 (when diary (todos-update-count 'diary diary))
5433 (when done (todos-update-count 'done done))
5434 (todos-update-categories-sexp)
5435 (with-current-buffer (find-buffer-visiting file1)
5444 (concat "^" (regexp-quote todos-category-beg)) nil t)
5447 (setq end (if (re-search-forward
5448 (concat "^" (regexp-quote
5449 todos-category-beg)) nil t)
5453 (while (< (point) end)
5454 (if (todos-marked-item-p)
5456 (todos-forward-item)))
5457 (setq todos-categories-with-marks
5458 (assq-delete-all cat1 todos-categories-with-marks)))
5459 (if ov (delete-overlay ov))
5460 (todos-remove-item))))
5461 (when todo (todos-update-count 'todo (- todo) cat1))
5462 (when diary (todos-update-count 'diary (- diary) cat1))
5463 (when done (todos-update-count 'done (- done) cat1))
5464 (todos-update-categories-sexp))
5465 (set-window-buffer (selected-window)
5466 (set-buffer (find-file-noselect file2 'nowarn)))
5467 (setq todos-category-number (todos-category-number cat2))
5468 (let ((todos-show-with-done (or done-items done-item)))
5469 (todos-category-select))
5471 ;; If item is moved to end of category, make sure the
5472 ;; items above it are displayed in the window.
5474 ;; User quit before setting priority of todo item(s), so
5475 ;; return to starting category.
5477 (todos-category-number cat1)
5478 (todos-category-select)
5479 (goto-char omark))))))))
5481 (defun todos-item-done (&optional arg)
5482 "Tag a todo item in this category as done and relocate it.
5484 With prefix argument ARG prompt for a comment and append it to
5485 the done item; this is only possible if there are no marked
5486 items. If there are marked items, tag all of these with
5487 `todos-done-string' plus the current date and, if
5488 `todos-always-add-time-string' is non-nil, the current time;
5489 otherwise, just tag the item at point. Items tagged as done are
5490 relocated to the category's (by default hidden) done section. If
5491 done items are visible on invoking this command, they remain
5494 (let* ((cat (todos-current-category))
5495 (marked (assoc cat todos-categories-with-marks)))
5496 (unless (or (todos-done-item-p)
5497 ;; Point is between todo and done items.
5498 (and (looking-at "^$") (not marked)))
5499 (let* ((date-string (calendar-date-string (calendar-current-date) t t))
5500 (time-string (if todos-always-add-time-string
5501 (concat " " (substring (current-time-string) 11 16))
5503 (done-prefix (concat "[" todos-done-string date-string time-string
5505 (comment (and arg (not marked) (read-string "Enter a comment: ")))
5508 (show-done (save-excursion
5509 (goto-char (point-min))
5510 (re-search-forward todos-done-string-start nil t)))
5513 (and marked (goto-char (point-min)))
5515 ;; Stop looping when we hit the empty line below the last
5516 ;; todo item (this is eobp if only done items are hidden).
5517 (while (not (looking-at "^$")) ;(not (eobp))
5518 (if (or (not marked) (and marked (todos-marked-item-p)))
5520 (setq item (todos-item-string))
5521 (setq done-item (cond (marked
5522 (concat done-item done-prefix item "\n"))
5524 (concat done-prefix item " ["
5525 todos-comment-string
5528 (concat done-prefix item))))
5529 (setq item-count (1+ item-count))
5530 (when (todos-diary-item-p)
5531 (setq diary-count (1+ diary-count)))
5533 (unless marked (throw 'done nil)))
5534 (todos-forward-item))))
5536 ;; Chop off last newline of done item string.
5537 (setq done-item (substring done-item 0 -1))
5538 (setq todos-categories-with-marks
5539 (assq-delete-all cat todos-categories-with-marks)))
5543 (concat "^" (regexp-quote todos-category-done)) nil t)
5545 (insert done-item "\n"))
5546 (todos-update-count 'todo (- item-count))
5547 (todos-update-count 'done item-count)
5548 (todos-update-count 'diary (- diary-count))
5549 (todos-update-categories-sexp)
5550 (let ((todos-show-with-done show-done))
5551 (todos-category-select))))))
5553 (defun todos-done-item-add-edit-or-delete-comment (&optional arg)
5554 "Add a comment to this done item or edit an existing comment.
5555 With prefix ARG delete an existing comment."
5557 (when (todos-done-item-p)
5558 (let ((item (todos-item-string))
5559 (end (save-excursion (todos-item-end)))
5560 comment buffer-read-only)
5563 (if (re-search-forward (concat " \\["
5564 (regexp-quote todos-comment-string)
5565 ": \\([^]]+\\)\\]") end t)
5567 (when (y-or-n-p "Delete comment? ")
5568 (delete-region (match-beginning 0) (match-end 0)))
5569 (setq comment (read-string "Edit comment: "
5570 (cons (match-string 1) 1)))
5571 (replace-match comment nil nil nil 1))
5572 (setq comment (read-string "Enter a comment: "))
5574 (insert " [" todos-comment-string ": " comment "]"))))))
5576 (defun todos-item-undo ()
5577 "Restore this done item to the todo section of this category.
5578 If done item has a comment, ask whether to omit the comment from
5581 (let* ((cat (todos-current-category))
5582 (marked (assoc cat todos-categories-with-marks)))
5583 (when (or marked (todos-done-item-p))
5584 (let ((buffer-read-only)
5585 (bufmod (buffer-modified-p))
5587 (orig-mrk (progn (todos-item-start) (point-marker)))
5588 (orig-item (todos-item-string))
5592 start end item undone)
5593 (and marked (goto-char (point-min)))
5596 (if (or (not marked) (and marked (todos-marked-item-p)))
5597 (if (not (todos-done-item-p))
5598 (error "Only done items can be undone")
5600 ;; Find the end of the date string added upon tagging item as
5602 (setq start (search-forward "] "))
5603 (setq item-count (1+ item-count))
5604 (unless (looking-at (regexp-quote todos-nondiary-start))
5605 (setq diary-count (1+ diary-count)))
5606 (setq end (save-excursion (todos-item-end)))
5607 ;; Ask (once) whether to omit done item's comment. If
5608 ;; affirmed, omit subsequent comments without asking.
5609 (when (re-search-forward
5610 (concat " \\[" (regexp-quote todos-comment-string)
5611 ": [^]]+\\]") end t)
5612 (if (eq first 'first)
5614 (if (eq todos-undo-item-omit-comment 'ask)
5616 "Omit comment from restored item? ")
5618 (when todos-undo-item-omit-comment 'omit)))
5620 (when (eq first 'omit)
5621 (delete-region (match-beginning 0) (match-end 0))
5622 (setq end (point))))
5623 (setq item (concat item
5624 (buffer-substring-no-properties start end)
5625 (when marked "\n")))
5627 (unless marked (throw 'done nil)))
5628 (todos-forward-item))))
5631 (setq todos-categories-with-marks
5632 (assq-delete-all cat todos-categories-with-marks))
5633 ;; Insert undone items that were marked at end of todo item list.
5634 (goto-char (point-min))
5635 (re-search-forward (concat "^" (regexp-quote todos-category-done))
5639 (todos-update-count 'todo item-count)
5640 (todos-update-count 'done (- item-count))
5641 (when diary-count (todos-update-count 'diary diary-count))
5642 (todos-update-categories-sexp)
5643 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5644 (todos-category-select)))
5645 ;; With an unmarked undone item, prompt for its priority. If user
5646 ;; cancels before setting new priority, then leave the done item
5650 (todos-set-item-priority item (todos-current-category) t)
5652 (todos-update-count 'todo 1)
5653 (todos-update-count 'done -1)
5654 (and (todos-diary-item-p) (todos-update-count 'diary 1))
5655 (todos-update-categories-sexp)
5656 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5657 (todos-category-select)))
5659 (let ((todos-show-with-done t))
5661 (goto-char orig-mrk)
5662 (todos-insert-with-overlays orig-item)
5663 (set-buffer-modified-p bufmod)
5664 (todos-category-select))
5665 (goto-char opoint))))
5666 (set-marker orig-mrk nil)))))
5668 (defun todos-archive-done-item (&optional all)
5669 "Archive at least one done item in this category.
5671 If there are marked done items (and no marked todo items),
5672 archive all of these; otherwise, with non-nil argument ALL,
5673 archive all done items in this category; otherwise, archive the
5676 If the archive of this file does not exist, it is created. If
5677 this category does not exist in the archive, it is created."
5679 (when (eq major-mode 'todos-mode)
5680 (if (and all (zerop (todos-get-count 'done)))
5681 (message "No done items in this category")
5683 (let* ((cat (todos-current-category))
5684 (tbuf (current-buffer))
5685 (marked (assoc cat todos-categories-with-marks))
5686 (afile (concat (file-name-sans-extension
5687 todos-current-todos-file) ".toda"))
5688 (archive (if (file-exists-p afile)
5689 (find-file-noselect afile t)
5690 (get-buffer-create afile)))
5691 (item (and (todos-done-item-p) (concat (todos-item-string) "\n")))
5693 (opoint (unless (todos-done-item-p) (point)))
5694 marked-items beg end all-done
5699 (goto-char (point-min))
5701 (when (todos-marked-item-p)
5702 (if (not (todos-done-item-p))
5703 (throw 'end (message "Only done items can be archived"))
5705 (concat marked-items (todos-item-string) "\n"))
5706 (setq count (1+ count))))
5707 (todos-forward-item))))
5709 (if (y-or-n-p "Archive all done items in this category? ")
5712 (goto-char (point-min))
5715 (re-search-forward todos-done-string-start nil t)
5716 (match-beginning 0))
5717 end (if (re-search-forward
5718 (concat "^" (regexp-quote todos-category-beg))
5722 all-done (buffer-substring-no-properties beg end)
5723 count (todos-get-count 'done))
5724 ;; Restore starting point, unless it was on a done
5725 ;; item, since they will all be deleted.
5726 (when opoint (goto-char opoint))))
5728 (if (not (or marked all item))
5729 (throw 'end (message "Only done items can be archived"))
5730 (with-current-buffer archive
5731 (unless buffer-file-name (erase-buffer))
5732 (let (buffer-read-only)
5734 (goto-char (point-min))
5735 (if (and (re-search-forward
5736 (concat "^" (regexp-quote
5737 (concat todos-category-beg cat)) "$")
5739 (re-search-forward (regexp-quote todos-category-done)
5741 ;; Start of done items section in existing category.
5743 (todos-add-category nil cat)
5744 ;; Start of done items section in new category.
5745 (goto-char (point-max)))
5746 (insert (cond (marked marked-items)
5749 (todos-update-count 'done (if (or marked all) count 1) cat)
5750 (todos-update-categories-sexp)
5751 ;; If archive is new, save to file now (using write-region in
5752 ;; order not to get prompted for file to save to), to let
5753 ;; auto-mode-alist take effect below.
5754 (unless buffer-file-name
5755 (write-region nil nil afile)
5757 (with-current-buffer tbuf
5759 ;; If we're archiving all done items, can't
5760 ;; first archive item point was on, since
5761 ;; that will short-circuit the rest.
5762 (and item (not all)))
5763 (and marked (goto-char (point-min)))
5766 (if (or (and marked (todos-marked-item-p)) item)
5769 (todos-update-count 'done -1)
5770 (todos-update-count 'archived 1)
5771 ;; Don't leave point below last item.
5772 (and item (bolp) (eolp) (< (point-min) (point-max))
5773 (todos-backward-item))
5775 (throw 'done (setq item nil))))
5776 (todos-forward-item)))))
5780 ;; Make sure done items are accessible.
5782 (remove-overlays beg end)
5783 (delete-region beg end)
5784 (todos-update-count 'done (- count))
5785 (todos-update-count 'archived count)))))
5787 (setq todos-categories-with-marks
5788 (assq-delete-all cat todos-categories-with-marks)))
5789 (todos-update-categories-sexp)
5790 (todos-prefix-overlays)))
5792 (todos-category-number cat)
5793 (todos-category-select)
5794 (split-window-below)
5795 (set-window-buffer (selected-window) tbuf)
5796 ;; Make todo file current to select category.
5797 (find-file (buffer-file-name tbuf))
5798 ;; Make sure done item separator is hidden (if done items
5799 ;; were initially visible).
5800 (let (todos-show-with-done) (todos-category-select)))))))
5802 (defun todos-archive-category-done-items ()
5803 "Move all done items in this category to its archive."
5805 (todos-archive-done-item t))
5807 (defun todos-unarchive-items (&optional all)
5808 "Unarchive at least one item in this archive category.
5810 If there are marked items, unarchive all of these; otherwise,
5811 with non-nil argument ALL, unarchive all items in this category;
5812 otherwise, unarchive the item at point.
5814 Unarchived items are restored as done items to the corresponding
5815 category in the Todos file, inserted at the end of done section.
5816 If all items in the archive category were restored, the category
5817 is deleted from the archive. If this was the only category in the
5818 archive, the archive file is deleted."
5820 (when (eq major-mode 'todos-archive-mode)
5822 (let* ((cat (todos-current-category))
5823 (tbuf (find-file-noselect
5824 (concat (file-name-sans-extension todos-current-todos-file)
5826 (marked (assoc cat todos-categories-with-marks))
5827 (item (concat (todos-item-string) "\n"))
5828 (all-items (when all (buffer-substring-no-properties
5829 (point-min) (point-max))))
5830 (all-count (when all (todos-get-count 'done)))
5831 marked-items marked-count
5835 (goto-char (point-min))
5837 (when (todos-marked-item-p)
5838 (concat marked-items (todos-item-string) "\n")
5839 (setq marked-count (1+ marked-count)))
5840 (todos-forward-item))))
5841 ;; Restore items to end of category's done section and update counts.
5842 (with-current-buffer tbuf
5843 (let (buffer-read-only)
5845 (goto-char (point-min))
5846 (re-search-forward (concat "^" (regexp-quote
5847 (concat todos-category-beg cat)) "$")
5849 ;; Go to end of category's done section.
5850 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
5852 (goto-char (match-beginning 0))
5853 (goto-char (point-max)))
5855 (insert marked-items)
5856 (todos-update-count 'done marked-count cat)
5857 (todos-update-count 'archived (- marked-count) cat))
5860 (todos-update-count 'done all-count cat)
5861 (todos-update-count 'archived (- all-count) cat))
5864 (todos-update-count 'done 1 cat)
5865 (todos-update-count 'archived -1 cat)))
5866 (todos-update-categories-sexp)))
5867 ;; Delete restored items from archive.
5868 (cond ((or marked item)
5869 (and marked (goto-char (point-min)))
5872 (if (or (and marked (todos-marked-item-p)) item)
5875 ;; Don't leave point below last item.
5876 (and item (bolp) (eolp) (< (point-min) (point-max))
5877 (todos-backward-item))
5879 (throw 'done (setq item nil))))
5880 (todos-forward-item))))
5881 (todos-update-count 'done (if marked (- marked-count) -1) cat))
5883 (remove-overlays (point-min) (point-max))
5884 (delete-region (point-min) (point-max))))
5885 ;; If that was the last category in the archive, delete the whole file.
5886 (if (= (length todos-categories) 1)
5888 (delete-file todos-current-todos-file)
5889 ;; Don't bother confirming killing the archive buffer.
5890 (set-buffer-modified-p nil)
5892 ;; Otherwise, if the archive category is now empty, delete it.
5893 (when (eq (point-min) (point-max))
5895 (let ((beg (re-search-backward
5896 (concat "^" (regexp-quote todos-category-beg) cat "$")
5898 (end (if (re-search-forward
5899 (concat "^" (regexp-quote todos-category-beg))
5903 (remove-overlays beg end)
5904 (delete-region beg end)
5905 (setq todos-categories (delete (assoc cat todos-categories)
5907 (todos-update-categories-sexp))))
5908 ;; Visit category in Todos file and show restored done items.
5909 (let ((tfile (buffer-file-name tbuf))
5910 (todos-show-with-done t))
5911 (set-window-buffer (selected-window)
5912 (set-buffer (find-file-noselect tfile)))
5913 (todos-category-number cat)
5915 (message "Items unarchived."))))))
5917 (defun todos-unarchive-category ()
5918 "Unarchive all items in this category. See `todos-unarchive-items'."
5920 (todos-unarchive-items t))
5924 ;;; todos.el ends here
5926 ;; FIXME: remove when part of Emacs
5927 ;; ---------------------------------------------------------------------------
5928 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
5929 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
5930 (add-to-list 'auto-mode-alist '("\\.todt\\'" . todos-filtered-items-mode))
5932 ;;; Addition to calendar.el
5933 ;; FIXME: autoload when key-binding is defined in calendar.el
5934 (defun todos-insert-item-from-calendar (&optional arg)
5937 (setq todos-date-from-calendar
5938 (calendar-date-string (calendar-cursor-to-date t) t t))
5941 (todos-insert-item arg nil nil todos-date-from-calendar))
5943 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar)
5945 ;;; necessitated adaptations to diary-lib.el
5947 ;; (defun diary-goto-entry (button)
5948 ;; "Jump to the diary entry for the BUTTON at point."
5949 ;; (let* ((locator (button-get button 'locator))
5950 ;; (marker (car locator))
5951 ;; markbuf file opoint)
5952 ;; ;; If marker pointing to diary location is valid, use that.
5953 ;; (if (and marker (setq markbuf (marker-buffer marker)))
5955 ;; (pop-to-buffer markbuf)
5956 ;; (goto-char (marker-position marker)))
5957 ;; ;; Marker is invalid (eg buffer has been killed, as is the case with
5958 ;; ;; included diary files).
5959 ;; (or (and (setq file (cadr locator))
5960 ;; (file-exists-p file)
5961 ;; (find-file-other-window file)
5963 ;; (when (eq major-mode (default-value 'major-mode)) (diary-mode))
5964 ;; (when (eq major-mode 'todos-mode) (widen))
5965 ;; (goto-char (point-min))
5966 ;; (when (re-search-forward (format "%s.*\\(%s\\)"
5967 ;; (regexp-quote (nth 2 locator))
5968 ;; (regexp-quote (nth 3 locator)))
5970 ;; (goto-char (match-beginning 1))
5971 ;; (when (eq major-mode 'todos-mode)
5972 ;; (setq opoint (point))
5973 ;; (re-search-backward (concat "^"
5974 ;; (regexp-quote todos-category-beg)
5977 ;; (todos-category-number (match-string 1))
5978 ;; (todos-category-select)
5979 ;; (goto-char opoint)))))
5980 ;; (message "Unable to locate this diary entry")))))