todo-mode.el 42 KB
Newer Older
1
;; todo-mode.el -- Major mode for editing TODO list files
Oliver Seidel's avatar
Oliver Seidel committed
2

Oliver Seidel's avatar
Oliver Seidel committed
3
;; Copyright (C) 1997 Free Software Foundation, Inc.
Oliver Seidel's avatar
Oliver Seidel committed
4

Oliver Seidel's avatar
Oliver Seidel committed
5 6
;; Author: Oliver.Seidel@cl.cam.ac.uk (was valid on Aug 2, 1997)
;; Created: 2 Aug 1997
7
;; Version: $Id: todo-mode.el,v 1.26 1997/10/28 21:14:51 os10000 Exp os10000 $
Oliver Seidel's avatar
Oliver Seidel committed
8
;; Keywords: Categorised TODO list editor, todo-mode
Oliver Seidel's avatar
Oliver Seidel committed
9

Oliver Seidel's avatar
Oliver Seidel committed
10
;; This file is part of GNU Emacs.
11

Oliver Seidel's avatar
Oliver Seidel committed
12
;; GNU Emacs is free software; you can redistribute it and/or modify
13 14 15
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
Oliver Seidel's avatar
Oliver Seidel committed
16 17

;; GNU Emacs is distributed in the hope that it will be useful,
18 19 20
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Oliver Seidel's avatar
Oliver Seidel committed
21

22 23 24 25 26 27 28
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;; ---------------------------------------------------------------------------

