New maintainer address.
[bpt/emacs.git] / lisp / calendar / todo-mode.el
1 ;; todo-mode.el -- Major mode for editing TODO list files
2
3 ;; Copyright (C) 1997, 2000 Free Software Foundation, Inc.
4
5 ;; Author: os10000@seidel-space.de
6 ;; Created: 2 Aug 1997
7 ;; Version: $Id: todo-mode.el,v 1.40 2000/06/02 18:37:43 fx Exp $
8 ;; Keywords: Categorised TODO list editor, todo-mode
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;; ---------------------------------------------------------------------------
28
29 ;;; Commentary:
30
31 ;; Mode Description
32 ;;
33 ;; TODO is a major mode for EMACS which offers functionality to
34 ;; treat most lines in one buffer as a list of items one has to
35 ;; do. There are facilities to add new items, which are
36 ;; categorised, to edit or even delete items from the buffer.
37 ;; The buffer contents are currently compatible with the diary,
38 ;; so that the list of todo-items will show up in the FANCY diary
39 ;; mode.
40 ;;
41 ;; Notice: Besides the major mode, this file also exports the
42 ;; function `todo-show' which will change to the one specific
43 ;; TODO file that has been specified in the todo-file-do
44 ;; variable. If this file does not conform to the TODO mode
45 ;; conventions, the todo-show function will add the appropriate
46 ;; header and footer. I don't anticipate this to cause much
47 ;; grief, but be warned, in case you attempt to read a plain text
48 ;; file.
49 ;;
50 ;; Preface, Quickstart Installation
51 ;;
52 ;; To get this to work, make emacs execute the line
53 ;;
54 ;; (autoload 'todo-mode "todo-mode"
55 ;; "Major mode for editing TODO lists." t)
56 ;; (autoload 'todo-show "todo-mode"
57 ;; "Show TODO items." t)
58 ;; (autoload 'todo-insert-item "todo-mode"
59 ;; "Add TODO item." t)
60 ;;
61 ;; You may now enter new items by typing "M-x todo-insert-item",
62 ;; or enter your TODO list file by typing "M-x todo-show".
63 ;;
64 ;; -------------------------------------------------------------
65 ;;
66 ;; oh no, it doesn't work any more ... but Alex Schroeder
67 ;; <a.schroeder@bsiag.ch> writes:
68 ;;
69 ;; -------------------------------------------------------------
70 ;;
71 ;; 1. Call todo-show (I called todo-insert first)
72 ;; 2. Add some categories (I called todo-insert)
73 ;; 3. Save the buffer, restart Emacs (perhaps restarting Emacs is not
74 ;; required)
75 ;; 4. Only now can you start to add entries.
76 ;;
77 ;; This is a bit cumbersome, and it should be documented. You probably
78 ;;
79 ;; -------------------------------------------------------------
80 ;;
81 ;; and right he is. My apologies, I'll try to fix it sometime.
82 ;;
83 ;; -------------------------------------------------------------
84 ;;
85 ;; The TODO list file has a special format and some auxiliary
86 ;; information, which will be added by the todo-show function if
87 ;; it attempts to visit an un-initialised file. Hence it is
88 ;; recommended to use the todo-show function for the first time,
89 ;; in order to initialise the file, but it is not necessary
90 ;; afterwards.
91 ;;
92 ;; As these commands are quite long to type, I would recommend
93 ;; the addition of two bindings to your to your global keymap. I
94 ;; personally have the following in my initialisation file:
95 ;;
96 ;; (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
97 ;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
98 ;;
99 ;; Note, however, that this recommendation has prompted some
100 ;; criticism, since the keys C-c LETTER are reserved for user
101 ;; functions. I believe my recommendation is acceptable, since
102 ;; the Emacs Lisp Manual *Tips* section also details that the
103 ;; mode itself should not bind any functions to those keys. The
104 ;; express aim of the above two bindings is to work outside the
105 ;; mode, which doesn't need the show function and offers a
106 ;; different binding for the insert function. They serve as
107 ;; shortcuts and are not even needed (since the TODO mode will be
108 ;; entered by visiting the TODO file, and later by switching to
109 ;; its buffer).
110 ;;
111 ;; If you are an advanced user of this package, please consult
112 ;; the whole source code for autoloads, because there are several
113 ;; extensions that are not explicitly listed in the above quick
114 ;; installation.
115 ;;
116 ;; Version
117 ;;
118 ;; Which version of todo-mode.el does this documentation refer to?
119 ;;
120 ;; $Id: todo-mode.el,v 1.40 2000/06/02 18:37:43 fx Exp $
121 ;;
122 ;; Operation
123 ;;
124 ;; You will have the following facilities available:
125 ;;
126 ;; M-x todo-show will enter the todo list screen, here type
127 ;;
128 ;; spc will toggle the display of sub-trees
129 ;;
130 ;; + to go to next category
131 ;; - to go to previous category
132 ;; d to file the current entry, including a
133 ;; comment and timestamp
134 ;; e to edit the current entry
135 ;; E to edit a multi-line entry
136 ;; f to file the current entry, including a
137 ;; comment and timestamp
138 ;; i to insert a new entry, with prefix, omit category
139 ;; I to insert a new entry at current cursor position
140 ;; j jump to category
141 ;; k to kill the current entry
142 ;; l to lower the current entry's priority
143 ;; n for the next entry
144 ;; p for the previous entry
145 ;; P print
146 ;; q to save the list and exit the buffer
147 ;; r to raise the current entry's priority
148 ;; s to save the list
149 ;; S to save the list of top priorities
150 ;; t show top priority items for each category
151 ;;
152 ;; When you add a new entry, you are asked for the text and then
153 ;; for the category. I for example have categories for things
154 ;; that I want to do in the office (like mail my mum), that I
155 ;; want to do in town (like buy cornflakes) and things I want to
156 ;; do at home (move my suitcases). The categories can be
157 ;; selected with the cursor keys and if you type in the name of a
158 ;; category which didn't exist before, an empty category of the
159 ;; desired name will be added and filled with the new entry.
160 ;;
161 ;; Configuration
162 ;;
163 ;; Variable todo-prefix
164 ;;
165 ;; I would like to recommend that you use the prefix "*/*" (by
166 ;; leaving the variable 'todo-prefix' untouched) so that the
167 ;; diary displays each entry every day.
168 ;;
169 ;; To understand what I mean, please read the documentation that
170 ;; goes with the calendar since that will tell you how you can
171 ;; set up the fancy diary display and use the #include command to
172 ;; include your todo list file as part of your diary.
173 ;;
174 ;; If you have the diary package set up to usually display more
175 ;; than one day's entries at once, consider using
176 ;;
177 ;; "&%%(equal (calendar-current-date) date)"
178 ;;
179 ;; as the value of `todo-prefix'. Please note that this may slow
180 ;; down the processing of your diary file some.
181 ;;
182 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
183 ;;
184 ;; "&%%(todo-cp)"
185 ;;
186 ;; might be nicer and to that effect a function has been declared
187 ;; further down in the code. You may wish to auto-load this.
188 ;;
189 ;; Carsten also writes that that *changing* the prefix after the
190 ;; todo list is already established is not as simple as changing
191 ;; the variable - the todo files have to be changed by hand.
192 ;;
193 ;; Variable todo-file-do
194 ;;
195 ;; This variable is fairly self-explanatory. You have to store
196 ;; your TODO list somewhere. This variable tells the package
197 ;; where to go and find this file.
198 ;;
199 ;; Variable todo-file-done
200 ;;
201 ;; Even when you're done, you may wish to retain the entries.
202 ;; Given that they're timestamped and you are offered to add a
203 ;; comment, this can make a useful diary of past events. It will
204 ;; even blend in with the EMACS diary package. So anyway, this
205 ;; variable holds the name of the file for the filed todo-items.
206 ;;
207 ;; Variable todo-file-top
208 ;;
209 ;; File storing the top priorities of your TODO list when
210 ;; todo-save-top-priorities is non-nil. Nice to include in your
211 ;; diary instead of the complete TODO list.
212 ;;
213 ;; Variable todo-mode-hook
214 ;;
215 ;; Just like other modes, too, this mode offers to call your
216 ;; functions before it goes about its business. This variable
217 ;; will be inspected for any functions you may wish to have
218 ;; called once the other TODO mode preparations have been
219 ;; completed.
220 ;;
221 ;; Variable todo-insert-threshold
222 ;;
223 ;; Another nifty feature is the insertion accuracy. If you have
224 ;; 8 items in your TODO list, then you may get asked 4 questions
225 ;; by the binary insertion algorithm. However, you may not
226 ;; really have a need for such accurate priorities amongst your
227 ;; TODO items. If you now think about the binary insertion
228 ;; halfing the size of the window each time, then the threshhold
229 ;; is the window size at which it will stop. If you set the
230 ;; threshhold to zero, the upper and lower bound will coincide at
231 ;; the end of the loop and you will insert your item just before
232 ;; that point. If you set the threshhold to, e.g. 8, it will stop
233 ;; as soon as the window size drops below that amount and will
234 ;; insert the item in the approximate centre of that window. I
235 ;; got the idea for this feature after reading a very helpful
236 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
237 ;; corrected some of my awful coding and pointed me towards some
238 ;; good reading. Thanks Trey!
239 ;;
240 ;; Things to do
241 ;;
242 ;; These originally were my ideas, but now also include all the
243 ;; suggestions that I included before forgetting them:
244 ;;
245 ;; o Fancy fonts for todo/top-priority buffer
246 ;; o Remove todo-prefix option in todo-top-priorities
247 ;; o Rename category
248 ;; o Move entry from one category to another one
249 ;; o Entries which both have the generic */* prefix and a
250 ;; "deadline" entry which are understood by diary, indicating
251 ;; an event (unless marked by &)
252 ;; o The optional COUNT variable of todo-forward-item should be
253 ;; applied to the other functions performing similar tasks
254 ;; o Modularization could be done for repeaded elements of
255 ;; the code, like the completing-read lines of code.
256 ;; o license / version function
257 ;; o export to diary file
258 ;; o todo-report-bug
259 ;; o GNATS support
260 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
261 ;; outline mode)
262 ;; o rewrite complete package to store data as lisp objects
263 ;; and have display modes for display, for diary export,
264 ;; etc. (Richard Stallman pointed out this is a bad idea)
265 ;; o so base todo-mode.el on generic-mode.el instead
266 ;;
267 ;; History and Gossip
268 ;;
269 ;; Many thanks to all the ones who have contributed to the
270 ;; evolution of this package! I hope I have listed all of you
271 ;; somewhere in the documentation or at least in the RCS history!
272 ;;
273 ;; Enjoy this package and express your gratitude by sending nice
274 ;; things to my parents' address!
275 ;;
276 ;; Oliver Seidel
277 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
278
279 ;;; Code:
280
281 (eval-when-compile
282 (require 'outline)
283 (require 'calendar)
284 ;; Calendar dynamic bondage:
285 (defvar date)
286 (defvar entry))
287 (autoload 'time-stamp-string "time-stamp")
288
289 ;; User-configurable variables:
290
291 (defgroup todo nil
292 "Maintain a list of todo items."
293 :version "21.1"
294 :group 'calendar)
295
296 (defcustom todo-prefix "*/*"
297 "*TODO mode prefix for entries.
298
299 This is useful in conjunction with `calendar' and `diary' if you use
300
301 #include \"~/.todo-do\"
302
303 in your diary file to include your todo list file as part of your
304 diary. With the default value \"*/*\" the diary displays each entry
305 every day and it may also be marked on every day of the calendar.
306 Using \"&%%(equal (calendar-current-date) date)\" instead will only
307 show and mark todo entreis for today, but may slow down processing of
308 the diary file somewhat."
309 :type 'string
310 :group 'todo)
311 (defcustom todo-file-do "~/.todo-do"
312 "*TODO mode list file."
313 :type 'file
314 :group 'todo)
315 (defcustom todo-file-done "~/.todo-done"
316 "*TODO mode archive file."
317 :type 'file
318 :group 'todo)
319 (defcustom todo-mode-hook nil
320 "*TODO mode hooks."
321 :type 'hook
322 :group 'todo)
323 (defcustom todo-edit-mode-hook nil
324 "*TODO Edit mode hooks."
325 :type 'hook
326 :group 'todo)
327 (defcustom todo-insert-threshold 0
328 "*TODO mode insertion accuracy.
329
330 If you have 8 items in your TODO list, then you may get asked 4
331 questions by the binary insertion algorithm. However, you may not
332 really have a need for such accurate priorities amongst your TODO
333 items. If you now think about the binary insertion halfing the size
334 of the window each time, then the threshhold is the window size at
335 which it will stop. If you set the threshhold to zero, the upper and
336 lower bound will coincide at the end of the loop and you will insert
337 your item just before that point. If you set the threshhold to,
338 e.g. 8, it will stop as soon as the window size drops below that
339 amount and will insert the item in the approximate centre of that
340 window."
341 :type 'integer
342 :group 'todo)
343 (defvar todo-edit-buffer " *TODO Edit*" "TODO Edit buffer name.")
344 (defcustom todo-file-top "~/.todo-top"
345 "*TODO mode top priorities file.
346
347 Not in TODO format, but diary compatible.
348 Automatically generated when `todo-save-top-priorities' is non-nil."
349 :type 'string
350 :group 'todo)
351
352 (defcustom todo-print-function 'ps-print-buffer-with-faces
353 "*Function to print the current buffer."
354 :type 'symbol
355 :group 'todo)
356 (defcustom todo-show-priorities 1
357 "*Default number of priorities to show by \\[todo-top-priorities].
358 0 means show all entries."
359 :type 'integer
360 :group 'todo)
361 (defcustom todo-print-priorities 0
362 "*Default number of priorities to print by \\[todo-print].
363 0 means print all entries."
364 :type 'integer
365 :group 'todo)
366 (defcustom todo-remove-separator t
367 "*Non-nil to remove category separators in\
368 \\[todo-top-priorities] and \\[todo-print]."
369 :type 'boolean
370 :group 'todo)
371 (defcustom todo-save-top-priorities-too t
372 "*Non-nil makes `todo-save' automatically save top-priorities in `todo-file-top'."
373 :type 'boolean
374 :group 'todo)
375
376 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
377 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
378 ;;
379 (defcustom todo-time-string-format
380 "%04y-%02m-%02d %02H:%02M"
381 "*TODO mode time string format for done entries.
382 For details see the variable `time-stamp-format'."
383 :type 'string
384 :group 'todo)
385
386 (defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
387 "*Function producing text to insert at start of todo entry."
388 :type 'symbol
389 :group 'todo)
390 (defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
391 "*Initials of todo item author."
392 :type 'string
393 :group 'todo)
394
395 (defun todo-entry-timestamp-initials ()
396 "Prepend timestamp and your initials to the head of a TODO entry."
397 (let ((time-stamp-format todo-time-string-format))
398 (concat (time-stamp-string) " " todo-initials ": ")))
399
400 ;; Set up some helpful context ...
401
402 (defvar todo-categories nil
403 "TODO categories.")
404 (defvar todo-cats nil
405 "Old variable for holding the TODO categories.
406 Use `todo-categories' instead.")
407 (defvar todo-previous-line 0
408 "Previous line asked about.")
409 (defvar todo-previous-answer 0
410 "Previous answer got.")
411 (defvar todo-category-number 0
412 "TODO category number.")
413
414 (defvar todo-category-sep (make-string 75 ?-)
415 "Category separator.")
416 (defvar todo-category-beg " --- "
417 "Category start separator to be prepended onto category name.")
418 (defvar todo-category-end "--- End"
419 "Separator after a category.")
420 (defvar todo-header "-*- mode: todo; "
421 "Header of todo files.")
422
423 ;; ---------------------------------------------------------------------------
424
425 (defvar todo-mode-map
426 (let ((map (make-keymap)))
427 (suppress-keymap map t)
428 (define-key map "?" 'todo-help)
429 (define-key map " " 'todo-hide-show-subtree)
430 (define-key map "+" 'todo-forward-category)
431 (define-key map "-" 'todo-backward-category)
432 (define-key map "d" 'todo-file-item) ;done/delete
433 (define-key map "e" 'todo-edit-item)
434 (define-key map "E" 'todo-edit-multiline)
435 (define-key map "f" 'todo-file-item)
436 (define-key map "i" 'todo-insert-item)
437 (define-key map "I" 'todo-insert-item-here)
438 (define-key map "j" 'todo-jump-to-category)
439 (define-key map "k" 'todo-delete-item)
440 (define-key map "l" 'todo-lower-item)
441 (define-key map "n" 'todo-forward-item)
442 (define-key map "p" 'todo-backward-item)
443 (define-key map "P" 'todo-print)
444 (define-key map "q" 'todo-quit)
445 (define-key map "r" 'todo-raise-item)
446 (define-key map "s" 'todo-save)
447 (define-key map "S" 'todo-save-top-priorities)
448 (define-key map "t" 'todo-top-priorities)
449 map)
450 "TODO mode keymap.")
451
452 (defun todo-position (item list)
453 "Return the position of the element in LIST testing `equal' to ITEM.
454 Return nil if ITEM not found."
455 (let ((pos 0)
456 found)
457 (while list
458 (if (equal item (pop list))
459 (setq list nil
460 found pos)
461 (setq pos (1+ pos))))
462 found))
463
464 (defun todo-category-select ()
465 "Make TODO mode display the current category correctly."
466 (let ((name (nth todo-category-number todo-categories)))
467 (setq mode-line-buffer-identification
468 (concat "Category: " (format "%18s" name)))
469 (widen)
470 (goto-char (point-min))
471 (search-forward-regexp
472 (concat "^"
473 (regexp-quote (concat todo-prefix todo-category-beg name))
474 "$"))
475 (let ((begin (1+ (line-end-position))))
476 (search-forward-regexp (concat "^" todo-category-end))
477 (narrow-to-region begin (line-beginning-position))
478 (goto-char (point-min)))))
479 (defalias 'todo-cat-slct 'todo-category-select)
480
481 (defun todo-help () "Show TODO mode help."
482 (interactive)
483 (describe-function 'todo-mode))
484
485 (defun todo-hide-show-subtree ()
486 "Hide or Show subtrees in the TODO list."
487 (interactive)
488 (save-excursion
489 (end-of-line)
490 (if (outline-visible)
491 (hide-subtree)
492 (show-subtree))))
493
494 (defun todo-forward-category ()
495 "Go forward to TODO list of next category."
496 (interactive)
497 (setq todo-category-number
498 (mod (1+ todo-category-number) (length todo-categories)))
499 (todo-category-select))
500 (defalias 'todo-cmd-forw 'todo-forward-category)
501
502 (defun todo-backward-category ()
503 "Go back to TODO list of previous category."
504 (interactive)
505 (setq todo-category-number
506 (mod (1- todo-category-number) (length todo-categories)))
507 (todo-category-select))
508 (defalias 'todo-cmd-back 'todo-backward-category)
509
510 (defun todo-backward-item ()
511 "Select previous entry of TODO list."
512 (interactive)
513 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
514 (message ""))
515 (defalias 'todo-cmd-prev 'todo-backward-item)
516
517 (defun todo-forward-item (&optional count)
518 "Select COUNT-th next entry of TODO list."
519 (interactive "P")
520 (if (listp count) (setq count (car count)))
521 (end-of-line)
522 (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
523 nil 'goto-end count)
524 (beginning-of-line)
525 (message ""))
526 (defalias 'todo-cmd-next 'todo-forward-item)
527
528 (defun todo-save () "Save the TODO list."
529 (interactive)
530 (save-buffer)
531 (if todo-save-top-priorities-too (todo-save-top-priorities))
532 )
533 (defalias 'todo-cmd-save 'todo-save)
534
535 (defun todo-quit () "Done with TODO list for now."
536 (interactive)
537 (widen)
538 (todo-save)
539 (message "")
540 (bury-buffer))
541 (defalias 'todo-cmd-done 'todo-quit)
542
543 (defun todo-edit-item () "Edit current TODO list entry."
544 (interactive)
545 (let ((item (todo-item-string)))
546 (if (todo-string-multiline-p item)
547 (todo-edit-multiline)
548 (let ((new (read-from-minibuffer "Edit: " item)))
549 (todo-remove-item)
550 (insert new "\n")
551 (todo-backward-item)
552 (message "")))))
553 (defalias 'todo-cmd-edit 'todo-edit-item)
554
555 (defun todo-edit-multiline ()
556 "Set up a buffer for editing a multiline TODO list entry."
557 (interactive)
558 (let ((buffer-name (generate-new-buffer-name todo-edit-buffer)))
559 (switch-to-buffer
560 (make-indirect-buffer
561 (find-buffer-visiting todo-file-do) buffer-name))
562 (message "To exit, simply kill this buffer and return to list.")
563 (todo-edit-mode)
564 (narrow-to-region (todo-item-start) (todo-item-end))))
565
566 (defun todo-add-category (cat)
567 "Add new category CAT to the TODO list."
568 (interactive "sCategory: ")
569 (save-window-excursion
570 (add-to-list 'todo-categories cat)
571 (find-file todo-file-do)
572 (widen)
573 (goto-char (point-min))
574 (let ((posn (search-forward "-*- mode: todo; " 17 t)))
575 (if (not (null posn)) (goto-char posn))
576 (if (equal posn nil)
577 (progn
578 (insert "-*- mode: todo; \n")
579 (forward-char -1))
580 (kill-line)))
581 (insert (format "todo-categories: %S; -*-" todo-categories))
582 (forward-char 1)
583 (insert (format "%s%s%s\n%s\n%s %s\n"
584 todo-prefix todo-category-beg cat
585 todo-category-end
586 todo-prefix todo-category-sep))
587 (save-buffer))
588 0)
589
590 (defun todo-add-item-non-interactively (new-item category)
591 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
592 (save-excursion
593 (todo-show))
594 (save-excursion
595 (if (string= "" category)
596 (setq category (nth todo-category-number todo-categories)))
597 (setq todo-category-number
598 (or (todo-position category todo-categories)
599 (todo-add-category category)))
600 (todo-show)
601 (setq todo-previous-line 0)
602 (let ((top 1)
603 (bottom (1+ (count-lines (point-min) (point-max)))))
604 (while (> (- bottom top) todo-insert-threshold)
605 (let* ((current (/ (+ top bottom) 2))
606 (answer (if (< current bottom)
607 (todo-more-important-p current) nil)))
608 (if answer
609 (setq bottom current)
610 (setq top (1+ current)))))
611 (setq top (/ (+ top bottom) 2))
612 ;; goto-line doesn't have the desired behavior in a narrowed buffer
613 (goto-char (point-min))
614 (forward-line (1- top)))
615 (insert new-item "\n")
616 (todo-backward-item)
617 (progn ;;; horrible os10000 hack to make items appear when inserting into empty buffer
618 (widen)
619 (show-all)
620 (todo-forward-category)
621 (todo-backward-category))
622 (todo-save)
623 (message "")))
624
625 ;;;###autoload
626 (defun todo-insert-item (ARG)
627 "Insert new TODO list entry.
628 With a prefix argument solicit the category, otherwise use the current
629 category."
630 (interactive "P")
631 (save-excursion
632 (if (not (string-equal mode-name "TODO")) (todo-show))
633 (let* ((new-item (concat todo-prefix " "
634 (read-from-minibuffer
635 "New TODO entry: "
636 (if todo-entry-prefix-function
637 (funcall todo-entry-prefix-function)))))
638 (categories todo-categories)
639 (history (cons 'categories (1+ todo-category-number)))
640 (current-category (nth todo-category-number todo-categories))
641 (category
642 (if ARG
643 current-category
644 (completing-read
645 (concat "Category ["
646 current-category "]: ")
647 (todo-category-alist) nil nil nil history))))
648 (todo-add-item-non-interactively new-item category))))
649
650 (defalias 'todo-cmd-inst 'todo-insert-item)
651
652 (defun todo-insert-item-here ()
653 "Insert new TODO list entry under the cursor."
654 (interactive "")
655 (save-excursion
656 (if (not (string-equal mode-name "TODO")) (todo-show))
657 (let* ((new-item (concat todo-prefix " "
658 (read-from-minibuffer
659 "New TODO entry: "
660 (if todo-entry-prefix-function
661 (funcall todo-entry-prefix-function))))))
662 (insert (concat new-item "\n")))))
663
664 (defun todo-more-important-p (line)
665 "Ask whether entry is more important than the one at LINE."
666 (if (not (equal todo-previous-line line))
667 (progn
668 (setq todo-previous-line line)
669 (goto-char (point-min))
670 (forward-line (1- todo-previous-line))
671 (let ((item (todo-item-string-start)))
672 (setq todo-previous-answer
673 (y-or-n-p (concat "More important than '" item "'? "))))))
674 todo-previous-answer)
675 (defalias 'todo-ask-p 'todo-more-important-p)
676
677 (defun todo-delete-item () "Delete current TODO list entry."
678 (interactive)
679 (if (> (count-lines (point-min) (point-max)) 0)
680 (let* ((todo-entry (todo-item-string-start))
681 (todo-answer (y-or-n-p (concat "Permanently remove '"
682 todo-entry "'? "))))
683 (if todo-answer
684 (progn
685 (todo-remove-item)
686 (todo-backward-item)))
687 (message ""))
688 (error "No TODO list entry to delete")))
689 (defalias 'todo-cmd-kill 'todo-delete-item)
690
691 (defun todo-raise-item () "Raise priority of current entry."
692 (interactive)
693 (if (> (count-lines (point-min) (point)) 0)
694 (let ((item (todo-item-string)))
695 (todo-remove-item)
696 (todo-backward-item)
697 (save-excursion
698 (insert item "\n"))
699 (message ""))
700 (error "No TODO list entry to raise")))
701 (defalias 'todo-cmd-raise 'todo-raise-item)
702
703 (defun todo-lower-item () "Lower priority of current entry."
704 (interactive)
705 (if (> (count-lines (point) (point-max)) 1)
706 ;; Assume there is a final newline
707 (let ((item (todo-item-string)))
708 (todo-remove-item)
709 (todo-forward-item)
710 (save-excursion
711 (insert item "\n"))
712 (message ""))
713 (error "No TODO list entry to lower")))
714 (defalias 'todo-cmd-lowr 'todo-lower-item)
715
716 (defun todo-file-item (&optional comment)
717 "File the current TODO list entry away, annotated with an optional COMMENT."
718 (interactive "sComment: ")
719 (or (> (count-lines (point-min) (point-max)) 0)
720 (error "No TODO list entry to file away"))
721 (let ((time-stamp-format todo-time-string-format))
722 (if (and comment (> (length comment) 0))
723 (progn
724 (goto-char (todo-item-end))
725 (insert
726 (if (save-excursion (beginning-of-line)
727 (looking-at (regexp-quote todo-prefix)))
728 " "
729 "\n\t")
730 "(" comment ")")))
731 (goto-char (todo-item-end))
732 (insert " [" (nth todo-category-number todo-categories) "]")
733 (goto-char (todo-item-start))
734 (let ((temp-point (point)))
735 (if (looking-at (regexp-quote todo-prefix))
736 (replace-match (time-stamp-string))
737 ;; Standard prefix -> timestamp
738 ;; Else prefix non-standard item start with timestamp
739 (insert (time-stamp-string)))
740 (append-to-file temp-point (1+ (todo-item-end)) todo-file-done)
741 (delete-region temp-point (1+ (todo-item-end))))
742 (todo-backward-item)
743 (message "")))
744
745 ;; ---------------------------------------------------------------------------
746
747 ;; Utility functions:
748
749 (defun todo-top-priorities (&optional nof-priorities category-pr-page)
750 "List top priorities for each category.
751
752 Number of entries for each category is given by NOF-PRIORITIES which
753 defaults to \'todo-show-priorities\'.
754
755 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
756 between each category."
757
758 (interactive "P")
759 (or nof-priorities (setq nof-priorities todo-show-priorities))
760 (if (listp nof-priorities) ;universal argument
761 (setq nof-priorities (car nof-priorities)))
762 (let ((todo-print-buffer-name " *todo-tmp*")
763 ;;(todo-print-category-number 0)
764 (todo-category-break (if category-pr-page "\f" ""))
765 (cat-end
766 (concat
767 (if todo-remove-separator
768 (concat todo-category-end "\n"
769 (regexp-quote todo-prefix) " " todo-category-sep "\n")
770 (concat todo-category-end "\n"))))
771 beg end)
772 (todo-show)
773 (save-excursion
774 (save-restriction
775 (widen)
776 (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
777 (set-buffer todo-print-buffer-name)
778 (goto-char (point-min))
779 (if (re-search-forward (regexp-quote todo-header) nil t)
780 (progn
781 (beginning-of-line 1)
782 (kill-line))) ;Remove mode line
783 (while (re-search-forward ;Find category start
784 (regexp-quote (concat todo-prefix todo-category-beg))
785 nil t)
786 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
787 (re-search-forward cat-end nil t)
788 (setq end (match-beginning 0))
789 (replace-match todo-category-break)
790 (narrow-to-region beg end) ;In case we have too few entries.
791 (goto-char (point-min))
792 (if (= 0 nof-priorities) ;Traverse entries.
793 (goto-char end) ;All entries
794 (todo-forward-item nof-priorities))
795 (setq beg (point))
796 (delete-region beg end)
797 (widen))
798 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
799 (goto-char (point-min)) ;Due to display buffer
800 ))
801 ;; Could have used switch-to-buffer as it has a norecord argument,
802 ;; which is nice when we are called from e.g. todo-print.
803 ;; Else we could have used pop-to-buffer.
804 (display-buffer todo-print-buffer-name)
805 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
806 todo-print-buffer-name)))
807
808 (defun todo-save-top-priorities (&optional nof-priorities)
809 "Save top priorities for each category in `todo-file-top'.
810
811 Number of entries for each category is given by NOF-PRIORITIES which
812 defaults to `todo-show-priorities'."
813 (interactive "P")
814 (with-temp-buffer
815 (todo-top-priorities nof-priorities)
816 (write-file todo-file-top)))
817
818 ;;;###autoload
819 (defun todo-print (&optional category-pr-page)
820 "Print todo summary using `todo-print-function'.
821 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
822 between each category.
823
824 Number of entries for each category is given by
825 \'todo-print-priorities\'."
826 (interactive "P")
827 (with-temp-buffer
828 (todo-top-priorities todo-print-priorities
829 category-pr-page)
830 (funcall todo-print-function)
831 (message "Todo printing done.")))
832
833 (defun todo-jump-to-category ()
834 "Jump to a category. Default is previous category."
835 (interactive)
836 (let* ((categories todo-categories)
837 (history (cons 'categories (1+ todo-category-number)))
838 (category (completing-read
839 (concat "Category ["
840 (nth todo-category-number todo-categories) "]: ")
841 (todo-category-alist) nil nil nil history)))
842 (if (string= "" category)
843 (setq category (nth todo-category-number todo-categories)))
844 (setq todo-category-number
845 (or (todo-position category todo-categories)
846 (todo-add-category category)))
847 (todo-show)))
848
849 (defun todo-line-string ()
850 "Return current line in buffer as a string."
851 (buffer-substring (line-beginning-position) (line-end-position)))
852
853 (defun todo-item-string-start ()
854 "Return the start of this TODO list entry as a string."
855 ;; Suitable for putting in the minibuffer when asking the user
856 (let ((item (todo-item-string)))
857 (if (> (length item) 60)
858 (setq item (concat (substring item 0 56) "...")))
859 item))
860
861 (defun todo-item-start ()
862 "Return point at start of current TODO list item."
863 (save-excursion
864 (beginning-of-line)
865 (if (not (looking-at (regexp-quote todo-prefix)))
866 (search-backward-regexp
867 (concat "^" (regexp-quote todo-prefix)) nil t))
868 (point)))
869
870 (defun todo-item-end ()
871 "Return point at end of current TODO list item."
872 (save-excursion
873 (end-of-line)
874 (search-forward-regexp
875 (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
876 (1- (line-beginning-position))))
877
878 (defun todo-remove-item ()
879 "Delete the current entry from the TODO list."
880 (delete-region (todo-item-start) (1+ (todo-item-end))))
881
882 (defun todo-item-string () "Return current TODO list entry as a string."
883 (buffer-substring (todo-item-start) (todo-item-end)))
884
885 (defun todo-string-count-lines (string)
886 "Return the number of lines STRING spans."
887 (length (split-string string "\n")))
888
889 (defun todo-string-multiline-p (string)
890 "Return non-nil if STRING spans several lines."
891 (> (todo-string-count-lines string) 1))
892
893 (defun todo-category-alist ()
894 "Generate an alist for use in `completing-read' from `todo-categories'."
895 (mapcar #'list todo-categories))
896
897 ;; ---------------------------------------------------------------------------
898
899 (easy-menu-define
900 todo-menu todo-mode-map "Todo Menu"
901 '("Todo"
902 ["Next category" todo-forward-category t]
903 ["Previous category" todo-backward-category t]
904 ["Jump to category" todo-jump-to-category t]
905 ["Show top priority items" todo-top-priorities t]
906 ["Print categories" todo-print t]
907 "---"
908 ["Edit item" todo-edit-item t]
909 ["File item" todo-file-item t]
910 ["Insert new item" todo-insert-item t]
911 ["Insert item here" todo-insert-item-here t]
912 ["Kill item" todo-delete-item t]
913 "---"
914 ["Lower item priority" todo-lower-item t]
915 ["Raise item priority" todo-raise-item t]
916 "---"
917 ["Next item" todo-forward-item t]
918 ["Previous item" todo-backward-item t]
919 "---"
920 ["Save" todo-save t]
921 ["Save Top Priorities" todo-save-top-priorities t]
922 "---"
923 ["Quit" todo-quit t]
924 ))
925
926 ;; As calendar reads .todo-do before todo-mode is loaded.
927 ;;;###autoload
928 (defun todo-mode ()
929 "Major mode for editing TODO lists.
930
931 \\{todo-mode-map}"
932 (interactive)
933 (setq major-mode 'todo-mode)
934 (setq mode-name "TODO")
935 (use-local-map todo-mode-map)
936 (easy-menu-add todo-menu)
937 (setq fill-prefix "\t\t")
938 (let ((prefix (regexp-quote todo-prefix)))
939 (setq paragraph-separate prefix)
940 (setq outline-regexp prefix))
941 (outline-minor-mode 1)
942 (goto-char (point-min))
943 (outline-next-heading) ; get past -*- line
944 (hide-other)
945 (auto-fill-mode 1)
946 (run-hooks 'todo-mode-hook))
947
948 ;; Read about this function in the setup instructions above!
949 ;;;###autoload
950 (defun todo-cp ()
951 "Make a diary entry appear only in the current date's diary."
952 (if (equal (calendar-current-date) date)
953 entry))
954
955 (defun todo-edit-mode ()
956 "Major mode for editing items in the TODO list.
957
958 \\{todo-edit-mode-map}"
959 (text-mode)
960 (setq major-mode 'todo-edit-mode)
961 (setq mode-name "TODO Edit")
962 (run-hooks 'todo-edit-mode-hook))
963
964 ;;;###autoload
965 (defun todo-show ()
966 "Show TODO list."
967 (interactive)
968 (if (file-exists-p todo-file-do)
969 (find-file todo-file-do)
970 (todo-initial-setup))
971 (if (null todo-categories)
972 (if (null todo-cats)
973 (error "Error in %s: No categories in list `todo-categories'"
974 todo-file-do)
975 (goto-char (point-min))
976 (and (search-forward "todo-cats:" nil t)
977 (replace-match "todo-categories:"))
978 (make-local-variable 'todo-categories)
979 (setq todo-categories todo-cats)))
980 (beginning-of-line)
981 (todo-category-select))
982
983 (defun todo-initial-setup ()
984 "Set up things to work properly in TODO mode."
985 (find-file todo-file-do)
986 (erase-buffer)
987 (todo-mode)
988 (todo-add-category "Todo"))
989
990 (provide 'todo-mode)
991
992 ;; ---------------------------------------------------------------------------
993 ;;; todo-mode.el ends here
994 ;; ---------------------------------------------------------------------------