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