]> code.delx.au - gnu-emacs/blob - lisp/calendar/todos.el
* calendar/todos.el (todos-forward-category): Fix typo.
[gnu-emacs] / lisp / calendar / todos.el
1 ;;; Todos.el --- facilities for making and maintaining Todo lists
2
3 ;; Copyright (C) 1997, 1999, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Oliver Seidel <privat@os10000.net>
6 ;; Stephen Berman <stephen.berman@gmx.net>
7 ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
8 ;; Created: 2 Aug 1997
9 ;; Keywords: calendar, todo
10
11 ;; This file is [not yet] part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (require 'diary-lib)
31 ;; For remove-duplicates in todos-insertion-commands-args.
32 (eval-when-compile (require 'cl))
33
34 ;; ---------------------------------------------------------------------------
35 ;;; User options
36
37 (defgroup todos nil
38 "Create and maintain categorized lists of todo items."
39 :link '(emacs-commentary-link "todos")
40 :version "24.2"
41 :group 'calendar)
42
43 (defcustom todos-files-directory (locate-user-emacs-file "todos/")
44 "Directory where user's Todos files are saved."
45 :type 'directory
46 :group 'todos)
47
48 (defun todos-files (&optional archives)
49 "Default value of `todos-files-function'.
50 This returns the case-insensitive alphabetically sorted list of
51 file truenames in `todos-files-directory' with the extension
52 \".todo\". With non-nil ARCHIVES return the list of archive file
53 truenames (those with the extension \".toda\")."
54 (let ((files (if (file-exists-p todos-files-directory)
55 (mapcar 'file-truename
56 (directory-files todos-files-directory t
57 (if archives "\.toda$" "\.todo$") t)))))
58 (sort files (lambda (s1 s2) (let ((cis1 (upcase s1))
59 (cis2 (upcase s2)))
60 (string< cis1 cis2))))))
61
62 (defcustom todos-files-function 'todos-files
63 "Function returning the value of the variable `todos-files'.
64 This function should take an optional argument that, if non-nil,
65 makes it return the value of the variable `todos-archives'."
66 :type 'function
67 :group 'todos)
68
69 (defun todos-short-file-name (file)
70 "Return short form of Todos FILE.
71 This lacks the extension and directory components."
72 (file-name-sans-extension (file-name-nondirectory file)))
73
74 (defcustom todos-default-todos-file (car (funcall todos-files-function))
75 "Todos file visited by first session invocation of `todos-show'."
76 :type `(radio ,@(mapcar (lambda (f) (list 'const f))
77 (mapcar 'todos-short-file-name
78 (funcall todos-files-function))))
79 :group 'todos)
80
81 ;; FIXME: is there a better alternative to this?
82 (defun todos-reevaluate-default-file-defcustom ()
83 "Reevaluate defcustom of `todos-default-todos-file'.
84 Called after adding or deleting a Todos file."
85 (eval (defcustom todos-default-todos-file (car (funcall todos-files-function))
86 "Todos file visited by first session invocation of `todos-show'."
87 :type `(radio ,@(mapcar (lambda (f) (list 'const f))
88 (mapcar 'todos-short-file-name
89 (funcall todos-files-function))))
90 :group 'todos)))
91
92 (defcustom todos-show-current-file t
93 "Non-nil to make `todos-show' visit the current Todos file.
94 Otherwise, `todos-show' always visits `todos-default-todos-file'."
95 :type 'boolean
96 :initialize 'custom-initialize-default
97 :set 'todos-set-show-current-file
98 :group 'todos)
99
100 (defun todos-set-show-current-file (symbol value)
101 "The :set function for user option `todos-show-current-file'."
102 (custom-set-default symbol value)
103 (if value
104 (add-hook 'pre-command-hook 'todos-show-current-file nil t)
105 (remove-hook 'pre-command-hook 'todos-show-current-file t)))
106
107 (defcustom todos-visit-files-commands (list 'find-file 'dired-find-file)
108 "List of file finding commands for `todos-display-as-todos-file'.
109 Invoking these commands to visit a Todos or Todos Archive file
110 calls `todos-show' or `todos-show-archive', so that the file is
111 displayed correctly."
112 :type '(repeat function)
113 :group 'todos)
114
115 (defcustom todos-initial-file "Todo"
116 "Default file name offered on adding first Todos file."
117 :type 'string
118 :group 'todos)
119
120 (defcustom todos-initial-category "Todo"
121 "Default category name offered on initializing a new Todos file."
122 :type 'string
123 :group 'todos)
124
125 (defcustom todos-display-categories-first nil
126 "Non-nil to display category list on first visit to a Todos file."
127 :type 'boolean
128 :group 'todos)
129
130 (defcustom todos-completion-ignore-case nil
131 "Non-nil means case is ignored by `todos-read-*' functions."
132 :type 'boolean
133 :group 'todos)
134
135 (defcustom todos-undo-item-omit-comment 'ask
136 "Whether to omit done item comment on undoing the item.
137 Nil means never omit the comment, t means always omit it, `ask'
138 means prompt user and omit comment only on confirmation."
139 :type '(choice (const :tag "Never" nil)
140 (const :tag "Always" t)
141 (const :tag "Ask" ask))
142 :group 'todos)
143
144 (defcustom todos-print-function 'ps-print-buffer-with-faces
145 "Function called to print buffer content; see `todos-print'."
146 :type 'symbol
147 :group 'todos)
148
149 (defcustom todos-todo-mode-date-time-regexp
150 (concat "\\(?1:[0-9]\\{4\\}\\)-\\(?2:[0-9]\\{2\\}\\)-"
151 "\\(?3:[0-9]\\{2\\}\\) \\(?4:[0-9]\\{2\\}:[0-9]\\{2\\}\\)")
152 "Regexp matching legacy todo-mode.el item date-time strings.
153 In order for `todos-convert-legacy-files' to correctly convert this
154 string to the current Todos format, the regexp must contain four
155 explicitly numbered groups (see `(elisp) Regexp Backslash'),
156 where group 1 matches a string for the year, group 2 a string for
157 the month, group 3 a string for the day and group 4 a string for
158 the time. The default value converts date-time strings built
159 using the default value of `todo-time-string-format' from
160 todo-mode.el."
161 :type 'regexp
162 :group 'todos)
163
164 ;; ---------------------------------------------------------------------------
165 ;;; Todos mode display options
166
167 (defgroup todos-mode-display nil
168 "User display options for Todos mode."
169 :version "24.2"
170 :group 'todos)
171
172 (defcustom todos-prefix ""
173 "String prefixed to todo items for visual distinction."
174 :type 'string
175 :initialize 'custom-initialize-default
176 :set 'todos-reset-prefix
177 :group 'todos-mode-display)
178
179 (defcustom todos-number-priorities t
180 "Non-nil to prefix items with consecutively increasing integers.
181 These reflect the priorities of the items in each category."
182 :type 'boolean
183 :initialize 'custom-initialize-default
184 :set 'todos-reset-prefix
185 :group 'todos-mode-display)
186
187 (defun todos-reset-prefix (symbol value)
188 "The :set function for `todos-prefix' and `todos-number-priorities'."
189 (let ((oldvalue (symbol-value symbol))
190 (files (append todos-files todos-archives)))
191 (custom-set-default symbol value)
192 (when (not (equal value oldvalue))
193 (dolist (f files)
194 (with-current-buffer (find-file-noselect f)
195 (save-window-excursion
196 (todos-show)
197 (save-excursion
198 (widen)
199 (goto-char (point-min))
200 (while (not (eobp))
201 (remove-overlays (point) (point)); 'before-string prefix)
202 (forward-line)))
203 ;; Activate the new setting (save-restriction does not help).
204 (save-excursion (todos-category-select))))))))
205
206 (defcustom todos-done-separator-string "_"
207 "String for generating `todos-done-separator'.
208
209 If the string consists of a single character,
210 `todos-done-separator' will be the string made by repeating this
211 character for the width of the window, and the length is
212 automatically recalculated when the window width changes. If the
213 string consists of more (or less) than one character, it will be
214 the value of `todos-done-separator'."
215 :type 'string
216 :initialize 'custom-initialize-default
217 :set 'todos-reset-done-separator-string
218 :group 'todos-mode-display)
219
220 (defun todos-reset-done-separator-string (symbol value)
221 "The :set function for `todos-done-separator-string'."
222 (let ((oldvalue (symbol-value symbol))
223 (files todos-file-buffers)
224 (sep todos-done-separator))
225 (custom-set-default symbol value)
226 (setq todos-done-separator (todos-done-separator))
227 (when (= 1 (length value))
228 (todos-reset-done-separator sep))))
229
230 (defcustom todos-done-string "DONE "
231 "Identifying string appended to the front of done todos items."
232 :type 'string
233 :initialize 'custom-initialize-default
234 :set 'todos-reset-done-string
235 :group 'todos-mode-display)
236
237 (defun todos-reset-done-string (symbol value)
238 "The :set function for user option `todos-done-string'."
239 (let ((oldvalue (symbol-value symbol))
240 (files (append todos-files todos-archives)))
241 (custom-set-default symbol value)
242 ;; Need to reset this to get font-locking right.
243 (setq todos-done-string-start
244 (concat "^\\[" (regexp-quote todos-done-string)))
245 (when (not (equal value oldvalue))
246 (dolist (f files)
247 (with-current-buffer (find-file-noselect f)
248 (let (buffer-read-only)
249 (widen)
250 (goto-char (point-min))
251 (while (not (eobp))
252 (if (re-search-forward
253 (concat "^" (regexp-quote todos-nondiary-start)
254 "\\(" (regexp-quote oldvalue) "\\)")
255 nil t)
256 (replace-match value t t nil 1)
257 (forward-line)))
258 (todos-category-select)))))))
259
260 (defcustom todos-comment-string "COMMENT"
261 "String inserted before optional comment appended to done item."
262 :type 'string
263 :initialize 'custom-initialize-default
264 :set 'todos-reset-comment-string
265 :group 'todos-mode-display)
266
267 (defun todos-reset-comment-string (symbol value)
268 "The :set function for user option `todos-comment-string'."
269 (let ((oldvalue (symbol-value symbol))
270 (files (append todos-files todos-archives)))
271 (custom-set-default symbol value)
272 (when (not (equal value oldvalue))
273 (dolist (f files)
274 (with-current-buffer (find-file-noselect f)
275 (let (buffer-read-only)
276 (save-excursion
277 (widen)
278 (goto-char (point-min))
279 (while (not (eobp))
280 (if (re-search-forward
281 (concat
282 "\\[\\(" (regexp-quote oldvalue) "\\): [^]]*\\]")
283 nil t)
284 (replace-match value t t nil 1)
285 (forward-line)))
286 (todos-category-select))))))))
287
288 (defcustom todos-show-with-done nil
289 "Non-nil to display done items in all categories."
290 :type 'boolean
291 :group 'todos-mode-display)
292
293 (defun todos-mode-line-control (cat)
294 "Return a mode line control for Todos buffers.
295 Argument CAT is the name of the current Todos category.
296 This function is the value of the user variable
297 `todos-mode-line-function'."
298 (let ((file (todos-short-file-name todos-current-todos-file)))
299 (format "%s category %d: %s" file todos-category-number cat)))
300
301 (defcustom todos-mode-line-function 'todos-mode-line-control
302 "Function that returns a mode line control for Todos buffers.
303 The function expects one argument holding the name of the current
304 Todos category. The resulting control becomes the local value of
305 `mode-line-buffer-identification' in each Todos buffer."
306 :type 'function
307 :group 'todos-mode-display)
308
309 (defcustom todos-skip-archived-categories nil
310 "Non-nil to skip categories with only archived items when browsing.
311
312 Moving by category todos or archive file (with
313 \\[todos-forward-category] and \\[todos-backward-category]) skips
314 categories that contain only archived items. Other commands
315 still recognize these categories. In Todos Categories
316 mode (reached with \\[todos-display-categories]) these categories
317 shown in `todos-archived-only' face and clicking them in Todos
318 Categories mode visits the archived categories."
319 :type 'boolean
320 :group 'todos-mode-display)
321
322 (defcustom todos-highlight-item nil
323 "Non-nil means highlight items at point."
324 :type 'boolean
325 :initialize 'custom-initialize-default
326 :set 'todos-reset-highlight-item
327 :group 'todos-mode-display)
328
329 (defun todos-reset-highlight-item (symbol value)
330 "The :set function for `todos-highlight-item'."
331 (let ((oldvalue (symbol-value symbol))
332 (files (append todos-files todos-archives)))
333 (custom-set-default symbol value)
334 (when (not (equal value oldvalue))
335 (dolist (f files)
336 (let ((buf (find-buffer-visiting f)))
337 (when buf
338 (with-current-buffer buf
339 (require 'hl-line)
340 (if value
341 (hl-line-mode 1)
342 (hl-line-mode -1)))))))))
343
344 (defcustom todos-wrap-lines t
345 "Non-nil to wrap long lines via `todos-line-wrapping-function'."
346 :group 'todos-mode-display
347 :type 'boolean)
348
349 (defcustom todos-line-wrapping-function 'todos-wrap-and-indent
350 "Line wrapping function used with non-nil `todos-wrap-lines'."
351 :group 'todos-mode-display
352 :type 'function)
353
354 (defun todos-wrap-and-indent ()
355 "Use word wrapping on long lines and indent with a wrap prefix.
356 The amount of indentation is given by user option
357 `todos-indent-to-here'."
358 (set (make-local-variable 'word-wrap) t)
359 (set (make-local-variable 'wrap-prefix) (make-string todos-indent-to-here 32))
360 (unless (member '(continuation) fringe-indicator-alist)
361 (push '(continuation) fringe-indicator-alist)))
362
363 ;; FIXME: :set function to refill items with hard newlines and to immediately
364 ;; update wrapped prefix display
365 (defcustom todos-indent-to-here 6
366 "Number of spaces `todos-line-wrapping-function' indents to."
367 :type '(integer :validate
368 (lambda (widget)
369 (unless (> (widget-value widget) 0)
370 (widget-put widget :error
371 "Invalid value: must be a positive integer")
372 widget)))
373 :group 'todos)
374
375 (defun todos-indent ()
376 "Indent from point to `todos-indent-to-here'."
377 (indent-to todos-indent-to-here todos-indent-to-here))
378
379 ;; ---------------------------------------------------------------------------
380 ;;; Item insertion options
381
382 (defgroup todos-item-insertion nil
383 "User options for adding new todo items."
384 :version "24.2"
385 :group 'todos)
386
387 (defcustom todos-include-in-diary nil
388 "Non-nil to allow new Todo items to be included in the diary."
389 :type 'boolean
390 :group 'todos-item-insertion)
391
392 (defcustom todos-diary-nonmarking nil
393 "Non-nil to insert new Todo diary items as nonmarking by default.
394 This appends `diary-nonmarking-symbol' to the front of an item on
395 insertion provided it doesn't begin with `todos-nondiary-marker'."
396 :type 'boolean
397 :group 'todos-item-insertion)
398
399 (defcustom todos-nondiary-marker '("[" "]")
400 "List of strings surrounding item date to block diary inclusion.
401 The first string is inserted before the item date and must be a
402 non-empty string that does not match a diary date in order to
403 have its intended effect. The second string is inserted after
404 the diary date."
405 :type '(list string string)
406 :group 'todos-item-insertion
407 :initialize 'custom-initialize-default
408 :set 'todos-reset-nondiary-marker)
409
410 (defun todos-reset-nondiary-marker (symbol value)
411 "The :set function for user option `todos-nondiary-marker'."
412 (let ((oldvalue (symbol-value symbol))
413 (files (append todos-files todos-archives)))
414 (custom-set-default symbol value)
415 ;; Need to reset these to get font-locking right.
416 (setq todos-nondiary-start (nth 0 todos-nondiary-marker)
417 todos-nondiary-end (nth 1 todos-nondiary-marker)
418 todos-date-string-start
419 ;; See comment in defvar of `todos-date-string-start'.
420 (concat "^\\(" (regexp-quote todos-nondiary-start) "\\|"
421 (regexp-quote diary-nonmarking-symbol) "\\)?"))
422 (when (not (equal value oldvalue))
423 (dolist (f files)
424 (with-current-buffer (find-file-noselect f)
425 (let (buffer-read-only)
426 (widen)
427 (goto-char (point-min))
428 (while (not (eobp))
429 (if (re-search-forward
430 (concat "^\\(" todos-done-string-start "[^][]+] \\)?"
431 "\\(?1:" (regexp-quote (car oldvalue))
432 "\\)" todos-date-pattern "\\( "
433 diary-time-regexp "\\)?\\(?2:"
434 (regexp-quote (cadr oldvalue)) "\\)")
435 nil t)
436 (progn
437 (replace-match (nth 0 value) t t nil 1)
438 (replace-match (nth 1 value) t t nil 2))
439 (forward-line)))
440 (todos-category-select)))))))
441
442 (defcustom todos-always-add-time-string nil
443 "Non-nil adds current time to a new item's date header by default.
444 When the Todos insertion commands have a non-nil \"maybe-notime\"
445 argument, this reverses the effect of
446 `todos-always-add-time-string': if t, these commands omit the
447 current time, if nil, they include it."
448 :type 'boolean
449 :group 'todos-item-insertion)
450
451 (defcustom todos-use-only-highlighted-region t
452 "Non-nil to enable inserting only highlighted region as new item."
453 :type 'boolean
454 :group 'todos-item-insertion)
455
456 ;; ---------------------------------------------------------------------------
457 ;;; Todos Filter Items mode options
458
459 (defgroup todos-filtered nil
460 "User options for Todos Filter Items mode."
461 :version "24.2"
462 :group 'todos)
463
464 (defcustom todos-filtered-items-buffer "Todos filtered items"
465 "Initial name of buffer in Todos Filter Items mode."
466 :type 'string
467 :group 'todos-filtered)
468
469 (defcustom todos-top-priorities-buffer "Todos top priorities"
470 "Buffer type string for `todos-filtered-buffer-name'."
471 :type 'string
472 :group 'todos-filtered)
473
474 (defcustom todos-diary-items-buffer "Todos diary items"
475 "Buffer type string for `todos-filtered-buffer-name'."
476 :type 'string
477 :group 'todos-filtered)
478
479 (defcustom todos-regexp-items-buffer "Todos regexp items"
480 "Buffer type string for `todos-filtered-buffer-name'."
481 :type 'string
482 :group 'todos-filtered)
483
484 (defcustom todos-priorities-rules nil
485 "List of rules giving how many items `todos-top-priorities' shows.
486 This variable should be set interactively by
487 `\\[todos-set-top-priorities-in-file]' or
488 `\\[todos-set-top-priorities-in-category]'.
489
490 Each rule is a list of the form (FILE NUM ALIST), where FILE is a
491 member of `todos-files', NUM is a number specifying the default
492 number of top priority items for each category in that file, and
493 ALIST, when non-nil, consists of conses of a category name in
494 FILE and a number specifying the default number of top priority
495 items in that category, which overrides NUM."
496 :type 'list
497 :group 'todos-filtered)
498
499 (defcustom todos-show-priorities 1
500 "Default number of top priorities shown by `todos-top-priorities'."
501 :type 'integer
502 :group 'todos-filtered)
503
504 (defcustom todos-filter-files nil
505 "List of default files for multifile item filtering."
506 :type `(set ,@(mapcar (lambda (f) (list 'const f))
507 (mapcar 'todos-short-file-name
508 (funcall todos-files-function))))
509 :group 'todos-filtered)
510
511 ;; FIXME: is there a better alternative to this?
512 (defun todos-reevaluate-filter-files-defcustom ()
513 "Reevaluate defcustom of `todos-filter-files'.
514 Called after adding or deleting a Todos file."
515 (eval (defcustom todos-filter-files nil
516 "List of files for multifile item filtering."
517 :type `(set ,@(mapcar (lambda (f) (list 'const f))
518 (mapcar 'todos-short-file-name
519 (funcall todos-files-function))))
520 :group 'todos)))
521
522 (defcustom todos-filter-done-items nil
523 "Non-nil to include done items when processing regexp filters.
524 Done items from corresponding archive files are also included."
525 :type 'boolean
526 :group 'todos-filtered)
527
528 ;; ---------------------------------------------------------------------------
529 ;;; Todos Categories mode options
530
531 (defgroup todos-categories nil
532 "User options for Todos Categories mode."
533 :version "24.2"
534 :group 'todos)
535
536 (defcustom todos-categories-category-label "Category"
537 "Category button label in Todos Categories mode."
538 :type 'string
539 :group 'todos-categories)
540
541 (defcustom todos-categories-todo-label "Todo"
542 "Todo button label in Todos Categories mode."
543 :type 'string
544 :group 'todos-categories)
545
546 (defcustom todos-categories-diary-label "Diary"
547 "Diary button label in Todos Categories mode."
548 :type 'string
549 :group 'todos-categories)
550
551 (defcustom todos-categories-done-label "Done"
552 "Done button label in Todos Categories mode."
553 :type 'string
554 :group 'todos-categories)
555
556 (defcustom todos-categories-archived-label "Archived"
557 "Archived button label in Todos Categories mode."
558 :type 'string
559 :group 'todos-categories)
560
561 (defcustom todos-categories-totals-label "Totals"
562 "String to label total item counts in Todos Categories mode."
563 :type 'string
564 :group 'todos-categories)
565
566 (defcustom todos-categories-number-separator " | "
567 "String between number and category in Todos Categories mode.
568 This separates the number from the category name in the default
569 categories display according to priority."
570 :type 'string
571 :group 'todos-categories)
572
573 (defcustom todos-categories-align 'center
574 "Alignment of category names in Todos Categories mode."
575 :type '(radio (const left) (const center) (const right))
576 :group 'todos-categories)
577
578 ;; ---------------------------------------------------------------------------
579 ;;; Faces and font-lock matcher functions
580
581 (defgroup todos-faces nil
582 "Faces for the Todos modes."
583 :version "24.2"
584 :group 'todos)
585
586 (defface todos-prefix-string
587 ;; '((t :inherit font-lock-constant-face))
588 '((((class grayscale) (background light))
589 (:foreground "LightGray" :weight bold :underline t))
590 (((class grayscale) (background dark))
591 (:foreground "Gray50" :weight bold :underline t))
592 (((class color) (min-colors 88) (background light)) (:foreground "dark cyan"))
593 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
594 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
595 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
596 (((class color) (min-colors 8)) (:foreground "magenta"))
597 (t (:weight bold :underline t)))
598 "Face for Todos prefix string."
599 :group 'todos-faces)
600
601 (defface todos-mark
602 ;; '((t :inherit font-lock-warning-face))
603 '((((class color)
604 (min-colors 88)
605 (background light))
606 (:weight bold :foreground "Red1"))
607 (((class color)
608 (min-colors 88)
609 (background dark))
610 (:weight bold :foreground "Pink"))
611 (((class color)
612 (min-colors 16)
613 (background light))
614 (:weight bold :foreground "Red1"))
615 (((class color)
616 (min-colors 16)
617 (background dark))
618 (:weight bold :foreground "Pink"))
619 (((class color)
620 (min-colors 8))
621 (:foreground "red"))
622 (t
623 (:weight bold :inverse-video t)))
624 "Face for marks on Todos items."
625 :group 'todos-faces)
626
627 (defface todos-button
628 ;; '((t :inherit widget-field))
629 '((((type tty))
630 (:foreground "black" :background "yellow3"))
631 (((class grayscale color)
632 (background light))
633 (:background "gray85"))
634 (((class grayscale color)
635 (background dark))
636 (:background "dim gray"))
637 (t
638 (:slant italic)))
639 "Face for buttons in todos-display-categories."
640 :group 'todos-faces)
641
642 (defface todos-sorted-column
643 '((((type tty))
644 (:inverse-video t))
645 (((class color)
646 (background light))
647 (:background "grey85"))
648 (((class color)
649 (background dark))
650 (:background "grey85" :foreground "grey10"))
651 (t
652 (:background "gray")))
653 "Face for buttons in todos-display-categories."
654 :group 'todos-faces)
655
656 (defface todos-archived-only
657 ;; '((t (:inherit (shadow))))
658 '((((class color)
659 (background light))
660 (:foreground "grey50"))
661 (((class color)
662 (background dark))
663 (:foreground "grey70"))
664 (t
665 (:foreground "gray")))
666 "Face for archived-only categories in todos-display-categories."
667 :group 'todos-faces)
668
669 (defface todos-search
670 ;; '((t :inherit match))
671 '((((class color)
672 (min-colors 88)
673 (background light))
674 (:background "yellow1"))
675 (((class color)
676 (min-colors 88)
677 (background dark))
678 (:background "RoyalBlue3"))
679 (((class color)
680 (min-colors 8)
681 (background light))
682 (:foreground "black" :background "yellow"))
683 (((class color)
684 (min-colors 8)
685 (background dark))
686 (:foreground "white" :background "blue"))
687 (((type tty)
688 (class mono))
689 (:inverse-video t))
690 (t
691 (:background "gray")))
692 "Face for matches found by todos-search."
693 :group 'todos-faces)
694
695 (defface todos-diary-expired
696 ;; '((t :inherit font-lock-warning-face))
697 '((((class color)
698 (min-colors 16))
699 (:weight bold :foreground "DarkOrange"))
700 (((class color))
701 (:weight bold :foreground "yellow"))
702 (t
703 (:weight bold)))
704 "Face for expired dates of diary items."
705 :group 'todos-faces)
706 (defvar todos-diary-expired-face 'todos-diary-expired)
707
708 (defface todos-date
709 '((t :inherit diary))
710 "Face for the date string of a Todos item."
711 :group 'todos-faces)
712 (defvar todos-date-face 'todos-date)
713
714 (defface todos-time
715 '((t :inherit diary-time))
716 "Face for the time string of a Todos item."
717 :group 'todos-faces)
718 (defvar todos-time-face 'todos-time)
719
720 (defface todos-done
721 ;; '((t :inherit font-lock-comment-face))
722 '((((class grayscale)
723 (background light))
724 (:slant italic :weight bold :foreground "DimGray"))
725 (((class grayscale)
726 (background dark))
727 (:slant italic :weight bold :foreground "LightGray"))
728 (((class color)
729 (min-colors 88)
730 (background light))
731 (:foreground "Firebrick"))
732 (((class color)
733 (min-colors 88)
734 (background dark))
735 (:foreground "chocolate1"))
736 (((class color)
737 (min-colors 16)
738 (background light))
739 (:foreground "red"))
740 (((class color)
741 (min-colors 16)
742 (background dark))
743 (:foreground "red1"))
744 (((class color)
745 (min-colors 8)
746 (background light))
747 (:foreground "red"))
748 (((class color)
749 (min-colors 8)
750 (background dark))
751 (:foreground "yellow"))
752 (t
753 (:slant italic :weight bold)))
754 "Face for done Todos item header string."
755 :group 'todos-faces)
756 (defvar todos-done-face 'todos-done)
757
758 (defface todos-comment
759 '((t :inherit todos-done))
760 "Face for comments appended to done Todos items."
761 :group 'todos-faces)
762 (defvar todos-comment-face 'todos-comment)
763
764 (defface todos-done-sep
765 ;; '((t :inherit font-lock-type-face))
766 '((((class grayscale)
767 (background light))
768 (:weight bold :foreground "Gray90"))
769 (((class grayscale)
770 (background dark))
771 (:weight bold :foreground "DimGray"))
772 (((class color)
773 (min-colors 88)
774 (background light))
775 (:foreground "ForestGreen"))
776 (((class color)
777 (min-colors 88)
778 (background dark))
779 (:foreground "PaleGreen"))
780 (((class color)
781 (min-colors 16)
782 (background light))
783 (:foreground "ForestGreen"))
784 (((class color)
785 (min-colors 16)
786 (background dark))
787 (:foreground "PaleGreen"))
788 (((class color)
789 (min-colors 8))
790 (:foreground "green"))
791 (t
792 (:underline t :weight bold)))
793 "Face for separator string bewteen done and not done Todos items."
794 :group 'todos-faces)
795 (defvar todos-done-sep-face 'todos-done-sep)
796
797 (defun todos-date-string-matcher (lim)
798 "Search for Todos date string within LIM for font-locking."
799 (re-search-forward
800 (concat todos-date-string-start "\\(?1:" todos-date-pattern "\\)") lim t))
801
802 (defun todos-time-string-matcher (lim)
803 "Search for Todos time string within LIM for font-locking."
804 (re-search-forward (concat todos-date-string-start todos-date-pattern
805 " \\(?1:" diary-time-regexp "\\)") lim t))
806
807 (defun todos-nondiary-marker-matcher (lim)
808 "Search for Todos nondiary markers within LIM for font-locking."
809 (re-search-forward (concat "^\\(?1:" (regexp-quote todos-nondiary-start) "\\)"
810 todos-date-pattern "\\(?: " diary-time-regexp
811 "\\)?\\(?2:" (regexp-quote todos-nondiary-end) "\\)")
812 lim t))
813
814 (defun todos-diary-nonmarking-matcher (lim)
815 "Search for diary nonmarking symbol within LIM for font-locking."
816 (re-search-forward (concat "^\\(?1:" (regexp-quote diary-nonmarking-symbol)
817 "\\)" todos-date-pattern) lim t))
818
819 (defun todos-diary-expired-matcher (lim)
820 "Search for expired diary item date within LIM for font-locking."
821 (when (re-search-forward (concat "^\\(?:"
822 (regexp-quote diary-nonmarking-symbol)
823 "\\)?\\(?1:" todos-date-pattern "\\) \\(?2:"
824 diary-time-regexp "\\)?") lim t)
825 (let* ((date (match-string-no-properties 1))
826 (time (match-string-no-properties 2))
827 ;; Function days-between requires a non-empty time string.
828 (date-time (concat date " " (or time "00:00"))))
829 (or (and (not (string-match ".+day\\|\\*" date))
830 (< (days-between date-time (current-time-string)) 0))
831 (todos-diary-expired-matcher lim)))))
832
833 (defun todos-done-string-matcher (lim)
834 "Search for Todos done header within LIM for font-locking."
835 (re-search-forward (concat todos-done-string-start
836 "[^][]+]")
837 lim t))
838
839 (defun todos-comment-string-matcher (lim)
840 "Search for Todos done comment within LIM for font-locking."
841 (re-search-forward (concat "\\[\\(?1:" todos-comment-string "\\):")
842 lim t))
843
844 ;; (defun todos-category-string-matcher (lim)
845 ;; "Search for Todos category name within LIM for font-locking.
846 ;; This is for fontifying category names appearing in Todos filter
847 ;; mode."
848 ;; (if (eq major-mode 'todos-filtered-items-mode)
849 ;; (re-search-forward
850 ;; (concat "^\\(?:" todos-date-string-start "\\)?" todos-date-pattern
851 ;; "\\(?: " diary-time-regexp "\\)?\\(?:"
852 ;; (regexp-quote todos-nondiary-end) "\\)? \\(?1:\\[.+\\]\\)")
853 ;; lim t)))
854
855 (defun todos-category-string-matcher-1 (lim)
856 "Search for Todos category name within LIM for font-locking.
857 This is for fontifying category names appearing in Todos filter
858 mode following done items."
859 (if (eq major-mode 'todos-filtered-items-mode)
860 (re-search-forward (concat todos-done-string-start todos-date-pattern
861 "\\(?: " diary-time-regexp
862 ;; Use non-greedy operator to prevent
863 ;; capturing possible following non-diary
864 ;; date string.
865 "\\)?] \\(?1:\\[.+?\\]\\)")
866 lim t)))
867
868 (defun todos-category-string-matcher-2 (lim)
869 "Search for Todos category name within LIM for font-locking.
870 This is for fontifying category names appearing in Todos filter
871 mode following todo (not done) items."
872 (if (eq major-mode 'todos-filtered-items-mode)
873 (re-search-forward (concat todos-date-string-start todos-date-pattern
874 "\\(?: " diary-time-regexp "\\)?\\(?:"
875 (regexp-quote todos-nondiary-end)
876 "\\)? \\(?1:\\[.+\\]\\)")
877 lim t)))
878
879 (defvar todos-font-lock-keywords
880 (list
881 '(todos-nondiary-marker-matcher 1 todos-done-sep-face t)
882 '(todos-nondiary-marker-matcher 2 todos-done-sep-face t)
883 ;; This is the face used by diary-lib.el.
884 '(todos-diary-nonmarking-matcher 1 font-lock-constant-face t)
885 '(todos-date-string-matcher 1 todos-date-face t)
886 '(todos-time-string-matcher 1 todos-time-face t)
887 '(todos-done-string-matcher 0 todos-done-face t)
888 '(todos-comment-string-matcher 1 todos-done-face t)
889 ;; '(todos-category-string-matcher 1 todos-done-sep-face t)
890 '(todos-category-string-matcher-1 1 todos-done-sep-face t t)
891 '(todos-category-string-matcher-2 1 todos-done-sep-face t t)
892 '(todos-diary-expired-matcher 1 todos-diary-expired-face t)
893 '(todos-diary-expired-matcher 2 todos-diary-expired-face t t)
894 )
895 "Font-locking for Todos modes.")
896
897 ;; ---------------------------------------------------------------------------
898 ;;; Todos mode local variables and hook functions
899
900 (defvar todos-current-todos-file nil
901 "Variable holding the name of the currently active Todos file.")
902
903 (defun todos-show-current-file ()
904 "Visit current instead of default Todos file with `todos-show'.
905 This function is added to `pre-command-hook' when user option
906 `todos-show-current-file' is set to non-nil."
907 (setq todos-global-current-todos-file todos-current-todos-file))
908
909 (defun todos-display-as-todos-file ()
910 "Show Todos files correctly when visited from outside of Todos mode."
911 (and (member this-command todos-visit-files-commands)
912 (= (- (point-max) (point-min)) (buffer-size))
913 (member major-mode '(todos-mode todos-archive-mode))
914 (todos-category-select)))
915
916 (defun todos-add-to-buffer-list ()
917 "Add name of just visited Todos file to `todos-file-buffers'.
918 This function is added to `find-file-hook' in Todos mode."
919 (let ((filename (file-truename (buffer-file-name))))
920 (when (member filename todos-files)
921 (add-to-list 'todos-file-buffers filename))))
922
923 (defun todos-update-buffer-list ()
924 "Make current Todos mode buffer file car of `todos-file-buffers'.
925 This function is added to `post-command-hook' in Todos mode."
926 (let ((filename (file-truename (buffer-file-name))))
927 (unless (eq (car todos-file-buffers) filename)
928 (setq todos-file-buffers
929 (cons filename (delete filename todos-file-buffers))))))
930
931 (defun todos-reset-global-current-todos-file ()
932 "Update the value of `todos-global-current-todos-file'.
933 This becomes the latest existing Todos file or, if there is none,
934 the value of `todos-default-todos-file'.
935 This function is added to `kill-buffer-hook' in Todos mode."
936 (let ((filename (file-truename (buffer-file-name))))
937 (setq todos-file-buffers (delete filename todos-file-buffers))
938 (setq todos-global-current-todos-file (or (car todos-file-buffers)
939 todos-default-todos-file))))
940
941 (defvar todos-categories nil
942 "Alist of categories in the current Todos file.
943 The elements are cons cells whose car is a category name and
944 whose cdr is a vector of the category's item counts. These are,
945 in order, the numbers of todo items, of todo items included in
946 the Diary, of done items and of archived items.")
947
948 (defvar todos-categories-with-marks nil
949 "Alist of categories and number of marked items they contain.")
950
951 (defvar todos-category-number 1
952 "Variable holding the number of the current Todos category.
953 Todos categories are numbered starting from 1.")
954
955 (defvar todos-first-visit t
956 "Non-nil if first display of this file in the current session.
957 See `todos-display-categories-first'.")
958
959 (defvar todos-show-done-only nil
960 "If non-nil display only done items in current category.
961 Set by the command `todos-show-done-only' and used by
962 `todos-category-select'.")
963
964 (defun todos-reset-and-enable-done-separator ()
965 "Show resized catagory separator overlay after window size change.
966 Added to `window-configuration-change-hook' in `todos-mode'."
967 (when (= 1 (length todos-done-separator-string))
968 (let ((sep todos-done-separator))
969 (setq todos-done-separator (todos-done-separator))
970 (save-match-data (todos-reset-done-separator sep)))
971 ;; FIXME: If this is called while the separator overlay is shown, the
972 ;; separator with deleted overlay becomes visible when waiting for user
973 ;; input and remains so. The following workaround prevents this, but it
974 ;; also prevents widening category when edebugging todos.el.
975 ;; (save-excursion
976 ;; (goto-char (point-min))
977 ;; (when (re-search-forward todos-done-string-start nil t)
978 ;; (let ((todos-show-with-done nil))
979 ;; (todos-category-select))
980 ;; (let ((todos-show-with-done t))
981 ;; (todos-category-select))))
982 ))
983
984 ;; ---------------------------------------------------------------------------
985 ;;; Global variables and helper functions for files and buffers
986
987 (defvar todos-files (funcall todos-files-function)
988 "List of truenames of user's Todos files.")
989
990 (defvar todos-archives (funcall todos-files-function t)
991 "List of truenames of user's Todos archives.")
992
993 (defvar todos-file-buffers nil
994 "List of file names of live Todos mode buffers.")
995
996 (defvar todos-global-current-todos-file nil
997 "Variable holding name of current Todos file.
998 Used by functions called from outside of Todos mode to visit the
999 current Todos file rather than the default Todos file (i.e. when
1000 users option `todos-show-current-file' is non-nil).")
1001
1002 (defun todos-reevaluate-filelist-defcustoms ()
1003 "Reevaluate defcustoms that provide choice list of Todos files."
1004 (custom-set-default 'todos-default-todos-file
1005 (symbol-value 'todos-default-todos-file))
1006 (todos-reevaluate-default-file-defcustom)
1007 (custom-set-default 'todos-filter-files (symbol-value 'todos-filter-files))
1008 (todos-reevaluate-filter-files-defcustom))
1009
1010 (defvar todos-edit-buffer "*Todos Edit*"
1011 "Name of current buffer in Todos Edit mode.")
1012
1013 (defvar todos-categories-buffer "*Todos Categories*"
1014 "Name of buffer in Todos Categories mode.")
1015
1016 (defvar todos-print-buffer "*Todos Print*"
1017 "Name of buffer containing printable Todos text.")
1018
1019 (defun todos-check-format ()
1020 "Signal an error if the current Todos file is ill-formatted.
1021 Otherwise return t. The error message gives the line number
1022 where the invalid formatting was found."
1023 (save-excursion
1024 (save-restriction
1025 (widen)
1026 (goto-char (point-min))
1027 ;; Check for `todos-categories' sexp as the first line
1028 (let ((cats (prin1-to-string todos-categories)))
1029 (unless (looking-at (regexp-quote cats))
1030 (error "Invalid or missing todos-categories sexp")))
1031 (forward-line)
1032 (let ((legit (concat "\\(^" (regexp-quote todos-category-beg) "\\)"
1033 "\\|\\(" todos-date-string-start todos-date-pattern "\\)"
1034 "\\|\\(^[ \t]+[^ \t]*\\)"
1035 "\\|^$"
1036 "\\|\\(^" (regexp-quote todos-category-done) "\\)"
1037 "\\|\\(" todos-done-string-start "\\)")))
1038 (while (not (eobp))
1039 (unless (looking-at legit)
1040 (error "Illegitimate Todos file format at line %d"
1041 (line-number-at-pos (point))))
1042 (forward-line)))))
1043 ;; (message "This Todos file is well-formatted.")
1044 t)
1045
1046 ;; ---------------------------------------------------------------------------
1047 (defun todos-convert-legacy-date-time ()
1048 "Return converted date-time string.
1049 Helper function for `todos-convert-legacy-files'."
1050 (let* ((year (match-string 1))
1051 (month (match-string 2))
1052 (monthname (calendar-month-name (string-to-number month) t))
1053 (day (match-string 3))
1054 (time (match-string 4))
1055 dayname)
1056 (replace-match "")
1057 (insert (mapconcat 'eval calendar-date-display-form "")
1058 (when time (concat " " time)))))
1059
1060 ;; ---------------------------------------------------------------------------
1061 ;;; Global variables and helper functions for categories
1062
1063 (defun todos-category-number (cat)
1064 "Return the number of category CAT in this Todos file.
1065 The buffer-local variable `todos-category-number' holds this
1066 number as its value."
1067 (let ((categories (mapcar 'car todos-categories)))
1068 (setq todos-category-number
1069 ;; Increment by one, so that the highest priority category in Todos
1070 ;; Categories mode is numbered one rather than zero.
1071 (1+ (- (length categories)
1072 (length (member cat categories)))))))
1073
1074 (defun todos-current-category ()
1075 "Return the name of the current category."
1076 (car (nth (1- todos-category-number) todos-categories)))
1077
1078 (defconst todos-category-beg "--==-- "
1079 "String marking beginning of category (inserted with its name).")
1080
1081 (defconst todos-category-done "==--== DONE "
1082 "String marking beginning of category's done items.")
1083
1084 (defun todos-done-separator ()
1085 "Return string used as value of variable `todos-done-separator'."
1086 (let ((sep todos-done-separator-string))
1087 (if (= 1 (length sep))
1088 (make-string (window-width) (string-to-char sep))
1089 todos-done-separator-string)))
1090
1091 (defvar todos-done-separator (todos-done-separator)
1092 "String used to visually separate done from not done items.
1093 Displayed as an overlay instead of `todos-category-done' when
1094 done items are shown. Its value is determined by user option
1095 `todos-done-separator-string'.")
1096
1097 (defun todos-reset-done-separator (sep)
1098 "Replace existing overlays of done items separator string SEP."
1099 (save-excursion
1100 (save-restriction
1101 (widen)
1102 (goto-char (point-min))
1103 (while (re-search-forward
1104 (concat "\n\\(" (regexp-quote todos-category-done) "\\)") nil t)
1105 (let* ((beg (match-beginning 1))
1106 (end (match-end 0))
1107 (ovs (overlays-at beg))
1108 old-sep new-sep)
1109 (and ovs
1110 (setq old-sep (overlay-get (car ovs) 'display))
1111 (string= old-sep sep)
1112 (delete-overlay (car ovs))
1113 (setq new-sep (make-overlay beg end))
1114 (overlay-put new-sep 'display
1115 todos-done-separator)))))))
1116
1117 (defun todos-category-select ()
1118 "Display the current category correctly."
1119 (let ((name (todos-current-category))
1120 cat-begin cat-end done-start done-sep-start done-end)
1121 (widen)
1122 (goto-char (point-min))
1123 (re-search-forward
1124 (concat "^" (regexp-quote (concat todos-category-beg name)) "$") nil t)
1125 (setq cat-begin (1+ (line-end-position)))
1126 (setq cat-end (if (re-search-forward
1127 (concat "^" (regexp-quote todos-category-beg)) nil t)
1128 (match-beginning 0)
1129 (point-max)))
1130 (setq mode-line-buffer-identification
1131 (funcall todos-mode-line-function name))
1132 (narrow-to-region cat-begin cat-end)
1133 (todos-prefix-overlays)
1134 (goto-char (point-min))
1135 (if (re-search-forward (concat "\n\\(" (regexp-quote todos-category-done)
1136 "\\)") nil t)
1137 (progn
1138 (setq done-start (match-beginning 0))
1139 (setq done-sep-start (match-beginning 1))
1140 (setq done-end (match-end 0)))
1141 (error "Category %s is missing todos-category-done string" name))
1142 (if todos-show-done-only
1143 (narrow-to-region (1+ done-end) (point-max))
1144 (when (and todos-show-with-done
1145 (re-search-forward todos-done-string-start nil t))
1146 ;; Now we want to see the done items, so reset displayed end to end of
1147 ;; done items.
1148 (setq done-start cat-end)
1149 ;; Make display overlay for done items separator string, unless there
1150 ;; already is one.
1151 (let* ((done-sep todos-done-separator)
1152 (ovs (overlays-at done-sep-start))
1153 ov-sep)
1154 ;; There should never be more than one overlay here, so car suffices.
1155 (unless (and ovs (string= (overlay-get (car ovs) 'display) done-sep))
1156 (setq ov-sep (make-overlay done-sep-start done-end))
1157 (overlay-put ov-sep 'display done-sep))))
1158 (narrow-to-region (point-min) done-start)
1159 ;; Loading this from todos-mode, or adding it to the mode hook, causes
1160 ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
1161 (when todos-highlight-item
1162 (require 'hl-line)
1163 (hl-line-mode 1)))))
1164
1165 (defun todos-get-count (type &optional category)
1166 "Return count of TYPE items in CATEGORY.
1167 If CATEGORY is nil, default to the current category."
1168 (let* ((cat (or category (todos-current-category)))
1169 (counts (cdr (assoc cat todos-categories)))
1170 (idx (cond ((eq type 'todo) 0)
1171 ((eq type 'diary) 1)
1172 ((eq type 'done) 2)
1173 ((eq type 'archived) 3))))
1174 (aref counts idx)))
1175
1176 (defun todos-update-count (type increment &optional category)
1177 "Change count of TYPE items in CATEGORY by integer INCREMENT.
1178 With nil or omitted CATEGORY, default to the current category."
1179 (let* ((cat (or category (todos-current-category)))
1180 (counts (cdr (assoc cat todos-categories)))
1181 (idx (cond ((eq type 'todo) 0)
1182 ((eq type 'diary) 1)
1183 ((eq type 'done) 2)
1184 ((eq type 'archived) 3))))
1185 (aset counts idx (+ increment (aref counts idx)))))
1186
1187 (defun todos-set-categories ()
1188 "Set `todos-categories' from the sexp at the top of the file."
1189 ;; New archive files created by `todos-move-category' are empty, which would
1190 ;; make the sexp test fail and raise an error, so in this case we skip it.
1191 (unless (zerop (buffer-size))
1192 (save-excursion
1193 (save-restriction
1194 (widen)
1195 (goto-char (point-min))
1196 (setq todos-categories
1197 (if (looking-at "\(\(\"")
1198 (read (buffer-substring-no-properties
1199 (line-beginning-position)
1200 (line-end-position)))
1201 (error "Invalid or missing todos-categories sexp")))))))
1202
1203 (defun todos-update-categories-sexp ()
1204 "Update the `todos-categories' sexp at the top of the file."
1205 (let (buffer-read-only)
1206 (save-excursion
1207 (save-restriction
1208 (widen)
1209 (goto-char (point-min))
1210 (if (looking-at (concat "^" (regexp-quote todos-category-beg)))
1211 (progn (newline) (goto-char (point-min)) ; Make space for sexp.
1212 ;; No categories sexp means the first item was just added
1213 ;; to this file, so have to initialize Todos file and
1214 ;; categories variables in order e.g. to enable categories
1215 ;; display.
1216 (setq todos-default-todos-file (buffer-file-name))
1217 (setq todos-categories (todos-make-categories-list t)))
1218 ;; With empty buffer (e.g. with new archive in
1219 ;; `todos-move-category') `kill-line' signals end of buffer.
1220 (kill-region (line-beginning-position) (line-end-position)))
1221 (prin1 todos-categories (current-buffer))))))
1222
1223 (defun todos-make-categories-list (&optional force)
1224 "Return an alist of Todos categories and their item counts.
1225 With non-nil argument FORCE parse the entire file to build the
1226 list; otherwise, get the value by reading the sexp at the top of
1227 the file."
1228 (setq todos-categories nil)
1229 (save-excursion
1230 (save-restriction
1231 (widen)
1232 (goto-char (point-min))
1233 (let (counts cat archive)
1234 ;; If the file is a todo file and has archived items, identify the
1235 ;; archive, in order to count its items. But skip this with
1236 ;; `todos-convert-legacy-files', since that converts filed items to
1237 ;; archived items.
1238 (when buffer-file-name ; During conversion there is no file yet.
1239 ;; If the file is an archive, it doesn't have an archive.
1240 (unless (member (file-truename buffer-file-name)
1241 (funcall todos-files-function t))
1242 (setq archive (concat (file-name-sans-extension
1243 todos-current-todos-file) ".toda"))))
1244 (while (not (eobp))
1245 (cond ((looking-at (concat (regexp-quote todos-category-beg)
1246 "\\(.*\\)\n"))
1247 (setq cat (match-string-no-properties 1))
1248 ;; Counts for each category: [todo diary done archive]
1249 (setq counts (make-vector 4 0))
1250 (setq todos-categories
1251 (append todos-categories (list (cons cat counts))))
1252 ;; Add archived item count to the todo file item counts.
1253 ;; Make sure to include newly created archives, e.g. due to
1254 ;; todos-move-category.
1255 (when (member archive (funcall todos-files-function t))
1256 (let ((archive-count 0))
1257 (with-current-buffer (find-file-noselect archive)
1258 (widen)
1259 (goto-char (point-min))
1260 (when (re-search-forward
1261 (concat (regexp-quote todos-category-beg) cat)
1262 (point-max) t)
1263 (forward-line)
1264 (while (not (or (looking-at
1265 (concat
1266 (regexp-quote todos-category-beg)
1267 "\\(.*\\)\n"))
1268 (eobp)))
1269 (when (looking-at todos-done-string-start)
1270 (setq archive-count (1+ archive-count)))
1271 (forward-line))))
1272 (todos-update-count 'archived archive-count cat))))
1273 ((looking-at todos-done-string-start)
1274 (todos-update-count 'done 1 cat))
1275 ((looking-at (concat "^\\("
1276 (regexp-quote diary-nonmarking-symbol)
1277 "\\)?" todos-date-pattern))
1278 (todos-update-count 'diary 1 cat)
1279 (todos-update-count 'todo 1 cat))
1280 ((looking-at (concat todos-date-string-start todos-date-pattern))
1281 (todos-update-count 'todo 1 cat))
1282 ;; If first line is todos-categories list, use it and end loop
1283 ;; -- unless FORCEd to scan whole file.
1284 ((bobp)
1285 (unless force
1286 (setq todos-categories (read (buffer-substring-no-properties
1287 (line-beginning-position)
1288 (line-end-position))))
1289 (goto-char (1- (point-max))))))
1290 (forward-line)))))
1291 todos-categories)
1292
1293 (defun todos-repair-categories-sexp ()
1294 "Repair corrupt Todos categories sexp.
1295 This should only be needed as a consequence of careless manual
1296 editing or a bug in todos.el."
1297 (interactive)
1298 (let ((todos-categories (todos-make-categories-list t)))
1299 (todos-update-categories-sexp)))
1300
1301 (defvar todos-allcats-file (concat todos-files-directory "todos-allcats.el")
1302 "Name of file containing the value of `todos-all-categories-alist'.
1303 The contents of this file are automatically generated and
1304 executed when todos.el is loaded, hence users should not edit
1305 it.")
1306
1307 (defun todos-all-categories-alist ()
1308 ""
1309 ;; FIXME: loop through archive files for categories not in todo files?
1310 (let ((files todos-files)
1311 allcats)
1312 (dolist (f files)
1313 ;; FIXME: If file buffer is modified, save first.
1314 (with-temp-buffer
1315 (insert-file-contents f)
1316 (let ((cats (read (buffer-substring-no-properties
1317 (line-beginning-position)
1318 (line-end-position)))))
1319 (dolist (c cats)
1320 (let* ((cat (assoc (car c) allcats))
1321 (catcdr (cdr cat)))
1322 (unless (listp catcdr) (setq catcdr (list catcdr)))
1323 (if cat
1324 (setcdr cat (append catcdr (list (todos-short-file-name f))))
1325 (setq allcats (append allcats
1326 (list
1327 (cons (car c)
1328 (todos-short-file-name f)))))))))))
1329 allcats))
1330
1331 (defvar todos-all-categories-alist (if (file-exists-p todos-allcats-file)
1332 (load-file todos-allcats-file)
1333 (todos-all-categories-alist))
1334 "Alist of names of all Todos categories and their files.")
1335
1336 ;;; Global variables and helper functions for items
1337
1338 (defconst todos-date-pattern
1339 (let ((dayname (diary-name-pattern calendar-day-name-array nil t)))
1340 (concat "\\(?:" dayname "\\|"
1341 (let ((dayname)
1342 ;; FIXME: how to choose between abbreviated and unabbreviated
1343 ;; month name?
1344 (monthname (format "\\(?:%s\\|\\*\\)"
1345 (diary-name-pattern
1346 calendar-month-name-array
1347 calendar-month-abbrev-array t)))
1348 (month "\\(?:[0-9]+\\|\\*\\)")
1349 (day "\\(?:[0-9]+\\|\\*\\)")
1350 (year "-?\\(?:[0-9]+\\|\\*\\)"))
1351 (mapconcat 'eval calendar-date-display-form ""))
1352 "\\)"))
1353 "Regular expression matching a Todos date header.")
1354
1355 (defconst todos-nondiary-start (nth 0 todos-nondiary-marker)
1356 "String inserted before item date to block diary inclusion.")
1357
1358 (defconst todos-nondiary-end (nth 1 todos-nondiary-marker)
1359 "String inserted after item date matching `todos-nondiary-start'.")
1360
1361 ;; By itself this matches anything, because of the `?'; however, it's only
1362 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
1363 ;; lookahead).
1364 (defconst todos-date-string-start
1365 (concat "^\\(" (regexp-quote todos-nondiary-start) "\\|"
1366 (regexp-quote diary-nonmarking-symbol) "\\)?")
1367 "Regular expression matching part of item header before the date.")
1368
1369 (defconst todos-done-string-start
1370 (concat "^\\[" (regexp-quote todos-done-string))
1371 "Regular expression matching start of done item.")
1372
1373 (defconst todos-item-start (concat "\\(" todos-date-string-start "\\|"
1374 todos-done-string-start "\\)"
1375 todos-date-pattern)
1376 "String identifying start of a Todos item.")
1377
1378 (defun todos-item-start ()
1379 "Move to start of current Todos item and return its position."
1380 (unless (or
1381 ;; Buffer is empty (invocation possible e.g. via todos-forward-item
1382 ;; from todos-filter-items when processing category with no todo
1383 ;; items).
1384 (eq (point-min) (point-max))
1385 ;; Point is on the empty line below category's last todo item...
1386 (and (looking-at "^$")
1387 (or (eobp) ; ...and done items are hidden...
1388 (save-excursion ; ...or done items are visible.
1389 (forward-line)
1390 (looking-at (concat "^"
1391 (regexp-quote todos-category-done))))))
1392 ;; Buffer is widened.
1393 (looking-at (regexp-quote todos-category-beg)))
1394 (goto-char (line-beginning-position))
1395 (while (not (looking-at todos-item-start))
1396 (forward-line -1))
1397 (point)))
1398
1399 (defun todos-item-end ()
1400 "Move to end of current Todos item and return its position."
1401 ;; Items cannot end with a blank line.
1402 (unless (looking-at "^$")
1403 (let* ((done (todos-done-item-p))
1404 (to-lim nil)
1405 ;; For todo items, end is before the done items section, for done
1406 ;; items, end is before the next category. If these limits are
1407 ;; missing or inaccessible, end it before the end of the buffer.
1408 (lim (if (save-excursion
1409 (re-search-forward
1410 (concat "^" (regexp-quote (if done
1411 todos-category-beg
1412 todos-category-done)))
1413 nil t))
1414 (progn (setq to-lim t) (match-beginning 0))
1415 (point-max))))
1416 (when (bolp) (forward-char)) ; Find start of next item.
1417 (goto-char (if (re-search-forward todos-item-start lim t)
1418 (match-beginning 0)
1419 (if to-lim lim (point-max))))
1420 ;; For last todo item, skip back over the empty line before the done
1421 ;; items section, else just back to the end of the previous line.
1422 (backward-char (when (and to-lim (not done) (eq (point) lim)) 2))
1423 (point))))
1424
1425 (defun todos-item-string ()
1426 "Return bare text of current item as a string."
1427 (let ((opoint (point))
1428 (start (todos-item-start))
1429 (end (todos-item-end)))
1430 (goto-char opoint)
1431 (and start end (buffer-substring-no-properties start end))))
1432
1433 (defun todos-remove-item ()
1434 "Internal function called in editing, deleting or moving items."
1435 (let* ((beg (todos-item-start))
1436 (end (progn (todos-item-end) (1+ (point))))
1437 (ovs (overlays-in beg beg)))
1438 ;; There can be both prefix/number and mark overlays.
1439 (while ovs (delete-overlay (car ovs)) (pop ovs))
1440 (delete-region beg end)))
1441
1442 (defun todos-diary-item-p ()
1443 "Return non-nil if item at point has diary entry format."
1444 (save-excursion
1445 (when (todos-item-string) ; Exclude empty lines.
1446 (todos-item-start)
1447 (not (looking-at (regexp-quote todos-nondiary-start))))))
1448
1449 (defun todos-done-item-p ()
1450 "Return non-nil if item at point is a done item."
1451 (save-excursion
1452 (todos-item-start)
1453 (looking-at todos-done-string-start)))
1454
1455 (defvar todos-item-mark (propertize (if (equal todos-prefix "*") "@" "*")
1456 'face 'todos-mark)
1457 "String used to mark items.")
1458
1459 (defun todos-marked-item-p ()
1460 "If this item begins with `todos-item-mark', return mark overlay."
1461 (let ((ovs (overlays-in (line-beginning-position) (line-beginning-position)))
1462 (mark todos-item-mark)
1463 ov marked)
1464 (catch 'stop
1465 (while ovs
1466 (setq ov (pop ovs))
1467 (and (equal (overlay-get ov 'before-string) mark)
1468 (throw 'stop (setq marked t)))))
1469 (when marked ov)))
1470
1471 (defun todos-insert-with-overlays (item)
1472 "Insert ITEM at point and update prefix/priority number overlays."
1473 (todos-item-start)
1474 (insert item "\n")
1475 (todos-backward-item)
1476 (todos-prefix-overlays))
1477
1478 (defun todos-prefix-overlays ()
1479 "Put before-string overlay in front of this category's items.
1480 The overlay's value is the string `todos-prefix' or with non-nil
1481 `todos-number-priorities' an integer in the sequence from 1 to
1482 the number of todo or done items in the category indicating the
1483 item's priority. Todo and done items are numbered independently
1484 of each other."
1485 (when (or todos-number-priorities
1486 (not (string-match "^[[:space:]]*$" todos-prefix)))
1487 (let ((prefix (propertize (concat todos-prefix " ")
1488 'face 'todos-prefix-string))
1489 (num 0))
1490 (save-excursion
1491 (goto-char (point-min))
1492 (while (not (eobp))
1493 (when (or (todos-date-string-matcher (line-end-position))
1494 (todos-done-string-matcher (line-end-position)))
1495 (goto-char (match-beginning 0))
1496 (when todos-number-priorities
1497 (setq num (1+ num))
1498 ;; Reset number to 1 for first done item.
1499 (when (and (looking-at todos-done-string-start)
1500 (looking-back (concat "^"
1501 (regexp-quote todos-category-done)
1502 "\n")))
1503 (setq num 1))
1504 (setq prefix (propertize (concat (number-to-string num) " ")
1505 'face 'todos-prefix-string)))
1506 (let ((ovs (overlays-in (point) (point)))
1507 marked ov-pref)
1508 (if ovs
1509 (dolist (ov ovs)
1510 (let ((val (overlay-get ov 'before-string)))
1511 (if (equal val "*")
1512 (setq marked t)
1513 (setq ov-pref val)))))
1514 (unless (equal ov-pref prefix)
1515 ;; Why doesn't this work?
1516 ;; (remove-overlays (point) (point) 'before-string)
1517 (remove-overlays (point) (point))
1518 (overlay-put (make-overlay (point) (point))
1519 'before-string prefix)
1520 (and marked (overlay-put (make-overlay (point) (point))
1521 'before-string todos-item-mark)))))
1522 (forward-line))))))
1523
1524 ;; ---------------------------------------------------------------------------
1525 ;;; Helper functions for user input with prompting and completion
1526
1527 (defun todos-read-file-name (prompt &optional archive mustmatch)
1528 "Choose and return the name of a Todos file, prompting with PROMPT.
1529
1530 Show completions with TAB or SPC; the names are shown in short
1531 form but the absolute truename is returned. With non-nil ARCHIVE
1532 return the absolute truename of a Todos archive file. With non-nil
1533 MUSTMATCH the name of an existing file must be chosen;
1534 otherwise, a new file name is allowed."
1535 (let* ((completion-ignore-case todos-completion-ignore-case)
1536 (files (mapcar 'todos-short-file-name
1537 (if archive todos-archives todos-files)))
1538 (file (completing-read prompt files nil mustmatch nil nil
1539 (unless files
1540 ;; Trigger prompt for initial file.
1541 ""))))
1542 (unless (file-exists-p todos-files-directory)
1543 (make-directory todos-files-directory))
1544 (unless mustmatch
1545 (setq file (todos-validate-name file 'file)))
1546 (setq file (file-truename (concat todos-files-directory file
1547 (if archive ".toda" ".todo"))))))
1548
1549 (defun todos-read-category (prompt &optional mustmatch added)
1550 "Choose and return a category name, prompting with PROMPT.
1551 Show completions with TAB or SPC. With non-nil MUSTMATCH the
1552 name must be that of an existing category; otherwise, a new
1553 category name is allowed, after checking its validity. Non-nil
1554 argument ADDED means the caller is todos-add-category, so don't
1555 ask whether to add the category."
1556 ;; Allow SPC to insert spaces, for adding new category names.
1557 (let ((map minibuffer-local-completion-map))
1558 (define-key map " " nil)
1559 ;; Make a copy of todos-categories in case history-delete-duplicates is
1560 ;; non-nil, which makes completing-read alter todos-categories.
1561 (let* ((categories (copy-sequence todos-categories))
1562 (history (cons 'todos-categories (1+ todos-category-number)))
1563 (completion-ignore-case todos-completion-ignore-case)
1564 (cat (completing-read prompt todos-categories nil
1565 mustmatch nil history
1566 ;; Default for existing categories is the
1567 ;; current category.
1568 (if todos-categories
1569 (todos-current-category)
1570 ;; Trigger prompt for initial category.
1571 ""))))
1572 (unless (or mustmatch (assoc cat todos-categories))
1573 (todos-validate-name cat 'category)
1574 (unless added
1575 (if (y-or-n-p (format (concat "There is no category \"%s\" in "
1576 "this file; add it? ") cat))
1577 ;; Restore point and narrowing after adding new
1578 ;; category, to avoid moving to beginning of file when
1579 ;; moving marked items to a new category (todos-move-item).
1580 (save-excursion
1581 (save-restriction
1582 (todos-add-category cat)
1583 ;; We've changed todos-categories, so we must not
1584 ;; reset it below.
1585 (setq added t)))
1586 (keyboard-quit))))
1587 ;; Restore the original value of todos-categories unless a new category
1588 ;; was added (since todos-add-category changes todos-categories).
1589 (unless added (setq todos-categories categories))
1590 cat)))
1591
1592 (defun todos-validate-name (name type)
1593 "Prompt for new NAME for TYPE until it is valid, then return it.
1594 TYPE can be either a file or a category"
1595 (let ((categories todos-categories)
1596 (files (mapcar 'todos-short-file-name todos-files))
1597 prompt)
1598 (while
1599 (and (cond ((string= "" name)
1600 (setq prompt
1601 (cond ((eq type 'file)
1602 (if todos-files
1603 "Enter a non-empty file name: "
1604 ;; Empty string passed by todos-show to
1605 ;; prompt for initial Todos file.
1606 (concat "Initial file name ["
1607 todos-initial-file "]: ")))
1608 ((eq type 'category)
1609 (if todos-categories
1610 "Enter a non-empty category name: "
1611 ;; Empty string passed by todos-show to
1612 ;; prompt for initial category of a new
1613 ;; Todos file.
1614 (concat "Initial category name ["
1615 todos-initial-category "]: "))))))
1616 ((string-match "\\`\\s-+\\'" name)
1617 (setq prompt
1618 "Enter a name that does not contain only white space: "))
1619 ((and (eq type 'file) (member name todos-files))
1620 (setq prompt "Enter a non-existing file name: "))
1621 ((and (eq type 'category) (assoc name todos-categories))
1622 (setq prompt "Enter a non-existing category name: ")))
1623 (setq name (if (or (and (eq type 'file) todos-files)
1624 (and (eq type 'category) todos-categories))
1625 (completing-read prompt (cond ((eq type 'file)
1626 todos-files)
1627 ((eq type 'category)
1628 todos-categories)))
1629 ;; Offer default initial name.
1630 (completing-read prompt (if (eq type 'file)
1631 todos-files
1632 todos-categories)
1633 nil nil (if (eq type 'file)
1634 todos-initial-file
1635 todos-initial-category))))))
1636 name))
1637
1638 ;; Adapted from calendar-read-date and calendar-date-string.
1639 (defun todos-read-date ()
1640 "Prompt for Gregorian date and return it in the current format.
1641 Also accepts `*' as an unspecified month, day, or year."
1642 (let* ((year (let (x)
1643 (while (if (numberp x) (< x 0) (not (eq x '*)))
1644 (setq x (read-from-minibuffer
1645 "Year (>0 or RET for this year or * for any year): "
1646 nil nil t nil (number-to-string
1647 (calendar-extract-year
1648 (calendar-current-date))))))
1649 x))
1650 (month-array (vconcat calendar-month-name-array (vector "*")))
1651 (abbrevs (vconcat calendar-month-abbrev-array (vector "*")))
1652 (completion-ignore-case todos-completion-ignore-case)
1653 (monthname (completing-read
1654 "Month name (RET for current month, * for any month): "
1655 (mapcar 'list (append month-array nil))
1656 nil t nil nil
1657 (calendar-month-name (calendar-extract-month
1658 (calendar-current-date)) t)))
1659 (month (cdr (assoc-string
1660 monthname (calendar-make-alist month-array nil nil
1661 abbrevs))))
1662 (last (if (= month 13)
1663 ;; Use longest possible month for checking day number
1664 ;; input. Does Calendar do anything special when * is
1665 ;; currently a shorter month?
1666 31
1667 (let ((yr (if (eq year '*)
1668 ;; Use a leap year to allow Feb. 29.
1669 2012
1670 year)))
1671 (calendar-last-day-of-month month yr))))
1672 (day (let (x)
1673 (while (if (numberp x) (or (< x 0) (< last x)) (not (eq x '*)))
1674 (setq x (read-from-minibuffer
1675 (format
1676 "Day (1-%d or RET for today or * for any day): "
1677 last) nil nil t nil (number-to-string
1678 (calendar-extract-day
1679 (calendar-current-date))))))
1680 x))
1681 dayname) ; Needed by calendar-date-display-form.
1682 (setq year (if (eq year '*) (symbol-name '*) (number-to-string year)))
1683 (setq day (if (eq day '*) (symbol-name '*) (number-to-string day)))
1684 ;; FIXME: make abbreviation customizable
1685 (setq monthname
1686 (or (and (= month 13) "*")
1687 (calendar-month-name (calendar-extract-month (list month day year))
1688 t)))
1689 (mapconcat 'eval calendar-date-display-form "")))
1690
1691 (defun todos-read-dayname ()
1692 "Choose name of a day of the week with completion and return it."
1693 (let ((completion-ignore-case todos-completion-ignore-case))
1694 (completing-read "Enter a day name: "
1695 (append calendar-day-name-array nil)
1696 nil t)))
1697
1698 (defun todos-read-time ()
1699 "Prompt for and return a valid clock time as a string.
1700
1701 Valid time strings are those matching `diary-time-regexp'.
1702 Typing `<return>' at the prompt returns the current time, if the
1703 user option `todos-always-add-time-string' is non-nil, otherwise
1704 the empty string (i.e., no time string)."
1705 (let (valid answer)
1706 (while (not valid)
1707 (setq answer (read-string "Enter a clock time: " nil nil
1708 (when todos-always-add-time-string
1709 (substring (current-time-string) 11 16))))
1710 (when (or (string= "" answer)
1711 (string-match diary-time-regexp answer))
1712 (setq valid t)))
1713 answer))
1714
1715 ;; ---------------------------------------------------------------------------
1716 ;;; Item filtering infrastructure
1717
1718 (defvar todos-multiple-filter-files nil
1719 "List of files selected from `todos-multiple-filter-files' widget.")
1720
1721 (defvar todos-multiple-filter-files-widget nil
1722 "Variable holding widget created by `todos-multiple-filter-files'.")
1723
1724 (defun todos-multiple-filter-files ()
1725 "Pop to a buffer with a widget for choosing multiple filter files."
1726 (require 'widget)
1727 (eval-when-compile
1728 (require 'wid-edit))
1729 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
1730 (pop-to-buffer (current-buffer))
1731 (erase-buffer)
1732 (kill-all-local-variables)
1733 (widget-insert "Select files for generating the top priorities list.\n\n")
1734 (setq todos-multiple-filter-files-widget
1735 (widget-create
1736 `(set ,@(mapcar (lambda (x) (list 'const x))
1737 (mapcar 'todos-short-file-name
1738 (funcall todos-files-function))))))
1739 (widget-insert "\n")
1740 (widget-create 'push-button
1741 :notify (lambda (widget &rest ignore)
1742 (setq todos-multiple-filter-files 'quit)
1743 (quit-window t)
1744 (exit-recursive-edit))
1745 "Cancel")
1746 (widget-insert " ")
1747 (widget-create 'push-button
1748 :notify (lambda (&rest ignore)
1749 (setq todos-multiple-filter-files
1750 (mapcar (lambda (f)
1751 (concat todos-files-directory
1752 f ".todo"))
1753 (widget-value
1754 todos-multiple-filter-files-widget)))
1755 (quit-window t)
1756 (exit-recursive-edit))
1757 "Apply")
1758 (use-local-map widget-keymap)
1759 (widget-setup))
1760 (message "Click \"Apply\" after selecting files.")
1761 (recursive-edit))
1762
1763 (defun todos-filter-items (filter &optional multifile)
1764 "Build and display a list of items from different categories.
1765
1766 The items are selected according to the value of FILTER, which
1767 can be `top' for top priority items, `diary' for diary items,
1768 `regexp' for items matching a regular expresion entered by the
1769 user, or a cons cell of one of these symbols and a number set by
1770 the calling command, which overrides `todos-show-priorities'.
1771
1772 With non-nil argument MULTIFILE list top priorities of multiple
1773 Todos files, by default those in `todos-filter-files'."
1774 (let ((num (if (consp filter) (cdr filter) todos-show-priorities))
1775 (buf (get-buffer-create todos-filtered-items-buffer))
1776 (files (list todos-current-todos-file))
1777 regexp fname bufstr cat beg end done)
1778 (when multifile
1779 (setq files (or todos-multiple-filter-files ; Passed from todos-*-multifile.
1780 (if (or (consp filter)
1781 (null todos-filter-files))
1782 (progn (todos-multiple-filter-files)
1783 todos-multiple-filter-files)
1784 todos-filter-files))
1785 todos-multiple-filter-files nil))
1786 (if (eq files 'quit) (keyboard-quit))
1787 (if (null files)
1788 (error "No files have been chosen for filtering")
1789 (with-current-buffer buf
1790 (erase-buffer)
1791 (kill-all-local-variables)
1792 (todos-filtered-items-mode))
1793 (when (eq filter 'regexp)
1794 (setq regexp (read-string "Enter a regular expression: ")))
1795 (save-current-buffer
1796 (dolist (f files)
1797 ;; Before inserting file contents into temp buffer, save a modified
1798 ;; buffer visiting it.
1799 (let ((bf (find-buffer-visiting f)))
1800 (when (buffer-modified-p bf)
1801 (with-current-buffer bf (save-buffer))))
1802 (setq fname (todos-short-file-name f))
1803 (with-temp-buffer
1804 (when (and todos-filter-done-items (eq filter 'regexp))
1805 ;; If there is a corresponding archive file for the Todos file,
1806 ;; insert it first and add identifiers for todos-jump-to-item.
1807 (let ((arch (concat (file-name-sans-extension f) ".toda")))
1808 (when (file-exists-p arch)
1809 (insert-file-contents arch)
1810 ;; Delete Todos archive file categories sexp.
1811 (delete-region (line-beginning-position)
1812 (1+ (line-end-position)))
1813 (save-excursion
1814 (while (not (eobp))
1815 (when (re-search-forward
1816 (concat (if todos-filter-done-items
1817 (concat "\\(?:" todos-done-string-start
1818 "\\|" todos-date-string-start
1819 "\\)")
1820 todos-date-string-start)
1821 todos-date-pattern "\\(?: "
1822 diary-time-regexp "\\)?"
1823 (if todos-filter-done-items
1824 "\\]"
1825 (regexp-quote todos-nondiary-end)) "?")
1826 nil t)
1827 (insert "(archive) "))
1828 (forward-line))))))
1829 (insert-file-contents f)
1830 ;; Delete Todos file categories sexp.
1831 (delete-region (line-beginning-position) (1+ (line-end-position)))
1832 (let (fnum)
1833 ;; Unless the number of items to show was supplied by prefix
1834 ;; argument of caller, the file-wide value from
1835 ;; `todos-priorities-rules', if non-nil, overrides
1836 ;; `todos-show-priorities'.
1837 (unless (consp filter)
1838 (setq fnum (nth 1 (assoc f todos-priorities-rules))))
1839 (while (re-search-forward
1840 (concat "^" (regexp-quote todos-category-beg) "\\(.+\\)\n")
1841 nil t)
1842 (setq cat (match-string 1))
1843 (let (cnum)
1844 ;; Unless the number of items to show was supplied by prefix
1845 ;; argument of caller, the category-wide value from
1846 ;; `todos-priorities-rules', if non-nil, overrides a non-nil
1847 ;; file-wide value from `todos-priorities-rules' as well as
1848 ;; `todos-show-priorities'.
1849 (unless (consp filter)
1850 (let ((cats (nth 2 (assoc f todos-priorities-rules))))
1851 (setq cnum (or (cdr (assoc cat cats)) fnum))))
1852 (delete-region (match-beginning 0) (match-end 0))
1853 (setq beg (point)) ; First item in the current category.
1854 (setq end (if (re-search-forward
1855 (concat "^" (regexp-quote todos-category-beg))
1856 nil t)
1857 (match-beginning 0)
1858 (point-max)))
1859 (goto-char beg)
1860 (setq done
1861 (if (re-search-forward
1862 (concat "\n" (regexp-quote todos-category-done))
1863 end t)
1864 (match-beginning 0)
1865 end))
1866 (unless (and todos-filter-done-items (eq filter 'regexp))
1867 ;; Leave done items.
1868 (delete-region done end)
1869 (setq end done))
1870 (narrow-to-region beg end) ; Process only current category.
1871 (goto-char (point-min))
1872 ;; Apply the filter.
1873 (cond ((eq filter 'diary)
1874 (while (not (eobp))
1875 (if (looking-at (regexp-quote todos-nondiary-start))
1876 (todos-remove-item)
1877 (todos-forward-item))))
1878 ((eq filter 'regexp)
1879 (while (not (eobp))
1880 (if (looking-at todos-item-start)
1881 (if (string-match regexp (todos-item-string))
1882 (todos-forward-item)
1883 (todos-remove-item))
1884 ;; Kill lines that aren't part of a todo or done
1885 ;; item (empty or todos-category-done).
1886 (delete-region (line-beginning-position)
1887 (1+ (line-end-position))))
1888 ;; If last todo item in file matches regexp and
1889 ;; there are no following done items,
1890 ;; todos-category-done string is left dangling,
1891 ;; because todos-forward-item jumps over it.
1892 (if (and (eobp)
1893 (looking-back
1894 (concat (regexp-quote todos-done-string)
1895 "\n")))
1896 (delete-region (point) (progn
1897 (forward-line -2)
1898 (point))))))
1899 (t ; Filter top priority items.
1900 (setq num (or cnum fnum num))
1901 (unless (zerop num)
1902 (todos-forward-item num))))
1903 (setq beg (point))
1904 ;; Delete non-top-priority items.
1905 (unless (member filter '(diary regexp))
1906 (delete-region beg end))
1907 (goto-char (point-min))
1908 ;; Add file (if using multiple files) and category tags to
1909 ;; item.
1910 (while (not (eobp))
1911 (when (re-search-forward
1912 (concat (if todos-filter-done-items
1913 (concat "\\(?:" todos-done-string-start
1914 "\\|" todos-date-string-start
1915 "\\)")
1916 todos-date-string-start)
1917 todos-date-pattern "\\(?: " diary-time-regexp
1918 "\\)?" (if todos-filter-done-items
1919 "\\]"
1920 (regexp-quote todos-nondiary-end))
1921 "?")
1922 nil t)
1923 (insert " [")
1924 (when (looking-at "(archive) ") (goto-char (match-end 0)))
1925 (insert (if multifile (concat fname ":") "") cat "]"))
1926 (forward-line))
1927 (widen)))
1928 (setq bufstr (buffer-string))
1929 (with-current-buffer buf
1930 (let (buffer-read-only)
1931 (insert bufstr)))))))
1932 (set-window-buffer (selected-window) (set-buffer buf))
1933 (todos-prefix-overlays)
1934 (goto-char (point-min)))))
1935
1936 (defun todos-set-top-priorities (&optional arg)
1937 "Set number of top priorities shown by `todos-top-priorities'.
1938 With non-nil ARG, set the number only for the current Todos
1939 category; otherwise, set the number for all categories in the
1940 current Todos file.
1941
1942 Calling this function via either of the commands
1943 `todos-set-top-priorities-in-file' or
1944 `todos-set-top-priorities-in-category' is the recommended way to
1945 set the user customizable option `todos-priorities-rules'."
1946 (let* ((cat (todos-current-category))
1947 (file todos-current-todos-file)
1948 (rules todos-priorities-rules)
1949 (frule (assoc-string file rules))
1950 (crule (assoc-string cat (nth 2 frule)))
1951 (cur (or (if arg (cdr crule) (nth 1 frule))
1952 todos-show-priorities))
1953 (prompt (concat "Current number of top priorities in this "
1954 (if arg "category" "file") ": %d; "
1955 "enter new number: "))
1956 (new "-1")
1957 nrule)
1958 (while (< (string-to-number new) 0)
1959 (let ((cur0 cur))
1960 (setq new (read-number (format prompt cur0) cur0)
1961 prompt "Enter a non-negative number: "
1962 cur0 nil)))
1963 (setq nrule (if arg
1964 (append (nth 2 (delete crule frule)) (list (cons cat new)))
1965 (append (list file new) (list (nth 2 frule)))))
1966 (setq rules (cons (if arg
1967 (list file cur nrule)
1968 nrule)
1969 (delete frule rules)))
1970 (customize-save-variable 'todos-priorities-rules rules)))
1971
1972 (defun todos-filtered-buffer-name (buffer-type file-list)
1973 "Rename Todos filtered buffer using BUFFER-TYPE and FILE-LIST.
1974
1975 The new name is constructed from the string BUFFER-TYPE, which
1976 refers to one of the top priorities, diary or regexp item
1977 filters, and the names of the filtered files in FILE-LIST. Used
1978 in Todos Filter Items mode."
1979 (let* ((flist (if (listp file-list) file-list (list file-list)))
1980 (multi (> (length flist) 1))
1981 (fnames (mapconcat (lambda (f) (todos-short-file-name f))
1982 flist ", ")))
1983 (rename-buffer (format (concat "%s for file" (if multi "s" "")
1984 " \"%s\"") buffer-type fnames))))
1985
1986 ;; ---------------------------------------------------------------------------
1987 ;;; Sorting and display routines for Todos Categories mode.
1988
1989 (defun todos-longest-category-name-length (categories)
1990 "Return the length of the longest name in list CATEGORIES."
1991 (let ((longest 0))
1992 (dolist (c categories longest)
1993 (setq longest (max longest (length c))))))
1994
1995 (defun todos-padded-string (str)
1996 "Return string STR padded with spaces.
1997 The placement of the padding is determined by the value of user
1998 option `todos-categories-align'."
1999 (let* ((categories (mapcar 'car todos-categories))
2000 (len (max (todos-longest-category-name-length categories)
2001 (length todos-categories-category-label)))
2002 (strlen (length str))
2003 (strlen-odd (eq (logand strlen 1) 1)) ; oddp from cl.el
2004 (padding (max 0 (/ (- len strlen) 2)))
2005 (padding-left (cond ((eq todos-categories-align 'left) 0)
2006 ((eq todos-categories-align 'center) padding)
2007 ((eq todos-categories-align 'right)
2008 (if strlen-odd (1+ (* padding 2)) (* padding 2)))))
2009 (padding-right (cond ((eq todos-categories-align 'left)
2010 (if strlen-odd (1+ (* padding 2)) (* padding 2)))
2011 ((eq todos-categories-align 'center)
2012 (if strlen-odd (1+ padding) padding))
2013 ((eq todos-categories-align 'right) 0))))
2014 (concat (make-string padding-left 32) str (make-string padding-right 32))))
2015
2016 (defvar todos-descending-counts nil
2017 "List of keys for category counts sorted in descending order.")
2018
2019 (defun todos-sort (list &optional key)
2020 "Return a copy of LIST, possibly sorted according to KEY."
2021 (let* ((l (copy-sequence list))
2022 (fn (if (eq key 'alpha)
2023 (lambda (x) (upcase x)) ; Alphabetize case insensitively.
2024 (lambda (x) (todos-get-count key x))))
2025 ;; Keep track of whether the last sort by key was descending or
2026 ;; ascending.
2027 (descending (member key todos-descending-counts))
2028 (cmp (if (eq key 'alpha)
2029 'string<
2030 (if descending '< '>)))
2031 (pred (lambda (s1 s2) (let ((t1 (funcall fn (car s1)))
2032 (t2 (funcall fn (car s2))))
2033 (funcall cmp t1 t2)))))
2034 (when key
2035 (setq l (sort l pred))
2036 ;; Switch between descending and ascending sort order.
2037 (if descending
2038 (setq todos-descending-counts
2039 (delete key todos-descending-counts))
2040 (push key todos-descending-counts)))
2041 l))
2042
2043 (defun todos-display-sorted (type)
2044 "Keep point on the TYPE count sorting button just clicked."
2045 (let ((opoint (point)))
2046 (todos-update-categories-display type)
2047 (goto-char opoint)))
2048
2049 (defun todos-label-to-key (label)
2050 "Return symbol for sort key associated with LABEL."
2051 (let (key)
2052 (cond ((string= label todos-categories-category-label)
2053 (setq key 'alpha))
2054 ((string= label todos-categories-todo-label)
2055 (setq key 'todo))
2056 ((string= label todos-categories-diary-label)
2057 (setq key 'diary))
2058 ((string= label todos-categories-done-label)
2059 (setq key 'done))
2060 ((string= label todos-categories-archived-label)
2061 (setq key 'archived)))
2062 key))
2063
2064 (defun todos-insert-sort-button (label)
2065 "Insert button for displaying categories sorted by item counts.
2066 LABEL determines which type of count is sorted."
2067 (setq str (if (string= label todos-categories-category-label)
2068 (todos-padded-string label)
2069 label))
2070 (setq beg (point))
2071 (setq end (+ beg (length str)))
2072 (insert-button str 'face nil
2073 'action
2074 `(lambda (button)
2075 (let ((key (todos-label-to-key ,label)))
2076 (if (and (member key todos-descending-counts)
2077 (eq key 'alpha))
2078 (progn
2079 ;; If display is alphabetical, switch back to
2080 ;; category priority order.
2081 (todos-display-sorted nil)
2082 (setq todos-descending-counts
2083 (delete key todos-descending-counts)))
2084 (todos-display-sorted key)))))
2085 (setq ovl (make-overlay beg end))
2086 (overlay-put ovl 'face 'todos-button))
2087
2088 (defun todos-total-item-counts ()
2089 "Return a list of total item counts for the current file."
2090 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i))
2091 (mapcar 'cdr todos-categories))))
2092 (list 0 1 2 3)))
2093
2094 (defvar todos-categories-category-number 0
2095 "Variable for numbering categories in Todos Categories mode.")
2096
2097 (defun todos-insert-category-line (cat &optional nonum)
2098 "Insert button with category CAT's name and item counts.
2099 With non-nil argument NONUM show only these; otherwise, insert a
2100 number in front of the button indicating the category's priority.
2101 The number and the category name are separated by the string
2102 which is the value of the user option
2103 `todos-categories-number-separator'."
2104 (let ((archive (member todos-current-todos-file todos-archives))
2105 (num todos-categories-category-number)
2106 (str (todos-padded-string cat))
2107 (opoint (point)))
2108 (setq num (1+ num) todos-categories-category-number num)
2109 (insert-button
2110 (concat (if nonum
2111 (make-string (+ 4 (length todos-categories-number-separator))
2112 32)
2113 (format " %3d%s" num todos-categories-number-separator))
2114 str
2115 (mapconcat (lambda (elt)
2116 (concat
2117 (make-string (1+ (/ (length (car elt)) 2)) 32) ; label
2118 (format "%3d" (todos-get-count (cdr elt) cat)) ; count
2119 ;; Add an extra space if label length is odd
2120 ;; (using def of oddp from cl.el).
2121 (if (eq (logand (length (car elt)) 1) 1) " ")))
2122 (if archive
2123 (list (cons todos-categories-done-label 'done))
2124 (list (cons todos-categories-todo-label 'todo)
2125 (cons todos-categories-diary-label 'diary)
2126 (cons todos-categories-done-label 'done)
2127 (cons todos-categories-archived-label
2128 'archived)))
2129 "")
2130 " ") ; So highlighting of last column is consistent with the others.
2131 'face (if (and todos-skip-archived-categories
2132 (zerop (todos-get-count 'todo cat))
2133 (zerop (todos-get-count 'done cat))
2134 (not (zerop (todos-get-count 'archived cat))))
2135 'todos-archived-only
2136 nil)
2137 'action `(lambda (button) (let ((buf (current-buffer)))
2138 (todos-jump-to-category ,cat)
2139 (kill-buffer buf))))
2140 ;; Highlight the sorted count column.
2141 (let* ((beg (+ opoint 7 (length str)))
2142 end ovl)
2143 (cond ((eq nonum 'todo)
2144 (setq beg (+ beg 1 (/ (length todos-categories-todo-label) 2))))
2145 ((eq nonum 'diary)
2146 (setq beg (+ beg 1 (length todos-categories-todo-label)
2147 2 (/ (length todos-categories-diary-label) 2))))
2148 ((eq nonum 'done)
2149 (setq beg (+ beg 1 (length todos-categories-todo-label)
2150 2 (length todos-categories-diary-label)
2151 2 (/ (length todos-categories-done-label) 2))))
2152 ((eq nonum 'archived)
2153 (setq beg (+ beg 1 (length todos-categories-todo-label)
2154 2 (length todos-categories-diary-label)
2155 2 (length todos-categories-done-label)
2156 2 (/ (length todos-categories-archived-label) 2)))))
2157 (unless (= beg (+ opoint 7 (length str))) ; Don't highlight categories.
2158 (setq end (+ beg 4))
2159 (setq ovl (make-overlay beg end))
2160 (overlay-put ovl 'face 'todos-sorted-column)))
2161 (newline)))
2162
2163 (defun todos-display-categories-1 ()
2164 "Prepare buffer for displaying table of categories and item counts."
2165 (unless (eq major-mode 'todos-categories-mode)
2166 (setq todos-global-current-todos-file (or todos-current-todos-file
2167 todos-default-todos-file))
2168 (set-window-buffer (selected-window)
2169 (set-buffer (get-buffer-create todos-categories-buffer)))
2170 (kill-all-local-variables)
2171 (todos-categories-mode)
2172 (let ((archive (member todos-current-todos-file todos-archives))
2173 buffer-read-only)
2174 (erase-buffer)
2175 ;; FIXME: add usage tips?
2176 (insert (format (concat "Category counts for Todos "
2177 (if archive "archive" "file")
2178 " \"%s\".")
2179 (todos-short-file-name todos-current-todos-file)))
2180 (newline 2)
2181 ;; Make space for the column of category numbers.
2182 (insert (make-string (+ 4 (length todos-categories-number-separator)) 32))
2183 ;; Add the category and item count buttons (if this is the list of
2184 ;; categories in an archive, show only done item counts).
2185 (todos-insert-sort-button todos-categories-category-label)
2186 (if archive
2187 (progn
2188 (insert (make-string 3 32))
2189 (todos-insert-sort-button todos-categories-done-label))
2190 (insert (make-string 3 32))
2191 (todos-insert-sort-button todos-categories-todo-label)
2192 (insert (make-string 2 32))
2193 (todos-insert-sort-button todos-categories-diary-label)
2194 (insert (make-string 2 32))
2195 (todos-insert-sort-button todos-categories-done-label)
2196 (insert (make-string 2 32))
2197 (todos-insert-sort-button todos-categories-archived-label))
2198 (newline 2))))
2199
2200 (defun todos-update-categories-display (sortkey)
2201 ""
2202 (let* ((cats0 todos-categories)
2203 (cats (todos-sort cats0 sortkey))
2204 (archive (member todos-current-todos-file todos-archives))
2205 (todos-categories-category-number 0)
2206 ;; Find start of Category button if we just entered Todos Categories
2207 ;; mode.
2208 (pt (if (eq (point) (point-max))
2209 (save-excursion
2210 (forward-line -2)
2211 (goto-char (next-single-char-property-change
2212 (point) 'face nil (line-end-position))))))
2213 (buffer-read-only))
2214 (forward-line 2)
2215 (delete-region (point) (point-max))
2216 ;; Fill in the table with buttonized lines, each showing a category and
2217 ;; its item counts.
2218 (mapc (lambda (cat) (todos-insert-category-line cat sortkey))
2219 (mapcar 'car cats))
2220 (newline)
2221 ;; Add a line showing item count totals.
2222 (insert (make-string (+ 4 (length todos-categories-number-separator)) 32)
2223 (todos-padded-string todos-categories-totals-label)
2224 (mapconcat
2225 (lambda (elt)
2226 (concat
2227 (make-string (1+ (/ (length (car elt)) 2)) 32)
2228 (format "%3d" (nth (cdr elt) (todos-total-item-counts)))
2229 ;; Add an extra space if label length is odd (using
2230 ;; definition of oddp from cl.el).
2231 (if (eq (logand (length (car elt)) 1) 1) " ")))
2232 (if archive
2233 (list (cons todos-categories-done-label 2))
2234 (list (cons todos-categories-todo-label 0)
2235 (cons todos-categories-diary-label 1)
2236 (cons todos-categories-done-label 2)
2237 (cons todos-categories-archived-label 3)))
2238 ""))
2239 ;; Put cursor on Category button initially.
2240 (if pt (goto-char pt))
2241 (setq buffer-read-only t)))
2242
2243 ;; ---------------------------------------------------------------------------
2244 ;;; Routines for generating Todos insertion commands and key bindings
2245
2246 ;; Can either of these be included in Emacs? The originals are GFDL'd.
2247
2248 ;; Slightly reformulated from
2249 ;; http://rosettacode.org/wiki/Power_set#Common_Lisp.
2250 (defun powerset-recursive (l)
2251 (cond ((null l)
2252 (list nil))
2253 (t
2254 (let ((prev (powerset-recursive (cdr l))))
2255 (append (mapcar (lambda (elt) (cons (car l) elt))
2256 prev)
2257 prev)))))
2258
2259 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C
2260 (defun powerset-bitwise (l)
2261 (let ((binnum (lsh 1 (length l)))
2262 pset elt)
2263 (dotimes (i binnum)
2264 (let ((bits i)
2265 (ll l))
2266 (while (not (zerop bits))
2267 (let ((arg (pop ll)))
2268 (unless (zerop (logand bits 1))
2269 (setq elt (append elt (list arg))))
2270 (setq bits (lsh bits -1))))
2271 (setq pset (append pset (list elt)))
2272 (setq elt nil)))
2273 pset))
2274
2275 ;; (defalias 'todos-powerset 'powerset-recursive)
2276 (defalias 'todos-powerset 'powerset-bitwise)
2277
2278 ;; Return list of lists of non-nil atoms produced from ARGLIST. The elements
2279 ;; of ARGLIST may be atoms or lists.
2280 (defun todos-gen-arglists (arglist)
2281 (let (arglists)
2282 (while arglist
2283 (let ((arg (pop arglist)))
2284 (cond ((symbolp arg)
2285 (setq arglists (if arglists
2286 (mapcar (lambda (l) (push arg l)) arglists)
2287 (list (push arg arglists)))))
2288 ((listp arg)
2289 (setq arglists
2290 (mapcar (lambda (a)
2291 (if (= 1 (length arglists))
2292 (apply (lambda (l) (push a l)) arglists)
2293 (mapcar (lambda (l) (push a l)) arglists)))
2294 arg))))))
2295 (setq arglists (mapcar 'reverse (apply 'append (mapc 'car arglists))))))
2296
2297 (defvar todos-insertion-commands-args-genlist
2298 '(diary nonmarking (calendar date dayname) time (here region))
2299 "Generator list for argument lists of Todos insertion commands.")
2300
2301 (defvar todos-insertion-commands-args
2302 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist))
2303 res new)
2304 (setq res (remove-duplicates
2305 (apply 'append (mapcar 'todos-powerset argslist)) :test 'equal))
2306 (dolist (l res)
2307 (unless (= 5 (length l))
2308 (let ((v (make-vector 5 nil)) elt)
2309 (while l
2310 (setq elt (pop l))
2311 (cond ((eq elt 'diary)
2312 (aset v 0 elt))
2313 ((eq elt 'nonmarking)
2314 (aset v 1 elt))
2315 ((or (eq elt 'calendar)
2316 (eq elt 'date)
2317 (eq elt 'dayname))
2318 (aset v 2 elt))
2319 ((eq elt 'time)
2320 (aset v 3 elt))
2321 ((or (eq elt 'here)
2322 (eq elt 'region))
2323 (aset v 4 elt))))
2324 (setq l (append v nil))))
2325 (setq new (append new (list l))))
2326 new)
2327 "List of all argument lists for Todos insertion commands.")
2328
2329 (defun todos-insertion-command-name (arglist)
2330 "Generate Todos insertion command name from ARGLIST."
2331 (replace-regexp-in-string
2332 "-\\_>" ""
2333 (replace-regexp-in-string
2334 "-+" "-"
2335 ;; FIXME: "todos-insert-item-"
2336 (concat "todos-item-insert-"
2337 (mapconcat (lambda (e) (if e (symbol-name e))) arglist "-")))))
2338
2339 (defvar todos-insertion-commands-names
2340 (mapcar (lambda (l)
2341 (todos-insertion-command-name l))
2342 todos-insertion-commands-args)
2343 "List of names of Todos insertion commands.")
2344
2345 ;; FIXME: prefix argument ARG is nil
2346 (defmacro todos-define-insertion-command (&rest args)
2347 (let ((name (intern (todos-insertion-command-name args)))
2348 (arg0 (nth 0 args))
2349 (arg1 (nth 1 args))
2350 (arg2 (nth 2 args))
2351 (arg3 (nth 3 args))
2352 (arg4 (nth 4 args)))
2353 `(defun ,name (&optional arg &rest args)
2354 "Todos item insertion command generated from ARGS."
2355 (interactive (list current-prefix-arg))
2356 (todos-insert-item arg ',arg0 ',arg1 ',arg2 ',arg3 ',arg4))))
2357
2358 ;; FIXME: exclude todos-insert-item (or rather from
2359 ;; todos-insertion-key-bindings?), otherwise its doc string won't be
2360 ;; found with C-h k (but it will with M-x todos-insert-item)
2361 (defvar todos-insertion-commands
2362 (mapcar (lambda (c)
2363 (eval `(todos-define-insertion-command ,@c)))
2364 todos-insertion-commands-args)
2365 "List of Todos insertion commands.")
2366
2367 (defvar todos-insertion-commands-arg-key-list
2368 '(("diary" "y" "yy")
2369 ("nonmarking" "k" "kk")
2370 ("calendar" "c" "cc")
2371 ("date" "d" "dd")
2372 ("dayname" "n" "nn")
2373 ("time" "t" "tt")
2374 ("here" "h" "h")
2375 ("region" "r" "r"))
2376 "")
2377
2378 (defun todos-insertion-key-bindings (map)
2379 ""
2380 (dolist (c todos-insertion-commands)
2381 (let* ((key "")
2382 (cname (symbol-name c)))
2383 (mapc (lambda (l)
2384 (let ((arg (nth 0 l))
2385 (key1 (nth 1 l))
2386 (key2 (nth 2 l)))
2387 (if (string-match (concat (regexp-quote arg) "\\_>") cname)
2388 (setq key (concat key key2)))
2389 (if (string-match (concat (regexp-quote arg) ".+") cname)
2390 (setq key (concat key key1)))))
2391 todos-insertion-commands-arg-key-list)
2392 (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname)
2393 (setq key (concat key "i")))
2394 (define-key map key c))))
2395
2396 (defvar todos-insertion-map
2397 (let ((map (make-keymap)))
2398 (todos-insertion-key-bindings map)
2399 (define-key map "p" 'todos-copy-item)
2400 map)
2401 "Keymap for Todos mode insertion commands.")
2402
2403 ;; ---------------------------------------------------------------------------
2404 ;;; Key maps and menus
2405
2406 (defvar todos-key-bindings
2407 `(
2408 ;; display
2409 ("Cd" . todos-display-categories) ;FIXME: Cs todos-show-categories?
2410 ("H" . todos-highlight-item)
2411 ("N" . todos-hide-show-item-numbering)
2412 ("D" . todos-hide-show-date-time)
2413 ("*" . todos-mark-unmark-item)
2414 ("C*" . todos-mark-category)
2415 ("Cu" . todos-unmark-category)
2416 ("PP" . todos-print)
2417 ("PF" . todos-print-to-file)
2418 ("v" . todos-hide-show-done-items)
2419 ("V" . todos-show-done-only)
2420 ("As" . todos-show-archive)
2421 ("Ac" . todos-choose-archive)
2422 ("Y" . todos-diary-items)
2423 ("Fe" . todos-edit-multiline)
2424 ("Fh" . todos-highlight-item)
2425 ("Fn" . todos-hide-show-item-numbering)
2426 ("Fd" . todos-hide-show-date-time)
2427 ("Ftt" . todos-top-priorities)
2428 ("Ftm" . todos-top-priorities-multifile)
2429 ("Fts" . todos-set-top-priorities-in-file)
2430 ("Cts" . todos-set-top-priorities-in-category)
2431 ("Fyy" . todos-diary-items)
2432 ("Fym" . todos-diary-items-multifile)
2433 ("Fxx" . todos-regexp-items)
2434 ("Fxm" . todos-regexp-items-multifile)
2435 ;; navigation
2436 ("f" . todos-forward-category)
2437 ("b" . todos-backward-category)
2438 ("j" . todos-jump-to-category)
2439 ("J" . todos-jump-to-category-other-file)
2440 ("n" . todos-forward-item)
2441 ("p" . todos-backward-item)
2442 ("S" . todos-search)
2443 ("X" . todos-clear-matches)
2444 ;; editing
2445 ("Fa" . todos-add-file)
2446 ("Ca" . todos-add-category)
2447 ("Cr" . todos-rename-category)
2448 ("Cg" . todos-merge-category)
2449 ("Cm" . todos-move-category)
2450 ("Ck" . todos-delete-category)
2451 ("d" . todos-item-done)
2452 ("ee" . todos-edit-item)
2453 ("em" . todos-edit-multiline-item)
2454 ("eh" . todos-edit-item-header)
2455 ("edd" . todos-edit-item-date)
2456 ("edc" . todos-edit-item-date-from-calendar)
2457 ("edt" . todos-edit-item-date-is-today)
2458 ("et" . todos-edit-item-time)
2459 ("eyy" . todos-edit-item-diary-inclusion)
2460 ;; ("" . todos-edit-category-diary-inclusion)
2461 ("eyn" . todos-edit-item-diary-nonmarking)
2462 ;;("" . todos-edit-category-diary-nonmarking)
2463 ("ec" . todos-done-item-add-edit-or-delete-comment)
2464 ("i" . ,todos-insertion-map)
2465 ("k" . todos-delete-item) ;FIXME: not single letter?
2466 ("m" . todos-move-item)
2467 ("M" . todos-move-item-to-file)
2468 ("r" . todos-raise-item-priority)
2469 ("l" . todos-lower-item-priority)
2470 ("#" . todos-set-item-priority)
2471 ("u" . todos-item-undo)
2472 ("Ad" . todos-archive-done-item) ;FIXME: ad
2473 ("AD" . todos-archive-category-done-items) ;FIXME: aD or C-u ad ?
2474 ("s" . todos-save)
2475 ("q" . todos-quit)
2476 ([remap newline] . newline-and-indent)
2477 )
2478 "Alist pairing keys defined in Todos modes and their bindings.")
2479
2480 (defvar todos-mode-map
2481 (let ((map (make-keymap)))
2482 ;; Don't suppress digit keys, so they can supply prefix arguments.
2483 (suppress-keymap map)
2484 (dolist (ck todos-key-bindings)
2485 (define-key map (car ck) (cdr ck)))
2486 map)
2487 "Todos mode keymap.")
2488
2489 ;; FIXME
2490 (easy-menu-define
2491 todos-menu todos-mode-map "Todos Menu"
2492 '("Todos"
2493 ("Navigation"
2494 ["Next Item" todos-forward-item t]
2495 ["Previous Item" todos-backward-item t]
2496 "---"
2497 ["Next Category" todos-forward-category t]
2498 ["Previous Category" todos-backward-category t]
2499 ["Jump to Category" todos-jump-to-category t]
2500 ["Jump to Category in Other File" todos-jump-to-category-other-file t]
2501 "---"
2502 ["Search Todos File" todos-search t]
2503 ["Clear Highlighting on Search Matches" todos-category-done t])
2504 ("Display"
2505 ["List Current Categories" todos-display-categories t]
2506 ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
2507 ["Turn Item Highlighting on/off" todos-highlight-item t]
2508 ["Turn Item Numbering on/off" todos-hide-show-item-numbering t]
2509 ["Turn Item Time Stamp on/off" todos-hide-show-date-time t]
2510 ["View/Hide Done Items" todos-hide-show-done-items t]
2511 "---"
2512 ["View Diary Items" todos-diary-items t]
2513 ["View Top Priority Items" todos-top-priorities t]
2514 ["View Multifile Top Priority Items" todos-top-priorities-multifile t]
2515 "---"
2516 ["Print Category" todos-print t])
2517 ("Editing"
2518 ["Insert New Item" todos-insert-item t]
2519 ["Insert Item Here" todos-insert-item-here t]
2520 ("More Insertion Commands")
2521 ["Edit Item" todos-edit-item t]
2522 ["Edit Multiline Item" todos-edit-multiline t]
2523 ["Edit Item Header" todos-edit-item-header t]
2524 ["Edit Item Date" todos-edit-item-date t]
2525 ["Edit Item Time" todos-edit-item-time t]
2526 "---"
2527 ["Lower Item Priority" todos-lower-item-priority t]
2528 ["Raise Item Priority" todos-raise-item-priority t]
2529 ["Set Item Priority" todos-set-item-priority t]
2530 ["Move (Recategorize) Item" todos-move-item t]
2531 ["Delete Item" todos-delete-item t]
2532 ["Undo Done Item" todos-item-undo t]
2533 ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t]
2534 ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t]
2535 ["Mark & Hide Done Item" todos-item-done t]
2536 ["Archive Done Items" todos-archive-category-done-items t]
2537 "---"
2538 ["Add New Todos File" todos-add-file t]
2539 ["Add New Category" todos-add-category t]
2540 ["Delete Current Category" todos-delete-category t]
2541 ["Rename Current Category" todos-rename-category t]
2542 "---"
2543 ["Save Todos File" todos-save t]
2544 )
2545 "---"
2546 ["Quit" todos-quit t]
2547 ))
2548
2549 (defvar todos-archive-mode-map
2550 (let ((map (make-sparse-keymap)))
2551 (suppress-keymap map t)
2552 ;; navigation commands
2553 (define-key map "f" 'todos-forward-category)
2554 (define-key map "b" 'todos-backward-category)
2555 (define-key map "j" 'todos-jump-to-category)
2556 (define-key map "n" 'todos-forward-item)
2557 (define-key map "p" 'todos-backward-item)
2558 ;; display commands
2559 (define-key map "C" 'todos-display-categories)
2560 (define-key map "H" 'todos-highlight-item)
2561 (define-key map "N" 'todos-hide-show-item-numbering)
2562 ;; (define-key map "" 'todos-hide-show-date-time)
2563 (define-key map "P" 'todos-print)
2564 (define-key map "q" 'todos-quit)
2565 (define-key map "s" 'todos-save)
2566 (define-key map "S" 'todos-search)
2567 (define-key map "t" 'todos-show)
2568 (define-key map "u" 'todos-unarchive-items)
2569 (define-key map "U" 'todos-unarchive-category)
2570 map)
2571 "Todos Archive mode keymap.")
2572
2573 (defvar todos-edit-mode-map
2574 (let ((map (make-sparse-keymap)))
2575 (define-key map "\C-x\C-q" 'todos-edit-quit)
2576 (define-key map [remap newline] 'newline-and-indent)
2577 map)
2578 "Todos Edit mode keymap.")
2579
2580 (defvar todos-categories-mode-map
2581 (let ((map (make-sparse-keymap)))
2582 (suppress-keymap map t)
2583 (define-key map "c" 'todos-display-categories-alphabetically-or-by-priority)
2584 (define-key map "t" 'todos-display-categories-sorted-by-todo)
2585 (define-key map "y" 'todos-display-categories-sorted-by-diary)
2586 (define-key map "d" 'todos-display-categories-sorted-by-done)
2587 (define-key map "a" 'todos-display-categories-sorted-by-archived)
2588 (define-key map "l" 'todos-lower-category-priority)
2589 (define-key map "+" 'todos-lower-category-priority)
2590 (define-key map "r" 'todos-raise-category-priority)
2591 (define-key map "-" 'todos-raise-category-priority)
2592 (define-key map "n" 'todos-forward-button)
2593 (define-key map "p" 'todos-backward-button)
2594 (define-key map [tab] 'todos-forward-button)
2595 (define-key map [backtab] 'todos-backward-button)
2596 (define-key map "q" 'todos-quit)
2597 ;; (define-key map "A" 'todos-add-category)
2598 ;; (define-key map "D" 'todos-delete-category)
2599 ;; (define-key map "R" 'todos-rename-category)
2600 map)
2601 "Todos Categories mode keymap.")
2602
2603 (defvar todos-filtered-items-mode-map
2604 (let ((map (make-keymap)))
2605 (suppress-keymap map t)
2606 ;; navigation commands
2607 (define-key map "j" 'todos-jump-to-item)
2608 (define-key map [remap newline] 'todos-jump-to-item)
2609 (define-key map "n" 'todos-forward-item)
2610 (define-key map "p" 'todos-backward-item)
2611 (define-key map "H" 'todos-highlight-item)
2612 (define-key map "N" 'todos-hide-show-item-numbering)
2613 (define-key map "D" 'todos-hide-show-date-time)
2614 (define-key map "P" 'todos-print)
2615 (define-key map "q" 'todos-quit)
2616 (define-key map "s" 'todos-save)
2617 ;; editing commands
2618 (define-key map "l" 'todos-lower-item-priority)
2619 (define-key map "r" 'todos-raise-item-priority)
2620 (define-key map "#" 'todos-set-item-priority)
2621 map)
2622 "Todos Top Priorities mode keymap.")
2623
2624 ;; ---------------------------------------------------------------------------
2625 ;;; Mode definitions
2626
2627 (defun todos-modes-set-1 ()
2628 ""
2629 (set (make-local-variable 'font-lock-defaults) '(todos-font-lock-keywords t))
2630 (set (make-local-variable 'indent-line-function) 'todos-indent)
2631 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2632
2633 (defun todos-modes-set-2 ()
2634 ""
2635 (add-to-invisibility-spec 'todos)
2636 (setq buffer-read-only t)
2637 (set (make-local-variable 'hl-line-range-function)
2638 (lambda() (when (todos-item-end)
2639 (cons (todos-item-start) (todos-item-end))))))
2640
2641 (defun todos-modes-set-3 ()
2642 ""
2643 (set (make-local-variable 'todos-categories) (todos-set-categories))
2644 (set (make-local-variable 'todos-category-number) 1)
2645 (set (make-local-variable 'todos-first-visit) t)
2646 (add-hook 'find-file-hook 'todos-display-as-todos-file nil t))
2647
2648 (put 'todos-mode 'mode-class 'special)
2649
2650 (define-derived-mode todos-mode special-mode "Todos"
2651 "Major mode for displaying, navigating and editing Todo lists.
2652
2653 \\{todos-mode-map}"
2654 (easy-menu-add todos-menu)
2655 (todos-modes-set-1)
2656 (todos-modes-set-2)
2657 (todos-modes-set-3)
2658 ;; Initialize todos-current-todos-file.
2659 (when (member (file-truename (buffer-file-name))
2660 (funcall todos-files-function))
2661 (set (make-local-variable 'todos-current-todos-file)
2662 (file-truename (buffer-file-name))))
2663 (set (make-local-variable 'todos-first-visit) t)
2664 (set (make-local-variable 'todos-show-done-only) nil)
2665 (set (make-local-variable 'todos-categories-with-marks) nil)
2666 (add-hook 'find-file-hook 'todos-add-to-buffer-list nil t)
2667 (add-hook 'post-command-hook 'todos-update-buffer-list nil t)
2668 (when todos-show-current-file
2669 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
2670 (add-hook 'window-configuration-change-hook
2671 'todos-reset-and-enable-done-separator nil t)
2672 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
2673
2674 (defun todos-unload-hook ()
2675 ""
2676 (remove-hook 'pre-command-hook 'todos-show-current-file t)
2677 (remove-hook 'post-command-hook 'todos-update-buffer-list t)
2678 (remove-hook 'find-file-hook 'todos-display-as-todos-file t)
2679 (remove-hook 'find-file-hook 'todos-add-to-buffer-list t)
2680 (remove-hook 'window-configuration-change-hook
2681 'todos-reset-and-enable-done-separator t)
2682 (remove-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file t))
2683
2684 (put 'todos-archive-mode 'mode-class 'special)
2685
2686 ;; If todos-mode is parent, all todos-mode key bindings appear to be
2687 ;; available in todos-archive-mode (e.g. shown by C-h m).
2688 (define-derived-mode todos-archive-mode special-mode "Todos-Arch"
2689 "Major mode for archived Todos categories.
2690
2691 \\{todos-archive-mode-map}"
2692 (todos-modes-set-1)
2693 (todos-modes-set-2)
2694 (todos-modes-set-3)
2695 (set (make-local-variable 'todos-current-todos-file)
2696 (file-truename (buffer-file-name)))
2697 (set (make-local-variable 'todos-show-done-only) t))
2698
2699 (defun todos-mode-external-set ()
2700 ""
2701 (set (make-local-variable 'todos-current-todos-file)
2702 todos-global-current-todos-file)
2703 (let ((cats (with-current-buffer
2704 (find-buffer-visiting todos-current-todos-file)
2705 todos-categories)))
2706 (set (make-local-variable 'todos-categories) cats)))
2707
2708 (define-derived-mode todos-edit-mode text-mode "Todos-Ed"
2709 "Major mode for editing multiline Todo items.
2710
2711 \\{todos-edit-mode-map}"
2712 (todos-modes-set-1)
2713 (todos-mode-external-set))
2714
2715 (put 'todos-categories-mode 'mode-class 'special)
2716
2717 (define-derived-mode todos-categories-mode special-mode "Todos-Cats"
2718 "Major mode for displaying and editing Todos categories.
2719
2720 \\{todos-categories-mode-map}"
2721 (todos-mode-external-set))
2722
2723 (put 'todos-filter-mode 'mode-class 'special)
2724
2725 (define-derived-mode todos-filtered-items-mode special-mode "Todos-Fltr"
2726 "Mode for displaying and reprioritizing top priority Todos.
2727
2728 \\{todos-filtered-items-mode-map}"
2729 (todos-modes-set-1)
2730 (todos-modes-set-2))
2731
2732 ;; ---------------------------------------------------------------------------
2733 ;;; Todos Commands
2734
2735 ;; ---------------------------------------------------------------------------
2736 ;;; Entering and Exiting
2737
2738 ;;;###autoload
2739 (defun todos-show (&optional solicit-file)
2740 "Visit the current Todos file and display one of its categories.
2741 With non-nil prefix argument SOLICIT-FILE prompt for which todo
2742 file to visit.
2743
2744 Without a prefix argument, the first invocation of this command
2745 in a session visits `todos-default-todos-file' (creating it if it
2746 does not yet exist); subsequent invocations from outside of Todos
2747 mode revisit this file or, if the user option
2748 `todos-show-current-file' is non-nil, whichever Todos file
2749 \(either a todo or an archive file) was visited last.
2750
2751 The category displayed on initial invocation is the first member
2752 of `todos-categories' for the current Todos file, on subsequent
2753 invocations whichever category was displayed last. If
2754 `todos-display-categories-first' is non-nil, then the first
2755 invocation of `todos-show' displays a clickable listing of the
2756 categories in the current Todos file.
2757
2758 In Todos mode just the category's unfinished todo items are shown
2759 by default. The done items are hidden, but typing
2760 `\\[todos-hide-show-done-items]' displays them below the todo
2761 items. With non-nil user option `todos-show-with-done' both todo
2762 and done items are always shown on visiting a category.
2763
2764 If this command is invoked in Todos Archive mode, it visits the
2765 corresponding Todos file, displaying the corresponding category."
2766 (interactive "P")
2767 (let* ((cat)
2768 (file (cond (solicit-file
2769 (if (funcall todos-files-function)
2770 (todos-read-file-name "Choose a Todos file to visit: "
2771 nil t)
2772 (error "There are no Todos files")))
2773 ((and (eq major-mode 'todos-archive-mode)
2774 ;; Called noninteractively via todos-quit from
2775 ;; Todos Categories mode to return to archive file.
2776 (called-interactively-p 'any))
2777 (setq cat (todos-current-category))
2778 (concat (file-name-sans-extension todos-current-todos-file)
2779 ".todo"))
2780 (t
2781 (or todos-current-todos-file
2782 (and todos-show-current-file
2783 todos-global-current-todos-file)
2784 todos-default-todos-file
2785 (todos-add-file))))))
2786 (if (and todos-first-visit todos-display-categories-first)
2787 (todos-display-categories)
2788 (set-window-buffer (selected-window)
2789 (set-buffer (find-file-noselect file)))
2790 ;; If called from archive file, show corresponding category in Todos
2791 ;; file, if it exists.
2792 (when (assoc cat todos-categories)
2793 (setq todos-category-number (todos-category-number cat)))
2794 ;; If no Todos file exists, initialize one.
2795 (when (zerop (buffer-size))
2796 ;; Call with empty category name to get initial prompt.
2797 (setq todos-category-number (todos-add-category "")))
2798 (save-excursion (todos-category-select)))
2799 (setq todos-first-visit nil)))
2800
2801 (defun todos-display-categories ()
2802 "Display a table of the current file's categories and item counts.
2803
2804 In the initial display the categories are numbered, indicating
2805 their current order for navigating by \\[todos-forward-category]
2806 and \\[todos-backward-category]. You can persistantly change the
2807 order of the category at point by typing
2808 \\[todos-raise-category-priority] or
2809 \\[todos-lower-category-priority].
2810
2811 The labels above the category names and item counts are buttons,
2812 and clicking these changes the display: sorted by category name
2813 or by the respective item counts (alternately descending or
2814 ascending). In these displays the categories are not numbered
2815 and \\[todos-raise-category-priority] and
2816 \\[todos-lower-category-priority] are
2817 disabled. (Programmatically, the sorting is triggered by passing
2818 a non-nil SORTKEY argument.)
2819
2820 In addition, the lines with the category names and item counts
2821 are buttonized, and pressing one of these button jumps to the
2822 category in Todos mode (or Todos Archive mode, for categories
2823 containing only archived items, provided user option
2824 `todos-skip-archived-categories' is non-nil. These categories
2825 are shown in `todos-archived-only' face."
2826 (interactive)
2827 (todos-display-categories-1)
2828 (let (sortkey)
2829 (todos-update-categories-display sortkey)))
2830
2831 (defun todos-display-categories-alphabetically-or-by-priority ()
2832 ""
2833 (interactive)
2834 (save-excursion
2835 (goto-char (point-min))
2836 (forward-line 2)
2837 (if (member 'alpha todos-descending-counts)
2838 (progn
2839 (todos-update-categories-display nil)
2840 (setq todos-descending-counts
2841 (delete 'alpha todos-descending-counts)))
2842 (todos-update-categories-display 'alpha))))
2843
2844 (defun todos-display-categories-sorted-by-todo ()
2845 ""
2846 (interactive)
2847 (save-excursion
2848 (goto-char (point-min))
2849 (forward-line 2)
2850 (todos-update-categories-display 'todo)))
2851
2852 (defun todos-display-categories-sorted-by-diary ()
2853 ""
2854 (interactive)
2855 (save-excursion
2856 (goto-char (point-min))
2857 (forward-line 2)
2858 (todos-update-categories-display 'diary)))
2859
2860 (defun todos-display-categories-sorted-by-done ()
2861 ""
2862 (interactive)
2863 (save-excursion
2864 (goto-char (point-min))
2865 (forward-line 2)
2866 (todos-update-categories-display 'done)))
2867
2868 (defun todos-display-categories-sorted-by-archived ()
2869 ""
2870 (interactive)
2871 (save-excursion
2872 (goto-char (point-min))
2873 (forward-line 2)
2874 (todos-update-categories-display 'archived)))
2875
2876 (defun todos-show-archive (&optional ask)
2877 "Visit the archive of the current Todos category, if it exists.
2878 If the category has no archived items, prompt to visit the
2879 archive anyway. If there is no archive for this file or with
2880 non-nil argument ASK, prompt to visit another archive.
2881
2882 The buffer showing the archive is in Todos Archive mode. The
2883 first visit in a session displays the first category in the
2884 archive, subsequent visits return to the last category
2885 displayed."
2886 (interactive)
2887 (let* ((cat (todos-current-category))
2888 (count (todos-get-count 'archived cat))
2889 (archive (concat (file-name-sans-extension todos-current-todos-file)
2890 ".toda"))
2891 place)
2892 (setq place (cond (ask 'other-archive)
2893 ((file-exists-p archive) 'this-archive)
2894 (t (when (y-or-n-p (concat "This file has no archive; "
2895 "visit another archive? "))
2896 'other-archive))))
2897 (when (eq place 'other-archive)
2898 (setq archive (todos-read-file-name "Choose a Todos archive: " t t)))
2899 (when (and (eq place 'this-archive) (zerop count))
2900 (setq place (when (y-or-n-p
2901 (concat "This category has no archived items;"
2902 " visit archive anyway? "))
2903 'other-cat)))
2904 (when place
2905 (set-window-buffer (selected-window)
2906 (set-buffer (find-file-noselect archive)))
2907 (if (member place '(other-archive other-cat))
2908 (setq todos-category-number 1)
2909 (todos-category-number cat))
2910 (todos-category-select))))
2911
2912 (defun todos-choose-archive ()
2913 "Choose an archive and visit it."
2914 (interactive)
2915 (todos-show-archive t))
2916
2917 ;; FIXME: need this?
2918 (defun todos-save ()
2919 "Save the current Todos file."
2920 (interactive)
2921 (save-buffer))
2922
2923 (defun todos-quit ()
2924 "Exit the current Todos-related buffer.
2925 Depending on the specific mode, this either kills the buffer or
2926 buries it and restores state as needed."
2927 (interactive)
2928 (cond ((eq major-mode 'todos-categories-mode)
2929 (kill-buffer)
2930 (setq todos-descending-counts nil)
2931 (todos-show))
2932 ((eq major-mode 'todos-filtered-items-mode)
2933 (kill-buffer)
2934 (todos-show))
2935 ((member major-mode (list 'todos-mode 'todos-archive-mode))
2936 ;; Have to write previously nonexistant archives to file, and might
2937 ;; as well save Todos file also.
2938 (todos-save)
2939 (bury-buffer))))
2940
2941 (defun todos-print (&optional to-file)
2942 "Produce a printable version of the current Todos buffer.
2943 This converts overlays and soft line wrapping and, depending on
2944 the value of `todos-print-function', includes faces. With
2945 non-nil argument TO-FILE write the printable version to a file;
2946 otherwise, send it to the default printer."
2947 (interactive)
2948 (let ((buf todos-print-buffer)
2949 (header (cond
2950 ((eq major-mode 'todos-mode)
2951 (concat "Todos File: "
2952 (todos-short-file-name todos-current-todos-file)
2953 "\nCategory: " (todos-current-category)))
2954 ((eq major-mode 'todos-filtered-items-mode)
2955 "Todos Top Priorities")))
2956 (prefix (propertize (concat todos-prefix " ")
2957 'face 'todos-prefix-string))
2958 (num 0)
2959 (fill-prefix (make-string todos-indent-to-here 32))
2960 (content (buffer-string))
2961 file)
2962 (with-current-buffer (get-buffer-create buf)
2963 (insert content)
2964 (goto-char (point-min))
2965 (while (not (eobp))
2966 (let ((beg (point))
2967 (end (save-excursion (todos-item-end))))
2968 (when todos-number-priorities
2969 (setq num (1+ num))
2970 (setq prefix (propertize (concat (number-to-string num) " ")
2971 'face 'todos-prefix-string)))
2972 (insert prefix)
2973 (fill-region beg end))
2974 ;; Calling todos-forward-item infloops at todos-item-start due to
2975 ;; non-overlay prefix, so search for item start instead.
2976 (if (re-search-forward todos-item-start nil t)
2977 (beginning-of-line)
2978 (goto-char (point-max))))
2979 (if (re-search-backward (concat "^" (regexp-quote todos-category-done))
2980 nil t)
2981 (replace-match todos-done-separator))
2982 (goto-char (point-min))
2983 (insert header)
2984 (newline 2)
2985 (if to-file
2986 (let ((file (read-file-name "Print to file: ")))
2987 (funcall todos-print-function file))
2988 (funcall todos-print-function)))
2989 (kill-buffer buf)))
2990
2991 (defun todos-print-to-file ()
2992 "Save printable version of this Todos buffer to a file."
2993 (interactive)
2994 (todos-print t))
2995
2996 (defun todos-convert-legacy-files ()
2997 "Convert legacy Todo files to the current Todos format.
2998 The files `todo-file-do' and `todo-file-done' are converted and
2999 saved (the latter as a Todos Archive file) with a new name in
3000 `todos-files-directory'. See also the documentation string of
3001 `todos-todo-mode-date-time-regexp' for further details."
3002 (interactive)
3003 (if (fboundp 'todo-mode)
3004 (require 'todo-mode)
3005 (error "Void function `todo-mode'"))
3006 ;; Convert `todo-file-do'.
3007 (if (file-exists-p todo-file-do)
3008 (let ((default "todo-do-conv")
3009 file archive-sexp)
3010 (with-temp-buffer
3011 (insert-file-contents todo-file-do)
3012 (let ((end (search-forward ")" (line-end-position) t))
3013 (beg (search-backward "(" (line-beginning-position) t)))
3014 (setq todo-categories
3015 (read (buffer-substring-no-properties beg end))))
3016 (todo-mode)
3017 (delete-region (line-beginning-position) (1+ (line-end-position)))
3018 (while (not (eobp))
3019 (cond
3020 ((looking-at (regexp-quote (concat todo-prefix todo-category-beg)))
3021 (replace-match todos-category-beg))
3022 ((looking-at (regexp-quote todo-category-end))
3023 (replace-match ""))
3024 ((looking-at (regexp-quote (concat todo-prefix " "
3025 todo-category-sep)))
3026 (replace-match todos-category-done))
3027 ((looking-at (concat (regexp-quote todo-prefix) " "
3028 todos-todo-mode-date-time-regexp " "
3029 (regexp-quote todo-initials) ":"))
3030 (todos-convert-legacy-date-time)))
3031 (forward-line))
3032 (setq file (concat todos-files-directory
3033 (read-string
3034 (format "Save file as (default \"%s\"): " default)
3035 nil nil default)
3036 ".todo"))
3037 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3038 (with-temp-buffer
3039 (insert-file-contents file)
3040 (let ((todos-categories (todos-make-categories-list t)))
3041 (todos-update-categories-sexp))
3042 (write-region (point-min) (point-max) file nil 'nomessage))
3043 ;; Convert `todo-file-done'.
3044 (when (file-exists-p todo-file-done)
3045 (with-temp-buffer
3046 (insert-file-contents todo-file-done)
3047 (let ((beg (make-marker))
3048 (end (make-marker))
3049 cat cats comment item)
3050 (while (not (eobp))
3051 (when (looking-at todos-todo-mode-date-time-regexp)
3052 (set-marker beg (point))
3053 (todos-convert-legacy-date-time)
3054 (set-marker end (point))
3055 (goto-char beg)
3056 (insert "[" todos-done-string)
3057 (goto-char end)
3058 (insert "]")
3059 (forward-char)
3060 (when (looking-at todos-todo-mode-date-time-regexp)
3061 (todos-convert-legacy-date-time))
3062 (when (looking-at (concat " " (regexp-quote todo-initials) ":"))
3063 (replace-match "")))
3064 (if (re-search-forward
3065 (concat "^" todos-todo-mode-date-time-regexp) nil t)
3066 (goto-char (match-beginning 0))
3067 (goto-char (point-max)))
3068 (backward-char)
3069 (when (looking-back "\\[\\([^][]+\\)\\]")
3070 (setq cat (match-string 1))
3071 (goto-char (match-beginning 0))
3072 (replace-match ""))
3073 ;; If the item ends with a non-comment parenthesis not
3074 ;; followed by a period, we lose (but we inherit that problem
3075 ;; from todo-mode.el).
3076 (when (looking-back "(\\(.*\\)) ")
3077 (setq comment (match-string 1))
3078 (replace-match "")
3079 (insert "[" todos-comment-string ": " comment "]"))
3080 (set-marker end (point))
3081 (if (member cat cats)
3082 ;; If item is already in its category, leave it there.
3083 (unless (save-excursion
3084 (re-search-backward
3085 (concat "^" (regexp-quote todos-category-beg)
3086 "\\(.*\\)$") nil t)
3087 (string= (match-string 1) cat))
3088 ;; Else move it to its category.
3089 (setq item (buffer-substring-no-properties beg end))
3090 (delete-region beg (1+ end))
3091 (set-marker beg (point))
3092 (re-search-backward
3093 (concat "^" (regexp-quote (concat todos-category-beg cat)))
3094 nil t)
3095 (forward-line)
3096 (if (re-search-forward
3097 (concat "^" (regexp-quote todos-category-beg)
3098 "\\(.*\\)$") nil t)
3099 (progn (goto-char (match-beginning 0))
3100 (newline)
3101 (forward-line -1))
3102 (goto-char (point-max)))
3103 (insert item "\n")
3104 (goto-char beg))
3105 (push cat cats)
3106 (goto-char beg)
3107 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3108 (forward-line))
3109 (set-marker beg nil)
3110 (set-marker end nil))
3111 (setq file (concat (file-name-sans-extension file) ".toda"))
3112 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3113 (with-temp-buffer
3114 (insert-file-contents file)
3115 (let ((todos-categories (todos-make-categories-list t)))
3116 (todos-update-categories-sexp))
3117 (write-region (point-min) (point-max) file nil 'nomessage)
3118 (setq archive-sexp (read (buffer-substring-no-properties
3119 (line-beginning-position)
3120 (line-end-position)))))
3121 (setq file (concat (file-name-sans-extension file) ".todo"))
3122 ;; Update categories sexp of converted Todos file again, adding
3123 ;; counts of archived items.
3124 (with-temp-buffer
3125 (insert-file-contents file)
3126 (let ((sexp (read (buffer-substring-no-properties
3127 (line-beginning-position)
3128 (line-end-position)))))
3129 (dolist (cat sexp)
3130 (let ((archive-cat (assoc (car cat) archive-sexp)))
3131 (if archive-cat
3132 (aset (cdr cat) 3 (aref (cdr archive-cat) 2)))))
3133 (delete-region (line-beginning-position) (line-end-position))
3134 (prin1 sexp (current-buffer)))
3135 (write-region (point-min) (point-max) file nil 'nomessage)))
3136 (todos-reevaluate-filelist-defcustoms)
3137 (message "Format conversion done."))
3138 (error "No legacy Todo file exists")))
3139
3140 ;; ---------------------------------------------------------------------------
3141 ;;; Navigation Commands
3142
3143 (defun todos-forward-category (&optional back)
3144 "Visit the numerically next category in this Todos file.
3145 If the current category is the highest numbered, visit the first
3146 category. With non-nil argument BACK, visit the numerically
3147 previous category (the highest numbered one, if the current
3148 category is the first)."
3149 (interactive)
3150 (setq todos-category-number
3151 (1+ (mod (- todos-category-number (if back 2 0))
3152 (length todos-categories))))
3153 (when todos-skip-archived-categories
3154 (while (and (zerop (todos-get-count 'todo))
3155 (zerop (todos-get-count 'done))
3156 (not (zerop (todos-get-count 'archived))))
3157 (setq todos-category-number
3158 (apply (if back '1- '1+) (list todos-category-number)))))
3159 (todos-category-select)
3160 (goto-char (point-min)))
3161
3162 (defun todos-backward-category ()
3163 "Visit the numerically previous category in this Todos file.
3164 If the current category is the highest numbered, visit the first
3165 category."
3166 (interactive)
3167 (todos-forward-category t))
3168
3169 (defun todos-jump-to-category (&optional cat other-file)
3170 "Jump to a category in this or another Todos file.
3171
3172 Programmatically, optional argument CAT provides the category
3173 name. When nil (as in interactive calls), prompt for the
3174 category, with TAB completion on existing categories. If a
3175 non-existing category name is entered, ask whether to add a new
3176 category with this name; if affirmed, add it, then jump to that
3177 category. With non-nil argument OTHER-FILE, prompt for a Todos
3178 file, otherwise jump within the current Todos file."
3179 (interactive)
3180 (let ((file (or (and other-file
3181 (todos-read-file-name "Choose a Todos file: " nil t))
3182 ;; Jump to archived-only Categories from Todos Categories
3183 ;; mode.
3184 (and cat
3185 todos-skip-archived-categories
3186 (zerop (todos-get-count 'todo cat))
3187 (zerop (todos-get-count 'done cat))
3188 (not (zerop (todos-get-count 'archived cat)))
3189 (concat (file-name-sans-extension
3190 todos-current-todos-file) ".toda"))
3191 todos-current-todos-file
3192 ;; If invoked from outside of Todos mode before
3193 ;; todos-show...
3194 todos-default-todos-file)))
3195 (with-current-buffer (find-file-noselect file)
3196 (and other-file (setq todos-current-todos-file file))
3197 (let ((category (or (and (assoc cat todos-categories) cat)
3198 (todos-read-category "Jump to category: "))))
3199 ;; Clean up after selecting category in Todos Categories mode.
3200 (if (string= (buffer-name) todos-categories-buffer)
3201 (kill-buffer))
3202 (if (or cat other-file)
3203 (set-window-buffer (selected-window)
3204 (set-buffer (find-buffer-visiting file))))
3205 (unless todos-global-current-todos-file
3206 (setq todos-global-current-todos-file todos-current-todos-file))
3207 (todos-category-number category) ; (1+ (length t-c)) if new category.
3208 ;; (if (> todos-category-number (length todos-categories))
3209 ;; (setq todos-category-number (todos-add-category category)))
3210 (todos-category-select)
3211 (goto-char (point-min))))))
3212
3213 (defun todos-jump-to-category-other-file ()
3214 "Jump to a category in another Todos file.
3215 The category is chosen by prompt, with TAB completion."
3216 (interactive)
3217 (todos-jump-to-category nil t))
3218
3219 (defun todos-jump-to-any-category ()
3220 ""
3221 (interactive)
3222 (let* ((cats-alist todos-all-categories-alist)
3223 (cats (mapcar 'car cats-alist))
3224 (completion-ignore-case todos-completion-ignore-case)
3225 (cat (completing-read "Jump to category: " cats nil t))
3226 (files (if (zerop (length cat))
3227 (keyboard-quit)
3228 (cdr (assoc cat cats-alist))))
3229 (file (if (nlistp files)
3230 files
3231 (completing-read (format "Jump to \"%s\" in which file? " cat)
3232 files nil t))))
3233 (if (zerop (length file))
3234 (keyboard-quit)
3235 (setq file (concat todos-files-directory file ".todo"))
3236 (set-window-buffer (selected-window)
3237 (set-buffer (find-file-noselect file)))
3238 (unless todos-global-current-todos-file
3239 (setq todos-global-current-todos-file todos-current-todos-file))
3240 (todos-category-number cat)
3241 (todos-category-select)
3242 (goto-char (point-min)))))
3243
3244 (defun todos-jump-to-item ()
3245 "Jump to the file and category of the filtered item at point."
3246 (interactive)
3247 (let ((str (todos-item-string))
3248 (buf (current-buffer))
3249 cat file archive beg)
3250 (string-match (concat (if todos-filter-done-items
3251 (concat "\\(?:" todos-done-string-start "\\|"
3252 todos-date-string-start "\\)")
3253 todos-date-string-start)
3254 todos-date-pattern "\\(?: " diary-time-regexp "\\)?"
3255 (if todos-filter-done-items
3256 "\\]"
3257 (regexp-quote todos-nondiary-end)) "?"
3258 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
3259 "\\(?1:.*\\)\\]\\).*$") str)
3260 (setq cat (match-string 1 str))
3261 (setq file (match-string 2 str))
3262 (setq archive (string= (match-string 3 str) "(archive) "))
3263 (setq str (replace-match "" nil nil str 4))
3264 (setq file (if file
3265 (concat todos-files-directory (substring file 0 -1)
3266 (if archive ".toda" ".todo"))
3267 (if archive
3268 (concat (file-name-sans-extension
3269 todos-global-current-todos-file) ".toda")
3270 todos-global-current-todos-file)))
3271 (find-file-noselect file)
3272 (with-current-buffer (find-buffer-visiting file)
3273 (widen)
3274 (goto-char (point-min))
3275 (re-search-forward
3276 (concat "^" (regexp-quote (concat todos-category-beg cat))) nil t)
3277 (search-forward str)
3278 (setq beg (match-beginning 0)))
3279 (kill-buffer buf)
3280 (set-window-buffer (selected-window) (set-buffer (find-buffer-visiting file)))
3281 (setq todos-current-todos-file file)
3282 (setq todos-category-number (todos-category-number cat))
3283 (let ((todos-show-with-done (if todos-filter-done-items t
3284 todos-show-with-done)))
3285 (todos-category-select))
3286 (goto-char beg)))
3287
3288 (defun todos-forward-item (&optional count)
3289 "Move point down to start of item with next lower priority.
3290 With positive numerical prefix COUNT, move point COUNT items
3291 downward."
3292 (interactive "P")
3293 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3294 ;; the corresponding command.
3295 (if (and count (> 1 count))
3296 (error "This command only accepts a positive numerical prefix argument")
3297 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
3298 (start (line-end-position)))
3299 (goto-char start)
3300 (if (re-search-forward todos-item-start nil t (or count 1))
3301 (goto-char (match-beginning 0))
3302 (goto-char (point-max)))
3303 ;; If points advances by one from a todo to a done item, go back to the
3304 ;; space above todos-done-separator, since that is a legitimate place to
3305 ;; insert an item. But skip this space if count > 1, since that should
3306 ;; only stop on an item.
3307 (when (and not-done (todos-done-item-p))
3308 (if (or (not count) (= count 1))
3309 (re-search-backward "^$" start t))))))
3310 ;; FIXME: The preceding sexp is insufficient when buffer is not narrowed,
3311 ;; since there could be no done items in this category, so the search puts
3312 ;; us on first todo item of next category. Does this ever happen? If so:
3313 ;; (let ((opoint) (point))
3314 ;; (forward-line -1)
3315 ;; (when (or (not count) (= count 1))
3316 ;; (cond ((looking-at (concat "^" (regexp-quote todos-category-beg)))
3317 ;; (forward-line -2))
3318 ;; ((looking-at (concat "^" (regexp-quote todos-category-done)))
3319 ;; (forward-line -1))
3320 ;; (t
3321 ;; (goto-char opoint)))))))
3322
3323 (defun todos-backward-item (&optional count)
3324 "Move point up to start of item with next higher priority.
3325 With positive numerical prefix COUNT, move point COUNT items
3326 upward."
3327 (interactive "P")
3328 ;; Avoid moving to bob if on the first item but not at bob.
3329 (when (> (line-number-at-pos) 1)
3330 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3331 ;; the corresponding command.
3332 (if (and count (> 1 count))
3333 (error "This command only accepts a positive numerical prefix argument")
3334 (let* ((done (todos-done-item-p)))
3335 (todos-item-start)
3336 (unless (bobp)
3337 (re-search-backward todos-item-start nil t (or count 1)))
3338 ;; Unless this is a regexp filtered items buffer (which can contain
3339 ;; intermixed todo and done items), if points advances by one from a
3340 ;; done to a todo item, go back to the space above
3341 ;; todos-done-separator, since that is a legitimate place to insert an
3342 ;; item. But skip this space if count > 1, since that should only
3343 ;; stop on an item.
3344 (when (and done (not (todos-done-item-p)) (or (not count) (= count 1))
3345 (not (equal (buffer-name) todos-regexp-items-buffer)))
3346 (re-search-forward (concat "^" (regexp-quote todos-category-done))
3347 nil t)
3348 (forward-line -1))))))
3349
3350 (defun todos-forward-button (n &optional wrap display-message)
3351 ""
3352 (interactive "p\nd\nd")
3353 (forward-button n wrap display-message)
3354 (and (bolp) (button-at (point))
3355 ;; Align with beginning of category label.
3356 (forward-char (+ 4 (length todos-categories-number-separator)))))
3357
3358 (defun todos-backward-button (n &optional wrap display-message)
3359 ""
3360 (interactive "p\nd\nd")
3361 (backward-button n wrap display-message)
3362 (and (bolp) (button-at (point))
3363 ;; Align with beginning of category label.
3364 (forward-char (+ 4 (length todos-categories-number-separator)))))
3365
3366 ;; FIXME: (i) Extend search to other Todos files. (ii) Allow navigating among
3367 ;; hits. (But these features are effectively available with
3368 ;; todos-regexp-items-multifile, so maybe it's not worth the trouble here.)
3369 (defun todos-search ()
3370 "Search for a regular expression in this Todos file.
3371 The search runs through the whole file and encompasses all and
3372 only todo and done items; it excludes category names. Multiple
3373 matches are shown sequentially, highlighted in `todos-search'
3374 face."
3375 (interactive)
3376 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3377 (opoint (point))
3378 matches match cat in-done ov mlen msg)
3379 (widen)
3380 (goto-char (point-min))
3381 (while (not (eobp))
3382 (setq match (re-search-forward regex nil t))
3383 (goto-char (line-beginning-position))
3384 (unless (or (equal (point) 1)
3385 (looking-at (concat "^" (regexp-quote todos-category-beg))))
3386 (if match (push match matches)))
3387 (forward-line))
3388 (setq matches (reverse matches))
3389 (if matches
3390 (catch 'stop
3391 (while matches
3392 (setq match (pop matches))
3393 (goto-char match)
3394 (todos-item-start)
3395 (when (looking-at todos-done-string-start)
3396 (setq in-done t))
3397 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
3398 "\\(.*\\)\n") nil t)
3399 (setq cat (match-string-no-properties 1))
3400 (todos-category-number cat)
3401 (todos-category-select)
3402 (if in-done
3403 (unless todos-show-with-done (todos-hide-show-done-items)))
3404 (goto-char match)
3405 (setq ov (make-overlay (- (point) (length regex)) (point)))
3406 (overlay-put ov 'face 'todos-search)
3407 (when matches
3408 (setq mlen (length matches))
3409 (if (y-or-n-p
3410 (if (> mlen 1)
3411 (format "There are %d more matches; go to next match? "
3412 mlen)
3413 "There is one more match; go to it? "))
3414 (widen)
3415 (throw 'stop (setq msg (if (> mlen 1)
3416 (format "There are %d more matches."
3417 mlen)
3418 "There is one more match."))))))
3419 (setq msg "There are no more matches."))
3420 (todos-category-select)
3421 (goto-char opoint)
3422 (message "No match for \"%s\"" regex))
3423 (when msg
3424 (if (y-or-n-p (concat msg "\nUnhighlight matches? "))
3425 (todos-clear-matches)
3426 (message "You can unhighlight the matches later by typing %s"
3427 (key-description (car (where-is-internal
3428 'todos-clear-matches))))))))
3429
3430 (defun todos-clear-matches ()
3431 "Remove highlighting on matches found by todos-search."
3432 (interactive)
3433 (remove-overlays 1 (1+ (buffer-size)) 'face 'todos-search))
3434
3435 ;; ---------------------------------------------------------------------------
3436 ;;; Display Commands
3437
3438 (defun todos-hide-show-item-numbering ()
3439 ""
3440 (interactive)
3441 (todos-reset-prefix 'todos-number-priorities (not todos-number-priorities)))
3442
3443 (defun todos-hide-show-done-items ()
3444 "Show hidden or hide visible done items in current category."
3445 (interactive)
3446 (if (zerop (todos-get-count 'done (todos-current-category)))
3447 (message "There are no done items in this category.")
3448 (save-excursion
3449 (goto-char (point-min))
3450 (let ((todos-show-with-done (not (re-search-forward
3451 todos-done-string-start nil t))))
3452 (todos-category-select)))))
3453
3454 (defun todos-show-done-only ()
3455 "Switch between displaying only done or only todo items."
3456 (interactive)
3457 (setq todos-show-done-only (not todos-show-done-only))
3458 (todos-category-select))
3459
3460 (defun todos-highlight-item ()
3461 "Highlight or unhighlight the todo item the cursor is on."
3462 (interactive)
3463 (require 'hl-line)
3464 (if hl-line-mode
3465 (hl-line-mode -1)
3466 (hl-line-mode 1)))
3467
3468 (defun todos-hide-show-date-time ()
3469 "Hide or show date-time header of todo items in the current file."
3470 (interactive)
3471 (save-excursion
3472 (save-restriction
3473 (goto-char (point-min))
3474 (let ((ovs (overlays-in (point) (1+ (point))))
3475 ov hidden)
3476 (while ovs
3477 (setq ov (pop ovs))
3478 (if (equal (overlay-get ov 'display) "")
3479 (setq ovs nil hidden t)))
3480 (widen)
3481 (goto-char (point-min))
3482 (if hidden
3483 (remove-overlays (point-min) (point-max) 'display "")
3484 (while (not (eobp))
3485 (when (re-search-forward
3486 (concat todos-date-string-start todos-date-pattern
3487 "\\( " diary-time-regexp "\\)?"
3488 (regexp-quote todos-nondiary-end) "? ")
3489 nil t)
3490 (unless (save-match-data (todos-done-item-p))
3491 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
3492 (overlay-put ov 'display "")))
3493 (todos-forward-item)))))))
3494
3495 (defun todos-mark-unmark-item (&optional n all)
3496 "Mark item at point if unmarked, or unmark it if marked.
3497
3498 With a positive numerical prefix argument N, change the
3499 markedness of the next N items. With non-nil argument ALL, mark
3500 all visible items in the category (depending on visibility, all
3501 todo and done items, or just todo or just done items).
3502
3503 The mark is the character \"*\" inserted in front of the item's
3504 priority number or the `todos-prefix' string; if `todos-prefix'
3505 is \"*\", then the mark is \"@\"."
3506 (interactive "p")
3507 (if all (goto-char (point-min)))
3508 (unless (> n 0) (setq n 1))
3509 (let ((i 0))
3510 (while (or (and all (not (eobp)))
3511 (< i n))
3512 (let* ((cat (todos-current-category))
3513 (ov (todos-marked-item-p))
3514 (marked (assoc cat todos-categories-with-marks)))
3515 (if (and ov (not all))
3516 (progn
3517 (delete-overlay ov)
3518 (if (= (cdr marked) 1) ; Deleted last mark in this category.
3519 (setq todos-categories-with-marks
3520 (assq-delete-all cat todos-categories-with-marks))
3521 (setcdr marked (1- (cdr marked)))))
3522 (when (todos-item-start)
3523 (unless (and all (todos-marked-item-p))
3524 (setq ov (make-overlay (point) (point)))
3525 (overlay-put ov 'before-string todos-item-mark)
3526 (if marked
3527 (setcdr marked (1+ (cdr marked)))
3528 (push (cons cat 1) todos-categories-with-marks))))))
3529 (todos-forward-item)
3530 (setq i (1+ i)))))
3531
3532 (defun todos-mark-category ()
3533 "Put the \"*\" mark on all items in this category.
3534 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
3535 (interactive)
3536 (todos-mark-unmark-item 0 t))
3537
3538 (defun todos-unmark-category ()
3539 "Remove the \"*\" mark from all items in this category.
3540 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
3541 (interactive)
3542 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
3543 (setq todos-categories-with-marks
3544 (delq (assoc (todos-current-category) todos-categories-with-marks)
3545 todos-categories-with-marks)))
3546
3547 ;; ---------------------------------------------------------------------------
3548 ;;; Item filtering commands
3549
3550 (defun todos-set-top-priorities-in-file ()
3551 "Set number of top priorities for this file.
3552 See `todos-set-top-priorities' for more details."
3553 (interactive)
3554 (todos-set-top-priorities))
3555
3556 (defun todos-set-top-priorities-in-category ()
3557 "Set number of top priorities for this category.
3558 See `todos-set-top-priorities' for more details."
3559 (interactive)
3560 (todos-set-top-priorities t))
3561
3562 (defun todos-top-priorities (&optional num)
3563 "List top priorities of each category in `todos-filter-files'.
3564 Number of entries for each category is given by NUM, which
3565 defaults to `todos-show-priorities'."
3566 (interactive "P")
3567 (let ((arg (if num (cons 'top num) 'top))
3568 (buf todos-top-priorities-buffer)
3569 (file todos-current-todos-file))
3570 (todos-filter-items arg)
3571 (todos-filtered-buffer-name buf file)))
3572
3573 (defun todos-top-priorities-multifile (&optional arg)
3574 "List top priorities of each category in `todos-filter-files'.
3575
3576 If the prefix argument ARG is a number, this is the maximum
3577 number of top priorities to list in each category. If the prefix
3578 argument is `C-u', prompt for which files to filter and use
3579 `todos-show-priorities' as the number of top priorities to list
3580 in each category. If the prefix argument is `C-uC-u', prompt
3581 both for which files to filter and for how many top priorities to
3582 list in each category."
3583 (interactive "P")
3584 (let* ((buf todos-top-priorities-buffer)
3585 files
3586 (pref (if (numberp arg)
3587 (cons 'top arg)
3588 (setq files (if (or (consp arg)
3589 (null todos-filter-files))
3590 (progn (todos-multiple-filter-files)
3591 todos-multiple-filter-files)
3592 todos-filter-files))
3593 (if (equal arg '(16))
3594 (cons 'top (read-number
3595 "Enter number of top priorities to show: "
3596 todos-show-priorities))
3597 'top))))
3598 (todos-filter-items pref t)
3599 (todos-filtered-buffer-name buf files)))
3600
3601 (defun todos-diary-items ()
3602 "Display todo items for diary inclusion in this Todos file."
3603 (interactive)
3604 (let ((buf todos-diary-items-buffer)
3605 (file todos-current-todos-file))
3606 (todos-filter-items 'diary)
3607 (todos-filtered-buffer-name buf file)))
3608
3609 (defun todos-diary-items-multifile (&optional arg)
3610 "Display todo items for diary inclusion in one or more Todos file.
3611 The files are those listed in `todos-filter-files'."
3612 (interactive "P")
3613 (let ((buf todos-diary-items-buffer)
3614 (files (if (or arg (null todos-filter-files))
3615 (progn (todos-multiple-filter-files)
3616 todos-multiple-filter-files)
3617 todos-filter-files)))
3618 (todos-filter-items 'diary t)
3619 (todos-filtered-buffer-name buf files)))
3620
3621 (defun todos-regexp-items ()
3622 "Display todo items matching a user-entered regular expression.
3623 The items are those in the current Todos file."
3624 (interactive)
3625 (let ((buf todos-regexp-items-buffer)
3626 (file todos-current-todos-file))
3627 (todos-filter-items 'regexp)
3628 (todos-filtered-buffer-name buf file)))
3629
3630 (defun todos-regexp-items-multifile (&optional arg)
3631 "Display todo items matching a user-entered regular expression.
3632 The items are those in the files listed in `todos-filter-files'."
3633 (interactive "P")
3634 (let ((buf todos-regexp-items-buffer)
3635 (files (if (or arg (null todos-filter-files))
3636 (progn (todos-multiple-filter-files)
3637 todos-multiple-filter-files)
3638 todos-filter-files)))
3639 (todos-filter-items 'regexp t)
3640 (todos-filtered-buffer-name buf files)))
3641
3642 ;;; Editing Commands
3643
3644 (defun todos-add-file ()
3645 "Name and add a new Todos file.
3646 Interactively, prompt for a category and display it.
3647 Noninteractively, return the name of the new file."
3648 (interactive)
3649 (let ((prompt (concat "Enter name of new Todos file "
3650 "(TAB or SPC to see current names): "))
3651 file)
3652 (setq file (todos-read-file-name prompt))
3653 (with-current-buffer (get-buffer-create file)
3654 (erase-buffer)
3655 (write-region (point-min) (point-max) file nil 'nomessage nil t)
3656 (kill-buffer file))
3657 (todos-reevaluate-filelist-defcustoms)
3658 (if (called-interactively-p)
3659 (progn
3660 (set-window-buffer (selected-window)
3661 (set-buffer (find-file-noselect file)))
3662 (setq todos-current-todos-file file)
3663 (todos-show))
3664 file)))
3665
3666 ;; ---------------------------------------------------------------------------
3667 ;;; Category editing commands
3668
3669 (defun todos-add-category (&optional cat)
3670 "Add a new category to the current Todos file.
3671 Called interactively, prompts for category name, then visits the
3672 category in Todos mode. Non-interactively, argument CAT provides
3673 the category name and the return value is the category number."
3674 (interactive)
3675 (let* ((buffer-read-only)
3676 (num (1+ (length todos-categories)))
3677 (counts (make-vector 4 0))) ; [todo diary done archived]
3678 (unless cat
3679 (setq cat (todos-read-category "Enter new category name: " nil t)))
3680 (setq todos-categories (append todos-categories (list (cons cat counts))))
3681 (widen)
3682 (goto-char (point-max))
3683 (save-excursion ; Save point for todos-category-select.
3684 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3685 (todos-update-categories-sexp)
3686 ;; If invoked by user, display the newly added category, if called
3687 ;; programmatically return the category number to the caller.
3688 (if (called-interactively-p 'any)
3689 (progn
3690 (setq todos-category-number num)
3691 (todos-category-select))
3692 num)))
3693
3694 (defun todos-rename-category ()
3695 "Rename current Todos category.
3696 If this file has an archive containing this category, rename the
3697 category there as well."
3698 (interactive)
3699 (let* ((cat (todos-current-category))
3700 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat))))
3701 (setq new (todos-validate-name new 'category))
3702 (let* ((ofile todos-current-todos-file)
3703 (archive (concat (file-name-sans-extension ofile) ".toda"))
3704 (buffers (append (list ofile)
3705 (unless (zerop (todos-get-count 'archived cat))
3706 (list archive)))))
3707 (dolist (buf buffers)
3708 (with-current-buffer (find-file-noselect buf)
3709 (let (buffer-read-only)
3710 (setq todos-categories (todos-set-categories))
3711 (save-excursion
3712 (save-restriction
3713 (setcar (assoc cat todos-categories) new)
3714 (widen)
3715 (goto-char (point-min))
3716 (todos-update-categories-sexp)
3717 (re-search-forward (concat (regexp-quote todos-category-beg)
3718 "\\(" (regexp-quote cat) "\\)\n")
3719 nil t)
3720 (replace-match new t t nil 1)))))))
3721 (force-mode-line-update))
3722 (save-excursion (todos-category-select)))
3723
3724 (defun todos-delete-category (&optional arg)
3725 "Delete current Todos category provided it is empty.
3726 With ARG non-nil delete the category unconditionally,
3727 i.e. including all existing todo and done items."
3728 (interactive "P")
3729 (let* ((file todos-current-todos-file)
3730 (cat (todos-current-category))
3731 (todo (todos-get-count 'todo cat))
3732 (done (todos-get-count 'done cat))
3733 (archived (todos-get-count 'archived cat)))
3734 (if (and (not arg)
3735 (or (> todo 0) (> done 0)))
3736 (message "%s" (substitute-command-keys
3737 (concat "To delete a non-empty category, "
3738 "type C-u \\[todos-delete-category].")))
3739 (when (cond ((= (length todos-categories) 1)
3740 (y-or-n-p (concat "This is the only category in this file; "
3741 "deleting it will also delete the file.\n"
3742 "Do you want to proceed? ")))
3743 ((> archived 0)
3744 (y-or-n-p (concat "This category has archived items; "
3745 "the archived category will remain\n"
3746 "after deleting the todo category. "
3747 "Do you still want to delete it\n"
3748 "(see 'todos-skip-archived-categories' "
3749 "for another option)? ")))
3750 (t
3751 (y-or-n-p (concat "Permanently remove category \"" cat
3752 "\"" (and arg " and all its entries")
3753 "? "))))
3754 (widen)
3755 (let ((buffer-read-only)
3756 (beg (re-search-backward
3757 (concat "^" (regexp-quote (concat todos-category-beg cat))
3758 "\n") nil t))
3759 (end (if (re-search-forward
3760 (concat "\n\\(" (regexp-quote todos-category-beg)
3761 ".*\n\\)") nil t)
3762 (match-beginning 1)
3763 (point-max))))
3764 (remove-overlays beg end)
3765 (delete-region beg end)
3766 (if (= (length todos-categories) 1)
3767 ;; If deleted category was the only one, delete the file.
3768 (progn
3769 (todos-reevaluate-filelist-defcustoms)
3770 ;; Skip confirming killing the archive buffer if it has been
3771 ;; modified and not saved.
3772 (set-buffer-modified-p nil)
3773 (delete-file file)
3774 (kill-buffer)
3775 (message "Deleted Todos file %s." file))
3776 (setq todos-categories (delete (assoc cat todos-categories)
3777 todos-categories))
3778 (todos-update-categories-sexp)
3779 (setq todos-category-number
3780 (1+ (mod todos-category-number (length todos-categories))))
3781 (todos-category-select)
3782 (goto-char (point-min))
3783 (message "Deleted category %s." cat)))))))
3784
3785 (defun todos-move-category ()
3786 "Move current category to a different Todos file.
3787 If current category has archived items, also move those to the
3788 archive of the file moved to, creating it if it does not exist."
3789 (interactive)
3790 (when (or (> (length todos-categories) 1)
3791 (y-or-n-p (concat "This is the only category in this file; "
3792 "moving it will also delete the file.\n"
3793 "Do you want to proceed? ")))
3794 (let* ((ofile todos-current-todos-file)
3795 (cat (todos-current-category))
3796 (nfile (todos-read-file-name
3797 "Choose a Todos file to move this category to: " nil t))
3798 (archive (concat (file-name-sans-extension ofile) ".toda"))
3799 (buffers (append (list ofile)
3800 (unless (zerop (todos-get-count 'archived cat))
3801 (list archive))))
3802 new)
3803 (while (equal (file-truename nfile) (file-truename ofile))
3804 (setq nfile (todos-read-file-name
3805 "Choose a file distinct from this file: " nil t)))
3806 (dolist (buf buffers)
3807 (with-current-buffer (find-file-noselect buf)
3808 (widen)
3809 (goto-char (point-max))
3810 (let* ((beg (re-search-backward
3811 (concat "^"
3812 (regexp-quote (concat todos-category-beg cat)))
3813 nil t))
3814 (end (if (re-search-forward
3815 (concat "^" (regexp-quote todos-category-beg))
3816 nil t 2)
3817 (match-beginning 0)
3818 (point-max)))
3819 (content (buffer-substring-no-properties beg end))
3820 (counts (cdr (assoc cat todos-categories)))
3821 buffer-read-only)
3822 ;; Move the category to the new file. Also update or create
3823 ;; archive file if necessary.
3824 (with-current-buffer
3825 (find-file-noselect
3826 ;; Regenerate todos-archives in case there
3827 ;; is a newly created archive.
3828 (if (member buf (funcall todos-files-function t))
3829 (concat (file-name-sans-extension nfile) ".toda")
3830 nfile))
3831 (let* ((nfile-short (todos-short-file-name nfile))
3832 (prompt (concat
3833 (format "Todos file \"%s\" already has "
3834 nfile-short)
3835 (format "the category \"%s\";\n" cat)
3836 "enter a new category name: "))
3837 buffer-read-only)
3838 (widen)
3839 (goto-char (point-max))
3840 (insert content)
3841 ;; If the file moved to has a category with the same
3842 ;; name, rename the moved category.
3843 (when (assoc cat todos-categories)
3844 (unless (member (file-truename (buffer-file-name))
3845 (funcall todos-files-function t))
3846 (setq new (read-from-minibuffer prompt))
3847 (setq new (todos-validate-name new 'category))))
3848 ;; Replace old with new name in Todos and archive files.
3849 (when new
3850 (goto-char (point-max))
3851 (re-search-backward
3852 (concat "^" (regexp-quote todos-category-beg)
3853 "\\(" (regexp-quote cat) "\\)") nil t)
3854 (replace-match new nil nil nil 1)))
3855 (setq todos-categories
3856 (append todos-categories (list (cons new counts))))
3857 (todos-update-categories-sexp)
3858 ;; If archive was just created, save it to avoid "File <xyz> no
3859 ;; longer exists!" message on invoking
3860 ;; `todos-view-archived-items'. FIXME: maybe better to save
3861 ;; unconditionally?
3862 (unless (file-exists-p (buffer-file-name))
3863 (save-buffer))
3864 (todos-category-number (or new cat))
3865 (todos-category-select))
3866 ;; Delete the category from the old file, and if that was the
3867 ;; last category, delete the file. Also handle archive file
3868 ;; if necessary.
3869 (remove-overlays beg end)
3870 (delete-region beg end)
3871 (goto-char (point-min))
3872 ;; Put point after todos-categories sexp.
3873 (forward-line)
3874 (if (eobp) ; Aside from sexp, file is empty.
3875 (progn
3876 ;; Skip confirming killing the archive buffer.
3877 (set-buffer-modified-p nil)
3878 (delete-file todos-current-todos-file)
3879 (kill-buffer)
3880 (when (member todos-current-todos-file todos-files)
3881 (todos-reevaluate-filelist-defcustoms)))
3882 (setq todos-categories (delete (assoc cat todos-categories)
3883 todos-categories))
3884 (todos-update-categories-sexp)
3885 (todos-category-select)))))
3886 (set-window-buffer (selected-window)
3887 (set-buffer (find-file-noselect nfile)))
3888 (todos-category-number (or new cat))
3889 (todos-category-select))))
3890
3891 (defun todos-merge-category ()
3892 "Merge current category into another category in this file.
3893
3894 The current category's todo and done items are appended to the
3895 chosen goal category's todo and done items, respectively. The
3896 goal category becomes the current category, and the previous
3897 current category is deleted.
3898
3899 If both the first and goal categories also have archived items,
3900 the former are merged to the latter. If only the first category
3901 has archived items, the archived category is renamed to the goal
3902 category."
3903 (interactive)
3904 (let* ((tfile todos-current-todos-file)
3905 (archive (concat (file-name-sans-extension tfile) ".toda"))
3906 (cat (todos-current-category))
3907 (goal (todos-read-category "Category to merge to: " t))
3908 archived-count here)
3909 ;; Merge in todo file.
3910 (with-current-buffer (get-buffer (find-file-noselect tfile))
3911 (widen)
3912 (let* ((buffer-read-only nil)
3913 (cbeg (progn
3914 (re-search-backward
3915 (concat "^" (regexp-quote todos-category-beg)) nil t)
3916 (point-marker)))
3917 (tbeg (progn (forward-line) (point-marker)))
3918 (dbeg (progn
3919 (re-search-forward
3920 (concat "^" (regexp-quote todos-category-done)) nil t)
3921 (forward-line) (point-marker)))
3922 ;; Omit empty line between todo and done items.
3923 (tend (progn (forward-line -2) (point-marker)))
3924 (cend (progn
3925 (if (re-search-forward
3926 (concat "^" (regexp-quote todos-category-beg)) nil t)
3927 (progn
3928 (goto-char (match-beginning 0))
3929 (point-marker))
3930 (point-max-marker))))
3931 (todo (buffer-substring-no-properties tbeg tend))
3932 (done (buffer-substring-no-properties dbeg cend)))
3933 (goto-char (point-min))
3934 ;; Merge any todo items.
3935 (unless (zerop (length todo))
3936 (re-search-forward
3937 (concat "^" (regexp-quote (concat todos-category-beg goal))) nil t)
3938 (re-search-forward
3939 (concat "^" (regexp-quote todos-category-done)) nil t)
3940 (forward-line -1)
3941 (setq here (point-marker))
3942 (insert todo)
3943 (todos-update-count 'todo (todos-get-count 'todo cat) goal))
3944 ;; Merge any done items.
3945 (unless (zerop (length done))
3946 (goto-char (if (re-search-forward
3947 (concat "^" (regexp-quote todos-category-beg)) nil t)
3948 (match-beginning 0)
3949 (point-max)))
3950 (when (zerop (length todo)) (setq here (point-marker)))
3951 (insert done)
3952 (todos-update-count 'done (todos-get-count 'done cat) goal))
3953 (remove-overlays cbeg cend)
3954 (delete-region cbeg cend)
3955 (setq todos-categories (delete (assoc cat todos-categories)
3956 todos-categories))
3957 (todos-update-categories-sexp)
3958 (mapc (lambda (m) (set-marker m nil)) (list cbeg tbeg dbeg tend cend))))
3959 (when (file-exists-p archive)
3960 ;; Merge in archive file.
3961 (with-current-buffer (get-buffer (find-file-noselect archive))
3962 (widen)
3963 (goto-char (point-min))
3964 (let ((buffer-read-only nil)
3965 (cbeg (save-excursion
3966 (when (re-search-forward
3967 (concat "^" (regexp-quote
3968 (concat todos-category-beg cat)))
3969 nil t)
3970 (goto-char (match-beginning 0))
3971 (point-marker))))
3972 (gbeg (save-excursion
3973 (when (re-search-forward
3974 (concat "^" (regexp-quote
3975 (concat todos-category-beg goal)))
3976 nil t)
3977 (goto-char (match-beginning 0))
3978 (point-marker))))
3979 cend carch)
3980 (when cbeg
3981 (setq archived-count (todos-get-count 'done cat))
3982 (setq cend (save-excursion
3983 (if (re-search-forward
3984 (concat "^" (regexp-quote todos-category-beg))
3985 nil t)
3986 (match-beginning 0)
3987 (point-max))))
3988 (setq carch (save-excursion (goto-char cbeg) (forward-line)
3989 (buffer-substring-no-properties (point) cend)))
3990 ;; If both categories of the merge have archived items, merge the
3991 ;; source items to the goal items, else "merge" by renaming the
3992 ;; source category to goal.
3993 (if gbeg
3994 (progn
3995 (goto-char (if (re-search-forward
3996 (concat "^" (regexp-quote todos-category-beg))
3997 nil t)
3998 (match-beginning 0)
3999 (point-max)))
4000 (insert carch)
4001 (remove-overlays cbeg cend)
4002 (delete-region cbeg cend))
4003 (goto-char cbeg)
4004 (search-forward cat)
4005 (replace-match goal))
4006 (setq todos-categories (todos-make-categories-list t))
4007 (todos-update-categories-sexp)))))
4008 (with-current-buffer (get-file-buffer tfile)
4009 (when archived-count
4010 (unless (zerop archived-count)
4011 (todos-update-count 'archived archived-count goal)
4012 (todos-update-categories-sexp)))
4013 (todos-category-number goal)
4014 ;; If there are only merged done items, show them.
4015 (let ((todos-show-with-done (zerop (todos-get-count 'todo goal))))
4016 (todos-category-select)
4017 ;; Put point on the first merged item.
4018 (goto-char here)))
4019 (set-marker here nil)))
4020
4021 (defun todos-set-category-priority (&optional arg)
4022 "Change priority of category at point in Todos Categories buffer.
4023
4024 With ARG nil, prompt for the new priority number. Alternatively,
4025 the new priority can be provided by a numerical prefix ARG.
4026 Otherwise, if ARG is either of the symbols `raise' or `lower',
4027 raise or lower the category's priority by one."
4028 (interactive "P")
4029 (let ((curnum (save-excursion
4030 ;; Get the number representing the priority of the category
4031 ;; on the current line.
4032 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
4033 (when curnum ; Do nothing if we're not on a category line.
4034 (let* ((maxnum (length todos-categories))
4035 (prompt (format "Set category priority (1-%d): " maxnum))
4036 (col (current-column))
4037 (buffer-read-only nil)
4038 (priority (cond ((and (eq arg 'raise) (> curnum 1))
4039 (1- curnum))
4040 ((and (eq arg 'lower) (< curnum maxnum))
4041 (1+ curnum))))
4042 candidate)
4043 (while (not priority)
4044 (setq candidate (or arg (read-number prompt)))
4045 (setq arg nil)
4046 (setq prompt
4047 (cond ((or (< candidate 1) (> candidate maxnum))
4048 (format "Priority must be an integer between 1 and %d: "
4049 maxnum))
4050 ((= candidate curnum)
4051 "Choose a different priority than the current one: ")))
4052 (unless prompt (setq priority candidate)))
4053 (let* ((lower (< curnum priority)) ; Priority is being lowered.
4054 (head (butlast todos-categories
4055 (apply (if lower 'identity '1+)
4056 (list (- maxnum priority)))))
4057 (tail (nthcdr (apply (if lower 'identity '1-) (list priority))
4058 todos-categories))
4059 ;; Category's name and items counts list.
4060 (catcons (nth (1- curnum) todos-categories))
4061 (todos-categories (nconc head (list catcons) tail))
4062 newcats)
4063 (when lower (setq todos-categories (nreverse todos-categories)))
4064 (setq todos-categories (delete-dups todos-categories))
4065 (when lower (setq todos-categories (nreverse todos-categories)))
4066 (setq newcats todos-categories)
4067 (kill-buffer)
4068 (with-current-buffer (find-buffer-visiting todos-current-todos-file)
4069 (setq todos-categories newcats)
4070 (todos-update-categories-sexp))
4071 (todos-display-categories)
4072 (forward-line (1+ priority))
4073 (forward-char col))))))
4074
4075 (defun todos-raise-category-priority ()
4076 "Raise priority of category at point in Todos Categories buffer."
4077 (interactive)
4078 (todos-set-category-priority 'raise))
4079
4080 (defun todos-lower-category-priority ()
4081 "Lower priority of category at point in Todos Categories buffer."
4082 (interactive)
4083 (todos-set-category-priority 'lower))
4084
4085 ;; ---------------------------------------------------------------------------
4086 ;;; Item editing commands
4087
4088 ;; FIXME: make insertion options customizable per category?
4089 ;;;###autoload
4090 (defun todos-insert-item (&optional arg diary nonmarking date-type time
4091 region-or-here)
4092 "Add a new Todo item to a category.
4093 \(See the note at the end of this document string about key
4094 bindings and convenience commands derived from this command.)
4095
4096 With no (or nil) prefix argument ARG, add the item to the current
4097 category; with one prefix argument (C-u), prompt for a category
4098 from the current Todos file; with two prefix arguments (C-u C-u),
4099 first prompt for a Todos file, then a category in that file. If
4100 a non-existing category is entered, ask whether to add it to the
4101 Todos file; if answered affirmatively, add the category and
4102 insert the item there.
4103
4104 When argument DIARY is non-nil, this overrides the intent of the
4105 user option `todos-include-in-diary' for this item: if
4106 `todos-include-in-diary' is nil, include the item in the Fancy
4107 Diary display, and if it is non-nil, exclude the item from the
4108 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
4109 has its intended effect.
4110
4111 When the item is included in the Fancy Diary display and the
4112 argument NONMARKING is non-nil, this overrides the intent of the
4113 user option `todos-diary-nonmarking' for this item: if
4114 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
4115 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
4116
4117 The argument DATE-TYPE determines the content of the item's
4118 mandatory date header string and how it is added:
4119 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
4120 when the user puts the cursor on a date and hits RET, that
4121 date, in the format set by `calendar-date-display-form',
4122 becomes the date in the header.
4123 - If DATE-TYPE is a string matching the regexp
4124 `todos-date-pattern', that string becomes the date in the
4125 header. This case is for the command
4126 `todos-insert-item-from-calendar' which is called from the
4127 Calendar.
4128 - If DATE-TYPE is the symbol `date', the header contains the date
4129 in the format set by `calendar-date-display-form', with year,
4130 month and day individually prompted for (month with tab
4131 completion).
4132 - If DATE-TYPE is the symbol `dayname' the header contains a
4133 weekday name instead of a date, prompted for with tab
4134 completion.
4135 - If DATE-TYPE has any other value (including nil or none) the
4136 header contains the current date (in the format set by
4137 `calendar-date-display-form').
4138
4139 With non-nil argument TIME prompt for a time string, which must
4140 match `diary-time-regexp'. Typing `<return>' at the prompt
4141 returns the current time, if the user option
4142 `todos-always-add-time-string' is non-nil, otherwise the empty
4143 string (i.e., no time string). If TIME is absent or nil, add or
4144 omit the current time string according as
4145 `todos-always-add-time-string' is non-nil or nil, respectively.
4146
4147 The argument REGION-OR-HERE determines the source and location of
4148 the new item:
4149 - If the REGION-OR-HERE is the symbol `here', prompt for the text
4150 of the new item and insert it directly above the todo item at
4151 point (hence lowering the priority of the remaining items), or
4152 if point is on the empty line below the last todo item, insert
4153 the new item there. An error is signalled if
4154 `todos-insert-item' is invoked with `here' outside of the
4155 current category.
4156 - If REGION-OR-HERE is the symbol `region', use the region of the
4157 current buffer as the text of the new item, depending on the
4158 value of user option `todos-use-only-highlighted-region': if
4159 this is non-nil, then use the region only when it is
4160 highlighted; otherwise, use the region regardless of
4161 highlighting. An error is signalled if there is no region in
4162 the current buffer. Prompt for the item's priority in the
4163 category (an integer between 1 and one more than the number of
4164 items in the category), and insert the item accordingly.
4165 - If REGION-OR-HERE has any other value (in particular, nil or
4166 none), prompt for the text and the item's priority, and insert
4167 the item accordingly.
4168
4169 To facilitate using these arguments when inserting a new todo
4170 item, convenience commands have been defined for all admissible
4171 combinations together with mnenomic key bindings based on on the
4172 name of the arguments and their order in the command's argument
4173 list: diar_y_ - nonmar_k_ing - _c_alendar or _d_ate or day_n_ame
4174 - _t_ime - _r_egion or _h_ere. These key combinations are
4175 appended to the basic insertion key (i) and keys that allow a
4176 following key must be doubled when used finally. For example,
4177 `iyh' will insert a new item with today's date, marked according
4178 to the DIARY argument described above, and with priority
4179 according to the HERE argument; while `iyy' does the same except
4180 the priority is not given by HERE but by prompting."
4181 ;; An alternative interface for customizing key
4182 ;; binding is also provided with the function
4183 ;; `todos-insertion-bindings'." ;FIXME
4184 (interactive "P")
4185 (let ((region (eq region-or-here 'region))
4186 (here (eq region-or-here 'here)))
4187 (when region
4188 (let (use-empty-active-region)
4189 (unless (and todos-use-only-highlighted-region (use-region-p))
4190 (error "There is no active region"))))
4191 (let* ((buf (current-buffer))
4192 (new-item (if region
4193 (buffer-substring-no-properties
4194 (region-beginning) (region-end))
4195 (read-from-minibuffer "Todo item: ")))
4196 (date-string (cond
4197 ((eq date-type 'date)
4198 (todos-read-date))
4199 ((eq date-type 'dayname)
4200 (todos-read-dayname))
4201 ((eq date-type 'calendar)
4202 (setq todos-date-from-calendar t)
4203 (todos-set-date-from-calendar))
4204 ((and (stringp date-type)
4205 (string-match todos-date-pattern date-type))
4206 (setq todos-date-from-calendar date-type)
4207 (todos-set-date-from-calendar))
4208 (t (calendar-date-string (calendar-current-date) t t))))
4209 (time-string (or (and time (todos-read-time))
4210 (and todos-always-add-time-string
4211 (substring (current-time-string) 11 16)))))
4212 (setq todos-date-from-calendar nil)
4213 (cond ((equal arg '(16))
4214 (todos-jump-to-category nil t)
4215 (set-window-buffer
4216 (selected-window)
4217 (set-buffer (find-buffer-visiting todos-global-current-todos-file))))
4218 ((equal arg '(4))
4219 (todos-jump-to-category)
4220 (set-window-buffer
4221 (selected-window)
4222 (set-buffer (find-buffer-visiting todos-global-current-todos-file))))
4223 (t
4224 (when (not (derived-mode-p 'todos-mode)) (todos-show))))
4225 (let (buffer-read-only)
4226 (setq new-item
4227 ;; Add date, time and diary marking as required.
4228 (concat (if (not (and diary (not todos-include-in-diary)))
4229 todos-nondiary-start
4230 (when (and nonmarking (not todos-diary-nonmarking))
4231 diary-nonmarking-symbol))
4232 date-string (when (and time-string ; Can be empty string.
4233 (not (zerop (length time-string))))
4234 (concat " " time-string))
4235 (when (not (and diary (not todos-include-in-diary)))
4236 todos-nondiary-end)
4237 " " new-item))
4238 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4239 (setq new-item (replace-regexp-in-string
4240 "\\(\n\\)[^[:blank:]]"
4241 (concat "\n" (make-string todos-indent-to-here 32))
4242 new-item nil nil 1))
4243 ;; FIXME: after jumping to another category due to `C-u i h',
4244 ;; item is inserted as first item -- ok?
4245 (if here
4246 (cond ((not (eq major-mode 'todos-mode))
4247 (error "Cannot insert a todo item here outside of Todos mode"))
4248 ((not (eq buf (current-buffer)))
4249 (error "Cannot insert an item here after changing buffer"))
4250 ((or (todos-done-item-p)
4251 ;; Point on last blank line.
4252 (save-excursion (forward-line -1) (todos-done-item-p)))
4253 (error "Cannot insert a new item in the done item section"))
4254 (t
4255 (todos-insert-with-overlays new-item)))
4256 (todos-set-item-priority new-item (todos-current-category) t))
4257 (todos-update-count 'todo 1)
4258 (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
4259 (todos-update-categories-sexp)))))
4260
4261 (defun todos-copy-item ()
4262 "Copy item at point and insert the copy as a new item."
4263 (interactive)
4264 (unless (or (todos-done-item-p) (looking-at "^$"))
4265 (let ((copy (todos-item-string))
4266 (diary-item (todos-diary-item-p)))
4267 (todos-set-item-priority copy (todos-current-category) t)
4268 (todos-update-count 'todo 1)
4269 (when diary-item (todos-update-count 'diary 1))
4270 (todos-update-categories-sexp))))
4271
4272 (defvar todos-date-from-calendar nil
4273 "Helper variable for setting item date from the Emacs Calendar.")
4274
4275 (defun todos-set-date-from-calendar ()
4276 "Return string of date chosen from Calendar."
4277 (cond ((and (stringp todos-date-from-calendar)
4278 (string-match todos-date-pattern todos-date-from-calendar))
4279 todos-date-from-calendar)
4280 (todos-date-from-calendar
4281 (let (calendar-view-diary-initially-flag)
4282 (calendar))
4283 ;; *Calendar* is now current buffer.
4284 (local-set-key (kbd "RET") 'exit-recursive-edit)
4285 (message "Put cursor on a date and type <return> to set it.")
4286 ;; FIXME: is there a better way than recursive-edit?
4287 (recursive-edit)
4288 (setq todos-date-from-calendar
4289 (calendar-date-string (calendar-cursor-to-date t) t t))
4290 (calendar-exit)
4291 todos-date-from-calendar)))
4292
4293 (defun todos-delete-item ()
4294 "Delete at least one item in this category.
4295
4296 If there are marked items, delete all of these; otherwise, delete
4297 the item at point."
4298 (interactive)
4299 (let (ov)
4300 (unwind-protect
4301 (let* ((cat (todos-current-category))
4302 (marked (assoc cat todos-categories-with-marks))
4303 (item (unless marked (todos-item-string)))
4304 ;; FIXME: make confirmation an option?
4305 (answer (if marked
4306 (y-or-n-p "Permanently delete all marked items? ")
4307 (when item
4308 (setq ov (make-overlay
4309 (save-excursion (todos-item-start))
4310 (save-excursion (todos-item-end))))
4311 (overlay-put ov 'face 'todos-search)
4312 (y-or-n-p (concat "Permanently delete this item? ")))))
4313 (opoint (point))
4314 buffer-read-only)
4315 (when answer
4316 (and marked (goto-char (point-min)))
4317 (catch 'done
4318 (while (not (eobp))
4319 (if (or (and marked (todos-marked-item-p)) item)
4320 (progn
4321 (if (todos-done-item-p)
4322 (todos-update-count 'done -1)
4323 (todos-update-count 'todo -1 cat)
4324 (and (todos-diary-item-p) (todos-update-count 'diary -1)))
4325 (if ov (delete-overlay ov))
4326 (todos-remove-item)
4327 ;; Don't leave point below last item.
4328 (and item (bolp) (eolp) (< (point-min) (point-max))
4329 (todos-backward-item))
4330 (when item
4331 (throw 'done (setq item nil))))
4332 (todos-forward-item))))
4333 (when marked
4334 (remove-overlays (point-min) (point-max)
4335 'before-string todos-item-mark)
4336 (setq todos-categories-with-marks
4337 (assq-delete-all cat todos-categories-with-marks))
4338 (goto-char opoint))
4339 (todos-update-categories-sexp)
4340 (todos-prefix-overlays)))
4341 (if ov (delete-overlay ov)))))
4342
4343 (defun todos-edit-item ()
4344 "Edit the Todo item at point.
4345 If the item consists of only one logical line, edit it in the
4346 minibuffer; otherwise, edit it in Todos Edit mode."
4347 (interactive)
4348 (when (todos-item-string)
4349 (let* ((buffer-read-only)
4350 (start (todos-item-start))
4351 (item-beg (progn
4352 (re-search-forward
4353 (concat todos-date-string-start todos-date-pattern
4354 "\\( " diary-time-regexp "\\)?"
4355 (regexp-quote todos-nondiary-end) "?")
4356 (line-end-position) t)
4357 (1+ (- (point) start))))
4358 (item (todos-item-string))
4359 (multiline (> (length (split-string item "\n")) 1))
4360 (opoint (point)))
4361 (if multiline
4362 (todos-edit-multiline t)
4363 (let ((new (read-string "Edit: " (cons item item-beg))))
4364 (while (not (string-match
4365 (concat todos-date-string-start todos-date-pattern) new))
4366 (setq new (read-from-minibuffer
4367 "Item must start with a date: " new)))
4368 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4369 (setq new (replace-regexp-in-string
4370 "\\(\n\\)[^[:blank:]]"
4371 (concat "\n" (make-string todos-indent-to-here 32)) new
4372 nil nil 1))
4373 ;; If user moved point during editing, make sure it moves back.
4374 (goto-char opoint)
4375 (todos-remove-item)
4376 (todos-insert-with-overlays new)
4377 (move-to-column item-beg))))))
4378
4379 (defun todos-edit-multiline-item ()
4380 "Edit current Todo item in Todos Edit mode.
4381 Use of newlines invokes `todos-indent' to insure compliance with
4382 the format of Diary entries."
4383 (interactive)
4384 (todos-edit-multiline t))
4385
4386 (defun todos-edit-multiline (&optional item)
4387 ""
4388 (interactive)
4389 ;; FIXME: should there be only one live Todos Edit buffer?
4390 ;; (let ((buffer-name todos-edit-buffer))
4391 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
4392 (set-window-buffer
4393 (selected-window)
4394 (set-buffer (make-indirect-buffer
4395 (file-name-nondirectory todos-current-todos-file)
4396 buffer-name)))
4397 (if item
4398 (narrow-to-region (todos-item-start) (todos-item-end))
4399 (widen))
4400 (todos-edit-mode)
4401 (message "%s" (substitute-command-keys
4402 (concat "Type \\[todos-edit-quit] to check file format "
4403 "validity and return to Todos mode.\n")))))
4404
4405 (defun todos-edit-quit ()
4406 "Return from Todos Edit mode to Todos mode.
4407
4408 If the whole file was in Todos Edit mode, check before returning
4409 whether the file is still a valid Todos file and if so, also
4410 recalculate the Todos categories sexp, in case changes were made
4411 in the number or names of categories."
4412 (interactive)
4413 ;; FIXME: Should do todos-check-format only if file was actually changed --
4414 ;; but how to tell?
4415 (when (eq (buffer-size) (- (point-max) (point-min)))
4416 (when (todos-check-format) (todos-repair-categories-sexp)))
4417 (kill-buffer)
4418 ;; In case next buffer is not the one holding todos-current-todos-file.
4419 (todos-show))
4420
4421 (defun todos-edit-item-header (&optional what)
4422 "Edit date/time header of at least one item.
4423
4424 Interactively, ask whether to edit year, month and day or day of
4425 the week, as well as time. If there are marked items, apply the
4426 changes to all of these; otherwise, edit just the item at point.
4427
4428 Non-interactively, argument WHAT specifies whether to set the
4429 date from the Calendar or to today, or whether to edit only the
4430 date or day, or only the time."
4431 (interactive)
4432 (let* ((cat (todos-current-category))
4433 (marked (assoc cat todos-categories-with-marks))
4434 (first t) ; Match only first of marked items.
4435 (todos-date-from-calendar t)
4436 ndate ntime nheader)
4437 (save-excursion
4438 (or (and marked (goto-char (point-min))) (todos-item-start))
4439 (catch 'stop
4440 (while (not (eobp))
4441 (and marked
4442 (while (not (todos-marked-item-p))
4443 (todos-forward-item)
4444 (and (eobp) (throw 'stop nil))))
4445 (re-search-forward (concat todos-date-string-start "\\(?1:"
4446 todos-date-pattern
4447 "\\)\\(?2: " diary-time-regexp "\\)?")
4448 (line-end-position) t)
4449 (let* ((odate (match-string-no-properties 1))
4450 (otime (match-string-no-properties 2))
4451 (buffer-read-only))
4452 (cond ((eq what 'today)
4453 (progn
4454 (setq ndate (calendar-date-string
4455 (calendar-current-date) t t))
4456 (replace-match ndate nil nil nil 1)))
4457 ((eq what 'calendar)
4458 (setq ndate (save-match-data (todos-set-date-from-calendar)))
4459 (replace-match ndate nil nil nil 1))
4460 (t
4461 (unless (eq what 'timeonly)
4462 (when first
4463 (setq ndate (if (save-match-data
4464 (string-match "[0-9]+" odate))
4465 (if (y-or-n-p "Change date? ")
4466 (todos-read-date)
4467 (todos-read-dayname))
4468 (if (y-or-n-p "Change day? ")
4469 (todos-read-dayname)
4470 (todos-read-date)))))
4471 (replace-match ndate nil nil nil 1))
4472 (unless (eq what 'dateonly)
4473 (when first
4474 (setq ntime (save-match-data (todos-read-time)))
4475 (when (< 0 (length ntime))
4476 (setq ntime (concat " " ntime))))
4477 (if otime
4478 (replace-match ntime nil nil nil 2)
4479 (goto-char (match-end 1))
4480 (insert ntime)))))
4481 (setq todos-date-from-calendar nil)
4482 (setq first nil))
4483 (if marked
4484 (todos-forward-item)
4485 (goto-char (point-max))))))))
4486
4487 (defun todos-edit-item-date ()
4488 "Prompt for and apply changes to current item's date."
4489 (interactive)
4490 (todos-edit-item-header 'dateonly))
4491
4492 (defun todos-edit-item-date-from-calendar ()
4493 "Prompt for changes to current item's date and apply from Calendar."
4494 (interactive)
4495 (todos-edit-item-header 'calendar))
4496
4497 (defun todos-edit-item-date-is-today ()
4498 "Set item date to today's date."
4499 (interactive)
4500 (todos-edit-item-header 'today))
4501
4502 (defun todos-edit-item-time ()
4503 "Prompt For and apply changes to current item's time."
4504 (interactive)
4505 (todos-edit-item-header 'timeonly))
4506
4507 (defun todos-edit-item-diary-inclusion ()
4508 "Change diary status of one or more todo items in this category.
4509 That is, insert `todos-nondiary-marker' if the candidate items
4510 lack this marking; otherwise, remove it.
4511
4512 If there are marked todo items, change the diary status of all
4513 and only these, otherwise change the diary status of the item at
4514 point."
4515 (interactive)
4516 (let ((buffer-read-only)
4517 (marked (assoc (todos-current-category)
4518 todos-categories-with-marks)))
4519 (catch 'stop
4520 (save-excursion
4521 (when marked (goto-char (point-min)))
4522 (while (not (eobp))
4523 (if (todos-done-item-p)
4524 (throw 'stop (message "Done items cannot be edited"))
4525 (unless (and marked (not (todos-marked-item-p)))
4526 (let* ((beg (todos-item-start))
4527 (lim (save-excursion (todos-item-end)))
4528 (end (save-excursion
4529 (or (todos-time-string-matcher lim)
4530 (todos-date-string-matcher lim)))))
4531 (if (looking-at (regexp-quote todos-nondiary-start))
4532 (progn
4533 (replace-match "")
4534 (search-forward todos-nondiary-end (1+ end) t)
4535 (replace-match "")
4536 (todos-update-count 'diary 1))
4537 (when end
4538 (insert todos-nondiary-start)
4539 (goto-char (1+ end))
4540 (insert todos-nondiary-end)
4541 (todos-update-count 'diary -1)))))
4542 (unless marked (throw 'stop nil))
4543 (todos-forward-item)))))
4544 (todos-update-categories-sexp)))
4545
4546 (defun todos-edit-category-diary-inclusion (arg)
4547 "Make all items in this category diary items.
4548 With prefix ARG, make all items in this category non-diary
4549 items."
4550 (interactive "P")
4551 (save-excursion
4552 (goto-char (point-min))
4553 (let ((todo-count (todos-get-count 'todo))
4554 (diary-count (todos-get-count 'diary))
4555 (buffer-read-only))
4556 (catch 'stop
4557 (while (not (eobp))
4558 (if (todos-done-item-p) ; We've gone too far.
4559 (throw 'stop nil)
4560 (let* ((beg (todos-item-start))
4561 (lim (save-excursion (todos-item-end)))
4562 (end (save-excursion
4563 (or (todos-time-string-matcher lim)
4564 (todos-date-string-matcher lim)))))
4565 (if arg
4566 (unless (looking-at (regexp-quote todos-nondiary-start))
4567 (insert todos-nondiary-start)
4568 (goto-char (1+ end))
4569 (insert todos-nondiary-end))
4570 (when (looking-at (regexp-quote todos-nondiary-start))
4571 (replace-match "")
4572 (search-forward todos-nondiary-end (1+ end) t)
4573 (replace-match "")))))
4574 (todos-forward-item))
4575 (unless (if arg (zerop diary-count) (= diary-count todo-count))
4576 (todos-update-count 'diary (if arg
4577 (- diary-count)
4578 (- todo-count diary-count))))
4579 (todos-update-categories-sexp)))))
4580
4581 (defun todos-edit-item-diary-nonmarking ()
4582 "Change non-marking of one or more diary items in this category.
4583 That is, insert `diary-nonmarking-symbol' if the candidate items
4584 lack this marking; otherwise, remove it.
4585
4586 If there are marked todo items, change the non-marking status of
4587 all and only these, otherwise change the non-marking status of
4588 the item at point."
4589 (interactive)
4590 (let ((buffer-read-only)
4591 (marked (assoc (todos-current-category)
4592 todos-categories-with-marks)))
4593 (catch 'stop
4594 (save-excursion
4595 (when marked (goto-char (point-min)))
4596 (while (not (eobp))
4597 (if (todos-done-item-p)
4598 (throw 'stop (message "Done items cannot be edited"))
4599 (unless (and marked (not (todos-marked-item-p)))
4600 (todos-item-start)
4601 (unless (looking-at (regexp-quote todos-nondiary-start))
4602 (if (looking-at (regexp-quote diary-nonmarking-symbol))
4603 (replace-match "")
4604 (insert diary-nonmarking-symbol))))
4605 (unless marked (throw 'stop nil))
4606 (todos-forward-item)))))))
4607
4608 (defun todos-edit-category-diary-nonmarking (arg)
4609 "Add `diary-nonmarking-symbol' to all diary items in this category.
4610 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
4611 items in this category."
4612 (interactive "P")
4613 (save-excursion
4614 (goto-char (point-min))
4615 (let (buffer-read-only)
4616 (catch 'stop
4617 (while (not (eobp))
4618 (if (todos-done-item-p) ; We've gone too far.
4619 (throw 'stop nil)
4620 (unless (looking-at (regexp-quote todos-nondiary-start))
4621 (if arg
4622 (when (looking-at (regexp-quote diary-nonmarking-symbol))
4623 (replace-match ""))
4624 (unless (looking-at (regexp-quote diary-nonmarking-symbol))
4625 (insert diary-nonmarking-symbol))))
4626 (todos-forward-item)))))))
4627
4628 ;; FIXME: Make NOP if point isn't on a todo item (cf. todos-copy-item,
4629 ;; todos-move-item
4630 (defun todos-set-item-priority (&optional item cat new arg)
4631 "Set todo ITEM's priority in CATegory and move item accordingly.
4632
4633 Interactively, ITEM defaults to the item at point, CAT to the
4634 current category in Todos mode, and the priority is a number
4635 between 1 and the number of items in the category.
4636 Non-interactively, non-nil NEW means ITEM is a new item and the
4637 lowest priority is one more than the number of items in CAT.
4638
4639 The new priority is set either interactively by prompt or by a
4640 numerical prefix argument, or noninteractively by argument ARG,
4641 whose value can be either of the symbols `raise' or `lower',
4642 meaning to raise or lower the item's priority by one."
4643 (interactive)
4644 (let* ((item (or item (todos-item-string)))
4645 (marked (todos-marked-item-p))
4646 (cat (or cat (cond ((eq major-mode 'todos-mode)
4647 (todos-current-category))
4648 ((eq major-mode 'todos-filtered-items-mode)
4649 (let* ((regexp1
4650 (concat todos-date-string-start
4651 todos-date-pattern
4652 "\\( " diary-time-regexp "\\)?"
4653 (regexp-quote todos-nondiary-end)
4654 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
4655 (save-excursion
4656 (re-search-forward regexp1 nil t)
4657 (match-string-no-properties 1)))))))
4658 curnum
4659 (todo (cond ((or (eq arg 'raise) (eq arg 'lower)
4660 (eq major-mode 'todos-filtered-items-mode))
4661 (save-excursion
4662 (let ((curstart (todos-item-start))
4663 (count 0))
4664 (goto-char (point-min))
4665 (while (looking-at todos-item-start)
4666 (setq count (1+ count))
4667 (when (= (point) curstart) (setq curnum count))
4668 (todos-forward-item))
4669 count)))
4670 ((eq major-mode 'todos-mode)
4671 (todos-get-count 'todo cat))))
4672 (maxnum (if new (1+ todo) todo))
4673 (prompt (format "Set item priority (1-%d): " maxnum))
4674 (priority (cond ((numberp current-prefix-arg)
4675 current-prefix-arg)
4676 ((and (eq arg 'raise) (>= curnum 1))
4677 (1- curnum))
4678 ((and (eq arg 'lower) (<= curnum maxnum))
4679 (1+ curnum))))
4680 candidate
4681 buffer-read-only)
4682 (unless (and priority
4683 (or (and (eq arg 'raise) (zerop priority))
4684 (and (eq arg 'lower) (> priority maxnum))))
4685 ;; When moving item to another category, show the category before
4686 ;; prompting for its priority.
4687 (unless (or arg (called-interactively-p t))
4688 (todos-category-number cat)
4689 (todos-category-select))
4690 ;; Prompt for priority only when the category has at least one todo item.
4691 (when (> maxnum 1)
4692 (while (not priority)
4693 (setq candidate (read-number prompt))
4694 (setq prompt (when (or (< candidate 1) (> candidate maxnum))
4695 (format "Priority must be an integer between 1 and %d.\n"
4696 maxnum)))
4697 (unless prompt (setq priority candidate))))
4698 ;; In Top Priorities buffer, an item's priority can be changed
4699 ;; wrt items in another category, but not wrt items in the same
4700 ;; category.
4701 (when (eq major-mode 'todos-filtered-items-mode)
4702 (let* ((regexp2 (concat todos-date-string-start todos-date-pattern
4703 "\\( " diary-time-regexp "\\)?"
4704 (regexp-quote todos-nondiary-end)
4705 "?\\(?1:" (regexp-quote cat) "\\)"))
4706 (end (cond ((< curnum priority)
4707 (save-excursion (todos-item-end)))
4708 ((> curnum priority)
4709 (save-excursion (todos-item-start)))))
4710 (match (save-excursion
4711 (cond ((< curnum priority)
4712 (todos-forward-item (1+ (- priority curnum)))
4713 (when (re-search-backward regexp2 end t)
4714 (match-string-no-properties 1)))
4715 ((> curnum priority)
4716 (todos-backward-item (- curnum priority))
4717 (when (re-search-forward regexp2 end t)
4718 (match-string-no-properties 1)))))))
4719 (when match
4720 (error (concat "Cannot reprioritize items from the same "
4721 "category in this mode, only in Todos mode")))))
4722 ;; Interactively or with non-nil ARG, relocate the item within its
4723 ;; category.
4724 (when (or arg (called-interactively-p))
4725 (todos-remove-item))
4726 (goto-char (point-min))
4727 (when priority
4728 (unless (= priority 1)
4729 (todos-forward-item (1- priority))))
4730 (todos-insert-with-overlays item)
4731 ;; If item was marked, restore the mark.
4732 (and marked (overlay-put (make-overlay (point) (point))
4733 'before-string todos-item-mark)))))
4734
4735 (defun todos-raise-item-priority ()
4736 "Raise priority of current item by moving it up by one item."
4737 (interactive)
4738 (todos-set-item-priority nil nil nil 'raise))
4739
4740 (defun todos-lower-item-priority ()
4741 "Lower priority of current item by moving it down by one item."
4742 (interactive)
4743 (todos-set-item-priority nil nil nil 'lower))
4744
4745 (defun todos-move-item (&optional file)
4746 "Move at least one todo item to another category.
4747
4748 If there are marked items, move all of these; otherwise, move
4749 the item at point.
4750
4751 With non-nil argument FILE, first prompt for another Todos file and
4752 then a category in that file to move the item or items to.
4753
4754 If the chosen category is not one of the existing categories,
4755 then it is created and the item(s) become(s) the first
4756 entry/entries in that category."
4757 (interactive)
4758 (let* ((cat1 (todos-current-category))
4759 (marked (assoc cat1 todos-categories-with-marks)))
4760 (unless (or (todos-done-item-p)
4761 ;; Point is between todo and done items.
4762 (and (looking-at "^$") (not marked)))
4763 (let* ((buffer-read-only)
4764 (file1 todos-current-todos-file)
4765 (num todos-category-number)
4766 (item (todos-item-string))
4767 (diary-item (todos-diary-item-p))
4768 (omark (save-excursion (todos-item-start) (point-marker)))
4769 (file2 (if file
4770 (todos-read-file-name "Choose a Todos file: " nil t)
4771 file1))
4772 (count 0)
4773 (count-diary 0)
4774 ov cat2 moved nmark)
4775 (set-buffer (find-file-noselect file2))
4776 (unwind-protect
4777 (progn
4778 (unless marked
4779 (setq ov (make-overlay (save-excursion (todos-item-start))
4780 (save-excursion (todos-item-end))))
4781 (overlay-put ov 'face 'todos-search))
4782 (setq cat2 (let* ((pl (if (and marked (> (cdr marked) 1)) "s" ""))
4783 (name (todos-read-category
4784 (concat "Move item" pl " to category: ")))
4785 (prompt (concat "Choose a different category than "
4786 "the current one\n(type `"
4787 (key-description
4788 (car (where-is-internal
4789 'todos-set-item-priority)))
4790 "' to reprioritize item "
4791 "within the same category): ")))
4792 (while (equal name cat1)
4793 (setq name (todos-read-category prompt)))
4794 name)))
4795 (if ov (delete-overlay ov)))
4796 (set-buffer (find-buffer-visiting file1))
4797 (if marked
4798 (progn
4799 (setq item nil)
4800 (goto-char (point-min))
4801 (while (not (eobp))
4802 (when (todos-marked-item-p)
4803 (setq item (concat item (todos-item-string) "\n"))
4804 (setq count (1+ count))
4805 (when (todos-diary-item-p)
4806 (setq count-diary (1+ count-diary))))
4807 (todos-forward-item))
4808 ;; Chop off last newline.
4809 (setq item (substring item 0 -1)))
4810 (setq count 1)
4811 (when (todos-diary-item-p) (setq count-diary 1)))
4812 (set-window-buffer (selected-window)
4813 (set-buffer (find-file-noselect file2)))
4814 (unwind-protect
4815 (progn
4816 (todos-set-item-priority item cat2 t)
4817 (setq moved t))
4818 (cond
4819 ;; Move succeeded, so remove item from starting category,
4820 ;; update item counts and display the category containing
4821 ;; the moved item.
4822 (moved
4823 (setq nmark (point-marker))
4824 (todos-update-count 'todo count)
4825 (todos-update-count 'diary count-diary)
4826 (todos-update-categories-sexp)
4827 (with-current-buffer (find-buffer-visiting file1)
4828 (save-excursion
4829 (save-restriction
4830 (widen)
4831 (goto-char omark)
4832 (if marked
4833 (let (beg end)
4834 (setq item nil)
4835 (re-search-backward
4836 (concat "^" (regexp-quote todos-category-beg)) nil t)
4837 (forward-line)
4838 (setq beg (point))
4839 (re-search-forward
4840 (concat "^" (regexp-quote todos-category-done)) nil t)
4841 (setq end (match-beginning 0))
4842 (goto-char beg)
4843 (while (< (point) end)
4844 (if (todos-marked-item-p)
4845 (todos-remove-item)
4846 (todos-forward-item)))
4847 ;; FIXME: does this work?
4848 (remove-overlays (point-min) (point-max)
4849 'before-string todos-item-mark)
4850 (setq todos-categories-with-marks
4851 (assq-delete-all cat1 todos-categories-with-marks)))
4852 (if ov (delete-overlay ov))
4853 (todos-remove-item))))
4854 (todos-update-count 'todo (- count) cat1)
4855 (todos-update-count 'diary (- count-diary) cat1)
4856 (todos-update-categories-sexp))
4857 (set-window-buffer (selected-window)
4858 (set-buffer (find-file-noselect file2)))
4859 (setq todos-category-number (todos-category-number cat2))
4860 (todos-category-select)
4861 (goto-char nmark)
4862 ;; If item is moved to end of category, make sure the
4863 ;; items above it are displayed in the window.
4864 (recenter))
4865 ;; User quit before moving, so return to starting category.
4866 (t
4867 (todos-category-number cat1)
4868 (todos-category-select)
4869 (goto-char omark))))))))
4870
4871 (defun todos-move-item-to-file ()
4872 "Move the current todo item to a category in another Todos file."
4873 (interactive)
4874 (todos-move-item t))
4875
4876 ;; (defun todos-move-item-to-diary ()
4877 ;; "Move one or more items in current category to the diary file.
4878 ;;
4879 ;; If there are marked items, move all of these; otherwise, move
4880 ;; the item at point."
4881 ;; (interactive)
4882 ;; ;; FIXME
4883 ;; )
4884
4885 ;; FIXME: make adding date customizable, and make this and time customization
4886 ;; overridable via double prefix arg ??
4887 (defun todos-item-done (&optional arg)
4888 "Tag at least one item in this category as done and hide it.
4889
4890 With prefix argument ARG prompt for a comment and append it to
4891 the done item; this is only possible if there are no marked
4892 items. If there are marked items, tag all of these with
4893 `todos-done-string' plus the current date and, if
4894 `todos-always-add-time-string' is non-nil, the current time;
4895 otherwise, just tag the item at point. Items tagged as done are
4896 relocated to the category's (by default hidden) done section."
4897 (interactive "P")
4898 (let* ((cat (todos-current-category))
4899 (marked (assoc cat todos-categories-with-marks)))
4900 (unless (or (todos-done-item-p)
4901 ;; Point is between todo and done items.
4902 (and (looking-at "^$") (not marked)))
4903 (let* ((date-string (calendar-date-string (calendar-current-date) t t))
4904 (time-string (if todos-always-add-time-string
4905 (concat " " (substring (current-time-string) 11 16))
4906 ""))
4907 (done-prefix (concat "[" todos-done-string date-string time-string
4908 "] "))
4909 (comment (and arg (not marked) (read-string "Enter a comment: ")))
4910 (item-count 0)
4911 (diary-count 0)
4912 item done-item
4913 (buffer-read-only))
4914 (and marked (goto-char (point-min)))
4915 (catch 'done
4916 (while (not (eobp))
4917 (if (or (not marked) (and marked (todos-marked-item-p)))
4918 (progn
4919 (setq item (todos-item-string))
4920 (setq done-item (cond (marked
4921 (concat done-item done-prefix item "\n"))
4922 (comment
4923 (concat done-prefix item " ["
4924 todos-comment-string
4925 ": " comment "]"))
4926 (t
4927 (concat done-prefix item))))
4928 (setq item-count (1+ item-count))
4929 (when (todos-diary-item-p)
4930 (setq diary-count (1+ diary-count)))
4931 (todos-remove-item)
4932 (unless marked (throw 'done nil)))
4933 (todos-forward-item))))
4934 (when marked
4935 ;; Chop off last newline of done item string.
4936 (setq done-item (substring done-item 0 -1))
4937 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
4938 (setq todos-categories-with-marks
4939 (assq-delete-all cat todos-categories-with-marks)))
4940 (save-excursion
4941 (widen)
4942 (re-search-forward
4943 (concat "^" (regexp-quote todos-category-done)) nil t)
4944 (forward-char)
4945 (insert done-item "\n"))
4946 (todos-update-count 'todo (- item-count))
4947 (todos-update-count 'done item-count)
4948 (todos-update-count 'diary (- diary-count))
4949 (todos-update-categories-sexp)
4950 (save-excursion (todos-category-select))))))
4951
4952 (defun todos-done-item-add-edit-or-delete-comment (&optional arg)
4953 "Add a comment to this done item or edit an existing comment.
4954 With prefix ARG delete an existing comment."
4955 (interactive "P")
4956 (when (todos-done-item-p)
4957 (let ((item (todos-item-string))
4958 (end (save-excursion (todos-item-end)))
4959 comment buffer-read-only)
4960 (save-excursion
4961 (todos-item-start)
4962 (if (re-search-forward (concat " \\["
4963 (regexp-quote todos-comment-string)
4964 ": \\([^]]+\\)\\]") end t)
4965 (if arg
4966 (when (y-or-n-p "Delete comment? ")
4967 (delete-region (match-beginning 0) (match-end 0)))
4968 (setq comment (read-string "Edit comment: "
4969 (cons (match-string 1) 1)))
4970 (replace-match comment nil nil nil 1))
4971 (setq comment (read-string "Enter a comment: "))
4972 (todos-item-end)
4973 (insert " [" todos-comment-string ": " comment "]"))))))
4974
4975 (defun todos-item-undo ()
4976 "Restore this done item to the todo section of this category.
4977 If done item has a comment, ask whether to omit the comment from
4978 the restored item."
4979 (interactive)
4980 (let* ((cat (todos-current-category))
4981 (marked (assoc cat todos-categories-with-marks)))
4982 (when (or marked (todos-done-item-p))
4983 (let ((buffer-read-only)
4984 (bufmod (buffer-modified-p))
4985 (opoint (point))
4986 (orig-mrk (progn (todos-item-start) (point-marker)))
4987 (orig-item (todos-item-string))
4988 (first 'first)
4989 (item-count 0)
4990 (diary-count 0)
4991 start end item undone)
4992 (and marked (goto-char (point-min)))
4993 (catch 'done
4994 (while (not (eobp))
4995 (if (or (not marked) (and marked (todos-marked-item-p)))
4996 (if (not (todos-done-item-p))
4997 (error "Only done items can be undone")
4998 (todos-item-start)
4999 ;; Find the end of the date string added upon tagging item as
5000 ;; done.
5001 (setq start (search-forward "] "))
5002 (setq item-count (1+ item-count))
5003 (unless (looking-at (regexp-quote todos-nondiary-start))
5004 (setq diary-count (1+ diary-count)))
5005 (setq end (save-excursion (todos-item-end)))
5006 ;; Ask (once) whether to omit done item's comment. If
5007 ;; affirmed, omit subsequent comments without asking.
5008 (when (re-search-forward
5009 (concat " \\[" (regexp-quote todos-comment-string)
5010 ": [^]]+\\]") end t)
5011 (if (eq first 'first)
5012 (setq first
5013 (if (eq todos-undo-item-omit-comment 'ask)
5014 (when (y-or-n-p
5015 "Omit comment from restored item? ")
5016 'omit)
5017 (when todos-undo-item-omit-comment 'omit)))
5018 t)
5019 (when (eq first 'omit)
5020 (delete-region (match-beginning 0) (match-end 0))
5021 (setq end (point))))
5022 (setq item (concat item
5023 (buffer-substring-no-properties start end)
5024 (when marked "\n")))
5025 (todos-remove-item)
5026 (unless marked (throw 'done nil)))
5027 (todos-forward-item))))
5028 (if marked
5029 (progn
5030 ;; (remove-overlays (point-min) (point-max)
5031 ;; 'before-string todos-item-mark)
5032 (setq todos-categories-with-marks
5033 (assq-delete-all cat todos-categories-with-marks))
5034 ;; Insert undone items that were marked at end of todo item list.
5035 (goto-char (point-min))
5036 (re-search-forward (concat "^" (regexp-quote todos-category-done))
5037 nil t)
5038 (forward-line -1)
5039 (insert item)
5040 (todos-update-count 'todo item-count)
5041 (todos-update-count 'done (- item-count))
5042 (when diary-count (todos-update-count 'diary diary-count))
5043 (todos-update-categories-sexp)
5044 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5045 (todos-category-select)))
5046 ;; With an unmarked undone item, prompt for its priority. If user
5047 ;; cancels before setting new priority, then leave the done item
5048 ;; unchanged.
5049 (unwind-protect
5050 (progn
5051 (todos-set-item-priority item (todos-current-category) t)
5052 (setq undone t)
5053 (todos-update-count 'todo 1)
5054 (todos-update-count 'done -1)
5055 (and (todos-diary-item-p) (todos-update-count 'diary 1))
5056 (todos-update-categories-sexp)
5057 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5058 (todos-category-select)))
5059 (unless undone
5060 (let ((todos-show-with-done t))
5061 (widen)
5062 (goto-char orig-mrk)
5063 (todos-insert-with-overlays orig-item)
5064 (set-buffer-modified-p bufmod)
5065 (todos-category-select))
5066 (goto-char opoint))))
5067 (set-marker orig-mrk nil)))))
5068
5069 (defun todos-archive-done-item (&optional all)
5070 "Archive at least one done item in this category.
5071
5072 If there are marked done items (and no marked todo items),
5073 archive all of these; otherwise, with non-nil argument ALL,
5074 archive all done items in this category; otherwise, archive the
5075 done item at point.
5076
5077 If the archive of this file does not exist, it is created. If
5078 this category does not exist in the archive, it is created."
5079 (interactive)
5080 (when (eq major-mode 'todos-mode)
5081 (if (and all (zerop (todos-get-count 'done)))
5082 (message "No done items in this category")
5083 (catch 'end
5084 (let* ((cat (todos-current-category))
5085 (tbuf (current-buffer))
5086 (marked (assoc cat todos-categories-with-marks))
5087 (afile (concat (file-name-sans-extension
5088 todos-current-todos-file) ".toda"))
5089 (archive (if (file-exists-p afile)
5090 (find-file-noselect afile t)
5091 (get-buffer-create afile)))
5092 (item (and (todos-done-item-p) (concat (todos-item-string) "\n")))
5093 (count 0)
5094 marked-items beg end all-done
5095 buffer-read-only)
5096 (cond
5097 (marked
5098 (save-excursion
5099 (goto-char (point-min))
5100 (while (not (eobp))
5101 (when (todos-marked-item-p)
5102 (if (not (todos-done-item-p))
5103 (throw 'end (message "Only done items can be archived"))
5104 (setq marked-items
5105 (concat marked-items (todos-item-string) "\n"))
5106 (setq count (1+ count))))
5107 (todos-forward-item))))
5108 (all
5109 (if (y-or-n-p "Archive all done items in this category? ")
5110 (save-excursion
5111 (save-restriction
5112 (goto-char (point-min))
5113 (widen)
5114 (setq beg (progn
5115 (re-search-forward todos-done-string-start nil t)
5116 (match-beginning 0))
5117 end (if (re-search-forward
5118 (concat "^" (regexp-quote todos-category-beg))
5119 nil t)
5120 (match-beginning 0)
5121 (point-max))
5122 all-done (buffer-substring-no-properties beg end)
5123 count (todos-get-count 'done))))
5124 (throw 'end nil))))
5125 (when (or marked all item)
5126 (with-current-buffer archive
5127 (unless buffer-file-name (erase-buffer))
5128 (let (buffer-read-only)
5129 (widen)
5130 (goto-char (point-min))
5131 (if (and (re-search-forward (concat "^"
5132 (regexp-quote
5133 (concat todos-category-beg
5134 cat)))
5135 nil t)
5136 (re-search-forward (regexp-quote todos-category-done)
5137 nil t))
5138 ;; Start of done items section in existing category.
5139 (forward-char)
5140 (todos-add-category cat)
5141 ;; Start of done items section in new category.
5142 (goto-char (point-max)))
5143 (insert (cond (marked marked-items)
5144 (all all-done)
5145 (item)))
5146 (todos-update-count 'done (if (or marked all) count 1) cat)
5147 (todos-update-categories-sexp)
5148 ;; If archive is new, save to file now (using write-region in
5149 ;; order not to get prompted for file to save to), to let
5150 ;; auto-mode-alist take effect below.
5151 (unless buffer-file-name
5152 (write-region nil nil afile)
5153 (kill-buffer))))
5154 (with-current-buffer tbuf
5155 (cond ((or marked item)
5156 (and marked (goto-char (point-min)))
5157 (catch 'done
5158 (while (not (eobp))
5159 (if (or (and marked (todos-marked-item-p)) item)
5160 (progn
5161 (todos-remove-item)
5162 (todos-update-count 'done -1)
5163 (todos-update-count 'archived 1)
5164 ;; Don't leave point below last item.
5165 (and item (bolp) (eolp) (< (point-min) (point-max))
5166 (todos-backward-item))
5167 (when item
5168 (throw 'done (setq item nil))))
5169 (todos-forward-item)))))
5170 (all
5171 (remove-overlays beg end)
5172 (delete-region beg end)
5173 (todos-update-count 'done (- count))
5174 (todos-update-count 'archived count)))
5175 (when marked
5176 (remove-overlays (point-min) (point-max)
5177 'before-string todos-item-mark)
5178 (setq todos-categories-with-marks
5179 (assq-delete-all cat todos-categories-with-marks)))
5180 (todos-update-categories-sexp)
5181 (todos-prefix-overlays)))
5182 (find-file afile)
5183 (todos-category-number cat)
5184 (todos-category-select)
5185 (split-window-below)
5186 (set-window-buffer (selected-window) tbuf))))))
5187
5188 (defun todos-archive-category-done-items ()
5189 "Move all done items in this category to its archive."
5190 (interactive)
5191 (todos-archive-done-item t))
5192
5193 (defun todos-unarchive-items (&optional all)
5194 "Unarchive at least one item in this archive category.
5195
5196 If there are marked items, unarchive all of these; otherwise,
5197 with non-nil argument ALL, unarchive all items in this category;
5198 otherwise, unarchive the item at point.
5199
5200 Unarchived items are restored as done items to the corresponding
5201 category in the Todos file, inserted at the end of done section.
5202 If all items in the archive category were restored, the category
5203 is deleted from the archive. If this was the only category in the
5204 archive, the archive file is deleted."
5205 (interactive)
5206 (when (eq major-mode 'todos-archive-mode)
5207 (catch 'end
5208 (let* ((cat (todos-current-category))
5209 (tbuf (find-file-noselect
5210 (concat (file-name-sans-extension todos-current-todos-file)
5211 ".todo") t))
5212 (marked (assoc cat todos-categories-with-marks))
5213 (item (concat (todos-item-string) "\n"))
5214 (all-items (when all (buffer-substring-no-properties
5215 (point-min) (point-max))))
5216 (all-count (when all (todos-get-count 'done)))
5217 marked-items marked-count
5218 buffer-read-only)
5219 (when marked
5220 (save-excursion
5221 (goto-char (point-min))
5222 (while (not (eobp))
5223 (when (todos-marked-item-p)
5224 (concat marked-items (todos-item-string) "\n")
5225 (setq marked-count (1+ marked-count)))
5226 (todos-forward-item))))
5227 ;; Restore items to end of category's done section and update counts.
5228 (with-current-buffer tbuf
5229 (let (buffer-read-only)
5230 (widen)
5231 (goto-char (point-min))
5232 (re-search-forward (concat "^" (regexp-quote
5233 (concat todos-category-beg cat)))
5234 nil t)
5235 ;; Go to end of category's done section.
5236 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
5237 nil t)
5238 (goto-char (match-beginning 0))
5239 (goto-char (point-max)))
5240 (cond (marked
5241 (insert marked-items)
5242 (todos-update-count 'done marked-count cat)
5243 (todos-update-count 'archived (- marked-count) cat))
5244 (all
5245 (insert all-items)
5246 (todos-update-count 'done all-count cat)
5247 (todos-update-count 'archived (- all-count) cat))
5248 (t
5249 (insert item)
5250 (todos-update-count 'done 1 cat)
5251 (todos-update-count 'archived -1 cat)))
5252 (todos-update-categories-sexp)))
5253 ;; Delete restored items from archive.
5254 (cond ((or marked item)
5255 (and marked (goto-char (point-min)))
5256 (catch 'done
5257 (while (not (eobp))
5258 (if (or (and marked (todos-marked-item-p)) item)
5259 (progn
5260 (todos-remove-item)
5261 ;; Don't leave point below last item.
5262 (and item (bolp) (eolp) (< (point-min) (point-max))
5263 (todos-backward-item))
5264 (when item
5265 (throw 'done (setq item nil))))
5266 (todos-forward-item))))
5267 (todos-update-count 'done (if marked (- marked-count) -1) cat))
5268 (all
5269 (remove-overlays (point-min) (point-max))
5270 (delete-region (point-min) (point-max))))
5271 ;; If that was the last category in the archive, delete the whole file.
5272 (if (= (length todos-categories) 1)
5273 (progn
5274 (delete-file todos-current-todos-file)
5275 ;; Don't bother confirming killing the archive buffer.
5276 (set-buffer-modified-p nil)
5277 (kill-buffer))
5278 ;; Otherwise, if the archive category is now empty, delete it.
5279 (when (eq (point-min) (point-max))
5280 (widen)
5281 (let ((beg (re-search-backward
5282 (concat "^" (regexp-quote todos-category-beg) cat)
5283 nil t))
5284 (end (if (re-search-forward
5285 (concat "^" (regexp-quote todos-category-beg))
5286 nil t 2)
5287 (match-beginning 0)
5288 (point-max))))
5289 (remove-overlays beg end)
5290 (delete-region beg end)
5291 (setq todos-categories (delete (assoc cat todos-categories)
5292 todos-categories))
5293 (todos-update-categories-sexp))))
5294 ;; Visit category in Todos file and show restored done items.
5295 (let ((tfile (buffer-file-name tbuf))
5296 (todos-show-with-done t))
5297 (set-window-buffer (selected-window)
5298 (set-buffer (find-file-noselect tfile)))
5299 (todos-category-number cat)
5300 (todos-show)
5301 (message "Items unarchived."))))))
5302
5303 (defun todos-unarchive-category ()
5304 "Unarchive all items in this category. See `todos-unarchive-items'."
5305 (interactive)
5306 (todos-unarchive-items t))
5307
5308 (provide 'todos)
5309
5310 ;;; todos.el ends here
5311
5312 ;; FIXME: remove when part of Emacs
5313 ;; ---------------------------------------------------------------------------
5314 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
5315 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
5316
5317 ;;; Addition to calendar.el
5318 ;; FIXME: autoload when key-binding is defined in calendar.el
5319 (defun todos-insert-item-from-calendar (&optional arg)
5320 ""
5321 (interactive "P")
5322 (setq todos-date-from-calendar
5323 (calendar-date-string (calendar-cursor-to-date t) t t))
5324 (calendar-exit)
5325 (todos-show)
5326 (todos-insert-item arg nil nil todos-date-from-calendar))
5327
5328 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar)
5329
5330 ;;; necessitated adaptations to diary-lib.el
5331
5332 ;; (defun diary-goto-entry (button)
5333 ;; "Jump to the diary entry for the BUTTON at point."
5334 ;; (let* ((locator (button-get button 'locator))
5335 ;; (marker (car locator))
5336 ;; markbuf file opoint)
5337 ;; ;; If marker pointing to diary location is valid, use that.
5338 ;; (if (and marker (setq markbuf (marker-buffer marker)))
5339 ;; (progn
5340 ;; (pop-to-buffer markbuf)
5341 ;; (goto-char (marker-position marker)))
5342 ;; ;; Marker is invalid (eg buffer has been killed, as is the case with
5343 ;; ;; included diary files).
5344 ;; (or (and (setq file (cadr locator))
5345 ;; (file-exists-p file)
5346 ;; (find-file-other-window file)
5347 ;; (progn
5348 ;; (when (eq major-mode (default-value 'major-mode)) (diary-mode))
5349 ;; (when (eq major-mode 'todos-mode) (widen))
5350 ;; (goto-char (point-min))
5351 ;; (when (re-search-forward (format "%s.*\\(%s\\)"
5352 ;; (regexp-quote (nth 2 locator))
5353 ;; (regexp-quote (nth 3 locator)))
5354 ;; nil t)
5355 ;; (goto-char (match-beginning 1))
5356 ;; (when (eq major-mode 'todos-mode)
5357 ;; (setq opoint (point))
5358 ;; (re-search-backward (concat "^"
5359 ;; (regexp-quote todos-category-beg)
5360 ;; "\\(.*\\)\n")
5361 ;; nil t)
5362 ;; (todos-category-number (match-string 1))
5363 ;; (todos-category-select)
5364 ;; (goto-char opoint)))))
5365 ;; (message "Unable to locate this diary entry")))))