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