1 ;; todo-mode.el -- Major mode for editing TODO list files
3 ;; Copyright (C) 1997, 2000 Free Software Foundation, Inc.
5 ;; Author: os10000@seidel-space.de
7 ;; Version: $Id: todo-mode.el,v 1.40 2000/06/02 18:37:43 fx Exp $
8 ;; Keywords: Categorised TODO list editor, todo-mode
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;; ---------------------------------------------------------------------------
33 ;; TODO is a major mode for EMACS which offers functionality to
34 ;; treat most lines in one buffer as a list of items one has to
35 ;; do. There are facilities to add new items, which are
36 ;; categorised, to edit or even delete items from the buffer.
37 ;; The buffer contents are currently compatible with the diary,
38 ;; so that the list of todo-items will show up in the FANCY diary
41 ;; Notice: Besides the major mode, this file also exports the
42 ;; function `todo-show' which will change to the one specific
43 ;; TODO file that has been specified in the todo-file-do
44 ;; variable. If this file does not conform to the TODO mode
45 ;; conventions, the todo-show function will add the appropriate
46 ;; header and footer. I don't anticipate this to cause much
47 ;; grief, but be warned, in case you attempt to read a plain text
50 ;; Preface, Quickstart Installation
52 ;; To get this to work, make emacs execute the line
54 ;; (autoload 'todo-mode "todo-mode"
55 ;; "Major mode for editing TODO lists." t)
56 ;; (autoload 'todo-show "todo-mode"
57 ;; "Show TODO items." t)
58 ;; (autoload 'todo-insert-item "todo-mode"
59 ;; "Add TODO item." t)
61 ;; You may now enter new items by typing "M-x todo-insert-item",
62 ;; or enter your TODO list file by typing "M-x todo-show".
64 ;; -------------------------------------------------------------
66 ;; oh no, it doesn't work any more ... but Alex Schroeder
67 ;; <a.schroeder@bsiag.ch> writes:
69 ;; -------------------------------------------------------------
71 ;; 1. Call todo-show (I called todo-insert first)
72 ;; 2. Add some categories (I called todo-insert)
73 ;; 3. Save the buffer, restart Emacs (perhaps restarting Emacs is not
75 ;; 4. Only now can you start to add entries.
77 ;; This is a bit cumbersome, and it should be documented. You probably
79 ;; -------------------------------------------------------------
81 ;; and right he is. My apologies, I'll try to fix it sometime.
83 ;; -------------------------------------------------------------
85 ;; The TODO list file has a special format and some auxiliary
86 ;; information, which will be added by the todo-show function if
87 ;; it attempts to visit an un-initialised file. Hence it is
88 ;; recommended to use the todo-show function for the first time,
89 ;; in order to initialise the file, but it is not necessary
92 ;; As these commands are quite long to type, I would recommend
93 ;; the addition of two bindings to your to your global keymap. I
94 ;; personally have the following in my initialisation file:
96 ;; (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
97 ;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
99 ;; Note, however, that this recommendation has prompted some
100 ;; criticism, since the keys C-c LETTER are reserved for user
101 ;; functions. I believe my recommendation is acceptable, since
102 ;; the Emacs Lisp Manual *Tips* section also details that the
103 ;; mode itself should not bind any functions to those keys. The
104 ;; express aim of the above two bindings is to work outside the
105 ;; mode, which doesn't need the show function and offers a
106 ;; different binding for the insert function. They serve as
107 ;; shortcuts and are not even needed (since the TODO mode will be
108 ;; entered by visiting the TODO file, and later by switching to
111 ;; If you are an advanced user of this package, please consult
112 ;; the whole source code for autoloads, because there are several
113 ;; extensions that are not explicitly listed in the above quick
118 ;; Which version of todo-mode.el does this documentation refer to?
120 ;; $Id: todo-mode.el,v 1.40 2000/06/02 18:37:43 fx Exp $
124 ;; You will have the following facilities available:
126 ;; M-x todo-show will enter the todo list screen, here type
128 ;; spc will toggle the display of sub-trees
130 ;; + to go to next category
131 ;; - to go to previous category
132 ;; d to file the current entry, including a
133 ;; comment and timestamp
134 ;; e to edit the current entry
135 ;; E to edit a multi-line entry
136 ;; f to file the current entry, including a
137 ;; comment and timestamp
138 ;; i to insert a new entry, with prefix, omit category
139 ;; I to insert a new entry at current cursor position
140 ;; j jump to category
141 ;; k to kill the current entry
142 ;; l to lower the current entry's priority
143 ;; n for the next entry
144 ;; p for the previous entry
146 ;; q to save the list and exit the buffer
147 ;; r to raise the current entry's priority
148 ;; s to save the list
149 ;; S to save the list of top priorities
150 ;; t show top priority items for each category
152 ;; When you add a new entry, you are asked for the text and then
153 ;; for the category. I for example have categories for things
154 ;; that I want to do in the office (like mail my mum), that I
155 ;; want to do in town (like buy cornflakes) and things I want to
156 ;; do at home (move my suitcases). The categories can be
157 ;; selected with the cursor keys and if you type in the name of a
158 ;; category which didn't exist before, an empty category of the
159 ;; desired name will be added and filled with the new entry.
163 ;; Variable todo-prefix
165 ;; I would like to recommend that you use the prefix "*/*" (by
166 ;; leaving the variable 'todo-prefix' untouched) so that the
167 ;; diary displays each entry every day.
169 ;; To understand what I mean, please read the documentation that
170 ;; goes with the calendar since that will tell you how you can
171 ;; set up the fancy diary display and use the #include command to
172 ;; include your todo list file as part of your diary.
174 ;; If you have the diary package set up to usually display more
175 ;; than one day's entries at once, consider using
177 ;; "&%%(equal (calendar-current-date) date)"
179 ;; as the value of `todo-prefix'. Please note that this may slow
180 ;; down the processing of your diary file some.
182 ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
186 ;; might be nicer and to that effect a function has been declared
187 ;; further down in the code. You may wish to auto-load this.
189 ;; Carsten also writes that that *changing* the prefix after the
190 ;; todo list is already established is not as simple as changing
191 ;; the variable - the todo files have to be changed by hand.
193 ;; Variable todo-file-do
195 ;; This variable is fairly self-explanatory. You have to store
196 ;; your TODO list somewhere. This variable tells the package
197 ;; where to go and find this file.
199 ;; Variable todo-file-done
201 ;; Even when you're done, you may wish to retain the entries.
202 ;; Given that they're timestamped and you are offered to add a
203 ;; comment, this can make a useful diary of past events. It will
204 ;; even blend in with the EMACS diary package. So anyway, this
205 ;; variable holds the name of the file for the filed todo-items.
207 ;; Variable todo-file-top
209 ;; File storing the top priorities of your TODO list when
210 ;; todo-save-top-priorities is non-nil. Nice to include in your
211 ;; diary instead of the complete TODO list.
213 ;; Variable todo-mode-hook
215 ;; Just like other modes, too, this mode offers to call your
216 ;; functions before it goes about its business. This variable
217 ;; will be inspected for any functions you may wish to have
218 ;; called once the other TODO mode preparations have been
221 ;; Variable todo-insert-threshold
223 ;; Another nifty feature is the insertion accuracy. If you have
224 ;; 8 items in your TODO list, then you may get asked 4 questions
225 ;; by the binary insertion algorithm. However, you may not
226 ;; really have a need for such accurate priorities amongst your
227 ;; TODO items. If you now think about the binary insertion
228 ;; halfing the size of the window each time, then the threshhold
229 ;; is the window size at which it will stop. If you set the
230 ;; threshhold to zero, the upper and lower bound will coincide at
231 ;; the end of the loop and you will insert your item just before
232 ;; that point. If you set the threshhold to, e.g. 8, it will stop
233 ;; as soon as the window size drops below that amount and will
234 ;; insert the item in the approximate centre of that window. I
235 ;; got the idea for this feature after reading a very helpful
236 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
237 ;; corrected some of my awful coding and pointed me towards some
238 ;; good reading. Thanks Trey!
242 ;; These originally were my ideas, but now also include all the
243 ;; suggestions that I included before forgetting them:
245 ;; o Fancy fonts for todo/top-priority buffer
246 ;; o Remove todo-prefix option in todo-top-priorities
248 ;; o Move entry from one category to another one
249 ;; o Entries which both have the generic */* prefix and a
250 ;; "deadline" entry which are understood by diary, indicating
251 ;; an event (unless marked by &)
252 ;; o The optional COUNT variable of todo-forward-item should be
253 ;; applied to the other functions performing similar tasks
254 ;; o Modularization could be done for repeaded elements of
255 ;; the code, like the completing-read lines of code.
256 ;; o license / version function
257 ;; o export to diary file
260 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
262 ;; o rewrite complete package to store data as lisp objects
263 ;; and have display modes for display, for diary export,
264 ;; etc. (Richard Stallman pointed out this is a bad idea)
265 ;; o so base todo-mode.el on generic-mode.el instead
267 ;; History and Gossip
269 ;; Many thanks to all the ones who have contributed to the
270 ;; evolution of this package! I hope I have listed all of you
271 ;; somewhere in the documentation or at least in the RCS history!
273 ;; Enjoy this package and express your gratitude by sending nice
274 ;; things to my parents' address!
277 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
284 ;; Calendar dynamic bondage:
287 (autoload 'time-stamp-string
"time-stamp")
289 ;; User-configurable variables:
292 "Maintain a list of todo items."
296 (defcustom todo-prefix
"*/*"
297 "*TODO mode prefix for entries.
299 This is useful in conjunction with `calendar' and `diary' if you use
301 #include \"~/.todo-do\"
303 in your diary file to include your todo list file as part of your
304 diary. With the default value \"*/*\" the diary displays each entry
305 every day and it may also be marked on every day of the calendar.
306 Using \"&%%(equal (calendar-current-date) date)\" instead will only
307 show and mark todo entreis for today, but may slow down processing of
308 the diary file somewhat."
311 (defcustom todo-file-do
"~/.todo-do"
312 "*TODO mode list file."
315 (defcustom todo-file-done
"~/.todo-done"
316 "*TODO mode archive file."
319 (defcustom todo-mode-hook nil
323 (defcustom todo-edit-mode-hook nil
324 "*TODO Edit mode hooks."
327 (defcustom todo-insert-threshold
0
328 "*TODO mode insertion accuracy.
330 If you have 8 items in your TODO list, then you may get asked 4
331 questions by the binary insertion algorithm. However, you may not
332 really have a need for such accurate priorities amongst your TODO
333 items. If you now think about the binary insertion halfing the size
334 of the window each time, then the threshhold is the window size at
335 which it will stop. If you set the threshhold to zero, the upper and
336 lower bound will coincide at the end of the loop and you will insert
337 your item just before that point. If you set the threshhold to,
338 e.g. 8, it will stop as soon as the window size drops below that
339 amount and will insert the item in the approximate centre of that
343 (defvar todo-edit-buffer
" *TODO Edit*" "TODO Edit buffer name.")
344 (defcustom todo-file-top
"~/.todo-top"
345 "*TODO mode top priorities file.
347 Not in TODO format, but diary compatible.
348 Automatically generated when `todo-save-top-priorities' is non-nil."
352 (defcustom todo-print-function
'ps-print-buffer-with-faces
353 "*Function to print the current buffer."
356 (defcustom todo-show-priorities
1
357 "*Default number of priorities to show by \\[todo-top-priorities].
358 0 means show all entries."
361 (defcustom todo-print-priorities
0
362 "*Default number of priorities to print by \\[todo-print].
363 0 means print all entries."
366 (defcustom todo-remove-separator t
367 "*Non-nil to remove category separators in\
368 \\[todo-top-priorities] and \\[todo-print]."
371 (defcustom todo-save-top-priorities-too t
372 "*Non-nil makes `todo-save' automatically save top-priorities in `todo-file-top'."
376 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
377 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
379 (defcustom todo-time-string-format
380 "%04y-%02m-%02d %02H:%02M"
381 "*TODO mode time string format for done entries.
382 For details see the variable `time-stamp-format'."
386 (defcustom todo-entry-prefix-function
'todo-entry-timestamp-initials
387 "*Function producing text to insert at start of todo entry."
390 (defcustom todo-initials
(or (getenv "INITIALS") (user-login-name))
391 "*Initials of todo item author."
395 (defun todo-entry-timestamp-initials ()
396 "Prepend timestamp and your initials to the head of a TODO entry."
397 (let ((time-stamp-format todo-time-string-format
))
398 (concat (time-stamp-string) " " todo-initials
": ")))
400 ;; Set up some helpful context ...
402 (defvar todo-categories nil
404 (defvar todo-cats nil
405 "Old variable for holding the TODO categories.
406 Use `todo-categories' instead.")
407 (defvar todo-previous-line
0
408 "Previous line asked about.")
409 (defvar todo-previous-answer
0
410 "Previous answer got.")
411 (defvar todo-category-number
0
412 "TODO category number.")
414 (defvar todo-category-sep
(make-string 75 ?-
)
415 "Category separator.")
416 (defvar todo-category-beg
" --- "
417 "Category start separator to be prepended onto category name.")
418 (defvar todo-category-end
"--- End"
419 "Separator after a category.")
420 (defvar todo-header
"-*- mode: todo; "
421 "Header of todo files.")
423 ;; ---------------------------------------------------------------------------
425 (defvar todo-mode-map
426 (let ((map (make-keymap)))
427 (suppress-keymap map t
)
428 (define-key map
"?" 'todo-help
)
429 (define-key map
" " 'todo-hide-show-subtree
)
430 (define-key map
"+" 'todo-forward-category
)
431 (define-key map
"-" 'todo-backward-category
)
432 (define-key map
"d" 'todo-file-item
) ;done/delete
433 (define-key map
"e" 'todo-edit-item
)
434 (define-key map
"E" 'todo-edit-multiline
)
435 (define-key map
"f" 'todo-file-item
)
436 (define-key map
"i" 'todo-insert-item
)
437 (define-key map
"I" 'todo-insert-item-here
)
438 (define-key map
"j" 'todo-jump-to-category
)
439 (define-key map
"k" 'todo-delete-item
)
440 (define-key map
"l" 'todo-lower-item
)
441 (define-key map
"n" 'todo-forward-item
)
442 (define-key map
"p" 'todo-backward-item
)
443 (define-key map
"P" 'todo-print
)
444 (define-key map
"q" 'todo-quit
)
445 (define-key map
"r" 'todo-raise-item
)
446 (define-key map
"s" 'todo-save
)
447 (define-key map
"S" 'todo-save-top-priorities
)
448 (define-key map
"t" 'todo-top-priorities
)
452 (defun todo-position (item list
)
453 "Return the position of the element in LIST testing `equal' to ITEM.
454 Return nil if ITEM not found."
458 (if (equal item
(pop list
))
461 (setq pos
(1+ pos
))))
464 (defun todo-category-select ()
465 "Make TODO mode display the current category correctly."
466 (let ((name (nth todo-category-number todo-categories
)))
467 (setq mode-line-buffer-identification
468 (concat "Category: " (format "%18s" name
)))
470 (goto-char (point-min))
471 (search-forward-regexp
473 (regexp-quote (concat todo-prefix todo-category-beg name
))
475 (let ((begin (1+ (line-end-position))))
476 (search-forward-regexp (concat "^" todo-category-end
))
477 (narrow-to-region begin
(line-beginning-position))
478 (goto-char (point-min)))))
479 (defalias 'todo-cat-slct
'todo-category-select
)
481 (defun todo-help () "Show TODO mode help."
483 (describe-function 'todo-mode
))
485 (defun todo-hide-show-subtree ()
486 "Hide or Show subtrees in the TODO list."
490 (if (outline-visible)
494 (defun todo-forward-category ()
495 "Go forward to TODO list of next category."
497 (setq todo-category-number
498 (mod (1+ todo-category-number
) (length todo-categories
)))
499 (todo-category-select))
500 (defalias 'todo-cmd-forw
'todo-forward-category
)
502 (defun todo-backward-category ()
503 "Go back to TODO list of previous category."
505 (setq todo-category-number
506 (mod (1- todo-category-number
) (length todo-categories
)))
507 (todo-category-select))
508 (defalias 'todo-cmd-back
'todo-backward-category
)
510 (defun todo-backward-item ()
511 "Select previous entry of TODO list."
513 (search-backward-regexp (concat "^" (regexp-quote todo-prefix
)) nil t
)
515 (defalias 'todo-cmd-prev
'todo-backward-item
)
517 (defun todo-forward-item (&optional count
)
518 "Select COUNT-th next entry of TODO list."
520 (if (listp count
) (setq count
(car count
)))
522 (search-forward-regexp (concat "^" (regexp-quote todo-prefix
))
526 (defalias 'todo-cmd-next
'todo-forward-item
)
528 (defun todo-save () "Save the TODO list."
531 (if todo-save-top-priorities-too
(todo-save-top-priorities))
533 (defalias 'todo-cmd-save
'todo-save
)
535 (defun todo-quit () "Done with TODO list for now."
541 (defalias 'todo-cmd-done
'todo-quit
)
543 (defun todo-edit-item () "Edit current TODO list entry."
545 (let ((item (todo-item-string)))
546 (if (todo-string-multiline-p item
)
547 (todo-edit-multiline)
548 (let ((new (read-from-minibuffer "Edit: " item
)))
553 (defalias 'todo-cmd-edit
'todo-edit-item
)
555 (defun todo-edit-multiline ()
556 "Set up a buffer for editing a multiline TODO list entry."
558 (let ((buffer-name (generate-new-buffer-name todo-edit-buffer
)))
560 (make-indirect-buffer
561 (find-buffer-visiting todo-file-do
) buffer-name
))
562 (message "To exit, simply kill this buffer and return to list.")
564 (narrow-to-region (todo-item-start) (todo-item-end))))
566 (defun todo-add-category (cat)
567 "Add new category CAT to the TODO list."
568 (interactive "sCategory: ")
569 (save-window-excursion
570 (add-to-list 'todo-categories cat
)
571 (find-file todo-file-do
)
573 (goto-char (point-min))
574 (let ((posn (search-forward "-*- mode: todo; " 17 t
)))
575 (if (not (null posn
)) (goto-char posn
))
578 (insert "-*- mode: todo; \n")
581 (insert (format "todo-categories: %S; -*-" todo-categories
))
583 (insert (format "%s%s%s\n%s\n%s %s\n"
584 todo-prefix todo-category-beg cat
586 todo-prefix todo-category-sep
))
590 (defun todo-add-item-non-interactively (new-item category
)
591 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
595 (if (string= "" category
)
596 (setq category
(nth todo-category-number todo-categories
)))
597 (setq todo-category-number
598 (or (todo-position category todo-categories
)
599 (todo-add-category category
)))
601 (setq todo-previous-line
0)
603 (bottom (1+ (count-lines (point-min) (point-max)))))
604 (while (> (- bottom top
) todo-insert-threshold
)
605 (let* ((current (/ (+ top bottom
) 2))
606 (answer (if (< current bottom
)
607 (todo-more-important-p current
) nil
)))
609 (setq bottom current
)
610 (setq top
(1+ current
)))))
611 (setq top
(/ (+ top bottom
) 2))
612 ;; goto-line doesn't have the desired behavior in a narrowed buffer
613 (goto-char (point-min))
614 (forward-line (1- top
)))
615 (insert new-item
"\n")
617 (progn ;;; horrible os10000 hack to make items appear when inserting into empty buffer
620 (todo-forward-category)
621 (todo-backward-category))
626 (defun todo-insert-item (ARG)
627 "Insert new TODO list entry.
628 With a prefix argument solicit the category, otherwise use the current
632 (if (not (string-equal mode-name
"TODO")) (todo-show))
633 (let* ((new-item (concat todo-prefix
" "
634 (read-from-minibuffer
636 (if todo-entry-prefix-function
637 (funcall todo-entry-prefix-function
)))))
638 (categories todo-categories
)
639 (history (cons 'categories
(1+ todo-category-number
)))
640 (current-category (nth todo-category-number todo-categories
))
646 current-category
"]: ")
647 (todo-category-alist) nil nil nil history
))))
648 (todo-add-item-non-interactively new-item category
))))
650 (defalias 'todo-cmd-inst
'todo-insert-item
)
652 (defun todo-insert-item-here ()
653 "Insert new TODO list entry under the cursor."
656 (if (not (string-equal mode-name
"TODO")) (todo-show))
657 (let* ((new-item (concat todo-prefix
" "
658 (read-from-minibuffer
660 (if todo-entry-prefix-function
661 (funcall todo-entry-prefix-function
))))))
662 (insert (concat new-item
"\n")))))
664 (defun todo-more-important-p (line)
665 "Ask whether entry is more important than the one at LINE."
666 (if (not (equal todo-previous-line line
))
668 (setq todo-previous-line line
)
669 (goto-char (point-min))
670 (forward-line (1- todo-previous-line
))
671 (let ((item (todo-item-string-start)))
672 (setq todo-previous-answer
673 (y-or-n-p (concat "More important than '" item
"'? "))))))
674 todo-previous-answer
)
675 (defalias 'todo-ask-p
'todo-more-important-p
)
677 (defun todo-delete-item () "Delete current TODO list entry."
679 (if (> (count-lines (point-min) (point-max)) 0)
680 (let* ((todo-entry (todo-item-string-start))
681 (todo-answer (y-or-n-p (concat "Permanently remove '"
686 (todo-backward-item)))
688 (error "No TODO list entry to delete")))
689 (defalias 'todo-cmd-kill
'todo-delete-item
)
691 (defun todo-raise-item () "Raise priority of current entry."
693 (if (> (count-lines (point-min) (point)) 0)
694 (let ((item (todo-item-string)))
700 (error "No TODO list entry to raise")))
701 (defalias 'todo-cmd-raise
'todo-raise-item
)
703 (defun todo-lower-item () "Lower priority of current entry."
705 (if (> (count-lines (point) (point-max)) 1)
706 ;; Assume there is a final newline
707 (let ((item (todo-item-string)))
713 (error "No TODO list entry to lower")))
714 (defalias 'todo-cmd-lowr
'todo-lower-item
)
716 (defun todo-file-item (&optional comment
)
717 "File the current TODO list entry away, annotated with an optional COMMENT."
718 (interactive "sComment: ")
719 (or (> (count-lines (point-min) (point-max)) 0)
720 (error "No TODO list entry to file away"))
721 (let ((time-stamp-format todo-time-string-format
))
722 (if (and comment
(> (length comment
) 0))
724 (goto-char (todo-item-end))
726 (if (save-excursion (beginning-of-line)
727 (looking-at (regexp-quote todo-prefix
)))
731 (goto-char (todo-item-end))
732 (insert " [" (nth todo-category-number todo-categories
) "]")
733 (goto-char (todo-item-start))
734 (let ((temp-point (point)))
735 (if (looking-at (regexp-quote todo-prefix
))
736 (replace-match (time-stamp-string))
737 ;; Standard prefix -> timestamp
738 ;; Else prefix non-standard item start with timestamp
739 (insert (time-stamp-string)))
740 (append-to-file temp-point
(1+ (todo-item-end)) todo-file-done
)
741 (delete-region temp-point
(1+ (todo-item-end))))
745 ;; ---------------------------------------------------------------------------
747 ;; Utility functions:
749 (defun todo-top-priorities (&optional nof-priorities category-pr-page
)
750 "List top priorities for each category.
752 Number of entries for each category is given by NOF-PRIORITIES which
753 defaults to \'todo-show-priorities\'.
755 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
756 between each category."
759 (or nof-priorities
(setq nof-priorities todo-show-priorities
))
760 (if (listp nof-priorities
) ;universal argument
761 (setq nof-priorities
(car nof-priorities
)))
762 (let ((todo-print-buffer-name " *todo-tmp*")
763 ;;(todo-print-category-number 0)
764 (todo-category-break (if category-pr-page
"\f" ""))
767 (if todo-remove-separator
768 (concat todo-category-end
"\n"
769 (regexp-quote todo-prefix
) " " todo-category-sep
"\n")
770 (concat todo-category-end
"\n"))))
776 (copy-to-buffer todo-print-buffer-name
(point-min) (point-max))
777 (set-buffer todo-print-buffer-name
)
778 (goto-char (point-min))
779 (if (re-search-forward (regexp-quote todo-header
) nil t
)
781 (beginning-of-line 1)
782 (kill-line))) ;Remove mode line
783 (while (re-search-forward ;Find category start
784 (regexp-quote (concat todo-prefix todo-category-beg
))
786 (setq beg
(+ (line-end-position) 1)) ;Start of first entry.
787 (re-search-forward cat-end nil t
)
788 (setq end
(match-beginning 0))
789 (replace-match todo-category-break
)
790 (narrow-to-region beg end
) ;In case we have too few entries.
791 (goto-char (point-min))
792 (if (= 0 nof-priorities
) ;Traverse entries.
793 (goto-char end
) ;All entries
794 (todo-forward-item nof-priorities
))
796 (delete-region beg end
)
798 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
799 (goto-char (point-min)) ;Due to display buffer
801 ;; Could have used switch-to-buffer as it has a norecord argument,
802 ;; which is nice when we are called from e.g. todo-print.
803 ;; Else we could have used pop-to-buffer.
804 (display-buffer todo-print-buffer-name
)
805 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
806 todo-print-buffer-name
)))
808 (defun todo-save-top-priorities (&optional nof-priorities
)
809 "Save top priorities for each category in `todo-file-top'.
811 Number of entries for each category is given by NOF-PRIORITIES which
812 defaults to `todo-show-priorities'."
815 (todo-top-priorities nof-priorities
)
816 (write-file todo-file-top
)))
819 (defun todo-print (&optional category-pr-page
)
820 "Print todo summary using `todo-print-function'.
821 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
822 between each category.
824 Number of entries for each category is given by
825 \'todo-print-priorities\'."
828 (todo-top-priorities todo-print-priorities
830 (funcall todo-print-function
)
831 (message "Todo printing done.")))
833 (defun todo-jump-to-category ()
834 "Jump to a category. Default is previous category."
836 (let* ((categories todo-categories
)
837 (history (cons 'categories
(1+ todo-category-number
)))
838 (category (completing-read
840 (nth todo-category-number todo-categories
) "]: ")
841 (todo-category-alist) nil nil nil history
)))
842 (if (string= "" category
)
843 (setq category
(nth todo-category-number todo-categories
)))
844 (setq todo-category-number
845 (or (todo-position category todo-categories
)
846 (todo-add-category category
)))
849 (defun todo-line-string ()
850 "Return current line in buffer as a string."
851 (buffer-substring (line-beginning-position) (line-end-position)))
853 (defun todo-item-string-start ()
854 "Return the start of this TODO list entry as a string."
855 ;; Suitable for putting in the minibuffer when asking the user
856 (let ((item (todo-item-string)))
857 (if (> (length item
) 60)
858 (setq item
(concat (substring item
0 56) "...")))
861 (defun todo-item-start ()
862 "Return point at start of current TODO list item."
865 (if (not (looking-at (regexp-quote todo-prefix
)))
866 (search-backward-regexp
867 (concat "^" (regexp-quote todo-prefix
)) nil t
))
870 (defun todo-item-end ()
871 "Return point at end of current TODO list item."
874 (search-forward-regexp
875 (concat "^" (regexp-quote todo-prefix
)) nil
'goto-end
)
876 (1- (line-beginning-position))))
878 (defun todo-remove-item ()
879 "Delete the current entry from the TODO list."
880 (delete-region (todo-item-start) (1+ (todo-item-end))))
882 (defun todo-item-string () "Return current TODO list entry as a string."
883 (buffer-substring (todo-item-start) (todo-item-end)))
885 (defun todo-string-count-lines (string)
886 "Return the number of lines STRING spans."
887 (length (split-string string
"\n")))
889 (defun todo-string-multiline-p (string)
890 "Return non-nil if STRING spans several lines."
891 (> (todo-string-count-lines string
) 1))
893 (defun todo-category-alist ()
894 "Generate an alist for use in `completing-read' from `todo-categories'."
895 (mapcar #'list todo-categories
))
897 ;; ---------------------------------------------------------------------------
900 todo-menu todo-mode-map
"Todo Menu"
902 ["Next category" todo-forward-category t
]
903 ["Previous category" todo-backward-category t
]
904 ["Jump to category" todo-jump-to-category t
]
905 ["Show top priority items" todo-top-priorities t
]
906 ["Print categories" todo-print t
]
908 ["Edit item" todo-edit-item t
]
909 ["File item" todo-file-item t
]
910 ["Insert new item" todo-insert-item t
]
911 ["Insert item here" todo-insert-item-here t
]
912 ["Kill item" todo-delete-item t
]
914 ["Lower item priority" todo-lower-item t
]
915 ["Raise item priority" todo-raise-item t
]
917 ["Next item" todo-forward-item t
]
918 ["Previous item" todo-backward-item t
]
921 ["Save Top Priorities" todo-save-top-priorities t
]
926 ;; As calendar reads .todo-do before todo-mode is loaded.
929 "Major mode for editing TODO lists.
933 (setq major-mode
'todo-mode
)
934 (setq mode-name
"TODO")
935 (use-local-map todo-mode-map
)
936 (easy-menu-add todo-menu
)
937 (setq fill-prefix
"\t\t")
938 (let ((prefix (regexp-quote todo-prefix
)))
939 (setq paragraph-separate prefix
)
940 (setq outline-regexp prefix
))
941 (outline-minor-mode 1)
942 (goto-char (point-min))
943 (outline-next-heading) ; get past -*- line
946 (run-hooks 'todo-mode-hook
))
948 ;; Read about this function in the setup instructions above!
951 "Make a diary entry appear only in the current date's diary."
952 (if (equal (calendar-current-date) date
)
955 (defun todo-edit-mode ()
956 "Major mode for editing items in the TODO list.
958 \\{todo-edit-mode-map}"
960 (setq major-mode
'todo-edit-mode
)
961 (setq mode-name
"TODO Edit")
962 (run-hooks 'todo-edit-mode-hook
))
968 (if (file-exists-p todo-file-do
)
969 (find-file todo-file-do
)
970 (todo-initial-setup))
971 (if (null todo-categories
)
973 (error "Error in %s: No categories in list `todo-categories'"
975 (goto-char (point-min))
976 (and (search-forward "todo-cats:" nil t
)
977 (replace-match "todo-categories:"))
978 (make-local-variable 'todo-categories
)
979 (setq todo-categories todo-cats
)))
981 (todo-category-select))
983 (defun todo-initial-setup ()
984 "Set up things to work properly in TODO mode."
985 (find-file todo-file-do
)
988 (todo-add-category "Todo"))
992 ;; ---------------------------------------------------------------------------
993 ;;; todo-mode.el ends here
994 ;; ---------------------------------------------------------------------------