1 ;;; todos.el --- facilities for making and maintaining todo lists
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Stephen Berman <stephen.berman@gmx.net>
6 ;; Keywords: calendar, todo
8 ;; This file is [not yet] part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package provides facilities for making, displaying, navigating
26 ;; and editing todo lists, which are prioritized lists of todo items.
27 ;; Todo lists are identified with named categories, providing a means
28 ;; of grouping thematically related todo items. Each category is
29 ;; stored in a file, which provides a further level of organization.
31 ;; You can navigate among the items of a category, and between
32 ;; categories and files. You can edit items, reprioritize them within
33 ;; their category, move them to another category, delete them, or mark
34 ;; items as done and store them separately from the not yet done items
35 ;; in a category. You can add new files and categories, rename
36 ;; categories, move them to another file or delete them. You can also
37 ;; build cross-categorial lists of items that satisfy various
38 ;; criteria. And you can display summary tables of the categories in
39 ;; a file and the types of items they contain.
41 ;; To get started, load this package and type `M-x todos-show'. This
42 ;; will prompt you for the name of the first todo file and its first
43 ;; category, create these and display the empty category in Todos
44 ;; mode. Then type `i i' to add the first todo item to the category
45 ;; (i.e., to the list). To see a list of all Todos mode commands,
46 ;; which include entry points to several auxiliary modes, type `C-h
47 ;; m'. Consult the document strings of the commands for details of
48 ;; their use. The `todos' customization group and its subgroups list
49 ;; the options you can set to alter the behavior of many commands and
50 ;; various aspects of the display.
52 ;; This package is a new version of Oliver Seidel's todo-mode.el,
53 ;; which retains the same basic organization and handling of todo
54 ;; lists and the basic UI, but extends these in many ways and
55 ;; reimplements most of the internals.
60 ;; For cl-remove-duplicates (in todos-insertion-commands-args) and cl-oddp.
63 ;; =============================================================================
65 ;; =============================================================================
66 ;; -----------------------------------------------------------------------------
67 ;;; Options for file and category selection
68 ;; -----------------------------------------------------------------------------
70 (defcustom todos-directory
(locate-user-emacs-file "todos/")
71 "Directory where user's Todos files are saved."
75 (defun todos-files (&optional archives
)
76 "Default value of `todos-files-function'.
77 This returns the case-insensitive alphabetically sorted list of
78 file truenames in `todos-directory' with the extension
79 \".todo\". With non-nil ARCHIVES return the list of archive file
80 truenames (those with the extension \".toda\")."
81 (let ((files (if (file-exists-p todos-directory
)
82 (mapcar 'file-truename
83 (directory-files todos-directory t
84 (if archives
"\.toda$" "\.todo$") t
)))))
85 (sort files
(lambda (s1 s2
) (let ((cis1 (upcase s1
))
87 (string< cis1 cis2
))))))
89 (defcustom todos-files-function
'todos-files
90 "Function returning the value of the variable `todos-files'.
91 This function should take an optional argument that, if non-nil,
92 makes it return the value of the variable `todos-archives'."
96 (defun todos-short-file-name (file)
97 "Return short form of Todos FILE.
98 This lacks the extension and directory components."
100 (file-name-sans-extension (file-name-nondirectory file
))))
102 (defcustom todos-visit-files-commands
(list 'find-file
'dired-find-file
)
103 "List of file finding commands for `todos-display-as-todos-file'.
104 Invoking these commands to visit a Todos or Todos Archive file
105 calls `todos-show' or `todos-find-archive', so that the file is
106 displayed correctly."
107 :type
'(repeat function
)
110 (defcustom todos-default-todos-file
(todos-short-file-name
111 (car (funcall todos-files-function
)))
112 "Todos file visited by first session invocation of `todos-show'."
113 :type
`(radio ,@(mapcar (lambda (f) (list 'const f
))
114 (mapcar 'todos-short-file-name
115 (funcall todos-files-function
))))
118 (defcustom todos-show-current-file t
119 "Non-nil to make `todos-show' visit the current Todos file.
120 Otherwise, `todos-show' always visits `todos-default-todos-file'."
122 :initialize
'custom-initialize-default
123 :set
'todos-set-show-current-file
126 (defcustom todos-show-first
'first
127 "What action to take on first use of `todos-show' on a file."
128 :type
'(choice (const :tag
"Show first category" first
)
129 (const :tag
"Show table of categories" table
)
130 (const :tag
"Show top priorities" top
)
131 (const :tag
"Show diary items" diary
)
132 (const :tag
"Show regexp items" regexp
))
135 (defcustom todos-initial-file
"Todo"
136 "Default file name offered on adding first Todos file."
140 (defcustom todos-initial-category
"Todo"
141 "Default category name offered on initializing a new Todos file."
145 (defcustom todos-category-completions-files nil
146 "List of files for building `todos-read-category' completions."
147 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
148 (mapcar 'todos-short-file-name
149 (funcall todos-files-function
))))
152 (defcustom todos-completion-ignore-case nil
153 "Non-nil means case is ignored by `todos-read-*' functions."
157 ;; -----------------------------------------------------------------------------
158 ;;; Entering and exiting Todos mode
159 ;; -----------------------------------------------------------------------------
162 (defun todos-show (&optional solicit-file
)
163 "Visit a Todos file and display one of its categories.
165 When invoked in Todos mode, prompt for which todo file to visit.
166 When invoked outside of Todos mode with non-nil prefix argument
167 SOLICIT-FILE prompt for which todo file to visit; otherwise visit
168 `todos-default-todos-file'. Subsequent invocations from outside
169 of Todos mode revisit this file or, with option
170 `todos-show-current-file' non-nil (the default), whichever Todos
171 file was last visited.
173 Calling this command before any Todos file exists prompts for a
174 file name and an initial category (defaulting to
175 `todos-initial-file' and `todos-initial-category'), creates both
176 of these, visits the file and displays the category.
178 The first invocation of this command on an existing Todos file
179 interacts with the option `todos-show-first': if its value is
180 `first' (the default), show the first category in the file; if
181 its value is `table', show the table of categories in the file;
182 if its value is one of `top', `diary' or `regexp', show the
183 corresponding saved top priorities, diary items, or regexp items
184 file, if any. Subsequent invocations always show the file's
185 current (i.e., last displayed) category.
187 In Todos mode just the category's unfinished todo items are shown
188 by default. The done items are hidden, but typing
189 `\\[todos-toggle-view-done-items]' displays them below the todo
190 items. With non-nil user option `todos-show-with-done' both todo
191 and done items are always shown on visiting a category.
193 Invoking this command in Todos Archive mode visits the
194 corresponding Todos file, displaying the corresponding category."
198 (show-first todos-show-first
)
199 (file (cond ((or solicit-file
200 (and (called-interactively-p 'any
)
201 (memq major-mode
'(todos-mode
203 todos-filtered-items-mode
))))
204 (if (funcall todos-files-function
)
205 (todos-read-file-name "Choose a Todos file to visit: "
207 (user-error "There are no Todos files")))
208 ((and (eq major-mode
'todos-archive-mode
)
209 ;; Called noninteractively via todos-quit
210 ;; to jump to corresponding category in
212 (not (called-interactively-p 'any
)))
213 (setq cat
(todos-current-category))
214 (concat (file-name-sans-extension
215 todos-current-todos-file
) ".todo"))
217 (or todos-current-todos-file
218 (and todos-show-current-file
219 todos-global-current-todos-file
)
220 (todos-absolute-file-name todos-default-todos-file
)
221 (todos-add-file))))))
222 (unless todos-default-todos-file
223 ;; We just initialized the first todo file, so make it the default.
224 (setq todos-default-todos-file
(todos-short-file-name file
))
225 (todos-reevaluate-default-file-defcustom))
226 (unless (member file todos-visited
)
227 ;; Can't setq t-c-t-f here, otherwise wrong file shown when
228 ;; todos-show is called from todos-show-categories-table.
229 (let ((todos-current-todos-file file
))
230 (cond ((eq todos-show-first
'table
)
231 (todos-show-categories-table))
232 ((memq todos-show-first
'(top diary regexp
))
233 (let* ((shortf (todos-short-file-name file
))
234 (fi-file (todos-absolute-file-name
235 shortf todos-show-first
)))
236 (when (eq todos-show-first
'regexp
)
237 (let ((rxfiles (directory-files todos-directory t
239 (when (and rxfiles
(> (length rxfiles
) 1))
240 (let ((rxf (mapcar 'todos-short-file-name rxfiles
)))
241 (setq fi-file
(todos-absolute-file-name
243 "Choose a regexp items file: "
245 (if (file-exists-p fi-file
)
248 (set-buffer (find-file-noselect fi-file
'nowarn
)))
249 (message "There is no %s file for %s"
250 (cond ((eq todos-show-first
'top
)
252 ((eq todos-show-first
'diary
)
254 ((eq todos-show-first
'regexp
)
257 (setq todos-show-first
'first
)))))))
258 (when (or (member file todos-visited
)
259 (eq todos-show-first
'first
))
260 (set-window-buffer (selected-window)
261 (set-buffer (find-file-noselect file
'nowarn
)))
262 ;; When quitting archive file, show corresponding category in
263 ;; Todos file, if it exists.
264 (when (assoc cat todos-categories
)
265 (setq todos-category-number
(todos-category-number cat
)))
266 ;; If this is a new Todos file, add its first category.
267 (when (zerop (buffer-size))
268 (setq todos-category-number
(todos-add-category todos-current-todos-file
270 add-item todos-add-item-if-new-category
))
271 (save-excursion (todos-category-select))
272 (when add-item
(todos-basic-insert-item)))
273 (setq todos-show-first show-first
)
274 (add-to-list 'todos-visited file
)))
277 "Save the current Todos file."
279 (cond ((eq major-mode
'todos-filtered-items-mode
)
280 (todos-check-filtered-items-file)
281 (todos-save-filtered-items-buffer))
286 "Exit the current Todos-related buffer.
287 Depending on the specific mode, this either kills the buffer or
288 buries it and restores state as needed."
290 (let ((buf (current-buffer)))
291 (cond ((eq major-mode
'todos-categories-mode
)
292 ;; Postpone killing buffer till after calling todos-show, to
293 ;; prevent killing todos-mode buffer.
294 (setq todos-descending-counts nil
)
295 ;; Ensure todos-show calls todos-show-categories-table only on
296 ;; first invocation per file.
297 (when (eq todos-show-first
'table
)
298 (add-to-list 'todos-visited todos-current-todos-file
))
301 ((eq major-mode
'todos-filtered-items-mode
)
303 (unless (eq major-mode
'todos-mode
) (todos-show)))
304 ((eq major-mode
'todos-archive-mode
)
305 (todos-save) ; Have to write previously nonexistant archives to file.
308 ((eq major-mode
'todos-mode
)
310 ;; If we just quit archive mode, just burying the buffer
311 ;; in todos-mode would return to archive.
312 (set-window-buffer (selected-window)
313 (set-buffer (other-buffer)))
314 (bury-buffer buf
)))))
316 ;; -----------------------------------------------------------------------------
317 ;;; Navigation commands
318 ;; -----------------------------------------------------------------------------
320 (defun todos-forward-category (&optional back
)
321 "Visit the numerically next category in this Todos file.
322 If the current category is the highest numbered, visit the first
323 category. With non-nil argument BACK, visit the numerically
324 previous category (the highest numbered one, if the current
325 category is the first)."
327 (setq todos-category-number
328 (1+ (mod (- todos-category-number
(if back
2 0))
329 (length todos-categories
))))
330 (when todos-skip-archived-categories
331 (while (and (zerop (todos-get-count 'todo
))
332 (zerop (todos-get-count 'done
))
333 (not (zerop (todos-get-count 'archived
))))
334 (setq todos-category-number
335 (apply (if back
'1-
'1+) (list todos-category-number
)))))
336 (todos-category-select)
337 (goto-char (point-min)))
339 (defun todos-backward-category ()
340 "Visit the numerically previous category in this Todos file.
341 If the current category is the highest numbered, visit the first
344 (todos-forward-category t
))
347 (defun todos-jump-to-category (&optional file where
)
348 "Prompt for a category in a Todos file and jump to it.
350 With non-nil FILE (interactively a prefix argument), prompt for a
351 specific Todos file and choose (with TAB completion) a category
352 in it to jump to; otherwise, choose and jump to any category in
353 either the current Todos file or a file in
354 `todos-category-completions-files'.
356 You can also enter a non-existing category name, triggering a
357 prompt whether to add a new category by that name; on
358 confirmation it is added and you jump to that category.
360 In noninteractive calls non-nil WHERE specifies either the goal
361 category or its file. If its value is `archive', the choice of
362 categories is restricted to the current archive file or the
363 archive you were prompted to choose; this is used by
364 `todos-jump-to-archive-category'. If its value is the name of a
365 category, jump directly to that category; this is used in Todos
368 ;; If invoked outside of Todos mode and there is not yet any Todos
369 ;; file, initialize one.
370 (if (null todos-files
)
372 (let* ((archive (eq where
'archive
))
373 (cat (unless archive where
))
374 (file0 (when cat
; We're in Todos Categories mode.
375 ;; With non-nil `todos-skip-archived-categories'
376 ;; jump to archive file of a category with only
378 (if (and todos-skip-archived-categories
379 (zerop (todos-get-count 'todo cat
))
380 (zerop (todos-get-count 'done cat
))
381 (not (zerop (todos-get-count 'archived cat
))))
382 (concat (file-name-sans-extension
383 todos-current-todos-file
) ".toda")
384 ;; Otherwise, jump to current todos file.
385 todos-current-todos-file
)))
386 (len (length todos-categories
))
387 (cat+file
(unless cat
388 (todos-read-category "Jump to category: "
389 (if archive
'archive
) file
)))
390 (add-item (and todos-add-item-if-new-category
391 (> (length todos-categories
) len
))))
392 (setq category
(or cat
(car cat
+file
)))
393 (unless cat
(setq file0
(cdr cat
+file
)))
394 (with-current-buffer (find-file-noselect file0
'nowarn
)
395 (setq todos-current-todos-file file0
)
396 ;; If called from Todos Categories mode, clean up before jumping.
397 (if (string= (buffer-name) todos-categories-buffer
)
399 (set-window-buffer (selected-window)
400 (set-buffer (find-buffer-visiting file0
)))
401 (unless todos-global-current-todos-file
402 (setq todos-global-current-todos-file todos-current-todos-file
))
403 (todos-category-number category
)
404 (todos-category-select)
405 (goto-char (point-min))
406 (when add-item
(todos-basic-insert-item))))))
408 (defun todos-next-item (&optional count
)
409 "Move point down to the beginning of the next item.
410 With positive numerical prefix COUNT, move point COUNT items
413 If the category's done items are hidden, this command also moves
414 point to the empty line below the last todo item from any higher
415 item in the category, i.e., when invoked with or without a prefix
416 argument. If the category's done items are visible, this command
417 called with a prefix argument only moves point to a lower item,
418 e.g., with point on the last todo item and called with prefix 1,
419 it moves point to the first done item; but if called with point
420 on the last todo item without a prefix argument, it moves point
421 the the empty line above the done items separator."
423 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
424 ;; the corresponding command.
425 (cond ((and current-prefix-arg
(< count
1))
426 (user-error "The prefix argument must be a positive number"))
428 (todos-forward-item count
))
430 (todos-forward-item))))
432 (defun todos-previous-item (&optional count
)
433 "Move point up to start of item with next higher priority.
434 With positive numerical prefix COUNT, move point COUNT items
437 If the category's done items are visible, this command called
438 with a prefix argument only moves point to a higher item, e.g.,
439 with point on the first done item and called with prefix 1, it
440 moves to the last todo item; but if called with point on the
441 first done item without a prefix argument, it moves point the the
442 empty line above the done items separator."
444 ;; Avoid moving to bob if on the first item but not at bob.
445 (when (> (line-number-at-pos) 1)
446 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
447 ;; the corresponding command.
448 (cond ((and current-prefix-arg
(< count
1))
449 (user-error "The prefix argument must be a positive number"))
451 (todos-backward-item count
))
453 (todos-backward-item)))))
455 ;; -----------------------------------------------------------------------------
456 ;;; File editing commands
457 ;; -----------------------------------------------------------------------------
459 (defun todos-add-file ()
460 "Name and initialize a new Todos file.
461 Interactively, prompt for a category and display it.
462 Noninteractively, return the name of the new file.
464 This command does not save the file to disk; to do that type
465 \\[todos-save] or \\[todos-quit]."
467 (let ((prompt (concat "Enter name of new Todos file "
468 "(TAB or SPC to see current names): "))
470 (setq file
(todos-read-file-name prompt
))
471 (with-current-buffer (get-buffer-create file
)
473 (write-region (point-min) (point-max) file nil
'nomessage nil t
)
475 (setq todos-files
(funcall todos-files-function
))
476 (todos-reevaluate-filelist-defcustoms)
477 (if (called-interactively-p)
479 (set-window-buffer (selected-window)
480 (set-buffer (find-file-noselect file
)))
481 (setq todos-current-todos-file file
)
485 (defvar todos-edit-buffer
"*Todos Edit*"
486 "Name of current buffer in Todos Edit mode.")
488 (defun todos-edit-file ()
489 "Put current buffer in `todos-edit-mode'.
490 This makes the entire file visible and the buffer writeable and
491 you can use the self-insertion keys and standard Emacs editing
492 commands to make changes. To return to Todos mode, type
493 \\[todos-edit-quit]. This runs a file format check, signalling
494 an error if the format has become invalid. However, this check
495 cannot tell if the number of items changed, which could result in
496 the file containing inconsistent information. For this reason
497 this command should be used with caution."
502 (message "%s" (substitute-command-keys
503 (concat "Type \\[todos-edit-quit] to check file format "
504 "validity and return to Todos mode.\n"))))
506 ;; -----------------------------------------------------------------------------
507 ;;; Category editing commands
508 ;; -----------------------------------------------------------------------------
510 (defun todos-add-category (&optional file cat
)
511 "Add a new category to a Todos file.
513 Called interactively with prefix argument FILE, prompt for a file
514 and then for a new category to add to that file, otherwise prompt
515 just for a category to add to the current Todos file. After adding
516 the category, visit it in Todos mode.
518 Non-interactively, add category CAT to file FILE; if FILE is nil,
519 add CAT to the current Todos file. After adding the category,
520 return the new category number."
523 ;; If cat is passed from caller, don't prompt, unless it is "",
524 ;; which means the file was just added and has no category yet.
525 (if (and cat
(> (length cat
) 0))
526 (setq file0
(or (and (stringp file
) file
)
527 todos-current-todos-file
))
528 (setq catfil
(todos-read-category "Enter a new category name: "
529 'add
(when (called-interactively-p 'any
)
532 file0
(if (called-interactively-p 'any
)
536 (let ((counts (make-vector 4 0)) ; [todo diary done archived]
537 (num (1+ (length todos-categories
)))
538 (buffer-read-only nil
))
539 (setq todos-current-todos-file file0
)
540 (setq todos-categories
(append todos-categories
541 (list (cons cat counts
))))
543 (goto-char (point-max))
544 (save-excursion ; Save point for todos-category-select.
545 (insert todos-category-beg cat
"\n\n" todos-category-done
"\n"))
546 (todos-update-categories-sexp)
547 ;; If invoked by user, display the newly added category, if
548 ;; called programmatically return the category number to the
550 (if (called-interactively-p 'any
)
552 (setq todos-category-number num
)
553 (todos-category-select)
554 (when todos-add-item-if-new-category
555 (todos-basic-insert-item)))
558 (defun todos-rename-category ()
559 "Rename current Todos category.
560 If this file has an archive containing this category, rename the
561 category there as well."
563 (let* ((cat (todos-current-category))
564 (new (read-from-minibuffer
565 (format "Rename category \"%s\" to: " cat
))))
566 (setq new
(todos-validate-name new
'category
))
567 (let* ((ofile todos-current-todos-file
)
568 (archive (concat (file-name-sans-extension ofile
) ".toda"))
569 (buffers (append (list ofile
)
570 (unless (zerop (todos-get-count 'archived cat
))
572 (dolist (buf buffers
)
573 (with-current-buffer (find-file-noselect buf
)
574 (let (buffer-read-only)
575 (setq todos-categories
(todos-set-categories))
578 (setcar (assoc cat todos-categories
) new
)
580 (goto-char (point-min))
581 (todos-update-categories-sexp)
582 (re-search-forward (concat (regexp-quote todos-category-beg
)
583 "\\(" (regexp-quote cat
) "\\)\n")
585 (replace-match new t t nil
1)))))))
586 (force-mode-line-update))
587 (save-excursion (todos-category-select)))
589 (defun todos-delete-category (&optional arg
)
590 "Delete current Todos category provided it is empty.
591 With ARG non-nil delete the category unconditionally,
592 i.e. including all existing todo and done items."
594 (let* ((file todos-current-todos-file
)
595 (cat (todos-current-category))
596 (todo (todos-get-count 'todo cat
))
597 (done (todos-get-count 'done cat
))
598 (archived (todos-get-count 'archived cat
)))
600 (or (> todo
0) (> done
0)))
601 (message "%s" (substitute-command-keys
602 (concat "To delete a non-empty category, "
603 "type C-u \\[todos-delete-category].")))
604 (when (cond ((= (length todos-categories
) 1)
606 (concat "This is the only category in this file; "
607 "deleting it will also delete the file.\n"
608 "Do you want to proceed? ")))
610 (todos-y-or-n-p (concat "This category has archived items; "
611 "the archived category will remain\n"
612 "after deleting the todo category. "
613 "Do you still want to delete it\n"
614 "(see `todos-skip-archived-categories' "
615 "for another option)? ")))
617 (todos-y-or-n-p (concat "Permanently remove category \"" cat
618 "\"" (and arg
" and all its entries")
621 (let ((buffer-read-only)
622 (beg (re-search-backward
623 (concat "^" (regexp-quote (concat todos-category-beg cat
))
625 (end (if (re-search-forward
626 (concat "\n\\(" (regexp-quote todos-category-beg
)
630 (remove-overlays beg end
)
631 (delete-region beg end
)
632 (if (= (length todos-categories
) 1)
633 ;; If deleted category was the only one, delete the file.
635 (todos-reevaluate-filelist-defcustoms)
636 ;; Skip confirming killing the archive buffer if it has been
637 ;; modified and not saved.
638 (set-buffer-modified-p nil
)
641 (message "Deleted Todos file %s." file
))
642 (setq todos-categories
(delete (assoc cat todos-categories
)
644 (todos-update-categories-sexp)
645 (setq todos-category-number
646 (1+ (mod todos-category-number
(length todos-categories
))))
647 (todos-category-select)
648 (goto-char (point-min))
649 (message "Deleted category %s." cat
)))))))
651 (defun todos-move-category ()
652 "Move current category to a different Todos file.
653 If current category has archived items, also move those to the
654 archive of the file moved to, creating it if it does not exist."
656 (when (or (> (length todos-categories
) 1)
657 (todos-y-or-n-p (concat "This is the only category in this file; "
658 "moving it will also delete the file.\n"
659 "Do you want to proceed? ")))
660 (let* ((ofile todos-current-todos-file
)
661 (cat (todos-current-category))
662 (nfile (todos-read-file-name
663 "Choose a Todos file to move this category to: " nil t
))
664 (archive (concat (file-name-sans-extension ofile
) ".toda"))
665 (buffers (append (list ofile
)
666 (unless (zerop (todos-get-count 'archived cat
))
669 (while (equal (file-truename nfile
) (file-truename ofile
))
670 (setq nfile
(todos-read-file-name
671 "Choose a file distinct from this file: " nil t
)))
672 (dolist (buf buffers
)
673 (with-current-buffer (find-file-noselect buf
)
675 (goto-char (point-max))
676 (let* ((beg (re-search-backward
678 (regexp-quote (concat todos-category-beg cat
))
681 (end (if (re-search-forward
682 (concat "^" (regexp-quote todos-category-beg
))
686 (content (buffer-substring-no-properties beg end
))
687 (counts (cdr (assoc cat todos-categories
)))
689 ;; Move the category to the new file. Also update or create
690 ;; archive file if necessary.
693 ;; Regenerate todos-archives in case there
694 ;; is a newly created archive.
695 (if (member buf
(funcall todos-files-function t
))
696 (concat (file-name-sans-extension nfile
) ".toda")
698 (let* ((nfile-short (todos-short-file-name nfile
))
700 (format "Todos file \"%s\" already has "
702 (format "the category \"%s\";\n" cat
)
703 "enter a new category name: "))
706 (goto-char (point-max))
708 ;; If the file moved to has a category with the same
709 ;; name, rename the moved category.
710 (when (assoc cat todos-categories
)
711 (unless (member (file-truename (buffer-file-name))
712 (funcall todos-files-function t
))
713 (setq new
(read-from-minibuffer prompt
))
714 (setq new
(todos-validate-name new
'category
))))
715 ;; Replace old with new name in Todos and archive files.
717 (goto-char (point-max))
719 (concat "^" (regexp-quote todos-category-beg
)
720 "\\(" (regexp-quote cat
) "\\)$") nil t
)
721 (replace-match new nil nil nil
1)))
722 (setq todos-categories
723 (append todos-categories
(list (cons new counts
))))
724 (todos-update-categories-sexp)
725 ;; If archive was just created, save it to avoid "File
726 ;; <xyz> no longer exists!" message on invoking
727 ;; `todos-view-archived-items'.
728 (unless (file-exists-p (buffer-file-name))
730 (todos-category-number (or new cat
))
731 (todos-category-select))
732 ;; Delete the category from the old file, and if that was the
733 ;; last category, delete the file. Also handle archive file
735 (remove-overlays beg end
)
736 (delete-region beg end
)
737 (goto-char (point-min))
738 ;; Put point after todos-categories sexp.
740 (if (eobp) ; Aside from sexp, file is empty.
742 ;; Skip confirming killing the archive buffer.
743 (set-buffer-modified-p nil
)
744 (delete-file todos-current-todos-file
)
746 (when (member todos-current-todos-file todos-files
)
747 (todos-reevaluate-filelist-defcustoms)))
748 (setq todos-categories
(delete (assoc cat todos-categories
)
750 (todos-update-categories-sexp)
751 (todos-category-select)))))
752 (set-window-buffer (selected-window)
753 (set-buffer (find-file-noselect nfile
)))
754 (todos-category-number (or new cat
))
755 (todos-category-select))))
757 (defun todos-merge-category (&optional file
)
758 "Merge current category into another existing category.
760 With prefix argument FILE, prompt for a specific Todos file and
761 choose (with TAB completion) a category in it to merge into;
762 otherwise, choose and merge into a category in either the
763 current Todos file or a file in `todos-category-completions-files'.
765 After merging, the current category's todo and done items are
766 appended to the chosen goal category's todo and done items,
767 respectively. The goal category becomes the current category,
768 and the previous current category is deleted.
770 If both the first and goal categories also have archived items,
771 the former are merged to the latter. If only the first category
772 has archived items, the archived category is renamed to the goal
775 (let* ((tfile todos-current-todos-file
)
776 (archive (concat (file-name-sans-extension (if file gfile tfile
))
778 (cat (todos-current-category))
779 (cat+file
(todos-read-category "Merge into category: " 'todo file
))
780 (goal (car cat
+file
))
781 (gfile (cdr cat
+file
))
783 ;; Merge in todo file.
784 (with-current-buffer (get-buffer (find-file-noselect tfile
))
786 (let* ((buffer-read-only nil
)
789 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
791 (tbeg (progn (forward-line) (point-marker)))
794 (concat "^" (regexp-quote todos-category-done
)) nil t
)
795 (forward-line) (point-marker)))
796 ;; Omit empty line between todo and done items.
797 (tend (progn (forward-line -
2) (point-marker)))
799 (if (re-search-forward
800 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
802 (goto-char (match-beginning 0))
804 (point-max-marker))))
805 (todo (buffer-substring-no-properties tbeg tend
))
806 (done (buffer-substring-no-properties dbeg cend
)))
807 (goto-char (point-min))
808 ;; Merge any todo items.
809 (unless (zerop (length todo
))
811 (concat "^" (regexp-quote (concat todos-category-beg goal
)) "$")
814 (concat "^" (regexp-quote todos-category-done
)) nil t
)
816 (setq here
(point-marker))
818 (todos-update-count 'todo
(todos-get-count 'todo cat
) goal
))
819 ;; Merge any done items.
820 (unless (zerop (length done
))
821 (goto-char (if (re-search-forward
822 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
825 (when (zerop (length todo
)) (setq here
(point-marker)))
827 (todos-update-count 'done
(todos-get-count 'done cat
) goal
))
828 (remove-overlays cbeg cend
)
829 (delete-region cbeg cend
)
830 (setq todos-categories
(delete (assoc cat todos-categories
)
832 (todos-update-categories-sexp)
833 (mapc (lambda (m) (set-marker m nil
)) (list cbeg tbeg dbeg tend cend
))))
834 (when (file-exists-p archive
)
835 ;; Merge in archive file.
836 (with-current-buffer (get-buffer (find-file-noselect archive
))
838 (goto-char (point-min))
839 (let ((buffer-read-only nil
)
840 (cbeg (save-excursion
841 (when (re-search-forward
842 (concat "^" (regexp-quote
843 (concat todos-category-beg cat
)) "$")
845 (goto-char (match-beginning 0))
847 (gbeg (save-excursion
848 (when (re-search-forward
849 (concat "^" (regexp-quote
850 (concat todos-category-beg goal
)) "$")
852 (goto-char (match-beginning 0))
856 (setq archived-count
(todos-get-count 'done cat
))
857 (setq cend
(save-excursion
858 (if (re-search-forward
859 (concat "^" (regexp-quote todos-category-beg
))
863 (setq carch
(save-excursion (goto-char cbeg
) (forward-line)
864 (buffer-substring-no-properties (point) cend
)))
865 ;; If both categories of the merge have archived items, merge the
866 ;; source items to the goal items, else "merge" by renaming the
867 ;; source category to goal.
870 (goto-char (if (re-search-forward
871 (concat "^" (regexp-quote todos-category-beg
))
876 (remove-overlays cbeg cend
)
877 (delete-region cbeg cend
))
880 (replace-match goal
))
881 (setq todos-categories
(todos-make-categories-list t
))
882 (todos-update-categories-sexp)))))
883 (with-current-buffer (get-file-buffer tfile
)
885 (unless (zerop archived-count
)
886 (todos-update-count 'archived archived-count goal
)
887 (todos-update-categories-sexp)))
888 (todos-category-number goal
)
889 ;; If there are only merged done items, show them.
890 (let ((todos-show-with-done (zerop (todos-get-count 'todo goal
))))
891 (todos-category-select)
892 ;; Put point on the first merged item.
894 (set-marker here nil
)))
896 ;; -----------------------------------------------------------------------------
898 ;; -----------------------------------------------------------------------------
900 (defcustom todos-item-mark
"*"
901 "String used to mark items.
902 To ensure item marking works, change the value of this option
903 only when no items are marked."
904 :type
'(string :validate
906 (when (string= (widget-value widget
) todos-prefix
)
909 "Invalid value: must be distinct from `todos-prefix'")
911 :set
(lambda (symbol value
)
912 (custom-set-default symbol
(propertize value
'face
'todos-mark
)))
915 (defun todos-toggle-mark-item (&optional n
)
916 "Mark item with `todos-item-mark' if unmarked, otherwise unmark it.
917 With a positive numerical prefix argument N, change the
918 marking of the next N items."
920 (when (todos-item-string)
921 (unless (> n
1) (setq n
1))
923 (let* ((cat (todos-current-category))
924 (marks (assoc cat todos-categories-with-marks
))
926 (unless (looking-at todos-item-start
)
928 (todos-get-overlay 'prefix
)))
929 (pref (overlay-get ov
'before-string
)))
930 (if (todos-marked-item-p)
932 (overlay-put ov
'before-string
(substring pref
1))
933 (if (= (cdr marks
) 1) ; Deleted last mark in this category.
934 (setq todos-categories-with-marks
935 (assq-delete-all cat todos-categories-with-marks
))
936 (setcdr marks
(1- (cdr marks
)))))
937 (overlay-put ov
'before-string
(concat todos-item-mark pref
))
939 (setcdr marks
(1+ (cdr marks
)))
940 (push (cons cat
1) todos-categories-with-marks
))))
941 (todos-forward-item))))
943 (defun todos-mark-category ()
944 "Mark all visiblw items in this category with `todos-item-mark'."
946 (let* ((cat (todos-current-category))
947 (marks (assoc cat todos-categories-with-marks
)))
949 (goto-char (point-min))
951 (let* ((ov (todos-get-overlay 'prefix
))
952 (pref (overlay-get ov
'before-string
)))
953 (unless (todos-marked-item-p)
954 (overlay-put ov
'before-string
(concat todos-item-mark pref
))
956 (setcdr marks
(1+ (cdr marks
)))
957 (push (cons cat
1) todos-categories-with-marks
))))
958 (todos-forward-item)))))
960 (defun todos-unmark-category ()
961 "Remove `todos-item-mark' from all visible items in this category."
963 (let* ((cat (todos-current-category))
964 (marks (assoc cat todos-categories-with-marks
)))
966 (goto-char (point-min))
968 (let* ((ov (todos-get-overlay 'prefix
))
969 ;; No overlay on empty line between todo and done items.
970 (pref (when ov
(overlay-get ov
'before-string
))))
971 (when (todos-marked-item-p)
972 (overlay-put ov
'before-string
(substring pref
1)))
973 (todos-forward-item))))
974 (setq todos-categories-with-marks
975 (delq marks todos-categories-with-marks
))))
977 ;; -----------------------------------------------------------------------------
978 ;;; Item editing options
979 ;; -----------------------------------------------------------------------------
981 (defcustom todos-add-item-if-new-category nil
982 "Non-nil to prompt for an item after adding a new category."
986 (defcustom todos-include-in-diary nil
987 "Non-nil to allow new Todo items to be included in the diary."
991 (defcustom todos-diary-nonmarking nil
992 "Non-nil to insert new Todo diary items as nonmarking by default.
993 This appends `diary-nonmarking-symbol' to the front of an item on
994 insertion provided it doesn't begin with `todos-nondiary-marker'."
998 (defcustom todos-nondiary-marker
'("[" "]")
999 "List of strings surrounding item date to block diary inclusion.
1000 The first string is inserted before the item date and must be a
1001 non-empty string that does not match a diary date in order to
1002 have its intended effect. The second string is inserted after
1004 :type
'(list string string
)
1006 :initialize
'custom-initialize-default
1007 :set
'todos-reset-nondiary-marker
)
1009 (defcustom todos-always-add-time-string nil
1010 "Non-nil adds current time to a new item's date header by default.
1011 When the Todos insertion commands have a non-nil \"maybe-notime\"
1012 argument, this reverses the effect of
1013 `todos-always-add-time-string': if t, these commands omit the
1014 current time, if nil, they include it."
1018 (defcustom todos-use-only-highlighted-region t
1019 "Non-nil to enable inserting only highlighted region as new item."
1023 (defcustom todos-undo-item-omit-comment
'ask
1024 "Whether to omit done item comment on undoing the item.
1025 Nil means never omit the comment, t means always omit it, `ask'
1026 means prompt user and omit comment only on confirmation."
1027 :type
'(choice (const :tag
"Never" nil
)
1028 (const :tag
"Always" t
)
1029 (const :tag
"Ask" ask
))
1032 ;; -----------------------------------------------------------------------------
1033 ;;; Item editing commands
1034 ;; -----------------------------------------------------------------------------
1037 (defun todos-basic-insert-item (&optional arg diary nonmarking date-type time
1039 "Insert a new Todo item into a category.
1040 This is the function from which the generated Todos item
1041 insertion commands derive.
1043 The generated commands have mnenomic key bindings based on the
1044 arguments' values and their order in the command's argument list,
1045 as follows: (1) for DIARY `d', (2) for NONMARKING `k', (3) for
1046 DATE-TYPE either `c' for calendar or `d' for date or `n' for
1047 weekday name, (4) for TIME `t', (5) for REGION-OR-HERE either `r'
1048 for region or `h' for here. Sequences of these keys are appended
1049 to the insertion prefix key `i'. Keys that allow a following
1050 key (i.e., any but `r' or `h') must be doubled when used finally.
1051 For example, the command bound to the key sequence `i y h' will
1052 insert a new item with today's date, marked according to the
1053 DIARY argument described below, and with priority according to
1054 the HERE argument; `i y y' does the same except that the priority
1055 is not given by HERE but by prompting.
1057 In command invocations, ARG is passed as a prefix argument as
1058 follows. With no prefix argument, add the item to the current
1059 category; with one prefix argument (`C-u'), prompt for a category
1060 from the current Todos file; with two prefix arguments (`C-u C-u'),
1061 first prompt for a Todos file, then a category in that file. If
1062 a non-existing category is entered, ask whether to add it to the
1063 Todos file; if answered affirmatively, add the category and
1064 insert the item there.
1066 The remaining arguments are set or left nil by the generated item
1067 insertion commands; their meanings are described in the follows
1070 When argument DIARY is non-nil, this overrides the intent of the
1071 user option `todos-include-in-diary' for this item: if
1072 `todos-include-in-diary' is nil, include the item in the Fancy
1073 Diary display, and if it is non-nil, exclude the item from the
1074 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
1075 has its intended effect.
1077 When the item is included in the Fancy Diary display and the
1078 argument NONMARKING is non-nil, this overrides the intent of the
1079 user option `todos-diary-nonmarking' for this item: if
1080 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
1081 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
1083 The argument DATE-TYPE determines the content of the item's
1084 mandatory date header string and how it is added:
1085 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
1086 when the user puts the cursor on a date and hits RET, that
1087 date, in the format set by `calendar-date-display-form',
1088 becomes the date in the header.
1089 - If DATE-TYPE is a string matching the regexp
1090 `todos-date-pattern', that string becomes the date in the
1091 header. This case is for the command
1092 `todos-insert-item-from-calendar' which is called from the
1094 - If DATE-TYPE is the symbol `date', the header contains the date
1095 in the format set by `calendar-date-display-form', with year,
1096 month and day individually prompted for (month with tab
1098 - If DATE-TYPE is the symbol `dayname' the header contains a
1099 weekday name instead of a date, prompted for with tab
1101 - If DATE-TYPE has any other value (including nil or none) the
1102 header contains the current date (in the format set by
1103 `calendar-date-display-form').
1105 With non-nil argument TIME prompt for a time string, which must
1106 match `diary-time-regexp'. Typing `<return>' at the prompt
1107 returns the current time, if the user option
1108 `todos-always-add-time-string' is non-nil, otherwise the empty
1109 string (i.e., no time string). If TIME is absent or nil, add or
1110 omit the current time string according as
1111 `todos-always-add-time-string' is non-nil or nil, respectively.
1113 The argument REGION-OR-HERE determines the source and location of
1115 - If the REGION-OR-HERE is the symbol `here', prompt for the text of
1116 the new item and, if the command was invoked with point in the todo
1117 items section of the current category, give the new item the
1118 priority of the item at point, lowering the latter's priority and
1119 the priority of the remaining items. If point is in the done items
1120 section of the category, insert the new item as the first todo item
1121 in the category. Likewise, if the command with `here' is invoked
1122 outside of the current category, jump to the chosen category and
1123 insert the new item as the first item in the category.
1124 - If REGION-OR-HERE is the symbol `region', use the region of the
1125 current buffer as the text of the new item, depending on the
1126 value of user option `todos-use-only-highlighted-region': if
1127 this is non-nil, then use the region only when it is
1128 highlighted; otherwise, use the region regardless of
1129 highlighting. An error is signalled if there is no region in
1130 the current buffer. Prompt for the item's priority in the
1131 category (an integer between 1 and one more than the number of
1132 items in the category), and insert the item accordingly.
1133 - If REGION-OR-HERE has any other value (in particular, nil or
1134 none), prompt for the text and the item's priority, and insert
1135 the item accordingly."
1136 ;; If invoked outside of Todos mode and there is not yet any Todos
1137 ;; file, initialize one.
1138 (if (null todos-files
)
1140 (let ((region (eq region-or-here
'region
))
1141 (here (eq region-or-here
'here
)))
1143 (let (use-empty-active-region)
1144 (unless (and todos-use-only-highlighted-region
(use-region-p))
1145 (user-error "There is no active region"))))
1146 (let* ((obuf (current-buffer))
1147 (ocat (todos-current-category))
1149 (todos-mm (eq major-mode
'todos-mode
))
1150 (cat+file
(cond ((equal arg
'(4))
1151 (todos-read-category "Insert in category: "))
1153 (todos-read-category "Insert in category: "
1156 (cons (todos-current-category)
1157 (or todos-current-todos-file
1158 (and todos-show-current-file
1159 todos-global-current-todos-file
)
1160 (todos-absolute-file-name
1161 todos-default-todos-file
))))))
1162 (cat (car cat
+file
))
1163 (file (cdr cat
+file
))
1164 (new-item (if region
1165 (buffer-substring-no-properties
1166 (region-beginning) (region-end))
1167 (read-from-minibuffer "Todo item: ")))
1169 ((eq date-type
'date
)
1171 ((eq date-type
'dayname
)
1172 (todos-read-dayname))
1173 ((eq date-type
'calendar
)
1174 (setq todos-date-from-calendar t
)
1175 (or (todos-set-date-from-calendar)
1176 ;; If user exits Calendar before choosing
1177 ;; a date, cancel item insertion.
1179 ((and (stringp date-type
)
1180 (string-match todos-date-pattern date-type
))
1181 (setq todos-date-from-calendar date-type
)
1182 (todos-set-date-from-calendar))
1184 (calendar-date-string
1185 (calendar-current-date) t t
))))
1186 (time-string (or (and time
(todos-read-time))
1187 (and todos-always-add-time-string
1188 (substring (current-time-string) 11 16)))))
1189 (setq todos-date-from-calendar nil
)
1190 (find-file-noselect file
'nowarn
)
1191 (set-window-buffer (selected-window)
1192 (set-buffer (find-buffer-visiting file
)))
1193 ;; If this command was invoked outside of a Todos buffer, the
1194 ;; call to todos-current-category above returned nil. If we
1195 ;; just entered Todos mode now, then cat was set to the file's
1196 ;; first category, but if todos-mode was already enabled, cat
1197 ;; did not get set, so we have to set it explicitly.
1199 (setq cat
(todos-current-category)))
1200 (setq todos-current-todos-file file
)
1201 (unless todos-global-current-todos-file
1202 (setq todos-global-current-todos-file todos-current-todos-file
))
1203 (let ((buffer-read-only nil
)
1204 (called-from-outside (not (and todos-mm
(equal cat ocat
))))
1205 done-only item-added
)
1207 ;; Add date, time and diary marking as required.
1208 (concat (if (not (and diary
(not todos-include-in-diary
)))
1209 todos-nondiary-start
1210 (when (and nonmarking
(not todos-diary-nonmarking
))
1211 diary-nonmarking-symbol
))
1212 date-string
(when (and time-string
; Can be empty.
1215 (concat " " time-string
))
1216 (when (not (and diary
(not todos-include-in-diary
)))
1219 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
1220 (setq new-item
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
1221 "\n\t" new-item nil nil
1))
1224 ;; Make sure the correct category is selected. There
1225 ;; are two cases: (i) we just visited the file, so no
1226 ;; category is selected yet, or (ii) we invoked
1227 ;; insertion "here" from outside the category we want
1228 ;; to insert in (with priority insertion, category
1229 ;; selection is done by todos-set-item-priority).
1230 (when (or (= (- (point-max) (point-min)) (buffer-size))
1231 (and here called-from-outside
))
1232 (todos-category-number cat
)
1233 (todos-category-select))
1234 ;; If only done items are displayed in category,
1235 ;; toggle to todo items before inserting new item.
1236 (when (save-excursion
1237 (goto-char (point-min))
1238 (looking-at todos-done-string-start
))
1240 (todos-toggle-view-done-only))
1243 ;; If command was invoked with point in done
1244 ;; items section or outside of the current
1245 ;; category, can't insert "here", so to be
1246 ;; useful give new item top priority.
1247 (when (or (todos-done-item-section-p)
1250 (goto-char (point-min)))
1251 (todos-insert-with-overlays new-item
))
1252 (todos-set-item-priority new-item cat t
))
1253 (setq item-added t
))
1254 ;; If user cancels before setting priority, restore
1259 (unless (equal cat ocat
)
1260 (todos-category-number ocat
)
1261 (todos-category-select))
1262 (and done-only
(todos-toggle-view-done-only)))
1263 (set-window-buffer (selected-window) (set-buffer obuf
)))
1265 ;; If the todo items section is not visible when the
1266 ;; insertion command is called (either because only done
1267 ;; items were shown or because the category was not in the
1268 ;; current buffer), then if the item is inserted at the
1269 ;; end of the category, point is at eob and eob at
1270 ;; window-start, so that higher priority todo items are
1271 ;; out of view. So we recenter to make sure the todo
1272 ;; items are displayed in the window.
1273 (when item-added
(recenter)))
1274 (todos-update-count 'todo
1)
1275 (if (or diary todos-include-in-diary
) (todos-update-count 'diary
1))
1276 (todos-update-categories-sexp))))))
1278 (defvar todos-date-from-calendar nil
1279 "Helper variable for setting item date from the Emacs Calendar.")
1281 (defun todos-set-date-from-calendar ()
1282 "Return string of date chosen from Calendar."
1283 (cond ((and (stringp todos-date-from-calendar
)
1284 (string-match todos-date-pattern todos-date-from-calendar
))
1285 todos-date-from-calendar
)
1286 (todos-date-from-calendar
1287 (let (calendar-view-diary-initially-flag)
1288 (calendar)) ; *Calendar* is now current buffer.
1289 (define-key calendar-mode-map
[remap newline
] 'exit-recursive-edit
)
1290 ;; If user exits Calendar before choosing a date, clean up properly.
1291 (define-key calendar-mode-map
1292 [remap calendar-exit
] (lambda ()
1296 (exit-recursive-edit))))
1297 (message "Put cursor on a date and type <return> to set it.")
1300 (when (equal (buffer-name) calendar-buffer
)
1301 (setq todos-date-from-calendar
1302 (calendar-date-string (calendar-cursor-to-date t
) t t
))
1304 todos-date-from-calendar
)
1305 (define-key calendar-mode-map
[remap newline
] nil
)
1306 (define-key calendar-mode-map
[remap calendar-exit
] nil
)
1307 (unless (zerop (recursion-depth)) (exit-recursive-edit))
1308 (when (stringp todos-date-from-calendar
)
1309 todos-date-from-calendar
)))))
1311 (defun todos-insert-item-from-calendar (&optional arg
)
1312 "Prompt for and insert a new item with date selected from calendar.
1313 Invoked without prefix argument ARG, insert the item into the
1314 current category, without one prefix argument, prompt for the
1315 category from the current todo file or from one listed in
1316 `todos-category-completions-files'; with two prefix arguments,
1317 prompt for a todo file and then for a category in it."
1319 (setq todos-date-from-calendar
1320 (calendar-date-string (calendar-cursor-to-date t
) t t
))
1322 (todos-basic-insert-item arg nil nil todos-date-from-calendar
))
1324 (define-key calendar-mode-map
"it" 'todos-insert-item-from-calendar
)
1326 (defun todos-copy-item ()
1327 "Copy item at point and insert the copy as a new item."
1329 (unless (or (todos-done-item-p) (looking-at "^$"))
1330 (let ((copy (todos-item-string))
1331 (diary-item (todos-diary-item-p)))
1332 (todos-set-item-priority copy
(todos-current-category) t
)
1333 (todos-update-count 'todo
1)
1334 (when diary-item
(todos-update-count 'diary
1))
1335 (todos-update-categories-sexp))))
1337 (defun todos-delete-item ()
1338 "Delete at least one item in this category.
1340 If there are marked items, delete all of these; otherwise, delete
1345 (let* ((cat (todos-current-category))
1346 (marked (assoc cat todos-categories-with-marks
))
1347 (item (unless marked
(todos-item-string)))
1350 "Permanently delete all marked items? ")
1352 (setq ov
(make-overlay
1353 (save-excursion (todos-item-start))
1354 (save-excursion (todos-item-end))))
1355 (overlay-put ov
'face
'todos-search
)
1356 (todos-y-or-n-p "Permanently delete this item? "))))
1359 (and marked
(goto-char (point-min)))
1362 (if (or (and marked
(todos-marked-item-p)) item
)
1364 (if (todos-done-item-p)
1365 (todos-update-count 'done -
1)
1366 (todos-update-count 'todo -
1 cat
)
1367 (and (todos-diary-item-p)
1368 (todos-update-count 'diary -
1)))
1369 (if ov
(delete-overlay ov
))
1371 ;; Don't leave point below last item.
1372 (and item
(bolp) (eolp) (< (point-min) (point-max))
1373 (todos-backward-item))
1375 (throw 'done
(setq item nil
))))
1376 (todos-forward-item))))
1378 (setq todos-categories-with-marks
1379 (assq-delete-all cat todos-categories-with-marks
)))
1380 (todos-update-categories-sexp)
1381 (todos-prefix-overlays)))
1382 (if ov
(delete-overlay ov
)))))
1384 (defun todos-edit-item (&optional arg
)
1385 "Edit the Todo item at point.
1387 With non-nil prefix argument ARG, include the item's date/time
1388 header, making it also editable; otherwise, include only the item
1391 If the item consists of only one logical line, edit it in the
1392 minibuffer; otherwise, edit it in Todos Edit mode."
1394 (when (todos-item-string)
1395 (let* ((opoint (point))
1396 (start (todos-item-start))
1399 (concat todos-date-string-start todos-date-pattern
1400 "\\( " diary-time-regexp
"\\)?"
1401 (regexp-quote todos-nondiary-end
) "?")
1402 (line-end-position) t
)
1403 (1+ (- (point) start
))))
1404 (header (substring (todos-item-string) 0 item-beg
))
1405 (item (if arg
(todos-item-string)
1406 (substring (todos-item-string) item-beg
)))
1407 (multiline (> (length (split-string item
"\n")) 1))
1408 (buffer-read-only nil
))
1410 (todos-edit-multiline-item)
1411 (let ((new (concat (if arg
"" header
)
1412 (read-string "Edit: " (if arg
1413 (cons item item-beg
)
1416 (while (not (string-match (concat todos-date-string-start
1417 todos-date-pattern
) new
))
1418 (setq new
(read-from-minibuffer
1419 "Item must start with a date: " new
))))
1420 ;; Ensure lines following hard newlines are indented.
1421 (setq new
(replace-regexp-in-string "\\(\n\\)[^[:blank:]]"
1422 "\n\t" new nil nil
1))
1423 ;; If user moved point during editing, make sure it moves back.
1426 (todos-insert-with-overlays new
)
1427 (move-to-column item-beg
))))))
1429 (defun todos-edit-multiline-item ()
1430 "Edit current Todo item in Todos Edit mode.
1431 Use of newlines invokes `todos-indent' to insure compliance with
1432 the format of Diary entries."
1434 (when (todos-item-string)
1435 (let ((buf todos-edit-buffer
))
1436 (set-window-buffer (selected-window)
1437 (set-buffer (make-indirect-buffer (buffer-name) buf
)))
1438 (narrow-to-region (todos-item-start) (todos-item-end))
1440 (message "%s" (substitute-command-keys
1441 (concat "Type \\[todos-edit-quit] "
1442 "to return to Todos mode.\n"))))))
1444 (defun todos-edit-quit ()
1445 "Return from Todos Edit mode to Todos mode.
1446 If the item contains hard line breaks, make sure the following
1447 lines are indented by `todos-indent-to-here' to conform to diary
1450 If the whole file was in Todos Edit mode, check before returning
1451 whether the file is still a valid Todos file and if so, also
1452 recalculate the Todos categories sexp, in case changes were made
1453 in the number or names of categories."
1455 (if (> (buffer-size) (- (point-max) (point-min)))
1456 ;; We got here via `e m'.
1457 (let ((item (buffer-string))
1458 (regex "\\(\n\\)[^[:blank:]]")
1459 (buf (buffer-base-buffer)))
1460 (while (not (string-match (concat todos-date-string-start
1461 todos-date-pattern
) item
))
1462 (setq item
(read-from-minibuffer
1463 "Item must start with a date: " item
)))
1464 ;; Ensure lines following hard newlines are indented.
1465 (when (string-match regex
(buffer-string))
1466 (setq item
(replace-regexp-in-string regex
"\n\t" item nil nil
1))
1467 (delete-region (point-min) (point-max))
1470 (unless (eq (current-buffer) buf
)
1471 (set-window-buffer (selected-window) (set-buffer buf
))))
1472 ;; We got here via `F e'.
1473 (when (todos-check-format)
1474 ;; FIXME: separate out sexp check?
1475 ;; If manual editing makes e.g. item counts change, have to
1476 ;; call this to update todos-categories, but it restores
1477 ;; category order to list order.
1478 ;; (todos-repair-categories-sexp)
1479 ;; Compare (todos-make-categories-list t) with sexp and if
1480 ;; different ask (todos-update-categories-sexp) ?
1482 (let* ((cat-beg (concat "^" (regexp-quote todos-category-beg
)
1484 (curline (buffer-substring-no-properties
1485 (line-beginning-position) (line-end-position)))
1486 (cat (cond ((string-match cat-beg curline
)
1487 (match-string-no-properties 1 curline
))
1488 ((or (re-search-backward cat-beg nil t
)
1489 (re-search-forward cat-beg nil t
))
1490 (match-string-no-properties 1)))))
1491 (todos-category-number cat
)
1492 (todos-category-select)
1493 (goto-char (point-min))))))
1495 (defun todos-basic-edit-item-header (what &optional inc
)
1496 "Function underlying commands to edit item date/time header.
1498 The argument WHAT (passed by invoking commands) specifies what
1499 part of the header to edit; possible values are these symbols:
1500 `date', to edit the year, month, and day of the date string;
1501 `time', to edit just the time string; `calendar', to select the
1502 date from the Calendar; `today', to set the date to today's date;
1503 `dayname', to set the date string to the name of a day or to
1504 change the day name; and `year', `month' or `day', to edit only
1505 these respective parts of the date string (`day' is the number of
1506 the given day of the month, and `month' is either the name of the
1507 given month or its number, depending on the value of
1508 `calendar-date-display-form').
1510 The optional argument INC is a positive or negative integer
1511 \(passed by invoking commands as a numerical prefix argument)
1512 that in conjunction with the WHAT values `year', `month' or
1513 `day', increments or decrements the specified date string
1514 component by the specified number of suitable units, i.e., years,
1515 months, or days, with automatic adjustment of the other date
1516 string components as necessary.
1518 If there are marked items, apply the same edit to all of these;
1519 otherwise, edit just the item at point."
1520 (let* ((cat (todos-current-category))
1521 (marked (assoc cat todos-categories-with-marks
))
1523 (todos-date-from-calendar t
)
1524 (buffer-read-only nil
)
1525 ndate ntime year monthname month day
1526 dayname
) ; Needed by calendar-date-display-form.
1528 (or (and marked
(goto-char (point-min))) (todos-item-start))
1532 (while (not (todos-marked-item-p))
1533 (todos-forward-item)
1534 (and (eobp) (throw 'end nil
))))
1535 (re-search-forward (concat todos-date-string-start
"\\(?1:"
1537 "\\)\\(?2: " diary-time-regexp
"\\)?"
1538 (regexp-quote todos-nondiary-end
) "?")
1539 (line-end-position) t
)
1540 (let* ((odate (match-string-no-properties 1))
1541 (otime (match-string-no-properties 2))
1542 (omonthname (match-string-no-properties 6))
1543 (omonth (match-string-no-properties 7))
1544 (oday (match-string-no-properties 8))
1545 (oyear (match-string-no-properties 9))
1546 (tmn-array todos-month-name-array
)
1547 (mlist (append tmn-array nil
))
1548 (tma-array todos-month-abbrev-array
)
1549 (mablist (append tma-array nil
))
1550 (yy (and oyear
(unless (string= oyear
"*")
1551 (string-to-number oyear
))))
1552 (mm (or (and omonth
(unless (string= omonth
"*")
1553 (string-to-number omonth
)))
1554 (1+ (- (length mlist
)
1555 (length (or (member omonthname mlist
)
1556 (member omonthname mablist
)))))))
1557 (dd (and oday
(unless (string= oday
"*")
1558 (string-to-number oday
)))))
1559 ;; If there are marked items, use only the first to set
1560 ;; header changes, and apply these to all marked items.
1564 (setq ndate
(todos-read-date)))
1565 ((eq what
'calendar
)
1566 (setq ndate
(save-match-data (todos-set-date-from-calendar))))
1568 (setq ndate
(calendar-date-string (calendar-current-date) t t
)))
1570 (setq ndate
(todos-read-dayname)))
1572 (setq ntime
(save-match-data (todos-read-time)))
1573 (when (> (length ntime
) 0)
1574 (setq ntime
(concat " " ntime
))))
1575 ;; When date string consists only of a day name,
1576 ;; passing other date components is a NOP.
1577 ((and (memq what
'(year month day
))
1578 (not (or oyear omonth oday
))))
1581 monthname omonthname
1583 year
(cond ((not current-prefix-arg
)
1584 (todos-read-date 'year
))
1585 ((string= oyear
"*")
1586 (user-error "Cannot increment *"))
1588 (number-to-string (+ yy inc
))))))
1592 (if (memq 'month calendar-date-display-form
)
1595 (cond ((not current-prefix-arg
)
1596 (todos-read-date 'month
))
1597 ((or (string= omonth
"*") (= mm
13))
1598 (user-error "Cannot increment *"))
1600 (let ((mminc (+ mm inc
)))
1601 ;; Increment or decrement month by INC
1603 (setq mm
(% mminc
12))
1604 ;; If result is 0, make month December.
1605 (setq mm
(if (= mm
0) 12 (abs mm
)))
1606 ;; Adjust year if necessary.
1607 (setq year
(or (and (cond ((> mminc
12)
1608 (+ yy
(/ mminc
12)))
1610 (- yy
(/ mminc
12) 1))
1612 (number-to-string yy
))
1614 ;; Return the changed numerical month as
1615 ;; a string or the corresponding month name.
1617 (number-to-string mm
)
1618 (aref tma-array
(1- mm
))))))
1619 (let ((yy (string-to-number year
)) ; 0 if year is "*".
1620 ;; When mm is 13 (corresponding to "*" as value
1621 ;; of month), this raises an args-out-of-range
1622 ;; error in calendar-last-day-of-month, so use 1
1623 ;; (corresponding to January) to get 31 days.
1624 (mm (if (= mm
13) 1 mm
)))
1625 (if (> (string-to-number day
)
1626 (calendar-last-day-of-month mm yy
))
1627 (user-error "%s %s does not have %s days"
1628 (aref tmn-array
(1- mm
))
1629 (if (= mm
2) yy
"") day
))))
1633 monthname omonthname
1635 ((not current-prefix-arg
)
1636 (todos-read-date 'day mm oyear
))
1638 (user-error "Cannot increment *"))
1639 ((or (string= omonth
"*") (string= omonthname
"*"))
1640 (setq dd
(+ dd inc
))
1642 (user-error "A month cannot have more than 31 days")
1643 (number-to-string dd
)))
1644 ;; Increment or decrement day by INC,
1645 ;; adjusting month and year if necessary
1646 ;; (if year is "*" assume current year to
1647 ;; calculate adjustment).
1649 (let* ((yy (or yy
(calendar-extract-year
1650 (calendar-current-date))))
1651 (date (calendar-gregorian-from-absolute
1652 (+ (calendar-absolute-from-gregorian
1653 (list mm dd yy
)) inc
)))
1654 (adjmm (nth 0 date
)))
1655 ;; Set year and month(name) to adjusted values.
1656 (unless (string= year
"*")
1657 (setq year
(number-to-string (nth 2 date
))))
1659 (setq month
(number-to-string adjmm
))
1660 (setq monthname
(aref tma-array
(1- adjmm
))))
1661 ;; Return changed numerical day as a string.
1662 (number-to-string (nth 1 date
)))))))))
1663 ;; If new year, month or day date string components were
1664 ;; calculated, rebuild the whole date string from them.
1665 (when (memq what
'(year month day
))
1666 (if (or oyear omonth omonthname oday
)
1667 (setq ndate
(mapconcat 'eval calendar-date-display-form
""))
1668 (message "Cannot edit date component of empty date string")))
1669 (when ndate
(replace-match ndate nil nil nil
1))
1670 ;; Add new time string to the header, if it was supplied.
1673 (replace-match ntime nil nil nil
2)
1674 (goto-char (match-end 1))
1676 (setq todos-date-from-calendar nil
)
1678 ;; Apply the changes to the first marked item header to the
1679 ;; remaining marked items. If there are no marked items,
1682 (todos-forward-item)
1683 (goto-char (point-max))))))))
1685 (defun todos-edit-item-header ()
1686 "Interactively edit at least the date of item's date/time header.
1687 If user option `todos-always-add-time-string' is non-nil, also
1688 edit item's time string."
1690 (todos-basic-edit-item-header 'date
)
1691 (when todos-always-add-time-string
1692 (todos-edit-item-time)))
1694 (defun todos-edit-item-time ()
1695 "Interactively edit the time string of item's date/time header."
1697 (todos-basic-edit-item-header 'time
))
1699 (defun todos-edit-item-date-from-calendar ()
1700 "Interactively edit item's date using the Calendar."
1702 (todos-basic-edit-item-header 'calendar
))
1704 (defun todos-edit-item-date-to-today ()
1705 "Set item's date to today's date."
1707 (todos-basic-edit-item-header 'today
))
1709 (defun todos-edit-item-date-day-name ()
1710 "Replace item's date with the name of a day of the week."
1712 (todos-basic-edit-item-header 'dayname
))
1714 (defun todos-edit-item-date-year (&optional inc
)
1715 "Interactively edit the year of item's date string.
1716 With prefix argument INC a positive or negative integer,
1717 increment or decrement the year by INC."
1719 (todos-basic-edit-item-header 'year inc
))
1721 (defun todos-edit-item-date-month (&optional inc
)
1722 "Interactively edit the month of item's date string.
1723 With prefix argument INC a positive or negative integer,
1724 increment or decrement the month by INC."
1726 (todos-basic-edit-item-header 'month inc
))
1728 (defun todos-edit-item-date-day (&optional inc
)
1729 "Interactively edit the day of the month of item's date string.
1730 With prefix argument INC a positive or negative integer,
1731 increment or decrement the day by INC."
1733 (todos-basic-edit-item-header 'day inc
))
1735 (defun todos-edit-item-diary-inclusion ()
1736 "Change diary status of one or more todo items in this category.
1737 That is, insert `todos-nondiary-marker' if the candidate items
1738 lack this marking; otherwise, remove it.
1740 If there are marked todo items, change the diary status of all
1741 and only these, otherwise change the diary status of the item at
1744 (let ((buffer-read-only)
1745 (marked (assoc (todos-current-category)
1746 todos-categories-with-marks
)))
1749 (when marked
(goto-char (point-min)))
1751 (if (todos-done-item-p)
1752 (throw 'stop
(message "Done items cannot be edited"))
1753 (unless (and marked
(not (todos-marked-item-p)))
1754 (let* ((beg (todos-item-start))
1755 (lim (save-excursion (todos-item-end)))
1756 (end (save-excursion
1757 (or (todos-time-string-matcher lim
)
1758 (todos-date-string-matcher lim
)))))
1759 (if (looking-at (regexp-quote todos-nondiary-start
))
1762 (search-forward todos-nondiary-end
(1+ end
) t
)
1764 (todos-update-count 'diary
1))
1766 (insert todos-nondiary-start
)
1767 (goto-char (1+ end
))
1768 (insert todos-nondiary-end
)
1769 (todos-update-count 'diary -
1)))))
1770 (unless marked
(throw 'stop nil
))
1771 (todos-forward-item)))))
1772 (todos-update-categories-sexp)))
1774 (defun todos-edit-category-diary-inclusion (arg)
1775 "Make all items in this category diary items.
1776 With prefix ARG, make all items in this category non-diary
1780 (goto-char (point-min))
1781 (let ((todo-count (todos-get-count 'todo
))
1782 (diary-count (todos-get-count 'diary
))
1786 (if (todos-done-item-p) ; We've gone too far.
1788 (let* ((beg (todos-item-start))
1789 (lim (save-excursion (todos-item-end)))
1790 (end (save-excursion
1791 (or (todos-time-string-matcher lim
)
1792 (todos-date-string-matcher lim
)))))
1794 (unless (looking-at (regexp-quote todos-nondiary-start
))
1795 (insert todos-nondiary-start
)
1796 (goto-char (1+ end
))
1797 (insert todos-nondiary-end
))
1798 (when (looking-at (regexp-quote todos-nondiary-start
))
1800 (search-forward todos-nondiary-end
(1+ end
) t
)
1801 (replace-match "")))))
1802 (todos-forward-item))
1803 (unless (if arg
(zerop diary-count
) (= diary-count todo-count
))
1804 (todos-update-count 'diary
(if arg
1806 (- todo-count diary-count
))))
1807 (todos-update-categories-sexp)))))
1809 (defun todos-edit-item-diary-nonmarking ()
1810 "Change non-marking of one or more diary items in this category.
1811 That is, insert `diary-nonmarking-symbol' if the candidate items
1812 lack this marking; otherwise, remove it.
1814 If there are marked todo items, change the non-marking status of
1815 all and only these, otherwise change the non-marking status of
1818 (let ((buffer-read-only)
1819 (marked (assoc (todos-current-category)
1820 todos-categories-with-marks
)))
1823 (when marked
(goto-char (point-min)))
1825 (if (todos-done-item-p)
1826 (throw 'stop
(message "Done items cannot be edited"))
1827 (unless (and marked
(not (todos-marked-item-p)))
1829 (unless (looking-at (regexp-quote todos-nondiary-start
))
1830 (if (looking-at (regexp-quote diary-nonmarking-symbol
))
1832 (insert diary-nonmarking-symbol
))))
1833 (unless marked
(throw 'stop nil
))
1834 (todos-forward-item)))))))
1836 (defun todos-edit-category-diary-nonmarking (arg)
1837 "Add `diary-nonmarking-symbol' to all diary items in this category.
1838 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
1839 items in this category."
1842 (goto-char (point-min))
1843 (let (buffer-read-only)
1846 (if (todos-done-item-p) ; We've gone too far.
1848 (unless (looking-at (regexp-quote todos-nondiary-start
))
1850 (when (looking-at (regexp-quote diary-nonmarking-symbol
))
1852 (unless (looking-at (regexp-quote diary-nonmarking-symbol
))
1853 (insert diary-nonmarking-symbol
))))
1854 (todos-forward-item)))))))
1856 (defun todos-set-item-priority (&optional item cat new arg
)
1857 "Prompt for and set ITEM's priority in CATegory.
1859 Interactively, ITEM is the todo item at point, CAT is the current
1860 category, and the priority is a number between 1 and the number
1861 of items in the category. Non-interactively, non-nil NEW means
1862 ITEM is a new item and the lowest priority is one more than the
1863 number of items in CAT.
1865 The new priority is set either interactively by prompt or by a
1866 numerical prefix argument, or noninteractively by argument ARG,
1867 whose value can be either of the symbols `raise' or `lower',
1868 meaning to raise or lower the item's priority by one."
1870 (unless (and (called-interactively-p 'any
)
1871 (or (todos-done-item-p) (looking-at "^$")))
1872 (let* ((item (or item
(todos-item-string)))
1873 (marked (todos-marked-item-p))
1874 (cat (or cat
(cond ((eq major-mode
'todos-mode
)
1875 (todos-current-category))
1876 ((eq major-mode
'todos-filtered-items-mode
)
1878 (concat todos-date-string-start
1880 "\\( " diary-time-regexp
"\\)?"
1881 (regexp-quote todos-nondiary-end
)
1882 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
1884 (re-search-forward regexp1 nil t
)
1885 (match-string-no-properties 1)))))))
1887 (todo (cond ((or (eq arg
'raise
) (eq arg
'lower
)
1888 (eq major-mode
'todos-filtered-items-mode
))
1890 (let ((curstart (todos-item-start))
1892 (goto-char (point-min))
1893 (while (looking-at todos-item-start
)
1894 (setq count
(1+ count
))
1895 (when (= (point) curstart
) (setq curnum count
))
1896 (todos-forward-item))
1898 ((eq major-mode
'todos-mode
)
1899 (todos-get-count 'todo cat
))))
1900 (maxnum (if new
(1+ todo
) todo
))
1901 (prompt (format "Set item priority (1-%d): " maxnum
))
1902 (priority (cond ((and (not arg
) (numberp current-prefix-arg
))
1904 ((and (eq arg
'raise
) (>= curnum
1))
1906 ((and (eq arg
'lower
) (<= curnum maxnum
))
1910 (unless (and priority
1911 (or (and (eq arg
'raise
) (zerop priority
))
1912 (and (eq arg
'lower
) (> priority maxnum
))))
1913 ;; When moving item to another category, show the category before
1914 ;; prompting for its priority.
1915 (unless (or arg
(called-interactively-p 'any
))
1916 (todos-category-number cat
)
1917 ;; If done items in category are visible, keep them visible.
1918 (let ((done todos-show-with-done
))
1919 (when (> (buffer-size) (- (point-max) (point-min)))
1921 (goto-char (point-min))
1922 (setq done
(re-search-forward todos-done-string-start nil t
))))
1923 (let ((todos-show-with-done done
))
1924 (todos-category-select)
1925 ;; Keep top of category in view while setting priority.
1926 (goto-char (point-min)))))
1927 ;; Prompt for priority only when the category has at least one
1930 (while (not priority
)
1931 (setq candidate
(read-number prompt
))
1932 (setq prompt
(when (or (< candidate
1) (> candidate maxnum
))
1933 (format "Priority must be an integer between 1 and %d.\n"
1935 (unless prompt
(setq priority candidate
))))
1936 ;; In Top Priorities buffer, an item's priority can be changed
1937 ;; wrt items in another category, but not wrt items in the same
1939 (when (eq major-mode
'todos-filtered-items-mode
)
1940 (let* ((regexp2 (concat todos-date-string-start todos-date-pattern
1941 "\\( " diary-time-regexp
"\\)?"
1942 (regexp-quote todos-nondiary-end
)
1943 "?\\(?1:" (regexp-quote cat
) "\\)"))
1944 (end (cond ((< curnum priority
)
1945 (save-excursion (todos-item-end)))
1946 ((> curnum priority
)
1947 (save-excursion (todos-item-start)))))
1948 (match (save-excursion
1949 (cond ((< curnum priority
)
1950 (todos-forward-item (1+ (- priority curnum
)))
1951 (when (re-search-backward regexp2 end t
)
1952 (match-string-no-properties 1)))
1953 ((> curnum priority
)
1954 (todos-backward-item (- curnum priority
))
1955 (when (re-search-forward regexp2 end t
)
1956 (match-string-no-properties 1)))))))
1958 (user-error (concat "Cannot reprioritize items from the same "
1959 "category in this mode, only in Todos mode")))))
1960 ;; Interactively or with non-nil ARG, relocate the item within its
1962 (when (or arg
(called-interactively-p 'any
))
1963 (todos-remove-item))
1964 (goto-char (point-min))
1966 (unless (= priority
1)
1967 (todos-forward-item (1- priority
))
1968 ;; When called from todos-item-undone and the highest priority
1969 ;; is chosen, this advances point to the first done item, so
1970 ;; move it up to the empty line above the done items
1972 (when (looking-back (concat "^"
1973 (regexp-quote todos-category-done
)
1975 (todos-backward-item))))
1976 (todos-insert-with-overlays item
)
1977 ;; If item was marked, restore the mark.
1979 (let* ((ov (todos-get-overlay 'prefix
))
1980 (pref (overlay-get ov
'before-string
)))
1981 (overlay-put ov
'before-string
1982 (concat todos-item-mark pref
))))))))
1984 (defun todos-raise-item-priority ()
1985 "Raise priority of current item by moving it up by one item."
1987 (todos-set-item-priority nil nil nil
'raise
))
1989 (defun todos-lower-item-priority ()
1990 "Lower priority of current item by moving it down by one item."
1992 (todos-set-item-priority nil nil nil
'lower
))
1994 (defun todos-move-item (&optional file
)
1995 "Move at least one todo or done item to another category.
1996 If there are marked items, move all of these; otherwise, move
1999 With prefix argument FILE, prompt for a specific Todos file and
2000 choose (with TAB completion) a category in it to move the item or
2001 items to; otherwise, choose and move to any category in either
2002 the current Todos file or one of the files in
2003 `todos-category-completions-files'. If the chosen category is
2004 not an existing categories, then it is created and the item(s)
2005 become(s) the first entry/entries in that category.
2007 With moved Todo items, prompt to set the priority in the category
2008 moved to (with multiple todos items, the one that had the highest
2009 priority in the category moved from gets the new priority and the
2010 rest of the moved todo items are inserted in sequence below it).
2011 Moved done items are appended to the top of the done items
2012 section in the category moved to."
2014 (let* ((cat1 (todos-current-category))
2015 (marked (assoc cat1 todos-categories-with-marks
)))
2016 ;; Noop if point is not on an item and there are no marked items.
2017 (unless (and (looking-at "^$")
2019 (let* ((buffer-read-only)
2020 (file1 todos-current-todos-file
)
2021 (num todos-category-number
)
2022 (item (todos-item-string))
2023 (diary-item (todos-diary-item-p))
2024 (done-item (and (todos-done-item-p) (concat item
"\n")))
2025 (omark (save-excursion (todos-item-start) (point-marker)))
2029 ov cat2 file2 moved nmark todo-items done-items
)
2033 (setq ov
(make-overlay (save-excursion (todos-item-start))
2034 (save-excursion (todos-item-end))))
2035 (overlay-put ov
'face
'todos-search
))
2036 (let* ((pl (if (and marked
(> (cdr marked
) 1)) "s" ""))
2037 (cat+file
(todos-read-category (concat "Move item" pl
2040 (while (and (equal (car cat
+file
) cat1
)
2041 (equal (cdr cat
+file
) file1
))
2042 (setq cat
+file
(todos-read-category
2043 "Choose a different category: ")))
2044 (setq cat2
(car cat
+file
)
2045 file2
(cdr cat
+file
))))
2046 (if ov
(delete-overlay ov
)))
2047 (set-buffer (find-buffer-visiting file1
))
2050 (goto-char (point-min))
2052 (when (todos-marked-item-p)
2053 (if (todos-done-item-p)
2054 (setq done-items
(concat done-items
2055 (todos-item-string) "\n")
2057 (setq todo-items
(concat todo-items
2058 (todos-item-string) "\n")
2060 (when (todos-diary-item-p)
2061 (setq diary
(1+ diary
)))))
2062 (todos-forward-item))
2063 ;; Chop off last newline of multiple todo item string,
2064 ;; since it will be reinserted when setting priority
2065 ;; (but with done items priority is not set, so keep
2068 (setq todo-items
(substring todo-items
0 -
1))))
2069 (if (todos-done-item-p)
2072 (when (todos-diary-item-p) (setq diary
1))))
2073 (set-window-buffer (selected-window)
2074 (set-buffer (find-file-noselect file2
'nowarn
)))
2077 (when (or todo-items
(and item
(not done-item
)))
2078 (todos-set-item-priority (or todo-items item
) cat2 t
))
2079 ;; Move done items en bloc to top of done items section.
2080 (when (or done-items done-item
)
2081 (todos-category-number cat2
)
2083 (goto-char (point-min))
2085 (concat "^" (regexp-quote (concat todos-category-beg cat2
))
2088 (concat "^" (regexp-quote todos-category-done
)) nil t
)
2090 (insert (or done-items done-item
)))
2093 ;; Move succeeded, so remove item from starting category,
2094 ;; update item counts and display the category containing
2097 (setq nmark
(point-marker))
2098 (when todo
(todos-update-count 'todo todo
))
2099 (when diary
(todos-update-count 'diary diary
))
2100 (when done
(todos-update-count 'done done
))
2101 (todos-update-categories-sexp)
2102 (with-current-buffer (find-buffer-visiting file1
)
2111 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
2114 (setq end
(if (re-search-forward
2115 (concat "^" (regexp-quote
2116 todos-category-beg
)) nil t
)
2120 (while (< (point) end
)
2121 (if (todos-marked-item-p)
2123 (todos-forward-item)))
2124 (setq todos-categories-with-marks
2125 (assq-delete-all cat1 todos-categories-with-marks
)))
2126 (if ov
(delete-overlay ov
))
2127 (todos-remove-item))))
2128 (when todo
(todos-update-count 'todo
(- todo
) cat1
))
2129 (when diary
(todos-update-count 'diary
(- diary
) cat1
))
2130 (when done
(todos-update-count 'done
(- done
) cat1
))
2131 (todos-update-categories-sexp))
2132 (set-window-buffer (selected-window)
2133 (set-buffer (find-file-noselect file2
'nowarn
)))
2134 (setq todos-category-number
(todos-category-number cat2
))
2135 (let ((todos-show-with-done (or done-items done-item
)))
2136 (todos-category-select))
2138 ;; If item is moved to end of (just first?) category, make
2139 ;; sure the items above it are displayed in the window.
2141 ;; User quit before setting priority of todo item(s), so
2142 ;; return to starting category.
2144 (set-window-buffer (selected-window)
2145 (set-buffer (find-file-noselect file1
'nowarn
)))
2146 (todos-category-number cat1
)
2147 (todos-category-select)
2148 (goto-char omark
))))))))
2150 (defun todos-item-done (&optional arg
)
2151 "Tag a todo item in this category as done and relocate it.
2153 With prefix argument ARG prompt for a comment and append it to
2154 the done item; this is only possible if there are no marked
2155 items. If there are marked items, tag all of these with
2156 `todos-done-string' plus the current date and, if
2157 `todos-always-add-time-string' is non-nil, the current time;
2158 otherwise, just tag the item at point. Items tagged as done are
2159 relocated to the category's (by default hidden) done section. If
2160 done items are visible on invoking this command, they remain
2163 (let* ((cat (todos-current-category))
2164 (marked (assoc cat todos-categories-with-marks
)))
2168 (goto-char (point-max))
2169 (todos-backward-item)
2170 (unless (todos-done-item-p)
2172 (unless (re-search-forward
2173 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
2174 (goto-char (point-max)))
2176 (while (todos-done-item-p)
2177 (when (todos-marked-item-p)
2178 (user-error "This command does not apply to done items"))
2179 (todos-backward-item)))))
2180 (unless (and (not marked
)
2181 (or (todos-done-item-p)
2182 ;; Point is between todo and done items.
2184 (let* ((date-string (calendar-date-string (calendar-current-date) t t
))
2185 (time-string (if todos-always-add-time-string
2186 (concat " " (substring (current-time-string)
2189 (done-prefix (concat "[" todos-done-string date-string time-string
2191 (comment (and arg
(read-string "Enter a comment: ")))
2194 (show-done (save-excursion
2195 (goto-char (point-min))
2196 (re-search-forward todos-done-string-start nil t
)))
2197 (buffer-read-only nil
)
2198 item done-item opoint
)
2199 ;; Don't add empty comment to done item.
2200 (setq comment
(unless (zerop (length comment
))
2201 (concat " [" todos-comment-string
": " comment
"]")))
2202 (and marked
(goto-char (point-min)))
2204 ;; Stop looping when we hit the empty line below the last
2205 ;; todo item (this is eobp if only done items are hidden).
2206 (while (not (looking-at "^$"))
2207 (if (or (not marked
) (and marked
(todos-marked-item-p)))
2209 (setq item
(todos-item-string))
2210 (setq done-item
(concat done-item done-prefix item
2211 comment
(and marked
"\n")))
2212 (setq item-count
(1+ item-count
))
2213 (when (todos-diary-item-p)
2214 (setq diary-count
(1+ diary-count
)))
2216 (unless marked
(throw 'done nil
)))
2217 (todos-forward-item))))
2219 ;; Chop off last newline of done item string.
2220 (setq done-item
(substring done-item
0 -
1))
2221 (setq todos-categories-with-marks
2222 (assq-delete-all cat todos-categories-with-marks
)))
2226 (concat "^" (regexp-quote todos-category-done
)) nil t
)
2228 (when show-done
(setq opoint
(point)))
2229 (insert done-item
"\n"))
2230 (todos-update-count 'todo
(- item-count
))
2231 (todos-update-count 'done item-count
)
2232 (todos-update-count 'diary
(- diary-count
))
2233 (todos-update-categories-sexp)
2234 (let ((todos-show-with-done show-done
))
2235 (todos-category-select)
2236 ;; When done items are shown, put cursor on first just done item.
2237 (when opoint
(goto-char opoint
)))))))
2239 (defun todos-done-item-add-edit-or-delete-comment (&optional arg
)
2240 "Add a comment to this done item or edit an existing comment.
2241 With prefix ARG delete an existing comment."
2243 (when (todos-done-item-p)
2244 (let ((item (todos-item-string))
2246 (end (save-excursion (todos-item-end)))
2247 comment buffer-read-only
)
2250 (if (re-search-forward (concat " \\["
2251 (regexp-quote todos-comment-string
)
2252 ": \\([^]]+\\)\\]") end t
)
2254 (when (todos-y-or-n-p "Delete comment? ")
2255 (delete-region (match-beginning 0) (match-end 0)))
2256 (setq comment
(read-string "Edit comment: "
2257 (cons (match-string 1) 1)))
2258 (replace-match comment nil nil nil
1))
2259 (setq comment
(read-string "Enter a comment: "))
2260 ;; If user moved point during editing, make sure it moves back.
2263 (insert " [" todos-comment-string
": " comment
"]"))))))
2265 (defun todos-item-undone ()
2266 "Restore at least one done item to this category's todo section.
2267 Prompt for the new priority. If there are marked items, undo all
2268 of these, giving the first undone item the new priority and the
2269 rest following directly in sequence; otherwise, undo just the
2272 If the done item has a comment, ask whether to omit the comment
2273 from the restored item. With multiple marked done items with
2274 comments, only ask once, and if affirmed, omit subsequent
2275 comments without asking."
2277 (let* ((cat (todos-current-category))
2278 (marked (assoc cat todos-categories-with-marks
))
2279 (pl (if (and marked
(> (cdr marked
) 1)) "s" "")))
2280 (when (or marked
(todos-done-item-p))
2281 (let ((buffer-read-only)
2283 (omark (point-marker))
2287 start end item ov npoint undone
)
2288 (and marked
(goto-char (point-min)))
2291 (when (or (not marked
) (and marked
(todos-marked-item-p)))
2292 (if (not (todos-done-item-p))
2293 (user-error "Only done items can be undone")
2296 (setq ov
(make-overlay (save-excursion (todos-item-start))
2297 (save-excursion (todos-item-end))))
2298 (overlay-put ov
'face
'todos-search
))
2299 ;; Find the end of the date string added upon tagging item as
2301 (setq start
(search-forward "] "))
2302 (setq item-count
(1+ item-count
))
2303 (unless (looking-at (regexp-quote todos-nondiary-start
))
2304 (setq diary-count
(1+ diary-count
)))
2305 (setq end
(save-excursion (todos-item-end)))
2306 ;; Ask (once) whether to omit done item's comment. If
2307 ;; affirmed, omit subsequent comments without asking.
2308 (when (re-search-forward
2309 (concat " \\[" (regexp-quote todos-comment-string
)
2310 ": [^]]+\\]") end t
)
2312 (if (eq first
'first
)
2314 (if (eq todos-undo-item-omit-comment
'ask
)
2315 (when (todos-y-or-n-p
2316 (concat "Omit comment" pl
2317 " from restored item"
2320 (when todos-undo-item-omit-comment
'omit
)))
2322 (when (and (eq first
'first
) ov
) (delete-overlay ov
)))
2323 (when (eq first
'omit
)
2324 (setq end
(match-beginning 0))))
2325 (setq item
(concat item
2326 (buffer-substring-no-properties start end
)
2327 (when marked
"\n")))
2328 (unless marked
(throw 'done nil
))))
2329 (todos-forward-item)))
2332 ;; Chop off last newline of multiple items string, since
2333 ;; it will be reinserted on setting priority.
2334 (and marked
(setq item
(substring item
0 -
1)))
2335 (todos-set-item-priority item cat t
)
2336 (setq npoint
(point))
2338 (when ov
(delete-overlay ov
))
2345 (concat "^" (regexp-quote todos-category-done
)) nil t
)
2347 (if (todos-marked-item-p)
2349 (todos-forward-item)))
2350 (setq todos-categories-with-marks
2351 (assq-delete-all cat todos-categories-with-marks
)))
2353 (todos-remove-item))
2354 (todos-update-count 'todo item-count
)
2355 (todos-update-count 'done
(- item-count
))
2356 (when diary-count
(todos-update-count 'diary diary-count
))
2357 (todos-update-categories-sexp)
2358 (let ((todos-show-with-done (> (todos-get-count 'done
) 0)))
2359 (todos-category-select))
2360 ;; Put cursor on undone item.
2361 (goto-char npoint
)))
2362 (set-marker omark nil
)))))
2364 ;; -----------------------------------------------------------------------------
2365 ;;; Done Item Archives
2366 ;; -----------------------------------------------------------------------------
2368 (defcustom todos-skip-archived-categories nil
2369 "Non-nil to handle categories with only archived items specially.
2371 Sequential category navigation using \\[todos-forward-category]
2372 or \\[todos-backward-category] skips categories that contain only
2373 archived items. Other commands still recognize these categories.
2374 In Todos Categories mode (\\[todos-show-categories-table]) these
2375 categories shown in `todos-archived-only' face and pressing the
2376 category button visits the category in the archive instead of the
2379 :group
'todos-display
)
2381 (defun todos-find-archive (&optional ask
)
2382 "Visit the archive of the current Todos category, if it exists.
2383 If the category has no archived items, prompt to visit the
2384 archive anyway. If there is no archive for this file or with
2385 non-nil argument ASK, prompt to visit another archive.
2387 The buffer showing the archive is in Todos Archive mode. The
2388 first visit in a session displays the first category in the
2389 archive, subsequent visits return to the last category
2392 (let* ((cat (todos-current-category))
2393 (count (todos-get-count 'archived cat
))
2394 (archive (concat (file-name-sans-extension todos-current-todos-file
)
2397 (setq place
(cond (ask 'other-archive
)
2398 ((file-exists-p archive
) 'this-archive
)
2399 (t (when (todos-y-or-n-p
2400 (concat "This file has no archive; "
2401 "visit another archive? "))
2403 (when (eq place
'other-archive
)
2404 (setq archive
(todos-read-file-name "Choose a Todos archive: " t t
)))
2405 (when (and (eq place
'this-archive
) (zerop count
))
2406 (setq place
(when (todos-y-or-n-p
2407 (concat "This category has no archived items;"
2408 " visit archive anyway? "))
2411 (set-window-buffer (selected-window)
2412 (set-buffer (find-file-noselect archive
)))
2413 (if (member place
'(other-archive other-cat
))
2414 (setq todos-category-number
1)
2415 (todos-category-number cat
))
2416 (todos-category-select))))
2418 (defun todos-choose-archive ()
2419 "Choose an archive and visit it."
2421 (todos-find-archive t
))
2423 (defun todos-archive-done-item (&optional all
)
2424 "Archive at least one done item in this category.
2426 With prefix argument ALL, prompt whether to archive all done
2427 items in this category and on confirmation archive them.
2428 Otherwise, if there are marked done items (and no marked todo
2429 items), archive all of these; otherwise, archive the done item at
2432 If the archive of this file does not exist, it is created. If
2433 this category does not exist in the archive, it is created."
2435 (when (eq major-mode
'todos-mode
)
2436 (if (and all
(zerop (todos-get-count 'done
)))
2437 (message "No done items in this category")
2439 (let* ((cat (todos-current-category))
2440 (tbuf (current-buffer))
2441 (marked (assoc cat todos-categories-with-marks
))
2442 (afile (concat (file-name-sans-extension
2443 todos-current-todos-file
) ".toda"))
2444 (archive (if (file-exists-p afile
)
2445 (find-file-noselect afile t
)
2446 (get-buffer-create afile
)))
2447 (item (and (todos-done-item-p)
2448 (concat (todos-item-string) "\n")))
2450 (opoint (unless (todos-done-item-p) (point)))
2451 marked-items beg end all-done
2455 (if (todos-y-or-n-p "Archive all done items in this category? ")
2458 (goto-char (point-min))
2461 (re-search-forward todos-done-string-start
2463 (match-beginning 0))
2464 end
(if (re-search-forward
2466 (regexp-quote todos-category-beg
))
2470 all-done
(buffer-substring-no-properties beg end
)
2471 count
(todos-get-count 'done
))
2472 ;; Restore starting point, unless it was on a done
2473 ;; item, since they will all be deleted.
2474 (when opoint
(goto-char opoint
))))
2478 (goto-char (point-min))
2480 (when (todos-marked-item-p)
2481 (if (not (todos-done-item-p))
2482 (throw 'end
(message "Only done items can be archived"))
2484 (concat marked-items
(todos-item-string) "\n"))
2485 (setq count
(1+ count
))))
2486 (todos-forward-item)))))
2487 (if (not (or marked all item
))
2488 (throw 'end
(message "Only done items can be archived"))
2489 (with-current-buffer archive
2490 (unless buffer-file-name
(erase-buffer))
2491 (let (buffer-read-only)
2493 (goto-char (point-min))
2494 (if (and (re-search-forward
2495 (concat "^" (regexp-quote
2496 (concat todos-category-beg cat
)) "$")
2498 (re-search-forward (regexp-quote todos-category-done
)
2500 ;; Start of done items section in existing category.
2502 (todos-add-category nil cat
)
2503 ;; Start of done items section in new category.
2504 (goto-char (point-max)))
2505 (insert (cond (marked marked-items
)
2508 (todos-update-count 'done
(if (or marked all
) count
1) cat
)
2509 (todos-update-categories-sexp)
2510 ;; If archive is new, save to file now (using write-region in
2511 ;; order not to get prompted for file to save to), to let
2512 ;; auto-mode-alist take effect below.
2513 (unless buffer-file-name
2514 (write-region nil nil afile
)
2516 (with-current-buffer tbuf
2521 ;; Make sure done items are accessible.
2523 (remove-overlays beg end
)
2524 (delete-region beg end
)
2525 (todos-update-count 'done
(- count
))
2526 (todos-update-count 'archived count
))))
2528 ;; If we're archiving all done items, can't
2529 ;; first archive item point was on, since
2530 ;; that will short-circuit the rest.
2531 (and item
(not all
)))
2532 (and marked
(goto-char (point-min)))
2535 (if (or (and marked
(todos-marked-item-p)) item
)
2538 (todos-update-count 'done -
1)
2539 (todos-update-count 'archived
1)
2540 ;; Don't leave point below last item.
2541 (and item
(bolp) (eolp) (< (point-min) (point-max))
2542 (todos-backward-item))
2544 (throw 'done
(setq item nil
))))
2545 (todos-forward-item))))))
2547 (setq todos-categories-with-marks
2548 (assq-delete-all cat todos-categories-with-marks
)))
2549 (todos-update-categories-sexp)
2550 (todos-prefix-overlays)))
2552 (todos-category-number cat
)
2553 (todos-category-select)
2554 (split-window-below)
2555 (set-window-buffer (selected-window) tbuf
)
2556 ;; Make todo file current to select category.
2557 (find-file (buffer-file-name tbuf
))
2558 ;; Make sure done item separator is hidden (if done items
2559 ;; were initially visible).
2560 (let (todos-show-with-done) (todos-category-select)))))))
2562 (defun todos-unarchive-items ()
2563 "Unarchive at least one item in this archive category.
2564 If there are marked items, unarchive all of these; otherwise,
2565 unarchive the item at point.
2567 Unarchived items are restored as done items to the corresponding
2568 category in the Todos file, inserted at the top of done items
2569 section. If all items in the archive category have been
2570 restored, the category is deleted from the archive. If this was
2571 the only category in the archive, the archive file is deleted."
2573 (when (eq major-mode
'todos-archive-mode
)
2574 (let* ((cat (todos-current-category))
2575 (tbuf (find-file-noselect
2576 (concat (file-name-sans-extension todos-current-todos-file
)
2578 (marked (assoc cat todos-categories-with-marks
))
2579 (item (concat (todos-item-string) "\n"))
2585 (goto-char (point-min))
2587 (when (todos-marked-item-p)
2588 (setq marked-items
(concat marked-items
(todos-item-string) "\n"))
2589 (setq marked-count
(1+ marked-count
)))
2590 (todos-forward-item))))
2591 ;; Restore items to top of category's done section and update counts.
2592 (with-current-buffer tbuf
2593 (let (buffer-read-only newcat
)
2595 (goto-char (point-min))
2596 ;; Find the corresponding todo category, or if there isn't
2598 (unless (re-search-forward
2599 (concat "^" (regexp-quote (concat todos-category-beg cat
))
2601 (todos-add-category nil cat
)
2603 ;; Go to top of category's done section.
2605 (concat "^" (regexp-quote todos-category-done
)) nil t
)
2608 (insert marked-items
)
2609 (todos-update-count 'done marked-count cat
)
2610 (unless newcat
; Newly added category has no archive.
2611 (todos-update-count 'archived
(- marked-count
) cat
)))
2614 (todos-update-count 'done
1 cat
)
2615 (unless newcat
; Newly added category has no archive.
2616 (todos-update-count 'archived -
1 cat
))))
2617 (todos-update-categories-sexp)))
2618 ;; Delete restored items from archive.
2621 (goto-char (point-min)))
2624 (if (or (todos-marked-item-p) item
)
2628 (throw 'done
(setq item nil
))))
2629 (todos-forward-item))))
2630 (todos-update-count 'done
(if marked
(- marked-count
) -
1) cat
)
2631 ;; If that was the last category in the archive, delete the whole file.
2632 (if (= (length todos-categories
) 1)
2634 (delete-file todos-current-todos-file
)
2635 ;; Kill the archive buffer silently.
2636 (set-buffer-modified-p nil
)
2638 ;; Otherwise, if the archive category is now empty, delete it.
2639 (when (eq (point-min) (point-max))
2641 (let ((beg (re-search-backward
2642 (concat "^" (regexp-quote todos-category-beg
) cat
"$")
2644 (end (if (re-search-forward
2645 (concat "^" (regexp-quote todos-category-beg
))
2649 (remove-overlays beg end
)
2650 (delete-region beg end
)
2651 (setq todos-categories
(delete (assoc cat todos-categories
)
2653 (todos-update-categories-sexp))))
2654 ;; Visit category in Todos file and show restored done items.
2655 (let ((tfile (buffer-file-name tbuf
))
2656 (todos-show-with-done t
))
2657 (set-window-buffer (selected-window)
2658 (set-buffer (find-file-noselect tfile
)))
2659 (todos-category-number cat
)
2660 (todos-category-select)
2661 (message "Items unarchived.")))))
2663 (defun todos-jump-to-archive-category (&optional file
)
2664 "Prompt for a category in a Todos archive and jump to it.
2665 With prefix argument FILE, prompt for an archive and choose (with
2666 TAB completion) a category in it to jump to; otherwise, choose
2667 and jump to any category in the current archive."
2669 (todos-jump-to-category file
'archive
))
2671 ;; -----------------------------------------------------------------------------
2672 ;;; Todos mode display options
2673 ;; -----------------------------------------------------------------------------
2675 (defcustom todos-prefix
""
2676 "String prefixed to todo items for visual distinction."
2677 :type
'(string :validate
2679 (when (string= (widget-value widget
) todos-item-mark
)
2682 "Invalid value: must be distinct from `todos-item-mark'")
2684 :initialize
'custom-initialize-default
2685 :set
'todos-reset-prefix
2686 :group
'todos-display
)
2688 (defcustom todos-number-prefix t
2689 "Non-nil to prefix items with consecutively increasing integers.
2690 These reflect the priorities of the items in each category."
2692 :initialize
'custom-initialize-default
2693 :set
'todos-reset-prefix
2694 :group
'todos-display
)
2696 (defcustom todos-done-separator-string
"="
2697 "String determining the value of variable `todos-done-separator'.
2699 If the string consists of a single character,
2700 `todos-done-separator' will be the string made by repeating this
2701 character for the width of the window, and the length is
2702 automatically recalculated when the window width changes. If the
2703 string consists of more (or less) than one character, it will be
2704 the value of `todos-done-separator'."
2706 :initialize
'custom-initialize-default
2707 :set
'todos-reset-done-separator-string
2708 :group
'todos-display
)
2710 (defcustom todos-done-string
"DONE "
2711 "Identifying string appended to the front of done todos items."
2713 :initialize
'custom-initialize-default
2714 :set
'todos-reset-done-string
2715 :group
'todos-display
)
2717 (defcustom todos-comment-string
"COMMENT"
2718 "String inserted before optional comment appended to done item."
2720 :initialize
'custom-initialize-default
2721 :set
'todos-reset-comment-string
2722 :group
'todos-display
)
2724 (defcustom todos-show-with-done nil
2725 "Non-nil to display done items in all categories."
2727 :group
'todos-display
)
2729 (defun todos-mode-line-control (cat)
2730 "Return a mode line control for todo or archive file buffers.
2731 Argument CAT is the name of the current Todos category.
2732 This function is the value of the user variable
2733 `todos-mode-line-function'."
2734 (let ((file (todos-short-file-name todos-current-todos-file
)))
2735 (format "%s category %d: %s" file todos-category-number cat
)))
2737 (defcustom todos-mode-line-function
'todos-mode-line-control
2738 "Function that returns a mode line control for Todos buffers.
2739 The function expects one argument holding the name of the current
2740 Todos category. The resulting control becomes the local value of
2741 `mode-line-buffer-identification' in each Todos buffer."
2743 :group
'todos-display
)
2745 (defcustom todos-highlight-item nil
2746 "Non-nil means highlight items at point."
2748 :initialize
'custom-initialize-default
2749 :set
'todos-reset-highlight-item
2750 :group
'todos-display
)
2752 (defcustom todos-wrap-lines t
2753 "Non-nil to activate Visual Line mode and use wrap prefix."
2755 :group
'todos-display
)
2757 (defcustom todos-indent-to-here
3
2758 "Number of spaces to indent continuation lines of items.
2759 This must be a positive number to ensure such items are fully
2760 shown in the Fancy Diary display."
2761 :type
'(integer :validate
2763 (unless (> (widget-value widget
) 0)
2764 (widget-put widget
:error
2765 "Invalid value: must be a positive integer")
2767 :group
'todos-display
)
2769 (defun todos-indent ()
2770 "Indent from point to `todos-indent-to-here'."
2771 (indent-to todos-indent-to-here todos-indent-to-here
))
2773 ;; -----------------------------------------------------------------------------
2774 ;;; Display Commands
2775 ;; -----------------------------------------------------------------------------
2777 (defun todos-toggle-prefix-numbers ()
2778 "Hide item numbering if shown, show if hidden."
2782 (goto-char (point-min))
2783 (let* ((ov (todos-get-overlay 'prefix
))
2784 (show-done (re-search-forward todos-done-string-start nil t
))
2785 (todos-show-with-done show-done
)
2786 (todos-number-prefix (not (equal (overlay-get ov
'before-string
)
2788 (if (eq major-mode
'todos-filtered-items-mode
)
2789 (todos-prefix-overlays)
2790 (todos-category-select))))))
2792 (defun todos-toggle-view-done-items ()
2793 "Show hidden or hide visible done items in current category."
2795 (if (zerop (todos-get-count 'done
(todos-current-category)))
2796 (message "There are no done items in this category.")
2797 (let ((opoint (point)))
2798 (goto-char (point-min))
2799 (let* ((shown (re-search-forward todos-done-string-start nil t
))
2800 (todos-show-with-done (not shown
)))
2801 (todos-category-select)
2803 ;; If start of done items sections is below the bottom of the
2804 ;; window, make it visible.
2807 (goto-char (point-min))
2808 (re-search-forward todos-done-string-start nil t
)))
2809 (if (not (pos-visible-in-window-p shown
))
2811 (goto-char opoint
)))))))
2813 (defun todos-toggle-view-done-only ()
2814 "Switch between displaying only done or only todo items."
2816 (setq todos-show-done-only
(not todos-show-done-only
))
2817 (todos-category-select))
2819 (defun todos-toggle-item-highlighting ()
2820 "Highlight or unhighlight the todo item the cursor is on."
2827 (defun todos-toggle-item-header ()
2828 "Hide or show item date-time headers in the current file.
2829 With done items, this hides only the done date-time string, not
2830 the the original date-time string."
2834 (goto-char (point-min))
2835 (if (todos-get-overlay 'header
)
2836 (remove-overlays 1 (1+ (buffer-size)) 'todos
'header
)
2838 (goto-char (point-min))
2840 (when (re-search-forward
2841 (concat todos-item-start
2842 "\\( " diary-time-regexp
"\\)?"
2843 (regexp-quote todos-nondiary-end
) "? ")
2845 (setq ov
(make-overlay (match-beginning 0) (match-end 0) nil t
))
2846 (overlay-put ov
'todos
'header
)
2847 (overlay-put ov
'display
""))
2848 (todos-forward-item))))))
2850 ;; -----------------------------------------------------------------------------
2852 ;; -----------------------------------------------------------------------------
2854 (defface todos-prefix-string
2855 ;; '((t :inherit font-lock-constant-face))
2856 '((((class grayscale
) (background light
))
2857 (:foreground
"LightGray" :weight bold
:underline t
))
2858 (((class grayscale
) (background dark
))
2859 (:foreground
"Gray50" :weight bold
:underline t
))
2860 (((class color
) (min-colors 88) (background light
)) (:foreground
"dark cyan"))
2861 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Aquamarine"))
2862 (((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
2863 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
2864 (((class color
) (min-colors 8)) (:foreground
"magenta"))
2865 (t (:weight bold
:underline t
)))
2866 "Face for Todos prefix or numerical priority string."
2867 :group
'todos-faces
)
2869 (defface todos-top-priority
2870 ;; bold font-lock-comment-face
2871 '((default :weight bold
)
2872 (((class grayscale
) (background light
)) :foreground
"DimGray" :slant italic
)
2873 (((class grayscale
) (background dark
)) :foreground
"LightGray" :slant italic
)
2874 (((class color
) (min-colors 88) (background light
)) :foreground
"Firebrick")
2875 (((class color
) (min-colors 88) (background dark
)) :foreground
"chocolate1")
2876 (((class color
) (min-colors 16) (background light
)) :foreground
"red")
2877 (((class color
) (min-colors 16) (background dark
)) :foreground
"red1")
2878 (((class color
) (min-colors 8) (background light
)) :foreground
"red")
2879 (((class color
) (min-colors 8) (background dark
)) :foreground
"yellow")
2881 "Face for top priority Todos item numerical priority string.
2882 The item's priority number string has this face if the number is
2883 less than or equal the category's top priority setting."
2884 :group
'todos-faces
)
2887 ;; '((t :inherit font-lock-warning-face))
2891 (:weight bold
:foreground
"Red1"))
2895 (:weight bold
:foreground
"Pink"))
2899 (:weight bold
:foreground
"Red1"))
2903 (:weight bold
:foreground
"Pink"))
2906 (:foreground
"red"))
2908 (:weight bold
:inverse-video t
)))
2909 "Face for marks on marked items."
2910 :group
'todos-faces
)
2912 (defface todos-button
2913 ;; '((t :inherit widget-field))
2915 (:foreground
"black" :background
"yellow3"))
2916 (((class grayscale color
)
2918 (:background
"gray85"))
2919 (((class grayscale color
)
2921 (:background
"dim gray"))
2924 "Face for buttons in table of categories."
2925 :group
'todos-faces
)
2927 (defface todos-sorted-column
2932 (:background
"grey85"))
2935 (:background
"grey85" :foreground
"grey10"))
2937 (:background
"gray")))
2938 "Face for sorted column in table of categories."
2939 :group
'todos-faces
)
2941 (defface todos-archived-only
2942 ;; '((t (:inherit (shadow))))
2945 (:foreground
"grey50"))
2948 (:foreground
"grey70"))
2950 (:foreground
"gray")))
2951 "Face for archived-only category names in table of categories."
2952 :group
'todos-faces
)
2954 (defface todos-search
2955 ;; '((t :inherit match))
2959 (:background
"yellow1"))
2963 (:background
"RoyalBlue3"))
2967 (:foreground
"black" :background
"yellow"))
2971 (:foreground
"white" :background
"blue"))
2976 (:background
"gray")))
2977 "Face for matches found by `todos-search'."
2978 :group
'todos-faces
)
2980 (defface todos-diary-expired
2981 ;; Doesn't contrast enough with todos-date (= diary) face.
2982 ;; ;; '((t :inherit warning))
2983 ;; '((default :weight bold)
2984 ;; (((class color) (min-colors 16)) :foreground "DarkOrange")
2985 ;; (((class color)) :foreground "yellow"))
2986 ;; bold font-lock-function-name-face
2987 '((default :weight bold
)
2988 (((class color
) (min-colors 88) (background light
)) :foreground
"Blue1")
2989 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSkyBlue")
2990 (((class color
) (min-colors 16) (background light
)) :foreground
"Blue")
2991 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSkyBlue")
2992 (((class color
) (min-colors 8)) :foreground
"blue")
2993 (t :inverse-video t
))
2994 "Face for expired dates of diary items."
2995 :group
'todos-faces
)
2998 '((t :inherit diary
))
2999 "Face for the date string of a Todos item."
3000 :group
'todos-faces
)
3003 '((t :inherit diary-time
))
3004 "Face for the time string of a Todos item."
3005 :group
'todos-faces
)
3007 (defface todos-nondiary
3008 ;; '((t :inherit font-lock-type-face))
3009 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
3010 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
3011 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
3012 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
3013 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
3014 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
3015 (((class color
) (min-colors 8)) :foreground
"green")
3016 (t :weight bold
:underline t
))
3017 "Face for non-diary markers around todo item date/time header."
3018 :group
'todos-faces
)
3020 (defface todos-category-string
3021 ;; '((t :inherit font-lock-type-face))
3022 '((((class grayscale
) (background light
)) :foreground
"Gray90" :weight bold
)
3023 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
3024 (((class color
) (min-colors 88) (background light
)) :foreground
"ForestGreen")
3025 (((class color
) (min-colors 88) (background dark
)) :foreground
"PaleGreen")
3026 (((class color
) (min-colors 16) (background light
)) :foreground
"ForestGreen")
3027 (((class color
) (min-colors 16) (background dark
)) :foreground
"PaleGreen")
3028 (((class color
) (min-colors 8)) :foreground
"green")
3029 (t :weight bold
:underline t
))
3030 "Face for category-file header in Todos Filtered Items mode."
3031 :group
'todos-faces
)
3034 ;; '((t :inherit font-lock-keyword-face))
3035 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
3036 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
3037 (((class color
) (min-colors 88) (background light
)) :foreground
"Purple")
3038 (((class color
) (min-colors 88) (background dark
)) :foreground
"Cyan1")
3039 (((class color
) (min-colors 16) (background light
)) :foreground
"Purple")
3040 (((class color
) (min-colors 16) (background dark
)) :foreground
"Cyan")
3041 (((class color
) (min-colors 8)) :foreground
"cyan" :weight bold
)
3043 "Face for done Todos item header string."
3044 :group
'todos-faces
)
3046 (defface todos-comment
3047 ;; '((t :inherit font-lock-comment-face))
3048 '((((class grayscale
) (background light
))
3049 :foreground
"DimGray" :weight bold
:slant italic
)
3050 (((class grayscale
) (background dark
))
3051 :foreground
"LightGray" :weight bold
:slant italic
)
3052 (((class color
) (min-colors 88) (background light
))
3053 :foreground
"Firebrick")
3054 (((class color
) (min-colors 88) (background dark
))
3055 :foreground
"chocolate1")
3056 (((class color
) (min-colors 16) (background light
))
3058 (((class color
) (min-colors 16) (background dark
))
3060 (((class color
) (min-colors 8) (background light
))
3062 (((class color
) (min-colors 8) (background dark
))
3063 :foreground
"yellow")
3064 (t :weight bold
:slant italic
))
3065 "Face for comments appended to done Todos items."
3066 :group
'todos-faces
)
3068 (defface todos-done-sep
3069 ;; '((t :inherit font-lock-builtin-face))
3070 '((((class grayscale
) (background light
)) :foreground
"LightGray" :weight bold
)
3071 (((class grayscale
) (background dark
)) :foreground
"DimGray" :weight bold
)
3072 (((class color
) (min-colors 88) (background light
)) :foreground
"dark slate blue")
3073 (((class color
) (min-colors 88) (background dark
)) :foreground
"LightSteelBlue")
3074 (((class color
) (min-colors 16) (background light
)) :foreground
"Orchid")
3075 (((class color
) (min-colors 16) (background dark
)) :foreground
"LightSteelBlue")
3076 (((class color
) (min-colors 8)) :foreground
"blue" :weight bold
)
3078 "Face for separator string bewteen done and not done Todos items."
3079 :group
'todos-faces
)
3081 ;; -----------------------------------------------------------------------------
3082 ;;; Todos Categories mode options
3083 ;; -----------------------------------------------------------------------------
3085 (defcustom todos-categories-category-label
"Category"
3086 "Category button label in Todos Categories mode."
3088 :group
'todos-categories
)
3090 (defcustom todos-categories-todo-label
"Todo"
3091 "Todo button label in Todos Categories mode."
3093 :group
'todos-categories
)
3095 (defcustom todos-categories-diary-label
"Diary"
3096 "Diary button label in Todos Categories mode."
3098 :group
'todos-categories
)
3100 (defcustom todos-categories-done-label
"Done"
3101 "Done button label in Todos Categories mode."
3103 :group
'todos-categories
)
3105 (defcustom todos-categories-archived-label
"Archived"
3106 "Archived button label in Todos Categories mode."
3108 :group
'todos-categories
)
3110 (defcustom todos-categories-totals-label
"Totals"
3111 "String to label total item counts in Todos Categories mode."
3113 :group
'todos-categories
)
3115 (defcustom todos-categories-number-separator
" | "
3116 "String between number and category in Todos Categories mode.
3117 This separates the number from the category name in the default
3118 categories display according to priority."
3120 :group
'todos-categories
)
3122 (defcustom todos-categories-align
'center
3123 "Alignment of category names in Todos Categories mode."
3124 :type
'(radio (const left
) (const center
) (const right
))
3125 :group
'todos-categories
)
3127 ;; -----------------------------------------------------------------------------
3128 ;;; Entering and using Todos Categories mode
3129 ;; -----------------------------------------------------------------------------
3131 (defun todos-show-categories-table ()
3132 "Display a table of the current file's categories and item counts.
3134 In the initial display the categories are numbered, indicating
3135 their current order for navigating by \\[todos-forward-category]
3136 and \\[todos-backward-category]. You can persistantly change the
3137 order of the category at point by typing
3138 \\[todos-set-category-number], \\[todos-raise-category] or
3139 \\[todos-lower-category].
3141 The labels above the category names and item counts are buttons,
3142 and clicking these changes the display: sorted by category name
3143 or by the respective item counts (alternately descending or
3144 ascending). In these displays the categories are not numbered
3145 and \\[todos-set-category-number], \\[todos-raise-category] and
3146 \\[todos-lower-category] are disabled. (Programmatically, the
3147 sorting is triggered by passing a non-nil SORTKEY argument.)
3149 In addition, the lines with the category names and item counts
3150 are buttonized, and pressing one of these button jumps to the
3151 category in Todos mode (or Todos Archive mode, for categories
3152 containing only archived items, provided user option
3153 `todos-skip-archived-categories' is non-nil. These categories
3154 are shown in `todos-archived-only' face."
3156 (todos-display-categories)
3158 (todos-update-categories-display sortkey
)))
3160 (defun todos-sort-categories-alphabetically-or-numerically ()
3161 "Sort table of categories alphabetically or numerically."
3164 (goto-char (point-min))
3166 (if (member 'alpha todos-descending-counts
)
3168 (todos-update-categories-display nil
)
3169 (setq todos-descending-counts
3170 (delete 'alpha todos-descending-counts
)))
3171 (todos-update-categories-display 'alpha
))))
3173 (defun todos-sort-categories-by-todo ()
3174 "Sort table of categories by number of todo items."
3177 (goto-char (point-min))
3179 (todos-update-categories-display 'todo
)))
3181 (defun todos-sort-categories-by-diary ()
3182 "Sort table of categories by number of diary items."
3185 (goto-char (point-min))
3187 (todos-update-categories-display 'diary
)))
3189 (defun todos-sort-categories-by-done ()
3190 "Sort table of categories by number of non-archived done items."
3193 (goto-char (point-min))
3195 (todos-update-categories-display 'done
)))
3197 (defun todos-sort-categories-by-archived ()
3198 "Sort table of categories by number of archived items."
3201 (goto-char (point-min))
3203 (todos-update-categories-display 'archived
)))
3205 (defun todos-next-button (n)
3206 "Move point to the Nth next button in the table of categories."
3208 (forward-button n
'wrap
'display-message
)
3209 (and (bolp) (button-at (point))
3210 ;; Align with beginning of category label.
3211 (forward-char (+ 4 (length todos-categories-number-separator
)))))
3213 (defun todos-previous-button (n)
3214 "Move point to the Nth previous button in the table of categories."
3216 (backward-button n
'wrap
'display-message
)
3217 (and (bolp) (button-at (point))
3218 ;; Align with beginning of category label.
3219 (forward-char (+ 4 (length todos-categories-number-separator
)))))
3221 (defun todos-set-category-number (&optional arg
)
3222 "Change number of category at point in the table of categories.
3224 With ARG nil, prompt for the new number. Alternatively, the
3225 enter the new number with numerical prefix ARG. Otherwise, if
3226 ARG is either of the symbols `raise' or `lower', raise or lower
3227 the category line in the table by one, respectively, thereby
3228 decreasing or increasing its number."
3230 (let ((curnum (save-excursion
3231 ;; Get the number representing the priority of the category
3232 ;; on the current line.
3233 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
3234 (when curnum
; Do nothing if we're not on a category line.
3235 (let* ((maxnum (length todos-categories
))
3236 (prompt (format "Set category priority (1-%d): " maxnum
))
3237 (col (current-column))
3238 (buffer-read-only nil
)
3239 (priority (cond ((and (eq arg
'raise
) (> curnum
1))
3241 ((and (eq arg
'lower
) (< curnum maxnum
))
3244 (while (not priority
)
3245 (setq candidate
(or arg
(read-number prompt
)))
3248 (cond ((or (< candidate
1) (> candidate maxnum
))
3249 (format "Priority must be an integer between 1 and %d: "
3251 ((= candidate curnum
)
3252 "Choose a different priority than the current one: ")))
3253 (unless prompt
(setq priority candidate
)))
3254 (let* ((lower (< curnum priority
)) ; Priority is being lowered.
3255 (head (butlast todos-categories
3256 (apply (if lower
'identity
'1+)
3257 (list (- maxnum priority
)))))
3258 (tail (nthcdr (apply (if lower
'identity
'1-
) (list priority
))
3260 ;; Category's name and items counts list.
3261 (catcons (nth (1- curnum
) todos-categories
))
3262 (todos-categories (nconc head
(list catcons
) tail
))
3264 (when lower
(setq todos-categories
(nreverse todos-categories
)))
3265 (setq todos-categories
(delete-dups todos-categories
))
3266 (when lower
(setq todos-categories
(nreverse todos-categories
)))
3267 (setq newcats todos-categories
)
3269 (with-current-buffer (find-buffer-visiting todos-current-todos-file
)
3270 (setq todos-categories newcats
)
3271 (todos-update-categories-sexp))
3272 (todos-show-categories-table)
3273 (forward-line (1+ priority
))
3274 (forward-char col
))))))
3276 (defun todos-raise-category ()
3277 "Raise priority of category at point in Todos Categories buffer."
3279 (todos-set-category-number 'raise
))
3281 (defun todos-lower-category ()
3282 "Lower priority of category at point in Todos Categories buffer."
3284 (todos-set-category-number 'lower
))
3286 ;; -----------------------------------------------------------------------------
3288 ;; -----------------------------------------------------------------------------
3290 (defun todos-search ()
3291 "Search for a regular expression in this Todos file.
3292 The search runs through the whole file and encompasses all and
3293 only todo and done items; it excludes category names. Multiple
3294 matches are shown sequentially, highlighted in `todos-search'
3297 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3299 matches match cat in-done ov mlen msg
)
3301 (goto-char (point-min))
3303 (setq match
(re-search-forward regex nil t
))
3304 (goto-char (line-beginning-position))
3305 (unless (or (equal (point) 1)
3306 (looking-at (concat "^" (regexp-quote todos-category-beg
))))
3307 (if match
(push match matches
)))
3309 (setq matches
(reverse matches
))
3313 (setq match
(pop matches
))
3316 (when (looking-at todos-done-string-start
)
3318 (re-search-backward (concat "^" (regexp-quote todos-category-beg
)
3319 "\\(.*\\)\n") nil t
)
3320 (setq cat
(match-string-no-properties 1))
3321 (todos-category-number cat
)
3322 (todos-category-select)
3324 (unless todos-show-with-done
(todos-toggle-view-done-items)))
3326 (setq ov
(make-overlay (- (point) (length regex
)) (point)))
3327 (overlay-put ov
'face
'todos-search
)
3329 (setq mlen
(length matches
))
3332 (format "There are %d more matches; go to next match? "
3334 "There is one more match; go to it? "))
3336 (throw 'stop
(setq msg
(if (> mlen
1)
3337 (format "There are %d more matches."
3339 "There is one more match."))))))
3340 (setq msg
"There are no more matches."))
3341 (todos-category-select)
3343 (message "No match for \"%s\"" regex
))
3345 (if (todos-y-or-n-p (concat msg
"\nUnhighlight matches? "))
3346 (todos-clear-matches)
3347 (message "You can unhighlight the matches later by typing %s"
3348 (key-description (car (where-is-internal
3349 'todos-clear-matches
))))))))
3351 (defun todos-clear-matches ()
3352 "Remove highlighting on matches found by todos-search."
3354 (remove-overlays 1 (1+ (buffer-size)) 'face
'todos-search
))
3356 ;; -----------------------------------------------------------------------------
3357 ;;; Item filtering options
3358 ;; -----------------------------------------------------------------------------
3360 (defcustom todos-top-priorities-overrides nil
3361 "List of rules specifying number of top priority items to show.
3362 These rules override `todos-top-priorities' on invocations of
3363 `\\[todos-filter-top-priorities]' and
3364 `\\[todos-filter-top-priorities-multifile]'. Each rule is a list
3365 of the form (FILE NUM ALIST), where FILE is a member of
3366 `todos-files', NUM is a number specifying the default number of
3367 top priority items for each category in that file, and ALIST,
3368 when non-nil, consists of conses of a category name in FILE and a
3369 number specifying the default number of top priority items in
3370 that category, which overrides NUM.
3372 This variable should be set interactively by
3373 `\\[todos-set-top-priorities-in-file]' or
3374 `\\[todos-set-top-priorities-in-category]'."
3376 :group
'todos-filtered
)
3378 (defcustom todos-top-priorities
1
3379 "Default number of top priorities shown by `todos-filter-top-priorities'."
3381 :group
'todos-filtered
)
3383 (defcustom todos-filter-files nil
3384 "List of default files for multifile item filtering."
3385 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
3386 (mapcar 'todos-short-file-name
3387 (funcall todos-files-function
))))
3388 :group
'todos-filtered
)
3390 (defcustom todos-filter-done-items nil
3391 "Non-nil to include done items when processing regexp filters.
3392 Done items from corresponding archive files are also included."
3394 :group
'todos-filtered
)
3396 ;; -----------------------------------------------------------------------------
3397 ;;; Item filtering commands
3398 ;; -----------------------------------------------------------------------------
3400 (defun todos-set-top-priorities-in-file ()
3401 "Set number of top priorities for this file.
3402 See `todos-set-top-priorities' for more details."
3404 (todos-set-top-priorities))
3406 (defun todos-set-top-priorities-in-category ()
3407 "Set number of top priorities for this category.
3408 See `todos-set-top-priorities' for more details."
3410 (todos-set-top-priorities t
))
3412 (defun todos-filter-top-priorities (&optional arg
)
3413 "Display a list of top priority items from different categories.
3414 The categories can be any of those in the current Todos file.
3416 With numerical prefix ARG show at most ARG top priority items
3417 from each category. With `C-u' as prefix argument show the
3418 numbers of top priority items specified by category in
3419 `todos-top-priorities-overrides', if this has an entry for the file(s);
3420 otherwise show `todos-top-priorities' items per category in the
3421 file(s). With no prefix argument, if a top priorities file for
3422 the current Todos file has previously been saved (see
3423 `todos-save-filtered-items-buffer'), visit this file; if there is
3424 no such file, build the list as with prefix argument `C-u'.
3426 The prefix ARG regulates how many top priorities from
3427 each category to show, as described above."
3429 (todos-filter-items 'top arg
))
3431 (defun todos-filter-top-priorities-multifile (&optional arg
)
3432 "Display a list of top priority items from different categories.
3433 The categories are a subset of the categories in the files listed
3434 in `todos-filter-files', or if this nil, in the files chosen from
3435 a file selection dialog that pops up in this case.
3437 With numerical prefix ARG show at most ARG top priority items
3438 from each category in each file. With `C-u' as prefix argument
3439 show the numbers of top priority items specified in
3440 `todos-top-priorities-overrides', if this is non-nil; otherwise show
3441 `todos-top-priorities' items per category. With no prefix
3442 argument, if a top priorities file for the chosen Todos files
3443 exists (see `todos-save-filtered-items-buffer'), visit this file;
3444 if there is no such file, do the same as with prefix argument
3447 (todos-filter-items 'top arg t
))
3449 (defun todos-filter-diary-items (&optional arg
)
3450 "Display a list of todo diary items from different categories.
3451 The categories can be any of those in the current Todos file.
3453 Called with no prefix ARG, if a diary items file for the current
3454 Todos file has previously been saved (see
3455 `todos-save-filtered-items-buffer'), visit this file; if there is
3456 no such file, build the list of diary items. Called with a
3457 prefix argument, build the list even if there is a saved file of
3460 (todos-filter-items 'diary arg
))
3462 (defun todos-filter-diary-items-multifile (&optional arg
)
3463 "Display a list of todo diary items from different categories.
3464 The categories are a subset of the categories in the files listed
3465 in `todos-filter-files', or if this nil, in the files chosen from
3466 a file selection dialog that pops up in this case.
3468 Called with no prefix ARG, if a diary items file for the chosen
3469 Todos files has previously been saved (see
3470 `todos-save-filtered-items-buffer'), visit this file; if there is
3471 no such file, build the list of diary items. Called with a
3472 prefix argument, build the list even if there is a saved file of
3475 (todos-filter-items 'diary arg t
))
3477 (defun todos-filter-regexp-items (&optional arg
)
3478 "Prompt for a regular expression and display items that match it.
3479 The matches can be from any categories in the current Todos file
3480 and with non-nil option `todos-filter-done-items', can include
3481 not only todo items but also done items, including those in
3484 Called with no prefix ARG, if a regexp items file for the current
3485 Todos file has previously been saved (see
3486 `todos-save-filtered-items-buffer'), visit this file; if there is
3487 no such file, build the list of regexp items. Called with a
3488 prefix argument, build the list even if there is a saved file of
3491 (todos-filter-items 'regexp arg
))
3493 (defun todos-filter-regexp-items-multifile (&optional arg
)
3494 "Prompt for a regular expression and display items that match it.
3495 The matches can be from any categories in the files listed in
3496 `todos-filter-files', or if this nil, in the files chosen from a
3497 file selection dialog that pops up in this case. With non-nil
3498 option `todos-filter-done-items', the matches can include not
3499 only todo items but also done items, including those in Archive
3502 Called with no prefix ARG, if a regexp items file for the current
3503 Todos file has previously been saved (see
3504 `todos-save-filtered-items-buffer'), visit this file; if there is
3505 no such file, build the list of regexp items. Called with a
3506 prefix argument, build the list even if there is a saved file of
3509 (todos-filter-items 'regexp arg t
))
3511 (defun todos-find-filtered-items-file ()
3512 "Choose a filtered items file and visit it."
3514 (let ((files (directory-files todos-directory t
"\.tod[rty]$" t
))
3517 (let ((type (cond ((equal (file-name-extension f
) "todr") "regexp")
3518 ((equal (file-name-extension f
) "todt") "top")
3519 ((equal (file-name-extension f
) "tody") "diary"))))
3520 (push (cons (concat (todos-short-file-name f
) " (" type
")") f
)
3522 (setq file
(completing-read "Choose a filtered items file: "
3523 falist nil t nil nil
(car falist
)))
3524 (setq file
(cdr (assoc-string file falist
)))
3527 (defun todos-go-to-source-item ()
3528 "Display the file and category of the filtered item at point."
3530 (let* ((str (todos-item-string))
3531 (buf (current-buffer))
3532 (res (todos-find-item str
))
3537 (message "Category %s does not contain this item." cat
)
3539 (set-window-buffer (selected-window)
3540 (set-buffer (find-buffer-visiting file
)))
3541 (setq todos-current-todos-file file
)
3542 (setq todos-category-number
(todos-category-number cat
))
3543 (let ((todos-show-with-done (if (or todos-filter-done-items
3544 (eq (cdr found
) 'done
))
3546 todos-show-with-done
)))
3547 (todos-category-select))
3548 (goto-char (car found
)))))
3550 ;; -----------------------------------------------------------------------------
3551 ;;; Printing Todos Buffers
3552 ;; -----------------------------------------------------------------------------
3554 (defcustom todos-print-buffer-function
'ps-print-buffer-with-faces
3555 "Function called by the command `todos-print-buffer'."
3559 (defvar todos-print-buffer
"*Todos Print*"
3560 "Name of buffer containing printable Todos text.")
3562 (defun todos-print-buffer (&optional to-file
)
3563 "Produce a printable version of the current Todos buffer.
3564 This converts overlays and soft line wrapping and, depending on
3565 the value of `todos-print-buffer-function', includes faces. With
3566 non-nil argument TO-FILE write the printable version to a file;
3567 otherwise, send it to the default printer."
3569 (let ((buf todos-print-buffer
)
3571 ((eq major-mode
'todos-mode
)
3572 (concat "Todos File: "
3573 (todos-short-file-name todos-current-todos-file
)
3574 "\nCategory: " (todos-current-category)))
3575 ((eq major-mode
'todos-filtered-items-mode
)
3577 (prefix (propertize (concat todos-prefix
" ")
3578 'face
'todos-prefix-string
))
3580 (fill-prefix (make-string todos-indent-to-here
32))
3581 (content (buffer-string))
3583 (with-current-buffer (get-buffer-create buf
)
3585 (goto-char (point-min))
3588 (end (save-excursion (todos-item-end))))
3589 (when todos-number-prefix
3591 (setq prefix
(propertize (concat (number-to-string num
) " ")
3592 'face
'todos-prefix-string
)))
3594 (fill-region beg end
))
3595 ;; Calling todos-forward-item infloops at todos-item-start due to
3596 ;; non-overlay prefix, so search for item start instead.
3597 (if (re-search-forward todos-item-start nil t
)
3599 (goto-char (point-max))))
3600 (if (re-search-backward (concat "^" (regexp-quote todos-category-done
))
3602 (replace-match todos-done-separator
))
3603 (goto-char (point-min))
3607 (let ((file (read-file-name "Print to file: ")))
3608 (funcall todos-print-buffer-function file
))
3609 (funcall todos-print-buffer-function
)))
3612 (defun todos-print-buffer-to-file ()
3613 "Save printable version of this Todos buffer to a file."
3615 (todos-print-buffer t
))
3617 ;; -----------------------------------------------------------------------------
3618 ;;; Legacy Todo Mode Files
3619 ;; -----------------------------------------------------------------------------
3621 (defcustom todos-todo-mode-date-time-regexp
3622 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
3623 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
3624 "Regexp matching legacy todo-mode.el item date-time strings.
3625 In order for `todos-convert-legacy-files' to correctly convert this
3626 string to the current Todos format, the regexp must contain four
3627 explicitly numbered groups (see `(elisp) Regexp Backslash'),
3628 where group 1 matches a string for the year, group 2 a string for
3629 the month, group 3 a string for the day and group 4 a string for
3630 the time. The default value converts date-time strings built
3631 using the default value of `todo-time-string-format' from
3636 (defun todos-convert-legacy-date-time ()
3637 "Return converted date-time string.
3638 Helper function for `todos-convert-legacy-files'."
3639 (let* ((year (match-string 1))
3640 (month (match-string 2))
3641 (monthname (calendar-month-name (string-to-number month
) t
))
3642 (day (match-string 3))
3643 (time (match-string 4))
3646 (insert (mapconcat 'eval calendar-date-display-form
"")
3647 (when time
(concat " " time
)))))
3649 (defun todos-convert-legacy-files ()
3650 "Convert legacy Todo files to the current Todos format.
3651 The files `todo-file-do' and `todo-file-done' are converted and
3652 saved (the latter as a Todos Archive file) with a new name in
3653 `todos-directory'. See also the documentation string of
3654 `todos-todo-mode-date-time-regexp' for further details."
3656 (if (fboundp 'todo-mode
)
3657 (require 'todo-mode
)
3658 (user-error "Void function `todo-mode'"))
3659 ;; Convert `todo-file-do'.
3660 (if (file-exists-p todo-file-do
)
3661 (let ((default "todo-do-conv")
3664 (insert-file-contents todo-file-do
)
3665 (let ((end (search-forward ")" (line-end-position) t
))
3666 (beg (search-backward "(" (line-beginning-position) t
)))
3667 (setq todo-categories
3668 (read (buffer-substring-no-properties beg end
))))
3670 (delete-region (line-beginning-position) (1+ (line-end-position)))
3673 ((looking-at (regexp-quote (concat todo-prefix todo-category-beg
)))
3674 (replace-match todos-category-beg
))
3675 ((looking-at (regexp-quote todo-category-end
))
3677 ((looking-at (regexp-quote (concat todo-prefix
" "
3678 todo-category-sep
)))
3679 (replace-match todos-category-done
))
3680 ((looking-at (concat (regexp-quote todo-prefix
) " "
3681 todos-todo-mode-date-time-regexp
" "
3682 (regexp-quote todo-initials
) ":"))
3683 (todos-convert-legacy-date-time)))
3685 (setq file
(concat todos-directory
3687 (format "Save file as (default \"%s\"): " default
)
3690 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
3692 (insert-file-contents file
)
3693 (let ((todos-categories (todos-make-categories-list t
)))
3694 (todos-update-categories-sexp))
3695 (write-region (point-min) (point-max) file nil
'nomessage
))
3696 ;; Convert `todo-file-done'.
3697 (when (file-exists-p todo-file-done
)
3699 (insert-file-contents todo-file-done
)
3700 (let ((beg (make-marker))
3702 cat cats comment item
)
3704 (when (looking-at todos-todo-mode-date-time-regexp
)
3705 (set-marker beg
(point))
3706 (todos-convert-legacy-date-time)
3707 (set-marker end
(point))
3709 (insert "[" todos-done-string
)
3713 (when (looking-at todos-todo-mode-date-time-regexp
)
3714 (todos-convert-legacy-date-time))
3715 (when (looking-at (concat " "
3716 (regexp-quote todo-initials
) ":"))
3717 (replace-match "")))
3718 (if (re-search-forward
3719 (concat "^" todos-todo-mode-date-time-regexp
) nil t
)
3720 (goto-char (match-beginning 0))
3721 (goto-char (point-max)))
3723 (when (looking-back "\\[\\([^][]+\\)\\]")
3724 (setq cat
(match-string 1))
3725 (goto-char (match-beginning 0))
3727 ;; If the item ends with a non-comment parenthesis not
3728 ;; followed by a period, we lose (but we inherit that problem
3729 ;; from todo-mode.el).
3730 (when (looking-back "(\\(.*\\)) ")
3731 (setq comment
(match-string 1))
3733 (insert "[" todos-comment-string
": " comment
"]"))
3734 (set-marker end
(point))
3735 (if (member cat cats
)
3736 ;; If item is already in its category, leave it there.
3737 (unless (save-excursion
3739 (concat "^" (regexp-quote todos-category-beg
)
3741 (string= (match-string 1) cat
))
3742 ;; Else move it to its category.
3743 (setq item
(buffer-substring-no-properties beg end
))
3744 (delete-region beg
(1+ end
))
3745 (set-marker beg
(point))
3748 (regexp-quote (concat todos-category-beg cat
))
3752 (if (re-search-forward
3753 (concat "^" (regexp-quote todos-category-beg
)
3755 (progn (goto-char (match-beginning 0))
3758 (goto-char (point-max)))
3763 (insert todos-category-beg cat
"\n\n" todos-category-done
"\n"))
3765 (set-marker beg nil
)
3766 (set-marker end nil
))
3767 (setq file
(concat (file-name-sans-extension file
) ".toda"))
3768 (write-region (point-min) (point-max) file nil
'nomessage nil t
))
3770 (insert-file-contents file
)
3771 (let ((todos-categories (todos-make-categories-list t
)))
3772 (todos-update-categories-sexp))
3773 (write-region (point-min) (point-max) file nil
'nomessage
)
3774 (setq archive-sexp
(read (buffer-substring-no-properties
3775 (line-beginning-position)
3776 (line-end-position)))))
3777 (setq file
(concat (file-name-sans-extension file
) ".todo"))
3778 ;; Update categories sexp of converted Todos file again, adding
3779 ;; counts of archived items.
3781 (insert-file-contents file
)
3782 (let ((sexp (read (buffer-substring-no-properties
3783 (line-beginning-position)
3784 (line-end-position)))))
3786 (let ((archive-cat (assoc (car cat
) archive-sexp
)))
3788 (aset (cdr cat
) 3 (aref (cdr archive-cat
) 2)))))
3789 (delete-region (line-beginning-position) (line-end-position))
3790 (prin1 sexp
(current-buffer)))
3791 (write-region (point-min) (point-max) file nil
'nomessage
)))
3792 (todos-reevaluate-filelist-defcustoms)
3793 (message "Format conversion done."))
3794 (user-error "No legacy Todo file exists")))
3796 ;; =============================================================================
3797 ;;; Todos utilities and internals
3798 ;; =============================================================================
3800 (defcustom todos-y-with-space nil
3801 "Non-nil means allow SPC to affirm a \"y or n\" question."
3805 (defun todos-y-or-n-p (prompt)
3806 "Ask \"y or n\" question PROMPT and return t if answer is \"y\".
3807 Also return t if answer is \"Y\", but unlike `y-or-n-p', allow
3808 SPC to affirm the question only if option `todos-y-with-space' is
3810 (unless todos-y-with-space
3811 (define-key query-replace-map
" " 'ignore
))
3814 (define-key query-replace-map
" " 'act
)))
3816 ;; -----------------------------------------------------------------------------
3817 ;;; File-level global variables and support functions
3818 ;; -----------------------------------------------------------------------------
3820 (defvar todos-files
(funcall todos-files-function
)
3821 "List of truenames of user's Todos files.")
3823 (defvar todos-archives
(funcall todos-files-function t
)
3824 "List of truenames of user's Todos archives.")
3826 (defvar todos-visited nil
3827 "List of Todos files visited in this session by `todos-show'.
3828 Used to determine initial display according to the value of
3829 `todos-show-first'.")
3831 (defvar todos-file-buffers nil
3832 "List of file names of live Todos mode buffers.")
3834 (defvar todos-global-current-todos-file nil
3835 "Variable holding name of current Todos file.
3836 Used by functions called from outside of Todos mode to visit the
3837 current Todos file rather than the default Todos file (i.e. when
3838 users option `todos-show-current-file' is non-nil).")
3840 (defun todos-absolute-file-name (name &optional type
)
3841 "Return the absolute file name of short Todos file NAME.
3842 With TYPE `archive' or `top' return the absolute file name of the
3843 short Todos Archive or Top Priorities file name, respectively."
3844 ;; NOP if there is no Todos file yet (i.e. don't concatenate nil).
3847 (concat todos-directory name
3848 (cond ((eq type
'archive
) ".toda")
3849 ((eq type
'top
) ".todt")
3850 ((eq type
'diary
) ".tody")
3851 ((eq type
'regexp
) ".todr")
3854 (defun todos-check-format ()
3855 "Signal an error if the current Todos file is ill-formatted.
3856 Otherwise return t. Display a message if the file is well-formed
3857 but the categories sexp differs from the current value of
3858 `todos-categories'."
3862 (goto-char (point-min))
3863 (let* ((cats (prin1-to-string todos-categories
))
3864 (ssexp (buffer-substring-no-properties (line-beginning-position)
3865 (line-end-position)))
3866 (sexp (read ssexp
)))
3867 ;; Check the first line for `todos-categories' sexp.
3870 (unless (and (stringp (car c
))
3873 (user-error "Invalid or missing todos-categories sexp"))))
3875 ;; Check well-formedness of categories.
3876 (let ((legit (concat
3877 "\\(^" (regexp-quote todos-category-beg
) "\\)"
3878 "\\|\\(" todos-date-string-start todos-date-pattern
"\\)"
3879 "\\|\\(^[ \t]+[^ \t]*\\)"
3881 "\\|\\(^" (regexp-quote todos-category-done
) "\\)"
3882 "\\|\\(" todos-done-string-start
"\\)")))
3884 (unless (looking-at legit
)
3885 (user-error "Illegitimate Todos file format at line %d"
3886 (line-number-at-pos (point))))
3888 ;; Warn user if categories sexp has changed.
3889 (unless (string= ssexp cats
)
3890 (message (concat "The sexp at the beginning of the file differs "
3891 "from the value of `todos-categories.\n"
3892 "If the sexp is wrong, you can fix it with "
3893 "M-x todos-repair-categories-sexp,\n"
3894 "but note this reverts any changes you have "
3895 "made in the order of the categories."))))))
3898 (defun todos-reevaluate-filelist-defcustoms ()
3899 "Reevaluate defcustoms that provide choice list of Todos files."
3900 (custom-set-default 'todos-default-todos-file
3901 (symbol-value 'todos-default-todos-file
))
3902 (todos-reevaluate-default-file-defcustom)
3903 (custom-set-default 'todos-filter-files
(symbol-value 'todos-filter-files
))
3904 (todos-reevaluate-filter-files-defcustom)
3905 (custom-set-default 'todos-category-completions-files
3906 (symbol-value 'todos-category-completions-files
))
3907 (todos-reevaluate-category-completions-files-defcustom))
3909 (defun todos-reevaluate-default-file-defcustom ()
3910 "Reevaluate defcustom of `todos-default-todos-file'.
3911 Called after adding or deleting a Todos file."
3912 (eval (defcustom todos-default-todos-file
(car (funcall todos-files-function
))
3913 "Todos file visited by first session invocation of `todos-show'."
3914 :type
`(radio ,@(mapcar (lambda (f) (list 'const f
))
3915 (mapcar 'todos-short-file-name
3916 (funcall todos-files-function
))))
3919 (defun todos-reevaluate-category-completions-files-defcustom ()
3920 "Reevaluate defcustom of `todos-category-completions-files'.
3921 Called after adding or deleting a Todos file."
3922 (eval (defcustom todos-category-completions-files nil
3923 "List of files for building `todos-read-category' completions."
3924 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
3925 (mapcar 'todos-short-file-name
3926 (funcall todos-files-function
))))
3929 (defun todos-reevaluate-filter-files-defcustom ()
3930 "Reevaluate defcustom of `todos-filter-files'.
3931 Called after adding or deleting a Todos file."
3932 (eval (defcustom todos-filter-files nil
3933 "List of files for multifile item filtering."
3934 :type
`(set ,@(mapcar (lambda (f) (list 'const f
))
3935 (mapcar 'todos-short-file-name
3936 (funcall todos-files-function
))))
3939 ;; -----------------------------------------------------------------------------
3940 ;;; Category-level global variables and support functions
3941 ;; -----------------------------------------------------------------------------
3943 (defun todos-category-number (cat)
3944 "Return the number of category CAT in this Todos file.
3945 The buffer-local variable `todos-category-number' holds this
3946 number as its value."
3947 (let ((categories (mapcar 'car todos-categories
)))
3948 (setq todos-category-number
3949 ;; Increment by one, so that the highest priority category in Todos
3950 ;; Categories mode is numbered one rather than zero.
3951 (1+ (- (length categories
)
3952 (length (member cat categories
)))))))
3954 (defun todos-current-category ()
3955 "Return the name of the current category."
3956 (car (nth (1- todos-category-number
) todos-categories
)))
3958 (defun todos-category-select ()
3959 "Display the current category correctly."
3960 (let ((name (todos-current-category))
3961 cat-begin cat-end done-start done-sep-start done-end
)
3963 (goto-char (point-min))
3965 (concat "^" (regexp-quote (concat todos-category-beg name
)) "$") nil t
)
3966 (setq cat-begin
(1+ (line-end-position)))
3967 (setq cat-end
(if (re-search-forward
3968 (concat "^" (regexp-quote todos-category-beg
)) nil t
)
3971 (setq mode-line-buffer-identification
3972 (funcall todos-mode-line-function name
))
3973 (narrow-to-region cat-begin cat-end
)
3974 (todos-prefix-overlays)
3975 (goto-char (point-min))
3976 (if (re-search-forward (concat "\n\\(" (regexp-quote todos-category-done
)
3979 (setq done-start
(match-beginning 0))
3980 (setq done-sep-start
(match-beginning 1))
3981 (setq done-end
(match-end 0)))
3982 (error "Category %s is missing todos-category-done string" name
))
3983 (if todos-show-done-only
3984 (narrow-to-region (1+ done-end
) (point-max))
3985 (when (and todos-show-with-done
3986 (re-search-forward todos-done-string-start nil t
))
3987 ;; Now we want to see the done items, so reset displayed end to end of
3989 (setq done-start cat-end
)
3990 ;; Make display overlay for done items separator string, unless there
3992 (let* ((done-sep todos-done-separator
)
3993 (ov (progn (goto-char done-sep-start
)
3994 (todos-get-overlay 'separator
))))
3996 (setq ov
(make-overlay done-sep-start done-end
))
3997 (overlay-put ov
'todos
'separator
)
3998 (overlay-put ov
'display done-sep
))))
3999 (narrow-to-region (point-min) done-start
)
4000 ;; Loading this from todos-mode, or adding it to the mode hook, causes
4001 ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
4002 (when todos-highlight-item
4004 (hl-line-mode 1)))))
4006 (defconst todos-category-beg
"--==-- "
4007 "String marking beginning of category (inserted with its name).")
4009 (defconst todos-category-done
"==--== DONE "
4010 "String marking beginning of category's done items.")
4012 (defun todos-done-separator ()
4013 "Return string used as value of variable `todos-done-separator'."
4014 (let ((sep todos-done-separator-string
))
4015 (propertize (if (= 1 (length sep
))
4016 ;; Until bug#2749 is fixed, if separator's length
4017 ;; is window-width, then with non-nil
4018 ;; todos-wrap-lines an indented empty line appears
4019 ;; between the separator and the first done item.
4020 ;; (make-string (1- (window-width)) (string-to-char sep))
4021 (make-string (window-width) (string-to-char sep
))
4022 todos-done-separator-string
)
4023 'face
'todos-done-sep
)))
4025 (defvar todos-done-separator
(todos-done-separator)
4026 "String used to visually separate done from not done items.
4027 Displayed as an overlay instead of `todos-category-done' when
4028 done items are shown. Its value is determined by user option
4029 `todos-done-separator-string'.")
4031 (defun todos-reset-done-separator (sep)
4032 "Replace existing overlays of done items separator string SEP."
4036 (goto-char (point-min))
4037 (while (re-search-forward
4038 (concat "\n\\(" (regexp-quote todos-category-done
) "\\)") nil t
)
4039 (let* ((beg (match-beginning 1))
4041 (ov (progn (goto-char beg
)
4042 (todos-get-overlay 'separator
)))
4043 (old-sep (when ov
(overlay-get ov
'display
)))
4046 (unless (string= old-sep sep
)
4047 (setq new-ov
(make-overlay beg end
))
4048 (overlay-put new-ov
'todos
'separator
)
4049 (overlay-put new-ov
'display todos-done-separator
)
4050 (delete-overlay ov
))))))))
4052 (defun todos-get-count (type &optional category
)
4053 "Return count of TYPE items in CATEGORY.
4054 If CATEGORY is nil, default to the current category."
4055 (let* ((cat (or category
(todos-current-category)))
4056 (counts (cdr (assoc cat todos-categories
)))
4057 (idx (cond ((eq type
'todo
) 0)
4058 ((eq type
'diary
) 1)
4060 ((eq type
'archived
) 3))))
4063 (defun todos-update-count (type increment
&optional category
)
4064 "Change count of TYPE items in CATEGORY by integer INCREMENT.
4065 With nil or omitted CATEGORY, default to the current category."
4066 (let* ((cat (or category
(todos-current-category)))
4067 (counts (cdr (assoc cat todos-categories
)))
4068 (idx (cond ((eq type
'todo
) 0)
4069 ((eq type
'diary
) 1)
4071 ((eq type
'archived
) 3))))
4072 (aset counts idx
(+ increment
(aref counts idx
)))))
4074 (defun todos-set-categories ()
4075 "Set `todos-categories' from the sexp at the top of the file."
4076 ;; New archive files created by `todos-move-category' are empty, which would
4077 ;; make the sexp test fail and raise an error, so in this case we skip it.
4078 (unless (zerop (buffer-size))
4082 (goto-char (point-min))
4083 (setq todos-categories
4084 (if (looking-at "\(\(\"")
4085 (read (buffer-substring-no-properties
4086 (line-beginning-position)
4087 (line-end-position)))
4088 (error "Invalid or missing todos-categories sexp")))))))
4090 (defun todos-update-categories-sexp ()
4091 "Update the `todos-categories' sexp at the top of the file."
4092 (let (buffer-read-only)
4096 (goto-char (point-min))
4097 (if (looking-at (concat "^" (regexp-quote todos-category-beg
)))
4098 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
4099 (setq todos-categories
(todos-make-categories-list t
)))
4100 (delete-region (line-beginning-position) (line-end-position)))
4101 (prin1 todos-categories
(current-buffer))))))
4103 (defun todos-make-categories-list (&optional force
)
4104 "Return an alist of Todos categories and their item counts.
4105 With non-nil argument FORCE parse the entire file to build the
4106 list; otherwise, get the value by reading the sexp at the top of
4108 (setq todos-categories nil
)
4112 (goto-char (point-min))
4113 (let (counts cat archive
)
4114 ;; If the file is a todo file and has archived items, identify the
4115 ;; archive, in order to count its items. But skip this with
4116 ;; `todos-convert-legacy-files', since that converts filed items to
4118 (when buffer-file-name
; During conversion there is no file yet.
4119 ;; If the file is an archive, it doesn't have an archive.
4120 (unless (member (file-truename buffer-file-name
)
4121 (funcall todos-files-function t
))
4122 (setq archive
(concat (file-name-sans-extension
4123 todos-current-todos-file
) ".toda"))))
4125 (cond ((looking-at (concat (regexp-quote todos-category-beg
)
4127 (setq cat
(match-string-no-properties 1))
4128 ;; Counts for each category: [todo diary done archive]
4129 (setq counts
(make-vector 4 0))
4130 (setq todos-categories
4131 (append todos-categories
(list (cons cat counts
))))
4132 ;; Add archived item count to the todo file item counts.
4133 ;; Make sure to include newly created archives, e.g. due to
4134 ;; todos-move-category.
4135 (when (member archive
(funcall todos-files-function t
))
4136 (let ((archive-count 0))
4137 (with-current-buffer (find-file-noselect archive
)
4139 (goto-char (point-min))
4140 (when (re-search-forward
4141 (concat "^" (regexp-quote todos-category-beg
)
4145 (while (not (or (looking-at
4147 (regexp-quote todos-category-beg
)
4150 (when (looking-at todos-done-string-start
)
4151 (setq archive-count
(1+ archive-count
)))
4153 (todos-update-count 'archived archive-count cat
))))
4154 ((looking-at todos-done-string-start
)
4155 (todos-update-count 'done
1 cat
))
4156 ((looking-at (concat "^\\("
4157 (regexp-quote diary-nonmarking-symbol
)
4158 "\\)?" todos-date-pattern
))
4159 (todos-update-count 'diary
1 cat
)
4160 (todos-update-count 'todo
1 cat
))
4161 ((looking-at (concat todos-date-string-start todos-date-pattern
))
4162 (todos-update-count 'todo
1 cat
))
4163 ;; If first line is todos-categories list, use it and end loop
4164 ;; -- unless FORCEd to scan whole file.
4167 (setq todos-categories
(read (buffer-substring-no-properties
4168 (line-beginning-position)
4169 (line-end-position))))
4170 (goto-char (1- (point-max))))))
4174 (defun todos-repair-categories-sexp ()
4175 "Repair corrupt Todos categories sexp.
4176 This should only be needed as a consequence of careless manual
4177 editing or a bug in todos.el.
4179 *Warning*: Calling this command restores the category order to
4180 the list element order in the Todos categories sexp, so any order
4181 changes made in Todos Categories mode will have to be made again."
4183 (let ((todos-categories (todos-make-categories-list t
)))
4184 (todos-update-categories-sexp)))
4186 ;; -----------------------------------------------------------------------------
4187 ;;; Item-level global variables and support functions
4188 ;; -----------------------------------------------------------------------------
4190 (defconst todos-month-name-array
4191 (vconcat calendar-month-name-array
(vector "*"))
4192 "Array of month names, in order.
4193 The final element is \"*\", indicating an unspecified month.")
4195 (defconst todos-month-abbrev-array
4196 (vconcat calendar-month-abbrev-array
(vector "*"))
4197 "Array of abbreviated month names, in order.
4198 The final element is \"*\", indicating an unspecified month.")
4200 (defconst todos-date-pattern
4201 (let ((dayname (diary-name-pattern calendar-day-name-array nil t
)))
4202 (concat "\\(?5:" dayname
"\\|"
4204 (monthname (format "\\(?6:%s\\)" (diary-name-pattern
4205 todos-month-name-array
4206 todos-month-abbrev-array
)))
4207 (month "\\(?7:[0-9]+\\|\\*\\)")
4208 (day "\\(?8:[0-9]+\\|\\*\\)")
4209 (year "-?\\(?9:[0-9]+\\|\\*\\)"))
4210 (mapconcat 'eval calendar-date-display-form
""))
4212 "Regular expression matching a Todos date header.")
4214 (defconst todos-nondiary-start
(nth 0 todos-nondiary-marker
)
4215 "String inserted before item date to block diary inclusion.")
4217 (defconst todos-nondiary-end
(nth 1 todos-nondiary-marker
)
4218 "String inserted after item date matching `todos-nondiary-start'.")
4220 ;; By itself this matches anything, because of the `?'; however, it's only
4221 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
4223 (defconst todos-date-string-start
4224 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
4225 (regexp-quote diary-nonmarking-symbol
) "\\)?")
4226 "Regular expression matching part of item header before the date.")
4228 (defconst todos-done-string-start
4229 (concat "^\\[" (regexp-quote todos-done-string
))
4230 "Regular expression matching start of done item.")
4232 (defconst todos-item-start
(concat "\\(" todos-date-string-start
"\\|"
4233 todos-done-string-start
"\\)"
4235 "String identifying start of a Todos item.")
4237 (defun todos-item-start ()
4238 "Move to start of current Todos item and return its position."
4240 ;; Buffer is empty (invocation possible e.g. via todos-forward-item
4241 ;; from todos-filter-items when processing category with no todo
4243 (eq (point-min) (point-max))
4244 ;; Point is on the empty line below category's last todo item...
4245 (and (looking-at "^$")
4246 (or (eobp) ; ...and done items are hidden...
4247 (save-excursion ; ...or done items are visible.
4249 (looking-at (concat "^"
4250 (regexp-quote todos-category-done
))))))
4251 ;; Buffer is widened.
4252 (looking-at (regexp-quote todos-category-beg
)))
4253 (goto-char (line-beginning-position))
4254 (while (not (looking-at todos-item-start
))
4258 (defun todos-item-end ()
4259 "Move to end of current Todos item and return its position."
4260 ;; Items cannot end with a blank line.
4261 (unless (looking-at "^$")
4262 (let* ((done (todos-done-item-p))
4264 ;; For todo items, end is before the done items section, for done
4265 ;; items, end is before the next category. If these limits are
4266 ;; missing or inaccessible, end it before the end of the buffer.
4267 (lim (if (save-excursion
4269 (concat "^" (regexp-quote (if done
4271 todos-category-done
)))
4273 (progn (setq to-lim t
) (match-beginning 0))
4275 (when (bolp) (forward-char)) ; Find start of next item.
4276 (goto-char (if (re-search-forward todos-item-start lim t
)
4278 (if to-lim lim
(point-max))))
4279 ;; For last todo item, skip back over the empty line before the done
4280 ;; items section, else just back to the end of the previous line.
4281 (backward-char (when (and to-lim
(not done
) (eq (point) lim
)) 2))
4284 (defun todos-item-string ()
4285 "Return bare text of current item as a string."
4286 (let ((opoint (point))
4287 (start (todos-item-start))
4288 (end (todos-item-end)))
4290 (and start end
(buffer-substring-no-properties start end
))))
4292 (defun todos-forward-item (&optional count
)
4293 "Move point COUNT items down (by default, move down by one item)."
4294 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
4295 (start (line-end-position)))
4297 (if (re-search-forward todos-item-start nil t
(or count
1))
4298 (goto-char (match-beginning 0))
4299 (goto-char (point-max)))
4300 ;; If points advances by one from a todo to a done item, go back
4301 ;; to the space above todos-done-separator, since that is a
4302 ;; legitimate place to insert an item. But skip this space if
4303 ;; count > 1, since that should only stop on an item.
4304 (when (and not-done
(todos-done-item-p) (not count
))
4305 ;; (if (or (not count) (= count 1))
4306 (re-search-backward "^$" start t
))));)
4307 ;; The preceding sexp is insufficient when buffer is not narrowed,
4308 ;; since there could be no done items in this category, so the
4309 ;; search puts us on first todo item of next category. Does this
4310 ;; ever happen? If so:
4311 ;; (let ((opoint) (point))
4312 ;; (forward-line -1)
4313 ;; (when (or (not count) (= count 1))
4314 ;; (cond ((looking-at (concat "^" (regexp-quote todos-category-beg)))
4315 ;; (forward-line -2))
4316 ;; ((looking-at (concat "^" (regexp-quote todos-category-done)))
4317 ;; (forward-line -1))
4319 ;; (goto-char opoint)))))))
4321 (defun todos-backward-item (&optional count
)
4322 "Move point up to start of item with next higher priority.
4323 With positive numerical prefix COUNT, move point COUNT items
4326 If the category's done items are visible, this command called
4327 with a prefix argument only moves point to a higher item, e.g.,
4328 with point on the first done item and called with prefix 1, it
4329 moves to the last todo item; but if called with point on the
4330 first done item without a prefix argument, it moves point the the
4331 empty line above the done items separator."
4332 (let* ((done (todos-done-item-p)))
4335 (re-search-backward todos-item-start nil t
(or count
1)))
4336 ;; Unless this is a regexp filtered items buffer (which can contain
4337 ;; intermixed todo and done items), if points advances by one from a
4338 ;; done to a todo item, go back to the space above
4339 ;; todos-done-separator, since that is a legitimate place to insert an
4340 ;; item. But skip this space if count > 1, since that should only
4342 (when (and done
(not (todos-done-item-p)) (not count
)
4343 ;(or (not count) (= count 1))
4344 (not (equal (buffer-name) todos-regexp-items-buffer
)))
4345 (re-search-forward (concat "^" (regexp-quote todos-category-done
))
4347 (forward-line -
1))))
4349 (defun todos-remove-item ()
4350 "Internal function called in editing, deleting or moving items."
4351 (let* ((end (progn (todos-item-end) (1+ (point))))
4352 (beg (todos-item-start))
4353 (ov (todos-get-overlay 'prefix
)))
4354 (when ov
(delete-overlay ov
))
4355 (delete-region beg end
)))
4357 (defun todos-diary-item-p ()
4358 "Return non-nil if item at point has diary entry format."
4360 (when (todos-item-string) ; Exclude empty lines.
4362 (not (looking-at (regexp-quote todos-nondiary-start
))))))
4364 (defun todos-done-item-p ()
4365 "Return non-nil if item at point is a done item."
4368 (looking-at todos-done-string-start
)))
4370 (defun todos-done-item-section-p ()
4371 "Return non-nil if point is in category's done items section."
4373 (or (re-search-backward (concat "^" (regexp-quote todos-category-done
))
4375 (progn (goto-char (point-min))
4376 (looking-at todos-done-string-start
)))))
4378 (defun todos-get-overlay (val)
4379 "Return the overlay at point whose `todos' property has value VAL."
4380 ;; Use overlays-in to find prefix overlays and check over two
4381 ;; positions to find done separator overlay.
4382 (let ((ovs (overlays-in (point) (1+ (point))))
4387 (when (eq (overlay-get ov
'todos
) val
)
4388 (throw 'done ov
))))))
4390 (defun todos-marked-item-p ()
4391 "Non-nil if this item begins with `todos-item-mark'.
4392 In that case, return the item's prefix overlay."
4393 (let* ((ov (todos-get-overlay 'prefix
))
4394 ;; If an item insertion command is called on a Todos file
4395 ;; before it is visited, it has no prefix overlays yet, so
4397 (pref (when ov
(overlay-get ov
'before-string
)))
4399 (string-match (concat "^" (regexp-quote todos-item-mark
))
4403 (defun todos-insert-with-overlays (item)
4404 "Insert ITEM at point and update prefix/priority number overlays."
4406 ;; Insertion pushes item down but not its prefix overlay. When the
4407 ;; overlay includes a mark, this would now mark the inserted ITEM,
4408 ;; so move it to the pushed down item.
4409 (let ((ov (todos-get-overlay 'prefix
))
4410 (marked (todos-marked-item-p)))
4412 (when marked
(move-overlay ov
(point) (point))))
4413 (todos-backward-item)
4414 (todos-prefix-overlays))
4416 (defun todos-prefix-overlays ()
4417 "Update the prefix overlays of the current category's items.
4418 The overlay's value is the string `todos-prefix' or with non-nil
4419 `todos-number-prefix' an integer in the sequence from 1 to
4420 the number of todo or done items in the category indicating the
4421 item's priority. Todo and done items are numbered independently
4424 (cat-tp (or (cdr (assoc-string
4425 (todos-current-category)
4426 (nth 2 (assoc-string todos-current-todos-file
4427 todos-top-priorities-overrides
))))
4428 todos-top-priorities
))
4431 (goto-char (point-min))
4433 (when (or (todos-date-string-matcher (line-end-position))
4434 (todos-done-string-matcher (line-end-position)))
4435 (goto-char (match-beginning 0))
4437 ;; Reset number to 1 for first done item.
4438 (when (and (eq major-mode
'todos-mode
)
4439 (looking-at todos-done-string-start
)
4440 (looking-back (concat "^"
4441 (regexp-quote todos-category-done
)
4445 (setq prefix
(concat (propertize
4446 (if todos-number-prefix
4447 (number-to-string num
)
4450 ;; Prefix of top priority items has a
4451 ;; distinct face in Todos mode.
4452 (if (and (eq major-mode
'todos-mode
)
4456 'todos-prefix-string
))
4458 (let ((ov (todos-get-overlay 'prefix
))
4459 (marked (todos-marked-item-p)))
4460 ;; Prefix overlay must be at a single position so its
4461 ;; bounds aren't changed when (re)moving an item.
4462 (unless ov
(setq ov
(make-overlay (point) (point))))
4463 (overlay-put ov
'todos
'prefix
)
4464 (overlay-put ov
'before-string
(if marked
4465 (concat todos-item-mark prefix
)
4469 ;; -----------------------------------------------------------------------------
4470 ;;; Generation of item insertion commands and key bindings
4471 ;; -----------------------------------------------------------------------------
4473 ;; Can either of these be included in Emacs? The originals are GFDL'd.
4475 ;; Reformulation of http://rosettacode.org/wiki/Power_set#Common_Lisp.
4476 (defun todos-powerset-recursive (list)
4480 (let ((recur (todos-powerset-recursive (cdr list
)))
4482 (dolist (elt recur pset
)
4483 (push (cons (car list
) elt
) pset
))
4484 (append pset recur
)))))
4486 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C.
4487 (defun todos-powerset-iterative (list)
4488 (let ((card (expt 2 (length list
)))
4493 (while (not (zerop i
))
4494 (let ((arg (pop l
)))
4496 (setq elt
(append elt
(list arg
))))
4498 (setq pset
(append pset
(list elt
)))
4502 ;; (defalias 'todos-powerset 'todos-powerset-recursive)
4503 (defalias 'todos-powerset
'todos-powerset-iterative
)
4505 (defun todos-gen-arglists (arglist)
4506 "Return list of lists of non-nil atoms produced from ARGLIST.
4507 The elements of ARGLIST may be atoms or lists."
4510 (let ((arg (pop arglist
)))
4511 (cond ((symbolp arg
)
4512 (setq arglists
(if arglists
4513 (mapcar (lambda (l) (push arg l
)) arglists
)
4514 (list (push arg arglists
)))))
4518 (if (= 1 (length arglists
))
4519 (apply (lambda (l) (push a l
)) arglists
)
4520 (mapcar (lambda (l) (push a l
)) arglists
)))
4522 (setq arglists
(mapcar 'reverse
(apply 'append
(mapc 'car arglists
))))))
4524 (defvar todos-insertion-commands-args-genlist
4525 '(diary nonmarking
(calendar date dayname
) time
(here region
))
4526 "Generator list for argument lists of item insertion commands.")
4528 (defvar todos-insertion-commands-args
4529 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist
))
4531 (setq res
(cl-remove-duplicates
4532 (apply 'append
(mapcar 'todos-powerset argslist
)) :test
'equal
))
4534 (unless (= 5 (length l
))
4535 (let ((v (make-vector 5 nil
)) elt
)
4538 (cond ((eq elt
'diary
)
4540 ((eq elt
'nonmarking
)
4542 ((or (eq elt
'calendar
)
4551 (setq l
(append v nil
))))
4552 (setq new
(append new
(list l
))))
4554 "List of all argument lists for Todos item insertion commands.")
4556 (defun todos-insertion-command-name (arglist)
4557 "Generate Todos item insertion command name from ARGLIST."
4558 (replace-regexp-in-string
4560 (replace-regexp-in-string
4562 ;; (concat "todos-item-insert-"
4563 (concat "todos-insert-item-"
4564 (mapconcat (lambda (e) (if e
(symbol-name e
))) arglist
"-")))))
4566 (defvar todos-insertion-commands-names
4568 (todos-insertion-command-name l
))
4569 todos-insertion-commands-args
)
4570 "List of names of Todos item insertion commands.")
4572 (defmacro todos-define-insertion-command
(&rest args
)
4573 "Generate item insertion command definitions from ARGS."
4574 (let ((name (intern (todos-insertion-command-name args
)))
4579 (arg4 (nth 4 args
)))
4580 `(defun ,name
(&optional arg
&rest args
)
4581 "Todos item insertion command generated from ARGS.
4582 For descriptions of the individual arguments, their values, and
4583 their relation to key bindings, see `todos-basic-insert-item'."
4584 (interactive (list current-prefix-arg
))
4585 (todos-basic-insert-item arg
',arg0
',arg1
',arg2
',arg3
',arg4
))))
4587 (defvar todos-insertion-commands
4589 (eval `(todos-define-insertion-command ,@c
)))
4590 todos-insertion-commands-args
)
4591 "List of Todos item insertion commands.")
4593 (defvar todos-insertion-commands-arg-key-list
4594 '(("diary" "y" "yy")
4595 ("nonmarking" "k" "kk")
4596 ("calendar" "c" "cc")
4598 ("dayname" "n" "nn")
4602 "List of mappings of insertion command arguments to key sequences.")
4604 (defun todos-insertion-key-bindings (map)
4605 "Generate key binding definitions for item insertion keymap MAP."
4606 (dolist (c todos-insertion-commands
)
4608 (cname (symbol-name c
)))
4610 (let ((arg (nth 0 l
))
4613 (if (string-match (concat (regexp-quote arg
) "\\_>") cname
)
4614 (setq key
(concat key key2
)))
4615 (if (string-match (concat (regexp-quote arg
) ".+") cname
)
4616 (setq key
(concat key key1
)))))
4617 todos-insertion-commands-arg-key-list
)
4618 (if (string-match (concat (regexp-quote "todos-insert-item") "\\_>") cname
)
4619 (setq key
(concat key
"i")))
4620 (define-key map key c
))))
4622 ;; -----------------------------------------------------------------------------
4623 ;;; Todos minibuffer completion
4624 ;; -----------------------------------------------------------------------------
4626 (defun todos-category-completions (&optional archive
)
4627 "Return a list of completions for `todos-read-category'.
4628 Each element of the list is a cons of a category name and the
4629 file or list of files (as short file names) it is in. The files
4630 are either the current (or if there is none, the default) todo
4631 file plus the files listed in `todos-category-completions-files',
4632 or, with non-nil ARCHIVE, the current archive file."
4633 (let* ((curfile (or todos-current-todos-file
4634 (and todos-show-current-file
4635 todos-global-current-todos-file
)
4636 (todos-absolute-file-name todos-default-todos-file
)))
4637 (files (or (unless archive
4638 (mapcar 'todos-absolute-file-name
4639 todos-category-completions-files
))
4642 ;; If file was just added, it has no category completions.
4643 (unless (zerop (buffer-size (find-buffer-visiting curfile
)))
4644 (unless (member curfile todos-archives
)
4645 (add-to-list 'files curfile
))
4646 (dolist (f files listall
)
4647 (with-current-buffer (find-file-noselect f
'nowarn
)
4648 ;; Ensure category is properly displayed in case user
4649 ;; switches to file via a non-Todos command. And if done
4650 ;; items in category are visible, keep them visible.
4651 (let ((done todos-show-with-done
))
4652 (when (> (buffer-size) (- (point-max) (point-min)))
4654 (goto-char (point-min))
4655 (setq done
(re-search-forward todos-done-string-start nil t
))))
4656 (let ((todos-show-with-done done
))
4657 (save-excursion (todos-category-select))))
4661 (goto-char (point-min))
4662 (setq listf
(read (buffer-substring-no-properties
4663 (line-beginning-position)
4664 (line-end-position)))))))
4665 (mapc (lambda (elt) (let* ((cat (car elt
))
4666 (la-elt (assoc cat listall
)))
4668 (setcdr la-elt
(append (list (cdr la-elt
))
4670 (push (cons cat f
) listall
))))
4673 (defun todos-read-file-name (prompt &optional archive mustmatch
)
4674 "Choose and return the name of a Todos file, prompting with PROMPT.
4676 Show completions with TAB or SPC; the names are shown in short
4677 form but the absolute truename is returned. With non-nil ARCHIVE
4678 return the absolute truename of a Todos archive file. With non-nil
4679 MUSTMATCH the name of an existing file must be chosen;
4680 otherwise, a new file name is allowed."
4681 (let* ((completion-ignore-case todos-completion-ignore-case
)
4682 (files (mapcar 'todos-short-file-name
4683 (if archive todos-archives todos-files
)))
4684 (file (completing-read prompt files nil mustmatch nil nil
4686 ;; If user hit RET without
4687 ;; choosing a file, default to
4688 ;; current or default file.
4689 (todos-short-file-name
4690 (or todos-current-todos-file
4691 (and todos-show-current-file
4692 todos-global-current-todos-file
)
4693 (todos-absolute-file-name
4694 todos-default-todos-file
)))
4695 ;; Trigger prompt for initial file.
4697 (unless (file-exists-p todos-directory
)
4698 (make-directory todos-directory
))
4700 (setq file
(todos-validate-name file
'file
)))
4701 (setq file
(file-truename (concat todos-directory file
4702 (if archive
".toda" ".todo"))))))
4704 (defun todos-read-category (prompt &optional match-type file
)
4705 "Choose and return a category name, prompting with PROMPT.
4706 Show completions for existing categories with TAB or SPC.
4708 The argument MATCH-TYPE specifies the matching requirements on
4709 the category name: with the value `todo' or `archive' the name
4710 must complete to that of an existing todo or archive category,
4711 respectively; with the value `add' the name must not be that of
4712 an existing category; with all other values both existing and new
4713 valid category names are accepted.
4715 With non-nil argument FILE prompt for a file and complete only
4716 against categories in that file; otherwise complete against all
4717 categories from `todos-category-completions-files'."
4718 ;; Allow SPC to insert spaces, for adding new category names.
4719 (let ((map minibuffer-local-completion-map
))
4720 (define-key map
" " nil
)
4721 (let* ((add (eq match-type
'add
))
4722 (archive (eq match-type
'archive
))
4723 (file0 (when (and file
(> (length todos-files
) 1))
4724 (todos-read-file-name (concat "Choose a" (if archive
4727 " file: ") archive t
)))
4728 (completions (unless file0
(todos-category-completions archive
)))
4729 (categories (cond (file0
4730 (with-current-buffer
4731 (find-file-noselect file0
'nowarn
)
4732 (let ((todos-current-todos-file file0
))
4734 ((and add
(not file
))
4735 (with-current-buffer
4736 (find-file-noselect todos-current-todos-file
)
4740 (completion-ignore-case todos-completion-ignore-case
)
4741 (cat (completing-read prompt categories nil
4742 (eq match-type
'todo
) nil nil
4743 ;; Unless we're adding a category via
4744 ;; todos-add-category, set default
4745 ;; for existing categories to the
4746 ;; current category of the chosen
4747 ;; file or else of the current file.
4748 (if (and categories
(not add
))
4749 (with-current-buffer
4752 todos-current-todos-file
4753 (todos-absolute-file-name
4754 todos-default-todos-file
)))
4755 (todos-current-category))
4756 ;; Trigger prompt for initial category.
4758 (catfil (cdr (assoc cat completions
)))
4759 (str "Category \"%s\" from which file (TAB for choices)? "))
4760 ;; If we do category completion and the chosen category name
4761 ;; occurs in more than one file, prompt to choose one file.
4762 (unless (or file0 add
(not catfil
))
4763 (setq file0
(file-truename
4766 (todos-absolute-file-name
4767 (let ((files (mapcar 'todos-short-file-name catfil
)))
4768 (completing-read (format str cat
) files
)))))))
4769 ;; Default to the current file.
4770 (unless file0
(setq file0 todos-current-todos-file
))
4771 ;; First validate only a name passed interactively from
4772 ;; todos-add-category, which must be of a nonexisting category.
4773 (unless (and (assoc cat categories
) (not add
))
4774 ;; Validate only against completion categories.
4775 (let ((todos-categories categories
))
4776 (setq cat
(todos-validate-name cat
'category
)))
4777 ;; When user enters a nonexisting category name by jumping or
4778 ;; moving, confirm that it should be added, then validate.
4780 (if (todos-y-or-n-p (format "Add new category \"%s\" to file \"%s\"? "
4781 cat
(todos-short-file-name file0
)))
4783 (when (assoc cat categories
)
4784 (let ((todos-categories categories
))
4785 (setq cat
(todos-validate-name cat
'category
))))
4786 ;; Restore point and narrowing after adding new
4787 ;; category, to avoid moving to beginning of file when
4788 ;; moving marked items to a new category
4789 ;; (todos-move-item).
4792 (todos-add-category file0 cat
))))
4793 ;; If we decide not to add a category, exit without returning.
4797 (defun todos-validate-name (name type
)
4798 "Prompt for new NAME for TYPE until it is valid, then return it.
4799 TYPE can be either of the symbols `file' or `category'."
4800 (let ((categories todos-categories
)
4801 (files (mapcar 'todos-short-file-name todos-files
))
4805 (cond ((string= "" name
)
4807 (cond ((eq type
'file
)
4809 "Enter a non-empty file name: "
4810 ;; Empty string passed by todos-show to
4811 ;; prompt for initial Todos file.
4812 (concat "Initial file name ["
4813 todos-initial-file
"]: ")))
4814 ((eq type
'category
)
4816 "Enter a non-empty category name: "
4817 ;; Empty string passed by todos-show to
4818 ;; prompt for initial category of a new
4820 (concat "Initial category name ["
4821 todos-initial-category
"]: "))))))
4822 ((string-match "\\`\\s-+\\'" name
)
4824 "Enter a name that does not contain only white space: "))
4825 ((and (eq type
'file
) (member name files
))
4826 (setq prompt
"Enter a non-existing file name: "))
4827 ((and (eq type
'category
) (assoc name categories
))
4828 (setq prompt
"Enter a non-existing category name: ")))
4829 (setq name
(if (or (and (eq type
'file
) files
)
4830 (and (eq type
'category
) categories
))
4831 (completing-read prompt
(cond ((eq type
'file
)
4833 ((eq type
'category
)
4835 ;; Offer default initial name.
4836 (completing-read prompt
(if (eq type
'file
)
4839 nil nil
(if (eq type
'file
)
4841 todos-initial-category
))))))
4844 ;; Adapted from calendar-read-date and calendar-date-string.
4845 (defun todos-read-date (&optional arg mo yr
)
4846 "Prompt for Gregorian date and return it in the current format.
4848 With non-nil ARG, prompt for and return only the date component
4849 specified by ARG, which can be one of these symbols:
4850 `month' (prompt for name, return name or number according to
4851 value of `calendar-date-display-form'), `day' of month, or
4852 `year'. The value of each of these components can be `*',
4853 indicating an unspecified month, day, or year.
4855 When ARG is `day', non-nil arguments MO and YR determine the
4856 number of the last the day of the month."
4857 (let (year monthname month day
4858 dayname
) ; Needed by calendar-date-display-form.
4859 (when (or (not arg
) (eq arg
'year
))
4860 (while (if (natnump year
) (< year
1) (not (eq year
'*)))
4861 (setq year
(read-from-minibuffer
4862 "Year (>0 or RET for this year or * for any year): "
4863 nil nil t nil
(number-to-string
4864 (calendar-extract-year
4865 (calendar-current-date)))))))
4866 (when (or (not arg
) (eq arg
'month
))
4867 (let* ((marray todos-month-name-array
)
4868 (mlist (append marray nil
))
4869 (mabarray todos-month-abbrev-array
)
4870 (mablist (append mabarray nil
))
4871 (completion-ignore-case todos-completion-ignore-case
))
4872 (setq monthname
(completing-read
4873 "Month name (RET for current month, * for any month): "
4874 ;; (mapcar 'list (append marray nil))
4876 (calendar-month-name (calendar-extract-month
4877 (calendar-current-date)) t
))
4878 ;; month (cdr (assoc-string
4879 ;; monthname (calendar-make-alist marray nil nil
4881 month
(1+ (- (length mlist
)
4882 (length (or (member monthname mlist
)
4883 (member monthname mablist
))))))
4884 (setq monthname
(aref mabarray
(1- month
)))))
4885 (when (or (not arg
) (eq arg
'day
))
4886 (let ((last (let ((mm (or month mo
))
4888 ;; If month is unspecified, use a month with 31
4889 ;; days for checking day of month input. Does
4890 ;; Calendar do anything special when * is
4891 ;; currently a shorter month?
4892 (if (= mm
13) (setq mm
1))
4893 ;; If year is unspecified, use a leap year to
4895 (if (eq year
'*) (setq yy
2012))
4896 (calendar-last-day-of-month mm yy
))))
4897 (while (if (natnump day
) (or (< day
1) (> day last
)) (not (eq day
'*)))
4898 (setq day
(read-from-minibuffer
4899 (format "Day (1-%d or RET for today or * for any day): "
4901 nil nil t nil
(number-to-string
4902 (calendar-extract-day
4903 (calendar-current-date))))))))
4904 ;; Stringify read values (monthname is already a string).
4905 (and year
(setq year
(if (eq year
'*)
4907 (number-to-string year
))))
4908 (and day
(setq day
(if (eq day
'*)
4910 (number-to-string day
))))
4911 (and month
(setq month
(if (eq month
'*)
4913 (number-to-string month
))))
4915 (cond ((eq arg
'year
) year
)
4918 (if (memq 'month calendar-date-display-form
)
4921 (mapconcat 'eval calendar-date-display-form
""))))
4923 (defun todos-read-dayname ()
4924 "Choose name of a day of the week with completion and return it."
4925 (let ((completion-ignore-case todos-completion-ignore-case
))
4926 (completing-read "Enter a day name: "
4927 (append calendar-day-name-array nil
)
4930 (defun todos-read-time ()
4931 "Prompt for and return a valid clock time as a string.
4933 Valid time strings are those matching `diary-time-regexp'.
4934 Typing `<return>' at the prompt returns the current time, if the
4935 user option `todos-always-add-time-string' is non-nil, otherwise
4936 the empty string (i.e., no time string)."
4939 (setq answer
(read-string "Enter a clock time: " nil nil
4940 (when todos-always-add-time-string
4941 (substring (current-time-string) 11 16))))
4942 (when (or (string= "" answer
)
4943 (string-match diary-time-regexp answer
))
4947 ;; -----------------------------------------------------------------------------
4948 ;;; Todos Categories mode tabulation and sorting
4949 ;; -----------------------------------------------------------------------------
4951 (defvar todos-categories-buffer
"*Todos Categories*"
4952 "Name of buffer in Todos Categories mode.")
4954 (defun todos-longest-category-name-length (categories)
4955 "Return the length of the longest name in list CATEGORIES."
4957 (dolist (c categories longest
)
4958 (setq longest
(max longest
(length c
))))))
4960 (defun todos-adjusted-category-label-length ()
4961 "Return adjusted length of category label button.
4962 The adjustment ensures proper tabular alignment in Todos
4964 (let* ((categories (mapcar 'car todos-categories
))
4965 (longest (todos-longest-category-name-length categories
))
4966 (catlablen (length todos-categories-category-label
))
4967 (lc-diff (- longest catlablen
)))
4968 (if (and (natnump lc-diff
) (cl-oddp lc-diff
))
4970 (max longest catlablen
))))
4972 (defun todos-padded-string (str)
4973 "Return category name or label string STR padded with spaces.
4974 The placement of the padding is determined by the value of user
4975 option `todos-categories-align'."
4976 (let* ((len (todos-adjusted-category-label-length))
4977 (strlen (length str
))
4978 (strlen-odd (eq (logand strlen
1) 1))
4979 (padding (max 0 (/ (- len strlen
) 2)))
4980 (padding-left (cond ((eq todos-categories-align
'left
) 0)
4981 ((eq todos-categories-align
'center
) padding
)
4982 ((eq todos-categories-align
'right
)
4983 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))))
4984 (padding-right (cond ((eq todos-categories-align
'left
)
4985 (if strlen-odd
(1+ (* padding
2)) (* padding
2)))
4986 ((eq todos-categories-align
'center
)
4987 (if strlen-odd
(1+ padding
) padding
))
4988 ((eq todos-categories-align
'right
) 0))))
4989 (concat (make-string padding-left
32) str
(make-string padding-right
32))))
4991 (defvar todos-descending-counts nil
4992 "List of keys for category counts sorted in descending order.")
4994 (defun todos-sort (list &optional key
)
4995 "Return a copy of LIST, possibly sorted according to KEY."
4996 (let* ((l (copy-sequence list
))
4997 (fn (if (eq key
'alpha
)
4998 (lambda (x) (upcase x
)) ; Alphabetize case insensitively.
4999 (lambda (x) (todos-get-count key x
))))
5000 ;; Keep track of whether the last sort by key was descending or
5002 (descending (member key todos-descending-counts
))
5003 (cmp (if (eq key
'alpha
)
5005 (if descending
'< '>)))
5006 (pred (lambda (s1 s2
) (let ((t1 (funcall fn
(car s1
)))
5007 (t2 (funcall fn
(car s2
))))
5008 (funcall cmp t1 t2
)))))
5010 (setq l
(sort l pred
))
5011 ;; Switch between descending and ascending sort order.
5013 (setq todos-descending-counts
5014 (delete key todos-descending-counts
))
5015 (push key todos-descending-counts
)))
5018 (defun todos-display-sorted (type)
5019 "Keep point on the TYPE count sorting button just clicked."
5020 (let ((opoint (point)))
5021 (todos-update-categories-display type
)
5022 (goto-char opoint
)))
5024 (defun todos-label-to-key (label)
5025 "Return symbol for sort key associated with LABEL."
5027 (cond ((string= label todos-categories-category-label
)
5029 ((string= label todos-categories-todo-label
)
5031 ((string= label todos-categories-diary-label
)
5033 ((string= label todos-categories-done-label
)
5035 ((string= label todos-categories-archived-label
)
5036 (setq key
'archived
)))
5039 (defun todos-insert-sort-button (label)
5040 "Insert button for displaying categories sorted by item counts.
5041 LABEL determines which type of count is sorted."
5042 (setq str
(if (string= label todos-categories-category-label
)
5043 (todos-padded-string label
)
5046 (setq end
(+ beg
(length str
)))
5047 (insert-button str
'face nil
5050 (let ((key (todos-label-to-key ,label
)))
5051 (if (and (member key todos-descending-counts
)
5054 ;; If display is alphabetical, switch back to
5055 ;; category priority order.
5056 (todos-display-sorted nil
)
5057 (setq todos-descending-counts
5058 (delete key todos-descending-counts
)))
5059 (todos-display-sorted key
)))))
5060 (setq ovl
(make-overlay beg end
))
5061 (overlay-put ovl
'face
'todos-button
))
5063 (defun todos-total-item-counts ()
5064 "Return a list of total item counts for the current file."
5065 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i
))
5066 (mapcar 'cdr todos-categories
))))
5069 (defvar todos-categories-category-number
0
5070 "Variable for numbering categories in Todos Categories mode.")
5072 (defun todos-insert-category-line (cat &optional nonum
)
5073 "Insert button with category CAT's name and item counts.
5074 With non-nil argument NONUM show only these; otherwise, insert a
5075 number in front of the button indicating the category's priority.
5076 The number and the category name are separated by the string
5077 which is the value of the user option
5078 `todos-categories-number-separator'."
5079 (let ((archive (member todos-current-todos-file todos-archives
))
5080 (num todos-categories-category-number
)
5081 (str (todos-padded-string cat
))
5083 (setq num
(1+ num
) todos-categories-category-number num
)
5086 (make-string (+ 4 (length todos-categories-number-separator
))
5088 (format " %3d%s" num todos-categories-number-separator
))
5090 (mapconcat (lambda (elt)
5092 (make-string (1+ (/ (length (car elt
)) 2)) 32) ; label
5093 (format "%3d" (todos-get-count (cdr elt
) cat
)) ; count
5094 ;; Add an extra space if label length is odd.
5095 (when (cl-oddp (length (car elt
))) " ")))
5097 (list (cons todos-categories-done-label
'done
))
5098 (list (cons todos-categories-todo-label
'todo
)
5099 (cons todos-categories-diary-label
'diary
)
5100 (cons todos-categories-done-label
'done
)
5101 (cons todos-categories-archived-label
5104 " ") ; Make highlighting on last column look better.
5105 'face
(if (and todos-skip-archived-categories
5106 (zerop (todos-get-count 'todo cat
))
5107 (zerop (todos-get-count 'done cat
))
5108 (not (zerop (todos-get-count 'archived cat
))))
5109 'todos-archived-only
5111 'action
`(lambda (button) (let ((buf (current-buffer)))
5112 (todos-jump-to-category nil
,cat
)
5113 (kill-buffer buf
))))
5114 ;; Highlight the sorted count column.
5115 (let* ((beg (+ opoint
7 (length str
)))
5117 (cond ((eq nonum
'todo
)
5118 (setq beg
(+ beg
1 (/ (length todos-categories-todo-label
) 2))))
5120 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
5121 2 (/ (length todos-categories-diary-label
) 2))))
5123 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
5124 2 (length todos-categories-diary-label
)
5125 2 (/ (length todos-categories-done-label
) 2))))
5126 ((eq nonum
'archived
)
5127 (setq beg
(+ beg
1 (length todos-categories-todo-label
)
5128 2 (length todos-categories-diary-label
)
5129 2 (length todos-categories-done-label
)
5130 2 (/ (length todos-categories-archived-label
) 2)))))
5131 (unless (= beg
(+ opoint
7 (length str
))) ; Don't highlight categories.
5132 (setq end
(+ beg
4))
5133 (setq ovl
(make-overlay beg end
))
5134 (overlay-put ovl
'face
'todos-sorted-column
)))
5137 (defun todos-display-categories ()
5138 "Prepare buffer for displaying table of categories and item counts."
5139 (unless (eq major-mode
'todos-categories-mode
)
5140 (setq todos-global-current-todos-file
5141 (or todos-current-todos-file
5142 (todos-absolute-file-name todos-default-todos-file
)))
5143 (set-window-buffer (selected-window)
5144 (set-buffer (get-buffer-create todos-categories-buffer
)))
5145 (kill-all-local-variables)
5146 (todos-categories-mode)
5147 (let ((archive (member todos-current-todos-file todos-archives
))
5150 (insert (format (concat "Category counts for Todos "
5151 (if archive
"archive" "file")
5153 (todos-short-file-name todos-current-todos-file
)))
5155 ;; Make space for the column of category numbers.
5156 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32))
5157 ;; Add the category and item count buttons (if this is the list of
5158 ;; categories in an archive, show only done item counts).
5159 (todos-insert-sort-button todos-categories-category-label
)
5162 (insert (make-string 3 32))
5163 (todos-insert-sort-button todos-categories-done-label
))
5164 (insert (make-string 3 32))
5165 (todos-insert-sort-button todos-categories-todo-label
)
5166 (insert (make-string 2 32))
5167 (todos-insert-sort-button todos-categories-diary-label
)
5168 (insert (make-string 2 32))
5169 (todos-insert-sort-button todos-categories-done-label
)
5170 (insert (make-string 2 32))
5171 (todos-insert-sort-button todos-categories-archived-label
))
5174 (defun todos-update-categories-display (sortkey)
5175 "Populate table of categories and sort by SORTKEY."
5176 (let* ((cats0 todos-categories
)
5177 (cats (todos-sort cats0 sortkey
))
5178 (archive (member todos-current-todos-file todos-archives
))
5179 (todos-categories-category-number 0)
5180 ;; Find start of Category button if we just entered Todos Categories
5182 (pt (if (eq (point) (point-max))
5185 (goto-char (next-single-char-property-change
5186 (point) 'face nil
(line-end-position))))))
5189 (delete-region (point) (point-max))
5190 ;; Fill in the table with buttonized lines, each showing a category and
5192 (mapc (lambda (cat) (todos-insert-category-line cat sortkey
))
5195 ;; Add a line showing item count totals.
5196 (insert (make-string (+ 4 (length todos-categories-number-separator
)) 32)
5197 (todos-padded-string todos-categories-totals-label
)
5201 (make-string (1+ (/ (length (car elt
)) 2)) 32)
5202 (format "%3d" (nth (cdr elt
) (todos-total-item-counts)))
5203 ;; Add an extra space if label length is odd.
5204 (when (cl-oddp (length (car elt
))) " ")))
5206 (list (cons todos-categories-done-label
2))
5207 (list (cons todos-categories-todo-label
0)
5208 (cons todos-categories-diary-label
1)
5209 (cons todos-categories-done-label
2)
5210 (cons todos-categories-archived-label
3)))
5212 ;; Put cursor on Category button initially.
5213 (if pt
(goto-char pt
))
5214 (setq buffer-read-only t
)))
5216 ;; -----------------------------------------------------------------------------
5217 ;;; Item filtering selection and display
5218 ;; -----------------------------------------------------------------------------
5220 (defvar todos-multiple-filter-files nil
5221 "List of files selected from `todos-multiple-filter-files' widget.")
5223 (defvar todos-multiple-filter-files-widget nil
5224 "Variable holding widget created by `todos-multiple-filter-files'.")
5226 (defun todos-multiple-filter-files ()
5227 "Pop to a buffer with a widget for choosing multiple filter files."
5230 (require 'wid-edit
))
5231 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
5232 (pop-to-buffer (current-buffer))
5234 (kill-all-local-variables)
5235 (widget-insert "Select files for generating the top priorities list.\n\n")
5236 (setq todos-multiple-filter-files-widget
5238 `(set ,@(mapcar (lambda (x) (list 'const x
))
5239 (mapcar 'todos-short-file-name
5240 (funcall todos-files-function
))))))
5241 (widget-insert "\n")
5242 (widget-create 'push-button
5243 :notify
(lambda (widget &rest ignore
)
5244 (setq todos-multiple-filter-files
'quit
)
5246 (exit-recursive-edit))
5249 (widget-create 'push-button
5250 :notify
(lambda (&rest ignore
)
5251 (setq todos-multiple-filter-files
5254 (concat todos-directory
5257 todos-multiple-filter-files-widget
)))
5259 (exit-recursive-edit))
5261 (use-local-map widget-keymap
)
5263 (message "Click \"Apply\" after selecting files.")
5266 (defun todos-filter-items (filter &optional new multifile
)
5267 "Display a cross-categorial list of items filtered by FILTER.
5268 The values of FILTER can be `top' for top priority items, a cons
5269 of `top' and a number passed by the caller, `diary' for diary
5270 items, or `regexp' for items matching a regular expresion entered
5271 by the user. The items can be from any categories in the current
5272 todo file or, with non-nil MULTIFILE, from several files. If NEW
5273 is nil, visit an appropriate file containing the list of filtered
5274 items; if there is no such file, or with non-nil NEW, build the
5275 list and display it.
5277 See the document strings of the commands
5278 `todos-filter-top-priorities', `todos-filter-diary-items',
5279 `todos-filter-regexp-items', and those of the corresponding
5280 multifile commands for further details."
5281 (let* ((top (eq filter
'top
))
5282 (diary (eq filter
'diary
))
5283 (regexp (eq filter
'regexp
))
5284 (buf (cond (top todos-top-priorities-buffer
)
5285 (diary todos-diary-items-buffer
)
5286 (regexp todos-regexp-items-buffer
)))
5287 (flist (if multifile
5288 (or todos-filter-files
5289 (progn (todos-multiple-filter-files)
5290 todos-multiple-filter-files
))
5291 (list todos-current-todos-file
)))
5292 (multi (> (length flist
) 1))
5293 (fname (if (equal flist
'quit
)
5294 ;; Pressed `cancel' in t-m-f-f file selection dialog.
5296 (concat todos-directory
5297 (mapconcat 'todos-short-file-name flist
"-")
5300 (regexp ".todr")))))
5301 (rxfiles (when regexp
5302 (directory-files todos-directory t
".*\\.todr$" t
)))
5303 (file-exists (or (file-exists-p fname
) rxfiles
)))
5304 (cond ((and top new
(natnump new
))
5305 (todos-filter-items-1 (cons 'top new
) flist
))
5306 ((and (not new
) file-exists
)
5307 (when (and rxfiles
(> (length rxfiles
) 1))
5308 (let ((rxf (mapcar 'todos-short-file-name rxfiles
)))
5309 (setq fname
(todos-absolute-file-name
5310 (completing-read "Choose a regexp items file: "
5313 (todos-prefix-overlays)
5314 (todos-check-filtered-items-file))
5316 (todos-filter-items-1 filter flist
)))
5317 (setq fname
(replace-regexp-in-string "-" ", "
5318 (todos-short-file-name fname
)))
5319 (rename-buffer (format (concat "%s for file" (if multi
"s" "")
5320 " \"%s\"") buf fname
))))
5322 (defun todos-filter-items-1 (filter file-list
)
5323 "Build a list of items by applying FILTER to FILE-LIST.
5324 Internal subroutine called by `todos-filter-items', which passes
5325 the values of FILTER and FILE-LIST."
5326 (let ((num (if (consp filter
) (cdr filter
) todos-top-priorities
))
5327 (buf (get-buffer-create todos-filtered-items-buffer
))
5328 (multifile (> (length file-list
) 1))
5329 regexp fname bufstr cat beg end done
)
5330 (if (null file-list
)
5331 (user-error "No files have been chosen for filtering")
5332 (with-current-buffer buf
5334 (kill-all-local-variables)
5335 (todos-filtered-items-mode))
5336 (when (eq filter
'regexp
)
5337 (setq regexp
(read-string "Enter a regular expression: ")))
5338 (save-current-buffer
5339 (dolist (f file-list
)
5340 ;; Before inserting file contents into temp buffer, save a modified
5341 ;; buffer visiting it.
5342 (let ((bf (find-buffer-visiting f
)))
5343 (when (buffer-modified-p bf
)
5344 (with-current-buffer bf
(save-buffer))))
5345 (setq fname
(todos-short-file-name f
))
5347 (when (and todos-filter-done-items
(eq filter
'regexp
))
5348 ;; If there is a corresponding archive file for the
5349 ;; Todos file, insert it first and add identifiers for
5350 ;; todos-go-to-source-item.
5351 (let ((arch (concat (file-name-sans-extension f
) ".toda")))
5352 (when (file-exists-p arch
)
5353 (insert-file-contents arch
)
5354 ;; Delete Todos archive file categories sexp.
5355 (delete-region (line-beginning-position)
5356 (1+ (line-end-position)))
5359 (when (re-search-forward
5360 (concat (if todos-filter-done-items
5361 (concat "\\(?:" todos-done-string-start
5362 "\\|" todos-date-string-start
5364 todos-date-string-start
)
5365 todos-date-pattern
"\\(?: "
5366 diary-time-regexp
"\\)?"
5367 (if todos-filter-done-items
5369 (regexp-quote todos-nondiary-end
)) "?")
5371 (insert "(archive) "))
5373 (insert-file-contents f
)
5374 ;; Delete Todos file categories sexp.
5375 (delete-region (line-beginning-position) (1+ (line-end-position)))
5377 ;; Unless the number of top priorities to show was
5378 ;; passed by the caller, the file-wide value from
5379 ;; `todos-top-priorities-overrides', if non-nil, overrides
5380 ;; `todos-top-priorities'.
5381 (unless (consp filter
)
5382 (setq fnum
(or (nth 1 (assoc f todos-top-priorities-overrides
))
5383 todos-top-priorities
)))
5384 (while (re-search-forward
5385 (concat "^" (regexp-quote todos-category-beg
)
5386 "\\(.+\\)\n") nil t
)
5387 (setq cat
(match-string 1))
5389 ;; Unless the number of top priorities to show was
5390 ;; passed by the caller, the category-wide value
5391 ;; from `todos-top-priorities-overrides', if non-nil,
5392 ;; overrides a non-nil file-wide value from
5393 ;; `todos-top-priorities-overrides' as well as
5394 ;; `todos-top-priorities'.
5395 (unless (consp filter
)
5396 (let ((cats (nth 2 (assoc f todos-top-priorities-overrides
))))
5397 (setq cnum
(or (cdr (assoc cat cats
)) fnum
))))
5398 (delete-region (match-beginning 0) (match-end 0))
5399 (setq beg
(point)) ; First item in the current category.
5400 (setq end
(if (re-search-forward
5401 (concat "^" (regexp-quote todos-category-beg
))
5407 (if (re-search-forward
5408 (concat "\n" (regexp-quote todos-category-done
))
5412 (unless (and todos-filter-done-items
(eq filter
'regexp
))
5413 ;; Leave done items.
5414 (delete-region done end
)
5416 (narrow-to-region beg end
) ; Process only current category.
5417 (goto-char (point-min))
5418 ;; Apply the filter.
5419 (cond ((eq filter
'diary
)
5421 (if (looking-at (regexp-quote todos-nondiary-start
))
5423 (todos-forward-item))))
5424 ((eq filter
'regexp
)
5426 (if (looking-at todos-item-start
)
5427 (if (string-match regexp
(todos-item-string))
5428 (todos-forward-item)
5429 (todos-remove-item))
5430 ;; Kill lines that aren't part of a todo or done
5431 ;; item (empty or todos-category-done).
5432 (delete-region (line-beginning-position)
5433 (1+ (line-end-position))))
5434 ;; If last todo item in file matches regexp and
5435 ;; there are no following done items,
5436 ;; todos-category-done string is left dangling,
5437 ;; because todos-forward-item jumps over it.
5440 (concat (regexp-quote todos-done-string
)
5442 (delete-region (point) (progn
5445 (t ; Filter top priority items.
5446 (setq num
(or cnum fnum num
))
5448 (todos-forward-item num
))))
5450 ;; Delete non-top-priority items.
5451 (unless (member filter
'(diary regexp
))
5452 (delete-region beg end
))
5453 (goto-char (point-min))
5454 ;; Add file (if using multiple files) and category tags to
5457 (when (re-search-forward
5458 (concat (if todos-filter-done-items
5459 (concat "\\(?:" todos-done-string-start
5460 "\\|" todos-date-string-start
5462 todos-date-string-start
)
5463 todos-date-pattern
"\\(?: " diary-time-regexp
5464 "\\)?" (if todos-filter-done-items
5466 (regexp-quote todos-nondiary-end
))
5470 (when (looking-at "(archive) ") (goto-char (match-end 0)))
5471 (insert (if multifile
(concat fname
":") "") cat
"]"))
5474 (setq bufstr
(buffer-string))
5475 (with-current-buffer buf
5476 (let (buffer-read-only)
5477 (insert bufstr
)))))))
5478 (set-window-buffer (selected-window) (set-buffer buf
))
5479 (todos-prefix-overlays)
5480 (goto-char (point-min)))))
5482 (defun todos-set-top-priorities (&optional arg
)
5483 "Set number of top priorities shown by `todos-filter-top-priorities'.
5484 With non-nil ARG, set the number only for the current Todos
5485 category; otherwise, set the number for all categories in the
5488 Calling this function via either of the commands
5489 `todos-set-top-priorities-in-file' or
5490 `todos-set-top-priorities-in-category' is the recommended way to
5491 set the user customizable option `todos-top-priorities-overrides'."
5492 (let* ((cat (todos-current-category))
5493 (file todos-current-todos-file
)
5494 (rules todos-top-priorities-overrides
)
5495 (frule (assoc-string file rules
))
5496 (crule (assoc-string cat
(nth 2 frule
)))
5497 (crules (nth 2 frule
))
5498 (cur (or (if arg
(cdr crule
) (nth 1 frule
))
5499 todos-top-priorities
))
5500 (prompt (if arg
(concat "Number of top priorities in this category"
5501 " (currently %d): ")
5502 (concat "Default number of top priorities per category"
5503 " in this file (currently %d): ")))
5508 (setq new
(read-number (format prompt cur0
))
5509 prompt
"Enter a non-negative number: "
5512 (append (delete crule crules
) (list (cons cat new
)))
5513 (append (list file new
) (list crules
))))
5514 (setq rules
(cons (if arg
5515 (list file cur nrule
)
5517 (delete frule rules
)))
5518 (customize-save-variable 'todos-top-priorities-overrides rules
)
5519 (todos-prefix-overlays)))
5521 (defconst todos-filtered-items-buffer
"Todos filtered items"
5522 "Initial name of buffer in Todos Filter Items mode.")
5524 (defconst todos-top-priorities-buffer
"Todos top priorities"
5525 "Buffer type string for `todos-filter-items'.")
5527 (defconst todos-diary-items-buffer
"Todos diary items"
5528 "Buffer type string for `todos-filter-items'.")
5530 (defconst todos-regexp-items-buffer
"Todos regexp items"
5531 "Buffer type string for `todos-filter-items'.")
5533 (defun todos-find-item (str)
5534 "Search for filtered item STR in its saved Todos file.
5535 Return the list (FOUND FILE CAT), where CAT and FILE are the
5536 item's category and file, and FOUND is a cons cell if the search
5537 succeeds, whose car is the start of the item in FILE and whose
5538 cdr is `done', if the item is now a done item, `changed', if its
5539 text was truncated or augmented or, for a top priority item, if
5540 its priority has changed, and `same' otherwise."
5541 (string-match (concat (if todos-filter-done-items
5542 (concat "\\(?:" todos-done-string-start
"\\|"
5543 todos-date-string-start
"\\)")
5544 todos-date-string-start
)
5545 todos-date-pattern
"\\(?: " diary-time-regexp
"\\)?"
5546 (if todos-filter-done-items
5548 (regexp-quote todos-nondiary-end
)) "?"
5549 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
5550 "\\(?1:.*\\)\\]\\).*$") str
)
5551 (let ((cat (match-string 1 str
))
5552 (file (match-string 2 str
))
5553 (archive (string= (match-string 3 str
) "(archive) "))
5554 (filcat (match-string 4 str
))
5556 (tpbuf (save-match-data (string-match "top" (buffer-name))))
5558 (setq str
(replace-match "" nil nil str
4))
5560 ;; Calculate priority of STR wrt its category.
5562 (while (search-backward filcat nil t
)
5563 (setq tpriority
(1+ tpriority
)))))
5565 (concat todos-directory
(substring file
0 -
1)
5566 (if archive
".toda" ".todo"))
5568 (concat (file-name-sans-extension
5569 todos-global-current-todos-file
) ".toda")
5570 todos-global-current-todos-file
)))
5571 (find-file-noselect file
)
5572 (with-current-buffer (find-buffer-visiting file
)
5575 (goto-char (point-min))
5576 (let ((beg (re-search-forward
5577 (concat "^" (regexp-quote (concat todos-category-beg cat
))
5580 (done (save-excursion
5582 (concat "^" (regexp-quote todos-category-done
)) nil t
)))
5583 (end (save-excursion
5584 (or (re-search-forward
5585 (concat "^" (regexp-quote todos-category-beg
))
5588 (setq found
(when (search-forward str end t
)
5589 (goto-char (match-beginning 0))))
5592 (cons found
(if (> (point) done
)
5594 (let ((cpriority 1))
5597 ;; Not top item in category.
5598 (while (> (point) (1+ beg
))
5599 (let ((opoint (point)))
5600 (todos-backward-item)
5601 ;; Can't move backward beyond
5602 ;; first item in file.
5603 (unless (= (point) opoint
)
5604 (setq cpriority
(1+ cpriority
)))))))
5605 (if (and (= tpriority cpriority
)
5606 ;; Proper substring is not the same.
5607 (string= (todos-item-string)
5611 (list found file cat
)))
5613 (defun todos-check-filtered-items-file ()
5614 "Check if filtered items file is up to date and a show suitable message."
5618 (let* ((item (todos-item-string))
5619 (found (car (todos-find-item item
))))
5620 (unless (eq (cdr found
) 'same
)
5622 (overlay-put (make-overlay (todos-item-start) (todos-item-end))
5623 'face
'todos-search
))
5624 (setq count
(1+ count
))))
5625 ;; (throw 'old (message "The marked item is not up to date.")))
5626 (todos-forward-item))
5628 (message "Filtered items file is up to date.")
5629 (message (concat "The highlighted item" (if (= count
1) " is " "s are ")
5631 ;; "\nType <return> on item for details."
5634 (defun todos-filter-items-filename ()
5635 "Return absolute file name for saving this Filtered Items buffer."
5636 (let ((bufname (buffer-name)))
5637 (string-match "\"\\([^\"]+\\)\"" bufname
)
5638 (let* ((filename-str (substring bufname
(match-beginning 1) (match-end 1)))
5639 (filename-base (replace-regexp-in-string ", " "-" filename-str
))
5640 (top-priorities (string-match "top priorities" bufname
))
5641 (diary-items (string-match "diary items" bufname
))
5642 (regexp-items (string-match "regexp items" bufname
)))
5644 (let ((prompt (concat "Enter a short identifying string"
5645 " to make this file name unique: ")))
5646 (setq filename-base
(concat filename-base
"-" (read-string prompt
)))))
5647 (concat todos-directory filename-base
5648 (cond (top-priorities ".todt")
5649 (diary-items ".tody")
5650 (regexp-items ".todr"))))))
5652 (defun todos-save-filtered-items-buffer ()
5653 "Save current Filtered Items buffer to a file.
5654 If the file already exists, overwrite it only on confirmation."
5655 (let ((filename (or (buffer-file-name) (todos-filter-items-filename))))
5656 (write-file filename t
)))
5658 ;; -----------------------------------------------------------------------------
5659 ;;; Customization groups and set functions
5660 ;; -----------------------------------------------------------------------------
5663 "Create and maintain categorized lists of todo items."
5664 :link
'(emacs-commentary-link "todos")
5668 (defgroup todos-edit nil
5669 "User options for adding and editing todo items."
5673 (defgroup todos-categories nil
5674 "User options for Todos Categories mode."
5678 (defgroup todos-filtered nil
5679 "User options for Todos Filter Items mode."
5683 (defgroup todos-display nil
5684 "User display options for Todos mode."
5688 (defgroup todos-faces nil
5689 "Faces for the Todos modes."
5693 (defun todos-set-show-current-file (symbol value
)
5694 "The :set function for user option `todos-show-current-file'."
5695 (custom-set-default symbol value
)
5697 (add-hook 'pre-command-hook
'todos-show-current-file nil t
)
5698 (remove-hook 'pre-command-hook
'todos-show-current-file t
)))
5700 (defun todos-reset-prefix (symbol value
)
5701 "The :set function for `todos-prefix' and `todos-number-prefix'."
5702 (let ((oldvalue (symbol-value symbol
))
5703 (files todos-file-buffers
))
5704 (custom-set-default symbol value
)
5705 (when (not (equal value oldvalue
))
5707 (with-current-buffer (find-file-noselect f
)
5708 ;; Activate the new setting in the current category.
5709 (save-excursion (todos-category-select)))))))
5711 (defun todos-reset-nondiary-marker (symbol value
)
5712 "The :set function for user option `todos-nondiary-marker'."
5713 (let ((oldvalue (symbol-value symbol
))
5714 (files (append todos-files todos-archives
)))
5715 (custom-set-default symbol value
)
5716 ;; Need to reset these to get font-locking right.
5717 (setq todos-nondiary-start
(nth 0 todos-nondiary-marker
)
5718 todos-nondiary-end
(nth 1 todos-nondiary-marker
)
5719 todos-date-string-start
5720 ;; See comment in defvar of `todos-date-string-start'.
5721 (concat "^\\(" (regexp-quote todos-nondiary-start
) "\\|"
5722 (regexp-quote diary-nonmarking-symbol
) "\\)?"))
5723 (when (not (equal value oldvalue
))
5725 (with-current-buffer (find-file-noselect f
)
5726 (let (buffer-read-only)
5728 (goto-char (point-min))
5730 (if (re-search-forward
5731 (concat "^\\(" todos-done-string-start
"[^][]+] \\)?"
5732 "\\(?1:" (regexp-quote (car oldvalue
))
5733 "\\)" todos-date-pattern
"\\( "
5734 diary-time-regexp
"\\)?\\(?2:"
5735 (regexp-quote (cadr oldvalue
)) "\\)")
5738 (replace-match (nth 0 value
) t t nil
1)
5739 (replace-match (nth 1 value
) t t nil
2))
5741 (todos-category-select)))))))
5743 (defun todos-reset-done-separator-string (symbol value
)
5744 "The :set function for `todos-done-separator-string'."
5745 (let ((oldvalue (symbol-value symbol
))
5746 (files todos-file-buffers
)
5747 (sep todos-done-separator
))
5748 (custom-set-default symbol value
)
5749 (when (not (equal value oldvalue
))
5751 (with-current-buffer (find-file-noselect f
)
5752 (let (buffer-read-only)
5753 (setq todos-done-separator
(todos-done-separator))
5754 (when (= 1 (length value
))
5755 (todos-reset-done-separator sep
)))
5756 (todos-category-select))))))
5758 (defun todos-reset-done-string (symbol value
)
5759 "The :set function for user option `todos-done-string'."
5760 (let ((oldvalue (symbol-value symbol
))
5761 (files (append todos-files todos-archives
)))
5762 (custom-set-default symbol value
)
5763 ;; Need to reset this to get font-locking right.
5764 (setq todos-done-string-start
5765 (concat "^\\[" (regexp-quote todos-done-string
)))
5766 (when (not (equal value oldvalue
))
5768 (with-current-buffer (find-file-noselect f
)
5769 (let (buffer-read-only)
5771 (goto-char (point-min))
5773 (if (re-search-forward
5774 (concat "^" (regexp-quote todos-nondiary-start
)
5775 "\\(" (regexp-quote oldvalue
) "\\)")
5777 (replace-match value t t nil
1)
5779 (todos-category-select)))))))
5781 (defun todos-reset-comment-string (symbol value
)
5782 "The :set function for user option `todos-comment-string'."
5783 (let ((oldvalue (symbol-value symbol
))
5784 (files (append todos-files todos-archives
)))
5785 (custom-set-default symbol value
)
5786 (when (not (equal value oldvalue
))
5788 (with-current-buffer (find-file-noselect f
)
5789 (let (buffer-read-only)
5792 (goto-char (point-min))
5794 (if (re-search-forward
5796 "\\[\\(" (regexp-quote oldvalue
) "\\): [^]]*\\]")
5798 (replace-match value t t nil
1)
5800 (todos-category-select))))))))
5802 (defun todos-reset-highlight-item (symbol value
)
5803 "The :set function for `todos-toggle-item-highlighting'."
5804 (let ((oldvalue (symbol-value symbol
))
5805 (files (append todos-files todos-archives
)))
5806 (custom-set-default symbol value
)
5807 (when (not (equal value oldvalue
))
5809 (let ((buf (find-buffer-visiting f
)))
5811 (with-current-buffer buf
5815 (hl-line-mode -
1)))))))))
5817 ;; -----------------------------------------------------------------------------
5819 ;; -----------------------------------------------------------------------------
5821 (defun todos-date-string-matcher (lim)
5822 "Search for Todos date string within LIM for font-locking."
5824 (concat todos-date-string-start
"\\(?1:" todos-date-pattern
"\\)") lim t
))
5826 (defun todos-time-string-matcher (lim)
5827 "Search for Todos time string within LIM for font-locking."
5828 (re-search-forward (concat todos-date-string-start todos-date-pattern
5829 " \\(?1:" diary-time-regexp
"\\)") lim t
))
5831 (defun todos-nondiary-marker-matcher (lim)
5832 "Search for Todos nondiary markers within LIM for font-locking."
5833 (re-search-forward (concat "^\\(?1:" (regexp-quote todos-nondiary-start
) "\\)"
5834 todos-date-pattern
"\\(?: " diary-time-regexp
5835 "\\)?\\(?2:" (regexp-quote todos-nondiary-end
) "\\)")
5838 (defun todos-diary-nonmarking-matcher (lim)
5839 "Search for diary nonmarking symbol within LIM for font-locking."
5840 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol
)
5841 "\\)" todos-date-pattern
) lim t
))
5843 (defun todos-diary-expired-matcher (lim)
5844 "Search for expired diary item date within LIM for font-locking."
5845 (when (re-search-forward (concat "^\\(?:"
5846 (regexp-quote diary-nonmarking-symbol
)
5847 "\\)?\\(?1:" todos-date-pattern
"\\) \\(?2:"
5848 diary-time-regexp
"\\)?") lim t
)
5849 (let* ((date (match-string-no-properties 1))
5850 (time (match-string-no-properties 2))
5851 ;; Function days-between requires a non-empty time string.
5852 (date-time (concat date
" " (or time
"00:00"))))
5853 (or (and (not (string-match ".+day\\|\\*" date
))
5854 (< (days-between date-time
(current-time-string)) 0))
5855 (todos-diary-expired-matcher lim
)))))
5857 (defun todos-done-string-matcher (lim)
5858 "Search for Todos done header within LIM for font-locking."
5859 (re-search-forward (concat todos-done-string-start
5863 (defun todos-comment-string-matcher (lim)
5864 "Search for Todos done comment within LIM for font-locking."
5865 (re-search-forward (concat "\\[\\(?1:" todos-comment-string
"\\):")
5868 (defun todos-category-string-matcher-1 (lim)
5869 "Search for Todos category name within LIM for font-locking.
5870 This is for fontifying category and file names appearing in Todos
5871 Filtered Items mode following done items."
5872 (if (eq major-mode
'todos-filtered-items-mode
)
5873 (re-search-forward (concat todos-done-string-start todos-date-pattern
5874 "\\(?: " diary-time-regexp
5875 ;; Use non-greedy operator to prevent
5876 ;; capturing possible following non-diary
5878 "\\)?] \\(?1:\\[.+?\\]\\)")
5881 (defun todos-category-string-matcher-2 (lim)
5882 "Search for Todos category name within LIM for font-locking.
5883 This is for fontifying category and file names appearing in Todos
5884 Filtered Items mode following todo (not done) items."
5885 (if (eq major-mode
'todos-filtered-items-mode
)
5886 (re-search-forward (concat todos-date-string-start todos-date-pattern
5887 "\\(?: " diary-time-regexp
"\\)?\\(?:"
5888 (regexp-quote todos-nondiary-end
)
5889 "\\)? \\(?1:\\[.+\\]\\)")
5892 (defvar todos-diary-expired-face
'todos-diary-expired
)
5893 (defvar todos-date-face
'todos-date
)
5894 (defvar todos-time-face
'todos-time
)
5895 (defvar todos-nondiary-face
'todos-nondiary
)
5896 (defvar todos-category-string-face
'todos-category-string
)
5897 (defvar todos-done-face
'todos-done
)
5898 (defvar todos-comment-face
'todos-comment
)
5899 (defvar todos-done-sep-face
'todos-done-sep
)
5901 (defvar todos-font-lock-keywords
5903 '(todos-nondiary-marker-matcher 1 todos-nondiary-face t
)
5904 '(todos-nondiary-marker-matcher 2 todos-nondiary-face t
)
5905 ;; diary-lib.el uses font-lock-constant-face for diary-nonmarking-symbol.
5906 '(todos-diary-nonmarking-matcher 1 font-lock-constant-face t
)
5907 '(todos-date-string-matcher 1 todos-date-face t
)
5908 '(todos-time-string-matcher 1 todos-time-face t
)
5909 '(todos-done-string-matcher 0 todos-done-face t
)
5910 '(todos-comment-string-matcher 1 todos-comment-face t
)
5911 '(todos-category-string-matcher-1 1 todos-category-string-face t t
)
5912 '(todos-category-string-matcher-2 1 todos-category-string-face t t
)
5913 '(todos-diary-expired-matcher 1 todos-diary-expired-face t
)
5914 '(todos-diary-expired-matcher 2 todos-diary-expired-face t t
)
5916 "Font-locking for Todos modes.")
5918 ;; -----------------------------------------------------------------------------
5919 ;;; Key maps and menus
5920 ;; -----------------------------------------------------------------------------
5922 (defvar todos-insertion-map
5923 (let ((map (make-keymap)))
5924 (todos-insertion-key-bindings map
)
5925 (define-key map
"p" 'todos-copy-item
)
5927 "Keymap for Todos mode item insertion commands.")
5929 (defvar todos-key-bindings-t
5931 ("Af" todos-find-archive
)
5932 ("Ac" todos-choose-archive
)
5933 ("Ad" todos-archive-done-item
)
5934 ("Cv" todos-toggle-view-done-items
)
5935 ("v" todos-toggle-view-done-items
)
5936 ("Ca" todos-add-category
)
5937 ("Cr" todos-rename-category
)
5938 ("Cg" todos-merge-category
)
5939 ("Cm" todos-move-category
)
5940 ("Ck" todos-delete-category
)
5941 ("Cts" todos-set-top-priorities-in-category
)
5942 ("Cey" todos-edit-category-diary-inclusion
)
5943 ("Cek" todos-edit-category-diary-nonmarking
)
5944 ("Fa" todos-add-file
)
5945 ("Ff" todos-find-filtered-items-file
)
5946 ("FV" todos-toggle-view-done-only
)
5947 ("V" todos-toggle-view-done-only
)
5948 ("Ftt" todos-filter-top-priorities
)
5949 ("Ftm" todos-filter-top-priorities-multifile
)
5950 ("Fts" todos-set-top-priorities-in-file
)
5951 ("Fyy" todos-filter-diary-items
)
5952 ("Fym" todos-filter-diary-items-multifile
)
5953 ("Frr" todos-filter-regexp-items
)
5954 ("Frm" todos-filter-regexp-items-multifile
)
5955 ("ee" todos-edit-item
)
5956 ("em" todos-edit-multiline-item
)
5957 ("edt" todos-edit-item-header
)
5958 ("edc" todos-edit-item-date-from-calendar
)
5959 ("eda" todos-edit-item-date-to-today
)
5960 ("edn" todos-edit-item-date-day-name
)
5961 ("edy" todos-edit-item-date-year
)
5962 ("edm" todos-edit-item-date-month
)
5963 ("edd" todos-edit-item-date-day
)
5964 ("et" todos-edit-item-time
)
5965 ("eyy" todos-edit-item-diary-inclusion
)
5966 ("eyk" todos-edit-item-diary-nonmarking
)
5967 ("ec" todos-done-item-add-edit-or-delete-comment
)
5968 ("d" todos-item-done
)
5969 ("i" ,todos-insertion-map
)
5970 ("k" todos-delete-item
)
5971 ("m" todos-move-item
)
5972 ("u" todos-item-undone
)
5973 ([remap newline
] newline-and-indent
)
5975 "List of key bindings for Todos mode only.")
5977 (defvar todos-key-bindings-t
+a
+f
5979 ("C*" todos-mark-category
)
5980 ("Cu" todos-unmark-category
)
5981 ("Fh" todos-toggle-item-header
)
5982 ("h" todos-toggle-item-header
)
5983 ("Fe" todos-edit-file
)
5984 ("FH" todos-toggle-item-highlighting
)
5985 ("H" todos-toggle-item-highlighting
)
5986 ("FN" todos-toggle-prefix-numbers
)
5987 ("N" todos-toggle-prefix-numbers
)
5988 ("PB" todos-print-buffer
)
5989 ("PF" todos-print-buffer-to-file
)
5990 ("b" todos-backward-category
)
5991 ("d" todos-item-done
)
5992 ("f" todos-forward-category
)
5993 ("j" todos-jump-to-category
)
5994 ("n" todos-next-item
)
5995 ("p" todos-previous-item
)
6000 "List of key bindings for Todos, Archive, and Filtered Items modes.")
6002 (defvar todos-key-bindings-t
+a
6004 ("Fc" todos-show-categories-table
)
6006 ("X" todos-clear-matches
)
6007 ("*" todos-toggle-mark-item
)
6009 "List of key bindings for Todos and Todos Archive modes.")
6011 (defvar todos-key-bindings-t
+f
6013 ("l" todos-lower-item-priority
)
6014 ("r" todos-raise-item-priority
)
6015 ("#" todos-set-item-priority
)
6017 "List of key bindings for Todos and Todos Filtered Items modes.")
6019 (defvar todos-mode-map
6020 (let ((map (make-keymap)))
6021 ;; Don't suppress digit keys, so they can supply prefix arguments.
6022 (suppress-keymap map
)
6023 (dolist (kb todos-key-bindings-t
)
6024 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6025 (dolist (kb todos-key-bindings-t
+a
+f
)
6026 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6027 (dolist (kb todos-key-bindings-t
+a
)
6028 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6029 (dolist (kb todos-key-bindings-t
+f
)
6030 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6032 "Todos mode keymap.")
6034 (defvar todos-archive-mode-map
6035 (let ((map (make-sparse-keymap)))
6036 (suppress-keymap map
)
6037 (dolist (kb todos-key-bindings-t
+a
+f
)
6038 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6039 (dolist (kb todos-key-bindings-t
+a
)
6040 (define-key map
(nth 0 kb
) (nth 1 kb
)))
6041 (define-key map
"a" 'todos-jump-to-archive-category
)
6042 (define-key map
"u" 'todos-unarchive-items
)
6044 "Todos Archive mode keymap.")
6046 (defvar todos-edit-mode-map
6047 (let ((map (make-sparse-keymap)))
6048 (define-key map
"\C-x\C-q" 'todos-edit-quit
)
6049 (define-key map
[remap newline
] 'newline-and-indent
)
6051 "Todos Edit mode keymap.")
6053 (defvar todos-categories-mode-map
6054 (let ((map (make-sparse-keymap)))
6055 (suppress-keymap map
)
6056 (define-key map
"c" 'todos-sort-categories-alphabetically-or-numerically
)
6057 (define-key map
"t" 'todos-sort-categories-by-todo
)
6058 (define-key map
"y" 'todos-sort-categories-by-diary
)
6059 (define-key map
"d" 'todos-sort-categories-by-done
)
6060 (define-key map
"a" 'todos-sort-categories-by-archived
)
6061 (define-key map
"#" 'todos-set-category-number
)
6062 (define-key map
"l" 'todos-lower-category
)
6063 (define-key map
"r" 'todos-raise-category
)
6064 (define-key map
"n" 'todos-next-button
)
6065 (define-key map
"p" 'todos-previous-button
)
6066 (define-key map
[tab] 'todos-next-button)
6067 (define-key map [backtab] 'todos-previous-button)
6068 (define-key map "q" 'todos-quit)
6070 "Todos Categories mode keymap.")
6072 (defvar todos-filtered-items-mode-map
6073 (let ((map (make-sparse-keymap)))
6074 (suppress-keymap map)
6075 (dolist (kb todos-key-bindings-t+a+f)
6076 (define-key map (nth 0 kb) (nth 1 kb)))
6077 (dolist (kb todos-key-bindings-t+f)
6078 (define-key map (nth 0 kb) (nth 1 kb)))
6079 (define-key map "g" 'todos-go-to-source-item)
6080 (define-key map [remap newline] 'todos-go-to-source-item)
6082 "Todos Filtered Items mode keymap.")
6084 ;; (easy-menu-define
6085 ;; todos-menu todos-mode-map "Todos Menu"
6088 ;; ["Next Item" todos-forward-item t]
6089 ;; ["Previous Item" todos-backward-item t]
6091 ;; ["Next Category" todos-forward-category t]
6092 ;; ["Previous Category" todos-backward-category t]
6093 ;; ["Jump to Category" todos-jump-to-category t]
6095 ;; ["Search Todos File" todos-search t]
6096 ;; ["Clear Highlighting on Search Matches" todos-category-done t])
6098 ;; ["List Current Categories" todos-show-categories-table t]
6099 ;; ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
6100 ;; ["Turn Item Highlighting on/off" todos-toggle-item-highlighting t]
6101 ;; ["Turn Item Numbering on/off" todos-toggle-prefix-numbers t]
6102 ;; ["Turn Item Time Stamp on/off" todos-toggle-item-header t]
6103 ;; ["View/Hide Done Items" todos-toggle-view-done-items t]
6105 ;; ["View Diary Items" todos-filter-diary-items t]
6106 ;; ["View Top Priority Items" todos-filter-top-priorities t]
6107 ;; ["View Multifile Top Priority Items" todos-filter-top-priorities-multifile t]
6109 ;; ["Print Category" todos-print-buffer t])
6111 ;; ["Insert New Item" todos-insert-item t]
6112 ;; ["Insert Item Here" todos-insert-item-here t]
6113 ;; ("More Insertion Commands")
6114 ;; ["Edit Item" todos-edit-item t]
6115 ;; ["Edit Multiline Item" todos-edit-multiline-item t]
6116 ;; ["Edit Item Header" todos-edit-item-header t]
6117 ;; ["Edit Item Date" todos-edit-item-date t]
6118 ;; ["Edit Item Time" todos-edit-item-time t]
6120 ;; ["Lower Item Priority" todos-lower-item-priority t]
6121 ;; ["Raise Item Priority" todos-raise-item-priority t]
6122 ;; ["Set Item Priority" todos-set-item-priority t]
6123 ;; ["Move (Recategorize) Item" todos-move-item t]
6124 ;; ["Delete Item" todos-delete-item t]
6125 ;; ["Undo Done Item" todos-item-undone t]
6126 ;; ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t]
6127 ;; ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t]
6128 ;; ["Mark & Hide Done Item" todos-item-done t]
6129 ;; ["Archive Done Items" todos-archive-category-done-items t]
6131 ;; ["Add New Todos File" todos-add-file t]
6132 ;; ["Add New Category" todos-add-category t]
6133 ;; ["Delete Current Category" todos-delete-category t]
6134 ;; ["Rename Current Category" todos-rename-category t]
6136 ;; ["Save Todos File" todos-save t]
6139 ;; ["Quit" todos-quit t]
6142 ;; -----------------------------------------------------------------------------
6143 ;;; Mode local variables and hook functions
6144 ;; -----------------------------------------------------------------------------
6146 (defvar todos-current-todos-file nil
6147 "Variable holding the name of the currently active Todos file.")
6149 (defun todos-show-current-file ()
6150 "Visit current instead of default Todos file with `todos-show'.
6151 This function is added to `pre-command-hook' when user option
6152 `todos-show-current-file' is set to non-nil."
6153 (setq todos-global-current-todos-file todos-current-todos-file))
6155 (defun todos-display-as-todos-file ()
6156 "Show Todos files correctly when visited from outside of Todos mode."
6157 (and (member this-command todos-visit-files-commands)
6158 (= (- (point-max) (point-min)) (buffer-size))
6159 (member major-mode '(todos-mode todos-archive-mode))
6160 (todos-category-select)))
6162 (defun todos-add-to-buffer-list ()
6163 "Add name of just visited Todos file to `todos-file-buffers'.
6164 This function is added to `find-file-hook' in Todos mode."
6165 (let ((filename (file-truename (buffer-file-name))))
6166 (when (member filename todos-files)
6167 (add-to-list 'todos-file-buffers filename))))
6169 (defun todos-update-buffer-list ()
6170 "Make current Todos mode buffer file car of `todos-file-buffers'.
6171 This function is added to `post-command-hook' in Todos mode."
6172 (let ((filename (file-truename (buffer-file-name))))
6173 (unless (eq (car todos-file-buffers) filename)
6174 (setq todos-file-buffers
6175 (cons filename (delete filename todos-file-buffers))))))
6177 (defun todos-reset-global-current-todos-file ()
6178 "Update the value of `todos-global-current-todos-file'.
6179 This becomes the latest existing Todos file or, if there is none,
6180 the value of `todos-default-todos-file'.
6181 This function is added to `kill-buffer-hook' in Todos mode."
6182 (let ((filename (file-truename (buffer-file-name))))
6183 (setq todos-file-buffers (delete filename todos-file-buffers))
6184 (setq todos-global-current-todos-file
6185 (or (car todos-file-buffers)
6186 (todos-absolute-file-name todos-default-todos-file)))))
6188 (defvar todos-categories nil
6189 "Alist of categories in the current Todos file.
6190 The elements are cons cells whose car is a category name and
6191 whose cdr is a vector of the category's item counts. These are,
6192 in order, the numbers of todo items, of todo items included in
6193 the Diary, of done items and of archived items.")
6195 (defvar todos-categories-with-marks nil
6196 "Alist of categories and number of marked items they contain.")
6198 (defvar todos-category-number 1
6199 "Variable holding the number of the current Todos category.
6200 Todos categories are numbered starting from 1.")
6202 (defvar todos-show-done-only nil
6203 "If non-nil display only done items in current category.
6204 Set by the command `todos-toggle-view-done-only' and used by
6205 `todos-category-select'.")
6207 (defun todos-reset-and-enable-done-separator ()
6208 "Show resized done items separator overlay after window change.
6209 Added to `window-configuration-change-hook' in `todos-mode'."
6210 (when (= 1 (length todos-done-separator-string))
6211 (let ((sep todos-done-separator))
6212 (setq todos-done-separator (todos-done-separator))
6213 (save-match-data (todos-reset-done-separator sep)))))
6215 ;; -----------------------------------------------------------------------------
6216 ;;; Mode definitions
6217 ;; -----------------------------------------------------------------------------
6219 (defun todos-modes-set-1 ()
6221 (setq-local font-lock-defaults '(todos-font-lock-keywords t))
6222 (setq-local tab-width todos-indent-to-here)
6223 (setq-local indent-line-function 'todos-indent)
6224 (when todos-wrap-lines
6226 (setq wrap-prefix (make-string todos-indent-to-here 32))))
6228 (defun todos-modes-set-2 ()
6230 (add-to-invisibility-spec 'todos)
6231 (setq buffer-read-only t)
6232 (setq-local hl-line-range-function (lambda() (save-excursion
6233 (when (todos-item-end)
6234 (cons (todos-item-start)
6235 (todos-item-end)))))))
6237 (defun todos-modes-set-3 ()
6239 (setq-local todos-categories (todos-set-categories))
6240 (setq-local todos-category-number 1)
6241 (add-hook 'find-file-hook 'todos-display-as-todos-file nil t))
6243 (put 'todos-mode 'mode-class 'special)
6245 (define-derived-mode todos-mode special-mode "Todos"
6246 "Major mode for displaying, navigating and editing Todo lists.
6249 ;; (easy-menu-add todos-menu)
6253 ;; Initialize todos-current-todos-file.
6254 (when (member (file-truename (buffer-file-name))
6255 (funcall todos-files-function))
6256 (setq-local todos-current-todos-file (file-truename (buffer-file-name))))
6257 (setq-local todos-show-done-only nil)
6258 (setq-local todos-categories-with-marks nil)
6259 (add-hook 'find-file-hook 'todos-add-to-buffer-list nil t)
6260 (add-hook 'post-command-hook 'todos-update-buffer-list nil t)
6261 (when todos-show-current-file
6262 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
6263 (add-hook 'window-configuration-change-hook
6264 'todos-reset-and-enable-done-separator nil t)
6265 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
6267 (put 'todos-archive-mode 'mode-class 'special)
6269 ;; If todos-mode is parent, all todos-mode key bindings appear to be
6270 ;; available in todos-archive-mode (e.g. shown by C-h m).
6271 (define-derived-mode todos-archive-mode special-mode "Todos-Arch"
6272 "Major mode for archived Todos categories.
6274 \\{todos-archive-mode-map}"
6278 (setq-local todos-current-todos-file (file-truename (buffer-file-name)))
6279 (setq-local todos-show-done-only t))
6281 (defun todos-mode-external-set ()
6283 (setq-local todos-current-todos-file todos-global-current-todos-file)
6284 (let ((cats (with-current-buffer
6285 ;; Can't use find-buffer-visiting when
6286 ;; `todos-show-categories-table' is called on first
6287 ;; invocation of `todos-show', since there is then
6288 ;; no buffer visiting the current file.
6289 (find-file-noselect todos-current-todos-file 'nowarn)
6290 (or todos-categories
6291 ;; In Todos Edit mode todos-categories is now nil
6292 ;; since it uses same buffer as Todos mode but
6293 ;; doesn't have the latter's local variables.
6295 (goto-char (point-min))
6296 (read (buffer-substring-no-properties
6297 (line-beginning-position)
6298 (line-end-position))))))))
6299 (setq-local todos-categories cats)))
6301 (define-derived-mode todos-edit-mode text-mode "Todos-Ed"
6302 "Major mode for editing multiline Todo items.
6304 \\{todos-edit-mode-map}"
6306 (todos-mode-external-set)
6307 (setq buffer-read-only nil))
6309 (put 'todos-categories-mode 'mode-class 'special)
6311 (define-derived-mode todos-categories-mode special-mode "Todos-Cats"
6312 "Major mode for displaying and editing Todos categories.
6314 \\{todos-categories-mode-map}"
6315 (todos-mode-external-set))
6317 (put 'todos-filtered-items-mode 'mode-class 'special)
6319 (define-derived-mode todos-filtered-items-mode special-mode "Todos-Fltr"
6320 "Mode for displaying and reprioritizing top priority Todos.
6322 \\{todos-filtered-items-mode-map}"
6324 (todos-modes-set-2))
6326 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
6327 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
6328 (add-to-list 'auto-mode-alist '("\\.tod[tyr]\\'" . todos-filtered-items-mode))
6330 ;; -----------------------------------------------------------------------------
6333 ;;; todos.el ends here
6335 ;;; necessitated adaptations to diary-lib.el
6337 (defun diary-goto-entry (button)
6338 "Jump to the diary entry for the BUTTON at point."
6339 (let* ((locator (button-get button 'locator))
6340 (marker (car locator))
6341 markbuf file opoint)
6342 ;; If marker pointing to diary location is valid, use that.
6343 (if (and marker (setq markbuf (marker-buffer marker)))
6345 (pop-to-buffer markbuf)
6346 (when (eq major-mode 'todos-mode) (widen))
6347 (goto-char (marker-position marker))
6348 (when (eq major-mode 'todos-mode)
6349 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
6350 "\\(.*\\)\n") nil t)
6351 (todos-category-number (match-string 1))
6352 (todos-category-select)
6353 (goto-char (marker-position marker))))
6354 ;; Marker is invalid (eg buffer has been killed).
6355 (or (and (setq file (cadr locator))
6356 (file-exists-p file)
6357 (find-file-other-window file)
6359 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
6360 (when (eq major-mode 'todos-mode) (widen))
6361 (goto-char (point-min))
6362 (when (re-search-forward (format "%s.*\\(%s\\)"
6363 (regexp-quote (nth 2 locator))
6364 (regexp-quote (nth 3 locator)))
6366 (goto-char (match-beginning 1))
6367 (when (eq major-mode 'todos-mode)
6368 (setq opoint (point))
6369 (re-search-backward (concat "^"
6370 (regexp-quote todos-category-beg)
6373 (todos-category-number (match-string 1))
6374 (todos-category-select)
6375 (goto-char opoint)))))
6376 (message "Unable to locate this diary entry")))))