Oliver Seidel's avatar
Oliver Seidel committed
29 30
;;; Commentary:

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
;;  Mode Description
;;
;;	TODO is a major mode for EMACS which offers functionality to
;;	treat most lines in one buffer as a list of items one has to
;;	do.  There are facilities to add new items, which are
;;	categorised, to edit or even delete items from the buffer.
;;	The buffer contents are currently compatible with the diary,
;;	so that the list of todo-items will show up in the FANCY diary
;;	mode.
;;
;;	Notice: Besides the major mode, this file also exports the
;;	function `todo-show' which will change to the one specific
;;	TODO file that has been specified in the todo-file-do
;;	variable.  If this file does not conform to the TODO mode
;;	conventions, the todo-show function will add the appropriate
;;	header and footer.  I don't anticipate this to cause much
;;	grief, but be warned, in case you attempt to read a plain text
;;	file.
;;
50
;;  Preface, Quickstart Installation
51
;;
52
;;      To get this to work, make emacs execute the line
53
;;
54
;;          (autoload 'todo-mode "todo-mode"
55 56 57 58 59
;;                    "Major mode for editing TODO lists." t)
;;          (autoload 'todo-show "todo-mode"
;;                    "Show TODO items." t)
;;          (autoload 'todo-insert-item "todo-mode"
;;                    "Add TODO item." t)
Oliver Seidel's avatar
Oliver Seidel committed
60
;;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
;;      You may now enter new items by typing "M-x todo-insert-item",
;;      or enter your TODO list file by typing "M-x todo-show".
;;
;;      The TODO list file has a special format and some auxiliary
;;      information, which will be added by the todo-show function if
;;      it attempts to visit an un-initialised file.  Hence it is
;;      recommended to use the todo-show function for the first time,
;;      in order to initialise the file, but it is not necessary
;;      afterwards.
;;
;;      As these commands are quite long to type, I would recommend
;;      the addition of two bindings to your to your global keymap.  I
;;      personally have the following in my initialisation file:
;;
;;          (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
76
;;	    (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
Oliver Seidel's avatar
Oliver Seidel committed
77
;;
78 79 80 81 82 83 84 85 86 87 88 89 90
;;      Note, however, that this recommendation has prompted some
;;      criticism, since the keys C-c LETTER are reserved for user
;;      functions.  I believe my recommendation is acceptable, since
;;      the Emacs Lisp Manual *Tips* section also details that the
;;      mode itself should not bind any functions to those keys.  The
;;      express aim of the above two bindings is to work outside the
;;      mode, which doesn't need the show function and offers a
;;      different binding for the insert function.  They serve as
;;      shortcuts and are not even needed (since the TODO mode will be
;;      entered by visiting the TODO file, and later by switching to
;;      its buffer).
;;
;;  Version
Oliver Seidel's avatar
Oliver Seidel committed
91
;;
92
;;      Which version of todo-mode.el does this documentation refer to?
93
;;
94
;;      $Id: todo-mode.el,v 1.26 1997/10/28 21:14:51 os10000 Exp os10000 $
95
;;
96 97 98 99 100 101
;;  Pre-Requisites
;;
;;      This package will require the following packages to be
;;      available on the load-path:
;;
;;          time-stamp
102 103
;;          easymenu
;;
104
;;  Operation
105 106 107 108 109 110 111
;;
;;	You will have the following facilities available:
;;
;;	    M-x todo-show   will enter the todo list screen, here type
;;
;;	    +  to go to next category
;;          -  to go to previous category
112 113
;;          d  to file the current entry, including a
;;            			    comment and timestamp
114 115 116 117
;;          e  to edit the current entry
;;          f  to file the current entry, including a
;;            			    comment and timestamp
;;          i  to insert a new entry
118
;;	    j  jump to category
119 120 121 122
;;          k  to kill the current entry
;;          l  to lower the current entry's priority
;;          n  for the next entry
;;          p  for the previous entry
123
;;	    P  print
124 125 126
;;          q  to save the list and exit the buffer
;;          r  to raise the current entry's priority
;;          s  to save the list
127
;;          S  to save the list of top priorities
128
;;	    t  show top priority items for each category
129
;;
130 131 132 133 134 135 136 137
;;	When you add a new entry, you are asked for the text and then
;;	for the category.  I for example have categories for things
;;	that I want to do in the office (like mail my mum), that I
;;	want to do in town (like buy cornflakes) and things I want to
;;	do at home (move my suitcases).  The categories can be
;;	selected with the cursor keys and if you type in the name of a
;;	category which didn't exist before, an empty category of the
;;	desired name will be added and filled with the new entry.
138 139 140 141 142 143
;;
;;  Configuration
;;
;;  Variable todo-prefix
;;
;;	I would like to recommend that you use the prefix "*/*" (by
144 145
;;	leaving the variable 'todo-prefix' untouched) so that the
;;	diary displays each entry every day.
146
;;
147 148 149 150
;;	To understand what I mean, please read the documentation that
;;	goes with the calendar since that will tell you how you can
;;	set up the fancy diary display and use the #include command to
;;	include your todo list file as part of your diary.
151
;;
152 153
;;	If you have the diary package set up to usually display more
;;	than one day's entries at once, consider using
154 155 156
;;
;;	    "&%%(equal (calendar-current-date) date)"
;;
157 158
;;	as the value of `todo-prefix'.  Please note that this may slow
;;	down the processing of your diary file some.
159 160 161
;;
;;  Variable todo-file-do
;;
162 163 164
;;	This variable is fairly self-explanatory.  You have to store
;;	your TODO list somewhere.  This variable tells the package
;;	where to go and find this file.
165 166 167
;;
;;  Variable todo-file-done
;;
168 169 170 171 172
;;	Even when you're done, you may wish to retain the entries.
;;	Given that they're timestamped and you are offered to add a
;;	comment, this can make a useful diary of past events.  It will
;;	even blend in with the EMACS diary package.  So anyway, this
;;	variable holds the name of the file for the filed todo-items.
173
;;
174 175 176 177 178 179
;;  Variable todo-file-top
;;
;;      File storing the top priorities of your TODO list when
;;      todo-save-top-priorities is non-nil.  Nice to include in your
;;      diary instead of the complete TODO list.
;;
180 181
;;  Variable todo-mode-hook
;;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
;;	Just like other modes, too, this mode offers to call your
;;	functions before it goes about its business.  This variable
;;	will be inspected for any functions you may wish to have
;;	called once the other TODO mode preparations have been
;;	completed.
;;
;;  Variable todo-insert-threshold
;;
;;     	Another nifty feature is the insertion accuracy.  If you have
;;     	8 items in your TODO list, then you may get asked 4 questions
;;     	by the binary insertion algorithm.  However, you may not
;;     	really have a need for such accurate priorities amongst your
;;     	TODO items.  If you now think about the binary insertion
;;     	halfing the size of the window each time, then the threshhold
;;     	is the window size at which it will stop.  If you set the
;;     	threshhold to zero, the upper and lower bound will coincide at
;;     	the end of the loop and you will insert your item just before
199
;;     	that point.  If you set the threshhold to, e.g. 8, it will stop
200 201 202 203 204 205
;;     	as soon as the window size drops below that amount and will
;;     	insert the item in the approximate centre of that window.  I
;;     	got the idea for this feature after reading a very helpful
;;     	e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
;;     	corrected some of my awful coding and pointed me towards some
;;     	good reading.  Thanks Trey!
206 207 208
;;
;;  Things to do
;;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
;;      These originally were my ideas, but now also include all the
;;      suggestions that I included before forgetting them:
;;
;;      o   Fancy fonts for todo/top-priority buffer
;;      o   Remove todo-prefix option in todo-top-priorities
;;      o   Rename category
;;      o   Move entry from one category to another one
;;      o   Entries which both have the generic */* prefix and a
;;          "deadline" entry which are understood by diary, indicating
;;          an event (unless marked by &)
;;      o   The optional COUNT variable of todo-forward-item should be
;;          applied to the other functions performing similar tasks
;;      o   Modularization could be done for repeaded elements of
;;          the code, like the completing-read lines of code.  
;;	o   license / version function
224 225 226
;;	o   export to diary file
;;	o   todo-report-bug
;;	o   GNATS support
227 228 229 230 231 232
;;	o   elide multiline (as in bbdb, or, to a lesser degree, in
;;          outline mode)
;;	o   rewrite complete package to store data as lisp objects
;;          and have display modes for display, for diary export,
;;          etc. (Richard Stallman pointed out this is a bad idea)
;;      o   so base todo-mode.el on generic-mode.el instead
233
;;
234
;;  History and Gossip
235
;;
236 237 238
;;	Many thanks to all the ones who have contributed to the
;;	evolution of this package!  I hope I have listed all of you
;;	somewhere in the documentation or at least in the RCS history!
239
;;
240 241
;;	Enjoy this package and express your gratitude by sending nice
;;	things to my parents' address!
242 243
;;
;;	Oliver Seidel
244
;;	(Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
245 246 247 248
;;

;; ---------------------------------------------------------------------------

Oliver Seidel's avatar
Oliver Seidel committed
249 250 251
;;; Change Log:

;; $Log: todo-mode.el,v $
252 253 254 255 256 257
;; Revision 1.26  1997/10/28 21:14:51  os10000
;; Improvements sent in by Dave Love <d.love@dl.ac.uk>:
;; todo-mode.el: Doc fixes.  Customization.
;; (todo-add-item-non-interactively): New arg -- don't dynamically bind ARG.
;; (todo-insert-item): Use it.
;;
258 259 260 261 262 263
;; Revision 1.25  1997/10/28 20:03:27  os10000
;; Harald Backer <harald.backer@fou.telenor.no> sent the following:
;; Added `todo-save-top-priorities' and option to automatically save top
;; priorities file when saving todo-file.  Changed some default values.
;; Bug fixes.
;;
264 265 266 267
;; Revision 1.24  1997/10/28 19:41:53  os10000
;; Added fix from Frank Ridderbusch <ridderbusch.pad@sni.de>,
;; an apostrophe was missing.
;;
268 269 270 271 272 273 274 275 276 277
;; Revision 1.23  1997/10/24  17:30:54  os10000
;; Added three suggestions from Carsten
;; Dominik <dominik@strw.LeidenUniv.nl>:
;;
;; - recommend autoloading instead of require
;; - inserting from different buffer didn't work
;;   (now fixed -- I pray)
;; - provided public entry point to insert items
;;   from normal lisp code
;;
278 279 280 281 282 283 284
;; Revision 1.22  1997/10/24  16:53:20  os10000
;; Paul Stodghill <stodghil@CS.Cornell.EDU> writes:
;;
;; When invoked with a prefix, todo-insert-item
;; should not prompt for a category.  (He adds:
;; At least that's what I think.)
;;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
;; Revision 1.21  1997/10/24  16:51:02  os10000
;; Rafael Laboissiere <rafael@icp.inpg.fr> writes:
;;
;; I was just annoyed with the fact that there is no way
;; to dynamically control the insertion accuracy.  I mean:
;; the variable `todo-insert-threshold' does the job, but
;; it is not very handy if one wants to mix the two
;; behaviors (bisection and "insert right here under the
;; cursor").
;;
;; Therefore I did a quick hack in the function
;; `todo-insert-item'.  Now by giving a prefix argument to
;; the insert command (i.e. by typing "C-u i"), entries
;; are inserted exactly at the line where the cursor is.
;; It would be better to give the value of
;; `todo-insert-threshold' as a numeric argument of
;; `todo-insert-item' (like "M-8 i"), but it's too late
;; now for continuing to hack.
;;
304 305 306 307 308 309 310 311 312 313 314 315 316 317
;; Revision 1.20  1997/10/17  15:41:57  os10000
;; Thanks to Harald Backer <harald.backer@fou.telenor.no>, we now have
;; the following facilities available:
;;
;; Added todo-print, todo-top-priorities and todo-jump with matching
;; variables; Parameterized todo-header, todo-category-beg,
;; todo-category-end and todo-category-sep; Added autoload comments;
;; todo-category-select: Modified regexp to make category names unique;
;; todo-forward-item: Added optional COUNT vaiable; todo-insert-item:
;; Rewrote completing read entry.
;;
;; Also, check out the extended list of things left to be done to this
;; package at the end of the documentation!
;;
318 319 320 321 322 323 324 325 326
;; Revision 1.19  1997/10/16  21:21:16  os10000
;; Jari Aalto <jari.aalto@poboxes.com> writes:
;;
;;     I just downloaded your package and after reading the docs I
;;     decided to do some reformatting.  Hope you don't mind.  Now
;;     they are in such a format that the html page can be
;;     automatically generated from the source file.  As an example, I
;;     generated the attached page using the following command:
;;     ripdoc.pls < todo-mode.el | t2html.pls -a "Oliver.Seidel" -e \
327
;;     Oliver.Seidel@cl.cam.ac.uk -simple -base
328 329 330 331 332 333
;;
;; And of course I appreciate it.  Jari's stuff can be found at:
;; ftp://cs.uta.fi/pub/ssjaaa/, while I'm making the rev 1.18 page
;; available at http://www.cl.cam.ac.uk/users/os10000/doc/todo-mode.html
;; (That link will be valid until 10/1998 or slightly longer.)
;;
334 335 336 337 338 339
;; Revision 1.18  1997/10/15  17:18:11  os10000
;; Everything seems to work in Harald Melands Emacs 20.02 and
;; my Emacs 19.34.  Beware of the spelling in some of the
;; variable names.  I looked up "threshold" in a dictionary
;; and here in Britain this appears to be the way to spell it.
;;
340 341 342
;; Revision 1.17  1997/10/15  14:30:41  os10000
;; Attempted to reconcile Harald's changes with mine since 1.15.
;;
343 344 345
;; Revision 1.16  1997/10/15  14:00:12  os10000
;; Fixed 'file-item' and added 20.02 split-string function.
;;
346 347 348 349 350
;; Revision 1.15  1997/10/14  22:22:35  os10000
;; Added string-split (which I stole from ediff-util), changed
;; pop-to-buffer to switch-to-buffer and added message on how
;; to exit the multi-line-edit mode.
;;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
;; Revision 1.14  1997/10/09  09:24:50  os10000
;; Harald Meland <harald.meland@usit.uio.no> asked for
;; the latest version, got 1.13, and returned this.
;; He writes:
;;
;; Thanks a lot for the new version of todo-mode.el.  As you will see I
;; have messed it up a bit, hopefully for the better -- I don't like
;; short, cryptic names for variables and functions, so I renamed most of
;; them, and `defalias'ed the old function names.  I hope you don't mind
;; too much, I just kinda couldn't stop myself.
;;
;; Additionally, I included some support for multiline entries, cleaned
;; up (IMHO :) a lot of the code, included completion-support for which
;; category to install a new entry in, and possibly some other changes I
;; can't remember :)
;;
;; It's getting rather late, and I have just done some preliminary
;; testing on whether all of this really works, but so far it looks
;; good.
;;
371 372 373 374 375 376
;; Revision 1.13  1997/08/19  14:00:36  seidel
;; - changed name to todo-mode
;; - fixed menu descriptions
;; - fixed "pressing abort while filing"
;; - attempted Emacs Lisp Manual *Tips* section compliance
;;
Oliver Seidel's avatar
Oliver Seidel committed
377 378
;; Revision 1.12  1997/08/06  10:56:15  os10000
;; Fixed header, typos, layout, documentation.
379
;;
380 381 382 383
;; Revision 1.11  1997/08/06  09:14:25  os10000
;; Applied patch from Istvan Marko <istvan@cmdmail.amd.com>
;; to make menus work anywhere.
;;
384 385 386 387
;; Revision 1.10  1997/08/06  08:56:03  os10000
;; Acted upon suggestion from Shane Holder <holder@rsn.hp.com>:
;; Cancelling the editing of an entry will not delete it any more.
;;
388 389 390 391 392
;; Revision 1.9  1997/08/06 08:12:03  os10000
;; Improved documentation.  Broke some lines to comply with
;; Richard Stallman's email to please keep in sync with the
;; rest of the Emacs distribution files.
;;
393
;; Revision 1.8  1997/08/05 22:39:04  os10000
Oliver Seidel's avatar
Oliver Seidel committed
394
;; Made todo-mode.el available under GPL.
395 396 397
;;
;; Revision 1.7  1997/08/05 22:34:14  os10000
;; Fixed insertion routine with help from Trey Jackson
398
;; <trey@cs.berkeley.edu>; added todo-inst-tresh;
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
;; fixed keyboard layout to remove unwanted keys.
;;
;; Revision 1.6  1997/08/05 16:47:01  os10000
;; Incorporated menus for XEmacs from Allan.Cochrane@soton.sc.philips.com,
;; fixed TYPO, fixed todo-file-cmd, cleaned up rcs history.
;;
;; Revision 1.5  1997/08/05  14:43:39  os10000
;; Added improvements from Ron Gut <rgut@aware.com>.
;; Added category management.
;;
;; Revision 1.4  1997/08/04  16:18:45  os10000
;; Added Raise/Lower item.
;;
;; Revision 1.3  1997/08/03  12:47:26  os10000
;; Cleaned up variables, prefix and cursor position.
;;
;; Revision 1.2  1997/08/03 12:15:28  os10000
;; It appears to work.
;;
;; Revision 1.1  1997/08/03 12:15:13  os10000
;; Initial revision
;;
421 422 423

;; ---------------------------------------------------------------------------

Oliver Seidel's avatar
Oliver Seidel committed
424 425
;;; Code:

426 427 428 429 430 431 432 433 434 435 436 437 438
(eval-and-compile                       ; Removable for installation in
                                        ; Emacs 20.
  (condition-case ()
      (require 'custom)
    (error nil))
  (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
      nil ;; We've got what we needed
    ;; We have the old custom-library, hack around it!
    (defmacro defgroup (&rest args)
      nil)
    (defmacro defcustom (var value doc &rest args) 
      (` (defvar (, var) (, value) (, doc))))))

Oliver Seidel's avatar
Oliver Seidel committed
439 440
;; User-configurable variables:

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
(defgroup todo nil
  "Maintain a list of todo items."
  :group 'calendar)

(defcustom todo-prefix     "*/*"
  "*TODO mode prefix for entries.

