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