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.29 1997/10/28 21:47:12 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 ;; 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
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:
75 ;; (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
76 ;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
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
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
97 ;; Which version of todo-mode.el does this documentation refer to?
99 ;; $Id: todo-mode.el,v 1.29 1997/10/28 21:47:12 os10000 Exp os10000 $
103 ;; This package will require the following packages to be
104 ;; available on the load-path:
111 ;; You will have the following facilities available:
113 ;; M-x todo-show will enter the todo list screen, here type
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 ;; E to edit a multi-line entry
121 ;; f to file the current entry, including a
122 ;; comment and timestamp
123 ;; i to insert a new entry
124 ;; I to insert a new entry at current cursor position
125 ;; j jump to category
126 ;; k to kill the current entry
127 ;; l to lower the current entry's priority
128 ;; n for the next entry
129 ;; p for the previous entry
131 ;; q to save the list and exit the buffer
132 ;; r to raise the current entry's priority
133 ;; s to save the list
134 ;; S to save the list of top priorities
135 ;; t show top priority items for each category
137 ;; When you add a new entry, you are asked for the text and then
138 ;; for the category. I for example have categories for things
139 ;; that I want to do in the office (like mail my mum), that I
140 ;; want to do in town (like buy cornflakes) and things I want to
141 ;; do at home (move my suitcases). The categories can be
142 ;; selected with the cursor keys and if you type in the name of a
143 ;; category which didn't exist before, an empty category of the
144 ;; desired name will be added and filled with the new entry.
148 ;; Variable todo-prefix
150 ;; I would like to recommend that you use the prefix "*/*" (by
151 ;; leaving the variable 'todo-prefix' untouched) so that the
152 ;; diary displays each entry every day.
154 ;; To understand what I mean, please read the documentation that
155 ;; goes with the calendar since that will tell you how you can
156 ;; set up the fancy diary display and use the #include command to
157 ;; include your todo list file as part of your diary.
159 ;; If you have the diary package set up to usually display more
160 ;; than one day's entries at once, consider using
162 ;; "&%%(equal (calendar-current-date) date)"
164 ;; as the value of `todo-prefix'. Please note that this may slow
165 ;; down the processing of your diary file some.
167 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
171 ;; might be nicer and to that effect a function has been declared
172 ;; further down in the code. You may wish to auto-load this.
174 ;; Carsten also writes that that *changing* the prefix after the
175 ;; todo list is already established is not as simple as changing
176 ;; the variable - the todo files have to be changed by hand.
178 ;; Variable todo-file-do
180 ;; This variable is fairly self-explanatory. You have to store
181 ;; your TODO list somewhere. This variable tells the package
182 ;; where to go and find this file.
184 ;; Variable todo-file-done
186 ;; Even when you're done, you may wish to retain the entries.
187 ;; Given that they're timestamped and you are offered to add a
188 ;; comment, this can make a useful diary of past events. It will
189 ;; even blend in with the EMACS diary package. So anyway, this
190 ;; variable holds the name of the file for the filed todo-items.
192 ;; Variable todo-file-top
194 ;; File storing the top priorities of your TODO list when
195 ;; todo-save-top-priorities is non-nil. Nice to include in your
196 ;; diary instead of the complete TODO list.
198 ;; Variable todo-mode-hook
200 ;; Just like other modes, too, this mode offers to call your
201 ;; functions before it goes about its business. This variable
202 ;; will be inspected for any functions you may wish to have
203 ;; called once the other TODO mode preparations have been
206 ;; Variable todo-insert-threshold
208 ;; Another nifty feature is the insertion accuracy. If you have
209 ;; 8 items in your TODO list, then you may get asked 4 questions
210 ;; by the binary insertion algorithm. However, you may not
211 ;; really have a need for such accurate priorities amongst your
212 ;; TODO items. If you now think about the binary insertion
213 ;; halfing the size of the window each time, then the threshhold
214 ;; is the window size at which it will stop. If you set the
215 ;; threshhold to zero, the upper and lower bound will coincide at
216 ;; the end of the loop and you will insert your item just before
217 ;; that point. If you set the threshhold to, e.g. 8, it will stop
218 ;; as soon as the window size drops below that amount and will
219 ;; insert the item in the approximate centre of that window. I
220 ;; got the idea for this feature after reading a very helpful
221 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
222 ;; corrected some of my awful coding and pointed me towards some
223 ;; good reading. Thanks Trey!
227 ;; These originally were my ideas, but now also include all the
228 ;; suggestions that I included before forgetting them:
230 ;; o Fancy fonts for todo/top-priority buffer
231 ;; o Remove todo-prefix option in todo-top-priorities
233 ;; o Move entry from one category to another one
234 ;; o Entries which both have the generic */* prefix and a
235 ;; "deadline" entry which are understood by diary, indicating
236 ;; an event (unless marked by &)
237 ;; o The optional COUNT variable of todo-forward-item should be
238 ;; applied to the other functions performing similar tasks
239 ;; o Modularization could be done for repeaded elements of
240 ;; the code, like the completing-read lines of code.
241 ;; o license / version function
242 ;; o export to diary file
245 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
247 ;; o rewrite complete package to store data as lisp objects
248 ;; and have display modes for display, for diary export,
249 ;; etc. (Richard Stallman pointed out this is a bad idea)
250 ;; o so base todo-mode.el on generic-mode.el instead
252 ;; History and Gossip
254 ;; Many thanks to all the ones who have contributed to the
255 ;; evolution of this package! I hope I have listed all of you
256 ;; somewhere in the documentation or at least in the RCS history!
258 ;; Enjoy this package and express your gratitude by sending nice
259 ;; things to my parents' address!
262 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
265 ;; ---------------------------------------------------------------------------
269 ;; $Log: todo-mode.el,v $
270 ;; Revision 1.29 1997/10/28 21:47:12 os10000
271 ;; Implemented "insert-under-cursor" as suggested by
272 ;; Kai Grossjohann <grossjohann@ls6.cs.uni-dortmund.de>.
274 ;; Revision 1.28 1997/10/28 21:37:05 os10000
275 ;; Incorporated simplifying suggestions from
276 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl>.
278 ;; Revision 1.27 1997/10/28 21:26:55 os10000
279 ;; Patch from Paul Stodghill <stodghil@CS.Cornell.EDU>:
280 ;; The patch below fixes todo-insert-item so that it will
281 ;; insert the item in place, instead of at the top of the
282 ;; buffer, when invoked with a prefix argument.
284 ;; Revision 1.26 1997/10/28 21:14:51 os10000
285 ;; Improvements sent in by Dave Love <d.love@dl.ac.uk>:
286 ;; todo-mode.el: Doc fixes. Customization.
287 ;; (todo-add-item-non-interactively): New arg -- don't dynamically bind ARG.
288 ;; (todo-insert-item): Use it.
290 ;; Revision 1.25 1997/10/28 20:03:27 os10000
291 ;; Harald Backer <harald.backer@fou.telenor.no> sent the following:
292 ;; Added `todo-save-top-priorities' and option to automatically save top
293 ;; priorities file when saving todo-file. Changed some default values.
296 ;; Revision 1.24 1997/10/28 19:41:53 os10000
297 ;; Added fix from Frank Ridderbusch <ridderbusch.pad@sni.de>,
298 ;; an apostrophe was missing.
300 ;; Revision 1.23 1997/10/24 17:30:54 os10000
301 ;; Added three suggestions from Carsten
302 ;; Dominik <dominik@strw.LeidenUniv.nl>:
304 ;; - recommend autoloading instead of require
305 ;; - inserting from different buffer didn't work
306 ;; (now fixed -- I pray)
307 ;; - provided public entry point to insert items
308 ;; from normal lisp code
310 ;; Revision 1.22 1997/10/24 16:53:20 os10000
311 ;; Paul Stodghill <stodghil@CS.Cornell.EDU> writes:
313 ;; When invoked with a prefix, todo-insert-item
314 ;; should not prompt for a category. (He adds:
315 ;; At least that's what I think.)
317 ;; Revision 1.21 1997/10/24 16:51:02 os10000
318 ;; Rafael Laboissiere <rafael@icp.inpg.fr> writes:
320 ;; I was just annoyed with the fact that there is no way
321 ;; to dynamically control the insertion accuracy. I mean:
322 ;; the variable `todo-insert-threshold' does the job, but
323 ;; it is not very handy if one wants to mix the two
324 ;; behaviors (bisection and "insert right here under the
327 ;; Therefore I did a quick hack in the function
328 ;; `todo-insert-item'. Now by giving a prefix argument to
329 ;; the insert command (i.e. by typing "C-u i"), entries
330 ;; are inserted exactly at the line where the cursor is.
331 ;; It would be better to give the value of
332 ;; `todo-insert-threshold' as a numeric argument of
333 ;; `todo-insert-item' (like "M-8 i"), but it's too late
334 ;; now for continuing to hack.
336 ;; Revision 1.20 1997/10/17 15:41:57 os10000
337 ;; Thanks to Harald Backer <harald.backer@fou.telenor.no>, we now have
338 ;; the following facilities available:
340 ;; Added todo-print, todo-top-priorities and todo-jump with matching
341 ;; variables; Parameterized todo-header, todo-category-beg,
342 ;; todo-category-end and todo-category-sep; Added autoload comments;
343 ;; todo-category-select: Modified regexp to make category names unique;
344 ;; todo-forward-item: Added optional COUNT vaiable; todo-insert-item:
345 ;; Rewrote completing read entry.
347 ;; Also, check out the extended list of things left to be done to this
348 ;; package at the end of the documentation!
350 ;; Revision 1.19 1997/10/16 21:21:16 os10000
351 ;; Jari Aalto <jari.aalto@poboxes.com> writes:
353 ;; I just downloaded your package and after reading the docs I
354 ;; decided to do some reformatting. Hope you don't mind. Now
355 ;; they are in such a format that the html page can be
356 ;; automatically generated from the source file. As an example, I
357 ;; generated the attached page using the following command:
358 ;; ripdoc.pls < todo-mode.el | t2html.pls -a "Oliver.Seidel" -e \
359 ;; Oliver.Seidel@cl.cam.ac.uk -simple -base
361 ;; And of course I appreciate it. Jari's stuff can be found at:
362 ;; ftp://cs.uta.fi/pub/ssjaaa/, while I'm making the rev 1.18 page
363 ;; available at http://www.cl.cam.ac.uk/users/os10000/doc/todo-mode.html
364 ;; (That link will be valid until 10/1998 or slightly longer.)
366 ;; Revision 1.18 1997/10/15 17:18:11 os10000
367 ;; Everything seems to work in Harald Melands Emacs 20.02 and
368 ;; my Emacs 19.34. Beware of the spelling in some of the
369 ;; variable names. I looked up "threshold" in a dictionary
370 ;; and here in Britain this appears to be the way to spell it.
372 ;; Revision 1.17 1997/10/15 14:30:41 os10000
373 ;; Attempted to reconcile Harald's changes with mine since 1.15.
375 ;; Revision 1.16 1997/10/15 14:00:12 os10000
376 ;; Fixed 'file-item' and added 20.02 split-string function.
378 ;; Revision 1.15 1997/10/14 22:22:35 os10000
379 ;; Added string-split (which I stole from ediff-util), changed
380 ;; pop-to-buffer to switch-to-buffer and added message on how
381 ;; to exit the multi-line-edit mode.
383 ;; Revision 1.14 1997/10/09 09:24:50 os10000
384 ;; Harald Meland <harald.meland@usit.uio.no> asked for
385 ;; the latest version, got 1.13, and returned this.
388 ;; Thanks a lot for the new version of todo-mode.el. As you will see I
389 ;; have messed it up a bit, hopefully for the better -- I don't like
390 ;; short, cryptic names for variables and functions, so I renamed most of
391 ;; them, and `defalias'ed the old function names. I hope you don't mind
392 ;; too much, I just kinda couldn't stop myself.
394 ;; Additionally, I included some support for multiline entries, cleaned
395 ;; up (IMHO :) a lot of the code, included completion-support for which
396 ;; category to install a new entry in, and possibly some other changes I
399 ;; It's getting rather late, and I have just done some preliminary
400 ;; testing on whether all of this really works, but so far it looks
403 ;; Revision 1.13 1997/08/19 14:00:36 seidel
404 ;; - changed name to todo-mode
405 ;; - fixed menu descriptions
406 ;; - fixed "pressing abort while filing"
407 ;; - attempted Emacs Lisp Manual *Tips* section compliance
409 ;; Revision 1.12 1997/08/06 10:56:15 os10000
410 ;; Fixed header, typos, layout, documentation.
412 ;; Revision 1.11 1997/08/06 09:14:25 os10000
413 ;; Applied patch from Istvan Marko <istvan@cmdmail.amd.com>
414 ;; to make menus work anywhere.
416 ;; Revision 1.10 1997/08/06 08:56:03 os10000
417 ;; Acted upon suggestion from Shane Holder <holder@rsn.hp.com>:
418 ;; Cancelling the editing of an entry will not delete it any more.
420 ;; Revision 1.9 1997/08/06 08:12:03 os10000
421 ;; Improved documentation. Broke some lines to comply with
422 ;; Richard Stallman's email to please keep in sync with the
423 ;; rest of the Emacs distribution files.
425 ;; Revision 1.8 1997/08/05 22:39:04 os10000
426 ;; Made todo-mode.el available under GPL.
428 ;; Revision 1.7 1997/08/05 22:34:14 os10000
429 ;; Fixed insertion routine with help from Trey Jackson
430 ;; <trey@cs.berkeley.edu>; added todo-inst-tresh;
431 ;; fixed keyboard layout to remove unwanted keys.
433 ;; Revision 1.6 1997/08/05 16:47:01 os10000
434 ;; Incorporated menus for XEmacs from Allan.Cochrane@soton.sc.philips.com,
435 ;; fixed TYPO, fixed todo-file-cmd, cleaned up rcs history.
437 ;; Revision 1.5 1997/08/05 14:43:39 os10000
438 ;; Added improvements from Ron Gut <rgut@aware.com>.
439 ;; Added category management.
441 ;; Revision 1.4 1997/08/04 16:18:45 os10000
442 ;; Added Raise/Lower item.
444 ;; Revision 1.3 1997/08/03 12:47:26 os10000
445 ;; Cleaned up variables, prefix and cursor position.
447 ;; Revision 1.2 1997/08/03 12:15:28 os10000
448 ;; It appears to work.
450 ;; Revision 1.1 1997/08/03 12:15:13 os10000
454 ;; ---------------------------------------------------------------------------
458 (eval-and-compile ; Removable for installation in
463 (if (and (featurep 'custom
) (fboundp 'custom-declare-variable
))
464 nil
;; We've got what we needed
465 ;; We have the old custom-library, hack around it!
466 (defmacro defgroup
(&rest args
)
468 (defmacro defcustom
(var value doc
&rest args
)
469 (` (defvar (, var
) (, value
) (, doc
))))))
471 ;; User-configurable variables:
474 "Maintain a list of todo items."
477 (defcustom todo-prefix
"*/*"
478 "*TODO mode prefix for entries.
480 This is useful in conjunction with `calendar' and `diary' if you use
482 #include \"~/.todo-do\"
484 in your diary file to include your todo list file as part of your
485 diary. With the default value \"*/*\" the diary displays each entry
486 every day and it may also be marked on every day of the calendar.
487 Using \"&%%(equal (calendar-current-date) date)\" instead will only
488 show and mark todo entreis for today, but may slow down processing of
489 the diary file somewhat."
492 (defcustom todo-file-do
"~/.todo-do"
493 "*TODO mode list file."
496 (defcustom todo-file-done
"~/.todo-done"
497 "*TODO mode archive file."
500 (defcustom todo-mode-hook nil
504 (defcustom todo-edit-mode-hook nil
505 "*TODO Edit mode hooks."
508 (defcustom todo-insert-threshold
0
509 "*TODO mode insertion accuracy.
511 If you have 8 items in your TODO list, then you may get asked 4
512 questions by the binary insertion algorithm. However, you may not
513 really have a need for such accurate priorities amongst your TODO
514 items. If you now think about the binary insertion halfing the size
515 of the window each time, then the threshhold is the window size at
516 which it will stop. If you set the threshhold to zero, the upper and
517 lower bound will coincide at the end of the loop and you will insert
518 your item just before that point. If you set the threshhold to,
519 e.g. 8, it will stop as soon as the window size drops below that
520 amount and will insert the item in the approximate centre of that
524 (defvar todo-edit-buffer
" *TODO Edit*" "TODO Edit buffer name.")
525 (defcustom todo-file-top
"~/.todo-top"
526 "*TODO mode top priorities file.
528 Not in TODO format, but diary compatible.
529 Automatically generated when `todo-save-top-priorities' is non-nil."
533 (defcustom todo-print-function
'ps-print-buffer-with-faces
534 "*Function to print the current buffer."
537 (defcustom todo-show-priorities
1
538 "*Default number of priorities to show by \\[todo-top-priorities].
539 0 means show all entries."
542 (defcustom todo-print-priorities
0
543 "*Default number of priorities to print by \\[todo-print].
544 0 means print all entries."
547 (defcustom todo-remove-separator t
548 "*Non-nil to remove category separators in\
549 \\[todo-top-priorities] and \\[todo-print]."
552 (defcustom todo-save-top-priorities-too t
553 "*Non-nil makes todo-save automatically save top-priorities in
558 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
559 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
561 (defcustom todo-time-string-format
562 "%:y-%02m-%02d %02H:%02M"
563 "*TODO mode time string format for done entries.
564 For details see the variable `time-stamp-format'."
568 (defcustom todo-entry-prefix-function
'todo-entry-timestamp-initials
569 "*Function producing text to insert at start of todo entry."
572 (defcustom todo-initials
(or (getenv "INITIALS") (user-login-name))
573 "*Initials of todo item author."
577 (defun todo-entry-timestamp-initials ()
578 "Prepend timestamp and your initials to the head of a TODO entry."
579 (let ((time-stamp-format todo-time-string-format
))
580 (concat (time-stamp-string) " " todo-initials
": ")))
582 ;; ---------------------------------------------------------------------------
584 ;; Get some outside help ...
586 (require 'time-stamp
)
589 ;; ---------------------------------------------------------------------------
591 ;; Set up some helpful context ...
593 (defvar todo-categories nil
"TODO categories.")
594 (defvar todo-cats nil
595 "Old variable for holding the TODO categories.
596 Use `todo-categories' instead.")
597 (defvar todo-previous-line
0 "Previous line that I asked about.")
598 (defvar todo-previous-answer
0 "Previous answer that I got.")
599 (defvar todo-mode-map nil
"TODO mode keymap.")
600 (defvar todo-category-number
0 "TODO category number.")
602 (defvar todo-tmp-buffer-name
"*Tmp*")
604 (defvar todo-category-sep
(make-string 75 ?-
)
605 "Category separator.")
606 (defvar todo-category-beg
" --- "
607 "Category start separator to be prepended onto category name.")
608 (defvar todo-category-end
"--- End"
609 "Separator after a category.")
610 (defvar todo-header
"-*- mode: todo; "
611 "Header of todo files.")
615 ;; ---------------------------------------------------------------------------
619 (let ((map (make-keymap)))
620 (suppress-keymap map t
)
621 (define-key map
"+" 'todo-forward-category
)
622 (define-key map
"-" 'todo-backward-category
)
623 (define-key map
"d" 'todo-file-item
) ;done/delete
624 (define-key map
"e" 'todo-edit-item
)
625 (define-key map
"E" 'todo-edit-multiline
)
626 (define-key map
"f" 'todo-file-item
)
627 (define-key map
"i" 'todo-insert-item
)
628 (define-key map
"I" 'todo-insert-item-here
)
629 (define-key map
"j" 'todo-jump-to-category
)
630 (define-key map
"k" 'todo-delete-item
)
631 (define-key map
"l" 'todo-lower-item
)
632 (define-key map
"n" 'todo-forward-item
)
633 (define-key map
"p" 'todo-backward-item
)
634 (define-key map
"P" 'todo-print
)
635 (define-key map
"q" 'todo-quit
)
636 (define-key map
"r" 'todo-raise-item
)
637 (define-key map
"s" 'todo-save
)
638 (define-key map
"S" 'todo-save-top-priorities
)
639 (define-key map
"t" 'todo-top-priorities
)
640 (setq todo-mode-map map
)))
642 (defun todo-category-select ()
643 "Make TODO mode display the current category correctly."
644 (let ((name (nth todo-category-number todo-categories
)))
645 (setq mode-line-buffer-identification
646 (concat "Category: " name
))
648 (goto-char (point-min))
649 (search-forward-regexp
651 (regexp-quote (concat todo-prefix todo-category-beg name
))
653 (let ((begin (1+ (point-at-eol))))
654 (search-forward-regexp (concat "^" todo-category-end
))
655 (narrow-to-region begin
(point-at-bol))
656 (goto-char (point-min)))))
657 (defalias 'todo-cat-slct
'todo-category-select
)
659 (defun todo-forward-category () "Go forward to TODO list of next category."
661 (setq todo-category-number
662 (mod (1+ todo-category-number
) (length todo-categories
)))
663 (todo-category-select))
664 (defalias 'todo-cmd-forw
'todo-forward-category
)
666 (defun todo-backward-category () "Go back to TODO list of previous category."
668 (setq todo-category-number
669 (mod (1- todo-category-number
) (length todo-categories
)))
670 (todo-category-select))
671 (defalias 'todo-cmd-back
'todo-backward-category
)
673 (defun todo-backward-item () "Select previous entry of TODO list."
675 (search-backward-regexp (concat "^" (regexp-quote todo-prefix
)) nil t
)
677 (defalias 'todo-cmd-prev
'todo-backward-item
)
679 (defun todo-forward-item (&optional count
)
680 "Select COUNT-th next entry of TODO list."
682 (if (listp count
) (setq count
(car count
)))
684 (search-forward-regexp (concat "^" (regexp-quote todo-prefix
))
688 (defalias 'todo-cmd-next
'todo-forward-item
)
690 (defun todo-save () "Save the TODO list."
693 (if todo-save-top-priorities-too
(todo-save-top-priorities))
695 (defalias 'todo-cmd-save
'todo-save
)
697 (defun todo-quit () "Done with TODO list for now."
703 (defalias 'todo-cmd-done
'todo-quit
)
705 (defun todo-edit-item () "Edit current TODO list entry."
707 (let ((item (todo-item-string)))
708 (if (todo-string-multiline-p item
)
709 (todo-edit-multiline)
710 (let ((new (read-from-minibuffer "Edit: " item
)))
715 (defalias 'todo-cmd-edit
'todo-edit-item
)
717 (defun todo-edit-multiline ()
718 "Set up a buffer for editing a multiline TODO list entry."
720 (let ((buffer-name (generate-new-buffer-name todo-edit-buffer
)))
722 (make-indirect-buffer
723 (file-name-nondirectory todo-file-do
) buffer-name
))
724 (message "To exit, simply kill this buffer and return to list.")
726 (narrow-to-region (todo-item-start) (todo-item-end))))
729 (defun todo-add-category (cat)
730 "Add new category CAT to the TODO list."
732 (save-window-excursion
733 (setq todo-categories
(cons cat todo-categories
))
734 (find-file todo-file-do
)
736 (goto-char (point-min))
737 (let ((posn (search-forward "-*- mode: todo; " 17 t
)))
738 (if (not (null posn
)) (goto-char posn
))
741 (insert "-*- mode: todo; \n")
744 (insert (format "todo-categories: %S; -*-" todo-categories
))
746 (insert (format "%s%s%s\n%s\n%s %s\n"
747 todo-prefix todo-category-beg cat
749 todo-prefix todo-category-sep
)))
753 (defun todo-add-item-non-interactively (new-item category ARG
)
754 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
758 (if (string= "" category
)
759 (setq category
(nth todo-category-number todo-categories
)))
760 (let ((cat-exists (member category todo-categories
)))
761 (setq todo-category-number
763 (- (length todo-categories
) (length cat-exists
))
764 (todo-add-category category
))))
768 (setq todo-previous-line
0)
770 (bottom (1+ (count-lines (point-min) (point-max)))))
771 (while (> (- bottom top
) todo-insert-threshold
)
772 (let* ((current (/ (+ top bottom
) 2))
773 (answer (if (< current bottom
)
774 (todo-more-important-p current
) nil
)))
776 (setq bottom current
)
777 (setq top
(1+ current
)))))
778 (setq top
(/ (+ top bottom
) 2))
779 ;; goto-line doesn't have the desired behavior in a narrowed buffer
780 (goto-char (point-min))
781 (forward-line (1- top
))))
783 (insert new-item
"\n")
789 (defun todo-insert-item (ARG)
790 "Insert new TODO list entry.
791 With a prefix argument solicit the category, otherwise use the current
795 (if (not (string-equal mode-name
"TODO")) (todo-show))
796 (let* ((new-item (concat todo-prefix
" "
797 (read-from-minibuffer
799 (if todo-entry-prefix-function
800 (funcall todo-entry-prefix-function
)))))
801 (categories todo-categories
)
802 (history (cons 'categories
(1+ todo-category-number
)))
803 (current-category (nth todo-category-number todo-categories
))
809 current-category
"]: ")
810 (todo-category-alist) nil nil nil history
))))
811 (todo-add-item-non-interactively new-item category ARG
))))
813 (defalias 'todo-cmd-inst
'todo-insert-item
)
816 (defun todo-insert-item-here ()
817 "Insert new TODO list entry under the cursor."
819 (todo-insert-item t
))
821 (defun todo-more-important-p (line)
822 "Ask whether entry is more important than the one at LINE."
823 (if (not (equal todo-previous-line line
))
825 (setq todo-previous-line line
)
826 (goto-char (point-min))
827 (forward-line (1- todo-previous-line
))
828 (let ((item (todo-item-string-start)))
829 (setq todo-previous-answer
830 (y-or-n-p (concat "More important than '" item
"'? "))))))
831 todo-previous-answer
)
832 (defalias 'todo-ask-p
'todo-more-important-p
)
834 (defun todo-delete-item () "Delete current TODO list entry."
836 (if (> (count-lines (point-min) (point-max)) 0)
837 (let* ((todo-entry (todo-item-string-start))
838 (todo-answer (y-or-n-p (concat "Permanently remove '"
843 (todo-backward-item)))
845 (error "No TODO list entry to delete")))
846 (defalias 'todo-cmd-kill
'todo-delete-item
)
848 (defun todo-raise-item () "Raise priority of current entry."
850 (if (> (count-lines (point-min) (point)) 0)
851 (let ((item (todo-item-string)))
857 (error "No TODO list entry to raise")))
858 (defalias 'todo-cmd-rais
'todo-raise-item
)
860 (defun todo-lower-item () "Lower priority of current entry."
862 (if (> (count-lines (point) (point-max)) 1)
863 ;; Assume there is a final newline
864 (let ((item (todo-item-string)))
870 (error "No TODO list entry to lower")))
871 (defalias 'todo-cmd-lowr
'todo-lower-item
)
873 (defun todo-file-item () "File the current TODO list entry away."
875 (if (> (count-lines (point-min) (point-max)) 0)
876 (let ((comment (read-from-minibuffer "Comment: "))
877 (time-stamp-format todo-time-string-format
))
878 (if (> (length comment
) 0)
880 (goto-char (todo-item-end))
882 (if (save-excursion (beginning-of-line)
883 (looking-at (regexp-quote todo-prefix
)))
886 "(" (nth todo-category-number todo-categories
) ": "
888 (goto-char (todo-item-start))
889 (let ((temp-point (point)))
890 (if (looking-at (regexp-quote todo-prefix
))
891 (replace-match (time-stamp-string))
892 ;; Standard prefix -> timestamp
893 ;; Else prefix non-standard item start with timestamp
894 (insert (time-stamp-string)))
895 (append-to-file temp-point
(todo-item-end) todo-file-done
)
896 (delete-region temp-point
(1+ (todo-item-end))))
899 (error "No TODO list entry to file away")))
901 ;; ---------------------------------------------------------------------------
903 ;; Utility functions:
907 (defun todo-top-priorities (&optional nof-priorities category-pr-page
)
908 "List top priorities for each category.
910 Number of entries for each category is given by NOF-PRIORITIES which
911 defaults to \'todo-show-priorities\'.
913 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
914 between each category."
917 (or nof-priorities
(setq nof-priorities todo-show-priorities
))
918 (if (listp nof-priorities
) ;universal argument
919 (setq nof-priorities
(car nof-priorities
)))
920 (let ((todo-print-buffer-name "*Tmp*")
921 ;;(todo-print-category-number 0)
922 (todo-category-break (if category-pr-page
"\f" ""))
925 (if todo-remove-separator
926 (concat todo-category-end
"\n"
927 (regexp-quote todo-prefix
) " " todo-category-sep
"\n")
928 (concat todo-category-end
"\n"))))
934 (copy-to-buffer todo-print-buffer-name
(point-min) (point-max))
935 (set-buffer todo-print-buffer-name
)
936 (goto-char (point-min))
937 (if (re-search-forward (regexp-quote todo-header
) nil t
)
939 (beginning-of-line 1)
940 (kill-line))) ;Remove mode line
941 (while (re-search-forward ;Find category start
942 (regexp-quote (concat todo-prefix todo-category-beg
))
944 (setq beg
(+ (point-at-eol) 1)) ;Start of first entry.
945 (re-search-forward cat-end nil t
)
946 (setq end
(match-beginning 0))
947 (replace-match todo-category-break
)
948 (narrow-to-region beg end
) ;In case we have too few entries.
949 (goto-char (point-min))
950 (if (= 0 nof-priorities
) ;Traverse entries.
951 (goto-char end
) ;All entries
952 (todo-forward-item nof-priorities
))
954 (delete-region beg end
)
956 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
957 (goto-char (point-min)) ;Due to display buffer
959 ;; Could have used switch-to-buffer as it has a norecord argument,
960 ;; which is nice when we are called from e.g. todo-print.
961 ;; Else we could have used pop-to-buffer.
962 (display-buffer todo-print-buffer-name
)
963 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
964 todo-print-buffer-name
)
968 (defun todo-save-top-priorities (&optional nof-priorities
)
969 "Save top priorities for each category in `todo-file-top'.
971 Number of entries for each category is given by NOF-PRIORITIES which
972 defaults to `todo-show-priorities'."
974 (save-window-excursion
977 (todo-top-priorities nof-priorities
)
978 (set-buffer todo-tmp-buffer-name
)
979 (write-file todo-file-top
)
984 (defun todo-print (&optional category-pr-page
)
985 "Print todo summary using \\\[todo-print-function].
986 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
987 between each category.
989 Number of entries for each category is given by
990 \'todo-print-priorities\'."
992 (if todo-print-function
994 (save-window-excursion
997 (todo-top-priorities todo-print-priorities
999 (set-buffer todo-tmp-buffer-name
)
1000 (and (funcall todo-print-function
)
1002 (message "Todo printing done."))
1004 (message "todo-print-function undefinded")
1007 (defun todo-jump-to-category ()
1008 "Jump to a category. Default is previous category."
1010 (let* ((categories todo-categories
)
1011 (history (cons 'categories
(1+ todo-category-number
)))
1012 (category (completing-read
1013 (concat "Category ["
1014 (nth todo-category-number todo-categories
) "]: ")
1015 (todo-category-alist) nil nil nil history
)))
1016 (if (string= "" category
)
1017 (setq category
(nth todo-category-number todo-categories
)))
1018 (setq todo-category-number
1019 (if (member category todo-categories
)
1020 (- (length todo-categories
)
1021 (length (member category todo-categories
)))
1022 (todo-add-category category
)))
1025 (defun todo-line-string () "Return current line in buffer as a string."
1026 (buffer-substring (point-at-bol) (point-at-eol)))
1028 (defun todo-item-string-start ()
1029 "Return the start of this TODO list entry as a string."
1030 ;; Suitable for putting in the minibuffer when asking the user
1031 (let ((item (todo-item-string)))
1032 (if (> (length item
) 60)
1033 (setq item
(concat (substring item
0 56) "...")))
1036 (defun todo-item-start () "Return point at start of current TODO list item."
1039 (if (not (looking-at (regexp-quote todo-prefix
)))
1040 (search-backward-regexp
1041 (concat "^" (regexp-quote todo-prefix
)) nil t
))
1044 (defun todo-item-end () "Return point at end of current TODO list item."
1047 (search-forward-regexp
1048 (concat "^" (regexp-quote todo-prefix
)) nil
'goto-end
)
1049 (1- (point-at-bol))))
1051 (defun todo-remove-item () "Delete the current entry from the TODO list."
1052 (delete-region (todo-item-start) (1+ (todo-item-end))))
1054 (defun todo-item-string () "Return current TODO list entry as a string."
1055 (buffer-substring (todo-item-start) (todo-item-end)))
1057 (defun todo-string-count-lines (string)
1058 "Return the number of lines STRING spans."
1059 (length (split-string string
"\n")))
1061 (defun todo-string-multiline-p (string)
1062 "Return non-nil if STRING spans several lines."
1063 (> (todo-string-count-lines string
) 1))
1065 (defun todo-category-alist ()
1066 "Generate an alist for use in `completing-read' from `todo-categories'."
1067 (mapcar (lambda (cat) (cons cat nil
))
1070 ;; utility functions: These are available in XEmacs, but not in Emacs 19.34
1072 (if (not (fboundp 'point-at-bol
))
1073 (defun point-at-bol () "Return value of point at beginning of line."
1078 (if (not (fboundp 'point-at-eol
))
1079 (defun point-at-eol () "Return value of point at end of line."
1084 ;; splits at a white space, returns a list
1085 (if (not (fboundp 'split-string
))
1086 (defun split-string (string &optional separators
)
1087 "Splits STRING into substrings where there are matches for SEPARATORS.
1088 Each match for SEPARATORS is a splitting point.
1089 The substrings between the splitting points are made into a list
1091 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
1092 (let ((rexp (or separators
"[ \f\t\n\r\v]+"))
1095 (while (string-match rexp string start
)
1096 (or (eq (match-beginning 0) 0)
1098 (cons (substring string start
(match-beginning 0))
1100 (setq start
(match-end 0)))
1101 (or (eq start
(length string
))
1103 (cons (substring string start
)
1107 ;; ---------------------------------------------------------------------------
1109 (easy-menu-define todo-menu todo-mode-map
"Todo Menu"
1111 ["Next category" todo-forward-category t
]
1112 ["Previous category" todo-backward-category t
]
1113 ["Jump to category" todo-jump-to-category t
]
1114 ["Show top priority items" todo-top-priorities t
]
1115 ["Print categories" todo-print t
]
1117 ["Edit item" todo-edit-item t
]
1118 ["File item" todo-file-item t
]
1119 ["Insert new item" todo-insert-item t
]
1120 ["Insert item here" todo-insert-item-here t
]
1121 ["Kill item" todo-delete-item t
]
1123 ["Lower item priority" todo-lower-item t
]
1124 ["Raise item priority" todo-raise-item t
]
1126 ["Next item" todo-forward-item t
]
1127 ["Previous item" todo-backward-item t
]
1129 ["Save" todo-save t
]
1130 ["Save Top Priorities" todo-save-top-priorities t
]
1132 ["Quit" todo-quit t
]
1135 ;; As calendar reads .todo-do before todo-mode is loaded.
1137 (defun todo-mode () "Major mode for editing TODO lists.\n\n\\{todo-mode-map}"
1139 (setq major-mode
'todo-mode
)
1140 (setq mode-name
"TODO")
1141 (use-local-map todo-mode-map
)
1142 (easy-menu-add todo-menu
)
1143 (run-hooks 'todo-mode-hook
))
1145 ;; Read about this function in the setup instructions above!
1148 "Make a diary entry appear only in the current date's diary"
1149 (if (equal (calendar-current-date) date
)
1153 (defun todo-edit-mode ()
1154 "Major mode for editing items in the TODO list\n\n\\{todo-edit-mode-map}"
1156 (setq major-mode
'todo-edit-mode
)
1157 (setq mode-name
"TODO Edit")
1158 (run-hooks 'todo-edit-mode-hook
))
1161 (defun todo-show () "Show TODO list."
1163 (if (file-exists-p todo-file-do
)
1164 (find-file todo-file-do
)
1165 (todo-initial-setup))
1166 (if (null todo-categories
)
1167 (if (null todo-cats
)
1168 (error "Error in %s: No categories in list `todo-categories'"
1170 (goto-char (point-min))
1171 (and (search-forward "todo-cats:" nil t
)
1172 (replace-match "todo-categories:"))
1173 (make-local-variable 'todo-categories
)
1174 (setq todo-categories todo-cats
)))
1176 (todo-category-select))
1178 (defun todo-initial-setup () "Set up things to work properly in TODO mode."
1179 (find-file todo-file-do
)
1182 (todo-add-category "Todo"))
1184 (provide 'todo-mode
)
1186 ;; ---------------------------------------------------------------------------
1187 ;;; todo-mode.el ends here
1188 ;; ---------------------------------------------------------------------------