This is useful in conjunction with `calendar' and `diary' if you use

#include \"~/.todo-do\"

in your diary file to include your todo list file as part of your
diary.  With the default value \"*/*\" the diary displays each entry
every day and it may also be marked on every day of the calendar.
Using \"&%%(equal (calendar-current-date) date)\" instead will only
show and mark todo entreis for today, but may slow down processing of
the diary file somewhat."
  :type 'string
  :group 'todo)
(defcustom todo-file-do    "~/.todo-do"
  "*TODO mode list file."
  :type 'file
  :group 'todo)
(defcustom todo-file-done  "~/.todo-done"
  "*TODO mode archive file."
  :type 'file
  :group 'todo)
(defcustom todo-mode-hook  nil
  "*TODO mode hooks."
  :type 'hook
  :group 'todo)
(defcustom todo-edit-mode-hook nil
  "*TODO Edit mode hooks."
  :type 'hook
  :group 'todo)
(defcustom todo-insert-threshold 0
  "*TODO mode insertion accuracy.

If you have 8 items in your TODO list, then you may get asked 4
questions by the binary insertion algorithm.  However, you may not
really have a need for such accurate priorities amongst your TODO
items.  If you now think about the binary insertion halfing the size
of the window each time, then the threshhold is the window size at
which it will stop.  If you set the threshhold to zero, the upper and
lower bound will coincide at the end of the loop and you will insert
your item just before that point.  If you set the threshhold to,
e.g. 8, it will stop as soon as the window size drops below that
amount and will insert the item in the approximate centre of that
window."
  :type 'integer
  :group 'todo)
492
(defvar todo-edit-buffer " *TODO Edit*" "TODO Edit buffer name.")
493
(defcustom todo-file-top "~/.todo-top"
494
  "*TODO mode top priorities file.
495

496
Not in TODO format, but diary compatible.
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
Automatically generated when `todo-save-top-priorities' is non-nil."
  :type 'string
  :group 'todo)

(defcustom todo-print-function 'ps-print-buffer-with-faces
  "*Function to print the current buffer."
  :type 'symbol
  :group 'todo)
(defcustom todo-show-priorities 1
  "*Default number of priorities to show by \\[todo-top-priorities].
0 means show all entries."
  :type 'integer
  :group 'todo)
(defcustom todo-print-priorities 0
  "*Default number of priorities to print by \\[todo-print].
0 means print all entries."
  :type 'integer
  :group 'todo)
(defcustom todo-remove-separator t
  "*Non-nil to remove category separators in\
\\[todo-top-priorities] and \\[todo-print]."
  :type 'boolean
  :group 'todo)
(defcustom todo-save-top-priorities-too t
521
  "*Non-nil makes todo-save automatically save top-priorities in
522 523 524
`todo-file-top'."
  :type 'boolean
  :group 'todo)
525

Oliver Seidel's avatar
Oliver Seidel committed
526 527 528
;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
;;
529
(defcustom todo-time-string-format
530
  "%:y-%02m-%02d %02H:%02M"
531 532 533 534 535 536 537 538 539 540 541 542 543
  "*TODO mode time string format for done entries.
For details see the variable `time-stamp-format'."
  :type 'string
  :group 'todo)

(defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
  "*Function producing text to insert at start of todo entry."
  :type 'symbol
  :group 'todo)
(defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
  "*Initials of todo item author."
  :type 'string
  :group 'todo)
544 545

(defun todo-entry-timestamp-initials ()
546
  "Prepend timestamp and your initials to the head of a TODO entry."
547 548 549
  (let ((time-stamp-format todo-time-string-format))
    (concat (time-stamp-string) " " todo-initials ": ")))

Oliver Seidel's avatar
Oliver Seidel committed
550 551
;; ---------------------------------------------------------------------------

552 553
;; Get some outside help ...

Oliver Seidel's avatar
Oliver Seidel committed
554
(require 'time-stamp)
555 556 557
(require 'easymenu)

;; ---------------------------------------------------------------------------
Oliver Seidel's avatar
Oliver Seidel committed
558

559 560
;; Set up some helpful context ...

561
(defvar todo-categories         nil     "TODO categories.")
562 563 564
(defvar todo-cats               nil
  "Old variable for holding the TODO categories.
Use `todo-categories' instead.")
565 566 567 568
(defvar todo-previous-line      0       "Previous line that I asked about.")
(defvar todo-previous-answer    0       "Previous answer that I got.")
(defvar todo-mode-map           nil     "TODO mode keymap.")
(defvar todo-category-number    0       "TODO category number.")
569

570
(defvar todo-tmp-buffer-name "*Tmp*")
571 572 573 574 575 576 577 578 579 580 581 582

(defvar todo-category-sep (make-string 75 ?-)
  "Category separator.")
(defvar todo-category-beg " --- "
  "Category start separator to be prepended onto category name.")
(defvar todo-category-end "--- End"
  "Separator after a category.")
(defvar todo-header "-*- mode: todo; "
  "Header of todo files.")



583 584
;; ---------------------------------------------------------------------------

585 586 587 588
(if todo-mode-map
    nil
  (let ((map (make-keymap)))
    (suppress-keymap map t)
589 590 591 592
    (define-key map "+" 'todo-forward-category)
    (define-key map "-" 'todo-backward-category)
    (define-key map "e" 'todo-edit-item)
    (define-key map "E" 'todo-edit-multiline)
593
    (define-key map "d" 'todo-file-item) ;done/delete
594 595
    (define-key map "f" 'todo-file-item)
    (define-key map "i" 'todo-insert-item)
596
    (define-key map "j" 'todo-jump-to-category)
597 598 599 600
    (define-key map "k" 'todo-delete-item)
    (define-key map "l" 'todo-lower-item)
    (define-key map "n" 'todo-forward-item)
    (define-key map "p" 'todo-backward-item)
601
    (define-key map "P" 'todo-print)
602 603 604
    (define-key map "q" 'todo-quit)
    (define-key map "r" 'todo-raise-item)
    (define-key map "s" 'todo-save)
605
    (define-key map "S" 'todo-save-top-priorities)
606
    (define-key map "t" 'todo-top-priorities)
607
    (setq todo-mode-map map)))
Oliver Seidel's avatar
Oliver Seidel committed
608

609 610 611
(defun todo-category-select ()
  "Make TODO mode display the current category correctly."
  (let ((name (nth todo-category-number todo-categories)))
612
    (setq mode-line-buffer-identification
613
          (concat "Category: " name))
614 615
    (widen)
    (goto-char (point-min))
616
    (search-forward-regexp
617 618 619
     (concat "^"
             (regexp-quote (concat todo-prefix todo-category-beg name))
             "$"))
620
    (let ((begin (1+ (point-at-eol))))
621
      (search-forward-regexp (concat "^" todo-category-end))
622 623 624 625 626
      (narrow-to-region begin (point-at-bol))
      (goto-char (point-min)))))
(defalias 'todo-cat-slct 'todo-category-select)

(defun todo-forward-category () "Go forward to TODO list of next category."
627
  (interactive)
628
  (setq todo-category-number
629
        (mod (1+ todo-category-number) (length todo-categories)))
630 631
  (todo-category-select))
(defalias 'todo-cmd-forw 'todo-forward-category)
632

633
(defun todo-backward-category () "Go back to TODO list of previous category."
634
  (interactive)
635
  (setq todo-category-number
636
        (mod (1- todo-category-number) (length todo-categories)))
637 638
  (todo-category-select))
(defalias 'todo-cmd-back 'todo-backward-category)
639

640
(defun todo-backward-item () "Select previous entry of TODO list."
Oliver Seidel's avatar
Oliver Seidel committed
641
  (interactive)
642
  (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
643
  (message ""))
644
(defalias 'todo-cmd-prev 'todo-backward-item)
Oliver Seidel's avatar
Oliver Seidel committed
645

646
(defun todo-forward-item (&optional count)
647
  "Select COUNT-th next entry of TODO list."
648 649
  (interactive "P")
  (if (listp count) (setq count (car count)))
650
  (end-of-line)
651 652
  (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
                         nil 'goto-end count)
653
  (beginning-of-line)
654
  (message ""))
655
(defalias 'todo-cmd-next 'todo-forward-item)
Oliver Seidel's avatar
Oliver Seidel committed
656

657
(defun todo-save () "Save the TODO list."
658
  (interactive)
659 660 661
  (save-buffer)
  (if todo-save-top-priorities-too (todo-save-top-priorities))
  )
662
(defalias 'todo-cmd-save 'todo-save)
663

664
(defun todo-quit () "Done with TODO list for now."
Oliver Seidel's avatar
Oliver Seidel committed
665
  (interactive)
666
  (widen)
667
  (todo-save)
668
  (message "")
669
  (bury-buffer))
670
(defalias 'todo-cmd-done 'todo-quit)
Oliver Seidel's avatar
Oliver Seidel committed
671

672
(defun todo-edit-item () "Edit current TODO list entry."
Oliver Seidel's avatar
Oliver Seidel committed
673
  (interactive)
674 675
  (let ((item (todo-item-string)))
    (if (todo-string-multiline-p item)
676
        (todo-edit-multiline)
677
      (let ((new (read-from-minibuffer "Edit: " item)))
678 679 680 681
        (todo-remove-item)
        (insert new "\n")
        (todo-backward-item)
        (message "")))))
682 683 684 685 686 687
(defalias 'todo-cmd-edit 'todo-edit-item)

(defun todo-edit-multiline ()
  "Set up a buffer for editing a multiline TODO list entry."
  (interactive)
  (let ((buffer-name (generate-new-buffer-name todo-edit-buffer)))
688 689 690
    (switch-to-buffer
     (make-indirect-buffer
      (file-name-nondirectory todo-file-do) buffer-name))
691
    (message "To exit, simply kill this buffer and return to list.")
692 693
    (todo-edit-mode)
    (narrow-to-region (todo-item-start) (todo-item-end))))
Oliver Seidel's avatar
Oliver Seidel committed
694

695
;;;### autoload
696 697
(defun todo-add-category (cat) 
  "Add new category CAT to the TODO list."
Oliver Seidel's avatar
Oliver Seidel committed
698 699
  (interactive)
  (save-window-excursion
700
    (setq todo-categories (cons cat todo-categories))
Oliver Seidel's avatar
Oliver Seidel committed
701
    (find-file todo-file-do)
702 703 704 705
    (widen)
    (goto-char (point-min))
    (let ((posn (search-forward "-*- mode: todo; " 17 t)))
      (if (not (null posn)) (goto-char posn))
706
      (if (equal posn nil)
707 708 709 710
          (progn
            (insert "-*- mode: todo; \n")
            (forward-char -1))
        (kill-line)))
711
    (insert (format "todo-categories: %S; -*-" todo-categories))
712
    (forward-char 1)
713 714 715 716
    (insert (format "%s%s%s\n%s\n%s %s\n"
                    todo-prefix todo-category-beg cat
                    todo-category-end 
                    todo-prefix todo-category-sep)))
717
  0)
718

719
;;;### autoload
720 721
(defun todo-add-item-non-interactively (new-item category ARG)
  "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
722
  (save-excursion
723 724
    (todo-show))
  (save-excursion
725 726
    (if (string= "" category)
        (setq category (nth todo-category-number todo-categories)))
727 728
    (let ((cat-exists (member category todo-categories)))
      (setq todo-category-number
729 730 731
            (if cat-exists
                (- (length todo-categories) (length cat-exists))
              (todo-add-category category))))
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    (if (not ARG)
	(progn
	  (todo-show)
	  (setq todo-previous-line 0)
	  (let ((top 1)
		(bottom (1+ (count-lines (point-min) (point-max)))))
	    (while (> (- bottom top) todo-insert-threshold)
	      (let* ((current (/ (+ top bottom) 2))
		     (answer (if (< current bottom)
				 (todo-more-important-p current) nil)))
		(if answer
		    (setq bottom current)
		  (setq top (1+ current)))))
	    (setq top (/ (+ top bottom) 2))
	    ;; goto-line doesn't have the desired behavior in a narrowed buffer
	    (goto-char (point-min))
	    (forward-line (1- top))))
      (beginning-of-line))
750 751
    (insert new-item "\n")
    (todo-backward-item)
752
    (todo-save)
753
    (message "")))
754 755 756

;;;### autoload
(defun todo-insert-item (ARG)
757 758 759
  "Insert new TODO list entry.
With a prefix argument solicit the category, otherwise use the current
category."
760
  (interactive "P")
761
  (save-excursion
762
  (todo-show)
763
  )
764 765 766
  (let* ((new-item (concat todo-prefix " "
			   (read-from-minibuffer
                            "New TODO entry: "
767 768
                                 (if todo-entry-prefix-function
                                     (funcall todo-entry-prefix-function)))))
769 770 771 772 773 774
         (categories todo-categories)
         (history (cons 'categories (1+ todo-category-number)))
	 (current-category (nth todo-category-number todo-categories))
	 (category 
	  (if ARG
	      current-category
775 776 777 778 779
	      (completing-read 
                    (concat "Category ["
                            current-category "]: ")
                    (todo-category-alist) nil nil nil history))))
    (todo-add-item-non-interactively new-item category ARG)))
780

781
(defalias 'todo-cmd-inst 'todo-insert-item)
782

783
(defun todo-more-important-p (line)
784 785
  "Ask whether entry is more important than the one at LINE."
  (if (not (equal todo-previous-line line))
786
      (progn
787
        (setq todo-previous-line line)
788
        (goto-char (point-min))
789
        (forward-line (1- todo-previous-line))
790 791 792
        (let ((item (todo-item-string-start)))
          (setq todo-previous-answer
                (y-or-n-p (concat "More important than '" item "'? "))))))
793 794 795 796
  todo-previous-answer)
(defalias 'todo-ask-p 'todo-more-important-p)

(defun todo-delete-item () "Delete current TODO list entry."
Oliver Seidel's avatar
Oliver Seidel committed
797 798
  (interactive)
  (if (> (count-lines (point-min) (point-max)) 0)
799
      (let* ((todo-entry (todo-item-string-start))
800 801 802 803 804 805 806
             (todo-answer (y-or-n-p (concat "Permanently remove '"
                                            todo-entry "'? "))))
        (if todo-answer
            (progn
              (todo-remove-item)
              (todo-backward-item)))
        (message ""))
807 808
    (error "No TODO list entry to delete")))
(defalias 'todo-cmd-kill 'todo-delete-item)
Oliver Seidel's avatar
Oliver Seidel committed
809

810
(defun todo-raise-item () "Raise priority of current entry."
Oliver Seidel's avatar
Oliver Seidel committed
811
  (interactive)
812 813
  (if (> (count-lines (point-min) (point)) 0)
      (let ((item (todo-item-string)))
814 815 816 817 818
        (todo-remove-item)
        (todo-backward-item)
        (save-excursion
          (insert item "\n"))
        (message ""))
819 820
    (error "No TODO list entry to raise")))
(defalias 'todo-cmd-rais 'todo-raise-item)
Oliver Seidel's avatar
Oliver Seidel committed
821

822
(defun todo-lower-item () "Lower priority of current entry."
Oliver Seidel's avatar
Oliver Seidel committed
823
  (interactive)
824 825
  (if (> (count-lines (point) (point-max)) 1)
      ;; Assume there is a final newline
826
      (let ((item (todo-item-string)))
827 828 829 830 831
        (todo-remove-item)
        (todo-forward-item)
        (save-excursion
          (insert item "\n"))
        (message ""))
832 833
    (error "No TODO list entry to lower")))
(defalias 'todo-cmd-lowr 'todo-lower-item)
Oliver Seidel's avatar
Oliver Seidel committed
834

835
(defun todo-file-item () "File the current TODO list entry away."
Oliver Seidel's avatar
Oliver Seidel committed
836 837
  (interactive)
  (if (> (count-lines (point-min) (point-max)) 0)
838
      (let ((comment (read-from-minibuffer "Comment: "))
839 840 841 842
            (time-stamp-format todo-time-string-format))
        (if (> (length comment) 0)
            (progn
              (goto-char (todo-item-end))
843 844 845 846 847 848 849
              (insert
	       (if (save-excursion (beginning-of-line)
				   (looking-at (regexp-quote todo-prefix)))
		   " "
		 "\n\t")
	       "(" (nth todo-category-number todo-categories) ": "
	       comment ")\n")))
850 851 852
        (goto-char (todo-item-start))
        (let ((temp-point (point)))
          (if (looking-at (regexp-quote todo-prefix))
853 854
              (replace-match (time-stamp-string))
	    ;; Standard prefix -> timestamp
855 856 857 858 859 860
            ;; Else prefix non-standard item start with timestamp
            (insert (time-stamp-string)))
          (append-to-file temp-point (todo-item-end) todo-file-done)
          (delete-region temp-point (1+ (todo-item-end))))
        (todo-backward-item)
        (message ""))
861
    (error "No TODO list entry to file away")))
Oliver Seidel's avatar
Oliver Seidel committed
862 863 864

;; ---------------------------------------------------------------------------

865 866
;; Utility functions:

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881

;;;###autoload
(defun todo-top-priorities (&optional nof-priorities category-pr-page)
  "List top priorities for each category.

Number of entries for each category is given by NOF-PRIORITIES which
defaults to \'todo-show-priorities\'.

If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
between each category."

  (interactive "P")
  (or nof-priorities (setq nof-priorities todo-show-priorities))
  (if (listp nof-priorities)            ;universal argument
      (setq nof-priorities (car nof-priorities)))
882 883 884
  (let ((todo-print-buffer-name "*Tmp*")
        ;;(todo-print-category-number 0)
        (todo-category-break (if category-pr-page "" ""))
885 886 887 888 889 890 891 892 893 894 895
        (cat-end
         (concat
          (if todo-remove-separator
              (concat todo-category-end "\n"
                      (regexp-quote todo-prefix) " " todo-category-sep "\n")
            (concat todo-category-end "\n"))))
        beg end)
    (todo-show)
    (save-excursion
      (save-restriction
        (widen)
896 897
        (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
        (set-buffer todo-print-buffer-name)
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
        (goto-char (point-min))
        (if (re-search-forward (regexp-quote todo-header) nil t)
            (progn
              (beginning-of-line 1)
              (kill-line)))             ;Remove mode line
        (while (re-search-forward       ;Find category start
                (regexp-quote (concat todo-prefix todo-category-beg))
                nil t)
          (setq beg (+ (point-at-eol) 1)) ;Start of first entry.
          (re-search-forward cat-end nil t)
          (setq end (match-beginning 0))
          (replace-match todo-category-break)
          (narrow-to-region beg end)    ;In case we have too few entries.
          (goto-char (point-min))
          (if (= 0 nof-priorities)      ;Traverse entries.
              (goto-char end)            ;All entries
            (todo-forward-item nof-priorities))
          (setq beg (point))
          (delete-region beg end)
          (widen))
918
        (and (looking-at "") (replace-match "")) ;Remove trailing form-feed.
919 920 921 922
        (goto-char (point-min))         ;Due to display buffer
        ))
    ;; Could have used switch-to-buffer as it has a norecord argument,
    ;; which is nice when we are called from e.g. todo-print.
923
    ;; Else we could have used pop-to-buffer.
924
    (display-buffer todo-print-buffer-name)
925
    (message "Type C-x 1 to remove %s window.  M-C-v to scroll the help."
926
             todo-print-buffer-name)
927 928
    ))

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
;;;###autoload
(defun todo-save-top-priorities (&optional nof-priorities)
  "Save top priorities for each category in `todo-file-top'.

Number of entries for each category is given by NOF-PRIORITIES which
defaults to `todo-show-priorities'."
  (interactive "P")
  (save-window-excursion
    (save-excursion
      (save-restriction
        (todo-top-priorities nof-priorities)
        (set-buffer todo-tmp-buffer-name)
        (write-file todo-file-top)
        (kill-this-buffer)
        ))))

945 946 947 948 949 950 951 952 953
;;;###autoload
(defun todo-print (&optional category-pr-page)
  "Print todo summary using \\\[todo-print-function].
If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
between each category.

Number of entries for each category is given by
\'todo-print-priorities\'."
  (interactive "P")
954 955
  (if todo-print-function
      (progn
956 957 958 959 960
  (save-window-excursion
  (save-excursion
    (save-restriction
      (todo-top-priorities todo-print-priorities
                                    category-pr-page)
961 962 963
              (set-buffer todo-tmp-buffer-name)
              (and (funcall todo-print-function)
                   (kill-this-buffer))
964
            (message "Todo printing done."))
965 966 967
            )))
    (message "todo-print-function undefinded")
    ))
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

(defun todo-jump-to-category ()
  "Jump to a category.  Default is previous category."
  (interactive)
  (let* ((categories todo-categories)
         (history (cons 'categories (1+ todo-category-number)))
	 (category (completing-read 
                    (concat "Category ["
                            (nth todo-category-number todo-categories) "]: ")
                    (todo-category-alist) nil nil nil history)))
    (if (string= "" category)
        (setq category (nth todo-category-number todo-categories)))
    (setq todo-category-number
          (if (member category todo-categories)
              (- (length todo-categories)
                 (length (member category todo-categories)))
            (todo-add-category category)))
    (todo-show)))

987 988 989 990 991 992 993 994
(defun todo-line-string () "Return current line in buffer as a string."
  (buffer-substring (point-at-bol) (point-at-eol)))

(defun todo-item-string-start ()
  "Return the start of this TODO list entry as a string."
  ;; Suitable for putting in the minibuffer when asking the user
  (let ((item (todo-item-string)))
    (if (> (length item) 60)
995
        (setq item (concat (substring item 0 56) "...")))
996 997 998 999 1000 1001
    item))

(defun todo-item-start () "Return point at start of current TODO list item."
  (save-excursion
    (beginning-of-line)
    (if (not (looking-at (regexp-quote todo-prefix)))
1002 1003
        (search-backward-regexp
         (concat "^" (regexp-quote todo-prefix)) nil t))
1004 1005 1006 1007 1008
    (point)))

(defun todo-item-end () "Return point at end of current TODO list item."
  (save-excursion
    (end-of-line)
1009 1010
    (search-forward-regexp
     (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    (1- (point-at-bol))))

(defun todo-remove-item () "Delete the current entry from the TODO list."
  (delete-region (todo-item-start) (1+ (todo-item-end))))

(defun todo-item-string () "Return current TODO list entry as a string."
  (buffer-substring (todo-item-start) (todo-item-end)))

(defun todo-string-count-lines (string)
  "Return the number of lines STRING spans."
  (length (split-string string "\n")))

(defun todo-string-multiline-p (string)
1024
  "Return non-nil if STRING spans several lines."
1025 1026 1027
  (> (todo-string-count-lines string) 1))

(defun todo-category-alist ()
1028
  "Generate an alist for use in `completing-read' from `todo-categories'."
1029
  (mapcar (lambda (cat) (cons cat nil))
1030
          todo-categories))
1031

1032 1033 1034
;; utility functions:  These are available in XEmacs, but not in Emacs 19.34

(if (not (fboundp 'point-at-bol))
1035
    (defun point-at-bol () "Return value of point at beginning of line."
1036
      (save-excursion
1037 1038
        (beginning-of-line)
        (point))))
1039 1040

(if (not (fboundp 'point-at-eol))
1041
    (defun point-at-eol () "Return value of point at end of line."
1042
      (save-excursion
1043 1044
        (end-of-line)
        (point))))
1045

1046 1047
;; splits at a white space, returns a list
(if (not (fboundp 'split-string))
1048 1049 1050 1051 1052 1053 1054
    (defun split-string (string &optional separators)
      "Splits STRING into substrings where there are matches for SEPARATORS.
Each match for SEPARATORS is a splitting point.
The substrings between the splitting points are made into a list
which is returned.
If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
      (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
            (start 0)
            (list nil))
        (while (string-match rexp string start)
          (or (eq (match-beginning 0) 0)
              (setq list
                    (cons (substring string start (match-beginning 0))
                          list)))
          (setq start (match-end 0)))
        (or (eq start (length string))
            (setq list
                  (cons (substring string start)
                        list)))
        (nreverse list))))
1068

1069 1070
;; ---------------------------------------------------------------------------

1071
(easy-menu-define todo-menu todo-mode-map "Todo Menu"
1072 1073 1074
                  '("Todo"
                    ["Next category"        todo-forward-category t]
                    ["Previous category"    todo-backward-category t]