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