]> code.delx.au - gnu-emacs/blob - lisp/calendar/todo-mode.el
Everything seems to work in Harald Melands Emacs 20.02 and
[gnu-emacs] / lisp / calendar / todo-mode.el
1 ;;; todo-mode.el -- Major mode for editing TODO list files
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: Oliver.Seidel@cl.cam.ac.uk (was valid on Aug 2, 1997)
6 ;; Created: 2 Aug 1997
7 ;; Version: $Id: todo-mode.el,v 1.17 1997/10/15 14:30:41 os10000 Exp os10000 $
8 ;; Keywords: Categorised TODO list editor, todo-mode
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;; ---------------------------------------------------------------------------
28
29 ;;; Commentary:
30
31 ;; Quickstart Installation:
32 ;; ========================
33 ;;
34 ;; To get this to work, make emacs execute the line
35 ;;
36 ;; (require 'todo-mode) ;; load the TODO package
37 ;;
38 ;; You may now enter new items by typing "M-x todo-insert-item", or enter
39 ;; your the TODO list file by typing "M-x todo-show".
40 ;;
41 ;; The TODO list file has a special format and some auxiliary information,
42 ;; which will be added by the todo-show function if it attempts to visit
43 ;; an un-initialised file. Hence it is recommended to use the todo-show
44 ;; function for the first time, in order to initialise the file, but it
45 ;; is not necessary afterwards.
46 ;;
47 ;; As these commands are quite long to type, I would recommend the addition
48 ;; of two bindings to your to your global keymap. I personally have the
49 ;; following in my initialisation file:
50 ;;
51 ;; (global-set-key "\C-ct" 'todo-show) ;; switch to TODO buffer
52 ;; (global-set-key "\C-ci" 'todo-insert-item) ;; insert new item
53 ;;
54 ;; Note, however, that this recommendation has prompted some criticism,
55 ;; since the keys C-c LETTER are reserved for user functions. I believe
56 ;; my recommendation is acceptable, since the Emacs Lisp Manual *Tips*
57 ;; section also details that the mode itself should not bind any functions
58 ;; to those keys. The express aim of the above two bindings is to work
59 ;; outside the mode, which doesn't need the show function and offers
60 ;; a different binding for the insert function. They serve as shortcuts
61 ;; and are not even needed (since the TODO mode will be entered by
62 ;; visiting the TODO file, and later by switching to its buffer).
63 ;;
64 ;;
65 ;;
66 ;; Pre-Requisites
67 ;; ==============
68 ;;
69 ;; This package will require the following packages to be available on
70 ;; the load-path:
71 ;; - time-stamp
72 ;; - easymenu
73 ;;
74 ;;
75 ;;
76 ;; Description:
77 ;; ============
78 ;;
79 ;; TODO is a major mode for EMACS which offers functionality to treat
80 ;; most lines in one buffer as a list of items one has to do. There
81 ;; are facilities to add new items, which are categorised, to edit or
82 ;; even delete items from the buffer. The buffer contents are currently
83 ;; compatible with the diary, so that the list of todo-items will show
84 ;; up in the FANCY diary mode.
85 ;;
86 ;; Notice: Besides the major mode, this file also exports the function
87 ;; "todo-show" which will change to the one specific TODO file that has
88 ;; been specified in the todo-file-do variable. If this file does not
89 ;; conform to the TODO mode conventions, the todo-show function will add
90 ;; the appropriate header and footer. I don't anticipate this to cause
91 ;; much grief, but be warned, in case you attempt to read a plain text file.
92 ;;
93 ;;
94 ;;
95 ;; Operation:
96 ;; ==========
97 ;;
98 ;; You will have the following facilities available:
99 ;;
100 ;; M-x todo-show will enter the todo list screen, here type
101 ;;
102 ;; + to go to next category
103 ;; - to go to previous category
104 ;; e to edit the current entry
105 ;; f to file the current entry, including a
106 ;; comment and timestamp
107 ;; i to insert a new entry
108 ;; k to kill the current entry
109 ;; l to lower the current entry's priority
110 ;; n for the next entry
111 ;; p for the previous entry
112 ;; q to save the list and exit the buffer
113 ;; r to raise the current entry's priority
114 ;; s to save the list
115 ;;
116 ;; When you add a new entry, you are asked for the text and then for the
117 ;; category. I for example have categories for things that I want to do
118 ;; in the office (like mail my mum), that I want to do in town (like buy
119 ;; cornflakes) and things I want to do at home (move my suitcases). The
120 ;; categories can be selected with the cursor keys and if you type in the
121 ;; name of a category which didn't exist before, an empty category of the
122 ;; desired name will be added and filled with the new entry.
123 ;;
124 ;;
125 ;;
126 ;; Configuration:
127 ;; ==============
128 ;;
129 ;; --- todo-prefix
130 ;;
131 ;; I would like to recommend that you use the prefix "*/*" (by
132 ;; leaving the variable 'todo-prefix' untouched) so that the diary
133 ;; displays each entry every day.
134 ;;
135 ;; To understand what I mean, please read the documentation that goes
136 ;; with the calendar since that will tell you how you can set up the
137 ;; fancy diary display and use the #include command to include your
138 ;; todo list file as part of your diary.
139 ;;
140 ;; If you have the diary package set up to usually display more than
141 ;; one day's entries at once, consider using
142 ;; "&%%(equal (calendar-current-date) date)"
143 ;; as the value of `todo-prefix'. Please note that this may slow down
144 ;; the processing of your diary file some.
145 ;;
146 ;;
147 ;; --- todo-file-do
148 ;;
149 ;; This variable is fairly self-explanatory. You have to store your TODO
150 ;; list somewhere. This variable tells the package where to go and find
151 ;; this file.
152 ;;
153 ;;
154 ;; --- todo-file-done
155 ;;
156 ;; Even when you're done, you may wish to retain the entries. Given
157 ;; that they're timestamped and you are offered to add a comment, this
158 ;; can make a useful diary of past events. It will even blend in with
159 ;; the EMACS diary package. So anyway, this variable holds the name
160 ;; of the file for the filed todo-items.
161 ;;
162 ;;
163 ;; --- todo-mode-hook
164 ;;
165 ;; Just like other modes, too, this mode offers to call your functions
166 ;; before it goes about its business. This variable will be inspected
167 ;; for any functions you may wish to have called once the other TODO
168 ;; mode preparations have been completed.
169 ;;
170 ;;
171 ;; --- todo-insert-threshold
172 ;;
173 ;; Another nifty feature is the insertion accuracy. If you have 8 items
174 ;; in your TODO list, then you may get asked 4 questions by the binary
175 ;; insertion algorithm. However, you may not really have a need for such
176 ;; accurate priorities amongst your TODO items. If you now think about
177 ;; the binary insertion halfing the size of the window each time, then
178 ;; the threshhold is the window size at which it will stop. If you set
179 ;; the threshhold to zero, the upper and lower bound will coincide at the
180 ;; end of the loop and you will insert your item just before that point.
181 ;; If you set the threshhold to i.e. 8, it will stop as soon as the window
182 ;; size drops below that amount and will insert the item in the approximate
183 ;; centre of that window. I got the idea for this feature after reading
184 ;; a very helpful e-mail reply from Trey Jackson <trey@cs.berkeley.edu>
185 ;; who corrected some of my awful coding and pointed me towards some good
186 ;; reading. Thanks Trey!
187 ;;
188 ;;
189 ;;
190 ;;
191 ;; Things to do:
192 ;; =============
193 ;;
194 ;; - licence / version function
195 ;; - export to diary file
196 ;; - todo-report-bug
197 ;; - GNATS support
198 ;; - add idea from Urban Boquist <boquist@cs.chalmers.se>: multi-line-entries
199 ;; - 'e' opens buffer for multi-line entry
200 ;; - elide multiline
201 ;; - rewrite complete package to store data as lisp objects and have
202 ;; display modes for display, for diary export, etc.
203 ;;
204 ;;
205 ;;
206 ;; History and Gossip:
207 ;; ===================
208 ;;
209 ;; Many thanks to all the ones who have contributed to the evolution of this
210 ;; package! I hope I have listed all of you somewhere in the documentation
211 ;; or at least in the RCS history!
212 ;;
213 ;; Enjoy this package and express your gratitude by sending nice things
214 ;; to my parents' address!
215 ;;
216 ;; Oliver Seidel
217 ;;
218 ;; (O Seidel, Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
219 ;;
220
221 ;; ---------------------------------------------------------------------------
222
223 ;; ---------------------------------------------------------------------------
224
225 ;;; Change Log:
226
227 ;; $Log: todo-mode.el,v $
228 ;; Revision 1.17 1997/10/15 14:30:41 os10000
229 ;; Attempted to reconcile Harald's changes with mine since 1.15.
230 ;;
231 ;; Revision 1.16 1997/10/15 14:00:12 os10000
232 ;; Fixed 'file-item' and added 20.02 split-string function.
233 ;;
234 ;; Revision 1.15 1997/10/14 22:22:35 os10000
235 ;; Added string-split (which I stole from ediff-util), changed
236 ;; pop-to-buffer to switch-to-buffer and added message on how
237 ;; to exit the multi-line-edit mode.
238 ;;
239 ;; Revision 1.14 1997/10/09 09:24:50 os10000
240 ;; Harald Meland <harald.meland@usit.uio.no> asked for
241 ;; the latest version, got 1.13, and returned this.
242 ;; He writes:
243 ;;
244 ;; Thanks a lot for the new version of todo-mode.el. As you will see I
245 ;; have messed it up a bit, hopefully for the better -- I don't like
246 ;; short, cryptic names for variables and functions, so I renamed most of
247 ;; them, and `defalias'ed the old function names. I hope you don't mind
248 ;; too much, I just kinda couldn't stop myself.
249 ;;
250 ;; Additionally, I included some support for multiline entries, cleaned
251 ;; up (IMHO :) a lot of the code, included completion-support for which
252 ;; category to install a new entry in, and possibly some other changes I
253 ;; can't remember :)
254 ;;
255 ;; It's getting rather late, and I have just done some preliminary
256 ;; testing on whether all of this really works, but so far it looks
257 ;; good.
258 ;;
259 ;; Revision 1.13 1997/08/19 14:00:36 seidel
260 ;; - changed name to todo-mode
261 ;; - fixed menu descriptions
262 ;; - fixed "pressing abort while filing"
263 ;; - attempted Emacs Lisp Manual *Tips* section compliance
264 ;;
265 ;; Revision 1.12 1997/08/06 10:56:15 os10000
266 ;; Fixed header, typos, layout, documentation.
267 ;;
268 ;; Revision 1.11 1997/08/06 09:14:25 os10000
269 ;; Applied patch from Istvan Marko <istvan@cmdmail.amd.com>
270 ;; to make menus work anywhere.
271 ;;
272 ;; Revision 1.10 1997/08/06 08:56:03 os10000
273 ;; Acted upon suggestion from Shane Holder <holder@rsn.hp.com>:
274 ;; Cancelling the editing of an entry will not delete it any more.
275 ;;
276 ;; Revision 1.9 1997/08/06 08:12:03 os10000
277 ;; Improved documentation. Broke some lines to comply with
278 ;; Richard Stallman's email to please keep in sync with the
279 ;; rest of the Emacs distribution files.
280 ;;
281 ;; Revision 1.8 1997/08/05 22:39:04 os10000
282 ;; Made todo-mode.el available under GPL.
283 ;;
284 ;; Revision 1.7 1997/08/05 22:34:14 os10000
285 ;; Fixed insertion routine with help from Trey Jackson
286 ;; <trey@cs.berkeley.edu>; added todo-inst-tresh;
287 ;; fixed keyboard layout to remove unwanted keys.
288 ;;
289 ;; Revision 1.6 1997/08/05 16:47:01 os10000
290 ;; Incorporated menus for XEmacs from Allan.Cochrane@soton.sc.philips.com,
291 ;; fixed TYPO, fixed todo-file-cmd, cleaned up rcs history.
292 ;;
293 ;; Revision 1.5 1997/08/05 14:43:39 os10000
294 ;; Added improvements from Ron Gut <rgut@aware.com>.
295 ;; Added category management.
296 ;;
297 ;; Revision 1.4 1997/08/04 16:18:45 os10000
298 ;; Added Raise/Lower item.
299 ;;
300 ;; Revision 1.3 1997/08/03 12:47:26 os10000
301 ;; Cleaned up variables, prefix and cursor position.
302 ;;
303 ;; Revision 1.2 1997/08/03 12:15:28 os10000
304 ;; It appears to work.
305 ;;
306 ;; Revision 1.1 1997/08/03 12:15:13 os10000
307 ;; Initial revision
308 ;;
309
310 ;; ---------------------------------------------------------------------------
311
312 ;;; Code:
313
314 ;; User-configurable variables:
315
316 (defvar todo-prefix "*/*" "*TODO mode prefix for entries.")
317 (defvar todo-file-do "~/.todo-do" "*TODO mode list file.")
318 (defvar todo-file-done "~/.todo-done" "*TODO mode archive file.")
319 (defvar todo-mode-hook nil "*TODO mode hooks.")
320 (defvar todo-edit-mode-hook nil "*TODO Edit mode hooks.")
321 (defvar todo-insert-threshold 0 "*TODO mode insertion accuracy.")
322 (defvar todo-edit-buffer " *TODO Edit*" "TODO Edit buffer name.")
323
324 ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
325 ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
326 ;;
327 (defvar todo-time-string-format
328 "%:y-%02m-%02d %02H:%02M"
329 "TODO mode time string format for done entries.
330 For details see the variable `time-stamp-format'.")
331
332 ;; ---------------------------------------------------------------------------
333
334 ;; Get some outside help ...
335
336 (require 'time-stamp)
337 (require 'easymenu)
338
339 ;; ---------------------------------------------------------------------------
340
341 ;; Set up some helpful context ...
342
343 (defvar todo-categories nil "TODO categories.")
344 (defvar todo-cats nil
345 "Old variable for holding the TODO categories. Use `todo-categories' instead.")
346 (defvar todo-previous-line 0 "Previous line that I asked about.")
347 (defvar todo-previous-answer 0 "Previous answer that I got.")
348 (defvar todo-mode-map nil "TODO mode keymap.")
349 (defvar todo-category-number 0 "TODO category number.")
350
351 ;; ---------------------------------------------------------------------------
352
353 (if todo-mode-map
354 nil
355 (let ((map (make-keymap)))
356 (suppress-keymap map t)
357 (define-key map "+" 'todo-forward-category)
358 (define-key map "-" 'todo-backward-category)
359 (define-key map "e" 'todo-edit-item)
360 (define-key map "E" 'todo-edit-multiline)
361 (define-key map "f" 'todo-file-item)
362 (define-key map "i" 'todo-insert-item)
363 (define-key map "k" 'todo-delete-item)
364 (define-key map "l" 'todo-lower-item)
365 (define-key map "n" 'todo-forward-item)
366 (define-key map "p" 'todo-backward-item)
367 (define-key map "q" 'todo-quit)
368 (define-key map "r" 'todo-raise-item)
369 (define-key map "s" 'todo-save)
370 (setq todo-mode-map map)))
371
372 (defun todo-category-select ()
373 "Make TODO mode display the current category correctly."
374 (let ((name (nth todo-category-number todo-categories)))
375 (setq mode-line-buffer-identification
376 (concat "Category: " name))
377 (widen)
378 (goto-char (point-min))
379 (search-forward-regexp
380 (concat "^" (regexp-quote (concat todo-prefix " --- " name))))
381 (let ((begin (1+ (point-at-eol))))
382 (search-forward-regexp "^--- End")
383 (narrow-to-region begin (point-at-bol))
384 (goto-char (point-min)))))
385 (defalias 'todo-cat-slct 'todo-category-select)
386
387 (defun todo-forward-category () "Go forward to TODO list of next category."
388 (interactive)
389 (setq todo-category-number
390 (mod (1+ todo-category-number) (length todo-categories)))
391 (todo-category-select))
392 (defalias 'todo-cmd-forw 'todo-forward-category)
393
394 (defun todo-backward-category () "Go back to TODO list of previous category."
395 (interactive)
396 (setq todo-category-number
397 (mod (1- todo-category-number) (length todo-categories)))
398 (todo-category-select))
399 (defalias 'todo-cmd-back 'todo-backward-category)
400
401 (defun todo-backward-item () "Select previous entry of TODO list."
402 (interactive)
403 (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
404 (message ""))
405 (defalias 'todo-cmd-prev 'todo-backward-item)
406
407 (defun todo-forward-item () "Select next entry of TODO list."
408 (interactive)
409 (end-of-line)
410 (search-forward-regexp (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
411 (beginning-of-line)
412 (message ""))
413 (defalias 'todo-cmd-next 'todo-forward-item)
414
415 (defun todo-save () "Save the TODO list."
416 (interactive)
417 (save-buffer))
418 (defalias 'todo-cmd-save 'todo-save)
419
420 (defun todo-quit () "Done with TODO list for now."
421 (interactive)
422 (widen)
423 (save-buffer)
424 (message "")
425 (bury-buffer))
426 (defalias 'todo-cmd-done 'todo-quit)
427
428 (defun todo-edit-item () "Edit current TODO list entry."
429 (interactive)
430 (let ((item (todo-item-string)))
431 (if (todo-string-multiline-p item)
432 (todo-edit-multiline)
433 (let ((new (read-from-minibuffer "Edit: " item)))
434 (todo-remove-item)
435 (insert new "\n")
436 (todo-backward-item)
437 (message "")))))
438 (defalias 'todo-cmd-edit 'todo-edit-item)
439
440 (defun todo-edit-multiline ()
441 "Set up a buffer for editing a multiline TODO list entry."
442 (interactive)
443 (let ((buffer-name (generate-new-buffer-name todo-edit-buffer)))
444 (switch-to-buffer (make-indirect-buffer (file-name-nondirectory todo-file-do)
445 buffer-name))
446 (message "To exit, simply kill this buffer and return to list.")
447 (todo-edit-mode)
448 (narrow-to-region (todo-item-start) (todo-item-end))))
449
450 (defun todo-add-category (cat) "Add a new category to the TODO list."
451 (interactive)
452 (save-window-excursion
453 (setq todo-categories (cons cat todo-categories))
454 (find-file todo-file-do)
455 (widen)
456 (goto-char (point-min))
457 (let ((posn (search-forward "-*- mode: todo; " 17 t)))
458 (if (not (null posn)) (goto-char posn))
459 (if (equal posn nil)
460 (progn
461 (insert "-*- mode: todo; \n")
462 (forward-char -1))
463 (kill-line)))
464 (insert (format "todo-categories: %S; -*-" todo-categories))
465 (forward-char 1)
466 (insert (format "%s --- %s\n--- End\n%s %s\n"
467 todo-prefix cat todo-prefix (make-string 75 ?-))))
468 0)
469
470 (defun todo-insert-item ()
471 "Insert new TODO list entry."
472 (interactive)
473 (let* ((new-item (concat todo-prefix " "
474 (read-from-minibuffer "New TODO entry: ")))
475 (categories todo-categories)
476 (history (cons 'categories (1+ todo-category-number)))
477 (category (completing-read "Category: "
478 (todo-category-alist) nil nil
479 (nth todo-category-number todo-categories)
480 history)))
481 (let ((cat-exists (member category todo-categories)))
482 (setq todo-category-number
483 (if cat-exists
484 (- (length todo-categories) (length cat-exists))
485 (todo-add-category category))))
486 (todo-show)
487 (setq todo-previous-line 0)
488 (let ((top 1)
489 (bottom (1+ (count-lines (point-min) (point-max)))))
490 (while (> (- bottom top) todo-insert-threshold)
491 (let* ((current (/ (+ top bottom) 2))
492 (answer (if (< current bottom)
493 (todo-more-important-p current) nil)))
494 (if answer
495 (setq bottom current)
496 (setq top (1+ current)))))
497 (setq top (/ (+ top bottom) 2))
498 ;; goto-line doesn't have the desired behavior in a narrowed buffer
499 (goto-char (point-min))
500 (forward-line (1- top)))
501 (insert new-item "\n")
502 (todo-backward-item)
503 (save-buffer)
504 (message "")))
505 (defalias 'todo-cmd-inst 'todo-insert-item)
506
507 (defun todo-more-important-p (line)
508 "Ask whether entry is more important than the one at LINE."
509 (if (not (equal todo-previous-line line))
510 (progn
511 (setq todo-previous-line line)
512 (goto-char (point-min))
513 (forward-line (1- todo-previous-line))
514 (let ((item (todo-item-string-start)))
515 (setq todo-previous-answer
516 (y-or-n-p (concat "More important than '" item "'? "))))))
517 todo-previous-answer)
518 (defalias 'todo-ask-p 'todo-more-important-p)
519
520 (defun todo-delete-item () "Delete current TODO list entry."
521 (interactive)
522 (if (> (count-lines (point-min) (point-max)) 0)
523 (let* ((todo-entry (todo-item-string-start))
524 (todo-answer (y-or-n-p (concat "Permanently remove '"
525 todo-entry "'? "))))
526 (if todo-answer
527 (progn
528 (todo-remove-item)
529 (todo-backward-item)))
530 (message ""))
531 (error "No TODO list entry to delete")))
532 (defalias 'todo-cmd-kill 'todo-delete-item)
533
534 (defun todo-raise-item () "Raise priority of current entry."
535 (interactive)
536 (if (> (count-lines (point-min) (point)) 0)
537 (let ((item (todo-item-string)))
538 (todo-remove-item)
539 (todo-backward-item)
540 (save-excursion
541 (insert item "\n"))
542 (message ""))
543 (error "No TODO list entry to raise")))
544 (defalias 'todo-cmd-rais 'todo-raise-item)
545
546 (defun todo-lower-item () "Lower priority of current entry."
547 (interactive)
548 (if (> (count-lines (point) (point-max)) 1) ; Assume there is a final newline
549 (let ((item (todo-item-string)))
550 (todo-remove-item)
551 (todo-forward-item)
552 (save-excursion
553 (insert item "\n"))
554 (message ""))
555 (error "No TODO list entry to lower")))
556 (defalias 'todo-cmd-lowr 'todo-lower-item)
557
558 (defun todo-file-item () "File the current TODO list entry away."
559 (interactive)
560 (if (> (count-lines (point-min) (point-max)) 0)
561 (let ((comment (read-from-minibuffer "Comment: "))
562 (time-stamp-format todo-time-string-format))
563 (if (> (length comment) 0)
564 (progn
565 (goto-char (todo-item-end))
566 (insert (if (save-excursion (beginning-of-line)
567 (looking-at (regexp-quote todo-prefix)))
568 " "
569 "\n\t")
570 "(" (nth todo-category-number todo-categories) ": "
571 comment ")\n")))
572 (goto-char (todo-item-start))
573 (let ((temp-point (point)))
574 (if (looking-at (regexp-quote todo-prefix))
575 (replace-match (time-stamp-string)) ; Standard prefix -> timestamp
576 ;; Else prefix non-standard item start with timestamp
577 (insert (time-stamp-string)))
578 (append-to-file temp-point (todo-item-end) todo-file-done)
579 (delete-region temp-point (1+ (todo-item-end))))
580 (todo-backward-item)
581 (message ""))
582 (error "No TODO list entry to file away")))
583
584 ;; ---------------------------------------------------------------------------
585
586 ;; Utility functions:
587
588 (defun todo-line-string () "Return current line in buffer as a string."
589 (buffer-substring (point-at-bol) (point-at-eol)))
590
591 (defun todo-item-string-start ()
592 "Return the start of this TODO list entry as a string."
593 ;; Suitable for putting in the minibuffer when asking the user
594 (let ((item (todo-item-string)))
595 (if (> (length item) 60)
596 (setq item (concat (substring item 0 56) "...")))
597 item))
598
599 (defun todo-item-start () "Return point at start of current TODO list item."
600 (save-excursion
601 (beginning-of-line)
602 (if (not (looking-at (regexp-quote todo-prefix)))
603 (search-backward-regexp
604 (concat "^" (regexp-quote todo-prefix)) nil t))
605 (point)))
606
607 (defun todo-item-end () "Return point at end of current TODO list item."
608 (save-excursion
609 (end-of-line)
610 (search-forward-regexp (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
611 (1- (point-at-bol))))
612
613 (defun todo-remove-item () "Delete the current entry from the TODO list."
614 (delete-region (todo-item-start) (1+ (todo-item-end))))
615
616 (defun todo-item-string () "Return current TODO list entry as a string."
617 (buffer-substring (todo-item-start) (todo-item-end)))
618
619 (defun todo-string-count-lines (string)
620 "Return the number of lines STRING spans."
621 (length (split-string string "\n")))
622
623 (defun todo-string-multiline-p (string)
624 "Returns non-nil if STRING spans several lines"
625 (> (todo-string-count-lines string) 1))
626
627 (defun todo-category-alist ()
628 "Generate an alist fro use in `completing-read' from `todo-categories'"
629 (mapcar (lambda (cat) (cons cat nil))
630 todo-categories))
631
632 ;; utility functions: These are available in XEmacs, but not in Emacs 19.34
633
634 (if (not (fboundp 'point-at-bol))
635 (defun point-at-bol () "Return value of point at beginning of line."
636 (save-excursion
637 (beginning-of-line)
638 (point))))
639
640 (if (not (fboundp 'point-at-eol))
641 (defun point-at-eol () "Return value of point at end of line."
642 (save-excursion
643 (end-of-line)
644 (point))))
645
646 ;; splits at a white space, returns a list
647 (if (not (fboundp 'split-string))
648 (defun split-string (string &optional separators)
649 "Splits STRING into substrings where there are matches for SEPARATORS.
650 Each match for SEPARATORS is a splitting point.
651 The substrings between the splitting points are made into a list
652 which is returned.
653 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
654 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
655 (start 0)
656 (list nil))
657 (while (string-match rexp string start)
658 (or (eq (match-beginning 0) 0)
659 (setq list
660 (cons (substring string start (match-beginning 0))
661 list)))
662 (setq start (match-end 0)))
663 (or (eq start (length string))
664 (setq list
665 (cons (substring string start)
666 list)))
667 (nreverse list))))
668
669 ;; ---------------------------------------------------------------------------
670
671 (easy-menu-define todo-menu todo-mode-map "Todo Menu"
672 '("Todo"
673 ["Next category" todo-forward-category t]
674 ["Previous category" todo-backward-category t]
675 "---"
676 ["Edit item" todo-edit-item t]
677 ["File item" todo-file-item t]
678 ["Insert new item" todo-insert-item t]
679 ["Kill item" todo-delete-item t]
680 "---"
681 ["Lower item priority" todo-lower-item t]
682 ["Raise item priority" todo-raise-item t]
683 "---"
684 ["Next item" todo-forward-item t]
685 ["Previous item" todo-backward-item t]
686 "---"
687 ["Save" todo-save t]
688 "---"
689 ["Quit" todo-quit t]
690 ))
691
692 (defun todo-mode () "Major mode for editing TODO lists.\n\n\\{todo-mode-map}"
693 (interactive)
694 (setq major-mode 'todo-mode)
695 (setq mode-name "TODO")
696 (use-local-map todo-mode-map)
697 (easy-menu-add todo-menu)
698 (run-hooks 'todo-mode-hook))
699
700 (defun todo-edit-mode ()
701 "Major mode for editing items in the TODO list\n\n\\{todo-edit-mode-map}"
702 (text-mode)
703 (setq major-mode 'todo-edit-mode)
704 (setq mode-name "TODO Edit")
705 (run-hooks 'todo-edit-mode-hook))
706
707 (defun todo-show () "Show TODO list."
708 (interactive)
709 (if (file-exists-p todo-file-do)
710 (find-file todo-file-do)
711 (todo-initial-setup))
712 (if (null todo-categories)
713 (if (null todo-cats)
714 (error "Error in %s: No categories in list `todo-categories'"
715 todo-file-do)
716 (goto-char (point-min))
717 (and (search-forward "todo-cats:" nil t)
718 (replace-match "todo-categories:"))
719 (make-local-variable todo-categories)
720 (setq todo-categories todo-cats)))
721 (beginning-of-line)
722 (todo-category-select))
723
724 (defun todo-initial-setup () "Set up things to work properly in TODO mode."
725 (find-file todo-file-do)
726 (erase-buffer)
727 (todo-mode)
728 (todo-add-category "Todo"))
729
730 (provide 'todo-mode)
731
732 ;; ---------------------------------------------------------------------------
733 ;;; todo-mode.el ends here
734 ;; ---------------------------------------------------------------------------