; Spelling fixes
[emacs.git] / lisp / obsolete / otodo-mode.el
blob5784601674c589428a2214d76763f5af1ac8a74a
1 ;;; todo-mode.el --- major mode for editing TODO list files
3 ;; Copyright (C) 1997, 1999, 2001-2017 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 \"~/.emacs.d/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 :version "24.4" ; added locate-user-emacs-file
288 :type 'file
289 :group 'todo)
290 (defcustom todo-file-done (locate-user-emacs-file "todo-done" ".todo-done")
291 "TODO mode archive file."
292 :version "24.4" ; added locate-user-emacs-file
293 :type 'file
294 :group 'todo)
295 (defcustom todo-mode-hook nil
296 "TODO mode hooks."
297 :type 'hook
298 :group 'todo)
299 (defcustom todo-edit-mode-hook nil
300 "TODO Edit mode hooks."
301 :type 'hook
302 :group 'todo)
303 (defcustom todo-insert-threshold 0
304 "TODO mode insertion accuracy.
306 If you have 8 items in your TODO list, then you may get asked 4
307 questions by the binary insertion algorithm. However, you may not
308 really have a need for such accurate priorities amongst your TODO
309 items. If you now think about the binary insertion halving the size
310 of the window each time, then the threshold is the window size at
311 which it will stop. If you set the threshold to zero, the upper and
312 lower bound will coincide at the end of the loop and you will insert
313 your item just before that point. If you set the threshold to,
314 e.g. 8, it will stop as soon as the window size drops below that
315 amount and will insert the item in the approximate center of that
316 window."
317 :type 'integer
318 :group 'todo)
319 (defvar todo-edit-buffer " *TODO Edit*"
320 "TODO Edit buffer name.")
321 (defcustom todo-file-top (locate-user-emacs-file "todo-top" ".todo-top")
322 "TODO mode top priorities file.
324 Not in TODO format, but diary compatible.
325 Automatically generated when `todo-save-top-priorities' is non-nil."
326 :version "24.4" ; added locate-user-emacs-file
327 :type 'string
328 :group 'todo)
330 (defcustom todo-print-function 'ps-print-buffer-with-faces
331 "Function to print the current buffer."
332 :type 'symbol
333 :group 'todo)
334 (defcustom todo-show-priorities 1
335 "Default number of priorities to show by \\[todo-top-priorities].
336 0 means show all entries."
337 :type 'integer
338 :group 'todo)
339 (defcustom todo-print-priorities 0
340 "Default number of priorities to print by \\[todo-print].
341 0 means print all entries."
342 :type 'integer
343 :group 'todo)
344 (defcustom todo-remove-separator t
345 "Non-nil to remove category separators in\
346 \\[todo-top-priorities] and \\[todo-print]."
347 :type 'boolean
348 :group 'todo)
349 (defcustom todo-save-top-priorities-too t
350 "Non-nil makes `todo-save' automatically save top-priorities in `todo-file-top'."
351 :type 'boolean
352 :group 'todo)
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".
357 (defcustom todo-time-string-format
358 "%:y-%02m-%02d %02H:%02M"
359 "TODO mode time string format for done entries.
360 For details see the variable `time-stamp-format'."
361 :type 'string
362 :group 'todo)
364 (defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
365 "Function producing text to insert at start of todo entry."
366 :type 'symbol
367 :group 'todo)
368 (defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
369 "Initials of todo item author."
370 :type 'string
371 :group 'todo)
373 (defun todo-entry-timestamp-initials ()
374 "Prepend timestamp and your initials to the head of a TODO entry."
375 (let ((time-stamp-format todo-time-string-format))
376 (concat (time-stamp-string) " " todo-initials ": ")))
378 ;; ---------------------------------------------------------------------------
380 ;; Set up some helpful context ...
382 (defvar todo-categories nil
383 "TODO categories.")
385 (defvar todo-cats nil
386 "Old variable for holding the TODO categories.
387 Use `todo-categories' instead.")
389 (defvar todo-previous-line 0
390 "Previous line asked about.")
392 (defvar todo-previous-answer 0
393 "Previous answer got.")
395 (defvar todo-mode-map
396 (let ((map (make-keymap)))
397 (suppress-keymap map t)
398 (define-key map "+" 'todo-forward-category)
399 (define-key map "-" 'todo-backward-category)
400 (define-key map "d" 'todo-file-item) ;done/delete
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)
405 (define-key map "I" 'todo-insert-item-here)
406 (define-key map "j" 'todo-jump-to-category)
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)
411 (define-key map "P" 'todo-print)
412 (define-key map "q" 'todo-quit)
413 (define-key map "r" 'todo-raise-item)
414 (define-key map "s" 'todo-save)
415 (define-key map "S" 'todo-save-top-priorities)
416 (define-key map "t" 'todo-top-priorities)
417 map)
418 "TODO mode keymap.")
420 (defvar todo-category-number 0 "TODO category number.")
422 (defvar todo-tmp-buffer-name " *todo tmp*")
424 (defvar todo-category-sep (make-string 75 ?-)
425 "Category separator.")
427 (defvar todo-category-beg " --- "
428 "Category start separator to be prepended onto category name.")
430 (defvar todo-category-end "--- End"
431 "Separator after a category.")
433 (defvar todo-header "-*- mode: todo; "
434 "Header of todo files.")
436 ;; ---------------------------------------------------------------------------
438 (defun todo-category-select ()
439 "Make TODO mode display the current category correctly."
440 (let ((name (nth todo-category-number todo-categories)))
441 (setq mode-line-buffer-identification
442 ;; (concat "Category: " name))
443 (concat "Category: " (format "%18s" name)))
444 (widen)
445 (goto-char (point-min))
446 (search-forward-regexp
447 (concat "^"
448 (regexp-quote (concat todo-prefix todo-category-beg name))
449 "$"))
450 (let ((begin (1+ (line-end-position))))
451 (search-forward-regexp (concat "^" todo-category-end))
452 (narrow-to-region begin (line-beginning-position))
453 (goto-char (point-min)))))
454 (defalias 'todo-cat-slct 'todo-category-select)
456 (defun todo-forward-category ()
457 "Go forward to TODO list of next category."
458 (interactive)
459 (setq todo-category-number
460 (mod (1+ todo-category-number) (length todo-categories)))
461 (todo-category-select))
462 (defalias 'todo-cmd-forw 'todo-forward-category)
464 (defun todo-backward-category ()
465 "Go back to TODO list of previous category."
466 (interactive)
467 (setq todo-category-number
468 (mod (1- todo-category-number) (length todo-categories)))
469 (todo-category-select))
470 (defalias 'todo-cmd-back 'todo-backward-category)
472 (defun todo-backward-item ()
473 "Select previous entry of TODO list."
474 (interactive)
475 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
476 (message ""))
477 (defalias 'todo-cmd-prev 'todo-backward-item)
479 (defun todo-forward-item (&optional count)
480 "Select COUNT-th next entry of TODO list."
481 (interactive "P")
482 (if (listp count) (setq count (car count)))
483 (end-of-line)
484 (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
485 nil 'goto-end count)
486 (beginning-of-line)
487 (message ""))
488 (defalias 'todo-cmd-next 'todo-forward-item)
490 (defun todo-save ()
491 "Save the TODO list."
492 (interactive)
493 (save-excursion
494 (save-restriction
495 (save-buffer)))
496 (if todo-save-top-priorities-too (todo-save-top-priorities)))
497 (defalias 'todo-cmd-save 'todo-save)
499 (defun todo-quit ()
500 "Done with TODO list for now."
501 (interactive)
502 (widen)
503 (todo-save)
504 (message "")
505 (bury-buffer))
506 (defalias 'todo-cmd-done 'todo-quit)
508 (defun todo-edit-item ()
509 "Edit current TODO list entry."
510 (interactive)
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")))
521 (defalias 'todo-cmd-edit 'todo-edit-item)
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)))
527 (switch-to-buffer
528 (make-indirect-buffer
529 (file-name-nondirectory todo-file-do) buffer-name))
530 (message "To exit, simply kill this buffer and return to list.")
531 (todo-edit-mode)
532 (narrow-to-region (todo-item-start) (todo-item-end))))
534 ;;;###autoload
535 (defun todo-add-category (&optional cat)
536 "Add new category CAT to the TODO list."
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)
543 (error "Error in %s: File is non-empty but contains no category"
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))
560 (if (search-forward "-*- mode: todo; " (+ (point-min) 16) t)
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))
570 (if (called-interactively-p 'interactive)
571 ;; properly display the newly added category
572 (progn (setq todo-category-number 0) (todo-show))
573 0))))
575 ;;;###autoload
576 (defun todo-add-item-non-interactively (new-item category)
577 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
578 (save-excursion
579 (todo-show))
580 (save-excursion
581 (if (string= "" category)
582 (setq category (nth todo-category-number todo-categories)))
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))))
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)
595 (todo-more-important-p current) nil)))
596 (if answer
597 (setq bottom current)
598 (setq top (1+ current)))))
599 (setq top (/ (+ top bottom) 2))
600 ;; goto-line doesn't have the desired behavior in a narrowed buffer.
601 (goto-char (point-min))
602 (forward-line (1- top)))
603 (insert new-item "\n")
604 (todo-backward-item)
605 (todo-save)
606 (message "")))
608 ;;;###autoload
609 (defun todo-insert-item (arg)
610 "Insert new TODO list entry.
611 With a prefix argument ARG solicit the category, otherwise use the current
612 category."
613 (interactive "P")
614 (save-excursion
615 (if (not (derived-mode-p 'todo-mode)) (todo-show))
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)))))
621 (current-category (nth todo-category-number todo-categories))
622 (category (if arg (todo-completing-read) current-category)))
623 (todo-add-item-non-interactively new-item category))))
625 (defalias 'todo-cmd-inst 'todo-insert-item)
627 (defun todo-insert-item-here ()
628 "Insert a new TODO list entry directly above the entry at point.
629 If 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))))))
637 (unless (and (bolp) (eolp)) (todo-item-start))
638 (insert (concat new-item "\n"))
639 (backward-char)
640 ;; put point at start of new entry
641 (todo-item-start)))
643 (defun todo-more-important-p (line)
644 "Ask whether entry is more important than the one at LINE."
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 (format-message "More important than `%s'? " item)))))
652 todo-previous-answer)
653 (defalias 'todo-ask-p 'todo-more-important-p)
655 (defun todo-delete-item ()
656 "Delete current TODO list entry."
657 (interactive)
658 (if (> (count-lines (point-min) (point-max)) 0)
659 (let* ((todo-entry (todo-item-string-start))
660 (todo-answer (y-or-n-p (concat "Permanently remove '"
661 todo-entry "'? "))))
662 (when todo-answer
663 (todo-remove-item)
664 (todo-backward-item))
665 (message ""))
666 (error "No TODO list entry to delete")))
667 (defalias 'todo-cmd-kill 'todo-delete-item)
669 (defun todo-raise-item ()
670 "Raise priority of current entry."
671 (interactive)
672 (if (> (count-lines (point-min) (point)) 0)
673 (let ((item (todo-item-string)))
674 (todo-remove-item)
675 (todo-backward-item)
676 (save-excursion
677 (insert item "\n"))
678 (message ""))
679 (error "No TODO list entry to raise")))
680 (defalias 'todo-cmd-rais 'todo-raise-item)
682 (defun todo-lower-item ()
683 "Lower priority of current entry."
684 (interactive)
685 (if (> (count-lines (point) (point-max)) 1)
686 ;; Assume there is a final newline
687 (let ((item (todo-item-string)))
688 (todo-remove-item)
689 (todo-forward-item)
690 (save-excursion
691 (insert item "\n"))
692 (message ""))
693 (error "No TODO list entry to lower")))
694 (defalias 'todo-cmd-lowr 'todo-lower-item)
696 (defun todo-file-item (&optional comment)
697 "File the current TODO list entry away, annotated with an optional COMMENT."
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))
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)))
708 "\n\t")
709 "(" comment ")"))
710 (goto-char (todo-item-end))
711 (insert " [" (nth todo-category-number todo-categories) "]")
712 (todo-item-start)
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)))
719 (append-to-file temp-point (todo-item-end 'include-sep) todo-file-done)
720 (delete-region temp-point (todo-item-end 'include-sep)))
721 (todo-backward-item)
722 (message "")))
724 ;; ---------------------------------------------------------------------------
726 ;; Utility functions:
729 ;;;###autoload
730 (defun todo-top-priorities (&optional nof-priorities category-pr-page
731 interactive)
732 "List top priorities for each category.
734 Number of entries for each category is given by NOF-PRIORITIES which
735 defaults to `todo-show-priorities'.
737 If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
738 between each category.
739 INTERACTIVE should be non-nil if this function is called interactively."
741 (interactive "P\ni\nP")
742 (or nof-priorities (setq nof-priorities todo-show-priorities))
743 (if (listp nof-priorities) ;universal argument
744 (setq nof-priorities (car nof-priorities)))
745 (let ((todo-print-buffer-name todo-tmp-buffer-name)
746 ;;(todo-print-category-number 0)
747 (todo-category-break (if category-pr-page "\f" ""))
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)
755 (save-excursion
756 (todo-show)
757 (save-restriction
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
784 (when interactive (display-buffer todo-print-buffer-name))
785 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
786 todo-print-buffer-name)))
788 (defun todo-save-top-priorities (&optional nof-priorities)
789 "Save top priorities for each category in `todo-file-top'.
791 Number of entries for each category is given by NOF-PRIORITIES which
792 defaults to `todo-show-priorities'."
793 (interactive "P")
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)))))
802 ;;;###autoload
803 (defun todo-print (&optional category-pr-page)
804 "Print todo summary using `todo-print-function'.
805 If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
806 between each category.
808 Number of entries for each category is given by `todo-print-priorities'."
809 (interactive "P")
810 (save-window-excursion
811 (save-excursion
812 (save-restriction
813 (todo-top-priorities todo-print-priorities
814 category-pr-page)
815 (set-buffer todo-tmp-buffer-name)
816 (and (funcall todo-print-function)
817 (kill-this-buffer))
818 (message "Todo printing done.")))))
820 (defun todo-jump-to-category ()
821 "Jump to a category. Default is previous category."
822 (interactive)
823 (let ((category (todo-completing-read)))
824 (if (string= "" category)
825 (setq category (nth todo-category-number todo-categories)))
826 (setq todo-category-number
827 (if (member category todo-categories)
828 (- (length todo-categories)
829 (length (member category todo-categories)))
830 (todo-add-category category)))
831 (todo-show)))
833 (defun todo-line-string ()
834 "Return current line in buffer as a string."
835 (buffer-substring (line-beginning-position) (line-end-position)))
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)
842 (setq item (concat (substring item 0 56) "...")))
843 item))
845 (defun todo-item-start ()
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))
853 (defun todo-item-end (&optional include-sep)
854 "Return point at end of current TODO list item.
855 If INCLUDE-SEP is non-nil, return point after the separator."
856 (save-excursion
857 (end-of-line)
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)))
864 (defun todo-remove-item ()
865 "Delete the current entry from the TODO list."
866 (delete-region (todo-item-start) (todo-item-end 'include-sep)))
868 (defun todo-item-string ()
869 "Return current TODO list entry as a string."
870 (buffer-substring (todo-item-start) (todo-item-end)))
872 (defun todo-string-count-lines (string)
873 "Return the number of lines STRING spans."
874 (length (split-string string "\n")))
876 (defun todo-string-multiline-p (string)
877 "Return non-nil if STRING spans several lines."
878 (> (todo-string-count-lines string) 1))
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))
894 ;; ---------------------------------------------------------------------------
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]
922 ;; As calendar reads todo-file-do before todo-mode is loaded.
923 ;;;###autoload
924 (define-derived-mode todo-mode nil "TODO"
925 "Major mode for editing TODO lists."
926 (easy-menu-add todo-menu))
928 (defvar date)
929 (defvar entry)
931 ;; t-c should be used from diary code, which requires calendar.
932 (declare-function calendar-current-date "calendar" (&optional offset))
934 ;; Read about this function in the setup instructions above!
935 ;;;###autoload
936 (defun todo-cp ()
937 "Make a diary entry appear only in the current date's diary."
938 (if (equal (calendar-current-date) date)
939 entry))
941 (define-derived-mode todo-edit-mode text-mode "TODO Edit"
942 "Major mode for editing items in the TODO list.
944 \\{todo-edit-mode-map}")
946 ;;;###autoload
947 (defun todo-show ()
948 "Show TODO list."
949 (interactive)
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)))
956 (find-file todo-file-do)
957 (todo-initial-setup))
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)))
967 (beginning-of-line)
968 (todo-category-select))
970 (defun todo-initial-setup ()
971 "Set up things to work properly in TODO mode."
972 (find-file todo-file-do)
973 (erase-buffer)
974 (todo-mode)
975 (todo-add-category "Todo"))
977 (provide 'todo-mode)
979 ;;; todo-mode.el ends here