todo-mode.el 10.9 KB
Newer Older
1
; todomode.el -- major mode for editing TODO list files
Oliver Seidel's avatar
Oliver Seidel committed
2 3 4 5 6 7 8

;; ---------------------------------------------------------------------------

;; Note: You may copy this file freely for non-commercial use; otherwise,
;;       please contact   (address) O Seidel, Lessingstr 8, Eschborn, FRG
;;                        (e-mail ) Oliver.Seidel@cl.cam.ac.uk (2 Aug 1997)

9
;; $Id: todomode.el,v 1.4 1997/08/04 16:18:45 os10000 Exp os10000 $
Oliver Seidel's avatar
Oliver Seidel committed
10
;;
Oliver Seidel's avatar
Oliver Seidel committed
11
;; $Log: todomode.el,v $
12 13 14
;; Revision 1.4  1997/08/04  16:18:45  os10000
;; Added Raise/Lower item.
;;
Oliver Seidel's avatar
Oliver Seidel committed
15 16 17
;; Revision 1.3  1997/08/03  12:47:26  os10000
;; Cleaned up variables, prefix and cursor position.
;;
18 19 20
;; Revision 1.2  1997/08/03 12:15:28  os10000
;; It appears to work.
;;
Oliver Seidel's avatar
Oliver Seidel committed
21 22
;; Revision 1.1  1997/08/03 12:15:13  os10000
;; Initial revision
Oliver Seidel's avatar
Oliver Seidel committed
23 24 25 26
;;

;; ---------------------------------------------------------------------------

