]> code.delx.au - gnu-emacs/blob - lisp/calendar/todo-mode.el
Incorporated simplifying suggestions from
[gnu-emacs] / lisp / calendar / todo-mode.el
1 ;; todo-mode.el -- Major mode for editing TODO list files
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: Oliver.Seidel@cl.cam.ac.uk (was valid on Aug 2, 1997)
6 ;; Created: 2 Aug 1997
7 ;; Version: $Id: todo-mode.el,v 1.27 1997/10/28 21:26:55 os10000 Exp os10000 $
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 ;; The TODO list file has a special format and some auxiliary
65 ;; information, which will be added by the todo-show function if
66 ;; it attempts to visit an un-initialised file. Hence it is
67 ;; recommended to use the todo-show function for the first time,
68 ;; in order to initialise the file, but it is not necessary
69 ;; afterwards.
70 ;;
71 ;; As these commands are quite long to type, I would recommend
72 ;; the addition of two bindings to your to your global keymap. I
73 ;; personally have the following in my initialisation file:
74 ;;
75 ;; (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
76 ;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
77 ;;
78 ;; Note, however, that this recommendation has prompted some
79 ;; criticism, since the keys C-c LETTER are reserved for user
80 ;; functions. I believe my recommendation is acceptable, since
81 ;; the Emacs Lisp Manual *Tips* section also details that the
82 ;; mode itself should not bind any functions to those keys. The
83 ;; express aim of the above two bindings is to work outside the
84 ;; mode, which doesn't need the show function and offers a
85 ;; different binding for the insert function. They serve as
86 ;; shortcuts and are not even needed (since the TODO mode will be
87 ;; entered by visiting the TODO file, and later by switching to
88 ;; its buffer).
89 ;;
90 ;; If you are an advanced user of this package, please consult
91 ;; the whole source code for autoloads, because there are several
92 ;; extensions that are not explicitly listed in the above quick
93 ;; installation.
94 ;;
95 ;; Version
96 ;;
97 ;; Which version of todo-mode.el does this documentation refer to?
98 ;;
99 ;; $Id: todo-mode.el,v 1.27 1997/10/28 21:26:55 os10000 Exp os10000 $
100 ;;
101 ;; Pre-Requisites
102 ;;
103 ;; This package will require the following packages to be
104 ;; available on the load-path:
105 ;;
106 ;; time-stamp
107 ;; easymenu
108 ;;
109 ;; Operation
110 ;;
111 ;; You will have the following facilities available:
112 ;;
113 ;; M-x todo-show will enter the todo list screen, here type
114 ;;
115 ;; + to go to next category
116 ;; - to go to previous category
117 ;; d to file the current entry, including a
118 ;; comment and timestamp
119 ;; e to edit the current entry
120 ;; f to file the current entry, including a
121 ;; comment and timestamp
122 ;; i to insert a new entry
123 ;; j jump to category
124 ;; k to kill the current entry
125 ;; l to lower the current entry's priority
126 ;; n for the next entry
127 ;; p for the previous entry
128 ;; P print
129 ;; q to save the list and exit the buffer
130 ;; r to raise the current entry's priority
131 ;; s to save the list
132 ;; S to save the list of top priorities
133 ;; t show top priority items for each category
134 ;;
135 ;; When you add a new entry, you are asked for the text and then
136 ;; for the category. I for example have categories for things
137 ;; that I want to do in the office (like mail my mum), that I
138 ;; want to do in town (like buy cornflakes) and things I want to
139 ;; do at home (move my suitcases). The categories can be
140 ;; selected with the cursor keys and if you type in the name of a
141 ;; category which didn't exist before, an empty category of the
142 ;; desired name will be added and filled with the new entry.
143 ;;
144 ;; Configuration
145 ;;
146 ;; Variable todo-prefix
147 ;;
148 ;; I would like to recommend that you use the prefix "*/*" (by
149 ;; leaving the variable 'todo-prefix' untouched) so that the
150 ;; diary displays each entry every day.
151 ;;
152 ;; To understand what I mean, please read the documentation that
153 ;; goes with the calendar since that will tell you how you can
154 ;; set up the fancy diary display and use the #include command to
155 ;; include your todo list file as part of your diary.
156 ;;
157 ;; If you have the diary package set up to usually display more
158 ;; than one day's entries at once, consider using
159 ;;
160 ;; "&%%(equal (calendar-current-date) date)"
161 ;;
162 ;; as the value of `todo-prefix'. Please note that this may slow
163 ;; down the processing of your diary file some.
164 ;;
165 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
166 ;;
167 ;; "&%%(todo-cp)"
168 ;;
169 ;; might be nicer and to that effect a function has been declared
170 ;; further down in the code. You may wish to auto-load this.
171 ;;
172 ;; Carsten also writes that that *changing* the prefix after the
173 ;; todo list is already established is not as simple as changing
174 ;; the variable - the todo files have to be changed by hand.
175 ;;
176 ;; Variable todo-file-do
177 ;;
178 ;; This variable is fairly self-explanatory. You have to store
179 ;; your TODO list somewhere. This variable tells the package
180 ;; where to go and find this file.
181 ;;
182 ;; Variable todo-file-done
183 ;;
184 ;; Even when you're done, you may wish to retain the entries.
185 ;; Given that they're timestamped and you are offered to add a
186 ;; comment, this can make a useful diary of past events. It will
187 ;; even blend in with the EMACS diary package. So anyway, this
188 ;; variable holds the name of the file for the filed todo-items.
189 ;;
190 ;; Variable todo-file-top
191 ;;
192 ;; File storing the top priorities of your TODO list when
193 ;; todo-save-top-priorities is non-nil. Nice to include in your
194 ;; diary instead of the complete TODO list.
195 ;;
196 ;; Variable todo-mode-hook
197 ;;
198 ;; Just like other modes, too, this mode offers to call your
199 ;; functions before it goes about its business. This variable
200 ;; will be inspected for any functions you may wish to have
201 ;; called once the other TODO mode preparations have been
202 ;; completed.
203 ;;
204 ;; Variable todo-insert-threshold
205 ;;
206 ;; Another nifty feature is the insertion accuracy. If you have
207 ;; 8 items in your TODO list, then you may get asked 4 questions
208 ;; by the binary insertion algorithm. However, you may not
209 ;; really have a need for such accurate priorities amongst your
210 ;; TODO items. If you now think about the binary insertion
211 ;; halfing the size of the window each time, then the threshhold
212 ;; is the window size at which it will stop. If you set the
213 ;; threshhold to zero, the upper and lower bound will coincide at
214 ;; the end of the loop and you will insert your item just before
215 ;; that point. If you set the threshhold to, e.g. 8, it will stop
216 ;; as soon as the window size drops below that amount and will
217 ;; insert the item in the approximate centre of that window. I
218 ;; got the idea for this feature after reading a very helpful
219 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
220 ;; corrected some of my awful coding and pointed me towards some
221 ;; good reading. Thanks Trey!
222 ;;
223 ;; Things to do
224 ;;
225 ;; These originally were my ideas, but now also include all the
226 ;; suggestions that I included before forgetting them:
227 ;;
228 ;; o Fancy fonts for todo/top-priority buffer
229 ;; o Remove todo-prefix option in todo-top-priorities
230 ;; o Rename category
231 ;; o Move entry from one category to another one
232 ;; o Entries which both have the generic */* prefix and a
233 ;; "deadline" entry which are understood by diary, indicating
234 ;; an event (unless marked by &)
235 ;; o The optional COUNT variable of todo-forward-item should be
236 ;; applied to the other functions performing similar tasks
237 ;; o Modularization could be done for repeaded elements of
238 ;; the code, like the completing-read lines of code.
239 ;; o license / version function
240 ;; o export to diary file
241 ;; o todo-report-bug
242 ;; o GNATS support
243 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
244 ;; outline mode)
245 ;; o rewrite complete package to store data as lisp objects
246 ;; and have display modes for display, for diary export,
247 ;; etc. (Richard Stallman pointed out this is a bad idea)
248 ;; o so base todo-mode.el on generic-mode.el instead
249 ;;
250 ;; History and Gossip
251 ;;
252 ;; Many thanks to all the ones who have contributed to the
253 ;; evolution of this package! I hope I have listed all of you
254 ;; somewhere in the documentation or at least in the RCS history!
255 ;;
256 ;; Enjoy this package and express your gratitude by sending nice
257 ;; things to my parents' address!
258 ;;
259 ;; Oliver Seidel
260 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
261 ;;
262
263 ;; ---------------------------------------------------------------------------
264
265 ;;; Change Log:
266
267 ;; $Log: todo-mode.el,v $
268 ;; Revision 1.27 1997/10/28 21:26:55 os10000
269 ;; Patch from Paul Stodghill <stodghil@CS.Cornell.EDU>:
270 ;; The patch below fixes todo-insert-item so that it will
271 ;; insert the item in place, instead of at the top of the
272 ;; buffer, when invoked with a prefix argument.
273 ;;
274 ;; Revision 1.26 1997/10/28 21:14:51 os10000
275 ;; Improvements sent in by Dave Love <d.love@dl.ac.uk>:
276 ;; todo-mode.el: Doc fixes. Customization.
277 ;; (todo-add-item-non-interactively): New arg -- don't dynamically bind ARG.
278 ;; (todo-insert-item): Use it.
279 ;;
280 ;; Revision 1.25 1997/10/28 20:03:27 os10000
281 ;; Harald Backer <harald.backer@fou.telenor.no> sent the following:
282 ;; Added `todo-save-top-priorities' and option to automatically save top
283 ;; priorities file when saving todo-file. Changed some default values.
284 ;; Bug fixes.
285 ;;
286 ;; Revision 1.24 1997/10/28 19:41:53 os10000
287 ;; Added fix from Frank Ridderbusch <ridderbusch.pad@sni.de>,
288 ;; an apostrophe was missing.
289 ;;
290 ;; Revision 1.23 1997/10/24 17:30:54 os10000
291 ;; Added three suggestions from Carsten
292 ;; Dominik <dominik@strw.LeidenUniv.nl>:
293 ;;
294 ;; - recommend autoloading instead of require
295 ;; - inserting from different buffer didn't work
296 ;; (now fixed -- I pray)
297 ;; - provided public entry point to insert items
298 ;; from normal lisp code
299 ;;
300 ;; Revision 1.22 1997/10/24 16:53:20 os10000
301 ;; Paul Stodghill <stodghil@CS.Cornell.EDU> writes:
302 ;;
303 ;; When invoked with a prefix, todo-insert-item
304 ;; should not prompt for a category. (He adds:
305 ;; At least that's what I think.)
306 ;;
307 ;; Revision 1.21 1997/10/24 16:51:02 os10000
308 ;; Rafael Laboissiere <rafael@icp.inpg.fr> writes:
309 ;;
310 ;; I was just annoyed with the fact that there is no way
311 ;; to dynamically control the insertion accuracy. I mean:
312 ;; the variable `todo-insert-threshold' does the job, but
313 ;; it is not very handy if one wants to mix the two
314 ;; behaviors (bisection and "insert right here under the
315 ;; cursor").
316 ;;
317 ;; Therefore I did a quick hack in the function
318 ;; `todo-insert-item'. Now by giving a prefix argument to
319 ;; the insert command (i.e. by typing "C-u i"), entries
320 ;; are inserted exactly at the line where the cursor is.
321 ;; It would be better to give the value of
322 ;; `todo-insert-threshold' as a numeric argument of
323 ;; `todo-insert-item' (like "M-8 i"), but it's too late
324 ;; now for continuing to hack.
325 ;;
326 ;; Revision 1.20 1997/10/17 15:41:57 os10000
327 ;; Thanks to Harald Backer <harald.backer@fou.telenor.no>, we now have
328 ;; the following facilities available:
329 ;;
330 ;; Added todo-print, todo-top-priorities and todo-jump with matching
331 ;; variables; Parameterized todo-header, todo-category-beg,
332 ;; todo-category-end and todo-category-sep; Added autoload comments;
333 ;; todo-category-select: Modified regexp to make category names unique;
334 ;; todo-forward-item: Added optional COUNT vaiable; todo-insert-item:
335 ;; Rewrote completing read entry.
336 ;;
337 ;; Also, check out the extended list of things left to be done to this
338 ;; package at the end of the documentation!
339 ;;
340 ;; Revision 1.19 1997/10/16 21:21:16 os10000
341 ;; Jari Aalto <jari.aalto@poboxes.com> writes:
342 ;;
343 ;; I just downloaded your package and after reading the docs I
344 ;; decided to do some reformatting. Hope you don't mind. Now
345 ;; they are in such a format that the html page can be
346 ;; automatically generated from the source file. As an example, I
347 ;; generated the attached page using the following command:
348 ;; ripdoc.pls < todo-mode.el | t2html.pls -a "Oliver.Seidel" -e \
349 ;; Oliver.Seidel@cl.cam.ac.uk -simple -base
350 ;;
351 ;; And of course I appreciate it. Jari's stuff can be found at:
352 ;; ftp://cs.uta.fi/pub/ssjaaa/, while I'm making the rev 1.18 page
353 ;; available at http://www.cl.cam.ac.uk/users/os10000/doc/todo-mode.html
354 ;; (That link will be valid until 10/1998 or slightly longer.)
355 ;;
356 ;; Revision 1.18 1997/10/15 17:18:11 os10000
357 ;; Everything seems to work in Harald Melands Emacs 20.02 and
358 ;; my Emacs 19.34. Beware of the spelling in some of the
359 ;; variable names. I looked up "threshold" in a dictionary
360 ;; and here in Britain this appears to be the way to spell it.
361 ;;
362 ;; Revision 1.17 1997/10/15 14:30:41 os10000
363 ;; Attempted to reconcile Harald's changes with mine since 1.15.
364 ;;
365 ;; Revision 1.16 1997/10/15 14:00:12 os10000
366 ;; Fixed 'file-item' and added 20.02 split-string function.
367 ;;
368 ;; Revision 1.15 1997/10/14 22:22:35 os10000
369 ;; Added string-split (which I stole from ediff-util), changed
370 ;; pop-to-buffer to switch-to-buffer and added message on how
371 ;; to exit the multi-line-edit mode.
372 ;;
373 ;; Revision 1.14 1997/10/09 09:24:50 os10000
374 ;; Harald Meland <harald.meland@usit.uio.no> asked for
375 ;; the latest version, got 1.13, and returned this.
376 ;; He writes:
377 ;;
378 ;; Thanks a lot for the new version of todo-mode.el. As you will see I
379 ;; have messed it up a bit, hopefully for the better -- I don't like
380 ;; short, cryptic names for variables and functions, so I renamed most of
381 ;; them, and `defalias'ed the old function names. I hope you don't mind
382 ;; too much, I just kinda couldn't stop myself.
383 ;;
384 ;; Additionally, I included some support for multiline entries, cleaned
385 ;; up (IMHO :) a lot of the code, included completion-support for which
386 ;; category to install a new entry in, and possibly some other changes I
387 ;; can't remember :)
388 ;;
389 ;; It's getting rather late, and I have just done some preliminary
390 ;; testing on whether all of this really works, but so far it looks
391 ;; good.
392 ;;
393 ;; Revision 1.13 1997/08/19 14:00:36 seidel
394 ;; - changed name to todo-mode
395 ;; - fixed menu descriptions
396 ;; - fixed "pressing abort while filing"
397 ;; - attempted Emacs Lisp Manual *Tips* section compliance
398 ;;
399 ;; Revision 1.12 1997/08/06 10:56:15 os10000
400 ;; Fixed header, typos, layout, documentation.
401 ;;
402 ;; Revision 1.11 1997/08/06 09:14:25 os10000
403 ;; Applied patch from Istvan Marko <istvan@cmdmail.amd.com>
404 ;; to make menus work anywhere.
405 ;;
406 ;; Revision 1.10 1997/08/06 08:56:03 os10000
407 ;; Acted upon suggestion from Shane Holder <holder@rsn.hp.com>:
408 ;; Cancelling the editing of an entry will not delete it any more.
409 ;;
410 ;; Revision 1.9 1997/08/06 08:12:03 os10000
411 ;; Improved documentation. Broke some lines to comply with
412 ;; Richard Stallman's email to please keep in sync with the
413 ;; rest of the Emacs distribution files.
414 ;;
415 ;; Revision 1.8 1997/08/05 22:39:04 os10000
416 ;; Made todo-mode.el available under GPL.
417 ;;
418 ;; Revision 1.7 1997/08/05 22:34:14 os10000
419 ;; Fixed insertion routine with help from Trey Jackson
420 ;; <trey@cs.berkeley.edu>; added todo-inst-tresh;
421 ;; fixed keyboard layout to remove unwanted keys.
422 ;;
423 ;; Revision 1.6 1997/08/05 16:47:01 os10000
424 ;; Incorporated menus for XEmacs from Allan.Cochrane@soton.sc.philips.com,
425 ;; fixed TYPO, fixed todo-file-cmd, cleaned up rcs history.
426 ;;
427 ;; Revision 1.5 1997/08/05 14:43:39 os10000
428 ;; Added improvements from Ron Gut <rgut@aware.com>.
429 ;; Added category management.
430 ;;
431 ;; Revision 1.4 1997/08/04 16:18:45 os10000
432 ;; Added Raise/Lower item.
433 ;;
434 ;; Revision 1.3 1997/08/03 12:47:26 os10000
435 ;; Cleaned up variables, prefix and cursor position.
436 ;;
437 ;; Revision 1.2 1997/08/03 12:15:28 os10000
438 ;; It appears to work.
439 ;;
440 ;; Revision 1.1 1997/08/03 12:15:13 os10000
441 ;; Initial revision
442 ;;
443
444 ;; ---------------------------------------------------------------------------
445
446 ;;; Code:
447
448 (eval-and-compile ; Removable for installation in
449 ; Emacs 20.
450 (condition-case ()
451 (require 'custom)
452 (error nil))
453 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
454 nil ;; We've got what we needed
455 ;; We have the old custom-library, hack around it!
456 (defmacro defgroup (&rest args)
457 nil)
458 (defmacro defcustom (var value doc &rest args)
459 (` (defvar (, var) (, value) (, doc))))))
460
461 ;; User-configurable variables:
462
463 (defgroup todo nil
464 "Maintain a list of todo items."
465 :group 'calendar)
466
467 (defcustom todo-prefix "*/*"
468 "*TODO mode prefix for entries.
469
470 This is useful in conjunction with `calendar' and `diary' if you use
471
472 #include \"~/.todo-do\"
473
474 in your diary file to include your todo list file as part of your
475 diary. With the default value \"*/*\" the diary displays each entry
476 every day and it may also be marked on every day of the calendar.
477 Using \"&%%(equal (calendar-current-date) date)\" instead will only
478 show and mark todo entreis for today, but may slow down processing of
479 the diary file somewhat."
480 :type 'string
481 :group 'todo)
482 (defcustom todo-file-do "~/.todo-do"
483 "*TODO mode list file."
484 :type 'file
485 :group 'todo)
486 (defcustom todo-file-done "~/.todo-done"
487 "*TODO mode archive file."
488 :type 'file
489 :group 'todo)
490 (defcustom todo-mode-hook nil
491 "*TODO mode hooks."
492 :type 'hook
493 :group 'todo)
494 (defcustom todo-edit-mode-hook nil
495 "*TODO Edit mode hooks."
496 :type 'hook
497 :group 'todo)
498 (defcustom todo-insert-threshold 0
499 "*TODO mode insertion accuracy.
500
501 If you have 8 items in your TODO list, then you may get asked 4
502 questions by the binary insertion algorithm. However, you may not
503 really have a need for such accurate priorities amongst your TODO
504 items. If you now think about the binary insertion halfing the size
505 of the window each time, then the threshhold is the window size at
506 which it will stop. If you set the threshhold to zero, the upper and
507 lower bound will coincide at the end of the loop and you will insert
508 your item just before that point. If you set the threshhold to,
509 e.g. 8, it will stop as soon as the window size drops below that
510 amount and will insert the item in the approximate centre of that
511 window."
512 :type 'integer
513 :group 'todo)
514 (defvar todo-edit-buffer " *TODO Edit*" "TODO Edit buffer name.")
515 (defcustom todo-file-top "~/.todo-top"
516 "*TODO mode top priorities file.
517
518 Not in TODO format, but diary compatible.
519 Automatically generated when `todo-save-top-priorities' is non-nil."
520 :type 'string
521 :group 'todo)
522
523 (defcustom todo-print-function 'ps-print-buffer-with-faces
524 "*Function to print the current buffer."
525 :type 'symbol
526 :group 'todo)
527 (defcustom todo-show-priorities 1
528 "*Default number of priorities to show by \\[todo-top-priorities].
529 0 means show all entries."
530 :type 'integer
531 :group 'todo)
532 (defcustom todo-print-priorities 0
533 "*Default number of priorities to print by \\[todo-print].
534 0 means print all entries."
535 :type 'integer
536 :group 'todo)
537 (defcustom todo-remove-separator t
538 "*Non-nil to remove category separators in\
539 \\[todo-top-priorities] and \\[todo-print]."
540 :type 'boolean
541 :group 'todo)
542 (defcustom todo-save-top-priorities-too t
543 "*Non-nil makes todo-save automatically save top-priorities in
544 `todo-file-top'."
545 :type 'boolean
546 :group 'todo)
547
548 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
549 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
550 ;;
551 (defcustom todo-time-string-format
552 "%:y-%02m-%02d %02H:%02M"
553 "*TODO mode time string format for done entries.
554 For details see the variable `time-stamp-format'."
555 :type 'string
556 :group 'todo)
557
558 (defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
559 "*Function producing text to insert at start of todo entry."
560 :type 'symbol
561 :group 'todo)
562 (defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
563 "*Initials of todo item author."
564 :type 'string
565 :group 'todo)
566
567 (defun todo-entry-timestamp-initials ()
568 "Prepend timestamp and your initials to the head of a TODO entry."
569 (let ((time-stamp-format todo-time-string-format))
570 (concat (time-stamp-string) " " todo-initials ": ")))
571
572 ;; ---------------------------------------------------------------------------
573
574 ;; Get some outside help ...
575
576 (require 'time-stamp)
577 (require 'easymenu)
578
579 ;; ---------------------------------------------------------------------------
580
581 ;; Set up some helpful context ...
582
583 (defvar todo-categories nil "TODO categories.")
584 (defvar todo-cats nil
585 "Old variable for holding the TODO categories.
586 Use `todo-categories' instead.")
587 (defvar todo-previous-line 0 "Previous line that I asked about.")
588 (defvar todo-previous-answer 0 "Previous answer that I got.")
589 (defvar todo-mode-map nil "TODO mode keymap.")
590 (defvar todo-category-number 0 "TODO category number.")
591
592 (defvar todo-tmp-buffer-name "*Tmp*")
593
594 (defvar todo-category-sep (make-string 75 ?-)
595 "Category separator.")
596 (defvar todo-category-beg " --- "
597 "Category start separator to be prepended onto category name.")
598 (defvar todo-category-end "--- End"
599 "Separator after a category.")
600 (defvar todo-header "-*- mode: todo; "
601 "Header of todo files.")
602
603
604
605 ;; ---------------------------------------------------------------------------
606
607 (if todo-mode-map
608 nil
609 (let ((map (make-keymap)))
610 (suppress-keymap map t)
611 (define-key map "+" 'todo-forward-category)
612 (define-key map "-" 'todo-backward-category)
613 (define-key map "e" 'todo-edit-item)
614 (define-key map "E" 'todo-edit-multiline)
615 (define-key map "d" 'todo-file-item) ;done/delete
616 (define-key map "f" 'todo-file-item)
617 (define-key map "i" 'todo-insert-item)
618 (define-key map "j" 'todo-jump-to-category)
619 (define-key map "k" 'todo-delete-item)
620 (define-key map "l" 'todo-lower-item)
621 (define-key map "n" 'todo-forward-item)
622 (define-key map "p" 'todo-backward-item)
623 (define-key map "P" 'todo-print)
624 (define-key map "q" 'todo-quit)
625 (define-key map "r" 'todo-raise-item)
626 (define-key map "s" 'todo-save)
627 (define-key map "S" 'todo-save-top-priorities)
628 (define-key map "t" 'todo-top-priorities)
629 (setq todo-mode-map map)))
630
631 (defun todo-category-select ()
632 "Make TODO mode display the current category correctly."
633 (let ((name (nth todo-category-number todo-categories)))
634 (setq mode-line-buffer-identification
635 (concat "Category: " name))
636 (widen)
637 (goto-char (point-min))
638 (search-forward-regexp
639 (concat "^"
640 (regexp-quote (concat todo-prefix todo-category-beg name))
641 "$"))
642 (let ((begin (1+ (point-at-eol))))
643 (search-forward-regexp (concat "^" todo-category-end))
644 (narrow-to-region begin (point-at-bol))
645 (goto-char (point-min)))))
646 (defalias 'todo-cat-slct 'todo-category-select)
647
648 (defun todo-forward-category () "Go forward to TODO list of next category."
649 (interactive)
650 (setq todo-category-number
651 (mod (1+ todo-category-number) (length todo-categories)))
652 (todo-category-select))
653 (defalias 'todo-cmd-forw 'todo-forward-category)
654
655 (defun todo-backward-category () "Go back to TODO list of previous category."
656 (interactive)
657 (setq todo-category-number
658 (mod (1- todo-category-number) (length todo-categories)))
659 (todo-category-select))
660 (defalias 'todo-cmd-back 'todo-backward-category)
661
662 (defun todo-backward-item () "Select previous entry of TODO list."
663 (interactive)
664 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
665 (message ""))
666 (defalias 'todo-cmd-prev 'todo-backward-item)
667
668 (defun todo-forward-item (&optional count)
669 "Select COUNT-th next entry of TODO list."
670 (interactive "P")
671 (if (listp count) (setq count (car count)))
672 (end-of-line)
673 (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
674 nil 'goto-end count)
675 (beginning-of-line)
676 (message ""))
677 (defalias 'todo-cmd-next 'todo-forward-item)
678
679 (defun todo-save () "Save the TODO list."
680 (interactive)
681 (save-buffer)
682 (if todo-save-top-priorities-too (todo-save-top-priorities))
683 )
684 (defalias 'todo-cmd-save 'todo-save)
685
686 (defun todo-quit () "Done with TODO list for now."
687 (interactive)
688 (widen)
689 (todo-save)
690 (message "")
691 (bury-buffer))
692 (defalias 'todo-cmd-done 'todo-quit)
693
694 (defun todo-edit-item () "Edit current TODO list entry."
695 (interactive)
696 (let ((item (todo-item-string)))
697 (if (todo-string-multiline-p item)
698 (todo-edit-multiline)
699 (let ((new (read-from-minibuffer "Edit: " item)))
700 (todo-remove-item)
701 (insert new "\n")
702 (todo-backward-item)
703 (message "")))))
704 (defalias 'todo-cmd-edit 'todo-edit-item)
705
706 (defun todo-edit-multiline ()
707 "Set up a buffer for editing a multiline TODO list entry."
708 (interactive)
709 (let ((buffer-name (generate-new-buffer-name todo-edit-buffer)))
710 (switch-to-buffer
711 (make-indirect-buffer
712 (file-name-nondirectory todo-file-do) buffer-name))
713 (message "To exit, simply kill this buffer and return to list.")
714 (todo-edit-mode)
715 (narrow-to-region (todo-item-start) (todo-item-end))))
716
717 ;;;### autoload
718 (defun todo-add-category (cat)
719 "Add new category CAT to the TODO list."
720 (interactive)
721 (save-window-excursion
722 (setq todo-categories (cons cat todo-categories))
723 (find-file todo-file-do)
724 (widen)
725 (goto-char (point-min))
726 (let ((posn (search-forward "-*- mode: todo; " 17 t)))
727 (if (not (null posn)) (goto-char posn))
728 (if (equal posn nil)
729 (progn
730 (insert "-*- mode: todo; \n")
731 (forward-char -1))
732 (kill-line)))
733 (insert (format "todo-categories: %S; -*-" todo-categories))
734 (forward-char 1)
735 (insert (format "%s%s%s\n%s\n%s %s\n"
736 todo-prefix todo-category-beg cat
737 todo-category-end
738 todo-prefix todo-category-sep)))
739 0)
740
741 ;;;### autoload
742 (defun todo-add-item-non-interactively (new-item category ARG)
743 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
744 (save-excursion
745 (todo-show))
746 (save-excursion
747 (if (string= "" category)
748 (setq category (nth todo-category-number todo-categories)))
749 (let ((cat-exists (member category todo-categories)))
750 (setq todo-category-number
751 (if cat-exists
752 (- (length todo-categories) (length cat-exists))
753 (todo-add-category category))))
754 (if (not ARG)
755 (progn
756 (todo-show)
757 (setq todo-previous-line 0)
758 (let ((top 1)
759 (bottom (1+ (count-lines (point-min) (point-max)))))
760 (while (> (- bottom top) todo-insert-threshold)
761 (let* ((current (/ (+ top bottom) 2))
762 (answer (if (< current bottom)
763 (todo-more-important-p current) nil)))
764 (if answer
765 (setq bottom current)
766 (setq top (1+ current)))))
767 (setq top (/ (+ top bottom) 2))
768 ;; goto-line doesn't have the desired behavior in a narrowed buffer
769 (goto-char (point-min))
770 (forward-line (1- top))))
771 (beginning-of-line))
772 (insert new-item "\n")
773 (todo-backward-item)
774 (todo-save)
775 (message "")))
776
777 ;;;### autoload
778 (defun todo-insert-item (ARG)
779 "Insert new TODO list entry.
780 With a prefix argument solicit the category, otherwise use the current
781 category."
782 (interactive "P")
783 (save-excursion
784 (todo-show)
785 )
786 (let* ((new-item (concat todo-prefix " "
787 (read-from-minibuffer
788 "New TODO entry: "
789 (if todo-entry-prefix-function
790 (funcall todo-entry-prefix-function)))))
791 (categories todo-categories)
792 (history (cons 'categories (1+ todo-category-number)))
793 (current-category (nth todo-category-number todo-categories))
794 (category
795 (if ARG
796 current-category
797 (completing-read
798 (concat "Category ["
799 current-category "]: ")
800 (todo-category-alist) nil nil nil history))))
801 (todo-add-item-non-interactively new-item category ARG)))
802
803 (defalias 'todo-cmd-inst 'todo-insert-item)
804
805 (defun todo-more-important-p (line)
806 "Ask whether entry is more important than the one at LINE."
807 (if (not (equal todo-previous-line line))
808 (progn
809 (setq todo-previous-line line)
810 (goto-char (point-min))
811 (forward-line (1- todo-previous-line))
812 (let ((item (todo-item-string-start)))
813 (setq todo-previous-answer
814 (y-or-n-p (concat "More important than '" item "'? "))))))
815 todo-previous-answer)
816 (defalias 'todo-ask-p 'todo-more-important-p)
817
818 (defun todo-delete-item () "Delete current TODO list entry."
819 (interactive)
820 (if (> (count-lines (point-min) (point-max)) 0)
821 (let* ((todo-entry (todo-item-string-start))
822 (todo-answer (y-or-n-p (concat "Permanently remove '"
823 todo-entry "'? "))))
824 (if todo-answer
825 (progn
826 (todo-remove-item)
827 (todo-backward-item)))
828 (message ""))
829 (error "No TODO list entry to delete")))
830 (defalias 'todo-cmd-kill 'todo-delete-item)
831
832 (defun todo-raise-item () "Raise priority of current entry."
833 (interactive)
834 (if (> (count-lines (point-min) (point)) 0)
835 (let ((item (todo-item-string)))
836 (todo-remove-item)
837 (todo-backward-item)
838 (save-excursion
839 (insert item "\n"))
840 (message ""))
841 (error "No TODO list entry to raise")))
842 (defalias 'todo-cmd-rais 'todo-raise-item)
843
844 (defun todo-lower-item () "Lower priority of current entry."
845 (interactive)
846 (if (> (count-lines (point) (point-max)) 1)
847 ;; Assume there is a final newline
848 (let ((item (todo-item-string)))
849 (todo-remove-item)
850 (todo-forward-item)
851 (save-excursion
852 (insert item "\n"))
853 (message ""))
854 (error "No TODO list entry to lower")))
855 (defalias 'todo-cmd-lowr 'todo-lower-item)
856
857 (defun todo-file-item () "File the current TODO list entry away."
858 (interactive)
859 (if (> (count-lines (point-min) (point-max)) 0)
860 (let ((comment (read-from-minibuffer "Comment: "))
861 (time-stamp-format todo-time-string-format))
862 (if (> (length comment) 0)
863 (progn
864 (goto-char (todo-item-end))
865 (insert
866 (if (save-excursion (beginning-of-line)
867 (looking-at (regexp-quote todo-prefix)))
868 " "
869 "\n\t")
870 "(" (nth todo-category-number todo-categories) ": "
871 comment ")\n")))
872 (goto-char (todo-item-start))
873 (let ((temp-point (point)))
874 (if (looking-at (regexp-quote todo-prefix))
875 (replace-match (time-stamp-string))
876 ;; Standard prefix -> timestamp
877 ;; Else prefix non-standard item start with timestamp
878 (insert (time-stamp-string)))
879 (append-to-file temp-point (todo-item-end) todo-file-done)
880 (delete-region temp-point (1+ (todo-item-end))))
881 (todo-backward-item)
882 (message ""))
883 (error "No TODO list entry to file away")))
884
885 ;; ---------------------------------------------------------------------------
886
887 ;; Utility functions:
888
889
890 ;;;###autoload
891 (defun todo-top-priorities (&optional nof-priorities category-pr-page)
892 "List top priorities for each category.
893
894 Number of entries for each category is given by NOF-PRIORITIES which
895 defaults to \'todo-show-priorities\'.
896
897 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
898 between each category."
899
900 (interactive "P")
901 (or nof-priorities (setq nof-priorities todo-show-priorities))
902 (if (listp nof-priorities) ;universal argument
903 (setq nof-priorities (car nof-priorities)))
904 (let ((todo-print-buffer-name "*Tmp*")
905 ;;(todo-print-category-number 0)
906 (todo-category-break (if category-pr-page "\f" ""))
907 (cat-end
908 (concat
909 (if todo-remove-separator
910 (concat todo-category-end "\n"
911 (regexp-quote todo-prefix) " " todo-category-sep "\n")
912 (concat todo-category-end "\n"))))
913 beg end)
914 (todo-show)
915 (save-excursion
916 (save-restriction
917 (widen)
918 (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
919 (set-buffer todo-print-buffer-name)
920 (goto-char (point-min))
921 (if (re-search-forward (regexp-quote todo-header) nil t)
922 (progn
923 (beginning-of-line 1)
924 (kill-line))) ;Remove mode line
925 (while (re-search-forward ;Find category start
926 (regexp-quote (concat todo-prefix todo-category-beg))
927 nil t)
928 (setq beg (+ (point-at-eol) 1)) ;Start of first entry.
929 (re-search-forward cat-end nil t)
930 (setq end (match-beginning 0))
931 (replace-match todo-category-break)
932 (narrow-to-region beg end) ;In case we have too few entries.
933 (goto-char (point-min))
934 (if (= 0 nof-priorities) ;Traverse entries.
935 (goto-char end) ;All entries
936 (todo-forward-item nof-priorities))
937 (setq beg (point))
938 (delete-region beg end)
939 (widen))
940 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
941 (goto-char (point-min)) ;Due to display buffer
942 ))
943 ;; Could have used switch-to-buffer as it has a norecord argument,
944 ;; which is nice when we are called from e.g. todo-print.
945 ;; Else we could have used pop-to-buffer.
946 (display-buffer todo-print-buffer-name)
947 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
948 todo-print-buffer-name)
949 ))
950
951 ;;;###autoload
952 (defun todo-save-top-priorities (&optional nof-priorities)
953 "Save top priorities for each category in `todo-file-top'.
954
955 Number of entries for each category is given by NOF-PRIORITIES which
956 defaults to `todo-show-priorities'."
957 (interactive "P")
958 (save-window-excursion
959 (save-excursion
960 (save-restriction
961 (todo-top-priorities nof-priorities)
962 (set-buffer todo-tmp-buffer-name)
963 (write-file todo-file-top)
964 (kill-this-buffer)
965 ))))
966
967 ;;;###autoload
968 (defun todo-print (&optional category-pr-page)
969 "Print todo summary using \\\[todo-print-function].
970 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
971 between each category.
972
973 Number of entries for each category is given by
974 \'todo-print-priorities\'."
975 (interactive "P")
976 (if todo-print-function
977 (progn
978 (save-window-excursion
979 (save-excursion
980 (save-restriction
981 (todo-top-priorities todo-print-priorities
982 category-pr-page)
983 (set-buffer todo-tmp-buffer-name)
984 (and (funcall todo-print-function)
985 (kill-this-buffer))
986 (message "Todo printing done."))
987 )))
988 (message "todo-print-function undefinded")
989 ))
990
991 (defun todo-jump-to-category ()
992 "Jump to a category. Default is previous category."
993 (interactive)
994 (let* ((categories todo-categories)
995 (history (cons 'categories (1+ todo-category-number)))
996 (category (completing-read
997 (concat "Category ["
998 (nth todo-category-number todo-categories) "]: ")
999 (todo-category-alist) nil nil nil history)))
1000 (if (string= "" category)
1001 (setq category (nth todo-category-number todo-categories)))
1002 (setq todo-category-number
1003 (if (member category todo-categories)
1004 (- (length todo-categories)
1005 (length (member category todo-categories)))
1006 (todo-add-category category)))
1007 (todo-show)))
1008
1009 (defun todo-line-string () "Return current line in buffer as a string."
1010 (buffer-substring (point-at-bol) (point-at-eol)))
1011
1012 (defun todo-item-string-start ()
1013 "Return the start of this TODO list entry as a string."
1014 ;; Suitable for putting in the minibuffer when asking the user
1015 (let ((item (todo-item-string)))
1016 (if (> (length item) 60)
1017 (setq item (concat (substring item 0 56) "...")))
1018 item))
1019
1020 (defun todo-item-start () "Return point at start of current TODO list item."
1021 (save-excursion
1022 (beginning-of-line)
1023 (if (not (looking-at (regexp-quote todo-prefix)))
1024 (search-backward-regexp
1025 (concat "^" (regexp-quote todo-prefix)) nil t))
1026 (point)))
1027
1028 (defun todo-item-end () "Return point at end of current TODO list item."
1029 (save-excursion
1030 (end-of-line)
1031 (search-forward-regexp
1032 (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
1033 (1- (point-at-bol))))
1034
1035 (defun todo-remove-item () "Delete the current entry from the TODO list."
1036 (delete-region (todo-item-start) (1+ (todo-item-end))))
1037
1038 (defun todo-item-string () "Return current TODO list entry as a string."
1039 (buffer-substring (todo-item-start) (todo-item-end)))
1040
1041 (defun todo-string-count-lines (string)
1042 "Return the number of lines STRING spans."
1043 (length (split-string string "\n")))
1044
1045 (defun todo-string-multiline-p (string)
1046 "Return non-nil if STRING spans several lines."
1047 (> (todo-string-count-lines string) 1))
1048
1049 (defun todo-category-alist ()
1050 "Generate an alist for use in `completing-read' from `todo-categories'."
1051 (mapcar (lambda (cat) (cons cat nil))
1052 todo-categories))
1053
1054 ;; utility functions: These are available in XEmacs, but not in Emacs 19.34
1055
1056 (if (not (fboundp 'point-at-bol))
1057 (defun point-at-bol () "Return value of point at beginning of line."
1058 (save-excursion
1059 (beginning-of-line)
1060 (point))))
1061
1062 (if (not (fboundp 'point-at-eol))
1063 (defun point-at-eol () "Return value of point at end of line."
1064 (save-excursion
1065 (end-of-line)
1066 (point))))
1067
1068 ;; splits at a white space, returns a list
1069 (if (not (fboundp 'split-string))
1070 (defun split-string (string &optional separators)
1071 "Splits STRING into substrings where there are matches for SEPARATORS.
1072 Each match for SEPARATORS is a splitting point.
1073 The substrings between the splitting points are made into a list
1074 which is returned.
1075 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
1076 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1077 (start 0)
1078 (list nil))
1079 (while (string-match rexp string start)
1080 (or (eq (match-beginning 0) 0)
1081 (setq list
1082 (cons (substring string start (match-beginning 0))
1083 list)))
1084 (setq start (match-end 0)))
1085 (or (eq start (length string))
1086 (setq list
1087 (cons (substring string start)
1088 list)))
1089 (nreverse list))))
1090
1091 ;; ---------------------------------------------------------------------------
1092
1093 (easy-menu-define todo-menu todo-mode-map "Todo Menu"
1094 '("Todo"
1095 ["Next category" todo-forward-category t]
1096 ["Previous category" todo-backward-category t]
1097 ["Jump to category" todo-jump-to-category t]
1098 ["Show top priority items" todo-top-priorities t]
1099 ["Print categories" todo-print t]
1100 "---"
1101 ["Edit item" todo-edit-item t]
1102 ["File item" todo-file-item t]
1103 ["Insert new item" todo-insert-item t]
1104 ["Kill item" todo-delete-item t]
1105 "---"
1106 ["Lower item priority" todo-lower-item t]
1107 ["Raise item priority" todo-raise-item t]
1108 "---"
1109 ["Next item" todo-forward-item t]
1110 ["Previous item" todo-backward-item t]
1111 "---"
1112 ["Save" todo-save t]
1113 ["Save Top Priorities" todo-save-top-priorities t]
1114 "---"
1115 ["Quit" todo-quit t]
1116 ))
1117
1118 ;; As calendar reads .todo-do before todo-mode is loaded.
1119 ;;;### autoload
1120 (defun todo-mode () "Major mode for editing TODO lists.\n\n\\{todo-mode-map}"
1121 (interactive)
1122 (setq major-mode 'todo-mode)
1123 (setq mode-name "TODO")
1124 (use-local-map todo-mode-map)
1125 (easy-menu-add todo-menu)
1126 (run-hooks 'todo-mode-hook))
1127
1128 ;; Read about this function in the setup instructions above!
1129 ;;;### autoload
1130 (defun todo-cp ()
1131 "Make a diary entry appear only in the current date's diary"
1132 (if (equal (calendar-current-date) date)
1133 entry
1134 nil))
1135
1136 (defun todo-edit-mode ()
1137 "Major mode for editing items in the TODO list\n\n\\{todo-edit-mode-map}"
1138 (text-mode)
1139 (setq major-mode 'todo-edit-mode)
1140 (setq mode-name "TODO Edit")
1141 (run-hooks 'todo-edit-mode-hook))
1142
1143 ;;;### autoload
1144 (defun todo-show () "Show TODO list."
1145 (interactive)
1146 (if (file-exists-p todo-file-do)
1147 (find-file todo-file-do)
1148 (todo-initial-setup))
1149 (if (null todo-categories)
1150 (if (null todo-cats)
1151 (error "Error in %s: No categories in list `todo-categories'"
1152 todo-file-do)
1153 (goto-char (point-min))
1154 (and (search-forward "todo-cats:" nil t)
1155 (replace-match "todo-categories:"))
1156 (make-local-variable 'todo-categories)
1157 (setq todo-categories todo-cats)))
1158 (beginning-of-line)
1159 (todo-category-select))
1160
1161 (defun todo-initial-setup () "Set up things to work properly in TODO mode."
1162 (find-file todo-file-do)
1163 (erase-buffer)
1164 (todo-mode)
1165 (todo-add-category "Todo"))
1166
1167 (provide 'todo-mode)
1168
1169 ;; ---------------------------------------------------------------------------
1170 ;;; todo-mode.el ends here
1171 ;; ---------------------------------------------------------------------------