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