1 ;;; todo-mode.el --- facilities for making and maintaining todo lists -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997, 1999, 2001-2017 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.")
192 ;; FIXME: These vars lack a prefix, but this is out of our control, because
193 ;; they're defined by Calendar, e.g. for calendar-date-display-form.
200 (defconst todo-date-pattern
201 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
202 (concat "\\(?4:\\(?5:" dayname
"\\)\\|"
204 (monthname (format "\\(?6:%s\\)" (diary-name-pattern
205 todo-month-name-array
206 todo-month-abbrev-array
)))
207 (month "\\(?7:[0-9]+\\|\\*\\)")
208 (day "\\(?8:[0-9]+\\|\\*\\)")
209 (year "-?\\(?9:[0-9]+\\|\\*\\)"))
210 (mapconcat #'eval calendar-date-display-form
""))
212 "Regular expression matching a todo item date header.")
214 ;; By itself this matches anything, because of the `?'; however, it's only
215 ;; used in the context of `todo-date-pattern' (but Emacs Lisp lacks
217 (defconst todo-date-string-start
218 (concat "^\\(" (regexp-quote todo-nondiary-start
) "\\|"
219 (regexp-quote diary-nonmarking-symbol
) "\\)?")
220 "Regular expression matching part of item header before the date.")
222 (defcustom todo-done-string
"DONE "
223 "Identifying string appended to the front of done todo items."
225 :initialize
'custom-initialize-default
226 :set
'todo-reset-done-string
229 (defconst todo-done-string-start
230 (concat "^\\[" (regexp-quote todo-done-string
))
231 "Regular expression matching start of done item.")
233 (defconst todo-item-start
(concat "\\(" todo-date-string-start
"\\|"
234 todo-done-string-start
"\\)"
236 "String identifying start of a todo item.")
238 ;; -----------------------------------------------------------------------------
239 ;;; Todo mode display options
240 ;; -----------------------------------------------------------------------------
242 (defcustom todo-prefix
""
243 "String prefixed to todo items for visual distinction."
244 :type
'(string :validate
246 (when (string= (widget-value widget
) todo-item-mark
)
250 "Invalid value: must be distinct from `todo-item-mark'"))
252 :initialize
'custom-initialize-default
253 :set
'todo-reset-prefix
254 :group
'todo-display
)
256 (defcustom todo-number-prefix t
257 "Non-nil to prefix items with consecutively increasing integers.
258 These reflect the priorities of the items in each category."
260 :initialize
'custom-initialize-default
261 :set
'todo-reset-prefix
262 :group
'todo-display
)
264 (defun todo-mode-line-control (cat)
265 "Return a mode line control for todo or archive file buffers.
266 Argument CAT is the name of the current todo category.
267 This function is the value of the user variable
268 `todo-mode-line-function'."
269 (let ((file (todo-short-file-name todo-current-todo-file
)))
270 (format "%s category %d: %s" file todo-category-number cat
)))
272 (defcustom todo-mode-line-function
#'todo-mode-line-control
273 "Function that returns a mode line control for Todo mode buffers.
274 The function expects one argument holding the name of the current
275 todo category. The resulting control becomes the local value of
276 `mode-line-buffer-identification' in each Todo mode buffer."
278 :group
'todo-display
)
280 (defcustom todo-highlight-item nil
281 "Non-nil means highlight items at point."
283 :initialize
'custom-initialize-default
284 :set
'todo-reset-highlight-item
285 :group
'todo-display
)
287 (defcustom todo-wrap-lines t
288 "Non-nil to activate Visual Line mode and use wrap prefix."
290 :group
'todo-display
)
292 (defcustom todo-indent-to-here
3
293 "Number of spaces to indent continuation lines of items.
294 This must be a positive number to ensure such items are fully
295 shown in the Fancy Diary display."
296 :type
'(integer :validate
298 (unless (> (widget-value widget
) 0)
299 (widget-put widget
:error
300 "Invalid value: must be a positive integer")
302 :group
'todo-display
)
304 (defun todo-indent ()
305 "Indent from point to `todo-indent-to-here'."
306 (indent-to todo-indent-to-here todo-indent-to-here
))
308 (defcustom todo-show-with-done nil
309 "Non-nil to display done items in all categories."
311 :group
'todo-display
)
313 ;; -----------------------------------------------------------------------------
315 ;; -----------------------------------------------------------------------------
317 (defface todo-key-prompt
318 '((t (:weight bold
)))
319 "Face for making keys in item insertion prompt stand out."
323 ;; '((t :inherit font-lock-warning-face))
327 (:weight bold
:foreground
"Red1"))
331 (:weight bold
:foreground
"Pink"))
335 (:weight bold
:foreground
"Red1"))
339 (:weight bold
:foreground
"Pink"))
344 (:weight bold
:inverse-video t
)))
345 "Face for marks on marked items."
348 (defface todo-prefix-string
349 ;; '((t :inherit font-lock-constant-face))
350 '((((class grayscale
) (background light
))
351 (:foreground
"LightGray" :weight bold
:underline t
))
352 (((class grayscale
) (background dark
))
353 (:foreground
"Gray50" :weight bold
:underline t
))
354 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
355 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
356 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
357 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
358 (((class color
) (min-colors 8)) (:foreground
"magenta"))
359 (t (:weight bold
:underline t
)))
360 "Face for todo item prefix or numerical priority string."
363 (defface todo-top-priority
364 ;; bold font-lock-comment-face
365 '((default :weight bold
)
366 (((class grayscale
) (background light
)) :foreground
"DimGray" :slant italic
)
367 (((class grayscale
) (background dark
)) :foreground
"LightGray" :slant italic
)
368 (((class color
) (min-colors 88) (background light
)) :foreground
"Firebrick")
369 (((class color
) (min-colors 88) (background dark
)) :foreground
"chocolate1")
370 (((class color
) (min-colors 16) (background light
)) :foreground
"red")
371 (((class color
) (min-colors 16) (background dark
)) :foreground
"red1")
372 (((class color
) (min-colors 8) (background light
)) :foreground
"red")
373 (((class color
) (min-colors 8) (background dark
)) :foreground
"yellow")
375 "Face for top priority todo item numerical priority string.
376 The item's priority number string has this face if the number is
377 less than or equal the category's top priority setting."
380 (defface todo-nondiary
381 ;; '((t :inherit font-lock-type-face))
382 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
383 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
384 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
385 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
386 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
387 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
388 (((class color
) (min-colors 8)) :foreground
"green")
389 (t :weight bold
:underline t
))
390 "Face for non-diary markers around todo item date/time header."
394 '((t :inherit diary
))
395 "Face for the date string of a todo item."
399 '((t :inherit diary-time
))
400 "Face for the time string of a todo item."
403 (defface todo-diary-expired
404 ;; Doesn't contrast enough with todo-date (= diary) face.
405 ;; ;; '((t :inherit warning))
406 ;; '((default :weight bold)
407 ;; (((class color) (min-colors 16)) :foreground "DarkOrange")
408 ;; (((class color)) :foreground "yellow"))
409 ;; bold font-lock-function-name-face
410 '((default :weight bold
)
411 (((class color
) (min-colors 88) (background light
)) :foreground
"Blue1")
412 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSkyBlue")
413 (((class color
) (min-colors 16) (background light
)) :foreground
"Blue")
414 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSkyBlue")
415 (((class color
) (min-colors 8)) :foreground
"blue")
416 (t :inverse-video t
))
417 "Face for expired dates of diary items."
420 (defface todo-done-sep
421 ;; '((t :inherit font-lock-builtin-face))
422 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
423 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
424 (((class color
) (min-colors 88) (background light
)) :foreground
"dark slate blue")
425 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSteelBlue")
426 (((class color
) (min-colors 16) (background light
)) :foreground
"Orchid")
427 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSteelBlue")
428 (((class color
) (min-colors 8)) :foreground
"blue" :weight bold
)
430 "Face for separator string between done and not done todo items."
434 ;; '((t :inherit font-lock-keyword-face))
435 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
436 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
437 (((class color
) (min-colors 88) (background light
)) :foreground
"Purple")
438 (((class color
) (min-colors 88) (background dark
)) :foreground
"Cyan1")
439 (((class color
) (min-colors 16) (background light
)) :foreground
"Purple")
440 (((class color
) (min-colors 16) (background dark
)) :foreground
"Cyan")
441 (((class color
) (min-colors 8)) :foreground
"cyan" :weight bold
)
443 "Face for done todo item header string."
446 (defface todo-comment
447 ;; '((t :inherit font-lock-comment-face))
448 '((((class grayscale
) (background light
))
449 :foreground
"DimGray" :weight bold
:slant italic
)
450 (((class grayscale
) (background dark
))
451 :foreground
"LightGray" :weight bold
:slant italic
)
452 (((class color
) (min-colors 88) (background light
))
453 :foreground
"Firebrick")
454 (((class color
) (min-colors 88) (background dark
))
455 :foreground
"chocolate1")
456 (((class color
) (min-colors 16) (background light
))
458 (((class color
) (min-colors 16) (background dark
))
460 (((class color
) (min-colors 8) (background light
))
462 (((class color
) (min-colors 8) (background dark
))
463 :foreground
"yellow")
464 (t :weight bold
:slant italic
))
465 "Face for comments appended to done todo items."
469 ;; '((t :inherit match))
473 (:background
"yellow1"))
477 (:background
"RoyalBlue3"))
481 (:foreground
"black" :background
"yellow"))
485 (:foreground
"white" :background
"blue"))
490 (:background
"gray")))
491 "Face for matches found by `todo-search'."
495 ;; '((t :inherit widget-field))
497 (:foreground
"black" :background
"yellow3"))
498 (((class grayscale color
)
500 (:background
"gray85"))
501 (((class grayscale color
)
503 (:background
"dim gray"))
506 "Face for buttons in table of categories."
509 (defface todo-sorted-column
514 (:background
"grey85"))
517 (:background
"grey85" :foreground
"grey10"))
519 (:background
"gray")))
520 "Face for sorted column in table of categories."
523 (defface todo-archived-only
524 ;; '((t (:inherit (shadow))))
527 (:foreground
"grey50"))
530 (:foreground
"grey70"))
532 (:foreground
"gray")))
533 "Face for archived-only category names in table of categories."
536 (defface todo-category-string
537 ;; '((t :inherit font-lock-type-face))
538 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
539 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
540 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
541 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
542 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
543 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
544 (((class color
) (min-colors 8)) :foreground
"green")
545 (t :weight bold
:underline t
))
546 "Face for category-file header in Todo Filtered Items mode."
549 ;; -----------------------------------------------------------------------------
550 ;;; Entering and exiting
551 ;; -----------------------------------------------------------------------------
553 ;; (defcustom todo-visit-files-commands (list 'find-file 'dired-find-file)
554 ;; "List of file finding commands for `todo-display-as-todo-file'.
555 ;; Invoking these commands to visit a todo file or todo archive file
556 ;; calls `todo-show' or `todo-find-archive', so that the file is
557 ;; displayed correctly."
558 ;; :type '(repeat function)
561 (defun todo-short-file-name (file)
562 "Return the short form of todo file FILE's name.
563 This lacks the extension and directory components."
565 (file-name-sans-extension (file-name-nondirectory file
))))
567 (defun todo--files-type-list ()
568 (mapcar (lambda (f) (list 'const
(todo-short-file-name f
)))
569 (funcall todo-files-function
)))
571 (defcustom todo-default-todo-file
(todo-short-file-name
572 (car (funcall todo-files-function
)))
573 "Todo file visited by first session invocation of `todo-show'."
574 :type
(when todo-files
575 `(radio ,@(todo--files-type-list)))
578 (defcustom todo-show-current-file t
579 "Non-nil to make `todo-show' visit the current todo file.
580 Otherwise, `todo-show' always visits `todo-default-todo-file'."
582 :initialize
'custom-initialize-default
583 :set
'todo-set-show-current-file
586 (defcustom todo-show-first
'first
587 "What action to take on first use of `todo-show' on a file."
588 :type
'(choice (const :tag
"Show first category" first
)
589 (const :tag
"Show table of categories" table
)
590 (const :tag
"Show top priorities" top
)
591 (const :tag
"Show diary items" diary
)
592 (const :tag
"Show regexp items" regexp
))
595 (defcustom todo-add-item-if-new-category t
596 "Non-nil to prompt for an item after adding a new category."
600 (defcustom todo-initial-file
"Todo"
601 "Default file name offered on adding first todo file."
605 (defcustom todo-initial-category
"Todo"
606 "Default category name offered on initializing a new todo file."
610 (defcustom todo-category-completions-files nil
611 "List of files for building `todo-read-category' completions."
612 :type
`(set ,@(todo--files-type-list))
615 (defcustom todo-completion-ignore-case nil
616 "Non-nil means case is ignored by `todo-read-*' functions."
621 (defun todo-show (&optional solicit-file interactive
)
622 "Visit a todo file and display one of its categories.
624 When invoked in Todo mode, Todo Archive mode or Todo Filtered
625 Items mode, or when invoked anywhere else with a prefix argument,
626 prompt for which todo file to visit. When invoked outside of a
627 Todo mode buffer without a prefix argument, visit
628 `todo-default-todo-file'. Subsequent invocations from outside of
629 Todo mode revisit this file or, with option
630 `todo-show-current-file' non-nil (the default), whichever todo
631 file was last visited.
633 If you call this command before you have created any todo file in
634 the current format, and you have an todo file in old format, it
635 will ask you whether to convert that file and show it.
636 Otherwise, calling this command before any todo file exists
637 prompts for a file name and an initial category (defaulting to
638 `todo-initial-file' and `todo-initial-category'), creates both of
639 these, visits the file and displays the category, and if option
640 `todo-add-item-if-new-category' is non-nil (the default), prompts
643 The first invocation of this command on an existing todo file
644 interacts with the option `todo-show-first': if its value is
645 `first' (the default), show the first category in the file; if
646 its value is `table', show the table of categories in the file;
647 if its value is one of `top', `diary' or `regexp', show the
648 corresponding saved top priorities, diary items, or regexp items
649 file, if any. Subsequent invocations always show the file's
650 current (i.e., last displayed) category.
652 In Todo mode just the category's unfinished todo items are shown
653 by default. The done items are hidden, but typing
654 `\\[todo-toggle-view-done-items]' displays them below the todo
655 items. With non-nil user option `todo-show-with-done' both todo
656 and done items are always shown on visiting a category."
658 (when todo-default-todo-file
659 (todo-check-file (todo-absolute-file-name todo-default-todo-file
)))
661 ;; Before initializing the first todo first, check if there is a
662 ;; legacy todo file and if so, offer to convert to the current
663 ;; format and make it the first new todo file.
664 (unless todo-default-todo-file
665 (let ((legacy-todo-file (if (boundp 'todo-file-do
)
667 (locate-user-emacs-file "todo-do" ".todo-do"))))
668 (when (and (file-exists-p legacy-todo-file
)
669 (y-or-n-p (concat "Do you want to convert a copy of your "
670 "old todo file to the new format? ")))
671 (when (todo-convert-legacy-files)
672 (throw 'shown nil
)))))
675 (show-first todo-show-first
)
676 (file (cond ((or solicit-file
678 (memq major-mode
'(todo-mode
680 todo-filtered-items-mode
))))
681 (if (funcall todo-files-function
)
682 (todo-read-file-name "Choose a todo file to visit: "
684 (user-error "There are no todo files")))
685 ((and (eq major-mode
'todo-archive-mode
)
686 ;; Called noninteractively via todo-quit
687 ;; to jump to corresponding category in
690 (setq cat
(todo-current-category))
691 (concat (file-name-sans-extension
692 todo-current-todo-file
) ".todo"))
694 (or todo-current-todo-file
695 (and todo-show-current-file
696 todo-global-current-todo-file
)
697 (todo-absolute-file-name todo-default-todo-file
)
700 (unless todo-default-todo-file
701 ;; We just initialized the first todo file, so make it the default.
702 (setq todo-default-todo-file
(todo-short-file-name file
)
704 (put 'todo-default-todo-file
'custom-type
705 `(radio ,@(todo--files-type-list))))
706 (unless (member file todo-visited
)
707 ;; Can't setq t-c-t-f here, otherwise wrong file shown when
708 ;; todo-show is called from todo-show-categories-table.
709 (let ((todo-current-todo-file file
))
710 (cond ((eq todo-show-first
'table
)
711 (todo-show-categories-table))
712 ((memq todo-show-first
'(top diary regexp
))
713 (let* ((shortf (todo-short-file-name file
))
714 (fi-file (todo-absolute-file-name
715 shortf todo-show-first
)))
716 (when (eq todo-show-first
'regexp
)
717 (let ((rxfiles (directory-files todo-directory t
719 (when (and rxfiles
(> (length rxfiles
) 1))
720 (let ((rxf (mapcar #'todo-short-file-name rxfiles
)))
721 (setq fi-file
(todo-absolute-file-name
723 "Choose a regexp items file: "
726 (if (file-exists-p fi-file
)
730 (set-buffer (find-file-noselect fi-file
'nowarn
)))
731 (unless (derived-mode-p 'todo-filtered-items-mode
)
732 (todo-filtered-items-mode)))
733 (message "There is no %s file for %s"
734 (cond ((eq todo-show-first
'top
)
736 ((eq todo-show-first
'diary
)
738 ((eq todo-show-first
'regexp
)
741 (setq todo-show-first
'first
)))))))
742 (when (or (member file todo-visited
)
743 (eq todo-show-first
'first
))
744 (unless (todo-check-file file
) (throw 'end nil
))
745 ;; If todo-show is called from the minibuffer, don't visit
746 ;; the todo file there.
747 (set-window-buffer (if (minibufferp) (minibuffer-selected-window)
749 (set-buffer (find-file-noselect file
'nowarn
)))
750 (if (equal (file-name-extension (buffer-file-name)) "toda")
751 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
752 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
753 ;; When quitting an archive file, show the corresponding
754 ;; category in the corresponding todo file, if it exists.
755 (when (assoc cat todo-categories
)
756 (setq todo-category-number
(todo-category-number cat
)))
757 ;; If this is a new todo file, add its first category.
758 (when (zerop (buffer-size))
759 ;; Don't confuse an erased buffer with a fresh buffer for
760 ;; adding a new todo file -- it might have been erased by
761 ;; mistake or due to a bug (e.g. Bug#20832).
762 (when (buffer-modified-p)
763 (error "Buffer is empty but modified, please report a bug"))
766 (setq todo-category-number
767 (todo-add-category todo-current-todo-file
"")
768 add-item todo-add-item-if-new-category
771 ;; If the category was added, save the file now, so we
772 ;; don't risk having an empty todo file, which would
773 ;; signal an error if we tried to visit it later,
774 ;; since doing that looks for category boundaries.
776 ;; If user cancels before adding the category, clean up
777 ;; and exit, so we have a fresh slate the next time.
779 ;; (setq todo-files (funcall todo-files-function))
780 (setq todo-files
(delete file todo-files
))
782 (setq todo-default-todo-file nil
783 todo-current-todo-file nil
)
784 (put 'todo-default-todo-file
'custom-type
785 `(radio ,@(todo--files-type-list))))
788 (save-excursion (todo-category-select))
789 (when add-item
(todo-insert-item--basic)))
790 (setq todo-show-first show-first
)
791 (add-to-list 'todo-visited file
)))))
794 "Save the current todo file."
796 (cond ((eq major-mode
'todo-filtered-items-mode
)
797 (todo-check-filtered-items-file)
798 (todo-save-filtered-items-buffer))
802 (defvar todo-descending-counts
)
805 "Exit the current Todo-related buffer.
806 Depending on the specific mode, this either kills the buffer or
807 buries it and restores state as needed."
809 (let ((buf (current-buffer)))
810 (cond ((eq major-mode
'todo-categories-mode
)
811 ;; Postpone killing buffer till after calling todo-show, to
812 ;; prevent killing todo-mode buffer.
813 (setq todo-descending-counts nil
)
814 ;; Ensure todo-show calls todo-show-categories-table only on
815 ;; first invocation per file.
816 (when (eq todo-show-first
'table
)
817 (add-to-list 'todo-visited todo-current-todo-file
))
820 ((eq major-mode
'todo-filtered-items-mode
)
822 (unless (eq major-mode
'todo-mode
) (todo-show)))
823 ((eq major-mode
'todo-archive-mode
)
824 ;; Have to write a newly created archive to file to avoid
825 ;; subsequent errors.
827 (let ((todo-file (concat todo-directory
828 (todo-short-file-name todo-current-todo-file
)
830 (if (todo-check-file todo-file
)
832 (message "There is no todo file for this archive")))
833 ;; When todo-check-file runs in todo-show, it kills the
834 ;; buffer if the archive file was deleted externally.
835 (when (buffer-live-p buf
) (kill-buffer buf
)))
836 ((eq major-mode
'todo-mode
)
840 ;; -----------------------------------------------------------------------------
841 ;;; Navigation between and within categories
842 ;; -----------------------------------------------------------------------------
844 (defcustom todo-skip-archived-categories nil
845 "Non-nil to handle categories with only archived items specially.
847 Sequential category navigation using \\[todo-forward-category]
848 or \\[todo-backward-category] skips categories that contain only
849 archived items. Other commands still recognize these categories.
850 In Todo Categories mode (\\[todo-show-categories-table]) these
851 categories shown in `todo-archived-only' face and pressing the
852 category button visits the category in the archive instead of the
855 :group
'todo-display
)
857 (defun todo-forward-category (&optional back
)
858 "Visit the numerically next category in this todo file.
859 If the current category is the highest numbered, visit the first
860 category. With non-nil argument BACK, visit the numerically
861 previous category (the highest numbered one, if the current
862 category is the first)."
864 (setq todo-category-number
865 (1+ (mod (- todo-category-number
(if back
2 0))
866 (length todo-categories
))))
867 (when todo-skip-archived-categories
868 (while (and (zerop (todo-get-count 'todo
))
869 (zerop (todo-get-count 'done
))
870 (not (zerop (todo-get-count 'archived
))))
871 (setq todo-category-number
872 (funcall (if back
#'1-
#'1+) todo-category-number
))))
873 (todo-category-select)
874 (goto-char (point-min)))
876 (defun todo-backward-category ()
877 "Visit the numerically previous category in this todo file.
878 If the current category is the highest numbered, visit the first
881 (todo-forward-category t
))
883 (defvar todo-categories-buffer
)
885 (defun todo-jump-to-category (&optional file where
)
886 "Prompt for a category in a todo file and jump to it.
888 With non-nil FILE (interactively a prefix argument), prompt for a
889 specific todo file and choose (with TAB completion) a category
890 in it to jump to; otherwise, choose and jump to any category in
891 either the current todo file or a file in
892 `todo-category-completions-files'.
894 Also accept a non-existing category name and ask whether to add a
895 new category by that name; on confirmation, add it and jump to
896 that category, and if option `todo-add-item-if-new-category' is
897 non-nil (the default), then prompt for the first item.
899 In noninteractive calls non-nil WHERE specifies either the goal
900 category or its file. If its value is `archive', the choice of
901 categories is restricted to the current archive file or the
902 archive you were prompted to choose; this is used by
903 `todo-jump-to-archive-category'. If its value is the name of a
904 category, jump directly to that category; this is used in Todo
907 ;; If invoked outside of Todo mode and there is not yet any Todo
908 ;; file, initialize one.
909 (if (null (funcall todo-files-function
))
911 (let* ((archive (eq where
'archive
))
912 (cat (unless archive where
))
913 (goto-archive (and cat
914 todo-skip-archived-categories
915 (zerop (todo-get-count 'todo cat
))
916 (zerop (todo-get-count 'done cat
))
917 (not (zerop (todo-get-count 'archived cat
)))))
918 (file0 (when cat
; We're in Todo Categories mode.
920 ;; If the category has only archived items and
921 ;; `todo-skip-archived-categories' is non-nil, jump to
922 ;; the archive category.
923 (concat (file-name-sans-extension
924 todo-current-todo-file
) ".toda")
925 ;; Otherwise, jump to the category in the todo file.
926 todo-current-todo-file
)))
927 (len (length todo-categories
))
928 (cat+file
(unless cat
929 (todo-read-category "Jump to category: "
930 (if archive
'archive
) file
)))
931 (add-item (and todo-add-item-if-new-category
932 (> (length todo-categories
) len
)))
933 (category (or cat
(car cat
+file
))))
934 (unless cat
(setq file0
(cdr cat
+file
)))
935 (with-current-buffer (find-file-noselect file0
'nowarn
)
936 (when goto-archive
(todo-archive-mode))
937 (set-window-buffer (selected-window)
938 (set-buffer (find-buffer-visiting file0
)))
939 (unless todo-global-current-todo-file
940 (setq todo-global-current-todo-file todo-current-todo-file
))
941 (todo-category-number category
)
942 (todo-category-select)
943 (goto-char (point-min))
944 (when add-item
(todo-insert-item--basic))))))
946 (defun todo-next-item (&optional count
)
947 "Move point down to the beginning of the next item.
948 With positive numerical prefix COUNT, move point COUNT items
951 If the category's done items are hidden, this command also moves
952 point to the empty line below the last todo item from any higher
953 item in the category, i.e., when invoked with or without a prefix
954 argument. If the category's done items are visible, this command
955 called with a prefix argument only moves point to a lower item,
956 e.g., with point on the last todo item and called with prefix 1,
957 it moves point to the first done item; but if called with point
958 on the last todo item without a prefix argument, it moves point
959 the the empty line above the done items separator."
961 ;; It's not worth the trouble to allow prefix arg value < 1, since
962 ;; we have the corresponding command.
963 (cond ((and current-prefix-arg
(< count
1))
964 (user-error "The prefix argument must be a positive number"))
966 (todo-forward-item count
))
968 (todo-forward-item))))
970 (defun todo-previous-item (&optional count
)
971 "Move point up to start of item with next higher priority.
972 With positive numerical prefix COUNT, move point COUNT items
975 If the category's done items are visible, this command called
976 with a prefix argument only moves point to a higher item, e.g.,
977 with point on the first done item and called with prefix 1, it
978 moves to the last todo item; but if called with point on the
979 first done item without a prefix argument, it moves point the the
980 empty line above the done items separator."
982 ;; Avoid moving to bob if on the first item but not at bob.
983 (when (> (line-number-at-pos) 1)
984 ;; It's not worth the trouble to allow prefix arg value < 1, since
985 ;; we have the corresponding command.
986 (cond ((and current-prefix-arg
(< count
1))
987 (user-error "The prefix argument must be a positive number"))
989 (todo-backward-item count
))
991 (todo-backward-item)))))
993 ;; -----------------------------------------------------------------------------
994 ;;; Display toggle commands
995 ;; -----------------------------------------------------------------------------
997 (defun todo-toggle-prefix-numbers ()
998 "Hide item numbering if shown, show if hidden."
1002 (goto-char (point-min))
1003 (let* ((ov (todo-get-overlay 'prefix
))
1004 (show-done (re-search-forward todo-done-string-start nil t
))
1005 (todo-show-with-done show-done
)
1006 (todo-number-prefix (not (equal (overlay-get ov
'before-string
)
1008 (if (eq major-mode
'todo-filtered-items-mode
)
1009 (todo-prefix-overlays)
1010 (todo-category-select))))))
1012 (defun todo-toggle-view-done-items ()
1013 "Show hidden or hide visible done items in current category."
1015 (if (zerop (todo-get-count 'done
(todo-current-category)))
1016 (message "There are no done items in this category.")
1017 (let ((opoint (point)))
1018 (goto-char (point-min))
1019 (let* ((shown (re-search-forward todo-done-string-start nil t
))
1020 (todo-show-with-done (not shown
)))
1021 (todo-category-select)
1023 ;; If start of done items sections is below the bottom of the
1024 ;; window, make it visible.
1027 (goto-char (point-min))
1028 (re-search-forward todo-done-string-start nil t
)))
1029 (if (not (pos-visible-in-window-p shown
))
1031 (goto-char opoint
)))))))
1033 (defun todo-toggle-view-done-only ()
1034 "Switch between displaying only done or only todo items."
1036 (setq todo-show-done-only
(not todo-show-done-only
))
1037 (todo-category-select))
1039 (defun todo-toggle-item-highlighting ()
1040 "Highlight or unhighlight the todo item the cursor is on."
1042 (eval-and-compile (require 'hl-line
))
1043 (when (memq major-mode
1044 '(todo-mode todo-archive-mode todo-filtered-items-mode
))
1049 (defvar todo--item-headers-hidden nil
1050 "Non-nil if item date-time headers in current buffer are hidden.")
1052 (defun todo-toggle-item-header ()
1053 "Hide or show item date-time headers in the current file.
1054 With done items, this hides only the done date-time string, not
1055 the the original date-time string."
1057 (unless (catch 'nonempty
1058 (dolist (type '(todo done
))
1059 (dolist (c todo-categories
)
1060 (let ((count (todo-get-count type
(car c
))))
1061 (unless (zerop count
)
1062 (throw 'nonempty t
))))))
1063 (user-error "This file has no items"))
1064 (if todo--item-headers-hidden
1066 (remove-overlays 1 (1+ (buffer-size)) 'todo
'header
)
1067 (setq todo--item-headers-hidden nil
))
1071 (goto-char (point-min))
1074 (when (re-search-forward
1075 (concat todo-item-start
1076 "\\( " diary-time-regexp
"\\)?"
1077 (regexp-quote todo-nondiary-end
) "? ")
1079 (setq ov
(make-overlay (match-beginning 0) (match-end 0) nil t
))
1080 (overlay-put ov
'todo
'header
)
1081 (overlay-put ov
'display
""))
1083 (setq todo--item-headers-hidden t
)))))
1085 ;; -----------------------------------------------------------------------------
1086 ;;; File and category editing
1087 ;; -----------------------------------------------------------------------------
1089 (defun todo-add-file ()
1090 "Name and initialize a new todo file.
1091 Interactively, prompt for a category and display it, and if
1092 option `todo-add-item-if-new-category' is non-nil (the default),
1093 prompt for the first item.
1094 Noninteractively, return the name of the new file."
1096 (let* ((prompt (concat "Enter name of new todo file "
1097 "(TAB or SPC to see current names): "))
1098 (file (todo-read-file-name prompt
)))
1099 ;; Don't accept the name of an existing todo file.
1100 (setq file
(todo-absolute-file-name
1101 (todo-validate-name (todo-short-file-name file
) 'file
)))
1102 (with-current-buffer (get-buffer-create file
)
1104 (write-region (point-min) (point-max) file nil
'nomessage nil t
)
1106 (setq todo-files
(funcall todo-files-function
))
1107 (todo-update-filelist-defcustoms)
1108 (if (called-interactively-p 'any
)
1110 (set-window-buffer (selected-window)
1111 (set-buffer (find-file-noselect file
)))
1112 (setq todo-current-todo-file file
)
1116 (defun todo-rename-file (&optional arg
)
1117 "Rename the current todo file.
1118 With prefix ARG, prompt for a todo file and rename it.
1119 If there are corresponding archive or filtered items files,
1120 rename these accordingly. If there are live buffers visiting
1121 these files, also rename them accordingly."
1123 (let* ((oname (or (and arg
1124 (todo-read-file-name "Choose a file to rename: "
1126 (buffer-file-name)))
1127 (soname (todo-short-file-name oname
))
1128 (nname (todo-read-file-name "New name for this file: "))
1129 (snname (todo-short-file-name nname
))
1130 (files (directory-files todo-directory t
1131 (concat ".*" (regexp-quote soname
)
1135 (let* ((sfname (todo-short-file-name f
))
1136 (fext (file-name-extension f t
))
1137 (fbuf (find-buffer-visiting f
))
1138 (fbname (buffer-name fbuf
)))
1139 (when (string-match (regexp-quote soname
) sfname
)
1140 (let* ((snfname (replace-match snname t t sfname
))
1141 (nfname (concat todo-directory snfname fext
)))
1142 (rename-file f nfname
)
1144 (with-current-buffer fbuf
1145 (set-visited-file-name nfname t t
)
1146 (cond ((member fext
'(".todo" ".toda"))
1147 (setq todo-current-todo-file
(buffer-file-name))
1148 (setq mode-line-buffer-identification
1149 (funcall todo-mode-line-function
1150 (todo-current-category))))
1153 (replace-regexp-in-string
1154 (regexp-quote soname
) snname fbname
))))))))))
1155 (setq todo-files
(funcall todo-files-function
)
1156 todo-archives
(funcall todo-files-function t
))
1157 (when (string= todo-default-todo-file soname
)
1158 (setq todo-default-todo-file snname
))
1159 (when (string= todo-global-current-todo-file oname
)
1160 (setq todo-global-current-todo-file nname
))
1161 (todo-update-filelist-defcustoms)))
1163 (defun todo-delete-file ()
1164 "Delete the current todo, archive or filtered items file.
1165 If the todo file has a corresponding archive file, or vice versa,
1166 prompt whether to delete that as well. Also kill the buffers
1167 visiting the deleted files."
1169 (let* ((file1 (buffer-file-name))
1170 (todo (eq major-mode
'todo-mode
))
1171 (archive (eq major-mode
'todo-archive-mode
))
1172 (filtered (eq major-mode
'todo-filtered-items-mode
))
1173 (file1-sn (todo-short-file-name file1
))
1174 (file2 (concat todo-directory file1-sn
(cond (todo ".toda")
1175 (archive ".todo"))))
1176 (buf1 (current-buffer))
1177 (buf2 (when file2
(find-buffer-visiting file2
)))
1178 (prompt1 (concat "Delete " (cond (todo "todo")
1180 (filtered "filtered items"))
1182 (prompt2 (concat "Also delete the corresponding "
1183 (cond (todo "archive") (archive "todo")) " file "
1184 (when buf2
"and kill the buffer visiting it? ")))
1185 (delete1 (yes-or-no-p (format prompt1 file1-sn
)))
1186 (delete2 (when (and delete1
(or (file-exists-p file2
) buf2
))
1187 (yes-or-no-p prompt2
))))
1189 (when (file-exists-p file1
) (delete-file file1
))
1190 (setq todo-visited
(delete file1 todo-visited
))
1194 (when (file-exists-p file2
) (delete-file file2
))
1195 (setq todo-visited
(delete file2 todo-visited
))
1196 (and buf2
(kill-buffer buf2
)))
1197 ;; If we deleted an archive but not its todo file, update the
1198 ;; latter's category sexp.
1199 (when (equal (file-name-extension file2
) "todo")
1200 (with-current-buffer (or buf2
(find-file-noselect file2
))
1204 (goto-char (point-min))
1205 (let ((sexp (read (buffer-substring-no-properties
1206 (line-beginning-position)
1207 (line-end-position))))
1208 (buffer-read-only nil
))
1209 (mapc (lambda (x) (aset (cdr x
) 3 0)) sexp
)
1210 (delete-region (line-beginning-position) (line-end-position))
1211 (prin1 sexp
(current-buffer)))))
1212 (todo-set-categories)
1213 (unless buf2
(kill-buffer)))))
1214 (setq todo-files
(funcall todo-files-function
)
1215 todo-archives
(funcall todo-files-function t
))
1216 (when (or (string= file1-sn todo-default-todo-file
)
1217 (and delete2
(string= file1-sn todo-default-todo-file
)))
1218 (setq todo-default-todo-file
(todo-short-file-name (car todo-files
))))
1219 (when (or (string= file1 todo-global-current-todo-file
)
1220 (and delete2
(string= file2 todo-global-current-todo-file
)))
1221 (setq todo-global-current-todo-file nil
))
1222 (todo-update-filelist-defcustoms)
1223 (message (concat (cond (todo "Todo") (archive "Archive")) " file \"%s\" "
1226 (cond (todo "archive") (archive "todo"))
1231 (defvar todo-edit-buffer
"*Todo Edit*"
1232 "Name of current buffer in Todo Edit mode.")
1234 (defun todo-edit-file ()
1235 "Put current buffer in `todo-edit-mode'.
1236 This makes the entire file visible and the buffer writable and
1237 you can use the self-insertion keys and standard Emacs editing
1238 commands to make changes. To return to Todo mode, type
1239 \\[todo-edit-quit]. This runs a file format check, signaling
1240 an error if the format has become invalid. However, this check
1241 cannot tell if the number of items changed, which could result in
1242 the file containing inconsistent information. For this reason
1243 this command should be used with caution."
1248 (display-warning 'todo
(format "\
1250 Type %s to return to Todo 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
1260 Categories')." (substitute-command-keys "\\[todo-edit-quit]"))))
1262 (defun todo-add-category (&optional file cat
)
1263 "Add a new category to a todo file.
1265 Called interactively with prefix argument FILE, prompt for a file
1266 and then for a new category to add to that file, otherwise prompt
1267 just for a category to add to the current todo file. After
1268 adding the category, visit it in Todo mode and if option
1269 `todo-add-item-if-new-category' is non-nil (the default), prompt
1272 Non-interactively, add category CAT to file FILE; if FILE is nil,
1273 add CAT to the current todo file. After adding the category,
1274 return the new category number."
1277 ;; If cat is passed from caller, don't prompt, unless it is "",
1278 ;; which means the file was just added and has no category yet.
1279 (if (and cat
(> (length cat
) 0))
1280 (setq file0
(or (and (stringp file
) file
)
1281 todo-current-todo-file
))
1282 (setq catfil
(todo-read-category "Enter a new category name: "
1283 'add
(when (called-interactively-p 'any
)
1286 file0
(if (called-interactively-p 'any
)
1290 (let ((counts (make-vector 4 0)) ; [todo diary done archived]
1291 (num (1+ (length todo-categories
)))
1292 (buffer-read-only nil
))
1293 (setq todo-current-todo-file file0
)
1294 (setq todo-categories
(append todo-categories
1295 (list (cons cat counts
))))
1297 (goto-char (point-max))
1298 (save-excursion ; Save point for todo-category-select.
1299 (insert todo-category-beg cat
"\n\n" todo-category-done
"\n"))
1300 (todo-update-categories-sexp)
1301 ;; If invoked by user, display the newly added category, if
1302 ;; called programmatically return the category number to the
1304 (if (called-interactively-p 'any
)
1306 (setq todo-category-number num
)
1307 (todo-category-select)
1308 (when todo-add-item-if-new-category
1309 (todo-insert-item--basic)))
1312 (defun todo-rename-category ()
1313 "Rename current todo category.
1314 If this file has an archive containing this category, rename the
1315 category there as well."
1317 (let* ((cat (todo-current-category))
1318 (new (read-from-minibuffer
1319 (format "Rename category \"%s\" to: " cat
))))
1320 (setq new
(todo-validate-name new
'category
))
1321 (let* ((ofile todo-current-todo-file
)
1322 (archive (concat (file-name-sans-extension ofile
) ".toda"))
1323 (buffers (append (list ofile
)
1324 (unless (zerop (todo-get-count 'archived cat
))
1326 (dolist (buf buffers
)
1327 (with-current-buffer (find-file-noselect buf
)
1328 (let (buffer-read-only)
1329 (setq todo-categories
(todo-set-categories))
1332 (setcar (assoc cat todo-categories
) new
)
1334 (goto-char (point-min))
1335 (todo-update-categories-sexp)
1336 (re-search-forward (concat (regexp-quote todo-category-beg
)
1337 "\\(" (regexp-quote cat
) "\\)\n")
1339 (replace-match new t t nil
1)))))))
1340 (force-mode-line-update))
1341 (save-excursion (todo-category-select)))
1343 (defun todo-delete-category (&optional arg
)
1344 "Delete current todo category provided it contains no items.
1345 With prefix ARG delete the category even if it does contain
1346 todo or done items."
1348 (let* ((file todo-current-todo-file
)
1349 (cat (todo-current-category))
1350 (todo (todo-get-count 'todo cat
))
1351 (done (todo-get-count 'done cat
))
1352 (archived (todo-get-count 'archived cat
)))
1354 (or (> todo
0) (> done
0)))
1355 (message "%s" (substitute-command-keys
1356 (concat "To delete a non-empty category, "
1357 "type C-u \\[todo-delete-category].")))
1358 (when (cond ((= (length todo-categories
) 1)
1360 (concat "This is the only category in this file; "
1361 "deleting it will also delete the file.\n"
1362 "Do you want to proceed? ")))
1364 (todo-y-or-n-p (format-message
1365 (concat "This category has archived items; "
1366 "the archived category will remain\n"
1367 "after deleting the todo category. "
1368 "Do you still want to delete it\n"
1369 "(see `todo-skip-archived-categories' "
1370 "for another option)? "))))
1372 (todo-y-or-n-p (concat "Permanently remove category \"" cat
1373 "\"" (and arg
" and all its entries")
1376 (let ((buffer-read-only)
1377 (beg (re-search-backward
1378 (concat "^" (regexp-quote (concat todo-category-beg cat
))
1381 (end (if (re-search-forward
1382 (concat "\n\\(" (regexp-quote todo-category-beg
)
1387 (remove-overlays beg end
)
1388 (delete-region beg end
)
1389 (if (= (length todo-categories
) 1)
1390 ;; If deleted category was the only one, delete the file.
1392 (todo-update-filelist-defcustoms)
1393 ;; Skip confirming killing the archive buffer if it has been
1394 ;; modified and not saved.
1395 (set-buffer-modified-p nil
)
1398 (message "Deleted todo file %s." file
))
1399 (setq todo-categories
(delete (assoc cat todo-categories
)
1401 (todo-update-categories-sexp)
1402 (setq todo-category-number
1403 (1+ (mod todo-category-number
(length todo-categories
))))
1404 (todo-category-select)
1405 (goto-char (point-min))
1406 (message "Deleted category %s." cat
)))))))
1408 (defun todo-move-category ()
1409 "Move current category to a different todo file.
1410 If the todo file chosen does not exist, it is created.
1411 If the current category has archived items, also move those to
1412 the archive of the file moved to, creating it if it does not exist."
1414 (when (or (> (length todo-categories
) 1)
1415 (todo-y-or-n-p (concat "This is the only category in this file; "
1416 "moving it will also delete the file.\n"
1417 "Do you want to proceed? ")))
1418 (let* ((ofile todo-current-todo-file
)
1419 (cat (todo-current-category))
1420 (nfile (todo-read-file-name "Todo file to move this category to: "))
1421 (archive (concat (file-name-sans-extension ofile
) ".toda"))
1422 (buffers (append (list ofile
)
1423 (unless (zerop (todo-get-count 'archived cat
))
1426 (while (equal nfile
(file-truename ofile
))
1427 (setq nfile
(todo-read-file-name
1428 "Choose a file distinct from this file: ")))
1429 (unless (member nfile todo-files
)
1430 (with-current-buffer (get-buffer-create nfile
)
1432 (write-region (point-min) (point-max) nfile nil
'nomessage nil t
)
1433 (kill-buffer nfile
))
1434 (setq todo-files
(funcall todo-files-function
))
1435 (todo-update-filelist-defcustoms))
1436 (dolist (buf buffers
)
1437 ;; Make sure archive file is in Todo Archive mode so that
1438 ;; todo-categories has correct value.
1439 (with-current-buffer (find-file-noselect buf
)
1440 (when (equal (file-name-extension (buffer-file-name)) "toda")
1441 (unless (derived-mode-p 'todo-archive-mode
)
1442 (todo-archive-mode)))
1444 (goto-char (point-max))
1445 (let* ((beg (re-search-backward
1447 (regexp-quote (concat todo-category-beg cat
))
1450 (end (if (re-search-forward
1451 (concat "^" (regexp-quote todo-category-beg
))
1455 (content (buffer-substring-no-properties beg end
))
1456 (counts (cdr (assoc cat todo-categories
)))
1458 ;; Move the category to the new file. Also update or create
1459 ;; archive file if necessary.
1460 (with-current-buffer
1462 ;; Regenerate todo-archives in case there
1463 ;; is a newly created archive.
1464 (if (member buf
(funcall todo-files-function t
))
1465 (concat (file-name-sans-extension nfile
) ".toda")
1467 (if (equal (file-name-extension (buffer-file-name)) "toda")
1468 (unless (derived-mode-p 'todo-archive-mode
)
1469 (todo-archive-mode))
1470 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
1471 (let* ((nfile-short (todo-short-file-name nfile
))
1473 (format "Todo file \"%s\" already has "
1475 (format "the category \"%s\";\n" cat
)
1476 "enter a new category name: "))
1479 (goto-char (point-max))
1481 ;; If the file moved to has a category with the same
1482 ;; name, rename the moved category.
1483 (when (assoc cat todo-categories
)
1484 (unless (member (file-truename (buffer-file-name))
1485 (funcall todo-files-function t
))
1486 (setq new
(read-from-minibuffer prompt
))
1487 (setq new
(todo-validate-name new
'category
))))
1488 ;; Replace old with new name in todo and archive files.
1490 (goto-char (point-max))
1492 (concat "^" (regexp-quote todo-category-beg
)
1493 "\\(" (regexp-quote cat
) "\\)$")
1495 (replace-match new nil nil nil
1))
1496 (setq todo-categories
1497 (append todo-categories
(list (cons (or new cat
) counts
))))
1498 (goto-char (point-min))
1499 (if (looking-at "((\"")
1500 ;; Delete existing sexp.
1501 (delete-region (line-beginning-position) (line-end-position))
1502 ;; Otherwise, file is new, so make space for categories sexp.
1504 (goto-char (point-min)))
1505 ;; Insert (new or updated) sexp.
1506 (prin1 todo-categories
(current-buffer)))
1507 ;; If archive was just created, save it to avoid "File
1508 ;; <xyz> no longer exists!" message on invoking
1509 ;; `todo-view-archived-items'.
1510 (unless (file-exists-p (buffer-file-name))
1512 (todo-category-number (or new cat
))
1513 (todo-category-select))
1514 ;; Delete the category from the old file, and if that was the
1515 ;; last category, delete the file. Also handle archive file
1517 (remove-overlays beg end
)
1518 (delete-region beg end
)
1519 (goto-char (point-min))
1520 ;; Put point after todo-categories sexp.
1522 (if (eobp) ; Aside from sexp, file is empty.
1524 ;; Skip confirming killing the archive buffer.
1525 (set-buffer-modified-p nil
)
1526 (delete-file todo-current-todo-file
)
1528 (when (member todo-current-todo-file todo-files
)
1529 (todo-update-filelist-defcustoms)))
1530 (setq todo-categories
(delete (assoc cat todo-categories
)
1532 (todo-update-categories-sexp)
1533 (when (> todo-category-number
(length todo-categories
))
1534 (setq todo-category-number
1))
1535 (todo-category-select)))))
1536 (set-window-buffer (selected-window)
1537 (set-buffer (find-file-noselect nfile
))))))
1539 (defun todo-merge-category (&optional file
)
1540 "Merge current category into another existing category.
1542 With prefix argument FILE, prompt for a specific todo file and
1543 choose (with TAB completion) a category in it to merge into;
1544 otherwise, choose and merge into a category in either the
1545 current todo file or a file in `todo-category-completions-files'.
1547 After merging, the source category's todo and done items are
1548 appended to the chosen goal category's todo and done items,
1549 respectively. The goal category becomes the current category,
1550 and the source category is deleted.
1552 If both the source and goal categories also have archived items,
1553 they are also merged. If only the source category has archived
1554 items, the goal category is added as a new category to the
1555 archive file and the source category is deleted."
1557 (let* ((tfile todo-current-todo-file
)
1558 (cat (todo-current-category))
1559 (cat+file
(todo-read-category "Merge into category: " 'todo file
))
1560 (goal (car cat
+file
))
1561 (gfile (cdr cat
+file
))
1562 (tarchive (concat (file-name-sans-extension tfile
) ".toda"))
1563 (garchive (concat (file-name-sans-extension gfile
) ".toda"))
1564 (archived-count (todo-get-count 'archived
))
1566 (with-current-buffer (get-buffer (find-file-noselect tfile
))
1568 (let* ((buffer-read-only nil
)
1571 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1573 (tbeg (progn (forward-line) (point-marker)))
1576 (concat "^" (regexp-quote todo-category-done
)) nil t
)
1577 (forward-line) (point-marker)))
1578 ;; Omit empty line between todo and done items.
1579 (tend (progn (forward-line -
2) (point-marker)))
1581 (if (re-search-forward
1582 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1584 (goto-char (match-beginning 0))
1586 (point-max-marker))))
1587 (todo (buffer-substring-no-properties tbeg tend
))
1588 (done (buffer-substring-no-properties dbeg cend
))
1589 (todo-count (todo-get-count 'todo cat
))
1590 (done-count (todo-get-count 'done cat
)))
1591 ;; Merge into goal todo category.
1592 (with-current-buffer (get-buffer (find-file-noselect gfile
))
1593 (unless (derived-mode-p 'todo-mode
) (todo-mode))
1595 (goto-char (point-min))
1596 (let ((buffer-read-only nil
))
1597 ;; Merge any todo items.
1598 (unless (zerop (length todo
))
1600 (concat "^" (regexp-quote (concat todo-category-beg goal
)) "$")
1603 (concat "^" (regexp-quote todo-category-done
)) nil t
)
1605 (setq here
(point-marker))
1607 (todo-update-count 'todo todo-count goal
))
1608 ;; Merge any done items.
1609 (unless (zerop (length done
))
1610 (goto-char (if (re-search-forward
1611 (concat "^" (regexp-quote todo-category-beg
))
1615 (when (zerop (length todo
)) (setq here
(point-marker)))
1617 (todo-update-count 'done done-count goal
)))
1618 (todo-update-categories-sexp))
1619 ;; Update and clean up source todo file.
1620 (remove-overlays cbeg cend
)
1621 (delete-region cbeg cend
)
1622 (setq todo-categories
(delete (assoc cat todo-categories
)
1624 (todo-update-categories-sexp)
1625 (when (> todo-category-number
(length todo-categories
))
1626 (setq todo-category-number
1))
1627 (todo-category-select)
1628 (mapc (lambda (m) (set-marker m nil
))
1629 (list cbeg tbeg dbeg tend cend
))))
1630 (when (> archived-count
0)
1631 (with-current-buffer (get-buffer (find-file-noselect tarchive
))
1633 (goto-char (point-min))
1634 (let* ((buffer-read-only nil
)
1636 (when (re-search-forward
1637 (concat "^" (regexp-quote
1638 (concat todo-category-beg cat
)) "$")
1640 (goto-char (match-beginning 0))
1642 (cend (if (re-search-forward
1643 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1649 (buffer-substring-no-properties (point) cend
))))
1650 ;; Merge into goal archive category, if it exists, else create it.
1651 (with-current-buffer (get-buffer (find-file-noselect garchive
))
1652 (let ((gbeg (when (re-search-forward
1653 (concat "^" (regexp-quote
1654 (concat todo-category-beg goal
))
1657 (goto-char (match-beginning 0))
1659 (goto-char (if (and gbeg
1661 (concat "^" (regexp-quote todo-category-beg
))
1665 (unless gbeg
(todo-add-category nil goal
))
1667 (todo-update-categories-sexp)))
1668 ;; Update and clean up source archive file.
1669 (remove-overlays cbeg cend
)
1670 (delete-region cbeg cend
)
1671 (setq todo-categories
(todo-make-categories-list t
))
1672 (todo-update-categories-sexp))))
1673 ;; Update goal todo file for merged archived items and display it.
1674 (set-window-buffer (selected-window) (set-buffer (get-file-buffer gfile
)))
1675 (unless (zerop archived-count
)
1676 (todo-update-count 'archived archived-count goal
)
1677 (todo-update-categories-sexp))
1678 (todo-category-number goal
)
1679 ;; If there are only merged done items, show them.
1680 (let ((todo-show-with-done (zerop (todo-get-count 'todo goal
))))
1681 (todo-category-select)
1682 ;; Put point on the first merged item.
1684 (set-marker here nil
)))
1686 ;; -----------------------------------------------------------------------------
1688 ;; -----------------------------------------------------------------------------
1690 (defcustom todo-include-in-diary nil
1691 "Non-nil to allow new todo items to be included in the diary."
1695 (defcustom todo-diary-nonmarking nil
1696 "Non-nil to insert new todo diary items as nonmarking by default.
1697 This appends `diary-nonmarking-symbol' to the front of an item on
1698 insertion provided it doesn't begin with `todo-nondiary-marker'."
1702 (defcustom todo-always-add-time-string nil
1703 "Non-nil adds current time to a new item's date header by default.
1704 When the todo insertion commands have a non-nil \"maybe-notime\"
1705 argument, this reverses the effect of
1706 `todo-always-add-time-string': if t, these commands omit the
1707 current time, if nil, they include it."
1711 (defcustom todo-use-only-highlighted-region t
1712 "Non-nil to enable inserting only highlighted region as new item."
1716 (defcustom todo-default-priority
'first
1717 "Default priority of new and moved items."
1718 :type
'(choice (const :tag
"Highest priority" first
)
1719 (const :tag
"Lowest priority" last
))
1722 (defcustom todo-item-mark
"*"
1723 "String used to mark items.
1724 To ensure item marking works, change the value of this option
1725 only when no items are marked."
1726 :type
'(string :validate
1728 (when (string= (widget-value widget
) todo-prefix
)
1732 "Invalid value: must be distinct from `todo-prefix'"))
1734 :set
(lambda (symbol value
)
1735 (custom-set-default symbol
(propertize value
'face
'todo-mark
)))
1738 (defcustom todo-comment-string
"COMMENT"
1739 "String inserted before optional comment appended to done item."
1741 :initialize
'custom-initialize-default
1742 :set
'todo-reset-comment-string
1745 (defcustom todo-undo-item-omit-comment
'ask
1746 "Whether to omit done item comment on undoing the item.
1747 Nil means never omit the comment, t means always omit it, `ask'
1748 means prompt user and omit comment only on confirmation."
1749 :type
'(choice (const :tag
"Never" nil
)
1750 (const :tag
"Always" t
)
1751 (const :tag
"Ask" ask
))
1754 (defun todo-toggle-mark-item (&optional n
)
1755 "Mark item with `todo-item-mark' if unmarked, otherwise unmark it.
1756 With positive numerical prefix argument N, change the marking of
1757 the next N items in the current category. If both the todo and
1758 done items sections are visible, the sequence of N items can
1759 consist of the the last todo items and the first done items."
1761 (when (todo-item-string)
1762 (let ((cat (todo-current-category)))
1763 (unless (> n
1) (setq n
1))
1766 (let* ((marks (assoc cat todo-categories-with-marks
))
1768 (unless (looking-at todo-item-start
)
1770 (todo-get-overlay 'prefix
)))
1771 (pref (overlay-get ov
'before-string
)))
1772 (if (todo-marked-item-p)
1774 (overlay-put ov
'before-string
(substring pref
1))
1775 (if (= (cdr marks
) 1) ; Deleted last mark in this category.
1776 (setq todo-categories-with-marks
1777 (assq-delete-all cat todo-categories-with-marks
))
1778 (setcdr marks
(1- (cdr marks
)))))
1779 (overlay-put ov
'before-string
(concat todo-item-mark pref
))
1781 (setcdr marks
(1+ (cdr marks
)))
1782 (push (cons cat
1) todo-categories-with-marks
))))
1784 ;; Don't try to mark the empty lines at the end of the todo
1785 ;; and done items sections.
1786 (when (looking-at "^$")
1789 (todo-forward-item))))))))
1791 (defun todo-mark-category ()
1792 "Mark all visible items in this category with `todo-item-mark'."
1794 (let ((cat (todo-current-category)))
1796 (goto-char (point-min))
1798 (let* ((marks (assoc cat todo-categories-with-marks
))
1799 (ov (todo-get-overlay 'prefix
))
1800 ;; When done items are shown and there are no todo items, the
1801 ;; loop starts on the empty line in the todo items sections,
1802 ;; which has no overlay, so don't try to get it.
1803 (pref (when ov
(overlay-get ov
'before-string
))))
1804 (unless (or (todo-marked-item-p) (not ov
))
1805 (overlay-put ov
'before-string
(concat todo-item-mark pref
))
1807 (setcdr marks
(1+ (cdr marks
)))
1808 (push (cons cat
1) todo-categories-with-marks
))))
1810 ;; Don't try to mark the empty line between the todo and done
1812 (when (looking-at "^$")
1814 (todo-forward-item)))))))
1816 (defun todo-unmark-category ()
1817 "Remove `todo-item-mark' from all visible items in this category."
1819 (let* ((cat (todo-current-category))
1820 (marks (assoc cat todo-categories-with-marks
)))
1822 (goto-char (point-min))
1824 (let* ((ov (todo-get-overlay 'prefix
))
1825 ;; See comment above in `todo-mark-category'.
1826 (pref (when ov
(overlay-get ov
'before-string
))))
1827 (when (todo-marked-item-p)
1828 (overlay-put ov
'before-string
(substring pref
1)))
1829 (todo-forward-item))))
1830 (setq todo-categories-with-marks
1831 (delq marks todo-categories-with-marks
))))
1833 (defvar todo-date-from-calendar nil
1834 "Helper variable for setting item date from the Emacs Calendar.")
1836 (defvar todo-insert-item--keys-so-far
)
1837 (defvar todo-insert-item--parameters
)
1839 (defun todo-insert-item (&optional arg
)
1840 "Choose an item insertion operation and carry it out.
1841 This inserts a new todo item into a category.
1843 With no prefix argument ARG, add the item to the current
1844 category; with one prefix argument (`C-u'), prompt for a category
1845 from the current todo file; with two prefix arguments (`C-u
1846 C-u'), first prompt for a todo file, then a category in that
1847 file. If a non-existing category is entered, ask whether to add
1848 it to the todo file; if answered affirmatively, add the category
1849 and insert the item there.
1851 There are a number of item insertion parameters which can be
1852 combined by entering specific keys to produce different insertion
1853 commands. After entering each key, a message shows which have
1854 already been entered and which remain available. See
1855 `(todo-mode) Inserting New Items' for details of the parameters,
1856 their associated keys and their effects."
1858 (setq todo-insert-item--keys-so-far
"i")
1859 (todo-insert-item--next-param nil
(list arg
) todo-insert-item--parameters
))
1861 (defun todo-insert-item--basic (&optional arg diary-type date-type time where
)
1862 "Function implementing the core of `todo-insert-item'."
1863 ;; If invoked outside of Todo mode and there is not yet any Todo
1864 ;; file, initialize one.
1865 (if (null (funcall todo-files-function
))
1867 (let ((copy (eq where
'copy
))
1868 (region (eq where
'region
))
1869 (here (eq where
'here
))
1873 ((not (eq major-mode
'todo-mode
))
1874 (user-error "You must be in Todo mode to copy a todo item"))
1876 (user-error "You cannot copy a done item as a new todo item"))
1878 (user-error "Point must be on a todo item to copy it")))
1879 (setq diary-item
(todo-diary-item-p)))
1881 (let (use-empty-active-region)
1882 (unless (and todo-use-only-highlighted-region
(use-region-p))
1883 (user-error "There is no active region"))))
1884 (let* ((obuf (current-buffer))
1885 (ocat (todo-current-category))
1887 (todo-mm (eq major-mode
'todo-mode
))
1888 (cat+file
(cond ((equal arg
'(4))
1889 (todo-read-category "Insert in category: "))
1891 (todo-read-category "Insert in category: "
1894 (cons (todo-current-category)
1895 (or todo-current-todo-file
1896 (and todo-show-current-file
1897 todo-global-current-todo-file
)
1898 (todo-absolute-file-name
1899 todo-default-todo-file
))))))
1900 (cat (car cat
+file
))
1901 (file (cdr cat
+file
))
1902 (new-item (cond (copy (todo-item-string))
1903 (region (buffer-substring-no-properties
1904 (region-beginning) (region-end)))
1905 (t (read-from-minibuffer "Todo item: "))))
1907 ((eq date-type
'date
)
1909 ((eq date-type
'dayname
)
1910 (todo-read-dayname))
1911 ((eq date-type
'calendar
)
1912 (setq todo-date-from-calendar t
)
1913 (or (todo-set-date-from-calendar)
1914 ;; If user exits Calendar before choosing
1915 ;; a date, cancel item insertion.
1917 ((and (stringp date-type
)
1918 (string-match todo-date-pattern date-type
))
1919 (setq todo-date-from-calendar date-type
)
1920 (todo-set-date-from-calendar))
1922 (calendar-date-string
1923 (calendar-current-date) t t
))))
1924 (time-string (or (and time
(todo-read-time))
1925 (and todo-always-add-time-string
1926 (substring (current-time-string) 11 16)))))
1927 (setq todo-date-from-calendar nil
)
1928 (find-file-noselect file
'nowarn
)
1929 (set-window-buffer (selected-window)
1930 (set-buffer (find-buffer-visiting file
)))
1931 ;; If this command was invoked outside of a Todo mode buffer,
1932 ;; the call to todo-current-category above returned nil. If
1933 ;; we just entered Todo mode now, then cat was set to the
1934 ;; file's first category, but if todo-mode was already
1935 ;; enabled, cat did not get set, so we have to do that.
1937 (setq cat
(todo-current-category)))
1938 (setq todo-current-todo-file file
)
1939 (unless todo-global-current-todo-file
1940 (setq todo-global-current-todo-file todo-current-todo-file
))
1941 (let ((buffer-read-only nil
)
1942 (called-from-outside (not (and todo-mm
(equal cat ocat
))))
1943 done-only item-added
)
1946 ;; Add date, time and diary marking as required.
1947 (concat (if (not (and diary-type
1948 (not todo-include-in-diary
)))
1950 (when (and (eq diary-type
'nonmarking
)
1951 (not todo-diary-nonmarking
))
1952 diary-nonmarking-symbol
))
1953 date-string
(when (and time-string
; Can be empty.
1956 (concat " " time-string
))
1957 (when (not (and diary-type
1958 (not todo-include-in-diary
)))
1961 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
1962 (setq new-item
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
1963 "\n\t" new-item nil nil
1)))
1966 ;; Make sure the correct category is selected. There
1967 ;; are two cases: (i) we just visited the file, so no
1968 ;; category is selected yet, or (ii) we invoked
1969 ;; insertion "here" from outside the category we want
1970 ;; to insert in (with priority insertion, category
1971 ;; selection is done by todo-set-item-priority).
1972 (when (or (= (- (point-max) (point-min)) (buffer-size))
1973 (and here called-from-outside
))
1974 (todo-category-number cat
)
1975 (todo-category-select))
1976 ;; If only done items are displayed in category,
1977 ;; toggle to todo items before inserting new item.
1978 (when (save-excursion
1979 (goto-char (point-min))
1980 (looking-at todo-done-string-start
))
1982 (todo-toggle-view-done-only))
1985 ;; If command was invoked with point in done
1986 ;; items section or outside of the current
1987 ;; category, can't insert "here", so to be
1988 ;; useful give new item top priority.
1989 (when (or (todo-done-item-section-p)
1992 (goto-char (point-min)))
1993 (todo-insert-with-overlays new-item
))
1994 (todo-set-item-priority new-item cat t
))
1995 (setq item-added t
))
1996 ;; If user cancels before setting priority, restore
1999 (set-window-buffer (selected-window) (set-buffer obuf
))
2001 (unless (equal cat ocat
)
2002 (todo-category-number ocat
)
2003 (todo-category-select))
2004 (and done-only
(todo-toggle-view-done-only)))
2006 ;; If the todo items section is not visible when the
2007 ;; insertion command is called (either because only done
2008 ;; items were shown or because the category was not in the
2009 ;; current buffer), then if the item is inserted at the
2010 ;; end of the category, point is at eob and eob at
2011 ;; window-start, so that higher priority todo items are
2012 ;; out of view. So we recenter to make sure the todo
2013 ;; items are displayed in the window.
2014 (when item-added
(recenter)))
2015 (todo-update-count 'todo
1)
2016 (when (or diary-item diary-type todo-include-in-diary
)
2017 (todo-update-count 'diary
1))
2018 (todo-update-categories-sexp))))))
2020 (defun todo-set-date-from-calendar ()
2021 "Return string of date chosen from Calendar."
2022 (cond ((and (stringp todo-date-from-calendar
)
2023 (string-match todo-date-pattern todo-date-from-calendar
))
2024 todo-date-from-calendar
)
2025 (todo-date-from-calendar
2026 (let (calendar-view-diary-initially-flag)
2027 (calendar)) ; *Calendar* is now current buffer.
2028 (define-key calendar-mode-map
[remap newline
] 'exit-recursive-edit
)
2029 ;; If user exits Calendar before choosing a date, clean up properly.
2030 (define-key calendar-mode-map
2031 [remap calendar-exit
] (lambda ()
2035 (exit-recursive-edit))))
2036 (message "Put cursor on a date and type <return> to set it.")
2039 (when (equal (buffer-name) calendar-buffer
)
2040 (setq todo-date-from-calendar
2041 (calendar-date-string (calendar-cursor-to-date t
) t t
))
2043 todo-date-from-calendar
)
2044 (define-key calendar-mode-map
[remap newline
] nil
)
2045 (define-key calendar-mode-map
[remap calendar-exit
] nil
)
2046 (unless (zerop (recursion-depth)) (exit-recursive-edit))
2047 (when (stringp todo-date-from-calendar
)
2048 todo-date-from-calendar
)))))
2050 (defun todo-insert-item-from-calendar (&optional arg
)
2051 "Prompt for and insert a new item with date selected from calendar.
2052 Invoked without prefix argument ARG, insert the item into the
2053 current category, without one prefix argument, prompt for the
2054 category from the current todo file or from one listed in
2055 `todo-category-completions-files'; with two prefix arguments,
2056 prompt for a todo file and then for a category in it."
2058 (setq todo-date-from-calendar
2059 (calendar-date-string (calendar-cursor-to-date t
) t t
))
2061 (todo-insert-item--basic arg nil todo-date-from-calendar
))
2063 (define-key calendar-mode-map
"it" 'todo-insert-item-from-calendar
)
2065 (defun todo-delete-item ()
2066 "Delete at least one item in this category.
2067 If there are marked items, delete all of these; otherwise, delete
2072 (let* ((cat (todo-current-category))
2073 (marked (assoc cat todo-categories-with-marks
))
2074 (item (unless marked
(todo-item-string)))
2077 "Permanently delete all marked items? ")
2079 (setq ov
(make-overlay
2080 (save-excursion (todo-item-start))
2081 (save-excursion (todo-item-end))))
2082 (overlay-put ov
'face
'todo-search
)
2083 (todo-y-or-n-p "Permanently delete this item? "))))
2086 (and marked
(goto-char (point-min)))
2089 (if (or (and marked
(todo-marked-item-p)) item
)
2091 (if (todo-done-item-p)
2092 (todo-update-count 'done -
1)
2093 (todo-update-count 'todo -
1 cat
)
2094 (and (todo-diary-item-p)
2095 (todo-update-count 'diary -
1)))
2096 (if ov
(delete-overlay ov
))
2098 ;; Don't leave point below last item.
2099 (and item
(bolp) (eolp) (< (point-min) (point-max))
2100 (todo-backward-item))
2102 (throw 'done
(setq item nil
))))
2103 (todo-forward-item))))
2105 (setq todo-categories-with-marks
2106 (assq-delete-all cat todo-categories-with-marks
)))
2107 (todo-update-categories-sexp)
2108 (todo-prefix-overlays)))
2109 (if ov
(delete-overlay ov
)))))
2111 (defvar todo-edit-item--param-key-alist
)
2112 (defvar todo-edit-done-item--param-key-alist
)
2114 (defun todo-edit-item (&optional arg
)
2115 "Choose an editing operation for the current item and carry it out."
2117 (let ((marked (assoc (todo-current-category) todo-categories-with-marks
)))
2118 (cond ((and (todo-done-item-p) (not marked
))
2119 (todo-edit-item--next-key todo-edit-done-item--param-key-alist
))
2120 ((or marked
(todo-item-string))
2121 (todo-edit-item--next-key todo-edit-item--param-key-alist arg
)))))
2123 (defun todo-edit-item--text (&optional arg
)
2124 "Function providing the text editing facilities of `todo-edit-item'."
2125 (let ((full-item (todo-item-string)))
2126 ;; If there are marked items and user invokes a text-editing
2127 ;; commands with point not on an item, todo-item-start is nil and
2128 ;; 1+ signals an error, so just make this a noop.
2130 (let* ((opoint (point))
2131 (start (todo-item-start))
2132 (end (save-excursion (todo-item-end)))
2135 (concat todo-date-string-start todo-date-pattern
2136 "\\( " diary-time-regexp
"\\)?"
2137 (regexp-quote todo-nondiary-end
) "?")
2138 (line-end-position) t
)
2139 (1+ (- (point) start
))))
2140 (include-header (eq arg
'include-header
))
2141 (comment-edit (eq arg
'comment-edit
))
2142 (comment-delete (eq arg
'comment-delete
))
2143 (header-string (substring full-item
0 item-beg
))
2144 (item (if (or include-header comment-edit comment-delete
)
2146 (substring full-item item-beg
)))
2147 (multiline (or (eq arg
'multiline
)
2148 (> (length (split-string item
"\n")) 1)))
2149 (comment (save-excursion
2152 (concat " \\[" (regexp-quote todo-comment-string
)
2155 (prompt (if comment
"Edit comment: " "Enter a comment: "))
2156 (buffer-read-only nil
))
2157 ;; When there are marked items, user can invoke todo-edit-item
2158 ;; even if point is not on an item, but text editing only
2159 ;; applies to the item at point.
2160 (when (or (and (todo-done-item-p)
2161 (or comment-edit comment-delete
))
2162 (and (not (todo-done-item-p))
2163 (or (not arg
) include-header multiline
)))
2165 ((or comment-edit comment-delete
)
2168 (if (re-search-forward (concat " \\["
2169 (regexp-quote todo-comment-string
)
2173 (when (todo-y-or-n-p "Delete comment? ")
2174 (delete-region (match-beginning 0) (match-end 0)))
2175 (replace-match (read-string prompt
(cons (match-string 1) 1))
2178 (user-error "There is no comment to delete")
2179 (insert " [" todo-comment-string
": "
2180 (prog1 (read-string prompt
)
2181 ;; If user moved point during editing,
2182 ;; make sure it moves back.
2187 (let ((buf todo-edit-buffer
))
2188 (set-window-buffer (selected-window)
2189 (set-buffer (make-indirect-buffer
2190 (buffer-name) buf
)))
2191 (narrow-to-region (todo-item-start) (todo-item-end))
2193 (message "%s" (substitute-command-keys
2194 (concat "Type \\[todo-edit-quit] "
2195 "to return to Todo mode.\n")))))
2197 (let ((new (concat (if include-header
"" header-string
)
2198 (read-string "Edit: " (if include-header
2199 (cons item item-beg
)
2201 (when include-header
2202 (while (not (string-match (concat todo-date-string-start
2205 (setq new
(read-from-minibuffer
2206 "Item must start with a date: " new
))))
2207 ;; Ensure lines following hard newlines are indented.
2208 (setq new
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
2209 "\n\t" new nil nil
1))
2210 ;; If user moved point during editing, make sure it moves back.
2213 (todo-insert-with-overlays new
)
2214 (move-to-column item-beg
)))))))))
2216 (defun todo-edit-quit ()
2217 "Return from Todo Edit mode to Todo mode.
2218 If the item contains hard line breaks, make sure the following
2219 lines are indented by `todo-indent-to-here' to conform to diary
2222 If the whole file was in Todo Edit mode, check before returning
2223 whether the file is still a valid todo file and if so, also
2224 recalculate the todo file's categories sexp, in case changes were
2225 made in the number or names of categories."
2227 (if (> (buffer-size) (- (point-max) (point-min)))
2228 ;; We got here via `e m'.
2229 (let ((item (buffer-string))
2230 (regex "\\(\n\\)[^[:blank:]]")
2231 (buf (buffer-base-buffer)))
2232 (while (not (string-match (concat todo-date-string-start
2235 (setq item
(read-from-minibuffer
2236 "Item must start with a date: " item
)))
2237 ;; Ensure lines following hard newlines are indented.
2238 (when (string-match regex
(buffer-string))
2239 (setq item
(replace-regexp-in-string regex
"\n\t" item nil nil
1))
2240 (delete-region (point-min) (point-max))
2243 (unless (eq (current-buffer) buf
)
2244 (set-window-buffer (selected-window) (set-buffer buf
))))
2245 ;; We got here via `F e'.
2246 (when (todo-check-format)
2247 ;; FIXME: separate out sexp check?
2248 ;; If manual editing makes e.g. item counts change, have to
2249 ;; call this to update todo-categories, but it restores
2250 ;; category order to list order.
2251 ;; (todo-repair-categories-sexp)
2252 ;; Compare (todo-make-categories-list t) with sexp and if
2253 ;; different ask (todo-update-categories-sexp) ?
2255 (let* ((cat-beg (concat "^" (regexp-quote todo-category-beg
)
2257 (curline (buffer-substring-no-properties
2258 (line-beginning-position) (line-end-position)))
2259 (cat (cond ((string-match cat-beg curline
)
2260 (match-string-no-properties 1 curline
))
2261 ((or (re-search-backward cat-beg nil t
)
2262 (re-search-forward cat-beg nil t
))
2263 (match-string-no-properties 1)))))
2264 (todo-category-number cat
)
2265 (todo-category-select)
2266 (goto-char (point-min))))))
2268 (defun todo-edit-item--header (what &optional inc
)
2269 "Function providing header editing facilities of `todo-edit-item'."
2270 (let ((marked (assoc (todo-current-category) todo-categories-with-marks
))
2272 (todo-date-from-calendar t
)
2273 ;; INC must be an integer, but users could pass it via
2274 ;; `todo-edit-item' as e.g. `-' or `C-u'.
2275 (inc (prefix-numeric-value inc
))
2276 (buffer-read-only nil
)
2277 ndate ntime year monthname month day
2278 dayname
) ; Needed by calendar-date-display-form.
2279 (when marked
(todo--user-error-if-marked-done-item))
2281 (or (and marked
(goto-char (point-min))) (todo-item-start))
2285 (while (not (todo-marked-item-p))
2287 (and (eobp) (throw 'end nil
))))
2288 (re-search-forward (concat todo-date-string-start
"\\(?1:"
2290 "\\)\\(?2: " diary-time-regexp
"\\)?"
2291 (regexp-quote todo-nondiary-end
) "?")
2292 (line-end-position) t
)
2293 (let* ((otime (match-string-no-properties 2))
2294 (odayname (match-string-no-properties 5))
2295 (omonthname (match-string-no-properties 6))
2296 (omonth (match-string-no-properties 7))
2297 (oday (match-string-no-properties 8))
2298 (oyear (match-string-no-properties 9))
2299 (tmn-array todo-month-name-array
)
2300 (mlist (append tmn-array nil
))
2301 (tma-array todo-month-abbrev-array
)
2302 (mablist (append tma-array nil
))
2303 (yy (and oyear
(string-to-number oyear
))) ; 0 if year is "*".
2304 (mm (or (and omonth
(if (string= omonth
"*") 13
2305 (string-to-number omonth
)))
2306 (1+ (- (length mlist
)
2307 (length (or (member omonthname mlist
)
2308 (member omonthname mablist
)))))))
2309 (dd (and oday
(unless (string= oday
"*")
2310 (string-to-number oday
)))))
2311 ;; If there are marked items, use only the first to set
2312 ;; header changes, and apply these to all marked items.
2316 (setq ndate
(todo-read-date)))
2317 ((eq what
'calendar
)
2318 (setq ndate
(save-match-data (todo-set-date-from-calendar))))
2320 (setq ndate
(calendar-date-string (calendar-current-date) t t
)))
2322 (setq ndate
(todo-read-dayname)))
2324 (setq ntime
(save-match-data (todo-read-time)))
2325 (when (> (length ntime
) 0)
2326 (setq ntime
(concat " " ntime
))))
2327 ;; When date string consists only of a day name,
2328 ;; passing other date components is a noop.
2329 ((and odayname
(memq what
'(year month day
))))
2332 monthname omonthname
2334 year
(cond ((not current-prefix-arg
)
2335 (todo-read-date 'year
))
2336 ((string= oyear
"*")
2337 (user-error "Cannot increment *"))
2339 (number-to-string (+ yy inc
))))))
2343 (if (memq 'month calendar-date-display-form
)
2346 (cond ((not current-prefix-arg
)
2347 (todo-read-date 'month
))
2348 ((or (string= omonth
"*") (= mm
13))
2349 (user-error "Cannot increment *"))
2351 (let ((mminc (+ mm inc
)))
2352 ;; Increment or decrement month by INC
2354 (setq mm
(% mminc
12))
2355 ;; If result is 0, make month December.
2356 (setq mm
(if (= mm
0) 12 (abs mm
)))
2357 ;; Adjust year if necessary.
2358 (setq year
(or (and (cond ((> mminc
12)
2359 (+ yy
(/ mminc
12)))
2361 (- yy
(/ mminc
12) 1))
2363 (number-to-string yy
))
2365 ;; Return the changed numerical month as
2366 ;; a string or the corresponding month name.
2368 (number-to-string mm
)
2369 (aref tma-array
(1- mm
))))))
2370 ;; Since the number corresponding to the arbitrary
2371 ;; month name "*" is out of the range of
2372 ;; calendar-last-day-of-month, set it to 1
2373 ;; (corresponding to January) to allow 31 days.
2374 (let ((mm (if (= mm
13) 1 mm
)))
2375 (if (> (string-to-number day
)
2376 (calendar-last-day-of-month mm yy
))
2377 (user-error "%s %s does not have %s days"
2378 (aref tmn-array
(1- mm
))
2379 (if (= mm
2) yy
"") day
))))
2383 monthname omonthname
2385 ((not current-prefix-arg
)
2386 (todo-read-date 'day mm yy
))
2388 (user-error "Cannot increment *"))
2389 ((or (string= omonth
"*") (string= omonthname
"*"))
2390 (setq dd
(+ dd inc
))
2393 "A month cannot have more than 31 days")
2394 (number-to-string dd
)))
2395 ;; Increment or decrement day by INC,
2396 ;; adjusting month and year if necessary
2397 ;; (if year is "*" assume current year to
2398 ;; calculate adjustment).
2400 (let* ((yy (or yy
(calendar-extract-year
2401 (calendar-current-date))))
2402 (date (calendar-gregorian-from-absolute
2403 (+ (calendar-absolute-from-gregorian
2406 (adjmm (nth 0 date
)))
2407 ;; Set year and month(name) to adjusted values.
2408 (unless (string= year
"*")
2409 (setq year
(number-to-string (nth 2 date
))))
2411 (setq month
(number-to-string adjmm
))
2412 (setq monthname
(aref tma-array
(1- adjmm
))))
2413 ;; Return changed numerical day as a string.
2414 (number-to-string (nth 1 date
)))))))))
2416 ;; If year, month or day date string components were
2417 ;; changed, rebuild the date string.
2418 (when (memq what
'(year month day
))
2419 (setq ndate
(mapconcat #'eval calendar-date-display-form
""))))
2420 (when ndate
(replace-match ndate nil nil nil
1))
2421 ;; Add new time string to the header, if it was supplied.
2424 (replace-match ntime nil nil nil
2)
2425 (goto-char (match-end 1))
2427 (setq todo-date-from-calendar nil
)
2429 ;; Apply the changes to the first marked item header to the
2430 ;; remaining marked items. If there are no marked items,
2434 (goto-char (point-max))))))))
2436 (defun todo-edit-item--diary-inclusion (&optional nonmarking
)
2437 "Function providing diary marking facilities of `todo-edit-item'."
2438 (let ((buffer-read-only)
2439 (marked (assoc (todo-current-category) todo-categories-with-marks
)))
2440 (when marked
(todo--user-error-if-marked-done-item))
2443 (when marked
(goto-char (point-min)))
2445 (unless (and marked
(not (todo-marked-item-p)))
2446 (let* ((_beg (todo-item-start))
2447 (lim (save-excursion (todo-item-end)))
2448 (end (save-excursion
2449 (or (todo-time-string-matcher lim
)
2450 (todo-date-string-matcher lim
)))))
2452 (if (looking-at (regexp-quote diary-nonmarking-symbol
))
2454 (when (looking-at (regexp-quote todo-nondiary-start
))
2457 (search-forward todo-nondiary-end
(1+ end
) t
)
2459 (todo-update-count 'diary
1)))
2460 (insert diary-nonmarking-symbol
))
2461 (if (looking-at (regexp-quote todo-nondiary-start
))
2464 (search-forward todo-nondiary-end
(1+ end
) t
)
2466 (todo-update-count 'diary
1))
2468 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2470 (setq end
(1- end
))) ; Since we deleted nonmarking symbol.
2471 (insert todo-nondiary-start
)
2472 (goto-char (1+ end
))
2473 (insert todo-nondiary-end
)
2474 (todo-update-count 'diary -
1))))))
2475 (unless marked
(throw 'stop nil
))
2476 (todo-forward-item)))))
2477 (todo-update-categories-sexp))
2479 (defun todo-edit-category-diary-inclusion (arg)
2480 "Make all items in this category diary items.
2481 With prefix ARG, make all items in this category non-diary
2485 (goto-char (point-min))
2486 (let ((todo-count (todo-get-count 'todo
))
2487 (diary-count (todo-get-count 'diary
))
2491 (if (todo-done-item-p) ; We've gone too far.
2493 (let* ((_beg (todo-item-start))
2494 (lim (save-excursion (todo-item-end)))
2495 (end (save-excursion
2496 (or (todo-time-string-matcher lim
)
2497 (todo-date-string-matcher lim
)))))
2499 (unless (looking-at (regexp-quote todo-nondiary-start
))
2500 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2502 (setq end
(1- end
))) ; Since we deleted nonmarking symbol.
2503 (insert todo-nondiary-start
)
2504 (goto-char (1+ end
))
2505 (insert todo-nondiary-end
))
2506 (when (looking-at (regexp-quote todo-nondiary-start
))
2508 (search-forward todo-nondiary-end
(1+ end
) t
)
2509 (replace-match "")))))
2510 (todo-forward-item))
2511 (unless (if arg
(zerop diary-count
) (= diary-count todo-count
))
2512 (todo-update-count 'diary
(if arg
2514 (- todo-count diary-count
))))
2515 (todo-update-categories-sexp)))))
2517 (defun todo-edit-category-diary-nonmarking (arg)
2518 "Add `diary-nonmarking-symbol' to all diary items in this category.
2519 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
2520 items in this category."
2523 (goto-char (point-min))
2524 (let (buffer-read-only)
2527 (if (todo-done-item-p) ; We've gone too far.
2529 (unless (looking-at (regexp-quote todo-nondiary-start
))
2531 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2533 (unless (looking-at (regexp-quote diary-nonmarking-symbol
))
2534 (insert diary-nonmarking-symbol
))))
2535 (todo-forward-item)))))))
2537 (defun todo-set-item-priority (&optional item cat new arg
)
2538 "Prompt for and set ITEM's priority in CATegory.
2540 Interactively, ITEM is the todo item at point, CAT is the current
2541 category, and the priority is a number between 1 and the number
2542 of items in the category. Non-interactively, non-nil NEW means
2543 ITEM is a new item and the lowest priority is one more than the
2544 number of items in CAT.
2546 The new priority is set either interactively by prompt or by a
2547 numerical prefix argument, or noninteractively by argument ARG,
2548 whose value can be either of the symbols `raise' or `lower',
2549 meaning to raise or lower the item's priority by one."
2551 (unless (and (or (called-interactively-p 'any
) (memq arg
'(raise lower
)))
2552 (or (todo-done-item-p) (looking-at "^$")))
2553 (let* ((item (or item
(todo-item-string)))
2554 (marked (todo-marked-item-p))
2555 (cat (or cat
(cond ((eq major-mode
'todo-mode
)
2556 (todo-current-category))
2557 ((eq major-mode
'todo-filtered-items-mode
)
2559 (concat todo-date-string-start
2561 "\\( " diary-time-regexp
"\\)?"
2562 (regexp-quote todo-nondiary-end
)
2563 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
2565 (re-search-forward regexp1 nil t
)
2566 (match-string-no-properties 1)))))))
2568 (todo (cond ((or (memq arg
'(raise lower
))
2569 (eq major-mode
'todo-filtered-items-mode
))
2571 (let ((curstart (todo-item-start))
2573 (goto-char (point-min))
2574 (while (looking-at todo-item-start
)
2575 (setq count
(1+ count
))
2576 (when (= (point) curstart
) (setq curnum count
))
2577 (todo-forward-item))
2579 ((eq major-mode
'todo-mode
)
2580 (todo-get-count 'todo cat
))))
2581 (maxnum (if new
(1+ todo
) todo
))
2582 (prompt (format "Set item priority (1-%d): " maxnum
))
2583 (priority (cond ((and (not arg
) (numberp current-prefix-arg
))
2585 ((and (eq arg
'raise
) (>= curnum
1))
2587 ((and (eq arg
'lower
) (<= curnum maxnum
))
2591 (unless (and priority
2592 (or (and (eq arg
'raise
) (zerop priority
))
2593 (and (eq arg
'lower
) (> priority maxnum
))))
2594 ;; When moving item to another category, show the category before
2595 ;; prompting for its priority.
2596 (unless (or arg
(called-interactively-p 'any
))
2597 (todo-category-number cat
)
2598 ;; If done items in category are visible, keep them visible.
2599 (let ((done todo-show-with-done
))
2600 (when (> (buffer-size) (- (point-max) (point-min)))
2602 (goto-char (point-min))
2603 (setq done
(re-search-forward todo-done-string-start nil t
))))
2604 (let ((todo-show-with-done done
))
2605 ;; Keep current item or top of moved to category in view
2606 ;; while setting priority.
2607 (save-excursion (todo-category-select)))))
2608 ;; Prompt for priority only when the category has at least one
2611 (while (not priority
)
2612 (setq candidate
(read-number prompt
2613 (if (eq todo-default-priority
'first
)
2615 (setq prompt
(when (or (< candidate
1) (> candidate maxnum
))
2616 (format "Priority must be an integer between 1 and %d.\n"
2618 (unless prompt
(setq priority candidate
))))
2619 ;; In Top Priorities buffer, an item's priority can be changed
2620 ;; wrt items in another category, but not wrt items in the same
2622 (when (eq major-mode
'todo-filtered-items-mode
)
2623 (let* ((regexp2 (concat todo-date-string-start todo-date-pattern
2624 "\\( " diary-time-regexp
"\\)?"
2625 (regexp-quote todo-nondiary-end
)
2626 "?\\(?1:" (regexp-quote cat
) "\\)"))
2627 (end (cond ((< curnum priority
)
2628 (save-excursion (todo-item-end)))
2629 ((> curnum priority
)
2630 (save-excursion (todo-item-start)))))
2631 (match (save-excursion
2632 (cond ((< curnum priority
)
2633 (todo-forward-item (1+ (- priority curnum
)))
2634 (when (re-search-backward regexp2 end t
)
2635 (match-string-no-properties 1)))
2636 ((> curnum priority
)
2637 (todo-backward-item (- curnum priority
))
2638 (when (re-search-forward regexp2 end t
)
2639 (match-string-no-properties 1)))))))
2641 (user-error (concat "Cannot reprioritize items from the same "
2642 "category in this mode, only in Todo mode")))))
2643 ;; Interactively or with non-nil ARG, relocate the item within its
2645 (when (or arg
(called-interactively-p 'any
))
2647 (goto-char (point-min))
2649 (unless (= priority
1)
2650 (todo-forward-item (1- priority
))
2651 ;; When called from todo-item-undone and the highest priority
2652 ;; is chosen, this advances point to the first done item, so
2653 ;; move it up to the empty line above the done items
2655 (when (looking-back (concat "^"
2656 (regexp-quote todo-category-done
)
2658 (line-beginning-position 0))
2659 (todo-backward-item))))
2660 (todo-insert-with-overlays item
)
2661 ;; If item was marked, restore the mark.
2663 (let* ((ov (todo-get-overlay 'prefix
))
2664 (pref (overlay-get ov
'before-string
)))
2665 (overlay-put ov
'before-string
2666 (concat todo-item-mark pref
))))))))
2668 (defun todo-raise-item-priority ()
2669 "Raise priority of current item by moving it up by one item."
2671 (todo-set-item-priority nil nil nil
'raise
))
2673 (defun todo-lower-item-priority ()
2674 "Lower priority of current item by moving it down by one item."
2676 (todo-set-item-priority nil nil nil
'lower
))
2678 (defun todo-move-item (&optional file
)
2679 "Move at least one todo or done item to another category.
2680 If there are marked items, move all of these; otherwise, move
2683 With prefix argument FILE, prompt for a specific todo file and
2684 choose (with TAB completion) a category in it to move the item or
2685 items to; otherwise, choose and move to any category in either
2686 the current todo file or one of the files in
2687 `todo-category-completions-files'. If the chosen category is
2688 not an existing categories, then it is created and the item(s)
2689 become(s) the first entry/entries in that category.
2691 With moved todo items, prompt to set the priority in the category
2692 moved to (with multiple todo items, the one that had the highest
2693 priority in the category moved from gets the new priority and the
2694 rest of the moved todo items are inserted in sequence below it).
2695 Moved done items are appended to the top of the done items
2696 section in the category moved to."
2698 (let* ((cat1 (todo-current-category))
2699 (marked (assoc cat1 todo-categories-with-marks
)))
2700 ;; Noop if point is not on an item and there are no marked items.
2701 (unless (and (looking-at "^$")
2703 (let* ((buffer-read-only)
2704 (file1 todo-current-todo-file
)
2705 (item (todo-item-string))
2706 (done-item (and (todo-done-item-p) item
))
2707 (omark (save-excursion (todo-item-start) (point-marker)))
2711 ov cat2 file2 moved nmark todo-items done-items
)
2715 (setq ov
(make-overlay (save-excursion (todo-item-start))
2716 (save-excursion (todo-item-end))))
2717 (overlay-put ov
'face
'todo-search
))
2718 (let* ((pl (if (and marked
(> (cdr marked
) 1)) "s" ""))
2719 (cat+file
(todo-read-category (concat "Move item" pl
2722 (while (and (equal (car cat
+file
) cat1
)
2723 (equal (cdr cat
+file
) file1
))
2724 (setq cat
+file
(todo-read-category
2725 "Choose a different category: ")))
2726 (setq cat2
(car cat
+file
)
2727 file2
(cdr cat
+file
))))
2728 (if ov
(delete-overlay ov
)))
2729 (set-buffer (find-buffer-visiting file1
))
2732 (goto-char (point-min))
2734 (when (todo-marked-item-p)
2735 (if (todo-done-item-p)
2737 (push (todo-item-string) done-items
)
2738 (setq done
(1+ done
)))
2739 (push (todo-item-string) todo-items
)
2740 (setq todo
(1+ todo
))
2741 (when (todo-diary-item-p)
2742 (setq diary
(1+ diary
)))))
2743 (todo-forward-item))
2744 (setq todo-items
(nreverse todo-items
))
2745 (setq done-items
(nreverse done-items
)))
2746 (if (todo-done-item-p)
2748 (push done-item done-items
)
2750 (push item todo-items
)
2752 (when (todo-diary-item-p) (setq diary
1))))
2753 (set-window-buffer (selected-window)
2754 (set-buffer (find-file-noselect file2
'nowarn
)))
2758 (todo-set-item-priority (pop todo-items
) cat2 t
)
2762 (todo-insert-with-overlays (pop todo-items
))))
2763 ;; Move done items en bloc to top of done items section.
2765 (todo-category-number cat2
)
2767 (goto-char (point-min))
2769 (concat "^" (regexp-quote (concat todo-category-beg cat2
)) "$")
2772 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2774 (unless here
(setq here
(point)))
2776 (todo-insert-with-overlays (pop done-items
))
2777 (todo-forward-item)))
2778 ;; If only done items were moved, move point to the top
2779 ;; one, otherwise, move point to the top moved todo item.
2783 ;; Move succeeded, so remove item from starting category,
2784 ;; update item counts and display the category containing
2787 (setq nmark
(point-marker))
2788 (when todo
(todo-update-count 'todo todo
))
2789 (when diary
(todo-update-count 'diary diary
))
2790 (when done
(todo-update-count 'done done
))
2791 (todo-update-categories-sexp)
2792 (with-current-buffer (find-buffer-visiting file1
)
2801 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
2804 (setq end
(if (re-search-forward
2806 (regexp-quote todo-category-beg
))
2809 (goto-char (match-beginning 0))
2811 (point-max-marker)))
2813 (while (< (point) end
)
2814 (if (todo-marked-item-p)
2816 (todo-forward-item)))
2817 (setq todo-categories-with-marks
2818 (assq-delete-all cat1 todo-categories-with-marks
)))
2819 (if ov
(delete-overlay ov
))
2820 (todo-remove-item))))
2821 (when todo
(todo-update-count 'todo
(- todo
) cat1
))
2822 (when diary
(todo-update-count 'diary
(- diary
) cat1
))
2823 (when done
(todo-update-count 'done
(- done
) cat1
))
2824 (todo-update-categories-sexp))
2825 (set-window-buffer (selected-window)
2826 (set-buffer (find-file-noselect file2
'nowarn
)))
2827 (setq todo-category-number
(todo-category-number cat2
))
2828 (let ((todo-show-with-done (> done
0)))
2829 (todo-category-select))
2831 ;; If item is moved to end of (just first?) category, make
2832 ;; sure the items above it are displayed in the window.
2834 ;; User quit before setting priority of todo item(s), so
2835 ;; return to starting category.
2837 (set-window-buffer (selected-window)
2838 (set-buffer (find-file-noselect file1
'nowarn
)))
2839 (todo-category-number cat1
)
2840 (todo-category-select)
2841 (goto-char omark
))))))))
2843 (defun todo-item-done (&optional arg
)
2844 "Tag a todo item in this category as done and relocate it.
2846 With prefix argument ARG prompt for a comment and append it to
2847 the done item; this is only possible if there are no marked
2848 items. If there are marked items, tag all of these with
2849 `todo-done-string' plus the current date and, if
2850 `todo-always-add-time-string' is non-nil, the current time;
2851 otherwise, just tag the item at point. Items tagged as done are
2852 relocated to the category's (by default hidden) done section. If
2853 done items are visible on invoking this command, they remain
2856 (let* ((cat (todo-current-category))
2857 (marked (assoc cat todo-categories-with-marks
)))
2858 (when marked
(todo--user-error-if-marked-done-item))
2859 (unless (and (not marked
)
2860 (or (todo-done-item-p)
2861 ;; Point is between todo and done items.
2863 (let* ((date-string (calendar-date-string (calendar-current-date) t t
))
2864 (time-string (if todo-always-add-time-string
2865 (concat " " (substring (current-time-string)
2868 (done-prefix (concat "[" todo-done-string date-string time-string
2870 (comment (and arg
(read-string "Enter a comment: ")))
2873 (show-done (save-excursion
2874 (goto-char (point-min))
2875 (re-search-forward todo-done-string-start nil t
)))
2876 (buffer-read-only nil
)
2877 header item done-items
2879 ;; Don't add empty comment to done item.
2880 (setq comment
(unless (zerop (length comment
))
2881 (concat " [" todo-comment-string
": " comment
"]")))
2882 (and marked
(goto-char (point-min)))
2883 (setq header
(todo-get-overlay 'header
))
2885 ;; Stop looping when we hit the empty line below the last
2886 ;; todo item (this is eobp if only done items are hidden).
2887 (while (not (looking-at "^$"))
2888 (if (or (not marked
) (and marked
(todo-marked-item-p)))
2890 (setq item
(todo-item-string))
2891 (push (concat done-prefix item comment
) done-items
)
2892 (setq item-count
(1+ item-count
))
2893 (when (todo-diary-item-p)
2894 (setq diary-count
(1+ diary-count
)))
2896 (unless marked
(throw 'done nil
)))
2897 (todo-forward-item))))
2898 (setq done-items
(nreverse done-items
))
2900 (setq todo-categories-with-marks
2901 (assq-delete-all cat todo-categories-with-marks
)))
2905 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2907 (when show-done
(setq opoint
(point)))
2909 (insert (pop done-items
) "\n")
2910 (when header
(let ((copy (copy-overlay header
)))
2912 (concat todo-item-start
2913 "\\( " diary-time-regexp
"\\)?"
2914 (regexp-quote todo-nondiary-end
) "? ")
2916 (move-overlay copy
(match-beginning 0) (match-end 0)))
2919 (todo-update-count 'todo
(- item-count
))
2920 (todo-update-count 'done item-count
)
2921 (todo-update-count 'diary
(- diary-count
))
2922 (todo-update-categories-sexp)
2923 (let ((todo-show-with-done show-done
))
2924 (todo-category-select)
2925 ;; When done items are visible, put point at the top of the
2926 ;; done items section. When done items are hidden, restore
2927 ;; point to its location prior to invoking this command.
2928 (when opoint
(goto-char opoint
)))))))
2930 (defun todo-item-undone ()
2931 "Restore at least one done item to this category's todo section.
2932 Prompt for the new priority. If there are marked items, undo all
2933 of these, giving the first undone item the new priority and the
2934 rest following directly in sequence; otherwise, undo just the
2937 If the done item has a comment, ask whether to omit the comment
2938 from the restored item. With multiple marked done items with
2939 comments, only ask once, and if affirmed, omit subsequent
2940 comments without asking."
2942 (let* ((cat (todo-current-category))
2943 (marked (assoc cat todo-categories-with-marks
))
2944 (pl (if (and marked
(> (cdr marked
) 1)) "s" "")))
2945 (when (or marked
(todo-done-item-p))
2946 (let ((buffer-read-only)
2948 (omark (point-marker))
2952 start end item ov npoint undone
)
2953 (and marked
(goto-char (point-min)))
2956 (when (or (not marked
) (and marked
(todo-marked-item-p)))
2957 (if (not (todo-done-item-p))
2960 (user-error "Only done items can be undone"))
2963 (setq ov
(make-overlay (save-excursion (todo-item-start))
2964 (save-excursion (todo-item-end))))
2965 (overlay-put ov
'face
'todo-search
))
2966 ;; Find the end of the date string added upon tagging item as
2968 (setq start
(search-forward "] "))
2969 (setq item-count
(1+ item-count
))
2970 (unless (looking-at (regexp-quote todo-nondiary-start
))
2971 (setq diary-count
(1+ diary-count
)))
2972 (setq end
(save-excursion (todo-item-end)))
2973 ;; Ask (once) whether to omit done item's comment. If
2974 ;; affirmed, omit subsequent comments without asking.
2975 (when (re-search-forward
2976 (concat " \\[" (regexp-quote todo-comment-string
)
2980 (if (eq first
'first
)
2982 (if (eq todo-undo-item-omit-comment
'ask
)
2983 (when (todo-y-or-n-p
2984 (concat "Omit comment" pl
2985 " from restored item"
2988 (when todo-undo-item-omit-comment
'omit
)))
2990 (when (and (eq first
'first
) ov
) (delete-overlay ov
)))
2991 (when (eq first
'omit
)
2992 (setq end
(match-beginning 0))))
2993 (setq item
(concat item
2994 (buffer-substring-no-properties start end
)
2995 (when marked
"\n")))
2996 (unless marked
(throw 'done nil
))))
2997 (todo-forward-item)))
3000 ;; Chop off last newline of multiple items string, since
3001 ;; it will be reinserted on setting priority.
3002 (and marked
(setq item
(substring item
0 -
1)))
3003 (todo-set-item-priority item cat t
)
3004 (setq npoint
(point))
3006 (when ov
(delete-overlay ov
))
3013 (concat "^" (regexp-quote todo-category-done
)) nil t
)
3015 (if (todo-marked-item-p)
3017 (todo-forward-item)))
3018 (setq todo-categories-with-marks
3019 (assq-delete-all cat todo-categories-with-marks
)))
3022 (todo-update-count 'todo item-count
)
3023 (todo-update-count 'done
(- item-count
))
3024 (when diary-count
(todo-update-count 'diary diary-count
))
3025 (todo-update-categories-sexp)
3026 (let ((todo-show-with-done (> (todo-get-count 'done
) 0)))
3027 (todo-category-select))
3028 ;; Put cursor on undone item.
3029 (goto-char npoint
)))
3030 (set-marker omark nil
)))))
3032 ;; -----------------------------------------------------------------------------
3033 ;;; Done item archives
3034 ;; -----------------------------------------------------------------------------
3036 (defun todo-find-archive (&optional ask
)
3037 "Visit the archive of the current todo category, if it exists.
3038 If the category has no archived items, prompt to visit the
3039 archive anyway. If there is no archive for this file or with
3040 non-nil argument ASK, prompt to visit another archive.
3042 The buffer showing the archive is in Todo Archive mode. The
3043 first visit in a session displays the first category in the
3044 archive, subsequent visits return to the last category
3047 (if (null (funcall todo-files-function t
))
3048 (message "There are no archive files")
3049 (let* ((cat (todo-current-category))
3050 (count (todo-get-count 'archived cat
))
3051 (archive (concat (file-name-sans-extension todo-current-todo-file
)
3053 (place (cond (ask 'other-archive
)
3054 ((file-exists-p archive
) 'this-archive
)
3055 (t (when (todo-y-or-n-p
3056 (concat "This file has no archive; "
3057 "visit another archive? "))
3059 (when (eq place
'other-archive
)
3060 (setq archive
(todo-read-file-name "Choose a todo archive: " t t
)))
3061 (when (and (eq place
'this-archive
) (zerop count
))
3062 (setq place
(when (todo-y-or-n-p
3063 (concat "This category has no archived items;"
3064 " visit archive anyway? "))
3067 (set-window-buffer (selected-window)
3068 (set-buffer (find-file-noselect archive
)))
3069 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
3070 (if (member place
'(other-archive other-cat
))
3071 (setq todo-category-number
1)
3072 (todo-category-number cat
))
3073 (todo-category-select)))))
3075 (defun todo-choose-archive ()
3076 "Choose an archive and visit it."
3078 (todo-find-archive t
))
3080 (defun todo-archive-done-item (&optional all
)
3081 "Archive at least one done item in this category.
3083 With prefix argument ALL, prompt whether to archive all done
3084 items in this category and on confirmation archive them.
3085 Otherwise, if there are marked done items (and no marked todo
3086 items), archive all of these; otherwise, archive the done item at
3089 If the archive of this file does not exist, it is created. If
3090 this category does not exist in the archive, it is created."
3092 (when (eq major-mode
'todo-mode
)
3093 (if (and all
(zerop (todo-get-count 'done
)))
3094 (message "No done items in this category")
3096 (let* ((cat (todo-current-category))
3097 (tbuf (current-buffer))
3098 (marked (assoc cat todo-categories-with-marks
))
3099 (afile (concat (file-name-sans-extension
3100 todo-current-todo-file
) ".toda"))
3101 (archive (find-file-noselect afile t
))
3102 (item (and (not marked
) (todo-done-item-p)
3103 (concat (todo-item-string) "\n")))
3105 (opoint (unless (todo-done-item-p) (point)))
3106 marked-items beg end all-done
3110 (if (todo-y-or-n-p "Archive all done items in this category? ")
3113 (goto-char (point-min))
3116 (re-search-forward todo-done-string-start
3118 (match-beginning 0))
3119 end
(if (re-search-forward
3121 (regexp-quote todo-category-beg
))
3125 all-done
(buffer-substring-no-properties beg end
)
3126 count
(todo-get-count 'done
))
3127 ;; Restore starting point, unless it was on a done
3128 ;; item, since they will all be deleted.
3129 (when opoint
(goto-char opoint
))))
3133 (goto-char (point-min))
3135 (when (todo-marked-item-p)
3136 (if (not (todo-done-item-p))
3137 (throw 'end
(message "Only done items can be archived"))
3139 (concat marked-items
(todo-item-string) "\n"))
3140 (setq count
(1+ count
))))
3141 (todo-forward-item)))))
3142 (if (not (or marked all item
))
3143 (throw 'end
(message "Only done items can be archived"))
3144 (with-current-buffer archive
3145 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
3146 (let ((headers-hidden todo--item-headers-hidden
)
3148 (if headers-hidden
(todo-toggle-item-header))
3150 (goto-char (point-min))
3151 (if (and (re-search-forward
3152 (concat "^" (regexp-quote
3153 (concat todo-category-beg cat
)) "$")
3155 (re-search-forward (regexp-quote todo-category-done
)
3157 ;; Start of done items section in existing category.
3159 (todo-add-category nil cat
)
3160 ;; Start of done items section in new category.
3161 (goto-char (point-max)))
3162 (insert (cond (marked marked-items
)
3165 (todo-update-count 'done
(if (or marked all
) count
1) cat
)
3166 (todo-update-categories-sexp)
3167 ;; If archive is new, save to file now (with
3168 ;; write-region to avoid prompt for file to save to)
3169 ;; to update todo-archives, and set the mode for
3170 ;; visiting the archive below.
3171 (unless (nth 7 (file-attributes afile
))
3172 (write-region nil nil afile t t
)
3173 (setq todo-archives
(funcall todo-files-function t
))
3174 (todo-archive-mode))
3175 (if headers-hidden
(todo-toggle-item-header))))
3176 (with-current-buffer tbuf
3181 ;; Make sure done items are accessible.
3183 (remove-overlays beg end
)
3184 (delete-region beg end
)
3185 (todo-update-count 'done
(- count
))
3186 (todo-update-count 'archived count
))))
3188 ;; If we're archiving all done items, can't
3189 ;; first archive item point was on, since
3190 ;; that will short-circuit the rest.
3191 (and item
(not all
)))
3192 (and marked
(goto-char (point-min)))
3195 (if (or (and marked
(todo-marked-item-p)) item
)
3198 (todo-update-count 'done -
1)
3199 (todo-update-count 'archived
1)
3200 ;; Don't leave point below last item.
3201 (and (or marked item
) (bolp) (eolp)
3202 (< (point-min) (point-max))
3203 (todo-backward-item))
3205 (throw 'done
(setq item nil
))))
3206 (todo-forward-item))))))
3208 (setq todo-categories-with-marks
3209 (assq-delete-all cat todo-categories-with-marks
)))
3210 (todo-update-categories-sexp)
3211 (todo-prefix-overlays)))
3213 (todo-category-number cat
)
3214 (todo-category-select)
3215 (split-window-below)
3216 (set-window-buffer (selected-window) tbuf
)
3217 ;; Make todo file current to select category.
3218 (find-file (buffer-file-name tbuf
))
3219 ;; Make sure done item separator is hidden (if done items
3220 ;; were initially visible).
3221 (let (todo-show-with-done) (todo-category-select)))))))
3223 (defun todo-unarchive-items ()
3224 "Unarchive at least one item in this archive category.
3225 If there are marked items, unarchive all of these; otherwise,
3226 unarchive the item at point.
3228 Unarchived items are restored as done items to the corresponding
3229 category in the todo file, inserted at the top of done items
3230 section. If all items in the archive category have been
3231 restored, the category is deleted from the archive. If this was
3232 the only category in the archive, the archive file is deleted."
3234 (when (eq major-mode
'todo-archive-mode
)
3235 (let* ((cat (todo-current-category))
3236 (tbuf (find-file-noselect
3237 (concat (file-name-sans-extension todo-current-todo-file
)
3240 (marked (assoc cat todo-categories-with-marks
))
3241 (item (concat (todo-item-string) "\n"))
3247 (goto-char (point-min))
3249 (when (todo-marked-item-p)
3250 (setq marked-items
(concat marked-items
(todo-item-string) "\n"))
3251 (setq marked-count
(1+ marked-count
)))
3252 (todo-forward-item))))
3253 ;; Restore items to top of category's done section and update counts.
3254 (with-current-buffer tbuf
3255 (let ((headers-hidden todo--item-headers-hidden
)
3256 buffer-read-only newcat
)
3257 (if headers-hidden
(todo-toggle-item-header))
3259 (goto-char (point-min))
3260 ;; Find the corresponding todo category, or if there isn't
3262 (unless (re-search-forward
3263 (concat "^" (regexp-quote (concat todo-category-beg cat
))
3266 (todo-add-category nil cat
)
3268 ;; Go to top of category's done section.
3270 (concat "^" (regexp-quote todo-category-done
)) nil t
)
3273 (insert marked-items
)
3274 (todo-update-count 'done marked-count cat
)
3275 (unless newcat
; Newly added category has no archive.
3276 (todo-update-count 'archived
(- marked-count
) cat
)))
3279 (todo-update-count 'done
1 cat
)
3280 (unless newcat
; Newly added category has no archive.
3281 (todo-update-count 'archived -
1 cat
))))
3282 (if headers-hidden
(todo-toggle-item-header))
3283 (todo-update-categories-sexp)))
3284 ;; Delete restored items from archive.
3287 (goto-char (point-min)))
3290 (if (or (todo-marked-item-p) item
)
3294 (throw 'done
(setq item nil
))))
3295 (todo-forward-item))))
3296 (todo-update-count 'done
(if marked
(- marked-count
) -
1) cat
)
3297 ;; If we unarchived the last item in category, then if that was
3298 ;; the only category, delete the whole file, otherwise, just
3299 ;; delete the category.
3300 (when (= 0 (todo-get-count 'done
))
3301 (if (= 1 (length todo-categories
))
3303 (delete-file todo-current-todo-file
)
3304 ;; Kill the archive buffer silently.
3305 (set-buffer-modified-p nil
)
3308 (let ((beg (re-search-backward
3309 (concat "^" (regexp-quote todo-category-beg
) cat
"$")
3311 (end (if (re-search-forward
3312 (concat "^" (regexp-quote todo-category-beg
))
3316 (remove-overlays beg end
)
3317 (delete-region beg end
)
3318 (setq todo-categories
(delete (assoc cat todo-categories
)
3319 todo-categories
)))))
3320 (todo-update-categories-sexp)
3321 ;; Visit category in todo file and show restored done items.
3322 (let ((tfile (buffer-file-name tbuf
))
3323 (todo-show-with-done t
))
3324 (set-window-buffer (selected-window)
3325 (set-buffer (find-file-noselect tfile
)))
3326 (todo-category-number cat
)
3327 (todo-category-select)
3328 ;; Selecting the category leaves point at the end of the done
3329 ;; items separator string, so move it to the (first) restored
3332 (message "Items unarchived.")))))
3334 (defun todo-jump-to-archive-category (&optional file
)
3335 "Prompt for a category in a todo archive and jump to it.
3336 With prefix argument FILE, prompt for an archive and choose (with
3337 TAB completion) a category in it to jump to; otherwise, choose
3338 and jump to any category in the current archive."
3340 (todo-jump-to-category file
'archive
))
3342 ;; -----------------------------------------------------------------------------
3343 ;;; Displaying and sorting tables of categories
3344 ;; -----------------------------------------------------------------------------
3346 (defcustom todo-categories-category-label
"Category"
3347 "Category button label in Todo Categories mode."
3349 :group
'todo-categories
)
3351 (defcustom todo-categories-todo-label
"Todo"
3352 "Todo button label in Todo Categories mode."
3354 :group
'todo-categories
)
3356 (defcustom todo-categories-diary-label
"Diary"
3357 "Diary button label in Todo Categories mode."
3359 :group
'todo-categories
)
3361 (defcustom todo-categories-done-label
"Done"
3362 "Done button label in Todo Categories mode."
3364 :group
'todo-categories
)
3366 (defcustom todo-categories-archived-label
"Archived"
3367 "Archived button label in Todo Categories mode."
3369 :group
'todo-categories
)
3371 (defcustom todo-categories-totals-label
"Totals"
3372 "String to label total item counts in Todo Categories mode."
3374 :group
'todo-categories
)
3376 (defcustom todo-categories-number-separator
" | "
3377 "String between number and category in Todo Categories mode.
3378 This separates the number from the category name in the default
3379 categories display according to priority."
3381 :group
'todo-categories
)
3383 (defcustom todo-categories-align
'center
3384 "Alignment of category names in Todo Categories mode."
3385 :type
'(radio (const left
) (const center
) (const right
))
3386 :group
'todo-categories
)
3388 (defun todo-show-categories-table ()
3389 "Display a table of the current file's categories and item counts.
3391 In the initial display the lines of the table are numbered,
3392 indicating the current order of the categories when sequentially
3393 navigating through the todo file with `\\[todo-forward-category]'
3394 and `\\[todo-backward-category]'. You can reorder the lines, and
3395 hence the category sequence, by typing `\\[todo-raise-category]'
3396 or `\\[todo-lower-category]' to raise or lower the category at
3397 point, or by typing `\\[todo-set-category-number]' and entering a
3398 number at the prompt or by typing `\\[todo-set-category-number]'
3399 with a numeric prefix. If you save the todo file after
3400 reordering the categories, the new order persists in subsequent
3403 The labels above the category names and item counts are buttons,
3404 and clicking these changes the display: sorted by category name
3405 or by the respective item counts (alternately descending or
3406 ascending). In these displays the categories are not numbered
3407 and `\\[todo-set-category-number]', `\\[todo-raise-category]' and
3408 `\\[todo-lower-category]' are disabled. (Programmatically, the
3409 sorting is triggered by passing a non-nil SORTKEY argument.)
3411 In addition, the lines with the category names and item counts
3412 are buttonized, and pressing one of these button jumps to the
3413 category in Todo mode (or Todo Archive mode, for categories
3414 containing only archived items, provided user option
3415 `todo-skip-archived-categories' is non-nil. These categories
3416 are shown in `todo-archived-only' face."
3418 (todo-display-categories)
3420 (todo-update-categories-display sortkey
)))
3422 (defun todo-next-button (n)
3423 "Move point to the Nth next button in the table of categories."
3425 (forward-button n
'wrap
'display-message
)
3426 (and (bolp) (button-at (point))
3427 ;; Align with beginning of category label.
3428 (forward-char (+ 4 (length todo-categories-number-separator
)))))
3430 (defun todo-previous-button (n)
3431 "Move point to the Nth previous button in the table of categories."
3433 (backward-button n
'wrap
'display-message
)
3434 (and (bolp) (button-at (point))
3435 ;; Align with beginning of category label.
3436 (forward-char (+ 4 (length todo-categories-number-separator
)))))
3438 (defun todo-set-category-number (&optional arg
)
3439 "Change number of category at point in the table of categories.
3441 With ARG nil, prompt for the new number. Alternatively, the
3442 enter the new number with numerical prefix ARG. Otherwise, if
3443 ARG is either of the symbols `raise' or `lower', raise or lower
3444 the category line in the table by one, respectively, thereby
3445 decreasing or increasing its number."
3447 (let ((curnum (save-excursion
3448 ;; Get the number representing the priority of the category
3449 ;; on the current line.
3450 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
3451 (when curnum
; Do nothing if we're not on a category line.
3452 (let* ((maxnum (length todo-categories
))
3453 (prompt (format "Set category priority (1-%d): " maxnum
))
3454 (col (current-column))
3455 (buffer-read-only nil
)
3456 (priority (cond ((and (eq arg
'raise
) (> curnum
1))
3458 ((and (eq arg
'lower
) (< curnum maxnum
))
3461 (while (not priority
)
3462 (setq candidate
(or arg
(read-number prompt
)))
3465 (cond ((or (< candidate
1) (> candidate maxnum
))
3466 (format "Priority must be an integer between 1 and %d: "
3468 ((= candidate curnum
)
3469 "Choose a different priority than the current one: ")))
3470 (unless prompt
(setq priority candidate
)))
3471 (let* ((lower (< curnum priority
)) ; Priority is being lowered.
3472 (head (butlast todo-categories
3473 (funcall (if lower
#'identity
#'1+)
3474 (- maxnum priority
))))
3475 (tail (nthcdr (funcall (if lower
#'identity
#'1-
) priority
)
3477 ;; Category's name and items counts list.
3478 (catcons (nth (1- curnum
) todo-categories
))
3479 (todo-categories (nconc head
(list catcons
) tail
))
3481 (when lower
(setq todo-categories
(nreverse todo-categories
)))
3482 (setq todo-categories
(delete-dups todo-categories
))
3483 (when lower
(setq todo-categories
(nreverse todo-categories
)))
3484 (setq newcats todo-categories
)
3486 (with-current-buffer (find-buffer-visiting todo-current-todo-file
)
3487 (setq todo-categories newcats
)
3488 (todo-update-categories-sexp))
3489 (todo-show-categories-table)
3490 (forward-line (1+ priority
))
3491 (forward-char col
))))))
3493 (defun todo-raise-category ()
3494 "Raise priority of category at point in the table of categories."
3496 (todo-set-category-number 'raise
))
3498 (defun todo-lower-category ()
3499 "Lower priority of category at point in the table of categories."
3501 (todo-set-category-number 'lower
))
3503 (defun todo-sort-categories-alphabetically-or-numerically ()
3504 "Sort table of categories alphabetically or numerically."
3507 (goto-char (point-min))
3509 (if (member 'alpha todo-descending-counts
)
3511 (todo-update-categories-display nil
)
3512 (setq todo-descending-counts
3513 (delete 'alpha todo-descending-counts
)))
3514 (todo-update-categories-display 'alpha
))))
3516 (defun todo-sort-categories-by-todo ()
3517 "Sort table of categories by number of todo items."
3520 (goto-char (point-min))
3522 (todo-update-categories-display 'todo
)))
3524 (defun todo-sort-categories-by-diary ()
3525 "Sort table of categories by number of diary items."
3528 (goto-char (point-min))
3530 (todo-update-categories-display 'diary
)))
3532 (defun todo-sort-categories-by-done ()
3533 "Sort table of categories by number of non-archived done items."
3536 (goto-char (point-min))
3538 (todo-update-categories-display 'done
)))
3540 (defun todo-sort-categories-by-archived ()
3541 "Sort table of categories by number of archived items."
3544 (goto-char (point-min))
3546 (todo-update-categories-display 'archived
)))
3548 (defvar todo-categories-buffer
"*Todo Categories*"
3549 "Name of buffer in Todo Categories mode.")
3551 (defun todo-longest-category-name-length (categories)
3552 "Return the length of the longest name in list CATEGORIES."
3554 (dolist (c categories longest
)
3555 (setq longest
(max longest
(length c
))))))
3557 (defun todo-adjusted-category-label-length ()
3558 "Return adjusted length of category label button.
3559 The adjustment ensures proper tabular alignment in Todo
3561 (let* ((categories (mapcar #'car todo-categories
))
3562 (longest (todo-longest-category-name-length categories
))
3563 (catlablen (length todo-categories-category-label
))
3564 (lc-diff (- longest catlablen
)))
3565 (if (and (natnump lc-diff
) (cl-oddp lc-diff
))
3567 (max longest catlablen
))))
3569 (defun todo-padded-string (str)
3570 "Return category name or label string STR padded with spaces.
3571 The placement of the padding is determined by the value of user
3572 option `todo-categories-align'."
3573 (let* ((len (todo-adjusted-category-label-length))
3574 (strlen (length str
))
3575 (strlen-odd (eq (logand strlen
1) 1))
3576 (padding (max 0 (/ (- len strlen
) 2)))
3577 (padding-left (cond ((eq todo-categories-align
'left
) 0)
3578 ((eq todo-categories-align
'center
) padding
)
3579 ((eq todo-categories-align
'right
)
3580 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
3581 (padding-right (cond ((eq todo-categories-align
'left
)
3582 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
3583 ((eq todo-categories-align
'center
)
3584 (if strlen-odd
(1+ padding
) padding
))
3585 ((eq todo-categories-align
'right
) 0))))
3586 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
3588 (defvar todo-descending-counts nil
3589 "List of keys for category counts sorted in descending order.")
3591 (defun todo-sort (list &optional key
)
3592 "Return a copy of LIST, possibly sorted according to KEY."
3593 (let* ((l (copy-sequence list
))
3594 (fn (if (eq key
'alpha
)
3595 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
3596 (lambda (x) (todo-get-count key x
))))
3597 ;; Keep track of whether the last sort by key was descending or
3599 (descending (member key todo-descending-counts
))
3600 (cmp (if (eq key
'alpha
)
3602 (if descending
'< '>)))
3603 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
3604 (t2 (funcall fn
(car s2
))))
3605 (funcall cmp t1 t2
)))))
3607 (setq l
(sort l pred
))
3608 ;; Switch between descending and ascending sort order.
3610 (setq todo-descending-counts
3611 (delete key todo-descending-counts
))
3612 (push key todo-descending-counts
)))
3615 (defun todo-display-sorted (type)
3616 "Keep point on the TYPE count sorting button just clicked."
3617 (let ((opoint (point)))
3618 (todo-update-categories-display type
)
3619 (goto-char opoint
)))
3621 (defun todo-label-to-key (label)
3622 "Return symbol for sort key associated with LABEL."
3624 (cond ((string= label todo-categories-category-label
)
3626 ((string= label todo-categories-todo-label
)
3628 ((string= label todo-categories-diary-label
)
3630 ((string= label todo-categories-done-label
)
3632 ((string= label todo-categories-archived-label
)
3633 (setq key
'archived
)))
3636 (defun todo-insert-sort-button (label)
3637 "Insert button for displaying categories sorted by item counts.
3638 LABEL determines which type of count is sorted."
3639 (let* ((str (if (string= label todo-categories-category-label
)
3640 (todo-padded-string label
)
3643 (end (+ beg
(length str
)))
3645 (insert-button str
'face nil
3648 (let ((key (todo-label-to-key label
)))
3649 (if (and (member key todo-descending-counts
)
3652 ;; If display is alphabetical, switch back to
3653 ;; category priority order.
3654 (todo-display-sorted nil
)
3655 (setq todo-descending-counts
3656 (delete key todo-descending-counts
)))
3657 (todo-display-sorted key
)))))
3658 (setq ov
(make-overlay beg end
))
3659 (overlay-put ov
'face
'todo-button
)))
3661 (defun todo-total-item-counts ()
3662 "Return a list of total item counts for the current file."
3663 (mapcar (lambda (i) (apply #'+ (mapcar (lambda (x) (aref (cdr x
) i
))
3667 (defvar todo-categories-category-number
0
3668 "Variable for numbering categories in Todo Categories mode.")
3670 (defun todo-insert-category-line (cat &optional nonum
)
3671 "Insert button with category CAT's name and item counts.
3672 With non-nil argument NONUM show only these; otherwise, insert a
3673 number in front of the button indicating the category's priority.
3674 The number and the category name are separated by the string
3675 which is the value of the user option
3676 `todo-categories-number-separator'."
3677 (let ((archive (member todo-current-todo-file todo-archives
))
3678 (num todo-categories-category-number
)
3679 (str (todo-padded-string cat
))
3681 (setq num
(1+ num
) todo-categories-category-number num
)
3684 (make-string (+ 4 (length todo-categories-number-separator
))
3686 (format " %3d%s" num todo-categories-number-separator
))
3688 (mapconcat (lambda (elt)
3690 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
3691 (format "%3d" (todo-get-count (cdr elt
) cat
)) ; count
3692 ;; Add an extra space if label length is odd.
3693 (when (cl-oddp (length (car elt
))) " ")))
3695 (list (cons todo-categories-done-label
'done
))
3696 (list (cons todo-categories-todo-label
'todo
)
3697 (cons todo-categories-diary-label
'diary
)
3698 (cons todo-categories-done-label
'done
)
3699 (cons todo-categories-archived-label
3702 " ") ; Make highlighting on last column look better.
3703 'face
(if (and todo-skip-archived-categories
3704 (zerop (todo-get-count 'todo cat
))
3705 (zerop (todo-get-count 'done cat
))
3706 (not (zerop (todo-get-count 'archived cat
))))
3709 'action
(lambda (_button)
3710 (let ((buf (current-buffer)))
3711 (todo-jump-to-category nil cat
)
3712 (kill-buffer buf
))))
3713 ;; Highlight the sorted count column.
3714 (let* ((beg (+ opoint
7 (length str
)))
3716 (cond ((eq nonum
'todo
)
3717 (setq beg
(+ beg
1 (/ (length todo-categories-todo-label
) 2))))
3719 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3720 2 (/ (length todo-categories-diary-label
) 2))))
3722 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3723 2 (length todo-categories-diary-label
)
3724 2 (/ (length todo-categories-done-label
) 2))))
3725 ((eq nonum
'archived
)
3726 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3727 2 (length todo-categories-diary-label
)
3728 2 (length todo-categories-done-label
)
3729 2 (/ (length todo-categories-archived-label
) 2)))))
3730 (unless (= beg
(+ opoint
7 (length str
))) ; Don't highlight categories.
3731 (setq end
(+ beg
4))
3732 (setq ovl
(make-overlay beg end
))
3733 (overlay-put ovl
'face
'todo-sorted-column
)))
3736 (defun todo-display-categories ()
3737 "Prepare buffer for displaying table of categories and item counts."
3738 (unless (eq major-mode
'todo-categories-mode
)
3739 (setq todo-global-current-todo-file
3740 (or todo-current-todo-file
3741 (todo-absolute-file-name todo-default-todo-file
)))
3742 (set-window-buffer (selected-window)
3743 (set-buffer (get-buffer-create todo-categories-buffer
)))
3744 (kill-all-local-variables)
3745 (todo-categories-mode)
3746 (let ((archive (member todo-current-todo-file todo-archives
))
3749 (insert (format (concat "Category counts for todo "
3750 (if archive
"archive" "file")
3752 (todo-short-file-name todo-current-todo-file
)))
3754 ;; Make space for the column of category numbers.
3755 (insert (make-string (+ 4 (length todo-categories-number-separator
)) 32))
3756 ;; Add the category and item count buttons (if this is the list of
3757 ;; categories in an archive, show only done item counts).
3758 (todo-insert-sort-button todo-categories-category-label
)
3761 (insert (make-string 3 32))
3762 (todo-insert-sort-button todo-categories-done-label
))
3763 (insert (make-string 3 32))
3764 (todo-insert-sort-button todo-categories-todo-label
)
3765 (insert (make-string 2 32))
3766 (todo-insert-sort-button todo-categories-diary-label
)
3767 (insert (make-string 2 32))
3768 (todo-insert-sort-button todo-categories-done-label
)
3769 (insert (make-string 2 32))
3770 (todo-insert-sort-button todo-categories-archived-label
))
3773 (defun todo-update-categories-display (sortkey)
3774 "Populate table of categories and sort by SORTKEY."
3775 (let* ((cats0 todo-categories
)
3776 (cats (todo-sort cats0 sortkey
))
3777 (archive (member todo-current-todo-file todo-archives
))
3778 (todo-categories-category-number 0)
3779 ;; Find start of Category button if we just entered Todo Categories
3781 (pt (if (eq (point) (point-max))
3784 (goto-char (next-single-char-property-change
3785 (point) 'face nil
(line-end-position))))))
3788 (delete-region (point) (point-max))
3789 ;; Fill in the table with buttonized lines, each showing a category and
3792 (todo-insert-category-line (car cat
) sortkey
))
3794 ;; Add a line showing item count totals.
3795 (insert (make-string (+ 4 (length todo-categories-number-separator
)) 32)
3796 (todo-padded-string todo-categories-totals-label
)
3800 (make-string (1+ (/ (length (car elt
)) 2)) 32)
3801 (format "%3d" (nth (cdr elt
) (todo-total-item-counts)))
3802 ;; Add an extra space if label length is odd.
3803 (when (cl-oddp (length (car elt
))) " ")))
3805 (list (cons todo-categories-done-label
2))
3806 (list (cons todo-categories-todo-label
0)
3807 (cons todo-categories-diary-label
1)
3808 (cons todo-categories-done-label
2)
3809 (cons todo-categories-archived-label
3)))
3811 ;; Put cursor on Category button initially.
3812 (if pt
(goto-char pt
))
3813 (setq buffer-read-only t
)))
3815 ;; -----------------------------------------------------------------------------
3816 ;;; Searching and item filtering
3817 ;; -----------------------------------------------------------------------------
3819 (defun todo-search ()
3820 "Search for a regular expression in this todo file.
3821 The search runs through the whole file and encompasses all and
3822 only todo and done items; it excludes category names. Multiple
3823 matches are shown sequentially, highlighted in `todo-search'
3826 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3828 matches match cat in-done ov mlen msg
)
3830 (goto-char (point-min))
3832 (setq match
(re-search-forward regex nil t
))
3833 (goto-char (line-beginning-position))
3834 (unless (or (equal (point) 1)
3835 (looking-at (concat "^" (regexp-quote todo-category-beg
))))
3836 (if match
(push match matches
)))
3838 (setq matches
(reverse matches
))
3842 (setq match
(pop matches
))
3845 (when (looking-at todo-done-string-start
)
3847 (re-search-backward (concat "^" (regexp-quote todo-category-beg
)
3850 (setq cat
(match-string-no-properties 1))
3851 (todo-category-number cat
)
3852 (todo-category-select)
3854 (unless todo-show-with-done
(todo-toggle-view-done-items)))
3856 (setq ov
(make-overlay (- (point) (length regex
)) (point)))
3857 (overlay-put ov
'face
'todo-search
)
3859 (setq mlen
(length matches
))
3862 (format "There are %d more matches; go to next match? "
3864 "There is one more match; go to it? "))
3866 (throw 'stop
(setq msg
(if (> mlen
1)
3867 (format "There are %d more matches."
3869 "There is one more match."))))))
3870 (setq msg
"There are no more matches."))
3871 (todo-category-select)
3873 (message "No match for \"%s\"" regex
))
3875 (if (todo-y-or-n-p (concat msg
"\nUnhighlight matches? "))
3876 (todo-clear-matches)
3877 (message "You can unhighlight the matches later by typing %s"
3878 (key-description (car (where-is-internal
3879 'todo-clear-matches
))))))))
3881 (defun todo-clear-matches ()
3882 "Remove highlighting on matches found by todo-search."
3884 (remove-overlays 1 (1+ (buffer-size)) 'face
'todo-search
))
3886 (defcustom todo-top-priorities-overrides nil
3887 "List of rules specifying number of top priority items to show.
3888 These rules override `todo-top-priorities' on invocations of
3889 `\\[todo-filter-top-priorities]' and
3890 `\\[todo-filter-top-priorities-multifile]'. Each rule is a list
3891 of the form (FILE NUM ALIST), where FILE is a member of
3892 `todo-files', NUM is a number specifying the default number of
3893 top priority items for each category in that file, and ALIST,
3894 when non-nil, consists of conses of a category name in FILE and a
3895 number specifying the default number of top priority items in
3896 that category, which overrides NUM.
3898 This variable should be set interactively by
3899 `\\[todo-set-top-priorities-in-file]' or
3900 `\\[todo-set-top-priorities-in-category]'."
3902 :group
'todo-filtered
)
3904 (defcustom todo-top-priorities
1
3905 "Default number of top priorities shown by `todo-filter-top-priorities'."
3907 :group
'todo-filtered
)
3909 (defcustom todo-filter-files nil
3910 "List of default files for multifile item filtering."
3911 :type
`(set ,@(todo--files-type-list))
3912 :group
'todo-filtered
)
3914 (defcustom todo-filter-done-items nil
3915 "Non-nil to include done items when processing regexp filters.
3916 Done items from corresponding archive files are also included."
3918 :group
'todo-filtered
)
3920 (defun todo-set-top-priorities-in-file ()
3921 "Set number of top priorities for this file.
3922 See `todo-set-top-priorities' for more details."
3924 (todo-set-top-priorities))
3926 (defun todo-set-top-priorities-in-category ()
3927 "Set number of top priorities for this category.
3928 See `todo-set-top-priorities' for more details."
3930 (todo-set-top-priorities t
))
3932 (defun todo-filter-top-priorities (&optional arg
)
3933 "Display a list of top priority items from different categories.
3934 The categories can be any of those in the current todo file.
3936 With numerical prefix ARG show at most ARG top priority items
3937 from each category. With `C-u' as prefix argument show the
3938 numbers of top priority items specified by category in
3939 `todo-top-priorities-overrides', if this has an entry for the file(s);
3940 otherwise show `todo-top-priorities' items per category in the
3941 file(s). With no prefix argument, if a top priorities file for
3942 the current todo file has previously been saved (see
3943 `todo-save-filtered-items-buffer'), visit this file; if there is
3944 no such file, build the list as with prefix argument `C-u'.
3946 The prefix ARG regulates how many top priorities from
3947 each category to show, as described above."
3949 (todo-filter-items 'top arg
))
3951 (defun todo-filter-top-priorities-multifile (&optional arg
)
3952 "Display a list of top priority items from different categories.
3953 The categories are a subset of the categories in the files listed
3954 in `todo-filter-files', or if this nil, in the files chosen from
3955 a file selection dialog that pops up in this case.
3957 With numerical prefix ARG show at most ARG top priority items
3958 from each category in each file. With `C-u' as prefix argument
3959 show the numbers of top priority items specified in
3960 `todo-top-priorities-overrides', if this is non-nil; otherwise show
3961 `todo-top-priorities' items per category. With no prefix
3962 argument, if a top priorities file for the chosen todo files
3963 exists (see `todo-save-filtered-items-buffer'), visit this file;
3964 if there is no such file, do the same as with prefix argument
3967 (todo-filter-items 'top arg t
))
3969 (defun todo-filter-diary-items (&optional arg
)
3970 "Display a list of todo diary items from different categories.
3971 The categories can be any of those in the current todo file.
3973 Called with no prefix ARG, if a diary items file for the current
3974 todo file has previously been saved (see
3975 `todo-save-filtered-items-buffer'), visit this file; if there is
3976 no such file, build the list of diary items. Called with a
3977 prefix argument, build the list even if there is a saved file of
3980 (todo-filter-items 'diary arg
))
3982 (defun todo-filter-diary-items-multifile (&optional arg
)
3983 "Display a list of todo diary items from different categories.
3984 The categories are a subset of the categories in the files listed
3985 in `todo-filter-files', or if this nil, in the files chosen from
3986 a file selection dialog that pops up in this case.
3988 Called with no prefix ARG, if a diary items file for the chosen
3989 todo files has previously been saved (see
3990 `todo-save-filtered-items-buffer'), visit this file; if there is
3991 no such file, build the list of diary items. Called with a
3992 prefix argument, build the list even if there is a saved file of
3995 (todo-filter-items 'diary arg t
))
3997 (defun todo-filter-regexp-items (&optional arg
)
3998 "Prompt for a regular expression and display items that match it.
3999 The matches can be from any categories in the current todo file
4000 and with non-nil option `todo-filter-done-items', can include
4001 not only todo items but also done items, including those in
4004 Called with no prefix ARG, if a regexp items file for the current
4005 todo file has previously been saved (see
4006 `todo-save-filtered-items-buffer'), visit this file; if there is
4007 no such file, build the list of regexp items. Called with a
4008 prefix argument, build the list even if there is a saved file of
4011 (todo-filter-items 'regexp arg
))
4013 (defun todo-filter-regexp-items-multifile (&optional arg
)
4014 "Prompt for a regular expression and display items that match it.
4015 The matches can be from any categories in the files listed in
4016 `todo-filter-files', or if this nil, in the files chosen from a
4017 file selection dialog that pops up in this case. With non-nil
4018 option `todo-filter-done-items', the matches can include not
4019 only todo items but also done items, including those in Archive
4022 Called with no prefix ARG, if a regexp items file for the current
4023 todo file has previously been saved (see
4024 `todo-save-filtered-items-buffer'), visit this file; if there is
4025 no such file, build the list of regexp items. Called with a
4026 prefix argument, build the list even if there is a saved file of
4029 (todo-filter-items 'regexp arg t
))
4031 (defun todo-find-filtered-items-file ()
4032 "Choose a filtered items file and visit it."
4034 (let ((files (directory-files todo-directory t
"\\.tod[rty]$" t
))
4037 (let ((type (cond ((equal (file-name-extension f
) "todr") "regexp")
4038 ((equal (file-name-extension f
) "todt") "top")
4039 ((equal (file-name-extension f
) "tody") "diary"))))
4040 (push (cons (concat (todo-short-file-name f
) " (" type
")") f
)
4042 (setq file
(completing-read "Choose a filtered items file: "
4043 falist nil t nil nil
(car falist
)))
4044 (setq file
(cdr (assoc-string file falist
)))
4046 (unless (derived-mode-p 'todo-filtered-items-mode
)
4047 (todo-filtered-items-mode))
4048 (todo-prefix-overlays)))
4050 (defun todo-go-to-source-item ()
4051 "Display the file and category of the filtered item at point."
4053 (let* ((str (todo-item-string))
4054 (buf (current-buffer))
4055 (res (todo-find-item str
))
4060 (message "Category %s does not contain this item." cat
)
4062 (set-window-buffer (selected-window)
4063 (set-buffer (find-buffer-visiting file
)))
4064 (setq todo-current-todo-file file
)
4065 (setq todo-category-number
(todo-category-number cat
))
4066 (let ((todo-show-with-done (if (or todo-filter-done-items
4067 (eq (cdr found
) 'done
))
4069 todo-show-with-done
)))
4070 (todo-category-select))
4071 (goto-char (car found
)))))
4073 (defvar todo-multiple-filter-files nil
4074 "List of files selected from `todo-multiple-filter-files' widget.")
4076 (defvar todo-multiple-filter-files-widget nil
4077 "Variable holding widget created by `todo-multiple-filter-files'.")
4079 (defun todo-multiple-filter-files ()
4080 "Pop to a buffer with a widget for choosing multiple filter files."
4083 (require 'wid-edit
))
4084 (with-current-buffer (get-buffer-create "*Todo Filter Files*")
4085 (pop-to-buffer (current-buffer))
4087 (kill-all-local-variables)
4088 (widget-insert "Select files for generating the top priorities list.\n\n")
4089 (setq todo-multiple-filter-files-widget
4091 `(set ,@(todo--files-type-list))))
4092 (widget-insert "\n")
4093 (widget-create 'push-button
4094 :notify
(lambda (&rest _
)
4095 (setq todo-multiple-filter-files
'quit
)
4097 (exit-recursive-edit))
4100 (widget-create 'push-button
4101 :notify
(lambda (&rest _
)
4102 (setq todo-multiple-filter-files
4105 (concat todo-directory
4108 todo-multiple-filter-files-widget
)))
4110 (exit-recursive-edit))
4112 (use-local-map widget-keymap
)
4114 (message "Click \"Apply\" after selecting files.")
4117 (defconst todo-filtered-items-buffer
"Todo filtered items"
4118 "Initial name of buffer in Todo Filter Items mode.")
4120 (defconst todo-top-priorities-buffer
"Todo top priorities"
4121 "Buffer type string for `todo-filter-items'.")
4123 (defconst todo-diary-items-buffer
"Todo diary items"
4124 "Buffer type string for `todo-filter-items'.")
4126 (defconst todo-regexp-items-buffer
"Todo regexp items"
4127 "Buffer type string for `todo-filter-items'.")
4129 (defun todo-filter-items (filter &optional new multifile
)
4130 "Display a list of items filtered by FILTER.
4131 The values of FILTER can be `top' for top priority items, a cons
4132 of `top' and a number passed by the caller, `diary' for diary
4133 items, or `regexp' for items matching a regular expression
4134 entered by the user. The items can come from any categories in
4135 the current todo file or, with non-nil MULTIFILE, from several
4136 files. If NEW is nil, visit an appropriate file containing the
4137 list of filtered items; if there is no such file, or with non-nil
4138 NEW, build the list and display it.
4140 See the documentation strings of the commands
4141 `todo-filter-top-priorities', `todo-filter-diary-items',
4142 `todo-filter-regexp-items', and those of the corresponding
4143 multifile commands for further details."
4144 (let* ((top (eq filter
'top
))
4145 (diary (eq filter
'diary
))
4146 (regexp (eq filter
'regexp
))
4147 (buf (cond (top todo-top-priorities-buffer
)
4148 (diary todo-diary-items-buffer
)
4149 (regexp todo-regexp-items-buffer
)))
4150 (flist (if multifile
4151 (or todo-filter-files
4152 (progn (todo-multiple-filter-files)
4153 todo-multiple-filter-files
))
4154 (list todo-current-todo-file
)))
4155 (fname (if (equal flist
'quit
)
4156 ;; Pressed `cancel' in t-m-f-f file selection dialog.
4158 (concat todo-directory
4159 (mapconcat #'todo-short-file-name flist
"-")
4162 (regexp ".todr")))))
4163 (multi (> (length flist
) 1))
4164 (rxfiles (when regexp
4165 (directory-files todo-directory t
".*\\.todr$" t
)))
4166 (file-exists (or (file-exists-p fname
) rxfiles
))
4168 (cond ((and top new
(natnump new
))
4169 (todo-filter-items-1 (cons 'top new
) flist
))
4170 ((and (not new
) file-exists
)
4171 (when (and rxfiles
(> (length rxfiles
) 1))
4172 (let ((rxf (mapcar #'todo-short-file-name rxfiles
)))
4173 (setq fname
(todo-absolute-file-name
4174 (completing-read "Choose a regexp items file: "
4178 (unless (derived-mode-p 'todo-filtered-items-mode
)
4179 (todo-filtered-items-mode))
4180 (todo-prefix-overlays)
4181 (todo-check-filtered-items-file))
4183 (todo-filter-items-1 filter flist
)))
4184 (dolist (s (split-string (todo-short-file-name fname
) "-"))
4185 (setq bufname
(if bufname
4186 (concat bufname
(if (member s
(mapcar
4187 #'todo-short-file-name
4192 (rename-buffer (format (concat "%s for file" (if multi
"s" "") " \"%s\"")
4195 (defun todo-filter-items-1 (filter file-list
)
4196 "Build a list of items by applying FILTER to FILE-LIST.
4197 Internal subroutine called by `todo-filter-items', which passes
4198 the values of FILTER and FILE-LIST."
4199 (let ((num (if (consp filter
) (cdr filter
) todo-top-priorities
))
4200 (buf (get-buffer-create todo-filtered-items-buffer
))
4201 (multifile (> (length file-list
) 1))
4202 regexp fname bufstr cat beg end done
)
4203 (if (null file-list
)
4204 (user-error "No files have been chosen for filtering")
4205 (with-current-buffer buf
4207 (kill-all-local-variables)
4208 (todo-filtered-items-mode))
4209 (when (eq filter
'regexp
)
4210 (setq regexp
(read-string "Enter a regular expression: ")))
4211 (save-current-buffer
4212 (dolist (f file-list
)
4213 ;; Before inserting file contents into temp buffer, save a modified
4214 ;; buffer visiting it.
4215 (let ((bf (find-buffer-visiting f
)))
4216 (when (buffer-modified-p bf
)
4217 (with-current-buffer bf
(save-buffer))))
4218 (setq fname
(todo-short-file-name f
))
4220 (when (and todo-filter-done-items
(eq filter
'regexp
))
4221 ;; If there is a corresponding archive file for the
4222 ;; todo file, insert it first and add identifiers for
4223 ;; todo-go-to-source-item.
4224 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
4225 (when (file-exists-p arch
)
4226 (insert-file-contents arch
)
4227 ;; Delete todo archive file's categories sexp.
4228 (delete-region (line-beginning-position)
4229 (1+ (line-end-position)))
4232 (when (re-search-forward
4233 (concat (if todo-filter-done-items
4234 (concat "\\(?:" todo-done-string-start
4235 "\\|" todo-date-string-start
4237 todo-date-string-start
)
4238 todo-date-pattern
"\\(?: "
4239 diary-time-regexp
"\\)?"
4240 (if todo-filter-done-items
4242 (regexp-quote todo-nondiary-end
)) "?")
4244 (insert "(archive) "))
4246 (insert-file-contents f
)
4247 ;; Delete todo file's categories sexp.
4248 (delete-region (line-beginning-position) (1+ (line-end-position)))
4250 ;; Unless the number of top priorities to show was
4251 ;; passed by the caller, the file-wide value from
4252 ;; `todo-top-priorities-overrides', if non-nil, overrides
4253 ;; `todo-top-priorities'.
4254 (unless (consp filter
)
4255 (setq fnum
(or (nth 1 (assoc f todo-top-priorities-overrides
))
4256 todo-top-priorities
)))
4257 (while (re-search-forward
4258 (concat "^" (regexp-quote todo-category-beg
)
4261 (setq cat
(match-string 1))
4263 ;; Unless the number of top priorities to show was
4264 ;; passed by the caller, the category-wide value
4265 ;; from `todo-top-priorities-overrides', if non-nil,
4266 ;; overrides a non-nil file-wide value from
4267 ;; `todo-top-priorities-overrides' as well as
4268 ;; `todo-top-priorities'.
4269 (unless (consp filter
)
4270 (let ((cats (nth 2 (assoc f todo-top-priorities-overrides
))))
4271 (setq cnum
(or (cdr (assoc cat cats
)) fnum
))))
4272 (delete-region (match-beginning 0) (match-end 0))
4273 (setq beg
(point)) ; First item in the current category.
4274 (setq end
(if (re-search-forward
4275 (concat "^" (regexp-quote todo-category-beg
))
4281 (if (re-search-forward
4282 (concat "\n" (regexp-quote todo-category-done
))
4286 (unless (and todo-filter-done-items
(eq filter
'regexp
))
4287 ;; Leave done items.
4288 (delete-region done end
)
4290 (narrow-to-region beg end
) ; Process only current category.
4291 (goto-char (point-min))
4292 ;; Apply the filter.
4293 (cond ((eq filter
'diary
)
4295 (if (looking-at (regexp-quote todo-nondiary-start
))
4297 (todo-forward-item))))
4298 ((eq filter
'regexp
)
4300 (if (looking-at todo-item-start
)
4301 (if (string-match regexp
(todo-item-string))
4304 ;; Kill lines that aren't part of a todo or done
4305 ;; item (empty or todo-category-done).
4306 (delete-region (line-beginning-position)
4307 (1+ (line-end-position))))
4308 ;; If last todo item in file matches regexp and
4309 ;; there are no following done items,
4310 ;; todo-category-done string is left dangling,
4311 ;; because todo-forward-item jumps over it.
4314 (concat (regexp-quote todo-done-string
)
4316 (line-beginning-position 0)))
4317 (delete-region (point) (progn
4320 (t ; Filter top priority items.
4321 (setq num
(or cnum fnum num
))
4323 (todo-forward-item num
))))
4325 ;; Delete non-top-priority items.
4326 (unless (member filter
'(diary regexp
))
4327 (delete-region beg end
))
4328 (goto-char (point-min))
4329 ;; Add file (if using multiple files) and category tags to
4332 (when (re-search-forward
4333 (concat (if todo-filter-done-items
4334 (concat "\\(?:" todo-done-string-start
4335 "\\|" todo-date-string-start
4337 todo-date-string-start
)
4338 todo-date-pattern
"\\(?: " diary-time-regexp
4339 "\\)?" (if todo-filter-done-items
4341 (regexp-quote todo-nondiary-end
))
4345 (when (looking-at "(archive) ") (goto-char (match-end 0)))
4346 (insert (if multifile
(concat fname
":") "") cat
"]"))
4349 (setq bufstr
(buffer-string))
4350 (with-current-buffer buf
4351 (let (buffer-read-only)
4352 (insert bufstr
)))))))
4353 (set-window-buffer (selected-window) (set-buffer buf
))
4354 (todo-prefix-overlays)
4355 (goto-char (point-min)))))
4357 (defun todo-set-top-priorities (&optional arg
)
4358 "Set number of top priorities shown by `todo-filter-top-priorities'.
4359 With non-nil ARG, set the number only for the current Todo
4360 category; otherwise, set the number for all categories in the
4363 Calling this function via either of the commands
4364 `todo-set-top-priorities-in-file' or
4365 `todo-set-top-priorities-in-category' is the recommended way to
4366 set the user customizable option `todo-top-priorities-overrides'."
4367 (let* ((cat (todo-current-category))
4368 (file todo-current-todo-file
)
4369 (rules todo-top-priorities-overrides
)
4370 (frule (assoc-string file rules
))
4371 (crules (nth 2 frule
))
4372 (crule (assoc-string cat crules
))
4373 (fcur (or (nth 1 frule
)
4374 todo-top-priorities
))
4375 (ccur (or (and arg
(cdr crule
))
4377 (prompt (if arg
(concat "Number of top priorities in this category"
4378 " (currently %d): ")
4379 (concat "Default number of top priorities per category"
4380 " in this file (currently %d): ")))
4383 (let ((cur (if arg ccur fcur
)))
4384 (setq new
(read-number (format prompt cur
))
4385 prompt
"Enter a non-negative number: "
4387 (let ((nrule (if arg
4388 (append (delete crule crules
) (list (cons cat new
)))
4389 (append (list file new
) (list crules
)))))
4390 (setq rules
(cons (if arg
4391 (list file fcur nrule
)
4393 (delete frule rules
)))
4394 (customize-save-variable 'todo-top-priorities-overrides rules
)
4395 (todo-prefix-overlays))))
4397 (defun todo-find-item (str)
4398 "Search for filtered item STR in its saved todo file.
4399 Return the list (FOUND FILE CAT), where CAT and FILE are the
4400 item's category and file, and FOUND is a cons cell if the search
4401 succeeds, whose car is the start of the item in FILE and whose
4402 cdr is `done', if the item is now a done item, `changed', if its
4403 text was truncated or augmented or, for a top priority item, if
4404 its priority has changed, and `same' otherwise."
4405 (string-match (concat (if todo-filter-done-items
4406 (concat "\\(?:" todo-done-string-start
"\\|"
4407 todo-date-string-start
"\\)")
4408 todo-date-string-start
)
4409 todo-date-pattern
"\\(?: " diary-time-regexp
"\\)?"
4410 (if todo-filter-done-items
4412 (regexp-quote todo-nondiary-end
)) "?"
4413 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
4414 "\\(?1:.*\\)\\]\\).*$")
4416 (let ((cat (match-string 1 str
))
4417 (file (match-string 2 str
))
4418 (archive (string= (match-string 3 str
) "(archive) "))
4419 (filcat (match-string 4 str
))
4421 (tpbuf (save-match-data (string-match "top" (buffer-name))))
4423 (setq str
(replace-match "" nil nil str
4))
4425 ;; Calculate priority of STR wrt its category.
4427 (while (search-backward filcat nil t
)
4428 (setq tpriority
(1+ tpriority
)))))
4430 (concat todo-directory
(substring file
0 -
1)
4431 (if archive
".toda" ".todo"))
4433 (concat (file-name-sans-extension
4434 todo-global-current-todo-file
) ".toda")
4435 todo-global-current-todo-file
)))
4436 (find-file-noselect file
)
4437 (with-current-buffer (find-buffer-visiting file
)
4439 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
4440 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
4443 (goto-char (point-min))
4444 (let ((beg (re-search-forward
4445 (concat "^" (regexp-quote (concat todo-category-beg cat
))
4448 (done (save-excursion
4450 (concat "^" (regexp-quote todo-category-done
)) nil t
)))
4451 (end (save-excursion
4452 (or (re-search-forward
4453 (concat "^" (regexp-quote todo-category-beg
))
4456 (setq found
(when (search-forward str end t
)
4457 (goto-char (match-beginning 0))))
4460 (cons found
(if (> (point) done
)
4462 (let ((cpriority 1))
4465 ;; Not top item in category.
4466 (while (> (point) (1+ beg
))
4467 (let ((opoint (point)))
4468 (todo-backward-item)
4469 ;; Can't move backward beyond
4470 ;; first item in file.
4471 (unless (= (point) opoint
)
4472 (setq cpriority
(1+ cpriority
)))))))
4473 (if (and (= tpriority cpriority
)
4474 ;; Proper substring is not the same.
4475 (string= (todo-item-string)
4479 (list found file cat
)))
4481 (defun todo-check-filtered-items-file ()
4482 "Check if filtered items file is up to date and a show suitable message."
4486 (let* ((item (todo-item-string))
4487 (found (car (todo-find-item item
))))
4488 (unless (eq (cdr found
) 'same
)
4490 (overlay-put (make-overlay (todo-item-start) (todo-item-end))
4491 'face
'todo-search
))
4492 (setq count
(1+ count
))))
4493 ;; (throw 'old (message "The marked item is not up to date.")))
4494 (todo-forward-item))
4496 (message "Filtered items file is up to date.")
4497 (message (concat "The highlighted item" (if (= count
1) " is " "s are ")
4499 ;; "\nType <return> on item for details."
4502 (defun todo-filter-items-filename ()
4503 "Return absolute file name for saving this Filtered Items buffer."
4504 (let ((bufname (buffer-name)))
4505 (string-match "\"\\([^\"]+\\)\"" bufname
)
4506 (let* ((filename-str (substring bufname
(match-beginning 1) (match-end 1)))
4507 (filename-base (replace-regexp-in-string ", " "-" filename-str
))
4508 (top-priorities (string-match "top priorities" bufname
))
4509 (diary-items (string-match "diary items" bufname
))
4510 (regexp-items (string-match "regexp items" bufname
)))
4512 (let ((prompt (concat "Enter a short identifying string"
4513 " to make this file name unique: ")))
4514 (setq filename-base
(concat filename-base
"-" (read-string prompt
)))))
4515 (concat todo-directory filename-base
4516 (cond (top-priorities ".todt")
4517 (diary-items ".tody")
4518 (regexp-items ".todr"))))))
4520 (defun todo-save-filtered-items-buffer ()
4521 "Save current Filtered Items buffer to a file.
4522 If the file already exists, overwrite it only on confirmation."
4523 (let ((filename (or (buffer-file-name) (todo-filter-items-filename))))
4524 (write-file filename t
)))
4526 ;; -----------------------------------------------------------------------------
4527 ;;; Printing Todo mode buffers
4528 ;; -----------------------------------------------------------------------------
4530 (defcustom todo-print-buffer-function
#'ps-print-buffer-with-faces
4531 "Function called by `todo-print-buffer' to print Todo mode buffers.
4532 Called with one argument which can either be:
4533 - a string, naming a file to save the print image to.
4534 - nil, to send the image to the printer."
4538 (defvar todo-print-buffer
"*Todo Print*"
4539 "Name of buffer with printable version of Todo mode buffer.")
4541 (defun todo-print-buffer (&optional to-file
)
4542 "Produce a printable version of the current Todo mode buffer.
4543 This converts overlays and soft line wrapping and, depending on
4544 the value of `todo-print-buffer-function', includes faces. With
4545 non-nil argument TO-FILE write the printable version to a file;
4546 otherwise, send it to the default printer."
4548 (let ((buf todo-print-buffer
)
4550 ((eq major-mode
'todo-mode
)
4551 (concat "Todo File: "
4552 (todo-short-file-name todo-current-todo-file
)
4553 "\nCategory: " (todo-current-category)))
4554 ((eq major-mode
'todo-filtered-items-mode
)
4556 (prefix (propertize (concat todo-prefix
" ")
4557 'face
'todo-prefix-string
))
4559 (fill-prefix (make-string todo-indent-to-here
32))
4560 (content (buffer-string)))
4561 (with-current-buffer (get-buffer-create buf
)
4563 (goto-char (point-min))
4566 (end (save-excursion (todo-item-end))))
4567 (when todo-number-prefix
4569 (setq prefix
(propertize (concat (number-to-string num
) " ")
4570 'face
'todo-prefix-string
)))
4572 (fill-region beg end
))
4573 ;; Calling todo-forward-item infloops at todo-item-start due to
4574 ;; non-overlay prefix, so search for item start instead.
4575 (if (re-search-forward todo-item-start nil t
)
4577 (goto-char (point-max))))
4578 (if (re-search-backward (concat "^" (regexp-quote todo-category-done
))
4580 (replace-match todo-done-separator
))
4581 (goto-char (point-min))
4584 (funcall todo-print-buffer-function
4586 (read-file-name "Print to file: "))))
4589 (defun todo-print-buffer-to-file ()
4590 "Save printable version of this Todo mode buffer to a file."
4592 (todo-print-buffer t
))
4594 ;; -----------------------------------------------------------------------------
4595 ;;; Legacy Todo mode files
4596 ;; -----------------------------------------------------------------------------
4598 (defcustom todo-legacy-date-time-regexp
4599 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
4600 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
4601 "Regexp matching legacy todo-mode.el item date-time strings.
4602 In order for `todo-convert-legacy-files' to correctly convert
4603 this string to the current Todo mode format, the regexp must
4604 contain four explicitly numbered groups (see `(elisp) Regexp
4605 Backslash'), where group 1 matches a string for the year, group 2
4606 a string for the month, group 3 a string for the day and group 4
4607 a string for the time. The default value converts date-time
4608 strings built using the default value of
4609 `todo-time-string-format' from todo-mode.el."
4613 (defun todo-convert-legacy-date-time ()
4614 "Return converted date-time string.
4615 Helper function for `todo-convert-legacy-files'."
4616 (let* ((year (match-string 1))
4617 (month (match-string 2))
4618 (monthname (calendar-month-name (string-to-number month
) t
))
4619 (day (match-string 3))
4620 (time (match-string 4))
4623 (insert (mapconcat #'eval calendar-date-display-form
"")
4624 (when time
(concat " " time
)))))
4626 (defun todo-convert-legacy-files ()
4627 "Convert legacy todo files to the current Todo mode format.
4628 The old-style files named by the variables `todo-file-do' and
4629 `todo-file-done' from the old package are converted to the new
4630 format and saved (the latter as a todo archive file) with a new
4631 name in `todo-directory'. See also the documentation string of
4632 `todo-legacy-date-time-regexp' for further details."
4634 ;; If there are user customizations of legacy options, use them,
4635 ;; otherwise use the legacy default values.
4636 (let ((todo-file-do-tem (if (boundp 'todo-file-do
)
4638 (locate-user-emacs-file "todo-do" ".todo-do")))
4639 (todo-file-done-tem (if (boundp 'todo-file-done
)
4641 (locate-user-emacs-file "todo-done" ".todo-done")))
4642 (todo-initials-tem (and (boundp 'todo-initials
) todo-initials
))
4643 (todo-entry-prefix-function-tem (and (boundp 'todo-entry-prefix-function
)
4644 todo-entry-prefix-function
))
4646 ;; Convert `todo-file-do'.
4647 (if (not (file-exists-p todo-file-do-tem
))
4648 (message "No legacy todo file exists")
4649 (let ((default "todo-do-conv")
4652 (insert-file-contents todo-file-do-tem
)
4653 ;; Eliminate old-style local variables list in first line.
4654 (delete-region (line-beginning-position) (1+ (line-end-position)))
4655 (search-forward " --- " nil t
) ; Legacy todo-category-beg.
4656 (setq todo-prefix-tem
(buffer-substring-no-properties
4657 (line-beginning-position) (match-beginning 0)))
4658 (goto-char (point-min))
4661 ;; Old-style category start delimiter.
4662 ((looking-at (regexp-quote (concat todo-prefix-tem
" --- ")))
4663 (replace-match todo-category-beg
))
4664 ;; Old-style category end delimiter.
4665 ((looking-at (regexp-quote "--- End"))
4667 ;; Old-style category separator.
4668 ((looking-at (regexp-quote
4669 (concat todo-prefix-tem
" "
4670 (make-string 75 ?-
))))
4671 (replace-match todo-category-done
))
4672 ;; Old-style item header (date/time/initials).
4673 ((looking-at (concat (regexp-quote todo-prefix-tem
) " "
4674 (if todo-entry-prefix-function-tem
4675 (funcall todo-entry-prefix-function-tem
)
4676 (concat todo-legacy-date-time-regexp
" "
4677 (if todo-initials-tem
4678 (regexp-quote todo-initials-tem
)
4681 (todo-convert-legacy-date-time)))
4683 (setq file
(concat todo-directory
4685 (format "Save file as (default \"%s\"): " default
)
4688 (unless (file-exists-p todo-directory
)
4689 (make-directory todo-directory
))
4690 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
4692 (insert-file-contents file
)
4693 (let ((todo-categories (todo-make-categories-list t
)))
4694 (todo-update-categories-sexp)
4695 (todo-check-format))
4696 (write-region (point-min) (point-max) file nil
'nomessage
))
4697 (setq todo-files
(funcall todo-files-function
))
4698 ;; Convert `todo-file-done'.
4699 (when (file-exists-p todo-file-done-tem
)
4701 (insert-file-contents todo-file-done-tem
)
4702 (let ((beg (make-marker))
4704 cat cats comment item
)
4706 (when (looking-at todo-legacy-date-time-regexp
)
4707 (set-marker beg
(point))
4708 (todo-convert-legacy-date-time)
4709 (set-marker end
(point))
4711 (insert "[" todo-done-string
)
4715 (when (looking-at todo-legacy-date-time-regexp
)
4716 (todo-convert-legacy-date-time))
4717 (when (looking-at (concat " " (if todo-initials-tem
4722 (replace-match "")))
4723 (if (re-search-forward
4724 (concat "^" todo-legacy-date-time-regexp
) nil t
)
4725 (goto-char (match-beginning 0))
4726 (goto-char (point-max)))
4728 (when (looking-back "\\[\\([^][]+\\)\\]"
4729 (line-beginning-position))
4730 (setq cat
(match-string 1))
4731 (goto-char (match-beginning 0))
4733 ;; If the item ends with a non-comment parenthesis not
4734 ;; followed by a period, we lose (but we inherit that
4735 ;; problem from the legacy code).
4736 ;; FIXME: fails on multiline comment
4737 (when (looking-back "(\\(.*\\)) " (line-beginning-position))
4738 (setq comment
(match-string 1))
4740 (insert "[" todo-comment-string
": " comment
"]"))
4741 (set-marker end
(point))
4742 (if (member cat cats
)
4743 ;; If item is already in its category, leave it there.
4744 (unless (save-excursion
4746 (concat "^" (regexp-quote todo-category-beg
)
4749 (string= (match-string 1) cat
))
4750 ;; Else move it to its category.
4751 (setq item
(buffer-substring-no-properties beg end
))
4752 (delete-region beg
(1+ end
))
4753 (set-marker beg
(point))
4756 (regexp-quote (concat todo-category-beg cat
))
4760 (if (re-search-forward
4761 (concat "^" (regexp-quote todo-category-beg
)
4764 (progn (goto-char (match-beginning 0))
4767 (goto-char (point-max)))
4772 (insert todo-category-beg cat
"\n\n"
4773 todo-category-done
"\n"))
4775 (set-marker beg nil
)
4776 (set-marker end nil
))
4777 (setq file
(concat (file-name-sans-extension file
) ".toda"))
4778 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
4780 (insert-file-contents file
)
4781 (let* ((todo-categories (todo-make-categories-list t
)))
4782 (todo-update-categories-sexp)
4783 (todo-check-format))
4784 (write-region (point-min) (point-max) file nil
'nomessage
)
4785 (setq archive-sexp
(read (buffer-substring-no-properties
4786 (line-beginning-position)
4787 (line-end-position)))))
4788 (setq file
(concat (file-name-sans-extension file
) ".todo"))
4789 ;; Update categories sexp of converted todo file again, adding
4790 ;; counts of archived items.
4792 (insert-file-contents file
)
4793 (let ((sexp (read (buffer-substring-no-properties
4794 (line-beginning-position)
4795 (line-end-position)))))
4797 (let ((archive-cat (assoc (car cat
) archive-sexp
)))
4799 (aset (cdr cat
) 3 (aref (cdr archive-cat
) 2)))))
4800 (delete-region (line-beginning-position) (line-end-position))
4801 (prin1 sexp
(current-buffer)))
4802 (write-region (point-min) (point-max) file nil
'nomessage
))
4803 (setq todo-archives
(funcall todo-files-function t
)))
4804 (todo-update-filelist-defcustoms)
4805 (when (y-or-n-p (concat "Format conversion done; do you want to "
4806 "visit the converted file now? "))
4807 (setq todo-current-todo-file file
)
4808 (unless todo-default-todo-file
4809 ;; We just initialized the first todo file, so make it the
4810 ;; default now to avoid an infinite recursion with todo-show.
4811 (setq todo-default-todo-file
(todo-short-file-name file
)))
4814 ;; -----------------------------------------------------------------------------
4815 ;;; Utility functions for todo files, categories and items
4816 ;; -----------------------------------------------------------------------------
4818 (defun todo-absolute-file-name (name &optional type
)
4819 "Return the absolute file name of short todo file NAME.
4820 With TYPE `archive' or `top' return the absolute file name of the
4821 short todo archive or top priorities file name, respectively."
4822 ;; No-op if there is no todo file yet (i.e. don't concatenate nil).
4825 (concat todo-directory name
4826 (cond ((eq type
'archive
) ".toda")
4827 ((eq type
'top
) ".todt")
4828 ((eq type
'diary
) ".tody")
4829 ((eq type
'regexp
) ".todr")
4832 (defun todo-check-file (file)
4833 "Check the state associated with FILE and update it if necessary.
4834 If FILE exists, return t. If it does not exist and there is no
4835 live buffer with its content, return nil; if there is such a
4836 buffer and the user tries to show it, ask whether to restore
4837 FILE, and if confirmed, do so and return t; else delete the
4838 buffer, clean up the state and return nil."
4839 (setq todo-files
(funcall todo-files-function
))
4840 (setq todo-archives
(funcall todo-files-function t
))
4841 (if (file-exists-p file
)
4843 (setq todo-visited
(delete file todo-visited
))
4844 (let ((buf (find-buffer-visiting file
)))
4848 (format (concat "Todo file \"%s\" has been deleted but "
4849 "its content is still in a buffer!\n")
4850 (todo-short-file-name file
))
4851 "Save that buffer and restore the todo file? ")))
4853 (with-current-buffer buf
(save-buffer))
4854 (setq todo-files
(funcall todo-files-function
))
4855 (setq todo-archives
(funcall todo-files-function t
))
4857 (let* ((files (append todo-files todo-archives
)))
4858 (unless (or (not todo-current-todo-file
)
4859 (member todo-current-todo-file files
))
4860 (setq todo-current-todo-file nil
))
4861 (unless (or (not todo-global-current-todo-file
)
4862 (member todo-global-current-todo-file files
))
4863 (setq todo-global-current-todo-file nil
))
4864 (unless (or (not todo-default-todo-file
)
4865 (member todo-default-todo-file files
))
4866 (setq todo-default-todo-file
(todo-short-file-name
4868 (todo-update-filelist-defcustoms)
4869 (when buf
(kill-buffer buf
))
4872 (defun todo-category-number (cat)
4873 "Return the number of category CAT in this todo file.
4874 The buffer-local variable `todo-category-number' holds this
4875 number as its value."
4876 (let ((categories (mapcar #'car todo-categories
)))
4877 (setq todo-category-number
4878 ;; Increment by one, so that the number of the first
4879 ;; category is one rather than zero.
4880 (1+ (- (length categories
)
4881 (length (member cat categories
)))))))
4883 (defun todo-current-category ()
4884 "Return the name of the current category."
4885 (car (nth (1- todo-category-number
) todo-categories
)))
4887 (defun todo-category-select ()
4888 "Display the current category correctly."
4889 (let ((name (todo-current-category))
4890 cat-begin cat-end done-start done-sep-start done-end
)
4892 (goto-char (point-min))
4894 (concat "^" (regexp-quote (concat todo-category-beg name
)) "$") nil t
)
4895 (setq cat-begin
(1+ (line-end-position)))
4896 (setq cat-end
(if (re-search-forward
4897 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
4900 (setq mode-line-buffer-identification
4901 (funcall todo-mode-line-function name
))
4902 (narrow-to-region cat-begin cat-end
)
4903 (todo-prefix-overlays)
4904 (goto-char (point-min))
4905 (if (re-search-forward (concat "\n\\(" (regexp-quote todo-category-done
)
4909 (setq done-start
(match-beginning 0))
4910 (setq done-sep-start
(match-beginning 1))
4911 (setq done-end
(match-end 0)))
4912 (error "Category %s is missing todo-category-done string" name
))
4913 (if todo-show-done-only
4914 (narrow-to-region (1+ done-end
) (point-max))
4915 (when (and todo-show-with-done
4916 (re-search-forward todo-done-string-start nil t
))
4917 ;; Now we want to see the done items, so reset displayed end to end of
4919 (setq done-start cat-end
)
4920 ;; Make display overlay for done items separator string, unless there
4922 (let* ((done-sep todo-done-separator
)
4923 (ov (progn (goto-char done-sep-start
)
4924 (todo-get-overlay 'separator
))))
4926 (setq ov
(make-overlay done-sep-start done-end
))
4927 (overlay-put ov
'todo
'separator
)
4928 (overlay-put ov
'display done-sep
))))
4929 (narrow-to-region (point-min) done-start
)
4930 ;; Loading this from todo-mode, or adding it to the mode hook, causes
4931 ;; Emacs to hang in todo-item-start, at (looking-at todo-item-start).
4932 (when todo-highlight-item
4934 (hl-line-mode 1)))))
4936 (defun todo-get-count (type &optional category
)
4937 "Return count of TYPE items in CATEGORY.
4938 If CATEGORY is nil, default to the current category."
4939 (let* ((cat (or category
(todo-current-category)))
4940 (counts (cdr (assoc cat todo-categories
)))
4941 (idx (cond ((eq type
'todo
) 0)
4942 ((eq type
'diary
) 1)
4944 ((eq type
'archived
) 3))))
4947 (defun todo-update-count (type increment
&optional category
)
4948 "Change count of TYPE items in CATEGORY by integer INCREMENT.
4949 With nil or omitted CATEGORY, default to the current category."
4950 (let* ((cat (or category
(todo-current-category)))
4951 (counts (cdr (assoc cat todo-categories
)))
4952 (idx (cond ((eq type
'todo
) 0)
4953 ((eq type
'diary
) 1)
4955 ((eq type
'archived
) 3))))
4956 (aset counts idx
(+ increment
(aref counts idx
)))))
4958 (defun todo-set-categories ()
4959 "Set `todo-categories' from the sexp at the top of the file."
4960 ;; New archive files created by `todo-move-category' are empty, which would
4961 ;; make the sexp test fail and raise an error, so in this case we skip it.
4962 (unless (zerop (buffer-size))
4966 (goto-char (point-min))
4967 (setq todo-categories
4968 (if (looking-at "((\"")
4969 (read (buffer-substring-no-properties
4970 (line-beginning-position)
4971 (line-end-position)))
4972 (error "Invalid or missing todo-categories sexp")))))))
4974 (defun todo-update-categories-sexp ()
4975 "Update the `todo-categories' sexp at the top of the file."
4976 (let (buffer-read-only)
4980 (goto-char (point-min))
4981 (if (looking-at (concat "^" (regexp-quote todo-category-beg
)))
4982 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
4983 (setq todo-categories
(todo-make-categories-list t
)))
4984 (delete-region (line-beginning-position) (line-end-position)))
4985 (prin1 todo-categories
(current-buffer))))))
4987 (defun todo-make-categories-list (&optional force
)
4988 "Return an alist of todo categories and their item counts.
4989 With non-nil argument FORCE parse the entire file to build the
4990 list; otherwise, get the value by reading the sexp at the top of
4992 (setq todo-categories nil
)
4996 (goto-char (point-min))
4997 (let (counts cat archive
)
4998 ;; If the file is a todo file and has archived items, identify the
4999 ;; archive, in order to count its items. But skip this with
5000 ;; `todo-convert-legacy-files', since that converts filed items to
5002 (when buffer-file-name
; During conversion there is no file yet.
5003 ;; If the file is an archive, it doesn't have an archive.
5004 (unless (member (file-truename buffer-file-name
)
5005 (funcall todo-files-function t
))
5006 (setq archive
(concat (file-name-sans-extension
5007 todo-current-todo-file
) ".toda"))))
5009 (cond ((looking-at (concat (regexp-quote todo-category-beg
)
5011 (setq cat
(match-string-no-properties 1))
5012 ;; Counts for each category: [todo diary done archive]
5013 (setq counts
(make-vector 4 0))
5014 (setq todo-categories
5015 (append todo-categories
(list (cons cat counts
))))
5016 ;; Add archived item count to the todo file item counts.
5017 ;; Make sure to include newly created archives, e.g. due to
5018 ;; todo-move-category.
5019 (when (member archive
(funcall todo-files-function t
))
5020 (let ((archive-count 0)
5021 (visiting (find-buffer-visiting archive
)))
5022 (with-current-buffer (or visiting
5023 (find-file-noselect archive
))
5027 (goto-char (point-min))
5028 (when (re-search-forward
5029 (concat "^" (regexp-quote todo-category-beg
)
5033 (while (not (or (looking-at
5035 (regexp-quote todo-category-beg
)
5038 (when (looking-at todo-done-string-start
)
5039 (setq archive-count
(1+ archive-count
)))
5041 (unless visiting
(kill-buffer)))
5042 (todo-update-count 'archived archive-count cat
))))
5043 ((looking-at todo-done-string-start
)
5044 (todo-update-count 'done
1 cat
))
5045 ((looking-at (concat "^\\("
5046 (regexp-quote diary-nonmarking-symbol
)
5047 "\\)?" todo-date-pattern
))
5048 (todo-update-count 'diary
1 cat
)
5049 (todo-update-count 'todo
1 cat
))
5050 ((looking-at (concat todo-date-string-start todo-date-pattern
))
5051 (todo-update-count 'todo
1 cat
))
5052 ;; If first line is todo-categories list, use it and end loop
5053 ;; -- unless FORCEd to scan whole file.
5056 (setq todo-categories
(read (buffer-substring-no-properties
5057 (line-beginning-position)
5058 (line-end-position))))
5059 (goto-char (1- (point-max))))))
5063 (defun todo-repair-categories-sexp ()
5064 "Repair corrupt todo file categories sexp.
5065 This should only be needed as a consequence of careless manual
5066 editing or a bug in todo.el.
5068 *Warning*: Calling this command restores the category order to
5069 the list element order in the todo file categories sexp, so any
5070 order changes made in Todo Categories mode will have to be made
5073 (let ((todo-categories (todo-make-categories-list t
)))
5074 (todo-update-categories-sexp)))
5076 (defun todo-check-format ()
5077 "Signal an error if the current todo file is ill-formatted.
5078 Otherwise return t. Display a message if the file is well-formed
5079 but the categories sexp differs from the current value of
5084 (goto-char (point-min))
5085 (let* ((cats (prin1-to-string todo-categories
))
5086 (ssexp (buffer-substring-no-properties (line-beginning-position)
5087 (line-end-position)))
5088 (sexp (read ssexp
)))
5089 ;; Check the first line for `todo-categories' sexp.
5092 (unless (and (stringp (car c
))
5095 (user-error "Invalid or missing todo-categories sexp"))))
5097 ;; Check well-formedness of categories.
5098 (let ((legit (concat
5099 "\\(^" (regexp-quote todo-category-beg
) "\\)"
5100 "\\|\\(" todo-date-string-start todo-date-pattern
"\\)"
5101 "\\|\\(^[ \t]+[^ \t]*\\)"
5103 "\\|\\(^" (regexp-quote todo-category-done
) "\\)"
5104 "\\|\\(" todo-done-string-start
"\\)")))
5106 (unless (looking-at legit
)
5107 (user-error "Illegitimate todo file format at line %d"
5108 (line-number-at-pos (point))))
5110 ;; Warn user if categories sexp has changed.
5111 (unless (string= ssexp cats
)
5112 (message (concat "The sexp at the beginning of the file differs "
5113 "from the value of `todo-categories'.\n"
5114 "If the sexp is wrong, you can fix it with "
5115 "M-x todo-repair-categories-sexp,\n"
5116 "but note this reverts any changes you have "
5117 "made in the order of the categories."))))))
5120 (defun todo-item-start ()
5121 "Move to start of current todo item and return its position."
5123 ;; Buffer is empty (invocation possible e.g. via todo-forward-item
5124 ;; from todo-filter-items when processing category with no todo
5126 (eq (point-min) (point-max))
5127 ;; Point is on the empty line below category's last todo item...
5128 (and (looking-at "^$")
5129 (or (eobp) ; ...and done items are hidden...
5130 (save-excursion ; ...or done items are visible.
5132 (looking-at (concat "^"
5133 (regexp-quote todo-category-done
))))))
5134 ;; Buffer is widened.
5135 (looking-at (regexp-quote todo-category-beg
)))
5136 (goto-char (line-beginning-position))
5137 (while (not (looking-at todo-item-start
))
5141 (defun todo-item-end ()
5142 "Move to end of current todo item and return its position."
5143 ;; Items cannot end with a blank line.
5144 (unless (looking-at "^$")
5145 (let* ((done (todo-done-item-p))
5147 ;; For todo items, end is before the done items section, for done
5148 ;; items, end is before the next category. If these limits are
5149 ;; missing or inaccessible, end it before the end of the buffer.
5150 (lim (if (save-excursion
5152 (concat "^" (regexp-quote (if done
5154 todo-category-done
)))
5156 (progn (setq to-lim t
) (match-beginning 0))
5158 (when (bolp) (forward-char)) ; Find start of next item.
5159 (goto-char (if (re-search-forward todo-item-start lim t
)
5161 (if to-lim lim
(point-max))))
5162 ;; For last todo item, skip back over the empty line before the done
5163 ;; items section, else just back to the end of the previous line.
5164 (backward-char (when (and to-lim
(not done
) (eq (point) lim
)) 2))
5167 (defun todo-item-string ()
5168 "Return bare text of current item as a string."
5169 (let ((opoint (point))
5170 (start (todo-item-start))
5171 (end (todo-item-end)))
5173 (and start end
(buffer-substring-no-properties start end
))))
5175 (defun todo-forward-item (&optional count
)
5176 "Move point COUNT items down (by default, move down by one item)."
5177 (let* ((not-done (not (or (todo-done-item-p) (looking-at "^$"))))
5178 (start (line-end-position)))
5180 (if (re-search-forward todo-item-start nil t
(or count
1))
5181 (goto-char (match-beginning 0))
5182 (goto-char (point-max)))
5183 ;; If points advances by one from a todo to a done item, go back
5184 ;; to the space above todo-done-separator, since that is a
5185 ;; legitimate place to insert an item. But skip this space if
5186 ;; count > 1, since that should only stop on an item.
5187 (when (and not-done
(todo-done-item-p) (not count
))
5188 ;; (if (or (not count) (= count 1))
5189 (re-search-backward "^$" start t
))));)
5190 ;; The preceding sexp is insufficient when buffer is not narrowed,
5191 ;; since there could be no done items in this category, so the
5192 ;; search puts us on first todo item of next category. Does this
5193 ;; ever happen? If so:
5194 ;; (let ((opoint) (point))
5195 ;; (forward-line -1)
5196 ;; (when (or (not count) (= count 1))
5197 ;; (cond ((looking-at (concat "^" (regexp-quote todo-category-beg)))
5198 ;; (forward-line -2))
5199 ;; ((looking-at (concat "^" (regexp-quote todo-category-done)))
5200 ;; (forward-line -1))
5202 ;; (goto-char opoint)))))))
5204 (defun todo-backward-item (&optional count
)
5205 "Move point up to start of item with next higher priority.
5206 With positive numerical prefix COUNT, move point COUNT items
5209 If the category's done items are visible, this command called
5210 with a prefix argument only moves point to a higher item, e.g.,
5211 with point on the first done item and called with prefix 1, it
5212 moves to the last todo item; but if called with point on the
5213 first done item without a prefix argument, it moves point the the
5214 empty line above the done items separator."
5215 (let* ((done (todo-done-item-p)))
5218 (re-search-backward (concat todo-item-start
5219 "\\( " diary-time-regexp
"\\)?"
5220 (regexp-quote todo-nondiary-end
) "? ")
5222 ;; If the item date-time header is hidden, the display engine
5223 ;; moves point to the next earlier displayable position, which
5224 ;; is the end of the next item above, so we move it to the start
5225 ;; of the current item's text (that's what the display engine
5226 ;; does with todo-forward-item in this case.)
5227 ;; FIXME: would it be better to use cursor-sensor-functions?
5228 (when todo--item-headers-hidden
(goto-char (match-end 0))))
5229 ;; Unless this is a regexp filtered items buffer (which can contain
5230 ;; intermixed todo and done items), if points advances by one from a
5231 ;; done to a todo item, go back to the space above
5232 ;; todo-done-separator, since that is a legitimate place to insert an
5233 ;; item. But skip this space if count > 1, since that should only
5235 (when (and done
(not (todo-done-item-p)) (not count
)
5236 ;(or (not count) (= count 1))
5237 (not (equal (buffer-name) todo-regexp-items-buffer
)))
5238 (re-search-forward (concat "^" (regexp-quote todo-category-done
))
5240 (forward-line -
1))))
5242 (defun todo-remove-item ()
5243 "Internal function called in editing, deleting or moving items."
5244 (let ((end (progn (todo-item-end) (1+ (point))))
5245 (beg (todo-item-start))
5247 (push (todo-get-overlay 'prefix
) ovs
)
5248 (push (todo-get-overlay 'header
) ovs
)
5249 (dolist (ov ovs
) (when ov
(delete-overlay ov
)))
5250 (delete-region beg end
)))
5252 (defun todo-diary-item-p ()
5253 "Return non-nil if item at point has diary entry format."
5255 (when (todo-item-string) ; Exclude empty lines.
5257 (not (looking-at (regexp-quote todo-nondiary-start
))))))
5259 ;; This duplicates the item locating code from diary-goto-entry, but
5260 ;; without the marker code, to test whether the latter is dispensable.
5261 ;; If it is, diary-goto-entry can be simplified. The code duplication
5262 ;; here can also be eliminated, leaving only the widening and category
5263 ;; selection, and instead of :override advice :around can be used.
5265 (defun todo-diary-goto-entry (button)
5266 "Jump to the diary entry for the BUTTON at point.
5267 If the entry is a todo item, display its category properly.
5268 Overrides `diary-goto-entry'."
5269 ;; Locate the diary item in its source file.
5270 (let* ((locator (button-get button
'locator
))
5271 (file (cadr locator
))
5272 (date (regexp-quote (nth 2 locator
)))
5273 (content (regexp-quote (nth 3 locator
))))
5274 (if (not (and (file-exists-p file
)
5275 (find-file-other-window file
)))
5276 (message "Unable to locate this diary entry")
5277 ;; If it's a Todo file, make sure it's in Todo mode.
5278 (when (and (equal (file-name-directory (file-truename file
))
5279 (file-truename todo-directory
))
5280 (not (derived-mode-p 'todo-mode
)))
5282 (when (eq major-mode
'todo-mode
) (widen))
5283 (goto-char (point-min))
5284 (when (re-search-forward (format "%s.*\\(%s\\)" date content
) nil t
)
5285 (goto-char (match-beginning 1)))
5286 ;; If it's a todo item, determine its category and display the
5287 ;; category properly.
5288 (when (eq major-mode
'todo-mode
)
5289 (let ((opoint (point)))
5290 (re-search-backward (concat "^" (regexp-quote todo-category-beg
)
5293 (todo-category-number (match-string 1))
5294 (todo-category-select)
5295 (goto-char opoint
))))))
5297 (add-function :override diary-goto-entry-function
#'todo-diary-goto-entry
)
5299 (defun todo-revert-buffer (&optional ignore-auto noconfirm
)
5300 "Call `revert-buffer', preserving buffer's current modes.
5301 Also preserve category display, if applicable."
5302 (interactive (list (not current-prefix-arg
)))
5303 (let ((revert-buffer-function nil
))
5304 (revert-buffer ignore-auto noconfirm
'preserve-modes
)
5305 (when (memq major-mode
'(todo-mode todo-archive-mode
))
5306 (save-excursion (todo-category-select))
5307 ;; revert-buffer--default calls after-find-file, which makes
5309 (setq buffer-read-only t
))))
5311 (defun todo-desktop-save-buffer (_dir)
5312 `((catnum .
,(todo-category-number (todo-current-category)))))
5314 (declare-function desktop-restore-file-buffer
"desktop"
5315 (buffer-filename buffer-name buffer-misc
))
5317 (defun todo-restore-desktop-buffer (file buffer misc
)
5318 (desktop-restore-file-buffer file buffer misc
)
5319 (with-current-buffer buffer
5321 (let ((todo-category-number (cdr (assq 'catnum misc
))))
5322 (todo-category-select)
5325 (add-to-list 'desktop-buffer-mode-handlers
5326 '(todo-mode . todo-restore-desktop-buffer
))
5328 (defun todo-done-item-p ()
5329 "Return non-nil if item at point is a done item."
5332 (looking-at todo-done-string-start
)))
5334 (defun todo-done-item-section-p ()
5335 "Return non-nil if point is in category's done items section."
5337 (or (re-search-backward (concat "^" (regexp-quote todo-category-done
))
5339 (progn (goto-char (point-min))
5340 (looking-at todo-done-string-start
)))))
5342 (defun todo--user-error-if-marked-done-item ()
5343 "Signal user error on marked done items.
5344 Helper function for editing commands that apply only to (possibly
5345 marked) not done todo items."
5348 (goto-char (point-max))
5349 (todo-backward-item)
5350 (unless (todo-done-item-p)
5352 (unless (re-search-forward
5353 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
5354 (goto-char (point-max)))
5356 (while (todo-done-item-p)
5357 (when (todo-marked-item-p)
5358 (user-error "This command does not apply to done items"))
5359 (todo-backward-item)))))
5361 (defun todo-reset-done-separator (sep)
5362 "Replace existing overlays of done items separator string SEP."
5366 (goto-char (point-min))
5367 (while (re-search-forward
5368 (concat "\n\\(" (regexp-quote todo-category-done
) "\\)") nil t
)
5369 (let* ((beg (match-beginning 1))
5371 (ov (progn (goto-char beg
)
5372 (todo-get-overlay 'separator
)))
5373 (old-sep (when ov
(overlay-get ov
'display
)))
5376 (unless (string= old-sep sep
)
5377 (setq new-ov
(make-overlay beg end
))
5378 (overlay-put new-ov
'todo
'separator
)
5379 (overlay-put new-ov
'display todo-done-separator
)
5380 (delete-overlay ov
))))))))
5382 (defun todo-get-overlay (val)
5383 "Return the overlay at point whose `todo' property has value VAL."
5385 ;; When headers are hidden, the display engine makes item's start
5386 ;; inaccessible to commands, so then we have to go there
5387 ;; non-interactively to check for prefix and header overlays.
5388 (when (memq val
'(prefix header
))
5389 (unless (looking-at todo-item-start
) (todo-item-start)))
5390 ;; Use overlays-in to find prefix overlays and check over two
5391 ;; positions to find done separator overlay.
5392 (let ((ovs (overlays-in (point) (1+ (point))))
5397 (when (eq (overlay-get ov
'todo
) val
)
5398 (throw 'done ov
)))))))
5400 (defun todo-marked-item-p ()
5401 "Non-nil if this item begins with `todo-item-mark'.
5402 In that case, return the item's prefix overlay."
5403 (let* ((ov (todo-get-overlay 'prefix
))
5404 ;; If an item insertion command is called on a todo file
5405 ;; before it is visited, it has no prefix overlays yet, so
5407 (pref (when ov
(overlay-get ov
'before-string
)))
5409 (string-match (concat "^" (regexp-quote todo-item-mark
))
5413 (defun todo-insert-with-overlays (item)
5414 "Insert ITEM at point and update prefix and header overlays."
5416 (let ((ov (todo-get-overlay 'prefix
))
5417 (marked (todo-marked-item-p)))
5419 ;; Insertion pushes item down but not its prefix overlay. When
5420 ;; the overlay includes a mark, this would now mark the inserted
5421 ;; ITEM, so move it to the pushed down item.
5422 (when marked
(move-overlay ov
(point) (point)))
5423 (todo-backward-item)
5424 ;; With hidden headers, todo-backward-item puts point on first
5425 ;; visible character after header, so we have to search backward.
5426 (when todo--item-headers-hidden
5427 (re-search-backward (concat todo-item-start
5428 "\\( " diary-time-regexp
"\\)?"
5429 (regexp-quote todo-nondiary-end
) "? ")
5431 (setq ov
(make-overlay (match-beginning 0) (match-end 0) nil t
))
5432 (overlay-put ov
'todo
'header
)
5433 (overlay-put ov
'display
"")))
5434 (todo-prefix-overlays))
5436 (defun todo-prefix-overlays ()
5437 "Update the prefix overlays of the current category's items.
5438 The overlay's value is the string `todo-prefix' or with non-nil
5439 `todo-number-prefix' an integer in the sequence from 1 to
5440 the number of todo or done items in the category indicating the
5441 item's priority. Todo and done items are numbered independently
5444 (cat-tp (or (cdr (assoc-string
5445 (todo-current-category)
5446 (nth 2 (assoc-string todo-current-todo-file
5447 todo-top-priorities-overrides
))))
5448 (nth 1 (assoc-string todo-current-todo-file
5449 todo-top-priorities-overrides
))
5450 todo-top-priorities
))
5453 (goto-char (point-min))
5455 (when (or (todo-date-string-matcher (line-end-position))
5456 (todo-done-string-matcher (line-end-position)))
5457 (goto-char (match-beginning 0))
5459 ;; Reset number to 1 for first done item.
5460 (when (and (eq major-mode
'todo-mode
)
5461 (looking-at todo-done-string-start
)
5462 (looking-back (concat "^"
5463 (regexp-quote todo-category-done
)
5465 (line-beginning-position 0)))
5468 (setq prefix
(concat (propertize
5469 (if todo-number-prefix
5470 (number-to-string num
)
5473 ;; Prefix of top priority items has a
5474 ;; distinct face in Todo mode.
5475 (if (and (eq major-mode
'todo-mode
)
5479 'todo-prefix-string
))
5481 (let ((ov (todo-get-overlay 'prefix
))
5482 (marked (todo-marked-item-p)))
5483 ;; Prefix overlay must be at a single position so its
5484 ;; bounds aren't changed when (re)moving an item.
5485 (unless ov
(setq ov
(make-overlay (point) (point))))
5486 (overlay-put ov
'todo
'prefix
)
5487 (overlay-put ov
'before-string
(if marked
5488 (concat todo-item-mark prefix
)
5492 ;; -----------------------------------------------------------------------------
5493 ;;; Generating and applying item insertion and editing key sequences
5494 ;; -----------------------------------------------------------------------------
5496 ;; Thanks to Stefan Monnier for suggesting dynamically generating item
5497 ;; insertion commands and their key bindings, and offering an elegant
5498 ;; implementation, which, however, relies on lexical scoping and so
5499 ;; cannot be used here until the Calendar code used by todo-mode.el is
5500 ;; converted to lexical binding. Hence, the following implementation
5501 ;; uses dynamic binding.
5503 (defconst todo-insert-item--parameters
5504 '((default copy
) (diary nonmarking
) (calendar date dayname
) time
(here region
))
5505 "List of all item insertion parameters.
5506 Passed by `todo-insert-item' to `todo-insert-item--next-param' to
5507 dynamically create item insertion commands.")
5509 (defconst todo-insert-item--param-key-alist
5520 "List pairing item insertion parameters with their completion keys.")
5522 (defsubst todo-insert-item--keyof
(param)
5523 "Return key paired with item insertion PARAM."
5524 (cdr (assoc param todo-insert-item--param-key-alist
)))
5526 (defun todo-insert-item--argsleft (key list
)
5527 "Return sublist of LIST whose first member corresponds to KEY."
5533 (when (equal key
(todo-insert-item--keyof s
))
5534 (throw 'found1
(setq sym s
))))))
5541 (setq list
(reverse l
)))
5542 (memq (catch 'found2
5543 (dolist (e todo-insert-item--param-key-alist
)
5544 (when (equal key
(cdr e
))
5545 (throw 'found2
(car e
)))))
5548 (defsubst todo-insert-item--this-key
() (char-to-string last-command-event
))
5550 (defvar todo-insert-item--keys-so-far
""
5551 "String of item insertion keys so far entered for this command.")
5553 (defvar todo-insert-item--args nil
)
5554 (defvar todo-insert-item--argleft nil
)
5555 (defvar todo-insert-item--argsleft nil
)
5556 (defvar todo-insert-item--newargsleft nil
)
5558 (defun todo-insert-item--apply-args ()
5559 "Build list of arguments for item insertion and apply them.
5560 The list consists of item insertion parameters that can be passed
5561 as insertion command arguments in fixed positions. If a position
5562 in the list is not occupied by the corresponding parameter, it is
5564 (let* ((arg (list (car todo-insert-item--args
)))
5565 (args (nconc (cdr todo-insert-item--args
)
5566 (list (car (todo-insert-item--argsleft
5567 (todo-insert-item--this-key)
5568 todo-insert-item--argsleft
)))))
5569 (arglist (if (= 4 (length args
))
5571 (let ((v (make-vector 4 nil
)) elt
)
5573 (setq elt
(pop args
))
5574 (cond ((memq elt
'(diary nonmarking
))
5576 ((memq elt
'(calendar date dayname
))
5580 ((memq elt
'(copy here region
))
5583 (apply #'todo-insert-item--basic
(nconc arg arglist
))))
5585 (defun todo-insert-item--next-param (last args argsleft
)
5586 "Build item insertion command from LAST, ARGS and ARGSLEFT and call it.
5587 Dynamically generate key bindings, prompting with the keys
5588 already entered and those still available."
5589 (cl-assert argsleft
)
5590 (let* ((map (make-sparse-keymap))
5598 (if (memq name
'(default diary calendar here
))
5601 (when (memq name
'(copy nonmarking dayname region
))
5603 (propertize k
'face
'todo-key-prompt
)
5605 (setq todo-insert-item--args args
)
5606 (setq todo-insert-item--argsleft argsleft
)
5608 (if (memq last
'(default copy
))
5610 (setq todo-insert-item--argsleft nil
)
5611 (todo-insert-item--apply-args))
5612 (let ((k (todo-insert-item--keyof last
)))
5613 (funcall addprompt k
(make-symbol (concat (symbol-name last
) ":GO!")))
5614 (define-key map
(todo-insert-item--keyof last
)
5615 (lambda () (interactive)
5616 (todo-insert-item--apply-args))))))
5617 (while todo-insert-item--argsleft
5618 (let ((x (car todo-insert-item--argsleft
)))
5619 (setq todo-insert-item--newargsleft
(cdr todo-insert-item--argsleft
))
5620 (dolist (argleft (if (consp x
) x
(list x
)))
5621 (let ((k (todo-insert-item--keyof argleft
)))
5622 (funcall addprompt k argleft
)
5624 (if (null todo-insert-item--newargsleft
)
5625 (lambda () (interactive)
5626 (todo-insert-item--apply-args))
5627 (lambda () (interactive)
5628 (setq todo-insert-item--keys-so-far
5629 (concat todo-insert-item--keys-so-far
" "
5630 (todo-insert-item--this-key)))
5631 (todo-insert-item--next-param
5632 (car (todo-insert-item--argsleft
5633 (todo-insert-item--this-key)
5634 todo-insert-item--argsleft
))
5635 (nconc todo-insert-item--args
5636 (list (car (todo-insert-item--argsleft
5637 (todo-insert-item--this-key)
5638 todo-insert-item--argsleft
))))
5639 (cdr (todo-insert-item--argsleft
5640 (todo-insert-item--this-key)
5641 todo-insert-item--argsleft
)))))))))
5642 (setq todo-insert-item--argsleft todo-insert-item--newargsleft
))
5643 (when prompt
(message "Press a key (so far `%s'): %s"
5644 todo-insert-item--keys-so-far prompt
))
5645 (set-transient-map map
)
5646 (setq todo-insert-item--argsleft argsleft
)))
5648 (defconst todo-edit-item--param-key-alist
5656 "Alist of item editing parameters and their keys.")
5658 (defconst todo-edit-item--date-param-key-alist
5666 "Alist of item date editing parameters and their keys.")
5668 (defconst todo-edit-done-item--param-key-alist
5671 "Alist of done item comment editing parameters and their keys.")
5673 (defvar todo-edit-item--prompt
"Press a key (so far `e'): ")
5675 (defun todo-edit-item--next-key (params &optional arg
)
5676 (let* ((p->k
(mapconcat (lambda (elt)
5678 (propertize (cdr elt
) 'face
5680 (concat (symbol-name (car elt
))
5681 (when (memq (car elt
)
5685 (key-prompt (substitute-command-keys todo-edit-item--prompt
))
5686 (this-key (let ((key (read-key (concat key-prompt p-
>k
))))
5687 (and (characterp key
) (char-to-string key
))))
5688 (this-param (car (rassoc this-key params
))))
5690 (`edit
(todo-edit-item--text))
5691 (`header
(todo-edit-item--text 'include-header
))
5692 (`multiline
(todo-edit-item--text 'multiline
))
5693 (`add
/edit
(todo-edit-item--text 'comment-edit
))
5694 (`delete
(todo-edit-item--text 'comment-delete
))
5695 (`diary
(todo-edit-item--diary-inclusion))
5696 (`nonmarking
(todo-edit-item--diary-inclusion 'nonmarking
))
5697 (`date
(let ((todo-edit-item--prompt "Press a key (so far `e d'): "))
5698 (todo-edit-item--next-key
5699 todo-edit-item--date-param-key-alist arg
)))
5700 (`full
(progn (todo-edit-item--header 'date
)
5701 (when todo-always-add-time-string
5702 (todo-edit-item--header 'time
))))
5703 (`calendar
(todo-edit-item--header 'calendar
))
5704 (`today
(todo-edit-item--header 'today
))
5705 (`dayname
(todo-edit-item--header 'dayname
))
5706 (`year
(todo-edit-item--header 'year arg
))
5707 (`month
(todo-edit-item--header 'month arg
))
5708 (`daynum
(todo-edit-item--header 'day arg
))
5709 (`time
(todo-edit-item--header 'time
)))))
5711 ;; -----------------------------------------------------------------------------
5712 ;;; Todo minibuffer utilities
5713 ;; -----------------------------------------------------------------------------
5715 (defcustom todo-y-with-space nil
5716 "Non-nil means allow SPC to affirm a \"y or n\" question."
5720 (defun todo-y-or-n-p (prompt)
5721 "Ask \"y or n\" question PROMPT and return t if answer is \"y\".
5722 Also return t if answer is \"Y\", but unlike `y-or-n-p', allow
5723 SPC to affirm the question only if option `todo-y-with-space' is
5725 (unless todo-y-with-space
5726 (define-key query-replace-map
" " 'ignore
))
5729 (define-key query-replace-map
" " 'act
)))
5731 (defun todo-category-completions (&optional archive
)
5732 "Return a list of completions for `todo-read-category'.
5733 Each element of the list is a cons of a category name and the
5734 file or list of files (as short file names) it is in. The files
5735 are either the current (or if there is none, the default) todo
5736 file plus the files listed in `todo-category-completions-files',
5737 or, with non-nil ARCHIVE, the current archive file.
5739 Before calculating the completions, update the value of
5740 `todo-category-completions-files' in case any files named in it
5743 (dolist (f todo-category-completions-files
)
5744 (unless (file-exists-p (todo-absolute-file-name f
))
5745 (setq todo-category-completions-files
5746 (delete f todo-category-completions-files
))
5749 (let ((pl (> (length deleted
) 1))
5750 (names (mapconcat (lambda (f) (concat "\"" f
"\"")) deleted
", ")))
5751 (message (concat "File" (if pl
"s" "") " %s ha" (if pl
"ve" "s")
5752 " been deleted and removed from\n"
5753 "the list of category completion files")
5755 (put 'todo-category-completions-files
'custom-type
5756 `(set ,@(todo--files-type-list)))
5757 (custom-set-default 'todo-category-completions-files
5758 (symbol-value 'todo-category-completions-files
))
5760 (let* ((curfile (or todo-current-todo-file
5761 (and todo-show-current-file
5762 todo-global-current-todo-file
)
5763 (todo-absolute-file-name todo-default-todo-file
)))
5764 (files (or (unless archive
5765 (mapcar #'todo-absolute-file-name
5766 todo-category-completions-files
))
5769 ;; If file was just added, it has no category completions.
5770 (unless (zerop (buffer-size (find-buffer-visiting curfile
)))
5771 (unless (member curfile todo-archives
)
5772 (cl-pushnew curfile files
:test
#'equal
))
5773 (dolist (f files listall
)
5774 (with-current-buffer (find-file-noselect f
'nowarn
)
5776 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
5777 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
5778 ;; Ensure category is properly displayed in case user
5779 ;; switches to file via a non-Todo mode command. And if
5780 ;; done items in category are visible, keep them visible.
5781 (let ((done todo-show-with-done
))
5782 (when (> (buffer-size) (- (point-max) (point-min)))
5784 (goto-char (point-min))
5785 (setq done
(re-search-forward todo-done-string-start nil t
))))
5786 (let ((todo-show-with-done done
))
5787 (save-excursion (todo-category-select))))
5791 (goto-char (point-min))
5792 (setq listf
(read (buffer-substring-no-properties
5793 (line-beginning-position)
5794 (line-end-position)))))))
5795 (mapc (lambda (elt) (let* ((cat (car elt
))
5796 (la-elt (assoc cat listall
)))
5798 (setcdr la-elt
(append (list (cdr la-elt
))
5800 (push (cons cat f
) listall
))))
5803 (defun todo-read-file-name (prompt &optional archive mustmatch
)
5804 "Choose and return the name of a todo file, prompting with PROMPT.
5806 Show completions with TAB or SPC; the names are shown in short
5807 form but the absolute truename is returned. With non-nil ARCHIVE
5808 return the absolute truename of a todo archive file. With non-nil
5809 MUSTMATCH the name of an existing file must be chosen;
5810 otherwise, a new file name is allowed."
5811 (let* ((completion-ignore-case todo-completion-ignore-case
)
5812 (files (mapcar #'todo-short-file-name
5813 ;; (funcall todo-files-function archive)))
5814 (if archive todo-archives todo-files
)))
5815 (file (completing-read prompt files nil mustmatch nil nil
5817 ;; If user hit RET without
5818 ;; choosing a file, default to
5819 ;; current or default file.
5820 (todo-short-file-name
5821 (or todo-current-todo-file
5822 (and todo-show-current-file
5823 todo-global-current-todo-file
)
5824 (todo-absolute-file-name
5825 todo-default-todo-file
)))
5826 ;; Trigger prompt for initial file.
5828 (unless (file-exists-p todo-directory
)
5829 (make-directory todo-directory
))
5830 (unless (or mustmatch
(member file files
))
5831 (setq file
(todo-validate-name file
'file
)))
5832 (setq file
(file-truename (concat todo-directory file
5833 (if archive
".toda" ".todo"))))))
5835 (defun todo-read-category (prompt &optional match-type file
)
5836 "Choose and return a category name, prompting with PROMPT.
5837 Show completions for existing categories with TAB or SPC.
5839 The argument MATCH-TYPE specifies the matching requirements on
5840 the category name: with the value `todo' or `archive' the name
5841 must complete to that of an existing todo or archive category,
5842 respectively; with the value `add' the name must not be that of
5843 an existing category; with all other values both existing and new
5844 valid category names are accepted.
5846 With non-nil argument FILE prompt for a file and complete only
5847 against categories in that file; otherwise complete against all
5848 categories from `todo-category-completions-files'."
5849 ;; Allow SPC to insert spaces, for adding new category names.
5850 (let ((minibuffer-local-completion-map
5851 (let ((map (make-sparse-keymap)))
5852 (set-keymap-parent map minibuffer-local-completion-map
)
5853 (define-key map
" " nil
)
5855 (let* ((add (eq match-type
'add
))
5856 (archive (eq match-type
'archive
))
5857 (file0 (when (and file
(> (length todo-files
) 1))
5858 (todo-read-file-name (concat "Choose a" (if archive
5863 (completions (unless file0
(todo-category-completions archive
)))
5864 (categories (cond (file0
5865 (with-current-buffer
5866 (find-file-noselect file0
'nowarn
)
5867 (unless (derived-mode-p 'todo-mode
) (todo-mode))
5868 (let ((todo-current-todo-file file0
))
5870 ((and add
(not file
))
5871 (with-current-buffer
5872 (find-file-noselect todo-current-todo-file
)
5876 (completion-ignore-case todo-completion-ignore-case
)
5877 (cat (completing-read prompt categories nil
5878 (eq match-type
'todo
) nil nil
5879 ;; Unless we're adding a category via
5880 ;; todo-add-category, set default
5881 ;; for existing categories to the
5882 ;; current category of the chosen
5883 ;; file or else of the current file.
5884 (if (and categories
(not add
))
5885 (with-current-buffer
5888 todo-current-todo-file
5889 (todo-absolute-file-name
5890 todo-default-todo-file
)))
5891 (todo-current-category))
5892 ;; Trigger prompt for initial category.
5894 (catfil (cdr (assoc cat completions
)))
5895 (str "Category \"%s\" from which file (TAB for choices)? "))
5896 ;; If we do category completion and the chosen category name
5897 ;; occurs in more than one file, prompt to choose one file.
5898 (unless (or file0 add
(not catfil
))
5899 (setq file0
(file-truename
5902 (todo-absolute-file-name
5903 (let ((files (mapcar #'todo-short-file-name catfil
)))
5904 (completing-read (format str cat
) files
)))))))
5905 ;; Default to the current file.
5906 (unless file0
(setq file0 todo-current-todo-file
))
5907 ;; First validate only a name passed interactively from
5908 ;; todo-add-category, which must be of a nonexistent category.
5909 (unless (and (assoc cat categories
) (not add
))
5910 ;; Validate only against completion categories.
5911 (let ((todo-categories categories
))
5912 (setq cat
(todo-validate-name cat
'category
)))
5913 ;; When user enters a nonexistent category name by jumping or
5914 ;; moving, confirm that it should be added, then validate.
5916 (if (todo-y-or-n-p (format "Add new category \"%s\" to file \"%s\"? "
5917 cat
(todo-short-file-name file0
)))
5919 (when (assoc cat categories
)
5920 (let ((todo-categories categories
))
5921 (setq cat
(todo-validate-name cat
'category
))))
5922 ;; Restore point and narrowing after adding new
5923 ;; category, to avoid moving to beginning of file when
5924 ;; moving marked items to a new category
5925 ;; (todo-move-item).
5928 (todo-add-category file0 cat
))))
5929 ;; If we decide not to add a category, exit without returning.
5933 (defun todo-validate-name (name type
)
5934 "Prompt for new NAME for TYPE until it is valid, then return it.
5935 TYPE can be either of the symbols `file' or `category'."
5936 (let ((categories todo-categories
)
5937 (files (mapcar #'todo-short-file-name todo-files
))
5941 (cond ((string= "" name
)
5943 (cond ((eq type
'file
)
5945 "Enter a non-empty file name: "
5946 ;; Empty string passed by todo-show to
5947 ;; prompt for initial todo file.
5948 (concat "Initial file name ["
5949 todo-initial-file
"]: ")))
5950 ((eq type
'category
)
5952 "Enter a non-empty category name: "
5953 ;; Empty string passed by todo-show to
5954 ;; prompt for initial category of a new
5956 (concat "Initial category name ["
5957 todo-initial-category
"]: "))))))
5958 ((string-match "\\`\\s-+\\'" name
)
5960 "Enter a name that does not contain only white space: "))
5961 ((and (eq type
'file
) (member name files
))
5962 (setq prompt
"Enter a non-existing file name: "))
5963 ((and (eq type
'category
) (assoc name categories
))
5964 (setq prompt
"Enter a non-existing category name: ")))
5965 (setq name
(if (or (and (eq type
'file
) files
)
5966 (and (eq type
'category
) categories
))
5967 (completing-read prompt
(cond ((eq type
'file
)
5969 ((eq type
'category
)
5971 ;; Offer default initial name.
5972 (completing-read prompt
(if (eq type
'file
)
5975 nil nil
(if (eq type
'file
)
5977 todo-initial-category
))))))
5980 ;; Adapted from calendar-read-date and calendar-date-string.
5981 (defun todo-read-date (&optional arg mo yr
)
5982 "Prompt for Gregorian date and return it in the current format.
5984 With non-nil ARG, prompt for and return only the date component
5985 specified by ARG, which can be one of these symbols:
5986 `month' (prompt for name, return name or number according to
5987 value of `calendar-date-display-form'), `day' of month, or
5988 `year'. The value of each of these components can be `*',
5989 indicating an unspecified month, day, or year.
5991 When ARG is `day', non-nil arguments MO and YR determine the
5992 number of the last the day of the month."
5993 (let (year monthname month day
5994 dayname
) ; Needed by calendar-date-display-form.
5995 (when (or (not arg
) (eq arg
'year
))
5996 (while (if (natnump year
) (< year
1) (not (eq year
'*)))
5997 (setq year
(read-from-minibuffer
5998 "Year (>0 or RET for this year or * for any year): "
5999 nil nil t nil
(number-to-string
6000 (calendar-extract-year
6001 (calendar-current-date)))))))
6002 (when (or (not arg
) (eq arg
'month
))
6003 (let* ((marray todo-month-name-array
)
6004 (mlist (append marray nil
))
6005 (mabarray todo-month-abbrev-array
)
6006 (mablist (append mabarray nil
))
6007 (completion-ignore-case todo-completion-ignore-case
))
6008 (setq monthname
(completing-read
6009 "Month name (RET for current month, * for any month): "
6011 (calendar-month-name
6012 (calendar-extract-month (calendar-current-date)) t
))
6013 month
(1+ (- (length mlist
)
6014 (length (or (member monthname mlist
)
6015 (member monthname mablist
))))))
6016 (setq monthname
(aref mabarray
(1- month
)))))
6017 (when (or (not arg
) (eq arg
'day
))
6018 (let ((last (let ((mm (or month mo
))
6020 ;; If month is unspecified, use a month with 31
6021 ;; days for checking day of month input. Does
6022 ;; Calendar do anything special when * is
6023 ;; currently a shorter month?
6024 (if (= mm
13) (setq mm
1))
6025 ;; If year is unspecified, use a leap year to
6027 (if (eq year
'*) (setq yy
2012))
6028 (calendar-last-day-of-month mm yy
))))
6029 (while (if (natnump day
) (or (< day
1) (> day last
)) (not (eq day
'*)))
6030 (setq day
(read-from-minibuffer
6031 (format "Day (1-%d or RET for today or * for any day): "
6033 nil nil t nil
(number-to-string
6034 (calendar-extract-day
6035 (calendar-current-date))))))))
6036 ;; Stringify read values (monthname is already a string).
6037 (and year
(setq year
(if (eq year
'*)
6039 (number-to-string year
))))
6040 (and day
(setq day
(if (eq day
'*)
6042 (number-to-string day
))))
6043 (and month
(setq month
(if (= month
13)
6045 (number-to-string month
))))
6047 (cond ((eq arg
'year
) year
)
6050 (if (memq 'month calendar-date-display-form
)
6053 (mapconcat #'eval calendar-date-display-form
""))))
6055 (defun todo-read-dayname ()
6056 "Choose name of a day of the week with completion and return it."
6057 (let ((completion-ignore-case todo-completion-ignore-case
))
6058 (completing-read "Enter a day name: "
6059 (append calendar-day-name-array nil
)
6062 (defun todo-read-time ()
6063 "Prompt for and return a valid clock time as a string.
6065 Valid time strings are those matching `diary-time-regexp'.
6066 Typing `<return>' at the prompt returns the current time, if the
6067 user option `todo-always-add-time-string' is non-nil, otherwise
6068 the empty string (i.e., no time string)."
6071 (setq answer
(read-string "Enter a clock time: " nil nil
6072 (when todo-always-add-time-string
6073 (substring (current-time-string) 11 16))))
6074 (when (or (string= "" answer
)
6075 (string-match diary-time-regexp answer
))
6079 ;; -----------------------------------------------------------------------------
6080 ;;; Customization groups and utilities
6081 ;; -----------------------------------------------------------------------------
6084 "Create and maintain categorized lists of todo items."
6085 :link
'(emacs-commentary-link "todo")
6089 (defgroup todo-edit nil
6090 "User options for adding and editing todo items."
6094 (defgroup todo-categories nil
6095 "User options for Todo Categories mode."
6099 (defgroup todo-filtered nil
6100 "User options for Todo Filter Items mode."
6104 (defgroup todo-display nil
6105 "User display options for Todo mode."
6109 (defgroup todo-faces nil
6110 "Faces for the Todo modes."
6114 (defun todo-set-show-current-file (symbol value
)
6115 "The :set function for user option `todo-show-current-file'."
6116 (custom-set-default symbol value
)
6118 (add-hook 'pre-command-hook
#'todo-show-current-file nil t
)
6119 (remove-hook 'pre-command-hook
#'todo-show-current-file t
)))
6121 (defun todo-reset-prefix (symbol value
)
6122 "The :set function for `todo-prefix' and `todo-number-prefix'."
6123 (let ((oldvalue (symbol-value symbol
))
6124 (files todo-file-buffers
))
6125 (custom-set-default symbol value
)
6126 (when (not (equal value oldvalue
))
6128 (with-current-buffer (find-file-noselect f
)
6129 ;; Activate the new setting in the current category.
6130 (save-excursion (todo-category-select)))))))
6132 (defun todo-reset-nondiary-marker (symbol value
)
6133 "The :set function for user option `todo-nondiary-marker'."
6134 (let* ((oldvalue (symbol-value symbol
))
6135 (files (append todo-files todo-archives
6136 (directory-files todo-directory t
"\\.tod[rty]$" t
))))
6137 (custom-set-default symbol value
)
6138 ;; Need to reset these to get font-locking right.
6139 (setq todo-nondiary-start
(nth 0 todo-nondiary-marker
)
6140 todo-nondiary-end
(nth 1 todo-nondiary-marker
)
6141 todo-date-string-start
6142 ;; See comment in defvar of `todo-date-string-start'.
6143 (concat "^\\(" (regexp-quote todo-nondiary-start
) "\\|"
6144 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
6145 (when (not (equal value oldvalue
))
6147 (let ((buf (find-buffer-visiting f
)))
6148 (with-current-buffer (find-file-noselect f
)
6149 (let (buffer-read-only)
6151 (goto-char (point-min))
6153 (if (re-search-forward
6154 (concat "^\\(" todo-done-string-start
"[^][]+] \\)?"
6155 "\\(?1:" (regexp-quote (car oldvalue
))
6156 "\\)" todo-date-pattern
"\\( "
6157 diary-time-regexp
"\\)?\\(?2:"
6158 (regexp-quote (cadr oldvalue
)) "\\)")
6161 (replace-match (nth 0 value
) t t nil
1)
6162 (replace-match (nth 1 value
) t t nil
2))
6165 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6166 (todo-category-select))
6168 (kill-buffer)))))))))
6170 (defun todo-reset-done-separator-string (symbol value
)
6171 "The :set function for `todo-done-separator-string'."
6172 (let ((oldvalue (symbol-value symbol
))
6173 (files todo-file-buffers
)
6174 (sep todo-done-separator
))
6175 (custom-set-default symbol value
)
6176 (when (not (equal value oldvalue
))
6178 (with-current-buffer (find-file-noselect f
)
6179 (let (buffer-read-only)
6180 (setq todo-done-separator
(todo-done-separator))
6181 (when (= 1 (length value
))
6182 (todo-reset-done-separator sep
)))
6183 (todo-category-select))))))
6185 (defun todo-reset-done-string (symbol value
)
6186 "The :set function for user option `todo-done-string'."
6187 (let ((oldvalue (symbol-value symbol
))
6188 (files (append todo-files todo-archives
6189 (directory-files todo-directory t
"\\.todr$" t
))))
6190 (custom-set-default symbol value
)
6191 ;; Need to reset this to get font-locking right.
6192 (setq todo-done-string-start
6193 (concat "^\\[" (regexp-quote todo-done-string
)))
6194 (when (not (equal value oldvalue
))
6196 (let ((buf (find-buffer-visiting f
)))
6197 (with-current-buffer (find-file-noselect f
)
6198 (let (buffer-read-only)
6200 (goto-char (point-min))
6202 (if (re-search-forward
6203 (concat "^" (regexp-quote todo-nondiary-start
)
6204 "\\(" (regexp-quote oldvalue
) "\\)")
6206 (replace-match value t t nil
1)
6209 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6210 (todo-category-select))
6212 (kill-buffer)))))))))
6214 (defun todo-reset-comment-string (symbol value
)
6215 "The :set function for user option `todo-comment-string'."
6216 (let ((oldvalue (symbol-value symbol
))
6217 (files (append todo-files todo-archives
6218 (directory-files todo-directory t
"\\.todr$" t
))))
6219 (custom-set-default symbol value
)
6220 (when (not (equal value oldvalue
))
6222 (let ((buf (find-buffer-visiting f
)))
6223 (with-current-buffer (find-file-noselect f
)
6224 (let (buffer-read-only)
6226 (goto-char (point-min))
6228 (if (re-search-forward
6229 (concat "\\[\\(" (regexp-quote oldvalue
)
6232 (replace-match value t t nil
1)
6235 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6236 (todo-category-select))
6238 (kill-buffer)))))))))
6240 (defun todo-reset-highlight-item (symbol value
)
6241 "The :set function for user option `todo-highlight-item'."
6242 (let ((oldvalue (symbol-value symbol
))
6243 (files (append todo-files todo-archives
6244 (directory-files todo-directory t
"\\.tod[rty]$" t
))))
6245 (custom-set-default symbol value
)
6246 (when (not (equal value oldvalue
))
6248 (let ((buf (find-buffer-visiting f
)))
6250 (with-current-buffer buf
6254 (hl-line-mode -
1)))))))))
6256 (defun todo-update-filelist-defcustoms ()
6257 "Update defcustoms that provide choice list of todo files."
6258 (put 'todo-default-todo-file
'custom-type
`(radio ,@(todo--files-type-list)))
6259 (put 'todo-category-completions-files
'custom-type
6260 `(set ,@(todo--files-type-list)))
6261 (put 'todo-filter-files
'custom-type
`(set ,@(todo--files-type-list))))
6263 ;; -----------------------------------------------------------------------------
6265 ;; -----------------------------------------------------------------------------
6267 (defun todo-nondiary-marker-matcher (lim)
6268 "Search for todo item nondiary markers within LIM for font-locking."
6269 (re-search-forward (concat "^\\(?1:" (regexp-quote todo-nondiary-start
) "\\)"
6270 todo-date-pattern
"\\(?: " diary-time-regexp
6271 "\\)?\\(?2:" (regexp-quote todo-nondiary-end
) "\\)")
6274 (defun todo-diary-nonmarking-matcher (lim)
6275 "Search for diary nonmarking symbol within LIM for font-locking."
6276 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
6277 "\\)" todo-date-pattern
)
6280 (defun todo-date-string-matcher (lim)
6281 "Search for todo item date string within LIM for font-locking."
6283 (concat todo-date-string-start
"\\(?1:" todo-date-pattern
"\\)") lim t
))
6285 (defun todo-time-string-matcher (lim)
6286 "Search for todo item time string within LIM for font-locking."
6287 (re-search-forward (concat todo-date-string-start todo-date-pattern
6288 " \\(?1:" diary-time-regexp
"\\)")
6291 (defun todo-diary-expired-matcher (lim)
6292 "Search for expired diary item date within LIM for font-locking."
6293 (when (re-search-forward (concat "^\\(?:"
6294 (regexp-quote diary-nonmarking-symbol
)
6295 "\\)?\\(?1:" todo-date-pattern
"\\) \\(?2:"
6296 diary-time-regexp
"\\)?")
6298 (let* ((date (match-string-no-properties 1))
6299 (time (match-string-no-properties 2))
6300 ;; Function days-between requires a non-empty time string.
6301 (date-time (concat date
" " (or time
"00:00"))))
6302 (or (and (not (string-match ".+day\\|\\*" date
))
6303 (< (days-between date-time
(current-time-string)) 0))
6304 (todo-diary-expired-matcher lim
)))))
6306 (defun todo-done-string-matcher (lim)
6307 "Search for done todo item header within LIM for font-locking."
6308 (re-search-forward (concat todo-done-string-start
6312 (defun todo-comment-string-matcher (lim)
6313 "Search for done todo item comment within LIM for font-locking."
6314 (re-search-forward (concat "\\[\\(?1:" todo-comment-string
"\\):")
6317 (defun todo-category-string-matcher-1 (lim)
6318 "Search for todo category name within LIM for font-locking.
6319 This is for fontifying category and file names appearing in Todo
6320 Filtered Items mode following done items."
6321 (if (eq major-mode
'todo-filtered-items-mode
)
6322 (re-search-forward (concat todo-done-string-start todo-date-pattern
6323 "\\(?: " diary-time-regexp
6324 ;; Use non-greedy operator to prevent
6325 ;; capturing possible following non-diary
6327 "\\)?] \\(?1:\\[.+?\\]\\)")
6330 (defun todo-category-string-matcher-2 (lim)
6331 "Search for todo category name within LIM for font-locking.
6332 This is for fontifying category and file names appearing in Todo
6333 Filtered Items mode following todo (not done) items."
6334 (if (eq major-mode
'todo-filtered-items-mode
)
6335 (re-search-forward (concat todo-date-string-start todo-date-pattern
6336 "\\(?: " diary-time-regexp
"\\)?\\(?:"
6337 (regexp-quote todo-nondiary-end
)
6338 "\\)? \\(?1:\\[.+\\]\\)")
6341 (defvar todo-nondiary-face
'todo-nondiary
)
6342 (defvar todo-date-face
'todo-date
)
6343 (defvar todo-time-face
'todo-time
)
6344 (defvar todo-diary-expired-face
'todo-diary-expired
)
6345 (defvar todo-done-sep-face
'todo-done-sep
)
6346 (defvar todo-done-face
'todo-done
)
6347 (defvar todo-comment-face
'todo-comment
)
6348 (defvar todo-category-string-face
'todo-category-string
)
6349 (defvar todo-font-lock-keywords
6351 '(todo-nondiary-marker-matcher 1 todo-nondiary-face t
)
6352 '(todo-nondiary-marker-matcher 2 todo-nondiary-face t
)
6353 ;; diary-lib.el uses font-lock-constant-face for diary-nonmarking-symbol.
6354 '(todo-diary-nonmarking-matcher 1 font-lock-constant-face t
)
6355 '(todo-date-string-matcher 1 todo-date-face t
)
6356 '(todo-time-string-matcher 1 todo-time-face t
)
6357 '(todo-done-string-matcher 0 todo-done-face t
)
6358 '(todo-comment-string-matcher 1 todo-comment-face t
)
6359 '(todo-category-string-matcher-1 1 todo-category-string-face t t
)
6360 '(todo-category-string-matcher-2 1 todo-category-string-face t t
)
6361 '(todo-diary-expired-matcher 1 todo-diary-expired-face t
)
6362 '(todo-diary-expired-matcher 2 todo-diary-expired-face t t
)
6364 "Font-locking for Todo modes.")
6366 ;; -----------------------------------------------------------------------------
6368 ;; -----------------------------------------------------------------------------
6370 (defvar todo-key-bindings-t
6372 ("Af" todo-find-archive
)
6373 ("Ac" todo-choose-archive
)
6374 ("Ad" todo-archive-done-item
)
6375 ("Cv" todo-toggle-view-done-items
)
6376 ("v" todo-toggle-view-done-items
)
6377 ("Ca" todo-add-category
)
6378 ("Cr" todo-rename-category
)
6379 ("Cg" todo-merge-category
)
6380 ("Cm" todo-move-category
)
6381 ("Ck" todo-delete-category
)
6382 ("Cts" todo-set-top-priorities-in-category
)
6383 ("Cey" todo-edit-category-diary-inclusion
)
6384 ("Cek" todo-edit-category-diary-nonmarking
)
6385 ("Fa" todo-add-file
)
6386 ("Fr" todo-rename-file
)
6387 ("Ff" todo-find-filtered-items-file
)
6388 ("FV" todo-toggle-view-done-only
)
6389 ("V" todo-toggle-view-done-only
)
6390 ("Ftt" todo-filter-top-priorities
)
6391 ("Ftm" todo-filter-top-priorities-multifile
)
6392 ("Fts" todo-set-top-priorities-in-file
)
6393 ("Fyy" todo-filter-diary-items
)
6394 ("Fym" todo-filter-diary-items-multifile
)
6395 ("Fxx" todo-filter-regexp-items
)
6396 ("Fxm" todo-filter-regexp-items-multifile
)
6397 ("e" todo-edit-item
)
6398 ("d" todo-item-done
)
6399 ("i" todo-insert-item
)
6400 ("k" todo-delete-item
)
6401 ("m" todo-move-item
)
6402 ("u" todo-item-undone
)
6403 ([remap newline
] newline-and-indent
)
6405 "List of key bindings for Todo mode only.")
6407 (defvar todo-key-bindings-t
+a
+f
6409 ("C*" todo-mark-category
)
6410 ("Cu" todo-unmark-category
)
6411 ("Fh" todo-toggle-item-header
)
6412 ("h" todo-toggle-item-header
)
6413 ("Fk" todo-delete-file
)
6414 ("Fe" todo-edit-file
)
6415 ("FH" todo-toggle-item-highlighting
)
6416 ("H" todo-toggle-item-highlighting
)
6417 ("FN" todo-toggle-prefix-numbers
)
6418 ("N" todo-toggle-prefix-numbers
)
6419 ("PB" todo-print-buffer
)
6420 ("PF" todo-print-buffer-to-file
)
6421 ("b" todo-backward-category
)
6422 ("d" todo-item-done
)
6423 ("f" todo-forward-category
)
6424 ("j" todo-jump-to-category
)
6425 ("n" todo-next-item
)
6426 ("p" todo-previous-item
)
6431 "List of key bindings for Todo, Archive, and Filtered Items modes.")
6433 (defvar todo-key-bindings-t
+a
6435 ("Fc" todo-show-categories-table
)
6437 ("X" todo-clear-matches
)
6438 ("*" todo-toggle-mark-item
)
6440 "List of key bindings for Todo and Todo Archive modes.")
6442 (defvar todo-key-bindings-t
+f
6444 ("l" todo-lower-item-priority
)
6445 ("r" todo-raise-item-priority
)
6446 ("#" todo-set-item-priority
)
6448 "List of key bindings for Todo and Todo Filtered Items modes.")
6450 (defvar todo-mode-map
6451 (let ((map (make-keymap)))
6452 (dolist (kb todo-key-bindings-t
)
6453 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6454 (dolist (kb todo-key-bindings-t
+a
+f
)
6455 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6456 (dolist (kb todo-key-bindings-t
+a
)
6457 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6458 (dolist (kb todo-key-bindings-t
+f
)
6459 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6461 "Todo mode keymap.")
6463 (defvar todo-archive-mode-map
6464 (let ((map (make-sparse-keymap)))
6465 (dolist (kb todo-key-bindings-t
+a
+f
)
6466 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6467 (dolist (kb todo-key-bindings-t
+a
)
6468 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6469 (define-key map
"a" 'todo-jump-to-archive-category
)
6470 (define-key map
"u" 'todo-unarchive-items
)
6472 "Todo Archive mode keymap.")
6474 (defvar todo-edit-mode-map
6475 (let ((map (make-sparse-keymap)))
6476 (define-key map
"\C-x\C-q" 'todo-edit-quit
)
6477 (define-key map
[remap newline
] 'newline-and-indent
)
6479 "Todo Edit mode keymap.")
6481 (defvar todo-categories-mode-map
6482 (let ((map (make-sparse-keymap)))
6483 (define-key map
"c" 'todo-sort-categories-alphabetically-or-numerically
)
6484 (define-key map
"t" 'todo-sort-categories-by-todo
)
6485 (define-key map
"y" 'todo-sort-categories-by-diary
)
6486 (define-key map
"d" 'todo-sort-categories-by-done
)
6487 (define-key map
"a" 'todo-sort-categories-by-archived
)
6488 (define-key map
"#" 'todo-set-category-number
)
6489 (define-key map
"l" 'todo-lower-category
)
6490 (define-key map
"r" 'todo-raise-category
)
6491 (define-key map
"n" 'todo-next-button
)
6492 (define-key map
"p" 'todo-previous-button
)
6493 (define-key map
[tab] 'todo-next-button)
6494 (define-key map [backtab] 'todo-previous-button)
6495 (define-key map "q" 'todo-quit)
6497 "Todo Categories mode keymap.")
6499 (defvar todo-filtered-items-mode-map
6500 (let ((map (make-sparse-keymap)))
6501 (dolist (kb todo-key-bindings-t+a+f)
6502 (define-key map (nth 0 kb) (nth 1 kb)))
6503 (dolist (kb todo-key-bindings-t+f)
6504 (define-key map (nth 0 kb) (nth 1 kb)))
6505 (define-key map "g" 'todo-go-to-source-item)
6506 (define-key map [remap newline] 'todo-go-to-source-item)
6508 "Todo Filtered Items mode keymap.")
6511 todo-menu todo-mode-map "Todo Menu"
6514 ["Next Item" todo-next-item t]
6515 ["Previous Item" todo-previous-item t]
6517 ["Next Category" todo-forward-category t]
6518 ["Previous Category" todo-backward-category t]
6519 ["Jump to Another Category" todo-jump-to-category t]
6521 ["Visit Another Todo File" todo-show t]
6522 ["Visit Archive" todo-find-archive t]
6523 ["Visit Filtered Items File" todo-find-filtered-items-file t]
6526 ["Insert New Item" todo-insert-item t]
6527 ["Edit Item" todo-edit-item t]
6528 ["Lower Item Priority" todo-lower-item-priority t]
6529 ["Raise Item Priority" todo-raise-item-priority t]
6530 ["Set Item Priority" todo-set-item-priority t]
6531 ["Mark/Unmark Item" todo-toggle-mark-item t]
6532 ["Move (Recategorize) Item" todo-move-item t]
6533 ["Delete Item" todo-delete-item t]
6534 ["Mark and Bury Done Item" todo-item-done t]
6535 ["Undo Done Item" todo-item-undone t]
6536 ["Archive Done Item" todo-archive-done-item t]
6538 ["Add New Category" todo-add-category t]
6539 ["Rename Current Category" todo-rename-category t]
6540 ["Delete Current Category" todo-delete-category t]
6541 ["Move Current Category" todo-move-category t]
6542 ["Merge Current Category" todo-merge-category t]
6544 ["Add New Todo File" todo-add-file t]
6545 ["Rename Todo File" todo-rename-file t]
6546 ["Delete Todo File" todo-delete-file t]
6547 ["Edit Todo File" todo-edit-file t]
6549 ("Searching and Item Filtering"
6550 ["Search Todo File" todo-search t]
6551 ["Clear Match Highlighting" todo-clear-matches t]
6553 ["Set Top Priorities in File" todo-set-top-priorities-in-file t]
6554 ["Set Top Priorities in Category" todo-set-top-priorities-in-category t]
6555 ["Filter Top Priorities" todo-filter-top-priorities t]
6556 ["Filter Multifile Top Priorities" todo-filter-top-priorities-multifile t]
6557 ["Filter Diary Items" todo-filter-diary-items t]
6558 ["Filter Multifile Diary Items" todo-filter-diary-items-multifile t]
6559 ["Filter Regexp" todo-filter-regexp-items t]
6560 ["Filter Multifile Regexp" todo-filter-regexp-items-multifile t]
6562 ("Display and Printing"
6563 ["Show/Hide Done Items" todo-toggle-view-done-items t]
6564 ["Show/Hide Done Items Only" todo-toggle-view-done-only t]
6565 ["Show/Hide Item Highlighting" todo-toggle-item-highlighting t]
6566 ["Show/Hide Item Numbering" todo-toggle-prefix-numbers t]
6567 ["Show/Hide Item Header" todo-toggle-item-header t]
6569 ["Display Table of Categories" todo-show-categories-table t]
6571 ["Print Category" todo-print-buffer t]
6572 ["Print Category to File" todo-print-buffer-to-file t]
6575 ["Save Todo File" todo-save t]
6576 ["Quit Todo Mode" todo-quit t]
6579 ;; -----------------------------------------------------------------------------
6580 ;;; Hook functions and mode definitions
6581 ;; -----------------------------------------------------------------------------
6583 (defun todo-show-current-file ()
6584 "Visit current instead of default todo file with `todo-show'.
6585 Added to `pre-command-hook' in Todo mode when user option
6586 `todo-show-current-file' is set to non-nil."
6587 (setq todo-global-current-todo-file todo-current-todo-file))
6589 ;; (defun todo-display-as-todo-file ()
6590 ;; "Show todo files correctly when visited from outside of Todo mode.
6591 ;; Added to `find-file-hook' in Todo mode and Todo Archive mode."
6592 ;; (and (member this-command todo-visit-files-commands)
6593 ;; (= (- (point-max) (point-min)) (buffer-size))
6594 ;; (member major-mode '(todo-mode todo-archive-mode))
6595 ;; (todo-category-select)))
6597 ;; (defun todo-add-to-buffer-list ()
6598 ;; "Add name of just visited todo file to `todo-file-buffers'.
6599 ;; This function is added to `find-file-hook' in Todo mode."
6600 ;; (let ((filename (file-truename (buffer-file-name))))
6601 ;; (when (member filename todo-files)
6602 ;; (add-to-list 'todo-file-buffers filename))))
6604 (defun todo-update-buffer-list ()
6605 "Make current Todo mode buffer file car of `todo-file-buffers'.
6606 This function is added to `post-command-hook' in Todo mode."
6607 (let ((filename (file-truename (buffer-file-name))))
6608 (unless (eq (car todo-file-buffers) filename)
6609 (setq todo-file-buffers
6610 (cons filename (delete filename todo-file-buffers))))))
6612 (defun todo-reset-global-current-todo-file ()
6613 "Update the value of `todo-global-current-todo-file'.
6614 This becomes the latest existing todo file or, if there is none,
6615 the value of `todo-default-todo-file'.
6616 This function is added to `kill-buffer-hook' in Todo mode."
6617 (let ((filename (file-truename (buffer-file-name))))
6618 (setq todo-file-buffers (delete filename todo-file-buffers))
6619 (setq todo-global-current-todo-file
6620 (or (car todo-file-buffers)
6621 (todo-absolute-file-name todo-default-todo-file)))))
6623 (defun todo-reset-and-enable-done-separator ()
6624 "Show resized done items separator overlay after window change.
6625 Added to `window-configuration-change-hook' in Todo mode."
6626 (when (= 1 (length todo-done-separator-string))
6627 (let ((sep todo-done-separator))
6628 (setq todo-done-separator (todo-done-separator))
6629 (save-match-data (todo-reset-done-separator sep)))))
6631 (defun todo-modes-set-1 ()
6632 "Make some settings that apply to multiple Todo modes."
6633 (setq-local font-lock-defaults '(todo-font-lock-keywords t))
6634 (setq-local revert-buffer-function #'todo-revert-buffer)
6635 (setq-local tab-width todo-indent-to-here)
6636 (setq-local indent-line-function #'todo-indent)
6637 (when todo-wrap-lines
6639 (setq wrap-prefix (make-string todo-indent-to-here 32))))
6641 (defun todo-hl-line-range ()
6642 "Make `todo-toggle-item-highlighting' highlight entire item."
6644 (when (todo-item-end)
6645 (cons (todo-item-start)
6648 (defun todo-modes-set-2 ()
6649 "Make some settings that apply to multiple Todo modes."
6650 (add-to-invisibility-spec 'todo)
6651 (setq buffer-read-only t)
6652 (setq-local todo--item-headers-hidden nil)
6653 (setq-local desktop-save-buffer 'todo-desktop-save-buffer)
6654 (setq-local hl-line-range-function #'todo-hl-line-range))
6656 (defun todo-modes-set-3 ()
6657 "Make some settings that apply to multiple Todo modes."
6658 (setq-local todo-categories (todo-set-categories))
6659 (setq-local todo-category-number 1)
6660 ;; (add-hook 'find-file-hook #'todo-display-as-todo-file nil t)
6663 (put 'todo-mode 'mode-class 'special)
6666 (define-derived-mode todo-mode special-mode "Todo"
6667 "Major mode for displaying, navigating and editing todo lists.
6670 (if (called-interactively-p 'any)
6672 (substitute-command-keys
6673 "Type `\\[todo-show]' to enter Todo mode"))
6677 ;; Initialize todo-current-todo-file.
6678 (when (member (file-truename (buffer-file-name))
6679 (funcall todo-files-function))
6680 (setq-local todo-current-todo-file (file-truename (buffer-file-name))))
6681 (setq-local todo-show-done-only nil)
6682 (setq-local todo-categories-with-marks nil)
6683 ;; (add-hook 'find-file-hook #'todo-add-to-buffer-list nil t)
6684 (add-hook 'post-command-hook #'todo-update-buffer-list nil t)
6685 (when todo-show-current-file
6686 (add-hook 'pre-command-hook #'todo-show-current-file nil t))
6687 (add-hook 'window-configuration-change-hook
6688 #'todo-reset-and-enable-done-separator nil t)
6689 (add-hook 'kill-buffer-hook #'todo-reset-global-current-todo-file nil t)))
6691 (put 'todo-archive-mode 'mode-class 'special)
6693 ;; If todo-mode is parent, all todo-mode key bindings appear to be
6694 ;; available in todo-archive-mode (e.g. shown by C-h m).
6696 (define-derived-mode todo-archive-mode special-mode "Todo-Arch"
6697 "Major mode for archived todo categories.
6699 \\{todo-archive-mode-map}"
6703 (setq-local todo-current-todo-file (file-truename (buffer-file-name)))
6704 (setq-local todo-show-done-only t))
6706 (defun todo-mode-external-set ()
6707 "Set `todo-categories' externally to `todo-current-todo-file'."
6708 (setq-local todo-current-todo-file todo-global-current-todo-file)
6709 (let ((cats (with-current-buffer
6710 ;; Can't use find-buffer-visiting when
6711 ;; `todo-show-categories-table' is called on first
6712 ;; invocation of `todo-show', since there is then
6713 ;; no buffer visiting the current file.
6714 (find-file-noselect todo-current-todo-file 'nowarn)
6716 ;; In Todo Edit mode todo-categories is now nil
6717 ;; since it uses same buffer as Todo mode but
6718 ;; doesn't have the latter's local variables.
6720 (goto-char (point-min))
6721 (read (buffer-substring-no-properties
6722 (line-beginning-position)
6723 (line-end-position))))))))
6724 (setq-local todo-categories cats)))
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 (todo-mode-external-set)
6732 (setq buffer-read-only nil))
6734 (put 'todo-categories-mode 'mode-class 'special)
6736 (define-derived-mode todo-categories-mode special-mode "Todo-Cats"
6737 "Major mode for displaying and editing todo categories.
6739 \\{todo-categories-mode-map}"
6740 (todo-mode-external-set))
6742 (put 'todo-filtered-items-mode 'mode-class 'special)
6745 (define-derived-mode todo-filtered-items-mode special-mode "Todo-Fltr"
6746 "Mode for displaying and reprioritizing top priority Todo.
6748 \\{todo-filtered-items-mode-map}"
6752 ;; -----------------------------------------------------------------------------
6753 (provide 'todo-mode)
6755 ;;; todo-mode.el ends here