]> code.delx.au - gnu-emacs/blob - lisp/calendar/todos.el
* calendar/todos.el: Comment out calendar require, since diary-lib
[gnu-emacs] / lisp / calendar / todos.el
1 ;;; todos.el --- major mode for editing TODO list files
2
3 ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Oliver Seidel <privat@os10000.net>
7 ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
8 ;; Created: 2 Aug 1997
9 ;; Keywords: calendar, todo
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;; ---------------------------------------------------------------------------
27
28 ;;; Commentary:
29
30 ;; Mode Description
31 ;;
32 ;; TODO is a major mode for EMACS which offers functionality to
33 ;; treat most lines in one buffer as a list of items one has to
34 ;; do. There are facilities to add new items, which are
35 ;; categorised, to edit or even delete items from the buffer.
36 ;; The buffer contents are currently compatible with the diary,
37 ;; so that the list of todos-items will show up in the FANCY diary
38 ;; mode.
39 ;;
40 ;; Notice: Besides the major mode, this file also exports the
41 ;; function `todos-show' which will change to the one specific
42 ;; TODO file that has been specified in the todos-file-do
43 ;; variable. If this file does not conform to the TODO mode
44 ;; conventions, the todos-show function will add the appropriate
45 ;; header and footer. I don't anticipate this to cause much
46 ;; grief, but be warned, in case you attempt to read a plain text
47 ;; file.
48 ;;
49 ;; Preface, Quickstart Installation
50 ;;
51 ;; To get this to work, make emacs execute the line
52 ;;
53 ;; (autoload 'todos "todos"
54 ;; "Major mode for editing TODO lists." t)
55 ;; (autoload 'todos-show "todos"
56 ;; "Show TODO items." t)
57 ;; (autoload 'todos-insert-item "todos"
58 ;; "Add TODO item." t)
59 ;;
60 ;; You may now enter new items by typing "M-x todos-insert-item",
61 ;; or enter your TODO list file by typing "M-x todos-show".
62 ;;
63 ;; The TODO list file has a special format and some auxiliary
64 ;; information, which will be added by the todos-show function if
65 ;; it attempts to visit an un-initialised file. Hence it is
66 ;; recommended to use the todos-show function for the first time,
67 ;; in order to initialise the file, but it is not necessary
68 ;; afterwards.
69 ;;
70 ;; As these commands are quite long to type, I would recommend
71 ;; the addition of two bindings to your to your global keymap. I
72 ;; personally have the following in my initialisation file:
73 ;;
74 ;; (global-set-key "\C-ct" 'todos-show) ; switch to TODO buffer
75 ;; (global-set-key "\C-ci" 'todos-insert-item) ; insert new item
76 ;;
77 ;; Note, however, that this recommendation has prompted some
78 ;; criticism, since the keys C-c LETTER are reserved for user
79 ;; functions. I believe my recommendation is acceptable, since
80 ;; the Emacs Lisp Manual *Tips* section also details that the
81 ;; mode itself should not bind any functions to those keys. The
82 ;; express aim of the above two bindings is to work outside the
83 ;; mode, which doesn't need the show function and offers a
84 ;; different binding for the insert function. They serve as
85 ;; shortcuts and are not even needed (since the TODO mode will be
86 ;; entered by visiting the TODO file, and later by switching to
87 ;; its buffer).
88 ;;
89 ;; If you are an advanced user of this package, please consult
90 ;; the whole source code for autoloads, because there are several
91 ;; extensions that are not explicitly listed in the above quick
92 ;; installation.
93 ;;
94 ;; Pre-Requisites
95 ;;
96 ;; This package will require the following packages to be
97 ;; available on the load-path:
98 ;;
99 ;; time-stamp
100 ;; easymenu
101 ;;
102 ;; Operation
103 ;;
104 ;; You will have the following facilities available:
105 ;;
106 ;; M-x todos-show will enter the todo list screen, here type
107 ;;
108 ;; + to go to next category
109 ;; - to go to previous category
110 ;; d to file the current entry, including a
111 ;; comment and timestamp
112 ;; e to edit the current entry
113 ;; E to edit a multi-line entry
114 ;; f to file the current entry, including a
115 ;; comment and timestamp
116 ;; i to insert a new entry, with prefix, omit category
117 ;; I to insert a new entry at current cursor position
118 ;; j jump to category
119 ;; k to kill the current entry
120 ;; l to lower the current entry's priority
121 ;; n for the next entry
122 ;; p for the previous entry
123 ;; P print
124 ;; q to save the list and exit the buffer
125 ;; r to raise the current entry's priority
126 ;; s to save the list
127 ;; S to save the list of top priorities
128 ;; t show top priority items for each category
129 ;;
130 ;; When you add a new entry, you are asked for the text and then
131 ;; for the category. I for example have categories for things
132 ;; that I want to do in the office (like mail my mum), that I
133 ;; want to do in town (like buy cornflakes) and things I want to
134 ;; do at home (move my suitcases). The categories can be
135 ;; selected with the cursor keys and if you type in the name of a
136 ;; category which didn't exist before, an empty category of the
137 ;; desired name will be added and filled with the new entry.
138 ;;
139 ;; Configuration
140 ;;
141 ;; Variable todos-prefix
142 ;;
143 ;; I would like to recommend that you use the prefix "*/*" (by
144 ;; leaving the variable 'todos-prefix' untouched) so that the
145 ;; diary displays each entry every day.
146 ;;
147 ;; To understand what I mean, please read the documentation that
148 ;; goes with the calendar since that will tell you how you can
149 ;; set up the fancy diary display and use the #include command to
150 ;; include your todo list file as part of your diary.
151 ;;
152 ;; If you have the diary package set up to usually display more
153 ;; than one day's entries at once, consider using
154 ;;
155 ;; "&%%(equal (calendar-current-date) date)"
156 ;;
157 ;; as the value of `todos-prefix'. Please note that this may slow
158 ;; down the processing of your diary file some.
159 ;;
160 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
161 ;;
162 ;; "&%%(todos-cp)"
163 ;;
164 ;; might be nicer and to that effect a function has been declared
165 ;; further down in the code. You may wish to auto-load this.
166 ;;
167 ;; Carsten also writes that that *changing* the prefix after the
168 ;; todo list is already established is not as simple as changing
169 ;; the variable - the todo files have to be changed by hand.
170 ;;
171 ;; Variable todos-file-do
172 ;;
173 ;; This variable is fairly self-explanatory. You have to store
174 ;; your TODO list somewhere. This variable tells the package
175 ;; where to go and find this file.
176 ;;
177 ;; Variable todos-file-done
178 ;;
179 ;; Even when you're done, you may wish to retain the entries.
180 ;; Given that they're timestamped and you are offered to add a
181 ;; comment, this can make a useful diary of past events. It will
182 ;; even blend in with the EMACS diary package. So anyway, this
183 ;; variable holds the name of the file for the filed todos-items.
184 ;;
185 ;; Variable todos-file-top
186 ;;
187 ;; File storing the top priorities of your TODO list when
188 ;; todos-save-top-priorities is non-nil. Nice to include in your
189 ;; diary instead of the complete TODO list.
190 ;;
191 ;; Variable todos-mode-hook
192 ;;
193 ;; Just like other modes, too, this mode offers to call your
194 ;; functions before it goes about its business. This variable
195 ;; will be inspected for any functions you may wish to have
196 ;; called once the other TODO mode preparations have been
197 ;; completed.
198 ;;
199 ;; Variable todos-insert-threshold
200 ;;
201 ;; Another nifty feature is the insertion accuracy. If you have
202 ;; 8 items in your TODO list, then you may get asked 4 questions
203 ;; by the binary insertion algorithm. However, you may not
204 ;; really have a need for such accurate priorities amongst your
205 ;; TODO items. If you now think about the binary insertion
206 ;; halving the size of the window each time, then the threshold
207 ;; is the window size at which it will stop. If you set the
208 ;; threshold to zero, the upper and lower bound will coincide at
209 ;; the end of the loop and you will insert your item just before
210 ;; that point. If you set the threshold to, e.g. 8, it will stop
211 ;; as soon as the window size drops below that amount and will
212 ;; insert the item in the approximate center of that window. I
213 ;; got the idea for this feature after reading a very helpful
214 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
215 ;; corrected some of my awful coding and pointed me towards some
216 ;; good reading. Thanks Trey!
217 ;;
218 ;; Things to do
219 ;;
220 ;; These originally were my ideas, but now also include all the
221 ;; suggestions that I included before forgetting them:
222 ;;
223 ;; o Fancy fonts for todo/top-priority buffer
224 ;; o Remove todos-prefix option in todos-top-priorities
225 ;; o Rename category
226 ;; o Move entry from one category to another one
227 ;; o Entries which both have the generic */* prefix and a
228 ;; "deadline" entry which are understood by diary, indicating
229 ;; an event (unless marked by &)
230 ;; o The optional COUNT variable of todos-forward-item should be
231 ;; applied to the other functions performing similar tasks
232 ;; o Modularization could be done for repeated elements of
233 ;; the code, like the completing-read lines of code.
234 ;; o license / version function
235 ;; o export to diary file
236 ;; o todos-report-bug
237 ;; o GNATS support
238 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
239 ;; outline mode)
240 ;; o rewrite complete package to store data as lisp objects
241 ;; and have display modes for display, for diary export,
242 ;; etc. (Richard Stallman pointed out this is a bad idea)
243 ;; o so base todos.el on generic-mode.el instead
244 ;;
245 ;; History and Gossip
246 ;;
247 ;; Many thanks to all the ones who have contributed to the
248 ;; evolution of this package! I hope I have listed all of you
249 ;; somewhere in the documentation or at least in the RCS history!
250 ;;
251 ;; Enjoy this package and express your gratitude by sending nice
252 ;; things to my parents' address!
253 ;;
254 ;; Oliver Seidel
255 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
256
257 ;;; Code:
258
259 ;; (require 'time-stamp)
260 ;; (require 'calendar) ; required by diary-lib
261 (require 'diary-lib)
262
263 ;; ---------------------------------------------------------------------------
264
265 ;;; Customizable options
266 (defgroup todos nil
267 "Maintain lists of todo items."
268 :link '(emacs-commentary-link "todos")
269 :version "21.1"
270 :group 'calendar)
271
272 (defcustom todos-prefix "ยง" ; "*/*" FIXME ascii default
273 "String prefixed to todo items for visual distinction."
274 :type 'string
275 :initialize 'custom-initialize-default
276 :set 'todos-reset-prefix
277 :group 'todos)
278
279 ;; "TODO mode prefix for entries.
280
281 ;; This is useful in conjunction with `calendar' and `diary' if you use
282
283 ;; #include \"~/.todos-do\"
284
285 ;; in your diary file to include your todo list file as part of your
286 ;; diary. With the default value \"*/*\" the diary displays each entry
287 ;; every day and it may also be marked on every day of the calendar.
288 ;; Using \"&%%(equal (calendar-current-date) date)\" instead will only
289 ;; show and mark todo entries for today, but may slow down processing of
290 ;; the diary file somewhat."
291 ;; :type 'string
292 ;; :group 'todos)
293
294 (defcustom todos-number-prefix t
295 "Non-nil to show item prefixes as consecutively increasing integers."
296 :type 'boolean
297 :initialize 'custom-initialize-default
298 :set 'todos-reset-prefix
299 :group 'todos)
300
301 (defcustom todos-done-separator (make-string (window-width) ?-)
302 "String used to visual separate done from not done items.
303 Displayed in a before-string overlay by `todos-toggle-view-done-items'."
304 :type 'string
305 :initialize 'custom-initialize-default
306 :set 'todos-reset-separator
307 :group 'todos)
308
309 (defcustom todos-done-string "DONE "
310 "Identifying string appended to the front of done todos items."
311 :type 'string
312 ;; :initialize 'custom-initialize-default
313 ;; :set
314 :group 'todos)
315
316 (defcustom todos-show-with-done nil
317 "Non-nil to display done items in all categories."
318 :type 'boolean
319 :group 'todos)
320
321 ;; FIXME: use user-emacs-directory here and below
322 (defcustom todos-file-do (convert-standard-filename "~/.emacs.d/.todos-do")
323 "TODO mode list file."
324 :type 'file
325 :group 'todos)
326
327 (defcustom todos-files '((convert-standard-filename "~/.emacs.d/.todos"))
328 "List of Todos files."
329 :type 'list
330 :group 'todos)
331
332 (defcustom todos-archive-file (convert-standard-filename "~/.emacs.d/.todos-archive")
333 "File of finished Todos categories."
334 :type 'file
335 :group 'todos)
336
337 ;; (defcustom todos-file-done (convert-standard-filename "~/.emacs.d/.todos-done")
338 ;; "TODO mode archive file."
339 ;; :type 'file
340 ;; :group 'todos)
341
342 (defcustom todos-mode-hook nil
343 "TODO mode hooks."
344 :type 'hook
345 :group 'todos)
346
347 (defcustom todos-edit-mode-hook nil
348 "TODO Edit mode hooks."
349 :type 'hook
350 :group 'todos)
351
352 ;; (defcustom todos-insert-threshold 0
353 ;; "TODO mode insertion accuracy.
354
355 ;; If you have 8 items in your TODO list, then you may get asked 4
356 ;; questions by the binary insertion algorithm. However, you may not
357 ;; really have a need for such accurate priorities amongst your TODO
358 ;; items. If you now think about the binary insertion halving the size
359 ;; of the window each time, then the threshold is the window size at
360 ;; which it will stop. If you set the threshold to zero, the upper and
361 ;; lower bound will coincide at the end of the loop and you will insert
362 ;; your item just before that point. If you set the threshold to,
363 ;; e.g. 8, it will stop as soon as the window size drops below that
364 ;; amount and will insert the item in the approximate center of that
365 ;; window."
366 ;; :type 'integer
367 ;; :group 'todos)
368
369 (defcustom todos-categories-buffer "*TODOS Categories*"
370 "Name of buffer displayed by `todos-display-categories'"
371 :type 'string
372 :group 'todos)
373
374 (defcustom todos-archived-categories-buffer "*TODOS Archived Categories*"
375 "Name of buffer displayed by `todos-display-categories'"
376 :type 'string
377 :group 'todos)
378
379 (defcustom todos-edit-buffer " *TODO Edit*"
380 "TODO Edit buffer name."
381 :type 'string
382 :group 'todos)
383
384 (defcustom todos-file-top (convert-standard-filename "~/.todos-top")
385 "TODO mode top priorities file.
386
387 Not in TODO format, but diary compatible.
388 Automatically generated when `todos-save-top-priorities' is non-nil."
389 :type 'string
390 :group 'todos)
391
392 (defcustom todos-include-in-diary nil
393 "Non-nil to allow new Todo items to be included in the diary."
394 :type 'boolean
395 :group 'todos)
396
397 (defcustom todos-exclusion-start "["
398 "String prepended to item date to block diary inclusion."
399 :type 'string
400 :group 'todos
401 ;; :initialize 'custom-initialize-default
402 ;; :set ; change in whole Todos file
403 )
404
405 (defcustom todos-exclusion-end "]"
406 "String appended to item date to match todos-exclusion-start."
407 :type 'string
408 :group 'todos
409 ;; :initialize 'custom-initialize-default
410 ;; :set ; change in whole Todos file
411 )
412
413 (defcustom todos-print-function 'ps-print-buffer-with-faces
414 "Function to print the current buffer."
415 :type 'symbol
416 :group 'todos)
417
418 (defcustom todos-show-priorities 1
419 "Default number of priorities to show by \\[todos-top-priorities].
420 0 means show all entries."
421 :type 'integer
422 :group 'todos)
423
424 (defcustom todos-print-priorities 0
425 "Default number of priorities to print by \\[todos-print].
426 0 means print all entries."
427 :type 'integer
428 :group 'todos)
429 ;; (defcustom todos-remove-separator t
430 ;; "Non-nil to remove category separators in\
431 ;; \\[todos-top-priorities] and \\[todos-print]."
432 ;; :type 'boolean
433 ;; :group 'todos)
434
435 (defcustom todos-save-top-priorities-too t
436 "Non-nil makes `todos-save' automatically save top-priorities in `todos-file-top'."
437 :type 'boolean
438 :group 'todos)
439
440 (defcustom todos-completion-ignore-case t ;; FIXME: nil for release
441 "Non-nil means don't consider case significant in todos-completing-read."
442 :type 'boolean
443 :group 'todos)
444
445 (defcustom todos-add-time-string t
446 "Add current time to date string inserted in front of new items."
447 :type 'boolean
448 :group 'todos)
449
450 (defcustom todos-wrap-lines t
451 "" ;FIXME
452 :group 'todos
453 :type 'boolean)
454
455 (defcustom todos-line-wrapping-function 'todos-wrap-and-indent
456 "" ;FIXME
457 :group 'todos
458 :type 'function)
459
460 ;; ---------------------------------------------------------------------------
461
462 ;;; Faces
463 (defface todos-prefix-string
464 '((t
465 :inherit font-lock-constant-face
466 ))
467 "Face for Todos prefix string."
468 :group 'todos)
469
470 (defface todos-date
471 '((t
472 :inherit diary
473 ))
474 "Face for Todos prefix string."
475 :group 'todos)
476 (defvar todos-date-face 'todos-date)
477
478 (defface todos-time
479 '((t
480 :inherit diary-time
481 ))
482 "Face for Todos prefix string."
483 :group 'todos)
484 (defvar todos-time-face 'todos-time)
485
486 (defface todos-done
487 '((t
488 :inherit font-lock-comment-face
489 ))
490 "Face for done Todos item header string."
491 :group 'todos)
492 (defvar todos-done-face 'todos-done)
493
494 (defface todos-done-sep
495 '((t
496 :inherit font-lock-type-face
497 ))
498 "Face for separator string bewteen done and not done Todos items."
499 :group 'todos)
500 (defvar todos-done-sep-face 'todos-done-sep)
501
502 (defvar todos-font-lock-keywords
503 (list
504 '(todos-date-string-match 1 todos-date-face t)
505 '(todos-time-string-match 1 todos-time-face t)
506 '(todos-done-string-match 0 todos-done-face t)
507 '(todos-category-string-match 0 todos-done-sep-face t))
508 "Font-locking for Todos mode.")
509
510 ;; ---------------------------------------------------------------------------
511
512 ;;; Internal variables
513 (defvar todos-categories nil
514 "TODO categories.")
515
516 (defvar todos-previous-line 0
517 "Previous line asked about.")
518
519 (defvar todos-previous-answer 0
520 "Previous answer got.")
521
522 (defvar todos-mode-map
523 (let ((map (make-keymap)))
524 (suppress-keymap map t)
525 ;; navigation commands
526 (define-key map "+" 'todos-forward-category)
527 (define-key map "-" 'todos-backward-category)
528 (define-key map "j" 'todos-jump-to-category)
529 (define-key map "n" 'todos-forward-item)
530 (define-key map "p" 'todos-backward-item)
531 (define-key map "S" 'todos-search)
532 ;; display commands
533 (define-key map "C" 'todos-display-categories)
534 (define-key map "h" 'todos-highlight-item)
535 (define-key map "N" 'todos-toggle-item-numbering)
536 ;; (define-key map "" 'todos-toggle-display-date-time)
537 (define-key map "P" 'todos-print)
538 (define-key map "q" 'todos-quit)
539 (define-key map "s" 'todos-save)
540 (define-key map "v" 'todos-toggle-view-done-items)
541 (define-key map "Y" 'todos-diary-items)
542 ;; (define-key map "S" 'todos-save-top-priorities)
543 (define-key map "t" 'todos-top-priorities)
544 ;; editing commands
545 (define-key map "A" 'todos-add-category)
546 (define-key map "d" 'todos-item-done)
547 ;; (define-key map "" 'todos-archive-done-items)
548 (define-key map "D" 'todos-delete-category)
549 (define-key map "e" 'todos-edit-item)
550 (define-key map "E" 'todos-edit-multiline)
551 ;; (define-key map "" 'todos-change-date)
552 ;; (define-key map "f" 'todos-file-item)
553 (define-key map "ii" 'todos-insert-item)
554 (define-key map "ih" 'todos-insert-item-here)
555 (define-key map "ia" 'todos-insert-item-ask-date)
556 (define-key map "id" 'todos-insert-item-for-diary)
557 ;; (define-key map "in" 'todos-insert-item-no-time)
558 (define-key map "k" 'todos-delete-item)
559 (define-key map "l" 'todos-lower-item)
560 (define-key map "m" 'todos-move-item)
561 (define-key map "r" 'todos-raise-item)
562 (define-key map "R" 'todos-rename-category)
563 (define-key map "u" 'todos-item-undo)
564 (define-key map "y" 'todos-toggle-item-diary-inclusion)
565 ;; (define-key map "" 'todos-toggle-diary-inclusion)
566 map)
567 "Todos mode keymap.")
568
569 (defvar todos-archive-mode-map
570 (let ((map (make-sparse-keymap)))
571 (suppress-keymap map t)
572 ;; navigation commands
573 (define-key map "+" 'todos-forward-category)
574 (define-key map "-" 'todos-backward-category)
575 (define-key map "j" 'todos-jump-to-category)
576 (define-key map "n" 'todos-forward-item)
577 (define-key map "p" 'todos-backward-item)
578 ;; display commands
579 (define-key map "C" 'todos-display-categories)
580 (define-key map "h" 'todos-highlight-item)
581 (define-key map "N" 'todos-toggle-item-numbering)
582 ;; (define-key map "" 'todos-toggle-display-date-time)
583 (define-key map "P" 'todos-print)
584 (define-key map "q" 'todos-quit)
585 (define-key map "s" 'todos-save)
586 (define-key map "S" 'todos-search)
587 map)
588 "Todos Archive mode keymap.")
589
590 (defvar todos-edit-mode-map
591 (let ((map (make-keymap)))
592 (define-key map "\C-c\C-q" 'todos-edit-quit)
593 map)
594 "Todos Edit mode keymap.")
595
596 (defvar todos-category-number 0 "TODO category number.")
597
598 (defvar todos-tmp-buffer-name " *todo tmp*")
599
600 (defvar todos-category-beg "--==-- "
601 "Category start separator to be prepended onto category name.")
602
603 ;; ---------------------------------------------------------------------------
604
605 ;;; Commands
606
607 ;;; Navigation
608
609 (defun todos-forward-category ()
610 "Go forward to TODO list of next category."
611 (interactive)
612 (setq todos-category-number
613 (mod (1+ todos-category-number) (length todos-categories)))
614 (todos-category-select))
615
616 (defun todos-backward-category ()
617 "Go back to TODO list of previous category."
618 (interactive)
619 (setq todos-category-number
620 (mod (1- todos-category-number) (length todos-categories)))
621 (todos-category-select))
622
623 ;; FIXME: Document that a non-existing name creates that category, and add
624 ;; y-or-n-p confirmation -- or eliminate this possibility?
625 (defun todos-jump-to-category ()
626 "Jump to a category. Default is previous category."
627 (interactive)
628 (let ((category (todos-completing-read)))
629 (if (string= "" category)
630 (setq category (nth todos-category-number todos-categories)))
631 (setq todos-category-number
632 (if (member category todos-categories)
633 (- (length todos-categories)
634 (length (member category todos-categories)))
635 (todos-add-category category)))
636 ;; (todos-show)))
637 (todos-category-select)))
638
639 ;; FIXME ? todos-{backward,forward}-item skip over empty line between done and
640 ;; not done items (but todos-forward-item gets there when done items are not
641 ;; displayed)
642 (defun todos-backward-item (&optional count)
643 "Select previous entry of TODO list."
644 (interactive "P")
645 ;; FIXME ? this moves to bob if on the first item (but so does previous-line)
646 (todos-item-start)
647 (unless (bobp)
648 (re-search-backward (concat "^\\(\\[\\(" (regexp-quote todos-done-string)
649 "\\)?\\)?\\(" todos-date-pattern "\\)")
650 nil t (or count 1))))
651
652 (defun todos-forward-item (&optional count)
653 "Select COUNT-th next entry of TODO list."
654 (interactive "P")
655 (goto-char (line-end-position))
656 (if (re-search-forward (concat "^\\(\\[\\(" (regexp-quote todos-done-string)
657 "\\)?\\)?\\(" todos-date-pattern "\\)")
658 nil t (or count 1))
659 (goto-char (match-beginning 0))
660 (goto-char (point-max))))
661
662 ;; (defun todos-forward-item (&optional count)
663 ;; "Select COUNT-th next entry of TODO list."
664 ;; (interactive "P")
665 ;; (let ((opoint (point))
666 ;; (done (save-excursion
667 ;; (if (re-search-forward (concat "\n\n\\\(\\["
668 ;; (regexp-quote todos-done-string)
669 ;; "\\)") nil t)
670 ;; (match-beginning 1)))))
671 ;; ;; FIXME: can this be simplified?
672 ;; (if (looking-at (concat "^\\(\\[\\(" (regexp-quote todos-done-string) "\\)?\\)?"
673 ;; todos-date-pattern)) ; on item header
674 ;; (re-search-forward (concat "^\\(\\[\\(" (regexp-quote todos-done-string)
675 ;; "\\)?\\)?\\(" todos-date-pattern "\\)")
676 ;; nil t (if count (1+ count) 2))
677 ;; (re-search-forward (concat "^\\(\\[\\(" (regexp-quote todos-done-string)
678 ;; "\\)?\\)?\\(" todos-date-pattern "\\)")
679 ;; nil t (or count 1)))
680 ;; (cond ((save-excursion
681 ;; (goto-char opoint)
682 ;; (looking-at "^$")) ; between done and not done items
683 ;; (forward-line 0))
684 ;; ((and done (> (point) done))
685 ;; (forward-line -1)) ; FIXME: count ?
686 ;; ((eq (point) opoint) ; on last item
687 ;; (goto-char (point-max)))
688 ;; (t
689 ;; (goto-char (match-beginning 0))))))
690
691 (defvar todos-search-string nil
692 "" ;FIXME
693 )
694 (defun todos-search ()
695 "" ;FIXME
696 (interactive)
697 (let ((regex (read-from-minibuffer "Enter a search string (regexp): "))
698 (start (point))
699 found cat in-done)
700 (widen)
701 (goto-char (point-min))
702 (while (and (setq found (re-search-forward regex nil t))
703 (save-excursion
704 (goto-char (line-beginning-position))
705 (looking-at (concat "^" (regexp-quote todos-category-beg)))))
706 (forward-line))
707 (if found
708 (progn
709 (setq found (match-beginning 0))
710 (todos-item-start)
711 (when (looking-at (concat "^\\[" (regexp-quote todos-done-string)))
712 (setq in-done t))
713 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
714 "\\(.*\\)\n") nil t)
715 (setq cat (match-string-no-properties 1))
716 (setq todos-category-number
717 (- (length todos-categories) (length (member cat todos-categories))))
718 (todos-category-select)
719 (when in-done (unless todos-show-with-done (todos-toggle-view-done-items)))
720 (goto-char found))
721 (todos-category-select)
722 (goto-char start)
723 (message "No match for \"%s\"" regex))))
724
725 ;;; Display
726
727 (defun todos-display-categories ()
728 "Display an alphabetical list of clickable Todos category names.
729 Click or type RET on a category name to go to it."
730 (interactive)
731 (let ((categories (copy-sequence todos-categories))
732 (cat-alist (todos-categories-alist))
733 (len (todos-longest-category-name-length))
734 beg)
735 ;; alphabetize the list case insensitively
736 (setq categories (sort categories (lambda (s1 s2) (let ((cis1 (upcase s1))
737 (cis2 (upcase s2)))
738 (string< cis1 cis2)))))
739 (with-current-buffer (get-buffer-create todos-categories-buffer)
740 (switch-to-buffer (current-buffer))
741 (erase-buffer)
742 (kill-all-local-variables)
743 (insert "Press a button to display the corresponding category.\n\n")
744 (setq beg (point))
745 (mapc (lambda (cat)
746 (let* ((catlen (length cat))
747 (catlen-odd (eq (logand catlen 1) 1)) ; oddp from cl.el
748 (padding (/ (- len catlen) 2)))
749 (insert-button (concat (make-string padding 32) cat
750 (make-string (if catlen-odd
751 (1+ padding)
752 padding)
753 32))
754 'face 'tool-bar
755 'action
756 `(lambda (button)
757 (todos-jump-to-category-noninteractively ,cat)))
758 (insert (make-string 8 32)
759 "(not done: "
760 (number-to-string (car (cadr (assoc cat cat-alist))))
761 ", done: "
762 (number-to-string (cdr (cadr (assoc cat cat-alist))))
763 ")")
764 (newline)))
765 categories))))
766 ;; (require 'widget)
767 ;; (eval-when-compile
768 ;; (require 'wid-edit))
769 ;; (with-current-buffer (get-buffer-create todos-categories-buffer)
770 ;; (switch-to-buffer (current-buffer))
771 ;; (erase-buffer)
772 ;; (kill-all-local-variables)
773 ;; (widget-insert "Press a button to display the corresponding category.\n\n")
774 ;; (setq beg (point))
775 ;; (mapc (lambda (cat)
776 ;; (widget-create 'push-button
777 ;; :notify (lambda (widget &rest ignore)
778 ;; (todos-jump-to-category-noninteractively
779 ;; (widget-get widget :value)))
780
781 ;; cat)
782 ;; (widget-insert " (not done: "
783 ;; (number-to-string (car (cadr (assoc cat cat-alist))))
784 ;; ", done: "
785 ;; (number-to-string (cdr (cadr (assoc cat cat-alist))))
786 ;; ")\n"))
787 ;; categories)
788 ;; (use-local-map widget-keymap)
789 ;; (widget-setup))))
790
791 (defun todos-toggle-item-numbering ()
792 "" ;FIXME
793 (interactive)
794 (todos-reset-prefix 'todos-number-prefix (not todos-number-prefix)))
795
796 (defun todos-toggle-view-done-items ()
797 "" ; FIXME
798 (interactive)
799 (let ((beg (point-min))
800 (done-sep (if (string-match "^[[:space:]]*$" todos-done-separator)
801 todos-done-separator
802 (propertize (concat todos-done-separator "\n")
803 'face 'todos-done-sep)))
804 (todos-show-with-done nil)
805 (done (point-max))
806 end ov)
807 (save-excursion
808 (goto-char beg)
809 (if (re-search-forward (concat "\n\\[" (regexp-quote todos-done-string))
810 nil t)
811 ;; hide done items
812 (progn (setq end (match-beginning 0))
813 (narrow-to-region beg end))
814 (widen)
815 (re-search-forward (concat "^" (regexp-quote todos-category-beg)) nil t)
816 (setq end (or (match-beginning 0) (point-max)))
817 (goto-char beg)
818 (if (re-search-forward
819 (concat (if (eq beg done) "" "\n") ; no newline if no unfinished items
820 "\n\\(\\[" (regexp-quote todos-done-string) "\\)")
821 end t)
822 ;; show done items
823 (let ((prefix (propertize
824 (concat (if todos-number-prefix "1" todos-prefix) " ")
825 'face 'todos-prefix-string))
826 ov-done ov-pref)
827 (setq done (match-beginning 1))
828 (narrow-to-region beg end)
829 (todos-prefix-overlays)
830 ;; add non-empty separator overlay in front of prefix overlay on
831 ;; first done item
832 (unless (string= done-sep todos-done-separator)
833 (goto-char done)
834 (remove-overlays done done)
835 ;; must make separator overlay after making prefix overlay to
836 ;; get the order separator before prefix
837 (setq ov-pref (make-overlay done done)
838 ov-done (make-overlay done done))
839 (overlay-put ov-pref 'before-string prefix)
840 (overlay-put ov-done 'before-string done-sep)))
841 ;; (when (setq ov (car (overlays-in done done)))
842 ;; (when (equal (overlay-get ov 'before-string) done-sep)
843 ;; (push ov todos-done-overlays)
844 ;; (delete-overlay ov)))
845 (todos-category-select)
846 (error "No done items in this category"))))))
847
848 (defun todos-view-archive (&optional cat)
849 ""
850 (interactive)
851 (if (file-exists-p todos-archive-file)
852 (progn
853 (find-file todos-archive-file)
854 (if cat
855 (if (member cat (todos-categories-list (current-buffer)))
856 (todos-jump-to-category-noninteractively cat)
857 (error "No archived items from this category"))
858 (todos-category-select)))
859 (error "There is currently no Todos archive")))
860
861 ;; FIXME: very slow
862 (defun todos-diary-items ()
863 "Display all todo items marked for diary inclusion."
864 (interactive)
865 (let ((bufname "*Todo diary entries*")
866 opoint)
867 (save-restriction
868 (save-current-buffer
869 (widen)
870 (copy-to-buffer bufname (point-min) (point-max))))
871 (with-current-buffer bufname
872 ;; (todos-mode)
873 (goto-char (point-min))
874 (while (not (eobp))
875 (setq opoint (point))
876 (cond ((looking-at "\\[")
877 (progn
878 (todos-forward-item)
879 (if (string-match
880 (concat "^" (regexp-quote todos-category-beg) ".*$")
881 (buffer-substring opoint (point)))
882 (kill-region opoint (+ opoint (match-beginning 0)))
883 (kill-region opoint (point)))))
884 ((looking-at "^$")
885 (kill-line))
886 (t
887 (todos-forward-item))))
888 (goto-char (point-min))
889 (while (not (eobp))
890 (setq opoint (point))
891 (if (looking-at (regexp-quote todos-category-beg))
892 (when (progn (forward-line)
893 (or (looking-at (regexp-quote todos-category-beg))
894 ;; category has done but no unfinished items
895 (and (looking-at "^$") (forward-line))
896 (eobp)))
897 (kill-region opoint (point)))
898 (forward-line)))
899 (make-local-variable 'font-lock-defaults)
900 (setq font-lock-defaults '(todos-font-lock-keywords t))
901 (font-lock-fontify-buffer)
902 (setq buffer-read-only t))
903 (display-buffer bufname)))
904
905 (defun todos-highlight-item ()
906 "Highlight the todo item the cursor is on."
907 (interactive)
908 (if hl-line-mode ; todos-highlight-item
909 (hl-line-mode 0)
910 (hl-line-mode 1)))
911
912 ;; FIXME: make this a customizable option for whole Todos file
913 (defun todos-toggle-display-date-time ()
914 "" ; FIXME
915 (interactive)
916 (save-excursion
917 (goto-char (point-min))
918 (let ((ovs (overlays-in (point) (line-end-position)))
919 ov hidden)
920 (while ovs
921 (setq ov (car ovs))
922 (if (equal (overlay-get ov 'display) "")
923 (setq ovs nil
924 hidden t)
925 (setq ovs (cdr ovs))))
926 (if hidden (remove-overlays (point-min) (point-max) 'display "")
927 (while (not (eobp))
928 (re-search-forward (concat "^\\[?" todos-date-pattern
929 " \\(" diary-time-regexp "\\)?\\]? ")
930 ; FIXME: this space in header? ^
931 nil t)
932 (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
933 (overlay-put ov 'display "")
934 (forward-line)))
935 (todos-update-numbered-prefix))))
936
937 ;;;###autoload
938 (defun todos-top-priorities (&optional nof-priorities category-pr-page show-done)
939 "List top priorities for each category.
940
941 Number of entries for each category is given by NOF-PRIORITIES which
942 defaults to \'todos-show-priorities\'.
943
944 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
945 between each category.
946
947 With non-nil SHOW-DONE, include done items in the listing."
948
949 (interactive "P")
950 (or nof-priorities (setq nof-priorities todos-show-priorities))
951 (if (listp nof-priorities) ;universal argument
952 (setq nof-priorities (car nof-priorities)))
953 (let ((todos-print-buffer-name todos-tmp-buffer-name)
954 (todos-category-break (if category-pr-page "\f" ""))
955 beg end done)
956 (save-excursion
957 (todos-show))
958 (save-restriction
959 (save-current-buffer
960 (widen)
961 (if (buffer-live-p (get-buffer todos-print-buffer-name))
962 (kill-buffer todos-print-buffer-name))
963 (copy-to-buffer todos-print-buffer-name (point-min) (point-max))))
964 (with-current-buffer todos-print-buffer-name
965 (goto-char (point-min))
966 (while (re-search-forward ;Find category start
967 (concat "^" (regexp-quote todos-category-beg))
968 nil t)
969 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
970 (setq end (if (re-search-forward todos-category-beg nil t)
971 (match-beginning 0)
972 (point-max)))
973 (goto-char beg)
974 (setq done
975 (if (re-search-forward
976 (concat
977 (if (looking-at "^$") "" "\n") ; no unfinished items
978 "\n\\(\\[" (regexp-quote todos-done-string) "\\)")
979 end t)
980 (match-beginning 1)
981 end))
982 (unless show-done
983 (delete-region done end)
984 (setq end done))
985 (narrow-to-region beg end) ;In case we have too few entries.
986 (goto-char (point-min))
987 (if (zerop nof-priorities) ;Traverse entries.
988 (goto-char end) ;All entries
989 (todos-forward-item nof-priorities))
990 (setq beg (point))
991 (delete-region beg end)
992 (widen))
993 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
994 (goto-char (point-min)) ;Due to display buffer
995 (make-local-variable 'font-lock-defaults)
996 (setq font-lock-defaults '(todos-font-lock-keywords t))
997 (font-lock-fontify-buffer)
998 (setq buffer-read-only t))
999 ;; Could have used switch-to-buffer as it has a norecord argument,
1000 ;; which is nice when we are called from e.g. todos-print.
1001 ;; Else we could have used pop-to-buffer.
1002 ;; (display-buffer todos-print-buffer-name)
1003 (display-buffer todos-print-buffer-name)
1004 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
1005 todos-print-buffer-name)))
1006
1007 ;;; Editing
1008
1009 ;;;###autoload
1010 (defun todos-add-category (&optional cat)
1011 "Add new category CAT to the TODO list."
1012 (interactive)
1013 (let ((buffer-read-only)
1014 (buf (find-file-noselect todos-file-do t))
1015 (prompt "Category: "))
1016 (unless (zerop (buffer-size buf))
1017 (and (null todos-categories)
1018 (error "Error in %s: File is non-empty but contains no category"
1019 todos-file-do)))
1020 (unless cat (setq cat (read-from-minibuffer prompt)))
1021 (with-current-buffer buf
1022 ;; reject names that could induce bugs and confusion
1023 (while (and (cond ((string= "" cat)
1024 (setq prompt "Enter a non-empty category name: "))
1025 ((string-match "\\`\\s-+\\'" cat)
1026 (setq prompt "Enter a category name that is not only white space: "))
1027 ((member cat todos-categories)
1028 (setq prompt "Enter a non-existing category name: ")))
1029 (setq cat (read-from-minibuffer prompt))))
1030 ;; initialize a newly created Todo buffer for Todo mode
1031 (unless (file-exists-p todos-file-do) (todos-mode))
1032 (setq todos-categories (cons cat todos-categories))
1033 (widen)
1034 (goto-char (point-min))
1035 ;; make sure file does not begin with empty lines (shouldn't, but may be
1036 ;; added by mistake), otherwise new categories will contain them, so
1037 ;; won't be really empty
1038 (while (looking-at "^$") (kill-line))
1039 (insert todos-category-beg cat "\n")
1040 (if (interactive-p)
1041 ;; properly display the newly added category
1042 (progn (setq todos-category-number 0) (todos-show))
1043 0))))
1044
1045 ;; FIXME: use function for category name choice here and in todos-add-category
1046 (defun todos-rename-category ()
1047 "Rename current Todos category."
1048 (interactive)
1049 (let* ((buffer-read-only)
1050 (cat (nth todos-category-number todos-categories))
1051 (vec (vconcat todos-categories))
1052 (new (read-from-minibuffer (format "Rename category \"%s\" to: " cat)))
1053 prompt)
1054 (while (and (cond ((string= "" new)
1055 (setq prompt "Enter a non-empty category name: "))
1056 ((string-match "\\`\\s-+\\'" new)
1057 (setq prompt "Enter a category name that is not only white space: "))
1058 ((member new todos-categories)
1059 (setq prompt "Enter a non-existing category name: ")))
1060 (setq new (read-from-minibuffer prompt))))
1061 (aset vec todos-category-number new)
1062 (setq todos-categories (append vec nil))
1063 (save-excursion
1064 (widen)
1065 (re-search-backward (concat (regexp-quote todos-category-beg) "\\("
1066 (regexp-quote cat) "\\)\n") nil t)
1067 (replace-match new t t nil 1)
1068 (goto-char (point-min))
1069 (setq mode-line-buffer-identification
1070 (concat "Category: " new))))
1071 ;; (concat "Category: " (format "%18s" new)))))
1072 (todos-category-select))
1073
1074 (defun todos-delete-category (&optional arg)
1075 "Delete current Todos category provided it is empty.
1076 With ARG non-nil delete the category unconditionally,
1077 i.e. including all existing entries."
1078 (interactive "P")
1079 (if (and (null arg)
1080 ;; FIXME: what about done items?
1081 (not (eq (point-max) (point-min))))
1082 (message "To delete a non-empty category, call the command with a prefix argument.")
1083 (let ((cat (nth todos-category-number todos-categories)) beg end)
1084 (when (y-or-n-p (concat "Permanently remove category \"" cat
1085 "\"" (and arg " and all its entries") "? "))
1086 (let ((buffer-read-only))
1087 (widen)
1088 (setq beg (re-search-backward (concat "^" (regexp-quote todos-category-beg)
1089 cat "\n") nil t)
1090 end (progn
1091 (re-search-forward (concat "\n\\("
1092 (regexp-quote todos-category-beg)
1093 ".*\n\\)") nil t)
1094 (match-beginning 1)))
1095 (remove-overlays beg end)
1096 (kill-region beg end)
1097 (setq todos-categories (delete cat todos-categories))
1098 (todos-category-select)
1099 (message "Deleted category %s" cat))))))
1100
1101 ;;;###autoload
1102 (defun todos-insert-item (&optional arg here date-time) ; FIXME revise docstring
1103 "Insert new TODO list item.
1104
1105 With prefix argument ARG solicit the category, otherwise use the
1106 current category.
1107
1108 With non-nil argument HERE insert the new item directly above the
1109 item at point. If point is on an empty line, insert the new item
1110 there.
1111
1112 If the value of TIME is `omit', insert the item without a time
1113 string; with the value `ask', solicit a time string; with any
1114 other value, add or omit the current time in accordance with
1115 `todos-add-time-string'."
1116 (interactive "P")
1117 (unless (or (todos-done-item-p)
1118 (save-excursion (forward-line -1) (todos-done-item-p)))
1119 (if (not (derived-mode-p 'todos-mode)) (todos-show))
1120 (let* ((buffer-read-only)
1121 (date-string (cond ;; ((eq date-time 'omit) "")
1122 ((eq date-time 'ask)
1123 (read-from-minibuffer "Enter a date: "))
1124 ((eq date-time 'to-date)
1125 (with-current-buffer "*Calendar*"
1126 (calendar-date-string (calendar-cursor-to-date t) t t)))
1127 (t (calendar-date-string (calendar-current-date) t t))))
1128 (time-string (if todos-add-time-string
1129 (cond ((eq date-time 'omit) "")
1130 ((eq date-time 'ask)
1131 (read-from-minibuffer "Enter a clock time: "))
1132 (t (substring (current-time-string) 11 16)))
1133 ""))
1134 (new-item (concat (unless todos-include-in-diary "[")
1135 date-string (unless (string= time-string "")
1136 (concat " " time-string))
1137 (unless todos-include-in-diary "]") " "
1138 (read-from-minibuffer "New TODO entry: ")))
1139 (current-category (nth todos-category-number todos-categories))
1140 (category (if arg (todos-completing-read) current-category)))
1141 (if here
1142 (todos-insert-with-overlays new-item)
1143 (todos-add-item-non-interactively new-item category)))))
1144
1145 (defun todos-insert-item-here (&optional date-time)
1146 "" ;FIXME add docstring
1147 (interactive)
1148 (todos-insert-item nil t date-time))
1149
1150 (defun todos-insert-item-no-time (&optional here)
1151 "" ;FIXME add docstring
1152 (interactive)
1153 (todos-insert-item nil here 'omit))
1154
1155 (defun todos-insert-item-ask-date (&optional here)
1156 "" ;FIXME add docstring
1157 (interactive)
1158 (todos-insert-item nil here 'ask))
1159
1160 (defun todos-insert-item-for-diary (&optional arg here date-time)
1161 "" ;FIXME
1162 (interactive "P")
1163 (let ((todos-include-in-diary t))
1164 (todos-insert-item arg here date-time)))
1165
1166 ;; FIXME: autoload when key-binding is defined in calendar.el
1167 (defun todos-insert-item-from-calendar ()
1168 "" ;FIXME
1169 (interactive)
1170 (pop-to-buffer (file-name-nondirectory todos-file-do))
1171 (todos-show)
1172 (todos-insert-item t nil 'to-date))
1173
1174 ;; FIXME: calendar is loaded before todos
1175 ;; (add-hook 'calendar-load-hook
1176 ;; (lambda ()
1177 (define-key calendar-mode-map "it" 'todos-insert-item-from-calendar);))
1178
1179 (defun todos-delete-item ()
1180 "Delete current TODO list entry."
1181 (interactive)
1182 (if (> (count-lines (point-min) (point-max)) 0)
1183 (let* ((buffer-read-only)
1184 (todos-entry (todos-item-string-start))
1185 (todos-answer (y-or-n-p (concat "Permanently remove '"
1186 todos-entry "'? "))))
1187 (when todos-answer
1188 (todos-remove-item)
1189 (when (and (bolp) (eolp)
1190 ;; not if last item was deleted
1191 (< (point-min) (point-max)))
1192 (todos-backward-item))
1193 ;; FIXME: is todos-prefix-overlays part of if-sexp, and is it needed
1194 ;; at all?
1195 (if todos-number-prefix
1196 (todos-update-numbered-prefix)
1197 (todos-prefix-overlays))))
1198 (error "No TODO list entry to delete")))
1199
1200 (defun todos-edit-item ()
1201 "Edit current TODO list entry."
1202 (interactive)
1203 (let ((buffer-read-only)
1204 (item (todos-item-string))
1205 (opoint (point)))
1206 (if (todos-string-multiline-p item)
1207 (todos-edit-multiline)
1208 (let ((new (read-from-minibuffer "Edit: " item)))
1209 (while (not (string-match (concat "^\\[?" todos-date-pattern) new))
1210 (setq new (read-from-minibuffer "Item must start with a date: " new)))
1211 ;; If user moved point during editing, make sure it moves back.
1212 (goto-char opoint)
1213 (todos-remove-item)
1214 (todos-insert-with-overlays new)))))
1215
1216 ;; FIXME: run todos-check-format on exiting buffer (or check for date string
1217 ;; and indentation)
1218 (defun todos-edit-multiline ()
1219 "Set up a buffer for editing a multiline TODO list entry."
1220 (interactive)
1221 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
1222 (switch-to-buffer
1223 (make-indirect-buffer
1224 (file-name-nondirectory todos-file-do) buffer-name))
1225 (message "To exit, simply kill this buffer and return to list.")
1226 (todos-edit-mode)
1227 (narrow-to-region (todos-item-start) (todos-item-end))))
1228
1229 (defun todos-edit-quit ()
1230 "" ;FIXME
1231 (interactive)
1232 (save-excursion (todos-category-select)))
1233
1234 ;; FIXME
1235 (defun todos-change-date (&optional event)
1236 "" ;FIXME
1237 (interactive)
1238 (let (dmarker
1239 calendar-view-diary-initially-flag
1240 new-date)
1241 (save-excursion
1242 (todos-item-start)
1243 (setq dmarker (point-marker)))
1244 (calendar)
1245 (message "Put the cursor on the desired date in the Calendar and press `q'")
1246 (setq new-date
1247 (calendar-date-string (calendar-cursor-to-date t) t t))
1248 ;; (pop-to-buffer (file-name-nondirectory todos-file-do))
1249 ;; (todos-show)
1250 (when (eq last-command 'calendar-exit)
1251 (goto-char (marker-position dmarker))
1252 (re-search-forward (concat "^\\[?\\(" todos-date-pattern "\\)\\]?")
1253 (line-end-position) t)
1254 (replace-match new-date nil nil nil 1))))
1255
1256 (defun todos-raise-item ()
1257 "Raise priority of current entry."
1258 (interactive)
1259 (unless (or (todos-done-item-p)
1260 (looking-at "^$")) ; between done and not done items
1261 (let (buffer-read-only)
1262 (if (> (count-lines (point-min) (point)) 0)
1263 (let ((item (todos-item-string)))
1264 (todos-remove-item)
1265 (todos-backward-item)
1266 (todos-insert-with-overlays item))
1267 (error "No TODO list entry to raise")))))
1268
1269 (defun todos-lower-item ()
1270 "Lower priority of current entry."
1271 (interactive)
1272 (unless (or (todos-done-item-p)
1273 (looking-at "^$")) ; between done and not done items
1274 (let ((buffer-read-only)
1275 (done (save-excursion
1276 (if (re-search-forward (concat "\n\n\\\["
1277 (regexp-quote todos-done-string))
1278 nil t)
1279 (match-beginning 0)
1280 (point-max)))))
1281 (if (> (count-lines (point) done) 1)
1282 ;; Assume there is a final newline
1283 (let ((item (todos-item-string))
1284 opoint)
1285 (todos-remove-item)
1286 (todos-forward-item)
1287 (todos-insert-with-overlays item))
1288 (error "No TODO list entry to lower")))))
1289
1290 ;; FIXME: moves last not done item when point on empty line below it
1291 ;; (defun todos-move-item ()
1292 ;; "Move the current todo item to another, interactively named, category.
1293
1294 ;; If the named category is not one of the current todo categories, then
1295 ;; it is created and the item becomes the first entry in that category."
1296 ;; (interactive)
1297 ;; (unless (or (todos-done-item-p)
1298 ;; (looking-at "^$")) ; between done and not done items
1299 ;; (let ((item (todos-item-string))
1300 ;; (category (todos-completing-read))
1301 ;; orig moved)
1302 ;; (setq (save-excursion (todos-item-start)))
1303 ;; (todos-remove-item)
1304 ;; ;; numbered prefix isn't cached (see todos-remove-item) so have to update
1305 ;; (if todos-number-prefix (todos-update-numbered-prefix))
1306 ;; (setq chgr (prepare-change-group))
1307 ;; ;; FIXME
1308 ;; (unwind-protect
1309 ;; (progn
1310 ;; (activate-change-group chgr)
1311 ;; (todos-add-item-non-interactively item category)
1312 ;; (setq moved t))
1313 ;; (if moved
1314 ;; (accept-change-group chgr)
1315 ;; (cancel-change-group chgr))))))
1316
1317 (defun todos-move-item ()
1318 "Move the current todo item to another, interactively named, category.
1319
1320 If the named category is not one of the current todo categories, then
1321 it is created and the item becomes the first entry in that category."
1322 (interactive)
1323 (unless (or (todos-done-item-p)
1324 (looking-at "^$")) ; between done and not done items
1325 (let ((buffer-read-only)
1326 (oldnum todos-category-number)
1327 (oldcat (nth todos-category-number todos-categories))
1328 (item (todos-item-string))
1329 (newcat (todos-completing-read))
1330 (opoint (point))
1331 (orig-mrk (save-excursion (todos-item-start) (point-marker)))
1332 moved)
1333 (todos-remove-item)
1334 ;; numbered prefix isn't cached (see todos-remove-item) so have to update
1335 (if todos-number-prefix (todos-update-numbered-prefix))
1336 (unwind-protect
1337 (progn
1338 (todos-add-item-non-interactively item newcat)
1339 (setq moved t))
1340 (unless moved
1341 (widen)
1342 (goto-char orig-mrk)
1343 (todos-insert-with-overlays item)
1344 (setq todos-category-number oldnum)
1345 (todos-category-select)
1346 ;; FIXME: does this work?
1347 (goto-char opoint))
1348 (set-marker orig-mrk nil)))))
1349
1350 ;; (defun todos-file-item (&optional comment)
1351 ;; "File the current TODO list entry away, annotated with an optional COMMENT."
1352 ;; (interactive "sComment: ")
1353 ;; (or (> (count-lines (point-min) (point-max)) 0)
1354 ;; (error "No TODO list entry to file away"))
1355 ;; (let ((time-stamp-format todos-time-string-format))
1356 ;; (when (and comment (> (length comment) 0))
1357 ;; (goto-char (todos-item-end))
1358 ;; (insert
1359 ;; (if (save-excursion (beginning-of-line)
1360 ;; (looking-at (regexp-quote todos-prefix)))
1361 ;; " "
1362 ;; "\n\t")
1363 ;; "(" comment ")"))
1364 ;; (goto-char (todos-item-end))
1365 ;; (insert " [" (nth todos-category-number todos-categories) "]")
1366 ;; (goto-char (todos-item-start))
1367 ;; (let ((temp-point (point)))
1368 ;; (if (looking-at (regexp-quote todos-prefix))
1369 ;; (replace-match (time-stamp-string))
1370 ;; ;; Standard prefix -> timestamp
1371 ;; ;; Else prefix non-standard item start with timestamp
1372 ;; (insert (time-stamp-string)))
1373 ;; (append-to-file temp-point (1+ (todos-item-end)) todos-file-done)
1374 ;; (delete-region temp-point (1+ (todos-item-end))))
1375 ;; (todos-backward-item)
1376 ;; (message ""))
1377
1378 (defun todos-item-done ()
1379 "Mark current item as done and move it to category's done section."
1380 (interactive)
1381 (unless (or (todos-done-item-p)
1382 (looking-at "^$"))
1383 (let* ((buffer-read-only)
1384 (item (todos-item-string))
1385 (date-string (calendar-date-string (calendar-current-date) t t))
1386 (time-string (if todos-add-time-string
1387 (concat " " (substring (current-time-string) 11 16))
1388 ""))
1389 (done-item (concat "[" todos-done-string date-string time-string "] " item))
1390 (items-end (point-max))
1391 next-cat)
1392 (todos-remove-item)
1393 (save-excursion
1394 (widen)
1395 (setq next-cat
1396 (save-excursion
1397 (if (re-search-forward (concat "^" (regexp-quote todos-category-beg))
1398 nil t)
1399 (match-beginning 0)
1400 (point-max))))
1401 ;; insert next done item at the top of the done items list
1402 (if (re-search-forward (concat "^\\[" (regexp-quote todos-done-string))
1403 next-cat t)
1404 (goto-char (match-beginning 0))
1405 ;; need empty line between done and not done items in order not to have
1406 ;; hanging todos-prefix when done items are hidden
1407 (goto-char next-cat)
1408 (newline))
1409 (todos-insert-with-overlays done-item)))
1410 (todos-show)))
1411
1412 (defun todos-archive-done-items ()
1413 "Archive the done items in the current category."
1414 (interactive)
1415 (let ((archive (find-file-noselect todos-archive-file t))
1416 (cat (nth todos-category-number todos-categories))
1417 beg end)
1418 (save-excursion
1419 (save-restriction
1420 (widen)
1421 (re-search-forward (concat "^" (regexp-quote todos-category-beg)) nil t)
1422 (setq end (or (match-beginning 0) (point-max)))
1423 (re-search-backward (concat "^" (regexp-quote todos-category-beg)
1424 (regexp-quote cat))
1425 nil t)
1426 (if (not (re-search-forward (concat "\\[" (regexp-quote todos-done-string))
1427 nil t))
1428 (error "No done items in this category")
1429 (setq beg (match-beginning 0))
1430 (setq done (buffer-substring beg end))
1431 (with-current-buffer archive
1432 (goto-char (point-min))
1433 (if (re-search-forward (regexp-quote (concat "^" todos-category-beg cat))
1434 nil t)
1435 (forward-char)
1436 (insert todos-category-beg cat "\n"))
1437 (insert done))
1438 (delete-region beg end)
1439 (remove-overlays beg end)
1440 (kill-line -1)))))
1441 (message "Done items archived."))
1442
1443 ;; FIXME: undone item leaves item number overlay behind
1444 (defun todos-item-undo ()
1445 "" ;FIXME
1446 (interactive)
1447 (when (todos-done-item-p)
1448 (let* ((buffer-read-only)
1449 (cat (nth todos-category-number todos-categories))
1450 (start (progn
1451 (todos-item-start)
1452 (search-forward "] "))) ; end of done date string
1453 (item (buffer-substring start (todos-item-end))))
1454 (todos-remove-item)
1455 (todos-add-item-non-interactively item cat))))
1456
1457 (defun todos-toggle-item-diary-inclusion ()
1458 "" ;FIXME add docstring
1459 (interactive)
1460 (save-excursion
1461 (let* ((buffer-read-only)
1462 (beg (todos-item-start))
1463 (lim (save-excursion (todos-item-end)))
1464 (end (save-excursion
1465 (or (todos-time-string-match lim)
1466 (todos-date-string-match lim)))))
1467 (if (looking-at "\\[") ; FIXME use todos-exclusion-start
1468 (progn
1469 (replace-match "")
1470 (search-forward "]" (1+ end) t) ; FIXME use todos-exclusion-end
1471 (replace-match ""))
1472 (when end
1473 (insert "[") ; FIXME use todos-exclusion-start
1474 (goto-char (1+ end))
1475 (insert "]")))))) ; FIXME use todos-exclusion-end
1476
1477 (defun todos-toggle-diary-inclusion (arg)
1478 "" ;FIXME add docstring
1479 (interactive "p")
1480 (save-excursion
1481 (save-restriction
1482 (when (eq arg 2) (widen)) ;FIXME: don't toggle done items
1483 (when (or (eq arg 1) (eq arg 2))
1484 (goto-char (point-min))
1485 (when (eq arg 2)
1486 (re-search-forward (concat "^" (regexp-quote todos-category-beg)) nil t)
1487 (forward-line)
1488 (when (looking-at (regexp-quote todos-category-end)) (forward-line)))
1489 (while (not (eobp))
1490 (todos-toggle-item-diary-inclusion)
1491 (todos-forward-item))))))
1492
1493 (defun todos-save-top-priorities (&optional nof-priorities)
1494 "Save top priorities for each category in `todos-file-top'.
1495
1496 Number of entries for each category is given by NOF-PRIORITIES which
1497 defaults to `todos-show-priorities'."
1498 (interactive "P")
1499 (save-window-excursion
1500 (save-excursion
1501 (save-restriction
1502 (todos-top-priorities nof-priorities)
1503 (set-buffer todos-tmp-buffer-name)
1504 (write-file todos-file-top)
1505 (kill-this-buffer)))))
1506
1507 ;;;###autoload
1508 (defun todos-print (&optional category-pr-page)
1509 "Print todo summary using `todos-print-function'.
1510 If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
1511 between each category.
1512
1513 Number of entries for each category is given by `todos-print-priorities'."
1514 (interactive "P")
1515 (when (yes-or-no-p "Print Todos list? ")
1516 (save-window-excursion
1517 (save-excursion
1518 (save-restriction
1519 (todos-top-priorities todos-print-priorities
1520 category-pr-page)
1521 (set-buffer todos-tmp-buffer-name)
1522 (and (funcall todos-print-function)
1523 (kill-this-buffer))
1524 (message "Todo printing done."))))))
1525
1526 ;; ---------------------------------------------------------------------------
1527
1528 ;;; Internal functions
1529
1530 ;; "Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec")
1531 ;; (regexp-opt (list "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31")))
1532
1533 ;; FIXME: use diary-date-forms instead?
1534 ;; (defun todos-date-string ()
1535 ;; "Return a regexp matching a diary date string."
1536 ;; (let ((month (regexp-opt (list "Jan" "Feb" "Mar" "Apr" "May" "Jun"
1537 ;; "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")))
1538 ;; (day "[0-3]?[0-9]")
1539 ;; (year "[0-9]\\{4\\}"))
1540 ;; (concat month " " day ", " year)))
1541
1542 ;; FIXME: use diary-time-regexp
1543 ;; (defun todos-time-string ()
1544 ;; "Return a regexp matching a diary time string."
1545 ;; "[0-9]?[0-9][:.][0-9]\\{2\\}")
1546
1547 (defvar todos-date-nodayname-pattern
1548 (let ((dayname)
1549 (monthname (format "\\(%s\\|\\*\\)"
1550 (diary-name-pattern calendar-month-name-array
1551 calendar-month-abbrev-array t)))
1552 (month "\\([0-9]+\\|\\*\\)")
1553 (day "\\([0-9]+\\|\\*\\)")
1554 (year "-?\\([0-9]+\\|\\*\\)"))
1555 (mapconcat 'eval calendar-date-display-form ""))
1556 "Regular expression matching a Todos date header without day name.")
1557
1558 ;; (defvar todos-dayname-pattern
1559 ;; (diary-name-pattern calendar-day-name-array nil t)
1560 ;; "Regular expression matching a day name in a Todos date header.")
1561
1562 (defvar todos-dayname-date-pattern
1563 (let ((dayname (diary-name-pattern calendar-day-name-array nil t)))
1564 (concat dayname "\\(?:, " todos-date-nodayname-pattern "\\)?"))
1565 "Regular expression matching a Todos date header with day name.")
1566
1567 (defvar todos-date-pattern
1568 (concat "\\(?:" todos-date-nodayname-pattern "\\)\\|"
1569 "\\(?:" todos-date-dayname-pattern "\\)")
1570 "Regular expression matching a Todos date header.")
1571
1572 (defun todos-date-string-match (lim)
1573 "Find Todos date strings for font-locking."
1574 (re-search-forward (concat "^\\[?\\(" todos-date-pattern "\\)") lim t))
1575
1576 (defun todos-time-string-match (lim)
1577 "Find Todos time strings for font-locking."
1578 (re-search-forward (concat "^\\[?\\(?:" todos-date-pattern "\\)"
1579 " \\(?1:" diary-time-regexp "\\)") lim t))
1580
1581 (defun todos-done-string-match (lim)
1582 "Find Todos done headers for font-locking."
1583 (re-search-forward (concat "^\\[" (regexp-quote todos-done-string) "[^][]+]")
1584 lim t))
1585
1586 (defun todos-category-string-match (lim)
1587 "Find Todos category headers for font-locking."
1588 (re-search-forward (concat "^" (regexp-quote todos-category-beg) ".*$")
1589 lim t))
1590
1591 (defun todos-check-format ()
1592 "Signal an error if the current Todos file is ill-formatted."
1593 (save-excursion
1594 (save-restriction
1595 (widen)
1596 (goto-char (point-min))
1597 (let ((legit (concat "^\\(" (regexp-quote todos-category-beg) "\\)"
1598 "\\|\\(\\[?" todos-date-pattern "\\)"
1599 "\\|\\([ \t]+[^ \t]*\\)"
1600 "\\|$")))
1601 (while (not (eobp))
1602 (unless (looking-at legit)
1603 (error "Illegitimate Todos file format at line %d"
1604 (line-number-at-pos (point))))
1605 (forward-line)))))
1606 (message "This Todos file is well-formatted."))
1607
1608 (defun todos-wrap-and-indent ()
1609 "" ;FIXME
1610 (make-local-variable 'word-wrap)
1611 (setq word-wrap t)
1612 (make-local-variable 'wrap-prefix)
1613 (setq wrap-prefix (make-string (+ 5 (length todos-prefix)) 32))
1614 (unless (member '(continuation) fringe-indicator-alist)
1615 (push '(continuation) fringe-indicator-alist)))
1616
1617 (defun todos-reset-prefix (symbol value)
1618 "Set SYMBOL's value to VALUE, and ." ; FIXME
1619 (let ((oldvalue (symbol-value symbol)))
1620 (custom-set-default symbol value)
1621 (when (not (equal value oldvalue))
1622 (save-window-excursion
1623 (todos-show)
1624 (save-excursion
1625 (widen)
1626 (goto-char (point-min))
1627 (while (not (eobp))
1628 (remove-overlays (point) (point)); 'before-string prefix)
1629 (forward-line)))
1630 ;; activate the prefix setting (save-restriction does not help)
1631 (todos-show)))))
1632
1633 ;; FIXME: rename and/or rewrite
1634 (defun todos-update-numbered-prefix ()
1635 "Update consecutive item numbering in the current category."
1636 (save-excursion
1637 (goto-char (point-min))
1638 (while (not (eobp))
1639 (remove-overlays (point) (point) 'before-string)
1640 (todos-forward-item))
1641 (todos-show)))
1642
1643 (defvar todos-item-start-overlays nil "")
1644
1645 ;; (defvar todos-done-overlays nil "")
1646
1647 ;; (defun todos-check-overlay (prop)
1648 ;; "" ;FIXME add docstring
1649 ;; ;; (let ((ovlist (overlays-in (1- (point)) (1+ (point)))))
1650 ;; (let ((ovlist (overlays-in (point) (point))))
1651 ;; (when ovlist (overlay-get (car ovlist) prop))))
1652
1653 (defun todos-prefix-overlays ()
1654 "" ;FIXME add docstring
1655 (when (or todos-number-prefix
1656 (not (string-match "^[[:space:]]*$" todos-prefix)))
1657 (let ((prefix (propertize (concat todos-prefix " ") 'face 'todos-prefix-string))
1658 (num 0)
1659 lim ov-pref)
1660 (save-excursion
1661 (goto-char (point-min))
1662 (while (or (todos-date-string-match lim)
1663 (todos-done-string-match lim))
1664 (goto-char (match-beginning 0))
1665 (when todos-number-prefix
1666 (setq num (1+ num))
1667 ;; reset number for done items
1668 (if (or (looking-at (concat "\n\\[" (regexp-quote todos-done-string)))
1669 ;; if last not done item is multiline, then
1670 ;; todos-done-string-match skips empty line, so have
1671 ;; to look back
1672 (and (looking-at (concat "^\\[" (regexp-quote todos-done-string)))
1673 (looking-back "\n\n")))
1674 (setq num 1))
1675 (setq prefix (propertize (concat (number-to-string num) " ")
1676 'face 'todos-prefix-string)))
1677 (or (and (setq ov-pref (car (overlays-in (point) (point))))
1678 (equal (overlay-get ov-pref 'before-string) prefix))
1679 (and (setq ov-pref (pop todos-item-start-overlays))
1680 (move-overlay ov-pref (point) (point)))
1681 (and (setq ov-pref (make-overlay (point) (point)))
1682 (overlay-put ov-pref 'before-string prefix)))
1683 (forward-line))))))
1684
1685 ;; (defun todos-show-paren-hack ()
1686 ;; "Purge overlay duplication due to show-paren-mode."
1687 ;; (save-excursion
1688 ;; (when show-paren-mode
1689 ;; (goto-char (point-min))
1690 ;; (while (not (eobp))
1691 ;; ;; (let ((ovlist (overlays-in (1- (point)) (1+ (point))))
1692 ;; (let ((ovlist (overlays-in (point) (point)))
1693 ;; ov)
1694 ;; (while (> (length ovlist) 1)
1695 ;; (setq ov (pop ovlist))
1696 ;; (delete-overlay ov)))
1697 ;; (forward-line))
1698 ;; (if (and (bolp) (eolp))
1699 ;; ;; (let ((ovlist (overlays-in (1- (point)) (1+ (point)))))
1700 ;; (let ((ovlist (overlays-in (point) (point))))
1701 ;; (remove-overlays (1- (point)) (1+ (point))))))))
1702
1703 (defun todos-reset-separator (symbol value)
1704 "Set SYMBOL's value to VALUE, and ." ; FIXME
1705 (let ((oldvalue (symbol-value symbol)))
1706 (custom-set-default symbol value)
1707 ;; (setq todos-done-overlays nil)
1708 (when (not (equal value oldvalue))
1709 (save-window-excursion
1710 (todos-show)
1711 (save-excursion
1712 (goto-char (point-min))
1713 (when (re-search-forward (concat "^\\[" (regexp-quote todos-done-string)) nil t)
1714 (remove-overlays (point) (point))))
1715 ;; activate the prefix setting (save-restriction does not help)
1716 (todos-show)))))
1717
1718 ;; FIXME: use this; should be defsubst?
1719 (defun todos-current-category ()
1720 "Return the name of the current category."
1721 (nth todos-category-number todos-categories))
1722
1723 (defun todos-category-select ()
1724 "Make TODO mode display the current category correctly."
1725 (let ((name (nth todos-category-number todos-categories)))
1726 (setq mode-line-buffer-identification (concat "Category: " name))
1727 (widen)
1728 (goto-char (point-min))
1729 (search-forward-regexp
1730 (concat "^" (regexp-quote (concat todos-category-beg name))
1731 "$"))
1732 (let ((begin (1+ (line-end-position)))
1733 (end (or (and (re-search-forward (concat "^" todos-category-beg) nil t)
1734 (match-beginning 0))
1735 (point-max))))
1736 (narrow-to-region begin end)
1737 (goto-char (point-min))))
1738 (todos-prefix-overlays)
1739 (let ((beg (point-min))
1740 (done-sep (if (string-match "^[[:space:]]*$" todos-done-separator)
1741 todos-done-separator
1742 (propertize (concat todos-done-separator "\n")
1743 'face 'todos-done-sep)))
1744 done ov)
1745 (when (re-search-forward (concat "\n\\(\\[" (regexp-quote todos-done-string)
1746 "\\)") nil t)
1747 (setq done (match-beginning 1)
1748 end (match-beginning 0))
1749 (if todos-show-with-done
1750 ;; with an empty separator just display the done items
1751 (if (string= done-sep todos-done-separator)
1752 (narrow-to-region (point-min) (point-max))
1753 ;; else display the separator in an overlay in front of the prefix
1754 ;; overlay on first done item
1755 (let ((prefix (propertize
1756 (concat (if todos-number-prefix "1" todos-prefix) " ")
1757 'face 'todos-prefix-string)))
1758 (goto-char done)
1759 (remove-overlays done done)
1760 ;; must make separator overlay after making prefix overlay to get
1761 ;; the order separator before prefix
1762 (setq ov-pref (make-overlay done done)
1763 ov-done (make-overlay done done))
1764 (overlay-put ov-pref 'before-string prefix)
1765 (overlay-put ov-done 'before-string done-sep)))
1766 ;; hide done items
1767 (narrow-to-region (point-min) end))))
1768 (goto-char (point-min)))
1769
1770 ;; FIXME: using numbering for priority instead of importance?
1771 ;;;###autoload
1772 (defun todos-add-item-non-interactively (new-item category)
1773 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
1774 ;; FIXME: really need this? (and in save-excursion?)
1775 (save-excursion
1776 (todos-show))
1777 (if (string= "" category)
1778 (setq category (nth todos-category-number todos-categories)))
1779 (let ((cat-exists (member category todos-categories)))
1780 (setq todos-category-number
1781 (if cat-exists
1782 (- (length todos-categories) (length cat-exists))
1783 (todos-add-category category))))
1784 ;; FIXME: really need this? (yes for todos-move-item, to show moved to category)
1785 (todos-show) ; now at point-min
1786 ;; (setq todos-previous-line 0)
1787 ;; (let* ((top 1)
1788 ;; (end (save-excursion
1789 ;; (goto-char (point-min))
1790 ;; (if (re-search-forward (concat "\n\n\\\(\\["
1791 ;; (regexp-quote todos-done-string)
1792 ;; "\\)") nil t)
1793 ;; (match-beginning 1)
1794 ;; (point-max))))
1795 ;; (bottom (count-lines (point-min) end)))
1796 ;; (while (> (- bottom top) todos-insert-threshold)
1797 ;; (let* ((current (/ (+ top bottom) 2))
1798 ;; (answer (if (< current bottom)
1799 ;; (todos-more-important-p current) nil)))
1800 ;; (if answer
1801 ;; (setq bottom current)
1802 ;; (setq top (1+ current)))))
1803 ;; (setq top (/ (+ top bottom) 2))
1804 ;; (goto-char (point-min))
1805 ;; (forward-line (1- top)))
1806 (unless (or (eq (point-min) (point-max)) ; no unfinished items
1807 (when (re-search-forward (concat "^\\["
1808 (regexp-quote todos-done-string))
1809 nil t)
1810 (forward-line -1)
1811 (bobp))) ; there are done items but no unfinished items
1812 (let* ((num-items (1+ (car (todos-count-items-in-category))))
1813 (priority (string-to-number (read-from-minibuffer
1814 (format "Set item priority (1-%d): "
1815 num-items))))
1816 prompt)
1817 (while (cond ((not (integerp priority))
1818 (setq prompt "Priority must be an integer.\n"))
1819 ((< priority 1)
1820 (setq prompt "Priority cannot be higher than 1.\n"))
1821 ((> priority num-items)
1822 (setq prompt (format "Priority cannot be lower than %d.\n"
1823 num-items))))
1824 (setq priority
1825 (string-to-number (read-from-minibuffer
1826 (concat prompt
1827 (format "Set item priority (1-%d): "
1828 num-items))))))
1829 (goto-char (point-min))
1830 (todos-forward-item (1- priority))))
1831 (todos-insert-with-overlays new-item))
1832
1833 (defun todos-jump-to-category-noninteractively (cat)
1834 (let ((bufname (buffer-name)))
1835 (cond ((string= bufname todos-categories-buffer)
1836 (switch-to-buffer (file-name-nondirectory todos-file-do)))
1837 ((string= bufname todos-archived-categories-buffer)
1838 ;; Is pop-to-buffer better for this case?
1839 (switch-to-buffer (file-name-nondirectory todos-archive-file))))
1840 (kill-buffer bufname))
1841 (widen)
1842 (goto-char (point-min))
1843 (setq todos-category-number (- (length todos-categories)
1844 (length (member cat todos-categories))))
1845 (todos-category-select))
1846
1847 (defun todos-insert-with-overlays (item)
1848 "" ;FIXME add docstring
1849 ;; FIXME: breaks without narrowing, e.g. todos-item-done
1850 ;; (unless (and (bolp) (eolp)) (goto-char (todos-item-start)))
1851 (insert item "\n")
1852 (todos-backward-item)
1853 (if todos-number-prefix
1854 (todos-update-numbered-prefix)
1855 (todos-prefix-overlays)))
1856
1857 ;; (defun todos-more-important-p (line)
1858 ;; "Ask whether entry is more important than the one at LINE."
1859 ;; (unless (equal todos-previous-line line)
1860 ;; (setq todos-previous-line line)
1861 ;; (goto-char (point-min))
1862 ;; (forward-line (1- todos-previous-line))
1863 ;; (let ((item (todos-item-string-start)))
1864 ;; (setq todos-previous-answer
1865 ;; (y-or-n-p (concat "More important than '" item "'? ")))))
1866 ;; todos-previous-answer)
1867
1868 (defun todos-line-string ()
1869 "Return current line in buffer as a string."
1870 (buffer-substring (line-beginning-position) (line-end-position)))
1871
1872 (defun todos-item-string-start ()
1873 "Return the start of this TODO list entry as a string."
1874 ;; Suitable for putting in the minibuffer when asking the user
1875 (let ((item (todos-item-string)))
1876 (if (> (length item) 60)
1877 (setq item (concat (substring item 0 56) "...")))
1878 item))
1879
1880 (defun todos-item-start ()
1881 "Move to start of current TODO list item and return its position."
1882 (unless (or (looking-at "^$") ; last item or between done and not done
1883 (looking-at (regexp-quote todos-category-beg))) ; for todos-count-items
1884 (goto-char (line-beginning-position))
1885 (while (not (looking-at (concat "^\\(\\[\\(" (regexp-quote todos-done-string)
1886 "\\)?\\)?" todos-date-pattern)))
1887 (forward-line -1)))
1888 (point))
1889
1890 (defun todos-item-end ()
1891 "Move to end of current TODO list item and return its position."
1892 (unless (looking-at "^$") ; last item or between done and not done
1893 (todos-forward-item)
1894 (backward-char))
1895 (point))
1896
1897 (defun todos-remove-item ()
1898 "Delete the current entry from the TODO list."
1899 (let* ((end (progn (todos-forward-item) (point)))
1900 (beg (progn (todos-backward-item) (point)))
1901 (ov-start (car (overlays-in beg beg))))
1902 (when ov-start
1903 ;; don't cache numbers, since they can be popped out of order in
1904 ;; todos-prefix-overlays
1905 (unless todos-number-prefix
1906 (push ov-start todos-item-start-overlays))
1907 (delete-overlay ov-start))
1908 (delete-region beg end)))
1909
1910 (defun todos-item-string ()
1911 "Return current TODO list entry as a string."
1912 (buffer-substring (todos-item-start) (todos-item-end)))
1913
1914 (defun todos-done-item-p ()
1915 "" ;FIXME
1916 (save-excursion
1917 (todos-item-start)
1918 (looking-at (concat "^\\[" (regexp-quote todos-done-string)))))
1919
1920 (defun todos-count-items-in-category ()
1921 "Return number of not done and done items in current category."
1922 (save-excursion
1923 (let ((not-done 0)
1924 (done 0)
1925 (beg (point-min))
1926 end)
1927 (save-restriction
1928 (widen)
1929 (re-search-forward (concat "^" (regexp-quote todos-category-beg)) nil t)
1930 (setq end (or (match-beginning 0) (point-max)))
1931 (goto-char beg)
1932 (while (> end (point))
1933 (if (todos-done-item-p)
1934 (setq done (1+ done))
1935 (setq not-done (1+ not-done)))
1936 (todos-forward-item)
1937 (when (and (not (> (point) end))
1938 (looking-at "^$")
1939 (not (eobp)))
1940 ;; point is between done and not done items
1941 (setq not-done (1- not-done))))
1942 (cons not-done done)))))
1943
1944 ;; FIXME: rename, since *-alist is by convention a variable name
1945 (defun todos-categories-alist ()
1946 "Return alist of categories and some of their properties.
1947 The properties are at least the numbers of the unfinished and
1948 done items in the category."
1949 (let (todos-categories-alist)
1950 (save-excursion
1951 (save-restriction
1952 (widen)
1953 (goto-char (point-min))
1954 (let ((not-done 0)
1955 (done 0)
1956 category beg end)
1957 (while (not (eobp))
1958 (cond ((looking-at (concat (regexp-quote todos-category-beg)
1959 "\\(.*\\)\n"))
1960 (setq not-done 0 done 0)
1961 (push (list (match-string-no-properties 1) (cons not-done done))
1962 todos-categories-alist))
1963 ((looking-at (concat "^\\[" (regexp-quote todos-done-string)))
1964 (setq done (1+ done))
1965 (setcdr (cadr (car todos-categories-alist)) done))
1966 ((looking-at (concat "^\\[?" todos-date-pattern))
1967 (setq not-done (1+ not-done))
1968 (setcar (cadr (car todos-categories-alist)) not-done)))
1969 (forward-line)))))
1970 todos-categories-alist))
1971
1972 (defun todos-count-all-items ()
1973 ""
1974 (let ((unfinished 0)
1975 (done 0))
1976 (dolist (l (todos-categories-alist))
1977 (setq unfinished (+ unfinished (car (cadr l)))
1978 done (+ done (cdr (cadr l)))))
1979 (cons unfinished done)))
1980
1981 (defun todos-longest-category-name-length ()
1982 ""
1983 (let ((longest 0))
1984 (dolist (c (todos-categories-alist) longest)
1985 (setq longest (max longest (length (car c)))))))
1986
1987 (defun todos-string-count-lines (string)
1988 "Return the number of lines STRING spans."
1989 (length (split-string string "\n")))
1990
1991 (defun todos-string-multiline-p (string)
1992 "Return non-nil if STRING spans several lines."
1993 (> (todos-string-count-lines string) 1))
1994
1995 (defun todos-completing-read ()
1996 "Return a category name, with completion, for use in Todo mode."
1997 ;; allow SPC to insert spaces, for adding new category names with
1998 ;; todos-move-item
1999 (let ((map minibuffer-local-completion-map))
2000 (define-key map " " nil)
2001 ;; make a copy of todos-categories in case history-delete-duplicates is
2002 ;; non-nil, which makes completing-read alter todos-categories
2003 (let* ((categories (copy-sequence todos-categories))
2004 (history (cons 'todos-categories (1+ todos-category-number)))
2005 (default (nth todos-category-number todos-categories))
2006 (completion-ignore-case todos-completion-ignore-case)
2007 (category (completing-read
2008 (concat "Category [" default "]: ")
2009 todos-categories nil nil nil history default)))
2010 ;; restore the original value of todos-categories
2011 (setq todos-categories categories)
2012 category)))
2013
2014 (defun todos-categories-list (buf)
2015 "Return a list of the Todo mode categories in buffer BUF."
2016 (let (categories)
2017 (with-current-buffer buf
2018 (save-excursion
2019 (save-restriction
2020 (widen)
2021 (goto-char (point-max))
2022 (while (re-search-backward (concat "^" (regexp-quote todos-category-beg)
2023 "\\(.*\\)\n") nil t)
2024 (push (match-string-no-properties 1) categories)))))
2025 categories))
2026
2027 ;; ---------------------------------------------------------------------------
2028 ;;; Mode setup
2029
2030 (easy-menu-define todos-menu todos-mode-map "Todo Menu"
2031 '("Todo"
2032 ["Next category" todos-forward-category t]
2033 ["Previous category" todos-backward-category t]
2034 ["Jump to category" todos-jump-to-category t]
2035 ["Show top priority items" todos-top-priorities t]
2036 ["Print categories" todos-print t]
2037 "---"
2038 ["Edit item" todos-edit-item t]
2039 ["File item" todos-file-item t]
2040 ["Insert new item" todos-insert-item t]
2041 ["Insert item here" todos-insert-item-here t]
2042 ["Kill item" todos-delete-item t]
2043 "---"
2044 ["Lower item priority" todos-lower-item t]
2045 ["Raise item priority" todos-raise-item t]
2046 "---"
2047 ["Next item" todos-forward-item t]
2048 ["Previous item" todos-backward-item t]
2049 "---"
2050 ["Save" todos-save t]
2051 ["Save Top Priorities" todos-save-top-priorities t]
2052 "---"
2053 ["Quit" todos-quit t]
2054 ))
2055
2056 ;; As calendar reads .todos-do before todos-mode is loaded.
2057 ;;;###autoload
2058 (defun todos-mode ()
2059 "Major mode for editing TODO lists.
2060
2061 \\{todos-mode-map}"
2062 (interactive)
2063 (kill-all-local-variables)
2064 (setq major-mode 'todos-mode)
2065 (setq mode-name "TODOS")
2066 (use-local-map todos-mode-map)
2067 (easy-menu-add todos-menu)
2068 (when todos-wrap-lines (funcall todos-line-wrapping-function))
2069 (make-local-variable 'font-lock-defaults)
2070 (setq font-lock-defaults '(todos-font-lock-keywords t))
2071 (make-local-variable 'hl-line-range-function)
2072 (setq hl-line-range-function
2073 (lambda() (when (todos-item-end)
2074 (cons (todos-item-start) (todos-item-end)))))
2075 ;; (add-hook 'post-command-hook 'todos-show-paren-hack nil t)
2076 (add-to-invisibility-spec 'todos)
2077 ;; FIXME: use this and let-bind in editing commands?
2078 (setq buffer-read-only t)
2079 (run-mode-hooks 'todos-mode-hook))
2080
2081 (defun todos-archive-mode ()
2082 "Major mode for archived Todos categories.
2083
2084 \\{todos-mode-map}"
2085 (interactive)
2086 (kill-all-local-variables)
2087 (setq major-mode 'todos-archive-mode)
2088 (setq mode-name "TODOS Arch")
2089 (use-local-map todos-archive-mode-map)
2090 ;; (easy-menu-add todos-menu)
2091 (when todos-wrap-lines (funcall todos-line-wrapping-function))
2092 (make-local-variable 'font-lock-defaults)
2093 (setq font-lock-defaults '(todos-font-lock-keywords t))
2094 (make-local-variable 'hl-line-range-function)
2095 (setq hl-line-range-function
2096 (lambda() (when (todos-item-end)
2097 (cons (todos-item-start) (todos-item-end)))))
2098 ;; (add-hook 'post-command-hook 'todos-show-paren-hack nil t)
2099 (add-to-invisibility-spec 'todos)
2100 (run-mode-hooks 'todos-mode-hook))
2101
2102 (defun todos-edit-mode ()
2103 "Major mode for editing items in the TODO list.
2104
2105 \\{todos-edit-mode-map}"
2106 (interactive)
2107 (setq major-mode 'todos-edit-mode)
2108 (setq mode-name "TODOS Edit")
2109 (use-local-map todos-edit-mode-map)
2110 (make-local-variable 'font-lock-defaults)
2111 (setq font-lock-defaults '(todos-font-lock-keywords t))
2112 (when todos-wrap-lines (funcall todos-line-wrapping-function)))
2113
2114 (defun todos-save ()
2115 "Save the TODO list."
2116 (interactive)
2117 (save-excursion
2118 (save-restriction
2119 (save-buffer)))
2120 ;; (if todos-save-top-priorities-too (todos-save-top-priorities)))
2121 )
2122
2123 (defun todos-quit ()
2124 "Done with TODO list for now."
2125 (interactive)
2126 (widen)
2127 (todos-save)
2128 (message "")
2129 (bury-buffer))
2130
2131 ;;;###autoload
2132 (defun todos-show ()
2133 "Show TODO list."
2134 (interactive)
2135 ;; Make this a no-op if called interactively in narrowed Todos mode, since
2136 ;; it is in that case redundant, but in particular to work around the bug of
2137 ;; item prefix reduplication with show-paren-mode enabled.
2138 (unless (and (called-interactively-p)
2139 (eq major-mode 'todos-mode)
2140 (< (- ( point-max) (point-min)) (buffer-size)))
2141 ;; Call todos-initial-setup only if there is neither a Todo file nor
2142 ;; a corresponding unsaved buffer.
2143 (if (or (file-exists-p todos-file-do)
2144 (let* ((buf (get-buffer (file-name-nondirectory todos-file-do)))
2145 (bufname (buffer-file-name buf)))
2146 (equal (expand-file-name todos-file-do) bufname)))
2147 (find-file todos-file-do)
2148 (todos-initial-setup))
2149 (unless (eq major-mode 'todos-mode) (todos-mode))
2150 (unless todos-categories
2151 (setq todos-categories (todos-categories-list (buffer-name))))
2152 ;; (beginning-of-line)
2153 (save-excursion
2154 (todos-category-select)
2155 ;; (todos-show-paren-hack)
2156 )))
2157
2158 (defun todos-initial-setup ()
2159 "Set up things to work properly in TODO mode."
2160 (find-file todos-file-do)
2161 (erase-buffer)
2162 (todos-mode)
2163 (todos-add-category "Todos"))
2164
2165 (provide 'todos)
2166
2167 ;; arch-tag: 6fd91be5-776e-4464-a109-da4ea0e4e497
2168 ;;; todos.el ends here