Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / obsolete / otodo-mode.el
blob53220f064810bf8507587cf0fdaa79581cacdb3d
1 ;;; todo-mode.el --- major mode for editing TODO list files
3 ;; Copyright (C) 1997, 1999, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Oliver Seidel <privat@os10000.net>
6 ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
7 ;; Created: 2 Aug 1997
8 ;; Keywords: calendar, todo
9 ;; Obsolete-since: 24.4
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; ---------------------------------------------------------------------------
28 ;;; Commentary:
30 ;; Mode Description
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
35 ;; categorized, to edit or even delete items from the buffer.
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.
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.
49 ;; Preface, Quickstart Installation
51 ;; To get this to work, make Emacs execute the line
53 ;; (autoload 'todo-mode "todo-mode"
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)
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".
63 ;; The TODO list file has a special format and some auxiliary
64 ;; information, which will be added by the todo-show function if
65 ;; it attempts to visit an un-initialized file. Hence it is
66 ;; recommended to use the todo-show function for the first time,
67 ;; in order to initialize the file, but it is not necessary
68 ;; afterwards.
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
72 ;; personally have the following in my initialization file:
74 ;; (global-set-key "\C-ct" 'todo-show) ; switch to TODO buffer
75 ;; (global-set-key "\C-ci" 'todo-insert-item) ; insert new item
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).
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.
94 ;; Pre-Requisites
96 ;; This package will require the following packages to be
97 ;; available on the load-path:
99 ;; time-stamp
100 ;; easymenu
102 ;; Operation
104 ;; You will have the following facilities available:
106 ;; M-x todo-show will enter the todo list screen, here type
108 ;; + to go to next category
109 ;; - to go to previous category
110 ;; d to file the current entry, including a
111 ;; comment and timestamp
112 ;; e to edit the current entry
113 ;; E to edit a multi-line entry
114 ;; f to file the current entry, including a
115 ;; comment and timestamp
116 ;; i to insert a new entry, with prefix, omit category
117 ;; I to insert a new entry at current cursor position
118 ;; j jump to category
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
123 ;; P print
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
127 ;; S to save the list of top priorities
128 ;; t show top priority items for each category
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.
139 ;; Configuration
141 ;; Variable todo-prefix
143 ;; I would like to recommend that you use the prefix "*/*" (by
144 ;; leaving the variable 'todo-prefix' untouched) so that the
145 ;; diary displays each entry every day.
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.
152 ;; If you have the diary package set up to usually display more
153 ;; than one day's entries at once, consider using
155 ;; "&%%(equal (calendar-current-date) date)"
157 ;; as the value of `todo-prefix'. Please note that this may slow
158 ;; down the processing of your diary file some.
160 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
162 ;; "&%%(todo-cp)"
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.
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.
171 ;; Variable todo-file-do
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.
177 ;; Variable todo-file-done
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.
185 ;; Variable todo-file-top
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.
191 ;; Variable todo-mode-hook
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.
199 ;; Variable todo-insert-threshold
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
206 ;; halving the size of the window each time, then the threshold
207 ;; is the window size at which it will stop. If you set the
208 ;; threshold to zero, the upper and lower bound will coincide at
209 ;; the end of the loop and you will insert your item just before
210 ;; that point. If you set the threshold to, e.g. 8, it will stop
211 ;; as soon as the window size drops below that amount and will
212 ;; insert the item in the approximate center of that window. I
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!
218 ;; Things to do
220 ;; These originally were my ideas, but now also include all the
221 ;; suggestions that I included before forgetting them:
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
232 ;; o Modularization could be done for repeated elements of
233 ;; the code, like the completing-read lines of code.
234 ;; o license / version function
235 ;; o export to diary file
236 ;; o todo-report-bug
237 ;; o GNATS support
238 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
239 ;; outline mode)
240 ;; o rewrite complete package to store data as Lisp objects
241 ;; and have display modes for display, for diary export,
242 ;; etc. (Richard Stallman pointed out this is a bad idea)
243 ;; o so base todo-mode.el on generic-mode.el instead
245 ;; History and Gossip
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!
251 ;; Enjoy this package and express your gratitude by sending nice
252 ;; things to my parents' address!
254 ;; Oliver Seidel
255 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
257 ;;; Code:
259 (require 'time-stamp)
262 ;; User-configurable variables:
264 (defgroup todo nil
265 "Maintain a list of todo items."
266 :link '(emacs-commentary-link "todo-mode")
267 :version "21.1"
268 :group 'calendar)
270 (defcustom todo-prefix "*/*"
271 "TODO mode prefix for entries.
273 This is useful in conjunction with `calendar' and `diary' if you use
275 #include \"~/.todo-do\"
277 in your diary file to include your todo list file as part of your
278 diary. With the default value \"*/*\" the diary displays each entry
279 every day and it may also be marked on every day of the calendar.
280 Using \"&%%(equal (calendar-current-date) date)\" instead will only
281 show and mark todo entries for today, but may slow down processing of
282 the diary file somewhat."
283 :type 'string
284 :group 'todo)
285 (defcustom todo-file-do (locate-user-emacs-file "todo-do" ".todo-do")
286 "TODO mode list file."
287 :type 'file
288 :group 'todo)
289 (defcustom todo-file-done (locate-user-emacs-file "todo-done" ".todo-done")
290 "TODO mode archive file."
291 :type 'file
292 :group 'todo)
293 (defcustom todo-mode-hook nil
294 "TODO mode hooks."
295 :type 'hook
296 :group 'todo)
297 (defcustom todo-edit-mode-hook nil
298 "TODO Edit mode hooks."
299 :type 'hook
300 :group 'todo)
301 (defcustom todo-insert-threshold 0
302 "TODO mode insertion accuracy.
304 If you have 8 items in your TODO list, then you may get asked 4
305 questions by the binary insertion algorithm. However, you may not
306 really have a need for such accurate priorities amongst your TODO
307 items. If you now think about the binary insertion halving the size
308 of the window each time, then the threshold is the window size at
309 which it will stop. If you set the threshold to zero, the upper and
310 lower bound will coincide at the end of the loop and you will insert
311 your item just before that point. If you set the threshold to,
312 e.g. 8, it will stop as soon as the window size drops below that
313 amount and will insert the item in the approximate center of that
314 window."
315 :type 'integer
316 :group 'todo)
317 (defvar todo-edit-buffer " *TODO Edit*"
318 "TODO Edit buffer name.")
319 (defcustom todo-file-top (locate-user-emacs-file "todo-top" ".todo-top")
320 "TODO mode top priorities file.
322 Not in TODO format, but diary compatible.
323 Automatically generated when `todo-save-top-priorities' is non-nil."
324 :type 'string
325 :group 'todo)
327 (defcustom todo-print-function 'ps-print-buffer-with-faces
328 "Function to print the current buffer."
329 :type 'symbol
330 :group 'todo)
331 (defcustom todo-show-priorities 1
332 "Default number of priorities to show by \\[todo-top-priorities].
333 0 means show all entries."
334 :type 'integer
335 :group 'todo)
336 (defcustom todo-print-priorities 0
337 "Default number of priorities to print by \\[todo-print].
338 0 means print all entries."
339 :type 'integer
340 :group 'todo)
341 (defcustom todo-remove-separator t
342 "Non-nil to remove category separators in\
343 \\[todo-top-priorities] and \\[todo-print]."
344 :type 'boolean
345 :group 'todo)
346 (defcustom todo-save-top-priorities-too t
347 "Non-nil makes `todo-save' automatically save top-priorities in `todo-file-top'."
348 :type 'boolean
349 :group 'todo)
351 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
352 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
354 (defcustom todo-time-string-format
355 "%:y-%02m-%02d %02H:%02M"
356 "TODO mode time string format for done entries.
357 For details see the variable `time-stamp-format'."
358 :type 'string
359 :group 'todo)
361 (defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
362 "Function producing text to insert at start of todo entry."
363 :type 'symbol
364 :group 'todo)
365 (defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
366 "Initials of todo item author."
367 :type 'string
368 :group 'todo)
370 (defun todo-entry-timestamp-initials ()
371 "Prepend timestamp and your initials to the head of a TODO entry."
372 (let ((time-stamp-format todo-time-string-format))
373 (concat (time-stamp-string) " " todo-initials ": ")))
375 ;; ---------------------------------------------------------------------------
377 ;; Set up some helpful context ...
379 (defvar todo-categories nil
380 "TODO categories.")
382 (defvar todo-cats nil
383 "Old variable for holding the TODO categories.
384 Use `todo-categories' instead.")
386 (defvar todo-previous-line 0
387 "Previous line asked about.")
389 (defvar todo-previous-answer 0
390 "Previous answer got.")
392 (defvar todo-mode-map
393 (let ((map (make-keymap)))
394 (suppress-keymap map t)
395 (define-key map "+" 'todo-forward-category)
396 (define-key map "-" 'todo-backward-category)
397 (define-key map "d" 'todo-file-item) ;done/delete
398 (define-key map "e" 'todo-edit-item)
399 (define-key map "E" 'todo-edit-multiline)
400 (define-key map "f" 'todo-file-item)
401 (define-key map "i" 'todo-insert-item)
402 (define-key map "I" 'todo-insert-item-here)
403 (define-key map "j" 'todo-jump-to-category)
404 (define-key map "k" 'todo-delete-item)
405 (define-key map "l" 'todo-lower-item)
406 (define-key map "n" 'todo-forward-item)
407 (define-key map "p" 'todo-backward-item)
408 (define-key map "P" 'todo-print)
409 (define-key map "q" 'todo-quit)
410 (define-key map "r" 'todo-raise-item)
411 (define-key map "s" 'todo-save)
412 (define-key map "S" 'todo-save-top-priorities)
413 (define-key map "t" 'todo-top-priorities)
414 map)
415 "TODO mode keymap.")
417 (defvar todo-category-number 0 "TODO category number.")
419 (defvar todo-tmp-buffer-name " *todo tmp*")
421 (defvar todo-category-sep (make-string 75 ?-)
422 "Category separator.")
424 (defvar todo-category-beg " --- "
425 "Category start separator to be prepended onto category name.")
427 (defvar todo-category-end "--- End"
428 "Separator after a category.")
430 (defvar todo-header "-*- mode: todo; "
431 "Header of todo files.")
433 ;; ---------------------------------------------------------------------------
435 (defun todo-category-select ()
436 "Make TODO mode display the current category correctly."
437 (let ((name (nth todo-category-number todo-categories)))
438 (setq mode-line-buffer-identification
439 ;; (concat "Category: " name))
440 (concat "Category: " (format "%18s" name)))
441 (widen)
442 (goto-char (point-min))
443 (search-forward-regexp
444 (concat "^"
445 (regexp-quote (concat todo-prefix todo-category-beg name))
446 "$"))
447 (let ((begin (1+ (line-end-position))))
448 (search-forward-regexp (concat "^" todo-category-end))
449 (narrow-to-region begin (line-beginning-position))
450 (goto-char (point-min)))))
451 (defalias 'todo-cat-slct 'todo-category-select)
453 (defun todo-forward-category ()
454 "Go forward to TODO list of next category."
455 (interactive)
456 (setq todo-category-number
457 (mod (1+ todo-category-number) (length todo-categories)))
458 (todo-category-select))
459 (defalias 'todo-cmd-forw 'todo-forward-category)
461 (defun todo-backward-category ()
462 "Go back to TODO list of previous category."
463 (interactive)
464 (setq todo-category-number
465 (mod (1- todo-category-number) (length todo-categories)))
466 (todo-category-select))
467 (defalias 'todo-cmd-back 'todo-backward-category)
469 (defun todo-backward-item ()
470 "Select previous entry of TODO list."
471 (interactive)
472 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
473 (message ""))
474 (defalias 'todo-cmd-prev 'todo-backward-item)
476 (defun todo-forward-item (&optional count)
477 "Select COUNT-th next entry of TODO list."
478 (interactive "P")
479 (if (listp count) (setq count (car count)))
480 (end-of-line)
481 (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
482 nil 'goto-end count)
483 (beginning-of-line)
484 (message ""))
485 (defalias 'todo-cmd-next 'todo-forward-item)
487 (defun todo-save ()
488 "Save the TODO list."
489 (interactive)
490 (save-excursion
491 (save-restriction
492 (save-buffer)))
493 (if todo-save-top-priorities-too (todo-save-top-priorities)))
494 (defalias 'todo-cmd-save 'todo-save)
496 (defun todo-quit ()
497 "Done with TODO list for now."
498 (interactive)
499 (widen)
500 (todo-save)
501 (message "")
502 (bury-buffer))
503 (defalias 'todo-cmd-done 'todo-quit)
505 (defun todo-edit-item ()
506 "Edit current TODO list entry."
507 (interactive)
508 (if (< (point-min) (point-max))
509 (let ((item (todo-item-string)))
510 (if (todo-string-multiline-p item)
511 (todo-edit-multiline)
512 (let ((new (read-from-minibuffer "Edit: " item)))
513 (todo-remove-item)
514 (insert new "\n")
515 (todo-backward-item)
516 (message ""))))
517 (error "No TODO list entry to edit")))
518 (defalias 'todo-cmd-edit 'todo-edit-item)
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)))
524 (switch-to-buffer
525 (make-indirect-buffer
526 (file-name-nondirectory todo-file-do) buffer-name))
527 (message "To exit, simply kill this buffer and return to list.")
528 (todo-edit-mode)
529 (narrow-to-region (todo-item-start) (todo-item-end))))
531 ;;;###autoload
532 (defun todo-add-category (&optional cat)
533 "Add new category CAT to the TODO list."
534 (interactive)
535 (let ((buf (find-file-noselect todo-file-do t))
536 (prompt "Category: "))
537 (unless (zerop (buffer-size buf))
538 (and (null todo-categories)
539 (null todo-cats)
540 (error "Error in %s: File is non-empty but contains no category"
541 todo-file-do)))
542 (unless cat (setq cat (read-from-minibuffer prompt)))
543 (with-current-buffer buf
544 ;; reject names that could induce bugs and confusion
545 (while (and (cond ((string= "" cat)
546 (setq prompt "Enter a non-empty category name: "))
547 ((string-match "\\`\\s-+\\'" cat)
548 (setq prompt "Enter a category name that is not only white space: "))
549 ((member cat todo-categories)
550 (setq prompt "Enter a non-existing category name: ")))
551 (setq cat (read-from-minibuffer prompt))))
552 ;; initialize a newly created Todo buffer for Todo mode
553 (unless (file-exists-p todo-file-do) (todo-mode))
554 (setq todo-categories (cons cat todo-categories))
555 (widen)
556 (goto-char (point-min))
557 (if (search-forward "-*- mode: todo; " (+ (point-min) 16) t)
558 (kill-line)
559 (insert "-*- mode: todo; \n")
560 (forward-char -1))
561 (insert (format "todo-categories: %S; -*-" todo-categories))
562 (forward-char 1)
563 (insert (format "%s%s%s\n%s\n%s %s\n"
564 todo-prefix todo-category-beg cat
565 todo-category-end
566 todo-prefix todo-category-sep))
567 (if (called-interactively-p 'interactive)
568 ;; properly display the newly added category
569 (progn (setq todo-category-number 0) (todo-show))
570 0))))
572 ;;;###autoload
573 (defun todo-add-item-non-interactively (new-item category)
574 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
575 (save-excursion
576 (todo-show))
577 (save-excursion
578 (if (string= "" category)
579 (setq category (nth todo-category-number todo-categories)))
580 (let ((cat-exists (member category todo-categories)))
581 (setq todo-category-number
582 (if cat-exists
583 (- (length todo-categories) (length cat-exists))
584 (todo-add-category category))))
585 (todo-show)
586 (setq todo-previous-line 0)
587 (let ((top 1)
588 (bottom (1+ (count-lines (point-min) (point-max)))))
589 (while (> (- bottom top) todo-insert-threshold)
590 (let* ((current (/ (+ top bottom) 2))
591 (answer (if (< current bottom)
592 (todo-more-important-p current) nil)))
593 (if answer
594 (setq bottom current)
595 (setq top (1+ current)))))
596 (setq top (/ (+ top bottom) 2))
597 ;; goto-line doesn't have the desired behavior in a narrowed buffer.
598 (goto-char (point-min))
599 (forward-line (1- top)))
600 (insert new-item "\n")
601 (todo-backward-item)
602 (todo-save)
603 (message "")))
605 ;;;###autoload
606 (defun todo-insert-item (arg)
607 "Insert new TODO list entry.
608 With a prefix argument ARG solicit the category, otherwise use the current
609 category."
610 (interactive "P")
611 (save-excursion
612 (if (not (derived-mode-p 'todo-mode)) (todo-show))
613 (let* ((new-item (concat todo-prefix " "
614 (read-from-minibuffer
615 "New TODO entry: "
616 (if todo-entry-prefix-function
617 (funcall todo-entry-prefix-function)))))
618 (current-category (nth todo-category-number todo-categories))
619 (category (if arg (todo-completing-read) current-category)))
620 (todo-add-item-non-interactively new-item category))))
622 (defalias 'todo-cmd-inst 'todo-insert-item)
624 (defun todo-insert-item-here ()
625 "Insert a new TODO list entry directly above the entry at point.
626 If point is on an empty line, insert the entry there."
627 (interactive)
628 (if (not (derived-mode-p 'todo-mode)) (todo-show))
629 (let ((new-item (concat todo-prefix " "
630 (read-from-minibuffer
631 "New TODO entry: "
632 (if todo-entry-prefix-function
633 (funcall todo-entry-prefix-function))))))
634 (unless (and (bolp) (eolp)) (todo-item-start))
635 (insert (concat new-item "\n"))
636 (backward-char)
637 ;; put point at start of new entry
638 (todo-item-start)))
640 (defun todo-more-important-p (line)
641 "Ask whether entry is more important than the one at LINE."
642 (unless (equal todo-previous-line line)
643 (setq todo-previous-line line)
644 (goto-char (point-min))
645 (forward-line (1- todo-previous-line))
646 (let ((item (todo-item-string-start)))
647 (setq todo-previous-answer
648 (y-or-n-p (concat "More important than '" item "'? ")))))
649 todo-previous-answer)
650 (defalias 'todo-ask-p 'todo-more-important-p)
652 (defun todo-delete-item ()
653 "Delete current TODO list entry."
654 (interactive)
655 (if (> (count-lines (point-min) (point-max)) 0)
656 (let* ((todo-entry (todo-item-string-start))
657 (todo-answer (y-or-n-p (concat "Permanently remove '"
658 todo-entry "'? "))))
659 (when todo-answer
660 (todo-remove-item)
661 (todo-backward-item))
662 (message ""))
663 (error "No TODO list entry to delete")))
664 (defalias 'todo-cmd-kill 'todo-delete-item)
666 (defun todo-raise-item ()
667 "Raise priority of current entry."
668 (interactive)
669 (if (> (count-lines (point-min) (point)) 0)
670 (let ((item (todo-item-string)))
671 (todo-remove-item)
672 (todo-backward-item)
673 (save-excursion
674 (insert item "\n"))
675 (message ""))
676 (error "No TODO list entry to raise")))
677 (defalias 'todo-cmd-rais 'todo-raise-item)
679 (defun todo-lower-item ()
680 "Lower priority of current entry."
681 (interactive)
682 (if (> (count-lines (point) (point-max)) 1)
683 ;; Assume there is a final newline
684 (let ((item (todo-item-string)))
685 (todo-remove-item)
686 (todo-forward-item)
687 (save-excursion
688 (insert item "\n"))
689 (message ""))
690 (error "No TODO list entry to lower")))
691 (defalias 'todo-cmd-lowr 'todo-lower-item)
693 (defun todo-file-item (&optional comment)
694 "File the current TODO list entry away, annotated with an optional COMMENT."
695 (interactive "sComment: ")
696 (or (> (count-lines (point-min) (point-max)) 0)
697 (error "No TODO list entry to file away"))
698 (let ((time-stamp-format todo-time-string-format))
699 (when (and comment (> (length comment) 0))
700 (goto-char (todo-item-end))
701 (insert
702 (if (save-excursion (beginning-of-line)
703 (looking-at (regexp-quote todo-prefix)))
705 "\n\t")
706 "(" comment ")"))
707 (goto-char (todo-item-end))
708 (insert " [" (nth todo-category-number todo-categories) "]")
709 (todo-item-start)
710 (let ((temp-point (point)))
711 (if (looking-at (regexp-quote todo-prefix))
712 (replace-match (time-stamp-string))
713 ;; Standard prefix -> timestamp
714 ;; Else prefix non-standard item start with timestamp
715 (insert (time-stamp-string)))
716 (append-to-file temp-point (todo-item-end 'include-sep) todo-file-done)
717 (delete-region temp-point (todo-item-end 'include-sep)))
718 (todo-backward-item)
719 (message "")))
721 ;; ---------------------------------------------------------------------------
723 ;; Utility functions:
726 ;;;###autoload
727 (defun todo-top-priorities (&optional nof-priorities category-pr-page
728 interactive)
729 "List top priorities for each category.
731 Number of entries for each category is given by NOF-PRIORITIES which
732 defaults to `todo-show-priorities'.
734 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
735 between each category.
736 INTERACTIVE should be non-nil if this function is called interactively."
738 (interactive "P\ni\nP")
739 (or nof-priorities (setq nof-priorities todo-show-priorities))
740 (if (listp nof-priorities) ;universal argument
741 (setq nof-priorities (car nof-priorities)))
742 (let ((todo-print-buffer-name todo-tmp-buffer-name)
743 ;;(todo-print-category-number 0)
744 (todo-category-break (if category-pr-page "\f" ""))
745 (cat-end
746 (concat
747 (if todo-remove-separator
748 (concat todo-category-end "\n"
749 (regexp-quote todo-prefix) " " todo-category-sep "\n")
750 (concat todo-category-end "\n"))))
751 beg end)
752 (save-excursion
753 (todo-show)
754 (save-restriction
755 (save-current-buffer
756 (widen)
757 (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
758 (set-buffer todo-print-buffer-name)
759 (goto-char (point-min))
760 (when (re-search-forward (regexp-quote todo-header) nil t)
761 (beginning-of-line 1)
762 (delete-region (point) (line-end-position)))
763 (while (re-search-forward ;Find category start
764 (regexp-quote (concat todo-prefix todo-category-beg))
765 nil t)
766 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
767 (re-search-forward cat-end nil t)
768 (setq end (match-beginning 0))
769 (replace-match todo-category-break)
770 (narrow-to-region beg end) ;In case we have too few entries.
771 (goto-char (point-min))
772 (if (zerop nof-priorities) ;Traverse entries.
773 (goto-char end) ;All entries
774 (todo-forward-item nof-priorities))
775 (setq beg (point))
776 (delete-region beg end)
777 (widen))
778 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
779 (goto-char (point-min)) ;Due to display buffer
781 (when interactive (display-buffer todo-print-buffer-name))
782 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
783 todo-print-buffer-name)))
785 (defun todo-save-top-priorities (&optional nof-priorities)
786 "Save top priorities for each category in `todo-file-top'.
788 Number of entries for each category is given by NOF-PRIORITIES which
789 defaults to `todo-show-priorities'."
790 (interactive "P")
791 (save-window-excursion
792 (save-excursion
793 (save-restriction
794 (todo-top-priorities nof-priorities)
795 (set-buffer todo-tmp-buffer-name)
796 (write-file todo-file-top)
797 (kill-this-buffer)))))
799 ;;;###autoload
800 (defun todo-print (&optional category-pr-page)
801 "Print todo summary using `todo-print-function'.
802 If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
803 between each category.
805 Number of entries for each category is given by `todo-print-priorities'."
806 (interactive "P")
807 (save-window-excursion
808 (save-excursion
809 (save-restriction
810 (todo-top-priorities todo-print-priorities
811 category-pr-page)
812 (set-buffer todo-tmp-buffer-name)
813 (and (funcall todo-print-function)
814 (kill-this-buffer))
815 (message "Todo printing done.")))))
817 (defun todo-jump-to-category ()
818 "Jump to a category. Default is previous category."
819 (interactive)
820 (let ((category (todo-completing-read)))
821 (if (string= "" category)
822 (setq category (nth todo-category-number todo-categories)))
823 (setq todo-category-number
824 (if (member category todo-categories)
825 (- (length todo-categories)
826 (length (member category todo-categories)))
827 (todo-add-category category)))
828 (todo-show)))
830 (defun todo-line-string ()
831 "Return current line in buffer as a string."
832 (buffer-substring (line-beginning-position) (line-end-position)))
834 (defun todo-item-string-start ()
835 "Return the start of this TODO list entry as a string."
836 ;; Suitable for putting in the minibuffer when asking the user
837 (let ((item (todo-item-string)))
838 (if (> (length item) 60)
839 (setq item (concat (substring item 0 56) "...")))
840 item))
842 (defun todo-item-start ()
843 "Go to start of current TODO list item and return point."
844 (beginning-of-line)
845 (if (not (looking-at (regexp-quote todo-prefix)))
846 (search-backward-regexp
847 (concat "^" (regexp-quote todo-prefix)) nil t))
848 (point))
850 (defun todo-item-end (&optional include-sep)
851 "Return point at end of current TODO list item.
852 If INCLUDE-SEP is non-nil, return point after the separator."
853 (save-excursion
854 (end-of-line)
855 (if (search-forward-regexp
856 (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
857 (goto-char (match-beginning 0)))
858 (unless include-sep (skip-chars-backward "\n"))
859 (point)))
861 (defun todo-remove-item ()
862 "Delete the current entry from the TODO list."
863 (delete-region (todo-item-start) (todo-item-end 'include-sep)))
865 (defun todo-item-string ()
866 "Return current TODO list entry as a string."
867 (buffer-substring (todo-item-start) (todo-item-end)))
869 (defun todo-string-count-lines (string)
870 "Return the number of lines STRING spans."
871 (length (split-string string "\n")))
873 (defun todo-string-multiline-p (string)
874 "Return non-nil if STRING spans several lines."
875 (> (todo-string-count-lines string) 1))
877 (defun todo-completing-read ()
878 "Return a category name, with completion, for use in Todo mode."
879 ;; make a copy of todo-categories in case history-delete-duplicates is
880 ;; non-nil, which makes completing-read alter todo-categories
881 (let* ((categories (copy-sequence todo-categories))
882 (history (cons 'todo-categories (1+ todo-category-number)))
883 (default (nth todo-category-number todo-categories))
884 (category (completing-read
885 (concat "Category [" default "]: ")
886 todo-categories nil nil nil history default)))
887 ;; restore the original value of todo-categories
888 (setq todo-categories categories)
889 category))
891 ;; ---------------------------------------------------------------------------
893 (easy-menu-define todo-menu todo-mode-map "Todo Menu"
894 '("Todo"
895 ["Next category" todo-forward-category t]
896 ["Previous category" todo-backward-category t]
897 ["Jump to category" todo-jump-to-category t]
898 ["Show top priority items" todo-top-priorities t]
899 ["Print categories" todo-print t]
900 "---"
901 ["Edit item" todo-edit-item t]
902 ["File item" todo-file-item t]
903 ["Insert new item" todo-insert-item t]
904 ["Insert item here" todo-insert-item-here t]
905 ["Kill item" todo-delete-item t]
906 "---"
907 ["Lower item priority" todo-lower-item t]
908 ["Raise item priority" todo-raise-item t]
909 "---"
910 ["Next item" todo-forward-item t]
911 ["Previous item" todo-backward-item t]
912 "---"
913 ["Save" todo-save t]
914 ["Save Top Priorities" todo-save-top-priorities t]
915 "---"
916 ["Quit" todo-quit t]
919 ;; As calendar reads .todo-do before todo-mode is loaded.
920 ;;;###autoload
921 (define-derived-mode todo-mode nil "TODO"
922 "Major mode for editing TODO lists."
923 (easy-menu-add todo-menu))
925 (defvar date)
926 (defvar entry)
928 ;; t-c should be used from diary code, which requires calendar.
929 (declare-function calendar-current-date "calendar" (&optional offset))
931 ;; Read about this function in the setup instructions above!
932 ;;;###autoload
933 (defun todo-cp ()
934 "Make a diary entry appear only in the current date's diary."
935 (if (equal (calendar-current-date) date)
936 entry))
938 (define-derived-mode todo-edit-mode text-mode "TODO Edit"
939 "Major mode for editing items in the TODO list.
941 \\{todo-edit-mode-map}")
943 ;;;###autoload
944 (defun todo-show ()
945 "Show TODO list."
946 (interactive)
947 ;; Call todo-initial-setup only if there is neither a Todo file nor
948 ;; a corresponding unsaved buffer.
949 (if (or (file-exists-p todo-file-do)
950 (let* ((buf (get-buffer (file-name-nondirectory todo-file-do)))
951 (bufname (buffer-file-name buf)))
952 (equal (expand-file-name todo-file-do) bufname)))
953 (find-file todo-file-do)
954 (todo-initial-setup))
955 (if (null todo-categories)
956 (if (null todo-cats)
957 (error "Error in %s: No categories in list `todo-categories'"
958 todo-file-do)
959 (goto-char (point-min))
960 (and (search-forward "todo-cats:" nil t)
961 (replace-match "todo-categories:"))
962 (make-local-variable 'todo-categories)
963 (setq todo-categories todo-cats)))
964 (beginning-of-line)
965 (todo-category-select))
967 (defun todo-initial-setup ()
968 "Set up things to work properly in TODO mode."
969 (find-file todo-file-do)
970 (erase-buffer)
971 (todo-mode)
972 (todo-add-category "Todo"))
974 (provide 'todo-mode)
976 ;;; todo-mode.el ends here