aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Berman2009-02-12 22:00:00 +0100
committerStephen Berman2009-02-12 22:00:00 +0100
commit3f0317671049e0aefe3b10a99b7ad60b8d8eb8bd (patch)
tree9ecbdb77b4211f037b54ac21239cf7bf20a69e2a
parent9011078f9df40965a6ef3cade5bba3e5d0eb730d (diff)
downloademacs-3f0317671049e0aefe3b10a99b7ad60b8d8eb8bd.tar.gz
emacs-3f0317671049e0aefe3b10a99b7ad60b8d8eb8bd.zip
Reconstructed and backdated initial commit of todos.el.
* calendar/todos.el: New file. This is a copy of todo-mode.el from 2009-01-30T13:06:07Z!lekktu@gmail.com except for the following changes: replace all occurrences of the namespace prefix "todo-" with "todos-", delete the defvar todo-cats (the old name of todo-categories) and its use in todos-add-category, delete all defaliases of old command names.
-rw-r--r--lisp/ChangeLog8
-rw-r--r--lisp/calendar/todos.el962
2 files changed, 970 insertions, 0 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index cdc021d192f..96d79e8c15f 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,11 @@
12012-09-11 Stephen Berman <stephen.berman@gmx.net>
2
3 * calendar/todos.el: New file. This is a copy of todo-mode.el
4 from revision 94343 except for the following changes: replace all
5 occurrences of the namespace prefix "todo-" with "todos-", delete
6 the defvar todo-cats (the old name of todo-categories) and its use
7 in todos-add-category, delete all defaliases of old command names.
8
12012-09-11 Bastien Guerry <bzg@gnu.org> 92012-09-11 Bastien Guerry <bzg@gnu.org>
2 10
3 * subr.el (set-temporary-overlay-map): Add a docstring. 11 * subr.el (set-temporary-overlay-map): Add a docstring.
diff --git a/lisp/calendar/todos.el b/lisp/calendar/todos.el
new file mode 100644
index 00000000000..883f2081b3e
--- /dev/null
+++ b/lisp/calendar/todos.el
@@ -0,0 +1,962 @@
1;;; todos.el --- major mode for editing TODO list files
2
3;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4;; 2008, 2009 Free Software Foundation, Inc.
5
6;; Author: Oliver Seidel <privat@os10000.net>
7;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
8;; Created: 2 Aug 1997
9;; Keywords: calendar, todo
10
11;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26;; ---------------------------------------------------------------------------
27
28;;; Commentary:
29
30;; Mode Description
31;;
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.
39;;
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.
48;;
49;; Preface, Quickstart Installation
50;;
51;; To get this to work, make emacs execute the line
52;;
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)
59;;
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".
62;;
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.
69;;
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:
73;;
74;; (global-set-key "\C-ct" 'todos-show) ; switch to TODO buffer
75;; (global-set-key "\C-ci" 'todos-insert-item) ; insert new item
76;;
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).
88;;
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.
93;;
94;; Pre-Requisites
95;;
96;; This package will require the following packages to be
97;; available on the load-path:
98;;
99;; time-stamp
100;; easymenu
101;;
102;; Operation
103;;
104;; You will have the following facilities available:
105;;
106;; M-x todos-show will enter the todo list screen, here type
107;;
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
129;;
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.
138;;
139;; Configuration
140;;
141;; Variable todos-prefix
142;;
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.
146;;
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.
151;;
152;; If you have the diary package set up to usually display more
153;; than one day's entries at once, consider using
154;;
155;; "&%%(equal (calendar-current-date) date)"
156;;
157;; as the value of `todos-prefix'. Please note that this may slow
158;; down the processing of your diary file some.
159;;
160;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
161;;
162;; "&%%(todos-cp)"
163;;
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.
166;;
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.
170;;
171;; Variable todos-file-do
172;;
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.
176;;
177;; Variable todos-file-done
178;;
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.
184;;
185;; Variable todos-file-top
186;;
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.
190;;
191;; Variable todos-mode-hook
192;;
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.
198;;
199;; Variable todos-insert-threshold
200;;
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!
217;;
218;; Things to do
219;;
220;; These originally were my ideas, but now also include all the
221;; suggestions that I included before forgetting them:
222;;
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
244;;
245;; History and Gossip
246;;
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!
250;;
251;; Enjoy this package and express your gratitude by sending nice
252;; things to my parents' address!
253;;
254;; Oliver Seidel
255;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
256
257;;; Code:
258
259(require 'time-stamp)
260
261
262;; User-configurable variables:
263
264(defgroup todos nil
265 "Maintain a list of todo items."
266 :link '(emacs-commentary-link "todos")
267 :version "21.1"
268 :group 'calendar)
269
270(defcustom todos-prefix "*/*"
271 "TODO mode prefix for entries.
272
273This is useful in conjunction with `calendar' and `diary' if you use
274
275#include \"~/.todos-do\"
276
277in your diary file to include your todo list file as part of your
278diary. With the default value \"*/*\" the diary displays each entry
279every day and it may also be marked on every day of the calendar.
280Using \"&%%(equal (calendar-current-date) date)\" instead will only
281show and mark todo entries for today, but may slow down processing of
282the diary file somewhat."
283 :type 'string
284 :group 'todos)
285(defcustom todos-file-do (convert-standard-filename "~/.todos-do")
286 "TODO mode list file."
287 :type 'file
288 :group 'todos)
289(defcustom todos-file-done (convert-standard-filename "~/.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.
303
304If you have 8 items in your TODO list, then you may get asked 4
305questions by the binary insertion algorithm. However, you may not
306really have a need for such accurate priorities amongst your TODO
307items. If you now think about the binary insertion halving the size
308of the window each time, then the threshold is the window size at
309which it will stop. If you set the threshold to zero, the upper and
310lower bound will coincide at the end of the loop and you will insert
311your item just before that point. If you set the threshold to,
312e.g. 8, it will stop as soon as the window size drops below that
313amount and will insert the item in the approximate center of that
314window."
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.
321
322Not in TODO format, but diary compatible.
323Automatically generated when `todos-save-top-priorities' is non-nil."
324 :type 'string
325 :group 'todos)
326
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].
3330 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].
3380 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
351;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
352;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
353;;
354(defcustom todos-time-string-format
355 "%:y-%02m-%02d %02H:%02M"
356 "TODO mode time string format for done entries.
357For details see the variable `time-stamp-format'."
358 :type 'string
359 :group 'todos)
360
361(defcustom todos-entry-prefix-function 'todos-entry-timestamp-initials
362 "Function producing text to insert at start of todo entry."
363 :type 'symbol
364 :group 'todos)
365(defcustom todos-initials (or (getenv "INITIALS") (user-login-name))
366 "Initials of todo item author."
367 :type 'string
368 :group 'todos)
369
370(defun todos-entry-timestamp-initials ()
371 "Prepend timestamp and your initials to the head of a TODO entry."
372 (let ((time-stamp-format todos-time-string-format))
373 (concat (time-stamp-string) " " todos-initials ": ")))
374
375;; ---------------------------------------------------------------------------
376
377;; Set up some helpful context ...
378
379(defvar todos-categories nil
380 "TODO categories.")
381
382(defvar todos-previous-line 0
383 "Previous line asked about.")
384
385(defvar todos-previous-answer 0
386 "Previous answer got.")
387
388(defvar todos-mode-map
389 (let ((map (make-keymap)))
390 (suppress-keymap map t)
391 (define-key map "+" 'todos-forward-category)
392 (define-key map "-" 'todos-backward-category)
393 (define-key map "d" 'todos-file-item) ;done/delete
394 (define-key map "e" 'todos-edit-item)
395 (define-key map "E" 'todos-edit-multiline)
396 (define-key map "f" 'todos-file-item)
397 (define-key map "i" 'todos-insert-item)
398 (define-key map "I" 'todos-insert-item-here)
399 (define-key map "j" 'todos-jump-to-category)
400 (define-key map "k" 'todos-delete-item)
401 (define-key map "l" 'todos-lower-item)
402 (define-key map "n" 'todos-forward-item)
403 (define-key map "p" 'todos-backward-item)
404 (define-key map "P" 'todos-print)
405 (define-key map "q" 'todos-quit)
406 (define-key map "r" 'todos-raise-item)
407 (define-key map "s" 'todos-save)
408 (define-key map "S" 'todos-save-top-priorities)
409 (define-key map "t" 'todos-top-priorities)
410 map)
411 "TODO mode keymap.")
412
413(defvar todos-category-number 0 "TODO category number.")
414
415(defvar todos-tmp-buffer-name " *todo tmp*")
416
417(defvar todos-category-sep (make-string 75 ?-)
418 "Category separator.")
419
420(defvar todos-category-beg " --- "
421 "Category start separator to be prepended onto category name.")
422
423(defvar todos-category-end "--- End"
424 "Separator after a category.")
425
426(defvar todos-header "-*- mode: todo; "
427 "Header of todo files.")
428
429;; ---------------------------------------------------------------------------
430
431(defun todos-category-select ()
432 "Make TODO mode display the current category correctly."
433 (let ((name (nth todos-category-number todos-categories)))
434 (setq mode-line-buffer-identification
435;; (concat "Category: " name))
436 (concat "Category: " (format "%18s" name)))
437 (widen)
438 (goto-char (point-min))
439 (search-forward-regexp
440 (concat "^"
441 (regexp-quote (concat todos-prefix todos-category-beg name))
442 "$"))
443 (let ((begin (1+ (line-end-position))))
444 (search-forward-regexp (concat "^" todos-category-end))
445 (narrow-to-region begin (line-beginning-position))
446 (goto-char (point-min)))))
447
448(defun todos-forward-category ()
449 "Go forward to TODO list of next category."
450 (interactive)
451 (setq todos-category-number
452 (mod (1+ todos-category-number) (length todos-categories)))
453 (todos-category-select))
454
455(defun todos-backward-category ()
456 "Go back to TODO list of previous category."
457 (interactive)
458 (setq todos-category-number
459 (mod (1- todos-category-number) (length todos-categories)))
460 (todos-category-select))
461
462(defun todos-backward-item ()
463 "Select previous entry of TODO list."
464 (interactive)
465 (search-backward-regexp (concat "^" (regexp-quote todos-prefix)) nil t)
466 (message ""))
467
468(defun todos-forward-item (&optional count)
469 "Select COUNT-th next entry of TODO list."
470 (interactive "P")
471 (if (listp count) (setq count (car count)))
472 (end-of-line)
473 (search-forward-regexp (concat "^" (regexp-quote todos-prefix))
474 nil 'goto-end count)
475 (beginning-of-line)
476 (message ""))
477
478(defun todos-save ()
479 "Save the TODO list."
480 (interactive)
481 (save-excursion
482 (save-restriction
483 (save-buffer)))
484 (if todos-save-top-priorities-too (todos-save-top-priorities)))
485
486(defun todos-quit ()
487 "Done with TODO list for now."
488 (interactive)
489 (widen)
490 (todos-save)
491 (message "")
492 (bury-buffer))
493
494(defun todos-edit-item ()
495 "Edit current TODO list entry."
496 (interactive)
497 (let ((item (todos-item-string)))
498 (if (todos-string-multiline-p item)
499 (todos-edit-multiline)
500 (let ((new (read-from-minibuffer "Edit: " item)))
501 (todos-remove-item)
502 (insert new "\n")
503 (todos-backward-item)
504 (message "")))))
505
506(defun todos-edit-multiline ()
507 "Set up a buffer for editing a multiline TODO list entry."
508 (interactive)
509 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
510 (switch-to-buffer
511 (make-indirect-buffer
512 (file-name-nondirectory todos-file-do) buffer-name))
513 (message "To exit, simply kill this buffer and return to list.")
514 (todos-edit-mode)
515 (narrow-to-region (todos-item-start) (todos-item-end))))
516
517;;;###autoload
518(defun todos-add-category (&optional cat)
519 "Add new category CAT to the TODO list."
520 (interactive)
521 (let ((buf (find-file-noselect todos-file-do t))
522 (prompt "Category: "))
523 (unless (zerop (buffer-size buf))
524 (and (null todos-categories)
525 (error "Error in %s: File is non-empty but contains no category"
526 todos-file-do)))
527 (unless cat (setq cat (read-from-minibuffer prompt)))
528 (with-current-buffer buf
529 ;; reject names that could induce bugs and confusion
530 (while (and (cond ((string= "" cat)
531 (setq prompt "Enter a non-empty category name: "))
532 ((string-match "\\`\\s-+\\'" cat)
533 (setq prompt "Enter a category name that is not only white space: "))
534 ((member cat todos-categories)
535 (setq prompt "Enter a non-existing category name: ")))
536 (setq cat (read-from-minibuffer prompt))))
537 ;; initialize a newly created Todo buffer for Todo mode
538 (unless (file-exists-p todos-file-do) (todos-mode))
539 (setq todos-categories (cons cat todos-categories))
540 (widen)
541 (goto-char (point-min))
542 (if (search-forward "-*- mode: todo; " 17 t)
543 (kill-line)
544 (insert "-*- mode: todo; \n")
545 (forward-char -1))
546 (insert (format "todos-categories: %S; -*-" todos-categories))
547 (forward-char 1)
548 (insert (format "%s%s%s\n%s\n%s %s\n"
549 todos-prefix todos-category-beg cat
550 todos-category-end
551 todos-prefix todos-category-sep))
552 (if (interactive-p)
553 ;; properly display the newly added category
554 (progn (setq todos-category-number 0) (todos-show))
555 0))))
556
557;;;###autoload
558(defun todos-add-item-non-interactively (new-item category)
559 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
560 (save-excursion
561 (todos-show))
562 (save-excursion
563 (if (string= "" category)
564 (setq category (nth todos-category-number todos-categories)))
565 (let ((cat-exists (member category todos-categories)))
566 (setq todos-category-number
567 (if cat-exists
568 (- (length todos-categories) (length cat-exists))
569 (todos-add-category category))))
570 (todos-show)
571 (setq todos-previous-line 0)
572 (let ((top 1)
573 (bottom (1+ (count-lines (point-min) (point-max)))))
574 (while (> (- bottom top) todos-insert-threshold)
575 (let* ((current (/ (+ top bottom) 2))
576 (answer (if (< current bottom)
577 (todos-more-important-p current) nil)))
578 (if answer
579 (setq bottom current)
580 (setq top (1+ current)))))
581 (setq top (/ (+ top bottom) 2))
582 ;; goto-line doesn't have the desired behavior in a narrowed buffer.
583 (goto-char (point-min))
584 (forward-line (1- top)))
585 (insert new-item "\n")
586 (todos-backward-item)
587 (todos-save)
588 (message "")))
589
590;;;###autoload
591(defun todos-insert-item (arg)
592 "Insert new TODO list entry.
593With a prefix argument solicit the category, otherwise use the current
594category."
595 (interactive "P")
596 (save-excursion
597 (if (not (derived-mode-p 'todos-mode)) (todos-show))
598 (let* ((new-item (concat todos-prefix " "
599 (read-from-minibuffer
600 "New TODO entry: "
601 (if todos-entry-prefix-function
602 (funcall todos-entry-prefix-function)))))
603 (current-category (nth todos-category-number todos-categories))
604 (category (if arg (todos-completing-read) current-category)))
605 (todos-add-item-non-interactively new-item category))))
606
607(defun todos-insert-item-here ()
608 "Insert a new TODO list entry directly above the entry at point.
609If point is on an empty line, insert the entry there."
610 (interactive)
611 (if (not (derived-mode-p 'todos-mode)) (todos-show))
612 (let ((new-item (concat todos-prefix " "
613 (read-from-minibuffer
614 "New TODO entry: "
615 (if todos-entry-prefix-function
616 (funcall todos-entry-prefix-function))))))
617 (unless (and (bolp) (eolp)) (goto-char (todos-item-start)))
618 (insert (concat new-item "\n"))
619 (backward-char)
620 ;; put point at start of new entry
621 (goto-char (todos-item-start))))
622
623(defun todos-more-important-p (line)
624 "Ask whether entry is more important than the one at LINE."
625 (unless (equal todos-previous-line line)
626 (setq todos-previous-line line)
627 (goto-char (point-min))
628 (forward-line (1- todos-previous-line))
629 (let ((item (todos-item-string-start)))
630 (setq todos-previous-answer
631 (y-or-n-p (concat "More important than '" item "'? ")))))
632 todos-previous-answer)
633
634(defun todos-delete-item ()
635 "Delete current TODO list entry."
636 (interactive)
637 (if (> (count-lines (point-min) (point-max)) 0)
638 (let* ((todos-entry (todos-item-string-start))
639 (todos-answer (y-or-n-p (concat "Permanently remove '"
640 todos-entry "'? "))))
641 (when todos-answer
642 (todos-remove-item)
643 (todos-backward-item))
644 (message ""))
645 (error "No TODO list entry to delete")))
646
647(defun todos-raise-item ()
648 "Raise priority of current entry."
649 (interactive)
650 (if (> (count-lines (point-min) (point)) 0)
651 (let ((item (todos-item-string)))
652 (todos-remove-item)
653 (todos-backward-item)
654 (save-excursion
655 (insert item "\n"))
656 (message ""))
657 (error "No TODO list entry to raise")))
658
659(defun todos-lower-item ()
660 "Lower priority of current entry."
661 (interactive)
662 (if (> (count-lines (point) (point-max)) 1)
663 ;; Assume there is a final newline
664 (let ((item (todos-item-string)))
665 (todos-remove-item)
666 (todos-forward-item)
667 (save-excursion
668 (insert item "\n"))
669 (message ""))
670 (error "No TODO list entry to lower")))
671
672(defun todos-file-item (&optional comment)
673 "File the current TODO list entry away, annotated with an optional COMMENT."
674 (interactive "sComment: ")
675 (or (> (count-lines (point-min) (point-max)) 0)
676 (error "No TODO list entry to file away"))
677 (let ((time-stamp-format todos-time-string-format))
678 (when (and comment (> (length comment) 0))
679 (goto-char (todos-item-end))
680 (insert
681 (if (save-excursion (beginning-of-line)
682 (looking-at (regexp-quote todos-prefix)))
683 " "
684 "\n\t")
685 "(" comment ")"))
686 (goto-char (todos-item-end))
687 (insert " [" (nth todos-category-number todos-categories) "]")
688 (goto-char (todos-item-start))
689 (let ((temp-point (point)))
690 (if (looking-at (regexp-quote todos-prefix))
691 (replace-match (time-stamp-string))
692 ;; Standard prefix -> timestamp
693 ;; Else prefix non-standard item start with timestamp
694 (insert (time-stamp-string)))
695 (append-to-file temp-point (1+ (todos-item-end)) todos-file-done)
696 (delete-region temp-point (1+ (todos-item-end))))
697 (todos-backward-item)
698 (message "")))
699
700;; ---------------------------------------------------------------------------
701
702;; Utility functions:
703
704
705;;;###autoload
706(defun todos-top-priorities (&optional nof-priorities category-pr-page)
707 "List top priorities for each category.
708
709Number of entries for each category is given by NOF-PRIORITIES which
710defaults to \'todos-show-priorities\'.
711
712If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
713between each category."
714
715 (interactive "P")
716 (or nof-priorities (setq nof-priorities todos-show-priorities))
717 (if (listp nof-priorities) ;universal argument
718 (setq nof-priorities (car nof-priorities)))
719 (let ((todos-print-buffer-name todos-tmp-buffer-name)
720 ;;(todos-print-category-number 0)
721 (todos-category-break (if category-pr-page " " ""))
722 (cat-end
723 (concat
724 (if todos-remove-separator
725 (concat todos-category-end "\n"
726 (regexp-quote todos-prefix) " " todos-category-sep "\n")
727 (concat todos-category-end "\n"))))
728 beg end)
729 (todos-show)
730 (save-excursion
731 (save-restriction
732 (widen)
733 (copy-to-buffer todos-print-buffer-name (point-min) (point-max))
734 (set-buffer todos-print-buffer-name)
735 (goto-char (point-min))
736 (when (re-search-forward (regexp-quote todos-header) nil t)
737 (beginning-of-line 1)
738 (delete-region (point) (line-end-position)))
739 (while (re-search-forward ;Find category start
740 (regexp-quote (concat todos-prefix todos-category-beg))
741 nil t)
742 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
743 (re-search-forward cat-end nil t)
744 (setq end (match-beginning 0))
745 (replace-match todos-category-break)
746 (narrow-to-region beg end) ;In case we have too few entries.
747 (goto-char (point-min))
748 (if (zerop nof-priorities) ;Traverse entries.
749 (goto-char end) ;All entries
750 (todos-forward-item nof-priorities))
751 (setq beg (point))
752 (delete-region beg end)
753 (widen))
754 (and (looking-at " ") (replace-match "")) ;Remove trailing form-feed.
755 (goto-char (point-min)) ;Due to display buffer
756 ))
757 ;; Could have used switch-to-buffer as it has a norecord argument,
758 ;; which is nice when we are called from e.g. todos-print.
759 ;; Else we could have used pop-to-buffer.
760 (display-buffer todos-print-buffer-name)
761 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
762 todos-print-buffer-name)))
763
764(defun todos-save-top-priorities (&optional nof-priorities)
765 "Save top priorities for each category in `todos-file-top'.
766
767Number of entries for each category is given by NOF-PRIORITIES which
768defaults to `todos-show-priorities'."
769 (interactive "P")
770 (save-window-excursion
771 (save-excursion
772 (save-restriction
773 (todos-top-priorities nof-priorities)
774 (set-buffer todos-tmp-buffer-name)
775 (write-file todos-file-top)
776 (kill-this-buffer)))))
777
778;;;###autoload
779(defun todos-print (&optional category-pr-page)
780 "Print todo summary using `todos-print-function'.
781If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
782between each category.
783
784Number of entries for each category is given by `todos-print-priorities'."
785 (interactive "P")
786 (save-window-excursion
787 (save-excursion
788 (save-restriction
789 (todos-top-priorities todos-print-priorities
790 category-pr-page)
791 (set-buffer todos-tmp-buffer-name)
792 (and (funcall todos-print-function)
793 (kill-this-buffer))
794 (message "Todo printing done.")))))
795
796(defun todos-jump-to-category ()
797 "Jump to a category. Default is previous category."
798 (interactive)
799 (let ((category (todos-completing-read)))
800 (if (string= "" category)
801 (setq category (nth todos-category-number todos-categories)))
802 (setq todos-category-number
803 (if (member category todos-categories)
804 (- (length todos-categories)
805 (length (member category todos-categories)))
806 (todos-add-category category)))
807 (todos-show)))
808
809(defun todos-line-string ()
810 "Return current line in buffer as a string."
811 (buffer-substring (line-beginning-position) (line-end-position)))
812
813(defun todos-item-string-start ()
814 "Return the start of this TODO list entry as a string."
815 ;; Suitable for putting in the minibuffer when asking the user
816 (let ((item (todos-item-string)))
817 (if (> (length item) 60)
818 (setq item (concat (substring item 0 56) "...")))
819 item))
820
821(defun todos-item-start ()
822 "Return point at start of current TODO list item."
823 (save-excursion
824 (beginning-of-line)
825 (if (not (looking-at (regexp-quote todos-prefix)))
826 (search-backward-regexp
827 (concat "^" (regexp-quote todos-prefix)) nil t))
828 (point)))
829
830(defun todos-item-end ()
831 "Return point at end of current TODO list item."
832 (save-excursion
833 (end-of-line)
834 (search-forward-regexp
835 (concat "^" (regexp-quote todos-prefix)) nil 'goto-end)
836 (1- (line-beginning-position))))
837
838(defun todos-remove-item ()
839 "Delete the current entry from the TODO list."
840 (delete-region (todos-item-start) (1+ (todos-item-end))))
841
842(defun todos-item-string ()
843 "Return current TODO list entry as a string."
844 (buffer-substring (todos-item-start) (todos-item-end)))
845
846(defun todos-string-count-lines (string)
847 "Return the number of lines STRING spans."
848 (length (split-string string "\n")))
849
850(defun todos-string-multiline-p (string)
851 "Return non-nil if STRING spans several lines."
852 (> (todos-string-count-lines string) 1))
853
854(defun todos-completing-read ()
855 "Return a category name, with completion, for use in Todo mode."
856 ;; make a copy of todos-categories in case history-delete-duplicates is
857 ;; non-nil, which makes completing-read alter todos-categories
858 (let* ((categories (copy-sequence todos-categories))
859 (history (cons 'todos-categories (1+ todos-category-number)))
860 (default (nth todos-category-number todos-categories))
861 (category (completing-read
862 (concat "Category [" default "]: ")
863 todos-categories nil nil nil history default)))
864 ;; restore the original value of todos-categories
865 (setq todos-categories categories)
866 category))
867
868;; ---------------------------------------------------------------------------
869
870(easy-menu-define todos-menu todos-mode-map "Todo Menu"
871 '("Todo"
872 ["Next category" todos-forward-category t]
873 ["Previous category" todos-backward-category t]
874 ["Jump to category" todos-jump-to-category t]
875 ["Show top priority items" todos-top-priorities t]
876 ["Print categories" todos-print t]
877 "---"
878 ["Edit item" todos-edit-item t]
879 ["File item" todos-file-item t]
880 ["Insert new item" todos-insert-item t]
881 ["Insert item here" todos-insert-item-here t]
882 ["Kill item" todos-delete-item t]
883 "---"
884 ["Lower item priority" todos-lower-item t]
885 ["Raise item priority" todos-raise-item t]
886 "---"
887 ["Next item" todos-forward-item t]
888 ["Previous item" todos-backward-item t]
889 "---"
890 ["Save" todos-save t]
891 ["Save Top Priorities" todos-save-top-priorities t]
892 "---"
893 ["Quit" todos-quit t]
894 ))
895
896;; As calendar reads .todos-do before todos-mode is loaded.
897;;;###autoload
898(defun todos-mode ()
899 "Major mode for editing TODO lists.
900
901\\{todos-mode-map}"
902 (interactive)
903 (kill-all-local-variables)
904 (setq major-mode 'todos-mode)
905 (setq mode-name "TODO")
906 (use-local-map todos-mode-map)
907 (easy-menu-add todos-menu)
908 (run-mode-hooks 'todos-mode-hook))
909
910(defvar date)
911(defvar entry)
912
913;; t-c should be used from diary code, which requires calendar.
914(declare-function calendar-current-date "calendar" nil)
915
916;; Read about this function in the setup instructions above!
917;;;###autoload
918(defun todos-cp ()
919 "Make a diary entry appear only in the current date's diary."
920 (if (equal (calendar-current-date) date)
921 entry))
922
923(define-derived-mode todos-edit-mode text-mode "TODO Edit"
924 "Major mode for editing items in the TODO list.
925
926\\{todos-edit-mode-map}")
927
928;;;###autoload
929(defun todos-show ()
930 "Show TODO list."
931 (interactive)
932 ;; Call todos-initial-setup only if there is neither a Todo file nor
933 ;; a corresponding unsaved buffer.
934 (if (or (file-exists-p todos-file-do)
935 (let* ((buf (get-buffer (file-name-nondirectory todos-file-do)))
936 (bufname (buffer-file-name buf)))
937 (equal (expand-file-name todos-file-do) bufname)))
938 (find-file todos-file-do)
939 (todos-initial-setup))
940 (if (null todos-categories)
941 (if (null todos-cats)
942 (error "Error in %s: No categories in list `todos-categories'"
943 todos-file-do)
944 (goto-char (point-min))
945 (and (search-forward "todos-cats:" nil t)
946 (replace-match "todos-categories:"))
947 (make-local-variable 'todos-categories)
948 (setq todos-categories todos-cats)))
949 (beginning-of-line)
950 (todos-category-select))
951
952(defun todos-initial-setup ()
953 "Set up things to work properly in TODO mode."
954 (find-file todos-file-do)
955 (erase-buffer)
956 (todos-mode)
957 (todos-add-category "Todos"))
958
959(provide 'todos)
960
961;; arch-tag: 6fd91be5-776e-4464-a109-da4ea0e4e497
962;;; todos.el ends here