New maintainer address.
[bpt/emacs.git] / lisp / calendar / todo-mode.el
CommitLineData
01b864bc 1;; todo-mode.el -- Major mode for editing TODO list files
3cb152f9 2
a554b301 3;; Copyright (C) 1997, 2000 Free Software Foundation, Inc.
3cb152f9 4
346181f0 5;; Author: os10000@seidel-space.de
7c896f63 6;; Created: 2 Aug 1997
346181f0 7;; Version: $Id: todo-mode.el,v 1.40 2000/06/02 18:37:43 fx Exp $
7c896f63 8;; Keywords: Categorised TODO list editor, todo-mode
3cb152f9 9
7c896f63 10;; This file is part of GNU Emacs.
595b2334 11
7c896f63 12;; GNU Emacs is free software; you can redistribute it and/or modify
595b2334
OS
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.
7c896f63
OS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
595b2334
OS
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.
7c896f63 21
595b2334
OS
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
7c896f63
OS
29;;; Commentary:
30
85b3b166
OS
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;;
01b864bc 50;; Preface, Quickstart Installation
595b2334 51;;
e4541b67 52;; To get this to work, make emacs execute the line
3d8105fb 53;;
e4541b67 54;; (autoload 'todo-mode "todo-mode"
4cbc9d5a
OS
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)
7c896f63 60;;
e4541b67
OS
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;;
dad8ca4c
OS
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;;
e4541b67
OS
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
01b864bc 97;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
7c896f63 98;;
e4541b67
OS
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;;
5d035cad
OS
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;;
e4541b67 116;; Version
7c896f63 117;;
e4541b67 118;; Which version of todo-mode.el does this documentation refer to?
579e1c67 119;;
346181f0 120;; $Id: todo-mode.el,v 1.40 2000/06/02 18:37:43 fx Exp $
01b864bc 121;;
e4541b67 122;; Operation
01b864bc
OS
123;;
124;; You will have the following facilities available:
125;;
126;; M-x todo-show will enter the todo list screen, here type
127;;
bd40f1f2
OS
128;; spc will toggle the display of sub-trees
129;;
01b864bc
OS
130;; + to go to next category
131;; - to go to previous category
e4541b67
OS
132;; d to file the current entry, including a
133;; comment and timestamp
01b864bc 134;; e to edit the current entry
1966902e 135;; E to edit a multi-line entry
01b864bc
OS
136;; f to file the current entry, including a
137;; comment and timestamp
4dc1a160 138;; i to insert a new entry, with prefix, omit category
1966902e 139;; I to insert a new entry at current cursor position
e4541b67 140;; j jump to category
01b864bc
OS
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
e4541b67 145;; P print
01b864bc
OS
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
f1757bdd 149;; S to save the list of top priorities
85b3b166 150;; t show top priority items for each category
01b864bc 151;;
e4541b67
OS
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.
01b864bc
OS
160;;
161;; Configuration
162;;
163;; Variable todo-prefix
164;;
165;; I would like to recommend that you use the prefix "*/*" (by
e4541b67
OS
166;; leaving the variable 'todo-prefix' untouched) so that the
167;; diary displays each entry every day.
01b864bc 168;;
e4541b67
OS
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.
01b864bc 173;;
e4541b67
OS
174;; If you have the diary package set up to usually display more
175;; than one day's entries at once, consider using
01b864bc
OS
176;;
177;; "&%%(equal (calendar-current-date) date)"
178;;
e4541b67
OS
179;; as the value of `todo-prefix'. Please note that this may slow
180;; down the processing of your diary file some.
01b864bc 181;;
5d035cad
OS
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;;
01b864bc
OS
193;; Variable todo-file-do
194;;
e4541b67
OS
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.
01b864bc
OS
198;;
199;; Variable todo-file-done
200;;
e4541b67
OS
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.
01b864bc 206;;
f1757bdd
OS
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;;
01b864bc
OS
213;; Variable todo-mode-hook
214;;
e4541b67
OS
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
85b3b166 232;; that point. If you set the threshhold to, e.g. 8, it will stop
e4541b67
OS
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!
01b864bc
OS
239;;
240;; Things to do
241;;
e4541b67
OS
242;; These originally were my ideas, but now also include all the
243;; suggestions that I included before forgetting them:
244;;
e4541b67
OS
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
dad8ca4c 255;; the code, like the completing-read lines of code.
e4541b67 256;; o license / version function
01b864bc
OS
257;; o export to diary file
258;; o todo-report-bug
259;; o GNATS support
e4541b67
OS
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
01b864bc 266;;
e4541b67 267;; History and Gossip
01b864bc 268;;
e4541b67
OS
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!
01b864bc 272;;
e4541b67
OS
273;; Enjoy this package and express your gratitude by sending nice
274;; things to my parents' address!
01b864bc
OS
275;;
276;; Oliver Seidel
e4541b67 277;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
7e6ed9b9 278
7c896f63
OS
279;;; Code:
280
9278c60d
DL
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")
85b3b166 288
3cb152f9
OS
289;; User-configurable variables:
290
85b3b166
OS
291(defgroup todo nil
292 "Maintain a list of todo items."
a554b301 293 :version "21.1"
85b3b166
OS
294 :group 'calendar)
295
a554b301 296(defcustom todo-prefix "*/*"
85b3b166
OS
297 "*TODO mode prefix for entries.
298
299This is useful in conjunction with `calendar' and `diary' if you use
300
301#include \"~/.todo-do\"
302
303in your diary file to include your todo list file as part of your
304diary. With the default value \"*/*\" the diary displays each entry
305every day and it may also be marked on every day of the calendar.
306Using \"&%%(equal (calendar-current-date) date)\" instead will only
307show and mark todo entreis for today, but may slow down processing of
308the diary file somewhat."
309 :type 'string
310 :group 'todo)
a554b301 311(defcustom todo-file-do "~/.todo-do"
85b3b166
OS
312 "*TODO mode list file."
313 :type 'file
314 :group 'todo)
a554b301 315(defcustom todo-file-done "~/.todo-done"
85b3b166
OS
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
330If you have 8 items in your TODO list, then you may get asked 4
331questions by the binary insertion algorithm. However, you may not
332really have a need for such accurate priorities amongst your TODO
333items. If you now think about the binary insertion halfing the size
334of the window each time, then the threshhold is the window size at
335which it will stop. If you set the threshhold to zero, the upper and
336lower bound will coincide at the end of the loop and you will insert
337your item just before that point. If you set the threshhold to,
338e.g. 8, it will stop as soon as the window size drops below that
339amount and will insert the item in the approximate centre of that
340window."
341 :type 'integer
342 :group 'todo)
01b864bc 343(defvar todo-edit-buffer " *TODO Edit*" "TODO Edit buffer name.")
85b3b166 344(defcustom todo-file-top "~/.todo-top"
f1757bdd 345 "*TODO mode top priorities file.
85b3b166 346
f1757bdd 347Not in TODO format, but diary compatible.
85b3b166
OS
348Automatically 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].
3580 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].
3630 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
a554b301 372 "*Non-nil makes `todo-save' automatically save top-priorities in `todo-file-top'."
85b3b166
OS
373 :type 'boolean
374 :group 'todo)
e4541b67 375
7c896f63
OS
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;;
85b3b166 379(defcustom todo-time-string-format
bd40f1f2 380 "%04y-%02m-%02d %02H:%02M"
85b3b166
OS
381 "*TODO mode time string format for done entries.
382For 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)
e4541b67
OS
394
395(defun todo-entry-timestamp-initials ()
f1757bdd 396 "Prepend timestamp and your initials to the head of a TODO entry."
e4541b67
OS
397 (let ((time-stamp-format todo-time-string-format))
398 (concat (time-stamp-string) " " todo-initials ": ")))
399
cf1ebf43
OS
400;; Set up some helpful context ...
401
9278c60d
DL
402(defvar todo-categories nil
403 "TODO categories.")
404(defvar todo-cats nil
85b3b166
OS
405 "Old variable for holding the TODO categories.
406Use `todo-categories' instead.")
9278c60d
DL
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.")
e4541b67
OS
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
cf1ebf43
OS
423;; ---------------------------------------------------------------------------
424
9278c60d 425(defvar todo-mode-map
3d8105fb
OS
426 (let ((map (make-keymap)))
427 (suppress-keymap map t)
bd40f1f2
OS
428 (define-key map "?" 'todo-help)
429 (define-key map " " 'todo-hide-show-subtree)
7f6241ea
OS
430 (define-key map "+" 'todo-forward-category)
431 (define-key map "-" 'todo-backward-category)
1966902e 432 (define-key map "d" 'todo-file-item) ;done/delete
7f6241ea
OS
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)
d145aa83 437 (define-key map "I" 'todo-insert-item-here)
e4541b67 438 (define-key map "j" 'todo-jump-to-category)
7f6241ea
OS
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)
e4541b67 443 (define-key map "P" 'todo-print)
7f6241ea
OS
444 (define-key map "q" 'todo-quit)
445 (define-key map "r" 'todo-raise-item)
446 (define-key map "s" 'todo-save)
f1757bdd 447 (define-key map "S" 'todo-save-top-priorities)
e4541b67 448 (define-key map "t" 'todo-top-priorities)
9278c60d
DL
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.
454Return 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))
3cb152f9 463
7f6241ea
OS
464(defun todo-category-select ()
465 "Make TODO mode display the current category correctly."
466 (let ((name (nth todo-category-number todo-categories)))
579e1c67 467 (setq mode-line-buffer-identification
6fe3681b 468 (concat "Category: " (format "%18s" name)))
da2ee685
OS
469 (widen)
470 (goto-char (point-min))
7f6241ea 471 (search-forward-regexp
e4541b67
OS
472 (concat "^"
473 (regexp-quote (concat todo-prefix todo-category-beg name))
474 "$"))
9278c60d 475 (let ((begin (1+ (line-end-position))))
e4541b67 476 (search-forward-regexp (concat "^" todo-category-end))
9278c60d 477 (narrow-to-region begin (line-beginning-position))
7f6241ea
OS
478 (goto-char (point-min)))))
479(defalias 'todo-cat-slct 'todo-category-select)
480
bd40f1f2
OS
481(defun todo-help () "Show TODO mode help."
482 (interactive)
483 (describe-function 'todo-mode))
484
9278c60d
DL
485(defun todo-hide-show-subtree ()
486 "Hide or Show subtrees in the TODO list."
bd40f1f2
OS
487 (interactive)
488 (save-excursion
489 (end-of-line)
490 (if (outline-visible)
491 (hide-subtree)
492 (show-subtree))))
493
9278c60d
DL
494(defun todo-forward-category ()
495 "Go forward to TODO list of next category."
da2ee685 496 (interactive)
7f6241ea 497 (setq todo-category-number
01b864bc 498 (mod (1+ todo-category-number) (length todo-categories)))
7f6241ea
OS
499 (todo-category-select))
500(defalias 'todo-cmd-forw 'todo-forward-category)
da2ee685 501
9278c60d
DL
502(defun todo-backward-category ()
503 "Go back to TODO list of previous category."
da2ee685 504 (interactive)
7f6241ea 505 (setq todo-category-number
01b864bc 506 (mod (1- todo-category-number) (length todo-categories)))
7f6241ea
OS
507 (todo-category-select))
508(defalias 'todo-cmd-back 'todo-backward-category)
da2ee685 509
9278c60d
DL
510(defun todo-backward-item ()
511 "Select previous entry of TODO list."
3cb152f9 512 (interactive)
7f6241ea 513 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
cf1ebf43 514 (message ""))
7f6241ea 515(defalias 'todo-cmd-prev 'todo-backward-item)
3cb152f9 516
e4541b67 517(defun todo-forward-item (&optional count)
85b3b166 518 "Select COUNT-th next entry of TODO list."
e4541b67
OS
519 (interactive "P")
520 (if (listp count) (setq count (car count)))
7f6241ea 521 (end-of-line)
e4541b67
OS
522 (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
523 nil 'goto-end count)
7f6241ea 524 (beginning-of-line)
cf1ebf43 525 (message ""))
7f6241ea 526(defalias 'todo-cmd-next 'todo-forward-item)
3cb152f9 527
7f6241ea 528(defun todo-save () "Save the TODO list."
da2ee685 529 (interactive)
f1757bdd
OS
530 (save-buffer)
531 (if todo-save-top-priorities-too (todo-save-top-priorities))
532 )
7f6241ea 533(defalias 'todo-cmd-save 'todo-save)
da2ee685 534
7f6241ea 535(defun todo-quit () "Done with TODO list for now."
3cb152f9 536 (interactive)
da2ee685 537 (widen)
f1757bdd 538 (todo-save)
7e6ed9b9 539 (message "")
cf1ebf43 540 (bury-buffer))
7f6241ea 541(defalias 'todo-cmd-done 'todo-quit)
3cb152f9 542
7f6241ea 543(defun todo-edit-item () "Edit current TODO list entry."
3cb152f9 544 (interactive)
7f6241ea
OS
545 (let ((item (todo-item-string)))
546 (if (todo-string-multiline-p item)
01b864bc 547 (todo-edit-multiline)
7f6241ea 548 (let ((new (read-from-minibuffer "Edit: " item)))
01b864bc
OS
549 (todo-remove-item)
550 (insert new "\n")
551 (todo-backward-item)
552 (message "")))))
7f6241ea
OS
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)))
e4541b67
OS
559 (switch-to-buffer
560 (make-indirect-buffer
bd40f1f2 561 (find-buffer-visiting todo-file-do) buffer-name))
2186b974 562 (message "To exit, simply kill this buffer and return to list.")
7f6241ea
OS
563 (todo-edit-mode)
564 (narrow-to-region (todo-item-start) (todo-item-end))))
3cb152f9 565
dad8ca4c 566(defun todo-add-category (cat)
85b3b166 567 "Add new category CAT to the TODO list."
6b04f517 568 (interactive "sCategory: ")
3cb152f9 569 (save-window-excursion
a554b301 570 (add-to-list 'todo-categories cat)
3cb152f9 571 (find-file todo-file-do)
da2ee685
OS
572 (widen)
573 (goto-char (point-min))
574 (let ((posn (search-forward "-*- mode: todo; " 17 t)))
575 (if (not (null posn)) (goto-char posn))
579e1c67 576 (if (equal posn nil)
01b864bc
OS
577 (progn
578 (insert "-*- mode: todo; \n")
579 (forward-char -1))
580 (kill-line)))
7f6241ea 581 (insert (format "todo-categories: %S; -*-" todo-categories))
da2ee685 582 (forward-char 1)
e4541b67
OS
583 (insert (format "%s%s%s\n%s\n%s %s\n"
584 todo-prefix todo-category-beg cat
dad8ca4c 585 todo-category-end
bd40f1f2
OS
586 todo-prefix todo-category-sep))
587 (save-buffer))
cf1ebf43 588 0)
da2ee685 589
4dc1a160 590(defun todo-add-item-non-interactively (new-item category)
85b3b166 591 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
4cbc9d5a 592 (save-excursion
49b2ae0e
OS
593 (todo-show))
594 (save-excursion
e4541b67
OS
595 (if (string= "" category)
596 (setq category (nth todo-category-number todo-categories)))
bd40f1f2 597 (setq todo-category-number
9278c60d 598 (or (todo-position category todo-categories)
bd40f1f2 599 (todo-add-category category)))
4dc1a160
OS
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)))
7f6241ea
OS
615 (insert new-item "\n")
616 (todo-backward-item)
bd40f1f2
OS
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))
f1757bdd 622 (todo-save)
3d8105fb 623 (message "")))
4cbc9d5a 624
6c7331ff 625;;;###autoload
4cbc9d5a 626(defun todo-insert-item (ARG)
85b3b166
OS
627 "Insert new TODO list entry.
628With a prefix argument solicit the category, otherwise use the current
629category."
4cbc9d5a 630 (interactive "P")
49b2ae0e 631 (save-excursion
1966902e
OS
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))
dad8ca4c 641 (category
1966902e
OS
642 (if ARG
643 current-category
dad8ca4c 644 (completing-read
1966902e
OS
645 (concat "Category ["
646 current-category "]: ")
647 (todo-category-alist) nil nil nil history))))
4dc1a160 648 (todo-add-item-non-interactively new-item category))))
4cbc9d5a 649
7f6241ea 650(defalias 'todo-cmd-inst 'todo-insert-item)
3d8105fb 651
d145aa83
OS
652(defun todo-insert-item-here ()
653 "Insert new TODO list entry under the cursor."
4dc1a160
OS
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")))))
d145aa83 663
01b864bc 664(defun todo-more-important-p (line)
7f6241ea
OS
665 "Ask whether entry is more important than the one at LINE."
666 (if (not (equal todo-previous-line line))
3d8105fb 667 (progn
7f6241ea 668 (setq todo-previous-line line)
3d8105fb 669 (goto-char (point-min))
7f6241ea 670 (forward-line (1- todo-previous-line))
01b864bc
OS
671 (let ((item (todo-item-string-start)))
672 (setq todo-previous-answer
673 (y-or-n-p (concat "More important than '" item "'? "))))))
7f6241ea
OS
674 todo-previous-answer)
675(defalias 'todo-ask-p 'todo-more-important-p)
676
677(defun todo-delete-item () "Delete current TODO list entry."
3cb152f9
OS
678 (interactive)
679 (if (> (count-lines (point-min) (point-max)) 0)
7f6241ea 680 (let* ((todo-entry (todo-item-string-start))
01b864bc
OS
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 ""))
7f6241ea
OS
688 (error "No TODO list entry to delete")))
689(defalias 'todo-cmd-kill 'todo-delete-item)
3cb152f9 690
7f6241ea 691(defun todo-raise-item () "Raise priority of current entry."
8cdc3b3d 692 (interactive)
7f6241ea
OS
693 (if (> (count-lines (point-min) (point)) 0)
694 (let ((item (todo-item-string)))
01b864bc
OS
695 (todo-remove-item)
696 (todo-backward-item)
697 (save-excursion
698 (insert item "\n"))
699 (message ""))
7f6241ea 700 (error "No TODO list entry to raise")))
a554b301 701(defalias 'todo-cmd-raise 'todo-raise-item)
8cdc3b3d 702
7f6241ea 703(defun todo-lower-item () "Lower priority of current entry."
8cdc3b3d 704 (interactive)
e4541b67
OS
705 (if (> (count-lines (point) (point-max)) 1)
706 ;; Assume there is a final newline
7f6241ea 707 (let ((item (todo-item-string)))
01b864bc
OS
708 (todo-remove-item)
709 (todo-forward-item)
710 (save-excursion
711 (insert item "\n"))
712 (message ""))
7f6241ea
OS
713 (error "No TODO list entry to lower")))
714(defalias 'todo-cmd-lowr 'todo-lower-item)
8cdc3b3d 715
0561decb 716(defun todo-file-item (&optional comment)
a554b301 717 "File the current TODO list entry away, annotated with an optional COMMENT."
0561decb
OS
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 "")))
3cb152f9
OS
744
745;; ---------------------------------------------------------------------------
746
7f6241ea
OS
747;; Utility functions:
748
e4541b67
OS
749(defun todo-top-priorities (&optional nof-priorities category-pr-page)
750 "List top priorities for each category.
751
752Number of entries for each category is given by NOF-PRIORITIES which
753defaults to \'todo-show-priorities\'.
754
755If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
756between 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)))
a554b301 762 (let ((todo-print-buffer-name " *todo-tmp*")
85b3b166
OS
763 ;;(todo-print-category-number 0)
764 (todo-category-break (if category-pr-page "\f" ""))
e4541b67
OS
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)
85b3b166
OS
776 (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
777 (set-buffer todo-print-buffer-name)
e4541b67
OS
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)
9278c60d 786 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
e4541b67
OS
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))
f1757bdd 798 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
e4541b67
OS
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.
f1757bdd 803 ;; Else we could have used pop-to-buffer.
85b3b166 804 (display-buffer todo-print-buffer-name)
e4541b67 805 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
9278c60d 806 todo-print-buffer-name)))
e4541b67 807
f1757bdd
OS
808(defun todo-save-top-priorities (&optional nof-priorities)
809 "Save top priorities for each category in `todo-file-top'.
810
811Number of entries for each category is given by NOF-PRIORITIES which
812defaults to `todo-show-priorities'."
813 (interactive "P")
9278c60d
DL
814 (with-temp-buffer
815 (todo-top-priorities nof-priorities)
816 (write-file todo-file-top)))
f1757bdd 817
e4541b67
OS
818;;;###autoload
819(defun todo-print (&optional category-pr-page)
a554b301 820 "Print todo summary using `todo-print-function'.
e4541b67
OS
821If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
822between each category.
823
824Number of entries for each category is given by
825\'todo-print-priorities\'."
826 (interactive "P")
9278c60d
DL
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.")))
e4541b67
OS
832
833(defun todo-jump-to-category ()
834 "Jump to a category. Default is previous category."
835 (interactive)
836 (let* ((categories todo-categories)
bd40f1f2 837 (history (cons 'categories (1+ todo-category-number)))
dad8ca4c 838 (category (completing-read
e4541b67
OS
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
9278c60d 845 (or (todo-position category todo-categories)
bd40f1f2 846 (todo-add-category category)))
e4541b67
OS
847 (todo-show)))
848
9278c60d
DL
849(defun todo-line-string ()
850 "Return current line in buffer as a string."
851 (buffer-substring (line-beginning-position) (line-end-position)))
7f6241ea
OS
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)
01b864bc 858 (setq item (concat (substring item 0 56) "...")))
7f6241ea
OS
859 item))
860
9278c60d
DL
861(defun todo-item-start ()
862 "Return point at start of current TODO list item."
7f6241ea
OS
863 (save-excursion
864 (beginning-of-line)
865 (if (not (looking-at (regexp-quote todo-prefix)))
01b864bc
OS
866 (search-backward-regexp
867 (concat "^" (regexp-quote todo-prefix)) nil t))
7f6241ea
OS
868 (point)))
869
9278c60d
DL
870(defun todo-item-end ()
871 "Return point at end of current TODO list item."
7f6241ea
OS
872 (save-excursion
873 (end-of-line)
e4541b67
OS
874 (search-forward-regexp
875 (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
9278c60d 876 (1- (line-beginning-position))))
7f6241ea 877
9278c60d
DL
878(defun todo-remove-item ()
879 "Delete the current entry from the TODO list."
7f6241ea
OS
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)
85b3b166 890 "Return non-nil if STRING spans several lines."
7f6241ea
OS
891 (> (todo-string-count-lines string) 1))
892
893(defun todo-category-alist ()
85b3b166 894 "Generate an alist for use in `completing-read' from `todo-categories'."
a554b301 895 (mapcar #'list todo-categories))
7f6241ea 896
da2ee685
OS
897;; ---------------------------------------------------------------------------
898
a554b301
DL
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 ))
a360be79 925
e4541b67 926;; As calendar reads .todo-do before todo-mode is loaded.
6c7331ff 927;;;###autoload
a554b301
DL
928(defun todo-mode ()
929 "Major mode for editing TODO lists.
930
931\\{todo-mode-map}"
3cb152f9 932 (interactive)
3cb152f9
OS
933 (setq major-mode 'todo-mode)
934 (setq mode-name "TODO")
935 (use-local-map todo-mode-map)
49c48a1b 936 (easy-menu-add todo-menu)
bd40f1f2 937 (setq fill-prefix "\t\t")
a554b301
DL
938 (let ((prefix (regexp-quote todo-prefix)))
939 (setq paragraph-separate prefix)
940 (setq outline-regexp prefix))
bd40f1f2 941 (outline-minor-mode 1)
a554b301
DL
942 (goto-char (point-min))
943 (outline-next-heading) ; get past -*- line
bd40f1f2
OS
944 (hide-other)
945 (auto-fill-mode 1)
49c48a1b 946 (run-hooks 'todo-mode-hook))
da2ee685 947
5d035cad 948;; Read about this function in the setup instructions above!
6c7331ff 949;;;###autoload
5d035cad 950(defun todo-cp ()
a554b301 951 "Make a diary entry appear only in the current date's diary."
5d035cad 952 (if (equal (calendar-current-date) date)
a554b301 953 entry))
5d035cad 954
7f6241ea 955(defun todo-edit-mode ()
a554b301
DL
956 "Major mode for editing items in the TODO list.
957
958\\{todo-edit-mode-map}"
7f6241ea
OS
959 (text-mode)
960 (setq major-mode 'todo-edit-mode)
961 (setq mode-name "TODO Edit")
962 (run-hooks 'todo-edit-mode-hook))
963
6c7331ff 964;;;###autoload
a554b301
DL
965(defun todo-show ()
966 "Show TODO list."
da2ee685 967 (interactive)
7f6241ea
OS
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)
01b864bc
OS
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:"))
e05e3850 978 (make-local-variable 'todo-categories)
01b864bc 979 (setq todo-categories todo-cats)))
7f6241ea
OS
980 (beginning-of-line)
981 (todo-category-select))
982
a554b301
DL
983(defun todo-initial-setup ()
984 "Set up things to work properly in TODO mode."
da2ee685 985 (find-file todo-file-do)
7f6241ea
OS
986 (erase-buffer)
987 (todo-mode)
988 (todo-add-category "Todo"))
3cb152f9 989
7c896f63 990(provide 'todo-mode)
3cb152f9
OS
991
992;; ---------------------------------------------------------------------------
7c896f63 993;;; todo-mode.el ends here
3cb152f9 994;; ---------------------------------------------------------------------------