1 ;;; todo-mode.el --- facilities for making and maintaining todo lists -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997, 1999, 2001-2018 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>
8 ;; Keywords: calendar, todo
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 ;; This package provides facilities for making and maintaining
28 ;; prioritized lists of things to do. These todo lists are identified
29 ;; with named categories, so you can group together thematically
30 ;; related todo items. Each category is stored in a file, providing a
31 ;; further level of organization. You can create as many todo files,
32 ;; and in each as many categories, as you want.
34 ;; With Todo mode you can navigate among the items of a category, and
35 ;; between categories in the same and in different todo files. You
36 ;; can add and edit todo items, reprioritize them, move them to
37 ;; another category, or delete them. You can also mark items as done
38 ;; and store them within their category or in separate archive files.
39 ;; You can include todo items in the Emacs Fancy Diary display and
40 ;; treat them as appointments. You can add new todo files, and rename
41 ;; or delete them. You can add new categories to a file, rename or
42 ;; delete them, move a category to another file and merge the items of
43 ;; two categories. You can also reorder the sequence of categories in
44 ;; a todo file for the purpose of navigation. You can display
45 ;; sortable summary tables of the categories in a file and the types
46 ;; of items they contain. And you can filter items by various
47 ;; criteria from multiple categories in one or more todo files to
48 ;; create prioritizable cross-category overviews of your todo items.
50 ;; To get started, type `M-x todo-show'. For full details of the user
51 ;; interface, commands and options, consult the Todo mode user manual,
52 ;; which is included in the Info documentation.
57 (require 'cl-lib
) ; For cl-oddp and cl-assert.
59 ;; -----------------------------------------------------------------------------
60 ;;; Setting up todo files, categories, and items
61 ;; -----------------------------------------------------------------------------
63 (defcustom todo-directory
(locate-user-emacs-file "todo/")
64 "Directory where user's todo files are saved."
68 (defun todo-files (&optional archives
)
69 "Default value of `todo-files-function'.
70 This returns the case-insensitive alphabetically sorted list of
71 file truenames in `todo-directory' with the extension
72 \".todo\". With non-nil ARCHIVES return the list of archive file
73 truenames (those with the extension \".toda\")."
74 (let ((files (if (file-exists-p todo-directory
)
75 (mapcar #'file-truename
76 (directory-files todo-directory t
77 (if archives
"\\.toda\\'" "\\.todo\\'") t
)))))
78 (sort files
(lambda (s1 s2
) (let ((cis1 (upcase s1
))
80 (string< cis1 cis2
))))))
82 (defcustom todo-files-function
#'todo-files
83 "Function returning the value of the variable `todo-files'.
84 This function should take an optional argument that, if non-nil,
85 makes it return the value of the variable `todo-archives'."
89 (defvar todo-files
(funcall todo-files-function
)
90 "List of truenames of user's todo files.")
92 (defvar todo-archives
(funcall todo-files-function t
)
93 "List of truenames of user's todo archives.")
95 (defvar todo-visited nil
96 "List of todo files visited in this session by `todo-show'.
97 Used to determine initial display according to the value of
100 (defvar todo-file-buffers nil
101 "List of file names of live Todo mode buffers.")
103 (defvar todo-global-current-todo-file nil
104 "Variable holding name of current todo file.
105 Used by functions called from outside of Todo mode to visit the
106 current todo file rather than the default todo file (i.e. when
107 users option `todo-show-current-file' is non-nil).")
109 (defvar todo-current-todo-file nil
110 "Variable holding the name of the currently active todo file.")
112 (defvar todo-categories nil
113 "Alist of categories in the current todo file.
114 The elements are cons cells whose car is a category name and
115 whose cdr is a vector of the category's item counts. These are,
116 in order, the numbers of todo items, of todo items included in
117 the Diary, of done items and of archived items.")
119 (defvar todo-category-number
1
120 "Variable holding the number of the current todo category.
121 Todo categories are numbered starting from 1.")
123 (defvar todo-categories-with-marks nil
124 "Alist of categories and number of marked items they contain.")
126 (defconst todo-category-beg
"--==-- "
127 "String marking beginning of category (inserted with its name).")
129 (defconst todo-category-done
"==--== DONE "
130 "String marking beginning of category's done items.")
132 (defcustom todo-done-separator-string
"="
133 "String determining the value of variable `todo-done-separator'.
134 If the string consists of a single character,
135 `todo-done-separator' will be the string made by repeating this
136 character for the width of the window, and the length is
137 automatically recalculated when the window width changes. If the
138 string consists of more (or less) than one character, it will be
139 the value of `todo-done-separator'."
141 :initialize
'custom-initialize-default
142 :set
'todo-reset-done-separator-string
143 :group
'todo-display
)
145 (defun todo-done-separator ()
146 "Return string used as value of variable `todo-done-separator'."
147 (let ((sep todo-done-separator-string
))
148 (propertize (if (= 1 (length sep
))
149 (make-string (window-width) (string-to-char sep
))
150 todo-done-separator-string
)
151 'face
'todo-done-sep
)))
153 (defvar todo-done-separator
(todo-done-separator)
154 "String used to visually separate done from not done items.
155 Displayed as an overlay instead of `todo-category-done' when
156 done items are shown. Its value is determined by user option
157 `todo-done-separator-string'.")
159 (defvar todo-show-done-only nil
160 "If non-nil display only done items in current category.
161 Set by the command `todo-toggle-view-done-only' and used by
162 `todo-category-select'.")
164 (defcustom todo-nondiary-marker
'("[" "]")
165 "List of strings surrounding item date to block diary inclusion.
166 The first string is inserted before the item date and must be a
167 non-empty string that does not match a diary date in order to
168 have its intended effect. The second string is inserted after
170 :type
'(list string string
)
172 :initialize
'custom-initialize-default
173 :set
'todo-reset-nondiary-marker
)
175 (defconst todo-nondiary-start
(nth 0 todo-nondiary-marker
)
176 "String inserted before item date to block diary inclusion.")
178 (defconst todo-nondiary-end
(nth 1 todo-nondiary-marker
)
179 "String inserted after item date matching `todo-nondiary-start'.")
181 (defconst todo-month-name-array
182 (vconcat calendar-month-name-array
(vector "*"))
183 "Array of month names, in order.
184 The final element is \"*\", indicating an unspecified month.")
186 (defconst todo-month-abbrev-array
187 (vconcat calendar-month-abbrev-array
(vector "*"))
188 "Array of abbreviated month names, in order.
189 The final element is \"*\", indicating an unspecified month.")
191 (defconst todo-date-pattern
192 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
193 (concat "\\(?4:\\(?5:" dayname
"\\)\\|"
196 (monthname (format "\\(?6:%s\\)" (diary-name-pattern
197 todo-month-name-array
198 todo-month-abbrev-array
)))
199 (month "\\(?7:[0-9]+\\|\\*\\)")
200 (day "\\(?8:[0-9]+\\|\\*\\)")
201 (year "-?\\(?9:[0-9]+\\|\\*\\)"))
202 (mapconcat #'eval calendar-date-display-form
""))
204 "Regular expression matching a todo item date header.")
206 ;; By itself this matches anything, because of the `?'; however, it's only
207 ;; used in the context of `todo-date-pattern' (but Emacs Lisp lacks
209 (defconst todo-date-string-start
210 (concat "^\\(" (regexp-quote todo-nondiary-start
) "\\|"
211 (regexp-quote diary-nonmarking-symbol
) "\\)?")
212 "Regular expression matching part of item header before the date.")
214 (defcustom todo-done-string
"DONE "
215 "Identifying string appended to the front of done todo items."
217 :initialize
'custom-initialize-default
218 :set
'todo-reset-done-string
221 (defconst todo-done-string-start
222 (concat "^\\[" (regexp-quote todo-done-string
))
223 "Regular expression matching start of done item.")
225 (defconst todo-item-start
(concat "\\(" todo-date-string-start
"\\|"
226 todo-done-string-start
"\\)"
228 "String identifying start of a todo item.")
230 ;; -----------------------------------------------------------------------------
231 ;;; Todo mode display options
232 ;; -----------------------------------------------------------------------------
234 (defcustom todo-prefix
""
235 "String prefixed to todo items for visual distinction."
236 :type
'(string :validate
238 (when (string= (widget-value widget
) todo-item-mark
)
242 "Invalid value: must be distinct from `todo-item-mark'"))
244 :initialize
'custom-initialize-default
245 :set
'todo-reset-prefix
246 :group
'todo-display
)
248 (defcustom todo-number-prefix t
249 "Non-nil to prefix items with consecutively increasing integers.
250 These reflect the priorities of the items in each category."
252 :initialize
'custom-initialize-default
253 :set
'todo-reset-prefix
254 :group
'todo-display
)
256 (defun todo-mode-line-control (cat)
257 "Return a mode line control for todo or archive file buffers.
258 Argument CAT is the name of the current todo category.
259 This function is the value of the user variable
260 `todo-mode-line-function'."
261 (let ((file (todo-short-file-name todo-current-todo-file
)))
262 (format "%s category %d: %s" file todo-category-number cat
)))
264 (defcustom todo-mode-line-function
#'todo-mode-line-control
265 "Function that returns a mode line control for Todo mode buffers.
266 The function expects one argument holding the name of the current
267 todo category. The resulting control becomes the local value of
268 `mode-line-buffer-identification' in each Todo mode buffer."
270 :group
'todo-display
)
272 (defcustom todo-highlight-item nil
273 "Non-nil means highlight items at point."
275 :initialize
'custom-initialize-default
276 :set
'todo-reset-highlight-item
277 :group
'todo-display
)
279 (defcustom todo-wrap-lines t
280 "Non-nil to activate Visual Line mode and use wrap prefix."
282 :group
'todo-display
)
284 (defcustom todo-indent-to-here
3
285 "Number of spaces to indent continuation lines of items.
286 This must be a positive number to ensure such items are fully
287 shown in the Fancy Diary display."
288 :type
'(integer :validate
290 (unless (> (widget-value widget
) 0)
291 (widget-put widget
:error
292 "Invalid value: must be a positive integer")
294 :group
'todo-display
)
296 (defun todo-indent ()
297 "Indent from point to `todo-indent-to-here'."
298 (indent-to todo-indent-to-here todo-indent-to-here
))
300 (defcustom todo-show-with-done nil
301 "Non-nil to display done items in all categories."
303 :group
'todo-display
)
305 ;; -----------------------------------------------------------------------------
307 ;; -----------------------------------------------------------------------------
309 (defface todo-key-prompt
310 '((t (:weight bold
)))
311 "Face for making keys in item insertion prompt stand out."
315 ;; '((t :inherit font-lock-warning-face))
319 (:weight bold
:foreground
"Red1"))
323 (:weight bold
:foreground
"Pink"))
327 (:weight bold
:foreground
"Red1"))
331 (:weight bold
:foreground
"Pink"))
336 (:weight bold
:inverse-video t
)))
337 "Face for marks on marked items."
340 (defface todo-prefix-string
341 ;; '((t :inherit font-lock-constant-face))
342 '((((class grayscale
) (background light
))
343 (:foreground
"LightGray" :weight bold
:underline t
))
344 (((class grayscale
) (background dark
))
345 (:foreground
"Gray50" :weight bold
:underline t
))
346 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
347 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
348 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
349 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
350 (((class color
) (min-colors 8)) (:foreground
"magenta"))
351 (t (:weight bold
:underline t
)))
352 "Face for todo item prefix or numerical priority string."
355 (defface todo-top-priority
356 ;; bold font-lock-comment-face
357 '((default :weight bold
)
358 (((class grayscale
) (background light
)) :foreground
"DimGray" :slant italic
)
359 (((class grayscale
) (background dark
)) :foreground
"LightGray" :slant italic
)
360 (((class color
) (min-colors 88) (background light
)) :foreground
"Firebrick")
361 (((class color
) (min-colors 88) (background dark
)) :foreground
"chocolate1")
362 (((class color
) (min-colors 16) (background light
)) :foreground
"red")
363 (((class color
) (min-colors 16) (background dark
)) :foreground
"red1")
364 (((class color
) (min-colors 8) (background light
)) :foreground
"red")
365 (((class color
) (min-colors 8) (background dark
)) :foreground
"yellow")
367 "Face for top priority todo item numerical priority string.
368 The item's priority number string has this face if the number is
369 less than or equal the category's top priority setting."
372 (defface todo-nondiary
373 ;; '((t :inherit font-lock-type-face))
374 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
375 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
376 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
377 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
378 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
379 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
380 (((class color
) (min-colors 8)) :foreground
"green")
381 (t :weight bold
:underline t
))
382 "Face for non-diary markers around todo item date/time header."
386 '((t :inherit diary
))
387 "Face for the date string of a todo item."
391 '((t :inherit diary-time
))
392 "Face for the time string of a todo item."
395 (defface todo-diary-expired
396 ;; Doesn't contrast enough with todo-date (= diary) face.
397 ;; ;; '((t :inherit warning))
398 ;; '((default :weight bold)
399 ;; (((class color) (min-colors 16)) :foreground "DarkOrange")
400 ;; (((class color)) :foreground "yellow"))
401 ;; bold font-lock-function-name-face
402 '((default :weight bold
)
403 (((class color
) (min-colors 88) (background light
)) :foreground
"Blue1")
404 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSkyBlue")
405 (((class color
) (min-colors 16) (background light
)) :foreground
"Blue")
406 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSkyBlue")
407 (((class color
) (min-colors 8)) :foreground
"blue")
408 (t :inverse-video t
))
409 "Face for expired dates of diary items."
412 (defface todo-done-sep
413 ;; '((t :inherit font-lock-builtin-face))
414 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
415 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
416 (((class color
) (min-colors 88) (background light
)) :foreground
"dark slate blue")
417 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSteelBlue")
418 (((class color
) (min-colors 16) (background light
)) :foreground
"Orchid")
419 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSteelBlue")
420 (((class color
) (min-colors 8)) :foreground
"blue" :weight bold
)
422 "Face for separator string between done and not done todo items."
426 ;; '((t :inherit font-lock-keyword-face))
427 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
428 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
429 (((class color
) (min-colors 88) (background light
)) :foreground
"Purple")
430 (((class color
) (min-colors 88) (background dark
)) :foreground
"Cyan1")
431 (((class color
) (min-colors 16) (background light
)) :foreground
"Purple")
432 (((class color
) (min-colors 16) (background dark
)) :foreground
"Cyan")
433 (((class color
) (min-colors 8)) :foreground
"cyan" :weight bold
)
435 "Face for done todo item header string."
438 (defface todo-comment
439 ;; '((t :inherit font-lock-comment-face))
440 '((((class grayscale
) (background light
))
441 :foreground
"DimGray" :weight bold
:slant italic
)
442 (((class grayscale
) (background dark
))
443 :foreground
"LightGray" :weight bold
:slant italic
)
444 (((class color
) (min-colors 88) (background light
))
445 :foreground
"Firebrick")
446 (((class color
) (min-colors 88) (background dark
))
447 :foreground
"chocolate1")
448 (((class color
) (min-colors 16) (background light
))
450 (((class color
) (min-colors 16) (background dark
))
452 (((class color
) (min-colors 8) (background light
))
454 (((class color
) (min-colors 8) (background dark
))
455 :foreground
"yellow")
456 (t :weight bold
:slant italic
))
457 "Face for comments appended to done todo items."
461 ;; '((t :inherit match))
465 (:background
"yellow1"))
469 (:background
"RoyalBlue3"))
473 (:foreground
"black" :background
"yellow"))
477 (:foreground
"white" :background
"blue"))
482 (:background
"gray")))
483 "Face for matches found by `todo-search'."
487 ;; '((t :inherit widget-field))
489 (:foreground
"black" :background
"yellow3"))
490 (((class grayscale color
)
492 (:background
"gray85"))
493 (((class grayscale color
)
495 (:background
"dim gray"))
498 "Face for buttons in table of categories."
501 (defface todo-sorted-column
506 (:background
"grey85"))
509 (:background
"grey85" :foreground
"grey10"))
511 (:background
"gray")))
512 "Face for sorted column in table of categories."
515 (defface todo-archived-only
516 ;; '((t (:inherit (shadow))))
519 (:foreground
"grey50"))
522 (:foreground
"grey70"))
524 (:foreground
"gray")))
525 "Face for archived-only category names in table of categories."
528 (defface todo-category-string
529 ;; '((t :inherit font-lock-type-face))
530 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
531 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
532 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
533 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
534 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
535 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
536 (((class color
) (min-colors 8)) :foreground
"green")
537 (t :weight bold
:underline t
))
538 "Face for category-file header in Todo Filtered Items mode."
541 ;; -----------------------------------------------------------------------------
542 ;;; Entering and exiting
543 ;; -----------------------------------------------------------------------------
545 ;; (defcustom todo-visit-files-commands (list 'find-file 'dired-find-file)
546 ;; "List of file finding commands for `todo-display-as-todo-file'.
547 ;; Invoking these commands to visit a todo file or todo archive file
548 ;; calls `todo-show' or `todo-find-archive', so that the file is
549 ;; displayed correctly."
550 ;; :type '(repeat function)
553 (defun todo-short-file-name (file)
554 "Return the short form of todo file FILE's name.
555 This lacks the extension and directory components."
557 (file-name-sans-extension (file-name-nondirectory file
))))
559 (defun todo--files-type-list ()
560 (mapcar (lambda (f) (list 'const
(todo-short-file-name f
)))
561 (funcall todo-files-function
)))
563 (defcustom todo-default-todo-file
(todo-short-file-name
564 (car (funcall todo-files-function
)))
565 "Todo file visited by first session invocation of `todo-show'."
566 :type
(when todo-files
567 `(radio ,@(todo--files-type-list)))
570 (defcustom todo-show-current-file t
571 "Non-nil to make `todo-show' visit the current todo file.
572 Otherwise, `todo-show' always visits `todo-default-todo-file'."
574 :initialize
'custom-initialize-default
575 :set
'todo-set-show-current-file
578 (defcustom todo-show-first
'first
579 "What action to take on first use of `todo-show' on a file."
580 :type
'(choice (const :tag
"Show first category" first
)
581 (const :tag
"Show table of categories" table
)
582 (const :tag
"Show top priorities" top
)
583 (const :tag
"Show diary items" diary
)
584 (const :tag
"Show regexp items" regexp
))
587 (defcustom todo-add-item-if-new-category t
588 "Non-nil to prompt for an item after adding a new category."
592 (defcustom todo-initial-file
"Todo"
593 "Default file name offered on adding first todo file."
597 (defcustom todo-initial-category
"Todo"
598 "Default category name offered on initializing a new todo file."
602 (defcustom todo-category-completions-files nil
603 "List of files for building `todo-read-category' completions."
604 :type
`(set ,@(todo--files-type-list))
607 (defcustom todo-completion-ignore-case nil
608 "Non-nil means case is ignored by `todo-read-*' functions."
613 (defun todo-show (&optional solicit-file interactive
)
614 "Visit a todo file and display one of its categories.
616 When invoked in Todo mode, Todo Archive mode or Todo Filtered
617 Items mode, or when invoked anywhere else with a prefix argument,
618 prompt for which todo file to visit. When invoked outside of a
619 Todo mode buffer without a prefix argument, visit
620 `todo-default-todo-file'. Subsequent invocations from outside of
621 Todo mode revisit this file or, with option
622 `todo-show-current-file' non-nil (the default), whichever todo
623 file was last visited.
625 If you call this command before you have created any todo file in
626 the current format, and you have a todo file in old format, it
627 will ask you whether to convert that file and show it.
628 Otherwise, calling this command before any todo file exists
629 prompts for a file name and an initial category (defaulting to
630 `todo-initial-file' and `todo-initial-category'), creates both of
631 these, visits the file and displays the category, and if option
632 `todo-add-item-if-new-category' is non-nil (the default), prompts
635 The first invocation of this command on an existing todo file
636 interacts with the option `todo-show-first': if its value is
637 `first' (the default), show the first category in the file; if
638 its value is `table', show the table of categories in the file;
639 if its value is one of `top', `diary' or `regexp', show the
640 corresponding saved top priorities, diary items, or regexp items
641 file, if any. Subsequent invocations always show the file's
642 current (i.e., last displayed) category.
644 In Todo mode just the category's unfinished todo items are shown
645 by default. The done items are hidden, but typing
646 `\\[todo-toggle-view-done-items]' displays them below the todo
647 items. With non-nil user option `todo-show-with-done' both todo
648 and done items are always shown on visiting a category."
650 (when todo-default-todo-file
651 (todo-check-file (todo-absolute-file-name todo-default-todo-file
)))
653 ;; Before initializing the first todo first, check if there is a
654 ;; legacy todo file and if so, offer to convert to the current
655 ;; format and make it the first new todo file.
656 (unless todo-default-todo-file
657 (let ((legacy-todo-file (if (boundp 'todo-file-do
)
659 (locate-user-emacs-file "todo-do" ".todo-do"))))
660 (when (and (file-exists-p legacy-todo-file
)
661 (y-or-n-p (concat "Do you want to convert a copy of your "
662 "old todo file to the new format? ")))
663 (when (todo-convert-legacy-files)
664 (throw 'shown nil
)))))
667 (show-first todo-show-first
)
668 (file (cond ((or solicit-file
670 (memq major-mode
'(todo-mode
672 todo-filtered-items-mode
))))
673 (if (funcall todo-files-function
)
674 (todo-read-file-name "Choose a todo file to visit: "
676 (user-error "There are no todo files")))
677 ((and (eq major-mode
'todo-archive-mode
)
678 ;; Called noninteractively via todo-quit
679 ;; to jump to corresponding category in
682 (setq cat
(todo-current-category))
683 (concat (file-name-sans-extension
684 todo-current-todo-file
) ".todo"))
686 (or todo-current-todo-file
687 (and todo-show-current-file
688 todo-global-current-todo-file
)
689 (todo-absolute-file-name todo-default-todo-file
)
692 (unless todo-default-todo-file
693 ;; We just initialized the first todo file, so make it the default.
694 (setq todo-default-todo-file
(todo-short-file-name file
)
696 (put 'todo-default-todo-file
'custom-type
697 `(radio ,@(todo--files-type-list))))
698 (unless (member file todo-visited
)
699 ;; Can't setq t-c-t-f here, otherwise wrong file shown when
700 ;; todo-show is called from todo-show-categories-table.
701 (let ((todo-current-todo-file file
))
702 (cond ((eq todo-show-first
'table
)
703 (todo-show-categories-table))
704 ((memq todo-show-first
'(top diary regexp
))
705 (let* ((shortf (todo-short-file-name file
))
706 (fi-file (todo-absolute-file-name
707 shortf todo-show-first
)))
708 (when (eq todo-show-first
'regexp
)
709 (let ((rxfiles (directory-files todo-directory t
711 (when (and rxfiles
(> (length rxfiles
) 1))
712 (let ((rxf (mapcar #'todo-short-file-name rxfiles
)))
713 (setq fi-file
(todo-absolute-file-name
715 "Choose a regexp items file: "
718 (if (file-exists-p fi-file
)
722 (set-buffer (find-file-noselect fi-file
'nowarn
)))
723 (unless (derived-mode-p 'todo-filtered-items-mode
)
724 (todo-filtered-items-mode)))
725 (message "There is no %s file for %s"
726 (cond ((eq todo-show-first
'top
)
728 ((eq todo-show-first
'diary
)
730 ((eq todo-show-first
'regexp
)
733 (setq todo-show-first
'first
)))))))
734 (when (or (member file todo-visited
)
735 (eq todo-show-first
'first
))
736 (unless (todo-check-file file
) (throw 'end nil
))
737 ;; If todo-show is called from the minibuffer, don't visit
738 ;; the todo file there.
739 (set-window-buffer (if (minibufferp) (minibuffer-selected-window)
741 (set-buffer (find-file-noselect file
'nowarn
)))
742 (if (equal (file-name-extension (buffer-file-name)) "toda")
743 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
744 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
745 ;; When quitting an archive file, show the corresponding
746 ;; category in the corresponding todo file, if it exists.
747 (when (assoc cat todo-categories
)
748 (setq todo-category-number
(todo-category-number cat
)))
749 ;; If this is a new todo file, add its first category.
750 (when (zerop (buffer-size))
751 ;; Don't confuse an erased buffer with a fresh buffer for
752 ;; adding a new todo file -- it might have been erased by
753 ;; mistake or due to a bug (e.g. Bug#20832).
754 (when (buffer-modified-p)
755 (error "Buffer is empty but modified, please report a bug"))
758 (setq todo-category-number
759 (todo-add-category todo-current-todo-file
"")
760 add-item todo-add-item-if-new-category
763 ;; If the category was added, save the file now, so we
764 ;; don't risk having an empty todo file, which would
765 ;; signal an error if we tried to visit it later,
766 ;; since doing that looks for category boundaries.
768 ;; If user cancels before adding the category, clean up
769 ;; and exit, so we have a fresh slate the next time.
771 ;; (setq todo-files (funcall todo-files-function))
772 (setq todo-files
(delete file todo-files
))
774 (setq todo-default-todo-file nil
775 todo-current-todo-file nil
)
776 (put 'todo-default-todo-file
'custom-type
777 `(radio ,@(todo--files-type-list))))
780 (save-excursion (todo-category-select))
781 (when add-item
(todo-insert-item--basic)))
782 (setq todo-show-first show-first
)
783 (add-to-list 'todo-visited file
)))))
786 "Save the current todo file."
788 (cond ((eq major-mode
'todo-filtered-items-mode
)
789 (todo-check-filtered-items-file)
790 (todo-save-filtered-items-buffer))
794 (defvar todo-descending-counts
)
797 "Exit the current Todo-related buffer.
798 Depending on the specific mode, this either kills the buffer or
799 buries it and restores state as needed."
801 (let ((buf (current-buffer)))
802 (cond ((eq major-mode
'todo-categories-mode
)
803 ;; Postpone killing buffer till after calling todo-show, to
804 ;; prevent killing todo-mode buffer.
805 (setq todo-descending-counts nil
)
806 ;; Ensure todo-show calls todo-show-categories-table only on
807 ;; first invocation per file.
808 (when (eq todo-show-first
'table
)
809 (add-to-list 'todo-visited todo-current-todo-file
))
812 ((eq major-mode
'todo-filtered-items-mode
)
814 (unless (eq major-mode
'todo-mode
) (todo-show)))
815 ((eq major-mode
'todo-archive-mode
)
816 ;; Have to write a newly created archive to file to avoid
817 ;; subsequent errors.
819 (let ((todo-file (concat todo-directory
820 (todo-short-file-name todo-current-todo-file
)
822 (if (todo-check-file todo-file
)
824 (message "There is no todo file for this archive")))
825 ;; When todo-check-file runs in todo-show, it kills the
826 ;; buffer if the archive file was deleted externally.
827 (when (buffer-live-p buf
) (kill-buffer buf
)))
828 ((eq major-mode
'todo-mode
)
832 ;; -----------------------------------------------------------------------------
833 ;;; Navigation between and within categories
834 ;; -----------------------------------------------------------------------------
836 (defcustom todo-skip-archived-categories nil
837 "Non-nil to handle categories with only archived items specially.
839 Sequential category navigation using \\[todo-forward-category]
840 or \\[todo-backward-category] skips categories that contain only
841 archived items. Other commands still recognize these categories.
842 In Todo Categories mode (\\[todo-show-categories-table]) these
843 categories shown in `todo-archived-only' face and pressing the
844 category button visits the category in the archive instead of the
847 :group
'todo-display
)
849 (defun todo-forward-category (&optional back
)
850 "Visit the numerically next category in this todo file.
851 If the current category is the highest numbered, visit the first
852 category. With non-nil argument BACK, visit the numerically
853 previous category (the highest numbered one, if the current
854 category is the first)."
856 (let ((setcatnum (lambda () (1+ (mod (- todo-category-number
858 (length todo-categories
))))))
859 (setq todo-category-number
(funcall setcatnum
))
860 (when todo-skip-archived-categories
861 (while (and (zerop (todo-get-count 'todo
))
862 (zerop (todo-get-count 'done
))
863 (not (zerop (todo-get-count 'archived
))))
864 (setq todo-category-number
(funcall setcatnum
))))
865 (todo-category-select)
866 (if transient-mark-mode
(deactivate-mark))
867 (goto-char (point-min))))
869 (defun todo-backward-category ()
870 "Visit the numerically previous category in this todo file.
871 If the current category is the highest numbered, visit the first
874 (todo-forward-category t
))
876 (defvar todo-categories-buffer
)
878 (defun todo-jump-to-category (&optional file where
)
879 "Prompt for a category in a todo file and jump to it.
881 With non-nil FILE (interactively a prefix argument), prompt for a
882 specific todo file and choose (with TAB completion) a category
883 in it to jump to; otherwise, choose and jump to any category in
884 either the current todo file or a file in
885 `todo-category-completions-files'.
887 Also accept a non-existing category name and ask whether to add a
888 new category by that name; on confirmation, add it and jump to
889 that category, and if option `todo-add-item-if-new-category' is
890 non-nil (the default), then prompt for the first item.
892 In noninteractive calls non-nil WHERE specifies either the goal
893 category or its file. If its value is `archive', the choice of
894 categories is restricted to the current archive file or the
895 archive you were prompted to choose; this is used by
896 `todo-jump-to-archive-category'. If its value is the name of a
897 category, jump directly to that category; this is used in Todo
900 ;; If invoked outside of Todo mode and there is not yet any Todo
901 ;; file, initialize one.
902 (if (null (funcall todo-files-function
))
904 (let* ((archive (eq where
'archive
))
905 (cat (unless archive where
))
906 (goto-archive (and cat
907 todo-skip-archived-categories
908 (zerop (todo-get-count 'todo cat
))
909 (zerop (todo-get-count 'done cat
))
910 (not (zerop (todo-get-count 'archived cat
)))))
911 (file0 (when cat
; We're in Todo Categories mode.
913 ;; If the category has only archived items and
914 ;; `todo-skip-archived-categories' is non-nil, jump to
915 ;; the archive category.
916 (concat (file-name-sans-extension
917 todo-current-todo-file
) ".toda")
918 ;; Otherwise, jump to the category in the todo file.
919 todo-current-todo-file
)))
920 (len (length todo-categories
))
921 (cat+file
(unless cat
922 (todo-read-category "Jump to category: "
923 (if archive
'archive
) file
)))
924 (add-item (and todo-add-item-if-new-category
925 (> (length todo-categories
) len
)))
926 (category (or cat
(car cat
+file
))))
927 (unless cat
(setq file0
(cdr cat
+file
)))
928 (with-current-buffer (find-file-noselect file0
'nowarn
)
929 (when goto-archive
(todo-archive-mode))
930 (set-window-buffer (selected-window)
931 (set-buffer (find-buffer-visiting file0
)))
932 (if transient-mark-mode
(deactivate-mark))
933 (unless todo-global-current-todo-file
934 (setq todo-global-current-todo-file todo-current-todo-file
))
935 (todo-category-number category
)
936 (todo-category-select)
937 (goto-char (point-min))
938 (if (bound-and-true-p hl-line-mode
) (hl-line-highlight))
939 (when add-item
(todo-insert-item--basic))))))
941 (defun todo-next-item (&optional count
)
942 "Move point down to the beginning of the next item.
943 With positive numerical prefix COUNT, move point COUNT items
946 If the category's done items are hidden, this command also moves
947 point to the empty line below the last todo item from any higher
948 item in the category, i.e., when invoked with or without a prefix
949 argument. If the category's done items are visible, this command
950 called with a prefix argument only moves point to a lower item,
951 e.g., with point on the last todo item and called with prefix 1,
952 it moves point to the first done item; but if called with point
953 on the last todo item without a prefix argument, it moves point
954 to the empty line above the done items separator."
956 ;; It's not worth the trouble to allow prefix arg value < 1, since
957 ;; we have the corresponding command.
958 (cond ((and current-prefix-arg
(< count
1))
959 (user-error "The prefix argument must be a positive number"))
961 (todo-forward-item count
))
963 (todo-forward-item))))
965 (defun todo-previous-item (&optional count
)
966 "Move point up to start of item with next higher priority.
967 With positive numerical prefix COUNT, move point COUNT items
970 If the category's done items are visible, this command called
971 with a prefix argument only moves point to a higher item, e.g.,
972 with point on the first done item and called with prefix 1, it
973 moves to the last todo item; but if called with point on the
974 first done item without a prefix argument, it moves point to the
975 empty line above the done items separator."
977 ;; Avoid moving to bob if on the first item but not at bob.
978 (when (> (line-number-at-pos) 1)
979 ;; It's not worth the trouble to allow prefix arg value < 1, since
980 ;; we have the corresponding command.
981 (cond ((and current-prefix-arg
(< count
1))
982 (user-error "The prefix argument must be a positive number"))
984 (todo-backward-item count
))
986 (todo-backward-item)))))
988 ;; -----------------------------------------------------------------------------
989 ;;; Display toggle commands
990 ;; -----------------------------------------------------------------------------
992 (defun todo-toggle-prefix-numbers ()
993 "Hide item numbering if shown, show if hidden."
997 (goto-char (point-min))
998 (let* ((ov (todo-get-overlay 'prefix
))
999 (show-done (re-search-forward todo-done-string-start nil t
))
1000 (todo-show-with-done show-done
)
1001 (todo-number-prefix (not (equal (overlay-get ov
'before-string
)
1003 (if (eq major-mode
'todo-filtered-items-mode
)
1004 (todo-prefix-overlays)
1005 (todo-category-select))))))
1007 (defun todo-toggle-view-done-items ()
1008 "Show hidden or hide visible done items in current category."
1010 (if (zerop (todo-get-count 'done
(todo-current-category)))
1011 (message "There are no done items in this category.")
1012 (let ((opoint (point)))
1013 (goto-char (point-min))
1014 (let* ((shown (re-search-forward todo-done-string-start nil t
))
1015 (todo-show-with-done (not shown
)))
1016 (todo-category-select)
1018 ;; If start of done items sections is below the bottom of the
1019 ;; window, make it visible.
1022 (goto-char (point-min))
1023 (re-search-forward todo-done-string-start nil t
)))
1024 (if (pos-visible-in-window-p shown
)
1027 (if transient-mark-mode
(deactivate-mark))))))))
1029 (defun todo-toggle-view-done-only ()
1030 "Switch between displaying only done or only todo items."
1032 (setq todo-show-done-only
(not todo-show-done-only
))
1033 (todo-category-select)
1034 (if transient-mark-mode
(deactivate-mark)))
1036 (defun todo-toggle-item-highlighting ()
1037 "Highlight or unhighlight the todo item the cursor is on."
1039 (eval-and-compile (require 'hl-line
))
1040 (when (memq major-mode
1041 '(todo-mode todo-archive-mode todo-filtered-items-mode
))
1046 (defvar todo--item-headers-hidden nil
1047 "Non-nil if item date-time headers in current buffer are hidden.")
1049 (defun todo-toggle-item-header ()
1050 "Hide or show item date-time headers in the current file.
1051 With done items, this hides only the done date-time string, not
1052 the original date-time string."
1054 (unless (catch 'nonempty
1055 (dolist (type '(todo done
))
1056 (dolist (c todo-categories
)
1057 (let ((count (todo-get-count type
(car c
))))
1058 (unless (zerop count
)
1059 (throw 'nonempty t
))))))
1060 (user-error "This file has no items"))
1061 (if todo--item-headers-hidden
1063 (remove-overlays 1 (1+ (buffer-size)) 'todo
'header
)
1064 (setq todo--item-headers-hidden nil
))
1068 (goto-char (point-min))
1071 (when (re-search-forward
1072 (concat todo-item-start
1073 "\\( " diary-time-regexp
"\\)?"
1074 (regexp-quote todo-nondiary-end
) "? ")
1076 (setq ov
(make-overlay (match-beginning 0) (match-end 0) nil t
))
1077 (overlay-put ov
'todo
'header
)
1078 (overlay-put ov
'display
""))
1080 (setq todo--item-headers-hidden t
)))))
1082 ;; -----------------------------------------------------------------------------
1083 ;;; File and category editing
1084 ;; -----------------------------------------------------------------------------
1086 (defun todo-add-file ()
1087 "Name and initialize a new todo file.
1088 Interactively, prompt for a category and display it, and if
1089 option `todo-add-item-if-new-category' is non-nil (the default),
1090 prompt for the first item.
1091 Noninteractively, return the name of the new file."
1093 (let* ((prompt (concat "Enter name of new todo file "
1094 "(TAB or SPC to see current names): "))
1095 (file (todo-read-file-name prompt
)))
1096 ;; Don't accept the name of an existing todo file.
1097 (setq file
(todo-absolute-file-name
1098 (todo-validate-name (todo-short-file-name file
) 'file
)))
1099 (with-current-buffer (get-buffer-create file
)
1101 (write-region (point-min) (point-max) file nil
'nomessage nil t
)
1103 (setq todo-files
(funcall todo-files-function
))
1104 (todo-update-filelist-defcustoms)
1105 (if (called-interactively-p 'any
)
1107 (set-window-buffer (selected-window)
1108 (set-buffer (find-file-noselect file
)))
1109 ;; Since buffer is not yet in todo-mode, we need to
1110 ;; explicitly make todo-current-todo-file buffer local.
1111 (setq-local todo-current-todo-file file
)
1115 (defun todo-rename-file (&optional arg
)
1116 "Rename the current todo file.
1117 With prefix ARG, prompt for a todo file and rename it.
1118 If there are corresponding archive or filtered items files,
1119 rename these accordingly. If there are live buffers visiting
1120 these files, also rename them accordingly."
1122 (let* ((oname (or (and arg
1123 (todo-read-file-name "Choose a file to rename: "
1125 (buffer-file-name)))
1126 (soname (todo-short-file-name oname
))
1127 (nname (todo-read-file-name "New name for this file: "))
1128 (snname (todo-short-file-name nname
))
1129 (files (directory-files todo-directory t
1130 (concat ".*" (regexp-quote soname
)
1134 (let* ((sfname (todo-short-file-name f
))
1135 (fext (file-name-extension f t
))
1136 (fbuf (find-buffer-visiting f
))
1137 (fbname (buffer-name fbuf
)))
1138 (when (string-match (regexp-quote soname
) sfname
)
1139 (let* ((snfname (replace-match snname t t sfname
))
1140 (nfname (concat todo-directory snfname fext
)))
1141 (rename-file f nfname
)
1143 (with-current-buffer fbuf
1144 (set-visited-file-name nfname t t
)
1145 (cond ((member fext
'(".todo" ".toda"))
1146 (setq todo-current-todo-file
(buffer-file-name))
1147 (setq mode-line-buffer-identification
1148 (funcall todo-mode-line-function
1149 (todo-current-category))))
1152 (replace-regexp-in-string
1153 (regexp-quote soname
) snname fbname
))))))))))
1154 (setq todo-files
(funcall todo-files-function
)
1155 todo-archives
(funcall todo-files-function t
))
1156 (when (string= todo-default-todo-file soname
)
1157 (setq todo-default-todo-file snname
))
1158 (when (string= todo-global-current-todo-file oname
)
1159 (setq todo-global-current-todo-file nname
))
1160 (todo-update-filelist-defcustoms)))
1162 (defun todo-delete-file ()
1163 "Delete the current todo, archive or filtered items file.
1164 If the todo file has a corresponding archive file, or vice versa,
1165 prompt whether to delete that as well. Also kill the buffers
1166 visiting the deleted files."
1168 (let* ((file1 (buffer-file-name))
1169 (todo (eq major-mode
'todo-mode
))
1170 (archive (eq major-mode
'todo-archive-mode
))
1171 (filtered (eq major-mode
'todo-filtered-items-mode
))
1172 (file1-sn (todo-short-file-name file1
))
1173 (file2 (concat todo-directory file1-sn
(cond (todo ".toda")
1174 (archive ".todo"))))
1175 (buf1 (current-buffer))
1176 (buf2 (when file2
(find-buffer-visiting file2
)))
1177 (prompt1 (concat "Delete " (cond (todo "todo")
1179 (filtered "filtered items"))
1181 (prompt2 (concat "Also delete the corresponding "
1182 (cond (todo "archive") (archive "todo")) " file "
1183 (when buf2
"and kill the buffer visiting it? ")))
1184 (delete1 (yes-or-no-p (format prompt1 file1-sn
)))
1185 (delete2 (when (and delete1
(or (file-exists-p file2
) buf2
))
1186 (yes-or-no-p prompt2
))))
1188 (when (file-exists-p file1
) (delete-file file1
))
1189 (setq todo-visited
(delete file1 todo-visited
))
1193 (when (file-exists-p file2
) (delete-file file2
))
1194 (setq todo-visited
(delete file2 todo-visited
))
1195 (and buf2
(kill-buffer buf2
)))
1196 ;; If we deleted an archive but not its todo file, update the
1197 ;; latter's category sexp.
1198 (when (equal (file-name-extension file2
) "todo")
1199 (with-current-buffer (or buf2
(find-file-noselect file2
))
1203 (goto-char (point-min))
1204 (let ((sexp (read (buffer-substring-no-properties
1205 (line-beginning-position)
1206 (line-end-position))))
1207 (buffer-read-only nil
))
1208 (mapc (lambda (x) (aset (cdr x
) 3 0)) sexp
)
1209 (delete-region (line-beginning-position) (line-end-position))
1210 (prin1 sexp
(current-buffer)))))
1211 (todo-set-categories)
1212 (unless buf2
(kill-buffer)))))
1213 (setq todo-files
(funcall todo-files-function
)
1214 todo-archives
(funcall todo-files-function t
))
1215 (when (or (string= file1-sn todo-default-todo-file
)
1216 (and delete2
(string= file1-sn todo-default-todo-file
)))
1217 (setq todo-default-todo-file
(todo-short-file-name (car todo-files
))))
1218 (when (or (string= file1 todo-global-current-todo-file
)
1219 (and delete2
(string= file2 todo-global-current-todo-file
)))
1220 (setq todo-global-current-todo-file nil
))
1221 (todo-update-filelist-defcustoms)
1222 (message (concat (cond (todo "Todo") (archive "Archive")) " file \"%s\" "
1225 (cond (todo "archive") (archive "todo"))
1230 (defvar todo-edit-buffer
"*Todo Edit*"
1231 "Name of current buffer in Todo Edit mode.")
1233 (defun todo-edit-file ()
1234 "Put current buffer in `todo-edit-mode'.
1235 This makes the entire file visible and the buffer writable and
1236 you can use the self-insertion keys and standard Emacs editing
1237 commands to make changes. To return to Todo mode, type
1238 \\[todo-edit-quit]. This runs a file format check, signaling
1239 an error if the format has become invalid. However, this check
1240 cannot tell if the number of items changed, which could result in
1241 the file containing inconsistent information. For this reason
1242 this command should be used with caution."
1250 Type %s to return to Todo%s mode.
1252 This also runs a file format check and signals an error if
1253 the format has become invalid. However, this check cannot
1254 tell if the number of items or categories changed, which
1255 could result in the file containing inconsistent information.
1256 You can repair this inconsistency by invoking the command
1257 `todo-repair-categories-sexp', but this will revert any
1258 renumbering of the categories you have made, so you will
1259 have to renumber them again (see `(todo-mode) Reordering
1262 (substitute-command-keys "\\[todo-edit-quit]")
1263 (if (equal "toda" (file-name-extension
1264 (buffer-file-name)))
1267 (defun todo-add-category (&optional file cat
)
1268 "Add a new category to a todo file.
1270 Called interactively with prefix argument FILE, prompt for a file
1271 and then for a new category to add to that file, otherwise prompt
1272 just for a category to add to the current todo file. After
1273 adding the category, visit it in Todo mode and if option
1274 `todo-add-item-if-new-category' is non-nil (the default), prompt
1277 Non-interactively, add category CAT to file FILE; if FILE is nil,
1278 add CAT to the current todo file. After adding the category,
1279 return the new category number."
1282 ;; If cat is passed from caller, don't prompt, unless it is "",
1283 ;; which means the file was just added and has no category yet.
1284 (if (and cat
(> (length cat
) 0))
1285 (setq file0
(or (and (stringp file
) file
)
1286 todo-current-todo-file
))
1287 (setq catfil
(todo-read-category "Enter a new category name: "
1288 'add
(when (called-interactively-p 'any
)
1291 file0
(if (called-interactively-p 'any
)
1295 (let ((counts (make-vector 4 0)) ; [todo diary done archived]
1296 (num (1+ (length todo-categories
)))
1297 (buffer-read-only nil
))
1298 (setq todo-current-todo-file file0
)
1299 (setq todo-categories
(append todo-categories
1300 (list (cons cat counts
))))
1302 (goto-char (point-max))
1303 (save-excursion ; Save point for todo-category-select.
1304 (insert todo-category-beg cat
"\n\n" todo-category-done
"\n"))
1305 (todo-update-categories-sexp)
1306 ;; If invoked by user, display the newly added category, if
1307 ;; called programmatically return the category number to the
1309 (if (called-interactively-p 'any
)
1311 (setq todo-category-number num
)
1312 (todo-category-select)
1313 (when todo-add-item-if-new-category
1314 (todo-insert-item--basic)))
1317 (defun todo-rename-category ()
1318 "Rename current todo category.
1319 If this file has an archive containing this category, rename the
1320 category there as well."
1322 (let* ((cat (todo-current-category))
1323 (new (read-from-minibuffer
1324 (format "Rename category \"%s\" to: " cat
))))
1325 (setq new
(todo-validate-name new
'category
))
1326 (let* ((ofile todo-current-todo-file
)
1327 (archive (concat (file-name-sans-extension ofile
) ".toda"))
1328 (buffers (append (list ofile
)
1329 (unless (zerop (todo-get-count 'archived cat
))
1331 (dolist (buf buffers
)
1332 (with-current-buffer (find-file-noselect buf
)
1333 (let (buffer-read-only)
1334 (setq todo-categories
(todo-set-categories))
1337 (setcar (assoc cat todo-categories
) new
)
1339 (goto-char (point-min))
1340 (todo-update-categories-sexp)
1341 (re-search-forward (concat (regexp-quote todo-category-beg
)
1342 "\\(" (regexp-quote cat
) "\\)\n")
1344 (replace-match new t t nil
1)))))))
1345 (force-mode-line-update))
1346 (save-excursion (todo-category-select)))
1348 (defun todo-delete-category (&optional arg
)
1349 "Delete current todo category provided it contains no items.
1350 With prefix ARG delete the category even if it does contain
1351 todo or done items."
1353 (let* ((file todo-current-todo-file
)
1354 (cat (todo-current-category))
1355 (todo (todo-get-count 'todo cat
))
1356 (done (todo-get-count 'done cat
))
1357 (archived (todo-get-count 'archived cat
)))
1359 (or (> todo
0) (> done
0)))
1360 (message "%s" (substitute-command-keys
1361 (concat "To delete a non-empty category, "
1362 "type C-u \\[todo-delete-category].")))
1363 (when (cond ((= (length todo-categories
) 1)
1365 (concat "This is the only category in this file; "
1366 "deleting it will also delete the file.\n"
1367 "Do you want to proceed? ")))
1369 (todo-y-or-n-p (format-message
1370 (concat "This category has archived items; "
1371 "the archived category will remain\n"
1372 "after deleting the todo category. "
1373 "Do you still want to delete it\n"
1374 "(see `todo-skip-archived-categories' "
1375 "for another option)? "))))
1377 (todo-y-or-n-p (concat "Permanently remove category \"" cat
1378 "\"" (and arg
" and all its entries")
1381 (let ((buffer-read-only)
1382 (beg (re-search-backward
1383 (concat "^" (regexp-quote (concat todo-category-beg cat
))
1386 (end (if (re-search-forward
1387 (concat "\n\\(" (regexp-quote todo-category-beg
)
1392 (remove-overlays beg end
)
1393 (delete-region beg end
)
1394 (if (= (length todo-categories
) 1)
1395 ;; If deleted category was the only one, delete the file.
1397 (todo-update-filelist-defcustoms)
1398 ;; Skip confirming killing the archive buffer if it has been
1399 ;; modified and not saved.
1400 (set-buffer-modified-p nil
)
1403 (message "Deleted todo file %s." file
))
1404 (setq todo-categories
(delete (assoc cat todo-categories
)
1406 (todo-update-categories-sexp)
1407 (setq todo-category-number
1408 (1+ (mod todo-category-number
(length todo-categories
))))
1409 (todo-category-select)
1410 (goto-char (point-min))
1411 (message "Deleted category %s." cat
)))))))
1413 (defun todo-move-category ()
1414 "Move current category to a different todo file.
1415 If the todo file chosen does not exist, it is created.
1416 If the current category has archived items, also move those to
1417 the archive of the file moved to, creating it if it does not exist."
1419 (when (or (> (length todo-categories
) 1)
1420 (todo-y-or-n-p (concat "This is the only category in this file; "
1421 "moving it will also delete the file.\n"
1422 "Do you want to proceed? ")))
1423 (let* ((ofile todo-current-todo-file
)
1424 (cat (todo-current-category))
1425 (nfile (todo-read-file-name "Todo file to move this category to: "))
1426 (archive (concat (file-name-sans-extension ofile
) ".toda"))
1427 (buffers (append (list ofile
)
1428 (unless (zerop (todo-get-count 'archived cat
))
1431 (while (equal nfile
(file-truename ofile
))
1432 (setq nfile
(todo-read-file-name
1433 "Choose a file distinct from this file: ")))
1434 (unless (member nfile todo-files
)
1435 (with-current-buffer (get-buffer-create nfile
)
1437 (write-region (point-min) (point-max) nfile nil
'nomessage nil t
)
1438 (kill-buffer nfile
))
1439 (setq todo-files
(funcall todo-files-function
))
1440 (todo-update-filelist-defcustoms))
1441 (dolist (buf buffers
)
1442 ;; Make sure archive file is in Todo Archive mode so that
1443 ;; todo-categories has correct value.
1444 (with-current-buffer (find-file-noselect buf
)
1445 (when (equal (file-name-extension (buffer-file-name)) "toda")
1446 (unless (derived-mode-p 'todo-archive-mode
)
1447 (todo-archive-mode)))
1449 (goto-char (point-max))
1450 (let* ((beg (re-search-backward
1452 (regexp-quote (concat todo-category-beg cat
))
1455 (end (if (re-search-forward
1456 (concat "^" (regexp-quote todo-category-beg
))
1460 (content (buffer-substring-no-properties beg end
))
1461 (counts (cdr (assoc cat todo-categories
)))
1463 ;; Move the category to the new file. Also update or create
1464 ;; archive file if necessary.
1465 (with-current-buffer
1467 ;; Regenerate todo-archives in case there
1468 ;; is a newly created archive.
1469 (if (member buf
(funcall todo-files-function t
))
1470 (concat (file-name-sans-extension nfile
) ".toda")
1472 (if (equal (file-name-extension (buffer-file-name)) "toda")
1473 (unless (derived-mode-p 'todo-archive-mode
)
1474 (todo-archive-mode))
1475 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
1476 (let* ((nfile-short (todo-short-file-name nfile
))
1478 (format "Todo file \"%s\" already has "
1480 (format "the category \"%s\";\n" cat
)
1481 "enter a new category name: "))
1484 (goto-char (point-max))
1486 ;; If the file moved to has a category with the same
1487 ;; name, rename the moved category.
1488 (when (assoc cat todo-categories
)
1489 (unless (member (file-truename (buffer-file-name))
1490 (funcall todo-files-function t
))
1491 (setq new
(read-from-minibuffer prompt
))
1492 (setq new
(todo-validate-name new
'category
))))
1493 ;; Replace old with new name in todo and archive files.
1495 (goto-char (point-max))
1497 (concat "^" (regexp-quote todo-category-beg
)
1498 "\\(" (regexp-quote cat
) "\\)$")
1500 (replace-match new nil nil nil
1))
1501 (setq todo-categories
1502 (append todo-categories
(list (cons (or new cat
) counts
))))
1503 (goto-char (point-min))
1504 (if (looking-at "((\"")
1505 ;; Delete existing sexp.
1506 (delete-region (line-beginning-position) (line-end-position))
1507 ;; Otherwise, file is new, so make space for categories sexp.
1509 (goto-char (point-min)))
1510 ;; Insert (new or updated) sexp.
1511 (prin1 todo-categories
(current-buffer)))
1512 ;; If archive was just created, save it to avoid "File
1513 ;; <xyz> no longer exists!" message on invoking
1514 ;; `todo-view-archived-items'.
1515 (unless (file-exists-p (buffer-file-name))
1517 (todo-category-number (or new cat
))
1518 (todo-category-select))
1519 ;; Delete the category from the old file, and if that was the
1520 ;; last category, delete the file. Also handle archive file
1522 (remove-overlays beg end
)
1523 (delete-region beg end
)
1524 (goto-char (point-min))
1525 ;; Put point after todo-categories sexp.
1527 (if (eobp) ; Aside from sexp, file is empty.
1529 ;; Skip confirming killing the archive buffer.
1530 (set-buffer-modified-p nil
)
1531 (delete-file todo-current-todo-file
)
1533 (when (member todo-current-todo-file todo-files
)
1534 (todo-update-filelist-defcustoms)))
1535 (setq todo-categories
(delete (assoc cat todo-categories
)
1537 (todo-update-categories-sexp)
1538 (when (> todo-category-number
(length todo-categories
))
1539 (setq todo-category-number
1))
1540 (todo-category-select)))))
1541 (set-window-buffer (selected-window)
1542 (set-buffer (find-file-noselect nfile
))))))
1544 (defun todo-merge-category (&optional file
)
1545 "Merge current category into another existing category.
1547 With prefix argument FILE, prompt for a specific todo file and
1548 choose (with TAB completion) a category in it to merge into;
1549 otherwise, choose and merge into a category in either the
1550 current todo file or a file in `todo-category-completions-files'.
1552 After merging, the source category's todo and done items are
1553 appended to the chosen goal category's todo and done items,
1554 respectively. The goal category becomes the current category,
1555 and the source category is deleted.
1557 If both the source and goal categories also have archived items,
1558 they are also merged. If only the source category has archived
1559 items, the goal category is added as a new category to the
1560 archive file and the source category is deleted."
1562 (let* ((tfile todo-current-todo-file
)
1563 (cat (todo-current-category))
1564 (cat+file
(todo-read-category "Merge into category: " 'todo file
))
1565 (goal (car cat
+file
))
1566 (gfile (cdr cat
+file
))
1567 (tarchive (concat (file-name-sans-extension tfile
) ".toda"))
1568 (garchive (concat (file-name-sans-extension gfile
) ".toda"))
1569 (archived-count (todo-get-count 'archived
))
1571 (with-current-buffer (get-buffer (find-file-noselect tfile
))
1573 (let* ((buffer-read-only nil
)
1576 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1578 (tbeg (progn (forward-line) (point-marker)))
1581 (concat "^" (regexp-quote todo-category-done
)) nil t
)
1582 (forward-line) (point-marker)))
1583 ;; Omit empty line between todo and done items.
1584 (tend (progn (forward-line -
2) (point-marker)))
1586 (if (re-search-forward
1587 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1589 (goto-char (match-beginning 0))
1591 (point-max-marker))))
1592 (todo (buffer-substring-no-properties tbeg tend
))
1593 (done (buffer-substring-no-properties dbeg cend
))
1594 (todo-count (todo-get-count 'todo cat
))
1595 (done-count (todo-get-count 'done cat
)))
1596 ;; Merge into goal todo category.
1597 (with-current-buffer (get-buffer (find-file-noselect gfile
))
1598 (unless (derived-mode-p 'todo-mode
) (todo-mode))
1600 (goto-char (point-min))
1601 (let ((buffer-read-only nil
))
1602 ;; Merge any todo items.
1603 (unless (zerop (length todo
))
1605 (concat "^" (regexp-quote (concat todo-category-beg goal
)) "$")
1608 (concat "^" (regexp-quote todo-category-done
)) nil t
)
1610 (setq here
(point-marker))
1612 (todo-update-count 'todo todo-count goal
))
1613 ;; Merge any done items.
1614 (unless (zerop (length done
))
1615 (goto-char (if (re-search-forward
1616 (concat "^" (regexp-quote todo-category-beg
))
1620 (when (zerop (length todo
)) (setq here
(point-marker)))
1622 (todo-update-count 'done done-count goal
)))
1623 (todo-update-categories-sexp))
1624 ;; Update and clean up source todo file.
1625 (remove-overlays cbeg cend
)
1626 (delete-region cbeg cend
)
1627 (setq todo-categories
(delete (assoc cat todo-categories
)
1629 (todo-update-categories-sexp)
1630 (when (> todo-category-number
(length todo-categories
))
1631 (setq todo-category-number
1))
1632 (todo-category-select)
1633 (mapc (lambda (m) (set-marker m nil
))
1634 (list cbeg tbeg dbeg tend cend
))))
1635 (when (> archived-count
0)
1636 (with-current-buffer (get-buffer (find-file-noselect tarchive
))
1638 (goto-char (point-min))
1639 (let* ((buffer-read-only nil
)
1641 (when (re-search-forward
1642 (concat "^" (regexp-quote
1643 (concat todo-category-beg cat
)) "$")
1645 (goto-char (match-beginning 0))
1647 (cend (if (re-search-forward
1648 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1654 (buffer-substring-no-properties (point) cend
))))
1655 ;; Merge into goal archive category, if it exists, else create it.
1656 (with-current-buffer (get-buffer (find-file-noselect garchive
))
1657 (let ((gbeg (when (re-search-forward
1658 (concat "^" (regexp-quote
1659 (concat todo-category-beg goal
))
1662 (goto-char (match-beginning 0))
1664 (goto-char (if (and gbeg
1666 (concat "^" (regexp-quote todo-category-beg
))
1670 (unless gbeg
(todo-add-category nil goal
))
1672 (todo-update-categories-sexp)))
1673 ;; Update and clean up source archive file.
1674 (remove-overlays cbeg cend
)
1675 (delete-region cbeg cend
)
1676 (setq todo-categories
(todo-make-categories-list t
))
1677 (todo-update-categories-sexp))))
1678 ;; Update goal todo file for merged archived items and display it.
1679 (set-window-buffer (selected-window) (set-buffer (get-file-buffer gfile
)))
1680 (unless (zerop archived-count
)
1681 (todo-update-count 'archived archived-count goal
)
1682 (todo-update-categories-sexp))
1683 (todo-category-number goal
)
1684 ;; If there are only merged done items, show them.
1685 (let ((todo-show-with-done (zerop (todo-get-count 'todo goal
))))
1686 (todo-category-select)
1687 ;; Put point on the first merged item.
1689 (set-marker here nil
)))
1691 ;; -----------------------------------------------------------------------------
1693 ;; -----------------------------------------------------------------------------
1695 (defcustom todo-include-in-diary nil
1696 "Non-nil to allow new todo items to be included in the diary."
1700 (defcustom todo-diary-nonmarking nil
1701 "Non-nil to insert new todo diary items as nonmarking by default.
1702 This appends `diary-nonmarking-symbol' to the front of an item on
1703 insertion provided it doesn't begin with `todo-nondiary-marker'."
1707 (defcustom todo-always-add-time-string nil
1708 "Non-nil adds current time to a new item's date header by default.
1709 When the todo insertion commands have a non-nil \"maybe-notime\"
1710 argument, this reverses the effect of
1711 `todo-always-add-time-string': if t, these commands omit the
1712 current time, if nil, they include it."
1716 (defcustom todo-use-only-highlighted-region t
1717 "Non-nil to enable inserting only highlighted region as new item."
1721 (defcustom todo-default-priority
'first
1722 "Default priority of new and moved items."
1723 :type
'(choice (const :tag
"Highest priority" first
)
1724 (const :tag
"Lowest priority" last
))
1727 (defcustom todo-item-mark
"*"
1728 "String used to mark items.
1729 To ensure item marking works, change the value of this option
1730 only when no items are marked."
1731 :type
'(string :validate
1733 (when (string= (widget-value widget
) todo-prefix
)
1737 "Invalid value: must be distinct from `todo-prefix'"))
1739 :set
(lambda (symbol value
)
1740 (custom-set-default symbol
(propertize value
'face
'todo-mark
)))
1743 (defcustom todo-comment-string
"COMMENT"
1744 "String inserted before optional comment appended to done item."
1746 :initialize
'custom-initialize-default
1747 :set
'todo-reset-comment-string
1750 (defcustom todo-undo-item-omit-comment
'ask
1751 "Whether to omit done item comment on undoing the item.
1752 Nil means never omit the comment, t means always omit it, `ask'
1753 means prompt user and omit comment only on confirmation."
1754 :type
'(choice (const :tag
"Never" nil
)
1755 (const :tag
"Always" t
)
1756 (const :tag
"Ask" ask
))
1759 (defun todo-toggle-mark-item (&optional n
)
1760 "Mark item with `todo-item-mark' if unmarked, otherwise unmark it.
1761 With positive numerical prefix argument N, change the marking of
1762 the next N items in the current category. If both the todo and
1763 done items sections are visible, the sequence of N items can
1764 consist of the last todo items and the first done items."
1766 (when (todo-item-string)
1767 (let ((cat (todo-current-category)))
1768 (unless (> n
1) (setq n
1))
1771 (let* ((marks (assoc cat todo-categories-with-marks
))
1773 (unless (looking-at todo-item-start
)
1775 (todo-get-overlay 'prefix
)))
1776 (pref (overlay-get ov
'before-string
)))
1777 (if (todo-marked-item-p)
1779 (overlay-put ov
'before-string
(substring pref
1))
1780 (if (= (cdr marks
) 1) ; Deleted last mark in this category.
1781 (setq todo-categories-with-marks
1782 (assq-delete-all cat todo-categories-with-marks
))
1783 (setcdr marks
(1- (cdr marks
)))))
1784 (overlay-put ov
'before-string
(concat todo-item-mark pref
))
1786 (setcdr marks
(1+ (cdr marks
)))
1787 (push (cons cat
1) todo-categories-with-marks
))))
1789 ;; Don't try to mark the empty lines at the end of the todo
1790 ;; and done items sections.
1791 (when (looking-at "^$")
1794 (todo-forward-item))))))))
1796 (defun todo-mark-category ()
1797 "Mark all visible items in this category with `todo-item-mark'."
1799 (let ((cat (todo-current-category)))
1801 (goto-char (point-min))
1803 (let* ((marks (assoc cat todo-categories-with-marks
))
1804 (ov (todo-get-overlay 'prefix
))
1805 ;; When done items are shown and there are no todo items, the
1806 ;; loop starts on the empty line in the todo items sections,
1807 ;; which has no overlay, so don't try to get it.
1808 (pref (when ov
(overlay-get ov
'before-string
))))
1809 (unless (or (todo-marked-item-p) (not ov
))
1810 (overlay-put ov
'before-string
(concat todo-item-mark pref
))
1812 (setcdr marks
(1+ (cdr marks
)))
1813 (push (cons cat
1) todo-categories-with-marks
))))
1815 ;; Don't try to mark the empty line between the todo and done
1817 (when (looking-at "^$")
1819 (todo-forward-item)))))))
1821 (defun todo-unmark-category ()
1822 "Remove `todo-item-mark' from all visible items in this category."
1824 (let* ((cat (todo-current-category))
1825 (marks (assoc cat todo-categories-with-marks
)))
1827 (goto-char (point-min))
1829 (let* ((ov (todo-get-overlay 'prefix
))
1830 ;; See comment above in `todo-mark-category'.
1831 (pref (when ov
(overlay-get ov
'before-string
))))
1832 (when (todo-marked-item-p)
1833 (overlay-put ov
'before-string
(substring pref
1)))
1834 (todo-forward-item))))
1835 (setq todo-categories-with-marks
1836 (delq marks todo-categories-with-marks
))))
1838 (defvar todo-date-from-calendar nil
1839 "Helper variable for setting item date from the Emacs Calendar.")
1841 (defvar todo-insert-item--parameters
)
1843 (defun todo-insert-item (&optional arg
)
1844 "Choose an item insertion operation and carry it out.
1845 This inserts a new todo item into a category.
1847 With no prefix argument ARG, add the item to the current
1848 category; with one prefix argument (`C-u'), prompt for a category
1849 from the current todo file; with two prefix arguments (`C-u
1850 C-u'), first prompt for a todo file, then a category in that
1851 file. If a non-existing category is entered, ask whether to add
1852 it to the todo file; if answered affirmatively, add the category
1853 and insert the item there.
1855 There are a number of item insertion parameters which can be
1856 combined by entering specific keys to produce different insertion
1857 commands. After entering each key, a message shows which have
1858 already been entered and which remain available. See
1859 `(todo-mode) Inserting New Items' for details of the parameters,
1860 their associated keys and their effects."
1862 (todo-insert-item--next-param (list arg
) todo-insert-item--parameters nil
"i"))
1864 (defun todo-insert-item--basic (&optional arg diary-type date-type time where
)
1865 "Function implementing the core of `todo-insert-item'."
1866 ;; If invoked outside of Todo mode and there is not yet any Todo
1867 ;; file, initialize one.
1868 (if (null (funcall todo-files-function
))
1870 (let ((copy (eq where
'copy
))
1871 (region (eq where
'region
))
1872 (here (eq where
'here
))
1874 (when (and arg here
)
1875 (user-error "Here insertion only valid in current category"))
1876 (when (and (or copy here
)
1877 (or (not (eq major-mode
'todo-mode
)) (todo-done-item-p)
1878 (when copy
(looking-at "^$"))
1881 ;; Point is on done items separator.
1882 (looking-at todo-category-done
))))
1883 (user-error (concat "Item " (if copy
"copying" "insertion")
1884 " is not valid here")))
1885 (when copy
(setq diary-item
(todo-diary-item-p)))
1887 (let (use-empty-active-region)
1888 (unless (and todo-use-only-highlighted-region
(use-region-p))
1889 (user-error "There is no active region"))))
1890 (let* ((obuf (current-buffer))
1891 (ocat (todo-current-category))
1893 (cat+file
(cond ((equal arg
'(4))
1894 (todo-read-category "Insert in category: "))
1896 (todo-read-category "Insert in category: "
1899 (cons (todo-current-category)
1900 (or todo-current-todo-file
1901 (and todo-show-current-file
1902 todo-global-current-todo-file
)
1903 (todo-absolute-file-name
1904 todo-default-todo-file
))))))
1905 (cat (car cat
+file
))
1906 (file (cdr cat
+file
))
1907 (new-item (cond (copy (todo-item-string))
1908 (region (buffer-substring-no-properties
1909 (region-beginning) (region-end)))
1910 (t (if (eq major-mode
'todo-archive-mode
)
1911 (user-error (concat "Cannot insert a new Todo"
1912 " item in an archive"))
1913 (read-from-minibuffer "Todo item: ")))))
1915 ((eq date-type
'date
)
1917 ((eq date-type
'dayname
)
1918 (todo-read-dayname))
1919 ((eq date-type
'calendar
)
1920 (setq todo-date-from-calendar t
)
1921 (or (todo-set-date-from-calendar)
1922 ;; If user exits Calendar before choosing
1923 ;; a date, cancel item insertion.
1925 ((and (stringp date-type
)
1926 (string-match todo-date-pattern date-type
))
1927 (setq todo-date-from-calendar date-type
)
1928 (todo-set-date-from-calendar))
1930 (calendar-date-string
1931 (calendar-current-date) t t
))))
1932 (time-string (or (and time
(todo-read-time))
1933 (and todo-always-add-time-string
1934 (substring (current-time-string) 11 16)))))
1935 (setq todo-date-from-calendar nil
)
1936 (find-file-noselect file
'nowarn
)
1937 (set-window-buffer (selected-window)
1938 (set-buffer (find-buffer-visiting file
)))
1939 ;; If this command was invoked outside of a Todo mode buffer,
1940 ;; the call to todo-current-category above returned nil. If
1941 ;; we just entered Todo mode now, then cat was set to the
1942 ;; file's first category, but if todo-mode was already
1943 ;; enabled, cat did not get set, so we have to do that.
1945 (setq cat
(todo-current-category)))
1946 (setq todo-current-todo-file file
)
1947 (unless todo-global-current-todo-file
1948 (setq todo-global-current-todo-file todo-current-todo-file
))
1949 (let ((buffer-read-only nil
)
1950 done-only item-added
)
1953 ;; Add date, time and diary marking as required.
1954 (concat (if (not (and diary-type
1955 (not todo-include-in-diary
)))
1957 (when (and (eq diary-type
'nonmarking
)
1958 (not todo-diary-nonmarking
))
1959 diary-nonmarking-symbol
))
1960 date-string
(when (and time-string
; Can be empty.
1963 (concat " " time-string
))
1964 (when (not (and diary-type
1965 (not todo-include-in-diary
)))
1968 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
1969 (setq new-item
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
1970 "\n\t" new-item nil nil
1)))
1973 ;; If we just visited the file, no category is selected yet.
1974 (when (= (- (point-max) (point-min)) (buffer-size))
1975 (todo-category-number cat
)
1976 (todo-category-select))
1977 ;; If only done items are displayed in category,
1978 ;; toggle to todo items before inserting new item.
1979 (when (save-excursion
1980 (goto-char (point-min))
1981 (looking-at todo-done-string-start
))
1983 (todo-toggle-view-done-only))
1985 (todo-insert-with-overlays new-item
)
1986 (todo-set-item-priority new-item cat t
))
1987 (setq item-added t
))
1988 ;; If user cancels before setting priority, restore
1991 (set-window-buffer (selected-window) (set-buffer obuf
))
1993 (unless (equal cat ocat
)
1994 (todo-category-number ocat
)
1995 (todo-category-select))
1996 (and done-only
(todo-toggle-view-done-only)))
1998 ;; If the todo items section is not visible when the
1999 ;; insertion command is called (either because only done
2000 ;; items were shown or because the category was not in the
2001 ;; current buffer), then if the item is inserted at the
2002 ;; end of the category, point is at eob and eob at
2003 ;; window-start, so that higher priority todo items are
2004 ;; out of view. So we recenter to make sure the todo
2005 ;; items are displayed in the window.
2006 (when item-added
(recenter)))
2007 (todo-update-count 'todo
1)
2008 (when (or diary-item diary-type todo-include-in-diary
)
2009 (todo-update-count 'diary
1))
2010 (todo-update-categories-sexp))))))
2012 (defun todo-set-date-from-calendar ()
2013 "Return string of date chosen from Calendar."
2014 (cond ((and (stringp todo-date-from-calendar
)
2015 (string-match todo-date-pattern todo-date-from-calendar
))
2016 todo-date-from-calendar
)
2017 (todo-date-from-calendar
2018 (let (calendar-view-diary-initially-flag)
2019 (calendar)) ; *Calendar* is now current buffer.
2020 (define-key calendar-mode-map
[remap newline
] 'exit-recursive-edit
)
2021 ;; If user exits Calendar before choosing a date, clean up properly.
2022 (define-key calendar-mode-map
2023 [remap calendar-exit
] (lambda ()
2027 (exit-recursive-edit))))
2028 (message "Put cursor on a date and type <return> to set it.")
2031 (when (equal (buffer-name) calendar-buffer
)
2032 (setq todo-date-from-calendar
2033 (calendar-date-string (calendar-cursor-to-date t
) t t
))
2035 todo-date-from-calendar
)
2036 (define-key calendar-mode-map
[remap newline
] nil
)
2037 (define-key calendar-mode-map
[remap calendar-exit
] nil
)
2038 (unless (zerop (recursion-depth)) (exit-recursive-edit))
2039 (when (stringp todo-date-from-calendar
)
2040 todo-date-from-calendar
)))))
2042 (defun todo-insert-item-from-calendar (&optional arg
)
2043 "Prompt for and insert a new item with date selected from calendar.
2044 Invoked without prefix argument ARG, insert the item into the
2045 current category, without one prefix argument, prompt for the
2046 category from the current todo file or from one listed in
2047 `todo-category-completions-files'; with two prefix arguments,
2048 prompt for a todo file and then for a category in it."
2050 (setq todo-date-from-calendar
2051 (calendar-date-string (calendar-cursor-to-date t
) t t
))
2053 (todo-insert-item--basic arg nil todo-date-from-calendar
))
2055 (define-key calendar-mode-map
"it" 'todo-insert-item-from-calendar
)
2057 (defun todo-delete-item ()
2058 "Delete at least one item in this category.
2059 If there are marked items, delete all of these; otherwise, delete
2064 (let* ((cat (todo-current-category))
2065 (marked (assoc cat todo-categories-with-marks
))
2066 (item (unless marked
(todo-item-string)))
2069 "Permanently delete all marked items? ")
2071 (setq ov
(make-overlay
2072 (save-excursion (todo-item-start))
2073 (save-excursion (todo-item-end))))
2074 (overlay-put ov
'face
'todo-search
)
2075 (todo-y-or-n-p "Permanently delete this item? "))))
2078 (and marked
(goto-char (point-min)))
2081 (if (or (and marked
(todo-marked-item-p)) item
)
2083 (if (todo-done-item-p)
2084 (todo-update-count 'done -
1)
2085 (todo-update-count 'todo -
1 cat
)
2086 (and (todo-diary-item-p)
2087 (todo-update-count 'diary -
1)))
2088 (if ov
(delete-overlay ov
))
2090 ;; Don't leave point below last item.
2091 (and item
(bolp) (eolp) (< (point-min) (point-max))
2092 (todo-backward-item))
2094 (throw 'done
(setq item nil
))))
2095 (todo-forward-item))))
2097 (setq todo-categories-with-marks
2098 (assq-delete-all cat todo-categories-with-marks
)))
2099 (todo-update-categories-sexp)
2100 (todo-prefix-overlays)
2101 (when (and (zerop (todo-get-count 'diary
))
2103 (goto-char (point-min))
2105 (concat "^" (regexp-quote todo-category-done
))
2107 (let (todo-show-with-done) (todo-category-select)))))
2108 (if ov
(delete-overlay ov
)))))
2110 (defun todo-edit-item (&optional arg
)
2111 "Choose an editing operation for the current item and carry it out."
2113 (let ((marked (assoc (todo-current-category) todo-categories-with-marks
)))
2114 (cond ((and (todo-done-item-p) (not marked
))
2115 (todo-edit-item--next-key 'done arg
))
2116 ((or marked
(todo-item-string))
2117 (todo-edit-item--next-key 'todo arg
)))))
2119 (defun todo-edit-item--text (&optional arg
)
2120 "Function providing the text editing facilities of `todo-edit-item'."
2121 (let ((full-item (todo-item-string)))
2122 ;; If there are marked items and user invokes a text-editing
2123 ;; commands with point not on an item, todo-item-start is nil and
2124 ;; 1+ signals an error, so just make this a noop.
2126 (let* ((opoint (point))
2127 (start (todo-item-start))
2128 (end (save-excursion (todo-item-end)))
2131 (concat todo-date-string-start todo-date-pattern
2132 "\\( " diary-time-regexp
"\\)?"
2133 (regexp-quote todo-nondiary-end
) "?")
2134 (line-end-position) t
)
2135 (1+ (- (point) start
))))
2136 (include-header (eq arg
'include-header
))
2137 (comment-edit (eq arg
'comment-edit
))
2138 (comment-delete (eq arg
'comment-delete
))
2139 (header-string (substring full-item
0 item-beg
))
2140 (item (if (or include-header comment-edit comment-delete
)
2142 (substring full-item item-beg
)))
2143 (multiline (or (eq arg
'multiline
)
2144 (> (length (split-string item
"\n")) 1)))
2145 (comment (save-excursion
2148 (concat " \\[" (regexp-quote todo-comment-string
)
2151 (prompt (if comment
"Edit comment: " "Enter a comment: "))
2152 (buffer-read-only nil
))
2153 ;; When there are marked items, user can invoke todo-edit-item
2154 ;; even if point is not on an item, but text editing only
2155 ;; applies to the item at point.
2156 (when (or (and (todo-done-item-p)
2157 (or comment-edit comment-delete
))
2158 (and (not (todo-done-item-p))
2159 (or (not arg
) include-header multiline
)))
2161 ((or comment-edit comment-delete
)
2164 (if (re-search-forward (concat " \\["
2165 (regexp-quote todo-comment-string
)
2169 (when (todo-y-or-n-p "Delete comment? ")
2170 (delete-region (match-beginning 0) (match-end 0)))
2171 (replace-match (read-string prompt
(cons (match-string 1) 1))
2174 (user-error "There is no comment to delete")
2175 (insert " [" todo-comment-string
": "
2176 (prog1 (read-string prompt
)
2177 ;; If user moved point during editing,
2178 ;; make sure it moves back.
2183 (let ((buf todo-edit-buffer
))
2184 (set-window-buffer (selected-window)
2185 (set-buffer (make-indirect-buffer
2186 (buffer-name) buf
)))
2187 (narrow-to-region (todo-item-start) (todo-item-end))
2189 (message "%s" (substitute-command-keys
2190 (concat "Type \\[todo-edit-quit] "
2191 "to return to Todo mode.\n")))))
2193 (let ((new (concat (if include-header
"" header-string
)
2194 (read-string "Edit: " (if include-header
2195 (cons item item-beg
)
2197 (when include-header
2198 (while (not (string-match (concat todo-date-string-start
2201 (setq new
(read-from-minibuffer
2202 "Item must start with a date: " new
))))
2203 ;; Ensure lines following hard newlines are indented.
2204 (setq new
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
2205 "\n\t" new nil nil
1))
2206 ;; If user moved point during editing, make sure it moves back.
2209 (todo-insert-with-overlays new
)
2210 (move-to-column item-beg
)))))))))
2212 (defun todo-edit-quit ()
2213 "Return from Todo Edit mode to Todo mode.
2214 If the item contains hard line breaks, make sure the following
2215 lines are indented by `todo-indent-to-here' to conform to diary
2218 If the whole file was in Todo Edit mode, check before returning
2219 whether the file is still a valid todo file and if so, also
2220 recalculate the todo file's categories sexp, in case changes were
2221 made in the number or names of categories."
2223 (if (> (buffer-size) (- (point-max) (point-min)))
2224 ;; We got here via `e m'.
2225 (let ((item (buffer-string))
2226 (regex "\\(\n\\)[^[:blank:]]")
2227 (buf (buffer-base-buffer)))
2228 (while (not (string-match (concat todo-date-string-start
2231 (setq item
(read-from-minibuffer
2232 "Item must start with a date: " item
)))
2233 ;; Ensure lines following hard newlines are indented.
2234 (when (string-match regex
(buffer-string))
2235 (setq item
(replace-regexp-in-string regex
"\n\t" item nil nil
1))
2236 (delete-region (point-min) (point-max))
2239 (unless (eq (current-buffer) buf
)
2240 (set-window-buffer (selected-window) (set-buffer buf
)))
2241 (if transient-mark-mode
(deactivate-mark)))
2242 ;; We got here via `F e'.
2243 (when (todo-check-format)
2244 ;; FIXME: separate out sexp check?
2245 ;; If manual editing makes e.g. item counts change, have to
2246 ;; call this to update todo-categories, but it restores
2247 ;; category order to list order.
2248 ;; (todo-repair-categories-sexp)
2249 ;; Compare (todo-make-categories-list t) with sexp and if
2250 ;; different ask (todo-update-categories-sexp) ?
2251 (if (equal (file-name-extension (buffer-file-name)) "toda")
2254 (let* ((cat-beg (concat "^" (regexp-quote todo-category-beg
)
2256 (curline (buffer-substring-no-properties
2257 (line-beginning-position) (line-end-position)))
2258 (cat (cond ((string-match cat-beg curline
)
2259 (match-string-no-properties 1 curline
))
2260 ((or (re-search-backward cat-beg nil t
)
2261 (re-search-forward cat-beg nil t
))
2262 (match-string-no-properties 1)))))
2263 (todo-category-number cat
)
2264 (todo-category-select)
2265 (goto-char (point-min))))))
2267 (defun todo-edit-item--header (what &optional inc
)
2268 "Function providing header editing facilities of `todo-edit-item'."
2269 (let ((marked (assoc (todo-current-category) todo-categories-with-marks
))
2271 (todo-date-from-calendar t
)
2272 ;; INC must be an integer, but users could pass it via
2273 ;; `todo-edit-item' as e.g. `-' or `C-u'.
2274 (inc (prefix-numeric-value inc
))
2275 (buffer-read-only nil
)
2277 year monthname month day dayname
)
2278 (when marked
(todo--user-error-if-marked-done-item))
2280 (or (and marked
(goto-char (point-min))) (todo-item-start))
2284 (while (not (todo-marked-item-p))
2286 (and (eobp) (throw 'end nil
))))
2287 (re-search-forward (concat todo-date-string-start
"\\(?1:"
2289 "\\)\\(?2: " diary-time-regexp
"\\)?"
2290 (regexp-quote todo-nondiary-end
) "?")
2291 (line-end-position) t
)
2292 (let* ((otime (match-string-no-properties 2))
2293 (odayname (match-string-no-properties 5))
2294 (omonthname (match-string-no-properties 6))
2295 (omonth (match-string-no-properties 7))
2296 (oday (match-string-no-properties 8))
2297 (oyear (match-string-no-properties 9))
2298 (tmn-array todo-month-name-array
)
2299 (mlist (append tmn-array nil
))
2300 (tma-array todo-month-abbrev-array
)
2301 (mablist (append tma-array nil
))
2302 (yy (and oyear
(string-to-number oyear
))) ; 0 if year is "*".
2303 (mm (or (and omonth
(if (string= omonth
"*") 13
2304 (string-to-number omonth
)))
2305 (1+ (- (length mlist
)
2306 (length (or (member omonthname mlist
)
2307 (member omonthname mablist
)))))))
2308 (dd (and oday
(unless (string= oday
"*")
2309 (string-to-number oday
)))))
2310 ;; If there are marked items, use only the first to set
2311 ;; header changes, and apply these to all marked items.
2315 (setq ndate
(todo-read-date)))
2316 ((eq what
'calendar
)
2317 (setq ndate
(save-match-data (todo-set-date-from-calendar))))
2319 (setq ndate
(calendar-date-string (calendar-current-date) t t
)))
2321 (setq ndate
(todo-read-dayname)))
2323 (setq ntime
(save-match-data (todo-read-time)))
2324 (when (> (length ntime
) 0)
2325 (setq ntime
(concat " " ntime
))))
2326 ;; When date string consists only of a day name,
2327 ;; passing other date components is a noop.
2328 ((and odayname
(memq what
'(year month day
))))
2331 monthname omonthname
2333 year
(cond ((not current-prefix-arg
)
2334 (todo-read-date 'year
))
2335 ((string= oyear
"*")
2336 (user-error "Cannot increment *"))
2338 (number-to-string (+ yy inc
))))))
2342 (if (memq 'month calendar-date-display-form
)
2345 (cond ((not current-prefix-arg
)
2346 (todo-read-date 'month
))
2347 ((or (string= omonth
"*") (= mm
13))
2348 (user-error "Cannot increment *"))
2350 (let ((mminc (+ mm inc
(if (< inc
0) 12 0))))
2351 ;; Increment or decrement month by INC
2353 (setq mm
(% mminc
12))
2354 ;; If result is 0, make month December.
2355 (setq mm
(if (= mm
0) 12 (abs mm
)))
2356 ;; Adjust year if necessary.
2357 (setq year
(or (and (cond ((> mminc
12)
2358 (+ yy
(/ mminc
12)))
2360 (- yy
(/ mminc
12) 1))
2362 (number-to-string yy
))
2364 ;; Return the changed numerical month as
2365 ;; a string or the corresponding month name.
2367 (number-to-string mm
)
2368 (aref tma-array
(1- mm
))))))
2369 ;; Since the number corresponding to the arbitrary
2370 ;; month name "*" is out of the range of
2371 ;; calendar-last-day-of-month, set it to 1
2372 ;; (corresponding to January) to allow 31 days.
2373 (let ((mm (if (= mm
13) 1 mm
)))
2374 (if (> (string-to-number day
)
2375 (calendar-last-day-of-month mm yy
))
2376 (user-error "%s %s does not have %s days"
2377 (aref tmn-array
(1- mm
))
2378 (if (= mm
2) yy
"") day
))))
2382 monthname omonthname
2384 ((not current-prefix-arg
)
2385 (todo-read-date 'day mm yy
))
2387 (user-error "Cannot increment *"))
2388 ((or (string= omonth
"*") (string= omonthname
"*"))
2389 (setq dd
(+ dd inc
))
2392 "A month cannot have more than 31 days")
2393 (number-to-string dd
)))
2394 ;; Increment or decrement day by INC,
2395 ;; adjusting month and year if necessary
2396 ;; (if year is "*" assume current year to
2397 ;; calculate adjustment).
2399 (let* ((yy (or yy
(calendar-extract-year
2400 (calendar-current-date))))
2401 (date (calendar-gregorian-from-absolute
2402 (+ (calendar-absolute-from-gregorian
2405 (adjmm (nth 0 date
)))
2406 ;; Set year and month(name) to adjusted values.
2407 (unless (string= year
"*")
2408 (setq year
(number-to-string (nth 2 date
))))
2410 (setq month
(number-to-string adjmm
))
2411 (setq monthname
(aref tma-array
(1- adjmm
))))
2412 ;; Return changed numerical day as a string.
2413 (number-to-string (nth 1 date
)))))))))
2415 ;; If year, month or day date string components were
2416 ;; changed, rebuild the date string.
2417 (when (memq what
'(year month day
))
2420 ;; Needed by calendar-date-display-form.
2422 (monthname monthname
)
2426 (mapconcat #'eval calendar-date-display-form
"")))))
2427 (when ndate
(replace-match ndate nil nil nil
1))
2428 ;; Add new time string to the header, if it was supplied.
2431 (replace-match ntime nil nil nil
2)
2432 (goto-char (match-end 1))
2434 (setq todo-date-from-calendar nil
)
2436 ;; Apply the changes to the first marked item header to the
2437 ;; remaining marked items. If there are no marked items,
2441 (goto-char (point-max))))))))
2443 (defun todo-edit-item--diary-inclusion (&optional nonmarking
)
2444 "Function providing diary marking facilities of `todo-edit-item'."
2445 (let ((buffer-read-only)
2446 (marked (assoc (todo-current-category) todo-categories-with-marks
)))
2447 (when marked
(todo--user-error-if-marked-done-item))
2450 (when marked
(goto-char (point-min)))
2452 (unless (and marked
(not (todo-marked-item-p)))
2453 (let* ((_beg (todo-item-start))
2454 (lim (save-excursion (todo-item-end)))
2455 (end (save-excursion
2456 (or (todo-time-string-matcher lim
)
2457 (todo-date-string-matcher lim
)))))
2459 (if (looking-at (regexp-quote diary-nonmarking-symbol
))
2461 (when (looking-at (regexp-quote todo-nondiary-start
))
2464 (search-forward todo-nondiary-end
(1+ end
) t
)
2466 (todo-update-count 'diary
1)))
2467 (insert diary-nonmarking-symbol
))
2468 (if (looking-at (regexp-quote todo-nondiary-start
))
2471 (search-forward todo-nondiary-end
(1+ end
) t
)
2473 (todo-update-count 'diary
1))
2475 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2477 (setq end
(1- end
))) ; Since we deleted nonmarking symbol.
2478 (insert todo-nondiary-start
)
2479 (goto-char (1+ end
))
2480 (insert todo-nondiary-end
)
2481 (todo-update-count 'diary -
1))))))
2482 (unless marked
(throw 'stop nil
))
2483 (todo-forward-item)))))
2484 (todo-update-categories-sexp))
2486 (defun todo-edit-category-diary-inclusion (arg)
2487 "Make all items in this category diary items.
2488 With prefix ARG, make all items in this category non-diary
2492 (goto-char (point-min))
2493 (let ((todo-count (todo-get-count 'todo
))
2494 (diary-count (todo-get-count 'diary
))
2498 (if (todo-done-item-p) ; We've gone too far.
2500 (let* ((_beg (todo-item-start))
2501 (lim (save-excursion (todo-item-end)))
2502 (end (save-excursion
2503 (or (todo-time-string-matcher lim
)
2504 (todo-date-string-matcher lim
)))))
2506 (unless (looking-at (regexp-quote todo-nondiary-start
))
2507 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2509 (setq end
(1- end
))) ; Since we deleted nonmarking symbol.
2510 (insert todo-nondiary-start
)
2511 (goto-char (1+ end
))
2512 (insert todo-nondiary-end
))
2513 (when (looking-at (regexp-quote todo-nondiary-start
))
2515 (search-forward todo-nondiary-end
(1+ end
) t
)
2516 (replace-match "")))))
2517 (todo-forward-item))
2518 (unless (if arg
(zerop diary-count
) (= diary-count todo-count
))
2519 (todo-update-count 'diary
(if arg
2521 (- todo-count diary-count
))))
2522 (todo-update-categories-sexp)))))
2524 (defun todo-edit-category-diary-nonmarking (arg)
2525 "Add `diary-nonmarking-symbol' to all diary items in this category.
2526 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
2527 items in this category."
2530 (goto-char (point-min))
2531 (let (buffer-read-only)
2534 (if (todo-done-item-p) ; We've gone too far.
2536 (unless (looking-at (regexp-quote todo-nondiary-start
))
2538 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2540 (unless (looking-at (regexp-quote diary-nonmarking-symbol
))
2541 (insert diary-nonmarking-symbol
))))
2542 (todo-forward-item)))))))
2544 (defun todo-set-item-priority (&optional item cat new arg
)
2545 "Prompt for and set ITEM's priority in CATegory.
2547 Interactively, ITEM is the todo item at point, CAT is the current
2548 category, and the priority is a number between 1 and the number
2549 of items in the category. Non-interactively, non-nil NEW means
2550 ITEM is a new item and the lowest priority is one more than the
2551 number of items in CAT.
2553 The new priority is set either interactively by prompt or by a
2554 numerical prefix argument, or noninteractively by argument ARG,
2555 whose value can be either of the symbols `raise' or `lower',
2556 meaning to raise or lower the item's priority by one."
2558 (unless (and (or (called-interactively-p 'any
) (memq arg
'(raise lower
)))
2559 ;; Noop if point is not on a todo (i.e. not done) item.
2560 (or (todo-done-item-p) (looking-at "^$")
2561 ;; On done items separator.
2562 (save-excursion (beginning-of-line)
2563 (looking-at todo-category-done
))))
2564 (let* ((item (or item
(todo-item-string)))
2565 (marked (todo-marked-item-p))
2566 (cat (or cat
(cond ((eq major-mode
'todo-mode
)
2567 (todo-current-category))
2568 ((eq major-mode
'todo-filtered-items-mode
)
2570 (concat todo-date-string-start
2572 "\\( " diary-time-regexp
"\\)?"
2573 (regexp-quote todo-nondiary-end
)
2574 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
2576 (re-search-forward regexp1 nil t
)
2577 (match-string-no-properties 1)))))))
2579 (todo (cond ((or (memq arg
'(raise lower
))
2580 (eq major-mode
'todo-filtered-items-mode
))
2582 (let ((curstart (todo-item-start))
2584 (goto-char (point-min))
2585 (while (looking-at todo-item-start
)
2586 (setq count
(1+ count
))
2587 (when (= (point) curstart
) (setq curnum count
))
2588 (todo-forward-item))
2590 ((eq major-mode
'todo-mode
)
2591 (todo-get-count 'todo cat
))))
2592 (maxnum (if new
(1+ todo
) todo
))
2593 (prompt (format "Set item priority (1-%d): " maxnum
))
2594 (priority (cond ((and (not arg
) (numberp current-prefix-arg
))
2596 ((and (eq arg
'raise
) (>= curnum
1))
2598 ((and (eq arg
'lower
) (<= curnum maxnum
))
2602 (unless (and priority
2603 (or (and (eq arg
'raise
) (zerop priority
))
2604 (and (eq arg
'lower
) (> priority maxnum
))))
2605 ;; When moving item to another category, show the category before
2606 ;; prompting for its priority.
2607 (unless (or arg
(called-interactively-p 'any
))
2608 (todo-category-number cat
)
2609 ;; If done items in category are visible, keep them visible.
2610 (let ((done todo-show-with-done
))
2611 (when (> (buffer-size) (- (point-max) (point-min)))
2613 (goto-char (point-min))
2614 (setq done
(re-search-forward todo-done-string-start nil t
))))
2615 (let ((todo-show-with-done done
))
2616 ;; Keep current item or top of moved to category in view
2617 ;; while setting priority.
2618 (save-excursion (todo-category-select)))))
2619 ;; Prompt for priority only when the category has at least one
2622 (while (not priority
)
2623 (setq candidate
(read-number prompt
2624 (if (eq todo-default-priority
'first
)
2626 (setq prompt
(when (or (< candidate
1) (> candidate maxnum
))
2627 (format "Priority must be an integer between 1 and %d.\n"
2629 (unless prompt
(setq priority candidate
))))
2630 ;; In Top Priorities buffer, an item's priority can be changed
2631 ;; wrt items in another category, but not wrt items in the same
2633 (when (eq major-mode
'todo-filtered-items-mode
)
2634 (let* ((regexp2 (concat todo-date-string-start todo-date-pattern
2635 "\\( " diary-time-regexp
"\\)?"
2636 (regexp-quote todo-nondiary-end
)
2637 "?\\(?1:" (regexp-quote cat
) "\\)"))
2638 (end (cond ((< curnum priority
)
2639 (save-excursion (todo-item-end)))
2640 ((> curnum priority
)
2641 (save-excursion (todo-item-start)))))
2642 (match (save-excursion
2643 (cond ((< curnum priority
)
2644 (todo-forward-item (1+ (- priority curnum
)))
2645 (when (re-search-backward regexp2 end t
)
2646 (match-string-no-properties 1)))
2647 ((> curnum priority
)
2648 (todo-backward-item (- curnum priority
))
2649 (when (re-search-forward regexp2 end t
)
2650 (match-string-no-properties 1)))))))
2652 (user-error (concat "Cannot reprioritize items from the same "
2653 "category in this mode, only in Todo mode")))))
2654 ;; Interactively or with non-nil ARG, relocate the item within its
2656 (when (or arg
(called-interactively-p 'any
))
2658 (goto-char (point-min))
2660 (unless (= priority
1)
2661 (todo-forward-item (1- priority
))
2662 ;; When called from todo-item-undone and the highest priority
2663 ;; is chosen, this advances point to the first done item, so
2664 ;; move it up to the empty line above the done items
2666 (when (looking-back (concat "^"
2667 (regexp-quote todo-category-done
)
2669 (line-beginning-position 0))
2670 (todo-backward-item))))
2671 (todo-insert-with-overlays item
)
2672 ;; If item was marked, restore the mark.
2674 (let* ((ov (todo-get-overlay 'prefix
))
2675 (pref (overlay-get ov
'before-string
)))
2676 (overlay-put ov
'before-string
2677 (concat todo-item-mark pref
))))))))
2679 (defun todo-raise-item-priority ()
2680 "Raise priority of current item by moving it up by one item."
2682 (todo-set-item-priority nil nil nil
'raise
))
2684 (defun todo-lower-item-priority ()
2685 "Lower priority of current item by moving it down by one item."
2687 (todo-set-item-priority nil nil nil
'lower
))
2689 (defun todo-move-item (&optional file
)
2690 "Move at least one todo or done item to another category.
2691 If there are marked items, move all of these; otherwise, move
2694 With prefix argument FILE, prompt for a specific todo file and
2695 choose (with TAB completion) a category in it to move the item or
2696 items to; otherwise, choose and move to any category in either
2697 the current todo file or one of the files in
2698 `todo-category-completions-files'. If the chosen category is
2699 not an existing categories, then it is created and the item(s)
2700 become(s) the first entry/entries in that category.
2702 With moved todo items, prompt to set the priority in the category
2703 moved to (with multiple todo items, the one that had the highest
2704 priority in the category moved from gets the new priority and the
2705 rest of the moved todo items are inserted in sequence below it).
2706 Moved done items are appended to the top of the done items
2707 section in the category moved to."
2709 (let* ((cat1 (todo-current-category))
2710 (marked (assoc cat1 todo-categories-with-marks
)))
2712 ;; Noop if point is not on an item and there are no marked items.
2713 (and (or (looking-at "^$")
2714 ;; On done items separator.
2715 (save-excursion (beginning-of-line)
2716 (looking-at todo-category-done
)))
2718 (let* ((buffer-read-only)
2719 (file1 todo-current-todo-file
)
2720 (item (todo-item-string))
2721 (done-item (and (todo-done-item-p) item
))
2722 (omark (save-excursion (todo-item-start) (point-marker)))
2726 ov cat2 file2 moved nmark todo-items done-items
)
2730 (setq ov
(make-overlay (save-excursion (todo-item-start))
2731 (save-excursion (todo-item-end))))
2732 (overlay-put ov
'face
'todo-search
))
2733 (let* ((pl (if (and marked
(> (cdr marked
) 1)) "s" ""))
2734 (cat+file
(todo-read-category (concat "Move item" pl
2737 (while (and (equal (car cat
+file
) cat1
)
2738 (equal (cdr cat
+file
) file1
))
2739 (setq cat
+file
(todo-read-category
2740 "Choose a different category: ")))
2741 (setq cat2
(car cat
+file
)
2742 file2
(cdr cat
+file
))))
2743 (if ov
(delete-overlay ov
)))
2744 (set-buffer (find-buffer-visiting file1
))
2747 (goto-char (point-min))
2749 (when (todo-marked-item-p)
2750 (if (todo-done-item-p)
2752 (push (todo-item-string) done-items
)
2753 (setq done
(1+ done
)))
2754 (push (todo-item-string) todo-items
)
2755 (setq todo
(1+ todo
))
2756 (when (todo-diary-item-p)
2757 (setq diary
(1+ diary
)))))
2758 (todo-forward-item))
2759 (setq todo-items
(nreverse todo-items
))
2760 (setq done-items
(nreverse done-items
)))
2761 (if (todo-done-item-p)
2763 (push done-item done-items
)
2765 (push item todo-items
)
2767 (when (todo-diary-item-p) (setq diary
1))))
2768 (set-window-buffer (selected-window)
2769 (set-buffer (find-file-noselect file2
'nowarn
)))
2773 (todo-set-item-priority (pop todo-items
) cat2 t
)
2777 (todo-insert-with-overlays (pop todo-items
))))
2778 ;; Move done items en bloc to top of done items section.
2780 (todo-category-number cat2
)
2782 (goto-char (point-min))
2784 (concat "^" (regexp-quote (concat todo-category-beg cat2
)) "$")
2787 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2789 (unless here
(setq here
(point)))
2791 (todo-insert-with-overlays (pop done-items
))
2792 (todo-forward-item)))
2793 ;; If only done items were moved, move point to the top
2794 ;; one, otherwise, move point to the top moved todo item.
2798 ;; Move succeeded, so remove item from starting category,
2799 ;; update item counts and display the category containing
2802 (setq nmark
(point-marker))
2803 (when todo
(todo-update-count 'todo todo
))
2804 (when diary
(todo-update-count 'diary diary
))
2805 (when done
(todo-update-count 'done done
))
2806 (todo-update-categories-sexp)
2807 (with-current-buffer (find-buffer-visiting file1
)
2816 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
2819 (setq end
(if (re-search-forward
2821 (regexp-quote todo-category-beg
))
2824 (goto-char (match-beginning 0))
2826 (point-max-marker)))
2828 (while (< (point) end
)
2829 (if (todo-marked-item-p)
2831 (todo-forward-item)))
2832 (setq todo-categories-with-marks
2833 (assq-delete-all cat1 todo-categories-with-marks
)))
2834 (if ov
(delete-overlay ov
))
2835 (todo-remove-item))))
2836 (when todo
(todo-update-count 'todo
(- todo
) cat1
))
2837 (when diary
(todo-update-count 'diary
(- diary
) cat1
))
2838 (when done
(todo-update-count 'done
(- done
) cat1
))
2839 (todo-update-categories-sexp))
2840 (set-window-buffer (selected-window)
2841 (set-buffer (find-file-noselect file2
'nowarn
)))
2842 (setq todo-category-number
(todo-category-number cat2
))
2843 (let ((todo-show-with-done (> done
0)))
2844 (todo-category-select))
2846 ;; If item is moved to end of (just first?) category, make
2847 ;; sure the items above it are displayed in the window.
2849 ;; User quit before setting priority of todo item(s), so
2850 ;; return to starting category.
2852 (set-window-buffer (selected-window)
2853 (set-buffer (find-file-noselect file1
'nowarn
)))
2854 (todo-category-number cat1
)
2855 (todo-category-select)
2856 (goto-char omark
))))))))
2858 (defun todo-item-done (&optional arg
)
2859 "Tag a todo item in this category as done and relocate it.
2861 With prefix argument ARG prompt for a comment and append it to
2862 the done item; this is only possible if there are no marked
2863 items. If there are marked items, tag all of these with
2864 `todo-done-string' plus the current date and, if
2865 `todo-always-add-time-string' is non-nil, the current time;
2866 otherwise, just tag the item at point. Items tagged as done are
2867 relocated to the category's (by default hidden) done section. If
2868 done items are visible on invoking this command, they remain
2871 (let* ((cat (todo-current-category))
2872 (marked (assoc cat todo-categories-with-marks
)))
2873 (when marked
(todo--user-error-if-marked-done-item))
2875 ;; Noop if point is not on a todo (i.e. not done) item and
2876 ;; there are no marked items.
2877 (and (or (todo-done-item-p) (looking-at "^$")
2878 ;; On done items separator.
2879 (save-excursion (beginning-of-line)
2880 (looking-at todo-category-done
)))
2882 (let* ((date-string (calendar-date-string (calendar-current-date) t t
))
2883 (time-string (if todo-always-add-time-string
2884 (concat " " (substring (current-time-string)
2887 (done-prefix (concat "[" todo-done-string date-string time-string
2889 (comment (and arg
(read-string "Enter a comment: ")))
2892 (show-done (save-excursion
2893 (goto-char (point-min))
2894 (re-search-forward todo-done-string-start nil t
)))
2895 (buffer-read-only nil
)
2896 header item done-items
2898 ;; Don't add empty comment to done item.
2899 (setq comment
(unless (zerop (length comment
))
2900 (concat " [" todo-comment-string
": " comment
"]")))
2901 (and marked
(goto-char (point-min)))
2902 (setq header
(todo-get-overlay 'header
))
2904 ;; Stop looping when we hit the empty line below the last
2905 ;; todo item (this is eobp if only done items are hidden).
2906 (while (not (looking-at "^$"))
2907 (if (or (not marked
) (and marked
(todo-marked-item-p)))
2909 (setq item
(todo-item-string))
2910 (push (concat done-prefix item comment
) done-items
)
2911 (setq item-count
(1+ item-count
))
2912 (when (todo-diary-item-p)
2913 (setq diary-count
(1+ diary-count
)))
2915 (unless marked
(throw 'done nil
)))
2916 (todo-forward-item))))
2917 (setq done-items
(nreverse done-items
))
2919 (setq todo-categories-with-marks
2920 (assq-delete-all cat todo-categories-with-marks
)))
2924 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2926 (when show-done
(setq opoint
(point)))
2928 (insert (pop done-items
) "\n")
2929 (when header
(let ((copy (copy-overlay header
)))
2931 (concat todo-item-start
2932 "\\( " diary-time-regexp
"\\)?"
2933 (regexp-quote todo-nondiary-end
) "? ")
2935 (move-overlay copy
(match-beginning 0) (match-end 0)))
2938 (todo-update-count 'todo
(- item-count
))
2939 (todo-update-count 'done item-count
)
2940 (todo-update-count 'diary
(- diary-count
))
2941 (todo-update-categories-sexp)
2942 (let ((todo-show-with-done show-done
))
2943 (todo-category-select)
2944 ;; When done items are visible, put point at the top of the
2945 ;; done items section. When done items are hidden, restore
2946 ;; point to its location prior to invoking this command.
2947 (when opoint
(goto-char opoint
)))))))
2949 (defun todo-item-undone ()
2950 "Restore at least one done item to this category's todo section.
2951 Prompt for the new priority. If there are marked items, undo all
2952 of these, giving the first undone item the new priority and the
2953 rest following directly in sequence; otherwise, undo just the
2956 If the done item has a comment, ask whether to omit the comment
2957 from the restored item. With multiple marked done items with
2958 comments, only ask once, and if affirmed, omit subsequent
2959 comments without asking."
2961 (let* ((cat (todo-current-category))
2962 (marked (assoc cat todo-categories-with-marks
))
2963 (pl (if (and marked
(> (cdr marked
) 1)) "s" "")))
2964 (when (or marked
(todo-done-item-p))
2965 (let ((buffer-read-only)
2967 (omark (point-marker))
2971 start end item ov npoint undone
)
2972 (and marked
(goto-char (point-min)))
2975 (when (or (not marked
) (and marked
(todo-marked-item-p)))
2976 (if (not (todo-done-item-p))
2979 (user-error "Only done items can be undone"))
2982 (setq ov
(make-overlay (save-excursion (todo-item-start))
2983 (save-excursion (todo-item-end))))
2984 (overlay-put ov
'face
'todo-search
))
2985 ;; Find the end of the date string added upon tagging item as
2987 (setq start
(search-forward "] "))
2988 (setq item-count
(1+ item-count
))
2989 (unless (looking-at (regexp-quote todo-nondiary-start
))
2990 (setq diary-count
(1+ diary-count
)))
2991 (setq end
(save-excursion (todo-item-end)))
2992 ;; Ask (once) whether to omit done item's comment. If
2993 ;; affirmed, omit subsequent comments without asking.
2994 (when (re-search-forward
2995 (concat " \\[" (regexp-quote todo-comment-string
)
2999 (if (eq first
'first
)
3001 (if (eq todo-undo-item-omit-comment
'ask
)
3002 (when (todo-y-or-n-p
3003 (concat "Omit comment" pl
3004 " from restored item"
3007 (when todo-undo-item-omit-comment
'omit
)))
3009 (when (and (eq first
'first
) ov
) (delete-overlay ov
)))
3010 (when (eq first
'omit
)
3011 (setq end
(match-beginning 0))))
3012 (setq item
(concat item
3013 (buffer-substring-no-properties start end
)
3014 (when marked
"\n")))
3015 (unless marked
(throw 'done nil
))))
3016 (todo-forward-item)))
3019 ;; Chop off last newline of multiple items string, since
3020 ;; it will be reinserted on setting priority.
3021 (and marked
(setq item
(substring item
0 -
1)))
3022 (todo-set-item-priority item cat t
)
3023 (setq npoint
(point))
3025 (when ov
(delete-overlay ov
))
3032 (concat "^" (regexp-quote todo-category-done
)) nil t
)
3034 (if (todo-marked-item-p)
3036 (todo-forward-item)))
3037 (setq todo-categories-with-marks
3038 (assq-delete-all cat todo-categories-with-marks
)))
3041 (todo-update-count 'todo item-count
)
3042 (todo-update-count 'done
(- item-count
))
3043 (when diary-count
(todo-update-count 'diary diary-count
))
3044 (todo-update-categories-sexp)
3045 (let ((todo-show-with-done (> (todo-get-count 'done
) 0)))
3046 (todo-category-select))
3047 ;; Put cursor on undone item.
3048 (goto-char npoint
)))
3049 (set-marker omark nil
)))))
3051 ;; -----------------------------------------------------------------------------
3052 ;;; Done item archives
3053 ;; -----------------------------------------------------------------------------
3055 (defun todo-find-archive (&optional ask
)
3056 "Visit the archive of the current todo category, if it exists.
3057 If the category has no archived items, prompt to visit the
3058 archive anyway. If there is no archive for this file or with
3059 non-nil argument ASK, prompt to visit another archive.
3061 The buffer showing the archive is in Todo Archive mode. The
3062 first visit in a session displays the first category in the
3063 archive, subsequent visits return to the last category
3066 (if (null (funcall todo-files-function t
))
3067 (message "There are no archive files")
3068 (let* ((cat (todo-current-category))
3069 (count (todo-get-count 'archived cat
))
3070 (archive (concat (file-name-sans-extension todo-current-todo-file
)
3072 (place (cond (ask 'other-archive
)
3073 ((file-exists-p archive
) 'this-archive
)
3074 (t (when (todo-y-or-n-p
3075 (concat "This file has no archive; "
3076 "visit another archive? "))
3078 (when (eq place
'other-archive
)
3079 (setq archive
(todo-read-file-name "Choose a todo archive: " t t
)))
3080 (when (and (eq place
'this-archive
) (zerop count
))
3081 (setq place
(when (todo-y-or-n-p
3082 (concat "This category has no archived items;"
3083 " visit archive anyway? "))
3086 (set-window-buffer (selected-window)
3087 (set-buffer (find-file-noselect archive
)))
3088 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
3089 (if (member place
'(other-archive other-cat
))
3090 (setq todo-category-number
1)
3091 (todo-category-number cat
))
3092 (todo-category-select)))))
3094 (defun todo-choose-archive ()
3095 "Choose an archive and visit it."
3097 (todo-find-archive t
))
3099 (defun todo-archive-done-item (&optional all
)
3100 "Archive at least one done item in this category.
3102 With prefix argument ALL, prompt whether to archive all done
3103 items in this category and on confirmation archive them.
3104 Otherwise, if there are marked done items (and no marked todo
3105 items), archive all of these; otherwise, archive the done item at
3108 If the archive of this file does not exist, it is created. If
3109 this category does not exist in the archive, it is created."
3111 (when (eq major-mode
'todo-mode
)
3112 (if (and all
(zerop (todo-get-count 'done
)))
3113 (message "No done items in this category")
3115 (let* ((cat (todo-current-category))
3116 (tbuf (current-buffer))
3117 (marked (assoc cat todo-categories-with-marks
))
3118 (afile (concat (file-name-sans-extension
3119 todo-current-todo-file
) ".toda"))
3120 (archive (find-file-noselect afile t
))
3121 (item (and (not marked
) (todo-done-item-p)
3122 (concat (todo-item-string) "\n")))
3124 (opoint (unless (todo-done-item-p) (point)))
3125 marked-items beg end all-done
3129 (if (todo-y-or-n-p "Archive all done items in this category? ")
3132 (goto-char (point-min))
3135 (re-search-forward todo-done-string-start
3137 (match-beginning 0))
3138 end
(if (re-search-forward
3140 (regexp-quote todo-category-beg
))
3144 all-done
(buffer-substring-no-properties beg end
)
3145 count
(todo-get-count 'done
))
3146 ;; Restore starting point, unless it was on a done
3147 ;; item, since they will all be deleted.
3148 (when opoint
(goto-char opoint
))))
3152 (goto-char (point-min))
3154 (when (todo-marked-item-p)
3155 (if (not (todo-done-item-p))
3156 (throw 'end
(message "Only done items can be archived"))
3158 (concat marked-items
(todo-item-string) "\n"))
3159 (setq count
(1+ count
))))
3160 (todo-forward-item)))))
3161 (if (not (or marked all item
))
3162 (throw 'end
(message "Only done items can be archived"))
3163 (with-current-buffer archive
3164 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
3165 (let ((headers-hidden todo--item-headers-hidden
)
3167 (if headers-hidden
(todo-toggle-item-header))
3169 (goto-char (point-min))
3170 (if (and (re-search-forward
3171 (concat "^" (regexp-quote
3172 (concat todo-category-beg cat
)) "$")
3174 (re-search-forward (regexp-quote todo-category-done
)
3176 ;; Start of done items section in existing category.
3178 (todo-add-category nil cat
)
3179 ;; Start of done items section in new category.
3180 (goto-char (point-max)))
3181 (insert (cond (marked marked-items
)
3184 (todo-update-count 'done
(if (or marked all
) count
1) cat
)
3185 (todo-update-categories-sexp)
3186 ;; If archive is new, save to file now (with
3187 ;; write-region to avoid prompt for file to save to)
3188 ;; to update todo-archives, and set the mode for
3189 ;; visiting the archive below.
3190 (unless (nth 7 (file-attributes afile
))
3191 (write-region nil nil afile t t
)
3192 (setq todo-archives
(funcall todo-files-function t
))
3193 (todo-archive-mode))
3194 (if headers-hidden
(todo-toggle-item-header))))
3195 (with-current-buffer tbuf
3200 ;; Make sure done items are accessible.
3202 (remove-overlays beg end
)
3203 (delete-region beg end
)
3204 (todo-update-count 'done
(- count
))
3205 (todo-update-count 'archived count
))))
3207 ;; If we're archiving all done items, can't
3208 ;; first archive item point was on, since
3209 ;; that will short-circuit the rest.
3210 (and item
(not all
)))
3211 (and marked
(goto-char (point-min)))
3214 (if (or (and marked
(todo-marked-item-p)) item
)
3217 (todo-update-count 'done -
1)
3218 (todo-update-count 'archived
1)
3219 ;; Don't leave point below last item.
3220 (and (or marked item
) (bolp) (eolp)
3221 (< (point-min) (point-max))
3222 (todo-backward-item))
3224 (throw 'done
(setq item nil
))))
3225 (todo-forward-item))))))
3227 (setq todo-categories-with-marks
3228 (assq-delete-all cat todo-categories-with-marks
)))
3229 (todo-update-categories-sexp)
3230 (todo-prefix-overlays)))
3232 (todo-category-number cat
)
3233 (todo-category-select)
3234 (split-window-below)
3235 (set-window-buffer (selected-window) tbuf
)
3236 ;; Make todo file current to select category.
3237 (find-file (buffer-file-name tbuf
))
3238 ;; Make sure done item separator is hidden (if done items
3239 ;; were initially visible).
3240 (let (todo-show-with-done) (todo-category-select)))))))
3242 (defun todo-unarchive-items ()
3243 "Unarchive at least one item in this archive category.
3244 If there are marked items, unarchive all of these; otherwise,
3245 unarchive the item at point.
3247 Unarchived items are restored as done items to the corresponding
3248 category in the todo file, inserted at the top of done items
3249 section. If all items in the archive category have been
3250 restored, the category is deleted from the archive. If this was
3251 the only category in the archive, the archive file is deleted."
3253 (when (eq major-mode
'todo-archive-mode
)
3254 (let* ((cat (todo-current-category))
3255 (tbuf (find-file-noselect
3256 (concat (file-name-sans-extension todo-current-todo-file
)
3259 (marked (assoc cat todo-categories-with-marks
))
3260 (item (concat (todo-item-string) "\n"))
3266 (goto-char (point-min))
3268 (when (todo-marked-item-p)
3269 (setq marked-items
(concat marked-items
(todo-item-string) "\n"))
3270 (setq marked-count
(1+ marked-count
)))
3271 (todo-forward-item))))
3272 ;; Restore items to top of category's done section and update counts.
3273 (with-current-buffer tbuf
3274 (let ((headers-hidden todo--item-headers-hidden
)
3275 buffer-read-only newcat
)
3276 (if headers-hidden
(todo-toggle-item-header))
3278 (goto-char (point-min))
3279 ;; Find the corresponding todo category, or if there isn't
3281 (unless (re-search-forward
3282 (concat "^" (regexp-quote (concat todo-category-beg cat
))
3285 (todo-add-category nil cat
)
3287 ;; Go to top of category's done section.
3289 (concat "^" (regexp-quote todo-category-done
)) nil t
)
3292 (insert marked-items
)
3293 (todo-update-count 'done marked-count cat
)
3294 (unless newcat
; Newly added category has no archive.
3295 (todo-update-count 'archived
(- marked-count
) cat
)))
3298 (todo-update-count 'done
1 cat
)
3299 (unless newcat
; Newly added category has no archive.
3300 (todo-update-count 'archived -
1 cat
))))
3301 (if headers-hidden
(todo-toggle-item-header))
3302 (todo-update-categories-sexp)))
3303 ;; Delete restored items from archive.
3306 (goto-char (point-min)))
3309 (if (or (todo-marked-item-p) item
)
3313 (throw 'done
(setq item nil
))))
3314 (todo-forward-item))))
3315 (todo-update-count 'done
(if marked
(- marked-count
) -
1) cat
)
3316 ;; If we unarchived the last item in category, then if that was
3317 ;; the only category, delete the whole file, otherwise, just
3318 ;; delete the category.
3319 (when (= 0 (todo-get-count 'done
))
3320 (if (= 1 (length todo-categories
))
3322 (delete-file todo-current-todo-file
)
3323 ;; Kill the archive buffer silently.
3324 (set-buffer-modified-p nil
)
3327 (let ((beg (re-search-backward
3328 (concat "^" (regexp-quote todo-category-beg
) cat
"$")
3330 (end (if (re-search-forward
3331 (concat "^" (regexp-quote todo-category-beg
))
3335 (remove-overlays beg end
)
3336 (delete-region beg end
)
3337 (setq todo-categories
(delete (assoc cat todo-categories
)
3338 todo-categories
)))))
3339 (todo-update-categories-sexp)
3340 ;; Visit category in todo file and show restored done items.
3341 (let ((tfile (buffer-file-name tbuf
))
3342 (todo-show-with-done t
))
3343 (set-window-buffer (selected-window)
3344 (set-buffer (find-file-noselect tfile
)))
3345 (todo-category-number cat
)
3346 (todo-category-select)
3347 ;; Selecting the category leaves point at the end of the done
3348 ;; items separator string, so move it to the (first) restored
3351 (message "Items unarchived.")))))
3353 (defun todo-jump-to-archive-category (&optional file
)
3354 "Prompt for a category in a todo archive and jump to it.
3355 With prefix argument FILE, prompt for an archive and choose (with
3356 TAB completion) a category in it to jump to; otherwise, choose
3357 and jump to any category in the current archive."
3359 (todo-jump-to-category file
'archive
))
3361 ;; -----------------------------------------------------------------------------
3362 ;;; Displaying and sorting tables of categories
3363 ;; -----------------------------------------------------------------------------
3365 (defcustom todo-categories-category-label
"Category"
3366 "Category button label in Todo Categories mode."
3368 :group
'todo-categories
)
3370 (defcustom todo-categories-todo-label
"Todo"
3371 "Todo button label in Todo Categories mode."
3373 :group
'todo-categories
)
3375 (defcustom todo-categories-diary-label
"Diary"
3376 "Diary button label in Todo Categories mode."
3378 :group
'todo-categories
)
3380 (defcustom todo-categories-done-label
"Done"
3381 "Done button label in Todo Categories mode."
3383 :group
'todo-categories
)
3385 (defcustom todo-categories-archived-label
"Archived"
3386 "Archived button label in Todo Categories mode."
3388 :group
'todo-categories
)
3390 (defcustom todo-categories-totals-label
"Totals"
3391 "String to label total item counts in Todo Categories mode."
3393 :group
'todo-categories
)
3395 (defcustom todo-categories-number-separator
" | "
3396 "String between number and category in Todo Categories mode.
3397 This separates the number from the category name in the default
3398 categories display according to priority."
3400 :group
'todo-categories
)
3402 (defcustom todo-categories-align
'center
3403 "Alignment of category names in Todo Categories mode."
3404 :type
'(radio (const left
) (const center
) (const right
))
3405 :group
'todo-categories
)
3407 (defun todo-show-categories-table ()
3408 "Display a table of the current file's categories and item counts.
3410 In the initial display the lines of the table are numbered,
3411 indicating the current order of the categories when sequentially
3412 navigating through the todo file with `\\[todo-forward-category]'
3413 and `\\[todo-backward-category]'. You can reorder the lines, and
3414 hence the category sequence, by typing `\\[todo-raise-category]'
3415 or `\\[todo-lower-category]' to raise or lower the category at
3416 point, or by typing `\\[todo-set-category-number]' and entering a
3417 number at the prompt or by typing `\\[todo-set-category-number]'
3418 with a numeric prefix. If you save the todo file after
3419 reordering the categories, the new order persists in subsequent
3422 The labels above the category names and item counts are buttons,
3423 and clicking these changes the display: sorted by category name
3424 or by the respective item counts (alternately descending or
3425 ascending). In these displays the categories are not numbered
3426 and `\\[todo-set-category-number]', `\\[todo-raise-category]' and
3427 `\\[todo-lower-category]' are disabled. (Programmatically, the
3428 sorting is triggered by passing a non-nil SORTKEY argument.)
3430 In addition, the lines with the category names and item counts
3431 are buttonized, and pressing one of these button jumps to the
3432 category in Todo mode (or Todo Archive mode, for categories
3433 containing only archived items, provided user option
3434 `todo-skip-archived-categories' is non-nil. These categories
3435 are shown in `todo-archived-only' face."
3437 (todo-display-categories)
3439 (todo-update-categories-display sortkey
)))
3441 (defun todo-next-button (n)
3442 "Move point to the Nth next button in the table of categories."
3444 (forward-button n
'wrap
'display-message
)
3445 (and (bolp) (button-at (point))
3446 ;; Align with beginning of category label.
3447 (forward-char (+ 4 (length todo-categories-number-separator
)))))
3449 (defun todo-previous-button (n)
3450 "Move point to the Nth previous button in the table of categories."
3452 (backward-button n
'wrap
'display-message
)
3453 (and (bolp) (button-at (point))
3454 ;; Align with beginning of category label.
3455 (forward-char (+ 4 (length todo-categories-number-separator
)))))
3457 (defun todo-set-category-number (&optional arg
)
3458 "Change number of category at point in the table of categories.
3460 With ARG nil, prompt for the new number. Alternatively, the
3461 enter the new number with numerical prefix ARG. Otherwise, if
3462 ARG is either of the symbols `raise' or `lower', raise or lower
3463 the category line in the table by one, respectively, thereby
3464 decreasing or increasing its number."
3466 (let ((curnum (save-excursion
3467 ;; Get the number representing the priority of the category
3468 ;; on the current line.
3469 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
3470 (when curnum
; Do nothing if we're not on a category line.
3471 (let* ((maxnum (length todo-categories
))
3472 (prompt (format "Set category priority (1-%d): " maxnum
))
3473 (col (current-column))
3474 (buffer-read-only nil
)
3475 (priority (cond ((and (eq arg
'raise
) (> curnum
1))
3477 ((and (eq arg
'lower
) (< curnum maxnum
))
3480 (while (not priority
)
3481 (setq candidate
(or arg
(read-number prompt
)))
3484 (cond ((or (< candidate
1) (> candidate maxnum
))
3485 (format "Priority must be an integer between 1 and %d: "
3487 ((= candidate curnum
)
3488 "Choose a different priority than the current one: ")))
3489 (unless prompt
(setq priority candidate
)))
3490 (let* ((lower (< curnum priority
)) ; Priority is being lowered.
3491 (head (butlast todo-categories
3492 (funcall (if lower
#'identity
#'1+)
3493 (- maxnum priority
))))
3494 (tail (nthcdr (funcall (if lower
#'identity
#'1-
) priority
)
3496 ;; Category's name and items counts list.
3497 (catcons (nth (1- curnum
) todo-categories
))
3498 (todo-categories (nconc head
(list catcons
) tail
))
3500 (when lower
(setq todo-categories
(nreverse todo-categories
)))
3501 (setq todo-categories
(delete-dups todo-categories
))
3502 (when lower
(setq todo-categories
(nreverse todo-categories
)))
3503 (setq newcats todo-categories
)
3505 (with-current-buffer (find-buffer-visiting todo-current-todo-file
)
3506 (setq todo-categories newcats
)
3507 (todo-update-categories-sexp))
3508 (todo-show-categories-table)
3509 (forward-line (1+ priority
))
3510 (forward-char col
))))))
3512 (defun todo-raise-category ()
3513 "Raise priority of category at point in the table of categories."
3515 (todo-set-category-number 'raise
))
3517 (defun todo-lower-category ()
3518 "Lower priority of category at point in the table of categories."
3520 (todo-set-category-number 'lower
))
3522 (defun todo-sort-categories-alphabetically-or-numerically ()
3523 "Sort table of categories alphabetically or numerically."
3526 (goto-char (point-min))
3528 (if (member 'alpha todo-descending-counts
)
3530 (todo-update-categories-display nil
)
3531 (setq todo-descending-counts
3532 (delete 'alpha todo-descending-counts
)))
3533 (todo-update-categories-display 'alpha
))))
3535 (defun todo-sort-categories-by-todo ()
3536 "Sort table of categories by number of todo items."
3539 (goto-char (point-min))
3541 (todo-update-categories-display 'todo
)))
3543 (defun todo-sort-categories-by-diary ()
3544 "Sort table of categories by number of diary items."
3547 (goto-char (point-min))
3549 (todo-update-categories-display 'diary
)))
3551 (defun todo-sort-categories-by-done ()
3552 "Sort table of categories by number of non-archived done items."
3555 (goto-char (point-min))
3557 (todo-update-categories-display 'done
)))
3559 (defun todo-sort-categories-by-archived ()
3560 "Sort table of categories by number of archived items."
3563 (goto-char (point-min))
3565 (todo-update-categories-display 'archived
)))
3567 (defvar todo-categories-buffer
"*Todo Categories*"
3568 "Name of buffer in Todo Categories mode.")
3570 (defun todo-longest-category-name-length (categories)
3571 "Return the length of the longest name in list CATEGORIES."
3573 (dolist (c categories longest
)
3574 (setq longest
(max longest
(length c
))))))
3576 (defun todo-adjusted-category-label-length ()
3577 "Return adjusted length of category label button.
3578 The adjustment ensures proper tabular alignment in Todo
3580 (let* ((categories (mapcar #'car todo-categories
))
3581 (longest (todo-longest-category-name-length categories
))
3582 (catlablen (length todo-categories-category-label
))
3583 (lc-diff (- longest catlablen
)))
3584 (if (and (natnump lc-diff
) (cl-oddp lc-diff
))
3586 (max longest catlablen
))))
3588 (defun todo-padded-string (str)
3589 "Return category name or label string STR padded with spaces.
3590 The placement of the padding is determined by the value of user
3591 option `todo-categories-align'."
3592 (let* ((len (todo-adjusted-category-label-length))
3593 (strlen (length str
))
3594 (strlen-odd (eq (logand strlen
1) 1))
3595 (padding (max 0 (/ (- len strlen
) 2)))
3596 (padding-left (cond ((eq todo-categories-align
'left
) 0)
3597 ((eq todo-categories-align
'center
) padding
)
3598 ((eq todo-categories-align
'right
)
3599 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
3600 (padding-right (cond ((eq todo-categories-align
'left
)
3601 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
3602 ((eq todo-categories-align
'center
)
3603 (if strlen-odd
(1+ padding
) padding
))
3604 ((eq todo-categories-align
'right
) 0))))
3605 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
3607 (defvar todo-descending-counts nil
3608 "List of keys for category counts sorted in descending order.")
3610 (defun todo-sort (list &optional key
)
3611 "Return a copy of LIST, possibly sorted according to KEY."
3612 (let* ((l (copy-sequence list
))
3613 (fn (if (eq key
'alpha
)
3614 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
3615 (lambda (x) (todo-get-count key x
))))
3616 ;; Keep track of whether the last sort by key was descending or
3618 (descending (member key todo-descending-counts
))
3619 (cmp (if (eq key
'alpha
)
3621 (if descending
'< '>)))
3622 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
3623 (t2 (funcall fn
(car s2
))))
3624 (funcall cmp t1 t2
)))))
3626 (setq l
(sort l pred
))
3627 ;; Switch between descending and ascending sort order.
3629 (setq todo-descending-counts
3630 (delete key todo-descending-counts
))
3631 (push key todo-descending-counts
)))
3634 (defun todo-display-sorted (type)
3635 "Keep point on the TYPE count sorting button just clicked."
3636 (let ((opoint (point)))
3637 (todo-update-categories-display type
)
3638 (goto-char opoint
)))
3640 (defun todo-label-to-key (label)
3641 "Return symbol for sort key associated with LABEL."
3643 (cond ((string= label todo-categories-category-label
)
3645 ((string= label todo-categories-todo-label
)
3647 ((string= label todo-categories-diary-label
)
3649 ((string= label todo-categories-done-label
)
3651 ((string= label todo-categories-archived-label
)
3652 (setq key
'archived
)))
3655 (defun todo-insert-sort-button (label)
3656 "Insert button for displaying categories sorted by item counts.
3657 LABEL determines which type of count is sorted."
3658 (let* ((str (if (string= label todo-categories-category-label
)
3659 (todo-padded-string label
)
3662 (end (+ beg
(length str
)))
3664 (insert-button str
'face nil
3667 (let ((key (todo-label-to-key label
)))
3668 (if (and (member key todo-descending-counts
)
3671 ;; If display is alphabetical, switch back to
3672 ;; category priority order.
3673 (todo-display-sorted nil
)
3674 (setq todo-descending-counts
3675 (delete key todo-descending-counts
)))
3676 (todo-display-sorted key
)))))
3677 (setq ov
(make-overlay beg end
))
3678 (overlay-put ov
'face
'todo-button
)))
3680 (defun todo-total-item-counts ()
3681 "Return a list of total item counts for the current file."
3682 (mapcar (lambda (i) (apply #'+ (mapcar (lambda (x) (aref (cdr x
) i
))
3686 (defvar todo-categories-category-number
0
3687 "Variable for numbering categories in Todo Categories mode.")
3689 (defun todo-insert-category-line (cat &optional nonum
)
3690 "Insert button with category CAT's name and item counts.
3691 With non-nil argument NONUM show only these; otherwise, insert a
3692 number in front of the button indicating the category's priority.
3693 The number and the category name are separated by the string
3694 which is the value of the user option
3695 `todo-categories-number-separator'."
3696 (let ((archive (member todo-current-todo-file todo-archives
))
3697 (num todo-categories-category-number
)
3698 (str (todo-padded-string cat
))
3700 (setq num
(1+ num
) todo-categories-category-number num
)
3703 (make-string (+ 4 (length todo-categories-number-separator
))
3705 (format " %3d%s" num todo-categories-number-separator
))
3707 (mapconcat (lambda (elt)
3709 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
3710 (format "%3d" (todo-get-count (cdr elt
) cat
)) ; count
3711 ;; Add an extra space if label length is odd.
3712 (when (cl-oddp (length (car elt
))) " ")))
3714 (list (cons todo-categories-done-label
'done
))
3715 (list (cons todo-categories-todo-label
'todo
)
3716 (cons todo-categories-diary-label
'diary
)
3717 (cons todo-categories-done-label
'done
)
3718 (cons todo-categories-archived-label
3721 " ") ; Make highlighting on last column look better.
3722 'face
(if (and todo-skip-archived-categories
3723 (zerop (todo-get-count 'todo cat
))
3724 (zerop (todo-get-count 'done cat
))
3725 (not (zerop (todo-get-count 'archived cat
))))
3728 'action
(lambda (_button)
3729 (let ((buf (current-buffer)))
3730 (todo-jump-to-category nil cat
)
3731 (kill-buffer buf
))))
3732 ;; Highlight the sorted count column.
3733 (let* ((beg (+ opoint
7 (length str
)))
3735 (cond ((eq nonum
'todo
)
3736 (setq beg
(+ beg
1 (/ (length todo-categories-todo-label
) 2))))
3738 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3739 2 (/ (length todo-categories-diary-label
) 2))))
3741 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3742 2 (length todo-categories-diary-label
)
3743 2 (/ (length todo-categories-done-label
) 2))))
3744 ((eq nonum
'archived
)
3745 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3746 2 (length todo-categories-diary-label
)
3747 2 (length todo-categories-done-label
)
3748 2 (/ (length todo-categories-archived-label
) 2)))))
3749 (unless (= beg
(+ opoint
7 (length str
))) ; Don't highlight categories.
3750 (setq end
(+ beg
4))
3751 (setq ovl
(make-overlay beg end
))
3752 (overlay-put ovl
'face
'todo-sorted-column
)))
3755 (defun todo-display-categories ()
3756 "Prepare buffer for displaying table of categories and item counts."
3757 (unless (eq major-mode
'todo-categories-mode
)
3758 (setq todo-global-current-todo-file
3759 (or todo-current-todo-file
3760 (todo-absolute-file-name todo-default-todo-file
)))
3761 (set-window-buffer (selected-window)
3762 (set-buffer (get-buffer-create todo-categories-buffer
)))
3763 (kill-all-local-variables)
3764 (todo-categories-mode)
3765 (let ((archive (member todo-current-todo-file todo-archives
))
3768 (insert (format (concat "Category counts for todo "
3769 (if archive
"archive" "file")
3771 (todo-short-file-name todo-current-todo-file
)))
3773 ;; Make space for the column of category numbers.
3774 (insert (make-string (+ 4 (length todo-categories-number-separator
)) 32))
3775 ;; Add the category and item count buttons (if this is the list of
3776 ;; categories in an archive, show only done item counts).
3777 (todo-insert-sort-button todo-categories-category-label
)
3780 (insert (make-string 3 32))
3781 (todo-insert-sort-button todo-categories-done-label
))
3782 (insert (make-string 3 32))
3783 (todo-insert-sort-button todo-categories-todo-label
)
3784 (insert (make-string 2 32))
3785 (todo-insert-sort-button todo-categories-diary-label
)
3786 (insert (make-string 2 32))
3787 (todo-insert-sort-button todo-categories-done-label
)
3788 (insert (make-string 2 32))
3789 (todo-insert-sort-button todo-categories-archived-label
))
3792 (defun todo-update-categories-display (sortkey)
3793 "Populate table of categories and sort by SORTKEY."
3794 (let* ((cats0 todo-categories
)
3795 (cats (todo-sort cats0 sortkey
))
3796 (archive (member todo-current-todo-file todo-archives
))
3797 (todo-categories-category-number 0)
3798 ;; Find start of Category button if we just entered Todo Categories
3800 (pt (if (eq (point) (point-max))
3803 (goto-char (next-single-char-property-change
3804 (point) 'face nil
(line-end-position))))))
3807 (delete-region (point) (point-max))
3808 ;; Fill in the table with buttonized lines, each showing a category and
3811 (todo-insert-category-line (car cat
) sortkey
))
3813 ;; Add a line showing item count totals.
3814 (insert (make-string (+ 4 (length todo-categories-number-separator
)) 32)
3815 (todo-padded-string todo-categories-totals-label
)
3819 (make-string (1+ (/ (length (car elt
)) 2)) 32)
3820 (format "%3d" (nth (cdr elt
) (todo-total-item-counts)))
3821 ;; Add an extra space if label length is odd.
3822 (when (cl-oddp (length (car elt
))) " ")))
3824 (list (cons todo-categories-done-label
2))
3825 (list (cons todo-categories-todo-label
0)
3826 (cons todo-categories-diary-label
1)
3827 (cons todo-categories-done-label
2)
3828 (cons todo-categories-archived-label
3)))
3830 ;; Put cursor on Category button initially.
3831 (if pt
(goto-char pt
))
3832 (setq buffer-read-only t
)))
3834 ;; -----------------------------------------------------------------------------
3835 ;;; Searching and item filtering
3836 ;; -----------------------------------------------------------------------------
3838 (defun todo-search ()
3839 "Search for a regular expression in this todo file.
3840 The search runs through the whole file and encompasses all and
3841 only todo and done items; it excludes category names. Multiple
3842 matches are shown sequentially, highlighted in `todo-search'
3845 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3847 matches match cat in-done ov mlen msg
)
3849 (goto-char (point-min))
3851 (setq match
(re-search-forward regex nil t
))
3852 (if (and match transient-mark-mode
) (deactivate-mark))
3853 (goto-char (line-beginning-position))
3854 (unless (or (equal (point) 1)
3855 (looking-at (concat "^" (regexp-quote todo-category-beg
))))
3856 (if match
(push match matches
)))
3858 (setq matches
(reverse matches
))
3862 (setq match
(pop matches
))
3865 (when (looking-at todo-done-string-start
)
3867 (re-search-backward (concat "^" (regexp-quote todo-category-beg
)
3870 (setq cat
(match-string-no-properties 1))
3871 (todo-category-number cat
)
3872 (todo-category-select)
3874 (unless todo-show-with-done
(todo-toggle-view-done-items)))
3876 (setq ov
(make-overlay (- (point) (length regex
)) (point)))
3877 (overlay-put ov
'face
'todo-search
)
3879 (setq mlen
(length matches
))
3882 (format "There are %d more matches; go to next match? "
3884 "There is one more match; go to it? "))
3886 (throw 'stop
(setq msg
(if (> mlen
1)
3887 (format "There are %d more matches."
3889 "There is one more match."))))))
3890 (setq msg
"There are no more matches."))
3891 (todo-category-select)
3893 (message "No match for \"%s\"" regex
))
3895 (if (todo-y-or-n-p (concat msg
"\nUnhighlight matches? "))
3896 (todo-clear-matches)
3897 (message "You can unhighlight the matches later by typing %s"
3898 (key-description (car (where-is-internal
3899 'todo-clear-matches
))))))))
3901 (defun todo-clear-matches ()
3902 "Remove highlighting on matches found by todo-search."
3904 (remove-overlays 1 (1+ (buffer-size)) 'face
'todo-search
))
3906 (defcustom todo-top-priorities-overrides nil
3907 "List of rules specifying number of top priority items to show.
3908 These rules override `todo-top-priorities' on invocations of
3909 `\\[todo-filter-top-priorities]' and
3910 `\\[todo-filter-top-priorities-multifile]'. Each rule is a list
3911 of the form (FILE NUM ALIST), where FILE is a member of
3912 `todo-files', NUM is a number specifying the default number of
3913 top priority items for each category in that file, and ALIST,
3914 when non-nil, consists of conses of a category name in FILE and a
3915 number specifying the default number of top priority items in
3916 that category, which overrides NUM.
3918 This variable should be set interactively by
3919 `\\[todo-set-top-priorities-in-file]' or
3920 `\\[todo-set-top-priorities-in-category]'."
3922 :group
'todo-filtered
)
3924 (defcustom todo-top-priorities
1
3925 "Default number of top priorities shown by `todo-filter-top-priorities'."
3927 :group
'todo-filtered
)
3929 (defcustom todo-filter-files nil
3930 "List of default files for multifile item filtering."
3931 :type
`(set ,@(todo--files-type-list))
3932 :group
'todo-filtered
)
3934 (defcustom todo-filter-done-items nil
3935 "Non-nil to include done items when processing regexp filters.
3936 Done items from corresponding archive files are also included."
3938 :group
'todo-filtered
)
3940 (defun todo-set-top-priorities-in-file ()
3941 "Set number of top priorities for this file.
3942 See `todo-set-top-priorities' for more details."
3944 (todo-set-top-priorities))
3946 (defun todo-set-top-priorities-in-category ()
3947 "Set number of top priorities for this category.
3948 See `todo-set-top-priorities' for more details."
3950 (todo-set-top-priorities t
))
3952 (defun todo-filter-top-priorities (&optional arg
)
3953 "Display a list of top priority items from different categories.
3954 The categories can be any of those in the current todo file.
3956 With numerical prefix ARG show at most ARG top priority items
3957 from each category. With `C-u' as prefix argument show the
3958 numbers of top priority items specified by category in
3959 `todo-top-priorities-overrides', if this has an entry for the file(s);
3960 otherwise show `todo-top-priorities' items per category in the
3961 file(s). With no prefix argument, if a top priorities file for
3962 the current todo file has previously been saved (see
3963 `todo-save-filtered-items-buffer'), visit this file; if there is
3964 no such file, build the list as with prefix argument `C-u'.
3966 The prefix ARG regulates how many top priorities from
3967 each category to show, as described above."
3969 (todo-filter-items 'top arg
))
3971 (defun todo-filter-top-priorities-multifile (&optional arg
)
3972 "Display a list of top priority items from different categories.
3973 The categories are a subset of the categories in the files listed
3974 in `todo-filter-files', or if this nil, in the files chosen from
3975 a file selection dialog that pops up in this case.
3977 With numerical prefix ARG show at most ARG top priority items
3978 from each category in each file. With `C-u' as prefix argument
3979 show the numbers of top priority items specified in
3980 `todo-top-priorities-overrides', if this is non-nil; otherwise show
3981 `todo-top-priorities' items per category. With no prefix
3982 argument, if a top priorities file for the chosen todo files
3983 exists (see `todo-save-filtered-items-buffer'), visit this file;
3984 if there is no such file, do the same as with prefix argument
3987 (todo-filter-items 'top arg t
))
3989 (defun todo-filter-diary-items (&optional arg
)
3990 "Display a list of todo diary items from different categories.
3991 The categories can be any of those in the current todo file.
3993 Called with no prefix ARG, if a diary items file for the current
3994 todo file has previously been saved (see
3995 `todo-save-filtered-items-buffer'), visit this file; if there is
3996 no such file, build the list of diary items. Called with a
3997 prefix argument, build the list even if there is a saved file of
4000 (todo-filter-items 'diary arg
))
4002 (defun todo-filter-diary-items-multifile (&optional arg
)
4003 "Display a list of todo diary items from different categories.
4004 The categories are a subset of the categories in the files listed
4005 in `todo-filter-files', or if this nil, in the files chosen from
4006 a file selection dialog that pops up in this case.
4008 Called with no prefix ARG, if a diary items file for the chosen
4009 todo files has previously been saved (see
4010 `todo-save-filtered-items-buffer'), visit this file; if there is
4011 no such file, build the list of diary items. Called with a
4012 prefix argument, build the list even if there is a saved file of
4015 (todo-filter-items 'diary arg t
))
4017 (defun todo-filter-regexp-items (&optional arg
)
4018 "Prompt for a regular expression and display items that match it.
4019 The matches can be from any categories in the current todo file
4020 and with non-nil option `todo-filter-done-items', can include
4021 not only todo items but also done items, including those in
4024 Called with no prefix ARG, if a regexp items file for the current
4025 todo file has previously been saved (see
4026 `todo-save-filtered-items-buffer'), visit this file; if there is
4027 no such file, build the list of regexp items. Called with a
4028 prefix argument, build the list even if there is a saved file of
4031 (todo-filter-items 'regexp arg
))
4033 (defun todo-filter-regexp-items-multifile (&optional arg
)
4034 "Prompt for a regular expression and display items that match it.
4035 The matches can be from any categories in the files listed in
4036 `todo-filter-files', or if this nil, in the files chosen from a
4037 file selection dialog that pops up in this case. With non-nil
4038 option `todo-filter-done-items', the matches can include not
4039 only todo items but also done items, including those in Archive
4042 Called with no prefix ARG, if a regexp items file for the current
4043 todo file has previously been saved (see
4044 `todo-save-filtered-items-buffer'), visit this file; if there is
4045 no such file, build the list of regexp items. Called with a
4046 prefix argument, build the list even if there is a saved file of
4049 (todo-filter-items 'regexp arg t
))
4051 (defvar todo--fifiles-history nil
4052 "List of short file names used by todo-find-filtered-items-file.")
4054 (defun todo-find-filtered-items-file ()
4055 "Choose a filtered items file and visit it."
4057 (let ((files (directory-files todo-directory t
"\\.tod[rty]$" t
))
4060 (let ((sf-name (todo-short-file-name f
))
4061 (type (cond ((equal (file-name-extension f
) "todr") "regexp")
4062 ((equal (file-name-extension f
) "todt") "top")
4063 ((equal (file-name-extension f
) "tody") "diary"))))
4064 (push (cons (concat sf-name
" (" type
")") f
) falist
)))
4065 (setq file
(completing-read "Choose a filtered items file: " falist nil t nil
4066 'todo--fifiles-history
(caar falist
)))
4067 (setq file
(cdr (assoc-string file falist
)))
4069 (unless (derived-mode-p 'todo-filtered-items-mode
)
4070 (todo-filtered-items-mode))
4071 (todo-prefix-overlays)))
4073 (defun todo-go-to-source-item ()
4074 "Display the file and category of the filtered item at point."
4076 (unless (looking-at "^$") ; Empty line at EOB.
4077 (let* ((str (todo-item-string))
4078 (buf (current-buffer))
4079 (res (todo-find-item str
))
4084 (message "Category %s does not contain this item." cat
)
4086 (set-window-buffer (selected-window)
4087 (set-buffer (find-buffer-visiting file
)))
4088 (setq todo-current-todo-file file
)
4089 (setq todo-category-number
(todo-category-number cat
))
4090 (let ((todo-show-with-done (if (or todo-filter-done-items
4091 (eq (cdr found
) 'done
))
4093 todo-show-with-done
)))
4094 (todo-category-select))
4095 (if transient-mark-mode
(deactivate-mark))
4096 (goto-char (car found
))))))
4098 (defvar todo-multiple-filter-files nil
4099 "List of files selected from `todo-multiple-filter-files' widget.")
4101 (defvar todo-multiple-filter-files-widget nil
4102 "Variable holding widget created by `todo-multiple-filter-files'.")
4104 (defun todo-multiple-filter-files ()
4105 "Pop to a buffer with a widget for choosing multiple filter files."
4108 (require 'wid-edit
))
4109 (with-current-buffer (get-buffer-create "*Todo Filter Files*")
4110 (pop-to-buffer (current-buffer))
4112 (kill-all-local-variables)
4113 (widget-insert "Select files for generating the top priorities list.\n\n")
4114 (setq todo-multiple-filter-files-widget
4116 `(set ,@(todo--files-type-list))))
4117 (widget-insert "\n")
4118 (widget-create 'push-button
4119 :notify
(lambda (&rest _
)
4120 (setq todo-multiple-filter-files
'quit
)
4122 (exit-recursive-edit))
4125 (widget-create 'push-button
4126 :notify
(lambda (&rest _
)
4127 (setq todo-multiple-filter-files
4130 (concat todo-directory
4133 todo-multiple-filter-files-widget
)))
4135 (exit-recursive-edit))
4137 (use-local-map widget-keymap
)
4139 (message "Click \"Apply\" after selecting files.")
4142 (defconst todo-filtered-items-buffer
"Todo filtered items"
4143 "Initial name of buffer in Todo Filter Items mode.")
4145 (defconst todo-top-priorities-buffer
"Todo top priorities"
4146 "Buffer type string for `todo-filter-items'.")
4148 (defconst todo-diary-items-buffer
"Todo diary items"
4149 "Buffer type string for `todo-filter-items'.")
4151 (defconst todo-regexp-items-buffer
"Todo regexp items"
4152 "Buffer type string for `todo-filter-items'.")
4154 (defun todo-filter-items (filter &optional new multifile
)
4155 "Display a list of items filtered by FILTER.
4156 The values of FILTER can be `top' for top priority items, a cons
4157 of `top' and a number passed by the caller, `diary' for diary
4158 items, or `regexp' for items matching a regular expression
4159 entered by the user. The items can come from any categories in
4160 the current todo file or, with non-nil MULTIFILE, from several
4161 files. If NEW is nil, visit an appropriate file containing the
4162 list of filtered items; if there is no such file, or with non-nil
4163 NEW, build the list and display it.
4165 See the documentation strings of the commands
4166 `todo-filter-top-priorities', `todo-filter-diary-items',
4167 `todo-filter-regexp-items', and those of the corresponding
4168 multifile commands for further details."
4169 (let* ((top (eq filter
'top
))
4170 (diary (eq filter
'diary
))
4171 (regexp (eq filter
'regexp
))
4172 (buf (cond (top todo-top-priorities-buffer
)
4173 (diary todo-diary-items-buffer
)
4174 (regexp todo-regexp-items-buffer
)))
4175 (flist (if multifile
4176 (or todo-filter-files
4177 (progn (todo-multiple-filter-files)
4178 todo-multiple-filter-files
))
4179 (list todo-current-todo-file
)))
4180 (fname (if (equal flist
'quit
)
4181 ;; Pressed `cancel' in t-m-f-f file selection dialog.
4183 (concat todo-directory
4184 (mapconcat #'todo-short-file-name flist
"-")
4187 (regexp ".todr")))))
4188 (multi (> (length flist
) 1))
4189 (rxfiles (when regexp
4190 (directory-files todo-directory t
".*\\.todr$" t
)))
4191 (file-exists (or (file-exists-p fname
) rxfiles
))
4193 (cond ((and top new
(natnump new
))
4194 (todo-filter-items-1 (cons 'top new
) flist
))
4195 ((and (not new
) file-exists
)
4196 (when (and rxfiles
(> (length rxfiles
) 1))
4197 (let ((rxf (mapcar #'todo-short-file-name rxfiles
)))
4198 (setq fname
(todo-absolute-file-name
4199 (completing-read "Choose a regexp items file: "
4203 (unless (derived-mode-p 'todo-filtered-items-mode
)
4204 (todo-filtered-items-mode))
4205 (todo-prefix-overlays)
4206 (todo-check-filtered-items-file))
4208 (todo-filter-items-1 filter flist
)))
4209 (dolist (s (split-string (todo-short-file-name fname
) "-"))
4210 (setq bufname
(if bufname
4211 (concat bufname
(if (member s
(mapcar
4212 #'todo-short-file-name
4217 (rename-buffer (format (concat "%s for file" (if multi
"s" "") " \"%s\"")
4220 (defun todo-filter-items-1 (filter file-list
)
4221 "Build a list of items by applying FILTER to FILE-LIST.
4222 Internal subroutine called by `todo-filter-items', which passes
4223 the values of FILTER and FILE-LIST."
4224 (let ((num (if (consp filter
) (cdr filter
) todo-top-priorities
))
4225 (buf (get-buffer-create todo-filtered-items-buffer
))
4226 (multifile (> (length file-list
) 1))
4227 regexp fname bufstr cat beg end done
)
4228 (if (null file-list
)
4229 (user-error "No files have been chosen for filtering")
4230 (with-current-buffer buf
4232 (kill-all-local-variables)
4233 (todo-filtered-items-mode))
4234 (when (eq filter
'regexp
)
4235 (setq regexp
(read-string "Enter a regular expression: ")))
4236 (save-current-buffer
4237 (dolist (f file-list
)
4238 ;; Before inserting file contents into temp buffer, save a modified
4239 ;; buffer visiting it.
4240 (let ((bf (find-buffer-visiting f
)))
4241 (when (buffer-modified-p bf
)
4242 (with-current-buffer bf
(save-buffer))))
4243 (setq fname
(todo-short-file-name f
))
4245 (when (and todo-filter-done-items
(eq filter
'regexp
))
4246 ;; If there is a corresponding archive file for the
4247 ;; todo file, insert it first and add identifiers for
4248 ;; todo-go-to-source-item.
4249 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
4250 (when (file-exists-p arch
)
4251 (insert-file-contents arch
)
4252 ;; Delete todo archive file's categories sexp.
4253 (delete-region (line-beginning-position)
4254 (1+ (line-end-position)))
4257 (when (re-search-forward
4258 (concat (if todo-filter-done-items
4259 (concat "\\(?:" todo-done-string-start
4260 "\\|" todo-date-string-start
4262 todo-date-string-start
)
4263 todo-date-pattern
"\\(?: "
4264 diary-time-regexp
"\\)?"
4265 (if todo-filter-done-items
4267 (regexp-quote todo-nondiary-end
)) "?")
4269 (insert "(archive) "))
4271 (insert-file-contents f
)
4272 ;; Delete todo file's categories sexp.
4273 (delete-region (line-beginning-position) (1+ (line-end-position)))
4275 ;; Unless the number of top priorities to show was
4276 ;; passed by the caller, the file-wide value from
4277 ;; `todo-top-priorities-overrides', if non-nil, overrides
4278 ;; `todo-top-priorities'.
4279 (unless (consp filter
)
4280 (setq fnum
(or (nth 1 (assoc f todo-top-priorities-overrides
))
4281 todo-top-priorities
)))
4282 (while (re-search-forward
4283 (concat "^" (regexp-quote todo-category-beg
)
4286 (setq cat
(match-string 1))
4288 ;; Unless the number of top priorities to show was
4289 ;; passed by the caller, the category-wide value
4290 ;; from `todo-top-priorities-overrides', if non-nil,
4291 ;; overrides a non-nil file-wide value from
4292 ;; `todo-top-priorities-overrides' as well as
4293 ;; `todo-top-priorities'.
4294 (unless (consp filter
)
4295 (let ((cats (nth 2 (assoc f todo-top-priorities-overrides
))))
4296 (setq cnum
(or (cdr (assoc cat cats
)) fnum
))))
4297 (delete-region (match-beginning 0) (match-end 0))
4298 (setq beg
(point)) ; First item in the current category.
4299 (setq end
(if (re-search-forward
4300 (concat "^" (regexp-quote todo-category-beg
))
4306 (if (re-search-forward
4307 (concat "\n" (regexp-quote todo-category-done
))
4311 (unless (and todo-filter-done-items
(eq filter
'regexp
))
4312 ;; Leave done items.
4313 (delete-region done end
)
4315 (narrow-to-region beg end
) ; Process only current category.
4316 (goto-char (point-min))
4317 ;; Apply the filter.
4318 (cond ((eq filter
'diary
)
4320 (if (looking-at (regexp-quote todo-nondiary-start
))
4322 (todo-forward-item))))
4323 ((eq filter
'regexp
)
4325 (if (looking-at todo-item-start
)
4326 (if (string-match regexp
(todo-item-string))
4329 ;; Kill lines that aren't part of a todo or done
4330 ;; item (empty or todo-category-done).
4331 (delete-region (line-beginning-position)
4332 (1+ (line-end-position))))
4333 ;; If last todo item in file matches regexp and
4334 ;; there are no following done items,
4335 ;; todo-category-done string is left dangling,
4336 ;; because todo-forward-item jumps over it.
4339 (concat (regexp-quote todo-done-string
)
4341 (line-beginning-position 0)))
4342 (delete-region (point) (progn
4345 (t ; Filter top priority items.
4346 (setq num
(or cnum fnum num
))
4348 (todo-forward-item num
))))
4350 ;; Delete non-top-priority items.
4351 (unless (member filter
'(diary regexp
))
4352 (delete-region beg end
))
4353 (goto-char (point-min))
4354 ;; Add file (if using multiple files) and category tags to
4357 (when (re-search-forward
4358 (concat (if todo-filter-done-items
4359 (concat "\\(?:" todo-done-string-start
4360 "\\|" todo-date-string-start
4362 todo-date-string-start
)
4363 todo-date-pattern
"\\(?: " diary-time-regexp
4364 "\\)?" (if todo-filter-done-items
4366 (regexp-quote todo-nondiary-end
))
4370 (when (looking-at "(archive) ") (goto-char (match-end 0)))
4371 (insert (if multifile
(concat fname
":") "") cat
"]"))
4374 (setq bufstr
(buffer-string))
4375 (with-current-buffer buf
4376 (let (buffer-read-only)
4377 (insert bufstr
)))))))
4378 (set-window-buffer (selected-window) (set-buffer buf
))
4379 (todo-prefix-overlays)
4380 (goto-char (point-min)))))
4382 (defun todo-set-top-priorities (&optional arg
)
4383 "Set number of top priorities shown by `todo-filter-top-priorities'.
4384 With non-nil ARG, set the number only for the current Todo
4385 category; otherwise, set the number for all categories in the
4388 Calling this function via either of the commands
4389 `todo-set-top-priorities-in-file' or
4390 `todo-set-top-priorities-in-category' is the recommended way to
4391 set the user customizable option `todo-top-priorities-overrides'."
4392 (let* ((cat (todo-current-category))
4393 (file todo-current-todo-file
)
4394 (rules todo-top-priorities-overrides
)
4395 (frule (assoc-string file rules
))
4396 (crules (nth 2 frule
))
4397 (crule (assoc-string cat crules
))
4398 (fcur (or (nth 1 frule
)
4399 todo-top-priorities
))
4400 (ccur (or (and arg
(cdr crule
))
4402 (prompt (if arg
(concat "Number of top priorities in this category"
4403 " (currently %d): ")
4404 (concat "Default number of top priorities per category"
4405 " in this file (currently %d): ")))
4408 (let ((cur (if arg ccur fcur
)))
4409 (setq new
(read-number (format prompt cur
))
4410 prompt
"Enter a non-negative number: "
4412 (let ((nrule (if arg
4413 (append (delete crule crules
) (list (cons cat new
)))
4414 (append (list file new
) (list crules
)))))
4415 (setq rules
(cons (if arg
4416 (list file fcur nrule
)
4418 (delete frule rules
)))
4419 (customize-save-variable 'todo-top-priorities-overrides rules
)
4420 (todo-prefix-overlays))))
4422 (defun todo-find-item (str)
4423 "Search for filtered item STR in its saved todo file.
4424 Return the list (FOUND FILE CAT), where CAT and FILE are the
4425 item's category and file, and FOUND is a cons cell if the search
4426 succeeds, whose car is the start of the item in FILE and whose
4427 cdr is `done', if the item is now a done item, `changed', if its
4428 text was truncated or augmented or, for a top priority item, if
4429 its priority has changed, and `same' otherwise."
4430 (string-match (concat (if todo-filter-done-items
4431 (concat "\\(?:" todo-done-string-start
"\\|"
4432 todo-date-string-start
"\\)")
4433 todo-date-string-start
)
4434 todo-date-pattern
"\\(?: " diary-time-regexp
"\\)?"
4435 (if todo-filter-done-items
4437 (regexp-quote todo-nondiary-end
)) "?"
4438 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
4439 "\\(?1:.*\\)\\]\\).*$")
4441 (let ((cat (match-string 1 str
))
4442 (file (match-string 2 str
))
4443 (archive (string= (match-string 3 str
) "(archive) "))
4444 (filcat (match-string 4 str
))
4446 (tpbuf (save-match-data (string-match "top" (buffer-name))))
4448 (setq str
(replace-match "" nil nil str
4))
4450 ;; Calculate priority of STR wrt its category.
4452 (while (search-backward filcat nil t
)
4453 (setq tpriority
(1+ tpriority
)))))
4455 (concat todo-directory
(substring file
0 -
1)
4456 (if archive
".toda" ".todo"))
4458 (concat (file-name-sans-extension
4459 todo-global-current-todo-file
) ".toda")
4460 todo-global-current-todo-file
)))
4461 (find-file-noselect file
)
4462 (with-current-buffer (find-buffer-visiting file
)
4464 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
4465 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
4468 (goto-char (point-min))
4469 (let ((beg (re-search-forward
4470 (concat "^" (regexp-quote (concat todo-category-beg cat
))
4473 (done (save-excursion
4475 (concat "^" (regexp-quote todo-category-done
)) nil t
)))
4476 (end (save-excursion
4477 (or (re-search-forward
4478 (concat "^" (regexp-quote todo-category-beg
))
4481 (setq found
(when (search-forward str end t
)
4482 (goto-char (match-beginning 0))))
4485 (cons found
(if (> (point) done
)
4487 (let ((cpriority 1))
4490 ;; Not top item in category.
4491 (while (> (point) (1+ beg
))
4492 (let ((opoint (point)))
4493 (todo-backward-item)
4494 ;; Can't move backward beyond
4495 ;; first item in file.
4496 (unless (= (point) opoint
)
4497 (setq cpriority
(1+ cpriority
)))))))
4498 (if (and (= tpriority cpriority
)
4499 ;; Proper substring is not the same.
4500 (string= (todo-item-string)
4504 (list found file cat
)))
4506 (defun todo-check-filtered-items-file ()
4507 "Check if filtered items file is up to date and a show suitable message."
4511 (let* ((item (todo-item-string))
4512 (found (car (todo-find-item item
))))
4513 (unless (eq (cdr found
) 'same
)
4515 (overlay-put (make-overlay (todo-item-start) (todo-item-end))
4516 'face
'todo-search
))
4517 (setq count
(1+ count
))))
4518 ;; (throw 'old (message "The marked item is not up to date.")))
4519 (todo-forward-item))
4521 (message "Filtered items file is up to date.")
4522 (message (concat "The highlighted item" (if (= count
1) " is " "s are ")
4524 ;; "\nType <return> on item for details."
4527 (defun todo-filter-items-filename ()
4528 "Return absolute file name for saving this Filtered Items buffer."
4529 (let ((bufname (buffer-name)))
4530 (string-match "\"\\([^\"]+\\)\"" bufname
)
4531 (let* ((filename-str (substring bufname
(match-beginning 1) (match-end 1)))
4532 (filename-base (replace-regexp-in-string ", " "-" filename-str
))
4533 (top-priorities (string-match "top priorities" bufname
))
4534 (diary-items (string-match "diary items" bufname
))
4535 (regexp-items (string-match "regexp items" bufname
)))
4537 (let ((prompt (concat "Enter a short identifying string"
4538 " to make this file name unique: ")))
4539 (setq filename-base
(concat filename-base
"-" (read-string prompt
)))))
4540 (concat todo-directory filename-base
4541 (cond (top-priorities ".todt")
4542 (diary-items ".tody")
4543 (regexp-items ".todr"))))))
4545 (defun todo-save-filtered-items-buffer ()
4546 "Save current Filtered Items buffer to a file.
4547 If the file already exists, overwrite it only on confirmation."
4548 (let ((filename (or (buffer-file-name) (todo-filter-items-filename)))
4549 (bufname (buffer-name)))
4550 (write-file filename t
)
4551 (setq buffer-read-only t
)
4552 (rename-buffer bufname
)))
4554 ;; -----------------------------------------------------------------------------
4555 ;;; Printing Todo mode buffers
4556 ;; -----------------------------------------------------------------------------
4558 (defcustom todo-print-buffer-function
#'ps-print-buffer-with-faces
4559 "Function called by `todo-print-buffer' to print Todo mode buffers.
4560 Called with one argument which can either be:
4561 - a string, naming a file to save the print image to.
4562 - nil, to send the image to the printer."
4566 (defvar todo-print-buffer
"*Todo Print*"
4567 "Name of buffer with printable version of Todo mode buffer.")
4569 (defun todo-print-buffer (&optional to-file
)
4570 "Produce a printable version of the current Todo mode buffer.
4571 This converts overlays and soft line wrapping and, depending on
4572 the value of `todo-print-buffer-function', includes faces. With
4573 non-nil argument TO-FILE write the printable version to a file;
4574 otherwise, send it to the default printer."
4576 (let ((buf todo-print-buffer
)
4578 ((eq major-mode
'todo-mode
)
4579 (concat "Todo File: "
4580 (todo-short-file-name todo-current-todo-file
)
4581 "\nCategory: " (todo-current-category)))
4582 ((eq major-mode
'todo-filtered-items-mode
)
4584 (prefix (propertize (concat todo-prefix
" ")
4585 'face
'todo-prefix-string
))
4587 (fill-prefix (make-string todo-indent-to-here
32))
4588 (content (buffer-string)))
4589 (with-current-buffer (get-buffer-create buf
)
4591 (goto-char (point-min))
4594 (end (save-excursion (todo-item-end))))
4595 (when todo-number-prefix
4597 (setq prefix
(propertize (concat (number-to-string num
) " ")
4598 'face
'todo-prefix-string
)))
4600 (fill-region beg end
))
4601 ;; Calling todo-forward-item infloops at todo-item-start due to
4602 ;; non-overlay prefix, so search for item start instead.
4603 (if (re-search-forward todo-item-start nil t
)
4605 (goto-char (point-max))))
4606 (if (re-search-backward (concat "^" (regexp-quote todo-category-done
))
4608 (replace-match todo-done-separator
))
4609 (goto-char (point-min))
4612 (funcall todo-print-buffer-function
4614 (read-file-name "Print to file: "))))
4617 (defun todo-print-buffer-to-file ()
4618 "Save printable version of this Todo mode buffer to a file."
4620 (todo-print-buffer t
))
4622 ;; -----------------------------------------------------------------------------
4623 ;;; Legacy Todo mode files
4624 ;; -----------------------------------------------------------------------------
4626 (defcustom todo-legacy-date-time-regexp
4627 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
4628 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
4629 "Regexp matching legacy todo-mode.el item date-time strings.
4630 In order for `todo-convert-legacy-files' to correctly convert
4631 this string to the current Todo mode format, the regexp must
4632 contain four explicitly numbered groups (see `(elisp) Regexp
4633 Backslash'), where group 1 matches a string for the year, group 2
4634 a string for the month, group 3 a string for the day and group 4
4635 a string for the time. The default value converts date-time
4636 strings built using the default value of
4637 `todo-time-string-format' from todo-mode.el."
4641 (defun todo-convert-legacy-date-time ()
4642 "Return converted date-time string.
4643 Helper function for `todo-convert-legacy-files'."
4645 ((year (match-string 1))
4646 (month (match-string 2))
4647 (monthname (calendar-month-name (string-to-number month
) t
))
4648 (day (match-string 3))
4649 (time (match-string 4))
4652 (insert (mapconcat #'eval calendar-date-display-form
"")
4653 (when time
(concat " " time
)))))
4655 (defun todo-convert-legacy-files ()
4656 "Convert legacy todo files to the current Todo mode format.
4657 The old-style files named by the variables `todo-file-do' and
4658 `todo-file-done' from the old package are converted to the new
4659 format and saved (the latter as a todo archive file) with a new
4660 name in `todo-directory'. See also the documentation string of
4661 `todo-legacy-date-time-regexp' for further details."
4663 ;; If there are user customizations of legacy options, use them,
4664 ;; otherwise use the legacy default values.
4665 (let ((todo-file-do-tem (if (boundp 'todo-file-do
)
4667 (locate-user-emacs-file "todo-do" ".todo-do")))
4668 (todo-file-done-tem (if (boundp 'todo-file-done
)
4670 (locate-user-emacs-file "todo-done" ".todo-done")))
4671 (todo-initials-tem (and (boundp 'todo-initials
) todo-initials
))
4672 (todo-entry-prefix-function-tem (and (boundp 'todo-entry-prefix-function
)
4673 todo-entry-prefix-function
))
4675 ;; Convert `todo-file-do'.
4676 (if (not (file-exists-p todo-file-do-tem
))
4677 (message "No legacy todo file exists")
4678 (let ((default "todo-do-conv")
4681 (insert-file-contents todo-file-do-tem
)
4682 ;; Eliminate old-style local variables list in first line.
4683 (delete-region (line-beginning-position) (1+ (line-end-position)))
4684 (search-forward " --- " nil t
) ; Legacy todo-category-beg.
4685 (setq todo-prefix-tem
(buffer-substring-no-properties
4686 (line-beginning-position) (match-beginning 0)))
4687 (goto-char (point-min))
4690 ;; Old-style category start delimiter.
4691 ((looking-at (regexp-quote (concat todo-prefix-tem
" --- ")))
4692 (replace-match todo-category-beg
))
4693 ;; Old-style category end delimiter.
4694 ((looking-at (regexp-quote "--- End"))
4696 ;; Old-style category separator.
4697 ((looking-at (regexp-quote
4698 (concat todo-prefix-tem
" "
4699 (make-string 75 ?-
))))
4700 (replace-match todo-category-done
))
4701 ;; Old-style item header (date/time/initials).
4702 ((looking-at (concat (regexp-quote todo-prefix-tem
) " "
4703 (if todo-entry-prefix-function-tem
4704 (funcall todo-entry-prefix-function-tem
)
4705 (concat todo-legacy-date-time-regexp
" "
4706 (if todo-initials-tem
4707 (regexp-quote todo-initials-tem
)
4710 (todo-convert-legacy-date-time)))
4712 (setq file
(concat todo-directory
4714 (format "Save file as (default \"%s\"): " default
)
4717 (unless (file-exists-p todo-directory
)
4718 (make-directory todo-directory
))
4719 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
4721 (insert-file-contents file
)
4722 (let ((todo-categories (todo-make-categories-list t
)))
4723 (todo-update-categories-sexp)
4724 (todo-check-format))
4725 (write-region (point-min) (point-max) file nil
'nomessage
))
4726 (setq todo-files
(funcall todo-files-function
))
4727 ;; Convert `todo-file-done'.
4728 (when (file-exists-p todo-file-done-tem
)
4730 (insert-file-contents todo-file-done-tem
)
4731 (let ((beg (make-marker))
4733 cat cats comment item
)
4735 (when (looking-at todo-legacy-date-time-regexp
)
4736 (set-marker beg
(point))
4737 (todo-convert-legacy-date-time)
4738 (set-marker end
(point))
4740 (insert "[" todo-done-string
)
4744 (when (looking-at todo-legacy-date-time-regexp
)
4745 (todo-convert-legacy-date-time))
4746 (when (looking-at (concat " " (if todo-initials-tem
4751 (replace-match "")))
4752 (if (re-search-forward
4753 (concat "^" todo-legacy-date-time-regexp
) nil t
)
4754 (goto-char (match-beginning 0))
4755 (goto-char (point-max)))
4757 (when (looking-back "\\[\\([^][]+\\)\\]"
4758 (line-beginning-position))
4759 (setq cat
(match-string 1))
4760 (goto-char (match-beginning 0))
4762 ;; If the item ends with a non-comment parenthesis not
4763 ;; followed by a period, we lose (but we inherit that
4764 ;; problem from the legacy code).
4765 ;; FIXME: fails on multiline comment
4766 (when (looking-back "(\\(.*\\)) " (line-beginning-position))
4767 (setq comment
(match-string 1))
4769 (insert "[" todo-comment-string
": " comment
"]"))
4770 (set-marker end
(point))
4771 (if (member cat cats
)
4772 ;; If item is already in its category, leave it there.
4773 (unless (save-excursion
4775 (concat "^" (regexp-quote todo-category-beg
)
4778 (string= (match-string 1) cat
))
4779 ;; Else move it to its category.
4780 (setq item
(buffer-substring-no-properties beg end
))
4781 (delete-region beg
(1+ end
))
4782 (set-marker beg
(point))
4785 (regexp-quote (concat todo-category-beg cat
))
4789 (if (re-search-forward
4790 (concat "^" (regexp-quote todo-category-beg
)
4793 (progn (goto-char (match-beginning 0))
4796 (goto-char (point-max)))
4801 (insert todo-category-beg cat
"\n\n"
4802 todo-category-done
"\n"))
4804 (set-marker beg nil
)
4805 (set-marker end nil
))
4806 (setq file
(concat (file-name-sans-extension file
) ".toda"))
4807 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
4809 (insert-file-contents file
)
4810 (let* ((todo-categories (todo-make-categories-list t
)))
4811 (todo-update-categories-sexp)
4812 (todo-check-format))
4813 (write-region (point-min) (point-max) file nil
'nomessage
)
4814 (setq archive-sexp
(read (buffer-substring-no-properties
4815 (line-beginning-position)
4816 (line-end-position)))))
4817 (setq file
(concat (file-name-sans-extension file
) ".todo"))
4818 ;; Update categories sexp of converted todo file again, adding
4819 ;; counts of archived items.
4821 (insert-file-contents file
)
4822 (let ((sexp (read (buffer-substring-no-properties
4823 (line-beginning-position)
4824 (line-end-position)))))
4826 (let ((archive-cat (assoc (car cat
) archive-sexp
)))
4828 (aset (cdr cat
) 3 (aref (cdr archive-cat
) 2)))))
4829 (delete-region (line-beginning-position) (line-end-position))
4830 (prin1 sexp
(current-buffer)))
4831 (write-region (point-min) (point-max) file nil
'nomessage
))
4832 (setq todo-archives
(funcall todo-files-function t
)))
4833 (todo-update-filelist-defcustoms)
4834 (when (y-or-n-p (concat "Format conversion done; do you want to "
4835 "visit the converted file now? "))
4836 (setq todo-current-todo-file file
)
4837 (unless todo-default-todo-file
4838 ;; We just initialized the first todo file, so make it the
4839 ;; default now to avoid an infinite recursion with todo-show.
4840 (setq todo-default-todo-file
(todo-short-file-name file
)))
4843 ;; -----------------------------------------------------------------------------
4844 ;;; Utility functions for todo files, categories and items
4845 ;; -----------------------------------------------------------------------------
4847 (defun todo-absolute-file-name (name &optional type
)
4848 "Return the absolute file name of short todo file NAME.
4849 With TYPE `archive' or `top' return the absolute file name of the
4850 short todo archive or top priorities file name, respectively."
4851 ;; No-op if there is no todo file yet (i.e. don't concatenate nil).
4854 (concat todo-directory name
4855 (cond ((eq type
'archive
) ".toda")
4856 ((eq type
'top
) ".todt")
4857 ((eq type
'diary
) ".tody")
4858 ((eq type
'regexp
) ".todr")
4861 (defun todo-check-file (file)
4862 "Check the state associated with FILE and update it if necessary.
4863 If FILE exists, return t. If it does not exist and there is no
4864 live buffer with its content, return nil; if there is such a
4865 buffer and the user tries to show it, ask whether to restore
4866 FILE, and if confirmed, do so and return t; else delete the
4867 buffer, clean up the state and return nil."
4868 (setq todo-files
(funcall todo-files-function
))
4869 (setq todo-archives
(funcall todo-files-function t
))
4870 (if (file-exists-p file
)
4872 (setq todo-visited
(delete file todo-visited
))
4873 (let ((buf (find-buffer-visiting file
)))
4877 (format (concat "Todo file \"%s\" has been deleted but "
4878 "its content is still in a buffer!\n")
4879 (todo-short-file-name file
))
4880 "Save that buffer and restore the todo file? ")))
4882 (with-current-buffer buf
(save-buffer))
4883 (setq todo-files
(funcall todo-files-function
))
4884 (setq todo-archives
(funcall todo-files-function t
))
4886 (let* ((files (append todo-files todo-archives
)))
4887 (unless (or (not todo-current-todo-file
)
4888 (member todo-current-todo-file files
))
4889 (setq todo-current-todo-file nil
))
4890 (unless (or (not todo-global-current-todo-file
)
4891 (member todo-global-current-todo-file files
))
4892 (setq todo-global-current-todo-file nil
))
4893 (unless (or (not todo-default-todo-file
)
4894 (member todo-default-todo-file files
))
4895 (setq todo-default-todo-file
(todo-short-file-name
4897 (todo-update-filelist-defcustoms)
4898 (when buf
(kill-buffer buf
))
4901 (defun todo-category-number (cat)
4902 "Return the number of category CAT in this todo file.
4903 The buffer-local variable `todo-category-number' holds this
4904 number as its value."
4905 (let ((categories (mapcar #'car todo-categories
)))
4906 (setq todo-category-number
4907 ;; Increment by one, so that the number of the first
4908 ;; category is one rather than zero.
4909 (1+ (- (length categories
)
4910 (length (member cat categories
)))))))
4912 (defun todo-current-category ()
4913 "Return the name of the current category."
4914 (car (nth (1- todo-category-number
) todo-categories
)))
4916 (defun todo-category-select ()
4917 "Display the current category correctly."
4918 (let ((name (todo-current-category))
4919 cat-begin cat-end done-start done-sep-start done-end
)
4921 (goto-char (point-min))
4923 (concat "^" (regexp-quote (concat todo-category-beg name
)) "$") nil t
)
4924 (setq cat-begin
(1+ (line-end-position)))
4925 (setq cat-end
(if (re-search-forward
4926 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
4929 (setq mode-line-buffer-identification
4930 (funcall todo-mode-line-function name
))
4931 (narrow-to-region cat-begin cat-end
)
4932 (todo-prefix-overlays)
4933 (goto-char (point-min))
4934 (if (re-search-forward (concat "\n\\(" (regexp-quote todo-category-done
)
4938 (setq done-start
(match-beginning 0))
4939 (setq done-sep-start
(match-beginning 1))
4940 (setq done-end
(match-end 0)))
4941 (error "Category %s is missing todo-category-done string" name
))
4942 (if todo-show-done-only
4943 (narrow-to-region (1+ done-end
) (point-max))
4944 (when (and todo-show-with-done
4945 (re-search-forward todo-done-string-start nil t
))
4946 ;; Now we want to see the done items, so reset displayed end to end of
4948 (setq done-start cat-end
)
4949 ;; Make display overlay for done items separator string, unless there
4951 (let* ((done-sep todo-done-separator
)
4952 (ov (progn (goto-char done-sep-start
)
4953 (todo-get-overlay 'separator
))))
4955 (setq ov
(make-overlay done-sep-start done-end
))
4956 (overlay-put ov
'todo
'separator
)
4957 (overlay-put ov
'display done-sep
))))
4958 (narrow-to-region (point-min) done-start
)
4959 ;; Loading this from todo-mode, or adding it to the mode hook, causes
4960 ;; Emacs to hang in todo-item-start, at (looking-at todo-item-start).
4961 (when todo-highlight-item
4963 (hl-line-mode 1)))))
4965 (defun todo-get-count (type &optional category
)
4966 "Return count of TYPE items in CATEGORY.
4967 If CATEGORY is nil, default to the current category."
4968 (let* ((cat (or category
(todo-current-category)))
4969 (counts (cdr (assoc cat todo-categories
)))
4970 (idx (cond ((eq type
'todo
) 0)
4971 ((eq type
'diary
) 1)
4973 ((eq type
'archived
) 3))))
4976 (defun todo-update-count (type increment
&optional category
)
4977 "Change count of TYPE items in CATEGORY by integer INCREMENT.
4978 With nil or omitted CATEGORY, default to the current category."
4979 (let* ((cat (or category
(todo-current-category)))
4980 (counts (cdr (assoc cat todo-categories
)))
4981 (idx (cond ((eq type
'todo
) 0)
4982 ((eq type
'diary
) 1)
4984 ((eq type
'archived
) 3))))
4985 (aset counts idx
(+ increment
(aref counts idx
)))))
4987 (defun todo-set-categories ()
4988 "Set `todo-categories' from the sexp at the top of the file."
4989 ;; New archive files created by `todo-move-category' are empty, which would
4990 ;; make the sexp test fail and raise an error, so in this case we skip it.
4991 (unless (zerop (buffer-size))
4995 (goto-char (point-min))
4996 (setq todo-categories
4997 (if (looking-at "((\"")
4998 (read (buffer-substring-no-properties
4999 (line-beginning-position)
5000 (line-end-position)))
5001 (error "Invalid or missing todo-categories sexp")))))))
5003 (defun todo-update-categories-sexp ()
5004 "Update the `todo-categories' sexp at the top of the file."
5005 (let (buffer-read-only)
5009 (goto-char (point-min))
5010 (if (looking-at (concat "^" (regexp-quote todo-category-beg
)))
5011 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
5012 (setq todo-categories
(todo-make-categories-list t
)))
5013 (delete-region (line-beginning-position) (line-end-position)))
5014 (prin1 todo-categories
(current-buffer))))))
5016 (defun todo-make-categories-list (&optional force
)
5017 "Return an alist of todo categories and their item counts.
5018 With non-nil argument FORCE parse the entire file to build the
5019 list; otherwise, get the value by reading the sexp at the top of
5021 (setq todo-categories nil
)
5025 (goto-char (point-min))
5026 (let (counts cat archive
)
5027 ;; If the file is a todo file and has archived items, identify the
5028 ;; archive, in order to count its items. But skip this with
5029 ;; `todo-convert-legacy-files', since that converts filed items to
5031 (when buffer-file-name
; During conversion there is no file yet.
5032 ;; If the file is an archive, it doesn't have an archive.
5033 (unless (member (file-truename buffer-file-name
)
5034 (funcall todo-files-function t
))
5035 (setq archive
(concat (file-name-sans-extension
5036 todo-current-todo-file
) ".toda"))))
5038 (cond ((looking-at (concat (regexp-quote todo-category-beg
)
5040 (setq cat
(match-string-no-properties 1))
5041 ;; Counts for each category: [todo diary done archive]
5042 (setq counts
(make-vector 4 0))
5043 (setq todo-categories
5044 (append todo-categories
(list (cons cat counts
))))
5045 ;; Add archived item count to the todo file item counts.
5046 ;; Make sure to include newly created archives, e.g. due to
5047 ;; todo-move-category.
5048 (when (member archive
(funcall todo-files-function t
))
5049 (let ((archive-count 0)
5050 (visiting (find-buffer-visiting archive
)))
5051 (with-current-buffer (or visiting
5052 (find-file-noselect archive
))
5056 (goto-char (point-min))
5057 (when (re-search-forward
5058 (concat "^" (regexp-quote todo-category-beg
)
5062 (while (not (or (looking-at
5064 (regexp-quote todo-category-beg
)
5067 (when (looking-at todo-done-string-start
)
5068 (setq archive-count
(1+ archive-count
)))
5070 (unless visiting
(kill-buffer)))
5071 (todo-update-count 'archived archive-count cat
))))
5072 ((looking-at todo-done-string-start
)
5073 (todo-update-count 'done
1 cat
))
5074 ((looking-at (concat "^\\("
5075 (regexp-quote diary-nonmarking-symbol
)
5076 "\\)?" todo-date-pattern
))
5077 (todo-update-count 'diary
1 cat
)
5078 (todo-update-count 'todo
1 cat
))
5079 ((looking-at (concat todo-date-string-start todo-date-pattern
))
5080 (todo-update-count 'todo
1 cat
))
5081 ;; If first line is todo-categories list, use it and end loop
5082 ;; -- unless FORCEd to scan whole file.
5085 (setq todo-categories
(read (buffer-substring-no-properties
5086 (line-beginning-position)
5087 (line-end-position))))
5088 (goto-char (1- (point-max))))))
5092 (defun todo-repair-categories-sexp ()
5093 "Repair corrupt todo file categories sexp.
5094 This should only be needed as a consequence of careless manual
5095 editing or a bug in todo.el.
5097 *Warning*: Calling this command restores the category order to
5098 the list element order in the todo file categories sexp, so any
5099 order changes made in Todo Categories mode will have to be made
5102 (let ((todo-categories (todo-make-categories-list t
)))
5103 (todo-update-categories-sexp)))
5105 (defun todo-check-format ()
5106 "Signal an error if the current todo file is ill-formatted.
5107 Otherwise return t. Display a warning if the file is well-formed
5108 but the categories sexp differs from the current value of
5113 (goto-char (point-min))
5114 (let* ((cats (prin1-to-string todo-categories
))
5115 (ssexp (buffer-substring-no-properties (line-beginning-position)
5116 (line-end-position)))
5117 (sexp (read ssexp
)))
5118 ;; Check the first line for `todo-categories' sexp.
5121 (unless (and (stringp (car c
))
5124 (user-error "Invalid or missing todo-categories sexp"))))
5126 ;; Check well-formedness of categories.
5127 (let ((legit (concat
5128 "\\(^" (regexp-quote todo-category-beg
) "\\)"
5129 "\\|\\(" todo-date-string-start todo-date-pattern
"\\)"
5130 "\\|\\(^[ \t]+[^ \t]*\\)"
5132 "\\|\\(^" (regexp-quote todo-category-done
) "\\)"
5133 "\\|\\(" todo-done-string-start
"\\)")))
5135 (unless (looking-at legit
)
5136 (user-error "Illegitimate todo file format at line %d"
5137 (line-number-at-pos (point))))
5139 ;; Warn user if categories sexp has changed.
5140 (unless (string= ssexp cats
)
5141 (display-warning 'todo
"\
5143 The sexp at the beginning of the file differs from the value of
5144 `todo-categories'. If the sexp is wrong, you can fix it with
5145 M-x todo-repair-categories-sexp, but note this reverts any
5146 changes you have made in the order of the categories.
5151 (defun todo-item-start ()
5152 "Move to start of current todo item and return its position."
5154 ;; Buffer is empty (invocation possible e.g. via todo-forward-item
5155 ;; from todo-filter-items when processing category with no todo
5157 (eq (point-min) (point-max))
5158 ;; Point is on the empty line below category's last todo item...
5159 (and (looking-at "^$")
5160 (or (eobp) ; ...and done items are hidden...
5161 (save-excursion ; ...or done items are visible.
5163 (looking-at (concat "^"
5164 (regexp-quote todo-category-done
))))))
5165 ;; Point is on done items separator.
5166 (save-excursion (beginning-of-line) (looking-at todo-category-done
))
5167 ;; Buffer is widened.
5168 (looking-at (regexp-quote todo-category-beg
)))
5169 (goto-char (line-beginning-position))
5170 (while (not (looking-at todo-item-start
))
5174 (defun todo-item-end ()
5175 "Move to end of current todo item and return its position."
5177 ;; Items cannot end with a blank line.
5179 ;; Point is on done items separator.
5180 (save-excursion (beginning-of-line) (looking-at todo-category-done
)))
5181 (let* ((done (todo-done-item-p))
5183 ;; For todo items, end is before the done items section, for done
5184 ;; items, end is before the next category. If these limits are
5185 ;; missing or inaccessible, end it before the end of the buffer.
5186 (lim (if (save-excursion
5188 (concat "^" (regexp-quote (if done
5190 todo-category-done
)))
5192 (progn (setq to-lim t
) (match-beginning 0))
5194 (when (bolp) (forward-char)) ; Find start of next item.
5195 (goto-char (if (re-search-forward todo-item-start lim t
)
5197 (if to-lim lim
(point-max))))
5198 ;; For last todo item, skip back over the empty line before the done
5199 ;; items section, else just back to the end of the previous line.
5200 (backward-char (when (and to-lim
(not done
) (eq (point) lim
)) 2))
5203 (defun todo-item-string ()
5204 "Return bare text of current item as a string."
5205 (let ((opoint (point))
5206 (start (todo-item-start))
5207 (end (todo-item-end)))
5209 (and start end
(buffer-substring-no-properties start end
))))
5211 (defun todo-forward-item (&optional count
)
5212 "Move point COUNT items down (by default, move down by one item)."
5213 (let* ((not-done (not (or (todo-done-item-p) (looking-at "^$"))))
5214 (start (line-end-position)))
5216 (if (re-search-forward todo-item-start nil t
(or count
1))
5217 (goto-char (match-beginning 0))
5218 (goto-char (point-max)))
5219 ;; If points advances by one from a todo to a done item, go back
5220 ;; to the space above todo-done-separator, since that is a
5221 ;; legitimate place to insert an item. But skip this space if
5222 ;; count > 1, since that should only stop on an item.
5223 (when (and not-done
(todo-done-item-p) (not count
))
5224 ;; (if (or (not count) (= count 1))
5225 (re-search-backward "^$" start t
))));)
5226 ;; The preceding sexp is insufficient when buffer is not narrowed,
5227 ;; since there could be no done items in this category, so the
5228 ;; search puts us on first todo item of next category. Does this
5229 ;; ever happen? If so:
5230 ;; (let ((opoint) (point))
5231 ;; (forward-line -1)
5232 ;; (when (or (not count) (= count 1))
5233 ;; (cond ((looking-at (concat "^" (regexp-quote todo-category-beg)))
5234 ;; (forward-line -2))
5235 ;; ((looking-at (concat "^" (regexp-quote todo-category-done)))
5236 ;; (forward-line -1))
5238 ;; (goto-char opoint)))))))
5240 (defun todo-backward-item (&optional count
)
5241 "Move point up to start of item with next higher priority.
5242 With positive numerical prefix COUNT, move point COUNT items
5245 If the category's done items are visible, this command called
5246 with a prefix argument only moves point to a higher item, e.g.,
5247 with point on the first done item and called with prefix 1, it
5248 moves to the last todo item; but if called with point on the
5249 first done item without a prefix argument, it moves point to the
5250 empty line above the done items separator."
5251 (let* ((done (todo-done-item-p)))
5254 (re-search-backward (concat todo-item-start
5255 "\\( " diary-time-regexp
"\\)?"
5256 (regexp-quote todo-nondiary-end
) "? ")
5258 ;; If the item date-time header is hidden, the display engine
5259 ;; moves point to the next earlier displayable position, which
5260 ;; is the end of the next item above, so we move it to the start
5261 ;; of the current item's text (that's what the display engine
5262 ;; does with todo-forward-item in this case.)
5263 ;; FIXME: would it be better to use cursor-sensor-functions?
5264 (when todo--item-headers-hidden
(goto-char (match-end 0))))
5265 ;; Unless this is a regexp filtered items buffer (which can contain
5266 ;; intermixed todo and done items), if points advances by one from a
5267 ;; done to a todo item, go back to the space above
5268 ;; todo-done-separator, since that is a legitimate place to insert an
5269 ;; item. But skip this space if count > 1, since that should only
5271 (when (and done
(not (todo-done-item-p)) (not count
)
5272 ;(or (not count) (= count 1))
5273 (not (equal (buffer-name) todo-regexp-items-buffer
)))
5274 (re-search-forward (concat "^" (regexp-quote todo-category-done
))
5276 (forward-line -
1))))
5278 (defun todo-remove-item ()
5279 "Internal function called in editing, deleting or moving items."
5280 (let ((end (progn (todo-item-end) (1+ (point))))
5281 (beg (todo-item-start))
5283 (push (todo-get-overlay 'prefix
) ovs
)
5284 (push (todo-get-overlay 'header
) ovs
)
5285 (dolist (ov ovs
) (when ov
(delete-overlay ov
)))
5286 (delete-region beg end
)))
5288 (defun todo-diary-item-p ()
5289 "Return non-nil if item at point has diary entry format."
5291 (when (todo-item-string) ; Exclude empty lines.
5293 (not (looking-at (regexp-quote todo-nondiary-start
))))))
5295 ;; This duplicates the item locating code from diary-goto-entry, but
5296 ;; without the marker code, to test whether the latter is dispensable.
5297 ;; If it is, diary-goto-entry can be simplified. The code duplication
5298 ;; here can also be eliminated, leaving only the widening and category
5299 ;; selection, and instead of :override advice :around can be used.
5301 (defun todo-diary-goto-entry (button)
5302 "Jump to the diary entry for the BUTTON at point.
5303 If the entry is a todo item, display its category properly.
5304 Overrides `diary-goto-entry'."
5305 ;; Locate the diary item in its source file.
5306 (let* ((locator (button-get button
'locator
))
5307 (file (cadr locator
))
5308 (date (regexp-quote (nth 2 locator
)))
5309 (content (regexp-quote (nth 3 locator
))))
5310 (if (not (and (file-exists-p file
)
5311 (find-file-other-window file
)))
5312 (message "Unable to locate this diary entry")
5313 ;; If it's a Todo file, make sure it's in Todo mode.
5314 (when (and (equal (file-name-directory (file-truename file
))
5315 (file-truename todo-directory
))
5316 (not (derived-mode-p 'todo-mode
)))
5318 (when (eq major-mode
'todo-mode
) (widen))
5319 (goto-char (point-min))
5320 (when (re-search-forward (format "%s.*\\(%s\\)" date content
) nil t
)
5321 (goto-char (match-beginning 1)))
5322 ;; If it's a todo item, determine its category and display the
5323 ;; category properly.
5324 (when (eq major-mode
'todo-mode
)
5325 (let ((opoint (point)))
5326 (re-search-backward (concat "^" (regexp-quote todo-category-beg
)
5329 (todo-category-number (match-string 1))
5330 (todo-category-select)
5331 (if transient-mark-mode
(deactivate-mark))
5332 (goto-char opoint
))))))
5334 (add-function :override diary-goto-entry-function
#'todo-diary-goto-entry
)
5336 (defun todo-revert-buffer (&optional ignore-auto noconfirm
)
5337 "Call `revert-buffer', preserving buffer's current modes.
5338 Also preserve category display, if applicable."
5339 (interactive (list (not current-prefix-arg
)))
5340 (let ((revert-buffer-function nil
))
5341 (revert-buffer ignore-auto noconfirm
'preserve-modes
)
5342 (when (memq major-mode
'(todo-mode todo-archive-mode
))
5343 (save-excursion (todo-category-select))
5344 ;; revert-buffer--default calls after-find-file, which makes
5346 (setq buffer-read-only t
))))
5348 (defun todo-desktop-save-buffer (_dir)
5349 `((catnum .
,(todo-category-number (todo-current-category)))))
5351 (declare-function desktop-restore-file-buffer
"desktop"
5352 (buffer-filename buffer-name buffer-misc
))
5354 (defun todo-restore-desktop-buffer (file buffer misc
)
5355 (desktop-restore-file-buffer file buffer misc
)
5356 (with-current-buffer buffer
5358 (let ((todo-category-number (cdr (assq 'catnum misc
))))
5359 (todo-category-select)
5362 (add-to-list 'desktop-buffer-mode-handlers
5363 '(todo-mode . todo-restore-desktop-buffer
))
5365 (defun todo-done-item-p ()
5366 "Return non-nil if item at point is a done item."
5369 (looking-at todo-done-string-start
)))
5371 (defun todo-done-item-section-p ()
5372 "Return non-nil if point is in category's done items section."
5374 (or (re-search-backward (concat "^" (regexp-quote todo-category-done
))
5376 (progn (goto-char (point-min))
5377 (looking-at todo-done-string-start
)))))
5379 (defun todo--user-error-if-marked-done-item ()
5380 "Signal user error on marked done items.
5381 Helper function for editing commands that apply only to (possibly
5382 marked) not done todo items."
5385 (goto-char (point-max))
5386 (todo-backward-item)
5387 (unless (todo-done-item-p)
5389 (unless (re-search-forward
5390 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
5391 (goto-char (point-max)))
5393 (while (todo-done-item-p)
5394 (when (todo-marked-item-p)
5395 (user-error "This command does not apply to done items"))
5396 (todo-backward-item)))))
5398 (defun todo-reset-done-separator (sep)
5399 "Replace existing overlays of done items separator string SEP."
5403 (goto-char (point-min))
5404 (while (re-search-forward
5405 (concat "\n\\(" (regexp-quote todo-category-done
) "\\)") nil t
)
5406 (let* ((beg (match-beginning 1))
5408 (ov (progn (goto-char beg
)
5409 (todo-get-overlay 'separator
)))
5410 (old-sep (when ov
(overlay-get ov
'display
)))
5413 (unless (string= old-sep sep
)
5414 (setq new-ov
(make-overlay beg end
))
5415 (overlay-put new-ov
'todo
'separator
)
5416 (overlay-put new-ov
'display todo-done-separator
)
5417 (delete-overlay ov
))))))))
5419 (defun todo-get-overlay (val)
5420 "Return the overlay at point whose `todo' property has value VAL."
5422 ;; When headers are hidden, the display engine makes item's start
5423 ;; inaccessible to commands, so then we have to go there
5424 ;; non-interactively to check for prefix and header overlays.
5425 (when (memq val
'(prefix header
))
5426 (unless (looking-at todo-item-start
) (todo-item-start)))
5427 ;; Use overlays-in to find prefix overlays and check over two
5428 ;; positions to find done separator overlay.
5429 (let ((ovs (overlays-in (point) (1+ (point))))
5434 (when (eq (overlay-get ov
'todo
) val
)
5435 (throw 'done ov
)))))))
5437 (defun todo-marked-item-p ()
5438 "Non-nil if this item begins with `todo-item-mark'.
5439 In that case, return the item's prefix overlay."
5440 (let* ((ov (todo-get-overlay 'prefix
))
5441 ;; If an item insertion command is called on a todo file
5442 ;; before it is visited, it has no prefix overlays yet, so
5444 (pref (when ov
(overlay-get ov
'before-string
)))
5446 (string-match (concat "^" (regexp-quote todo-item-mark
))
5450 (defun todo-insert-with-overlays (item)
5451 "Insert ITEM at point and update prefix and header overlays."
5453 (let ((ov (todo-get-overlay 'prefix
))
5454 (marked (todo-marked-item-p)))
5456 ;; Insertion pushes item down but not its prefix overlay. When
5457 ;; the overlay includes a mark, this would now mark the inserted
5458 ;; ITEM, so move it to the pushed down item.
5459 (when marked
(move-overlay ov
(point) (point)))
5460 (todo-backward-item)
5461 ;; With hidden headers, todo-backward-item puts point on first
5462 ;; visible character after header, so we have to search backward.
5463 (when todo--item-headers-hidden
5464 (re-search-backward (concat todo-item-start
5465 "\\( " diary-time-regexp
"\\)?"
5466 (regexp-quote todo-nondiary-end
) "? ")
5468 (setq ov
(make-overlay (match-beginning 0) (match-end 0) nil t
))
5469 (overlay-put ov
'todo
'header
)
5470 (overlay-put ov
'display
"")))
5471 (todo-prefix-overlays))
5473 (defun todo-prefix-overlays ()
5474 "Update the prefix overlays of the current category's items.
5475 The overlay's value is the string `todo-prefix' or with non-nil
5476 `todo-number-prefix' an integer in the sequence from 1 to
5477 the number of todo or done items in the category indicating the
5478 item's priority. Todo and done items are numbered independently
5481 (cat-tp (or (cdr (assoc-string
5482 (todo-current-category)
5483 (nth 2 (assoc-string todo-current-todo-file
5484 todo-top-priorities-overrides
))))
5485 (nth 1 (assoc-string todo-current-todo-file
5486 todo-top-priorities-overrides
))
5487 todo-top-priorities
))
5490 (goto-char (point-min))
5492 (when (or (todo-date-string-matcher (line-end-position))
5493 (todo-done-string-matcher (line-end-position)))
5494 (goto-char (match-beginning 0))
5496 ;; Reset number to 1 for first done item.
5497 (when (and (eq major-mode
'todo-mode
)
5498 (looking-at todo-done-string-start
)
5499 (looking-back (concat "^"
5500 (regexp-quote todo-category-done
)
5502 (line-beginning-position 0)))
5505 (setq prefix
(concat (propertize
5506 (if todo-number-prefix
5507 (number-to-string num
)
5510 ;; Prefix of top priority items has a
5511 ;; distinct face in Todo mode.
5512 (if (and (eq major-mode
'todo-mode
)
5516 'todo-prefix-string
))
5518 (let ((ov (todo-get-overlay 'prefix
))
5519 (marked (todo-marked-item-p)))
5520 ;; Prefix overlay must be at a single position so its
5521 ;; bounds aren't changed when (re)moving an item.
5522 (unless ov
(setq ov
(make-overlay (point) (point))))
5523 (overlay-put ov
'todo
'prefix
)
5524 (overlay-put ov
'before-string
(if marked
5525 (concat todo-item-mark prefix
)
5529 ;; -----------------------------------------------------------------------------
5530 ;;; Generating and applying item insertion and editing key sequences
5531 ;; -----------------------------------------------------------------------------
5533 ;; Thanks to Stefan Monnier for (i) not only suggesting dynamically
5534 ;; generating item insertion commands and their key bindings but also
5535 ;; offering an elegant implementation which, however, since it used
5536 ;; lexical binding, was at the time incompatible with the Calendar and
5537 ;; Diary code in todo-mode.el; and (ii) later making that code
5538 ;; compatible with lexical binding, so that his implementation, of
5539 ;; which the following is a somewhat expanded version, could be
5540 ;; realized in todo-mode.el.
5542 (defconst todo-insert-item--parameters
5543 '((default copy
) (diary nonmarking
) (calendar date dayname
) time
(here region
))
5544 "List of all item insertion parameters.
5545 Passed by `todo-insert-item' to `todo-insert-item--next-param' to
5546 dynamically create item insertion commands.")
5548 (defun todo-insert-item--next-param (args params last keys-so-far
)
5549 "Generate and invoke an item insertion command.
5550 Dynamically generate the command, its arguments ARGS and its key
5551 binding by recursing through the list of parameters PARAMS,
5552 taking the LAST from a sublist and prompting with KEYS-SO-FAR
5553 keys already entered and those still available."
5555 (let* ((map (make-sparse-keymap))
5556 (param-key-alist '((default .
"i")
5566 ;; Return key paired with given item insertion parameter.
5567 (key-of (lambda (param) (cdr (assoc param param-key-alist
))))
5568 ;; The key just typed.
5569 (this-key (lambda () (char-to-string last-command-event
)))
5571 ;; Add successively entered keys to the prompt and show what
5572 ;; possibilities remain.
5579 (if (memq name
'(default diary calendar here
))
5582 (when (memq name
'(copy nonmarking dayname region
))
5584 (propertize key
'face
'todo-key-prompt
)
5586 ;; Return the sublist of the given list of parameters whose
5587 ;; first member is paired with the given key.
5590 (setq lst
(if (consp lst
) lst
(list lst
)))
5596 (when (equal key
(funcall key-of s
))
5597 (throw 'found1
(setq sym s
))))))
5604 (setq lst
(reverse l
)))
5605 (memq (catch 'found2
5606 (dolist (e param-key-alist
)
5607 (when (equal key
(cdr e
))
5608 (throw 'found2
(car e
)))))
5610 ;; Build list of arguments for item insertion and then
5611 ;; execute the basic insertion function. The list consists of
5612 ;; item insertion parameters that can be passed as insertion
5613 ;; command arguments in fixed positions. If a position in
5614 ;; the list is not occupied by the corresponding parameter,
5615 ;; it is occupied by nil.
5618 (let* ((arg (list (car args
))) ; Possible prefix argument.
5619 (rest (nconc (cdr args
)
5620 (list (car (funcall get-params
5623 (parlist (if (= 4 (length rest
))
5625 (let ((v (make-vector 4 nil
)) elt
)
5627 (setq elt
(pop rest
))
5628 (cond ((memq elt
'(diary nonmarking
))
5630 ((memq elt
'(calendar date dayname
))
5634 ((memq elt
'(copy here region
))
5637 (apply #'todo-insert-item--basic
(nconc arg parlist
)))))
5638 ;; Operate on a copy of the parameter list so the original is
5639 ;; not consumed, thus available for the next key typed.
5642 (if (memq last
'(default copy
))
5645 (funcall gen-and-exec
))
5646 (let ((key (funcall key-of last
)))
5647 (funcall add-to-prompt key
(make-symbol
5648 (concat (symbol-name last
) ":GO!")))
5649 (define-key map
(funcall key-of last
)
5650 (lambda () (interactive)
5651 (funcall gen-and-exec
))))))
5653 (let* ((x (car params0
))
5654 (restparams (cdr params0
)))
5655 (dolist (param (if (consp x
) x
(list x
)))
5656 (let ((key (funcall key-of param
)))
5657 (funcall add-to-prompt key param
)
5659 (if (null restparams
)
5660 (lambda () (interactive)
5661 (funcall gen-and-exec
))
5662 (lambda () (interactive)
5663 (setq keys-so-far
(concat keys-so-far
" " (funcall this-key
)))
5664 (todo-insert-item--next-param
5665 (nconc args
(list (car (funcall get-params
5666 (funcall this-key
) param
))))
5667 (cdr (funcall get-params
(funcall this-key
) params
))
5668 (car (funcall get-params
(funcall this-key
) param
))
5670 (setq params0 restparams
)))
5671 (set-transient-map map
)
5672 (when prompt
(message "Press a key (so far `%s'): %s" keys-so-far prompt
))
5673 (setq params0 params
)))
5675 (defun todo-edit-item--next-key (type &optional arg
)
5676 (let* ((todo-param-key-alist '((edit .
"e")
5683 (done-param-key-alist '((add/edit .
"c")
5685 (date-param-key-alist '((full .
"f")
5693 ('todo todo-param-key-alist
)
5694 ('done done-param-key-alist
)
5695 ('date date-param-key-alist
)))
5696 (p->k
(mapconcat (lambda (elt)
5698 (propertize (cdr elt
) 'face
5700 (concat (symbol-name (car elt
))
5701 (when (memq (car elt
)
5705 (key-prompt (substitute-command-keys
5706 (concat "Press a key (so far `e"
5707 (if (eq type
'date
) " d" "")
5709 (this-key (let ((key (read-key (concat key-prompt p-
>k
))))
5710 (and (characterp key
) (char-to-string key
))))
5711 (this-param (car (rassoc this-key params
))))
5713 ('edit
(todo-edit-item--text))
5714 ('header
(todo-edit-item--text 'include-header
))
5715 ('multiline
(todo-edit-item--text 'multiline
))
5716 ('add
/edit
(todo-edit-item--text 'comment-edit
))
5717 ('delete
(todo-edit-item--text 'comment-delete
))
5718 ('diary
(todo-edit-item--diary-inclusion))
5719 ('nonmarking
(todo-edit-item--diary-inclusion 'nonmarking
))
5720 ('date
(todo-edit-item--next-key 'date arg
))
5721 ('full
(progn (todo-edit-item--header 'date
)
5722 (when todo-always-add-time-string
5723 (todo-edit-item--header 'time
))))
5724 ('calendar
(todo-edit-item--header 'calendar
))
5725 ('today
(todo-edit-item--header 'today
))
5726 ('dayname
(todo-edit-item--header 'dayname
))
5727 ('year
(todo-edit-item--header 'year arg
))
5728 ('month
(todo-edit-item--header 'month arg
))
5729 ('daynum
(todo-edit-item--header 'day arg
))
5730 ('time
(todo-edit-item--header 'time
)))))
5732 ;; -----------------------------------------------------------------------------
5733 ;;; Todo minibuffer utilities
5734 ;; -----------------------------------------------------------------------------
5736 (defcustom todo-y-with-space nil
5737 "Non-nil means allow SPC to affirm a \"y or n\" question."
5741 (defun todo-y-or-n-p (prompt)
5742 "Ask \"y or n\" question PROMPT and return t if answer is \"y\".
5743 Also return t if answer is \"Y\", but unlike `y-or-n-p', allow
5744 SPC to affirm the question only if option `todo-y-with-space' is
5746 (unless todo-y-with-space
5747 (define-key query-replace-map
" " 'ignore
))
5750 (define-key query-replace-map
" " 'act
)))
5752 (defun todo-category-completions (&optional archive
)
5753 "Return a list of completions for `todo-read-category'.
5754 Each element of the list is a cons of a category name and the
5755 file or list of files (as short file names) it is in. The files
5756 are either the current (or if there is none, the default) todo
5757 file plus the files listed in `todo-category-completions-files',
5758 or, with non-nil ARCHIVE, the current archive file.
5760 Before calculating the completions, update the value of
5761 `todo-category-completions-files' in case any files named in it
5764 (dolist (f todo-category-completions-files
)
5765 (unless (file-exists-p (todo-absolute-file-name f
))
5766 (setq todo-category-completions-files
5767 (delete f todo-category-completions-files
))
5770 (let ((pl (> (length deleted
) 1))
5771 (names (mapconcat (lambda (f) (concat "\"" f
"\"")) deleted
", ")))
5772 (message (concat "File" (if pl
"s" "") " %s ha" (if pl
"ve" "s")
5773 " been deleted and removed from\n"
5774 "the list of category completion files")
5776 (put 'todo-category-completions-files
'custom-type
5777 `(set ,@(todo--files-type-list)))
5778 (custom-set-default 'todo-category-completions-files
5779 (symbol-value 'todo-category-completions-files
))
5781 (let* ((curfile (or todo-current-todo-file
5782 (and todo-show-current-file
5783 todo-global-current-todo-file
)
5784 (todo-absolute-file-name todo-default-todo-file
)))
5785 (files (or (unless archive
5786 (mapcar #'todo-absolute-file-name
5787 todo-category-completions-files
))
5790 ;; If file was just added, it has no category completions.
5791 (unless (zerop (buffer-size (find-buffer-visiting curfile
)))
5792 (unless (member curfile todo-archives
)
5793 (cl-pushnew curfile files
:test
#'equal
))
5794 (dolist (f files listall
)
5795 (with-current-buffer (find-file-noselect f
'nowarn
)
5797 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
5798 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
5799 ;; Ensure category is properly displayed in case user
5800 ;; switches to file via a non-Todo mode command. And if
5801 ;; done items in category are visible, keep them visible.
5802 (let ((done todo-show-with-done
))
5803 (when (> (buffer-size) (- (point-max) (point-min)))
5805 (goto-char (point-min))
5806 (setq done
(re-search-forward todo-done-string-start nil t
))))
5807 (let ((todo-show-with-done done
))
5808 (save-excursion (todo-category-select))))
5812 (goto-char (point-min))
5813 (setq listf
(read (buffer-substring-no-properties
5814 (line-beginning-position)
5815 (line-end-position)))))))
5816 (mapc (lambda (elt) (let* ((cat (car elt
))
5817 (la-elt (assoc cat listall
)))
5819 (setcdr la-elt
(append (list (cdr la-elt
))
5821 (push (cons cat f
) listall
))))
5824 (defun todo-read-file-name (prompt &optional archive mustmatch
)
5825 "Choose and return the name of a todo file, prompting with PROMPT.
5827 Show completions with TAB or SPC; the names are shown in short
5828 form but the absolute truename is returned. With non-nil ARCHIVE
5829 return the absolute truename of a todo archive file. With non-nil
5830 MUSTMATCH the name of an existing file must be chosen;
5831 otherwise, a new file name is allowed."
5832 (let* ((completion-ignore-case todo-completion-ignore-case
)
5833 (files (mapcar #'todo-short-file-name
5834 ;; (funcall todo-files-function archive)))
5835 (if archive todo-archives todo-files
)))
5836 (file (completing-read prompt files nil mustmatch nil nil
5838 ;; If user hit RET without
5839 ;; choosing a file, default to
5840 ;; current or default file.
5841 (todo-short-file-name
5842 (or todo-current-todo-file
5843 (and todo-show-current-file
5844 todo-global-current-todo-file
)
5845 (todo-absolute-file-name
5846 todo-default-todo-file
)))
5847 ;; Trigger prompt for initial file.
5849 (unless (file-exists-p todo-directory
)
5850 (make-directory todo-directory
))
5851 (unless (or mustmatch
(member file files
))
5852 (setq file
(todo-validate-name file
'file
)))
5853 (setq file
(file-truename (concat todo-directory file
5854 (if archive
".toda" ".todo"))))))
5856 (defun todo-read-category (prompt &optional match-type file
)
5857 "Choose and return a category name, prompting with PROMPT.
5858 Show completions for existing categories with TAB or SPC.
5860 The argument MATCH-TYPE specifies the matching requirements on
5861 the category name: with the value `todo' or `archive' the name
5862 must complete to that of an existing todo or archive category,
5863 respectively; with the value `add' the name must not be that of
5864 an existing category; with all other values both existing and new
5865 valid category names are accepted.
5867 With non-nil argument FILE prompt for a file and complete only
5868 against categories in that file; otherwise complete against all
5869 categories from `todo-category-completions-files'."
5870 ;; Allow SPC to insert spaces, for adding new category names.
5871 (let ((minibuffer-local-completion-map
5872 (let ((map (make-sparse-keymap)))
5873 (set-keymap-parent map minibuffer-local-completion-map
)
5874 (define-key map
" " nil
)
5876 (let* ((add (eq match-type
'add
))
5877 (archive (eq match-type
'archive
))
5878 (file0 (when (and file
(> (length todo-files
) 1))
5879 (todo-read-file-name (concat "Choose a" (if archive
5884 (completions (unless file0
(todo-category-completions archive
)))
5885 (categories (cond (file0
5886 (with-current-buffer
5887 (find-file-noselect file0
'nowarn
)
5888 (unless (derived-mode-p 'todo-mode
) (todo-mode))
5889 (let ((todo-current-todo-file file0
))
5891 ((and add
(not file
))
5892 (with-current-buffer
5893 (find-file-noselect todo-current-todo-file
)
5897 (completion-ignore-case todo-completion-ignore-case
)
5898 (cat (completing-read prompt categories nil
5899 (eq match-type
'todo
) nil nil
5900 ;; Unless we're adding a category via
5901 ;; todo-add-category, set default
5902 ;; for existing categories to the
5903 ;; current category of the chosen
5904 ;; file or else of the current file.
5905 (if (and categories
(not add
))
5906 (with-current-buffer
5909 todo-current-todo-file
5910 (todo-absolute-file-name
5911 todo-default-todo-file
)))
5912 (todo-current-category))
5913 ;; Trigger prompt for initial category.
5915 (catfil (cdr (assoc cat completions
)))
5916 (str "Category \"%s\" from which file (TAB for choices)? "))
5917 ;; If we do category completion and the chosen category name
5918 ;; occurs in more than one file, prompt to choose one file.
5919 (unless (or file0 add
(not catfil
))
5920 (setq file0
(file-truename
5923 (todo-absolute-file-name
5924 (let ((files (mapcar #'todo-short-file-name catfil
)))
5925 (completing-read (format str cat
) files
)))))))
5926 ;; Default to the current file.
5927 (unless file0
(setq file0 todo-current-todo-file
))
5928 ;; First validate only a name passed interactively from
5929 ;; todo-add-category, which must be of a nonexistent category.
5930 (unless (and (assoc cat categories
) (not add
))
5931 ;; Validate only against completion categories.
5932 (let ((todo-categories categories
))
5933 (setq cat
(todo-validate-name cat
'category
)))
5934 ;; When user enters a nonexistent category name by jumping or
5935 ;; moving, confirm that it should be added, then validate.
5937 (if (todo-y-or-n-p (format "Add new category \"%s\" to file \"%s\"? "
5938 cat
(todo-short-file-name file0
)))
5940 (when (assoc cat categories
)
5941 (let ((todo-categories categories
))
5942 (setq cat
(todo-validate-name cat
'category
))))
5943 ;; Restore point and narrowing after adding new
5944 ;; category, to avoid moving to beginning of file when
5945 ;; moving marked items to a new category
5946 ;; (todo-move-item).
5949 (todo-add-category file0 cat
))))
5950 ;; If we decide not to add a category, exit without returning.
5954 (defun todo-validate-name (name type
)
5955 "Prompt for new NAME for TYPE until it is valid, then return it.
5956 TYPE can be either of the symbols `file' or `category'."
5957 (let ((categories todo-categories
)
5958 (files (mapcar #'todo-short-file-name todo-files
))
5962 (cond ((string= "" name
)
5964 (cond ((eq type
'file
)
5966 "Enter a non-empty file name: "
5967 ;; Empty string passed by todo-show to
5968 ;; prompt for initial todo file.
5969 (concat "Initial file name ["
5970 todo-initial-file
"]: ")))
5971 ((eq type
'category
)
5973 "Enter a non-empty category name: "
5974 ;; Empty string passed by todo-show to
5975 ;; prompt for initial category of a new
5977 (concat "Initial category name ["
5978 todo-initial-category
"]: "))))))
5979 ((string-match "\\`\\s-+\\'" name
)
5981 "Enter a name that does not contain only white space: "))
5982 ((and (eq type
'file
) (member name files
))
5983 (setq prompt
"Enter a non-existing file name: "))
5984 ((and (eq type
'category
) (assoc name categories
))
5985 (setq prompt
"Enter a non-existing category name: ")))
5986 (setq name
(if (or (and (eq type
'file
) files
)
5987 (and (eq type
'category
) categories
))
5988 (completing-read prompt
(cond ((eq type
'file
)
5990 ((eq type
'category
)
5992 ;; Offer default initial name.
5993 (completing-read prompt
(if (eq type
'file
)
5996 nil nil
(if (eq type
'file
)
5998 todo-initial-category
))))))
6001 ;; Adapted from calendar-read-date and calendar-date-string.
6002 (defun todo-read-date (&optional arg mo yr
)
6003 "Prompt for Gregorian date and return it in the current format.
6005 With non-nil ARG, prompt for and return only the date component
6006 specified by ARG, which can be one of these symbols:
6007 `month' (prompt for name, return name or number according to
6008 value of `calendar-date-display-form'), `day' of month, or
6009 `year'. The value of each of these components can be `*',
6010 indicating an unspecified month, day, or year.
6012 When ARG is `day', non-nil arguments MO and YR determine the
6013 number of the last the day of the month."
6015 (year monthname month day dayname
) ;Needed by calendar-date-display-form.
6016 (when (or (not arg
) (eq arg
'year
))
6017 (while (if (natnump year
) (< year
1) (not (eq year
'*)))
6018 (setq year
(read-from-minibuffer
6019 "Year (>0 or RET for this year or * for any year): "
6020 nil nil t nil
(number-to-string
6021 (calendar-extract-year
6022 (calendar-current-date)))))))
6023 (when (or (not arg
) (eq arg
'month
))
6024 (let* ((marray todo-month-name-array
)
6025 (mlist (append marray nil
))
6026 (mabarray todo-month-abbrev-array
)
6027 (mablist (append mabarray nil
))
6028 (completion-ignore-case todo-completion-ignore-case
))
6029 (setq monthname
(completing-read
6030 "Month name (RET for current month, * for any month): "
6032 (calendar-month-name
6033 (calendar-extract-month (calendar-current-date)) t
))
6034 month
(1+ (- (length mlist
)
6035 (length (or (member monthname mlist
)
6036 (member monthname mablist
))))))
6037 (setq monthname
(aref mabarray
(1- month
)))))
6038 (when (or (not arg
) (eq arg
'day
))
6039 (let ((last (let ((mm (or month mo
))
6041 ;; If month is unspecified, use a month with 31
6042 ;; days for checking day of month input. Does
6043 ;; Calendar do anything special when * is
6044 ;; currently a shorter month?
6045 (if (= mm
13) (setq mm
1))
6046 ;; If year is unspecified, use a leap year to
6048 (if (eq year
'*) (setq yy
2012))
6049 (calendar-last-day-of-month mm yy
))))
6050 (while (if (natnump day
) (or (< day
1) (> day last
)) (not (eq day
'*)))
6051 (setq day
(read-from-minibuffer
6052 (format "Day (1-%d or RET for today or * for any day): "
6054 nil nil t nil
(number-to-string
6055 (calendar-extract-day
6056 (calendar-current-date))))))))
6057 ;; Stringify read values (monthname is already a string).
6058 (and year
(setq year
(if (eq year
'*)
6060 (number-to-string year
))))
6061 (and day
(setq day
(if (eq day
'*)
6063 (number-to-string day
))))
6064 (and month
(setq month
(if (= month
13)
6066 (number-to-string month
))))
6068 (cond ((eq arg
'year
) year
)
6071 (if (memq 'month calendar-date-display-form
)
6074 (mapconcat #'eval calendar-date-display-form
""))))
6076 (defun todo-read-dayname ()
6077 "Choose name of a day of the week with completion and return it."
6078 (let ((completion-ignore-case todo-completion-ignore-case
))
6079 (completing-read "Enter a day name: "
6080 (append calendar-day-name-array nil
)
6083 (defun todo-read-time ()
6084 "Prompt for and return a valid clock time as a string.
6086 Valid time strings are those matching `diary-time-regexp'.
6087 Typing `<return>' at the prompt returns the current time, if the
6088 user option `todo-always-add-time-string' is non-nil, otherwise
6089 the empty string (i.e., no time string)."
6092 (setq answer
(read-string "Enter a clock time: " nil nil
6093 (when todo-always-add-time-string
6094 (substring (current-time-string) 11 16))))
6095 (when (or (string= "" answer
)
6096 (string-match diary-time-regexp answer
))
6100 ;; -----------------------------------------------------------------------------
6101 ;;; Customization groups and utilities
6102 ;; -----------------------------------------------------------------------------
6105 "Create and maintain categorized lists of todo items."
6106 :link
'(emacs-commentary-link "todo")
6110 (defgroup todo-edit nil
6111 "User options for adding and editing todo items."
6115 (defgroup todo-categories nil
6116 "User options for Todo Categories mode."
6120 (defgroup todo-filtered nil
6121 "User options for Todo Filter Items mode."
6125 (defgroup todo-display nil
6126 "User display options for Todo mode."
6130 (defgroup todo-faces nil
6131 "Faces for the Todo modes."
6135 (defun todo-set-show-current-file (symbol value
)
6136 "The :set function for user option `todo-show-current-file'."
6137 (custom-set-default symbol value
)
6139 (add-hook 'pre-command-hook
#'todo-show-current-file nil t
)
6140 (remove-hook 'pre-command-hook
#'todo-show-current-file t
)))
6142 (defun todo-reset-prefix (symbol value
)
6143 "The :set function for `todo-prefix' and `todo-number-prefix'."
6144 (let ((oldvalue (symbol-value symbol
))
6145 (files todo-file-buffers
))
6146 (custom-set-default symbol value
)
6147 (when (not (equal value oldvalue
))
6149 (with-current-buffer (find-file-noselect f
)
6150 ;; Activate the new setting in the current category.
6151 (save-excursion (todo-category-select)))))))
6153 (defun todo-reset-nondiary-marker (symbol value
)
6154 "The :set function for user option `todo-nondiary-marker'."
6155 (let* ((oldvalue (symbol-value symbol
))
6156 (files (append todo-files todo-archives
6157 (directory-files todo-directory t
"\\.tod[rty]$" t
))))
6158 (custom-set-default symbol value
)
6159 ;; Need to reset these to get font-locking right.
6160 (setq todo-nondiary-start
(nth 0 todo-nondiary-marker
)
6161 todo-nondiary-end
(nth 1 todo-nondiary-marker
)
6162 todo-date-string-start
6163 ;; See comment in defvar of `todo-date-string-start'.
6164 (concat "^\\(" (regexp-quote todo-nondiary-start
) "\\|"
6165 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
6166 (when (not (equal value oldvalue
))
6168 (let ((buf (find-buffer-visiting f
)))
6169 (with-current-buffer (find-file-noselect f
)
6170 (let (buffer-read-only)
6172 (goto-char (point-min))
6174 (if (re-search-forward
6175 (concat "^\\(" todo-done-string-start
"[^][]+] \\)?"
6176 "\\(?1:" (regexp-quote (car oldvalue
))
6177 "\\)" todo-date-pattern
"\\( "
6178 diary-time-regexp
"\\)?\\(?2:"
6179 (regexp-quote (cadr oldvalue
)) "\\)")
6182 (replace-match (nth 0 value
) t t nil
1)
6183 (replace-match (nth 1 value
) t t nil
2))
6186 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6187 (todo-category-select))
6189 (kill-buffer)))))))))
6191 (defun todo-reset-done-separator-string (symbol value
)
6192 "The :set function for `todo-done-separator-string'."
6193 (let ((oldvalue (symbol-value symbol
))
6194 (files todo-file-buffers
)
6195 (sep todo-done-separator
))
6196 (custom-set-default symbol value
)
6197 (when (not (equal value oldvalue
))
6199 (with-current-buffer (find-file-noselect f
)
6200 (let (buffer-read-only)
6201 (setq todo-done-separator
(todo-done-separator))
6202 (when (= 1 (length value
))
6203 (todo-reset-done-separator sep
)))
6204 (todo-category-select))))))
6206 (defun todo-reset-done-string (symbol value
)
6207 "The :set function for user option `todo-done-string'."
6208 (let ((oldvalue (symbol-value symbol
))
6209 (files (append todo-files todo-archives
6210 (directory-files todo-directory t
"\\.todr$" t
))))
6211 (custom-set-default symbol value
)
6212 ;; Need to reset this to get font-locking right.
6213 (setq todo-done-string-start
6214 (concat "^\\[" (regexp-quote todo-done-string
)))
6215 (when (not (equal value oldvalue
))
6217 (let ((buf (find-buffer-visiting f
)))
6218 (with-current-buffer (find-file-noselect f
)
6219 (let (buffer-read-only)
6221 (goto-char (point-min))
6223 (if (re-search-forward
6224 (concat "^" (regexp-quote todo-nondiary-start
)
6225 "\\(" (regexp-quote oldvalue
) "\\)")
6227 (replace-match value t t nil
1)
6230 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6231 (todo-category-select))
6233 (kill-buffer)))))))))
6235 (defun todo-reset-comment-string (symbol value
)
6236 "The :set function for user option `todo-comment-string'."
6237 (let ((oldvalue (symbol-value symbol
))
6238 (files (append todo-files todo-archives
6239 (directory-files todo-directory t
"\\.todr$" t
))))
6240 (custom-set-default symbol value
)
6241 (when (not (equal value oldvalue
))
6243 (let ((buf (find-buffer-visiting f
)))
6244 (with-current-buffer (find-file-noselect f
)
6245 (let (buffer-read-only)
6247 (goto-char (point-min))
6249 (if (re-search-forward
6250 (concat "\\[\\(" (regexp-quote oldvalue
)
6253 (replace-match value t t nil
1)
6256 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6257 (todo-category-select))
6259 (kill-buffer)))))))))
6261 (defun todo-reset-highlight-item (symbol value
)
6262 "The :set function for user option `todo-highlight-item'."
6263 (let ((oldvalue (symbol-value symbol
))
6264 (files (append todo-files todo-archives
6265 (directory-files todo-directory t
"\\.tod[rty]$" t
))))
6266 (custom-set-default symbol value
)
6267 (when (not (equal value oldvalue
))
6269 (let ((buf (find-buffer-visiting f
)))
6271 (with-current-buffer buf
6275 (hl-line-mode -
1)))))))))
6277 (defun todo-update-filelist-defcustoms ()
6278 "Update defcustoms that provide choice list of todo files."
6279 (put 'todo-default-todo-file
'custom-type
`(radio ,@(todo--files-type-list)))
6280 (put 'todo-category-completions-files
'custom-type
6281 `(set ,@(todo--files-type-list)))
6282 (put 'todo-filter-files
'custom-type
`(set ,@(todo--files-type-list))))
6284 ;; -----------------------------------------------------------------------------
6286 ;; -----------------------------------------------------------------------------
6288 (defun todo-nondiary-marker-matcher (lim)
6289 "Search for todo item nondiary markers within LIM for font-locking."
6290 (re-search-forward (concat "^\\(?1:" (regexp-quote todo-nondiary-start
) "\\)"
6291 todo-date-pattern
"\\(?: " diary-time-regexp
6292 "\\)?\\(?2:" (regexp-quote todo-nondiary-end
) "\\)")
6295 (defun todo-diary-nonmarking-matcher (lim)
6296 "Search for diary nonmarking symbol within LIM for font-locking."
6297 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
6298 "\\)" todo-date-pattern
)
6301 (defun todo-date-string-matcher (lim)
6302 "Search for todo item date string within LIM for font-locking."
6304 (concat todo-date-string-start
"\\(?1:" todo-date-pattern
"\\)") lim t
))
6306 (defun todo-time-string-matcher (lim)
6307 "Search for todo item time string within LIM for font-locking."
6308 (re-search-forward (concat todo-date-string-start todo-date-pattern
6309 " \\(?1:" diary-time-regexp
"\\)")
6312 (defun todo-diary-expired-matcher (lim)
6313 "Search for expired diary item date within LIM for font-locking."
6314 (when (re-search-forward (concat "^\\(?:"
6315 (regexp-quote diary-nonmarking-symbol
)
6316 "\\)?\\(?1:" todo-date-pattern
"\\) \\(?2:"
6317 diary-time-regexp
"\\)?")
6319 (let* ((date (match-string-no-properties 1))
6320 (time (match-string-no-properties 2))
6321 ;; Function days-between requires a non-empty time string.
6322 (date-time (concat date
" " (or time
"00:00"))))
6323 (or (and (not (string-match ".+day\\|\\*" date
))
6324 (< (days-between date-time
(current-time-string)) 0))
6325 (todo-diary-expired-matcher lim
)))))
6327 (defun todo-done-string-matcher (lim)
6328 "Search for done todo item header within LIM for font-locking."
6329 (re-search-forward (concat todo-done-string-start
6333 (defun todo-comment-string-matcher (lim)
6334 "Search for done todo item comment within LIM for font-locking."
6335 (re-search-forward (concat "\\[\\(?1:" todo-comment-string
"\\):")
6338 (defun todo-category-string-matcher-1 (lim)
6339 "Search for todo category name within LIM for font-locking.
6340 This is for fontifying category and file names appearing in Todo
6341 Filtered Items mode following done items."
6342 (if (eq major-mode
'todo-filtered-items-mode
)
6343 (re-search-forward (concat todo-done-string-start todo-date-pattern
6344 "\\(?: " diary-time-regexp
6345 ;; Use non-greedy operator to prevent
6346 ;; capturing possible following non-diary
6348 "\\)?] \\(?1:\\[.+?\\]\\)")
6351 (defun todo-category-string-matcher-2 (lim)
6352 "Search for todo category name within LIM for font-locking.
6353 This is for fontifying category and file names appearing in Todo
6354 Filtered Items mode following todo (not done) items."
6355 (if (eq major-mode
'todo-filtered-items-mode
)
6356 (re-search-forward (concat todo-date-string-start todo-date-pattern
6357 "\\(?: " diary-time-regexp
"\\)?\\(?:"
6358 (regexp-quote todo-nondiary-end
)
6359 "\\)? \\(?1:\\[.+\\]\\)")
6362 (defvar todo-nondiary-face
'todo-nondiary
)
6363 (defvar todo-date-face
'todo-date
)
6364 (defvar todo-time-face
'todo-time
)
6365 (defvar todo-diary-expired-face
'todo-diary-expired
)
6366 (defvar todo-done-sep-face
'todo-done-sep
)
6367 (defvar todo-done-face
'todo-done
)
6368 (defvar todo-comment-face
'todo-comment
)
6369 (defvar todo-category-string-face
'todo-category-string
)
6370 (defvar todo-font-lock-keywords
6372 '(todo-nondiary-marker-matcher 1 todo-nondiary-face t
)
6373 '(todo-nondiary-marker-matcher 2 todo-nondiary-face t
)
6374 ;; diary-lib.el uses font-lock-constant-face for diary-nonmarking-symbol.
6375 '(todo-diary-nonmarking-matcher 1 font-lock-constant-face t
)
6376 '(todo-date-string-matcher 1 todo-date-face t
)
6377 '(todo-time-string-matcher 1 todo-time-face t
)
6378 '(todo-done-string-matcher 0 todo-done-face t
)
6379 '(todo-comment-string-matcher 1 todo-comment-face t
)
6380 '(todo-category-string-matcher-1 1 todo-category-string-face t t
)
6381 '(todo-category-string-matcher-2 1 todo-category-string-face t t
)
6382 '(todo-diary-expired-matcher 1 todo-diary-expired-face t
)
6383 '(todo-diary-expired-matcher 2 todo-diary-expired-face t t
)
6385 "Font-locking for Todo modes.")
6387 ;; -----------------------------------------------------------------------------
6389 ;; -----------------------------------------------------------------------------
6391 (defvar todo-key-bindings-t
6393 ("Af" todo-find-archive
)
6394 ("Ac" todo-choose-archive
)
6395 ("Ad" todo-archive-done-item
)
6396 ("Cv" todo-toggle-view-done-items
)
6397 ("v" todo-toggle-view-done-items
)
6398 ("Ca" todo-add-category
)
6399 ("Cr" todo-rename-category
)
6400 ("Cg" todo-merge-category
)
6401 ("Cm" todo-move-category
)
6402 ("Ck" todo-delete-category
)
6403 ("Cts" todo-set-top-priorities-in-category
)
6404 ("Cey" todo-edit-category-diary-inclusion
)
6405 ("Cek" todo-edit-category-diary-nonmarking
)
6406 ("Fa" todo-add-file
)
6407 ("Fr" todo-rename-file
)
6408 ("Ff" todo-find-filtered-items-file
)
6409 ("FV" todo-toggle-view-done-only
)
6410 ("V" todo-toggle-view-done-only
)
6411 ("Ftt" todo-filter-top-priorities
)
6412 ("Ftm" todo-filter-top-priorities-multifile
)
6413 ("Fts" todo-set-top-priorities-in-file
)
6414 ("Fyy" todo-filter-diary-items
)
6415 ("Fym" todo-filter-diary-items-multifile
)
6416 ("Fxx" todo-filter-regexp-items
)
6417 ("Fxm" todo-filter-regexp-items-multifile
)
6418 ("e" todo-edit-item
)
6419 ("d" todo-item-done
)
6420 ("i" todo-insert-item
)
6421 ("k" todo-delete-item
)
6422 ("m" todo-move-item
)
6423 ("u" todo-item-undone
)
6424 ([remap newline
] newline-and-indent
)
6426 "List of key bindings for Todo mode only.")
6428 (defvar todo-key-bindings-t
+a
+f
6430 ("C*" todo-mark-category
)
6431 ("Cu" todo-unmark-category
)
6432 ("Fh" todo-toggle-item-header
)
6433 ("h" todo-toggle-item-header
)
6434 ("Fk" todo-delete-file
)
6435 ("Fe" todo-edit-file
)
6436 ("FH" todo-toggle-item-highlighting
)
6437 ("H" todo-toggle-item-highlighting
)
6438 ("FN" todo-toggle-prefix-numbers
)
6439 ("N" todo-toggle-prefix-numbers
)
6440 ("PB" todo-print-buffer
)
6441 ("PF" todo-print-buffer-to-file
)
6442 ("j" todo-jump-to-category
)
6443 ("n" todo-next-item
)
6444 ("p" todo-previous-item
)
6449 "List of key bindings for Todo, Archive, and Filtered Items modes.")
6451 (defvar todo-key-bindings-t
+a
6453 ("Fc" todo-show-categories-table
)
6455 ("X" todo-clear-matches
)
6456 ("b" todo-backward-category
)
6457 ("f" todo-forward-category
)
6458 ("*" todo-toggle-mark-item
)
6460 "List of key bindings for Todo and Todo Archive modes.")
6462 (defvar todo-key-bindings-t
+f
6464 ("l" todo-lower-item-priority
)
6465 ("r" todo-raise-item-priority
)
6466 ("#" todo-set-item-priority
)
6468 "List of key bindings for Todo and Todo Filtered Items modes.")
6470 (defvar todo-mode-map
6471 (let ((map (make-keymap)))
6472 (dolist (kb todo-key-bindings-t
)
6473 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6474 (dolist (kb todo-key-bindings-t
+a
+f
)
6475 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6476 (dolist (kb todo-key-bindings-t
+a
)
6477 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6478 (dolist (kb todo-key-bindings-t
+f
)
6479 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6481 "Todo mode keymap.")
6483 (defvar todo-archive-mode-map
6484 (let ((map (make-sparse-keymap)))
6485 (dolist (kb todo-key-bindings-t
+a
+f
)
6486 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6487 (dolist (kb todo-key-bindings-t
+a
)
6488 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6489 (define-key map
"a" 'todo-jump-to-archive-category
)
6490 (define-key map
"u" 'todo-unarchive-items
)
6492 "Todo Archive mode keymap.")
6494 (defvar todo-edit-mode-map
6495 (let ((map (make-sparse-keymap)))
6496 (define-key map
"\C-x\C-q" 'todo-edit-quit
)
6497 (define-key map
[remap newline
] 'newline-and-indent
)
6499 "Todo Edit mode keymap.")
6501 (defvar todo-categories-mode-map
6502 (let ((map (make-sparse-keymap)))
6503 (define-key map
"c" 'todo-sort-categories-alphabetically-or-numerically
)
6504 (define-key map
"t" 'todo-sort-categories-by-todo
)
6505 (define-key map
"y" 'todo-sort-categories-by-diary
)
6506 (define-key map
"d" 'todo-sort-categories-by-done
)
6507 (define-key map
"a" 'todo-sort-categories-by-archived
)
6508 (define-key map
"#" 'todo-set-category-number
)
6509 (define-key map
"l" 'todo-lower-category
)
6510 (define-key map
"r" 'todo-raise-category
)
6511 (define-key map
"n" 'todo-next-button
)
6512 (define-key map
"p" 'todo-previous-button
)
6513 (define-key map
[tab] 'todo-next-button)
6514 (define-key map [backtab] 'todo-previous-button)
6515 (define-key map "q" 'todo-quit)
6517 "Todo Categories mode keymap.")
6519 (defvar todo-filtered-items-mode-map
6520 (let ((map (make-sparse-keymap)))
6521 (dolist (kb todo-key-bindings-t+a+f)
6522 (define-key map (nth 0 kb) (nth 1 kb)))
6523 (dolist (kb todo-key-bindings-t+f)
6524 (define-key map (nth 0 kb) (nth 1 kb)))
6525 (define-key map "g" 'todo-go-to-source-item)
6526 (define-key map [remap newline] 'todo-go-to-source-item)
6528 "Todo Filtered Items mode keymap.")
6531 todo-menu todo-mode-map "Todo Menu"
6534 ["Next Item" todo-next-item t]
6535 ["Previous Item" todo-previous-item t]
6537 ["Next Category" todo-forward-category t]
6538 ["Previous Category" todo-backward-category t]
6539 ["Jump to Another Category" todo-jump-to-category t]
6541 ["Visit Another Todo File" todo-show t]
6542 ["Visit Archive" todo-find-archive t]
6543 ["Visit Filtered Items File" todo-find-filtered-items-file t]
6546 ["Insert New Item" todo-insert-item t]
6547 ["Edit Item" todo-edit-item t]
6548 ["Lower Item Priority" todo-lower-item-priority t]
6549 ["Raise Item Priority" todo-raise-item-priority t]
6550 ["Set Item Priority" todo-set-item-priority t]
6551 ["Mark/Unmark Item" todo-toggle-mark-item t]
6552 ["Move (Recategorize) Item" todo-move-item t]
6553 ["Delete Item" todo-delete-item t]
6554 ["Mark and Bury Done Item" todo-item-done t]
6555 ["Undo Done Item" todo-item-undone t]
6556 ["Archive Done Item" todo-archive-done-item t]
6558 ["Add New Category" todo-add-category t]
6559 ["Rename Current Category" todo-rename-category t]
6560 ["Delete Current Category" todo-delete-category t]
6561 ["Move Current Category" todo-move-category t]
6562 ["Merge Current Category" todo-merge-category t]
6564 ["Add New Todo File" todo-add-file t]
6565 ["Rename Todo File" todo-rename-file t]
6566 ["Delete Todo File" todo-delete-file t]
6567 ["Edit Todo File" todo-edit-file t]
6569 ("Searching and Item Filtering"
6570 ["Search Todo File" todo-search t]
6571 ["Clear Match Highlighting" todo-clear-matches t]
6573 ["Set Top Priorities in File" todo-set-top-priorities-in-file t]
6574 ["Set Top Priorities in Category" todo-set-top-priorities-in-category t]
6575 ["Filter Top Priorities" todo-filter-top-priorities t]
6576 ["Filter Multifile Top Priorities" todo-filter-top-priorities-multifile t]
6577 ["Filter Diary Items" todo-filter-diary-items t]
6578 ["Filter Multifile Diary Items" todo-filter-diary-items-multifile t]
6579 ["Filter Regexp" todo-filter-regexp-items t]
6580 ["Filter Multifile Regexp" todo-filter-regexp-items-multifile t]
6582 ("Display and Printing"
6583 ["Show/Hide Done Items" todo-toggle-view-done-items t]
6584 ["Show/Hide Done Items Only" todo-toggle-view-done-only t]
6585 ["Show/Hide Item Highlighting" todo-toggle-item-highlighting t]
6586 ["Show/Hide Item Numbering" todo-toggle-prefix-numbers t]
6587 ["Show/Hide Item Header" todo-toggle-item-header t]
6589 ["Display Table of Categories" todo-show-categories-table t]
6591 ["Print Category" todo-print-buffer t]
6592 ["Print Category to File" todo-print-buffer-to-file t]
6595 ["Save Todo File" todo-save t]
6596 ["Quit Todo Mode" todo-quit t]
6599 ;; -----------------------------------------------------------------------------
6600 ;;; Hook functions and mode definitions
6601 ;; -----------------------------------------------------------------------------
6603 (defun todo-show-current-file ()
6604 "Visit current instead of default todo file with `todo-show'.
6605 Added to `pre-command-hook' in Todo mode when user option
6606 `todo-show-current-file' is set to non-nil."
6607 (setq todo-global-current-todo-file todo-current-todo-file))
6609 ;; (defun todo-display-as-todo-file ()
6610 ;; "Show todo files correctly when visited from outside of Todo mode.
6611 ;; Added to `find-file-hook' in Todo mode and Todo Archive mode."
6612 ;; (and (member this-command todo-visit-files-commands)
6613 ;; (= (- (point-max) (point-min)) (buffer-size))
6614 ;; (member major-mode '(todo-mode todo-archive-mode))
6615 ;; (todo-category-select)))
6617 ;; (defun todo-add-to-buffer-list ()
6618 ;; "Add name of just visited todo file to `todo-file-buffers'.
6619 ;; This function is added to `find-file-hook' in Todo mode."
6620 ;; (let ((filename (file-truename (buffer-file-name))))
6621 ;; (when (member filename todo-files)
6622 ;; (add-to-list 'todo-file-buffers filename))))
6624 (defun todo-update-buffer-list ()
6625 "Make current Todo mode buffer file car of `todo-file-buffers'.
6626 This function is added to `post-command-hook' in Todo mode."
6627 (let ((filename (file-truename (buffer-file-name))))
6628 (unless (eq (car todo-file-buffers) filename)
6629 (setq todo-file-buffers
6630 (cons filename (delete filename todo-file-buffers))))))
6632 (defun todo-reset-global-current-todo-file ()
6633 "Update the value of `todo-global-current-todo-file'.
6634 This becomes the latest existing todo file or, if there is none,
6635 the value of `todo-default-todo-file'.
6636 This function is added to `kill-buffer-hook' in Todo mode."
6637 (let ((filename (file-truename (buffer-file-name))))
6638 (setq todo-file-buffers (delete filename todo-file-buffers))
6639 (setq todo-global-current-todo-file
6640 (or (car todo-file-buffers)
6641 (todo-absolute-file-name todo-default-todo-file)))))
6643 (defun todo-reset-and-enable-done-separator ()
6644 "Show resized done items separator overlay after window change.
6645 Added to `window-configuration-change-hook' in Todo mode."
6646 (when (= 1 (length todo-done-separator-string))
6647 (let ((sep todo-done-separator))
6648 (setq todo-done-separator (todo-done-separator))
6649 (save-match-data (todo-reset-done-separator sep)))))
6651 (defun todo-modes-set-1 ()
6652 "Make some settings that apply to multiple Todo modes."
6653 (setq-local font-lock-defaults '(todo-font-lock-keywords t))
6654 (setq-local revert-buffer-function #'todo-revert-buffer)
6655 (setq-local tab-width todo-indent-to-here)
6656 (setq-local indent-line-function #'todo-indent)
6657 (when todo-wrap-lines
6659 (setq wrap-prefix (make-string todo-indent-to-here 32))))
6661 (defun todo-hl-line-range ()
6662 "Make `todo-toggle-item-highlighting' highlight entire item."
6664 (when (todo-item-end)
6665 (cons (todo-item-start)
6668 (defun todo-modes-set-2 ()
6669 "Make some settings that apply to multiple Todo modes."
6670 (add-to-invisibility-spec 'todo)
6671 (setq buffer-read-only t)
6672 (setq-local todo--item-headers-hidden nil)
6673 (setq-local desktop-save-buffer 'todo-desktop-save-buffer)
6674 (setq-local hl-line-range-function #'todo-hl-line-range))
6676 (defun todo-modes-set-3 ()
6677 "Make some settings that apply to multiple Todo modes."
6678 (setq-local todo-categories (todo-set-categories))
6679 (setq-local todo-category-number 1)
6680 ;; (add-hook 'find-file-hook #'todo-display-as-todo-file nil t)
6683 (put 'todo-mode 'mode-class 'special)
6686 (define-derived-mode todo-mode special-mode "Todo"
6687 "Major mode for displaying, navigating and editing todo lists.
6690 (if (called-interactively-p 'any)
6692 (substitute-command-keys
6693 "Type `\\[todo-show]' to enter Todo mode"))
6697 ;; Initialize todo-current-todo-file.
6698 (when (member (file-truename (buffer-file-name))
6699 (funcall todo-files-function))
6700 (setq-local todo-current-todo-file (file-truename (buffer-file-name))))
6701 (setq-local todo-show-done-only nil)
6702 (setq-local todo-categories-with-marks nil)
6703 ;; (add-hook 'find-file-hook #'todo-add-to-buffer-list nil t)
6704 (add-hook 'post-command-hook #'todo-update-buffer-list nil t)
6705 (when todo-show-current-file
6706 (add-hook 'pre-command-hook #'todo-show-current-file nil t))
6707 (add-hook 'window-configuration-change-hook
6708 #'todo-reset-and-enable-done-separator nil t)
6709 (add-hook 'kill-buffer-hook #'todo-reset-global-current-todo-file nil t)))
6711 (put 'todo-archive-mode 'mode-class 'special)
6713 ;; If todo-mode is parent, all todo-mode key bindings appear to be
6714 ;; available in todo-archive-mode (e.g. shown by C-h m).
6716 (define-derived-mode todo-archive-mode special-mode "Todo-Arch"
6717 "Major mode for archived todo categories.
6719 \\{todo-archive-mode-map}"
6723 (setq-local todo-current-todo-file (file-truename (buffer-file-name)))
6724 (setq-local todo-show-done-only t))
6726 (define-derived-mode todo-edit-mode text-mode "Todo-Ed"
6727 "Major mode for editing multiline todo items.
6729 \\{todo-edit-mode-map}"
6731 (if (> (buffer-size) (- (point-max) (point-min)))
6732 ;; Editing one item in an indirect buffer, so buffer-file-name is nil.
6733 (setq-local todo-current-todo-file todo-global-current-todo-file)
6734 ;; When editing archive file, make sure it is current todo file.
6735 (setq-local todo-current-todo-file (file-truename (buffer-file-name)))
6736 ;; Need this when editing the whole file to return to the category
6737 ;; editing was invoked from.
6738 (setq-local todo-categories (todo-set-categories)))
6739 (setq buffer-read-only nil))
6741 (put 'todo-categories-mode 'mode-class 'special)
6743 (define-derived-mode todo-categories-mode special-mode "Todo-Cats"
6744 "Major mode for displaying and editing todo categories.
6746 \\{todo-categories-mode-map}"
6747 (setq-local todo-current-todo-file todo-global-current-todo-file)
6748 (setq-local todo-categories
6749 ;; Can't use find-buffer-visiting when
6750 ;; `todo-show-categories-table' is called on first
6751 ;; invocation of `todo-show', since there is then no
6752 ;; buffer visiting the current file.
6753 (with-current-buffer (find-file-noselect
6754 todo-current-todo-file 'nowarn)
6757 (put 'todo-filtered-items-mode 'mode-class 'special)
6760 (define-derived-mode todo-filtered-items-mode special-mode "Todo-Fltr"
6761 "Mode for displaying and reprioritizing top priority Todo.
6763 \\{todo-filtered-items-mode-map}"
6767 ;; -----------------------------------------------------------------------------
6768 (provide 'todo-mode)
6770 ;;; todo-mode.el ends here