* calendar/todos.el (todos-window-configuration): Comment out.
[emacs.git] / lisp / calendar / todos.el
blobeaf4b92f5c8a577f53928ca7853ed7a3c0c15206
1 ;;; todos.el --- major mode for editing TODO list files
3 ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Oliver Seidel <privat@os10000.net>
7 ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
8 ;; Created: 2 Aug 1997
9 ;; Keywords: calendar, todo
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 ;; categorised, 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 todos-items will show up in the FANCY diary
38 ;; mode.
40 ;; Notice: Besides the major mode, this file also exports the
41 ;; function `todos-show' which will change to the one specific
42 ;; TODO file that has been specified in the todos-file-do
43 ;; variable. If this file does not conform to the TODO mode
44 ;; conventions, the todos-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 'todos "todos"
54 ;; "Major mode for editing TODO lists." t)
55 ;; (autoload 'todos-show "todos"
56 ;; "Show TODO items." t)
57 ;; (autoload 'todos-insert-item "todos"
58 ;; "Add TODO item." t)
60 ;; You may now enter new items by typing "M-x todos-insert-item",
61 ;; or enter your TODO list file by typing "M-x todos-show".
63 ;; The TODO list file has a special format and some auxiliary
64 ;; information, which will be added by the todos-show function if
65 ;; it attempts to visit an un-initialised file. Hence it is
66 ;; recommended to use the todos-show function for the first time,
67 ;; in order to initialise 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 initialisation file:
74 ;; (global-set-key "\C-ct" 'todos-show) ; switch to TODO buffer
75 ;; (global-set-key "\C-ci" 'todos-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 todos-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 todos-prefix
143 ;; I would like to recommend that you use the prefix "*/*" (by
144 ;; leaving the variable 'todos-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 `todos-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 ;; "&%%(todos-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 todos-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 todos-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 todos-items.
185 ;; Variable todos-file-top
187 ;; File storing the top priorities of your TODO list when
188 ;; todos-save-top-priorities is non-nil. Nice to include in your
189 ;; diary instead of the complete TODO list.
191 ;; Variable todos-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 todos-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 todos-prefix option in todos-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 todos-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 todos-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 todos.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 todos nil
265 "Maintain a list of todo items."
266 :link '(emacs-commentary-link "todos")
267 :version "21.1"
268 :group 'calendar)
270 (defcustom todos-prefix "*/*"
271 "TODO mode prefix for entries.
273 This is useful in conjunction with `calendar' and `diary' if you use
275 #include \"~/.todos-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 'todos)
285 (defcustom todos-file-do (convert-standard-filename "~/.emacs.d/.todos-do")
286 "TODO mode list file."
287 :type 'file
288 :group 'todos)
289 (defcustom todos-file-done (convert-standard-filename "~/.emacs.d/.todos-done")
290 "TODO mode archive file."
291 :type 'file
292 :group 'todos)
293 (defcustom todos-mode-hook nil
294 "TODO mode hooks."
295 :type 'hook
296 :group 'todos)
297 (defcustom todos-edit-mode-hook nil
298 "TODO Edit mode hooks."
299 :type 'hook
300 :group 'todos)
301 (defcustom todos-insert-threshold 0
302 "TODO mode insertion accuracy.
304 If you have 8 items in your TODO list, then you may get asked 4
305 questions by the binary insertion algorithm. However, you may not
306 really have a need for such accurate priorities amongst your TODO
307 items. If you now think about the binary insertion halving the size
308 of the window each time, then the threshold is the window size at
309 which it will stop. If you set the threshold to zero, the upper and
310 lower bound will coincide at the end of the loop and you will insert
311 your item just before that point. If you set the threshold to,
312 e.g. 8, it will stop as soon as the window size drops below that
313 amount and will insert the item in the approximate center of that
314 window."
315 :type 'integer
316 :group 'todos)
317 (defvar todos-edit-buffer " *TODO Edit*"
318 "TODO Edit buffer name.")
319 (defcustom todos-file-top (convert-standard-filename "~/.todos-top")
320 "TODO mode top priorities file.
322 Not in TODO format, but diary compatible.
323 Automatically generated when `todos-save-top-priorities' is non-nil."
324 :type 'string
325 :group 'todos)
327 (defcustom todos-print-function 'ps-print-buffer-with-faces
328 "Function to print the current buffer."
329 :type 'symbol
330 :group 'todos)
331 (defcustom todos-show-priorities 1
332 "Default number of priorities to show by \\[todos-top-priorities].
333 0 means show all entries."
334 :type 'integer
335 :group 'todos)
336 (defcustom todos-print-priorities 0
337 "Default number of priorities to print by \\[todos-print].
338 0 means print all entries."
339 :type 'integer
340 :group 'todos)
341 (defcustom todos-remove-separator t
342 "Non-nil to remove category separators in\
343 \\[todos-top-priorities] and \\[todos-print]."
344 :type 'boolean
345 :group 'todos)
346 (defcustom todos-save-top-priorities-too t
347 "Non-nil makes `todos-save' automatically save top-priorities in `todos-file-top'."
348 :type 'boolean
349 :group 'todos)
350 (defcustom todos-completion-ignore-case t ;; FIXME: nil for release
351 "Non-nil means don't consider case significant in todos-completing-read."
352 :type 'boolean
353 :group 'todos)
355 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
356 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
358 (defcustom todos-time-string-format
359 "%:y-%02m-%02d %02H:%02M"
360 "TODO mode time string format for done entries.
361 For details see the variable `time-stamp-format'."
362 :type 'string
363 :group 'todos)
365 (defcustom todos-entry-prefix-function 'todos-entry-timestamp-initials
366 "Function producing text to insert at start of todo entry."
367 :type 'symbol
368 :group 'todos)
369 (defcustom todos-initials (or (getenv "INITIALS") (user-login-name))
370 "Initials of todo item author."
371 :type 'string
372 :group 'todos)
374 (defun todos-entry-timestamp-initials ()
375 "Prepend timestamp and your initials to the head of a TODO entry."
376 (let ((time-stamp-format todos-time-string-format))
377 (concat (time-stamp-string) " " todos-initials ": ")))
379 (defface todos-prefix-string
380 '((t
381 :inherit font-lock-variable-name-face
383 "Face for Todos prefix string."
384 :group 'todos)
385 (defvar todos-prefix-face 'todos-prefix-string)
387 (defface todos-item-header
388 '((t
389 :inherit font-lock-function-name-face
391 "Face for Todos item header string."
392 :group 'todos)
393 (defvar todos-item-header-face 'todos-item-header)
395 (defvar todos-font-lock-keywords
396 (list
397 (list (concat "^" (regexp-quote todos-prefix)) 0 'todos-prefix-face t)
398 (list (concat "^" (regexp-quote todos-prefix) "\\(.*[0-9]+ [A-Ba-z0-9]*\\]?:\\)")
399 1 'todos-item-header-face t))
400 "Font-locking for Todos mode.")
402 ;; ---------------------------------------------------------------------------
404 ;; Set up some helpful context ...
406 (defvar todos-categories nil
407 "TODO categories.")
409 (defvar todos-previous-line 0
410 "Previous line asked about.")
412 (defvar todos-previous-answer 0
413 "Previous answer got.")
415 (defvar todos-mode-map
416 (let ((map (make-keymap)))
417 (suppress-keymap map t)
418 (define-key map "+" 'todos-forward-category)
419 (define-key map "-" 'todos-backward-category)
420 (define-key map "A" 'todos-add-category)
421 (define-key map "C" 'todos-display-categories)
422 (define-key map "d" 'todos-file-item) ;done/delete
423 (define-key map "D" 'todos-delete-category)
424 (define-key map "e" 'todos-edit-item)
425 (define-key map "E" 'todos-edit-multiline)
426 (define-key map "f" 'todos-file-item)
427 (define-key map "i" 'todos-insert-item)
428 (define-key map "I" 'todos-insert-item-here)
429 (define-key map "j" 'todos-jump-to-category)
430 (define-key map "k" 'todos-delete-item)
431 (define-key map "l" 'todos-lower-item)
432 (define-key map "m" 'todos-move-item)
433 (define-key map "n" 'todos-forward-item)
434 (define-key map "p" 'todos-backward-item)
435 (define-key map "P" 'todos-print)
436 (define-key map "q" 'todos-quit)
437 (define-key map "r" 'todos-raise-item)
438 (define-key map "R" 'todos-rename-category)
439 (define-key map "s" 'todos-save)
440 (define-key map "S" 'todos-save-top-priorities)
441 (define-key map "t" 'todos-top-priorities)
442 map)
443 "TODO mode keymap.")
445 (defvar todos-category-number 0 "TODO category number.")
447 (defvar todos-tmp-buffer-name " *todo tmp*")
449 (defvar todos-category-sep (make-string 75 ?-)
450 "Category separator.")
452 (defvar todos-category-beg " --- "
453 "Category start separator to be prepended onto category name.")
455 (defvar todos-category-end "--- End"
456 "Separator after a category.")
458 ;; (defvar todos-window-configuration nil
459 ;; "Variable for storing current window configuration in Todos mode.
461 ;; Set before leaving Todos mode buffer by todos-display-categories.
462 ;; Restored before re-entering Todo mode buffer by todo-kill-buffer
463 ;; and todo-jump-to-category-noninteractively.")
465 ;; ---------------------------------------------------------------------------
467 (defun todos-category-select ()
468 "Make TODO mode display the current category correctly."
469 (let ((name (nth todos-category-number todos-categories)))
470 (setq mode-line-buffer-identification
471 ;; (concat "Category: " name))
472 (concat "Category: " (format "%18s" name)))
473 (widen)
474 (goto-char (point-min))
475 (search-forward-regexp
476 (concat "^"
477 (regexp-quote (concat todos-prefix todos-category-beg name))
478 "$"))
479 (let ((begin (1+ (line-end-position))))
480 (search-forward-regexp (concat "^" todos-category-end))
481 (narrow-to-region begin (line-beginning-position))
482 (goto-char (point-min)))))
484 (defun todos-forward-category ()
485 "Go forward to TODO list of next category."
486 (interactive)
487 (setq todos-category-number
488 (mod (1+ todos-category-number) (length todos-categories)))
489 (todos-category-select))
491 (defun todos-backward-category ()
492 "Go back to TODO list of previous category."
493 (interactive)
494 (setq todos-category-number
495 (mod (1- todos-category-number) (length todos-categories)))
496 (todos-category-select))
498 (defun todos-backward-item ()
499 "Select previous entry of TODO list."
500 (interactive)
501 (search-backward-regexp (concat "^" (regexp-quote todos-prefix)) nil t)
502 (message ""))
504 (defun todos-forward-item (&optional count)
505 "Select COUNT-th next entry of TODO list."
506 (interactive "P")
507 (if (listp count) (setq count (car count)))
508 (end-of-line)
509 (search-forward-regexp (concat "^" (regexp-quote todos-prefix))
510 nil 'goto-end count)
511 (beginning-of-line)
512 (message ""))
514 (defun todos-save ()
515 "Save the TODO list."
516 (interactive)
517 (save-excursion
518 (save-restriction
519 (save-buffer)))
520 (if todos-save-top-priorities-too (todos-save-top-priorities)))
522 (defun todos-quit ()
523 "Done with TODO list for now."
524 (interactive)
525 (widen)
526 (todos-save)
527 (message "")
528 (bury-buffer))
530 (defun todos-edit-item ()
531 "Edit current TODO list entry."
532 (interactive)
533 (let* ((prefix (concat todos-prefix " " (todos-entry-timestamp-initials)))
534 ;; don't allow editing of Todos prefix string
535 ;; FIXME: or should this be automatically updated upon editing?
536 (item (substring (todos-item-string) (length prefix))))
537 ;; FIXME: disable minibuffer-history ??
538 ;; (minibuffer-history))
539 (if (todos-string-multiline-p item)
540 (todos-edit-multiline)
541 (let ((new (concat prefix (read-from-minibuffer "Edit: " item))))
542 (todos-remove-item)
543 (insert new "\n")
544 (todos-backward-item)
545 (message "")))))
547 (defun todos-edit-multiline ()
548 "Set up a buffer for editing a multiline TODO list entry."
549 (interactive)
550 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
551 (switch-to-buffer
552 (make-indirect-buffer
553 (file-name-nondirectory todos-file-do) buffer-name))
554 (message "To exit, simply kill this buffer and return to list.")
555 (todos-edit-mode)
556 (narrow-to-region (todos-item-start) (todos-item-end))))
558 ;;;###autoload
559 (defun todos-add-category (&optional cat)
560 "Add new category CAT to the TODO list."
561 (interactive)
562 (let ((buf (find-file-noselect todos-file-do t))
563 (prompt "Category: "))
564 (unless (zerop (buffer-size buf))
565 (and (null todos-categories)
566 (error "Error in %s: File is non-empty but contains no category"
567 todos-file-do)))
568 (unless cat (setq cat (read-from-minibuffer prompt)))
569 (with-current-buffer buf
570 ;; reject names that could induce bugs and confusion
571 (while (and (cond ((string= "" cat)
572 (setq prompt "Enter a non-empty category name: "))
573 ((string-match "\\`\\s-+\\'" cat)
574 (setq prompt "Enter a category name that is not only white space: "))
575 ((member cat todos-categories)
576 (setq prompt "Enter a non-existing category name: ")))
577 (setq cat (read-from-minibuffer prompt))))
578 ;; initialize a newly created Todo buffer for Todo mode
579 (unless (file-exists-p todos-file-do) (todos-mode))
580 (setq todos-categories (cons cat todos-categories))
581 (widen)
582 (goto-char (point-min))
583 ;; (if (search-forward "-*- mode: todo; " 17 t)
584 ;; (kill-line)
585 ;; (insert "-*- mode: todo; \n")
586 ;; (forward-char -1))
587 ;; (insert (format "todos-categories: %S; -*-" todos-categories))
588 ;; (forward-char 1)
589 (insert (format "%s%s%s\n%s\n%s %s\n"
590 todos-prefix todos-category-beg cat
591 todos-category-end
592 todos-prefix todos-category-sep))
593 (if (interactive-p)
594 ;; properly display the newly added category
595 (progn (setq todos-category-number 0) (todos-show))
596 0))))
598 ;;;###autoload
599 (defun todos-add-item-non-interactively (new-item category)
600 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
601 (save-excursion
602 (todos-show))
603 (save-excursion
604 (if (string= "" category)
605 (setq category (nth todos-category-number todos-categories)))
606 (let ((cat-exists (member category todos-categories)))
607 (setq todos-category-number
608 (if cat-exists
609 (- (length todos-categories) (length cat-exists))
610 (todos-add-category category))))
611 (todos-show)
612 (setq todos-previous-line 0)
613 (let ((top 1)
614 (bottom (1+ (count-lines (point-min) (point-max)))))
615 (while (> (- bottom top) todos-insert-threshold)
616 (let* ((current (/ (+ top bottom) 2))
617 (answer (if (< current bottom)
618 (todos-more-important-p current) nil)))
619 (if answer
620 (setq bottom current)
621 (setq top (1+ current)))))
622 (setq top (/ (+ top bottom) 2))
623 ;; goto-line doesn't have the desired behavior in a narrowed buffer.
624 (goto-char (point-min))
625 (forward-line (1- top)))
626 (insert new-item "\n")
627 (todos-backward-item)
628 (todos-save)
629 (message "")))
631 (defun todos-rename-category (new)
632 "Rename current Todos category."
633 (interactive "sCategory: ")
634 (let ((cat (nth todos-category-number todos-categories))
635 (vec (vconcat todos-categories))
636 prompt)
637 (while (and (cond ((string= "" new)
638 (setq prompt "Enter a non-empty category name: "))
639 ((string-match "\\`\\s-+\\'" new)
640 (setq prompt "Enter a category name that is not only white space: "))
641 ((member new todos-categories)
642 (setq prompt "Enter a non-existing category name: ")))
643 (setq new (read-from-minibuffer prompt))))
644 (aset vec todos-category-number new)
645 (setq todos-categories (append vec nil))
646 (save-excursion
647 (widen)
648 (search-backward (concat todos-prefix todos-category-beg))
649 (goto-char (match-end 0))
650 (when (looking-at (regexp-quote cat))
651 (replace-match new t))
652 (goto-char (point-min))
653 (setq mode-line-buffer-identification
654 (concat "Category: " new))))
655 ;; (concat "Category: " (format "%18s" new)))))
656 (todos-category-select))
658 (defun todos-delete-category ()
659 "Delete current Todos category provided it is empty."
660 (interactive)
661 (if (not (eq (point-max) (point-min)))
662 (message "This category is not empty, so it cannot be deleted")
663 (let ((cat (nth todos-category-number todos-categories)) beg end)
664 (when (y-or-n-p (concat "Permanently remove category '" cat "'? "))
665 (widen)
666 (setq beg (re-search-backward
667 (concat "^" (regexp-quote todos-prefix) todos-category-beg cat)
668 (point-min) nil)
669 end (1+ (re-search-forward
670 (concat "^" todos-category-end "\n"
671 (regexp-quote todos-prefix) " " todos-category-sep)
672 (point-max) nil)))
673 (kill-region beg end)
674 (setq todos-categories (delete cat todos-categories))
675 (todos-category-select)
676 (message "Deleted category \"%s\"" cat)))))
678 (defcustom todos-categories-buffer "*TODOS Categories*"
679 "Name of buffer displayed by `todos-display-categories'"
680 :type 'string
681 :group 'todos)
683 (defun todos-display-categories ()
684 "Display an alphabetical list of clickable Todos category names.
685 Click or type RET on a category name to go to it."
686 (interactive)
687 ;; (setq todos-window-configuration (current-window-configuration))
688 (let ((categories (copy-sequence todos-categories))
689 beg)
690 ;; alphabetize the list case insensitively
691 (setq categories (sort categories (lambda (s1 s2) (let ((cis1 (upcase s1))
692 (cis2 (upcase s2)))
693 (string< cis1 cis2)))))
694 (require 'widget)
695 (eval-when-compile
696 (require 'wid-edit))
697 (with-current-buffer (get-buffer-create todos-categories-buffer)
698 (switch-to-buffer (current-buffer))
699 (erase-buffer)
700 (kill-all-local-variables)
701 (widget-insert "Press a button to display the corresponding category.\n\n")
702 (setq beg (point))
703 (mapc (lambda (cat)
704 (widget-create 'push-button
705 :notify (lambda (widget &rest ignore)
706 (todos-jump-to-category-noninteractively
707 (widget-get widget :value)))
709 cat)
710 (widget-insert "\n"))
711 categories)
712 (use-local-map widget-keymap)
713 (widget-setup))))
715 (defun todos-jump-to-category-noninteractively (cat)
716 (let ((name todos-categories-buffer))
717 (if (string= (buffer-name) name)
718 (kill-buffer name)))
719 ;; (set-window-configuration todos-window-configuration)
720 (switch-to-buffer (file-name-nondirectory todos-file-do))
721 (widen)
722 (goto-char (point-min))
723 (setq todos-category-number (- (length todos-categories)
724 (length (member cat todos-categories))))
725 (todos-category-select))
727 ;;;###autoload
728 (defun todos-insert-item (arg)
729 "Insert new TODO list entry.
730 With a prefix argument solicit the category, otherwise use the current
731 category."
732 (interactive "P")
733 (save-excursion
734 (if (not (derived-mode-p 'todos-mode)) (todos-show))
735 (let* ((new-item (concat todos-prefix " "
736 (if todos-entry-prefix-function
737 (funcall todos-entry-prefix-function))
738 (read-from-minibuffer "New TODO entry: ")))
739 (current-category (nth todos-category-number todos-categories))
740 (category (if arg (todos-completing-read) current-category)))
741 (todos-add-item-non-interactively new-item category))))
743 (defun todos-insert-item-here ()
744 "Insert a new TODO list entry directly above the entry at point.
745 If point is on an empty line, insert the entry there."
746 (interactive)
747 (if (not (derived-mode-p 'todos-mode)) (todos-show))
748 (let ((new-item (concat todos-prefix " "
749 (if todos-entry-prefix-function
750 (funcall todos-entry-prefix-function))
751 (read-from-minibuffer "New TODO entry: "))))
752 (unless (and (bolp) (eolp)) (goto-char (todos-item-start)))
753 (insert (concat new-item "\n"))
754 (backward-char)
755 ;; put point at start of new entry
756 (goto-char (todos-item-start))))
758 (defun todos-more-important-p (line)
759 "Ask whether entry is more important than the one at LINE."
760 (unless (equal todos-previous-line line)
761 (setq todos-previous-line line)
762 (goto-char (point-min))
763 (forward-line (1- todos-previous-line))
764 (let ((item (todos-item-string-start)))
765 (setq todos-previous-answer
766 (y-or-n-p (concat "More important than '" item "'? ")))))
767 todos-previous-answer)
769 (defun todos-delete-item ()
770 "Delete current TODO list entry."
771 (interactive)
772 (if (> (count-lines (point-min) (point-max)) 0)
773 (let* ((todos-entry (todos-item-string-start))
774 (todos-answer (y-or-n-p (concat "Permanently remove '"
775 todos-entry "'? "))))
776 (when todos-answer
777 (todos-remove-item)
778 (todos-backward-item))
779 (message ""))
780 (error "No TODO list entry to delete")))
782 (defun todos-raise-item ()
783 "Raise priority of current entry."
784 (interactive)
785 (if (> (count-lines (point-min) (point)) 0)
786 (let ((item (todos-item-string)))
787 (todos-remove-item)
788 (todos-backward-item)
789 (save-excursion
790 (insert item "\n"))
791 (message ""))
792 (error "No TODO list entry to raise")))
794 (defun todos-lower-item ()
795 "Lower priority of current entry."
796 (interactive)
797 (if (> (count-lines (point) (point-max)) 1)
798 ;; Assume there is a final newline
799 (let ((item (todos-item-string)))
800 (todos-remove-item)
801 (todos-forward-item)
802 (save-excursion
803 (insert item "\n"))
804 (message ""))
805 (error "No TODO list entry to lower")))
807 (defun todos-move-item ()
808 "Move the current todo item to another, interactively named, category.
810 If the named category is not one of the current todo categories, then
811 it is created and the item becomes the first entry in that category."
812 (interactive)
813 (let ((item (todos-item-string))
814 (inhibit-quit t)
815 (category (todos-completing-read)))
816 (todos-remove-item)
817 (todos-add-item-non-interactively item category)))
819 (defun todos-file-item (&optional comment)
820 "File the current TODO list entry away, annotated with an optional COMMENT."
821 (interactive "sComment: ")
822 (or (> (count-lines (point-min) (point-max)) 0)
823 (error "No TODO list entry to file away"))
824 (let ((time-stamp-format todos-time-string-format))
825 (when (and comment (> (length comment) 0))
826 (goto-char (todos-item-end))
827 (insert
828 (if (save-excursion (beginning-of-line)
829 (looking-at (regexp-quote todos-prefix)))
831 "\n\t")
832 "(" comment ")"))
833 (goto-char (todos-item-end))
834 (insert " [" (nth todos-category-number todos-categories) "]")
835 (goto-char (todos-item-start))
836 (let ((temp-point (point)))
837 (if (looking-at (regexp-quote todos-prefix))
838 (replace-match (time-stamp-string))
839 ;; Standard prefix -> timestamp
840 ;; Else prefix non-standard item start with timestamp
841 (insert (time-stamp-string)))
842 (append-to-file temp-point (1+ (todos-item-end)) todos-file-done)
843 (delete-region temp-point (1+ (todos-item-end))))
844 (todos-backward-item)
845 (message "")))
847 ;; ---------------------------------------------------------------------------
849 ;; Utility functions:
852 ;;;###autoload
853 (defun todos-top-priorities (&optional nof-priorities category-pr-page)
854 "List top priorities for each category.
856 Number of entries for each category is given by NOF-PRIORITIES which
857 defaults to \'todos-show-priorities\'.
859 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
860 between each category."
862 (interactive "P")
863 (or nof-priorities (setq nof-priorities todos-show-priorities))
864 (if (listp nof-priorities) ;universal argument
865 (setq nof-priorities (car nof-priorities)))
866 (let ((todos-print-buffer-name todos-tmp-buffer-name)
867 ;;(todos-print-category-number 0)
868 (todos-category-break (if category-pr-page "\f" ""))
869 (cat-end
870 (concat
871 (if todos-remove-separator
872 (concat todos-category-end "\n"
873 (regexp-quote todos-prefix) " " todos-category-sep "\n")
874 (concat todos-category-end "\n"))))
875 beg end)
876 (todos-show)
877 (save-excursion
878 (save-restriction
879 (widen)
880 (copy-to-buffer todos-print-buffer-name (point-min) (point-max))
881 (set-buffer todos-print-buffer-name)
882 (goto-char (point-min))
883 ;; (when (re-search-forward (regexp-quote todos-header) nil t)
884 ;; (beginning-of-line 1)
885 ;; (delete-region (point) (line-end-position)))
886 (while (re-search-forward ;Find category start
887 (regexp-quote (concat todos-prefix todos-category-beg))
888 nil t)
889 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
890 (re-search-forward cat-end nil t)
891 (setq end (match-beginning 0))
892 (replace-match todos-category-break)
893 (narrow-to-region beg end) ;In case we have too few entries.
894 (goto-char (point-min))
895 (if (zerop nof-priorities) ;Traverse entries.
896 (goto-char end) ;All entries
897 (todos-forward-item nof-priorities))
898 (setq beg (point))
899 (delete-region beg end)
900 (widen))
901 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
902 (goto-char (point-min)) ;Due to display buffer
904 ;; Could have used switch-to-buffer as it has a norecord argument,
905 ;; which is nice when we are called from e.g. todos-print.
906 ;; Else we could have used pop-to-buffer.
907 (display-buffer todos-print-buffer-name)
908 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
909 todos-print-buffer-name)))
911 (defun todos-save-top-priorities (&optional nof-priorities)
912 "Save top priorities for each category in `todos-file-top'.
914 Number of entries for each category is given by NOF-PRIORITIES which
915 defaults to `todos-show-priorities'."
916 (interactive "P")
917 (save-window-excursion
918 (save-excursion
919 (save-restriction
920 (todos-top-priorities nof-priorities)
921 (set-buffer todos-tmp-buffer-name)
922 (write-file todos-file-top)
923 (kill-this-buffer)))))
925 ;;;###autoload
926 (defun todos-print (&optional category-pr-page)
927 "Print todo summary using `todos-print-function'.
928 If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
929 between each category.
931 Number of entries for each category is given by `todos-print-priorities'."
932 (interactive "P")
933 (save-window-excursion
934 (save-excursion
935 (save-restriction
936 (todos-top-priorities todos-print-priorities
937 category-pr-page)
938 (set-buffer todos-tmp-buffer-name)
939 (and (funcall todos-print-function)
940 (kill-this-buffer))
941 (message "Todo printing done.")))))
943 (defun todos-list-categories ()
944 "Return a list of the Todo mode categories."
945 (let ((todos-buf (find-file-noselect todos-file-do))
946 categories)
947 (with-current-buffer todos-buf
948 (save-excursion
949 (save-restriction
950 (widen)
951 (goto-char (point-max))
952 (while (re-search-backward
953 (concat "^" (regexp-quote (concat todos-prefix todos-category-beg))
954 "\\(.*\\)\n")
955 (point-min) t)
956 (push (match-string-no-properties 1) categories)))))
957 categories))
959 (defun todos-jump-to-category ()
960 "Jump to a category. Default is previous category."
961 (interactive)
962 (let ((category (todos-completing-read)))
963 (if (string= "" category)
964 (setq category (nth todos-category-number todos-categories)))
965 (setq todos-category-number
966 (if (member category todos-categories)
967 (- (length todos-categories)
968 (length (member category todos-categories)))
969 (todos-add-category category)))
970 (todos-show)))
972 (defun todos-line-string ()
973 "Return current line in buffer as a string."
974 (buffer-substring (line-beginning-position) (line-end-position)))
976 (defun todos-item-string-start ()
977 "Return the start of this TODO list entry as a string."
978 ;; Suitable for putting in the minibuffer when asking the user
979 (let ((item (todos-item-string)))
980 (if (> (length item) 60)
981 (setq item (concat (substring item 0 56) "...")))
982 item))
984 (defun todos-item-start ()
985 "Return point at start of current TODO list item."
986 (save-excursion
987 (beginning-of-line)
988 (if (not (looking-at (regexp-quote todos-prefix)))
989 (search-backward-regexp
990 (concat "^" (regexp-quote todos-prefix)) nil t))
991 (point)))
993 (defun todos-item-end ()
994 "Return point at end of current TODO list item."
995 (save-excursion
996 (end-of-line)
997 (search-forward-regexp
998 (concat "^" (regexp-quote todos-prefix)) nil 'goto-end)
999 (1- (line-beginning-position))))
1001 (defun todos-remove-item ()
1002 "Delete the current entry from the TODO list."
1003 (delete-region (todos-item-start) (1+ (todos-item-end))))
1005 (defun todos-item-string ()
1006 "Return current TODO list entry as a string."
1007 (buffer-substring (todos-item-start) (todos-item-end)))
1009 (defun todos-string-count-lines (string)
1010 "Return the number of lines STRING spans."
1011 (length (split-string string "\n")))
1013 (defun todos-string-multiline-p (string)
1014 "Return non-nil if STRING spans several lines."
1015 (> (todos-string-count-lines string) 1))
1017 (defun todos-completing-read ()
1018 "Return a category name, with completion, for use in Todo mode."
1019 ;; allow SPC to insert spaces, for adding new category names with
1020 ;; todos-move-item
1021 (let ((map minibuffer-local-completion-map))
1022 (define-key map " " nil)
1023 ;; make a copy of todos-categories in case history-delete-duplicates is
1024 ;; non-nil, which makes completing-read alter todos-categories
1025 (let* ((categories (copy-sequence todos-categories))
1026 (history (cons 'todos-categories (1+ todos-category-number)))
1027 (default (nth todos-category-number todos-categories))
1028 (completion-ignore-case todos-completion-ignore-case)
1029 (category (completing-read
1030 (concat "Category [" default "]: ")
1031 todos-categories nil nil nil history default)))
1032 ;; restore the original value of todos-categories
1033 (setq todos-categories categories)
1034 category)))
1036 ;; ---------------------------------------------------------------------------
1038 (easy-menu-define todos-menu todos-mode-map "Todo Menu"
1039 '("Todo"
1040 ["Next category" todos-forward-category t]
1041 ["Previous category" todos-backward-category t]
1042 ["Jump to category" todos-jump-to-category t]
1043 ["Show top priority items" todos-top-priorities t]
1044 ["Print categories" todos-print t]
1045 "---"
1046 ["Edit item" todos-edit-item t]
1047 ["File item" todos-file-item t]
1048 ["Insert new item" todos-insert-item t]
1049 ["Insert item here" todos-insert-item-here t]
1050 ["Kill item" todos-delete-item t]
1051 "---"
1052 ["Lower item priority" todos-lower-item t]
1053 ["Raise item priority" todos-raise-item t]
1054 "---"
1055 ["Next item" todos-forward-item t]
1056 ["Previous item" todos-backward-item t]
1057 "---"
1058 ["Save" todos-save t]
1059 ["Save Top Priorities" todos-save-top-priorities t]
1060 "---"
1061 ["Quit" todos-quit t]
1064 ;; As calendar reads .todos-do before todos-mode is loaded.
1065 ;;;###autoload
1066 (defun todos-mode ()
1067 "Major mode for editing TODO lists.
1069 \\{todos-mode-map}"
1070 (interactive)
1071 (kill-all-local-variables)
1072 (setq major-mode 'todos-mode)
1073 (setq mode-name "TODOS")
1074 (use-local-map todos-mode-map)
1075 (easy-menu-add todos-menu)
1076 (make-local-variable 'font-lock-defaults)
1077 (setq font-lock-defaults '(todos-font-lock-keywords t))
1078 (make-local-variable 'word-wrap)
1079 (setq word-wrap t)
1080 (make-local-variable 'wrap-prefix)
1081 (setq wrap-prefix
1082 (make-string (length (concat todos-prefix " "
1083 (todos-entry-timestamp-initials))) 32))
1084 (unless (member '(continuation) fringe-indicator-alist)
1085 (push '(continuation) fringe-indicator-alist))
1086 (run-mode-hooks 'todos-mode-hook))
1088 (defvar date)
1089 (defvar entry)
1091 ;; t-c should be used from diary code, which requires calendar.
1092 (declare-function calendar-current-date "calendar" nil)
1094 ;; Read about this function in the setup instructions above!
1095 ;;;###autoload
1096 (defun todos-cp ()
1097 "Make a diary entry appear only in the current date's diary."
1098 (if (equal (calendar-current-date) date)
1099 entry))
1101 (define-derived-mode todos-edit-mode text-mode "TODO Edit"
1102 "Major mode for editing items in the TODO list.
1104 \\{todos-edit-mode-map}")
1106 ;;;###autoload
1107 (defun todos-show ()
1108 "Show TODO list."
1109 (interactive)
1110 ;; Call todos-initial-setup only if there is neither a Todo file nor
1111 ;; a corresponding unsaved buffer.
1112 (if (or (file-exists-p todos-file-do)
1113 (let* ((buf (get-buffer (file-name-nondirectory todos-file-do)))
1114 (bufname (buffer-file-name buf)))
1115 (equal (expand-file-name todos-file-do) bufname)))
1116 (find-file todos-file-do)
1117 (todos-initial-setup))
1118 (unless (eq major-mode 'todos-mode) (todos-mode))
1119 (unless todos-categories
1120 (setq todos-categories (todos-list-categories)))
1121 ;; (beginning-of-line)
1122 (todos-category-select))
1124 (defun todos-initial-setup ()
1125 "Set up things to work properly in TODO mode."
1126 (find-file todos-file-do)
1127 (erase-buffer)
1128 (todos-mode)
1129 (todos-add-category "Todos"))
1131 (provide 'todos)
1133 ;; arch-tag: 6fd91be5-776e-4464-a109-da4ea0e4e497
1134 ;;; todos.el ends here