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