27 28 29 30 31 32 33 34
;; Description:
;;
;; To get this to work, make emacs execute the line "(require 'todomode)"
;; and maybe initialise the variables below on startup.
;;
;; Then you have the following facilities available:
;;
;; M-x todo-mode              will enter the todo list screen, here type
35 36
;; +                          to go to next category
;; -                          to go to previous category
37
;; e                          to edit the current entry
38 39
;; f                          to file the current entry, including a
;;                                                 comment and timestamp
40 41
;; i                          to insert a new entry
;; k                          to kill the current entry
Oliver Seidel's avatar
Oliver Seidel committed
42
;; l                          lower the current entry's priority
43 44 45 46 47
;; n                          for the next entry
;; p                          for the previous entry
;; q                          to save the list and exit the buffer
;; r                          raise current entryk's priority
;; s                          to save the list
48 49 50
;;
;; I would recommend to add the following bindings to your global keymap:
;;
51
;; (global-set-key "\C-ct" 'todo-show)
52 53 54 55 56 57
;; (global-set-key "\C-ci" 'todo-cmd-inst)
;;
;; This will enable you to quickly find the todo-list, or to simply add an
;; entry, without changing to it and getting sidetracked from your current
;; project.
;;
58 59 60
;; I would also recommend that use the prefix "*/*" (by leaving the
;; variable 'todo-prefix' untouched) so that the diary displays
;; each entry every day.
61 62 63

;; ---------------------------------------------------------------------------

Oliver Seidel's avatar
Oliver Seidel committed
64 65
;; User-configurable variables:

66
(defvar todo-prefix "*/*" "TODO mode prefix when creating entries")
67
(defvar todo-file-do "~/.todo-do" "TODO mode filename of list file")
Oliver Seidel's avatar
Oliver Seidel committed
68 69 70 71 72 73 74
(defvar todo-file-done "~/.todo-done" "TODO mode filename of archive file")
(defvar todo-mode-hook nil "Hooks invoked when the *TODO* buffer is created.")

;; ---------------------------------------------------------------------------

(require 'time-stamp)

75 76 77
(defvar todo-mode-map (make-sparse-keymap) "TODO mode keymap.  See `todo-mode'")
(define-key todo-mode-map "+" 'todo-cmd-forw)
(define-key todo-mode-map "-" 'todo-cmd-back)
Oliver Seidel's avatar
Oliver Seidel committed
78
(define-key todo-mode-map "e" 'todo-cmd-edit)
79
(define-key todo-mode-map "f" 'todo-cmd-file)
Oliver Seidel's avatar
Oliver Seidel committed
80 81
(define-key todo-mode-map "i" 'todo-cmd-inst)
(define-key todo-mode-map "k" 'todo-cmd-kill)
Oliver Seidel's avatar
Oliver Seidel committed
82
(define-key todo-mode-map "l" 'todo-cmd-lowr)
83 84 85 86 87
(define-key todo-mode-map "n" 'todo-cmd-next)
(define-key todo-mode-map "p" 'todo-cmd-prev)
(define-key todo-mode-map "q" 'todo-cmd-done)
(define-key todo-mode-map "r" 'todo-cmd-rais)
(define-key todo-mode-map "s" 'todo-cmd-save)
Oliver Seidel's avatar
Oliver Seidel committed
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
(defun todo-cat-slct ()
  (let ((todo-category-name (nth todo-category-number todo-cats)))
    (setq mode-line-buffer-identification (concat "Category: " todo-category-name))
    (widen)
    (goto-char (point-min))
    (search-forward (concat "--- " todo-category-name))
    (setq begin (+ (point-at-eol) 1))
    (search-forward "--- End")
    (narrow-to-region begin (point-at-bol))
    (goto-char (point-min))
    )
)

(defun todo-cmd-forw () "Go forward to TODO list of next category."
  (interactive)
  (let ((todo-cat-cnt (- (length todo-cats) 1)))
    (setq todo-category-number (if (< todo-category-number todo-cat-cnt)
				   (+ todo-category-number 1) 0))
    (todo-cat-slct)
    )
  )

(defun todo-cmd-back () "Go back to TODO list of previous category."
  (interactive)
  (let ((todo-cat-cnt (- (length todo-cats) 1)))
    (setq todo-category-number (if (> todo-category-number 0)
				   (- todo-category-number 1) todo-cat-cnt))
    (todo-cat-slct)
    )
  )

(defun todo-cmd-prev () "Select previous entry of TODO list."
Oliver Seidel's avatar
Oliver Seidel committed
121 122 123 124 125 126
  (interactive)
  (forward-line -1)
  (beginning-of-line nil)
  (message "")
  )

127
(defun todo-cmd-next () "Select next entry of TODO list."
Oliver Seidel's avatar
Oliver Seidel committed
128 129 130 131 132 133
  (interactive)
  (forward-line 1)
  (beginning-of-line nil)
  (message "")
  )

134 135 136 137 138 139
(defun todo-cmd-save () "Save the TODO list."
  (interactive)
  (save-buffer)
  )

(defun todo-cmd-done () "Done with TODO list for now."
Oliver Seidel's avatar
Oliver Seidel committed
140
  (interactive)
141 142
  (widen)
  (save-buffer)
143 144
  (beginning-of-line nil)
  (message "")
Oliver Seidel's avatar
Oliver Seidel committed
145 146 147
  (bury-buffer)
  )

148
(defun todo-line () "Find current line in buffer."  (buffer-substring (point-at-bol) (point-at-eol)))
Oliver Seidel's avatar
Oliver Seidel committed
149

150
(defun todo-cmd-edit () "Edit current TODO list entry."
Oliver Seidel's avatar
Oliver Seidel committed
151
  (interactive)
152 153 154 155 156 157
  (let ((todo-entry (todo-line)))
    (delete-region (point-at-bol) (point-at-eol))
    (insert (read-from-minibuffer "Edit: " todo-entry))
    (beginning-of-line nil)
    (message "")
    )
Oliver Seidel's avatar
Oliver Seidel committed
158 159 160 161 162 163
  )

(defvar todo-prv-lne 0 "previous line that I asked about.")
(defvar todo-prv-ans 0 "previous answer that I got.")

(defun todo-ask (lne) "Ask whether entry is more important than at LNE."
164
  (if (not (equal todo-prv-lne lne))
Oliver Seidel's avatar
Oliver Seidel committed
165 166 167 168 169 170 171 172 173
      (progn
	(setq todo-prv-lne lne)
	(goto-line todo-prv-lne)
	(setq todo-prv-ans (y-or-n-p (concat "More important than '" (todo-line) "'? ")))
	)
    )
  todo-prv-ans
  )

174
(defun todo-add-category (cat) "Add a new category to the TODO list."
Oliver Seidel's avatar
Oliver Seidel committed
175 176
  (interactive)
  (save-window-excursion
177
    (setq todo-cats (cons cat todo-cats))
Oliver Seidel's avatar
Oliver Seidel committed
178
    (find-file todo-file-do)
179 180 181 182 183
    (widen)
    (goto-char (point-min))
    (let ((posn (search-forward "-*- mode: todo; " 17 t)))
      (if (not (null posn)) (goto-char posn))
      (if (equal posn nil) (progn (insert "-*- mode: todo; \n") (forward-char -1)) (kill-line))
Oliver Seidel's avatar
Oliver Seidel committed
184
      )
185 186 187
    (insert (format "todo-cats: %S; -*-" todo-cats))
    (forward-char 1)
    (insert (format "%s --- %s\n--- End\n%s %s\n" todo-prefix cat todo-prefix (make-string 75 ?-)))
Oliver Seidel's avatar
Oliver Seidel committed
188
    )
189 190 191 192 193
  0
  )

(defun todo-cmd-inst () "Insert new TODO list entry."
  (interactive)
Oliver Seidel's avatar
Oliver Seidel committed
194
  (beginning-of-line nil)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  (let* ((todo-entry (concat todo-prefix " " (read-from-minibuffer "New TODO entry: ")))
	 (temp-catgs todo-cats)
	 (todo-hstry (cons 'temp-cats (+ todo-category-number 1))))
    (save-window-excursion
      (setq todo-category
	    (read-from-minibuffer "Category: " (nth todo-category-number todo-cats) nil nil todo-hstry))
      (let* ((ltrgt todo-category)
	     (lnmbr 0)
	     (ltext (car todo-cats))
	     (lrest (cdr todo-cats)))
	(setq ltext (car todo-cats))
	(while (not (or (null lrest) (string-equal ltext ltrgt)))
	  (setq ltext (car lrest))
	  (setq lrest (cdr lrest))
	  (setq lnmbr (+ 1 lnmbr))
	  )
	(setq todo-category-number
	      (if (string-equal ltext todo-category) lnmbr (todo-add-category todo-category)))
	)
      (todo-show)
      (setq todo-prv-lne 0)
      (let* ((todo-fst 1)
	     (todo-lst (+ 1 (count-lines (point-min) (point-max)))))
	(while (< todo-fst todo-lst)
	  (let* ((todo-cur (/ (+ todo-fst todo-lst) 2))
		 (todo-ans (if (< todo-cur todo-lst) (todo-ask todo-cur) nil)))
	    (if todo-ans
		(setq todo-lst todo-cur)
	      (setq todo-fst (+ todo-cur 1)))
	    )
	  )
	(goto-line todo-fst)
	)
      (insert (concat todo-entry "\n"))
      (forward-line -1)
      )
    (beginning-of-line nil)
    (message "")
    )
Oliver Seidel's avatar
Oliver Seidel committed
234 235
  )

236
(defun todo-cmd-kill () "Delete current TODO list entry."
Oliver Seidel's avatar
Oliver Seidel committed
237 238 239
  (interactive)
  (if (> (count-lines (point-min) (point-max)) 0)
      (progn
240 241 242 243 244 245 246 247 248 249
	(let* ((todo-entry (todo-line))
	       (todo-answer (y-or-n-p (concat "Permanently remove '" todo-entry "'? "))))
	  (if todo-answer
	      (progn
		(delete-region (point-at-bol) (+ 1 (point-at-eol))) 
		(forward-line -1)
		)
	    )
	  )
	(message "")
Oliver Seidel's avatar
Oliver Seidel committed
250
	)
251
    (message "No TODO list entry to delete.")
Oliver Seidel's avatar
Oliver Seidel committed
252 253 254 255
    )
  (beginning-of-line nil)
  )

Oliver Seidel's avatar
Oliver Seidel committed
256 257 258 259 260
(defun todo-cmd-rais () "Raise priority of current entry."
  (interactive)
  (if (> (count-lines (point-min) (point-max)) 0)
      (progn
	(setq todo-entry (todo-line))
261
	(delete-region (point-at-bol) (+ 1 (point-at-eol))) 
Oliver Seidel's avatar
Oliver Seidel committed
262 263
	(forward-line -1)
	(insert (concat todo-entry "\n"))
264 265
	(forward-line -1)
	(message "")
Oliver Seidel's avatar
Oliver Seidel committed
266
	)
267
    (message "No TODO list entry to raise.")
Oliver Seidel's avatar
Oliver Seidel committed
268 269 270 271 272 273 274 275 276
    )
  (beginning-of-line nil)
  )

(defun todo-cmd-lowr () "Lower priority of current entry."
  (interactive)
  (if (> (count-lines (point-min) (point-max)) 0)
      (progn
	(setq todo-entry (todo-line))
277
	(delete-region (point-at-bol) (+ 1 (point-at-eol))) 
Oliver Seidel's avatar
Oliver Seidel committed
278 279
	(forward-line 1)
	(insert (concat todo-entry "\n"))
280 281
	(forward-line -1)
	(message "")
Oliver Seidel's avatar
Oliver Seidel committed
282
	)
283
    (message "No TODO list entry to raise.")
Oliver Seidel's avatar
Oliver Seidel committed
284 285 286 287
    )
  (beginning-of-line nil)
  )

288
(defun todo-cmd-file () "File away the current TODO list entry."
Oliver Seidel's avatar
Oliver Seidel committed
289 290 291
  (interactive)
  (if (> (count-lines (point-min) (point-max)) 0)
      (progn
292 293 294 295 296 297 298 299 300 301
	(let ((time-stamp-format "%3b %2d, %y, %02I:%02M%p"))
	  (beginning-of-line nil)
	  (insert (time-stamp-string))
	  (end-of-line nil)
	  (insert (concat " (" (read-fromminibuffer "Comment: ") ")"))
	  (append-to-file (point-at-bol) (+ 1 (point-at-eol)) todo-file-done)
	  (delete-region (point-at-bol) (+ 1 (point-at-eol)))
	  (forward-line -1)
	  )
	(message "")
Oliver Seidel's avatar
Oliver Seidel committed
302
	)
303
    (message "No TODO list entry to delete.")
Oliver Seidel's avatar
Oliver Seidel committed
304 305 306 307 308 309
    )
  (beginning-of-line nil)
  )

;; ---------------------------------------------------------------------------

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
;; utility functions:  These are available in XEmacs, but not in Emacs 19.34

(if (not (fboundp 'point-at-bol))
    (defun point-at-bol ()
      (save-excursion
	(beginning-of-line)
	(point))))

(if (not (fboundp 'point-at-eol))
    (defun point-at-eol ()
      (save-excursion
	(end-of-line)
	(point))))

;; ---------------------------------------------------------------------------

(defvar todo-cats nil "TODO categories.")
(defvar todo-category-number 0 "TODO category number.")

(defun todo-mode () "Major mode for editing TODO lists.\n\n\\{todo-mode-map}"
Oliver Seidel's avatar
Oliver Seidel committed
330 331 332 333
  (interactive)
  (setq major-mode 'todo-mode)
  (setq mode-name "TODO")
  (use-local-map todo-mode-map)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  (run-hooks 'todo-mode-hook)
  )

(defun todo-show () "Show TODO list."
  (interactive)
  (find-file todo-file-do)
  (if (null todo-cats)
      (progn
	(todo-add-category "Todo")
	(goto-char (point-min))
	(goto-char (search-forward "--- End"))
	(let ((bol (point-at-bol)))
	  (forward-line 1)
	  (let* ((eol (+ (point-at-eol) 1))
		 (mrkr (buffer-substring bol eol)))
	    (delete-region bol eol)
	    (goto-char (point-max))
	    (insert mrkr)
	    )
	  )
	(save-buffer)
	(kill-buffer (current-buffer))
	(find-file todo-file-do)
	)
    )
359
  (beginning-of-line nil)
360 361
  (todo-cat-slct)
  )
Oliver Seidel's avatar
Oliver Seidel committed
362 363 364 365 366

(provide 'todomode)

;; ---------------------------------------------------------------------------

367
;; todomode.el ends here
Oliver Seidel's avatar
Oliver Seidel committed
368 369

;; ---------------------------------------------------------------------------