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