1 ;; todo-mode.el -- Major mode for editing TODO list files
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; Author: Oliver.Seidel@cl.cam.ac.uk (was valid on Aug 2, 1997)
7 ;; Version: $Id: todo-mode.el,v 1.34 1998/01/12 11:43:22 os10000 Exp os10000 $
8 ;; Keywords: Categorised TODO list editor, todo-mode
10 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; ---------------------------------------------------------------------------
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
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
50 ;; Preface, Quickstart Installation
52 ;; To get this to work, make emacs execute the line
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)
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".
64 ;; -------------------------------------------------------------
66 ;; oh no, it doesn't work any more ... but Alex Schroeder
67 ;; <a.schroeder@bsiag.ch> writes:
69 ;; -------------------------------------------------------------
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
75 ;; 4. Only now can you start to add entries.
77 ;; This is a bit cumbersome, and it should be documented. You probably
79 ;; -------------------------------------------------------------
81 ;; and right he is. My apologies, I'll try to fix it sometime.
83 ;; -------------------------------------------------------------
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
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:
96 ;; (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
97 ;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
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
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
118 ;; Which version of todo-mode.el does this documentation refer to?
120 ;; $Id: todo-mode.el,v 1.34 1998/01/12 11:43:22 os10000 Exp os10000 $
124 ;; This package will require the following packages to be
125 ;; available on the load-path:
132 ;; You will have the following facilities available:
134 ;; M-x todo-show will enter the todo list screen, here type
136 ;; + to go to next category
137 ;; - to go to previous category
138 ;; d to file the current entry, including a
139 ;; comment and timestamp
140 ;; e to edit the current entry
141 ;; E to edit a multi-line entry
142 ;; f to file the current entry, including a
143 ;; comment and timestamp
144 ;; i to insert a new entry, with prefix, omit category
145 ;; I to insert a new entry at current cursor position
146 ;; j jump to category
147 ;; k to kill the current entry
148 ;; l to lower the current entry's priority
149 ;; n for the next entry
150 ;; p for the previous entry
152 ;; q to save the list and exit the buffer
153 ;; r to raise the current entry's priority
154 ;; s to save the list
155 ;; S to save the list of top priorities
156 ;; t show top priority items for each category
158 ;; When you add a new entry, you are asked for the text and then
159 ;; for the category. I for example have categories for things
160 ;; that I want to do in the office (like mail my mum), that I
161 ;; want to do in town (like buy cornflakes) and things I want to
162 ;; do at home (move my suitcases). The categories can be
163 ;; selected with the cursor keys and if you type in the name of a
164 ;; category which didn't exist before, an empty category of the
165 ;; desired name will be added and filled with the new entry.
169 ;; Variable todo-prefix
171 ;; I would like to recommend that you use the prefix "*/*" (by
172 ;; leaving the variable 'todo-prefix' untouched) so that the
173 ;; diary displays each entry every day.
175 ;; To understand what I mean, please read the documentation that
176 ;; goes with the calendar since that will tell you how you can
177 ;; set up the fancy diary display and use the #include command to
178 ;; include your todo list file as part of your diary.
180 ;; If you have the diary package set up to usually display more
181 ;; than one day's entries at once, consider using
183 ;; "&%%(equal (calendar-current-date) date)"
185 ;; as the value of `todo-prefix'. Please note that this may slow
186 ;; down the processing of your diary file some.
188 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
192 ;; might be nicer and to that effect a function has been declared
193 ;; further down in the code. You may wish to auto-load this.
195 ;; Carsten also writes that that *changing* the prefix after the
196 ;; todo list is already established is not as simple as changing
197 ;; the variable - the todo files have to be changed by hand.
199 ;; Variable todo-file-do
201 ;; This variable is fairly self-explanatory. You have to store
202 ;; your TODO list somewhere. This variable tells the package
203 ;; where to go and find this file.
205 ;; Variable todo-file-done
207 ;; Even when you're done, you may wish to retain the entries.
208 ;; Given that they're timestamped and you are offered to add a
209 ;; comment, this can make a useful diary of past events. It will
210 ;; even blend in with the EMACS diary package. So anyway, this
211 ;; variable holds the name of the file for the filed todo-items.
213 ;; Variable todo-file-top
215 ;; File storing the top priorities of your TODO list when
216 ;; todo-save-top-priorities is non-nil. Nice to include in your
217 ;; diary instead of the complete TODO list.
219 ;; Variable todo-mode-hook
221 ;; Just like other modes, too, this mode offers to call your
222 ;; functions before it goes about its business. This variable
223 ;; will be inspected for any functions you may wish to have
224 ;; called once the other TODO mode preparations have been
227 ;; Variable todo-insert-threshold
229 ;; Another nifty feature is the insertion accuracy. If you have
230 ;; 8 items in your TODO list, then you may get asked 4 questions
231 ;; by the binary insertion algorithm. However, you may not
232 ;; really have a need for such accurate priorities amongst your
233 ;; TODO items. If you now think about the binary insertion
234 ;; halfing the size of the window each time, then the threshhold
235 ;; is the window size at which it will stop. If you set the
236 ;; threshhold to zero, the upper and lower bound will coincide at
237 ;; the end of the loop and you will insert your item just before
238 ;; that point. If you set the threshhold to, e.g. 8, it will stop
239 ;; as soon as the window size drops below that amount and will
240 ;; insert the item in the approximate centre of that window. I
241 ;; got the idea for this feature after reading a very helpful
242 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
243 ;; corrected some of my awful coding and pointed me towards some
244 ;; good reading. Thanks Trey!
248 ;; These originally were my ideas, but now also include all the
249 ;; suggestions that I included before forgetting them:
251 ;; o Fancy fonts for todo/top-priority buffer
252 ;; o Remove todo-prefix option in todo-top-priorities
254 ;; o Move entry from one category to another one
255 ;; o Entries which both have the generic */* prefix and a
256 ;; "deadline" entry which are understood by diary, indicating
257 ;; an event (unless marked by &)
258 ;; o The optional COUNT variable of todo-forward-item should be
259 ;; applied to the other functions performing similar tasks
260 ;; o Modularization could be done for repeaded elements of
261 ;; the code, like the completing-read lines of code.
262 ;; o license / version function
263 ;; o export to diary file
266 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
268 ;; o rewrite complete package to store data as lisp objects
269 ;; and have display modes for display, for diary export,
270 ;; etc. (Richard Stallman pointed out this is a bad idea)
271 ;; o so base todo-mode.el on generic-mode.el instead
273 ;; History and Gossip
275 ;; Many thanks to all the ones who have contributed to the
276 ;; evolution of this package! I hope I have listed all of you
277 ;; somewhere in the documentation or at least in the RCS history!
279 ;; Enjoy this package and express your gratitude by sending nice
280 ;; things to my parents' address!
283 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
286 ;; ---------------------------------------------------------------------------
290 ;; $Log: todo-mode.el,v $
291 ;; Revision 1.34 1998/01/12 11:43:22 os10000
292 ;; Added patch from Don Hejna <djhejna@oasis.ambit.com>.
294 ;; Revision 1.33 1997/12/04 17:45:22 os10000
295 ;; Another patch by Michael Cook to fix annotation.
297 ;; Revision 1.32 1997/12/03 12:18:20 os10000
298 ;; Added category patch by Michael R Cook <mcook@cognex.com>.
300 ;; Revision 1.31 1997/10/28 22:16:24 os10000
301 ;; Three insertion options:
302 ;; i without prefix: ask for category, do binary insertion
303 ;; i with prefix: do binary insertion in current category
304 ;; uppercase I: insert directly under cursor
306 ;; Revision 1.30 1997/10/28 21:59:48 os10000
307 ;; Improved documentation, fixed insertion with prefix.
309 ;; Revision 1.29 1997/10/28 21:47:12 os10000
310 ;; Implemented "insert-under-cursor" as suggested by
311 ;; Kai Grossjohann <grossjohann@ls6.cs.uni-dortmund.de>.
313 ;; Revision 1.28 1997/10/28 21:37:05 os10000
314 ;; Incorporated simplifying suggestions from
315 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl>.
317 ;; Revision 1.27 1997/10/28 21:26:55 os10000
318 ;; Patch from Paul Stodghill <stodghil@CS.Cornell.EDU>:
319 ;; The patch below fixes todo-insert-item so that it will
320 ;; insert the item in place, instead of at the top of the
321 ;; buffer, when invoked with a prefix argument.
323 ;; Revision 1.26 1997/10/28 21:14:51 os10000
324 ;; Improvements sent in by Dave Love <d.love@dl.ac.uk>:
325 ;; todo-mode.el: Doc fixes. Customization.
326 ;; (todo-add-item-non-interactively): New arg -- don't dynamically bind ARG.
327 ;; (todo-insert-item): Use it.
329 ;; Revision 1.25 1997/10/28 20:03:27 os10000
330 ;; Harald Backer <harald.backer@fou.telenor.no> sent the following:
331 ;; Added `todo-save-top-priorities' and option to automatically save top
332 ;; priorities file when saving todo-file. Changed some default values.
335 ;; Revision 1.24 1997/10/28 19:41:53 os10000
336 ;; Added fix from Frank Ridderbusch <ridderbusch.pad@sni.de>,
337 ;; an apostrophe was missing.
339 ;; Revision 1.23 1997/10/24 17:30:54 os10000
340 ;; Added three suggestions from Carsten
341 ;; Dominik <dominik@strw.LeidenUniv.nl>:
343 ;; - recommend autoloading instead of require
344 ;; - inserting from different buffer didn't work
345 ;; (now fixed -- I pray)
346 ;; - provided public entry point to insert items
347 ;; from normal lisp code
349 ;; Revision 1.22 1997/10/24 16:53:20 os10000
350 ;; Paul Stodghill <stodghil@CS.Cornell.EDU> writes:
352 ;; When invoked with a prefix, todo-insert-item
353 ;; should not prompt for a category. (He adds:
354 ;; At least that's what I think.)
356 ;; Revision 1.21 1997/10/24 16:51:02 os10000
357 ;; Rafael Laboissiere <rafael@icp.inpg.fr> writes:
359 ;; I was just annoyed with the fact that there is no way
360 ;; to dynamically control the insertion accuracy. I mean:
361 ;; the variable `todo-insert-threshold' does the job, but
362 ;; it is not very handy if one wants to mix the two
363 ;; behaviors (bisection and "insert right here under the
366 ;; Therefore I did a quick hack in the function
367 ;; `todo-insert-item'. Now by giving a prefix argument to
368 ;; the insert command (i.e. by typing "C-u i"), entries
369 ;; are inserted exactly at the line where the cursor is.
370 ;; It would be better to give the value of
371 ;; `todo-insert-threshold' as a numeric argument of
372 ;; `todo-insert-item' (like "M-8 i"), but it's too late
373 ;; now for continuing to hack.
375 ;; Revision 1.20 1997/10/17 15:41:57 os10000
376 ;; Thanks to Harald Backer <harald.backer@fou.telenor.no>, we now have
377 ;; the following facilities available:
379 ;; Added todo-print, todo-top-priorities and todo-jump with matching
380 ;; variables; Parameterized todo-header, todo-category-beg,
381 ;; todo-category-end and todo-category-sep; Added autoload comments;
382 ;; todo-category-select: Modified regexp to make category names unique;
383 ;; todo-forward-item: Added optional COUNT vaiable; todo-insert-item:
384 ;; Rewrote completing read entry.
386 ;; Also, check out the extended list of things left to be done to this
387 ;; package at the end of the documentation!
389 ;; Revision 1.19 1997/10/16 21:21:16 os10000
390 ;; Jari Aalto <jari.aalto@poboxes.com> writes:
392 ;; I just downloaded your package and after reading the docs I
393 ;; decided to do some reformatting. Hope you don't mind. Now
394 ;; they are in such a format that the html page can be
395 ;; automatically generated from the source file. As an example, I
396 ;; generated the attached page using the following command:
397 ;; ripdoc.pls < todo-mode.el | t2html.pls -a "Oliver.Seidel" -e \
398 ;; Oliver.Seidel@cl.cam.ac.uk -simple -base
400 ;; And of course I appreciate it. Jari's stuff can be found at:
401 ;; ftp://cs.uta.fi/pub/ssjaaa/, while I'm making the rev 1.18 page
402 ;; available at http://www.cl.cam.ac.uk/users/os10000/doc/todo-mode.html
403 ;; (That link will be valid until 10/1998 or slightly longer.)
405 ;; Revision 1.18 1997/10/15 17:18:11 os10000
406 ;; Everything seems to work in Harald Melands Emacs 20.02 and
407 ;; my Emacs 19.34. Beware of the spelling in some of the
408 ;; variable names. I looked up "threshold" in a dictionary
409 ;; and here in Britain this appears to be the way to spell it.
411 ;; Revision 1.17 1997/10/15 14:30:41 os10000
412 ;; Attempted to reconcile Harald's changes with mine since 1.15.
414 ;; Revision 1.16 1997/10/15 14:00:12 os10000
415 ;; Fixed 'file-item' and added 20.02 split-string function.
417 ;; Revision 1.15 1997/10/14 22:22:35 os10000
418 ;; Added string-split (which I stole from ediff-util), changed
419 ;; pop-to-buffer to switch-to-buffer and added message on how
420 ;; to exit the multi-line-edit mode.
422 ;; Revision 1.14 1997/10/09 09:24:50 os10000
423 ;; Harald Meland <harald.meland@usit.uio.no> asked for
424 ;; the latest version, got 1.13, and returned this.
427 ;; Thanks a lot for the new version of todo-mode.el. As you will see I
428 ;; have messed it up a bit, hopefully for the better -- I don't like
429 ;; short, cryptic names for variables and functions, so I renamed most of
430 ;; them, and `defalias'ed the old function names. I hope you don't mind
431 ;; too much, I just kinda couldn't stop myself.
433 ;; Additionally, I included some support for multiline entries, cleaned
434 ;; up (IMHO :) a lot of the code, included completion-support for which
435 ;; category to install a new entry in, and possibly some other changes I
438 ;; It's getting rather late, and I have just done some preliminary
439 ;; testing on whether all of this really works, but so far it looks
442 ;; Revision 1.13 1997/08/19 14:00:36 seidel
443 ;; - changed name to todo-mode
444 ;; - fixed menu descriptions
445 ;; - fixed "pressing abort while filing"
446 ;; - attempted Emacs Lisp Manual *Tips* section compliance
448 ;; Revision 1.12 1997/08/06 10:56:15 os10000
449 ;; Fixed header, typos, layout, documentation.
451 ;; Revision 1.11 1997/08/06 09:14:25 os10000
452 ;; Applied patch from Istvan Marko <istvan@cmdmail.amd.com>
453 ;; to make menus work anywhere.
455 ;; Revision 1.10 1997/08/06 08:56:03 os10000
456 ;; Acted upon suggestion from Shane Holder <holder@rsn.hp.com>:
457 ;; Cancelling the editing of an entry will not delete it any more.
459 ;; Revision 1.9 1997/08/06 08:12:03 os10000
460 ;; Improved documentation. Broke some lines to comply with
461 ;; Richard Stallman's email to please keep in sync with the
462 ;; rest of the Emacs distribution files.
464 ;; Revision 1.8 1997/08/05 22:39:04 os10000
465 ;; Made todo-mode.el available under GPL.
467 ;; Revision 1.7 1997/08/05 22:34:14 os10000
468 ;; Fixed insertion routine with help from Trey Jackson
469 ;; <trey@cs.berkeley.edu>; added todo-inst-tresh;
470 ;; fixed keyboard layout to remove unwanted keys.
472 ;; Revision 1.6 1997/08/05 16:47:01 os10000
473 ;; Incorporated menus for XEmacs from Allan.Cochrane@soton.sc.philips.com,
474 ;; fixed TYPO, fixed todo-file-cmd, cleaned up rcs history.
476 ;; Revision 1.5 1997/08/05 14:43:39 os10000
477 ;; Added improvements from Ron Gut <rgut@aware.com>.
478 ;; Added category management.
480 ;; Revision 1.4 1997/08/04 16:18:45 os10000
481 ;; Added Raise/Lower item.
483 ;; Revision 1.3 1997/08/03 12:47:26 os10000
484 ;; Cleaned up variables, prefix and cursor position.
486 ;; Revision 1.2 1997/08/03 12:15:28 os10000
487 ;; It appears to work.
489 ;; Revision 1.1 1997/08/03 12:15:13 os10000
493 ;; ---------------------------------------------------------------------------
497 (eval-and-compile ; Removable for installation in
502 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
503 nil ;; We've got what we needed
504 ;; We have the old custom-library, hack around it!
505 (defmacro defgroup (&rest args)
507 (defmacro defcustom (var value doc &rest args)
508 (` (defvar (, var) (, value) (, doc))))))
510 ;; User-configurable variables:
513 "Maintain a list of todo items."
516 (defcustom todo-prefix "*/*"
517 "*TODO mode prefix for entries.
519 This is useful in conjunction with `calendar' and `diary' if you use
521 #include \"~/.todo-do\"
523 in your diary file to include your todo list file as part of your
524 diary. With the default value \"*/*\" the diary displays each entry
525 every day and it may also be marked on every day of the calendar.
526 Using \"&%%(equal (calendar-current-date) date)\" instead will only
527 show and mark todo entreis for today, but may slow down processing of
528 the diary file somewhat."
531 (defcustom todo-file-do "~/.todo-do"
532 "*TODO mode list file."
535 (defcustom todo-file-done "~/.todo-done"
536 "*TODO mode archive file."
539 (defcustom todo-mode-hook nil
543 (defcustom todo-edit-mode-hook nil
544 "*TODO Edit mode hooks."
547 (defcustom todo-insert-threshold 0
548 "*TODO mode insertion accuracy.
550 If you have 8 items in your TODO list, then you may get asked 4
551 questions by the binary insertion algorithm. However, you may not
552 really have a need for such accurate priorities amongst your TODO
553 items. If you now think about the binary insertion halfing the size
554 of the window each time, then the threshhold is the window size at
555 which it will stop. If you set the threshhold to zero, the upper and
556 lower bound will coincide at the end of the loop and you will insert
557 your item just before that point. If you set the threshhold to,
558 e.g. 8, it will stop as soon as the window size drops below that
559 amount and will insert the item in the approximate centre of that
563 (defvar todo-edit-buffer " *TODO Edit*" "TODO Edit buffer name.")
564 (defcustom todo-file-top "~/.todo-top"
565 "*TODO mode top priorities file.
567 Not in TODO format, but diary compatible.
568 Automatically generated when `todo-save-top-priorities' is non-nil."
572 (defcustom todo-print-function 'ps-print-buffer-with-faces
573 "*Function to print the current buffer."
576 (defcustom todo-show-priorities 1
577 "*Default number of priorities to show by \\[todo-top-priorities].
578 0 means show all entries."
581 (defcustom todo-print-priorities 0
582 "*Default number of priorities to print by \\[todo-print].
583 0 means print all entries."
586 (defcustom todo-remove-separator t
587 "*Non-nil to remove category separators in\
588 \\[todo-top-priorities] and \\[todo-print]."
591 (defcustom todo-save-top-priorities-too t
592 "*Non-nil makes todo-save automatically save top-priorities in
597 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
598 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
600 (defcustom todo-time-string-format
601 "%:y-%02m-%02d %02H:%02M"
602 "*TODO mode time string format for done entries.
603 For details see the variable `time-stamp-format'."
607 (defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
608 "*Function producing text to insert at start of todo entry."
611 (defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
612 "*Initials of todo item author."
616 (defun todo-entry-timestamp-initials ()
617 "Prepend timestamp and your initials to the head of a TODO entry."
618 (let ((time-stamp-format todo-time-string-format))
619 (concat (time-stamp-string) " " todo-initials ": ")))
621 ;; ---------------------------------------------------------------------------
623 ;; Get some outside help ...
625 (require 'time-stamp)
628 ;; ---------------------------------------------------------------------------
630 ;; Set up some helpful context ...
632 (defvar todo-categories nil "TODO categories.")
633 (defvar todo-cats nil
634 "Old variable for holding the TODO categories.
635 Use `todo-categories' instead.")
636 (defvar todo-previous-line 0 "Previous line that I asked about.")
637 (defvar todo-previous-answer 0 "Previous answer that I got.")
638 (defvar todo-mode-map nil "TODO mode keymap.")
639 (defvar todo-category-number 0 "TODO category number.")
641 (defvar todo-tmp-buffer-name "*Tmp*")
643 (defvar todo-category-sep (make-string 75 ?-)
644 "Category separator.")
645 (defvar todo-category-beg " --- "
646 "Category start separator to be prepended onto category name.")
647 (defvar todo-category-end "--- End"
648 "Separator after a category.")
649 (defvar todo-header "-*- mode: todo; "
650 "Header of todo files.")
654 ;; ---------------------------------------------------------------------------
658 (let ((map (make-keymap)))
659 (suppress-keymap map t)
660 (define-key map "+" 'todo-forward-category)
661 (define-key map "-" 'todo-backward-category)
662 (define-key map "d" 'todo-file-item) ;done/delete
663 (define-key map "e" 'todo-edit-item)
664 (define-key map "E" 'todo-edit-multiline)
665 (define-key map "f" 'todo-file-item)
666 (define-key map "i" 'todo-insert-item)
667 (define-key map "I" 'todo-insert-item-here)
668 (define-key map "j" 'todo-jump-to-category)
669 (define-key map "k" 'todo-delete-item)
670 (define-key map "l" 'todo-lower-item)
671 (define-key map "n" 'todo-forward-item)
672 (define-key map "p" 'todo-backward-item)
673 (define-key map "P" 'todo-print)
674 (define-key map "q" 'todo-quit)
675 (define-key map "r" 'todo-raise-item)
676 (define-key map "s" 'todo-save)
677 (define-key map "S" 'todo-save-top-priorities)
678 (define-key map "t" 'todo-top-priorities)
679 (setq todo-mode-map map)))
681 (defun todo-category-select ()
682 "Make TODO mode display the current category correctly."
683 (let ((name (nth todo-category-number todo-categories)))
684 (setq mode-line-buffer-identification
685 ;; (concat "Category: " name))
686 (concat "Category: " (format "%18s" name)))
688 (goto-char (point-min))
689 (search-forward-regexp
691 (regexp-quote (concat todo-prefix todo-category-beg name))
693 (let ((begin (1+ (point-at-eol))))
694 (search-forward-regexp (concat "^" todo-category-end))
695 (narrow-to-region begin (point-at-bol))
696 (goto-char (point-min)))))
697 (defalias 'todo-cat-slct 'todo-category-select)
699 (defun todo-forward-category () "Go forward to TODO list of next category."
701 (setq todo-category-number
702 (mod (1+ todo-category-number) (length todo-categories)))
703 (todo-category-select))
704 (defalias 'todo-cmd-forw 'todo-forward-category)
706 (defun todo-backward-category () "Go back to TODO list of previous category."
708 (setq todo-category-number
709 (mod (1- todo-category-number) (length todo-categories)))
710 (todo-category-select))
711 (defalias 'todo-cmd-back 'todo-backward-category)
713 (defun todo-backward-item () "Select previous entry of TODO list."
715 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
717 (defalias 'todo-cmd-prev 'todo-backward-item)
719 (defun todo-forward-item (&optional count)
720 "Select COUNT-th next entry of TODO list."
722 (if (listp count) (setq count (car count)))
724 (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
728 (defalias 'todo-cmd-next 'todo-forward-item)
730 (defun todo-save () "Save the TODO list."
733 (if todo-save-top-priorities-too (todo-save-top-priorities))
735 (defalias 'todo-cmd-save 'todo-save)
737 (defun todo-quit () "Done with TODO list for now."
743 (defalias 'todo-cmd-done 'todo-quit)
745 (defun todo-edit-item () "Edit current TODO list entry."
747 (let ((item (todo-item-string)))
748 (if (todo-string-multiline-p item)
749 (todo-edit-multiline)
750 (let ((new (read-from-minibuffer "Edit: " item)))
755 (defalias 'todo-cmd-edit 'todo-edit-item)
757 (defun todo-edit-multiline ()
758 "Set up a buffer for editing a multiline TODO list entry."
760 (let ((buffer-name (generate-new-buffer-name todo-edit-buffer)))
762 (make-indirect-buffer
763 (file-name-nondirectory todo-file-do) buffer-name))
764 (message "To exit, simply kill this buffer and return to list.")
766 (narrow-to-region (todo-item-start) (todo-item-end))))
769 (defun todo-add-category (cat)
770 "Add new category CAT to the TODO list."
771 (interactive "sCategory: ")
772 (save-window-excursion
773 (setq todo-categories (cons cat todo-categories))
774 (find-file todo-file-do)
776 (goto-char (point-min))
777 (let ((posn (search-forward "-*- mode: todo; " 17 t)))
778 (if (not (null posn)) (goto-char posn))
781 (insert "-*- mode: todo; \n")
784 (insert (format "todo-categories: %S; -*-" todo-categories))
786 (insert (format "%s%s%s\n%s\n%s %s\n"
787 todo-prefix todo-category-beg cat
789 todo-prefix todo-category-sep)))
793 (defun todo-add-item-non-interactively (new-item category)
794 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
798 (if (string= "" category)
799 (setq category (nth todo-category-number todo-categories)))
800 (let ((cat-exists (member category todo-categories)))
801 (setq todo-category-number
803 (- (length todo-categories) (length cat-exists))
804 (todo-add-category category))))
806 (setq todo-previous-line 0)
808 (bottom (1+ (count-lines (point-min) (point-max)))))
809 (while (> (- bottom top) todo-insert-threshold)
810 (let* ((current (/ (+ top bottom) 2))
811 (answer (if (< current bottom)
812 (todo-more-important-p current) nil)))
814 (setq bottom current)
815 (setq top (1+ current)))))
816 (setq top (/ (+ top bottom) 2))
817 ;; goto-line doesn't have the desired behavior in a narrowed buffer
818 (goto-char (point-min))
819 (forward-line (1- top)))
820 (insert new-item "\n")
826 (defun todo-insert-item (ARG)
827 "Insert new TODO list entry.
828 With a prefix argument solicit the category, otherwise use the current
832 (if (not (string-equal mode-name "TODO")) (todo-show))
833 (let* ((new-item (concat todo-prefix " "
834 (read-from-minibuffer
836 (if todo-entry-prefix-function
837 (funcall todo-entry-prefix-function)))))
838 (categories todo-categories)
839 (history (cons 'categories (1+ todo-category-number)))
840 (current-category (nth todo-category-number todo-categories))
846 current-category "]: ")
847 (todo-category-alist) nil nil nil history))))
848 (todo-add-item-non-interactively new-item category))))
850 (defalias 'todo-cmd-inst 'todo-insert-item)
853 (defun todo-insert-item-here ()
854 "Insert new TODO list entry under the cursor."
857 (if (not (string-equal mode-name "TODO")) (todo-show))
858 (let* ((new-item (concat todo-prefix " "
859 (read-from-minibuffer
861 (if todo-entry-prefix-function
862 (funcall todo-entry-prefix-function))))))
863 (insert (concat new-item "\n")))))
865 (defun todo-more-important-p (line)
866 "Ask whether entry is more important than the one at LINE."
867 (if (not (equal todo-previous-line line))
869 (setq todo-previous-line line)
870 (goto-char (point-min))
871 (forward-line (1- todo-previous-line))
872 (let ((item (todo-item-string-start)))
873 (setq todo-previous-answer
874 (y-or-n-p (concat "More important than '" item "'? "))))))
875 todo-previous-answer)
876 (defalias 'todo-ask-p 'todo-more-important-p)
878 (defun todo-delete-item () "Delete current TODO list entry."
880 (if (> (count-lines (point-min) (point-max)) 0)
881 (let* ((todo-entry (todo-item-string-start))
882 (todo-answer (y-or-n-p (concat "Permanently remove '"
887 (todo-backward-item)))
889 (error "No TODO list entry to delete")))
890 (defalias 'todo-cmd-kill 'todo-delete-item)
892 (defun todo-raise-item () "Raise priority of current entry."
894 (if (> (count-lines (point-min) (point)) 0)
895 (let ((item (todo-item-string)))
901 (error "No TODO list entry to raise")))
902 (defalias 'todo-cmd-rais 'todo-raise-item)
904 (defun todo-lower-item () "Lower priority of current entry."
906 (if (> (count-lines (point) (point-max)) 1)
907 ;; Assume there is a final newline
908 (let ((item (todo-item-string)))
914 (error "No TODO list entry to lower")))
915 (defalias 'todo-cmd-lowr 'todo-lower-item)
917 (defun todo-file-item (&optional comment)
918 "File the current TODO list entry away,
919 annotated with an optional COMMENT."
920 (interactive "sComment: ")
921 (or (> (count-lines (point-min) (point-max)) 0)
922 (error "No TODO list entry to file away"))
923 (let ((time-stamp-format todo-time-string-format))
924 (if (and comment (> (length comment) 0))
926 (goto-char (todo-item-end))
928 (if (save-excursion (beginning-of-line)
929 (looking-at (regexp-quote todo-prefix)))
933 (goto-char (todo-item-end))
934 (insert " [" (nth todo-category-number todo-categories) "]")
935 (goto-char (todo-item-start))
936 (let ((temp-point (point)))
937 (if (looking-at (regexp-quote todo-prefix))
938 (replace-match (time-stamp-string))
939 ;; Standard prefix -> timestamp
940 ;; Else prefix non-standard item start with timestamp
941 (insert (time-stamp-string)))
942 (append-to-file temp-point (1+ (todo-item-end)) todo-file-done)
943 (delete-region temp-point (1+ (todo-item-end))))
947 ;; ---------------------------------------------------------------------------
949 ;; Utility functions:
953 (defun todo-top-priorities (&optional nof-priorities category-pr-page)
954 "List top priorities for each category.
956 Number of entries for each category is given by NOF-PRIORITIES which
957 defaults to \'todo-show-priorities\'.
959 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
960 between each category."
963 (or nof-priorities (setq nof-priorities todo-show-priorities))
964 (if (listp nof-priorities) ;universal argument
965 (setq nof-priorities (car nof-priorities)))
966 (let ((todo-print-buffer-name "*Tmp*")
967 ;;(todo-print-category-number 0)
968 (todo-category-break (if category-pr-page "
\f" ""))
971 (if todo-remove-separator
972 (concat todo-category-end "\n"
973 (regexp-quote todo-prefix) " " todo-category-sep "\n")
974 (concat todo-category-end "\n"))))
980 (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
981 (set-buffer todo-print-buffer-name)
982 (goto-char (point-min))
983 (if (re-search-forward (regexp-quote todo-header) nil t)
985 (beginning-of-line 1)
986 (kill-line))) ;Remove mode line
987 (while (re-search-forward ;Find category start
988 (regexp-quote (concat todo-prefix todo-category-beg))
990 (setq beg (+ (point-at-eol) 1)) ;Start of first entry.
991 (re-search-forward cat-end nil t)
992 (setq end (match-beginning 0))
993 (replace-match todo-category-break)
994 (narrow-to-region beg end) ;In case we have too few entries.
995 (goto-char (point-min))
996 (if (= 0 nof-priorities) ;Traverse entries.
997 (goto-char end) ;All entries
998 (todo-forward-item nof-priorities))
1000 (delete-region beg end)
1002 (and (looking-at "
\f") (replace-match "")) ;Remove trailing form-feed.
1003 (goto-char (point-min)) ;Due to display buffer
1005 ;; Could have used switch-to-buffer as it has a norecord argument,
1006 ;; which is nice when we are called from e.g. todo-print.
1007 ;; Else we could have used pop-to-buffer.
1008 (display-buffer todo-print-buffer-name)
1009 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
1010 todo-print-buffer-name)
1014 (defun todo-save-top-priorities (&optional nof-priorities)
1015 "Save top priorities for each category in `todo-file-top'.
1017 Number of entries for each category is given by NOF-PRIORITIES which
1018 defaults to `todo-show-priorities'."
1020 (save-window-excursion
1023 (todo-top-priorities nof-priorities)
1024 (set-buffer todo-tmp-buffer-name)
1025 (write-file todo-file-top)
1030 (defun todo-print (&optional category-pr-page)
1031 "Print todo summary using \\\[todo-print-function].
1032 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
1033 between each category.
1035 Number of entries for each category is given by
1036 \'todo-print-priorities\'."
1038 (if todo-print-function
1040 (save-window-excursion
1043 (todo-top-priorities todo-print-priorities
1045 (set-buffer todo-tmp-buffer-name)
1046 (and (funcall todo-print-function)
1048 (message "Todo printing done."))
1050 (message "todo-print-function undefinded")
1053 (defun todo-jump-to-category ()
1054 "Jump to a category. Default is previous category."
1056 (let* ((categories todo-categories)
1057 (history (cons 'categories (1+ todo-category-number)))
1058 (category (completing-read
1059 (concat "Category ["
1060 (nth todo-category-number todo-categories) "]: ")
1061 (todo-category-alist) nil nil nil history)))
1062 (if (string= "" category)
1063 (setq category (nth todo-category-number todo-categories)))
1064 (setq todo-category-number
1065 (if (member category todo-categories)
1066 (- (length todo-categories)
1067 (length (member category todo-categories)))
1068 (todo-add-category category)))
1071 (defun todo-line-string () "Return current line in buffer as a string."
1072 (buffer-substring (point-at-bol) (point-at-eol)))
1074 (defun todo-item-string-start ()
1075 "Return the start of this TODO list entry as a string."
1076 ;; Suitable for putting in the minibuffer when asking the user
1077 (let ((item (todo-item-string)))
1078 (if (> (length item) 60)
1079 (setq item (concat (substring item 0 56) "...")))
1082 (defun todo-item-start () "Return point at start of current TODO list item."
1085 (if (not (looking-at (regexp-quote todo-prefix)))
1086 (search-backward-regexp
1087 (concat "^" (regexp-quote todo-prefix)) nil t))
1090 (defun todo-item-end () "Return point at end of current TODO list item."
1093 (search-forward-regexp
1094 (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
1095 (1- (point-at-bol))))
1097 (defun todo-remove-item () "Delete the current entry from the TODO list."
1098 (delete-region (todo-item-start) (1+ (todo-item-end))))
1100 (defun todo-item-string () "Return current TODO list entry as a string."
1101 (buffer-substring (todo-item-start) (todo-item-end)))
1103 (defun todo-string-count-lines (string)
1104 "Return the number of lines STRING spans."
1105 (length (split-string string "\n")))
1107 (defun todo-string-multiline-p (string)
1108 "Return non-nil if STRING spans several lines."
1109 (> (todo-string-count-lines string) 1))
1111 (defun todo-category-alist ()
1112 "Generate an alist for use in `completing-read' from `todo-categories'."
1113 (mapcar (lambda (cat) (cons cat nil))
1116 ;; utility functions: These are available in XEmacs, but not in Emacs 19.34
1118 (if (not (fboundp 'point-at-bol))
1119 (defun point-at-bol () "Return value of point at beginning of line."
1124 (if (not (fboundp 'point-at-eol))
1125 (defun point-at-eol () "Return value of point at end of line."
1130 ;; splits at a white space, returns a list
1131 (if (not (fboundp 'split-string))
1132 (defun split-string (string &optional separators)
1133 "Splits STRING into substrings where there are matches for SEPARATORS.
1134 Each match for SEPARATORS is a splitting point.
1135 The substrings between the splitting points are made into a list
1137 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
1138 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1141 (while (string-match rexp string start)
1142 (or (eq (match-beginning 0) 0)
1144 (cons (substring string start (match-beginning 0))
1146 (setq start (match-end 0)))
1147 (or (eq start (length string))
1149 (cons (substring string start)
1153 ;; ---------------------------------------------------------------------------
1155 (easy-menu-define todo-menu todo-mode-map "Todo Menu"
1157 ["Next category" todo-forward-category t]
1158 ["Previous category" todo-backward-category t]
1159 ["Jump to category" todo-jump-to-category t]
1160 ["Show top priority items" todo-top-priorities t]
1161 ["Print categories" todo-print t]
1163 ["Edit item" todo-edit-item t]
1164 ["File item" todo-file-item t]
1165 ["Insert new item" todo-insert-item t]
1166 ["Insert item here" todo-insert-item-here t]
1167 ["Kill item" todo-delete-item t]
1169 ["Lower item priority" todo-lower-item t]
1170 ["Raise item priority" todo-raise-item t]
1172 ["Next item" todo-forward-item t]
1173 ["Previous item" todo-backward-item t]
1175 ["Save" todo-save t]
1176 ["Save Top Priorities" todo-save-top-priorities t]
1178 ["Quit" todo-quit t]
1181 ;; As calendar reads .todo-do before todo-mode is loaded.
1183 (defun todo-mode () "Major mode for editing TODO lists.\n\n\\{todo-mode-map}"
1185 (setq major-mode 'todo-mode)
1186 (setq mode-name "TODO")
1187 (use-local-map todo-mode-map)
1188 (easy-menu-add todo-menu)
1189 (run-hooks 'todo-mode-hook))
1191 ;; Read about this function in the setup instructions above!
1194 "Make a diary entry appear only in the current date's diary"
1195 (if (equal (calendar-current-date) date)
1199 (defun todo-edit-mode ()
1200 "Major mode for editing items in the TODO list\n\n\\{todo-edit-mode-map}"
1202 (setq major-mode 'todo-edit-mode)
1203 (setq mode-name "TODO Edit")
1204 (run-hooks 'todo-edit-mode-hook))
1207 (defun todo-show () "Show TODO list."
1209 (if (file-exists-p todo-file-do)
1210 (find-file todo-file-do)
1211 (todo-initial-setup))
1212 (if (null todo-categories)
1213 (if (null todo-cats)
1214 (error "Error in %s: No categories in list `todo-categories'"
1216 (goto-char (point-min))
1217 (and (search-forward "todo-cats:" nil t)
1218 (replace-match "todo-categories:"))
1219 (make-local-variable 'todo-categories)
1220 (setq todo-categories todo-cats)))
1222 (todo-category-select))
1224 (defun todo-initial-setup () "Set up things to work properly in TODO mode."
1225 (find-file todo-file-do)
1228 (todo-add-category "Todo"))
1230 (provide 'todo-mode)
1232 ;; ---------------------------------------------------------------------------
1233 ;;; todo-mode.el ends here
1234 ;; ---------------------------------------------------------------------------