1 ;;; todo-mode.el --- facilities for making and maintaining todo lists
3 ;; Copyright (C) 1997, 1999, 2001-2015 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 <http://www.gnu.org/licenses/>.
27 ;; This package provides facilities for making and maintaining
28 ;; prioritized lists of things to do. These todo lists are identified
29 ;; with named categories, so you can group together thematically
30 ;; related todo items. Each category is stored in a file, providing a
31 ;; further level of organization. You can create as many todo files,
32 ;; and in each as many categories, as you want.
34 ;; With Todo mode you can navigate among the items of a category, and
35 ;; between categories in the same and in different todo files. You
36 ;; can add and edit todo items, reprioritize them, move them to
37 ;; another category, or delete them. You can also mark items as done
38 ;; and store them within their category or in separate archive files.
39 ;; You can include todo items in the Emacs Fancy Diary display and
40 ;; treat them as appointments. You can add new todo files, and rename
41 ;; or delete them. You can add new categories to a file, rename or
42 ;; delete them, move a category to another file and merge the items of
43 ;; two categories. You can also reorder the sequence of categories in
44 ;; a todo file for the purpose of navigation. You can display
45 ;; sortable summary tables of the categories in a file and the types
46 ;; of items they contain. And you can filter items by various
47 ;; criteria from multiple categories in one or more todo files to
48 ;; create prioritizable cross-category overviews of your todo items.
50 ;; To get started, type `M-x todo-show'. For full details of the user
51 ;; interface, commands and options, consult the Todo mode user manual,
52 ;; which is included in the Info documentation.
57 (require 'cl-lib
) ; For cl-oddp and cl-assert.
59 ;; -----------------------------------------------------------------------------
60 ;;; Setting up todo files, categories, and items
61 ;; -----------------------------------------------------------------------------
63 (defcustom todo-directory
(locate-user-emacs-file "todo/")
64 "Directory where user's todo files are saved."
68 (defun todo-files (&optional archives
)
69 "Default value of `todo-files-function'.
70 This returns the case-insensitive alphabetically sorted list of
71 file truenames in `todo-directory' with the extension
72 \".todo\". With non-nil ARCHIVES return the list of archive file
73 truenames (those with the extension \".toda\")."
74 (let ((files (if (file-exists-p todo-directory
)
75 (mapcar 'file-truename
76 (directory-files todo-directory t
77 (if archives
"\.toda$" "\.todo$") t
)))))
78 (sort files
(lambda (s1 s2
) (let ((cis1 (upcase s1
))
80 (string< cis1 cis2
))))))
82 (defcustom todo-files-function
'todo-files
83 "Function returning the value of the variable `todo-files'.
84 This function should take an optional argument that, if non-nil,
85 makes it return the value of the variable `todo-archives'."
89 (defvar todo-files
(funcall todo-files-function
)
90 "List of truenames of user's todo files.")
92 (defvar todo-archives
(funcall todo-files-function t
)
93 "List of truenames of user's todo archives.")
95 (defvar todo-visited nil
96 "List of todo files visited in this session by `todo-show'.
97 Used to determine initial display according to the value of
100 (defvar todo-file-buffers nil
101 "List of file names of live Todo mode buffers.")
103 (defvar todo-global-current-todo-file nil
104 "Variable holding name of current todo file.
105 Used by functions called from outside of Todo mode to visit the
106 current todo file rather than the default todo file (i.e. when
107 users option `todo-show-current-file' is non-nil).")
109 (defvar todo-current-todo-file nil
110 "Variable holding the name of the currently active todo file.")
112 (defvar todo-categories nil
113 "Alist of categories in the current todo file.
114 The elements are cons cells whose car is a category name and
115 whose cdr is a vector of the category's item counts. These are,
116 in order, the numbers of todo items, of todo items included in
117 the Diary, of done items and of archived items.")
119 (defvar todo-category-number
1
120 "Variable holding the number of the current todo category.
121 Todo categories are numbered starting from 1.")
123 (defvar todo-categories-with-marks nil
124 "Alist of categories and number of marked items they contain.")
126 (defconst todo-category-beg
"--==-- "
127 "String marking beginning of category (inserted with its name).")
129 (defconst todo-category-done
"==--== DONE "
130 "String marking beginning of category's done items.")
132 (defcustom todo-done-separator-string
"="
133 "String determining the value of variable `todo-done-separator'.
134 If the string consists of a single character,
135 `todo-done-separator' will be the string made by repeating this
136 character for the width of the window, and the length is
137 automatically recalculated when the window width changes. If the
138 string consists of more (or less) than one character, it will be
139 the value of `todo-done-separator'."
141 :initialize
'custom-initialize-default
142 :set
'todo-reset-done-separator-string
143 :group
'todo-display
)
145 (defun todo-done-separator ()
146 "Return string used as value of variable `todo-done-separator'."
147 (let ((sep todo-done-separator-string
))
148 (propertize (if (= 1 (length sep
))
149 (make-string (window-width) (string-to-char sep
))
150 todo-done-separator-string
)
151 'face
'todo-done-sep
)))
153 (defvar todo-done-separator
(todo-done-separator)
154 "String used to visually separate done from not done items.
155 Displayed as an overlay instead of `todo-category-done' when
156 done items are shown. Its value is determined by user option
157 `todo-done-separator-string'.")
159 (defvar todo-show-done-only nil
160 "If non-nil display only done items in current category.
161 Set by the command `todo-toggle-view-done-only' and used by
162 `todo-category-select'.")
164 (defcustom todo-nondiary-marker
'("[" "]")
165 "List of strings surrounding item date to block diary inclusion.
166 The first string is inserted before the item date and must be a
167 non-empty string that does not match a diary date in order to
168 have its intended effect. The second string is inserted after
170 :type
'(list string string
)
172 :initialize
'custom-initialize-default
173 :set
'todo-reset-nondiary-marker
)
175 (defconst todo-nondiary-start
(nth 0 todo-nondiary-marker
)
176 "String inserted before item date to block diary inclusion.")
178 (defconst todo-nondiary-end
(nth 1 todo-nondiary-marker
)
179 "String inserted after item date matching `todo-nondiary-start'.")
181 (defconst todo-month-name-array
182 (vconcat calendar-month-name-array
(vector "*"))
183 "Array of month names, in order.
184 The final element is \"*\", indicating an unspecified month.")
186 (defconst todo-month-abbrev-array
187 (vconcat calendar-month-abbrev-array
(vector "*"))
188 "Array of abbreviated month names, in order.
189 The final element is \"*\", indicating an unspecified month.")
191 (defconst todo-date-pattern
192 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
193 (concat "\\(?4:\\(?5:" dayname
"\\)\\|"
195 (monthname (format "\\(?6:%s\\)" (diary-name-pattern
196 todo-month-name-array
197 todo-month-abbrev-array
)))
198 (month "\\(?7:[0-9]+\\|\\*\\)")
199 (day "\\(?8:[0-9]+\\|\\*\\)")
200 (year "-?\\(?9:[0-9]+\\|\\*\\)"))
201 (mapconcat 'eval calendar-date-display-form
""))
203 "Regular expression matching a todo item date header.")
205 ;; By itself this matches anything, because of the `?'; however, it's only
206 ;; used in the context of `todo-date-pattern' (but Emacs Lisp lacks
208 (defconst todo-date-string-start
209 (concat "^\\(" (regexp-quote todo-nondiary-start
) "\\|"
210 (regexp-quote diary-nonmarking-symbol
) "\\)?")
211 "Regular expression matching part of item header before the date.")
213 (defcustom todo-done-string
"DONE "
214 "Identifying string appended to the front of done todo items."
216 :initialize
'custom-initialize-default
217 :set
'todo-reset-done-string
220 (defconst todo-done-string-start
221 (concat "^\\[" (regexp-quote todo-done-string
))
222 "Regular expression matching start of done item.")
224 (defconst todo-item-start
(concat "\\(" todo-date-string-start
"\\|"
225 todo-done-string-start
"\\)"
227 "String identifying start of a todo item.")
229 ;; -----------------------------------------------------------------------------
230 ;;; Todo mode display options
231 ;; -----------------------------------------------------------------------------
233 (defcustom todo-prefix
""
234 "String prefixed to todo items for visual distinction."
235 :type
'(string :validate
237 (when (string= (widget-value widget
) todo-item-mark
)
240 "Invalid value: must be distinct from `todo-item-mark'")
242 :initialize
'custom-initialize-default
243 :set
'todo-reset-prefix
244 :group
'todo-display
)
246 (defcustom todo-number-prefix t
247 "Non-nil to prefix items with consecutively increasing integers.
248 These reflect the priorities of the items in each category."
250 :initialize
'custom-initialize-default
251 :set
'todo-reset-prefix
252 :group
'todo-display
)
254 (defun todo-mode-line-control (cat)
255 "Return a mode line control for todo or archive file buffers.
256 Argument CAT is the name of the current todo category.
257 This function is the value of the user variable
258 `todo-mode-line-function'."
259 (let ((file (todo-short-file-name todo-current-todo-file
)))
260 (format "%s category %d: %s" file todo-category-number cat
)))
262 (defcustom todo-mode-line-function
'todo-mode-line-control
263 "Function that returns a mode line control for Todo mode buffers.
264 The function expects one argument holding the name of the current
265 todo category. The resulting control becomes the local value of
266 `mode-line-buffer-identification' in each Todo mode buffer."
268 :group
'todo-display
)
270 (defcustom todo-highlight-item nil
271 "Non-nil means highlight items at point."
273 :initialize
'custom-initialize-default
274 :set
'todo-reset-highlight-item
275 :group
'todo-display
)
277 (defcustom todo-wrap-lines t
278 "Non-nil to activate Visual Line mode and use wrap prefix."
280 :group
'todo-display
)
282 (defcustom todo-indent-to-here
3
283 "Number of spaces to indent continuation lines of items.
284 This must be a positive number to ensure such items are fully
285 shown in the Fancy Diary display."
286 :type
'(integer :validate
288 (unless (> (widget-value widget
) 0)
289 (widget-put widget
:error
290 "Invalid value: must be a positive integer")
292 :group
'todo-display
)
294 (defun todo-indent ()
295 "Indent from point to `todo-indent-to-here'."
296 (indent-to todo-indent-to-here todo-indent-to-here
))
298 (defcustom todo-show-with-done nil
299 "Non-nil to display done items in all categories."
301 :group
'todo-display
)
303 ;; -----------------------------------------------------------------------------
305 ;; -----------------------------------------------------------------------------
307 (defface todo-key-prompt
308 '((t (:weight bold
)))
309 "Face for making keys in item insertion prompt stand out."
313 ;; '((t :inherit font-lock-warning-face))
317 (:weight bold
:foreground
"Red1"))
321 (:weight bold
:foreground
"Pink"))
325 (:weight bold
:foreground
"Red1"))
329 (:weight bold
:foreground
"Pink"))
334 (:weight bold
:inverse-video t
)))
335 "Face for marks on marked items."
338 (defface todo-prefix-string
339 ;; '((t :inherit font-lock-constant-face))
340 '((((class grayscale
) (background light
))
341 (:foreground
"LightGray" :weight bold
:underline t
))
342 (((class grayscale
) (background dark
))
343 (:foreground
"Gray50" :weight bold
:underline t
))
344 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
345 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
346 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
347 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
348 (((class color
) (min-colors 8)) (:foreground
"magenta"))
349 (t (:weight bold
:underline t
)))
350 "Face for todo item prefix or numerical priority string."
353 (defface todo-top-priority
354 ;; bold font-lock-comment-face
355 '((default :weight bold
)
356 (((class grayscale
) (background light
)) :foreground
"DimGray" :slant italic
)
357 (((class grayscale
) (background dark
)) :foreground
"LightGray" :slant italic
)
358 (((class color
) (min-colors 88) (background light
)) :foreground
"Firebrick")
359 (((class color
) (min-colors 88) (background dark
)) :foreground
"chocolate1")
360 (((class color
) (min-colors 16) (background light
)) :foreground
"red")
361 (((class color
) (min-colors 16) (background dark
)) :foreground
"red1")
362 (((class color
) (min-colors 8) (background light
)) :foreground
"red")
363 (((class color
) (min-colors 8) (background dark
)) :foreground
"yellow")
365 "Face for top priority todo item numerical priority string.
366 The item's priority number string has this face if the number is
367 less than or equal the category's top priority setting."
370 (defface todo-nondiary
371 ;; '((t :inherit font-lock-type-face))
372 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
373 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
374 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
375 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
376 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
377 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
378 (((class color
) (min-colors 8)) :foreground
"green")
379 (t :weight bold
:underline t
))
380 "Face for non-diary markers around todo item date/time header."
384 '((t :inherit diary
))
385 "Face for the date string of a todo item."
389 '((t :inherit diary-time
))
390 "Face for the time string of a todo item."
393 (defface todo-diary-expired
394 ;; Doesn't contrast enough with todo-date (= diary) face.
395 ;; ;; '((t :inherit warning))
396 ;; '((default :weight bold)
397 ;; (((class color) (min-colors 16)) :foreground "DarkOrange")
398 ;; (((class color)) :foreground "yellow"))
399 ;; bold font-lock-function-name-face
400 '((default :weight bold
)
401 (((class color
) (min-colors 88) (background light
)) :foreground
"Blue1")
402 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSkyBlue")
403 (((class color
) (min-colors 16) (background light
)) :foreground
"Blue")
404 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSkyBlue")
405 (((class color
) (min-colors 8)) :foreground
"blue")
406 (t :inverse-video t
))
407 "Face for expired dates of diary items."
410 (defface todo-done-sep
411 ;; '((t :inherit font-lock-builtin-face))
412 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
413 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
414 (((class color
) (min-colors 88) (background light
)) :foreground
"dark slate blue")
415 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSteelBlue")
416 (((class color
) (min-colors 16) (background light
)) :foreground
"Orchid")
417 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSteelBlue")
418 (((class color
) (min-colors 8)) :foreground
"blue" :weight bold
)
420 "Face for separator string between done and not done todo items."
424 ;; '((t :inherit font-lock-keyword-face))
425 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
426 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
427 (((class color
) (min-colors 88) (background light
)) :foreground
"Purple")
428 (((class color
) (min-colors 88) (background dark
)) :foreground
"Cyan1")
429 (((class color
) (min-colors 16) (background light
)) :foreground
"Purple")
430 (((class color
) (min-colors 16) (background dark
)) :foreground
"Cyan")
431 (((class color
) (min-colors 8)) :foreground
"cyan" :weight bold
)
433 "Face for done todo item header string."
436 (defface todo-comment
437 ;; '((t :inherit font-lock-comment-face))
438 '((((class grayscale
) (background light
))
439 :foreground
"DimGray" :weight bold
:slant italic
)
440 (((class grayscale
) (background dark
))
441 :foreground
"LightGray" :weight bold
:slant italic
)
442 (((class color
) (min-colors 88) (background light
))
443 :foreground
"Firebrick")
444 (((class color
) (min-colors 88) (background dark
))
445 :foreground
"chocolate1")
446 (((class color
) (min-colors 16) (background light
))
448 (((class color
) (min-colors 16) (background dark
))
450 (((class color
) (min-colors 8) (background light
))
452 (((class color
) (min-colors 8) (background dark
))
453 :foreground
"yellow")
454 (t :weight bold
:slant italic
))
455 "Face for comments appended to done todo items."
459 ;; '((t :inherit match))
463 (:background
"yellow1"))
467 (:background
"RoyalBlue3"))
471 (:foreground
"black" :background
"yellow"))
475 (:foreground
"white" :background
"blue"))
480 (:background
"gray")))
481 "Face for matches found by `todo-search'."
485 ;; '((t :inherit widget-field))
487 (:foreground
"black" :background
"yellow3"))
488 (((class grayscale color
)
490 (:background
"gray85"))
491 (((class grayscale color
)
493 (:background
"dim gray"))
496 "Face for buttons in table of categories."
499 (defface todo-sorted-column
504 (:background
"grey85"))
507 (:background
"grey85" :foreground
"grey10"))
509 (:background
"gray")))
510 "Face for sorted column in table of categories."
513 (defface todo-archived-only
514 ;; '((t (:inherit (shadow))))
517 (:foreground
"grey50"))
520 (:foreground
"grey70"))
522 (:foreground
"gray")))
523 "Face for archived-only category names in table of categories."
526 (defface todo-category-string
527 ;; '((t :inherit font-lock-type-face))
528 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
529 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
530 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
531 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
532 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
533 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
534 (((class color
) (min-colors 8)) :foreground
"green")
535 (t :weight bold
:underline t
))
536 "Face for category-file header in Todo Filtered Items mode."
539 ;; -----------------------------------------------------------------------------
540 ;;; Entering and exiting
541 ;; -----------------------------------------------------------------------------
543 ;; (defcustom todo-visit-files-commands (list 'find-file 'dired-find-file)
544 ;; "List of file finding commands for `todo-display-as-todo-file'.
545 ;; Invoking these commands to visit a todo file or todo archive file
546 ;; calls `todo-show' or `todo-find-archive', so that the file is
547 ;; displayed correctly."
548 ;; :type '(repeat function)
551 (defun todo-short-file-name (file)
552 "Return the short form of todo file FILE's name.
553 This lacks the extension and directory components."
555 (file-name-sans-extension (file-name-nondirectory file
))))
557 (defcustom todo-default-todo-file
(todo-short-file-name
558 (car (funcall todo-files-function
)))
559 "Todo file visited by first session invocation of `todo-show'."
560 :type
(when todo-files
561 `(radio ,@(mapcar (lambda (f) (list 'const f
))
562 (mapcar 'todo-short-file-name
563 (funcall todo-files-function
)))))
566 (defcustom todo-show-current-file t
567 "Non-nil to make `todo-show' visit the current todo file.
568 Otherwise, `todo-show' always visits `todo-default-todo-file'."
570 :initialize
'custom-initialize-default
571 :set
'todo-set-show-current-file
574 (defcustom todo-show-first
'first
575 "What action to take on first use of `todo-show' on a file."
576 :type
'(choice (const :tag
"Show first category" first
)
577 (const :tag
"Show table of categories" table
)
578 (const :tag
"Show top priorities" top
)
579 (const :tag
"Show diary items" diary
)
580 (const :tag
"Show regexp items" regexp
))
583 (defcustom todo-add-item-if-new-category t
584 "Non-nil to prompt for an item after adding a new category."
588 (defcustom todo-initial-file
"Todo"
589 "Default file name offered on adding first todo file."
593 (defcustom todo-initial-category
"Todo"
594 "Default category name offered on initializing a new todo file."
598 (defcustom todo-category-completions-files nil
599 "List of files for building `todo-read-category' completions."
600 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
601 (mapcar 'todo-short-file-name
602 (funcall todo-files-function
))))
605 (defcustom todo-completion-ignore-case nil
606 "Non-nil means case is ignored by `todo-read-*' functions."
611 (defun todo-show (&optional solicit-file interactive
)
612 "Visit a todo file and display one of its categories.
614 When invoked in Todo mode, prompt for which todo file to visit.
615 When invoked outside of Todo mode with non-nil prefix argument
616 SOLICIT-FILE prompt for which todo file to visit; otherwise visit
617 `todo-default-todo-file'. Subsequent invocations from outside
618 of Todo mode revisit this file or, with option
619 `todo-show-current-file' non-nil (the default), whichever todo
620 file was last visited.
622 If you call this command before you have created any todo file in
623 the current format, and you have an todo file in old format, it
624 will ask you whether to convert that file and show it.
625 Otherwise, calling this command before any todo file exists
626 prompts for a file name and an initial category (defaulting to
627 `todo-initial-file' and `todo-initial-category'), creates both of
628 these, visits the file and displays the category, and if option
629 `todo-add-item-if-new-category' is non-nil (the default), prompts
632 The first invocation of this command on an existing todo file
633 interacts with the option `todo-show-first': if its value is
634 `first' (the default), show the first category in the file; if
635 its value is `table', show the table of categories in the file;
636 if its value is one of `top', `diary' or `regexp', show the
637 corresponding saved top priorities, diary items, or regexp items
638 file, if any. Subsequent invocations always show the file's
639 current (i.e., last displayed) category.
641 In Todo mode just the category's unfinished todo items are shown
642 by default. The done items are hidden, but typing
643 `\\[todo-toggle-view-done-items]' displays them below the todo
644 items. With non-nil user option `todo-show-with-done' both todo
645 and done items are always shown on visiting a category.
647 Invoking this command in Todo Archive mode visits the
648 corresponding todo file, displaying the corresponding category."
650 (when todo-default-todo-file
651 (todo-check-file (todo-absolute-file-name todo-default-todo-file
)))
653 ;; Before initializing the first todo first, check if there is a
654 ;; legacy todo file and if so, offer to convert to the current
655 ;; format and make it the first new todo file.
656 (unless todo-default-todo-file
657 (let ((legacy-todo-file (if (boundp 'todo-file-do
)
659 (locate-user-emacs-file "todo-do" ".todo-do"))))
660 (when (and (file-exists-p legacy-todo-file
)
661 (y-or-n-p (concat "Do you want to convert a copy of your "
662 "old todo file to the new format? ")))
663 (when (todo-convert-legacy-files)
664 (throw 'shown nil
)))))
667 (show-first todo-show-first
)
668 (file (cond ((or solicit-file
670 (memq major-mode
'(todo-mode
672 todo-filtered-items-mode
))))
673 (if (funcall todo-files-function
)
674 (todo-read-file-name "Choose a todo file to visit: "
676 (user-error "There are no todo files")))
677 ((and (eq major-mode
'todo-archive-mode
)
678 ;; Called noninteractively via todo-quit
679 ;; to jump to corresponding category in
682 (setq cat
(todo-current-category))
683 (concat (file-name-sans-extension
684 todo-current-todo-file
) ".todo"))
686 (or todo-current-todo-file
687 (and todo-show-current-file
688 todo-global-current-todo-file
)
689 (todo-absolute-file-name todo-default-todo-file
)
692 (unless todo-default-todo-file
693 ;; We just initialized the first todo file, so make it the default.
694 (setq todo-default-todo-file
(todo-short-file-name file
)
696 (todo-reevaluate-default-file-defcustom))
697 (unless (member file todo-visited
)
698 ;; Can't setq t-c-t-f here, otherwise wrong file shown when
699 ;; todo-show is called from todo-show-categories-table.
700 (let ((todo-current-todo-file file
))
701 (cond ((eq todo-show-first
'table
)
702 (todo-show-categories-table))
703 ((memq todo-show-first
'(top diary regexp
))
704 (let* ((shortf (todo-short-file-name file
))
705 (fi-file (todo-absolute-file-name
706 shortf todo-show-first
)))
707 (when (eq todo-show-first
'regexp
)
708 (let ((rxfiles (directory-files todo-directory t
710 (when (and rxfiles
(> (length rxfiles
) 1))
711 (let ((rxf (mapcar 'todo-short-file-name rxfiles
)))
712 (setq fi-file
(todo-absolute-file-name
714 "Choose a regexp items file: "
716 (if (file-exists-p fi-file
)
720 (set-buffer (find-file-noselect fi-file
'nowarn
)))
721 (unless (derived-mode-p 'todo-filtered-items-mode
)
722 (todo-filtered-items-mode)))
723 (message "There is no %s file for %s"
724 (cond ((eq todo-show-first
'top
)
726 ((eq todo-show-first
'diary
)
728 ((eq todo-show-first
'regexp
)
731 (setq todo-show-first
'first
)))))))
732 (when (or (member file todo-visited
)
733 (eq todo-show-first
'first
))
734 (unless (todo-check-file file
) (throw 'end nil
))
735 (set-window-buffer (selected-window)
736 (set-buffer (find-file-noselect file
'nowarn
)))
737 (if (equal (file-name-extension (buffer-file-name)) "toda")
738 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
739 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
740 ;; When quitting an archive file, show the corresponding
741 ;; category in the corresponding todo file, if it exists.
742 (when (assoc cat todo-categories
)
743 (setq todo-category-number
(todo-category-number cat
)))
744 ;; If this is a new todo file, add its first category.
745 (when (zerop (buffer-size))
748 (setq todo-category-number
749 (todo-add-category todo-current-todo-file
"")
750 add-item todo-add-item-if-new-category
753 ;; If the category was added, save the file now, so we
754 ;; don't risk having an empty todo file, which would
755 ;; signal an error if we tried to visit it later,
756 ;; since doing that looks for category boundaries.
758 ;; If user cancels before adding the category, clean up
759 ;; and exit, so we have a fresh slate the next time.
761 ;; (setq todo-files (funcall todo-files-function))
762 (setq todo-files
(delete file todo-files
))
764 (setq todo-default-todo-file nil
765 todo-current-todo-file nil
)
766 (todo-reevaluate-default-file-defcustom))
769 (save-excursion (todo-category-select))
770 (when add-item
(todo-insert-item--basic)))
771 (setq todo-show-first show-first
)
772 (add-to-list 'todo-visited file
)))))
775 "Save the current todo file."
777 (cond ((eq major-mode
'todo-filtered-items-mode
)
778 (todo-check-filtered-items-file)
779 (todo-save-filtered-items-buffer))
783 (defvar todo-descending-counts
)
786 "Exit the current Todo-related buffer.
787 Depending on the specific mode, this either kills the buffer or
788 buries it and restores state as needed."
790 (let ((buf (current-buffer)))
791 (cond ((eq major-mode
'todo-categories-mode
)
792 ;; Postpone killing buffer till after calling todo-show, to
793 ;; prevent killing todo-mode buffer.
794 (setq todo-descending-counts nil
)
795 ;; Ensure todo-show calls todo-show-categories-table only on
796 ;; first invocation per file.
797 (when (eq todo-show-first
'table
)
798 (add-to-list 'todo-visited todo-current-todo-file
))
801 ((eq major-mode
'todo-filtered-items-mode
)
803 (unless (eq major-mode
'todo-mode
) (todo-show)))
804 ((eq major-mode
'todo-archive-mode
)
805 ;; Have to write a newly created archive to file to avoid
806 ;; subsequent errors.
808 (let ((todo-file (concat todo-directory
809 (todo-short-file-name todo-current-todo-file
)
811 (if (todo-check-file todo-file
)
813 (message "There is no todo file for this archive")))
814 ;; When todo-check-file runs in todo-show, it kills the
815 ;; buffer if the archive file was deleted externally.
816 (when (buffer-live-p buf
) (bury-buffer buf
)))
817 ((eq major-mode
'todo-mode
)
819 ;; If we just quit archive mode, just burying the buffer
820 ;; in todo-mode would return to archive.
821 (set-window-buffer (selected-window)
822 (set-buffer (other-buffer)))
823 (bury-buffer buf
)))))
825 ;; -----------------------------------------------------------------------------
826 ;;; Navigation between and within categories
827 ;; -----------------------------------------------------------------------------
829 (defcustom todo-skip-archived-categories nil
830 "Non-nil to handle categories with only archived items specially.
832 Sequential category navigation using \\[todo-forward-category]
833 or \\[todo-backward-category] skips categories that contain only
834 archived items. Other commands still recognize these categories.
835 In Todo Categories mode (\\[todo-show-categories-table]) these
836 categories shown in `todo-archived-only' face and pressing the
837 category button visits the category in the archive instead of the
840 :group
'todo-display
)
842 (defun todo-forward-category (&optional back
)
843 "Visit the numerically next category in this todo file.
844 If the current category is the highest numbered, visit the first
845 category. With non-nil argument BACK, visit the numerically
846 previous category (the highest numbered one, if the current
847 category is the first)."
849 (setq todo-category-number
850 (1+ (mod (- todo-category-number
(if back
2 0))
851 (length todo-categories
))))
852 (when todo-skip-archived-categories
853 (while (and (zerop (todo-get-count 'todo
))
854 (zerop (todo-get-count 'done
))
855 (not (zerop (todo-get-count 'archived
))))
856 (setq todo-category-number
857 (apply (if back
'1-
'1+) (list todo-category-number
)))))
858 (todo-category-select)
859 (goto-char (point-min)))
861 (defun todo-backward-category ()
862 "Visit the numerically previous category in this todo file.
863 If the current category is the highest numbered, visit the first
866 (todo-forward-category t
))
868 (defvar todo-categories-buffer
)
870 (defun todo-jump-to-category (&optional file where
)
871 "Prompt for a category in a todo file and jump to it.
873 With non-nil FILE (interactively a prefix argument), prompt for a
874 specific todo file and choose (with TAB completion) a category
875 in it to jump to; otherwise, choose and jump to any category in
876 either the current todo file or a file in
877 `todo-category-completions-files'.
879 Also accept a non-existing category name and ask whether to add a
880 new category by that name; on confirmation, add it and jump to
881 that category, and if option `todo-add-item-if-new-category' is
882 non-nil (the default), then prompt for the first item.
884 In noninteractive calls non-nil WHERE specifies either the goal
885 category or its file. If its value is `archive', the choice of
886 categories is restricted to the current archive file or the
887 archive you were prompted to choose; this is used by
888 `todo-jump-to-archive-category'. If its value is the name of a
889 category, jump directly to that category; this is used in Todo
892 ;; If invoked outside of Todo mode and there is not yet any Todo
893 ;; file, initialize one.
894 (if (null (funcall todo-files-function
))
896 (let* ((archive (eq where
'archive
))
897 (cat (unless archive where
))
898 (file0 (when cat
; We're in Todo Categories mode.
899 ;; With non-nil `todo-skip-archived-categories'
900 ;; jump to archive file of a category with only
902 (if (and todo-skip-archived-categories
903 (zerop (todo-get-count 'todo cat
))
904 (zerop (todo-get-count 'done cat
))
905 (not (zerop (todo-get-count 'archived cat
))))
906 (concat (file-name-sans-extension
907 todo-current-todo-file
) ".toda")
908 ;; Otherwise, jump to current todo file.
909 todo-current-todo-file
)))
910 (len (length todo-categories
))
911 (cat+file
(unless cat
912 (todo-read-category "Jump to category: "
913 (if archive
'archive
) file
)))
914 (add-item (and todo-add-item-if-new-category
915 (> (length todo-categories
) len
)))
916 (category (or cat
(car cat
+file
))))
917 (unless cat
(setq file0
(cdr cat
+file
)))
918 (with-current-buffer (find-file-noselect file0
'nowarn
)
919 (setq todo-current-todo-file file0
)
920 ;; If called from Todo Categories mode, clean up before jumping.
921 (if (string= (buffer-name) todo-categories-buffer
)
923 (set-window-buffer (selected-window)
924 (set-buffer (find-buffer-visiting file0
)))
925 (unless todo-global-current-todo-file
926 (setq todo-global-current-todo-file todo-current-todo-file
))
927 (todo-category-number category
)
928 (todo-category-select)
929 (goto-char (point-min))
930 (when add-item
(todo-insert-item--basic))))))
932 (defun todo-next-item (&optional count
)
933 "Move point down to the beginning of the next item.
934 With positive numerical prefix COUNT, move point COUNT items
937 If the category's done items are hidden, this command also moves
938 point to the empty line below the last todo item from any higher
939 item in the category, i.e., when invoked with or without a prefix
940 argument. If the category's done items are visible, this command
941 called with a prefix argument only moves point to a lower item,
942 e.g., with point on the last todo item and called with prefix 1,
943 it moves point to the first done item; but if called with point
944 on the last todo item without a prefix argument, it moves point
945 the the empty line above the done items separator."
947 ;; It's not worth the trouble to allow prefix arg value < 1, since
948 ;; we have the corresponding command.
949 (cond ((and current-prefix-arg
(< count
1))
950 (user-error "The prefix argument must be a positive number"))
952 (todo-forward-item count
))
954 (todo-forward-item))))
956 (defun todo-previous-item (&optional count
)
957 "Move point up to start of item with next higher priority.
958 With positive numerical prefix COUNT, move point COUNT items
961 If the category's done items are visible, this command called
962 with a prefix argument only moves point to a higher item, e.g.,
963 with point on the first done item and called with prefix 1, it
964 moves to the last todo item; but if called with point on the
965 first done item without a prefix argument, it moves point the the
966 empty line above the done items separator."
968 ;; Avoid moving to bob if on the first item but not at bob.
969 (when (> (line-number-at-pos) 1)
970 ;; It's not worth the trouble to allow prefix arg value < 1, since
971 ;; we have the corresponding command.
972 (cond ((and current-prefix-arg
(< count
1))
973 (user-error "The prefix argument must be a positive number"))
975 (todo-backward-item count
))
977 (todo-backward-item)))))
979 ;; -----------------------------------------------------------------------------
980 ;;; Display toggle commands
981 ;; -----------------------------------------------------------------------------
983 (defun todo-toggle-prefix-numbers ()
984 "Hide item numbering if shown, show if hidden."
988 (goto-char (point-min))
989 (let* ((ov (todo-get-overlay 'prefix
))
990 (show-done (re-search-forward todo-done-string-start nil t
))
991 (todo-show-with-done show-done
)
992 (todo-number-prefix (not (equal (overlay-get ov
'before-string
)
994 (if (eq major-mode
'todo-filtered-items-mode
)
995 (todo-prefix-overlays)
996 (todo-category-select))))))
998 (defun todo-toggle-view-done-items ()
999 "Show hidden or hide visible done items in current category."
1001 (if (zerop (todo-get-count 'done
(todo-current-category)))
1002 (message "There are no done items in this category.")
1003 (let ((opoint (point)))
1004 (goto-char (point-min))
1005 (let* ((shown (re-search-forward todo-done-string-start nil t
))
1006 (todo-show-with-done (not shown
)))
1007 (todo-category-select)
1009 ;; If start of done items sections is below the bottom of the
1010 ;; window, make it visible.
1013 (goto-char (point-min))
1014 (re-search-forward todo-done-string-start nil t
)))
1015 (if (not (pos-visible-in-window-p shown
))
1017 (goto-char opoint
)))))))
1019 (defun todo-toggle-view-done-only ()
1020 "Switch between displaying only done or only todo items."
1022 (setq todo-show-done-only
(not todo-show-done-only
))
1023 (todo-category-select))
1025 (defun todo-toggle-item-highlighting ()
1026 "Highlight or unhighlight the todo item the cursor is on."
1028 (eval-and-compile (require 'hl-line
))
1029 (when (memq major-mode
1030 '(todo-mode todo-archive-mode todo-filtered-items-mode
))
1035 (defun todo-toggle-item-header ()
1036 "Hide or show item date-time headers in the current file.
1037 With done items, this hides only the done date-time string, not
1038 the the original date-time string."
1042 (goto-char (point-min))
1043 (let ((ov (todo-get-overlay 'header
)))
1045 (remove-overlays 1 (1+ (buffer-size)) 'todo
'header
)
1047 (goto-char (point-min))
1049 (when (re-search-forward
1050 (concat todo-item-start
1051 "\\( " diary-time-regexp
"\\)?"
1052 (regexp-quote todo-nondiary-end
) "? ")
1054 (setq ov
(make-overlay (match-beginning 0) (match-end 0) nil t
))
1055 (overlay-put ov
'todo
'header
)
1056 (overlay-put ov
'display
""))
1057 (todo-forward-item)))))))
1059 ;; -----------------------------------------------------------------------------
1060 ;;; File and category editing
1061 ;; -----------------------------------------------------------------------------
1063 (defun todo-add-file ()
1064 "Name and initialize a new todo file.
1065 Interactively, prompt for a category and display it, and if
1066 option `todo-add-item-if-new-category' is non-nil (the default),
1067 prompt for the first item.
1068 Noninteractively, return the name of the new file."
1070 (let* ((prompt (concat "Enter name of new todo file "
1071 "(TAB or SPC to see current names): "))
1072 (file (todo-read-file-name prompt
)))
1073 ;; Don't accept the name of an existing todo file.
1074 (setq file
(todo-absolute-file-name
1075 (todo-validate-name (todo-short-file-name file
) 'file
)))
1076 (with-current-buffer (get-buffer-create file
)
1078 (write-region (point-min) (point-max) file nil
'nomessage nil t
)
1080 (setq todo-files
(funcall todo-files-function
))
1081 (todo-reevaluate-filelist-defcustoms)
1082 (if (called-interactively-p 'any
)
1084 (set-window-buffer (selected-window)
1085 (set-buffer (find-file-noselect file
)))
1086 (setq todo-current-todo-file file
)
1090 (defun todo-rename-file (&optional arg
)
1091 "Rename the current todo file.
1092 With prefix ARG, prompt for a todo file and rename it.
1093 If there are corresponding archive or filtered items files,
1094 rename these accordingly. If there are live buffers visiting
1095 these files, also rename them accordingly."
1097 (let* ((oname (or (and arg
1098 (todo-read-file-name "Choose a file to rename: "
1100 (buffer-file-name)))
1101 (soname (todo-short-file-name oname
))
1102 (nname (todo-read-file-name "New name for this file: "))
1103 (snname (todo-short-file-name nname
))
1104 (files (directory-files todo-directory t
1105 (concat ".*" (regexp-quote soname
)
1106 ".*\.tod[aorty]$") t
)))
1108 (let* ((sfname (todo-short-file-name f
))
1109 (fext (file-name-extension f t
))
1110 (fbuf (find-buffer-visiting f
))
1111 (fbname (buffer-name fbuf
)))
1112 (when (string-match (regexp-quote soname
) sfname
)
1113 (let* ((snfname (replace-match snname t t sfname
))
1114 (nfname (concat todo-directory snfname fext
)))
1115 (rename-file f nfname
)
1117 (with-current-buffer fbuf
1118 (set-visited-file-name nfname t t
)
1119 (cond ((member fext
'(".todo" ".toda"))
1120 (setq todo-current-todo-file
(buffer-file-name))
1121 (setq mode-line-buffer-identification
1122 (funcall todo-mode-line-function
1123 (todo-current-category))))
1126 (replace-regexp-in-string
1127 (regexp-quote soname
) snname fbname
))))))))))
1128 (setq todo-files
(funcall todo-files-function
)
1129 todo-archives
(funcall todo-files-function t
))
1130 (when (string= todo-default-todo-file soname
)
1131 (setq todo-default-todo-file snname
))
1132 (when (string= todo-global-current-todo-file oname
)
1133 (setq todo-global-current-todo-file nname
))
1134 (todo-reevaluate-filelist-defcustoms)))
1136 (defun todo-delete-file ()
1137 "Delete the current todo, archive or filtered items file.
1138 If the todo file has a corresponding archive file, or vice versa,
1139 prompt whether to delete that as well. Also kill the buffers
1140 visiting the deleted files."
1142 (let* ((file1 (buffer-file-name))
1143 (todo (eq major-mode
'todo-mode
))
1144 (archive (eq major-mode
'todo-archive-mode
))
1145 (filtered (eq major-mode
'todo-filtered-items-mode
))
1146 (file1-sn (todo-short-file-name file1
))
1147 (file2 (concat todo-directory file1-sn
(cond (todo ".toda")
1148 (archive ".todo"))))
1149 (buf1 (current-buffer))
1150 (buf2 (when file2
(find-buffer-visiting file2
)))
1151 (prompt1 (concat "Delete " (cond (todo "todo")
1153 (filtered "filtered items"))
1155 (prompt2 (concat "Also delete the corresponding "
1156 (cond (todo "archive") (archive "todo")) " file "
1157 (when buf2
"and kill the buffer visiting it? ")))
1158 (delete1 (yes-or-no-p (format prompt1 file1-sn
)))
1159 (delete2 (when (and delete1
(or (file-exists-p file2
) buf2
))
1160 (yes-or-no-p prompt2
))))
1162 (when (file-exists-p file1
) (delete-file file1
))
1163 (setq todo-visited
(delete file1 todo-visited
))
1167 (when (file-exists-p file2
) (delete-file file2
))
1168 (setq todo-visited
(delete file2 todo-visited
))
1169 (and buf2
(kill-buffer buf2
)))
1170 ;; If we deleted an archive but not its todo file, update the
1171 ;; latter's category sexp.
1172 (when (equal (file-name-extension file2
) "todo")
1173 (with-current-buffer (or buf2
(find-file-noselect file2
))
1177 (goto-char (point-min))
1178 (let ((sexp (read (buffer-substring-no-properties
1179 (line-beginning-position)
1180 (line-end-position))))
1181 (buffer-read-only nil
))
1182 (mapc (lambda (x) (aset (cdr x
) 3 0)) sexp
)
1183 (delete-region (line-beginning-position) (line-end-position))
1184 (prin1 sexp
(current-buffer)))))
1185 (todo-set-categories)
1186 (unless buf2
(kill-buffer)))))
1187 (setq todo-files
(funcall todo-files-function
)
1188 todo-archives
(funcall todo-files-function t
))
1189 (when (or (string= file1-sn todo-default-todo-file
)
1190 (and delete2
(string= file1-sn todo-default-todo-file
)))
1191 (setq todo-default-todo-file
(todo-short-file-name (car todo-files
))))
1192 (when (or (string= file1 todo-global-current-todo-file
)
1193 (and delete2
(string= file2 todo-global-current-todo-file
)))
1194 (setq todo-global-current-todo-file nil
))
1195 (todo-reevaluate-filelist-defcustoms)
1196 (message (concat (cond (todo "Todo") (archive "Archive")) " file \"%s\" "
1199 (cond (todo "archive") (archive "todo"))
1204 (defvar todo-edit-buffer
"*Todo Edit*"
1205 "Name of current buffer in Todo Edit mode.")
1207 (defun todo-edit-file ()
1208 "Put current buffer in `todo-edit-mode'.
1209 This makes the entire file visible and the buffer writable and
1210 you can use the self-insertion keys and standard Emacs editing
1211 commands to make changes. To return to Todo mode, type
1212 \\[todo-edit-quit]. This runs a file format check, signaling
1213 an error if the format has become invalid. However, this check
1214 cannot tell if the number of items changed, which could result in
1215 the file containing inconsistent information. For this reason
1216 this command should be used with caution."
1221 (display-warning 'todo
(format "\
1223 Type %s to return to Todo mode.
1225 This also runs a file format check and signals an error if
1226 the format has become invalid. However, this check cannot
1227 tell if the number of items or categories changed, which
1228 could result in the file containing inconsistent information.
1229 You can repair this inconsistency by invoking the command
1230 `todo-repair-categories-sexp', but this will revert any
1231 renumbering of the categories you have made, so you will
1232 have to renumber them again (see `(todo-mode) Reordering
1233 Categories')." (substitute-command-keys "\\[todo-edit-quit]"))))
1235 (defun todo-add-category (&optional file cat
)
1236 "Add a new category to a todo file.
1238 Called interactively with prefix argument FILE, prompt for a file
1239 and then for a new category to add to that file, otherwise prompt
1240 just for a category to add to the current todo file. After
1241 adding the category, visit it in Todo mode and if option
1242 `todo-add-item-if-new-category' is non-nil (the default), prompt
1245 Non-interactively, add category CAT to file FILE; if FILE is nil,
1246 add CAT to the current todo file. After adding the category,
1247 return the new category number."
1250 ;; If cat is passed from caller, don't prompt, unless it is "",
1251 ;; which means the file was just added and has no category yet.
1252 (if (and cat
(> (length cat
) 0))
1253 (setq file0
(or (and (stringp file
) file
)
1254 todo-current-todo-file
))
1255 (setq catfil
(todo-read-category "Enter a new category name: "
1256 'add
(when (called-interactively-p 'any
)
1259 file0
(if (called-interactively-p 'any
)
1263 (let ((counts (make-vector 4 0)) ; [todo diary done archived]
1264 (num (1+ (length todo-categories
)))
1265 (buffer-read-only nil
))
1266 (setq todo-current-todo-file file0
)
1267 (setq todo-categories
(append todo-categories
1268 (list (cons cat counts
))))
1270 (goto-char (point-max))
1271 (save-excursion ; Save point for todo-category-select.
1272 (insert todo-category-beg cat
"\n\n" todo-category-done
"\n"))
1273 (todo-update-categories-sexp)
1274 ;; If invoked by user, display the newly added category, if
1275 ;; called programmatically return the category number to the
1277 (if (called-interactively-p 'any
)
1279 (setq todo-category-number num
)
1280 (todo-category-select)
1281 (when todo-add-item-if-new-category
1282 (todo-insert-item--basic)))
1285 (defun todo-rename-category ()
1286 "Rename current todo category.
1287 If this file has an archive containing this category, rename the
1288 category there as well."
1290 (let* ((cat (todo-current-category))
1291 (new (read-from-minibuffer
1292 (format "Rename category \"%s\" to: " cat
))))
1293 (setq new
(todo-validate-name new
'category
))
1294 (let* ((ofile todo-current-todo-file
)
1295 (archive (concat (file-name-sans-extension ofile
) ".toda"))
1296 (buffers (append (list ofile
)
1297 (unless (zerop (todo-get-count 'archived cat
))
1299 (dolist (buf buffers
)
1300 (with-current-buffer (find-file-noselect buf
)
1301 (let (buffer-read-only)
1302 (setq todo-categories
(todo-set-categories))
1305 (setcar (assoc cat todo-categories
) new
)
1307 (goto-char (point-min))
1308 (todo-update-categories-sexp)
1309 (re-search-forward (concat (regexp-quote todo-category-beg
)
1310 "\\(" (regexp-quote cat
) "\\)\n")
1312 (replace-match new t t nil
1)))))))
1313 (force-mode-line-update))
1314 (save-excursion (todo-category-select)))
1316 (defun todo-delete-category (&optional arg
)
1317 "Delete current todo category provided it contains no items.
1318 With prefix ARG delete the category even if it does contain
1319 todo or done items."
1321 (let* ((file todo-current-todo-file
)
1322 (cat (todo-current-category))
1323 (todo (todo-get-count 'todo cat
))
1324 (done (todo-get-count 'done cat
))
1325 (archived (todo-get-count 'archived cat
)))
1327 (or (> todo
0) (> done
0)))
1328 (message "%s" (substitute-command-keys
1329 (concat "To delete a non-empty category, "
1330 "type C-u \\[todo-delete-category].")))
1331 (when (cond ((= (length todo-categories
) 1)
1333 (concat "This is the only category in this file; "
1334 "deleting it will also delete the file.\n"
1335 "Do you want to proceed? ")))
1337 (todo-y-or-n-p (concat "This category has archived items; "
1338 "the archived category will remain\n"
1339 "after deleting the todo category. "
1340 "Do you still want to delete it\n"
1341 "(see `todo-skip-archived-categories' "
1342 "for another option)? ")))
1344 (todo-y-or-n-p (concat "Permanently remove category \"" cat
1345 "\"" (and arg
" and all its entries")
1348 (let ((buffer-read-only)
1349 (beg (re-search-backward
1350 (concat "^" (regexp-quote (concat todo-category-beg cat
))
1352 (end (if (re-search-forward
1353 (concat "\n\\(" (regexp-quote todo-category-beg
)
1357 (remove-overlays beg end
)
1358 (delete-region beg end
)
1359 (if (= (length todo-categories
) 1)
1360 ;; If deleted category was the only one, delete the file.
1362 (todo-reevaluate-filelist-defcustoms)
1363 ;; Skip confirming killing the archive buffer if it has been
1364 ;; modified and not saved.
1365 (set-buffer-modified-p nil
)
1368 (message "Deleted todo file %s." file
))
1369 (setq todo-categories
(delete (assoc cat todo-categories
)
1371 (todo-update-categories-sexp)
1372 (setq todo-category-number
1373 (1+ (mod todo-category-number
(length todo-categories
))))
1374 (todo-category-select)
1375 (goto-char (point-min))
1376 (message "Deleted category %s." cat
)))))))
1378 (defun todo-move-category ()
1379 "Move current category to a different todo file.
1380 If the todo file chosen does not exist, it is created.
1381 If the current category has archived items, also move those to
1382 the archive of the file moved to, creating it if it does not exist."
1384 (when (or (> (length todo-categories
) 1)
1385 (todo-y-or-n-p (concat "This is the only category in this file; "
1386 "moving it will also delete the file.\n"
1387 "Do you want to proceed? ")))
1388 (let* ((ofile todo-current-todo-file
)
1389 (cat (todo-current-category))
1390 (nfile (todo-read-file-name "Todo file to move this category to: "))
1391 (archive (concat (file-name-sans-extension ofile
) ".toda"))
1392 (buffers (append (list ofile
)
1393 (unless (zerop (todo-get-count 'archived cat
))
1396 (while (equal nfile
(file-truename ofile
))
1397 (setq nfile
(todo-read-file-name
1398 "Choose a file distinct from this file: ")))
1399 (unless (member nfile todo-files
)
1400 (with-current-buffer (get-buffer-create nfile
)
1402 (write-region (point-min) (point-max) nfile nil
'nomessage nil t
)
1403 (kill-buffer nfile
))
1404 (setq todo-files
(funcall todo-files-function
))
1405 (todo-reevaluate-filelist-defcustoms))
1406 (dolist (buf buffers
)
1407 (with-current-buffer (find-file-noselect buf
)
1409 (goto-char (point-max))
1410 (let* ((beg (re-search-backward
1412 (regexp-quote (concat todo-category-beg cat
))
1415 (end (if (re-search-forward
1416 (concat "^" (regexp-quote todo-category-beg
))
1420 (content (buffer-substring-no-properties beg end
))
1421 (counts (cdr (assoc cat todo-categories
)))
1423 ;; Move the category to the new file. Also update or create
1424 ;; archive file if necessary.
1425 (with-current-buffer
1427 ;; Regenerate todo-archives in case there
1428 ;; is a newly created archive.
1429 (if (member buf
(funcall todo-files-function t
))
1430 (concat (file-name-sans-extension nfile
) ".toda")
1432 (if (equal (file-name-extension (buffer-file-name)) "toda")
1433 (unless (derived-mode-p 'todo-archive-mode
)
1434 (todo-archive-mode))
1435 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
1436 (let* ((nfile-short (todo-short-file-name nfile
))
1438 (format "Todo file \"%s\" already has "
1440 (format "the category \"%s\";\n" cat
)
1441 "enter a new category name: "))
1444 (goto-char (point-max))
1446 ;; If the file moved to has a category with the same
1447 ;; name, rename the moved category.
1448 (when (assoc cat todo-categories
)
1449 (unless (member (file-truename (buffer-file-name))
1450 (funcall todo-files-function t
))
1451 (setq new
(read-from-minibuffer prompt
))
1452 (setq new
(todo-validate-name new
'category
))))
1453 ;; Replace old with new name in todo and archive files.
1455 (goto-char (point-max))
1457 (concat "^" (regexp-quote todo-category-beg
)
1458 "\\(" (regexp-quote cat
) "\\)$") nil t
)
1459 (replace-match new nil nil nil
1)))
1460 (setq todo-categories
1461 (append todo-categories
(list (cons (or new cat
) counts
))))
1462 (todo-update-categories-sexp)
1463 ;; If archive was just created, save it to avoid "File
1464 ;; <xyz> no longer exists!" message on invoking
1465 ;; `todo-view-archived-items'.
1466 (unless (file-exists-p (buffer-file-name))
1468 (todo-category-number (or new cat
))
1469 (todo-category-select))
1470 ;; Delete the category from the old file, and if that was the
1471 ;; last category, delete the file. Also handle archive file
1473 (remove-overlays beg end
)
1474 (delete-region beg end
)
1475 (goto-char (point-min))
1476 ;; Put point after todo-categories sexp.
1478 (if (eobp) ; Aside from sexp, file is empty.
1480 ;; Skip confirming killing the archive buffer.
1481 (set-buffer-modified-p nil
)
1482 (delete-file todo-current-todo-file
)
1484 (when (member todo-current-todo-file todo-files
)
1485 (todo-reevaluate-filelist-defcustoms)))
1486 (setq todo-categories
(delete (assoc cat todo-categories
)
1488 (todo-update-categories-sexp)
1489 (when (> todo-category-number
(length todo-categories
))
1490 (setq todo-category-number
1))
1491 (todo-category-select)))))
1492 (set-window-buffer (selected-window)
1493 (set-buffer (find-file-noselect nfile
)))
1494 (todo-category-number (or new cat
))
1495 (todo-category-select))))
1497 (defun todo-merge-category (&optional file
)
1498 "Merge current category into another existing category.
1500 With prefix argument FILE, prompt for a specific todo file and
1501 choose (with TAB completion) a category in it to merge into;
1502 otherwise, choose and merge into a category in either the
1503 current todo file or a file in `todo-category-completions-files'.
1505 After merging, the source category's todo and done items are
1506 appended to the chosen goal category's todo and done items,
1507 respectively. The goal category becomes the current category,
1508 and the source category is deleted.
1510 If both the source and goal categories also have archived items,
1511 they are also merged. If only the source category has archived
1512 items, the goal category is added as a new category to the
1513 archive file and the source category is deleted."
1515 (let* ((tfile todo-current-todo-file
)
1516 (cat (todo-current-category))
1517 (cat+file
(todo-read-category "Merge into category: " 'todo file
))
1518 (goal (car cat
+file
))
1519 (gfile (cdr cat
+file
))
1520 (tarchive (concat (file-name-sans-extension tfile
) ".toda"))
1521 (garchive (concat (file-name-sans-extension gfile
) ".toda"))
1522 (archived-count (todo-get-count 'archived
))
1524 (with-current-buffer (get-buffer (find-file-noselect tfile
))
1526 (let* ((buffer-read-only nil
)
1529 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1531 (tbeg (progn (forward-line) (point-marker)))
1534 (concat "^" (regexp-quote todo-category-done
)) nil t
)
1535 (forward-line) (point-marker)))
1536 ;; Omit empty line between todo and done items.
1537 (tend (progn (forward-line -
2) (point-marker)))
1539 (if (re-search-forward
1540 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1542 (goto-char (match-beginning 0))
1544 (point-max-marker))))
1545 (todo (buffer-substring-no-properties tbeg tend
))
1546 (done (buffer-substring-no-properties dbeg cend
))
1547 (todo-count (todo-get-count 'todo cat
))
1548 (done-count (todo-get-count 'done cat
)))
1549 ;; Merge into goal todo category.
1550 (with-current-buffer (get-buffer (find-file-noselect gfile
))
1551 (unless (derived-mode-p 'todo-mode
) (todo-mode))
1553 (goto-char (point-min))
1554 (let ((buffer-read-only nil
))
1555 ;; Merge any todo items.
1556 (unless (zerop (length todo
))
1558 (concat "^" (regexp-quote (concat todo-category-beg goal
)) "$")
1561 (concat "^" (regexp-quote todo-category-done
)) nil t
)
1563 (setq here
(point-marker))
1565 (todo-update-count 'todo todo-count goal
))
1566 ;; Merge any done items.
1567 (unless (zerop (length done
))
1568 (goto-char (if (re-search-forward
1569 (concat "^" (regexp-quote todo-category-beg
))
1573 (when (zerop (length todo
)) (setq here
(point-marker)))
1575 (todo-update-count 'done done-count goal
)))
1576 (todo-update-categories-sexp))
1577 ;; Update and clean up source todo file.
1578 (remove-overlays cbeg cend
)
1579 (delete-region cbeg cend
)
1580 (setq todo-categories
(delete (assoc cat todo-categories
)
1582 (todo-update-categories-sexp)
1583 (when (> todo-category-number
(length todo-categories
))
1584 (setq todo-category-number
1))
1585 (todo-category-select)
1586 (mapc (lambda (m) (set-marker m nil
))
1587 (list cbeg tbeg dbeg tend cend
))))
1588 (when (> archived-count
0)
1589 (with-current-buffer (get-buffer (find-file-noselect tarchive
))
1591 (goto-char (point-min))
1592 (let* ((buffer-read-only nil
)
1594 (when (re-search-forward
1595 (concat "^" (regexp-quote
1596 (concat todo-category-beg cat
)) "$")
1598 (goto-char (match-beginning 0))
1600 (cend (if (re-search-forward
1601 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
1607 (buffer-substring-no-properties (point) cend
))))
1608 ;; Merge into goal archive category, if it exists, else create it.
1609 (with-current-buffer (get-buffer (find-file-noselect garchive
))
1610 (let ((gbeg (when (re-search-forward
1611 (concat "^" (regexp-quote
1612 (concat todo-category-beg goal
))
1615 (goto-char (match-beginning 0))
1617 (goto-char (if (and gbeg
1619 (concat "^" (regexp-quote todo-category-beg
))
1623 (unless gbeg
(todo-add-category nil goal
))
1625 (todo-update-categories-sexp)))
1626 ;; Update and clean up source archive file.
1627 (remove-overlays cbeg cend
)
1628 (delete-region cbeg cend
)
1629 (setq todo-categories
(todo-make-categories-list t
))
1630 (todo-update-categories-sexp))))
1631 ;; Update goal todo file for merged archived items and display it.
1632 (set-window-buffer (selected-window) (set-buffer (get-file-buffer gfile
)))
1633 (unless (zerop archived-count
)
1634 (todo-update-count 'archived archived-count goal
)
1635 (todo-update-categories-sexp))
1636 (todo-category-number goal
)
1637 ;; If there are only merged done items, show them.
1638 (let ((todo-show-with-done (zerop (todo-get-count 'todo goal
))))
1639 (todo-category-select)
1640 ;; Put point on the first merged item.
1642 (set-marker here nil
)))
1644 ;; -----------------------------------------------------------------------------
1646 ;; -----------------------------------------------------------------------------
1648 (defcustom todo-include-in-diary nil
1649 "Non-nil to allow new todo items to be included in the diary."
1653 (defcustom todo-diary-nonmarking nil
1654 "Non-nil to insert new todo diary items as nonmarking by default.
1655 This appends `diary-nonmarking-symbol' to the front of an item on
1656 insertion provided it doesn't begin with `todo-nondiary-marker'."
1660 (defcustom todo-always-add-time-string nil
1661 "Non-nil adds current time to a new item's date header by default.
1662 When the todo insertion commands have a non-nil \"maybe-notime\"
1663 argument, this reverses the effect of
1664 `todo-always-add-time-string': if t, these commands omit the
1665 current time, if nil, they include it."
1669 (defcustom todo-use-only-highlighted-region t
1670 "Non-nil to enable inserting only highlighted region as new item."
1674 (defcustom todo-default-priority
'first
1675 "Default priority of new and moved items."
1676 :type
'(choice (const :tag
"Highest priority" first
)
1677 (const :tag
"Lowest priority" last
))
1680 (defcustom todo-item-mark
"*"
1681 "String used to mark items.
1682 To ensure item marking works, change the value of this option
1683 only when no items are marked."
1684 :type
'(string :validate
1686 (when (string= (widget-value widget
) todo-prefix
)
1689 "Invalid value: must be distinct from `todo-prefix'")
1691 :set
(lambda (symbol value
)
1692 (custom-set-default symbol
(propertize value
'face
'todo-mark
)))
1695 (defcustom todo-comment-string
"COMMENT"
1696 "String inserted before optional comment appended to done item."
1698 :initialize
'custom-initialize-default
1699 :set
'todo-reset-comment-string
1702 (defcustom todo-undo-item-omit-comment
'ask
1703 "Whether to omit done item comment on undoing the item.
1704 Nil means never omit the comment, t means always omit it, `ask'
1705 means prompt user and omit comment only on confirmation."
1706 :type
'(choice (const :tag
"Never" nil
)
1707 (const :tag
"Always" t
)
1708 (const :tag
"Ask" ask
))
1711 (defun todo-toggle-mark-item (&optional n
)
1712 "Mark item with `todo-item-mark' if unmarked, otherwise unmark it.
1713 With positive numerical prefix argument N, change the marking of
1714 the next N items in the current category. If both the todo and
1715 done items sections are visible, the sequence of N items can
1716 consist of the the last todo items and the first done items."
1718 (when (todo-item-string)
1719 (unless (> n
1) (setq n
1))
1722 (let* ((cat (todo-current-category))
1723 (marks (assoc cat todo-categories-with-marks
))
1725 (unless (looking-at todo-item-start
)
1727 (todo-get-overlay 'prefix
)))
1728 (pref (overlay-get ov
'before-string
)))
1729 (if (todo-marked-item-p)
1731 (overlay-put ov
'before-string
(substring pref
1))
1732 (if (= (cdr marks
) 1) ; Deleted last mark in this category.
1733 (setq todo-categories-with-marks
1734 (assq-delete-all cat todo-categories-with-marks
))
1735 (setcdr marks
(1- (cdr marks
)))))
1736 (overlay-put ov
'before-string
(concat todo-item-mark pref
))
1738 (setcdr marks
(1+ (cdr marks
)))
1739 (push (cons cat
1) todo-categories-with-marks
))))
1741 ;; Don't try to mark the empty lines at the end of the todo
1742 ;; and done items sections.
1743 (when (looking-at "^$")
1746 (todo-forward-item)))))))
1748 (defun todo-mark-category ()
1749 "Mark all visible items in this category with `todo-item-mark'."
1751 (let* ((cat (todo-current-category))
1752 (marks (assoc cat todo-categories-with-marks
)))
1754 (goto-char (point-min))
1756 (let* ((ov (todo-get-overlay 'prefix
))
1757 (pref (overlay-get ov
'before-string
)))
1758 (unless (todo-marked-item-p)
1759 (overlay-put ov
'before-string
(concat todo-item-mark pref
))
1761 (setcdr marks
(1+ (cdr marks
)))
1762 (push (cons cat
1) todo-categories-with-marks
))))
1764 ;; Don't try to mark the empty line between the todo and done
1766 (when (looking-at "^$")
1768 (todo-forward-item)))))))
1770 (defun todo-unmark-category ()
1771 "Remove `todo-item-mark' from all visible items in this category."
1773 (let* ((cat (todo-current-category))
1774 (marks (assoc cat todo-categories-with-marks
)))
1776 (goto-char (point-min))
1778 (let* ((ov (todo-get-overlay 'prefix
))
1779 ;; No overlay on empty line between todo and done items.
1780 (pref (when ov
(overlay-get ov
'before-string
))))
1781 (when (todo-marked-item-p)
1782 (overlay-put ov
'before-string
(substring pref
1)))
1783 (todo-forward-item))))
1784 (setq todo-categories-with-marks
1785 (delq marks todo-categories-with-marks
))))
1787 (defvar todo-date-from-calendar nil
1788 "Helper variable for setting item date from the Emacs Calendar.")
1790 (defvar todo-insert-item--keys-so-far
)
1791 (defvar todo-insert-item--parameters
)
1793 (defun todo-insert-item (&optional arg
)
1794 "Choose an item insertion operation and carry it out.
1795 This inserts a new todo item into a category.
1797 With no prefix argument ARG, add the item to the current
1798 category; with one prefix argument (`C-u'), prompt for a category
1799 from the current todo file; with two prefix arguments (`C-u
1800 C-u'), first prompt for a todo file, then a category in that
1801 file. If a non-existing category is entered, ask whether to add
1802 it to the todo file; if answered affirmatively, add the category
1803 and insert the item there.
1805 There are a number of item insertion parameters which can be
1806 combined by entering specific keys to produce different insertion
1807 commands. After entering each key, a message shows which have
1808 already been entered and which remain available. See
1809 `(todo-mode) Inserting New Items' for details of the parameters,
1810 their associated keys and their effects."
1812 (setq todo-insert-item--keys-so-far
"i")
1813 (todo-insert-item--next-param nil
(list arg
) todo-insert-item--parameters
))
1815 (defun todo-insert-item--basic (&optional arg diary-type date-type time where
)
1816 "Function implementing the core of `todo-insert-item'."
1817 ;; If invoked outside of Todo mode and there is not yet any Todo
1818 ;; file, initialize one.
1819 (if (null (funcall todo-files-function
))
1821 (let ((copy (eq where
'copy
))
1822 (region (eq where
'region
))
1823 (here (eq where
'here
))
1827 ((not (eq major-mode
'todo-mode
))
1828 (user-error "You must be in Todo mode to copy a todo item"))
1830 (user-error "You cannot copy a done item as a new todo item"))
1832 (user-error "Point must be on a todo item to copy it")))
1833 (setq diary-item
(todo-diary-item-p)))
1835 (let (use-empty-active-region)
1836 (unless (and todo-use-only-highlighted-region
(use-region-p))
1837 (user-error "There is no active region"))))
1838 (let* ((obuf (current-buffer))
1839 (ocat (todo-current-category))
1841 (todo-mm (eq major-mode
'todo-mode
))
1842 (cat+file
(cond ((equal arg
'(4))
1843 (todo-read-category "Insert in category: "))
1845 (todo-read-category "Insert in category: "
1848 (cons (todo-current-category)
1849 (or todo-current-todo-file
1850 (and todo-show-current-file
1851 todo-global-current-todo-file
)
1852 (todo-absolute-file-name
1853 todo-default-todo-file
))))))
1854 (cat (car cat
+file
))
1855 (file (cdr cat
+file
))
1856 (new-item (cond (copy (todo-item-string))
1857 (region (buffer-substring-no-properties
1858 (region-beginning) (region-end)))
1859 (t (read-from-minibuffer "Todo item: "))))
1861 ((eq date-type
'date
)
1863 ((eq date-type
'dayname
)
1864 (todo-read-dayname))
1865 ((eq date-type
'calendar
)
1866 (setq todo-date-from-calendar t
)
1867 (or (todo-set-date-from-calendar)
1868 ;; If user exits Calendar before choosing
1869 ;; a date, cancel item insertion.
1871 ((and (stringp date-type
)
1872 (string-match todo-date-pattern date-type
))
1873 (setq todo-date-from-calendar date-type
)
1874 (todo-set-date-from-calendar))
1876 (calendar-date-string
1877 (calendar-current-date) t t
))))
1878 (time-string (or (and time
(todo-read-time))
1879 (and todo-always-add-time-string
1880 (substring (current-time-string) 11 16)))))
1881 (setq todo-date-from-calendar nil
)
1882 (find-file-noselect file
'nowarn
)
1883 (set-window-buffer (selected-window)
1884 (set-buffer (find-buffer-visiting file
)))
1885 ;; If this command was invoked outside of a Todo mode buffer,
1886 ;; the call to todo-current-category above returned nil. If
1887 ;; we just entered Todo mode now, then cat was set to the
1888 ;; file's first category, but if todo-mode was already
1889 ;; enabled, cat did not get set, so we have to do that.
1891 (setq cat
(todo-current-category)))
1892 (setq todo-current-todo-file file
)
1893 (unless todo-global-current-todo-file
1894 (setq todo-global-current-todo-file todo-current-todo-file
))
1895 (let ((buffer-read-only nil
)
1896 (called-from-outside (not (and todo-mm
(equal cat ocat
))))
1897 done-only item-added
)
1900 ;; Add date, time and diary marking as required.
1901 (concat (if (not (and diary-type
1902 (not todo-include-in-diary
)))
1904 (when (and (eq diary-type
'nonmarking
)
1905 (not todo-diary-nonmarking
))
1906 diary-nonmarking-symbol
))
1907 date-string
(when (and time-string
; Can be empty.
1910 (concat " " time-string
))
1911 (when (not (and diary-type
1912 (not todo-include-in-diary
)))
1915 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
1916 (setq new-item
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
1917 "\n\t" new-item nil nil
1)))
1920 ;; Make sure the correct category is selected. There
1921 ;; are two cases: (i) we just visited the file, so no
1922 ;; category is selected yet, or (ii) we invoked
1923 ;; insertion "here" from outside the category we want
1924 ;; to insert in (with priority insertion, category
1925 ;; selection is done by todo-set-item-priority).
1926 (when (or (= (- (point-max) (point-min)) (buffer-size))
1927 (and here called-from-outside
))
1928 (todo-category-number cat
)
1929 (todo-category-select))
1930 ;; If only done items are displayed in category,
1931 ;; toggle to todo items before inserting new item.
1932 (when (save-excursion
1933 (goto-char (point-min))
1934 (looking-at todo-done-string-start
))
1936 (todo-toggle-view-done-only))
1939 ;; If command was invoked with point in done
1940 ;; items section or outside of the current
1941 ;; category, can't insert "here", so to be
1942 ;; useful give new item top priority.
1943 (when (or (todo-done-item-section-p)
1946 (goto-char (point-min)))
1947 (todo-insert-with-overlays new-item
))
1948 (todo-set-item-priority new-item cat t
))
1949 (setq item-added t
))
1950 ;; If user cancels before setting priority, restore
1953 (set-window-buffer (selected-window) (set-buffer obuf
))
1955 (unless (equal cat ocat
)
1956 (todo-category-number ocat
)
1957 (todo-category-select))
1958 (and done-only
(todo-toggle-view-done-only)))
1960 ;; If the todo items section is not visible when the
1961 ;; insertion command is called (either because only done
1962 ;; items were shown or because the category was not in the
1963 ;; current buffer), then if the item is inserted at the
1964 ;; end of the category, point is at eob and eob at
1965 ;; window-start, so that higher priority todo items are
1966 ;; out of view. So we recenter to make sure the todo
1967 ;; items are displayed in the window.
1968 (when item-added
(recenter)))
1969 (todo-update-count 'todo
1)
1970 (when (or diary-item diary-type todo-include-in-diary
)
1971 (todo-update-count 'diary
1))
1972 (todo-update-categories-sexp))))))
1974 (defun todo-set-date-from-calendar ()
1975 "Return string of date chosen from Calendar."
1976 (cond ((and (stringp todo-date-from-calendar
)
1977 (string-match todo-date-pattern todo-date-from-calendar
))
1978 todo-date-from-calendar
)
1979 (todo-date-from-calendar
1980 (let (calendar-view-diary-initially-flag)
1981 (calendar)) ; *Calendar* is now current buffer.
1982 (define-key calendar-mode-map
[remap newline
] 'exit-recursive-edit
)
1983 ;; If user exits Calendar before choosing a date, clean up properly.
1984 (define-key calendar-mode-map
1985 [remap calendar-exit
] (lambda ()
1989 (exit-recursive-edit))))
1990 (message "Put cursor on a date and type <return> to set it.")
1993 (when (equal (buffer-name) calendar-buffer
)
1994 (setq todo-date-from-calendar
1995 (calendar-date-string (calendar-cursor-to-date t
) t t
))
1997 todo-date-from-calendar
)
1998 (define-key calendar-mode-map
[remap newline
] nil
)
1999 (define-key calendar-mode-map
[remap calendar-exit
] nil
)
2000 (unless (zerop (recursion-depth)) (exit-recursive-edit))
2001 (when (stringp todo-date-from-calendar
)
2002 todo-date-from-calendar
)))))
2004 (defun todo-insert-item-from-calendar (&optional arg
)
2005 "Prompt for and insert a new item with date selected from calendar.
2006 Invoked without prefix argument ARG, insert the item into the
2007 current category, without one prefix argument, prompt for the
2008 category from the current todo file or from one listed in
2009 `todo-category-completions-files'; with two prefix arguments,
2010 prompt for a todo file and then for a category in it."
2012 (setq todo-date-from-calendar
2013 (calendar-date-string (calendar-cursor-to-date t
) t t
))
2015 (todo-insert-item--basic arg nil todo-date-from-calendar
))
2017 (define-key calendar-mode-map
"it" 'todo-insert-item-from-calendar
)
2019 (defun todo-delete-item ()
2020 "Delete at least one item in this category.
2021 If there are marked items, delete all of these; otherwise, delete
2026 (let* ((cat (todo-current-category))
2027 (marked (assoc cat todo-categories-with-marks
))
2028 (item (unless marked
(todo-item-string)))
2031 "Permanently delete all marked items? ")
2033 (setq ov
(make-overlay
2034 (save-excursion (todo-item-start))
2035 (save-excursion (todo-item-end))))
2036 (overlay-put ov
'face
'todo-search
)
2037 (todo-y-or-n-p "Permanently delete this item? "))))
2040 (and marked
(goto-char (point-min)))
2043 (if (or (and marked
(todo-marked-item-p)) item
)
2045 (if (todo-done-item-p)
2046 (todo-update-count 'done -
1)
2047 (todo-update-count 'todo -
1 cat
)
2048 (and (todo-diary-item-p)
2049 (todo-update-count 'diary -
1)))
2050 (if ov
(delete-overlay ov
))
2052 ;; Don't leave point below last item.
2053 (and item
(bolp) (eolp) (< (point-min) (point-max))
2054 (todo-backward-item))
2056 (throw 'done
(setq item nil
))))
2057 (todo-forward-item))))
2059 (setq todo-categories-with-marks
2060 (assq-delete-all cat todo-categories-with-marks
)))
2061 (todo-update-categories-sexp)
2062 (todo-prefix-overlays)))
2063 (if ov
(delete-overlay ov
)))))
2065 (defvar todo-edit-item--param-key-alist
)
2066 (defvar todo-edit-done-item--param-key-alist
)
2068 (defun todo-edit-item (&optional arg
)
2069 "Choose an editing operation for the current item and carry it out."
2071 (let ((marked (assoc (todo-current-category) todo-categories-with-marks
)))
2072 (cond ((and (todo-done-item-p) (not marked
))
2073 (todo-edit-item--next-key todo-edit-done-item--param-key-alist
))
2074 ((or marked
(todo-item-string))
2075 (todo-edit-item--next-key todo-edit-item--param-key-alist arg
)))))
2077 (defun todo-edit-item--text (&optional arg
)
2078 "Function providing the text editing facilities of `todo-edit-item'."
2079 (let ((full-item (todo-item-string)))
2080 ;; If there are marked items and user invokes a text-editing
2081 ;; commands with point not on an item, todo-item-start is nil and
2082 ;; 1+ signals an error, so just make this a noop.
2084 (let* ((opoint (point))
2085 (start (todo-item-start))
2086 (end (save-excursion (todo-item-end)))
2089 (concat todo-date-string-start todo-date-pattern
2090 "\\( " diary-time-regexp
"\\)?"
2091 (regexp-quote todo-nondiary-end
) "?")
2092 (line-end-position) t
)
2093 (1+ (- (point) start
))))
2094 (include-header (eq arg
'include-header
))
2095 (comment-edit (eq arg
'comment-edit
))
2096 (comment-delete (eq arg
'comment-delete
))
2097 (header-string (substring full-item
0 item-beg
))
2098 (item (if (or include-header comment-edit comment-delete
)
2100 (substring full-item item-beg
)))
2101 (multiline (or (eq arg
'multiline
)
2102 (> (length (split-string item
"\n")) 1)))
2103 (comment (save-excursion
2106 (concat " \\[" (regexp-quote todo-comment-string
)
2107 ": \\([^]]+\\)\\]") end t
)))
2108 (prompt (if comment
"Edit comment: " "Enter a comment: "))
2109 (buffer-read-only nil
))
2110 ;; When there are marked items, user can invoke todo-edit-item
2111 ;; even if point is not on an item, but text editing only
2112 ;; applies to the item at point.
2113 (when (or (and (todo-done-item-p)
2114 (or comment-edit comment-delete
))
2115 (and (not (todo-done-item-p))
2116 (or (not arg
) include-header multiline
)))
2118 ((or comment-edit comment-delete
)
2121 (if (re-search-forward (concat " \\["
2122 (regexp-quote todo-comment-string
)
2123 ": \\([^]]+\\)\\]") end t
)
2125 (when (todo-y-or-n-p "Delete comment? ")
2126 (delete-region (match-beginning 0) (match-end 0)))
2127 (replace-match (read-string prompt
(cons (match-string 1) 1))
2130 (user-error "There is no comment to delete")
2131 (insert " [" todo-comment-string
": "
2132 (prog1 (read-string prompt
)
2133 ;; If user moved point during editing,
2134 ;; make sure it moves back.
2139 (let ((buf todo-edit-buffer
))
2140 (set-window-buffer (selected-window)
2141 (set-buffer (make-indirect-buffer
2142 (buffer-name) buf
)))
2143 (narrow-to-region (todo-item-start) (todo-item-end))
2145 (message "%s" (substitute-command-keys
2146 (concat "Type \\[todo-edit-quit] "
2147 "to return to Todo mode.\n")))))
2149 (let ((new (concat (if include-header
"" header-string
)
2150 (read-string "Edit: " (if include-header
2151 (cons item item-beg
)
2153 (when include-header
2154 (while (not (string-match (concat todo-date-string-start
2155 todo-date-pattern
) new
))
2156 (setq new
(read-from-minibuffer
2157 "Item must start with a date: " new
))))
2158 ;; Ensure lines following hard newlines are indented.
2159 (setq new
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
2160 "\n\t" new nil nil
1))
2161 ;; If user moved point during editing, make sure it moves back.
2164 (todo-insert-with-overlays new
)
2165 (move-to-column item-beg
)))))))))
2167 (defun todo-edit-quit ()
2168 "Return from Todo Edit mode to Todo mode.
2169 If the item contains hard line breaks, make sure the following
2170 lines are indented by `todo-indent-to-here' to conform to diary
2173 If the whole file was in Todo Edit mode, check before returning
2174 whether the file is still a valid todo file and if so, also
2175 recalculate the todo file's categories sexp, in case changes were
2176 made in the number or names of categories."
2178 (if (> (buffer-size) (- (point-max) (point-min)))
2179 ;; We got here via `e m'.
2180 (let ((item (buffer-string))
2181 (regex "\\(\n\\)[^[:blank:]]")
2182 (buf (buffer-base-buffer)))
2183 (while (not (string-match (concat todo-date-string-start
2184 todo-date-pattern
) item
))
2185 (setq item
(read-from-minibuffer
2186 "Item must start with a date: " item
)))
2187 ;; Ensure lines following hard newlines are indented.
2188 (when (string-match regex
(buffer-string))
2189 (setq item
(replace-regexp-in-string regex
"\n\t" item nil nil
1))
2190 (delete-region (point-min) (point-max))
2193 (unless (eq (current-buffer) buf
)
2194 (set-window-buffer (selected-window) (set-buffer buf
))))
2195 ;; We got here via `F e'.
2196 (when (todo-check-format)
2197 ;; FIXME: separate out sexp check?
2198 ;; If manual editing makes e.g. item counts change, have to
2199 ;; call this to update todo-categories, but it restores
2200 ;; category order to list order.
2201 ;; (todo-repair-categories-sexp)
2202 ;; Compare (todo-make-categories-list t) with sexp and if
2203 ;; different ask (todo-update-categories-sexp) ?
2205 (let* ((cat-beg (concat "^" (regexp-quote todo-category-beg
)
2207 (curline (buffer-substring-no-properties
2208 (line-beginning-position) (line-end-position)))
2209 (cat (cond ((string-match cat-beg curline
)
2210 (match-string-no-properties 1 curline
))
2211 ((or (re-search-backward cat-beg nil t
)
2212 (re-search-forward cat-beg nil t
))
2213 (match-string-no-properties 1)))))
2214 (todo-category-number cat
)
2215 (todo-category-select)
2216 (goto-char (point-min))))))
2218 (defun todo-edit-item--header (what &optional inc
)
2219 "Function providing header editing facilities of `todo-edit-item'."
2220 (let ((marked (assoc (todo-current-category) todo-categories-with-marks
))
2222 (todo-date-from-calendar t
)
2223 ;; INC must be an integer, but users could pass it via
2224 ;; `todo-edit-item' as e.g. `-' or `C-u'.
2225 (inc (prefix-numeric-value inc
))
2226 (buffer-read-only nil
)
2227 ndate ntime year monthname month day
2228 dayname
) ; Needed by calendar-date-display-form.
2229 (when marked
(todo--user-error-if-marked-done-item))
2231 (or (and marked
(goto-char (point-min))) (todo-item-start))
2235 (while (not (todo-marked-item-p))
2237 (and (eobp) (throw 'end nil
))))
2238 (re-search-forward (concat todo-date-string-start
"\\(?1:"
2240 "\\)\\(?2: " diary-time-regexp
"\\)?"
2241 (regexp-quote todo-nondiary-end
) "?")
2242 (line-end-position) t
)
2243 (let* ((odate (match-string-no-properties 1))
2244 (otime (match-string-no-properties 2))
2245 (odayname (match-string-no-properties 5))
2246 (omonthname (match-string-no-properties 6))
2247 (omonth (match-string-no-properties 7))
2248 (oday (match-string-no-properties 8))
2249 (oyear (match-string-no-properties 9))
2250 (tmn-array todo-month-name-array
)
2251 (mlist (append tmn-array nil
))
2252 (tma-array todo-month-abbrev-array
)
2253 (mablist (append tma-array nil
))
2254 (yy (and oyear
(unless (string= oyear
"*")
2255 (string-to-number oyear
))))
2256 (mm (or (and omonth
(unless (string= omonth
"*")
2257 (string-to-number omonth
)))
2258 (1+ (- (length mlist
)
2259 (length (or (member omonthname mlist
)
2260 (member omonthname mablist
)))))))
2261 (dd (and oday
(unless (string= oday
"*")
2262 (string-to-number oday
)))))
2263 ;; If there are marked items, use only the first to set
2264 ;; header changes, and apply these to all marked items.
2268 (setq ndate
(todo-read-date)))
2269 ((eq what
'calendar
)
2270 (setq ndate
(save-match-data (todo-set-date-from-calendar))))
2272 (setq ndate
(calendar-date-string (calendar-current-date) t t
)))
2274 (setq ndate
(todo-read-dayname)))
2276 (setq ntime
(save-match-data (todo-read-time)))
2277 (when (> (length ntime
) 0)
2278 (setq ntime
(concat " " ntime
))))
2279 ;; When date string consists only of a day name,
2280 ;; passing other date components is a noop.
2281 ((and odayname
(memq what
'(year month day
))))
2284 monthname omonthname
2286 year
(cond ((not current-prefix-arg
)
2287 (todo-read-date 'year
))
2288 ((string= oyear
"*")
2289 (user-error "Cannot increment *"))
2291 (number-to-string (+ yy inc
))))))
2295 (if (memq 'month calendar-date-display-form
)
2298 (cond ((not current-prefix-arg
)
2299 (todo-read-date 'month
))
2300 ((or (string= omonth
"*") (= mm
13))
2301 (user-error "Cannot increment *"))
2303 (let ((mminc (+ mm inc
)))
2304 ;; Increment or decrement month by INC
2306 (setq mm
(% mminc
12))
2307 ;; If result is 0, make month December.
2308 (setq mm
(if (= mm
0) 12 (abs mm
)))
2309 ;; Adjust year if necessary.
2310 (setq year
(or (and (cond ((> mminc
12)
2311 (+ yy
(/ mminc
12)))
2313 (- yy
(/ mminc
12) 1))
2315 (number-to-string yy
))
2317 ;; Return the changed numerical month as
2318 ;; a string or the corresponding month name.
2320 (number-to-string mm
)
2321 (aref tma-array
(1- mm
))))))
2322 (let ((yy (string-to-number year
)) ; 0 if year is "*".
2323 ;; When mm is 13 (corresponding to "*" as value
2324 ;; of month), this raises an args-out-of-range
2325 ;; error in calendar-last-day-of-month, so use 1
2326 ;; (corresponding to January) to get 31 days.
2327 (mm (if (= mm
13) 1 mm
)))
2328 (if (> (string-to-number day
)
2329 (calendar-last-day-of-month mm yy
))
2330 (user-error "%s %s does not have %s days"
2331 (aref tmn-array
(1- mm
))
2332 (if (= mm
2) yy
"") day
))))
2336 monthname omonthname
2338 ((not current-prefix-arg
)
2339 (todo-read-date 'day mm oyear
))
2341 (user-error "Cannot increment *"))
2342 ((or (string= omonth
"*") (string= omonthname
"*"))
2343 (setq dd
(+ dd inc
))
2346 "A month cannot have more than 31 days")
2347 (number-to-string dd
)))
2348 ;; Increment or decrement day by INC,
2349 ;; adjusting month and year if necessary
2350 ;; (if year is "*" assume current year to
2351 ;; calculate adjustment).
2353 (let* ((yy (or yy
(calendar-extract-year
2354 (calendar-current-date))))
2355 (date (calendar-gregorian-from-absolute
2356 (+ (calendar-absolute-from-gregorian
2357 (list mm dd yy
)) inc
)))
2358 (adjmm (nth 0 date
)))
2359 ;; Set year and month(name) to adjusted values.
2360 (unless (string= year
"*")
2361 (setq year
(number-to-string (nth 2 date
))))
2363 (setq month
(number-to-string adjmm
))
2364 (setq monthname
(aref tma-array
(1- adjmm
))))
2365 ;; Return changed numerical day as a string.
2366 (number-to-string (nth 1 date
)))))))))
2368 ;; If year, month or day date string components were
2369 ;; changed, rebuild the date string.
2370 (when (memq what
'(year month day
))
2371 (setq ndate
(mapconcat 'eval calendar-date-display-form
""))))
2372 (when ndate
(replace-match ndate nil nil nil
1))
2373 ;; Add new time string to the header, if it was supplied.
2376 (replace-match ntime nil nil nil
2)
2377 (goto-char (match-end 1))
2379 (setq todo-date-from-calendar nil
)
2381 ;; Apply the changes to the first marked item header to the
2382 ;; remaining marked items. If there are no marked items,
2386 (goto-char (point-max))))))))
2388 (defun todo-edit-item--diary-inclusion (&optional nonmarking
)
2389 "Function providing diary marking facilities of `todo-edit-item'."
2390 (let ((buffer-read-only)
2391 (marked (assoc (todo-current-category) todo-categories-with-marks
)))
2392 (when marked
(todo--user-error-if-marked-done-item))
2395 (when marked
(goto-char (point-min)))
2397 (unless (and marked
(not (todo-marked-item-p)))
2398 (let* ((beg (todo-item-start))
2399 (lim (save-excursion (todo-item-end)))
2400 (end (save-excursion
2401 (or (todo-time-string-matcher lim
)
2402 (todo-date-string-matcher lim
)))))
2404 (if (looking-at (regexp-quote diary-nonmarking-symbol
))
2406 (when (looking-at (regexp-quote todo-nondiary-start
))
2409 (search-forward todo-nondiary-end
(1+ end
) t
)
2411 (todo-update-count 'diary
1)))
2412 (insert diary-nonmarking-symbol
))
2413 (if (looking-at (regexp-quote todo-nondiary-start
))
2416 (search-forward todo-nondiary-end
(1+ end
) t
)
2418 (todo-update-count 'diary
1))
2420 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2422 (setq end
(1- end
))) ; Since we deleted nonmarking symbol.
2423 (insert todo-nondiary-start
)
2424 (goto-char (1+ end
))
2425 (insert todo-nondiary-end
)
2426 (todo-update-count 'diary -
1))))))
2427 (unless marked
(throw 'stop nil
))
2428 (todo-forward-item)))))
2429 (todo-update-categories-sexp))
2431 (defun todo-edit-category-diary-inclusion (arg)
2432 "Make all items in this category diary items.
2433 With prefix ARG, make all items in this category non-diary
2437 (goto-char (point-min))
2438 (let ((todo-count (todo-get-count 'todo
))
2439 (diary-count (todo-get-count 'diary
))
2443 (if (todo-done-item-p) ; We've gone too far.
2445 (let* ((beg (todo-item-start))
2446 (lim (save-excursion (todo-item-end)))
2447 (end (save-excursion
2448 (or (todo-time-string-matcher lim
)
2449 (todo-date-string-matcher lim
)))))
2451 (unless (looking-at (regexp-quote todo-nondiary-start
))
2452 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2454 (setq end
(1- end
))) ; Since we deleted nonmarking symbol.
2455 (insert todo-nondiary-start
)
2456 (goto-char (1+ end
))
2457 (insert todo-nondiary-end
))
2458 (when (looking-at (regexp-quote todo-nondiary-start
))
2460 (search-forward todo-nondiary-end
(1+ end
) t
)
2461 (replace-match "")))))
2462 (todo-forward-item))
2463 (unless (if arg
(zerop diary-count
) (= diary-count todo-count
))
2464 (todo-update-count 'diary
(if arg
2466 (- todo-count diary-count
))))
2467 (todo-update-categories-sexp)))))
2469 (defun todo-edit-category-diary-nonmarking (arg)
2470 "Add `diary-nonmarking-symbol' to all diary items in this category.
2471 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
2472 items in this category."
2475 (goto-char (point-min))
2476 (let (buffer-read-only)
2479 (if (todo-done-item-p) ; We've gone too far.
2481 (unless (looking-at (regexp-quote todo-nondiary-start
))
2483 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
2485 (unless (looking-at (regexp-quote diary-nonmarking-symbol
))
2486 (insert diary-nonmarking-symbol
))))
2487 (todo-forward-item)))))))
2489 (defun todo-set-item-priority (&optional item cat new arg
)
2490 "Prompt for and set ITEM's priority in CATegory.
2492 Interactively, ITEM is the todo item at point, CAT is the current
2493 category, and the priority is a number between 1 and the number
2494 of items in the category. Non-interactively, non-nil NEW means
2495 ITEM is a new item and the lowest priority is one more than the
2496 number of items in CAT.
2498 The new priority is set either interactively by prompt or by a
2499 numerical prefix argument, or noninteractively by argument ARG,
2500 whose value can be either of the symbols `raise' or `lower',
2501 meaning to raise or lower the item's priority by one."
2503 (unless (and (called-interactively-p 'any
)
2504 (or (todo-done-item-p) (looking-at "^$")))
2505 (let* ((item (or item
(todo-item-string)))
2506 (marked (todo-marked-item-p))
2507 (cat (or cat
(cond ((eq major-mode
'todo-mode
)
2508 (todo-current-category))
2509 ((eq major-mode
'todo-filtered-items-mode
)
2511 (concat todo-date-string-start
2513 "\\( " diary-time-regexp
"\\)?"
2514 (regexp-quote todo-nondiary-end
)
2515 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
2517 (re-search-forward regexp1 nil t
)
2518 (match-string-no-properties 1)))))))
2520 (todo (cond ((or (eq arg
'raise
) (eq arg
'lower
)
2521 (eq major-mode
'todo-filtered-items-mode
))
2523 (let ((curstart (todo-item-start))
2525 (goto-char (point-min))
2526 (while (looking-at todo-item-start
)
2527 (setq count
(1+ count
))
2528 (when (= (point) curstart
) (setq curnum count
))
2529 (todo-forward-item))
2531 ((eq major-mode
'todo-mode
)
2532 (todo-get-count 'todo cat
))))
2533 (maxnum (if new
(1+ todo
) todo
))
2534 (prompt (format "Set item priority (1-%d): " maxnum
))
2535 (priority (cond ((and (not arg
) (numberp current-prefix-arg
))
2537 ((and (eq arg
'raise
) (>= curnum
1))
2539 ((and (eq arg
'lower
) (<= curnum maxnum
))
2543 (unless (and priority
2544 (or (and (eq arg
'raise
) (zerop priority
))
2545 (and (eq arg
'lower
) (> priority maxnum
))))
2546 ;; When moving item to another category, show the category before
2547 ;; prompting for its priority.
2548 (unless (or arg
(called-interactively-p 'any
))
2549 (todo-category-number cat
)
2550 ;; If done items in category are visible, keep them visible.
2551 (let ((done todo-show-with-done
))
2552 (when (> (buffer-size) (- (point-max) (point-min)))
2554 (goto-char (point-min))
2555 (setq done
(re-search-forward todo-done-string-start nil t
))))
2556 (let ((todo-show-with-done done
))
2557 ;; Keep current item or top of moved to category in view
2558 ;; while setting priority.
2559 (save-excursion (todo-category-select)))))
2560 ;; Prompt for priority only when the category has at least one
2563 (while (not priority
)
2564 (setq candidate
(read-number prompt
2565 (if (eq todo-default-priority
'first
)
2567 (setq prompt
(when (or (< candidate
1) (> candidate maxnum
))
2568 (format "Priority must be an integer between 1 and %d.\n"
2570 (unless prompt
(setq priority candidate
))))
2571 ;; In Top Priorities buffer, an item's priority can be changed
2572 ;; wrt items in another category, but not wrt items in the same
2574 (when (eq major-mode
'todo-filtered-items-mode
)
2575 (let* ((regexp2 (concat todo-date-string-start todo-date-pattern
2576 "\\( " diary-time-regexp
"\\)?"
2577 (regexp-quote todo-nondiary-end
)
2578 "?\\(?1:" (regexp-quote cat
) "\\)"))
2579 (end (cond ((< curnum priority
)
2580 (save-excursion (todo-item-end)))
2581 ((> curnum priority
)
2582 (save-excursion (todo-item-start)))))
2583 (match (save-excursion
2584 (cond ((< curnum priority
)
2585 (todo-forward-item (1+ (- priority curnum
)))
2586 (when (re-search-backward regexp2 end t
)
2587 (match-string-no-properties 1)))
2588 ((> curnum priority
)
2589 (todo-backward-item (- curnum priority
))
2590 (when (re-search-forward regexp2 end t
)
2591 (match-string-no-properties 1)))))))
2593 (user-error (concat "Cannot reprioritize items from the same "
2594 "category in this mode, only in Todo mode")))))
2595 ;; Interactively or with non-nil ARG, relocate the item within its
2597 (when (or arg
(called-interactively-p 'any
))
2599 (goto-char (point-min))
2601 (unless (= priority
1)
2602 (todo-forward-item (1- priority
))
2603 ;; When called from todo-item-undone and the highest priority
2604 ;; is chosen, this advances point to the first done item, so
2605 ;; move it up to the empty line above the done items
2607 (when (looking-back (concat "^"
2608 (regexp-quote todo-category-done
)
2610 (line-beginning-position 0))
2611 (todo-backward-item))))
2612 (todo-insert-with-overlays item
)
2613 ;; If item was marked, restore the mark.
2615 (let* ((ov (todo-get-overlay 'prefix
))
2616 (pref (overlay-get ov
'before-string
)))
2617 (overlay-put ov
'before-string
2618 (concat todo-item-mark pref
))))))))
2620 (defun todo-raise-item-priority ()
2621 "Raise priority of current item by moving it up by one item."
2623 (todo-set-item-priority nil nil nil
'raise
))
2625 (defun todo-lower-item-priority ()
2626 "Lower priority of current item by moving it down by one item."
2628 (todo-set-item-priority nil nil nil
'lower
))
2630 (defun todo-move-item (&optional file
)
2631 "Move at least one todo or done item to another category.
2632 If there are marked items, move all of these; otherwise, move
2635 With prefix argument FILE, prompt for a specific todo file and
2636 choose (with TAB completion) a category in it to move the item or
2637 items to; otherwise, choose and move to any category in either
2638 the current todo file or one of the files in
2639 `todo-category-completions-files'. If the chosen category is
2640 not an existing categories, then it is created and the item(s)
2641 become(s) the first entry/entries in that category.
2643 With moved todo items, prompt to set the priority in the category
2644 moved to (with multiple todo items, the one that had the highest
2645 priority in the category moved from gets the new priority and the
2646 rest of the moved todo items are inserted in sequence below it).
2647 Moved done items are appended to the top of the done items
2648 section in the category moved to."
2650 (let* ((cat1 (todo-current-category))
2651 (marked (assoc cat1 todo-categories-with-marks
)))
2652 ;; Noop if point is not on an item and there are no marked items.
2653 (unless (and (looking-at "^$")
2655 (let* ((buffer-read-only)
2656 (file1 todo-current-todo-file
)
2657 (num todo-category-number
)
2658 (item (todo-item-string))
2659 (diary-item (todo-diary-item-p))
2660 (done-item (and (todo-done-item-p) (concat item
"\n")))
2661 (omark (save-excursion (todo-item-start) (point-marker)))
2665 ov cat2 file2 moved nmark todo-items done-items
)
2669 (setq ov
(make-overlay (save-excursion (todo-item-start))
2670 (save-excursion (todo-item-end))))
2671 (overlay-put ov
'face
'todo-search
))
2672 (let* ((pl (if (and marked
(> (cdr marked
) 1)) "s" ""))
2673 (cat+file
(todo-read-category (concat "Move item" pl
2676 (while (and (equal (car cat
+file
) cat1
)
2677 (equal (cdr cat
+file
) file1
))
2678 (setq cat
+file
(todo-read-category
2679 "Choose a different category: ")))
2680 (setq cat2
(car cat
+file
)
2681 file2
(cdr cat
+file
))))
2682 (if ov
(delete-overlay ov
)))
2683 (set-buffer (find-buffer-visiting file1
))
2686 (goto-char (point-min))
2688 (when (todo-marked-item-p)
2689 (if (todo-done-item-p)
2690 (setq done-items
(concat done-items
2691 (todo-item-string) "\n")
2693 (setq todo-items
(concat todo-items
2694 (todo-item-string) "\n")
2696 (when (todo-diary-item-p)
2697 (setq diary
(1+ diary
)))))
2698 (todo-forward-item))
2699 ;; Chop off last newline of multiple todo item string,
2700 ;; since it will be reinserted when setting priority
2701 ;; (but with done items priority is not set, so keep
2704 (setq todo-items
(substring todo-items
0 -
1))))
2705 (if (todo-done-item-p)
2708 (when (todo-diary-item-p) (setq diary
1))))
2709 (set-window-buffer (selected-window)
2710 (set-buffer (find-file-noselect file2
'nowarn
)))
2713 (when (or todo-items
(and item
(not done-item
)))
2714 (todo-set-item-priority (or todo-items item
) cat2 t
))
2715 ;; Move done items en bloc to top of done items section.
2716 (when (or done-items done-item
)
2717 (todo-category-number cat2
)
2719 (goto-char (point-min))
2721 (concat "^" (regexp-quote (concat todo-category-beg cat2
))
2724 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2726 (insert (or done-items done-item
)))
2729 ;; Move succeeded, so remove item from starting category,
2730 ;; update item counts and display the category containing
2733 (setq nmark
(point-marker))
2734 (when todo
(todo-update-count 'todo todo
))
2735 (when diary
(todo-update-count 'diary diary
))
2736 (when done
(todo-update-count 'done done
))
2737 (todo-update-categories-sexp)
2738 (with-current-buffer (find-buffer-visiting file1
)
2747 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
2750 (setq end
(if (re-search-forward
2751 (concat "^" (regexp-quote
2752 todo-category-beg
)) nil t
)
2756 (while (< (point) end
)
2757 (if (todo-marked-item-p)
2759 (todo-forward-item)))
2760 (setq todo-categories-with-marks
2761 (assq-delete-all cat1 todo-categories-with-marks
)))
2762 (if ov
(delete-overlay ov
))
2763 (todo-remove-item))))
2764 (when todo
(todo-update-count 'todo
(- todo
) cat1
))
2765 (when diary
(todo-update-count 'diary
(- diary
) cat1
))
2766 (when done
(todo-update-count 'done
(- done
) cat1
))
2767 (todo-update-categories-sexp))
2768 (set-window-buffer (selected-window)
2769 (set-buffer (find-file-noselect file2
'nowarn
)))
2770 (setq todo-category-number
(todo-category-number cat2
))
2771 (let ((todo-show-with-done (or done-items done-item
)))
2772 (todo-category-select))
2774 ;; If item is moved to end of (just first?) category, make
2775 ;; sure the items above it are displayed in the window.
2777 ;; User quit before setting priority of todo item(s), so
2778 ;; return to starting category.
2780 (set-window-buffer (selected-window)
2781 (set-buffer (find-file-noselect file1
'nowarn
)))
2782 (todo-category-number cat1
)
2783 (todo-category-select)
2784 (goto-char omark
))))))))
2786 (defun todo-item-done (&optional arg
)
2787 "Tag a todo item in this category as done and relocate it.
2789 With prefix argument ARG prompt for a comment and append it to
2790 the done item; this is only possible if there are no marked
2791 items. If there are marked items, tag all of these with
2792 `todo-done-string' plus the current date and, if
2793 `todo-always-add-time-string' is non-nil, the current time;
2794 otherwise, just tag the item at point. Items tagged as done are
2795 relocated to the category's (by default hidden) done section. If
2796 done items are visible on invoking this command, they remain
2799 (let* ((cat (todo-current-category))
2800 (marked (assoc cat todo-categories-with-marks
)))
2801 (when marked
(todo--user-error-if-marked-done-item))
2802 (unless (and (not marked
)
2803 (or (todo-done-item-p)
2804 ;; Point is between todo and done items.
2806 (let* ((date-string (calendar-date-string (calendar-current-date) t t
))
2807 (time-string (if todo-always-add-time-string
2808 (concat " " (substring (current-time-string)
2811 (done-prefix (concat "[" todo-done-string date-string time-string
2813 (comment (and arg
(read-string "Enter a comment: ")))
2816 (show-done (save-excursion
2817 (goto-char (point-min))
2818 (re-search-forward todo-done-string-start nil t
)))
2819 (buffer-read-only nil
)
2822 ;; Don't add empty comment to done item.
2823 (setq comment
(unless (zerop (length comment
))
2824 (concat " [" todo-comment-string
": " comment
"]")))
2825 (and marked
(goto-char (point-min)))
2827 ;; Stop looping when we hit the empty line below the last
2828 ;; todo item (this is eobp if only done items are hidden).
2829 (while (not (looking-at "^$"))
2830 (if (or (not marked
) (and marked
(todo-marked-item-p)))
2832 (setq item
(todo-item-string))
2833 (setq done-item
(concat done-item done-prefix item
2834 comment
(and marked
"\n")))
2835 (setq item-count
(1+ item-count
))
2836 (when (todo-diary-item-p)
2837 (setq diary-count
(1+ diary-count
)))
2839 (unless marked
(throw 'done nil
)))
2840 (todo-forward-item))))
2842 ;; Chop off last newline of done item string.
2843 (setq done-item
(substring done-item
0 -
1))
2844 (setq todo-categories-with-marks
2845 (assq-delete-all cat todo-categories-with-marks
)))
2849 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2851 (when show-done
(setq opoint
(point)))
2852 (insert done-item
"\n"))
2853 (todo-update-count 'todo
(- item-count
))
2854 (todo-update-count 'done item-count
)
2855 (todo-update-count 'diary
(- diary-count
))
2856 (todo-update-categories-sexp)
2857 (let ((todo-show-with-done show-done
))
2858 (todo-category-select)
2859 ;; When done items are visible, put point at the top of the
2860 ;; done items section. When done items are hidden, restore
2861 ;; point to its location prior to invoking this command.
2862 (when opoint
(goto-char opoint
)))))))
2864 (defun todo-item-undone ()
2865 "Restore at least one done item to this category's todo section.
2866 Prompt for the new priority. If there are marked items, undo all
2867 of these, giving the first undone item the new priority and the
2868 rest following directly in sequence; otherwise, undo just the
2871 If the done item has a comment, ask whether to omit the comment
2872 from the restored item. With multiple marked done items with
2873 comments, only ask once, and if affirmed, omit subsequent
2874 comments without asking."
2876 (let* ((cat (todo-current-category))
2877 (marked (assoc cat todo-categories-with-marks
))
2878 (pl (if (and marked
(> (cdr marked
) 1)) "s" "")))
2879 (when (or marked
(todo-done-item-p))
2880 (let ((buffer-read-only)
2882 (omark (point-marker))
2886 start end item ov npoint undone
)
2887 (and marked
(goto-char (point-min)))
2890 (when (or (not marked
) (and marked
(todo-marked-item-p)))
2891 (if (not (todo-done-item-p))
2894 (user-error "Only done items can be undone"))
2897 (setq ov
(make-overlay (save-excursion (todo-item-start))
2898 (save-excursion (todo-item-end))))
2899 (overlay-put ov
'face
'todo-search
))
2900 ;; Find the end of the date string added upon tagging item as
2902 (setq start
(search-forward "] "))
2903 (setq item-count
(1+ item-count
))
2904 (unless (looking-at (regexp-quote todo-nondiary-start
))
2905 (setq diary-count
(1+ diary-count
)))
2906 (setq end
(save-excursion (todo-item-end)))
2907 ;; Ask (once) whether to omit done item's comment. If
2908 ;; affirmed, omit subsequent comments without asking.
2909 (when (re-search-forward
2910 (concat " \\[" (regexp-quote todo-comment-string
)
2911 ": [^]]+\\]") end t
)
2913 (if (eq first
'first
)
2915 (if (eq todo-undo-item-omit-comment
'ask
)
2916 (when (todo-y-or-n-p
2917 (concat "Omit comment" pl
2918 " from restored item"
2921 (when todo-undo-item-omit-comment
'omit
)))
2923 (when (and (eq first
'first
) ov
) (delete-overlay ov
)))
2924 (when (eq first
'omit
)
2925 (setq end
(match-beginning 0))))
2926 (setq item
(concat item
2927 (buffer-substring-no-properties start end
)
2928 (when marked
"\n")))
2929 (unless marked
(throw 'done nil
))))
2930 (todo-forward-item)))
2933 ;; Chop off last newline of multiple items string, since
2934 ;; it will be reinserted on setting priority.
2935 (and marked
(setq item
(substring item
0 -
1)))
2936 (todo-set-item-priority item cat t
)
2937 (setq npoint
(point))
2939 (when ov
(delete-overlay ov
))
2946 (concat "^" (regexp-quote todo-category-done
)) nil t
)
2948 (if (todo-marked-item-p)
2950 (todo-forward-item)))
2951 (setq todo-categories-with-marks
2952 (assq-delete-all cat todo-categories-with-marks
)))
2955 (todo-update-count 'todo item-count
)
2956 (todo-update-count 'done
(- item-count
))
2957 (when diary-count
(todo-update-count 'diary diary-count
))
2958 (todo-update-categories-sexp)
2959 (let ((todo-show-with-done (> (todo-get-count 'done
) 0)))
2960 (todo-category-select))
2961 ;; Put cursor on undone item.
2962 (goto-char npoint
)))
2963 (set-marker omark nil
)))))
2965 ;; -----------------------------------------------------------------------------
2966 ;;; Done item archives
2967 ;; -----------------------------------------------------------------------------
2969 (defun todo-find-archive (&optional ask
)
2970 "Visit the archive of the current todo category, if it exists.
2971 If the category has no archived items, prompt to visit the
2972 archive anyway. If there is no archive for this file or with
2973 non-nil argument ASK, prompt to visit another archive.
2975 The buffer showing the archive is in Todo Archive mode. The
2976 first visit in a session displays the first category in the
2977 archive, subsequent visits return to the last category
2980 (if (null (funcall todo-files-function t
))
2981 (message "There are no archive files")
2982 (let* ((cat (todo-current-category))
2983 (count (todo-get-count 'archived cat
))
2984 (archive (concat (file-name-sans-extension todo-current-todo-file
)
2986 (place (cond (ask 'other-archive
)
2987 ((file-exists-p archive
) 'this-archive
)
2988 (t (when (todo-y-or-n-p
2989 (concat "This file has no archive; "
2990 "visit another archive? "))
2992 (when (eq place
'other-archive
)
2993 (setq archive
(todo-read-file-name "Choose a todo archive: " t t
)))
2994 (when (and (eq place
'this-archive
) (zerop count
))
2995 (setq place
(when (todo-y-or-n-p
2996 (concat "This category has no archived items;"
2997 " visit archive anyway? "))
3000 (set-window-buffer (selected-window)
3001 (set-buffer (find-file-noselect archive
)))
3002 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
3003 (if (member place
'(other-archive other-cat
))
3004 (setq todo-category-number
1)
3005 (todo-category-number cat
))
3006 (todo-category-select)))))
3008 (defun todo-choose-archive ()
3009 "Choose an archive and visit it."
3011 (todo-find-archive t
))
3013 (defun todo-archive-done-item (&optional all
)
3014 "Archive at least one done item in this category.
3016 With prefix argument ALL, prompt whether to archive all done
3017 items in this category and on confirmation archive them.
3018 Otherwise, if there are marked done items (and no marked todo
3019 items), archive all of these; otherwise, archive the done item at
3022 If the archive of this file does not exist, it is created. If
3023 this category does not exist in the archive, it is created."
3025 (when (eq major-mode
'todo-mode
)
3026 (if (and all
(zerop (todo-get-count 'done
)))
3027 (message "No done items in this category")
3029 (let* ((cat (todo-current-category))
3030 (tbuf (current-buffer))
3031 (marked (assoc cat todo-categories-with-marks
))
3032 (afile (concat (file-name-sans-extension
3033 todo-current-todo-file
) ".toda"))
3034 (archive (find-file-noselect afile t
))
3035 (item (and (not marked
) (todo-done-item-p)
3036 (concat (todo-item-string) "\n")))
3038 (opoint (unless (todo-done-item-p) (point)))
3039 marked-items beg end all-done
3043 (if (todo-y-or-n-p "Archive all done items in this category? ")
3046 (goto-char (point-min))
3049 (re-search-forward todo-done-string-start
3051 (match-beginning 0))
3052 end
(if (re-search-forward
3054 (regexp-quote todo-category-beg
))
3058 all-done
(buffer-substring-no-properties beg end
)
3059 count
(todo-get-count 'done
))
3060 ;; Restore starting point, unless it was on a done
3061 ;; item, since they will all be deleted.
3062 (when opoint
(goto-char opoint
))))
3066 (goto-char (point-min))
3068 (when (todo-marked-item-p)
3069 (if (not (todo-done-item-p))
3070 (throw 'end
(message "Only done items can be archived"))
3072 (concat marked-items
(todo-item-string) "\n"))
3073 (setq count
(1+ count
))))
3074 (todo-forward-item)))))
3075 (if (not (or marked all item
))
3076 (throw 'end
(message "Only done items can be archived"))
3077 (with-current-buffer archive
3078 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
3079 (let (buffer-read-only)
3081 (goto-char (point-min))
3082 (if (and (re-search-forward
3083 (concat "^" (regexp-quote
3084 (concat todo-category-beg cat
)) "$")
3086 (re-search-forward (regexp-quote todo-category-done
)
3088 ;; Start of done items section in existing category.
3090 (todo-add-category nil cat
)
3091 ;; Start of done items section in new category.
3092 (goto-char (point-max)))
3093 (insert (cond (marked marked-items
)
3096 (todo-update-count 'done
(if (or marked all
) count
1) cat
)
3097 (todo-update-categories-sexp)
3098 ;; If archive is new, save to file now (with
3099 ;; write-region to avoid prompt for file to save to)
3100 ;; to update todo-archives, and set the mode for
3101 ;; visiting the archive below.
3102 (unless (nth 7 (file-attributes afile
))
3103 (write-region nil nil afile t t
)
3104 (setq todo-archives
(funcall todo-files-function t
))
3105 (todo-archive-mode))))
3106 (with-current-buffer tbuf
3111 ;; Make sure done items are accessible.
3113 (remove-overlays beg end
)
3114 (delete-region beg end
)
3115 (todo-update-count 'done
(- count
))
3116 (todo-update-count 'archived count
))))
3118 ;; If we're archiving all done items, can't
3119 ;; first archive item point was on, since
3120 ;; that will short-circuit the rest.
3121 (and item
(not all
)))
3122 (and marked
(goto-char (point-min)))
3125 (if (or (and marked
(todo-marked-item-p)) item
)
3128 (todo-update-count 'done -
1)
3129 (todo-update-count 'archived
1)
3130 ;; Don't leave point below last item.
3131 (and (or marked item
) (bolp) (eolp)
3132 (< (point-min) (point-max))
3133 (todo-backward-item))
3135 (throw 'done
(setq item nil
))))
3136 (todo-forward-item))))))
3138 (setq todo-categories-with-marks
3139 (assq-delete-all cat todo-categories-with-marks
)))
3140 (todo-update-categories-sexp)
3141 (todo-prefix-overlays)))
3143 (todo-category-number cat
)
3144 (todo-category-select)
3145 (split-window-below)
3146 (set-window-buffer (selected-window) tbuf
)
3147 ;; Make todo file current to select category.
3148 (find-file (buffer-file-name tbuf
))
3149 ;; Make sure done item separator is hidden (if done items
3150 ;; were initially visible).
3151 (let (todo-show-with-done) (todo-category-select)))))))
3153 (defun todo-unarchive-items ()
3154 "Unarchive at least one item in this archive category.
3155 If there are marked items, unarchive all of these; otherwise,
3156 unarchive the item at point.
3158 Unarchived items are restored as done items to the corresponding
3159 category in the todo file, inserted at the top of done items
3160 section. If all items in the archive category have been
3161 restored, the category is deleted from the archive. If this was
3162 the only category in the archive, the archive file is deleted."
3164 (when (eq major-mode
'todo-archive-mode
)
3165 (let* ((cat (todo-current-category))
3166 (tbuf (find-file-noselect
3167 (concat (file-name-sans-extension todo-current-todo-file
)
3169 (marked (assoc cat todo-categories-with-marks
))
3170 (item (concat (todo-item-string) "\n"))
3176 (goto-char (point-min))
3178 (when (todo-marked-item-p)
3179 (setq marked-items
(concat marked-items
(todo-item-string) "\n"))
3180 (setq marked-count
(1+ marked-count
)))
3181 (todo-forward-item))))
3182 ;; Restore items to top of category's done section and update counts.
3183 (with-current-buffer tbuf
3184 (let (buffer-read-only newcat
)
3186 (goto-char (point-min))
3187 ;; Find the corresponding todo category, or if there isn't
3189 (unless (re-search-forward
3190 (concat "^" (regexp-quote (concat todo-category-beg cat
))
3192 (todo-add-category nil cat
)
3194 ;; Go to top of category's done section.
3196 (concat "^" (regexp-quote todo-category-done
)) nil t
)
3199 (insert marked-items
)
3200 (todo-update-count 'done marked-count cat
)
3201 (unless newcat
; Newly added category has no archive.
3202 (todo-update-count 'archived
(- marked-count
) cat
)))
3205 (todo-update-count 'done
1 cat
)
3206 (unless newcat
; Newly added category has no archive.
3207 (todo-update-count 'archived -
1 cat
))))
3208 (todo-update-categories-sexp)))
3209 ;; Delete restored items from archive.
3212 (goto-char (point-min)))
3215 (if (or (todo-marked-item-p) item
)
3219 (throw 'done
(setq item nil
))))
3220 (todo-forward-item))))
3221 (todo-update-count 'done
(if marked
(- marked-count
) -
1) cat
)
3222 ;; If we unarchived the last item in category, then if that was
3223 ;; the only category, delete the whole file, otherwise, just
3224 ;; delete the category.
3225 (when (= 0 (todo-get-count 'done
))
3226 (if (= 1 (length todo-categories
))
3228 (delete-file todo-current-todo-file
)
3229 ;; Kill the archive buffer silently.
3230 (set-buffer-modified-p nil
)
3233 (let ((beg (re-search-backward
3234 (concat "^" (regexp-quote todo-category-beg
) cat
"$")
3236 (end (if (re-search-forward
3237 (concat "^" (regexp-quote todo-category-beg
))
3241 (remove-overlays beg end
)
3242 (delete-region beg end
)
3243 (setq todo-categories
(delete (assoc cat todo-categories
)
3244 todo-categories
)))))
3245 (todo-update-categories-sexp)
3246 ;; Visit category in todo file and show restored done items.
3247 (let ((tfile (buffer-file-name tbuf
))
3248 (todo-show-with-done t
))
3249 (set-window-buffer (selected-window)
3250 (set-buffer (find-file-noselect tfile
)))
3251 (todo-category-number cat
)
3252 (todo-category-select)
3253 (message "Items unarchived.")))))
3255 (defun todo-jump-to-archive-category (&optional file
)
3256 "Prompt for a category in a todo archive and jump to it.
3257 With prefix argument FILE, prompt for an archive and choose (with
3258 TAB completion) a category in it to jump to; otherwise, choose
3259 and jump to any category in the current archive."
3261 (todo-jump-to-category file
'archive
))
3263 ;; -----------------------------------------------------------------------------
3264 ;;; Displaying and sorting tables of categories
3265 ;; -----------------------------------------------------------------------------
3267 (defcustom todo-categories-category-label
"Category"
3268 "Category button label in Todo Categories mode."
3270 :group
'todo-categories
)
3272 (defcustom todo-categories-todo-label
"Todo"
3273 "Todo button label in Todo Categories mode."
3275 :group
'todo-categories
)
3277 (defcustom todo-categories-diary-label
"Diary"
3278 "Diary button label in Todo Categories mode."
3280 :group
'todo-categories
)
3282 (defcustom todo-categories-done-label
"Done"
3283 "Done button label in Todo Categories mode."
3285 :group
'todo-categories
)
3287 (defcustom todo-categories-archived-label
"Archived"
3288 "Archived button label in Todo Categories mode."
3290 :group
'todo-categories
)
3292 (defcustom todo-categories-totals-label
"Totals"
3293 "String to label total item counts in Todo Categories mode."
3295 :group
'todo-categories
)
3297 (defcustom todo-categories-number-separator
" | "
3298 "String between number and category in Todo Categories mode.
3299 This separates the number from the category name in the default
3300 categories display according to priority."
3302 :group
'todo-categories
)
3304 (defcustom todo-categories-align
'center
3305 "Alignment of category names in Todo Categories mode."
3306 :type
'(radio (const left
) (const center
) (const right
))
3307 :group
'todo-categories
)
3309 (defun todo-show-categories-table ()
3310 "Display a table of the current file's categories and item counts.
3312 In the initial display the lines of the table are numbered,
3313 indicating the current order of the categories when sequentially
3314 navigating through the todo file with `\\[todo-forward-category]'
3315 and `\\[todo-backward-category]'. You can reorder the lines, and
3316 hence the category sequence, by typing `\\[todo-raise-category]'
3317 or `\\[todo-lower-category]' to raise or lower the category at
3318 point, or by typing `\\[todo-set-category-number]' and entering a
3319 number at the prompt or by typing `\\[todo-set-category-number]'
3320 with a numeric prefix. If you save the todo file after
3321 reordering the categories, the new order persists in subsequent
3324 The labels above the category names and item counts are buttons,
3325 and clicking these changes the display: sorted by category name
3326 or by the respective item counts (alternately descending or
3327 ascending). In these displays the categories are not numbered
3328 and `\\[todo-set-category-number]', `\\[todo-raise-category]' and
3329 `\\[todo-lower-category]' are disabled. (Programmatically, the
3330 sorting is triggered by passing a non-nil SORTKEY argument.)
3332 In addition, the lines with the category names and item counts
3333 are buttonized, and pressing one of these button jumps to the
3334 category in Todo mode (or Todo Archive mode, for categories
3335 containing only archived items, provided user option
3336 `todo-skip-archived-categories' is non-nil. These categories
3337 are shown in `todo-archived-only' face."
3339 (todo-display-categories)
3341 (todo-update-categories-display sortkey
)))
3343 (defun todo-next-button (n)
3344 "Move point to the Nth next button in the table of categories."
3346 (forward-button n
'wrap
'display-message
)
3347 (and (bolp) (button-at (point))
3348 ;; Align with beginning of category label.
3349 (forward-char (+ 4 (length todo-categories-number-separator
)))))
3351 (defun todo-previous-button (n)
3352 "Move point to the Nth previous button in the table of categories."
3354 (backward-button n
'wrap
'display-message
)
3355 (and (bolp) (button-at (point))
3356 ;; Align with beginning of category label.
3357 (forward-char (+ 4 (length todo-categories-number-separator
)))))
3359 (defun todo-set-category-number (&optional arg
)
3360 "Change number of category at point in the table of categories.
3362 With ARG nil, prompt for the new number. Alternatively, the
3363 enter the new number with numerical prefix ARG. Otherwise, if
3364 ARG is either of the symbols `raise' or `lower', raise or lower
3365 the category line in the table by one, respectively, thereby
3366 decreasing or increasing its number."
3368 (let ((curnum (save-excursion
3369 ;; Get the number representing the priority of the category
3370 ;; on the current line.
3371 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
3372 (when curnum
; Do nothing if we're not on a category line.
3373 (let* ((maxnum (length todo-categories
))
3374 (prompt (format "Set category priority (1-%d): " maxnum
))
3375 (col (current-column))
3376 (buffer-read-only nil
)
3377 (priority (cond ((and (eq arg
'raise
) (> curnum
1))
3379 ((and (eq arg
'lower
) (< curnum maxnum
))
3382 (while (not priority
)
3383 (setq candidate
(or arg
(read-number prompt
)))
3386 (cond ((or (< candidate
1) (> candidate maxnum
))
3387 (format "Priority must be an integer between 1 and %d: "
3389 ((= candidate curnum
)
3390 "Choose a different priority than the current one: ")))
3391 (unless prompt
(setq priority candidate
)))
3392 (let* ((lower (< curnum priority
)) ; Priority is being lowered.
3393 (head (butlast todo-categories
3394 (apply (if lower
'identity
'1+)
3395 (list (- maxnum priority
)))))
3396 (tail (nthcdr (apply (if lower
'identity
'1-
) (list priority
))
3398 ;; Category's name and items counts list.
3399 (catcons (nth (1- curnum
) todo-categories
))
3400 (todo-categories (nconc head
(list catcons
) tail
))
3402 (when lower
(setq todo-categories
(nreverse todo-categories
)))
3403 (setq todo-categories
(delete-dups todo-categories
))
3404 (when lower
(setq todo-categories
(nreverse todo-categories
)))
3405 (setq newcats todo-categories
)
3407 (with-current-buffer (find-buffer-visiting todo-current-todo-file
)
3408 (setq todo-categories newcats
)
3409 (todo-update-categories-sexp))
3410 (todo-show-categories-table)
3411 (forward-line (1+ priority
))
3412 (forward-char col
))))))
3414 (defun todo-raise-category ()
3415 "Raise priority of category at point in the table of categories."
3417 (todo-set-category-number 'raise
))
3419 (defun todo-lower-category ()
3420 "Lower priority of category at point in the table of categories."
3422 (todo-set-category-number 'lower
))
3424 (defun todo-sort-categories-alphabetically-or-numerically ()
3425 "Sort table of categories alphabetically or numerically."
3428 (goto-char (point-min))
3430 (if (member 'alpha todo-descending-counts
)
3432 (todo-update-categories-display nil
)
3433 (setq todo-descending-counts
3434 (delete 'alpha todo-descending-counts
)))
3435 (todo-update-categories-display 'alpha
))))
3437 (defun todo-sort-categories-by-todo ()
3438 "Sort table of categories by number of todo items."
3441 (goto-char (point-min))
3443 (todo-update-categories-display 'todo
)))
3445 (defun todo-sort-categories-by-diary ()
3446 "Sort table of categories by number of diary items."
3449 (goto-char (point-min))
3451 (todo-update-categories-display 'diary
)))
3453 (defun todo-sort-categories-by-done ()
3454 "Sort table of categories by number of non-archived done items."
3457 (goto-char (point-min))
3459 (todo-update-categories-display 'done
)))
3461 (defun todo-sort-categories-by-archived ()
3462 "Sort table of categories by number of archived items."
3465 (goto-char (point-min))
3467 (todo-update-categories-display 'archived
)))
3469 (defvar todo-categories-buffer
"*Todo Categories*"
3470 "Name of buffer in Todo Categories mode.")
3472 (defun todo-longest-category-name-length (categories)
3473 "Return the length of the longest name in list CATEGORIES."
3475 (dolist (c categories longest
)
3476 (setq longest
(max longest
(length c
))))))
3478 (defun todo-adjusted-category-label-length ()
3479 "Return adjusted length of category label button.
3480 The adjustment ensures proper tabular alignment in Todo
3482 (let* ((categories (mapcar 'car todo-categories
))
3483 (longest (todo-longest-category-name-length categories
))
3484 (catlablen (length todo-categories-category-label
))
3485 (lc-diff (- longest catlablen
)))
3486 (if (and (natnump lc-diff
) (cl-oddp lc-diff
))
3488 (max longest catlablen
))))
3490 (defun todo-padded-string (str)
3491 "Return category name or label string STR padded with spaces.
3492 The placement of the padding is determined by the value of user
3493 option `todo-categories-align'."
3494 (let* ((len (todo-adjusted-category-label-length))
3495 (strlen (length str
))
3496 (strlen-odd (eq (logand strlen
1) 1))
3497 (padding (max 0 (/ (- len strlen
) 2)))
3498 (padding-left (cond ((eq todo-categories-align
'left
) 0)
3499 ((eq todo-categories-align
'center
) padding
)
3500 ((eq todo-categories-align
'right
)
3501 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
3502 (padding-right (cond ((eq todo-categories-align
'left
)
3503 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
3504 ((eq todo-categories-align
'center
)
3505 (if strlen-odd
(1+ padding
) padding
))
3506 ((eq todo-categories-align
'right
) 0))))
3507 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
3509 (defvar todo-descending-counts nil
3510 "List of keys for category counts sorted in descending order.")
3512 (defun todo-sort (list &optional key
)
3513 "Return a copy of LIST, possibly sorted according to KEY."
3514 (let* ((l (copy-sequence list
))
3515 (fn (if (eq key
'alpha
)
3516 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
3517 (lambda (x) (todo-get-count key x
))))
3518 ;; Keep track of whether the last sort by key was descending or
3520 (descending (member key todo-descending-counts
))
3521 (cmp (if (eq key
'alpha
)
3523 (if descending
'< '>)))
3524 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
3525 (t2 (funcall fn
(car s2
))))
3526 (funcall cmp t1 t2
)))))
3528 (setq l
(sort l pred
))
3529 ;; Switch between descending and ascending sort order.
3531 (setq todo-descending-counts
3532 (delete key todo-descending-counts
))
3533 (push key todo-descending-counts
)))
3536 (defun todo-display-sorted (type)
3537 "Keep point on the TYPE count sorting button just clicked."
3538 (let ((opoint (point)))
3539 (todo-update-categories-display type
)
3540 (goto-char opoint
)))
3542 (defun todo-label-to-key (label)
3543 "Return symbol for sort key associated with LABEL."
3545 (cond ((string= label todo-categories-category-label
)
3547 ((string= label todo-categories-todo-label
)
3549 ((string= label todo-categories-diary-label
)
3551 ((string= label todo-categories-done-label
)
3553 ((string= label todo-categories-archived-label
)
3554 (setq key
'archived
)))
3557 (defun todo-insert-sort-button (label)
3558 "Insert button for displaying categories sorted by item counts.
3559 LABEL determines which type of count is sorted."
3560 (let* ((str (if (string= label todo-categories-category-label
)
3561 (todo-padded-string label
)
3564 (end (+ beg
(length str
)))
3566 (insert-button str
'face nil
3569 (let ((key (todo-label-to-key ,label
)))
3570 (if (and (member key todo-descending-counts
)
3573 ;; If display is alphabetical, switch back to
3574 ;; category priority order.
3575 (todo-display-sorted nil
)
3576 (setq todo-descending-counts
3577 (delete key todo-descending-counts
)))
3578 (todo-display-sorted key
)))))
3579 (setq ov
(make-overlay beg end
))
3580 (overlay-put ov
'face
'todo-button
)))
3582 (defun todo-total-item-counts ()
3583 "Return a list of total item counts for the current file."
3584 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i
))
3585 (mapcar 'cdr todo-categories
))))
3588 (defvar todo-categories-category-number
0
3589 "Variable for numbering categories in Todo Categories mode.")
3591 (defun todo-insert-category-line (cat &optional nonum
)
3592 "Insert button with category CAT's name and item counts.
3593 With non-nil argument NONUM show only these; otherwise, insert a
3594 number in front of the button indicating the category's priority.
3595 The number and the category name are separated by the string
3596 which is the value of the user option
3597 `todo-categories-number-separator'."
3598 (let ((archive (member todo-current-todo-file todo-archives
))
3599 (num todo-categories-category-number
)
3600 (str (todo-padded-string cat
))
3602 (setq num
(1+ num
) todo-categories-category-number num
)
3605 (make-string (+ 4 (length todo-categories-number-separator
))
3607 (format " %3d%s" num todo-categories-number-separator
))
3609 (mapconcat (lambda (elt)
3611 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
3612 (format "%3d" (todo-get-count (cdr elt
) cat
)) ; count
3613 ;; Add an extra space if label length is odd.
3614 (when (cl-oddp (length (car elt
))) " ")))
3616 (list (cons todo-categories-done-label
'done
))
3617 (list (cons todo-categories-todo-label
'todo
)
3618 (cons todo-categories-diary-label
'diary
)
3619 (cons todo-categories-done-label
'done
)
3620 (cons todo-categories-archived-label
3623 " ") ; Make highlighting on last column look better.
3624 'face
(if (and todo-skip-archived-categories
3625 (zerop (todo-get-count 'todo cat
))
3626 (zerop (todo-get-count 'done cat
))
3627 (not (zerop (todo-get-count 'archived cat
))))
3630 'action
`(lambda (button) (let ((buf (current-buffer)))
3631 (todo-jump-to-category nil
,cat
)
3632 (kill-buffer buf
))))
3633 ;; Highlight the sorted count column.
3634 (let* ((beg (+ opoint
7 (length str
)))
3636 (cond ((eq nonum
'todo
)
3637 (setq beg
(+ beg
1 (/ (length todo-categories-todo-label
) 2))))
3639 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3640 2 (/ (length todo-categories-diary-label
) 2))))
3642 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3643 2 (length todo-categories-diary-label
)
3644 2 (/ (length todo-categories-done-label
) 2))))
3645 ((eq nonum
'archived
)
3646 (setq beg
(+ beg
1 (length todo-categories-todo-label
)
3647 2 (length todo-categories-diary-label
)
3648 2 (length todo-categories-done-label
)
3649 2 (/ (length todo-categories-archived-label
) 2)))))
3650 (unless (= beg
(+ opoint
7 (length str
))) ; Don't highlight categories.
3651 (setq end
(+ beg
4))
3652 (setq ovl
(make-overlay beg end
))
3653 (overlay-put ovl
'face
'todo-sorted-column
)))
3656 (defun todo-display-categories ()
3657 "Prepare buffer for displaying table of categories and item counts."
3658 (unless (eq major-mode
'todo-categories-mode
)
3659 (setq todo-global-current-todo-file
3660 (or todo-current-todo-file
3661 (todo-absolute-file-name todo-default-todo-file
)))
3662 (set-window-buffer (selected-window)
3663 (set-buffer (get-buffer-create todo-categories-buffer
)))
3664 (kill-all-local-variables)
3665 (todo-categories-mode)
3666 (let ((archive (member todo-current-todo-file todo-archives
))
3669 (insert (format (concat "Category counts for todo "
3670 (if archive
"archive" "file")
3672 (todo-short-file-name todo-current-todo-file
)))
3674 ;; Make space for the column of category numbers.
3675 (insert (make-string (+ 4 (length todo-categories-number-separator
)) 32))
3676 ;; Add the category and item count buttons (if this is the list of
3677 ;; categories in an archive, show only done item counts).
3678 (todo-insert-sort-button todo-categories-category-label
)
3681 (insert (make-string 3 32))
3682 (todo-insert-sort-button todo-categories-done-label
))
3683 (insert (make-string 3 32))
3684 (todo-insert-sort-button todo-categories-todo-label
)
3685 (insert (make-string 2 32))
3686 (todo-insert-sort-button todo-categories-diary-label
)
3687 (insert (make-string 2 32))
3688 (todo-insert-sort-button todo-categories-done-label
)
3689 (insert (make-string 2 32))
3690 (todo-insert-sort-button todo-categories-archived-label
))
3693 (defun todo-update-categories-display (sortkey)
3694 "Populate table of categories and sort by SORTKEY."
3695 (let* ((cats0 todo-categories
)
3696 (cats (todo-sort cats0 sortkey
))
3697 (archive (member todo-current-todo-file todo-archives
))
3698 (todo-categories-category-number 0)
3699 ;; Find start of Category button if we just entered Todo Categories
3701 (pt (if (eq (point) (point-max))
3704 (goto-char (next-single-char-property-change
3705 (point) 'face nil
(line-end-position))))))
3708 (delete-region (point) (point-max))
3709 ;; Fill in the table with buttonized lines, each showing a category and
3711 (mapc (lambda (cat) (todo-insert-category-line cat sortkey
))
3714 ;; Add a line showing item count totals.
3715 (insert (make-string (+ 4 (length todo-categories-number-separator
)) 32)
3716 (todo-padded-string todo-categories-totals-label
)
3720 (make-string (1+ (/ (length (car elt
)) 2)) 32)
3721 (format "%3d" (nth (cdr elt
) (todo-total-item-counts)))
3722 ;; Add an extra space if label length is odd.
3723 (when (cl-oddp (length (car elt
))) " ")))
3725 (list (cons todo-categories-done-label
2))
3726 (list (cons todo-categories-todo-label
0)
3727 (cons todo-categories-diary-label
1)
3728 (cons todo-categories-done-label
2)
3729 (cons todo-categories-archived-label
3)))
3731 ;; Put cursor on Category button initially.
3732 (if pt
(goto-char pt
))
3733 (setq buffer-read-only t
)))
3735 ;; -----------------------------------------------------------------------------
3736 ;;; Searching and item filtering
3737 ;; -----------------------------------------------------------------------------
3739 (defun todo-search ()
3740 "Search for a regular expression in this todo file.
3741 The search runs through the whole file and encompasses all and
3742 only todo and done items; it excludes category names. Multiple
3743 matches are shown sequentially, highlighted in `todo-search'
3746 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3748 matches match cat in-done ov mlen msg
)
3750 (goto-char (point-min))
3752 (setq match
(re-search-forward regex nil t
))
3753 (goto-char (line-beginning-position))
3754 (unless (or (equal (point) 1)
3755 (looking-at (concat "^" (regexp-quote todo-category-beg
))))
3756 (if match
(push match matches
)))
3758 (setq matches
(reverse matches
))
3762 (setq match
(pop matches
))
3765 (when (looking-at todo-done-string-start
)
3767 (re-search-backward (concat "^" (regexp-quote todo-category-beg
)
3768 "\\(.*\\)\n") nil t
)
3769 (setq cat
(match-string-no-properties 1))
3770 (todo-category-number cat
)
3771 (todo-category-select)
3773 (unless todo-show-with-done
(todo-toggle-view-done-items)))
3775 (setq ov
(make-overlay (- (point) (length regex
)) (point)))
3776 (overlay-put ov
'face
'todo-search
)
3778 (setq mlen
(length matches
))
3781 (format "There are %d more matches; go to next match? "
3783 "There is one more match; go to it? "))
3785 (throw 'stop
(setq msg
(if (> mlen
1)
3786 (format "There are %d more matches."
3788 "There is one more match."))))))
3789 (setq msg
"There are no more matches."))
3790 (todo-category-select)
3792 (message "No match for \"%s\"" regex
))
3794 (if (todo-y-or-n-p (concat msg
"\nUnhighlight matches? "))
3795 (todo-clear-matches)
3796 (message "You can unhighlight the matches later by typing %s"
3797 (key-description (car (where-is-internal
3798 'todo-clear-matches
))))))))
3800 (defun todo-clear-matches ()
3801 "Remove highlighting on matches found by todo-search."
3803 (remove-overlays 1 (1+ (buffer-size)) 'face
'todo-search
))
3805 (defcustom todo-top-priorities-overrides nil
3806 "List of rules specifying number of top priority items to show.
3807 These rules override `todo-top-priorities' on invocations of
3808 `\\[todo-filter-top-priorities]' and
3809 `\\[todo-filter-top-priorities-multifile]'. Each rule is a list
3810 of the form (FILE NUM ALIST), where FILE is a member of
3811 `todo-files', NUM is a number specifying the default number of
3812 top priority items for each category in that file, and ALIST,
3813 when non-nil, consists of conses of a category name in FILE and a
3814 number specifying the default number of top priority items in
3815 that category, which overrides NUM.
3817 This variable should be set interactively by
3818 `\\[todo-set-top-priorities-in-file]' or
3819 `\\[todo-set-top-priorities-in-category]'."
3821 :group
'todo-filtered
)
3823 (defcustom todo-top-priorities
1
3824 "Default number of top priorities shown by `todo-filter-top-priorities'."
3826 :group
'todo-filtered
)
3828 (defcustom todo-filter-files nil
3829 "List of default files for multifile item filtering."
3830 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
3831 (mapcar 'todo-short-file-name
3832 (funcall todo-files-function
))))
3833 :group
'todo-filtered
)
3835 (defcustom todo-filter-done-items nil
3836 "Non-nil to include done items when processing regexp filters.
3837 Done items from corresponding archive files are also included."
3839 :group
'todo-filtered
)
3841 (defun todo-set-top-priorities-in-file ()
3842 "Set number of top priorities for this file.
3843 See `todo-set-top-priorities' for more details."
3845 (todo-set-top-priorities))
3847 (defun todo-set-top-priorities-in-category ()
3848 "Set number of top priorities for this category.
3849 See `todo-set-top-priorities' for more details."
3851 (todo-set-top-priorities t
))
3853 (defun todo-filter-top-priorities (&optional arg
)
3854 "Display a list of top priority items from different categories.
3855 The categories can be any of those in the current todo file.
3857 With numerical prefix ARG show at most ARG top priority items
3858 from each category. With `C-u' as prefix argument show the
3859 numbers of top priority items specified by category in
3860 `todo-top-priorities-overrides', if this has an entry for the file(s);
3861 otherwise show `todo-top-priorities' items per category in the
3862 file(s). With no prefix argument, if a top priorities file for
3863 the current todo file has previously been saved (see
3864 `todo-save-filtered-items-buffer'), visit this file; if there is
3865 no such file, build the list as with prefix argument `C-u'.
3867 The prefix ARG regulates how many top priorities from
3868 each category to show, as described above."
3870 (todo-filter-items 'top arg
))
3872 (defun todo-filter-top-priorities-multifile (&optional arg
)
3873 "Display a list of top priority items from different categories.
3874 The categories are a subset of the categories in the files listed
3875 in `todo-filter-files', or if this nil, in the files chosen from
3876 a file selection dialog that pops up in this case.
3878 With numerical prefix ARG show at most ARG top priority items
3879 from each category in each file. With `C-u' as prefix argument
3880 show the numbers of top priority items specified in
3881 `todo-top-priorities-overrides', if this is non-nil; otherwise show
3882 `todo-top-priorities' items per category. With no prefix
3883 argument, if a top priorities file for the chosen todo files
3884 exists (see `todo-save-filtered-items-buffer'), visit this file;
3885 if there is no such file, do the same as with prefix argument
3888 (todo-filter-items 'top arg t
))
3890 (defun todo-filter-diary-items (&optional arg
)
3891 "Display a list of todo diary items from different categories.
3892 The categories can be any of those in the current todo file.
3894 Called with no prefix ARG, if a diary items file for the current
3895 todo file has previously been saved (see
3896 `todo-save-filtered-items-buffer'), visit this file; if there is
3897 no such file, build the list of diary items. Called with a
3898 prefix argument, build the list even if there is a saved file of
3901 (todo-filter-items 'diary arg
))
3903 (defun todo-filter-diary-items-multifile (&optional arg
)
3904 "Display a list of todo diary items from different categories.
3905 The categories are a subset of the categories in the files listed
3906 in `todo-filter-files', or if this nil, in the files chosen from
3907 a file selection dialog that pops up in this case.
3909 Called with no prefix ARG, if a diary items file for the chosen
3910 todo files has previously been saved (see
3911 `todo-save-filtered-items-buffer'), visit this file; if there is
3912 no such file, build the list of diary items. Called with a
3913 prefix argument, build the list even if there is a saved file of
3916 (todo-filter-items 'diary arg t
))
3918 (defun todo-filter-regexp-items (&optional arg
)
3919 "Prompt for a regular expression and display items that match it.
3920 The matches can be from any categories in the current todo file
3921 and with non-nil option `todo-filter-done-items', can include
3922 not only todo items but also done items, including those in
3925 Called with no prefix ARG, if a regexp items file for the current
3926 todo file has previously been saved (see
3927 `todo-save-filtered-items-buffer'), visit this file; if there is
3928 no such file, build the list of regexp items. Called with a
3929 prefix argument, build the list even if there is a saved file of
3932 (todo-filter-items 'regexp arg
))
3934 (defun todo-filter-regexp-items-multifile (&optional arg
)
3935 "Prompt for a regular expression and display items that match it.
3936 The matches can be from any categories in the files listed in
3937 `todo-filter-files', or if this nil, in the files chosen from a
3938 file selection dialog that pops up in this case. With non-nil
3939 option `todo-filter-done-items', the matches can include not
3940 only todo items but also done items, including those in Archive
3943 Called with no prefix ARG, if a regexp items file for the current
3944 todo file has previously been saved (see
3945 `todo-save-filtered-items-buffer'), visit this file; if there is
3946 no such file, build the list of regexp items. Called with a
3947 prefix argument, build the list even if there is a saved file of
3950 (todo-filter-items 'regexp arg t
))
3952 (defun todo-find-filtered-items-file ()
3953 "Choose a filtered items file and visit it."
3955 (let ((files (directory-files todo-directory t
"\.tod[rty]$" t
))
3958 (let ((type (cond ((equal (file-name-extension f
) "todr") "regexp")
3959 ((equal (file-name-extension f
) "todt") "top")
3960 ((equal (file-name-extension f
) "tody") "diary"))))
3961 (push (cons (concat (todo-short-file-name f
) " (" type
")") f
)
3963 (setq file
(completing-read "Choose a filtered items file: "
3964 falist nil t nil nil
(car falist
)))
3965 (setq file
(cdr (assoc-string file falist
)))
3967 (unless (derived-mode-p 'todo-filtered-items-mode
)
3968 (todo-filtered-items-mode))
3969 (todo-prefix-overlays)))
3971 (defun todo-go-to-source-item ()
3972 "Display the file and category of the filtered item at point."
3974 (let* ((str (todo-item-string))
3975 (buf (current-buffer))
3976 (res (todo-find-item str
))
3981 (message "Category %s does not contain this item." cat
)
3983 (set-window-buffer (selected-window)
3984 (set-buffer (find-buffer-visiting file
)))
3985 (setq todo-current-todo-file file
)
3986 (setq todo-category-number
(todo-category-number cat
))
3987 (let ((todo-show-with-done (if (or todo-filter-done-items
3988 (eq (cdr found
) 'done
))
3990 todo-show-with-done
)))
3991 (todo-category-select))
3992 (goto-char (car found
)))))
3994 (defvar todo-multiple-filter-files nil
3995 "List of files selected from `todo-multiple-filter-files' widget.")
3997 (defvar todo-multiple-filter-files-widget nil
3998 "Variable holding widget created by `todo-multiple-filter-files'.")
4000 (defun todo-multiple-filter-files ()
4001 "Pop to a buffer with a widget for choosing multiple filter files."
4004 (require 'wid-edit
))
4005 (with-current-buffer (get-buffer-create "*Todo Filter Files*")
4006 (pop-to-buffer (current-buffer))
4008 (kill-all-local-variables)
4009 (widget-insert "Select files for generating the top priorities list.\n\n")
4010 (setq todo-multiple-filter-files-widget
4012 `(set ,@(mapcar (lambda (x) (list 'const x
))
4013 (mapcar 'todo-short-file-name
4014 (funcall todo-files-function
))))))
4015 (widget-insert "\n")
4016 (widget-create 'push-button
4017 :notify
(lambda (widget &rest ignore
)
4018 (setq todo-multiple-filter-files
'quit
)
4020 (exit-recursive-edit))
4023 (widget-create 'push-button
4024 :notify
(lambda (&rest ignore
)
4025 (setq todo-multiple-filter-files
4028 (concat todo-directory
4031 todo-multiple-filter-files-widget
)))
4033 (exit-recursive-edit))
4035 (use-local-map widget-keymap
)
4037 (message "Click \"Apply\" after selecting files.")
4040 (defconst todo-filtered-items-buffer
"Todo filtered items"
4041 "Initial name of buffer in Todo Filter Items mode.")
4043 (defconst todo-top-priorities-buffer
"Todo top priorities"
4044 "Buffer type string for `todo-filter-items'.")
4046 (defconst todo-diary-items-buffer
"Todo diary items"
4047 "Buffer type string for `todo-filter-items'.")
4049 (defconst todo-regexp-items-buffer
"Todo regexp items"
4050 "Buffer type string for `todo-filter-items'.")
4052 (defun todo-filter-items (filter &optional new multifile
)
4053 "Display a list of items filtered by FILTER.
4054 The values of FILTER can be `top' for top priority items, a cons
4055 of `top' and a number passed by the caller, `diary' for diary
4056 items, or `regexp' for items matching a regular expression
4057 entered by the user. The items can come from any categories in
4058 the current todo file or, with non-nil MULTIFILE, from several
4059 files. If NEW is nil, visit an appropriate file containing the
4060 list of filtered items; if there is no such file, or with non-nil
4061 NEW, build the list and display it.
4063 See the documentation strings of the commands
4064 `todo-filter-top-priorities', `todo-filter-diary-items',
4065 `todo-filter-regexp-items', and those of the corresponding
4066 multifile commands for further details."
4067 (let* ((top (eq filter
'top
))
4068 (diary (eq filter
'diary
))
4069 (regexp (eq filter
'regexp
))
4070 (buf (cond (top todo-top-priorities-buffer
)
4071 (diary todo-diary-items-buffer
)
4072 (regexp todo-regexp-items-buffer
)))
4073 (flist (if multifile
4074 (or todo-filter-files
4075 (progn (todo-multiple-filter-files)
4076 todo-multiple-filter-files
))
4077 (list todo-current-todo-file
)))
4078 (fname (if (equal flist
'quit
)
4079 ;; Pressed `cancel' in t-m-f-f file selection dialog.
4081 (concat todo-directory
4082 (mapconcat 'todo-short-file-name flist
"-")
4085 (regexp ".todr")))))
4086 (multi (> (length flist
) 1))
4087 (rxfiles (when regexp
4088 (directory-files todo-directory t
".*\\.todr$" t
)))
4089 (file-exists (or (file-exists-p fname
) rxfiles
))
4091 (cond ((and top new
(natnump new
))
4092 (todo-filter-items-1 (cons 'top new
) flist
))
4093 ((and (not new
) file-exists
)
4094 (when (and rxfiles
(> (length rxfiles
) 1))
4095 (let ((rxf (mapcar 'todo-short-file-name rxfiles
)))
4096 (setq fname
(todo-absolute-file-name
4097 (completing-read "Choose a regexp items file: "
4100 (unless (derived-mode-p 'todo-filtered-items-mode
)
4101 (todo-filtered-items-mode))
4102 (todo-prefix-overlays)
4103 (todo-check-filtered-items-file))
4105 (todo-filter-items-1 filter flist
)))
4106 (dolist (s (split-string (todo-short-file-name fname
) "-"))
4107 (setq bufname
(if bufname
4108 (concat bufname
(if (member s
(mapcar
4109 'todo-short-file-name
4113 (rename-buffer (format (concat "%s for file" (if multi
"s" "")
4114 " \"%s\"") buf bufname
))))
4116 (defun todo-filter-items-1 (filter file-list
)
4117 "Build a list of items by applying FILTER to FILE-LIST.
4118 Internal subroutine called by `todo-filter-items', which passes
4119 the values of FILTER and FILE-LIST."
4120 (let ((num (if (consp filter
) (cdr filter
) todo-top-priorities
))
4121 (buf (get-buffer-create todo-filtered-items-buffer
))
4122 (multifile (> (length file-list
) 1))
4123 regexp fname bufstr cat beg end done
)
4124 (if (null file-list
)
4125 (user-error "No files have been chosen for filtering")
4126 (with-current-buffer buf
4128 (kill-all-local-variables)
4129 (todo-filtered-items-mode))
4130 (when (eq filter
'regexp
)
4131 (setq regexp
(read-string "Enter a regular expression: ")))
4132 (save-current-buffer
4133 (dolist (f file-list
)
4134 ;; Before inserting file contents into temp buffer, save a modified
4135 ;; buffer visiting it.
4136 (let ((bf (find-buffer-visiting f
)))
4137 (when (buffer-modified-p bf
)
4138 (with-current-buffer bf
(save-buffer))))
4139 (setq fname
(todo-short-file-name f
))
4141 (when (and todo-filter-done-items
(eq filter
'regexp
))
4142 ;; If there is a corresponding archive file for the
4143 ;; todo file, insert it first and add identifiers for
4144 ;; todo-go-to-source-item.
4145 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
4146 (when (file-exists-p arch
)
4147 (insert-file-contents arch
)
4148 ;; Delete todo archive file's categories sexp.
4149 (delete-region (line-beginning-position)
4150 (1+ (line-end-position)))
4153 (when (re-search-forward
4154 (concat (if todo-filter-done-items
4155 (concat "\\(?:" todo-done-string-start
4156 "\\|" todo-date-string-start
4158 todo-date-string-start
)
4159 todo-date-pattern
"\\(?: "
4160 diary-time-regexp
"\\)?"
4161 (if todo-filter-done-items
4163 (regexp-quote todo-nondiary-end
)) "?")
4165 (insert "(archive) "))
4167 (insert-file-contents f
)
4168 ;; Delete todo file's categories sexp.
4169 (delete-region (line-beginning-position) (1+ (line-end-position)))
4171 ;; Unless the number of top priorities to show was
4172 ;; passed by the caller, the file-wide value from
4173 ;; `todo-top-priorities-overrides', if non-nil, overrides
4174 ;; `todo-top-priorities'.
4175 (unless (consp filter
)
4176 (setq fnum
(or (nth 1 (assoc f todo-top-priorities-overrides
))
4177 todo-top-priorities
)))
4178 (while (re-search-forward
4179 (concat "^" (regexp-quote todo-category-beg
)
4180 "\\(.+\\)\n") nil t
)
4181 (setq cat
(match-string 1))
4183 ;; Unless the number of top priorities to show was
4184 ;; passed by the caller, the category-wide value
4185 ;; from `todo-top-priorities-overrides', if non-nil,
4186 ;; overrides a non-nil file-wide value from
4187 ;; `todo-top-priorities-overrides' as well as
4188 ;; `todo-top-priorities'.
4189 (unless (consp filter
)
4190 (let ((cats (nth 2 (assoc f todo-top-priorities-overrides
))))
4191 (setq cnum
(or (cdr (assoc cat cats
)) fnum
))))
4192 (delete-region (match-beginning 0) (match-end 0))
4193 (setq beg
(point)) ; First item in the current category.
4194 (setq end
(if (re-search-forward
4195 (concat "^" (regexp-quote todo-category-beg
))
4201 (if (re-search-forward
4202 (concat "\n" (regexp-quote todo-category-done
))
4206 (unless (and todo-filter-done-items
(eq filter
'regexp
))
4207 ;; Leave done items.
4208 (delete-region done end
)
4210 (narrow-to-region beg end
) ; Process only current category.
4211 (goto-char (point-min))
4212 ;; Apply the filter.
4213 (cond ((eq filter
'diary
)
4215 (if (looking-at (regexp-quote todo-nondiary-start
))
4217 (todo-forward-item))))
4218 ((eq filter
'regexp
)
4220 (if (looking-at todo-item-start
)
4221 (if (string-match regexp
(todo-item-string))
4224 ;; Kill lines that aren't part of a todo or done
4225 ;; item (empty or todo-category-done).
4226 (delete-region (line-beginning-position)
4227 (1+ (line-end-position))))
4228 ;; If last todo item in file matches regexp and
4229 ;; there are no following done items,
4230 ;; todo-category-done string is left dangling,
4231 ;; because todo-forward-item jumps over it.
4234 (concat (regexp-quote todo-done-string
)
4236 (line-beginning-position 0)))
4237 (delete-region (point) (progn
4240 (t ; Filter top priority items.
4241 (setq num
(or cnum fnum num
))
4243 (todo-forward-item num
))))
4245 ;; Delete non-top-priority items.
4246 (unless (member filter
'(diary regexp
))
4247 (delete-region beg end
))
4248 (goto-char (point-min))
4249 ;; Add file (if using multiple files) and category tags to
4252 (when (re-search-forward
4253 (concat (if todo-filter-done-items
4254 (concat "\\(?:" todo-done-string-start
4255 "\\|" todo-date-string-start
4257 todo-date-string-start
)
4258 todo-date-pattern
"\\(?: " diary-time-regexp
4259 "\\)?" (if todo-filter-done-items
4261 (regexp-quote todo-nondiary-end
))
4265 (when (looking-at "(archive) ") (goto-char (match-end 0)))
4266 (insert (if multifile
(concat fname
":") "") cat
"]"))
4269 (setq bufstr
(buffer-string))
4270 (with-current-buffer buf
4271 (let (buffer-read-only)
4272 (insert bufstr
)))))))
4273 (set-window-buffer (selected-window) (set-buffer buf
))
4274 (todo-prefix-overlays)
4275 (goto-char (point-min)))))
4277 (defun todo-set-top-priorities (&optional arg
)
4278 "Set number of top priorities shown by `todo-filter-top-priorities'.
4279 With non-nil ARG, set the number only for the current Todo
4280 category; otherwise, set the number for all categories in the
4283 Calling this function via either of the commands
4284 `todo-set-top-priorities-in-file' or
4285 `todo-set-top-priorities-in-category' is the recommended way to
4286 set the user customizable option `todo-top-priorities-overrides'."
4287 (let* ((cat (todo-current-category))
4288 (file todo-current-todo-file
)
4289 (rules todo-top-priorities-overrides
)
4290 (frule (assoc-string file rules
))
4291 (crules (nth 2 frule
))
4292 (crule (assoc-string cat crules
))
4293 (fcur (or (nth 1 frule
)
4294 todo-top-priorities
))
4295 (ccur (or (and arg
(cdr crule
))
4297 (prompt (if arg
(concat "Number of top priorities in this category"
4298 " (currently %d): ")
4299 (concat "Default number of top priorities per category"
4300 " in this file (currently %d): ")))
4303 (let ((cur (if arg ccur fcur
)))
4304 (setq new
(read-number (format prompt cur
))
4305 prompt
"Enter a non-negative number: "
4307 (let ((nrule (if arg
4308 (append (delete crule crules
) (list (cons cat new
)))
4309 (append (list file new
) (list crules
)))))
4310 (setq rules
(cons (if arg
4311 (list file fcur nrule
)
4313 (delete frule rules
)))
4314 (customize-save-variable 'todo-top-priorities-overrides rules
)
4315 (todo-prefix-overlays))))
4317 (defun todo-find-item (str)
4318 "Search for filtered item STR in its saved todo file.
4319 Return the list (FOUND FILE CAT), where CAT and FILE are the
4320 item's category and file, and FOUND is a cons cell if the search
4321 succeeds, whose car is the start of the item in FILE and whose
4322 cdr is `done', if the item is now a done item, `changed', if its
4323 text was truncated or augmented or, for a top priority item, if
4324 its priority has changed, and `same' otherwise."
4325 (string-match (concat (if todo-filter-done-items
4326 (concat "\\(?:" todo-done-string-start
"\\|"
4327 todo-date-string-start
"\\)")
4328 todo-date-string-start
)
4329 todo-date-pattern
"\\(?: " diary-time-regexp
"\\)?"
4330 (if todo-filter-done-items
4332 (regexp-quote todo-nondiary-end
)) "?"
4333 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
4334 "\\(?1:.*\\)\\]\\).*$") str
)
4335 (let ((cat (match-string 1 str
))
4336 (file (match-string 2 str
))
4337 (archive (string= (match-string 3 str
) "(archive) "))
4338 (filcat (match-string 4 str
))
4340 (tpbuf (save-match-data (string-match "top" (buffer-name))))
4342 (setq str
(replace-match "" nil nil str
4))
4344 ;; Calculate priority of STR wrt its category.
4346 (while (search-backward filcat nil t
)
4347 (setq tpriority
(1+ tpriority
)))))
4349 (concat todo-directory
(substring file
0 -
1)
4350 (if archive
".toda" ".todo"))
4352 (concat (file-name-sans-extension
4353 todo-global-current-todo-file
) ".toda")
4354 todo-global-current-todo-file
)))
4355 (find-file-noselect file
)
4356 (with-current-buffer (find-buffer-visiting file
)
4358 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
4359 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
4362 (goto-char (point-min))
4363 (let ((beg (re-search-forward
4364 (concat "^" (regexp-quote (concat todo-category-beg cat
))
4367 (done (save-excursion
4369 (concat "^" (regexp-quote todo-category-done
)) nil t
)))
4370 (end (save-excursion
4371 (or (re-search-forward
4372 (concat "^" (regexp-quote todo-category-beg
))
4375 (setq found
(when (search-forward str end t
)
4376 (goto-char (match-beginning 0))))
4379 (cons found
(if (> (point) done
)
4381 (let ((cpriority 1))
4384 ;; Not top item in category.
4385 (while (> (point) (1+ beg
))
4386 (let ((opoint (point)))
4387 (todo-backward-item)
4388 ;; Can't move backward beyond
4389 ;; first item in file.
4390 (unless (= (point) opoint
)
4391 (setq cpriority
(1+ cpriority
)))))))
4392 (if (and (= tpriority cpriority
)
4393 ;; Proper substring is not the same.
4394 (string= (todo-item-string)
4398 (list found file cat
)))
4400 (defun todo-check-filtered-items-file ()
4401 "Check if filtered items file is up to date and a show suitable message."
4405 (let* ((item (todo-item-string))
4406 (found (car (todo-find-item item
))))
4407 (unless (eq (cdr found
) 'same
)
4409 (overlay-put (make-overlay (todo-item-start) (todo-item-end))
4410 'face
'todo-search
))
4411 (setq count
(1+ count
))))
4412 ;; (throw 'old (message "The marked item is not up to date.")))
4413 (todo-forward-item))
4415 (message "Filtered items file is up to date.")
4416 (message (concat "The highlighted item" (if (= count
1) " is " "s are ")
4418 ;; "\nType <return> on item for details."
4421 (defun todo-filter-items-filename ()
4422 "Return absolute file name for saving this Filtered Items buffer."
4423 (let ((bufname (buffer-name)))
4424 (string-match "\"\\([^\"]+\\)\"" bufname
)
4425 (let* ((filename-str (substring bufname
(match-beginning 1) (match-end 1)))
4426 (filename-base (replace-regexp-in-string ", " "-" filename-str
))
4427 (top-priorities (string-match "top priorities" bufname
))
4428 (diary-items (string-match "diary items" bufname
))
4429 (regexp-items (string-match "regexp items" bufname
)))
4431 (let ((prompt (concat "Enter a short identifying string"
4432 " to make this file name unique: ")))
4433 (setq filename-base
(concat filename-base
"-" (read-string prompt
)))))
4434 (concat todo-directory filename-base
4435 (cond (top-priorities ".todt")
4436 (diary-items ".tody")
4437 (regexp-items ".todr"))))))
4439 (defun todo-save-filtered-items-buffer ()
4440 "Save current Filtered Items buffer to a file.
4441 If the file already exists, overwrite it only on confirmation."
4442 (let ((filename (or (buffer-file-name) (todo-filter-items-filename))))
4443 (write-file filename t
)))
4445 ;; -----------------------------------------------------------------------------
4446 ;;; Printing Todo mode buffers
4447 ;; -----------------------------------------------------------------------------
4449 (defcustom todo-print-buffer-function
'ps-print-buffer-with-faces
4450 "Function called by the command `todo-print-buffer'."
4454 (defvar todo-print-buffer
"*Todo Print*"
4455 "Name of buffer with printable version of Todo mode buffer.")
4457 (defun todo-print-buffer (&optional to-file
)
4458 "Produce a printable version of the current Todo mode buffer.
4459 This converts overlays and soft line wrapping and, depending on
4460 the value of `todo-print-buffer-function', includes faces. With
4461 non-nil argument TO-FILE write the printable version to a file;
4462 otherwise, send it to the default printer."
4464 (let ((buf todo-print-buffer
)
4466 ((eq major-mode
'todo-mode
)
4467 (concat "Todo File: "
4468 (todo-short-file-name todo-current-todo-file
)
4469 "\nCategory: " (todo-current-category)))
4470 ((eq major-mode
'todo-filtered-items-mode
)
4472 (prefix (propertize (concat todo-prefix
" ")
4473 'face
'todo-prefix-string
))
4475 (fill-prefix (make-string todo-indent-to-here
32))
4476 (content (buffer-string))
4478 (with-current-buffer (get-buffer-create buf
)
4480 (goto-char (point-min))
4483 (end (save-excursion (todo-item-end))))
4484 (when todo-number-prefix
4486 (setq prefix
(propertize (concat (number-to-string num
) " ")
4487 'face
'todo-prefix-string
)))
4489 (fill-region beg end
))
4490 ;; Calling todo-forward-item infloops at todo-item-start due to
4491 ;; non-overlay prefix, so search for item start instead.
4492 (if (re-search-forward todo-item-start nil t
)
4494 (goto-char (point-max))))
4495 (if (re-search-backward (concat "^" (regexp-quote todo-category-done
))
4497 (replace-match todo-done-separator
))
4498 (goto-char (point-min))
4502 (let ((file (read-file-name "Print to file: ")))
4503 (funcall todo-print-buffer-function file
))
4504 (funcall todo-print-buffer-function
)))
4507 (defun todo-print-buffer-to-file ()
4508 "Save printable version of this Todo mode buffer to a file."
4510 (todo-print-buffer t
))
4512 ;; -----------------------------------------------------------------------------
4513 ;;; Legacy Todo mode files
4514 ;; -----------------------------------------------------------------------------
4516 (defcustom todo-legacy-date-time-regexp
4517 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
4518 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
4519 "Regexp matching legacy todo-mode.el item date-time strings.
4520 In order for `todo-convert-legacy-files' to correctly convert
4521 this string to the current Todo mode format, the regexp must
4522 contain four explicitly numbered groups (see `(elisp) Regexp
4523 Backslash'), where group 1 matches a string for the year, group 2
4524 a string for the month, group 3 a string for the day and group 4
4525 a string for the time. The default value converts date-time
4526 strings built using the default value of
4527 `todo-time-string-format' from todo-mode.el."
4531 (defun todo-convert-legacy-date-time ()
4532 "Return converted date-time string.
4533 Helper function for `todo-convert-legacy-files'."
4534 (let* ((year (match-string 1))
4535 (month (match-string 2))
4536 (monthname (calendar-month-name (string-to-number month
) t
))
4537 (day (match-string 3))
4538 (time (match-string 4))
4541 (insert (mapconcat 'eval calendar-date-display-form
"")
4542 (when time
(concat " " time
)))))
4544 (defun todo-convert-legacy-files ()
4545 "Convert legacy todo files to the current Todo mode format.
4546 The old-style files named by the variables `todo-file-do' and
4547 `todo-file-done' from the old package are converted to the new
4548 format and saved (the latter as a todo archive file) with a new
4549 name in `todo-directory'. See also the documentation string of
4550 `todo-legacy-date-time-regexp' for further details."
4552 ;; If there are user customizations of legacy options, use them,
4553 ;; otherwise use the legacy default values.
4554 (let ((todo-file-do-tem (if (boundp 'todo-file-do
)
4556 (locate-user-emacs-file "todo-do" ".todo-do")))
4557 (todo-file-done-tem (if (boundp 'todo-file-done
)
4559 (locate-user-emacs-file "todo-done" ".todo-done")))
4560 (todo-initials-tem (and (boundp 'todo-initials
) todo-initials
))
4561 (todo-entry-prefix-function-tem (and (boundp 'todo-entry-prefix-function
)
4562 todo-entry-prefix-function
))
4564 ;; Convert `todo-file-do'.
4565 (if (not (file-exists-p todo-file-do-tem
))
4566 (message "No legacy todo file exists")
4567 (let ((default "todo-do-conv")
4570 (insert-file-contents todo-file-do-tem
)
4571 ;; Eliminate old-style local variables list in first line.
4572 (delete-region (line-beginning-position) (1+ (line-end-position)))
4573 (search-forward " --- " nil t
) ; Legacy todo-category-beg.
4574 (setq todo-prefix-tem
(buffer-substring-no-properties
4575 (line-beginning-position) (match-beginning 0)))
4576 (goto-char (point-min))
4579 ;; Old-style category start delimiter.
4580 ((looking-at (regexp-quote (concat todo-prefix-tem
" --- ")))
4581 (replace-match todo-category-beg
))
4582 ;; Old-style category end delimiter.
4583 ((looking-at (regexp-quote "--- End"))
4585 ;; Old-style category separator.
4586 ((looking-at (regexp-quote
4587 (concat todo-prefix-tem
" "
4588 (make-string 75 ?-
))))
4589 (replace-match todo-category-done
))
4590 ;; Old-style item header (date/time/initials).
4591 ((looking-at (concat (regexp-quote todo-prefix-tem
) " "
4592 (if todo-entry-prefix-function-tem
4593 (funcall todo-entry-prefix-function-tem
)
4594 (concat todo-legacy-date-time-regexp
" "
4595 (if todo-initials-tem
4596 (regexp-quote todo-initials-tem
)
4599 (todo-convert-legacy-date-time)))
4601 (setq file
(concat todo-directory
4603 (format "Save file as (default \"%s\"): " default
)
4606 (unless (file-exists-p todo-directory
)
4607 (make-directory todo-directory
))
4608 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
4610 (insert-file-contents file
)
4611 (let ((todo-categories (todo-make-categories-list t
)))
4612 (todo-update-categories-sexp)
4613 (todo-check-format))
4614 (write-region (point-min) (point-max) file nil
'nomessage
))
4615 (setq todo-files
(funcall todo-files-function
))
4616 ;; Convert `todo-file-done'.
4617 (when (file-exists-p todo-file-done-tem
)
4619 (insert-file-contents todo-file-done-tem
)
4620 (let ((beg (make-marker))
4622 cat cats comment item
)
4624 (when (looking-at todo-legacy-date-time-regexp
)
4625 (set-marker beg
(point))
4626 (todo-convert-legacy-date-time)
4627 (set-marker end
(point))
4629 (insert "[" todo-done-string
)
4633 (when (looking-at todo-legacy-date-time-regexp
)
4634 (todo-convert-legacy-date-time))
4635 (when (looking-at (concat " " (if todo-initials-tem
4640 (replace-match "")))
4641 (if (re-search-forward
4642 (concat "^" todo-legacy-date-time-regexp
) nil t
)
4643 (goto-char (match-beginning 0))
4644 (goto-char (point-max)))
4646 (when (looking-back "\\[\\([^][]+\\)\\]")
4647 (setq cat
(match-string 1))
4648 (goto-char (match-beginning 0))
4650 ;; If the item ends with a non-comment parenthesis not
4651 ;; followed by a period, we lose (but we inherit that
4652 ;; problem from the legacy code).
4653 (when (looking-back "(\\(.*\\)) " (line-beginning-position))
4654 (setq comment
(match-string 1))
4656 (insert "[" todo-comment-string
": " comment
"]"))
4657 (set-marker end
(point))
4658 (if (member cat cats
)
4659 ;; If item is already in its category, leave it there.
4660 (unless (save-excursion
4662 (concat "^" (regexp-quote todo-category-beg
)
4664 (string= (match-string 1) cat
))
4665 ;; Else move it to its category.
4666 (setq item
(buffer-substring-no-properties beg end
))
4667 (delete-region beg
(1+ end
))
4668 (set-marker beg
(point))
4671 (regexp-quote (concat todo-category-beg cat
))
4675 (if (re-search-forward
4676 (concat "^" (regexp-quote todo-category-beg
)
4678 (progn (goto-char (match-beginning 0))
4681 (goto-char (point-max)))
4686 (insert todo-category-beg cat
"\n\n"
4687 todo-category-done
"\n"))
4689 (set-marker beg nil
)
4690 (set-marker end nil
))
4691 (setq file
(concat (file-name-sans-extension file
) ".toda"))
4692 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
4694 (insert-file-contents file
)
4695 (let* ((todo-categories (todo-make-categories-list t
)))
4696 (todo-update-categories-sexp)
4697 (todo-check-format))
4698 (write-region (point-min) (point-max) file nil
'nomessage
)
4699 (setq archive-sexp
(read (buffer-substring-no-properties
4700 (line-beginning-position)
4701 (line-end-position)))))
4702 (setq file
(concat (file-name-sans-extension file
) ".todo"))
4703 ;; Update categories sexp of converted todo file again, adding
4704 ;; counts of archived items.
4706 (insert-file-contents file
)
4707 (let ((sexp (read (buffer-substring-no-properties
4708 (line-beginning-position)
4709 (line-end-position)))))
4711 (let ((archive-cat (assoc (car cat
) archive-sexp
)))
4713 (aset (cdr cat
) 3 (aref (cdr archive-cat
) 2)))))
4714 (delete-region (line-beginning-position) (line-end-position))
4715 (prin1 sexp
(current-buffer)))
4716 (write-region (point-min) (point-max) file nil
'nomessage
))
4717 (setq todo-archives
(funcall todo-files-function t
)))
4718 (todo-reevaluate-filelist-defcustoms)
4719 (when (y-or-n-p (concat "Format conversion done; do you want to "
4720 "visit the converted file now? "))
4721 (setq todo-current-todo-file file
)
4722 (unless todo-default-todo-file
4723 ;; We just initialized the first todo file, so make it the
4724 ;; default now to avoid an infinite recursion with todo-show.
4725 (setq todo-default-todo-file
(todo-short-file-name file
)))
4728 ;; -----------------------------------------------------------------------------
4729 ;;; Utility functions for todo files, categories and items
4730 ;; -----------------------------------------------------------------------------
4732 (defun todo-absolute-file-name (name &optional type
)
4733 "Return the absolute file name of short todo file NAME.
4734 With TYPE `archive' or `top' return the absolute file name of the
4735 short todo archive or top priorities file name, respectively."
4736 ;; No-op if there is no todo file yet (i.e. don't concatenate nil).
4739 (concat todo-directory name
4740 (cond ((eq type
'archive
) ".toda")
4741 ((eq type
'top
) ".todt")
4742 ((eq type
'diary
) ".tody")
4743 ((eq type
'regexp
) ".todr")
4746 (defun todo-check-file (file)
4747 "Check the state associated with FILE and update it if necessary.
4748 If FILE exists, return t. If it does not exist and there is no
4749 live buffer with its content, return nil; if there is such a
4750 buffer and the user tries to show it, ask whether to restore
4751 FILE, and if confirmed, do so and return t; else delete the
4752 buffer, clean up the state and return nil."
4753 (setq todo-files
(funcall todo-files-function
))
4754 (setq todo-archives
(funcall todo-files-function t
))
4755 (if (file-exists-p file
)
4757 (setq todo-visited
(delete file todo-visited
))
4758 (let ((buf (find-buffer-visiting file
)))
4762 (format (concat "Todo file \"%s\" has been deleted but "
4763 "its content is still in a buffer!\n")
4764 (todo-short-file-name file
))
4765 "Save that buffer and restore the todo file? ")))
4767 (with-current-buffer buf
(save-buffer))
4768 (setq todo-files
(funcall todo-files-function
))
4769 (setq todo-archives
(funcall todo-files-function t
))
4771 (let* ((files (append todo-files todo-archives
))
4772 (tctf todo-current-todo-file
)
4773 (tgctf todo-global-current-todo-file
)
4774 (tdtf (todo-absolute-file-name todo-default-todo-file
)))
4775 (unless (or (not todo-current-todo-file
)
4776 (member todo-current-todo-file files
))
4777 (setq todo-current-todo-file nil
))
4778 (unless (or (not todo-global-current-todo-file
)
4779 (member todo-global-current-todo-file files
))
4780 (setq todo-global-current-todo-file nil
))
4781 (unless (or (not todo-default-todo-file
)
4782 (member todo-default-todo-file files
))
4783 (setq todo-default-todo-file
(todo-short-file-name
4785 (todo-reevaluate-filelist-defcustoms)
4786 (when buf
(kill-buffer buf
))
4789 (defun todo-category-number (cat)
4790 "Return the number of category CAT in this todo file.
4791 The buffer-local variable `todo-category-number' holds this
4792 number as its value."
4793 (let ((categories (mapcar 'car todo-categories
)))
4794 (setq todo-category-number
4795 ;; Increment by one, so that the number of the first
4796 ;; category is one rather than zero.
4797 (1+ (- (length categories
)
4798 (length (member cat categories
)))))))
4800 (defun todo-current-category ()
4801 "Return the name of the current category."
4802 (car (nth (1- todo-category-number
) todo-categories
)))
4804 (defun todo-category-select ()
4805 "Display the current category correctly."
4806 (let ((name (todo-current-category))
4807 cat-begin cat-end done-start done-sep-start done-end
)
4809 (goto-char (point-min))
4811 (concat "^" (regexp-quote (concat todo-category-beg name
)) "$") nil t
)
4812 (setq cat-begin
(1+ (line-end-position)))
4813 (setq cat-end
(if (re-search-forward
4814 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
4817 (setq mode-line-buffer-identification
4818 (funcall todo-mode-line-function name
))
4819 (narrow-to-region cat-begin cat-end
)
4820 (todo-prefix-overlays)
4821 (goto-char (point-min))
4822 (if (re-search-forward (concat "\n\\(" (regexp-quote todo-category-done
)
4825 (setq done-start
(match-beginning 0))
4826 (setq done-sep-start
(match-beginning 1))
4827 (setq done-end
(match-end 0)))
4828 (error "Category %s is missing todo-category-done string" name
))
4829 (if todo-show-done-only
4830 (narrow-to-region (1+ done-end
) (point-max))
4831 (when (and todo-show-with-done
4832 (re-search-forward todo-done-string-start nil t
))
4833 ;; Now we want to see the done items, so reset displayed end to end of
4835 (setq done-start cat-end
)
4836 ;; Make display overlay for done items separator string, unless there
4838 (let* ((done-sep todo-done-separator
)
4839 (ov (progn (goto-char done-sep-start
)
4840 (todo-get-overlay 'separator
))))
4842 (setq ov
(make-overlay done-sep-start done-end
))
4843 (overlay-put ov
'todo
'separator
)
4844 (overlay-put ov
'display done-sep
))))
4845 (narrow-to-region (point-min) done-start
)
4846 ;; Loading this from todo-mode, or adding it to the mode hook, causes
4847 ;; Emacs to hang in todo-item-start, at (looking-at todo-item-start).
4848 (when todo-highlight-item
4850 (hl-line-mode 1)))))
4852 (defun todo-get-count (type &optional category
)
4853 "Return count of TYPE items in CATEGORY.
4854 If CATEGORY is nil, default to the current category."
4855 (let* ((cat (or category
(todo-current-category)))
4856 (counts (cdr (assoc cat todo-categories
)))
4857 (idx (cond ((eq type
'todo
) 0)
4858 ((eq type
'diary
) 1)
4860 ((eq type
'archived
) 3))))
4863 (defun todo-update-count (type increment
&optional category
)
4864 "Change count of TYPE items in CATEGORY by integer INCREMENT.
4865 With nil or omitted CATEGORY, default to the current category."
4866 (let* ((cat (or category
(todo-current-category)))
4867 (counts (cdr (assoc cat todo-categories
)))
4868 (idx (cond ((eq type
'todo
) 0)
4869 ((eq type
'diary
) 1)
4871 ((eq type
'archived
) 3))))
4872 (aset counts idx
(+ increment
(aref counts idx
)))))
4874 (defun todo-set-categories ()
4875 "Set `todo-categories' from the sexp at the top of the file."
4876 ;; New archive files created by `todo-move-category' are empty, which would
4877 ;; make the sexp test fail and raise an error, so in this case we skip it.
4878 (unless (zerop (buffer-size))
4882 (goto-char (point-min))
4883 (setq todo-categories
4884 (if (looking-at "\(\(\"")
4885 (read (buffer-substring-no-properties
4886 (line-beginning-position)
4887 (line-end-position)))
4888 (error "Invalid or missing todo-categories sexp")))))))
4890 (defun todo-update-categories-sexp ()
4891 "Update the `todo-categories' sexp at the top of the file."
4892 (let (buffer-read-only)
4896 (goto-char (point-min))
4897 (if (looking-at (concat "^" (regexp-quote todo-category-beg
)))
4898 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
4899 (setq todo-categories
(todo-make-categories-list t
)))
4900 (delete-region (line-beginning-position) (line-end-position)))
4901 (prin1 todo-categories
(current-buffer))))))
4903 (defun todo-make-categories-list (&optional force
)
4904 "Return an alist of todo categories and their item counts.
4905 With non-nil argument FORCE parse the entire file to build the
4906 list; otherwise, get the value by reading the sexp at the top of
4908 (setq todo-categories nil
)
4912 (goto-char (point-min))
4913 (let (counts cat archive
)
4914 ;; If the file is a todo file and has archived items, identify the
4915 ;; archive, in order to count its items. But skip this with
4916 ;; `todo-convert-legacy-files', since that converts filed items to
4918 (when buffer-file-name
; During conversion there is no file yet.
4919 ;; If the file is an archive, it doesn't have an archive.
4920 (unless (member (file-truename buffer-file-name
)
4921 (funcall todo-files-function t
))
4922 (setq archive
(concat (file-name-sans-extension
4923 todo-current-todo-file
) ".toda"))))
4925 (cond ((looking-at (concat (regexp-quote todo-category-beg
)
4927 (setq cat
(match-string-no-properties 1))
4928 ;; Counts for each category: [todo diary done archive]
4929 (setq counts
(make-vector 4 0))
4930 (setq todo-categories
4931 (append todo-categories
(list (cons cat counts
))))
4932 ;; Add archived item count to the todo file item counts.
4933 ;; Make sure to include newly created archives, e.g. due to
4934 ;; todo-move-category.
4935 (when (member archive
(funcall todo-files-function t
))
4936 (let ((archive-count 0)
4937 (visiting (find-buffer-visiting archive
)))
4938 (with-current-buffer (or visiting
4939 (find-file-noselect archive
))
4943 (goto-char (point-min))
4944 (when (re-search-forward
4945 (concat "^" (regexp-quote todo-category-beg
)
4949 (while (not (or (looking-at
4951 (regexp-quote todo-category-beg
)
4954 (when (looking-at todo-done-string-start
)
4955 (setq archive-count
(1+ archive-count
)))
4957 (unless visiting
(kill-buffer)))
4958 (todo-update-count 'archived archive-count cat
))))
4959 ((looking-at todo-done-string-start
)
4960 (todo-update-count 'done
1 cat
))
4961 ((looking-at (concat "^\\("
4962 (regexp-quote diary-nonmarking-symbol
)
4963 "\\)?" todo-date-pattern
))
4964 (todo-update-count 'diary
1 cat
)
4965 (todo-update-count 'todo
1 cat
))
4966 ((looking-at (concat todo-date-string-start todo-date-pattern
))
4967 (todo-update-count 'todo
1 cat
))
4968 ;; If first line is todo-categories list, use it and end loop
4969 ;; -- unless FORCEd to scan whole file.
4972 (setq todo-categories
(read (buffer-substring-no-properties
4973 (line-beginning-position)
4974 (line-end-position))))
4975 (goto-char (1- (point-max))))))
4979 (defun todo-repair-categories-sexp ()
4980 "Repair corrupt todo file categories sexp.
4981 This should only be needed as a consequence of careless manual
4982 editing or a bug in todo.el.
4984 *Warning*: Calling this command restores the category order to
4985 the list element order in the todo file categories sexp, so any
4986 order changes made in Todo Categories mode will have to be made
4989 (let ((todo-categories (todo-make-categories-list t
)))
4990 (todo-update-categories-sexp)))
4992 (defun todo-check-format ()
4993 "Signal an error if the current todo file is ill-formatted.
4994 Otherwise return t. Display a message if the file is well-formed
4995 but the categories sexp differs from the current value of
5000 (goto-char (point-min))
5001 (let* ((cats (prin1-to-string todo-categories
))
5002 (ssexp (buffer-substring-no-properties (line-beginning-position)
5003 (line-end-position)))
5004 (sexp (read ssexp
)))
5005 ;; Check the first line for `todo-categories' sexp.
5008 (unless (and (stringp (car c
))
5011 (user-error "Invalid or missing todo-categories sexp"))))
5013 ;; Check well-formedness of categories.
5014 (let ((legit (concat
5015 "\\(^" (regexp-quote todo-category-beg
) "\\)"
5016 "\\|\\(" todo-date-string-start todo-date-pattern
"\\)"
5017 "\\|\\(^[ \t]+[^ \t]*\\)"
5019 "\\|\\(^" (regexp-quote todo-category-done
) "\\)"
5020 "\\|\\(" todo-done-string-start
"\\)")))
5022 (unless (looking-at legit
)
5023 (user-error "Illegitimate todo file format at line %d"
5024 (line-number-at-pos (point))))
5026 ;; Warn user if categories sexp has changed.
5027 (unless (string= ssexp cats
)
5028 (message (concat "The sexp at the beginning of the file differs "
5029 "from the value of `todo-categories.\n"
5030 "If the sexp is wrong, you can fix it with "
5031 "M-x todo-repair-categories-sexp,\n"
5032 "but note this reverts any changes you have "
5033 "made in the order of the categories."))))))
5036 (defun todo-item-start ()
5037 "Move to start of current todo item and return its position."
5039 ;; Buffer is empty (invocation possible e.g. via todo-forward-item
5040 ;; from todo-filter-items when processing category with no todo
5042 (eq (point-min) (point-max))
5043 ;; Point is on the empty line below category's last todo item...
5044 (and (looking-at "^$")
5045 (or (eobp) ; ...and done items are hidden...
5046 (save-excursion ; ...or done items are visible.
5048 (looking-at (concat "^"
5049 (regexp-quote todo-category-done
))))))
5050 ;; Buffer is widened.
5051 (looking-at (regexp-quote todo-category-beg
)))
5052 (goto-char (line-beginning-position))
5053 (while (not (looking-at todo-item-start
))
5057 (defun todo-item-end ()
5058 "Move to end of current todo item and return its position."
5059 ;; Items cannot end with a blank line.
5060 (unless (looking-at "^$")
5061 (let* ((done (todo-done-item-p))
5063 ;; For todo items, end is before the done items section, for done
5064 ;; items, end is before the next category. If these limits are
5065 ;; missing or inaccessible, end it before the end of the buffer.
5066 (lim (if (save-excursion
5068 (concat "^" (regexp-quote (if done
5070 todo-category-done
)))
5072 (progn (setq to-lim t
) (match-beginning 0))
5074 (when (bolp) (forward-char)) ; Find start of next item.
5075 (goto-char (if (re-search-forward todo-item-start lim t
)
5077 (if to-lim lim
(point-max))))
5078 ;; For last todo item, skip back over the empty line before the done
5079 ;; items section, else just back to the end of the previous line.
5080 (backward-char (when (and to-lim
(not done
) (eq (point) lim
)) 2))
5083 (defun todo-item-string ()
5084 "Return bare text of current item as a string."
5085 (let ((opoint (point))
5086 (start (todo-item-start))
5087 (end (todo-item-end)))
5089 (and start end
(buffer-substring-no-properties start end
))))
5091 (defun todo-forward-item (&optional count
)
5092 "Move point COUNT items down (by default, move down by one item)."
5093 (let* ((not-done (not (or (todo-done-item-p) (looking-at "^$"))))
5094 (start (line-end-position)))
5096 (if (re-search-forward todo-item-start nil t
(or count
1))
5097 (goto-char (match-beginning 0))
5098 (goto-char (point-max)))
5099 ;; If points advances by one from a todo to a done item, go back
5100 ;; to the space above todo-done-separator, since that is a
5101 ;; legitimate place to insert an item. But skip this space if
5102 ;; count > 1, since that should only stop on an item.
5103 (when (and not-done
(todo-done-item-p) (not count
))
5104 ;; (if (or (not count) (= count 1))
5105 (re-search-backward "^$" start t
))));)
5106 ;; The preceding sexp is insufficient when buffer is not narrowed,
5107 ;; since there could be no done items in this category, so the
5108 ;; search puts us on first todo item of next category. Does this
5109 ;; ever happen? If so:
5110 ;; (let ((opoint) (point))
5111 ;; (forward-line -1)
5112 ;; (when (or (not count) (= count 1))
5113 ;; (cond ((looking-at (concat "^" (regexp-quote todo-category-beg)))
5114 ;; (forward-line -2))
5115 ;; ((looking-at (concat "^" (regexp-quote todo-category-done)))
5116 ;; (forward-line -1))
5118 ;; (goto-char opoint)))))))
5120 (defun todo-backward-item (&optional count
)
5121 "Move point up to start of item with next higher priority.
5122 With positive numerical prefix COUNT, move point COUNT items
5125 If the category's done items are visible, this command called
5126 with a prefix argument only moves point to a higher item, e.g.,
5127 with point on the first done item and called with prefix 1, it
5128 moves to the last todo item; but if called with point on the
5129 first done item without a prefix argument, it moves point the the
5130 empty line above the done items separator."
5131 (let* ((done (todo-done-item-p)))
5134 (re-search-backward todo-item-start nil t
(or count
1)))
5135 ;; Unless this is a regexp filtered items buffer (which can contain
5136 ;; intermixed todo and done items), if points advances by one from a
5137 ;; done to a todo item, go back to the space above
5138 ;; todo-done-separator, since that is a legitimate place to insert an
5139 ;; item. But skip this space if count > 1, since that should only
5141 (when (and done
(not (todo-done-item-p)) (not count
)
5142 ;(or (not count) (= count 1))
5143 (not (equal (buffer-name) todo-regexp-items-buffer
)))
5144 (re-search-forward (concat "^" (regexp-quote todo-category-done
))
5146 (forward-line -
1))))
5148 (defun todo-remove-item ()
5149 "Internal function called in editing, deleting or moving items."
5150 (let* ((end (progn (todo-item-end) (1+ (point))))
5151 (beg (todo-item-start))
5152 (ov (todo-get-overlay 'prefix
)))
5153 (when ov
(delete-overlay ov
))
5154 (delete-region beg end
)))
5156 (defun todo-diary-item-p ()
5157 "Return non-nil if item at point has diary entry format."
5159 (when (todo-item-string) ; Exclude empty lines.
5161 (not (looking-at (regexp-quote todo-nondiary-start
))))))
5163 ;; This duplicates the item locating code from diary-goto-entry, but
5164 ;; without the marker code, to test whether the latter is dispensable.
5165 ;; If it is, diary-goto-entry can be simplified. The code duplication
5166 ;; here can also be eliminated, leaving only the widening and category
5167 ;; selection, and instead of :override advice :around can be used.
5169 (defun todo-diary-goto-entry (button)
5170 "Jump to the diary entry for the BUTTON at point.
5171 If the entry is a todo item, display its category properly.
5172 Overrides `diary-goto-entry'."
5173 ;; Locate the diary item in its source file.
5174 (let* ((locator (button-get button
'locator
))
5175 (file (cadr locator
))
5176 (date (regexp-quote (nth 2 locator
)))
5177 (content (regexp-quote (nth 3 locator
))))
5178 (if (not (and (file-exists-p file
)
5179 (find-file-other-window file
)))
5180 (message "Unable to locate this diary entry")
5181 ;; If it's a Todo file, make sure it's in Todo mode.
5182 (when (and (equal (file-name-directory (file-truename file
))
5183 (file-truename todo-directory
))
5184 (not (derived-mode-p 'todo-mode
)))
5186 (when (eq major-mode
'todo-mode
) (widen))
5187 (goto-char (point-min))
5188 (when (re-search-forward (format "%s.*\\(%s\\)" date content
) nil t
)
5189 (goto-char (match-beginning 1)))
5190 ;; If it's a todo item, determine its category and display the
5191 ;; category properly.
5192 (when (eq major-mode
'todo-mode
)
5193 (let ((opoint (point)))
5194 (re-search-backward (concat "^" (regexp-quote todo-category-beg
)
5195 "\\(.*\\)\n") nil t
)
5196 (todo-category-number (match-string 1))
5197 (todo-category-select)
5198 (goto-char opoint
))))))
5200 (add-function :override diary-goto-entry-function
#'todo-diary-goto-entry
)
5202 (defun todo-revert-buffer (&optional ignore-auto noconfirm
)
5203 "Call `revert-buffer', preserving buffer's current modes.
5204 Also preserve category display, if applicable."
5205 (interactive (list (not current-prefix-arg
)))
5206 (let ((revert-buffer-function nil
))
5207 (revert-buffer ignore-auto noconfirm
'preserve-modes
)
5208 (when (memq major-mode
'(todo-mode todo-archive-mode
))
5209 (todo-category-select))))
5211 (defun todo-desktop-save-buffer (_dir)
5212 `((catnum .
,(todo-category-number (todo-current-category)))))
5214 (declare-function desktop-restore-file-buffer
"desktop"
5215 (buffer-filename buffer-name buffer-misc
))
5217 (defun todo-restore-desktop-buffer (file buffer misc
)
5218 (desktop-restore-file-buffer file buffer misc
)
5219 (with-current-buffer buffer
5221 (let ((todo-category-number (cdr (assq 'catnum misc
))))
5222 (todo-category-select))))
5224 (add-to-list 'desktop-buffer-mode-handlers
5225 '(todo-mode . todo-restore-desktop-buffer
))
5227 (defun todo-done-item-p ()
5228 "Return non-nil if item at point is a done item."
5231 (looking-at todo-done-string-start
)))
5233 (defun todo-done-item-section-p ()
5234 "Return non-nil if point is in category's done items section."
5236 (or (re-search-backward (concat "^" (regexp-quote todo-category-done
))
5238 (progn (goto-char (point-min))
5239 (looking-at todo-done-string-start
)))))
5241 (defun todo--user-error-if-marked-done-item ()
5242 "Signal user error on marked done items.
5243 Helper function for editing commands that apply only to (possibly
5244 marked) not done todo items."
5247 (goto-char (point-max))
5248 (todo-backward-item)
5249 (unless (todo-done-item-p)
5251 (unless (re-search-forward
5252 (concat "^" (regexp-quote todo-category-beg
)) nil t
)
5253 (goto-char (point-max)))
5255 (while (todo-done-item-p)
5256 (when (todo-marked-item-p)
5257 (user-error "This command does not apply to done items"))
5258 (todo-backward-item)))))
5260 (defun todo-reset-done-separator (sep)
5261 "Replace existing overlays of done items separator string SEP."
5265 (goto-char (point-min))
5266 (while (re-search-forward
5267 (concat "\n\\(" (regexp-quote todo-category-done
) "\\)") nil t
)
5268 (let* ((beg (match-beginning 1))
5270 (ov (progn (goto-char beg
)
5271 (todo-get-overlay 'separator
)))
5272 (old-sep (when ov
(overlay-get ov
'display
)))
5275 (unless (string= old-sep sep
)
5276 (setq new-ov
(make-overlay beg end
))
5277 (overlay-put new-ov
'todo
'separator
)
5278 (overlay-put new-ov
'display todo-done-separator
)
5279 (delete-overlay ov
))))))))
5281 (defun todo-get-overlay (val)
5282 "Return the overlay at point whose `todo' property has value VAL."
5283 ;; Use overlays-in to find prefix overlays and check over two
5284 ;; positions to find done separator overlay.
5285 (let ((ovs (overlays-in (point) (1+ (point))))
5290 (when (eq (overlay-get ov
'todo
) val
)
5291 (throw 'done ov
))))))
5293 (defun todo-marked-item-p ()
5294 "Non-nil if this item begins with `todo-item-mark'.
5295 In that case, return the item's prefix overlay."
5296 (let* ((ov (todo-get-overlay 'prefix
))
5297 ;; If an item insertion command is called on a todo file
5298 ;; before it is visited, it has no prefix overlays yet, so
5300 (pref (when ov
(overlay-get ov
'before-string
)))
5302 (string-match (concat "^" (regexp-quote todo-item-mark
))
5306 (defun todo-insert-with-overlays (item)
5307 "Insert ITEM at point and update prefix/priority number overlays."
5309 ;; Insertion pushes item down but not its prefix overlay. When the
5310 ;; overlay includes a mark, this would now mark the inserted ITEM,
5311 ;; so move it to the pushed down item.
5312 (let ((ov (todo-get-overlay 'prefix
))
5313 (marked (todo-marked-item-p)))
5315 (when marked
(move-overlay ov
(point) (point))))
5316 (todo-backward-item)
5317 (todo-prefix-overlays))
5319 (defun todo-prefix-overlays ()
5320 "Update the prefix overlays of the current category's items.
5321 The overlay's value is the string `todo-prefix' or with non-nil
5322 `todo-number-prefix' an integer in the sequence from 1 to
5323 the number of todo or done items in the category indicating the
5324 item's priority. Todo and done items are numbered independently
5327 (cat-tp (or (cdr (assoc-string
5328 (todo-current-category)
5329 (nth 2 (assoc-string todo-current-todo-file
5330 todo-top-priorities-overrides
))))
5331 (nth 1 (assoc-string todo-current-todo-file
5332 todo-top-priorities-overrides
))
5333 todo-top-priorities
))
5336 (goto-char (point-min))
5338 (when (or (todo-date-string-matcher (line-end-position))
5339 (todo-done-string-matcher (line-end-position)))
5340 (goto-char (match-beginning 0))
5342 ;; Reset number to 1 for first done item.
5343 (when (and (eq major-mode
'todo-mode
)
5344 (looking-at todo-done-string-start
)
5345 (looking-back (concat "^"
5346 (regexp-quote todo-category-done
)
5348 (line-beginning-position 0)))
5351 (setq prefix
(concat (propertize
5352 (if todo-number-prefix
5353 (number-to-string num
)
5356 ;; Prefix of top priority items has a
5357 ;; distinct face in Todo mode.
5358 (if (and (eq major-mode
'todo-mode
)
5362 'todo-prefix-string
))
5364 (let ((ov (todo-get-overlay 'prefix
))
5365 (marked (todo-marked-item-p)))
5366 ;; Prefix overlay must be at a single position so its
5367 ;; bounds aren't changed when (re)moving an item.
5368 (unless ov
(setq ov
(make-overlay (point) (point))))
5369 (overlay-put ov
'todo
'prefix
)
5370 (overlay-put ov
'before-string
(if marked
5371 (concat todo-item-mark prefix
)
5375 ;; -----------------------------------------------------------------------------
5376 ;;; Generating and applying item insertion and editing key sequences
5377 ;; -----------------------------------------------------------------------------
5379 ;; Thanks to Stefan Monnier for suggesting dynamically generating item
5380 ;; insertion commands and their key bindings, and offering an elegant
5381 ;; implementation, which, however, relies on lexical scoping and so
5382 ;; cannot be used here until the Calendar code used by todo-mode.el is
5383 ;; converted to lexical binding. Hence, the following implementation
5384 ;; uses dynamic binding.
5386 (defconst todo-insert-item--parameters
5387 '((default copy
) (diary nonmarking
) (calendar date dayname
) time
(here region
))
5388 "List of all item insertion parameters.
5389 Passed by `todo-insert-item' to `todo-insert-item--next-param' to
5390 dynamically create item insertion commands.")
5392 (defconst todo-insert-item--param-key-alist
5403 "List pairing item insertion parameters with their completion keys.")
5405 (defsubst todo-insert-item--keyof
(param)
5406 "Return key paired with item insertion PARAM."
5407 (cdr (assoc param todo-insert-item--param-key-alist
)))
5409 (defun todo-insert-item--argsleft (key list
)
5410 "Return sublist of LIST whose first member corresponds to KEY."
5416 (when (equal key
(todo-insert-item--keyof s
))
5417 (throw 'found1
(setq sym s
))))))
5424 (setq list
(reverse l
)))
5425 (memq (catch 'found2
5426 (dolist (e todo-insert-item--param-key-alist
)
5427 (when (equal key
(cdr e
))
5428 (throw 'found2
(car e
)))))
5431 (defsubst todo-insert-item--this-key
() (char-to-string last-command-event
))
5433 (defvar todo-insert-item--keys-so-far
""
5434 "String of item insertion keys so far entered for this command.")
5436 (defvar todo-insert-item--args nil
)
5437 (defvar todo-insert-item--argleft nil
)
5438 (defvar todo-insert-item--argsleft nil
)
5439 (defvar todo-insert-item--newargsleft nil
)
5441 (defun todo-insert-item--apply-args ()
5442 "Build list of arguments for item insertion and apply them.
5443 The list consists of item insertion parameters that can be passed
5444 as insertion command arguments in fixed positions. If a position
5445 in the list is not occupied by the corresponding parameter, it is
5447 (let* ((arg (list (car todo-insert-item--args
)))
5448 (args (nconc (cdr todo-insert-item--args
)
5449 (list (car (todo-insert-item--argsleft
5450 (todo-insert-item--this-key)
5451 todo-insert-item--argsleft
)))))
5452 (arglist (if (= 4 (length args
))
5454 (let ((v (make-vector 4 nil
)) elt
)
5456 (setq elt
(pop args
))
5457 (cond ((memq elt
'(diary nonmarking
))
5459 ((memq elt
'(calendar date dayname
))
5463 ((memq elt
'(copy here region
))
5466 (apply #'todo-insert-item--basic
(nconc arg arglist
))))
5468 (defun todo-insert-item--next-param (last args argsleft
)
5469 "Build item insertion command from LAST, ARGS and ARGSLEFT and call it.
5470 Dynamically generate key bindings, prompting with the keys
5471 already entered and those still available."
5472 (cl-assert argsleft
)
5473 (let* ((map (make-sparse-keymap))
5481 (if (memq name
'(default diary calendar here
))
5484 (when (memq name
'(copy nonmarking dayname region
))
5486 (propertize k
'face
'todo-key-prompt
)
5488 (setq todo-insert-item--args args
)
5489 (setq todo-insert-item--argsleft argsleft
)
5491 (if (memq last
'(default copy
))
5493 (setq todo-insert-item--argsleft nil
)
5494 (todo-insert-item--apply-args))
5495 (let ((k (todo-insert-item--keyof last
)))
5496 (funcall addprompt k
(make-symbol (concat (symbol-name last
) ":GO!")))
5497 (define-key map
(todo-insert-item--keyof last
)
5498 (lambda () (interactive)
5499 (todo-insert-item--apply-args))))))
5500 (while todo-insert-item--argsleft
5501 (let ((x (car todo-insert-item--argsleft
)))
5502 (setq todo-insert-item--newargsleft
(cdr todo-insert-item--argsleft
))
5503 (dolist (argleft (if (consp x
) x
(list x
)))
5504 (let ((k (todo-insert-item--keyof argleft
)))
5505 (funcall addprompt k argleft
)
5507 (if (null todo-insert-item--newargsleft
)
5508 (lambda () (interactive)
5509 (todo-insert-item--apply-args))
5510 (lambda () (interactive)
5511 (setq todo-insert-item--keys-so-far
5512 (concat todo-insert-item--keys-so-far
" "
5513 (todo-insert-item--this-key)))
5514 (todo-insert-item--next-param
5515 (car (todo-insert-item--argsleft
5516 (todo-insert-item--this-key)
5517 todo-insert-item--argsleft
))
5518 (nconc todo-insert-item--args
5519 (list (car (todo-insert-item--argsleft
5520 (todo-insert-item--this-key)
5521 todo-insert-item--argsleft
))))
5522 (cdr (todo-insert-item--argsleft
5523 (todo-insert-item--this-key)
5524 todo-insert-item--argsleft
)))))))))
5525 (setq todo-insert-item--argsleft todo-insert-item--newargsleft
))
5526 (when prompt
(message "Press a key (so far `%s'): %s"
5527 todo-insert-item--keys-so-far prompt
))
5528 (set-transient-map map
)
5529 (setq todo-insert-item--argsleft argsleft
)))
5531 (defconst todo-edit-item--param-key-alist
5539 "Alist of item editing parameters and their keys.")
5541 (defconst todo-edit-item--date-param-key-alist
5549 "Alist of item date editing parameters and their keys.")
5551 (defconst todo-edit-done-item--param-key-alist
5554 "Alist of done item comment editing parameters and their keys.")
5556 (defvar todo-edit-item--prompt
"Press a key (so far `e'): ")
5558 (defun todo-edit-item--next-key (params &optional arg
)
5559 (let* ((map (make-sparse-keymap))
5560 (p->k
(mapconcat (lambda (elt)
5562 (propertize (cdr elt
) 'face
5564 (concat (symbol-name (car elt
))
5565 (when (memq (car elt
)
5569 (this-key (let ((key (read-key (concat todo-edit-item--prompt p-
>k
))))
5570 (and (characterp key
) (char-to-string key
))))
5571 (this-param (car (rassoc this-key params
))))
5573 (`edit
(todo-edit-item--text))
5574 (`header
(todo-edit-item--text 'include-header
))
5575 (`multiline
(todo-edit-item--text 'multiline
))
5576 (`add
/edit
(todo-edit-item--text 'comment-edit
))
5577 (`delete
(todo-edit-item--text 'comment-delete
))
5578 (`diary
(todo-edit-item--diary-inclusion))
5579 (`nonmarking
(todo-edit-item--diary-inclusion 'nonmarking
))
5580 (`date
(let ((todo-edit-item--prompt "Press a key (so far `e d'): "))
5581 (todo-edit-item--next-key
5582 todo-edit-item--date-param-key-alist arg
)))
5583 (`full
(progn (todo-edit-item--header 'date
)
5584 (when todo-always-add-time-string
5585 (todo-edit-item--header 'time
))))
5586 (`calendar
(todo-edit-item--header 'calendar
))
5587 (`today
(todo-edit-item--header 'today
))
5588 (`dayname
(todo-edit-item--header 'dayname
))
5589 (`year
(todo-edit-item--header 'year arg
))
5590 (`month
(todo-edit-item--header 'month arg
))
5591 (`daynum
(todo-edit-item--header 'day arg
))
5592 (`time
(todo-edit-item--header 'time
)))))
5594 ;; -----------------------------------------------------------------------------
5595 ;;; Todo minibuffer utilities
5596 ;; -----------------------------------------------------------------------------
5598 (defcustom todo-y-with-space nil
5599 "Non-nil means allow SPC to affirm a \"y or n\" question."
5603 (defun todo-y-or-n-p (prompt)
5604 "Ask \"y or n\" question PROMPT and return t if answer is \"y\".
5605 Also return t if answer is \"Y\", but unlike `y-or-n-p', allow
5606 SPC to affirm the question only if option `todo-y-with-space' is
5608 (unless todo-y-with-space
5609 (define-key query-replace-map
" " 'ignore
))
5612 (define-key query-replace-map
" " 'act
)))
5614 (defun todo-category-completions (&optional archive
)
5615 "Return a list of completions for `todo-read-category'.
5616 Each element of the list is a cons of a category name and the
5617 file or list of files (as short file names) it is in. The files
5618 are either the current (or if there is none, the default) todo
5619 file plus the files listed in `todo-category-completions-files',
5620 or, with non-nil ARCHIVE, the current archive file.
5622 Before calculating the completions, update the value of
5623 `todo-category-completions-files' in case any files named in it
5626 (dolist (f todo-category-completions-files
)
5627 (unless (file-exists-p (todo-absolute-file-name f
))
5628 (setq todo-category-completions-files
5629 (delete f todo-category-completions-files
))
5632 (let ((pl (> (length deleted
) 1))
5633 (names (mapconcat (lambda (f) (concat "\"" f
"\"")) deleted
", ")))
5634 (message (concat "File" (if pl
"s" "") " " names
" ha" (if pl
"ve" "s")
5635 " been deleted and removed from\n"
5636 "the list of category completion files")))
5637 (todo-reevaluate-category-completions-files-defcustom)
5638 (custom-set-default 'todo-category-completions-files
5639 (symbol-value 'todo-category-completions-files
))
5641 (let* ((curfile (or todo-current-todo-file
5642 (and todo-show-current-file
5643 todo-global-current-todo-file
)
5644 (todo-absolute-file-name todo-default-todo-file
)))
5645 (files (or (unless archive
5646 (mapcar 'todo-absolute-file-name
5647 todo-category-completions-files
))
5650 ;; If file was just added, it has no category completions.
5651 (unless (zerop (buffer-size (find-buffer-visiting curfile
)))
5652 (unless (member curfile todo-archives
)
5653 (add-to-list 'files curfile
))
5654 (dolist (f files listall
)
5655 (with-current-buffer (find-file-noselect f
'nowarn
)
5657 (unless (derived-mode-p 'todo-archive-mode
) (todo-archive-mode))
5658 (unless (derived-mode-p 'todo-mode
) (todo-mode)))
5659 ;; Ensure category is properly displayed in case user
5660 ;; switches to file via a non-Todo mode command. And if
5661 ;; done items in category are visible, keep them visible.
5662 (let ((done todo-show-with-done
))
5663 (when (> (buffer-size) (- (point-max) (point-min)))
5665 (goto-char (point-min))
5666 (setq done
(re-search-forward todo-done-string-start nil t
))))
5667 (let ((todo-show-with-done done
))
5668 (save-excursion (todo-category-select))))
5672 (goto-char (point-min))
5673 (setq listf
(read (buffer-substring-no-properties
5674 (line-beginning-position)
5675 (line-end-position)))))))
5676 (mapc (lambda (elt) (let* ((cat (car elt
))
5677 (la-elt (assoc cat listall
)))
5679 (setcdr la-elt
(append (list (cdr la-elt
))
5681 (push (cons cat f
) listall
))))
5684 (defun todo-read-file-name (prompt &optional archive mustmatch
)
5685 "Choose and return the name of a todo file, prompting with PROMPT.
5687 Show completions with TAB or SPC; the names are shown in short
5688 form but the absolute truename is returned. With non-nil ARCHIVE
5689 return the absolute truename of a todo archive file. With non-nil
5690 MUSTMATCH the name of an existing file must be chosen;
5691 otherwise, a new file name is allowed."
5692 (let* ((completion-ignore-case todo-completion-ignore-case
)
5693 (files (mapcar 'todo-short-file-name
5694 ;; (funcall todo-files-function archive)))
5695 (if archive todo-archives todo-files
)))
5696 (file (completing-read prompt files nil mustmatch nil nil
5698 ;; If user hit RET without
5699 ;; choosing a file, default to
5700 ;; current or default file.
5701 (todo-short-file-name
5702 (or todo-current-todo-file
5703 (and todo-show-current-file
5704 todo-global-current-todo-file
)
5705 (todo-absolute-file-name
5706 todo-default-todo-file
)))
5707 ;; Trigger prompt for initial file.
5709 (unless (file-exists-p todo-directory
)
5710 (make-directory todo-directory
))
5711 (unless (or mustmatch
(member file files
))
5712 (setq file
(todo-validate-name file
'file
)))
5713 (setq file
(file-truename (concat todo-directory file
5714 (if archive
".toda" ".todo"))))))
5716 (defun todo-read-category (prompt &optional match-type file
)
5717 "Choose and return a category name, prompting with PROMPT.
5718 Show completions for existing categories with TAB or SPC.
5720 The argument MATCH-TYPE specifies the matching requirements on
5721 the category name: with the value `todo' or `archive' the name
5722 must complete to that of an existing todo or archive category,
5723 respectively; with the value `add' the name must not be that of
5724 an existing category; with all other values both existing and new
5725 valid category names are accepted.
5727 With non-nil argument FILE prompt for a file and complete only
5728 against categories in that file; otherwise complete against all
5729 categories from `todo-category-completions-files'."
5730 ;; Allow SPC to insert spaces, for adding new category names.
5731 (let ((map minibuffer-local-completion-map
))
5732 (define-key map
" " nil
)
5733 (let* ((add (eq match-type
'add
))
5734 (archive (eq match-type
'archive
))
5735 (file0 (when (and file
(> (length todo-files
) 1))
5736 (todo-read-file-name (concat "Choose a" (if archive
5739 " file: ") archive t
)))
5740 (completions (unless file0
(todo-category-completions archive
)))
5741 (categories (cond (file0
5742 (with-current-buffer
5743 (find-file-noselect file0
'nowarn
)
5744 (unless (derived-mode-p 'todo-mode
) (todo-mode))
5745 (let ((todo-current-todo-file file0
))
5747 ((and add
(not file
))
5748 (with-current-buffer
5749 (find-file-noselect todo-current-todo-file
)
5753 (completion-ignore-case todo-completion-ignore-case
)
5754 (cat (completing-read prompt categories nil
5755 (eq match-type
'todo
) nil nil
5756 ;; Unless we're adding a category via
5757 ;; todo-add-category, set default
5758 ;; for existing categories to the
5759 ;; current category of the chosen
5760 ;; file or else of the current file.
5761 (if (and categories
(not add
))
5762 (with-current-buffer
5765 todo-current-todo-file
5766 (todo-absolute-file-name
5767 todo-default-todo-file
)))
5768 (todo-current-category))
5769 ;; Trigger prompt for initial category.
5771 (catfil (cdr (assoc cat completions
)))
5772 (str "Category \"%s\" from which file (TAB for choices)? "))
5773 ;; If we do category completion and the chosen category name
5774 ;; occurs in more than one file, prompt to choose one file.
5775 (unless (or file0 add
(not catfil
))
5776 (setq file0
(file-truename
5779 (todo-absolute-file-name
5780 (let ((files (mapcar 'todo-short-file-name catfil
)))
5781 (completing-read (format str cat
) files
)))))))
5782 ;; Default to the current file.
5783 (unless file0
(setq file0 todo-current-todo-file
))
5784 ;; First validate only a name passed interactively from
5785 ;; todo-add-category, which must be of a nonexistent category.
5786 (unless (and (assoc cat categories
) (not add
))
5787 ;; Validate only against completion categories.
5788 (let ((todo-categories categories
))
5789 (setq cat
(todo-validate-name cat
'category
)))
5790 ;; When user enters a nonexistent category name by jumping or
5791 ;; moving, confirm that it should be added, then validate.
5793 (if (todo-y-or-n-p (format "Add new category \"%s\" to file \"%s\"? "
5794 cat
(todo-short-file-name file0
)))
5796 (when (assoc cat categories
)
5797 (let ((todo-categories categories
))
5798 (setq cat
(todo-validate-name cat
'category
))))
5799 ;; Restore point and narrowing after adding new
5800 ;; category, to avoid moving to beginning of file when
5801 ;; moving marked items to a new category
5802 ;; (todo-move-item).
5805 (todo-add-category file0 cat
))))
5806 ;; If we decide not to add a category, exit without returning.
5810 (defun todo-validate-name (name type
)
5811 "Prompt for new NAME for TYPE until it is valid, then return it.
5812 TYPE can be either of the symbols `file' or `category'."
5813 (let ((categories todo-categories
)
5814 (files (mapcar 'todo-short-file-name todo-files
))
5818 (cond ((string= "" name
)
5820 (cond ((eq type
'file
)
5822 "Enter a non-empty file name: "
5823 ;; Empty string passed by todo-show to
5824 ;; prompt for initial todo file.
5825 (concat "Initial file name ["
5826 todo-initial-file
"]: ")))
5827 ((eq type
'category
)
5829 "Enter a non-empty category name: "
5830 ;; Empty string passed by todo-show to
5831 ;; prompt for initial category of a new
5833 (concat "Initial category name ["
5834 todo-initial-category
"]: "))))))
5835 ((string-match "\\`\\s-+\\'" name
)
5837 "Enter a name that does not contain only white space: "))
5838 ((and (eq type
'file
) (member name files
))
5839 (setq prompt
"Enter a non-existing file name: "))
5840 ((and (eq type
'category
) (assoc name categories
))
5841 (setq prompt
"Enter a non-existing category name: ")))
5842 (setq name
(if (or (and (eq type
'file
) files
)
5843 (and (eq type
'category
) categories
))
5844 (completing-read prompt
(cond ((eq type
'file
)
5846 ((eq type
'category
)
5848 ;; Offer default initial name.
5849 (completing-read prompt
(if (eq type
'file
)
5852 nil nil
(if (eq type
'file
)
5854 todo-initial-category
))))))
5857 ;; Adapted from calendar-read-date and calendar-date-string.
5858 (defun todo-read-date (&optional arg mo yr
)
5859 "Prompt for Gregorian date and return it in the current format.
5861 With non-nil ARG, prompt for and return only the date component
5862 specified by ARG, which can be one of these symbols:
5863 `month' (prompt for name, return name or number according to
5864 value of `calendar-date-display-form'), `day' of month, or
5865 `year'. The value of each of these components can be `*',
5866 indicating an unspecified month, day, or year.
5868 When ARG is `day', non-nil arguments MO and YR determine the
5869 number of the last the day of the month."
5870 (let (year monthname month day
5871 dayname
) ; Needed by calendar-date-display-form.
5872 (when (or (not arg
) (eq arg
'year
))
5873 (while (if (natnump year
) (< year
1) (not (eq year
'*)))
5874 (setq year
(read-from-minibuffer
5875 "Year (>0 or RET for this year or * for any year): "
5876 nil nil t nil
(number-to-string
5877 (calendar-extract-year
5878 (calendar-current-date)))))))
5879 (when (or (not arg
) (eq arg
'month
))
5880 (let* ((marray todo-month-name-array
)
5881 (mlist (append marray nil
))
5882 (mabarray todo-month-abbrev-array
)
5883 (mablist (append mabarray nil
))
5884 (completion-ignore-case todo-completion-ignore-case
))
5885 (setq monthname
(completing-read
5886 "Month name (RET for current month, * for any month): "
5888 (calendar-month-name (calendar-extract-month
5889 (calendar-current-date)) t
))
5890 month
(1+ (- (length mlist
)
5891 (length (or (member monthname mlist
)
5892 (member monthname mablist
))))))
5893 (setq monthname
(aref mabarray
(1- month
)))))
5894 (when (or (not arg
) (eq arg
'day
))
5895 (let ((last (let ((mm (or month mo
))
5897 ;; If month is unspecified, use a month with 31
5898 ;; days for checking day of month input. Does
5899 ;; Calendar do anything special when * is
5900 ;; currently a shorter month?
5901 (if (= mm
13) (setq mm
1))
5902 ;; If year is unspecified, use a leap year to
5904 (if (eq year
'*) (setq yy
2012))
5905 (calendar-last-day-of-month mm yy
))))
5906 (while (if (natnump day
) (or (< day
1) (> day last
)) (not (eq day
'*)))
5907 (setq day
(read-from-minibuffer
5908 (format "Day (1-%d or RET for today or * for any day): "
5910 nil nil t nil
(number-to-string
5911 (calendar-extract-day
5912 (calendar-current-date))))))))
5913 ;; Stringify read values (monthname is already a string).
5914 (and year
(setq year
(if (eq year
'*)
5916 (number-to-string year
))))
5917 (and day
(setq day
(if (eq day
'*)
5919 (number-to-string day
))))
5920 (and month
(setq month
(if (eq month
'*)
5922 (number-to-string month
))))
5924 (cond ((eq arg
'year
) year
)
5927 (if (memq 'month calendar-date-display-form
)
5930 (mapconcat 'eval calendar-date-display-form
""))))
5932 (defun todo-read-dayname ()
5933 "Choose name of a day of the week with completion and return it."
5934 (let ((completion-ignore-case todo-completion-ignore-case
))
5935 (completing-read "Enter a day name: "
5936 (append calendar-day-name-array nil
)
5939 (defun todo-read-time ()
5940 "Prompt for and return a valid clock time as a string.
5942 Valid time strings are those matching `diary-time-regexp'.
5943 Typing `<return>' at the prompt returns the current time, if the
5944 user option `todo-always-add-time-string' is non-nil, otherwise
5945 the empty string (i.e., no time string)."
5948 (setq answer
(read-string "Enter a clock time: " nil nil
5949 (when todo-always-add-time-string
5950 (substring (current-time-string) 11 16))))
5951 (when (or (string= "" answer
)
5952 (string-match diary-time-regexp answer
))
5956 ;; -----------------------------------------------------------------------------
5957 ;;; Customization groups and utilities
5958 ;; -----------------------------------------------------------------------------
5961 "Create and maintain categorized lists of todo items."
5962 :link
'(emacs-commentary-link "todo")
5966 (defgroup todo-edit nil
5967 "User options for adding and editing todo items."
5971 (defgroup todo-categories nil
5972 "User options for Todo Categories mode."
5976 (defgroup todo-filtered nil
5977 "User options for Todo Filter Items mode."
5981 (defgroup todo-display nil
5982 "User display options for Todo mode."
5986 (defgroup todo-faces nil
5987 "Faces for the Todo modes."
5991 (defun todo-set-show-current-file (symbol value
)
5992 "The :set function for user option `todo-show-current-file'."
5993 (custom-set-default symbol value
)
5995 (add-hook 'pre-command-hook
'todo-show-current-file nil t
)
5996 (remove-hook 'pre-command-hook
'todo-show-current-file t
)))
5998 (defun todo-reset-prefix (symbol value
)
5999 "The :set function for `todo-prefix' and `todo-number-prefix'."
6000 (let ((oldvalue (symbol-value symbol
))
6001 (files todo-file-buffers
))
6002 (custom-set-default symbol value
)
6003 (when (not (equal value oldvalue
))
6005 (with-current-buffer (find-file-noselect f
)
6006 ;; Activate the new setting in the current category.
6007 (save-excursion (todo-category-select)))))))
6009 (defun todo-reset-nondiary-marker (symbol value
)
6010 "The :set function for user option `todo-nondiary-marker'."
6011 (let* ((oldvalue (symbol-value symbol
))
6012 (files (append todo-files todo-archives
6013 (directory-files todo-directory t
"\.tod[rty]$" t
))))
6014 (custom-set-default symbol value
)
6015 ;; Need to reset these to get font-locking right.
6016 (setq todo-nondiary-start
(nth 0 todo-nondiary-marker
)
6017 todo-nondiary-end
(nth 1 todo-nondiary-marker
)
6018 todo-date-string-start
6019 ;; See comment in defvar of `todo-date-string-start'.
6020 (concat "^\\(" (regexp-quote todo-nondiary-start
) "\\|"
6021 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
6022 (when (not (equal value oldvalue
))
6024 (let ((buf (find-buffer-visiting f
)))
6025 (with-current-buffer (find-file-noselect f
)
6026 (let (buffer-read-only)
6028 (goto-char (point-min))
6030 (if (re-search-forward
6031 (concat "^\\(" todo-done-string-start
"[^][]+] \\)?"
6032 "\\(?1:" (regexp-quote (car oldvalue
))
6033 "\\)" todo-date-pattern
"\\( "
6034 diary-time-regexp
"\\)?\\(?2:"
6035 (regexp-quote (cadr oldvalue
)) "\\)")
6038 (replace-match (nth 0 value
) t t nil
1)
6039 (replace-match (nth 1 value
) t t nil
2))
6042 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6043 (todo-category-select))
6045 (kill-buffer)))))))))
6047 (defun todo-reset-done-separator-string (symbol value
)
6048 "The :set function for `todo-done-separator-string'."
6049 (let ((oldvalue (symbol-value symbol
))
6050 (files todo-file-buffers
)
6051 (sep todo-done-separator
))
6052 (custom-set-default symbol value
)
6053 (when (not (equal value oldvalue
))
6055 (with-current-buffer (find-file-noselect f
)
6056 (let (buffer-read-only)
6057 (setq todo-done-separator
(todo-done-separator))
6058 (when (= 1 (length value
))
6059 (todo-reset-done-separator sep
)))
6060 (todo-category-select))))))
6062 (defun todo-reset-done-string (symbol value
)
6063 "The :set function for user option `todo-done-string'."
6064 (let ((oldvalue (symbol-value symbol
))
6065 (files (append todo-files todo-archives
6066 (directory-files todo-directory t
"\.todr$" t
))))
6067 (custom-set-default symbol value
)
6068 ;; Need to reset this to get font-locking right.
6069 (setq todo-done-string-start
6070 (concat "^\\[" (regexp-quote todo-done-string
)))
6071 (when (not (equal value oldvalue
))
6073 (let ((buf (find-buffer-visiting f
)))
6074 (with-current-buffer (find-file-noselect f
)
6075 (let (buffer-read-only)
6077 (goto-char (point-min))
6079 (if (re-search-forward
6080 (concat "^" (regexp-quote todo-nondiary-start
)
6081 "\\(" (regexp-quote oldvalue
) "\\)")
6083 (replace-match value t t nil
1)
6086 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6087 (todo-category-select))
6089 (kill-buffer)))))))))
6091 (defun todo-reset-comment-string (symbol value
)
6092 "The :set function for user option `todo-comment-string'."
6093 (let ((oldvalue (symbol-value symbol
))
6094 (files (append todo-files todo-archives
6095 (directory-files todo-directory t
"\.todr$" t
))))
6096 (custom-set-default symbol value
)
6097 (when (not (equal value oldvalue
))
6099 (let ((buf (find-buffer-visiting f
)))
6100 (with-current-buffer (find-file-noselect f
)
6101 (let (buffer-read-only)
6103 (goto-char (point-min))
6105 (if (re-search-forward
6106 (concat "\\[\\(" (regexp-quote oldvalue
)
6109 (replace-match value t t nil
1)
6112 (when (derived-mode-p 'todo-mode
'todo-archive-mode
)
6113 (todo-category-select))
6115 (kill-buffer)))))))))
6117 (defun todo-reset-highlight-item (symbol value
)
6118 "The :set function for user option `todo-highlight-item'."
6119 (let ((oldvalue (symbol-value symbol
))
6120 (files (append todo-files todo-archives
6121 (directory-files todo-directory t
"\.tod[rty]$" t
))))
6122 (custom-set-default symbol value
)
6123 (when (not (equal value oldvalue
))
6125 (let ((buf (find-buffer-visiting f
)))
6127 (with-current-buffer buf
6131 (hl-line-mode -
1)))))))))
6133 (defun todo-reevaluate-filelist-defcustoms ()
6134 "Reevaluate defcustoms that provide choice list of todo files."
6135 (custom-set-default 'todo-default-todo-file
6136 (symbol-value 'todo-default-todo-file
))
6137 (todo-reevaluate-default-file-defcustom)
6138 (custom-set-default 'todo-filter-files
(symbol-value 'todo-filter-files
))
6139 (todo-reevaluate-filter-files-defcustom)
6140 (custom-set-default 'todo-category-completions-files
6141 (symbol-value 'todo-category-completions-files
))
6142 (todo-reevaluate-category-completions-files-defcustom))
6144 (defun todo-reevaluate-default-file-defcustom ()
6145 "Reevaluate defcustom of `todo-default-todo-file'.
6146 Called after adding or deleting a todo file. If the value of
6147 `todo-default-todo-file' before calling this function was
6148 associated with an existing file, keep that value."
6149 ;; (let ((curval todo-default-todo-file))
6151 (defcustom todo-default-todo-file
(todo-short-file-name
6152 (car (funcall todo-files-function
)))
6153 "Todo file visited by first session invocation of `todo-show'."
6154 :type
(when todo-files
6155 `(radio ,@(mapcar (lambda (f) (list 'const f
))
6156 (mapcar 'todo-short-file-name
6157 (funcall todo-files-function
)))))
6159 ;; (when (and curval (file-exists-p (todo-absolute-file-name curval)))
6160 ;; (custom-set-default 'todo-default-todo-file curval)
6161 ;; ;; (custom-reevaluate-setting 'todo-default-todo-file)
6165 (defun todo-reevaluate-category-completions-files-defcustom ()
6166 "Reevaluate defcustom of `todo-category-completions-files'.
6167 Called after adding or deleting a todo file."
6168 (eval (defcustom todo-category-completions-files nil
6169 "List of files for building `todo-read-category' completions."
6170 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
6171 (mapcar 'todo-short-file-name
6172 (funcall todo-files-function
))))
6175 (defun todo-reevaluate-filter-files-defcustom ()
6176 "Reevaluate defcustom of `todo-filter-files'.
6177 Called after adding or deleting a todo file."
6178 (eval (defcustom todo-filter-files nil
6179 "List of files for multifile item filtering."
6180 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
6181 (mapcar 'todo-short-file-name
6182 (funcall todo-files-function
))))
6185 ;; -----------------------------------------------------------------------------
6187 ;; -----------------------------------------------------------------------------
6189 (defun todo-nondiary-marker-matcher (lim)
6190 "Search for todo item nondiary markers within LIM for font-locking."
6191 (re-search-forward (concat "^\\(?1:" (regexp-quote todo-nondiary-start
) "\\)"
6192 todo-date-pattern
"\\(?: " diary-time-regexp
6193 "\\)?\\(?2:" (regexp-quote todo-nondiary-end
) "\\)")
6196 (defun todo-diary-nonmarking-matcher (lim)
6197 "Search for diary nonmarking symbol within LIM for font-locking."
6198 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
6199 "\\)" todo-date-pattern
) lim t
))
6201 (defun todo-date-string-matcher (lim)
6202 "Search for todo item date string within LIM for font-locking."
6204 (concat todo-date-string-start
"\\(?1:" todo-date-pattern
"\\)") lim t
))
6206 (defun todo-time-string-matcher (lim)
6207 "Search for todo item time string within LIM for font-locking."
6208 (re-search-forward (concat todo-date-string-start todo-date-pattern
6209 " \\(?1:" diary-time-regexp
"\\)") lim t
))
6211 (defun todo-diary-expired-matcher (lim)
6212 "Search for expired diary item date within LIM for font-locking."
6213 (when (re-search-forward (concat "^\\(?:"
6214 (regexp-quote diary-nonmarking-symbol
)
6215 "\\)?\\(?1:" todo-date-pattern
"\\) \\(?2:"
6216 diary-time-regexp
"\\)?") lim t
)
6217 (let* ((date (match-string-no-properties 1))
6218 (time (match-string-no-properties 2))
6219 ;; Function days-between requires a non-empty time string.
6220 (date-time (concat date
" " (or time
"00:00"))))
6221 (or (and (not (string-match ".+day\\|\\*" date
))
6222 (< (days-between date-time
(current-time-string)) 0))
6223 (todo-diary-expired-matcher lim
)))))
6225 (defun todo-done-string-matcher (lim)
6226 "Search for done todo item header within LIM for font-locking."
6227 (re-search-forward (concat todo-done-string-start
6231 (defun todo-comment-string-matcher (lim)
6232 "Search for done todo item comment within LIM for font-locking."
6233 (re-search-forward (concat "\\[\\(?1:" todo-comment-string
"\\):")
6236 (defun todo-category-string-matcher-1 (lim)
6237 "Search for todo category name within LIM for font-locking.
6238 This is for fontifying category and file names appearing in Todo
6239 Filtered Items mode following done items."
6240 (if (eq major-mode
'todo-filtered-items-mode
)
6241 (re-search-forward (concat todo-done-string-start todo-date-pattern
6242 "\\(?: " diary-time-regexp
6243 ;; Use non-greedy operator to prevent
6244 ;; capturing possible following non-diary
6246 "\\)?] \\(?1:\\[.+?\\]\\)")
6249 (defun todo-category-string-matcher-2 (lim)
6250 "Search for todo category name within LIM for font-locking.
6251 This is for fontifying category and file names appearing in Todo
6252 Filtered Items mode following todo (not done) items."
6253 (if (eq major-mode
'todo-filtered-items-mode
)
6254 (re-search-forward (concat todo-date-string-start todo-date-pattern
6255 "\\(?: " diary-time-regexp
"\\)?\\(?:"
6256 (regexp-quote todo-nondiary-end
)
6257 "\\)? \\(?1:\\[.+\\]\\)")
6260 (defvar todo-nondiary-face
'todo-nondiary
)
6261 (defvar todo-date-face
'todo-date
)
6262 (defvar todo-time-face
'todo-time
)
6263 (defvar todo-diary-expired-face
'todo-diary-expired
)
6264 (defvar todo-done-sep-face
'todo-done-sep
)
6265 (defvar todo-done-face
'todo-done
)
6266 (defvar todo-comment-face
'todo-comment
)
6267 (defvar todo-category-string-face
'todo-category-string
)
6268 (defvar todo-font-lock-keywords
6270 '(todo-nondiary-marker-matcher 1 todo-nondiary-face t
)
6271 '(todo-nondiary-marker-matcher 2 todo-nondiary-face t
)
6272 ;; diary-lib.el uses font-lock-constant-face for diary-nonmarking-symbol.
6273 '(todo-diary-nonmarking-matcher 1 font-lock-constant-face t
)
6274 '(todo-date-string-matcher 1 todo-date-face t
)
6275 '(todo-time-string-matcher 1 todo-time-face t
)
6276 '(todo-done-string-matcher 0 todo-done-face t
)
6277 '(todo-comment-string-matcher 1 todo-comment-face t
)
6278 '(todo-category-string-matcher-1 1 todo-category-string-face t t
)
6279 '(todo-category-string-matcher-2 1 todo-category-string-face t t
)
6280 '(todo-diary-expired-matcher 1 todo-diary-expired-face t
)
6281 '(todo-diary-expired-matcher 2 todo-diary-expired-face t t
)
6283 "Font-locking for Todo modes.")
6285 ;; -----------------------------------------------------------------------------
6287 ;; -----------------------------------------------------------------------------
6289 (defvar todo-key-bindings-t
6291 ("Af" todo-find-archive
)
6292 ("Ac" todo-choose-archive
)
6293 ("Ad" todo-archive-done-item
)
6294 ("Cv" todo-toggle-view-done-items
)
6295 ("v" todo-toggle-view-done-items
)
6296 ("Ca" todo-add-category
)
6297 ("Cr" todo-rename-category
)
6298 ("Cg" todo-merge-category
)
6299 ("Cm" todo-move-category
)
6300 ("Ck" todo-delete-category
)
6301 ("Cts" todo-set-top-priorities-in-category
)
6302 ("Cey" todo-edit-category-diary-inclusion
)
6303 ("Cek" todo-edit-category-diary-nonmarking
)
6304 ("Fa" todo-add-file
)
6305 ("Fr" todo-rename-file
)
6306 ("Ff" todo-find-filtered-items-file
)
6307 ("FV" todo-toggle-view-done-only
)
6308 ("V" todo-toggle-view-done-only
)
6309 ("Ftt" todo-filter-top-priorities
)
6310 ("Ftm" todo-filter-top-priorities-multifile
)
6311 ("Fts" todo-set-top-priorities-in-file
)
6312 ("Fyy" todo-filter-diary-items
)
6313 ("Fym" todo-filter-diary-items-multifile
)
6314 ("Fxx" todo-filter-regexp-items
)
6315 ("Fxm" todo-filter-regexp-items-multifile
)
6316 ("e" todo-edit-item
)
6317 ("d" todo-item-done
)
6318 ("i" todo-insert-item
)
6319 ("k" todo-delete-item
)
6320 ("m" todo-move-item
)
6321 ("u" todo-item-undone
)
6322 ([remap newline
] newline-and-indent
)
6324 "List of key bindings for Todo mode only.")
6326 (defvar todo-key-bindings-t
+a
+f
6328 ("C*" todo-mark-category
)
6329 ("Cu" todo-unmark-category
)
6330 ("Fh" todo-toggle-item-header
)
6331 ("h" todo-toggle-item-header
)
6332 ("Fk" todo-delete-file
)
6333 ("Fe" todo-edit-file
)
6334 ("FH" todo-toggle-item-highlighting
)
6335 ("H" todo-toggle-item-highlighting
)
6336 ("FN" todo-toggle-prefix-numbers
)
6337 ("N" todo-toggle-prefix-numbers
)
6338 ("PB" todo-print-buffer
)
6339 ("PF" todo-print-buffer-to-file
)
6340 ("b" todo-backward-category
)
6341 ("d" todo-item-done
)
6342 ("f" todo-forward-category
)
6343 ("j" todo-jump-to-category
)
6344 ("n" todo-next-item
)
6345 ("p" todo-previous-item
)
6350 "List of key bindings for Todo, Archive, and Filtered Items modes.")
6352 (defvar todo-key-bindings-t
+a
6354 ("Fc" todo-show-categories-table
)
6356 ("X" todo-clear-matches
)
6357 ("*" todo-toggle-mark-item
)
6359 "List of key bindings for Todo and Todo Archive modes.")
6361 (defvar todo-key-bindings-t
+f
6363 ("l" todo-lower-item-priority
)
6364 ("r" todo-raise-item-priority
)
6365 ("#" todo-set-item-priority
)
6367 "List of key bindings for Todo and Todo Filtered Items modes.")
6369 (defvar todo-mode-map
6370 (let ((map (make-keymap)))
6371 ;; Don't suppress digit keys, so they can supply prefix arguments.
6372 (suppress-keymap map
)
6373 (dolist (kb todo-key-bindings-t
)
6374 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6375 (dolist (kb todo-key-bindings-t
+a
+f
)
6376 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6377 (dolist (kb todo-key-bindings-t
+a
)
6378 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6379 (dolist (kb todo-key-bindings-t
+f
)
6380 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6382 "Todo mode keymap.")
6384 (defvar todo-archive-mode-map
6385 (let ((map (make-sparse-keymap)))
6386 (suppress-keymap map
)
6387 (dolist (kb todo-key-bindings-t
+a
+f
)
6388 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6389 (dolist (kb todo-key-bindings-t
+a
)
6390 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6391 (define-key map
"a" 'todo-jump-to-archive-category
)
6392 (define-key map
"u" 'todo-unarchive-items
)
6394 "Todo Archive mode keymap.")
6396 (defvar todo-edit-mode-map
6397 (let ((map (make-sparse-keymap)))
6398 (define-key map
"\C-x\C-q" 'todo-edit-quit
)
6399 (define-key map
[remap newline
] 'newline-and-indent
)
6401 "Todo Edit mode keymap.")
6403 (defvar todo-categories-mode-map
6404 (let ((map (make-sparse-keymap)))
6405 (suppress-keymap map
)
6406 (define-key map
"c" 'todo-sort-categories-alphabetically-or-numerically
)
6407 (define-key map
"t" 'todo-sort-categories-by-todo
)
6408 (define-key map
"y" 'todo-sort-categories-by-diary
)
6409 (define-key map
"d" 'todo-sort-categories-by-done
)
6410 (define-key map
"a" 'todo-sort-categories-by-archived
)
6411 (define-key map
"#" 'todo-set-category-number
)
6412 (define-key map
"l" 'todo-lower-category
)
6413 (define-key map
"r" 'todo-raise-category
)
6414 (define-key map
"n" 'todo-next-button
)
6415 (define-key map
"p" 'todo-previous-button
)
6416 (define-key map
[tab] 'todo-next-button)
6417 (define-key map [backtab] 'todo-previous-button)
6418 (define-key map "q" 'todo-quit)
6420 "Todo Categories mode keymap.")
6422 (defvar todo-filtered-items-mode-map
6423 (let ((map (make-sparse-keymap)))
6424 (suppress-keymap map)
6425 (dolist (kb todo-key-bindings-t+a+f)
6426 (define-key map (nth 0 kb) (nth 1 kb)))
6427 (dolist (kb todo-key-bindings-t+f)
6428 (define-key map (nth 0 kb) (nth 1 kb)))
6429 (define-key map "g" 'todo-go-to-source-item)
6430 (define-key map [remap newline] 'todo-go-to-source-item)
6432 "Todo Filtered Items mode keymap.")
6435 todo-menu todo-mode-map "Todo Menu"
6438 ["Next Item" todo-next-item t]
6439 ["Previous Item" todo-previous-item t]
6441 ["Next Category" todo-forward-category t]
6442 ["Previous Category" todo-backward-category t]
6443 ["Jump to Another Category" todo-jump-to-category t]
6445 ["Visit Another Todo File" todo-show t]
6446 ["Visit Archive" todo-find-archive t]
6447 ["Visit Filtered Items File" todo-find-filtered-items-file t]
6450 ["Insert New Item" todo-insert-item t]
6451 ["Edit Item" todo-edit-item t]
6452 ["Lower Item Priority" todo-lower-item-priority t]
6453 ["Raise Item Priority" todo-raise-item-priority t]
6454 ["Set Item Priority" todo-set-item-priority t]
6455 ["Mark/Unmark Item" todo-toggle-mark-item t]
6456 ["Move (Recategorize) Item" todo-move-item t]
6457 ["Delete Item" todo-delete-item t]
6458 ["Mark and Bury Done Item" todo-item-done t]
6459 ["Undo Done Item" todo-item-undone t]
6460 ["Archive Done Item" todo-archive-done-item t]
6462 ["Add New Category" todo-add-category t]
6463 ["Rename Current Category" todo-rename-category t]
6464 ["Delete Current Category" todo-delete-category t]
6465 ["Move Current Category" todo-move-category t]
6466 ["Merge Current Category" todo-merge-category t]
6468 ["Add New Todo File" todo-add-file t]
6469 ["Rename Todo File" todo-rename-file t]
6470 ["Delete Todo File" todo-delete-file t]
6471 ["Edit Todo File" todo-edit-file t]
6473 ("Searching and Item Filtering"
6474 ["Search Todo File" todo-search t]
6475 ["Clear Match Highlighting" todo-clear-matches t]
6477 ["Set Top Priorities in File" todo-set-top-priorities-in-file t]
6478 ["Set Top Priorities in Category" todo-set-top-priorities-in-category t]
6479 ["Filter Top Priorities" todo-filter-top-priorities t]
6480 ["Filter Multifile Top Priorities" todo-filter-top-priorities-multifile t]
6481 ["Filter Diary Items" todo-filter-diary-items t]
6482 ["Filter Multifile Diary Items" todo-filter-diary-items-multifile t]
6483 ["Filter Regexp" todo-filter-regexp-items t]
6484 ["Filter Multifile Regexp" todo-filter-regexp-items-multifile t]
6486 ("Display and Printing"
6487 ["Show/Hide Done Items" todo-toggle-view-done-items t]
6488 ["Show/Hide Done Items Only" todo-toggle-view-done-only t]
6489 ["Show/Hide Item Highlighting" todo-toggle-item-highlighting t]
6490 ["Show/Hide Item Numbering" todo-toggle-prefix-numbers t]
6491 ["Show/Hide Item Header" todo-toggle-item-header t]
6493 ["Display Table of Categories" todo-show-categories-table t]
6495 ["Print Category" todo-print-buffer t]
6496 ["Print Category to File" todo-print-buffer-to-file t]
6499 ["Save Todo File" todo-save t]
6500 ["Quit Todo Mode" todo-quit t]
6503 ;; -----------------------------------------------------------------------------
6504 ;;; Hook functions and mode definitions
6505 ;; -----------------------------------------------------------------------------
6507 (defun todo-show-current-file ()
6508 "Visit current instead of default todo file with `todo-show'.
6509 Added to `pre-command-hook' in Todo mode when user option
6510 `todo-show-current-file' is set to non-nil."
6511 (setq todo-global-current-todo-file todo-current-todo-file))
6513 ;; (defun todo-display-as-todo-file ()
6514 ;; "Show todo files correctly when visited from outside of Todo mode.
6515 ;; Added to `find-file-hook' in Todo mode and Todo Archive mode."
6516 ;; (and (member this-command todo-visit-files-commands)
6517 ;; (= (- (point-max) (point-min)) (buffer-size))
6518 ;; (member major-mode '(todo-mode todo-archive-mode))
6519 ;; (todo-category-select)))
6521 ;; (defun todo-add-to-buffer-list ()
6522 ;; "Add name of just visited todo file to `todo-file-buffers'.
6523 ;; This function is added to `find-file-hook' in Todo mode."
6524 ;; (let ((filename (file-truename (buffer-file-name))))
6525 ;; (when (member filename todo-files)
6526 ;; (add-to-list 'todo-file-buffers filename))))
6528 (defun todo-update-buffer-list ()
6529 "Make current Todo mode buffer file car of `todo-file-buffers'.
6530 This function is added to `post-command-hook' in Todo mode."
6531 (let ((filename (file-truename (buffer-file-name))))
6532 (unless (eq (car todo-file-buffers) filename)
6533 (setq todo-file-buffers
6534 (cons filename (delete filename todo-file-buffers))))))
6536 (defun todo-reset-global-current-todo-file ()
6537 "Update the value of `todo-global-current-todo-file'.
6538 This becomes the latest existing todo file or, if there is none,
6539 the value of `todo-default-todo-file'.
6540 This function is added to `kill-buffer-hook' in Todo mode."
6541 (let ((filename (file-truename (buffer-file-name))))
6542 (setq todo-file-buffers (delete filename todo-file-buffers))
6543 (setq todo-global-current-todo-file
6544 (or (car todo-file-buffers)
6545 (todo-absolute-file-name todo-default-todo-file)))))
6547 (defun todo-reset-and-enable-done-separator ()
6548 "Show resized done items separator overlay after window change.
6549 Added to `window-configuration-change-hook' in Todo mode."
6550 (when (= 1 (length todo-done-separator-string))
6551 (let ((sep todo-done-separator))
6552 (setq todo-done-separator (todo-done-separator))
6553 (save-match-data (todo-reset-done-separator sep)))))
6555 (defun todo-modes-set-1 ()
6556 "Make some settings that apply to multiple Todo modes."
6557 (setq-local font-lock-defaults '(todo-font-lock-keywords t))
6558 (setq-local revert-buffer-function 'todo-revert-buffer)
6559 (setq-local tab-width todo-indent-to-here)
6560 (setq-local indent-line-function 'todo-indent)
6561 (when todo-wrap-lines
6563 (setq wrap-prefix (make-string todo-indent-to-here 32))))
6565 (defun todo-modes-set-2 ()
6566 "Make some settings that apply to multiple Todo modes."
6567 (add-to-invisibility-spec 'todo)
6568 (setq buffer-read-only t)
6569 (when (and (boundp 'desktop-save-mode) desktop-save-mode)
6570 (setq-local desktop-save-buffer 'todo-desktop-save-buffer))
6571 (when (boundp 'hl-line-range-function)
6572 (setq-local hl-line-range-function
6573 (lambda() (save-excursion
6574 (when (todo-item-end)
6575 (cons (todo-item-start)
6576 (todo-item-end))))))))
6578 (defun todo-modes-set-3 ()
6579 "Make some settings that apply to multiple Todo modes."
6580 (setq-local todo-categories (todo-set-categories))
6581 (setq-local todo-category-number 1)
6582 ;; (add-hook 'find-file-hook 'todo-display-as-todo-file nil t)
6585 (put 'todo-mode 'mode-class 'special)
6588 (define-derived-mode todo-mode special-mode "Todo"
6589 "Major mode for displaying, navigating and editing todo lists.
6592 (if (called-interactively-p 'any)
6593 (message "Type `M-x todo-show' to enter Todo mode")
6597 ;; Initialize todo-current-todo-file.
6598 (when (member (file-truename (buffer-file-name))
6599 (funcall todo-files-function))
6600 (setq-local todo-current-todo-file (file-truename (buffer-file-name))))
6601 (setq-local todo-show-done-only nil)
6602 (setq-local todo-categories-with-marks nil)
6603 ;; (add-hook 'find-file-hook 'todo-add-to-buffer-list nil t)
6604 (add-hook 'post-command-hook 'todo-update-buffer-list nil t)
6605 (when todo-show-current-file
6606 (add-hook 'pre-command-hook 'todo-show-current-file nil t))
6607 (add-hook 'window-configuration-change-hook
6608 'todo-reset-and-enable-done-separator nil t)
6609 (add-hook 'kill-buffer-hook 'todo-reset-global-current-todo-file nil t)))
6611 (put 'todo-archive-mode 'mode-class 'special)
6613 ;; If todo-mode is parent, all todo-mode key bindings appear to be
6614 ;; available in todo-archive-mode (e.g. shown by C-h m).
6616 (define-derived-mode todo-archive-mode special-mode "Todo-Arch"
6617 "Major mode for archived todo categories.
6619 \\{todo-archive-mode-map}"
6623 (setq-local todo-current-todo-file (file-truename (buffer-file-name)))
6624 (setq-local todo-show-done-only t))
6626 (defun todo-mode-external-set ()
6627 "Set `todo-categories' externally to `todo-current-todo-file'."
6628 (setq-local todo-current-todo-file todo-global-current-todo-file)
6629 (let ((cats (with-current-buffer
6630 ;; Can't use find-buffer-visiting when
6631 ;; `todo-show-categories-table' is called on first
6632 ;; invocation of `todo-show', since there is then
6633 ;; no buffer visiting the current file.
6634 (find-file-noselect todo-current-todo-file 'nowarn)
6636 ;; In Todo Edit mode todo-categories is now nil
6637 ;; since it uses same buffer as Todo mode but
6638 ;; doesn't have the latter's local variables.
6640 (goto-char (point-min))
6641 (read (buffer-substring-no-properties
6642 (line-beginning-position)
6643 (line-end-position))))))))
6644 (setq-local todo-categories cats)))
6646 (define-derived-mode todo-edit-mode text-mode "Todo-Ed"
6647 "Major mode for editing multiline todo items.
6649 \\{todo-edit-mode-map}"
6651 (todo-mode-external-set)
6652 (setq buffer-read-only nil))
6654 (put 'todo-categories-mode 'mode-class 'special)
6656 (define-derived-mode todo-categories-mode special-mode "Todo-Cats"
6657 "Major mode for displaying and editing todo categories.
6659 \\{todo-categories-mode-map}"
6660 (todo-mode-external-set))
6662 (put 'todo-filtered-items-mode 'mode-class 'special)
6665 (define-derived-mode todo-filtered-items-mode special-mode "Todo-Fltr"
6666 "Mode for displaying and reprioritizing top priority Todo.
6668 \\{todo-filtered-items-mode-map}"
6672 ;; -----------------------------------------------------------------------------
6673 (provide 'todo-mode)
6675 ;;; todo-mode.el ends here