* calendar/todos.el: Comment out time-stamp require; require
[bpt/emacs.git] / lisp / calendar / todos.el
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 ;; FIXME: eliminate variable todos-prefix, use overlays:
172 ;; (defun todos-prefix ()
173 ;; "Display a Todo prefix string as an overlay."
174 ;; (let (ov)
175 ;; (setq ov (make-overlay (line-beginning-position) (line-end-position)))
176 ;; (overlay-put ov 'before-string
177 ;; (propertize todos-prefix 'face 'todos-prefix-string))))
178 ;;
179 ;; Variable todos-file-do
180 ;;
181 ;; This variable is fairly self-explanatory. You have to store
182 ;; your TODO list somewhere. This variable tells the package
183 ;; where to go and find this file.
184 ;;
185 ;; Variable todos-file-done
186 ;;
187 ;; Even when you're done, you may wish to retain the entries.
188 ;; Given that they're timestamped and you are offered to add a
189 ;; comment, this can make a useful diary of past events. It will
190 ;; even blend in with the EMACS diary package. So anyway, this
191 ;; variable holds the name of the file for the filed todos-items.
192 ;;
193 ;; Variable todos-file-top
194 ;;
195 ;; File storing the top priorities of your TODO list when
196 ;; todos-save-top-priorities is non-nil. Nice to include in your
197 ;; diary instead of the complete TODO list.
198 ;;
199 ;; Variable todos-mode-hook
200 ;;
201 ;; Just like other modes, too, this mode offers to call your
202 ;; functions before it goes about its business. This variable
203 ;; will be inspected for any functions you may wish to have
204 ;; called once the other TODO mode preparations have been
205 ;; completed.
206 ;;
207 ;; Variable todos-insert-threshold
208 ;;
209 ;; Another nifty feature is the insertion accuracy. If you have
210 ;; 8 items in your TODO list, then you may get asked 4 questions
211 ;; by the binary insertion algorithm. However, you may not
212 ;; really have a need for such accurate priorities amongst your
213 ;; TODO items. If you now think about the binary insertion
214 ;; halving the size of the window each time, then the threshold
215 ;; is the window size at which it will stop. If you set the
216 ;; threshold to zero, the upper and lower bound will coincide at
217 ;; the end of the loop and you will insert your item just before
218 ;; that point. If you set the threshold to, e.g. 8, it will stop
219 ;; as soon as the window size drops below that amount and will
220 ;; insert the item in the approximate center of that window. I
221 ;; got the idea for this feature after reading a very helpful
222 ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
223 ;; corrected some of my awful coding and pointed me towards some
224 ;; good reading. Thanks Trey!
225 ;;
226 ;; Things to do
227 ;;
228 ;; These originally were my ideas, but now also include all the
229 ;; suggestions that I included before forgetting them:
230 ;;
231 ;; o Fancy fonts for todo/top-priority buffer
232 ;; o Remove todos-prefix option in todos-top-priorities
233 ;; o Rename category
234 ;; o Move entry from one category to another one
235 ;; o Entries which both have the generic */* prefix and a
236 ;; "deadline" entry which are understood by diary, indicating
237 ;; an event (unless marked by &)
238 ;; o The optional COUNT variable of todos-forward-item should be
239 ;; applied to the other functions performing similar tasks
240 ;; o Modularization could be done for repeated elements of
241 ;; the code, like the completing-read lines of code.
242 ;; o license / version function
243 ;; o export to diary file
244 ;; o todos-report-bug
245 ;; o GNATS support
246 ;; o elide multiline (as in bbdb, or, to a lesser degree, in
247 ;; outline mode)
248 ;; o rewrite complete package to store data as lisp objects
249 ;; and have display modes for display, for diary export,
250 ;; etc. (Richard Stallman pointed out this is a bad idea)
251 ;; o so base todos.el on generic-mode.el instead
252 ;;
253 ;; History and Gossip
254 ;;
255 ;; Many thanks to all the ones who have contributed to the
256 ;; evolution of this package! I hope I have listed all of you
257 ;; somewhere in the documentation or at least in the RCS history!
258 ;;
259 ;; Enjoy this package and express your gratitude by sending nice
260 ;; things to my parents' address!
261 ;;
262 ;; Oliver Seidel
263 ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
264
265 ;;; Code:
266
267 ;; (require 'time-stamp)
268 (require 'calendar)
269 (require 'diary-lib)
270
271 ;; User-configurable variables:
272
273 (defgroup todos nil
274 "Maintain a list of todo items."
275 :link '(emacs-commentary-link "todos")
276 :version "21.1"
277 :group 'calendar)
278
279 (defcustom todos-prefix "§" ; "*/*"
280 "String prefixed to todo items for visual distinction."
281 :type 'string
282 :initialize 'custom-initialize-default
283 :set 'todos-reset-prefix
284 :group 'todos)
285
286 ;; "TODO mode prefix for entries.
287
288 ;; This is useful in conjunction with `calendar' and `diary' if you use
289
290 ;; #include \"~/.todos-do\"
291
292 ;; in your diary file to include your todo list file as part of your
293 ;; diary. With the default value \"*/*\" the diary displays each entry
294 ;; every day and it may also be marked on every day of the calendar.
295 ;; Using \"&%%(equal (calendar-current-date) date)\" instead will only
296 ;; show and mark todo entries for today, but may slow down processing of
297 ;; the diary file somewhat."
298 ;; :type 'string
299 ;; :group 'todos)
300 (defcustom todos-file-do (convert-standard-filename "~/.emacs.d/.todos-do")
301 "TODO mode list file."
302 :type 'file
303 :group 'todos)
304 (defcustom todos-file-done (convert-standard-filename "~/.emacs.d/.todos-done")
305 "TODO mode archive file."
306 :type 'file
307 :group 'todos)
308 (defcustom todos-mode-hook nil
309 "TODO mode hooks."
310 :type 'hook
311 :group 'todos)
312 (defcustom todos-edit-mode-hook nil
313 "TODO Edit mode hooks."
314 :type 'hook
315 :group 'todos)
316 (defcustom todos-insert-threshold 0
317 "TODO mode insertion accuracy.
318
319 If you have 8 items in your TODO list, then you may get asked 4
320 questions by the binary insertion algorithm. However, you may not
321 really have a need for such accurate priorities amongst your TODO
322 items. If you now think about the binary insertion halving the size
323 of the window each time, then the threshold is the window size at
324 which it will stop. If you set the threshold to zero, the upper and
325 lower bound will coincide at the end of the loop and you will insert
326 your item just before that point. If you set the threshold to,
327 e.g. 8, it will stop as soon as the window size drops below that
328 amount and will insert the item in the approximate center of that
329 window."
330 :type 'integer
331 :group 'todos)
332 (defvar todos-edit-buffer " *TODO Edit*"
333 "TODO Edit buffer name.")
334 (defcustom todos-file-top (convert-standard-filename "~/.todos-top")
335 "TODO mode top priorities file.
336
337 Not in TODO format, but diary compatible.
338 Automatically generated when `todos-save-top-priorities' is non-nil."
339 :type 'string
340 :group 'todos)
341
342 (defcustom todos-print-function 'ps-print-buffer-with-faces
343 "Function to print the current buffer."
344 :type 'symbol
345 :group 'todos)
346 (defcustom todos-show-priorities 1
347 "Default number of priorities to show by \\[todos-top-priorities].
348 0 means show all entries."
349 :type 'integer
350 :group 'todos)
351 (defcustom todos-print-priorities 0
352 "Default number of priorities to print by \\[todos-print].
353 0 means print all entries."
354 :type 'integer
355 :group 'todos)
356 (defcustom todos-remove-separator t
357 "Non-nil to remove category separators in\
358 \\[todos-top-priorities] and \\[todos-print]."
359 :type 'boolean
360 :group 'todos)
361 (defcustom todos-save-top-priorities-too t
362 "Non-nil makes `todos-save' automatically save top-priorities in `todos-file-top'."
363 :type 'boolean
364 :group 'todos)
365 (defcustom todos-completion-ignore-case t ;; FIXME: nil for release
366 "Non-nil means don't consider case significant in todos-completing-read."
367 :type 'boolean
368 :group 'todos)
369
370 ;; ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
371 ;; ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
372 ;; ;;
373 ;; ;; FIXME: use calendar format instead
374 ;; (defcustom todos-time-string-format
375 ;; "%:y-%02m-%02d %02H:%02M"
376 ;; "TODO mode time string format for done entries.
377 ;; For details see the variable `time-stamp-format'."
378 ;; :type 'string
379 ;; :group 'todos)
380
381 ;; (defcustom todos-entry-prefix-function 'todos-entry-timestamp-initials
382 ;; "Function producing text to insert at start of todo entry."
383 ;; :type 'symbol
384 ;; :group 'todos)
385 ;; (defcustom todos-initials (or (getenv "INITIALS") (user-login-name))
386 ;; "Initials of todo item author."
387 ;; :type 'string
388 ;; :group 'todos)
389
390 ;; (defun todos-entry-timestamp-initials ()
391 ;; "Prepend timestamp and your initials to the head of a TODO entry."
392 ;; (let ((time-stamp-format todos-time-string-format))
393 ;; (concat (time-stamp-string) " " todos-initials ": ")))
394
395 ;; (defcustom todos-date (calendar-date-string (calendar-current-date) t t)
396 ;; "Date string inserted in front of a todo item."
397 ;; :type 'string
398 ;; :group 'todos)
399
400 ;; (defcustom todos-time (substring (current-time-string) 11 16)
401 ;; "Time string inserted in front of a todo item."
402 ;; :type 'string
403 ;; :group 'todos)
404
405 (defun todos-current-date (&optional time)
406 "Return current date as a string for insertion in front of a todo item.
407 With non-nil TIME append the current time."
408 (concat (calendar-date-string (calendar-current-date) t t)
409 (when time
410 (concat " " (substring (current-time-string) 11 16)))))
411
412 (defcustom todos-add-time-string t
413 "Add current time to date string inserted in front of new items."
414 :type 'boolean
415 :group 'todos)
416
417 ;; "Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec")
418 ;; (regexp-opt (list "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31")))
419 (defun todos-date-string ()
420 "Return a regexp matching a diary date string."
421 (let ((month (regexp-opt (list "Jan" "Feb" "Mar" "Apr" "May" "Jun"
422 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")))
423 (day "[0-3]?[0-9]")
424 (year "[0-9]\\{4\\}"))
425 (concat month " " day ", " year)))
426
427 (defun todos-time-string ()
428 "Return a regexp matching a diary time string."
429 "[0-9]?[0-9][:.][0-9]\\{2\\}")
430
431 (defface todos-prefix-string
432 '((t
433 :inherit font-lock-constant-face
434 ))
435 "Face for Todos prefix string."
436 :group 'todos)
437 ;; (defvar todos-prefix-face 'todos-prefix-string)
438
439 (defface todos-date
440 '((t
441 :inherit diary
442 ))
443 "Face for Todos prefix string."
444 :group 'todos)
445 (defvar todos-date-face 'todos-date)
446
447 (defface todos-time
448 '((t
449 :inherit diary-time
450 ))
451 "Face for Todos prefix string."
452 :group 'todos)
453 (defvar todos-time-face 'todos-time)
454
455 (defun todos-date-string-match (lim)
456 "Find Todos date strings for font-locking."
457 (let ((lim (point-max)))
458 (re-search-forward (concat "^\\[?\\(" (todos-date-string) "\\)") lim t)))
459
460 (defun todos-time-string-match (lim)
461 "Find Todos time strings for font-locking."
462 (let ((lim (point-max)))
463 (re-search-forward (concat "^\\[?" (todos-date-string)
464 " \\(" (todos-time-string) "\\)") lim t)))
465
466 (defvar todos-font-lock-keywords
467 (list
468 '(todos-date-string-match 1 todos-date-face t)
469 '(todos-time-string-match 1 todos-time-face t))
470 "Font-locking for Todos mode.")
471
472 (defcustom todos-include-in-diary nil
473 "Non-nil to allow new Todo items to be included in the diary."
474 :type 'boolean
475 :group 'todos)
476
477 (defcustom todos-exclusion-start "["
478 "String prepended to item date to block diary inclusion."
479 :type 'string
480 :group 'todos
481 ;; :initialize 'custom-initialize-default
482 ;; :set ; change in whole Todos file
483 )
484
485 (defcustom todos-exclusion-end "]"
486 "String appended to item date to match todos-exclusion-start."
487 :type 'string
488 :group 'todos
489 ;; :initialize 'custom-initialize-default
490 ;; :set ; change in whole Todos file
491 )
492
493 (defun todos-toggle-item-diary-inclusion ()
494 "" ;FIXME add docstring
495 (interactive)
496 (save-excursion
497 (let ((beg (goto-char (todos-item-start)))
498 (end (save-excursion
499 (or (todos-time-string-match (todos-item-end))
500 (todos-date-string-match (todos-item-end))))))
501 (if (looking-at "\\[") ; FIXME use todos-exclusion-start
502 (progn
503 (replace-match "")
504 (search-forward "]" (1+ end) t) ; FIXME use todos-exclusion-end
505 (replace-match ""))
506 (when end
507 (insert "[") ; FIXME use todos-exclusion-start
508 (goto-char (1+ end))
509 (insert "]")))))) ; FIXME use todos-exclusion-end
510
511 (defun todos-toggle-diary-inclusion (arg)
512 "" ;FIXME add docstring
513 (interactive "p")
514 (save-excursion
515 (save-restriction
516 (when (eq arg 2) (widen))
517 (when (or (eq arg 1) (eq arg 2))
518 (goto-char (point-min))
519 (when (eq arg 2)
520 (re-search-forward (concat "^" (regexp-quote todos-category-beg))
521 (point-max) t)
522 (forward-line)
523 (when (looking-at (regexp-quote todos-category-end)) (forward-line)))
524 (while (not (eobp))
525 (todos-toggle-item-diary-inclusion)
526 (todos-forward-item))))))
527
528 ;; ---------------------------------------------------------------------------
529
530 ;; Set up some helpful context ...
531
532 (defvar todos-categories nil
533 "TODO categories.")
534
535 (defvar todos-previous-line 0
536 "Previous line asked about.")
537
538 (defvar todos-previous-answer 0
539 "Previous answer got.")
540
541 (defvar todos-mode-map
542 (let ((map (make-keymap)))
543 (suppress-keymap map t)
544 (define-key map "+" 'todos-forward-category)
545 (define-key map "-" 'todos-backward-category)
546 (define-key map "A" 'todos-add-category)
547 (define-key map "C" 'todos-display-categories)
548 (define-key map "d" 'todos-file-item) ;done/delete
549 (define-key map "D" 'todos-delete-category)
550 (define-key map "e" 'todos-edit-item)
551 (define-key map "E" 'todos-edit-multiline)
552 (define-key map "f" 'todos-file-item)
553 (define-key map "i" 'todos-insert-item)
554 (define-key map "I" 'todos-insert-item-here)
555 (define-key map "j" 'todos-jump-to-category)
556 (define-key map "k" 'todos-delete-item)
557 (define-key map "l" 'todos-lower-item)
558 (define-key map "m" 'todos-move-item)
559 (define-key map "n" 'todos-forward-item)
560 (define-key map "p" 'todos-backward-item)
561 (define-key map "P" 'todos-print)
562 (define-key map "q" 'todos-quit)
563 (define-key map "r" 'todos-raise-item)
564 (define-key map "R" 'todos-rename-category)
565 (define-key map "s" 'todos-save)
566 (define-key map "S" 'todos-save-top-priorities)
567 (define-key map "t" 'todos-top-priorities)
568 map)
569 "TODO mode keymap.")
570
571 (defvar todos-category-number 0 "TODO category number.")
572
573 (defvar todos-tmp-buffer-name " *todo tmp*")
574
575 ;; ;; FIXME: should the following four be defconsts?
576 ;; (defvar todos-category-sep (make-string 75 ?-)
577 ;; "Category separator.")
578
579 (defvar todos-category-beg "--- " ;" --- "
580 "Category start separator to be prepended onto category name.")
581
582 ;; (defvar todos-category-end "--- End"
583 ;; "Separator after a category.")
584
585 (defvar todos-item-end " :::"
586 "String marking the end of a todo item.
587 In Todos mode it is made invisible with an overlay.")
588
589 ;; ---------------------------------------------------------------------------
590
591 (defcustom todos-number-prefix nil
592 "Non-nil to show item prefixes as consecutively increasing integers."
593 :type 'boolean
594 :initialize 'custom-initialize-default
595 :set 'todos-reset-prefix
596 :group 'todos)
597
598 (defun todos-reset-prefix (symbol value)
599 "Set SYMBOL's value to VALUE, and ." ; FIXME
600 (let ((oldvalue (symbol-value symbol)))
601 (custom-set-default symbol value)
602 (when (not (equal value oldvalue))
603 (save-window-excursion
604 (todos-show)
605 (save-excursion
606 (widen)
607 (goto-char (point-min))
608 (while (not (eobp))
609 (re-search-forward
610 (concat "^" (regexp-quote todos-category-beg)) (point-max) t)
611 (forward-line)
612 (or (eobp)
613 (while (not (looking-at (regexp-quote todos-category-end)))
614 (remove-overlays (1- (point)) (1+ (point)))
615 (forward-line)))))
616 ;; activate the prefix setting (save-restriction does not help)
617 (todos-show)))))
618
619 ;; FIXME: rename and/or rewrite
620 (defun todos-update-numbered-prefix ()
621 "Update consecutive item numbering in the current category."
622 (save-excursion
623 (goto-char (point-min))
624 (while (not (eobp))
625 (remove-overlays (1- (point)) (1+ (point)))
626 (todos-forward-item))
627 (todos-show)))
628
629 (defvar todos-item-start-overlays nil "")
630
631 (defvar todos-item-end-overlays nil "")
632
633 (defun todos-check-overlay (prop)
634 "" ;FIXME add docstring
635 (let ((ovlist (overlays-in (1- (point)) (1+ (point)))))
636 ;; (let ((ovlist (overlays-in (point) (point))))
637 (when ovlist (overlay-get (car ovlist) prop))))
638
639 (defun todos-item-overlays ()
640 "" ;FIXME add docstring
641 (let ((prefix (propertize (concat todos-prefix " ") 'face 'todos-prefix-string))
642 (num 1)
643 ;; (paren show-paren-mode)
644 ov-pref ov-end)
645 ;; turn off show-paren-mode to avoid overlay reduplication problem
646 ;; (if paren (show-paren-mode 0))
647 (save-excursion
648 (goto-char (point-min))
649 (while (not (eobp))
650 (if todos-number-prefix
651 (setq prefix (propertize (concat (number-to-string num) " ")
652 'face 'todos-prefix-string)))
653 (unless (todos-check-overlay 'before-string)
654 (or (and (setq ov-pref (pop todos-item-start-overlays))
655 (move-overlay ov-pref (point) (point)))
656 (and (setq ov-pref (make-overlay (point) (point)))
657 (overlay-put ov-pref 'before-string prefix))))
658 (re-search-forward (concat "\\(" (regexp-quote todos-item-end) "\\)\n"))
659 (backward-char)
660 (unless (todos-check-overlay 'invisible)
661 (or (and (setq ov-end (pop
662 todos-item-end-overlays))
663 (move-overlay ov-end (match-beginning 1) (match-end 1)))
664 (and (setq ov-end (make-overlay (match-beginning 1) (match-end 1)))
665 (overlay-put ov-end 'invisible t))))
666 (forward-line)
667 (if todos-number-prefix (setq num (1+ num))))
668 ;; (if paren (show-paren-mode 1))
669 ;; (todos-show-paren-hack)
670 )))
671
672 (defun todos-show-paren-hack ()
673 "Purge overlay duplication due to show-paren-mode."
674 (save-excursion
675 (when show-paren-mode
676 (goto-char (point-min))
677 (while (not (eobp))
678 ;; (let ((ovlist (overlays-in (1- (point)) (1+ (point))))
679 (let ((ovlist (overlays-in (point) (point)))
680 ov)
681 (while (> (length ovlist) 1)
682 (setq ov (pop ovlist))
683 (delete-overlay ov)))
684 (forward-line))
685 (if (and (bolp) (eolp))
686 ;; (let ((ovlist (overlays-in (1- (point)) (1+ (point)))))
687 (let ((ovlist (overlays-in (point) (point))))
688 (remove-overlays (1- (point)) (1+ (point))))))))
689
690 (defun todos-category-select ()
691 "Make TODO mode display the current category correctly."
692 (let ((name (nth todos-category-number todos-categories)))
693 (setq mode-line-buffer-identification
694 ;; (concat "Category: " name))
695 (concat "Category: " (format "%18s" name)))
696 (widen)
697 (goto-char (point-min))
698 (search-forward-regexp
699 (concat "\n" ;"^" (regexp-quote todos-category-sep) "\n"
700 (regexp-quote (concat todos-category-beg name))
701 "$"))
702 (let ((begin (1+ (line-end-position))))
703 ;; (search-forward-regexp (concat "^" todos-category-end))
704 (re-search-forward (concat "^" todos-category-beg) (point-max) t)
705 (narrow-to-region begin (line-beginning-position))
706 (goto-char (point-min))))
707 (todos-item-overlays)
708 ;; (todos-show-paren-hack)
709 )
710
711 (defun todos-forward-category ()
712 "Go forward to TODO list of next category."
713 (interactive)
714 (setq todos-category-number
715 (mod (1+ todos-category-number) (length todos-categories)))
716 (todos-category-select))
717
718 (defun todos-backward-category ()
719 "Go back to TODO list of previous category."
720 (interactive)
721 (setq todos-category-number
722 (mod (1- todos-category-number) (length todos-categories)))
723 (todos-category-select))
724
725 (defun todos-backward-item (&optional count)
726 "Select previous entry of TODO list."
727 (interactive "P")
728 (re-search-backward (concat (regexp-quote todos-item-end) "\n") nil t count)
729 (goto-char (todos-item-start)))
730
731 (defun todos-forward-item (&optional count)
732 "Select COUNT-th next entry of TODO list."
733 (interactive "P")
734 (when (todos-check-overlay 'invisible) (goto-char (todos-item-start)))
735 (re-search-forward (concat (regexp-quote todos-item-end) "\n") nil t count))
736
737 (defun todos-save ()
738 "Save the TODO list."
739 (interactive)
740 (save-excursion
741 (save-restriction
742 (save-buffer)))
743 (if todos-save-top-priorities-too (todos-save-top-priorities)))
744
745 (defun todos-quit ()
746 "Done with TODO list for now."
747 (interactive)
748 (widen)
749 (todos-save)
750 (message "")
751 (bury-buffer))
752
753 (defun todos-edit-item ()
754 "Edit current TODO list entry."
755 (interactive)
756 (let ((item (todos-item-string)))
757 (if (todos-string-multiline-p item)
758 (todos-edit-multiline)
759 (let ((new (read-from-minibuffer "Edit: " item)))
760 (todos-remove-item)
761 (insert new todos-item-end "\n")
762 (todos-backward-item)
763 (if todos-number-prefix
764 (todos-update-numbered-prefix)
765 (todos-item-overlays))))))
766
767 ;; FIXME to work with overlays
768 (defun todos-edit-multiline ()
769 "Set up a buffer for editing a multiline TODO list entry."
770 (interactive)
771 (let ((buffer-name (generate-new-buffer-name todos-edit-buffer)))
772 (switch-to-buffer
773 (make-indirect-buffer
774 (file-name-nondirectory todos-file-do) buffer-name))
775 (message "To exit, simply kill this buffer and return to list.")
776 (todos-edit-mode)
777 (narrow-to-region (todos-item-start) (todos-item-end))))
778
779 ;;;###autoload
780 (defun todos-add-category (&optional cat)
781 "Add new category CAT to the TODO list."
782 (interactive)
783 (let ((buf (find-file-noselect todos-file-do t))
784 (prompt "Category: "))
785 (unless (zerop (buffer-size buf))
786 (and (null todos-categories)
787 (error "Error in %s: File is non-empty but contains no category"
788 todos-file-do)))
789 (unless cat (setq cat (read-from-minibuffer prompt)))
790 (with-current-buffer buf
791 ;; reject names that could induce bugs and confusion
792 (while (and (cond ((string= "" cat)
793 (setq prompt "Enter a non-empty category name: "))
794 ((string-match "\\`\\s-+\\'" cat)
795 (setq prompt "Enter a category name that is not only white space: "))
796 ((member cat todos-categories)
797 (setq prompt "Enter a non-existing category name: ")))
798 (setq cat (read-from-minibuffer prompt))))
799 ;; initialize a newly created Todo buffer for Todo mode
800 (unless (file-exists-p todos-file-do) (todos-mode))
801 (setq todos-categories (cons cat todos-categories))
802 (widen)
803 (goto-char (point-min))
804 ;; (insert (format "%s\n%s%s\n%s\n" todos-category-sep todos-category-beg cat
805 ;; todos-category-end))
806 (insert todos-category-beg cat "\n")
807 (if (interactive-p)
808 ;; properly display the newly added category
809 (progn (setq todos-category-number 0) (todos-show))
810 0))))
811
812 ;;;###autoload
813 (defun todos-add-item-non-interactively (new-item category)
814 "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
815 ;; FIXME: really need this? (and in save-excursion?)
816 (save-excursion
817 (todos-show))
818 ;; (save-excursion
819 (if (string= "" category)
820 (setq category (nth todos-category-number todos-categories)))
821 (let ((cat-exists (member category todos-categories)))
822 (setq todos-category-number
823 (if cat-exists
824 (- (length todos-categories) (length cat-exists))
825 (todos-add-category category))))
826 ;; FIXME: really need this? (yes for todos-move-item, to show moved to category)
827 (todos-show)
828 (setq todos-previous-line 0)
829 (let ((top 1)
830 (bottom (1+ (count-lines (point-min) (point-max)))))
831 (while (> (- bottom top) todos-insert-threshold)
832 (let* ((current (/ (+ top bottom) 2))
833 (answer (if (< current bottom)
834 (todos-more-important-p current) nil)))
835 (if answer
836 (setq bottom current)
837 (setq top (1+ current)))))
838 (setq top (/ (+ top bottom) 2))
839 ;; goto-line doesn't have the desired behavior in a narrowed buffer.
840 (goto-char (point-min))
841 (forward-line (1- top)))
842 (todos-insert-with-overlays new-item)
843 ;; (todos-show-paren-hack)
844 );)
845
846 (defun todos-rename-category (new)
847 "Rename current Todos category."
848 (interactive "sCategory: ")
849 (let ((cat (nth todos-category-number todos-categories))
850 (vec (vconcat todos-categories))
851 prompt)
852 (while (and (cond ((string= "" new)
853 (setq prompt "Enter a non-empty category name: "))
854 ((string-match "\\`\\s-+\\'" new)
855 (setq prompt "Enter a category name that is not only white space: "))
856 ((member new todos-categories)
857 (setq prompt "Enter a non-existing category name: ")))
858 (setq new (read-from-minibuffer prompt))))
859 (aset vec todos-category-number new)
860 (setq todos-categories (append vec nil))
861 (save-excursion
862 (widen)
863 (re-search-backward (concat ;(regexp-quote todos-category-sep) "\n"
864 (regexp-quote todos-category-beg) "\\("
865 (regexp-quote cat) "\\)\n") (point-min) t)
866 ;; (goto-char (match-end 0))
867 ;; (when (looking-at (regexp-quote cat))
868 ;; (replace-match new t))
869 (replace-match new t t nil 1)
870 (goto-char (point-min))
871 (setq mode-line-buffer-identification
872 (concat "Category: " new))))
873 ;; (concat "Category: " (format "%18s" new)))))
874 (todos-category-select))
875
876 (defun todos-delete-category (&optional arg)
877 "Delete current Todos category provided it is empty.
878 With ARG non-nil delete the category unconditionally,
879 i.e. including all existing entries."
880 (interactive "P")
881 (if (and (null arg)
882 (not (eq (point-max) (point-min))))
883 (message "This category is not empty, so it cannot be deleted")
884 (let ((cat (nth todos-category-number todos-categories)) beg end)
885 (when (y-or-n-p (concat "Permanently remove category \"" cat
886 "\"" (and arg " and all its entries") "? "))
887 (widen)
888 (setq beg (re-search-backward
889 (concat "^" ;(regexp-quote todos-category-sep) "\n"
890 (regexp-quote todos-category-beg) cat "\n")
891 (point-min) nil)
892 end (progn
893 (re-search-forward
894 ;; (concat "^" (regexp-quote todos-category-end) "\n")))
895 (concat "\n" (regexp-quote todos-category-beg) ".*\n")
896 (point-max) t)
897 (match-beginning 0)))
898 (remove-overlays beg end)
899 (kill-region beg end)
900 (setq todos-categories (delete cat todos-categories))
901 (todos-category-select)
902 (message "Deleted category %s" cat)))))
903
904 (defcustom todos-categories-buffer "*TODOS Categories*"
905 "Name of buffer displayed by `todos-display-categories'"
906 :type 'string
907 :group 'todos)
908
909 (defun todos-display-categories ()
910 "Display an alphabetical list of clickable Todos category names.
911 Click or type RET on a category name to go to it."
912 (interactive)
913 ;; (setq todos-window-configuration (current-window-configuration))
914 (let ((categories (copy-sequence todos-categories))
915 beg)
916 ;; alphabetize the list case insensitively
917 (setq categories (sort categories (lambda (s1 s2) (let ((cis1 (upcase s1))
918 (cis2 (upcase s2)))
919 (string< cis1 cis2)))))
920 (require 'widget)
921 (eval-when-compile
922 (require 'wid-edit))
923 (with-current-buffer (get-buffer-create todos-categories-buffer)
924 (switch-to-buffer (current-buffer))
925 (erase-buffer)
926 (kill-all-local-variables)
927 (widget-insert "Press a button to display the corresponding category.\n\n")
928 (setq beg (point))
929 (mapc (lambda (cat)
930 (widget-create 'push-button
931 :notify (lambda (widget &rest ignore)
932 (todos-jump-to-category-noninteractively
933 (widget-get widget :value)))
934
935 cat)
936 (widget-insert "\n"))
937 categories)
938 (use-local-map widget-keymap)
939 (widget-setup))))
940
941 (defun todos-jump-to-category-noninteractively (cat)
942 (let ((name todos-categories-buffer))
943 (if (string= (buffer-name) name)
944 (kill-buffer name)))
945 ;; (set-window-configuration todos-window-configuration)
946 (switch-to-buffer (file-name-nondirectory todos-file-do))
947 (widen)
948 (goto-char (point-min))
949 (setq todos-category-number (- (length todos-categories)
950 (length (member cat todos-categories))))
951 (todos-category-select))
952
953 ;;;###autoload
954 (defun todos-insert-item (arg)
955 "Insert new TODO list entry.
956 With a prefix argument solicit the category, otherwise use the current
957 category."
958 (interactive "P")
959 ;; (save-excursion
960 (if (not (derived-mode-p 'todos-mode)) (todos-show))
961 (let* ((new-item (concat (unless todos-include-in-diary "[")
962 (todos-current-date todos-add-time-string)
963 (unless todos-include-in-diary "]") " "
964 (read-from-minibuffer "New TODO entry: ")))
965 (current-category (nth todos-category-number todos-categories))
966 (category (if arg (todos-completing-read) current-category)))
967 (todos-add-item-non-interactively new-item category)));)
968
969 (defun todos-insert-item-here ()
970 "Insert a new TODO list entry directly above the entry at point.
971 If point is on an empty line, insert the entry there."
972 (interactive)
973 (if (not (derived-mode-p 'todos-mode)) (todos-show))
974 (let ((new (concat (unless todos-include-in-diary "[")
975 (todos-current-date todos-add-time-string)
976 (unless todos-include-in-diary "]") " "
977 (read-from-minibuffer "New TODO entry: "))))
978 (todos-insert-with-overlays new)))
979
980 (defun todos-insert-with-overlays (item)
981 "" ;FIXME add docstring
982 (let (ov-start ov-end p1 p2)
983 (unless (and (bolp) (eolp)) (goto-char (todos-item-start)))
984 (insert item todos-item-end "\n")
985 (todos-backward-item)
986 (if todos-number-prefix
987 (todos-update-numbered-prefix)
988 (todos-item-overlays))))
989
990 (defun todos-more-important-p (line)
991 "Ask whether entry is more important than the one at LINE."
992 (unless (equal todos-previous-line line)
993 (setq todos-previous-line line)
994 (goto-char (point-min))
995 (forward-line (1- todos-previous-line))
996 (let ((item (todos-item-string-start)))
997 (setq todos-previous-answer
998 (y-or-n-p (concat "More important than '" item "'? ")))))
999 todos-previous-answer)
1000
1001 (defun todos-delete-item ()
1002 "Delete current TODO list entry."
1003 (interactive)
1004 (if (> (count-lines (point-min) (point-max)) 0)
1005 (let* ((todos-entry (todos-item-string-start))
1006 (todos-answer (y-or-n-p (concat "Permanently remove '"
1007 todos-entry "'? "))))
1008 (when todos-answer
1009 (todos-remove-item)
1010 (when (and (bolp) (eolp)) (todos-backward-item))
1011 (if todos-number-prefix
1012 (todos-update-numbered-prefix)
1013 (todos-item-overlays))))
1014 (error "No TODO list entry to delete")))
1015
1016 (defun todos-raise-item ()
1017 "Raise priority of current entry."
1018 (interactive)
1019 (if (and (not (and (bolp) (eolp)))
1020 (> (count-lines (point-min) (point)) 0))
1021 (let ((item (todos-item-string)))
1022 (todos-remove-item)
1023 (todos-backward-item)
1024 (todos-insert-with-overlays item))
1025 (error "No TODO list entry to raise")))
1026
1027 (defun todos-lower-item ()
1028 "Lower priority of current entry."
1029 (interactive)
1030 (if (> (count-lines (point) (point-max)) 1)
1031 ;; Assume there is a final newline
1032 (let ((item (todos-item-string)))
1033 (todos-remove-item)
1034 (todos-forward-item)
1035 (todos-insert-with-overlays item))
1036 (error "No TODO list entry to lower")))
1037
1038 (defun todos-move-item ()
1039 "Move the current todo item to another, interactively named, category.
1040
1041 If the named category is not one of the current todo categories, then
1042 it is created and the item becomes the first entry in that category."
1043 (interactive)
1044 (let ((item (todos-item-string))
1045 (inhibit-quit t)
1046 (category (todos-completing-read)))
1047 (todos-remove-item)
1048 (todos-add-item-non-interactively item category)))
1049
1050 (defun todos-file-item (&optional comment)
1051 "File the current TODO list entry away, annotated with an optional COMMENT."
1052 (interactive "sComment: ")
1053 (or (> (count-lines (point-min) (point-max)) 0)
1054 (error "No TODO list entry to file away"))
1055 (let ((time-stamp-format todos-time-string-format))
1056 (when (and comment (> (length comment) 0))
1057 (goto-char (todos-item-end))
1058 (insert
1059 (if (save-excursion (beginning-of-line)
1060 (looking-at (regexp-quote todos-prefix)))
1061 " "
1062 "\n\t")
1063 "(" comment ")"))
1064 (goto-char (todos-item-end))
1065 (insert " [" (nth todos-category-number todos-categories) "]")
1066 (goto-char (todos-item-start))
1067 (let ((temp-point (point)))
1068 (if (looking-at (regexp-quote todos-prefix))
1069 (replace-match (time-stamp-string))
1070 ;; Standard prefix -> timestamp
1071 ;; Else prefix non-standard item start with timestamp
1072 (insert (time-stamp-string)))
1073 (append-to-file temp-point (1+ (todos-item-end)) todos-file-done)
1074 (delete-region temp-point (1+ (todos-item-end))))
1075 (todos-backward-item)
1076 (message "")))
1077
1078 (defun todos-highlight-item ()
1079 "Highlight the todo item the cursor is on."
1080 (interactive)
1081 (if hl-line-mode ; todos-highlight-item
1082 (hl-line-mode 0)
1083 (hl-line-mode 1)))
1084 ;; ---------------------------------------------------------------------------
1085
1086 ;; Utility functions:
1087
1088
1089 ;;;###autoload
1090 (defun todos-top-priorities (&optional nof-priorities category-pr-page)
1091 "List top priorities for each category.
1092
1093 Number of entries for each category is given by NOF-PRIORITIES which
1094 defaults to \'todos-show-priorities\'.
1095
1096 If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
1097 between each category."
1098
1099 (interactive "P")
1100 (or nof-priorities (setq nof-priorities todos-show-priorities))
1101 (if (listp nof-priorities) ;universal argument
1102 (setq nof-priorities (car nof-priorities)))
1103 (let ((todos-print-buffer-name todos-tmp-buffer-name)
1104 ;;(todos-print-category-number 0)
1105 (todos-category-break (if category-pr-page "\f" ""))
1106 (cat-end
1107 (concat
1108 (if todos-remove-separator
1109 (concat todos-category-end "\n"
1110 (regexp-quote todos-prefix) " " todos-category-sep "\n")
1111 (concat todos-category-end "\n"))))
1112 beg end)
1113 (save-excursion ; FIXME: need this?
1114 (todos-show)
1115 (save-restriction
1116 (save-current-buffer
1117 (widen)
1118 (copy-to-buffer todos-print-buffer-name (point-min) (point-max))
1119 (set-buffer todos-print-buffer-name)
1120 (goto-char (point-min))
1121 ;; (when (re-search-forward (regexp-quote todos-header) nil t)
1122 ;; (beginning-of-line 1)
1123 ;; (delete-region (point) (line-end-position)))
1124 (while (re-search-forward ;Find category start
1125 (regexp-quote (concat todos-prefix todos-category-beg))
1126 nil t)
1127 (setq beg (+ (line-end-position) 1)) ;Start of first entry.
1128 (re-search-forward cat-end nil t)
1129 (setq end (match-beginning 0))
1130 (replace-match todos-category-break)
1131 (narrow-to-region beg end) ;In case we have too few entries.
1132 (goto-char (point-min))
1133 (if (zerop nof-priorities) ;Traverse entries.
1134 (goto-char end) ;All entries
1135 (todos-forward-item nof-priorities))
1136 (setq beg (point))
1137 (delete-region beg end)
1138 (widen))
1139 (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
1140 (goto-char (point-min)) ;Due to display buffer
1141 ;; FIXME: after todos-edit-multiline widening remains
1142 )))
1143 ;; Could have used switch-to-buffer as it has a norecord argument,
1144 ;; which is nice when we are called from e.g. todos-print.
1145 ;; Else we could have used pop-to-buffer.
1146 (display-buffer todos-print-buffer-name)
1147 (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
1148 todos-print-buffer-name)))
1149
1150 (defun todos-save-top-priorities (&optional nof-priorities)
1151 "Save top priorities for each category in `todos-file-top'.
1152
1153 Number of entries for each category is given by NOF-PRIORITIES which
1154 defaults to `todos-show-priorities'."
1155 (interactive "P")
1156 (save-window-excursion
1157 (save-excursion
1158 (save-restriction
1159 (todos-top-priorities nof-priorities)
1160 (set-buffer todos-tmp-buffer-name)
1161 (write-file todos-file-top)
1162 (kill-this-buffer)))))
1163
1164 ;;;###autoload
1165 (defun todos-print (&optional category-pr-page)
1166 "Print todo summary using `todos-print-function'.
1167 If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
1168 between each category.
1169
1170 Number of entries for each category is given by `todos-print-priorities'."
1171 (interactive "P")
1172 (save-window-excursion
1173 (save-excursion
1174 (save-restriction
1175 (todos-top-priorities todos-print-priorities
1176 category-pr-page)
1177 (set-buffer todos-tmp-buffer-name)
1178 (and (funcall todos-print-function)
1179 (kill-this-buffer))
1180 (message "Todo printing done.")))))
1181
1182 (defun todos-list-categories ()
1183 "Return a list of the Todo mode categories."
1184 (let ((todos-buf (find-file-noselect todos-file-do))
1185 categories)
1186 (with-current-buffer todos-buf
1187 (save-excursion
1188 (save-restriction
1189 (widen)
1190 (goto-char (point-max))
1191 (while (re-search-backward
1192 ;; (concat "^" (regexp-quote (concat todos-prefix todos-category-beg))
1193 (concat "^" ;(regexp-quote todos-category-sep) "\n"
1194 (regexp-quote todos-category-beg)
1195 "\\(.*\\)\n")
1196 (point-min) t)
1197 (push (match-string-no-properties 1) categories)))))
1198 categories))
1199
1200 (defun todos-jump-to-category ()
1201 "Jump to a category. Default is previous category."
1202 (interactive)
1203 (let ((category (todos-completing-read)))
1204 (if (string= "" category)
1205 (setq category (nth todos-category-number todos-categories)))
1206 (setq todos-category-number
1207 (if (member category todos-categories)
1208 (- (length todos-categories)
1209 (length (member category todos-categories)))
1210 (todos-add-category category)))
1211 (todos-show)))
1212
1213 (defun todos-line-string ()
1214 "Return current line in buffer as a string."
1215 (buffer-substring (line-beginning-position) (line-end-position)))
1216
1217 (defun todos-item-string-start ()
1218 "Return the start of this TODO list entry as a string."
1219 ;; Suitable for putting in the minibuffer when asking the user
1220 (let ((item (todos-item-string)))
1221 (if (> (length item) 60)
1222 (setq item (concat (substring item 0 56) "...")))
1223 item))
1224
1225 (defun todos-item-start ()
1226 "Return point at start of current TODO list item."
1227 (save-excursion
1228 (if (re-search-backward (concat (regexp-quote todos-item-end) "\n") nil t)
1229 (forward-line)
1230 (goto-char (point-min)))
1231 ;; for widened buffer in todos-toggle-diary-inclusion
1232 ;; (while (looking-at
1233 ;; (concat "^" (regexp-opt (list todos-category-sep todos-category-beg
1234 ;; todos-category-end))))
1235 ;; (forward-line))
1236 (point)))
1237
1238 (defun todos-item-end ()
1239 "Return point at end of current TODO list item."
1240 (if (todos-check-overlay 'invisible)
1241 (search-backward todos-item-end)
1242 (when (not (and (bolp) (eobp)))
1243 (save-excursion
1244 (re-search-forward (concat "\\(" (regexp-quote todos-item-end) "\\)\n"))
1245 (match-beginning 1)))))
1246
1247 (defun todos-remove-item ()
1248 "Delete the current entry from the TODO list."
1249 (let ((beg (todos-item-start))
1250 (end (save-excursion
1251 (unless (todos-check-overlay 'invisible) (goto-char (todos-item-end)))
1252 (line-end-position)))
1253 ov-start ov-end)
1254 (goto-char (todos-item-start))
1255 ;; (setq ov-start (car (overlays-in (1- (point)) (1+ (point)))))
1256 (setq ov-start (car (overlays-in (point) (point))))
1257 (push ov-start todos-item-start-overlays)
1258 (delete-overlay ov-start)
1259 (goto-char (todos-item-end))
1260 ;; (setq ov-end (car (overlays-in (1- (point)) (1+ (point)))))
1261 ;; FIXME
1262 (setq ov-end (car (overlays-in (point) (point))))
1263 (push ov-end todos-item-end-overlays)
1264 (delete-overlay ov-end)
1265 (delete-region (todos-item-start) (1+ end))))
1266
1267 (defun todos-item-string ()
1268 "Return current TODO list entry as a string."
1269 (buffer-substring (todos-item-start) (todos-item-end)))
1270
1271 (defun todos-string-count-lines (string)
1272 "Return the number of lines STRING spans."
1273 (length (split-string string "\n")))
1274
1275 (defun todos-string-multiline-p (string)
1276 "Return non-nil if STRING spans several lines."
1277 (> (todos-string-count-lines string) 1))
1278
1279 (defun todos-completing-read ()
1280 "Return a category name, with completion, for use in Todo mode."
1281 ;; allow SPC to insert spaces, for adding new category names with
1282 ;; todos-move-item
1283 (let ((map minibuffer-local-completion-map))
1284 (define-key map " " nil)
1285 ;; make a copy of todos-categories in case history-delete-duplicates is
1286 ;; non-nil, which makes completing-read alter todos-categories
1287 (let* ((categories (copy-sequence todos-categories))
1288 (history (cons 'todos-categories (1+ todos-category-number)))
1289 (default (nth todos-category-number todos-categories))
1290 (completion-ignore-case todos-completion-ignore-case)
1291 (category (completing-read
1292 (concat "Category [" default "]: ")
1293 todos-categories nil nil nil history default)))
1294 ;; restore the original value of todos-categories
1295 (setq todos-categories categories)
1296 category)))
1297
1298 ;; ---------------------------------------------------------------------------
1299
1300 (easy-menu-define todos-menu todos-mode-map "Todo Menu"
1301 '("Todo"
1302 ["Next category" todos-forward-category t]
1303 ["Previous category" todos-backward-category t]
1304 ["Jump to category" todos-jump-to-category t]
1305 ["Show top priority items" todos-top-priorities t]
1306 ["Print categories" todos-print t]
1307 "---"
1308 ["Edit item" todos-edit-item t]
1309 ["File item" todos-file-item t]
1310 ["Insert new item" todos-insert-item t]
1311 ["Insert item here" todos-insert-item-here t]
1312 ["Kill item" todos-delete-item t]
1313 "---"
1314 ["Lower item priority" todos-lower-item t]
1315 ["Raise item priority" todos-raise-item t]
1316 "---"
1317 ["Next item" todos-forward-item t]
1318 ["Previous item" todos-backward-item t]
1319 "---"
1320 ["Save" todos-save t]
1321 ["Save Top Priorities" todos-save-top-priorities t]
1322 "---"
1323 ["Quit" todos-quit t]
1324 ))
1325
1326 ;; As calendar reads .todos-do before todos-mode is loaded.
1327 ;;;###autoload
1328 (defun todos-mode ()
1329 "Major mode for editing TODO lists.
1330
1331 \\{todos-mode-map}"
1332 (interactive)
1333 (kill-all-local-variables)
1334 (setq major-mode 'todos-mode)
1335 (setq mode-name "TODOS")
1336 (use-local-map todos-mode-map)
1337 (easy-menu-add todos-menu)
1338 (make-local-variable 'font-lock-defaults)
1339 (setq font-lock-defaults '(todos-font-lock-keywords t))
1340 (make-local-variable 'word-wrap)
1341 (setq word-wrap t)
1342 (make-local-variable 'wrap-prefix)
1343 (setq wrap-prefix (make-string (+ 5 (length todos-prefix)) 32))
1344 (unless (member '(continuation) fringe-indicator-alist)
1345 (push '(continuation) fringe-indicator-alist))
1346 (make-local-variable 'hl-line-range-function)
1347 (setq hl-line-range-function
1348 (lambda() (when (todos-item-end)
1349 (cons (todos-item-start) (todos-item-end)))))
1350 ;; (add-hook 'post-command-hook 'todos-show-paren-hack nil t)
1351 (run-mode-hooks 'todos-mode-hook))
1352
1353 ;; (defvar date)
1354 ;; (defvar entry)
1355
1356 ;; ;; t-c should be used from diary code, which requires calendar.
1357 ;; (declare-function calendar-current-date "calendar" nil)
1358
1359 ;; ;; Read about this function in the setup instructions above!
1360 ;; ;;;###autoload
1361 ;; (defun todos-cp ()
1362 ;; "Make a diary entry appear only in the current date's diary."
1363 ;; (if (equal (calendar-current-date) date)
1364 ;; entry))
1365
1366 (define-derived-mode todos-edit-mode text-mode "TODO Edit"
1367 "Major mode for editing items in the TODO list.
1368
1369 \\{todos-edit-mode-map}")
1370
1371 ;;;###autoload
1372 (defun todos-show ()
1373 "Show TODO list."
1374 (interactive)
1375 ;; Call todos-initial-setup only if there is neither a Todo file nor
1376 ;; a corresponding unsaved buffer.
1377 (if (or (file-exists-p todos-file-do)
1378 (let* ((buf (get-buffer (file-name-nondirectory todos-file-do)))
1379 (bufname (buffer-file-name buf)))
1380 (equal (expand-file-name todos-file-do) bufname)))
1381 (find-file todos-file-do)
1382 (todos-initial-setup))
1383 (unless (eq major-mode 'todos-mode) (todos-mode))
1384 (unless todos-categories
1385 (setq todos-categories (todos-list-categories)))
1386 ;; (beginning-of-line)
1387 (save-excursion
1388 (todos-category-select)
1389 ;; (todos-show-paren-hack)
1390 ))
1391
1392 (defun todos-initial-setup ()
1393 "Set up things to work properly in TODO mode."
1394 (find-file todos-file-do)
1395 (erase-buffer)
1396 (todos-mode)
1397 (todos-add-category "Todos"))
1398
1399 (provide 'todos)
1400
1401 ;; arch-tag: 6fd91be5-776e-4464-a109-da4ea0e4e497
1402 ;;; todos.el ends here