]> code.delx.au - gnu-emacs/blob - lisp/calendar/todos.el
* calendar/todos.el (todos-show): Fix a comment.
[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)
1262 cat "$")
1263 (point-max) t)
1264 (forward-line)
1265 (while (not (or (looking-at
1266 (concat
1267 (regexp-quote todos-category-beg)
1268 "\\(.*\\)\n"))
1269 (eobp)))
1270 (when (looking-at todos-done-string-start)
1271 (setq archive-count (1+ archive-count)))
1272 (forward-line))))
1273 (todos-update-count 'archived archive-count cat))))
1274 ((looking-at todos-done-string-start)
1275 (todos-update-count 'done 1 cat))
1276 ((looking-at (concat "^\\("
1277 (regexp-quote diary-nonmarking-symbol)
1278 "\\)?" todos-date-pattern))
1279 (todos-update-count 'diary 1 cat)
1280 (todos-update-count 'todo 1 cat))
1281 ((looking-at (concat todos-date-string-start todos-date-pattern))
1282 (todos-update-count 'todo 1 cat))
1283 ;; If first line is todos-categories list, use it and end loop
1284 ;; -- unless FORCEd to scan whole file.
1285 ((bobp)
1286 (unless force
1287 (setq todos-categories (read (buffer-substring-no-properties
1288 (line-beginning-position)
1289 (line-end-position))))
1290 (goto-char (1- (point-max))))))
1291 (forward-line)))))
1292 todos-categories)
1293
1294 (defun todos-repair-categories-sexp ()
1295 "Repair corrupt Todos categories sexp.
1296 This should only be needed as a consequence of careless manual
1297 editing or a bug in todos.el."
1298 (interactive)
1299 (let ((todos-categories (todos-make-categories-list t)))
1300 (todos-update-categories-sexp)))
1301
1302 (defvar todos-allcats-file (concat todos-files-directory "todos-allcats.el")
1303 "Name of file containing the value of `todos-all-categories-alist'.
1304 The contents of this file are automatically generated and
1305 executed when todos.el is loaded, hence users should not edit
1306 it.")
1307
1308 (defun todos-all-categories-alist ()
1309 ""
1310 ;; FIXME: loop through archive files for categories not in todo files?
1311 (let ((files todos-files)
1312 allcats)
1313 (dolist (f files)
1314 ;; FIXME: If file buffer is modified, save first.
1315 (with-temp-buffer
1316 (insert-file-contents f)
1317 (let ((cats (read (buffer-substring-no-properties
1318 (line-beginning-position)
1319 (line-end-position)))))
1320 (dolist (c cats)
1321 (let* ((cat (assoc (car c) allcats))
1322 (catcdr (cdr cat)))
1323 (unless (listp catcdr) (setq catcdr (list catcdr)))
1324 (if cat
1325 (setcdr cat (append catcdr (list (todos-short-file-name f))))
1326 (setq allcats (append allcats
1327 (list
1328 (cons (car c)
1329 (todos-short-file-name f)))))))))))
1330 allcats))
1331
1332 (defvar todos-all-categories-alist (if (file-exists-p todos-allcats-file)
1333 (load-file todos-allcats-file)
1334 (todos-all-categories-alist))
1335 "Alist of names of all Todos categories and their files.")
1336
1337 ;;; Global variables and helper functions for items
1338
1339 (defconst todos-date-pattern
1340 (let ((dayname (diary-name-pattern calendar-day-name-array nil t)))
1341 (concat "\\(?:" dayname "\\|"
1342 (let ((dayname)
1343 ;; FIXME: how to choose between abbreviated and unabbreviated
1344 ;; month name?
1345 (monthname (format "\\(?:%s\\|\\*\\)"
1346 (diary-name-pattern
1347 calendar-month-name-array
1348 calendar-month-abbrev-array t)))
1349 (month "\\(?:[0-9]+\\|\\*\\)")
1350 (day "\\(?:[0-9]+\\|\\*\\)")
1351 (year "-?\\(?:[0-9]+\\|\\*\\)"))
1352 (mapconcat 'eval calendar-date-display-form ""))
1353 "\\)"))
1354 "Regular expression matching a Todos date header.")
1355
1356 (defconst todos-nondiary-start (nth 0 todos-nondiary-marker)
1357 "String inserted before item date to block diary inclusion.")
1358
1359 (defconst todos-nondiary-end (nth 1 todos-nondiary-marker)
1360 "String inserted after item date matching `todos-nondiary-start'.")
1361
1362 ;; By itself this matches anything, because of the `?'; however, it's only
1363 ;; used in the context of `todos-date-pattern' (but Emacs Lisp lacks
1364 ;; lookahead).
1365 (defconst todos-date-string-start
1366 (concat "^\\(" (regexp-quote todos-nondiary-start) "\\|"
1367 (regexp-quote diary-nonmarking-symbol) "\\)?")
1368 "Regular expression matching part of item header before the date.")
1369
1370 (defconst todos-done-string-start
1371 (concat "^\\[" (regexp-quote todos-done-string))
1372 "Regular expression matching start of done item.")
1373
1374 (defconst todos-item-start (concat "\\(" todos-date-string-start "\\|"
1375 todos-done-string-start "\\)"
1376 todos-date-pattern)
1377 "String identifying start of a Todos item.")
1378
1379 (defun todos-item-start ()
1380 "Move to start of current Todos item and return its position."
1381 (unless (or
1382 ;; Buffer is empty (invocation possible e.g. via todos-forward-item
1383 ;; from todos-filter-items when processing category with no todo
1384 ;; items).
1385 (eq (point-min) (point-max))
1386 ;; Point is on the empty line below category's last todo item...
1387 (and (looking-at "^$")
1388 (or (eobp) ; ...and done items are hidden...
1389 (save-excursion ; ...or done items are visible.
1390 (forward-line)
1391 (looking-at (concat "^"
1392 (regexp-quote todos-category-done))))))
1393 ;; Buffer is widened.
1394 (looking-at (regexp-quote todos-category-beg)))
1395 (goto-char (line-beginning-position))
1396 (while (not (looking-at todos-item-start))
1397 (forward-line -1))
1398 (point)))
1399
1400 (defun todos-item-end ()
1401 "Move to end of current Todos item and return its position."
1402 ;; Items cannot end with a blank line.
1403 (unless (looking-at "^$")
1404 (let* ((done (todos-done-item-p))
1405 (to-lim nil)
1406 ;; For todo items, end is before the done items section, for done
1407 ;; items, end is before the next category. If these limits are
1408 ;; missing or inaccessible, end it before the end of the buffer.
1409 (lim (if (save-excursion
1410 (re-search-forward
1411 (concat "^" (regexp-quote (if done
1412 todos-category-beg
1413 todos-category-done)))
1414 nil t))
1415 (progn (setq to-lim t) (match-beginning 0))
1416 (point-max))))
1417 (when (bolp) (forward-char)) ; Find start of next item.
1418 (goto-char (if (re-search-forward todos-item-start lim t)
1419 (match-beginning 0)
1420 (if to-lim lim (point-max))))
1421 ;; For last todo item, skip back over the empty line before the done
1422 ;; items section, else just back to the end of the previous line.
1423 (backward-char (when (and to-lim (not done) (eq (point) lim)) 2))
1424 (point))))
1425
1426 (defun todos-item-string ()
1427 "Return bare text of current item as a string."
1428 (let ((opoint (point))
1429 (start (todos-item-start))
1430 (end (todos-item-end)))
1431 (goto-char opoint)
1432 (and start end (buffer-substring-no-properties start end))))
1433
1434 (defun todos-remove-item ()
1435 "Internal function called in editing, deleting or moving items."
1436 (let* ((beg (todos-item-start))
1437 (end (progn (todos-item-end) (1+ (point))))
1438 (ovs (overlays-in beg beg)))
1439 ;; There can be both prefix/number and mark overlays.
1440 (while ovs (delete-overlay (car ovs)) (pop ovs))
1441 (delete-region beg end)))
1442
1443 (defun todos-diary-item-p ()
1444 "Return non-nil if item at point has diary entry format."
1445 (save-excursion
1446 (when (todos-item-string) ; Exclude empty lines.
1447 (todos-item-start)
1448 (not (looking-at (regexp-quote todos-nondiary-start))))))
1449
1450 (defun todos-done-item-p ()
1451 "Return non-nil if item at point is a done item."
1452 (save-excursion
1453 (todos-item-start)
1454 (looking-at todos-done-string-start)))
1455
1456 (defvar todos-item-mark (propertize (if (equal todos-prefix "*") "@" "*")
1457 'face 'todos-mark)
1458 "String used to mark items.")
1459
1460 (defun todos-marked-item-p ()
1461 "If this item begins with `todos-item-mark', return mark overlay."
1462 (let ((ovs (overlays-in (line-beginning-position) (line-beginning-position)))
1463 (mark todos-item-mark)
1464 ov marked)
1465 (catch 'stop
1466 (while ovs
1467 (setq ov (pop ovs))
1468 (and (equal (overlay-get ov 'before-string) mark)
1469 (throw 'stop (setq marked t)))))
1470 (when marked ov)))
1471
1472 (defun todos-insert-with-overlays (item)
1473 "Insert ITEM at point and update prefix/priority number overlays."
1474 (todos-item-start)
1475 (insert item "\n")
1476 (todos-backward-item)
1477 (todos-prefix-overlays))
1478
1479 (defun todos-prefix-overlays ()
1480 "Put before-string overlay in front of this category's items.
1481 The overlay's value is the string `todos-prefix' or with non-nil
1482 `todos-number-priorities' an integer in the sequence from 1 to
1483 the number of todo or done items in the category indicating the
1484 item's priority. Todo and done items are numbered independently
1485 of each other."
1486 (when (or todos-number-priorities
1487 (not (string-match "^[[:space:]]*$" todos-prefix)))
1488 (let ((prefix (propertize (concat todos-prefix " ")
1489 'face 'todos-prefix-string))
1490 (num 0))
1491 (save-excursion
1492 (goto-char (point-min))
1493 (while (not (eobp))
1494 (when (or (todos-date-string-matcher (line-end-position))
1495 (todos-done-string-matcher (line-end-position)))
1496 (goto-char (match-beginning 0))
1497 (when todos-number-priorities
1498 (setq num (1+ num))
1499 ;; Reset number to 1 for first done item.
1500 (when (and (looking-at todos-done-string-start)
1501 (looking-back (concat "^"
1502 (regexp-quote todos-category-done)
1503 "\n")))
1504 (setq num 1))
1505 (setq prefix (propertize (concat (number-to-string num) " ")
1506 'face 'todos-prefix-string)))
1507 (let ((ovs (overlays-in (point) (point)))
1508 marked ov-pref)
1509 (if ovs
1510 (dolist (ov ovs)
1511 (let ((val (overlay-get ov 'before-string)))
1512 (if (equal val "*")
1513 (setq marked t)
1514 (setq ov-pref val)))))
1515 (unless (equal ov-pref prefix)
1516 ;; Why doesn't this work?
1517 ;; (remove-overlays (point) (point) 'before-string)
1518 (remove-overlays (point) (point))
1519 (overlay-put (make-overlay (point) (point))
1520 'before-string prefix)
1521 (and marked (overlay-put (make-overlay (point) (point))
1522 'before-string todos-item-mark)))))
1523 (forward-line))))))
1524
1525 ;; ---------------------------------------------------------------------------
1526 ;;; Helper functions for user input with prompting and completion
1527
1528 (defun todos-read-file-name (prompt &optional archive mustmatch)
1529 "Choose and return the name of a Todos file, prompting with PROMPT.
1530
1531 Show completions with TAB or SPC; the names are shown in short
1532 form but the absolute truename is returned. With non-nil ARCHIVE
1533 return the absolute truename of a Todos archive file. With non-nil
1534 MUSTMATCH the name of an existing file must be chosen;
1535 otherwise, a new file name is allowed."
1536 (let* ((completion-ignore-case todos-completion-ignore-case)
1537 (files (mapcar 'todos-short-file-name
1538 (if archive todos-archives todos-files)))
1539 (file (completing-read prompt files nil mustmatch nil nil
1540 (unless files
1541 ;; Trigger prompt for initial file.
1542 ""))))
1543 (unless (file-exists-p todos-files-directory)
1544 (make-directory todos-files-directory))
1545 (unless mustmatch
1546 (setq file (todos-validate-name file 'file)))
1547 (setq file (file-truename (concat todos-files-directory file
1548 (if archive ".toda" ".todo"))))))
1549
1550 (defun todos-read-category (prompt &optional mustmatch added)
1551 "Choose and return a category name, prompting with PROMPT.
1552 Show completions with TAB or SPC. With non-nil MUSTMATCH the
1553 name must be that of an existing category; otherwise, a new
1554 category name is allowed, after checking its validity. Non-nil
1555 argument ADDED means the caller is todos-add-category, so don't
1556 ask whether to add the category."
1557 ;; Allow SPC to insert spaces, for adding new category names.
1558 (let ((map minibuffer-local-completion-map))
1559 (define-key map " " nil)
1560 ;; Make a copy of todos-categories in case history-delete-duplicates is
1561 ;; non-nil, which makes completing-read alter todos-categories.
1562 (let* ((categories (copy-sequence todos-categories))
1563 (history (cons 'todos-categories (1+ todos-category-number)))
1564 (completion-ignore-case todos-completion-ignore-case)
1565 (cat (completing-read prompt todos-categories nil
1566 mustmatch nil history
1567 ;; Default for existing categories is the
1568 ;; current category.
1569 (if todos-categories
1570 (todos-current-category)
1571 ;; Trigger prompt for initial category.
1572 ""))))
1573 (unless (or mustmatch (assoc cat todos-categories))
1574 (todos-validate-name cat 'category)
1575 (unless added
1576 (if (y-or-n-p (format (concat "There is no category \"%s\" in "
1577 "this file; add it? ") cat))
1578 ;; Restore point and narrowing after adding new
1579 ;; category, to avoid moving to beginning of file when
1580 ;; moving marked items to a new category (todos-move-item).
1581 (save-excursion
1582 (save-restriction
1583 (todos-add-category cat)
1584 ;; We've changed todos-categories, so we must not
1585 ;; reset it below.
1586 (setq added t)))
1587 (keyboard-quit))))
1588 ;; Restore the original value of todos-categories unless a new category
1589 ;; was added (since todos-add-category changes todos-categories).
1590 (unless added (setq todos-categories categories))
1591 cat)))
1592
1593 (defun todos-validate-name (name type)
1594 "Prompt for new NAME for TYPE until it is valid, then return it.
1595 TYPE can be either a file or a category"
1596 (let ((categories todos-categories)
1597 (files (mapcar 'todos-short-file-name todos-files))
1598 prompt)
1599 (while
1600 (and (cond ((string= "" name)
1601 (setq prompt
1602 (cond ((eq type 'file)
1603 (if todos-files
1604 "Enter a non-empty file name: "
1605 ;; Empty string passed by todos-show to
1606 ;; prompt for initial Todos file.
1607 (concat "Initial file name ["
1608 todos-initial-file "]: ")))
1609 ((eq type 'category)
1610 (if todos-categories
1611 "Enter a non-empty category name: "
1612 ;; Empty string passed by todos-show to
1613 ;; prompt for initial category of a new
1614 ;; Todos file.
1615 (concat "Initial category name ["
1616 todos-initial-category "]: "))))))
1617 ((string-match "\\`\\s-+\\'" name)
1618 (setq prompt
1619 "Enter a name that does not contain only white space: "))
1620 ((and (eq type 'file) (member name todos-files))
1621 (setq prompt "Enter a non-existing file name: "))
1622 ((and (eq type 'category) (assoc name todos-categories))
1623 (setq prompt "Enter a non-existing category name: ")))
1624 (setq name (if (or (and (eq type 'file) todos-files)
1625 (and (eq type 'category) todos-categories))
1626 (completing-read prompt (cond ((eq type 'file)
1627 todos-files)
1628 ((eq type 'category)
1629 todos-categories)))
1630 ;; Offer default initial name.
1631 (completing-read prompt (if (eq type 'file)
1632 todos-files
1633 todos-categories)
1634 nil nil (if (eq type 'file)
1635 todos-initial-file
1636 todos-initial-category))))))
1637 name))
1638
1639 ;; Adapted from calendar-read-date and calendar-date-string.
1640 (defun todos-read-date ()
1641 "Prompt for Gregorian date and return it in the current format.
1642 Also accepts `*' as an unspecified month, day, or year."
1643 (let* ((year (let (x)
1644 (while (if (numberp x) (< x 0) (not (eq x '*)))
1645 (setq x (read-from-minibuffer
1646 "Year (>0 or RET for this year or * for any year): "
1647 nil nil t nil (number-to-string
1648 (calendar-extract-year
1649 (calendar-current-date))))))
1650 x))
1651 (month-array (vconcat calendar-month-name-array (vector "*")))
1652 (abbrevs (vconcat calendar-month-abbrev-array (vector "*")))
1653 (completion-ignore-case todos-completion-ignore-case)
1654 (monthname (completing-read
1655 "Month name (RET for current month, * for any month): "
1656 (mapcar 'list (append month-array nil))
1657 nil t nil nil
1658 (calendar-month-name (calendar-extract-month
1659 (calendar-current-date)) t)))
1660 (month (cdr (assoc-string
1661 monthname (calendar-make-alist month-array nil nil
1662 abbrevs))))
1663 (last (if (= month 13)
1664 ;; Use longest possible month for checking day number
1665 ;; input. Does Calendar do anything special when * is
1666 ;; currently a shorter month?
1667 31
1668 (let ((yr (if (eq year '*)
1669 ;; Use a leap year to allow Feb. 29.
1670 2012
1671 year)))
1672 (calendar-last-day-of-month month yr))))
1673 (day (let (x)
1674 (while (if (numberp x) (or (< x 0) (< last x)) (not (eq x '*)))
1675 (setq x (read-from-minibuffer
1676 (format
1677 "Day (1-%d or RET for today or * for any day): "
1678 last) nil nil t nil (number-to-string
1679 (calendar-extract-day
1680 (calendar-current-date))))))
1681 x))
1682 dayname) ; Needed by calendar-date-display-form.
1683 (setq year (if (eq year '*) (symbol-name '*) (number-to-string year)))
1684 (setq day (if (eq day '*) (symbol-name '*) (number-to-string day)))
1685 ;; FIXME: make abbreviation customizable
1686 (setq monthname
1687 (or (and (= month 13) "*")
1688 (calendar-month-name (calendar-extract-month (list month day year))
1689 t)))
1690 (mapconcat 'eval calendar-date-display-form "")))
1691
1692 (defun todos-read-dayname ()
1693 "Choose name of a day of the week with completion and return it."
1694 (let ((completion-ignore-case todos-completion-ignore-case))
1695 (completing-read "Enter a day name: "
1696 (append calendar-day-name-array nil)
1697 nil t)))
1698
1699 (defun todos-read-time ()
1700 "Prompt for and return a valid clock time as a string.
1701
1702 Valid time strings are those matching `diary-time-regexp'.
1703 Typing `<return>' at the prompt returns the current time, if the
1704 user option `todos-always-add-time-string' is non-nil, otherwise
1705 the empty string (i.e., no time string)."
1706 (let (valid answer)
1707 (while (not valid)
1708 (setq answer (read-string "Enter a clock time: " nil nil
1709 (when todos-always-add-time-string
1710 (substring (current-time-string) 11 16))))
1711 (when (or (string= "" answer)
1712 (string-match diary-time-regexp answer))
1713 (setq valid t)))
1714 answer))
1715
1716 ;; ---------------------------------------------------------------------------
1717 ;;; Item filtering infrastructure
1718
1719 (defvar todos-multiple-filter-files nil
1720 "List of files selected from `todos-multiple-filter-files' widget.")
1721
1722 (defvar todos-multiple-filter-files-widget nil
1723 "Variable holding widget created by `todos-multiple-filter-files'.")
1724
1725 (defun todos-multiple-filter-files ()
1726 "Pop to a buffer with a widget for choosing multiple filter files."
1727 (require 'widget)
1728 (eval-when-compile
1729 (require 'wid-edit))
1730 (with-current-buffer (get-buffer-create "*Todos Filter Files*")
1731 (pop-to-buffer (current-buffer))
1732 (erase-buffer)
1733 (kill-all-local-variables)
1734 (widget-insert "Select files for generating the top priorities list.\n\n")
1735 (setq todos-multiple-filter-files-widget
1736 (widget-create
1737 `(set ,@(mapcar (lambda (x) (list 'const x))
1738 (mapcar 'todos-short-file-name
1739 (funcall todos-files-function))))))
1740 (widget-insert "\n")
1741 (widget-create 'push-button
1742 :notify (lambda (widget &rest ignore)
1743 (setq todos-multiple-filter-files 'quit)
1744 (quit-window t)
1745 (exit-recursive-edit))
1746 "Cancel")
1747 (widget-insert " ")
1748 (widget-create 'push-button
1749 :notify (lambda (&rest ignore)
1750 (setq todos-multiple-filter-files
1751 (mapcar (lambda (f)
1752 (concat todos-files-directory
1753 f ".todo"))
1754 (widget-value
1755 todos-multiple-filter-files-widget)))
1756 (quit-window t)
1757 (exit-recursive-edit))
1758 "Apply")
1759 (use-local-map widget-keymap)
1760 (widget-setup))
1761 (message "Click \"Apply\" after selecting files.")
1762 (recursive-edit))
1763
1764 (defun todos-filter-items (filter &optional multifile)
1765 "Build and display a list of items from different categories.
1766
1767 The items are selected according to the value of FILTER, which
1768 can be `top' for top priority items, `diary' for diary items,
1769 `regexp' for items matching a regular expresion entered by the
1770 user, or a cons cell of one of these symbols and a number set by
1771 the calling command, which overrides `todos-show-priorities'.
1772
1773 With non-nil argument MULTIFILE list top priorities of multiple
1774 Todos files, by default those in `todos-filter-files'."
1775 (let ((num (if (consp filter) (cdr filter) todos-show-priorities))
1776 (buf (get-buffer-create todos-filtered-items-buffer))
1777 (files (list todos-current-todos-file))
1778 regexp fname bufstr cat beg end done)
1779 (when multifile
1780 (setq files (or todos-multiple-filter-files ; Passed from todos-*-multifile.
1781 (if (or (consp filter)
1782 (null todos-filter-files))
1783 (progn (todos-multiple-filter-files)
1784 todos-multiple-filter-files)
1785 todos-filter-files))
1786 todos-multiple-filter-files nil))
1787 (if (eq files 'quit) (keyboard-quit))
1788 (if (null files)
1789 (error "No files have been chosen for filtering")
1790 (with-current-buffer buf
1791 (erase-buffer)
1792 (kill-all-local-variables)
1793 (todos-filtered-items-mode))
1794 (when (eq filter 'regexp)
1795 (setq regexp (read-string "Enter a regular expression: ")))
1796 (save-current-buffer
1797 (dolist (f files)
1798 ;; Before inserting file contents into temp buffer, save a modified
1799 ;; buffer visiting it.
1800 (let ((bf (find-buffer-visiting f)))
1801 (when (buffer-modified-p bf)
1802 (with-current-buffer bf (save-buffer))))
1803 (setq fname (todos-short-file-name f))
1804 (with-temp-buffer
1805 (when (and todos-filter-done-items (eq filter 'regexp))
1806 ;; If there is a corresponding archive file for the Todos file,
1807 ;; insert it first and add identifiers for todos-jump-to-item.
1808 (let ((arch (concat (file-name-sans-extension f) ".toda")))
1809 (when (file-exists-p arch)
1810 (insert-file-contents arch)
1811 ;; Delete Todos archive file categories sexp.
1812 (delete-region (line-beginning-position)
1813 (1+ (line-end-position)))
1814 (save-excursion
1815 (while (not (eobp))
1816 (when (re-search-forward
1817 (concat (if todos-filter-done-items
1818 (concat "\\(?:" todos-done-string-start
1819 "\\|" todos-date-string-start
1820 "\\)")
1821 todos-date-string-start)
1822 todos-date-pattern "\\(?: "
1823 diary-time-regexp "\\)?"
1824 (if todos-filter-done-items
1825 "\\]"
1826 (regexp-quote todos-nondiary-end)) "?")
1827 nil t)
1828 (insert "(archive) "))
1829 (forward-line))))))
1830 (insert-file-contents f)
1831 ;; Delete Todos file categories sexp.
1832 (delete-region (line-beginning-position) (1+ (line-end-position)))
1833 (let (fnum)
1834 ;; Unless the number of items to show was supplied by prefix
1835 ;; argument of caller, the file-wide value from
1836 ;; `todos-priorities-rules', if non-nil, overrides
1837 ;; `todos-show-priorities'.
1838 (unless (consp filter)
1839 (setq fnum (nth 1 (assoc f todos-priorities-rules))))
1840 (while (re-search-forward
1841 (concat "^" (regexp-quote todos-category-beg) "\\(.+\\)\n")
1842 nil t)
1843 (setq cat (match-string 1))
1844 (let (cnum)
1845 ;; Unless the number of items to show was supplied by prefix
1846 ;; argument of caller, the category-wide value from
1847 ;; `todos-priorities-rules', if non-nil, overrides a non-nil
1848 ;; file-wide value from `todos-priorities-rules' as well as
1849 ;; `todos-show-priorities'.
1850 (unless (consp filter)
1851 (let ((cats (nth 2 (assoc f todos-priorities-rules))))
1852 (setq cnum (or (cdr (assoc cat cats)) fnum))))
1853 (delete-region (match-beginning 0) (match-end 0))
1854 (setq beg (point)) ; First item in the current category.
1855 (setq end (if (re-search-forward
1856 (concat "^" (regexp-quote todos-category-beg))
1857 nil t)
1858 (match-beginning 0)
1859 (point-max)))
1860 (goto-char beg)
1861 (setq done
1862 (if (re-search-forward
1863 (concat "\n" (regexp-quote todos-category-done))
1864 end t)
1865 (match-beginning 0)
1866 end))
1867 (unless (and todos-filter-done-items (eq filter 'regexp))
1868 ;; Leave done items.
1869 (delete-region done end)
1870 (setq end done))
1871 (narrow-to-region beg end) ; Process only current category.
1872 (goto-char (point-min))
1873 ;; Apply the filter.
1874 (cond ((eq filter 'diary)
1875 (while (not (eobp))
1876 (if (looking-at (regexp-quote todos-nondiary-start))
1877 (todos-remove-item)
1878 (todos-forward-item))))
1879 ((eq filter 'regexp)
1880 (while (not (eobp))
1881 (if (looking-at todos-item-start)
1882 (if (string-match regexp (todos-item-string))
1883 (todos-forward-item)
1884 (todos-remove-item))
1885 ;; Kill lines that aren't part of a todo or done
1886 ;; item (empty or todos-category-done).
1887 (delete-region (line-beginning-position)
1888 (1+ (line-end-position))))
1889 ;; If last todo item in file matches regexp and
1890 ;; there are no following done items,
1891 ;; todos-category-done string is left dangling,
1892 ;; because todos-forward-item jumps over it.
1893 (if (and (eobp)
1894 (looking-back
1895 (concat (regexp-quote todos-done-string)
1896 "\n")))
1897 (delete-region (point) (progn
1898 (forward-line -2)
1899 (point))))))
1900 (t ; Filter top priority items.
1901 (setq num (or cnum fnum num))
1902 (unless (zerop num)
1903 (todos-forward-item num))))
1904 (setq beg (point))
1905 ;; Delete non-top-priority items.
1906 (unless (member filter '(diary regexp))
1907 (delete-region beg end))
1908 (goto-char (point-min))
1909 ;; Add file (if using multiple files) and category tags to
1910 ;; item.
1911 (while (not (eobp))
1912 (when (re-search-forward
1913 (concat (if todos-filter-done-items
1914 (concat "\\(?:" todos-done-string-start
1915 "\\|" todos-date-string-start
1916 "\\)")
1917 todos-date-string-start)
1918 todos-date-pattern "\\(?: " diary-time-regexp
1919 "\\)?" (if todos-filter-done-items
1920 "\\]"
1921 (regexp-quote todos-nondiary-end))
1922 "?")
1923 nil t)
1924 (insert " [")
1925 (when (looking-at "(archive) ") (goto-char (match-end 0)))
1926 (insert (if multifile (concat fname ":") "") cat "]"))
1927 (forward-line))
1928 (widen)))
1929 (setq bufstr (buffer-string))
1930 (with-current-buffer buf
1931 (let (buffer-read-only)
1932 (insert bufstr)))))))
1933 (set-window-buffer (selected-window) (set-buffer buf))
1934 (todos-prefix-overlays)
1935 (goto-char (point-min)))))
1936
1937 (defun todos-set-top-priorities (&optional arg)
1938 "Set number of top priorities shown by `todos-top-priorities'.
1939 With non-nil ARG, set the number only for the current Todos
1940 category; otherwise, set the number for all categories in the
1941 current Todos file.
1942
1943 Calling this function via either of the commands
1944 `todos-set-top-priorities-in-file' or
1945 `todos-set-top-priorities-in-category' is the recommended way to
1946 set the user customizable option `todos-priorities-rules'."
1947 (let* ((cat (todos-current-category))
1948 (file todos-current-todos-file)
1949 (rules todos-priorities-rules)
1950 (frule (assoc-string file rules))
1951 (crule (assoc-string cat (nth 2 frule)))
1952 (cur (or (if arg (cdr crule) (nth 1 frule))
1953 todos-show-priorities))
1954 (prompt (concat "Current number of top priorities in this "
1955 (if arg "category" "file") ": %d; "
1956 "enter new number: "))
1957 (new "-1")
1958 nrule)
1959 (while (< (string-to-number new) 0)
1960 (let ((cur0 cur))
1961 (setq new (read-number (format prompt cur0) cur0)
1962 prompt "Enter a non-negative number: "
1963 cur0 nil)))
1964 (setq nrule (if arg
1965 (append (nth 2 (delete crule frule)) (list (cons cat new)))
1966 (append (list file new) (list (nth 2 frule)))))
1967 (setq rules (cons (if arg
1968 (list file cur nrule)
1969 nrule)
1970 (delete frule rules)))
1971 (customize-save-variable 'todos-priorities-rules rules)))
1972
1973 (defun todos-filtered-buffer-name (buffer-type file-list)
1974 "Rename Todos filtered buffer using BUFFER-TYPE and FILE-LIST.
1975
1976 The new name is constructed from the string BUFFER-TYPE, which
1977 refers to one of the top priorities, diary or regexp item
1978 filters, and the names of the filtered files in FILE-LIST. Used
1979 in Todos Filter Items mode."
1980 (let* ((flist (if (listp file-list) file-list (list file-list)))
1981 (multi (> (length flist) 1))
1982 (fnames (mapconcat (lambda (f) (todos-short-file-name f))
1983 flist ", ")))
1984 (rename-buffer (format (concat "%s for file" (if multi "s" "")
1985 " \"%s\"") buffer-type fnames))))
1986
1987 ;; ---------------------------------------------------------------------------
1988 ;;; Sorting and display routines for Todos Categories mode.
1989
1990 (defun todos-longest-category-name-length (categories)
1991 "Return the length of the longest name in list CATEGORIES."
1992 (let ((longest 0))
1993 (dolist (c categories longest)
1994 (setq longest (max longest (length c))))))
1995
1996 (defun todos-padded-string (str)
1997 "Return string STR padded with spaces.
1998 The placement of the padding is determined by the value of user
1999 option `todos-categories-align'."
2000 (let* ((categories (mapcar 'car todos-categories))
2001 (len (max (todos-longest-category-name-length categories)
2002 (length todos-categories-category-label)))
2003 (strlen (length str))
2004 (strlen-odd (eq (logand strlen 1) 1)) ; oddp from cl.el
2005 (padding (max 0 (/ (- len strlen) 2)))
2006 (padding-left (cond ((eq todos-categories-align 'left) 0)
2007 ((eq todos-categories-align 'center) padding)
2008 ((eq todos-categories-align 'right)
2009 (if strlen-odd (1+ (* padding 2)) (* padding 2)))))
2010 (padding-right (cond ((eq todos-categories-align 'left)
2011 (if strlen-odd (1+ (* padding 2)) (* padding 2)))
2012 ((eq todos-categories-align 'center)
2013 (if strlen-odd (1+ padding) padding))
2014 ((eq todos-categories-align 'right) 0))))
2015 (concat (make-string padding-left 32) str (make-string padding-right 32))))
2016
2017 (defvar todos-descending-counts nil
2018 "List of keys for category counts sorted in descending order.")
2019
2020 (defun todos-sort (list &optional key)
2021 "Return a copy of LIST, possibly sorted according to KEY."
2022 (let* ((l (copy-sequence list))
2023 (fn (if (eq key 'alpha)
2024 (lambda (x) (upcase x)) ; Alphabetize case insensitively.
2025 (lambda (x) (todos-get-count key x))))
2026 ;; Keep track of whether the last sort by key was descending or
2027 ;; ascending.
2028 (descending (member key todos-descending-counts))
2029 (cmp (if (eq key 'alpha)
2030 'string<
2031 (if descending '< '>)))
2032 (pred (lambda (s1 s2) (let ((t1 (funcall fn (car s1)))
2033 (t2 (funcall fn (car s2))))
2034 (funcall cmp t1 t2)))))
2035 (when key
2036 (setq l (sort l pred))
2037 ;; Switch between descending and ascending sort order.
2038 (if descending
2039 (setq todos-descending-counts
2040 (delete key todos-descending-counts))
2041 (push key todos-descending-counts)))
2042 l))
2043
2044 (defun todos-display-sorted (type)
2045 "Keep point on the TYPE count sorting button just clicked."
2046 (let ((opoint (point)))
2047 (todos-update-categories-display type)
2048 (goto-char opoint)))
2049
2050 (defun todos-label-to-key (label)
2051 "Return symbol for sort key associated with LABEL."
2052 (let (key)
2053 (cond ((string= label todos-categories-category-label)
2054 (setq key 'alpha))
2055 ((string= label todos-categories-todo-label)
2056 (setq key 'todo))
2057 ((string= label todos-categories-diary-label)
2058 (setq key 'diary))
2059 ((string= label todos-categories-done-label)
2060 (setq key 'done))
2061 ((string= label todos-categories-archived-label)
2062 (setq key 'archived)))
2063 key))
2064
2065 (defun todos-insert-sort-button (label)
2066 "Insert button for displaying categories sorted by item counts.
2067 LABEL determines which type of count is sorted."
2068 (setq str (if (string= label todos-categories-category-label)
2069 (todos-padded-string label)
2070 label))
2071 (setq beg (point))
2072 (setq end (+ beg (length str)))
2073 (insert-button str 'face nil
2074 'action
2075 `(lambda (button)
2076 (let ((key (todos-label-to-key ,label)))
2077 (if (and (member key todos-descending-counts)
2078 (eq key 'alpha))
2079 (progn
2080 ;; If display is alphabetical, switch back to
2081 ;; category priority order.
2082 (todos-display-sorted nil)
2083 (setq todos-descending-counts
2084 (delete key todos-descending-counts)))
2085 (todos-display-sorted key)))))
2086 (setq ovl (make-overlay beg end))
2087 (overlay-put ovl 'face 'todos-button))
2088
2089 (defun todos-total-item-counts ()
2090 "Return a list of total item counts for the current file."
2091 (mapcar (lambda (i) (apply '+ (mapcar (lambda (l) (aref l i))
2092 (mapcar 'cdr todos-categories))))
2093 (list 0 1 2 3)))
2094
2095 (defvar todos-categories-category-number 0
2096 "Variable for numbering categories in Todos Categories mode.")
2097
2098 (defun todos-insert-category-line (cat &optional nonum)
2099 "Insert button with category CAT's name and item counts.
2100 With non-nil argument NONUM show only these; otherwise, insert a
2101 number in front of the button indicating the category's priority.
2102 The number and the category name are separated by the string
2103 which is the value of the user option
2104 `todos-categories-number-separator'."
2105 (let ((archive (member todos-current-todos-file todos-archives))
2106 (num todos-categories-category-number)
2107 (str (todos-padded-string cat))
2108 (opoint (point)))
2109 (setq num (1+ num) todos-categories-category-number num)
2110 (insert-button
2111 (concat (if nonum
2112 (make-string (+ 4 (length todos-categories-number-separator))
2113 32)
2114 (format " %3d%s" num todos-categories-number-separator))
2115 str
2116 (mapconcat (lambda (elt)
2117 (concat
2118 (make-string (1+ (/ (length (car elt)) 2)) 32) ; label
2119 (format "%3d" (todos-get-count (cdr elt) cat)) ; count
2120 ;; Add an extra space if label length is odd
2121 ;; (using def of oddp from cl.el).
2122 (if (eq (logand (length (car elt)) 1) 1) " ")))
2123 (if archive
2124 (list (cons todos-categories-done-label 'done))
2125 (list (cons todos-categories-todo-label 'todo)
2126 (cons todos-categories-diary-label 'diary)
2127 (cons todos-categories-done-label 'done)
2128 (cons todos-categories-archived-label
2129 'archived)))
2130 "")
2131 " ") ; So highlighting of last column is consistent with the others.
2132 'face (if (and todos-skip-archived-categories
2133 (zerop (todos-get-count 'todo cat))
2134 (zerop (todos-get-count 'done cat))
2135 (not (zerop (todos-get-count 'archived cat))))
2136 'todos-archived-only
2137 nil)
2138 'action `(lambda (button) (let ((buf (current-buffer)))
2139 (todos-jump-to-category ,cat)
2140 (kill-buffer buf))))
2141 ;; Highlight the sorted count column.
2142 (let* ((beg (+ opoint 7 (length str)))
2143 end ovl)
2144 (cond ((eq nonum 'todo)
2145 (setq beg (+ beg 1 (/ (length todos-categories-todo-label) 2))))
2146 ((eq nonum 'diary)
2147 (setq beg (+ beg 1 (length todos-categories-todo-label)
2148 2 (/ (length todos-categories-diary-label) 2))))
2149 ((eq nonum 'done)
2150 (setq beg (+ beg 1 (length todos-categories-todo-label)
2151 2 (length todos-categories-diary-label)
2152 2 (/ (length todos-categories-done-label) 2))))
2153 ((eq nonum 'archived)
2154 (setq beg (+ beg 1 (length todos-categories-todo-label)
2155 2 (length todos-categories-diary-label)
2156 2 (length todos-categories-done-label)
2157 2 (/ (length todos-categories-archived-label) 2)))))
2158 (unless (= beg (+ opoint 7 (length str))) ; Don't highlight categories.
2159 (setq end (+ beg 4))
2160 (setq ovl (make-overlay beg end))
2161 (overlay-put ovl 'face 'todos-sorted-column)))
2162 (newline)))
2163
2164 (defun todos-display-categories-1 ()
2165 "Prepare buffer for displaying table of categories and item counts."
2166 (unless (eq major-mode 'todos-categories-mode)
2167 (setq todos-global-current-todos-file (or todos-current-todos-file
2168 todos-default-todos-file))
2169 (set-window-buffer (selected-window)
2170 (set-buffer (get-buffer-create todos-categories-buffer)))
2171 (kill-all-local-variables)
2172 (todos-categories-mode)
2173 (let ((archive (member todos-current-todos-file todos-archives))
2174 buffer-read-only)
2175 (erase-buffer)
2176 ;; FIXME: add usage tips?
2177 (insert (format (concat "Category counts for Todos "
2178 (if archive "archive" "file")
2179 " \"%s\".")
2180 (todos-short-file-name todos-current-todos-file)))
2181 (newline 2)
2182 ;; Make space for the column of category numbers.
2183 (insert (make-string (+ 4 (length todos-categories-number-separator)) 32))
2184 ;; Add the category and item count buttons (if this is the list of
2185 ;; categories in an archive, show only done item counts).
2186 (todos-insert-sort-button todos-categories-category-label)
2187 (if archive
2188 (progn
2189 (insert (make-string 3 32))
2190 (todos-insert-sort-button todos-categories-done-label))
2191 (insert (make-string 3 32))
2192 (todos-insert-sort-button todos-categories-todo-label)
2193 (insert (make-string 2 32))
2194 (todos-insert-sort-button todos-categories-diary-label)
2195 (insert (make-string 2 32))
2196 (todos-insert-sort-button todos-categories-done-label)
2197 (insert (make-string 2 32))
2198 (todos-insert-sort-button todos-categories-archived-label))
2199 (newline 2))))
2200
2201 (defun todos-update-categories-display (sortkey)
2202 ""
2203 (let* ((cats0 todos-categories)
2204 (cats (todos-sort cats0 sortkey))
2205 (archive (member todos-current-todos-file todos-archives))
2206 (todos-categories-category-number 0)
2207 ;; Find start of Category button if we just entered Todos Categories
2208 ;; mode.
2209 (pt (if (eq (point) (point-max))
2210 (save-excursion
2211 (forward-line -2)
2212 (goto-char (next-single-char-property-change
2213 (point) 'face nil (line-end-position))))))
2214 (buffer-read-only))
2215 (forward-line 2)
2216 (delete-region (point) (point-max))
2217 ;; Fill in the table with buttonized lines, each showing a category and
2218 ;; its item counts.
2219 (mapc (lambda (cat) (todos-insert-category-line cat sortkey))
2220 (mapcar 'car cats))
2221 (newline)
2222 ;; Add a line showing item count totals.
2223 (insert (make-string (+ 4 (length todos-categories-number-separator)) 32)
2224 (todos-padded-string todos-categories-totals-label)
2225 (mapconcat
2226 (lambda (elt)
2227 (concat
2228 (make-string (1+ (/ (length (car elt)) 2)) 32)
2229 (format "%3d" (nth (cdr elt) (todos-total-item-counts)))
2230 ;; Add an extra space if label length is odd (using
2231 ;; definition of oddp from cl.el).
2232 (if (eq (logand (length (car elt)) 1) 1) " ")))
2233 (if archive
2234 (list (cons todos-categories-done-label 2))
2235 (list (cons todos-categories-todo-label 0)
2236 (cons todos-categories-diary-label 1)
2237 (cons todos-categories-done-label 2)
2238 (cons todos-categories-archived-label 3)))
2239 ""))
2240 ;; Put cursor on Category button initially.
2241 (if pt (goto-char pt))
2242 (setq buffer-read-only t)))
2243
2244 ;; ---------------------------------------------------------------------------
2245 ;;; Routines for generating Todos insertion commands and key bindings
2246
2247 ;; Can either of these be included in Emacs? The originals are GFDL'd.
2248
2249 ;; Slightly reformulated from
2250 ;; http://rosettacode.org/wiki/Power_set#Common_Lisp.
2251 (defun powerset-recursive (l)
2252 (cond ((null l)
2253 (list nil))
2254 (t
2255 (let ((prev (powerset-recursive (cdr l))))
2256 (append (mapcar (lambda (elt) (cons (car l) elt))
2257 prev)
2258 prev)))))
2259
2260 ;; Elisp implementation of http://rosettacode.org/wiki/Power_set#C
2261 (defun powerset-bitwise (l)
2262 (let ((binnum (lsh 1 (length l)))
2263 pset elt)
2264 (dotimes (i binnum)
2265 (let ((bits i)
2266 (ll l))
2267 (while (not (zerop bits))
2268 (let ((arg (pop ll)))
2269 (unless (zerop (logand bits 1))
2270 (setq elt (append elt (list arg))))
2271 (setq bits (lsh bits -1))))
2272 (setq pset (append pset (list elt)))
2273 (setq elt nil)))
2274 pset))
2275
2276 ;; (defalias 'todos-powerset 'powerset-recursive)
2277 (defalias 'todos-powerset 'powerset-bitwise)
2278
2279 ;; Return list of lists of non-nil atoms produced from ARGLIST. The elements
2280 ;; of ARGLIST may be atoms or lists.
2281 (defun todos-gen-arglists (arglist)
2282 (let (arglists)
2283 (while arglist
2284 (let ((arg (pop arglist)))
2285 (cond ((symbolp arg)
2286 (setq arglists (if arglists
2287 (mapcar (lambda (l) (push arg l)) arglists)
2288 (list (push arg arglists)))))
2289 ((listp arg)
2290 (setq arglists
2291 (mapcar (lambda (a)
2292 (if (= 1 (length arglists))
2293 (apply (lambda (l) (push a l)) arglists)
2294 (mapcar (lambda (l) (push a l)) arglists)))
2295 arg))))))
2296 (setq arglists (mapcar 'reverse (apply 'append (mapc 'car arglists))))))
2297
2298 (defvar todos-insertion-commands-args-genlist
2299 '(diary nonmarking (calendar date dayname) time (here region))
2300 "Generator list for argument lists of Todos insertion commands.")
2301
2302 (defvar todos-insertion-commands-args
2303 (let ((argslist (todos-gen-arglists todos-insertion-commands-args-genlist))
2304 res new)
2305 (setq res (remove-duplicates
2306 (apply 'append (mapcar 'todos-powerset argslist)) :test 'equal))
2307 (dolist (l res)
2308 (unless (= 5 (length l))
2309 (let ((v (make-vector 5 nil)) elt)
2310 (while l
2311 (setq elt (pop l))
2312 (cond ((eq elt 'diary)
2313 (aset v 0 elt))
2314 ((eq elt 'nonmarking)
2315 (aset v 1 elt))
2316 ((or (eq elt 'calendar)
2317 (eq elt 'date)
2318 (eq elt 'dayname))
2319 (aset v 2 elt))
2320 ((eq elt 'time)
2321 (aset v 3 elt))
2322 ((or (eq elt 'here)
2323 (eq elt 'region))
2324 (aset v 4 elt))))
2325 (setq l (append v nil))))
2326 (setq new (append new (list l))))
2327 new)
2328 "List of all argument lists for Todos insertion commands.")
2329
2330 (defun todos-insertion-command-name (arglist)
2331 "Generate Todos insertion command name from ARGLIST."
2332 (replace-regexp-in-string
2333 "-\\_>" ""
2334 (replace-regexp-in-string
2335 "-+" "-"
2336 ;; FIXME: "todos-insert-item-"
2337 (concat "todos-item-insert-"
2338 (mapconcat (lambda (e) (if e (symbol-name e))) arglist "-")))))
2339
2340 (defvar todos-insertion-commands-names
2341 (mapcar (lambda (l)
2342 (todos-insertion-command-name l))
2343 todos-insertion-commands-args)
2344 "List of names of Todos insertion commands.")
2345
2346 ;; FIXME: prefix argument ARG is nil
2347 (defmacro todos-define-insertion-command (&rest args)
2348 (let ((name (intern (todos-insertion-command-name args)))
2349 (arg0 (nth 0 args))
2350 (arg1 (nth 1 args))
2351 (arg2 (nth 2 args))
2352 (arg3 (nth 3 args))
2353 (arg4 (nth 4 args)))
2354 `(defun ,name (&optional arg &rest args)
2355 "Todos item insertion command generated from ARGS."
2356 (interactive (list current-prefix-arg))
2357 (todos-insert-item arg ',arg0 ',arg1 ',arg2 ',arg3 ',arg4))))
2358
2359 ;; FIXME: exclude todos-insert-item (or rather from
2360 ;; todos-insertion-key-bindings?), otherwise its doc string won't be
2361 ;; found with C-h k (but it will with M-x todos-insert-item)
2362 (defvar todos-insertion-commands
2363 (mapcar (lambda (c)
2364 (eval `(todos-define-insertion-command ,@c)))
2365 todos-insertion-commands-args)
2366 "List of Todos insertion commands.")
2367
2368 (defvar todos-insertion-commands-arg-key-list
2369 '(("diary" "y" "yy")
2370 ("nonmarking" "k" "kk")
2371 ("calendar" "c" "cc")
2372 ("date" "d" "dd")
2373 ("dayname" "n" "nn")
2374 ("time" "t" "tt")
2375 ("here" "h" "h")
2376 ("region" "r" "r"))
2377 "")
2378
2379 (defun todos-insertion-key-bindings (map)
2380 ""
2381 (dolist (c todos-insertion-commands)
2382 (let* ((key "")
2383 (cname (symbol-name c)))
2384 (mapc (lambda (l)
2385 (let ((arg (nth 0 l))
2386 (key1 (nth 1 l))
2387 (key2 (nth 2 l)))
2388 (if (string-match (concat (regexp-quote arg) "\\_>") cname)
2389 (setq key (concat key key2)))
2390 (if (string-match (concat (regexp-quote arg) ".+") cname)
2391 (setq key (concat key key1)))))
2392 todos-insertion-commands-arg-key-list)
2393 (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname)
2394 (setq key (concat key "i")))
2395 (define-key map key c))))
2396
2397 (defvar todos-insertion-map
2398 (let ((map (make-keymap)))
2399 (todos-insertion-key-bindings map)
2400 (define-key map "p" 'todos-copy-item)
2401 map)
2402 "Keymap for Todos mode insertion commands.")
2403
2404 ;; ---------------------------------------------------------------------------
2405 ;;; Key maps and menus
2406
2407 (defvar todos-key-bindings
2408 `(
2409 ;; display
2410 ("Cd" . todos-display-categories) ;FIXME: Cs todos-show-categories?
2411 ("H" . todos-highlight-item)
2412 ("N" . todos-hide-show-item-numbering)
2413 ("D" . todos-hide-show-date-time)
2414 ("*" . todos-mark-unmark-item)
2415 ("C*" . todos-mark-category)
2416 ("Cu" . todos-unmark-category)
2417 ("PP" . todos-print)
2418 ("PF" . todos-print-to-file)
2419 ("v" . todos-hide-show-done-items)
2420 ("V" . todos-show-done-only)
2421 ("As" . todos-show-archive)
2422 ("Ac" . todos-choose-archive)
2423 ("Y" . todos-diary-items)
2424 ("Fe" . todos-edit-multiline)
2425 ("Fh" . todos-highlight-item)
2426 ("Fn" . todos-hide-show-item-numbering)
2427 ("Fd" . todos-hide-show-date-time)
2428 ("Ftt" . todos-top-priorities)
2429 ("Ftm" . todos-top-priorities-multifile)
2430 ("Fts" . todos-set-top-priorities-in-file)
2431 ("Cts" . todos-set-top-priorities-in-category)
2432 ("Fyy" . todos-diary-items)
2433 ("Fym" . todos-diary-items-multifile)
2434 ("Fxx" . todos-regexp-items)
2435 ("Fxm" . todos-regexp-items-multifile)
2436 ;; navigation
2437 ("f" . todos-forward-category)
2438 ("b" . todos-backward-category)
2439 ("j" . todos-jump-to-category)
2440 ("J" . todos-jump-to-category-other-file)
2441 ("n" . todos-forward-item)
2442 ("p" . todos-backward-item)
2443 ("S" . todos-search)
2444 ("X" . todos-clear-matches)
2445 ;; editing
2446 ("Fa" . todos-add-file)
2447 ("Ca" . todos-add-category)
2448 ("Cr" . todos-rename-category)
2449 ("Cg" . todos-merge-category)
2450 ("Cm" . todos-move-category)
2451 ("Ck" . todos-delete-category)
2452 ("d" . todos-item-done)
2453 ("ee" . todos-edit-item)
2454 ("em" . todos-edit-multiline-item)
2455 ("eh" . todos-edit-item-header)
2456 ("edd" . todos-edit-item-date)
2457 ("edc" . todos-edit-item-date-from-calendar)
2458 ("edt" . todos-edit-item-date-is-today)
2459 ("et" . todos-edit-item-time)
2460 ("eyy" . todos-edit-item-diary-inclusion)
2461 ;; ("" . todos-edit-category-diary-inclusion)
2462 ("eyn" . todos-edit-item-diary-nonmarking)
2463 ;;("" . todos-edit-category-diary-nonmarking)
2464 ("ec" . todos-done-item-add-edit-or-delete-comment)
2465 ("i" . ,todos-insertion-map)
2466 ("k" . todos-delete-item) ;FIXME: not single letter?
2467 ("m" . todos-move-item)
2468 ("M" . todos-move-item-to-file)
2469 ("r" . todos-raise-item-priority)
2470 ("l" . todos-lower-item-priority)
2471 ("#" . todos-set-item-priority)
2472 ("u" . todos-item-undo)
2473 ("Ad" . todos-archive-done-item) ;FIXME: ad
2474 ("AD" . todos-archive-category-done-items) ;FIXME: aD or C-u ad ?
2475 ("s" . todos-save)
2476 ("q" . todos-quit)
2477 ([remap newline] . newline-and-indent)
2478 )
2479 "Alist pairing keys defined in Todos modes and their bindings.")
2480
2481 (defvar todos-mode-map
2482 (let ((map (make-keymap)))
2483 ;; Don't suppress digit keys, so they can supply prefix arguments.
2484 (suppress-keymap map)
2485 (dolist (ck todos-key-bindings)
2486 (define-key map (car ck) (cdr ck)))
2487 map)
2488 "Todos mode keymap.")
2489
2490 ;; FIXME
2491 (easy-menu-define
2492 todos-menu todos-mode-map "Todos Menu"
2493 '("Todos"
2494 ("Navigation"
2495 ["Next Item" todos-forward-item t]
2496 ["Previous Item" todos-backward-item t]
2497 "---"
2498 ["Next Category" todos-forward-category t]
2499 ["Previous Category" todos-backward-category t]
2500 ["Jump to Category" todos-jump-to-category t]
2501 ["Jump to Category in Other File" todos-jump-to-category-other-file t]
2502 "---"
2503 ["Search Todos File" todos-search t]
2504 ["Clear Highlighting on Search Matches" todos-category-done t])
2505 ("Display"
2506 ["List Current Categories" todos-display-categories t]
2507 ;; ["List Categories Alphabetically" todos-display-categories-alphabetically t]
2508 ["Turn Item Highlighting on/off" todos-highlight-item t]
2509 ["Turn Item Numbering on/off" todos-hide-show-item-numbering t]
2510 ["Turn Item Time Stamp on/off" todos-hide-show-date-time t]
2511 ["View/Hide Done Items" todos-hide-show-done-items t]
2512 "---"
2513 ["View Diary Items" todos-diary-items t]
2514 ["View Top Priority Items" todos-top-priorities t]
2515 ["View Multifile Top Priority Items" todos-top-priorities-multifile t]
2516 "---"
2517 ["Print Category" todos-print t])
2518 ("Editing"
2519 ["Insert New Item" todos-insert-item t]
2520 ["Insert Item Here" todos-insert-item-here t]
2521 ("More Insertion Commands")
2522 ["Edit Item" todos-edit-item t]
2523 ["Edit Multiline Item" todos-edit-multiline t]
2524 ["Edit Item Header" todos-edit-item-header t]
2525 ["Edit Item Date" todos-edit-item-date t]
2526 ["Edit Item Time" todos-edit-item-time t]
2527 "---"
2528 ["Lower Item Priority" todos-lower-item-priority t]
2529 ["Raise Item Priority" todos-raise-item-priority t]
2530 ["Set Item Priority" todos-set-item-priority t]
2531 ["Move (Recategorize) Item" todos-move-item t]
2532 ["Delete Item" todos-delete-item t]
2533 ["Undo Done Item" todos-item-undo t]
2534 ["Mark/Unmark Item for Diary" todos-toggle-item-diary-inclusion t]
2535 ["Mark/Unmark Items for Diary" todos-edit-item-diary-inclusion t]
2536 ["Mark & Hide Done Item" todos-item-done t]
2537 ["Archive Done Items" todos-archive-category-done-items t]
2538 "---"
2539 ["Add New Todos File" todos-add-file t]
2540 ["Add New Category" todos-add-category t]
2541 ["Delete Current Category" todos-delete-category t]
2542 ["Rename Current Category" todos-rename-category t]
2543 "---"
2544 ["Save Todos File" todos-save t]
2545 )
2546 "---"
2547 ["Quit" todos-quit t]
2548 ))
2549
2550 (defvar todos-archive-mode-map
2551 (let ((map (make-sparse-keymap)))
2552 (suppress-keymap map t)
2553 ;; navigation commands
2554 (define-key map "f" 'todos-forward-category)
2555 (define-key map "b" 'todos-backward-category)
2556 (define-key map "j" 'todos-jump-to-category)
2557 (define-key map "n" 'todos-forward-item)
2558 (define-key map "p" 'todos-backward-item)
2559 ;; display commands
2560 (define-key map "C" 'todos-display-categories)
2561 (define-key map "H" 'todos-highlight-item)
2562 (define-key map "N" 'todos-hide-show-item-numbering)
2563 ;; (define-key map "" 'todos-hide-show-date-time)
2564 (define-key map "P" 'todos-print)
2565 (define-key map "q" 'todos-quit)
2566 (define-key map "s" 'todos-save)
2567 (define-key map "S" 'todos-search)
2568 (define-key map "t" 'todos-show)
2569 (define-key map "u" 'todos-unarchive-items)
2570 (define-key map "U" 'todos-unarchive-category)
2571 map)
2572 "Todos Archive mode keymap.")
2573
2574 (defvar todos-edit-mode-map
2575 (let ((map (make-sparse-keymap)))
2576 (define-key map "\C-x\C-q" 'todos-edit-quit)
2577 (define-key map [remap newline] 'newline-and-indent)
2578 map)
2579 "Todos Edit mode keymap.")
2580
2581 (defvar todos-categories-mode-map
2582 (let ((map (make-sparse-keymap)))
2583 (suppress-keymap map t)
2584 (define-key map "c" 'todos-display-categories-alphabetically-or-by-priority)
2585 (define-key map "t" 'todos-display-categories-sorted-by-todo)
2586 (define-key map "y" 'todos-display-categories-sorted-by-diary)
2587 (define-key map "d" 'todos-display-categories-sorted-by-done)
2588 (define-key map "a" 'todos-display-categories-sorted-by-archived)
2589 (define-key map "l" 'todos-lower-category-priority)
2590 (define-key map "+" 'todos-lower-category-priority)
2591 (define-key map "r" 'todos-raise-category-priority)
2592 (define-key map "-" 'todos-raise-category-priority)
2593 (define-key map "n" 'todos-forward-button)
2594 (define-key map "p" 'todos-backward-button)
2595 (define-key map [tab] 'todos-forward-button)
2596 (define-key map [backtab] 'todos-backward-button)
2597 (define-key map "q" 'todos-quit)
2598 ;; (define-key map "A" 'todos-add-category)
2599 ;; (define-key map "D" 'todos-delete-category)
2600 ;; (define-key map "R" 'todos-rename-category)
2601 map)
2602 "Todos Categories mode keymap.")
2603
2604 (defvar todos-filtered-items-mode-map
2605 (let ((map (make-keymap)))
2606 (suppress-keymap map t)
2607 ;; navigation commands
2608 (define-key map "j" 'todos-jump-to-item)
2609 (define-key map [remap newline] 'todos-jump-to-item)
2610 (define-key map "n" 'todos-forward-item)
2611 (define-key map "p" 'todos-backward-item)
2612 (define-key map "H" 'todos-highlight-item)
2613 (define-key map "N" 'todos-hide-show-item-numbering)
2614 (define-key map "D" 'todos-hide-show-date-time)
2615 (define-key map "P" 'todos-print)
2616 (define-key map "q" 'todos-quit)
2617 (define-key map "s" 'todos-save)
2618 ;; editing commands
2619 (define-key map "l" 'todos-lower-item-priority)
2620 (define-key map "r" 'todos-raise-item-priority)
2621 (define-key map "#" 'todos-set-item-priority)
2622 map)
2623 "Todos Top Priorities mode keymap.")
2624
2625 ;; ---------------------------------------------------------------------------
2626 ;;; Mode definitions
2627
2628 (defun todos-modes-set-1 ()
2629 ""
2630 (set (make-local-variable 'font-lock-defaults) '(todos-font-lock-keywords t))
2631 (set (make-local-variable 'indent-line-function) 'todos-indent)
2632 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2633
2634 (defun todos-modes-set-2 ()
2635 ""
2636 (add-to-invisibility-spec 'todos)
2637 (setq buffer-read-only t)
2638 (set (make-local-variable 'hl-line-range-function)
2639 (lambda() (when (todos-item-end)
2640 (cons (todos-item-start) (todos-item-end))))))
2641
2642 (defun todos-modes-set-3 ()
2643 ""
2644 (set (make-local-variable 'todos-categories) (todos-set-categories))
2645 (set (make-local-variable 'todos-category-number) 1)
2646 (set (make-local-variable 'todos-first-visit) t)
2647 (add-hook 'find-file-hook 'todos-display-as-todos-file nil t))
2648
2649 (put 'todos-mode 'mode-class 'special)
2650
2651 (define-derived-mode todos-mode special-mode "Todos"
2652 "Major mode for displaying, navigating and editing Todo lists.
2653
2654 \\{todos-mode-map}"
2655 (easy-menu-add todos-menu)
2656 (todos-modes-set-1)
2657 (todos-modes-set-2)
2658 (todos-modes-set-3)
2659 ;; Initialize todos-current-todos-file.
2660 (when (member (file-truename (buffer-file-name))
2661 (funcall todos-files-function))
2662 (set (make-local-variable 'todos-current-todos-file)
2663 (file-truename (buffer-file-name))))
2664 (set (make-local-variable 'todos-first-visit) t)
2665 (set (make-local-variable 'todos-show-done-only) nil)
2666 (set (make-local-variable 'todos-categories-with-marks) nil)
2667 (add-hook 'find-file-hook 'todos-add-to-buffer-list nil t)
2668 (add-hook 'post-command-hook 'todos-update-buffer-list nil t)
2669 (when todos-show-current-file
2670 (add-hook 'pre-command-hook 'todos-show-current-file nil t))
2671 (add-hook 'window-configuration-change-hook
2672 'todos-reset-and-enable-done-separator nil t)
2673 (add-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file nil t))
2674
2675 (defun todos-unload-hook ()
2676 ""
2677 (remove-hook 'pre-command-hook 'todos-show-current-file t)
2678 (remove-hook 'post-command-hook 'todos-update-buffer-list t)
2679 (remove-hook 'find-file-hook 'todos-display-as-todos-file t)
2680 (remove-hook 'find-file-hook 'todos-add-to-buffer-list t)
2681 (remove-hook 'window-configuration-change-hook
2682 'todos-reset-and-enable-done-separator t)
2683 (remove-hook 'kill-buffer-hook 'todos-reset-global-current-todos-file t))
2684
2685 (put 'todos-archive-mode 'mode-class 'special)
2686
2687 ;; If todos-mode is parent, all todos-mode key bindings appear to be
2688 ;; available in todos-archive-mode (e.g. shown by C-h m).
2689 (define-derived-mode todos-archive-mode special-mode "Todos-Arch"
2690 "Major mode for archived Todos categories.
2691
2692 \\{todos-archive-mode-map}"
2693 (todos-modes-set-1)
2694 (todos-modes-set-2)
2695 (todos-modes-set-3)
2696 (set (make-local-variable 'todos-current-todos-file)
2697 (file-truename (buffer-file-name)))
2698 (set (make-local-variable 'todos-show-done-only) t))
2699
2700 (defun todos-mode-external-set ()
2701 ""
2702 (set (make-local-variable 'todos-current-todos-file)
2703 todos-global-current-todos-file)
2704 (let ((cats (with-current-buffer
2705 (find-buffer-visiting todos-current-todos-file)
2706 todos-categories)))
2707 (set (make-local-variable 'todos-categories) cats)))
2708
2709 (define-derived-mode todos-edit-mode text-mode "Todos-Ed"
2710 "Major mode for editing multiline Todo items.
2711
2712 \\{todos-edit-mode-map}"
2713 (todos-modes-set-1)
2714 (todos-mode-external-set))
2715
2716 (put 'todos-categories-mode 'mode-class 'special)
2717
2718 (define-derived-mode todos-categories-mode special-mode "Todos-Cats"
2719 "Major mode for displaying and editing Todos categories.
2720
2721 \\{todos-categories-mode-map}"
2722 (todos-mode-external-set))
2723
2724 (put 'todos-filter-mode 'mode-class 'special)
2725
2726 (define-derived-mode todos-filtered-items-mode special-mode "Todos-Fltr"
2727 "Mode for displaying and reprioritizing top priority Todos.
2728
2729 \\{todos-filtered-items-mode-map}"
2730 (todos-modes-set-1)
2731 (todos-modes-set-2))
2732
2733 ;; ---------------------------------------------------------------------------
2734 ;;; Todos Commands
2735
2736 ;; ---------------------------------------------------------------------------
2737 ;;; Entering and Exiting
2738
2739 ;;;###autoload
2740 (defun todos-show (&optional solicit-file)
2741 "Visit the current Todos file and display one of its categories.
2742 With non-nil prefix argument SOLICIT-FILE prompt for which todo
2743 file to visit.
2744
2745 Without a prefix argument, the first invocation of this command
2746 in a session visits `todos-default-todos-file' (creating it if it
2747 does not yet exist); subsequent invocations from outside of Todos
2748 mode revisit this file or, if the user option
2749 `todos-show-current-file' is non-nil, whichever Todos file
2750 \(either a todo or an archive file) was visited last.
2751
2752 The category displayed on initial invocation is the first member
2753 of `todos-categories' for the current Todos file, on subsequent
2754 invocations whichever category was displayed last. If
2755 `todos-display-categories-first' is non-nil, then the first
2756 invocation of `todos-show' displays a clickable listing of the
2757 categories in the current Todos file.
2758
2759 In Todos mode just the category's unfinished todo items are shown
2760 by default. The done items are hidden, but typing
2761 `\\[todos-hide-show-done-items]' displays them below the todo
2762 items. With non-nil user option `todos-show-with-done' both todo
2763 and done items are always shown on visiting a category.
2764
2765 If this command is invoked in Todos Archive mode, it visits the
2766 corresponding Todos file, displaying the corresponding category."
2767 (interactive "P")
2768 (let* ((cat)
2769 (file (cond (solicit-file
2770 (if (funcall todos-files-function)
2771 (todos-read-file-name "Choose a Todos file to visit: "
2772 nil t)
2773 (error "There are no Todos files")))
2774 ((and (eq major-mode 'todos-archive-mode)
2775 ;; Called noninteractively via todos-quit from
2776 ;; Todos Categories mode to return to archive file.
2777 (called-interactively-p 'any))
2778 (setq cat (todos-current-category))
2779 (concat (file-name-sans-extension todos-current-todos-file)
2780 ".todo"))
2781 (t
2782 (or todos-current-todos-file
2783 (and todos-show-current-file
2784 todos-global-current-todos-file)
2785 todos-default-todos-file
2786 (todos-add-file))))))
2787 (if (and todos-first-visit todos-display-categories-first)
2788 (todos-display-categories)
2789 (set-window-buffer (selected-window)
2790 (set-buffer (find-file-noselect file)))
2791 ;; If called from archive file, show corresponding category in Todos
2792 ;; file, if it exists.
2793 (when (assoc cat todos-categories)
2794 (setq todos-category-number (todos-category-number cat)))
2795 ;; If this is a new Todos file, add its first category.
2796 (when (zerop (buffer-size))
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 "$")
3095 nil t)
3096 (forward-line)
3097 (if (re-search-forward
3098 (concat "^" (regexp-quote todos-category-beg)
3099 "\\(.*\\)$") nil t)
3100 (progn (goto-char (match-beginning 0))
3101 (newline)
3102 (forward-line -1))
3103 (goto-char (point-max)))
3104 (insert item "\n")
3105 (goto-char beg))
3106 (push cat cats)
3107 (goto-char beg)
3108 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3109 (forward-line))
3110 (set-marker beg nil)
3111 (set-marker end nil))
3112 (setq file (concat (file-name-sans-extension file) ".toda"))
3113 (write-region (point-min) (point-max) file nil 'nomessage nil t))
3114 (with-temp-buffer
3115 (insert-file-contents file)
3116 (let ((todos-categories (todos-make-categories-list t)))
3117 (todos-update-categories-sexp))
3118 (write-region (point-min) (point-max) file nil 'nomessage)
3119 (setq archive-sexp (read (buffer-substring-no-properties
3120 (line-beginning-position)
3121 (line-end-position)))))
3122 (setq file (concat (file-name-sans-extension file) ".todo"))
3123 ;; Update categories sexp of converted Todos file again, adding
3124 ;; counts of archived items.
3125 (with-temp-buffer
3126 (insert-file-contents file)
3127 (let ((sexp (read (buffer-substring-no-properties
3128 (line-beginning-position)
3129 (line-end-position)))))
3130 (dolist (cat sexp)
3131 (let ((archive-cat (assoc (car cat) archive-sexp)))
3132 (if archive-cat
3133 (aset (cdr cat) 3 (aref (cdr archive-cat) 2)))))
3134 (delete-region (line-beginning-position) (line-end-position))
3135 (prin1 sexp (current-buffer)))
3136 (write-region (point-min) (point-max) file nil 'nomessage)))
3137 (todos-reevaluate-filelist-defcustoms)
3138 (message "Format conversion done."))
3139 (error "No legacy Todo file exists")))
3140
3141 ;; ---------------------------------------------------------------------------
3142 ;;; Navigation Commands
3143
3144 (defun todos-forward-category (&optional back)
3145 "Visit the numerically next category in this Todos file.
3146 If the current category is the highest numbered, visit the first
3147 category. With non-nil argument BACK, visit the numerically
3148 previous category (the highest numbered one, if the current
3149 category is the first)."
3150 (interactive)
3151 (setq todos-category-number
3152 (1+ (mod (- todos-category-number (if back 2 0))
3153 (length todos-categories))))
3154 (when todos-skip-archived-categories
3155 (while (and (zerop (todos-get-count 'todo))
3156 (zerop (todos-get-count 'done))
3157 (not (zerop (todos-get-count 'archived))))
3158 (setq todos-category-number
3159 (apply (if back '1- '1+) (list todos-category-number)))))
3160 (todos-category-select)
3161 (goto-char (point-min)))
3162
3163 (defun todos-backward-category ()
3164 "Visit the numerically previous category in this Todos file.
3165 If the current category is the highest numbered, visit the first
3166 category."
3167 (interactive)
3168 (todos-forward-category t))
3169
3170 (defun todos-jump-to-category (&optional cat other-file)
3171 "Jump to a category in this or another Todos file.
3172
3173 Programmatically, optional argument CAT provides the category
3174 name. When nil (as in interactive calls), prompt for the
3175 category, with TAB completion on existing categories. If a
3176 non-existing category name is entered, ask whether to add a new
3177 category with this name; if affirmed, add it, then jump to that
3178 category. With non-nil argument OTHER-FILE, prompt for a Todos
3179 file, otherwise jump within the current Todos file."
3180 (interactive)
3181 (let ((file (or (and other-file
3182 (todos-read-file-name "Choose a Todos file: " nil t))
3183 ;; Jump to archived-only Categories from Todos Categories
3184 ;; mode.
3185 (and cat
3186 todos-skip-archived-categories
3187 (zerop (todos-get-count 'todo cat))
3188 (zerop (todos-get-count 'done cat))
3189 (not (zerop (todos-get-count 'archived cat)))
3190 (concat (file-name-sans-extension
3191 todos-current-todos-file) ".toda"))
3192 todos-current-todos-file
3193 ;; If invoked from outside of Todos mode before
3194 ;; todos-show...
3195 todos-default-todos-file)))
3196 (with-current-buffer (find-file-noselect file)
3197 (and other-file (setq todos-current-todos-file file))
3198 (let ((category (or (and (assoc cat todos-categories) cat)
3199 (todos-read-category "Jump to category: "))))
3200 ;; Clean up after selecting category in Todos Categories mode.
3201 (if (string= (buffer-name) todos-categories-buffer)
3202 (kill-buffer))
3203 (if (or cat other-file)
3204 (set-window-buffer (selected-window)
3205 (set-buffer (find-buffer-visiting file))))
3206 (unless todos-global-current-todos-file
3207 (setq todos-global-current-todos-file todos-current-todos-file))
3208 (todos-category-number category) ; (1+ (length t-c)) if new category.
3209 ;; (if (> todos-category-number (length todos-categories))
3210 ;; (setq todos-category-number (todos-add-category category)))
3211 (todos-category-select)
3212 (goto-char (point-min))))))
3213
3214 (defun todos-jump-to-category-other-file ()
3215 "Jump to a category in another Todos file.
3216 The category is chosen by prompt, with TAB completion."
3217 (interactive)
3218 (todos-jump-to-category nil t))
3219
3220 (defun todos-jump-to-any-category ()
3221 ""
3222 (interactive)
3223 (let* ((cats-alist todos-all-categories-alist)
3224 (cats (mapcar 'car cats-alist))
3225 (completion-ignore-case todos-completion-ignore-case)
3226 (cat (completing-read "Jump to category: " cats nil t))
3227 (files (if (zerop (length cat))
3228 (keyboard-quit)
3229 (cdr (assoc cat cats-alist))))
3230 (file (if (nlistp files)
3231 files
3232 (completing-read (format "Jump to \"%s\" in which file? " cat)
3233 files nil t))))
3234 (if (zerop (length file))
3235 (keyboard-quit)
3236 (setq file (concat todos-files-directory file ".todo"))
3237 (set-window-buffer (selected-window)
3238 (set-buffer (find-file-noselect file)))
3239 (unless todos-global-current-todos-file
3240 (setq todos-global-current-todos-file todos-current-todos-file))
3241 (todos-category-number cat)
3242 (todos-category-select)
3243 (goto-char (point-min)))))
3244
3245 (defun todos-jump-to-item ()
3246 "Jump to the file and category of the filtered item at point."
3247 (interactive)
3248 (let ((str (todos-item-string))
3249 (buf (current-buffer))
3250 cat file archive beg)
3251 (string-match (concat (if todos-filter-done-items
3252 (concat "\\(?:" todos-done-string-start "\\|"
3253 todos-date-string-start "\\)")
3254 todos-date-string-start)
3255 todos-date-pattern "\\(?: " diary-time-regexp "\\)?"
3256 (if todos-filter-done-items
3257 "\\]"
3258 (regexp-quote todos-nondiary-end)) "?"
3259 "\\(?4: \\[\\(?3:(archive) \\)?\\(?2:.*:\\)?"
3260 "\\(?1:.*\\)\\]\\).*$") str)
3261 (setq cat (match-string 1 str))
3262 (setq file (match-string 2 str))
3263 (setq archive (string= (match-string 3 str) "(archive) "))
3264 (setq str (replace-match "" nil nil str 4))
3265 (setq file (if file
3266 (concat todos-files-directory (substring file 0 -1)
3267 (if archive ".toda" ".todo"))
3268 (if archive
3269 (concat (file-name-sans-extension
3270 todos-global-current-todos-file) ".toda")
3271 todos-global-current-todos-file)))
3272 (find-file-noselect file)
3273 (with-current-buffer (find-buffer-visiting file)
3274 (widen)
3275 (goto-char (point-min))
3276 (re-search-forward
3277 (concat "^" (regexp-quote (concat todos-category-beg cat)) "$") nil t)
3278 (search-forward str)
3279 (setq beg (match-beginning 0)))
3280 (kill-buffer buf)
3281 (set-window-buffer (selected-window) (set-buffer (find-buffer-visiting file)))
3282 (setq todos-current-todos-file file)
3283 (setq todos-category-number (todos-category-number cat))
3284 (let ((todos-show-with-done (if todos-filter-done-items t
3285 todos-show-with-done)))
3286 (todos-category-select))
3287 (goto-char beg)))
3288
3289 (defun todos-forward-item (&optional count)
3290 "Move point down to start of item with next lower priority.
3291 With positive numerical prefix COUNT, move point COUNT items
3292 downward."
3293 (interactive "P")
3294 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3295 ;; the corresponding command.
3296 (if (and count (> 1 count))
3297 (error "This command only accepts a positive numerical prefix argument")
3298 (let* ((not-done (not (or (todos-done-item-p) (looking-at "^$"))))
3299 (start (line-end-position)))
3300 (goto-char start)
3301 (if (re-search-forward todos-item-start nil t (or count 1))
3302 (goto-char (match-beginning 0))
3303 (goto-char (point-max)))
3304 ;; If points advances by one from a todo to a done item, go back to the
3305 ;; space above todos-done-separator, since that is a legitimate place to
3306 ;; insert an item. But skip this space if count > 1, since that should
3307 ;; only stop on an item.
3308 (when (and not-done (todos-done-item-p))
3309 (if (or (not count) (= count 1))
3310 (re-search-backward "^$" start t))))))
3311 ;; FIXME: The preceding sexp is insufficient when buffer is not narrowed,
3312 ;; since there could be no done items in this category, so the search puts
3313 ;; us on first todo item of next category. Does this ever happen? If so:
3314 ;; (let ((opoint) (point))
3315 ;; (forward-line -1)
3316 ;; (when (or (not count) (= count 1))
3317 ;; (cond ((looking-at (concat "^" (regexp-quote todos-category-beg)))
3318 ;; (forward-line -2))
3319 ;; ((looking-at (concat "^" (regexp-quote todos-category-done)))
3320 ;; (forward-line -1))
3321 ;; (t
3322 ;; (goto-char opoint)))))))
3323
3324 (defun todos-backward-item (&optional count)
3325 "Move point up to start of item with next higher priority.
3326 With positive numerical prefix COUNT, move point COUNT items
3327 upward."
3328 (interactive "P")
3329 ;; Avoid moving to bob if on the first item but not at bob.
3330 (when (> (line-number-at-pos) 1)
3331 ;; It's not worth the trouble to allow prefix arg value < 1, since we have
3332 ;; the corresponding command.
3333 (if (and count (> 1 count))
3334 (error "This command only accepts a positive numerical prefix argument")
3335 (let* ((done (todos-done-item-p)))
3336 (todos-item-start)
3337 (unless (bobp)
3338 (re-search-backward todos-item-start nil t (or count 1)))
3339 ;; Unless this is a regexp filtered items buffer (which can contain
3340 ;; intermixed todo and done items), if points advances by one from a
3341 ;; done to a todo item, go back to the space above
3342 ;; todos-done-separator, since that is a legitimate place to insert an
3343 ;; item. But skip this space if count > 1, since that should only
3344 ;; stop on an item.
3345 (when (and done (not (todos-done-item-p)) (or (not count) (= count 1))
3346 (not (equal (buffer-name) todos-regexp-items-buffer)))
3347 (re-search-forward (concat "^" (regexp-quote todos-category-done))
3348 nil t)
3349 (forward-line -1))))))
3350
3351 (defun todos-forward-button (n &optional wrap display-message)
3352 ""
3353 (interactive "p\nd\nd")
3354 (forward-button n wrap display-message)
3355 (and (bolp) (button-at (point))
3356 ;; Align with beginning of category label.
3357 (forward-char (+ 4 (length todos-categories-number-separator)))))
3358
3359 (defun todos-backward-button (n &optional wrap display-message)
3360 ""
3361 (interactive "p\nd\nd")
3362 (backward-button n wrap display-message)
3363 (and (bolp) (button-at (point))
3364 ;; Align with beginning of category label.
3365 (forward-char (+ 4 (length todos-categories-number-separator)))))
3366
3367 ;; FIXME: (i) Extend search to other Todos files. (ii) Allow navigating among
3368 ;; hits. (But these features are effectively available with
3369 ;; todos-regexp-items-multifile, so maybe it's not worth the trouble here.)
3370 (defun todos-search ()
3371 "Search for a regular expression in this Todos file.
3372 The search runs through the whole file and encompasses all and
3373 only todo and done items; it excludes category names. Multiple
3374 matches are shown sequentially, highlighted in `todos-search'
3375 face."
3376 (interactive)
3377 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
3378 (opoint (point))
3379 matches match cat in-done ov mlen msg)
3380 (widen)
3381 (goto-char (point-min))
3382 (while (not (eobp))
3383 (setq match (re-search-forward regex nil t))
3384 (goto-char (line-beginning-position))
3385 (unless (or (equal (point) 1)
3386 (looking-at (concat "^" (regexp-quote todos-category-beg))))
3387 (if match (push match matches)))
3388 (forward-line))
3389 (setq matches (reverse matches))
3390 (if matches
3391 (catch 'stop
3392 (while matches
3393 (setq match (pop matches))
3394 (goto-char match)
3395 (todos-item-start)
3396 (when (looking-at todos-done-string-start)
3397 (setq in-done t))
3398 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
3399 "\\(.*\\)\n") nil t)
3400 (setq cat (match-string-no-properties 1))
3401 (todos-category-number cat)
3402 (todos-category-select)
3403 (if in-done
3404 (unless todos-show-with-done (todos-hide-show-done-items)))
3405 (goto-char match)
3406 (setq ov (make-overlay (- (point) (length regex)) (point)))
3407 (overlay-put ov 'face 'todos-search)
3408 (when matches
3409 (setq mlen (length matches))
3410 (if (y-or-n-p
3411 (if (> mlen 1)
3412 (format "There are %d more matches; go to next match? "
3413 mlen)
3414 "There is one more match; go to it? "))
3415 (widen)
3416 (throw 'stop (setq msg (if (> mlen 1)
3417 (format "There are %d more matches."
3418 mlen)
3419 "There is one more match."))))))
3420 (setq msg "There are no more matches."))
3421 (todos-category-select)
3422 (goto-char opoint)
3423 (message "No match for \"%s\"" regex))
3424 (when msg
3425 (if (y-or-n-p (concat msg "\nUnhighlight matches? "))
3426 (todos-clear-matches)
3427 (message "You can unhighlight the matches later by typing %s"
3428 (key-description (car (where-is-internal
3429 'todos-clear-matches))))))))
3430
3431 (defun todos-clear-matches ()
3432 "Remove highlighting on matches found by todos-search."
3433 (interactive)
3434 (remove-overlays 1 (1+ (buffer-size)) 'face 'todos-search))
3435
3436 ;; ---------------------------------------------------------------------------
3437 ;;; Display Commands
3438
3439 (defun todos-hide-show-item-numbering ()
3440 ""
3441 (interactive)
3442 (todos-reset-prefix 'todos-number-priorities (not todos-number-priorities)))
3443
3444 (defun todos-hide-show-done-items ()
3445 "Show hidden or hide visible done items in current category."
3446 (interactive)
3447 (if (zerop (todos-get-count 'done (todos-current-category)))
3448 (message "There are no done items in this category.")
3449 (save-excursion
3450 (goto-char (point-min))
3451 (let ((todos-show-with-done (not (re-search-forward
3452 todos-done-string-start nil t))))
3453 (todos-category-select)))))
3454
3455 (defun todos-show-done-only ()
3456 "Switch between displaying only done or only todo items."
3457 (interactive)
3458 (setq todos-show-done-only (not todos-show-done-only))
3459 (todos-category-select))
3460
3461 (defun todos-highlight-item ()
3462 "Highlight or unhighlight the todo item the cursor is on."
3463 (interactive)
3464 (require 'hl-line)
3465 (if hl-line-mode
3466 (hl-line-mode -1)
3467 (hl-line-mode 1)))
3468
3469 (defun todos-hide-show-date-time ()
3470 "Hide or show date-time header of todo items in the current file."
3471 (interactive)
3472 (save-excursion
3473 (save-restriction
3474 (goto-char (point-min))
3475 (let ((ovs (overlays-in (point) (1+ (point))))
3476 ov hidden)
3477 (while ovs
3478 (setq ov (pop ovs))
3479 (if (equal (overlay-get ov 'display) "")
3480 (setq ovs nil hidden t)))
3481 (widen)
3482 (goto-char (point-min))
3483 (if hidden
3484 (remove-overlays (point-min) (point-max) 'display "")
3485 (while (not (eobp))
3486 (when (re-search-forward
3487 (concat todos-date-string-start todos-date-pattern
3488 "\\( " diary-time-regexp "\\)?"
3489 (regexp-quote todos-nondiary-end) "? ")
3490 nil t)
3491 (unless (save-match-data (todos-done-item-p))
3492 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
3493 (overlay-put ov 'display "")))
3494 (todos-forward-item)))))))
3495
3496 (defun todos-mark-unmark-item (&optional n all)
3497 "Mark item at point if unmarked, or unmark it if marked.
3498
3499 With a positive numerical prefix argument N, change the
3500 markedness of the next N items. With non-nil argument ALL, mark
3501 all visible items in the category (depending on visibility, all
3502 todo and done items, or just todo or just done items).
3503
3504 The mark is the character \"*\" inserted in front of the item's
3505 priority number or the `todos-prefix' string; if `todos-prefix'
3506 is \"*\", then the mark is \"@\"."
3507 (interactive "p")
3508 (if all (goto-char (point-min)))
3509 (unless (> n 0) (setq n 1))
3510 (let ((i 0))
3511 (while (or (and all (not (eobp)))
3512 (< i n))
3513 (let* ((cat (todos-current-category))
3514 (ov (todos-marked-item-p))
3515 (marked (assoc cat todos-categories-with-marks)))
3516 (if (and ov (not all))
3517 (progn
3518 (delete-overlay ov)
3519 (if (= (cdr marked) 1) ; Deleted last mark in this category.
3520 (setq todos-categories-with-marks
3521 (assq-delete-all cat todos-categories-with-marks))
3522 (setcdr marked (1- (cdr marked)))))
3523 (when (todos-item-start)
3524 (unless (and all (todos-marked-item-p))
3525 (setq ov (make-overlay (point) (point)))
3526 (overlay-put ov 'before-string todos-item-mark)
3527 (if marked
3528 (setcdr marked (1+ (cdr marked)))
3529 (push (cons cat 1) todos-categories-with-marks))))))
3530 (todos-forward-item)
3531 (setq i (1+ i)))))
3532
3533 (defun todos-mark-category ()
3534 "Put the \"*\" mark on all items in this category.
3535 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
3536 (interactive)
3537 (todos-mark-unmark-item 0 t))
3538
3539 (defun todos-unmark-category ()
3540 "Remove the \"*\" mark from all items in this category.
3541 \(If `todos-prefix' is \"*\", then the mark is \"@\".)"
3542 (interactive)
3543 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
3544 (setq todos-categories-with-marks
3545 (delq (assoc (todos-current-category) todos-categories-with-marks)
3546 todos-categories-with-marks)))
3547
3548 ;; ---------------------------------------------------------------------------
3549 ;;; Item filtering commands
3550
3551 (defun todos-set-top-priorities-in-file ()
3552 "Set number of top priorities for this file.
3553 See `todos-set-top-priorities' for more details."
3554 (interactive)
3555 (todos-set-top-priorities))
3556
3557 (defun todos-set-top-priorities-in-category ()
3558 "Set number of top priorities for this category.
3559 See `todos-set-top-priorities' for more details."
3560 (interactive)
3561 (todos-set-top-priorities t))
3562
3563 (defun todos-top-priorities (&optional num)
3564 "List top priorities of each category in `todos-filter-files'.
3565 Number of entries for each category is given by NUM, which
3566 defaults to `todos-show-priorities'."
3567 (interactive "P")
3568 (let ((arg (if num (cons 'top num) 'top))
3569 (buf todos-top-priorities-buffer)
3570 (file todos-current-todos-file))
3571 (todos-filter-items arg)
3572 (todos-filtered-buffer-name buf file)))
3573
3574 (defun todos-top-priorities-multifile (&optional arg)
3575 "List top priorities of each category in `todos-filter-files'.
3576
3577 If the prefix argument ARG is a number, this is the maximum
3578 number of top priorities to list in each category. If the prefix
3579 argument is `C-u', prompt for which files to filter and use
3580 `todos-show-priorities' as the number of top priorities to list
3581 in each category. If the prefix argument is `C-uC-u', prompt
3582 both for which files to filter and for how many top priorities to
3583 list in each category."
3584 (interactive "P")
3585 (let* ((buf todos-top-priorities-buffer)
3586 files
3587 (pref (if (numberp arg)
3588 (cons 'top arg)
3589 (setq files (if (or (consp arg)
3590 (null todos-filter-files))
3591 (progn (todos-multiple-filter-files)
3592 todos-multiple-filter-files)
3593 todos-filter-files))
3594 (if (equal arg '(16))
3595 (cons 'top (read-number
3596 "Enter number of top priorities to show: "
3597 todos-show-priorities))
3598 'top))))
3599 (todos-filter-items pref t)
3600 (todos-filtered-buffer-name buf files)))
3601
3602 (defun todos-diary-items ()
3603 "Display todo items for diary inclusion in this Todos file."
3604 (interactive)
3605 (let ((buf todos-diary-items-buffer)
3606 (file todos-current-todos-file))
3607 (todos-filter-items 'diary)
3608 (todos-filtered-buffer-name buf file)))
3609
3610 (defun todos-diary-items-multifile (&optional arg)
3611 "Display todo items for diary inclusion in one or more Todos file.
3612 The files are those listed in `todos-filter-files'."
3613 (interactive "P")
3614 (let ((buf todos-diary-items-buffer)
3615 (files (if (or arg (null todos-filter-files))
3616 (progn (todos-multiple-filter-files)
3617 todos-multiple-filter-files)
3618 todos-filter-files)))
3619 (todos-filter-items 'diary t)
3620 (todos-filtered-buffer-name buf files)))
3621
3622 (defun todos-regexp-items ()
3623 "Display todo items matching a user-entered regular expression.
3624 The items are those in the current Todos file."
3625 (interactive)
3626 (let ((buf todos-regexp-items-buffer)
3627 (file todos-current-todos-file))
3628 (todos-filter-items 'regexp)
3629 (todos-filtered-buffer-name buf file)))
3630
3631 (defun todos-regexp-items-multifile (&optional arg)
3632 "Display todo items matching a user-entered regular expression.
3633 The items are those in the files listed in `todos-filter-files'."
3634 (interactive "P")
3635 (let ((buf todos-regexp-items-buffer)
3636 (files (if (or arg (null todos-filter-files))
3637 (progn (todos-multiple-filter-files)
3638 todos-multiple-filter-files)
3639 todos-filter-files)))
3640 (todos-filter-items 'regexp t)
3641 (todos-filtered-buffer-name buf files)))
3642
3643 ;; ---------------------------------------------------------------------------
3644 ;;; Editing Commands
3645
3646 (defun todos-add-file ()
3647 "Name and add a new Todos file.
3648 Interactively, prompt for a category and display it.
3649 Noninteractively, return the name of the new file."
3650 (interactive)
3651 (let ((prompt (concat "Enter name of new Todos file "
3652 "(TAB or SPC to see current names): "))
3653 file)
3654 (setq file (todos-read-file-name prompt))
3655 (with-current-buffer (get-buffer-create file)
3656 (erase-buffer)
3657 (write-region (point-min) (point-max) file nil 'nomessage nil t)
3658 (kill-buffer file))
3659 (todos-reevaluate-filelist-defcustoms)
3660 (if (called-interactively-p)
3661 (progn
3662 (set-window-buffer (selected-window)
3663 (set-buffer (find-file-noselect file)))
3664 (setq todos-current-todos-file file)
3665 (todos-show))
3666 file)))
3667
3668 ;;; Category editing commands
3669
3670 (defun todos-add-category (&optional cat)
3671 "Add a new category to the current Todos file.
3672 Called interactively, prompts for category name, then visits the
3673 category in Todos mode. Non-interactively, argument CAT provides
3674 the category name and the return value is the category number."
3675 (interactive)
3676 (let* ((buffer-read-only)
3677 (num (1+ (length todos-categories)))
3678 (counts (make-vector 4 0))) ; [todo diary done archived]
3679 ;; If cat is passed from caller, don't prompt, unless it is "",
3680 ;; which means the file was just added and has no category yet.
3681 (unless (and cat (> (length cat) 0))
3682 (setq cat (todos-read-category "Enter new category name: " nil t)))
3683 (setq todos-categories (append todos-categories (list (cons cat counts))))
3684 (widen)
3685 (goto-char (point-max))
3686 (save-excursion ; Save point for todos-category-select.
3687 (insert todos-category-beg cat "\n\n" todos-category-done "\n"))
3688 (todos-update-categories-sexp)
3689 ;; If invoked by user, display the newly added category, if called
3690 ;; programmatically return the category number to the caller.
3691 (if (called-interactively-p 'any)
3692 (progn
3693 (setq todos-category-number num)
3694 (todos-category-select))
3695 num)))
3696
3697 (defun todos-rename-category ()
3698 "Rename current Todos category.
3699 If this file has an archive containing this category, rename the
3700 category there as well."
3701 (interactive)
3702 (let* ((cat (todos-current-category))
3703 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat))))
3704 (setq new (todos-validate-name new 'category))
3705 (let* ((ofile todos-current-todos-file)
3706 (archive (concat (file-name-sans-extension ofile) ".toda"))
3707 (buffers (append (list ofile)
3708 (unless (zerop (todos-get-count 'archived cat))
3709 (list archive)))))
3710 (dolist (buf buffers)
3711 (with-current-buffer (find-file-noselect buf)
3712 (let (buffer-read-only)
3713 (setq todos-categories (todos-set-categories))
3714 (save-excursion
3715 (save-restriction
3716 (setcar (assoc cat todos-categories) new)
3717 (widen)
3718 (goto-char (point-min))
3719 (todos-update-categories-sexp)
3720 (re-search-forward (concat (regexp-quote todos-category-beg)
3721 "\\(" (regexp-quote cat) "\\)\n")
3722 nil t)
3723 (replace-match new t t nil 1)))))))
3724 (force-mode-line-update))
3725 (save-excursion (todos-category-select)))
3726
3727 (defun todos-delete-category (&optional arg)
3728 "Delete current Todos category provided it is empty.
3729 With ARG non-nil delete the category unconditionally,
3730 i.e. including all existing todo and done items."
3731 (interactive "P")
3732 (let* ((file todos-current-todos-file)
3733 (cat (todos-current-category))
3734 (todo (todos-get-count 'todo cat))
3735 (done (todos-get-count 'done cat))
3736 (archived (todos-get-count 'archived cat)))
3737 (if (and (not arg)
3738 (or (> todo 0) (> done 0)))
3739 (message "%s" (substitute-command-keys
3740 (concat "To delete a non-empty category, "
3741 "type C-u \\[todos-delete-category].")))
3742 (when (cond ((= (length todos-categories) 1)
3743 (y-or-n-p (concat "This is the only category in this file; "
3744 "deleting it will also delete the file.\n"
3745 "Do you want to proceed? ")))
3746 ((> archived 0)
3747 (y-or-n-p (concat "This category has archived items; "
3748 "the archived category will remain\n"
3749 "after deleting the todo category. "
3750 "Do you still want to delete it\n"
3751 "(see 'todos-skip-archived-categories' "
3752 "for another option)? ")))
3753 (t
3754 (y-or-n-p (concat "Permanently remove category \"" cat
3755 "\"" (and arg " and all its entries")
3756 "? "))))
3757 (widen)
3758 (let ((buffer-read-only)
3759 (beg (re-search-backward
3760 (concat "^" (regexp-quote (concat todos-category-beg cat))
3761 "\n") nil t))
3762 (end (if (re-search-forward
3763 (concat "\n\\(" (regexp-quote todos-category-beg)
3764 ".*\n\\)") nil t)
3765 (match-beginning 1)
3766 (point-max))))
3767 (remove-overlays beg end)
3768 (delete-region beg end)
3769 (if (= (length todos-categories) 1)
3770 ;; If deleted category was the only one, delete the file.
3771 (progn
3772 (todos-reevaluate-filelist-defcustoms)
3773 ;; Skip confirming killing the archive buffer if it has been
3774 ;; modified and not saved.
3775 (set-buffer-modified-p nil)
3776 (delete-file file)
3777 (kill-buffer)
3778 (message "Deleted Todos file %s." file))
3779 (setq todos-categories (delete (assoc cat todos-categories)
3780 todos-categories))
3781 (todos-update-categories-sexp)
3782 (setq todos-category-number
3783 (1+ (mod todos-category-number (length todos-categories))))
3784 (todos-category-select)
3785 (goto-char (point-min))
3786 (message "Deleted category %s." cat)))))))
3787
3788 (defun todos-move-category ()
3789 "Move current category to a different Todos file.
3790 If current category has archived items, also move those to the
3791 archive of the file moved to, creating it if it does not exist."
3792 (interactive)
3793 (when (or (> (length todos-categories) 1)
3794 (y-or-n-p (concat "This is the only category in this file; "
3795 "moving it will also delete the file.\n"
3796 "Do you want to proceed? ")))
3797 (let* ((ofile todos-current-todos-file)
3798 (cat (todos-current-category))
3799 (nfile (todos-read-file-name
3800 "Choose a Todos file to move this category to: " nil t))
3801 (archive (concat (file-name-sans-extension ofile) ".toda"))
3802 (buffers (append (list ofile)
3803 (unless (zerop (todos-get-count 'archived cat))
3804 (list archive))))
3805 new)
3806 (while (equal (file-truename nfile) (file-truename ofile))
3807 (setq nfile (todos-read-file-name
3808 "Choose a file distinct from this file: " nil t)))
3809 (dolist (buf buffers)
3810 (with-current-buffer (find-file-noselect buf)
3811 (widen)
3812 (goto-char (point-max))
3813 (let* ((beg (re-search-backward
3814 (concat "^" (regexp-quote (concat todos-category-beg cat))
3815 "$")
3816 nil t))
3817 (end (if (re-search-forward
3818 (concat "^" (regexp-quote todos-category-beg))
3819 nil t 2)
3820 (match-beginning 0)
3821 (point-max)))
3822 (content (buffer-substring-no-properties beg end))
3823 (counts (cdr (assoc cat todos-categories)))
3824 buffer-read-only)
3825 ;; Move the category to the new file. Also update or create
3826 ;; archive file if necessary.
3827 (with-current-buffer
3828 (find-file-noselect
3829 ;; Regenerate todos-archives in case there
3830 ;; is a newly created archive.
3831 (if (member buf (funcall todos-files-function t))
3832 (concat (file-name-sans-extension nfile) ".toda")
3833 nfile))
3834 (let* ((nfile-short (todos-short-file-name nfile))
3835 (prompt (concat
3836 (format "Todos file \"%s\" already has "
3837 nfile-short)
3838 (format "the category \"%s\";\n" cat)
3839 "enter a new category name: "))
3840 buffer-read-only)
3841 (widen)
3842 (goto-char (point-max))
3843 (insert content)
3844 ;; If the file moved to has a category with the same
3845 ;; name, rename the moved category.
3846 (when (assoc cat todos-categories)
3847 (unless (member (file-truename (buffer-file-name))
3848 (funcall todos-files-function t))
3849 (setq new (read-from-minibuffer prompt))
3850 (setq new (todos-validate-name new 'category))))
3851 ;; Replace old with new name in Todos and archive files.
3852 (when new
3853 (goto-char (point-max))
3854 (re-search-backward
3855 (concat "^" (regexp-quote todos-category-beg)
3856 "\\(" (regexp-quote cat) "\\)$") nil t)
3857 (replace-match new nil nil nil 1)))
3858 (setq todos-categories
3859 (append todos-categories (list (cons new counts))))
3860 (todos-update-categories-sexp)
3861 ;; If archive was just created, save it to avoid "File <xyz> no
3862 ;; longer exists!" message on invoking
3863 ;; `todos-view-archived-items'. FIXME: maybe better to save
3864 ;; unconditionally?
3865 (unless (file-exists-p (buffer-file-name))
3866 (save-buffer))
3867 (todos-category-number (or new cat))
3868 (todos-category-select))
3869 ;; Delete the category from the old file, and if that was the
3870 ;; last category, delete the file. Also handle archive file
3871 ;; if necessary.
3872 (remove-overlays beg end)
3873 (delete-region beg end)
3874 (goto-char (point-min))
3875 ;; Put point after todos-categories sexp.
3876 (forward-line)
3877 (if (eobp) ; Aside from sexp, file is empty.
3878 (progn
3879 ;; Skip confirming killing the archive buffer.
3880 (set-buffer-modified-p nil)
3881 (delete-file todos-current-todos-file)
3882 (kill-buffer)
3883 (when (member todos-current-todos-file todos-files)
3884 (todos-reevaluate-filelist-defcustoms)))
3885 (setq todos-categories (delete (assoc cat todos-categories)
3886 todos-categories))
3887 (todos-update-categories-sexp)
3888 (todos-category-select)))))
3889 (set-window-buffer (selected-window)
3890 (set-buffer (find-file-noselect nfile)))
3891 (todos-category-number (or new cat))
3892 (todos-category-select))))
3893
3894 (defun todos-merge-category ()
3895 "Merge current category into another category in this file.
3896
3897 The current category's todo and done items are appended to the
3898 chosen goal category's todo and done items, respectively. The
3899 goal category becomes the current category, and the previous
3900 current category is deleted.
3901
3902 If both the first and goal categories also have archived items,
3903 the former are merged to the latter. If only the first category
3904 has archived items, the archived category is renamed to the goal
3905 category."
3906 (interactive)
3907 (let* ((tfile todos-current-todos-file)
3908 (archive (concat (file-name-sans-extension tfile) ".toda"))
3909 (cat (todos-current-category))
3910 (goal (todos-read-category "Category to merge to: " t))
3911 archived-count here)
3912 ;; Merge in todo file.
3913 (with-current-buffer (get-buffer (find-file-noselect tfile))
3914 (widen)
3915 (let* ((buffer-read-only nil)
3916 (cbeg (progn
3917 (re-search-backward
3918 (concat "^" (regexp-quote todos-category-beg)) nil t)
3919 (point-marker)))
3920 (tbeg (progn (forward-line) (point-marker)))
3921 (dbeg (progn
3922 (re-search-forward
3923 (concat "^" (regexp-quote todos-category-done)) nil t)
3924 (forward-line) (point-marker)))
3925 ;; Omit empty line between todo and done items.
3926 (tend (progn (forward-line -2) (point-marker)))
3927 (cend (progn
3928 (if (re-search-forward
3929 (concat "^" (regexp-quote todos-category-beg)) nil t)
3930 (progn
3931 (goto-char (match-beginning 0))
3932 (point-marker))
3933 (point-max-marker))))
3934 (todo (buffer-substring-no-properties tbeg tend))
3935 (done (buffer-substring-no-properties dbeg cend)))
3936 (goto-char (point-min))
3937 ;; Merge any todo items.
3938 (unless (zerop (length todo))
3939 (re-search-forward
3940 (concat "^" (regexp-quote (concat todos-category-beg goal)) "$")
3941 nil t)
3942 (re-search-forward
3943 (concat "^" (regexp-quote todos-category-done)) nil t)
3944 (forward-line -1)
3945 (setq here (point-marker))
3946 (insert todo)
3947 (todos-update-count 'todo (todos-get-count 'todo cat) goal))
3948 ;; Merge any done items.
3949 (unless (zerop (length done))
3950 (goto-char (if (re-search-forward
3951 (concat "^" (regexp-quote todos-category-beg)) nil t)
3952 (match-beginning 0)
3953 (point-max)))
3954 (when (zerop (length todo)) (setq here (point-marker)))
3955 (insert done)
3956 (todos-update-count 'done (todos-get-count 'done cat) goal))
3957 (remove-overlays cbeg cend)
3958 (delete-region cbeg cend)
3959 (setq todos-categories (delete (assoc cat todos-categories)
3960 todos-categories))
3961 (todos-update-categories-sexp)
3962 (mapc (lambda (m) (set-marker m nil)) (list cbeg tbeg dbeg tend cend))))
3963 (when (file-exists-p archive)
3964 ;; Merge in archive file.
3965 (with-current-buffer (get-buffer (find-file-noselect archive))
3966 (widen)
3967 (goto-char (point-min))
3968 (let ((buffer-read-only nil)
3969 (cbeg (save-excursion
3970 (when (re-search-forward
3971 (concat "^" (regexp-quote
3972 (concat todos-category-beg cat)) "$")
3973 nil t)
3974 (goto-char (match-beginning 0))
3975 (point-marker))))
3976 (gbeg (save-excursion
3977 (when (re-search-forward
3978 (concat "^" (regexp-quote
3979 (concat todos-category-beg goal)) "$")
3980 nil t)
3981 (goto-char (match-beginning 0))
3982 (point-marker))))
3983 cend carch)
3984 (when cbeg
3985 (setq archived-count (todos-get-count 'done cat))
3986 (setq cend (save-excursion
3987 (if (re-search-forward
3988 (concat "^" (regexp-quote todos-category-beg))
3989 nil t)
3990 (match-beginning 0)
3991 (point-max))))
3992 (setq carch (save-excursion (goto-char cbeg) (forward-line)
3993 (buffer-substring-no-properties (point) cend)))
3994 ;; If both categories of the merge have archived items, merge the
3995 ;; source items to the goal items, else "merge" by renaming the
3996 ;; source category to goal.
3997 (if gbeg
3998 (progn
3999 (goto-char (if (re-search-forward
4000 (concat "^" (regexp-quote todos-category-beg))
4001 nil t)
4002 (match-beginning 0)
4003 (point-max)))
4004 (insert carch)
4005 (remove-overlays cbeg cend)
4006 (delete-region cbeg cend))
4007 (goto-char cbeg)
4008 (search-forward cat)
4009 (replace-match goal))
4010 (setq todos-categories (todos-make-categories-list t))
4011 (todos-update-categories-sexp)))))
4012 (with-current-buffer (get-file-buffer tfile)
4013 (when archived-count
4014 (unless (zerop archived-count)
4015 (todos-update-count 'archived archived-count goal)
4016 (todos-update-categories-sexp)))
4017 (todos-category-number goal)
4018 ;; If there are only merged done items, show them.
4019 (let ((todos-show-with-done (zerop (todos-get-count 'todo goal))))
4020 (todos-category-select)
4021 ;; Put point on the first merged item.
4022 (goto-char here)))
4023 (set-marker here nil)))
4024
4025 (defun todos-set-category-priority (&optional arg)
4026 "Change priority of category at point in Todos Categories buffer.
4027
4028 With ARG nil, prompt for the new priority number. Alternatively,
4029 the new priority can be provided by a numerical prefix ARG.
4030 Otherwise, if ARG is either of the symbols `raise' or `lower',
4031 raise or lower the category's priority by one."
4032 (interactive "P")
4033 (let ((curnum (save-excursion
4034 ;; Get the number representing the priority of the category
4035 ;; on the current line.
4036 (forward-line 0) (skip-chars-forward " ") (number-at-point))))
4037 (when curnum ; Do nothing if we're not on a category line.
4038 (let* ((maxnum (length todos-categories))
4039 (prompt (format "Set category priority (1-%d): " maxnum))
4040 (col (current-column))
4041 (buffer-read-only nil)
4042 (priority (cond ((and (eq arg 'raise) (> curnum 1))
4043 (1- curnum))
4044 ((and (eq arg 'lower) (< curnum maxnum))
4045 (1+ curnum))))
4046 candidate)
4047 (while (not priority)
4048 (setq candidate (or arg (read-number prompt)))
4049 (setq arg nil)
4050 (setq prompt
4051 (cond ((or (< candidate 1) (> candidate maxnum))
4052 (format "Priority must be an integer between 1 and %d: "
4053 maxnum))
4054 ((= candidate curnum)
4055 "Choose a different priority than the current one: ")))
4056 (unless prompt (setq priority candidate)))
4057 (let* ((lower (< curnum priority)) ; Priority is being lowered.
4058 (head (butlast todos-categories
4059 (apply (if lower 'identity '1+)
4060 (list (- maxnum priority)))))
4061 (tail (nthcdr (apply (if lower 'identity '1-) (list priority))
4062 todos-categories))
4063 ;; Category's name and items counts list.
4064 (catcons (nth (1- curnum) todos-categories))
4065 (todos-categories (nconc head (list catcons) tail))
4066 newcats)
4067 (when lower (setq todos-categories (nreverse todos-categories)))
4068 (setq todos-categories (delete-dups todos-categories))
4069 (when lower (setq todos-categories (nreverse todos-categories)))
4070 (setq newcats todos-categories)
4071 (kill-buffer)
4072 (with-current-buffer (find-buffer-visiting todos-current-todos-file)
4073 (setq todos-categories newcats)
4074 (todos-update-categories-sexp))
4075 (todos-display-categories)
4076 (forward-line (1+ priority))
4077 (forward-char col))))))
4078
4079 (defun todos-raise-category-priority ()
4080 "Raise priority of category at point in Todos Categories buffer."
4081 (interactive)
4082 (todos-set-category-priority 'raise))
4083
4084 (defun todos-lower-category-priority ()
4085 "Lower priority of category at point in Todos Categories buffer."
4086 (interactive)
4087 (todos-set-category-priority 'lower))
4088
4089 ;; ---------------------------------------------------------------------------
4090 ;;; Item editing commands
4091
4092 ;; FIXME: make insertion options customizable per category?
4093 ;;;###autoload
4094 (defun todos-insert-item (&optional arg diary nonmarking date-type time
4095 region-or-here)
4096 "Add a new Todo item to a category.
4097 \(See the note at the end of this document string about key
4098 bindings and convenience commands derived from this command.)
4099
4100 With no (or nil) prefix argument ARG, add the item to the current
4101 category; with one prefix argument (C-u), prompt for a category
4102 from the current Todos file; with two prefix arguments (C-u C-u),
4103 first prompt for a Todos file, then a category in that file. If
4104 a non-existing category is entered, ask whether to add it to the
4105 Todos file; if answered affirmatively, add the category and
4106 insert the item there.
4107
4108 When argument DIARY is non-nil, this overrides the intent of the
4109 user option `todos-include-in-diary' for this item: if
4110 `todos-include-in-diary' is nil, include the item in the Fancy
4111 Diary display, and if it is non-nil, exclude the item from the
4112 Fancy Diary display. When DIARY is nil, `todos-include-in-diary'
4113 has its intended effect.
4114
4115 When the item is included in the Fancy Diary display and the
4116 argument NONMARKING is non-nil, this overrides the intent of the
4117 user option `todos-diary-nonmarking' for this item: if
4118 `todos-diary-nonmarking' is nil, append `diary-nonmarking-symbol'
4119 to the item, and if it is non-nil, omit `diary-nonmarking-symbol'.
4120
4121 The argument DATE-TYPE determines the content of the item's
4122 mandatory date header string and how it is added:
4123 - If DATE-TYPE is the symbol `calendar', the Calendar pops up and
4124 when the user puts the cursor on a date and hits RET, that
4125 date, in the format set by `calendar-date-display-form',
4126 becomes the date in the header.
4127 - If DATE-TYPE is a string matching the regexp
4128 `todos-date-pattern', that string becomes the date in the
4129 header. This case is for the command
4130 `todos-insert-item-from-calendar' which is called from the
4131 Calendar.
4132 - If DATE-TYPE is the symbol `date', the header contains the date
4133 in the format set by `calendar-date-display-form', with year,
4134 month and day individually prompted for (month with tab
4135 completion).
4136 - If DATE-TYPE is the symbol `dayname' the header contains a
4137 weekday name instead of a date, prompted for with tab
4138 completion.
4139 - If DATE-TYPE has any other value (including nil or none) the
4140 header contains the current date (in the format set by
4141 `calendar-date-display-form').
4142
4143 With non-nil argument TIME prompt for a time string, which must
4144 match `diary-time-regexp'. Typing `<return>' at the prompt
4145 returns the current time, if the user option
4146 `todos-always-add-time-string' is non-nil, otherwise the empty
4147 string (i.e., no time string). If TIME is absent or nil, add or
4148 omit the current time string according as
4149 `todos-always-add-time-string' is non-nil or nil, respectively.
4150
4151 The argument REGION-OR-HERE determines the source and location of
4152 the new item:
4153 - If the REGION-OR-HERE is the symbol `here', prompt for the text
4154 of the new item and insert it directly above the todo item at
4155 point (hence lowering the priority of the remaining items), or
4156 if point is on the empty line below the last todo item, insert
4157 the new item there. An error is signalled if
4158 `todos-insert-item' is invoked with `here' outside of the
4159 current category.
4160 - If REGION-OR-HERE is the symbol `region', use the region of the
4161 current buffer as the text of the new item, depending on the
4162 value of user option `todos-use-only-highlighted-region': if
4163 this is non-nil, then use the region only when it is
4164 highlighted; otherwise, use the region regardless of
4165 highlighting. An error is signalled if there is no region in
4166 the current buffer. Prompt for the item's priority in the
4167 category (an integer between 1 and one more than the number of
4168 items in the category), and insert the item accordingly.
4169 - If REGION-OR-HERE has any other value (in particular, nil or
4170 none), prompt for the text and the item's priority, and insert
4171 the item accordingly.
4172
4173 To facilitate using these arguments when inserting a new todo
4174 item, convenience commands have been defined for all admissible
4175 combinations together with mnenomic key bindings based on on the
4176 name of the arguments and their order in the command's argument
4177 list: diar_y_ - nonmar_k_ing - _c_alendar or _d_ate or day_n_ame
4178 - _t_ime - _r_egion or _h_ere. These key combinations are
4179 appended to the basic insertion key (i) and keys that allow a
4180 following key must be doubled when used finally. For example,
4181 `iyh' will insert a new item with today's date, marked according
4182 to the DIARY argument described above, and with priority
4183 according to the HERE argument; while `iyy' does the same except
4184 the priority is not given by HERE but by prompting."
4185 ;; An alternative interface for customizing key
4186 ;; binding is also provided with the function
4187 ;; `todos-insertion-bindings'." ;FIXME
4188 (interactive "P")
4189 (let ((region (eq region-or-here 'region))
4190 (here (eq region-or-here 'here)))
4191 (when region
4192 (let (use-empty-active-region)
4193 (unless (and todos-use-only-highlighted-region (use-region-p))
4194 (error "There is no active region"))))
4195 (let* ((buf (current-buffer))
4196 (new-item (if region
4197 (buffer-substring-no-properties
4198 (region-beginning) (region-end))
4199 (read-from-minibuffer "Todo item: ")))
4200 (date-string (cond
4201 ((eq date-type 'date)
4202 (todos-read-date))
4203 ((eq date-type 'dayname)
4204 (todos-read-dayname))
4205 ((eq date-type 'calendar)
4206 (setq todos-date-from-calendar t)
4207 (todos-set-date-from-calendar))
4208 ((and (stringp date-type)
4209 (string-match todos-date-pattern date-type))
4210 (setq todos-date-from-calendar date-type)
4211 (todos-set-date-from-calendar))
4212 (t (calendar-date-string (calendar-current-date) t t))))
4213 (time-string (or (and time (todos-read-time))
4214 (and todos-always-add-time-string
4215 (substring (current-time-string) 11 16)))))
4216 (setq todos-date-from-calendar nil)
4217 (cond ((equal arg '(16))
4218 (todos-jump-to-category nil t)
4219 (set-window-buffer
4220 (selected-window)
4221 (set-buffer (find-buffer-visiting todos-global-current-todos-file))))
4222 ((equal arg '(4))
4223 (todos-jump-to-category)
4224 (set-window-buffer
4225 (selected-window)
4226 (set-buffer (find-buffer-visiting todos-global-current-todos-file))))
4227 (t
4228 (when (not (derived-mode-p 'todos-mode)) (todos-show))))
4229 (let (buffer-read-only)
4230 (setq new-item
4231 ;; Add date, time and diary marking as required.
4232 (concat (if (not (and diary (not todos-include-in-diary)))
4233 todos-nondiary-start
4234 (when (and nonmarking (not todos-diary-nonmarking))
4235 diary-nonmarking-symbol))
4236 date-string (when (and time-string ; Can be empty string.
4237 (not (zerop (length time-string))))
4238 (concat " " time-string))
4239 (when (not (and diary (not todos-include-in-diary)))
4240 todos-nondiary-end)
4241 " " new-item))
4242 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4243 (setq new-item (replace-regexp-in-string
4244 "\\(\n\\)[^[:blank:]]"
4245 (concat "\n" (make-string todos-indent-to-here 32))
4246 new-item nil nil 1))
4247 ;; FIXME: after jumping to another category due to `C-u i h',
4248 ;; item is inserted as first item -- ok?
4249 (if here
4250 (cond ((not (eq major-mode 'todos-mode))
4251 (error "Cannot insert a todo item here outside of Todos mode"))
4252 ((not (eq buf (current-buffer)))
4253 (error "Cannot insert an item here after changing buffer"))
4254 ((or (todos-done-item-p)
4255 ;; Point on last blank line.
4256 (save-excursion (forward-line -1) (todos-done-item-p)))
4257 (error "Cannot insert a new item in the done item section"))
4258 (t
4259 (todos-insert-with-overlays new-item)))
4260 (todos-set-item-priority new-item (todos-current-category) t))
4261 (todos-update-count 'todo 1)
4262 (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
4263 (todos-update-categories-sexp)))))
4264
4265 (defun todos-copy-item ()
4266 "Copy item at point and insert the copy as a new item."
4267 (interactive)
4268 (unless (or (todos-done-item-p) (looking-at "^$"))
4269 (let ((copy (todos-item-string))
4270 (diary-item (todos-diary-item-p)))
4271 (todos-set-item-priority copy (todos-current-category) t)
4272 (todos-update-count 'todo 1)
4273 (when diary-item (todos-update-count 'diary 1))
4274 (todos-update-categories-sexp))))
4275
4276 (defvar todos-date-from-calendar nil
4277 "Helper variable for setting item date from the Emacs Calendar.")
4278
4279 (defun todos-set-date-from-calendar ()
4280 "Return string of date chosen from Calendar."
4281 (cond ((and (stringp todos-date-from-calendar)
4282 (string-match todos-date-pattern todos-date-from-calendar))
4283 todos-date-from-calendar)
4284 (todos-date-from-calendar
4285 (let (calendar-view-diary-initially-flag)
4286 (calendar))
4287 ;; *Calendar* is now current buffer.
4288 (local-set-key (kbd "RET") 'exit-recursive-edit)
4289 (message "Put cursor on a date and type <return> to set it.")
4290 ;; FIXME: is there a better way than recursive-edit?
4291 (recursive-edit)
4292 (setq todos-date-from-calendar
4293 (calendar-date-string (calendar-cursor-to-date t) t t))
4294 (calendar-exit)
4295 todos-date-from-calendar)))
4296
4297 (defun todos-delete-item ()
4298 "Delete at least one item in this category.
4299
4300 If there are marked items, delete all of these; otherwise, delete
4301 the item at point."
4302 (interactive)
4303 (let (ov)
4304 (unwind-protect
4305 (let* ((cat (todos-current-category))
4306 (marked (assoc cat todos-categories-with-marks))
4307 (item (unless marked (todos-item-string)))
4308 ;; FIXME: make confirmation an option?
4309 (answer (if marked
4310 (y-or-n-p "Permanently delete all marked items? ")
4311 (when item
4312 (setq ov (make-overlay
4313 (save-excursion (todos-item-start))
4314 (save-excursion (todos-item-end))))
4315 (overlay-put ov 'face 'todos-search)
4316 (y-or-n-p (concat "Permanently delete this item? ")))))
4317 (opoint (point))
4318 buffer-read-only)
4319 (when answer
4320 (and marked (goto-char (point-min)))
4321 (catch 'done
4322 (while (not (eobp))
4323 (if (or (and marked (todos-marked-item-p)) item)
4324 (progn
4325 (if (todos-done-item-p)
4326 (todos-update-count 'done -1)
4327 (todos-update-count 'todo -1 cat)
4328 (and (todos-diary-item-p) (todos-update-count 'diary -1)))
4329 (if ov (delete-overlay ov))
4330 (todos-remove-item)
4331 ;; Don't leave point below last item.
4332 (and item (bolp) (eolp) (< (point-min) (point-max))
4333 (todos-backward-item))
4334 (when item
4335 (throw 'done (setq item nil))))
4336 (todos-forward-item))))
4337 (when marked
4338 (remove-overlays (point-min) (point-max)
4339 'before-string todos-item-mark)
4340 (setq todos-categories-with-marks
4341 (assq-delete-all cat todos-categories-with-marks))
4342 (goto-char opoint))
4343 (todos-update-categories-sexp)
4344 (todos-prefix-overlays)))
4345 (if ov (delete-overlay ov)))))
4346
4347 (defun todos-edit-item ()
4348 "Edit the Todo item at point.
4349 If the item consists of only one logical line, edit it in the
4350 minibuffer; otherwise, edit it in Todos Edit mode."
4351 (interactive)
4352 (when (todos-item-string)
4353 (let* ((buffer-read-only)
4354 (start (todos-item-start))
4355 (item-beg (progn
4356 (re-search-forward
4357 (concat todos-date-string-start todos-date-pattern
4358 "\\( " diary-time-regexp "\\)?"
4359 (regexp-quote todos-nondiary-end) "?")
4360 (line-end-position) t)
4361 (1+ (- (point) start))))
4362 (item (todos-item-string))
4363 (multiline (> (length (split-string item "\n")) 1))
4364 (opoint (point)))
4365 (if multiline
4366 (todos-edit-multiline t)
4367 (let ((new (read-string "Edit: " (cons item item-beg))))
4368 (while (not (string-match
4369 (concat todos-date-string-start todos-date-pattern) new))
4370 (setq new (read-from-minibuffer
4371 "Item must start with a date: " new)))
4372 ;; Indent newlines inserted by C-q C-j if nonspace char follows.
4373 (setq new (replace-regexp-in-string
4374 "\\(\n\\)[^[:blank:]]"
4375 (concat "\n" (make-string todos-indent-to-here 32)) new
4376 nil nil 1))
4377 ;; If user moved point during editing, make sure it moves back.
4378 (goto-char opoint)
4379 (todos-remove-item)
4380 (todos-insert-with-overlays new)
4381 (move-to-column item-beg))))))
4382
4383 (defun todos-edit-multiline-item ()
4384 "Edit current Todo item in Todos Edit mode.
4385 Use of newlines invokes `todos-indent' to insure compliance with
4386 the format of Diary entries."
4387 (interactive)
4388 (todos-edit-multiline t))
4389
4390 (defun todos-edit-multiline (&optional item)
4391 ""
4392 (interactive)
4393 ;; FIXME: should there be only one live Todos Edit buffer?
4394 ;; (let ((buffer-name todos-edit-buffer))
4395 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
4396 (set-window-buffer
4397 (selected-window)
4398 (set-buffer (make-indirect-buffer
4399 (file-name-nondirectory todos-current-todos-file)
4400 buffer-name)))
4401 (if item
4402 (narrow-to-region (todos-item-start) (todos-item-end))
4403 (widen))
4404 (todos-edit-mode)
4405 (message "%s" (substitute-command-keys
4406 (concat "Type \\[todos-edit-quit] to check file format "
4407 "validity and return to Todos mode.\n")))))
4408
4409 (defun todos-edit-quit ()
4410 "Return from Todos Edit mode to Todos mode.
4411
4412 If the whole file was in Todos Edit mode, check before returning
4413 whether the file is still a valid Todos file and if so, also
4414 recalculate the Todos categories sexp, in case changes were made
4415 in the number or names of categories."
4416 (interactive)
4417 ;; FIXME: Should do todos-check-format only if file was actually changed --
4418 ;; but how to tell?
4419 (when (eq (buffer-size) (- (point-max) (point-min)))
4420 (when (todos-check-format) (todos-repair-categories-sexp)))
4421 (kill-buffer)
4422 ;; In case next buffer is not the one holding todos-current-todos-file.
4423 (todos-show))
4424
4425 (defun todos-edit-item-header (&optional what)
4426 "Edit date/time header of at least one item.
4427
4428 Interactively, ask whether to edit year, month and day or day of
4429 the week, as well as time. If there are marked items, apply the
4430 changes to all of these; otherwise, edit just the item at point.
4431
4432 Non-interactively, argument WHAT specifies whether to set the
4433 date from the Calendar or to today, or whether to edit only the
4434 date or day, or only the time."
4435 (interactive)
4436 (let* ((cat (todos-current-category))
4437 (marked (assoc cat todos-categories-with-marks))
4438 (first t) ; Match only first of marked items.
4439 (todos-date-from-calendar t)
4440 ndate ntime nheader)
4441 (save-excursion
4442 (or (and marked (goto-char (point-min))) (todos-item-start))
4443 (catch 'stop
4444 (while (not (eobp))
4445 (and marked
4446 (while (not (todos-marked-item-p))
4447 (todos-forward-item)
4448 (and (eobp) (throw 'stop nil))))
4449 (re-search-forward (concat todos-date-string-start "\\(?1:"
4450 todos-date-pattern
4451 "\\)\\(?2: " diary-time-regexp "\\)?")
4452 (line-end-position) t)
4453 (let* ((odate (match-string-no-properties 1))
4454 (otime (match-string-no-properties 2))
4455 (buffer-read-only))
4456 (cond ((eq what 'today)
4457 (progn
4458 (setq ndate (calendar-date-string
4459 (calendar-current-date) t t))
4460 (replace-match ndate nil nil nil 1)))
4461 ((eq what 'calendar)
4462 (setq ndate (save-match-data (todos-set-date-from-calendar)))
4463 (replace-match ndate nil nil nil 1))
4464 (t
4465 (unless (eq what 'timeonly)
4466 (when first
4467 (setq ndate (if (save-match-data
4468 (string-match "[0-9]+" odate))
4469 (if (y-or-n-p "Change date? ")
4470 (todos-read-date)
4471 (todos-read-dayname))
4472 (if (y-or-n-p "Change day? ")
4473 (todos-read-dayname)
4474 (todos-read-date)))))
4475 (replace-match ndate nil nil nil 1))
4476 (unless (eq what 'dateonly)
4477 (when first
4478 (setq ntime (save-match-data (todos-read-time)))
4479 (when (< 0 (length ntime))
4480 (setq ntime (concat " " ntime))))
4481 (if otime
4482 (replace-match ntime nil nil nil 2)
4483 (goto-char (match-end 1))
4484 (insert ntime)))))
4485 (setq todos-date-from-calendar nil)
4486 (setq first nil))
4487 (if marked
4488 (todos-forward-item)
4489 (goto-char (point-max))))))))
4490
4491 (defun todos-edit-item-date ()
4492 "Prompt for and apply changes to current item's date."
4493 (interactive)
4494 (todos-edit-item-header 'dateonly))
4495
4496 (defun todos-edit-item-date-from-calendar ()
4497 "Prompt for changes to current item's date and apply from Calendar."
4498 (interactive)
4499 (todos-edit-item-header 'calendar))
4500
4501 (defun todos-edit-item-date-is-today ()
4502 "Set item date to today's date."
4503 (interactive)
4504 (todos-edit-item-header 'today))
4505
4506 (defun todos-edit-item-time ()
4507 "Prompt For and apply changes to current item's time."
4508 (interactive)
4509 (todos-edit-item-header 'timeonly))
4510
4511 (defun todos-edit-item-diary-inclusion ()
4512 "Change diary status of one or more todo items in this category.
4513 That is, insert `todos-nondiary-marker' if the candidate items
4514 lack this marking; otherwise, remove it.
4515
4516 If there are marked todo items, change the diary status of all
4517 and only these, otherwise change the diary status of the item at
4518 point."
4519 (interactive)
4520 (let ((buffer-read-only)
4521 (marked (assoc (todos-current-category)
4522 todos-categories-with-marks)))
4523 (catch 'stop
4524 (save-excursion
4525 (when marked (goto-char (point-min)))
4526 (while (not (eobp))
4527 (if (todos-done-item-p)
4528 (throw 'stop (message "Done items cannot be edited"))
4529 (unless (and marked (not (todos-marked-item-p)))
4530 (let* ((beg (todos-item-start))
4531 (lim (save-excursion (todos-item-end)))
4532 (end (save-excursion
4533 (or (todos-time-string-matcher lim)
4534 (todos-date-string-matcher lim)))))
4535 (if (looking-at (regexp-quote todos-nondiary-start))
4536 (progn
4537 (replace-match "")
4538 (search-forward todos-nondiary-end (1+ end) t)
4539 (replace-match "")
4540 (todos-update-count 'diary 1))
4541 (when end
4542 (insert todos-nondiary-start)
4543 (goto-char (1+ end))
4544 (insert todos-nondiary-end)
4545 (todos-update-count 'diary -1)))))
4546 (unless marked (throw 'stop nil))
4547 (todos-forward-item)))))
4548 (todos-update-categories-sexp)))
4549
4550 (defun todos-edit-category-diary-inclusion (arg)
4551 "Make all items in this category diary items.
4552 With prefix ARG, make all items in this category non-diary
4553 items."
4554 (interactive "P")
4555 (save-excursion
4556 (goto-char (point-min))
4557 (let ((todo-count (todos-get-count 'todo))
4558 (diary-count (todos-get-count 'diary))
4559 (buffer-read-only))
4560 (catch 'stop
4561 (while (not (eobp))
4562 (if (todos-done-item-p) ; We've gone too far.
4563 (throw 'stop nil)
4564 (let* ((beg (todos-item-start))
4565 (lim (save-excursion (todos-item-end)))
4566 (end (save-excursion
4567 (or (todos-time-string-matcher lim)
4568 (todos-date-string-matcher lim)))))
4569 (if arg
4570 (unless (looking-at (regexp-quote todos-nondiary-start))
4571 (insert todos-nondiary-start)
4572 (goto-char (1+ end))
4573 (insert todos-nondiary-end))
4574 (when (looking-at (regexp-quote todos-nondiary-start))
4575 (replace-match "")
4576 (search-forward todos-nondiary-end (1+ end) t)
4577 (replace-match "")))))
4578 (todos-forward-item))
4579 (unless (if arg (zerop diary-count) (= diary-count todo-count))
4580 (todos-update-count 'diary (if arg
4581 (- diary-count)
4582 (- todo-count diary-count))))
4583 (todos-update-categories-sexp)))))
4584
4585 (defun todos-edit-item-diary-nonmarking ()
4586 "Change non-marking of one or more diary items in this category.
4587 That is, insert `diary-nonmarking-symbol' if the candidate items
4588 lack this marking; otherwise, remove it.
4589
4590 If there are marked todo items, change the non-marking status of
4591 all and only these, otherwise change the non-marking status of
4592 the item at point."
4593 (interactive)
4594 (let ((buffer-read-only)
4595 (marked (assoc (todos-current-category)
4596 todos-categories-with-marks)))
4597 (catch 'stop
4598 (save-excursion
4599 (when marked (goto-char (point-min)))
4600 (while (not (eobp))
4601 (if (todos-done-item-p)
4602 (throw 'stop (message "Done items cannot be edited"))
4603 (unless (and marked (not (todos-marked-item-p)))
4604 (todos-item-start)
4605 (unless (looking-at (regexp-quote todos-nondiary-start))
4606 (if (looking-at (regexp-quote diary-nonmarking-symbol))
4607 (replace-match "")
4608 (insert diary-nonmarking-symbol))))
4609 (unless marked (throw 'stop nil))
4610 (todos-forward-item)))))))
4611
4612 (defun todos-edit-category-diary-nonmarking (arg)
4613 "Add `diary-nonmarking-symbol' to all diary items in this category.
4614 With prefix ARG, remove `diary-nonmarking-symbol' from all diary
4615 items in this category."
4616 (interactive "P")
4617 (save-excursion
4618 (goto-char (point-min))
4619 (let (buffer-read-only)
4620 (catch 'stop
4621 (while (not (eobp))
4622 (if (todos-done-item-p) ; We've gone too far.
4623 (throw 'stop nil)
4624 (unless (looking-at (regexp-quote todos-nondiary-start))
4625 (if arg
4626 (when (looking-at (regexp-quote diary-nonmarking-symbol))
4627 (replace-match ""))
4628 (unless (looking-at (regexp-quote diary-nonmarking-symbol))
4629 (insert diary-nonmarking-symbol))))
4630 (todos-forward-item)))))))
4631
4632 ;; FIXME: Make NOP if point isn't on a todo item (cf. todos-copy-item,
4633 ;; todos-move-item
4634 (defun todos-set-item-priority (&optional item cat new arg)
4635 "Set todo ITEM's priority in CATegory and move item accordingly.
4636
4637 Interactively, ITEM defaults to the item at point, CAT to the
4638 current category in Todos mode, and the priority is a number
4639 between 1 and the number of items in the category.
4640 Non-interactively, non-nil NEW means ITEM is a new item and the
4641 lowest priority is one more than the number of items in CAT.
4642
4643 The new priority is set either interactively by prompt or by a
4644 numerical prefix argument, or noninteractively by argument ARG,
4645 whose value can be either of the symbols `raise' or `lower',
4646 meaning to raise or lower the item's priority by one."
4647 (interactive)
4648 (let* ((item (or item (todos-item-string)))
4649 (marked (todos-marked-item-p))
4650 (cat (or cat (cond ((eq major-mode 'todos-mode)
4651 (todos-current-category))
4652 ((eq major-mode 'todos-filtered-items-mode)
4653 (let* ((regexp1
4654 (concat todos-date-string-start
4655 todos-date-pattern
4656 "\\( " diary-time-regexp "\\)?"
4657 (regexp-quote todos-nondiary-end)
4658 "?\\(?1: \\[\\(.+:\\)?.+\\]\\)")))
4659 (save-excursion
4660 (re-search-forward regexp1 nil t)
4661 (match-string-no-properties 1)))))))
4662 curnum
4663 (todo (cond ((or (eq arg 'raise) (eq arg 'lower)
4664 (eq major-mode 'todos-filtered-items-mode))
4665 (save-excursion
4666 (let ((curstart (todos-item-start))
4667 (count 0))
4668 (goto-char (point-min))
4669 (while (looking-at todos-item-start)
4670 (setq count (1+ count))
4671 (when (= (point) curstart) (setq curnum count))
4672 (todos-forward-item))
4673 count)))
4674 ((eq major-mode 'todos-mode)
4675 (todos-get-count 'todo cat))))
4676 (maxnum (if new (1+ todo) todo))
4677 (prompt (format "Set item priority (1-%d): " maxnum))
4678 (priority (cond ((numberp current-prefix-arg)
4679 current-prefix-arg)
4680 ((and (eq arg 'raise) (>= curnum 1))
4681 (1- curnum))
4682 ((and (eq arg 'lower) (<= curnum maxnum))
4683 (1+ curnum))))
4684 candidate
4685 buffer-read-only)
4686 (unless (and priority
4687 (or (and (eq arg 'raise) (zerop priority))
4688 (and (eq arg 'lower) (> priority maxnum))))
4689 ;; When moving item to another category, show the category before
4690 ;; prompting for its priority.
4691 (unless (or arg (called-interactively-p t))
4692 (todos-category-number cat)
4693 (todos-category-select))
4694 ;; Prompt for priority only when the category has at least one todo item.
4695 (when (> maxnum 1)
4696 (while (not priority)
4697 (setq candidate (read-number prompt))
4698 (setq prompt (when (or (< candidate 1) (> candidate maxnum))
4699 (format "Priority must be an integer between 1 and %d.\n"
4700 maxnum)))
4701 (unless prompt (setq priority candidate))))
4702 ;; In Top Priorities buffer, an item's priority can be changed
4703 ;; wrt items in another category, but not wrt items in the same
4704 ;; category.
4705 (when (eq major-mode 'todos-filtered-items-mode)
4706 (let* ((regexp2 (concat todos-date-string-start todos-date-pattern
4707 "\\( " diary-time-regexp "\\)?"
4708 (regexp-quote todos-nondiary-end)
4709 "?\\(?1:" (regexp-quote cat) "\\)"))
4710 (end (cond ((< curnum priority)
4711 (save-excursion (todos-item-end)))
4712 ((> curnum priority)
4713 (save-excursion (todos-item-start)))))
4714 (match (save-excursion
4715 (cond ((< curnum priority)
4716 (todos-forward-item (1+ (- priority curnum)))
4717 (when (re-search-backward regexp2 end t)
4718 (match-string-no-properties 1)))
4719 ((> curnum priority)
4720 (todos-backward-item (- curnum priority))
4721 (when (re-search-forward regexp2 end t)
4722 (match-string-no-properties 1)))))))
4723 (when match
4724 (error (concat "Cannot reprioritize items from the same "
4725 "category in this mode, only in Todos mode")))))
4726 ;; Interactively or with non-nil ARG, relocate the item within its
4727 ;; category.
4728 (when (or arg (called-interactively-p))
4729 (todos-remove-item))
4730 (goto-char (point-min))
4731 (when priority
4732 (unless (= priority 1)
4733 (todos-forward-item (1- priority))))
4734 (todos-insert-with-overlays item)
4735 ;; If item was marked, restore the mark.
4736 (and marked (overlay-put (make-overlay (point) (point))
4737 'before-string todos-item-mark)))))
4738
4739 (defun todos-raise-item-priority ()
4740 "Raise priority of current item by moving it up by one item."
4741 (interactive)
4742 (todos-set-item-priority nil nil nil 'raise))
4743
4744 (defun todos-lower-item-priority ()
4745 "Lower priority of current item by moving it down by one item."
4746 (interactive)
4747 (todos-set-item-priority nil nil nil 'lower))
4748
4749 (defun todos-move-item (&optional file)
4750 "Move at least one todo item to another category.
4751
4752 If there are marked items, move all of these; otherwise, move
4753 the item at point.
4754
4755 With non-nil argument FILE, first prompt for another Todos file and
4756 then a category in that file to move the item or items to.
4757
4758 If the chosen category is not one of the existing categories,
4759 then it is created and the item(s) become(s) the first
4760 entry/entries in that category."
4761 (interactive)
4762 (let* ((cat1 (todos-current-category))
4763 (marked (assoc cat1 todos-categories-with-marks)))
4764 (unless (or (todos-done-item-p)
4765 ;; Point is between todo and done items.
4766 (and (looking-at "^$") (not marked)))
4767 (let* ((buffer-read-only)
4768 (file1 todos-current-todos-file)
4769 (num todos-category-number)
4770 (item (todos-item-string))
4771 (diary-item (todos-diary-item-p))
4772 (omark (save-excursion (todos-item-start) (point-marker)))
4773 (file2 (if file
4774 (todos-read-file-name "Choose a Todos file: " nil t)
4775 file1))
4776 (count 0)
4777 (count-diary 0)
4778 ov cat2 moved nmark)
4779 (set-buffer (find-file-noselect file2))
4780 (unwind-protect
4781 (progn
4782 (unless marked
4783 (setq ov (make-overlay (save-excursion (todos-item-start))
4784 (save-excursion (todos-item-end))))
4785 (overlay-put ov 'face 'todos-search))
4786 (setq cat2 (let* ((pl (if (and marked (> (cdr marked) 1)) "s" ""))
4787 (name (todos-read-category
4788 (concat "Move item" pl " to category: ")))
4789 (prompt (concat "Choose a different category than "
4790 "the current one\n(type `"
4791 (key-description
4792 (car (where-is-internal
4793 'todos-set-item-priority)))
4794 "' to reprioritize item "
4795 "within the same category): ")))
4796 (while (equal name cat1)
4797 (setq name (todos-read-category prompt)))
4798 name)))
4799 (if ov (delete-overlay ov)))
4800 (set-buffer (find-buffer-visiting file1))
4801 (if marked
4802 (progn
4803 (setq item nil)
4804 (goto-char (point-min))
4805 (while (not (eobp))
4806 (when (todos-marked-item-p)
4807 (setq item (concat item (todos-item-string) "\n"))
4808 (setq count (1+ count))
4809 (when (todos-diary-item-p)
4810 (setq count-diary (1+ count-diary))))
4811 (todos-forward-item))
4812 ;; Chop off last newline.
4813 (setq item (substring item 0 -1)))
4814 (setq count 1)
4815 (when (todos-diary-item-p) (setq count-diary 1)))
4816 (set-window-buffer (selected-window)
4817 (set-buffer (find-file-noselect file2)))
4818 (unwind-protect
4819 (progn
4820 (todos-set-item-priority item cat2 t)
4821 (setq moved t))
4822 (cond
4823 ;; Move succeeded, so remove item from starting category,
4824 ;; update item counts and display the category containing
4825 ;; the moved item.
4826 (moved
4827 (setq nmark (point-marker))
4828 (todos-update-count 'todo count)
4829 (todos-update-count 'diary count-diary)
4830 (todos-update-categories-sexp)
4831 (with-current-buffer (find-buffer-visiting file1)
4832 (save-excursion
4833 (save-restriction
4834 (widen)
4835 (goto-char omark)
4836 (if marked
4837 (let (beg end)
4838 (setq item nil)
4839 (re-search-backward
4840 (concat "^" (regexp-quote todos-category-beg)) nil t)
4841 (forward-line)
4842 (setq beg (point))
4843 (re-search-forward
4844 (concat "^" (regexp-quote todos-category-done)) nil t)
4845 (setq end (match-beginning 0))
4846 (goto-char beg)
4847 (while (< (point) end)
4848 (if (todos-marked-item-p)
4849 (todos-remove-item)
4850 (todos-forward-item)))
4851 ;; FIXME: does this work?
4852 (remove-overlays (point-min) (point-max)
4853 'before-string todos-item-mark)
4854 (setq todos-categories-with-marks
4855 (assq-delete-all cat1 todos-categories-with-marks)))
4856 (if ov (delete-overlay ov))
4857 (todos-remove-item))))
4858 (todos-update-count 'todo (- count) cat1)
4859 (todos-update-count 'diary (- count-diary) cat1)
4860 (todos-update-categories-sexp))
4861 (set-window-buffer (selected-window)
4862 (set-buffer (find-file-noselect file2)))
4863 (setq todos-category-number (todos-category-number cat2))
4864 (todos-category-select)
4865 (goto-char nmark)
4866 ;; If item is moved to end of category, make sure the
4867 ;; items above it are displayed in the window.
4868 (recenter))
4869 ;; User quit before moving, so return to starting category.
4870 (t
4871 (todos-category-number cat1)
4872 (todos-category-select)
4873 (goto-char omark))))))))
4874
4875 (defun todos-move-item-to-file ()
4876 "Move the current todo item to a category in another Todos file."
4877 (interactive)
4878 (todos-move-item t))
4879
4880 ;; (defun todos-move-item-to-diary ()
4881 ;; "Move one or more items in current category to the diary file.
4882 ;;
4883 ;; If there are marked items, move all of these; otherwise, move
4884 ;; the item at point."
4885 ;; (interactive)
4886 ;; ;; FIXME
4887 ;; )
4888
4889 ;; FIXME: make adding date customizable, and make this and time customization
4890 ;; overridable via double prefix arg ??
4891 (defun todos-item-done (&optional arg)
4892 "Tag at least one item in this category as done and hide it.
4893
4894 With prefix argument ARG prompt for a comment and append it to
4895 the done item; this is only possible if there are no marked
4896 items. If there are marked items, tag all of these with
4897 `todos-done-string' plus the current date and, if
4898 `todos-always-add-time-string' is non-nil, the current time;
4899 otherwise, just tag the item at point. Items tagged as done are
4900 relocated to the category's (by default hidden) done section."
4901 (interactive "P")
4902 (let* ((cat (todos-current-category))
4903 (marked (assoc cat todos-categories-with-marks)))
4904 (unless (or (todos-done-item-p)
4905 ;; Point is between todo and done items.
4906 (and (looking-at "^$") (not marked)))
4907 (let* ((date-string (calendar-date-string (calendar-current-date) t t))
4908 (time-string (if todos-always-add-time-string
4909 (concat " " (substring (current-time-string) 11 16))
4910 ""))
4911 (done-prefix (concat "[" todos-done-string date-string time-string
4912 "] "))
4913 (comment (and arg (not marked) (read-string "Enter a comment: ")))
4914 (item-count 0)
4915 (diary-count 0)
4916 item done-item
4917 (buffer-read-only))
4918 (and marked (goto-char (point-min)))
4919 (catch 'done
4920 (while (not (eobp))
4921 (if (or (not marked) (and marked (todos-marked-item-p)))
4922 (progn
4923 (setq item (todos-item-string))
4924 (setq done-item (cond (marked
4925 (concat done-item done-prefix item "\n"))
4926 (comment
4927 (concat done-prefix item " ["
4928 todos-comment-string
4929 ": " comment "]"))
4930 (t
4931 (concat done-prefix item))))
4932 (setq item-count (1+ item-count))
4933 (when (todos-diary-item-p)
4934 (setq diary-count (1+ diary-count)))
4935 (todos-remove-item)
4936 (unless marked (throw 'done nil)))
4937 (todos-forward-item))))
4938 (when marked
4939 ;; Chop off last newline of done item string.
4940 (setq done-item (substring done-item 0 -1))
4941 (remove-overlays (point-min) (point-max) 'before-string todos-item-mark)
4942 (setq todos-categories-with-marks
4943 (assq-delete-all cat todos-categories-with-marks)))
4944 (save-excursion
4945 (widen)
4946 (re-search-forward
4947 (concat "^" (regexp-quote todos-category-done)) nil t)
4948 (forward-char)
4949 (insert done-item "\n"))
4950 (todos-update-count 'todo (- item-count))
4951 (todos-update-count 'done item-count)
4952 (todos-update-count 'diary (- diary-count))
4953 (todos-update-categories-sexp)
4954 (save-excursion (todos-category-select))))))
4955
4956 (defun todos-done-item-add-edit-or-delete-comment (&optional arg)
4957 "Add a comment to this done item or edit an existing comment.
4958 With prefix ARG delete an existing comment."
4959 (interactive "P")
4960 (when (todos-done-item-p)
4961 (let ((item (todos-item-string))
4962 (end (save-excursion (todos-item-end)))
4963 comment buffer-read-only)
4964 (save-excursion
4965 (todos-item-start)
4966 (if (re-search-forward (concat " \\["
4967 (regexp-quote todos-comment-string)
4968 ": \\([^]]+\\)\\]") end t)
4969 (if arg
4970 (when (y-or-n-p "Delete comment? ")
4971 (delete-region (match-beginning 0) (match-end 0)))
4972 (setq comment (read-string "Edit comment: "
4973 (cons (match-string 1) 1)))
4974 (replace-match comment nil nil nil 1))
4975 (setq comment (read-string "Enter a comment: "))
4976 (todos-item-end)
4977 (insert " [" todos-comment-string ": " comment "]"))))))
4978
4979 (defun todos-item-undo ()
4980 "Restore this done item to the todo section of this category.
4981 If done item has a comment, ask whether to omit the comment from
4982 the restored item."
4983 (interactive)
4984 (let* ((cat (todos-current-category))
4985 (marked (assoc cat todos-categories-with-marks)))
4986 (when (or marked (todos-done-item-p))
4987 (let ((buffer-read-only)
4988 (bufmod (buffer-modified-p))
4989 (opoint (point))
4990 (orig-mrk (progn (todos-item-start) (point-marker)))
4991 (orig-item (todos-item-string))
4992 (first 'first)
4993 (item-count 0)
4994 (diary-count 0)
4995 start end item undone)
4996 (and marked (goto-char (point-min)))
4997 (catch 'done
4998 (while (not (eobp))
4999 (if (or (not marked) (and marked (todos-marked-item-p)))
5000 (if (not (todos-done-item-p))
5001 (error "Only done items can be undone")
5002 (todos-item-start)
5003 ;; Find the end of the date string added upon tagging item as
5004 ;; done.
5005 (setq start (search-forward "] "))
5006 (setq item-count (1+ item-count))
5007 (unless (looking-at (regexp-quote todos-nondiary-start))
5008 (setq diary-count (1+ diary-count)))
5009 (setq end (save-excursion (todos-item-end)))
5010 ;; Ask (once) whether to omit done item's comment. If
5011 ;; affirmed, omit subsequent comments without asking.
5012 (when (re-search-forward
5013 (concat " \\[" (regexp-quote todos-comment-string)
5014 ": [^]]+\\]") end t)
5015 (if (eq first 'first)
5016 (setq first
5017 (if (eq todos-undo-item-omit-comment 'ask)
5018 (when (y-or-n-p
5019 "Omit comment from restored item? ")
5020 'omit)
5021 (when todos-undo-item-omit-comment 'omit)))
5022 t)
5023 (when (eq first 'omit)
5024 (delete-region (match-beginning 0) (match-end 0))
5025 (setq end (point))))
5026 (setq item (concat item
5027 (buffer-substring-no-properties start end)
5028 (when marked "\n")))
5029 (todos-remove-item)
5030 (unless marked (throw 'done nil)))
5031 (todos-forward-item))))
5032 (if marked
5033 (progn
5034 ;; (remove-overlays (point-min) (point-max)
5035 ;; 'before-string todos-item-mark)
5036 (setq todos-categories-with-marks
5037 (assq-delete-all cat todos-categories-with-marks))
5038 ;; Insert undone items that were marked at end of todo item list.
5039 (goto-char (point-min))
5040 (re-search-forward (concat "^" (regexp-quote todos-category-done))
5041 nil t)
5042 (forward-line -1)
5043 (insert item)
5044 (todos-update-count 'todo item-count)
5045 (todos-update-count 'done (- item-count))
5046 (when diary-count (todos-update-count 'diary diary-count))
5047 (todos-update-categories-sexp)
5048 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5049 (todos-category-select)))
5050 ;; With an unmarked undone item, prompt for its priority. If user
5051 ;; cancels before setting new priority, then leave the done item
5052 ;; unchanged.
5053 (unwind-protect
5054 (progn
5055 (todos-set-item-priority item (todos-current-category) t)
5056 (setq undone t)
5057 (todos-update-count 'todo 1)
5058 (todos-update-count 'done -1)
5059 (and (todos-diary-item-p) (todos-update-count 'diary 1))
5060 (todos-update-categories-sexp)
5061 (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
5062 (todos-category-select)))
5063 (unless undone
5064 (let ((todos-show-with-done t))
5065 (widen)
5066 (goto-char orig-mrk)
5067 (todos-insert-with-overlays orig-item)
5068 (set-buffer-modified-p bufmod)
5069 (todos-category-select))
5070 (goto-char opoint))))
5071 (set-marker orig-mrk nil)))))
5072
5073 (defun todos-archive-done-item (&optional all)
5074 "Archive at least one done item in this category.
5075
5076 If there are marked done items (and no marked todo items),
5077 archive all of these; otherwise, with non-nil argument ALL,
5078 archive all done items in this category; otherwise, archive the
5079 done item at point.
5080
5081 If the archive of this file does not exist, it is created. If
5082 this category does not exist in the archive, it is created."
5083 (interactive)
5084 (when (eq major-mode 'todos-mode)
5085 (if (and all (zerop (todos-get-count 'done)))
5086 (message "No done items in this category")
5087 (catch 'end
5088 (let* ((cat (todos-current-category))
5089 (tbuf (current-buffer))
5090 (marked (assoc cat todos-categories-with-marks))
5091 (afile (concat (file-name-sans-extension
5092 todos-current-todos-file) ".toda"))
5093 (archive (if (file-exists-p afile)
5094 (find-file-noselect afile t)
5095 (get-buffer-create afile)))
5096 (item (and (todos-done-item-p) (concat (todos-item-string) "\n")))
5097 (count 0)
5098 marked-items beg end all-done
5099 buffer-read-only)
5100 (cond
5101 (marked
5102 (save-excursion
5103 (goto-char (point-min))
5104 (while (not (eobp))
5105 (when (todos-marked-item-p)
5106 (if (not (todos-done-item-p))
5107 (throw 'end (message "Only done items can be archived"))
5108 (setq marked-items
5109 (concat marked-items (todos-item-string) "\n"))
5110 (setq count (1+ count))))
5111 (todos-forward-item))))
5112 (all
5113 (if (y-or-n-p "Archive all done items in this category? ")
5114 (save-excursion
5115 (save-restriction
5116 (goto-char (point-min))
5117 (widen)
5118 (setq beg (progn
5119 (re-search-forward todos-done-string-start nil t)
5120 (match-beginning 0))
5121 end (if (re-search-forward
5122 (concat "^" (regexp-quote todos-category-beg))
5123 nil t)
5124 (match-beginning 0)
5125 (point-max))
5126 all-done (buffer-substring-no-properties beg end)
5127 count (todos-get-count 'done))))
5128 (throw 'end nil))))
5129 (when (or marked all item)
5130 (with-current-buffer archive
5131 (unless buffer-file-name (erase-buffer))
5132 (let (buffer-read-only)
5133 (widen)
5134 (goto-char (point-min))
5135 (if (and (re-search-forward
5136 (concat "^" (regexp-quote
5137 (concat todos-category-beg cat)) "$")
5138 nil t)
5139 (re-search-forward (regexp-quote todos-category-done)
5140 nil t))
5141 ;; Start of done items section in existing category.
5142 (forward-char)
5143 (todos-add-category cat)
5144 ;; Start of done items section in new category.
5145 (goto-char (point-max)))
5146 (insert (cond (marked marked-items)
5147 (all all-done)
5148 (item)))
5149 (todos-update-count 'done (if (or marked all) count 1) cat)
5150 (todos-update-categories-sexp)
5151 ;; If archive is new, save to file now (using write-region in
5152 ;; order not to get prompted for file to save to), to let
5153 ;; auto-mode-alist take effect below.
5154 (unless buffer-file-name
5155 (write-region nil nil afile)
5156 (kill-buffer))))
5157 (with-current-buffer tbuf
5158 (cond ((or marked item)
5159 (and marked (goto-char (point-min)))
5160 (catch 'done
5161 (while (not (eobp))
5162 (if (or (and marked (todos-marked-item-p)) item)
5163 (progn
5164 (todos-remove-item)
5165 (todos-update-count 'done -1)
5166 (todos-update-count 'archived 1)
5167 ;; Don't leave point below last item.
5168 (and item (bolp) (eolp) (< (point-min) (point-max))
5169 (todos-backward-item))
5170 (when item
5171 (throw 'done (setq item nil))))
5172 (todos-forward-item)))))
5173 (all
5174 (remove-overlays beg end)
5175 (delete-region beg end)
5176 (todos-update-count 'done (- count))
5177 (todos-update-count 'archived count)))
5178 (when marked
5179 (remove-overlays (point-min) (point-max)
5180 'before-string todos-item-mark)
5181 (setq todos-categories-with-marks
5182 (assq-delete-all cat todos-categories-with-marks)))
5183 (todos-update-categories-sexp)
5184 (todos-prefix-overlays)))
5185 (find-file afile)
5186 (todos-category-number cat)
5187 (todos-category-select)
5188 (split-window-below)
5189 (set-window-buffer (selected-window) tbuf))))))
5190
5191 (defun todos-archive-category-done-items ()
5192 "Move all done items in this category to its archive."
5193 (interactive)
5194 (todos-archive-done-item t))
5195
5196 (defun todos-unarchive-items (&optional all)
5197 "Unarchive at least one item in this archive category.
5198
5199 If there are marked items, unarchive all of these; otherwise,
5200 with non-nil argument ALL, unarchive all items in this category;
5201 otherwise, unarchive the item at point.
5202
5203 Unarchived items are restored as done items to the corresponding
5204 category in the Todos file, inserted at the end of done section.
5205 If all items in the archive category were restored, the category
5206 is deleted from the archive. If this was the only category in the
5207 archive, the archive file is deleted."
5208 (interactive)
5209 (when (eq major-mode 'todos-archive-mode)
5210 (catch 'end
5211 (let* ((cat (todos-current-category))
5212 (tbuf (find-file-noselect
5213 (concat (file-name-sans-extension todos-current-todos-file)
5214 ".todo") t))
5215 (marked (assoc cat todos-categories-with-marks))
5216 (item (concat (todos-item-string) "\n"))
5217 (all-items (when all (buffer-substring-no-properties
5218 (point-min) (point-max))))
5219 (all-count (when all (todos-get-count 'done)))
5220 marked-items marked-count
5221 buffer-read-only)
5222 (when marked
5223 (save-excursion
5224 (goto-char (point-min))
5225 (while (not (eobp))
5226 (when (todos-marked-item-p)
5227 (concat marked-items (todos-item-string) "\n")
5228 (setq marked-count (1+ marked-count)))
5229 (todos-forward-item))))
5230 ;; Restore items to end of category's done section and update counts.
5231 (with-current-buffer tbuf
5232 (let (buffer-read-only)
5233 (widen)
5234 (goto-char (point-min))
5235 (re-search-forward (concat "^" (regexp-quote
5236 (concat todos-category-beg cat)) "$")
5237 nil t)
5238 ;; Go to end of category's done section.
5239 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
5240 nil t)
5241 (goto-char (match-beginning 0))
5242 (goto-char (point-max)))
5243 (cond (marked
5244 (insert marked-items)
5245 (todos-update-count 'done marked-count cat)
5246 (todos-update-count 'archived (- marked-count) cat))
5247 (all
5248 (insert all-items)
5249 (todos-update-count 'done all-count cat)
5250 (todos-update-count 'archived (- all-count) cat))
5251 (t
5252 (insert item)
5253 (todos-update-count 'done 1 cat)
5254 (todos-update-count 'archived -1 cat)))
5255 (todos-update-categories-sexp)))
5256 ;; Delete restored items from archive.
5257 (cond ((or marked item)
5258 (and marked (goto-char (point-min)))
5259 (catch 'done
5260 (while (not (eobp))
5261 (if (or (and marked (todos-marked-item-p)) item)
5262 (progn
5263 (todos-remove-item)
5264 ;; Don't leave point below last item.
5265 (and item (bolp) (eolp) (< (point-min) (point-max))
5266 (todos-backward-item))
5267 (when item
5268 (throw 'done (setq item nil))))
5269 (todos-forward-item))))
5270 (todos-update-count 'done (if marked (- marked-count) -1) cat))
5271 (all
5272 (remove-overlays (point-min) (point-max))
5273 (delete-region (point-min) (point-max))))
5274 ;; If that was the last category in the archive, delete the whole file.
5275 (if (= (length todos-categories) 1)
5276 (progn
5277 (delete-file todos-current-todos-file)
5278 ;; Don't bother confirming killing the archive buffer.
5279 (set-buffer-modified-p nil)
5280 (kill-buffer))
5281 ;; Otherwise, if the archive category is now empty, delete it.
5282 (when (eq (point-min) (point-max))
5283 (widen)
5284 (let ((beg (re-search-backward
5285 (concat "^" (regexp-quote todos-category-beg) cat "$")
5286 nil t))
5287 (end (if (re-search-forward
5288 (concat "^" (regexp-quote todos-category-beg))
5289 nil t 2)
5290 (match-beginning 0)
5291 (point-max))))
5292 (remove-overlays beg end)
5293 (delete-region beg end)
5294 (setq todos-categories (delete (assoc cat todos-categories)
5295 todos-categories))
5296 (todos-update-categories-sexp))))
5297 ;; Visit category in Todos file and show restored done items.
5298 (let ((tfile (buffer-file-name tbuf))
5299 (todos-show-with-done t))
5300 (set-window-buffer (selected-window)
5301 (set-buffer (find-file-noselect tfile)))
5302 (todos-category-number cat)
5303 (todos-show)
5304 (message "Items unarchived."))))))
5305
5306 (defun todos-unarchive-category ()
5307 "Unarchive all items in this category. See `todos-unarchive-items'."
5308 (interactive)
5309 (todos-unarchive-items t))
5310
5311 (provide 'todos)
5312
5313 ;;; todos.el ends here
5314
5315 ;; FIXME: remove when part of Emacs
5316 ;; ---------------------------------------------------------------------------
5317 (add-to-list 'auto-mode-alist '("\\.todo\\'" . todos-mode))
5318 (add-to-list 'auto-mode-alist '("\\.toda\\'" . todos-archive-mode))
5319
5320 ;;; Addition to calendar.el
5321 ;; FIXME: autoload when key-binding is defined in calendar.el
5322 (defun todos-insert-item-from-calendar (&optional arg)
5323 ""
5324 (interactive "P")
5325 (setq todos-date-from-calendar
5326 (calendar-date-string (calendar-cursor-to-date t) t t))
5327 (calendar-exit)
5328 (todos-show)
5329 (todos-insert-item arg nil nil todos-date-from-calendar))
5330
5331 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar)
5332
5333 ;;; necessitated adaptations to diary-lib.el
5334
5335 ;; (defun diary-goto-entry (button)
5336 ;; "Jump to the diary entry for the BUTTON at point."
5337 ;; (let* ((locator (button-get button 'locator))
5338 ;; (marker (car locator))
5339 ;; markbuf file opoint)
5340 ;; ;; If marker pointing to diary location is valid, use that.
5341 ;; (if (and marker (setq markbuf (marker-buffer marker)))
5342 ;; (progn
5343 ;; (pop-to-buffer markbuf)
5344 ;; (goto-char (marker-position marker)))
5345 ;; ;; Marker is invalid (eg buffer has been killed, as is the case with
5346 ;; ;; included diary files).
5347 ;; (or (and (setq file (cadr locator))
5348 ;; (file-exists-p file)
5349 ;; (find-file-other-window file)
5350 ;; (progn
5351 ;; (when (eq major-mode (default-value 'major-mode)) (diary-mode))
5352 ;; (when (eq major-mode 'todos-mode) (widen))
5353 ;; (goto-char (point-min))
5354 ;; (when (re-search-forward (format "%s.*\\(%s\\)"
5355 ;; (regexp-quote (nth 2 locator))
5356 ;; (regexp-quote (nth 3 locator)))
5357 ;; nil t)
5358 ;; (goto-char (match-beginning 1))
5359 ;; (when (eq major-mode 'todos-mode)
5360 ;; (setq opoint (point))
5361 ;; (re-search-backward (concat "^"
5362 ;; (regexp-quote todos-category-beg)
5363 ;; "\\(.*\\)\n")
5364 ;; nil t)
5365 ;; (todos-category-number (match-string 1))
5366 ;; (todos-category-select)
5367 ;; (goto-char opoint)))))
5368 ;; (message "Unable to locate this diary entry")))))