todo-mode.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. ;;; todo-mode.el --- major mode for editing TODO list files
  2. ;; Copyright (C) 1997, 1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Oliver Seidel <privat@os10000.net>
  4. ;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
  5. ;; Created: 2 Aug 1997
  6. ;; Keywords: calendar, todo
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;; ---------------------------------------------------------------------------
  19. ;;; Commentary:
  20. ;; Mode Description
  21. ;;
  22. ;; TODO is a major mode for EMACS which offers functionality to
  23. ;; treat most lines in one buffer as a list of items one has to
  24. ;; do. There are facilities to add new items, which are
  25. ;; categorized, to edit or even delete items from the buffer.
  26. ;; The buffer contents are currently compatible with the diary,
  27. ;; so that the list of todo-items will show up in the FANCY diary
  28. ;; mode.
  29. ;;
  30. ;; Notice: Besides the major mode, this file also exports the
  31. ;; function `todo-show' which will change to the one specific
  32. ;; TODO file that has been specified in the todo-file-do
  33. ;; variable. If this file does not conform to the TODO mode
  34. ;; conventions, the todo-show function will add the appropriate
  35. ;; header and footer. I don't anticipate this to cause much
  36. ;; grief, but be warned, in case you attempt to read a plain text
  37. ;; file.
  38. ;;
  39. ;; Preface, Quickstart Installation
  40. ;;
  41. ;; To get this to work, make Emacs execute the line
  42. ;;
  43. ;; (autoload 'todo-mode "todo-mode"
  44. ;; "Major mode for editing TODO lists." t)
  45. ;; (autoload 'todo-show "todo-mode"
  46. ;; "Show TODO items." t)
  47. ;; (autoload 'todo-insert-item "todo-mode"
  48. ;; "Add TODO item." t)
  49. ;;
  50. ;; You may now enter new items by typing "M-x todo-insert-item",
  51. ;; or enter your TODO list file by typing "M-x todo-show".
  52. ;;
  53. ;; The TODO list file has a special format and some auxiliary
  54. ;; information, which will be added by the todo-show function if
  55. ;; it attempts to visit an un-initialized file. Hence it is
  56. ;; recommended to use the todo-show function for the first time,
  57. ;; in order to initialize the file, but it is not necessary
  58. ;; afterwards.
  59. ;;
  60. ;; As these commands are quite long to type, I would recommend
  61. ;; the addition of two bindings to your to your global keymap. I
  62. ;; personally have the following in my initialization file:
  63. ;;
  64. ;; (global-set-key "\C-ct" 'todo-show) ; switch to TODO buffer
  65. ;; (global-set-key "\C-ci" 'todo-insert-item) ; insert new item
  66. ;;
  67. ;; Note, however, that this recommendation has prompted some
  68. ;; criticism, since the keys C-c LETTER are reserved for user
  69. ;; functions. I believe my recommendation is acceptable, since
  70. ;; the Emacs Lisp Manual *Tips* section also details that the
  71. ;; mode itself should not bind any functions to those keys. The
  72. ;; express aim of the above two bindings is to work outside the
  73. ;; mode, which doesn't need the show function and offers a
  74. ;; different binding for the insert function. They serve as
  75. ;; shortcuts and are not even needed (since the TODO mode will be
  76. ;; entered by visiting the TODO file, and later by switching to
  77. ;; its buffer).
  78. ;;
  79. ;; If you are an advanced user of this package, please consult
  80. ;; the whole source code for autoloads, because there are several
  81. ;; extensions that are not explicitly listed in the above quick
  82. ;; installation.
  83. ;;
  84. ;; Pre-Requisites
  85. ;;
  86. ;; This package will require the following packages to be
  87. ;; available on the load-path:
  88. ;;
  89. ;; time-stamp
  90. ;; easymenu
  91. ;;
  92. ;; Operation
  93. ;;
  94. ;; You will have the following facilities available:
  95. ;;
  96. ;; M-x todo-show will enter the todo list screen, here type
  97. ;;
  98. ;; + to go to next category
  99. ;; - to go to previous category
  100. ;; d to file the current entry, including a
  101. ;; comment and timestamp
  102. ;; e to edit the current entry
  103. ;; E to edit a multi-line entry
  104. ;; f to file the current entry, including a
  105. ;; comment and timestamp
  106. ;; i to insert a new entry, with prefix, omit category
  107. ;; I to insert a new entry at current cursor position
  108. ;; j jump to category
  109. ;; k to kill the current entry
  110. ;; l to lower the current entry's priority
  111. ;; n for the next entry
  112. ;; p for the previous entry
  113. ;; P print
  114. ;; q to save the list and exit the buffer
  115. ;; r to raise the current entry's priority
  116. ;; s to save the list
  117. ;; S to save the list of top priorities
  118. ;; t show top priority items for each category
  119. ;;
  120. ;; When you add a new entry, you are asked for the text and then
  121. ;; for the category. I for example have categories for things
  122. ;; that I want to do in the office (like mail my mum), that I
  123. ;; want to do in town (like buy cornflakes) and things I want to
  124. ;; do at home (move my suitcases). The categories can be
  125. ;; selected with the cursor keys and if you type in the name of a
  126. ;; category which didn't exist before, an empty category of the
  127. ;; desired name will be added and filled with the new entry.
  128. ;;
  129. ;; Configuration
  130. ;;
  131. ;; Variable todo-prefix
  132. ;;
  133. ;; I would like to recommend that you use the prefix "*/*" (by
  134. ;; leaving the variable 'todo-prefix' untouched) so that the
  135. ;; diary displays each entry every day.
  136. ;;
  137. ;; To understand what I mean, please read the documentation that
  138. ;; goes with the calendar since that will tell you how you can
  139. ;; set up the fancy diary display and use the #include command to
  140. ;; include your todo list file as part of your diary.
  141. ;;
  142. ;; If you have the diary package set up to usually display more
  143. ;; than one day's entries at once, consider using
  144. ;;
  145. ;; "&%%(equal (calendar-current-date) date)"
  146. ;;
  147. ;; as the value of `todo-prefix'. Please note that this may slow
  148. ;; down the processing of your diary file some.
  149. ;;
  150. ;; Carsten Dominik <dominik@strw.LeidenUniv.nl> suggested that
  151. ;;
  152. ;; "&%%(todo-cp)"
  153. ;;
  154. ;; might be nicer and to that effect a function has been declared
  155. ;; further down in the code. You may wish to auto-load this.
  156. ;;
  157. ;; Carsten also writes that that *changing* the prefix after the
  158. ;; todo list is already established is not as simple as changing
  159. ;; the variable - the todo files have to be changed by hand.
  160. ;;
  161. ;; Variable todo-file-do
  162. ;;
  163. ;; This variable is fairly self-explanatory. You have to store
  164. ;; your TODO list somewhere. This variable tells the package
  165. ;; where to go and find this file.
  166. ;;
  167. ;; Variable todo-file-done
  168. ;;
  169. ;; Even when you're done, you may wish to retain the entries.
  170. ;; Given that they're timestamped and you are offered to add a
  171. ;; comment, this can make a useful diary of past events. It will
  172. ;; even blend in with the EMACS diary package. So anyway, this
  173. ;; variable holds the name of the file for the filed todo-items.
  174. ;;
  175. ;; Variable todo-file-top
  176. ;;
  177. ;; File storing the top priorities of your TODO list when
  178. ;; todo-save-top-priorities is non-nil. Nice to include in your
  179. ;; diary instead of the complete TODO list.
  180. ;;
  181. ;; Variable todo-mode-hook
  182. ;;
  183. ;; Just like other modes, too, this mode offers to call your
  184. ;; functions before it goes about its business. This variable
  185. ;; will be inspected for any functions you may wish to have
  186. ;; called once the other TODO mode preparations have been
  187. ;; completed.
  188. ;;
  189. ;; Variable todo-insert-threshold
  190. ;;
  191. ;; Another nifty feature is the insertion accuracy. If you have
  192. ;; 8 items in your TODO list, then you may get asked 4 questions
  193. ;; by the binary insertion algorithm. However, you may not
  194. ;; really have a need for such accurate priorities amongst your
  195. ;; TODO items. If you now think about the binary insertion
  196. ;; halving the size of the window each time, then the threshold
  197. ;; is the window size at which it will stop. If you set the
  198. ;; threshold to zero, the upper and lower bound will coincide at
  199. ;; the end of the loop and you will insert your item just before
  200. ;; that point. If you set the threshold to, e.g. 8, it will stop
  201. ;; as soon as the window size drops below that amount and will
  202. ;; insert the item in the approximate center of that window. I
  203. ;; got the idea for this feature after reading a very helpful
  204. ;; e-mail reply from Trey Jackson <trey@cs.berkeley.edu> who
  205. ;; corrected some of my awful coding and pointed me towards some
  206. ;; good reading. Thanks Trey!
  207. ;;
  208. ;; Things to do
  209. ;;
  210. ;; These originally were my ideas, but now also include all the
  211. ;; suggestions that I included before forgetting them:
  212. ;;
  213. ;; o Fancy fonts for todo/top-priority buffer
  214. ;; o Remove todo-prefix option in todo-top-priorities
  215. ;; o Rename category
  216. ;; o Move entry from one category to another one
  217. ;; o Entries which both have the generic */* prefix and a
  218. ;; "deadline" entry which are understood by diary, indicating
  219. ;; an event (unless marked by &)
  220. ;; o The optional COUNT variable of todo-forward-item should be
  221. ;; applied to the other functions performing similar tasks
  222. ;; o Modularization could be done for repeated elements of
  223. ;; the code, like the completing-read lines of code.
  224. ;; o license / version function
  225. ;; o export to diary file
  226. ;; o todo-report-bug
  227. ;; o GNATS support
  228. ;; o elide multiline (as in bbdb, or, to a lesser degree, in
  229. ;; outline mode)
  230. ;; o rewrite complete package to store data as Lisp objects
  231. ;; and have display modes for display, for diary export,
  232. ;; etc. (Richard Stallman pointed out this is a bad idea)
  233. ;; o so base todo-mode.el on generic-mode.el instead
  234. ;;
  235. ;; History and Gossip
  236. ;;
  237. ;; Many thanks to all the ones who have contributed to the
  238. ;; evolution of this package! I hope I have listed all of you
  239. ;; somewhere in the documentation or at least in the RCS history!
  240. ;;
  241. ;; Enjoy this package and express your gratitude by sending nice
  242. ;; things to my parents' address!
  243. ;;
  244. ;; Oliver Seidel
  245. ;; (Lessingstr. 8, 65760 Eschborn, Federal Republic of Germany)
  246. ;;; Code:
  247. (require 'time-stamp)
  248. ;; User-configurable variables:
  249. (defgroup todo nil
  250. "Maintain a list of todo items."
  251. :link '(emacs-commentary-link "todo-mode")
  252. :version "21.1"
  253. :group 'calendar)
  254. (defcustom todo-prefix "*/*"
  255. "TODO mode prefix for entries.
  256. This is useful in conjunction with `calendar' and `diary' if you use
  257. #include \"~/.todo-do\"
  258. in your diary file to include your todo list file as part of your
  259. diary. With the default value \"*/*\" the diary displays each entry
  260. every day and it may also be marked on every day of the calendar.
  261. Using \"&%%(equal (calendar-current-date) date)\" instead will only
  262. show and mark todo entries for today, but may slow down processing of
  263. the diary file somewhat."
  264. :type 'string
  265. :group 'todo)
  266. (defcustom todo-file-do (convert-standard-filename "~/.todo-do")
  267. "TODO mode list file."
  268. :type 'file
  269. :group 'todo)
  270. (defcustom todo-file-done (convert-standard-filename "~/.todo-done")
  271. "TODO mode archive file."
  272. :type 'file
  273. :group 'todo)
  274. (defcustom todo-mode-hook nil
  275. "TODO mode hooks."
  276. :type 'hook
  277. :group 'todo)
  278. (defcustom todo-edit-mode-hook nil
  279. "TODO Edit mode hooks."
  280. :type 'hook
  281. :group 'todo)
  282. (defcustom todo-insert-threshold 0
  283. "TODO mode insertion accuracy.
  284. If you have 8 items in your TODO list, then you may get asked 4
  285. questions by the binary insertion algorithm. However, you may not
  286. really have a need for such accurate priorities amongst your TODO
  287. items. If you now think about the binary insertion halving the size
  288. of the window each time, then the threshold is the window size at
  289. which it will stop. If you set the threshold to zero, the upper and
  290. lower bound will coincide at the end of the loop and you will insert
  291. your item just before that point. If you set the threshold to,
  292. e.g. 8, it will stop as soon as the window size drops below that
  293. amount and will insert the item in the approximate center of that
  294. window."
  295. :type 'integer
  296. :group 'todo)
  297. (defvar todo-edit-buffer " *TODO Edit*"
  298. "TODO Edit buffer name.")
  299. (defcustom todo-file-top (convert-standard-filename "~/.todo-top")
  300. "TODO mode top priorities file.
  301. Not in TODO format, but diary compatible.
  302. Automatically generated when `todo-save-top-priorities' is non-nil."
  303. :type 'string
  304. :group 'todo)
  305. (defcustom todo-print-function 'ps-print-buffer-with-faces
  306. "Function to print the current buffer."
  307. :type 'symbol
  308. :group 'todo)
  309. (defcustom todo-show-priorities 1
  310. "Default number of priorities to show by \\[todo-top-priorities].
  311. 0 means show all entries."
  312. :type 'integer
  313. :group 'todo)
  314. (defcustom todo-print-priorities 0
  315. "Default number of priorities to print by \\[todo-print].
  316. 0 means print all entries."
  317. :type 'integer
  318. :group 'todo)
  319. (defcustom todo-remove-separator t
  320. "Non-nil to remove category separators in\
  321. \\[todo-top-priorities] and \\[todo-print]."
  322. :type 'boolean
  323. :group 'todo)
  324. (defcustom todo-save-top-priorities-too t
  325. "Non-nil makes `todo-save' automatically save top-priorities in `todo-file-top'."
  326. :type 'boolean
  327. :group 'todo)
  328. ;; Thanks for the ISO time stamp format go to Karl Eichwalder <ke@suse.de>
  329. ;; My format string for the appt.el package is "%3b %2d, %y, %02I:%02M%p".
  330. ;;
  331. (defcustom todo-time-string-format
  332. "%:y-%02m-%02d %02H:%02M"
  333. "TODO mode time string format for done entries.
  334. For details see the variable `time-stamp-format'."
  335. :type 'string
  336. :group 'todo)
  337. (defcustom todo-entry-prefix-function 'todo-entry-timestamp-initials
  338. "Function producing text to insert at start of todo entry."
  339. :type 'symbol
  340. :group 'todo)
  341. (defcustom todo-initials (or (getenv "INITIALS") (user-login-name))
  342. "Initials of todo item author."
  343. :type 'string
  344. :group 'todo)
  345. (defun todo-entry-timestamp-initials ()
  346. "Prepend timestamp and your initials to the head of a TODO entry."
  347. (let ((time-stamp-format todo-time-string-format))
  348. (concat (time-stamp-string) " " todo-initials ": ")))
  349. ;; ---------------------------------------------------------------------------
  350. ;; Set up some helpful context ...
  351. (defvar todo-categories nil
  352. "TODO categories.")
  353. (defvar todo-cats nil
  354. "Old variable for holding the TODO categories.
  355. Use `todo-categories' instead.")
  356. (defvar todo-previous-line 0
  357. "Previous line asked about.")
  358. (defvar todo-previous-answer 0
  359. "Previous answer got.")
  360. (defvar todo-mode-map
  361. (let ((map (make-keymap)))
  362. (suppress-keymap map t)
  363. (define-key map "+" 'todo-forward-category)
  364. (define-key map "-" 'todo-backward-category)
  365. (define-key map "d" 'todo-file-item) ;done/delete
  366. (define-key map "e" 'todo-edit-item)
  367. (define-key map "E" 'todo-edit-multiline)
  368. (define-key map "f" 'todo-file-item)
  369. (define-key map "i" 'todo-insert-item)
  370. (define-key map "I" 'todo-insert-item-here)
  371. (define-key map "j" 'todo-jump-to-category)
  372. (define-key map "k" 'todo-delete-item)
  373. (define-key map "l" 'todo-lower-item)
  374. (define-key map "n" 'todo-forward-item)
  375. (define-key map "p" 'todo-backward-item)
  376. (define-key map "P" 'todo-print)
  377. (define-key map "q" 'todo-quit)
  378. (define-key map "r" 'todo-raise-item)
  379. (define-key map "s" 'todo-save)
  380. (define-key map "S" 'todo-save-top-priorities)
  381. (define-key map "t" 'todo-top-priorities)
  382. map)
  383. "TODO mode keymap.")
  384. (defvar todo-category-number 0 "TODO category number.")
  385. (defvar todo-tmp-buffer-name " *todo tmp*")
  386. (defvar todo-category-sep (make-string 75 ?-)
  387. "Category separator.")
  388. (defvar todo-category-beg " --- "
  389. "Category start separator to be prepended onto category name.")
  390. (defvar todo-category-end "--- End"
  391. "Separator after a category.")
  392. (defvar todo-header "-*- mode: todo; "
  393. "Header of todo files.")
  394. ;; ---------------------------------------------------------------------------
  395. (defun todo-category-select ()
  396. "Make TODO mode display the current category correctly."
  397. (let ((name (nth todo-category-number todo-categories)))
  398. (setq mode-line-buffer-identification
  399. ;; (concat "Category: " name))
  400. (concat "Category: " (format "%18s" name)))
  401. (widen)
  402. (goto-char (point-min))
  403. (search-forward-regexp
  404. (concat "^"
  405. (regexp-quote (concat todo-prefix todo-category-beg name))
  406. "$"))
  407. (let ((begin (1+ (line-end-position))))
  408. (search-forward-regexp (concat "^" todo-category-end))
  409. (narrow-to-region begin (line-beginning-position))
  410. (goto-char (point-min)))))
  411. (defalias 'todo-cat-slct 'todo-category-select)
  412. (defun todo-forward-category ()
  413. "Go forward to TODO list of next category."
  414. (interactive)
  415. (setq todo-category-number
  416. (mod (1+ todo-category-number) (length todo-categories)))
  417. (todo-category-select))
  418. (defalias 'todo-cmd-forw 'todo-forward-category)
  419. (defun todo-backward-category ()
  420. "Go back to TODO list of previous category."
  421. (interactive)
  422. (setq todo-category-number
  423. (mod (1- todo-category-number) (length todo-categories)))
  424. (todo-category-select))
  425. (defalias 'todo-cmd-back 'todo-backward-category)
  426. (defun todo-backward-item ()
  427. "Select previous entry of TODO list."
  428. (interactive)
  429. (search-backward-regexp (concat "^" (regexp-quote todo-prefix)) nil t)
  430. (message ""))
  431. (defalias 'todo-cmd-prev 'todo-backward-item)
  432. (defun todo-forward-item (&optional count)
  433. "Select COUNT-th next entry of TODO list."
  434. (interactive "P")
  435. (if (listp count) (setq count (car count)))
  436. (end-of-line)
  437. (search-forward-regexp (concat "^" (regexp-quote todo-prefix))
  438. nil 'goto-end count)
  439. (beginning-of-line)
  440. (message ""))
  441. (defalias 'todo-cmd-next 'todo-forward-item)
  442. (defun todo-save ()
  443. "Save the TODO list."
  444. (interactive)
  445. (save-excursion
  446. (save-restriction
  447. (save-buffer)))
  448. (if todo-save-top-priorities-too (todo-save-top-priorities)))
  449. (defalias 'todo-cmd-save 'todo-save)
  450. (defun todo-quit ()
  451. "Done with TODO list for now."
  452. (interactive)
  453. (widen)
  454. (todo-save)
  455. (message "")
  456. (bury-buffer))
  457. (defalias 'todo-cmd-done 'todo-quit)
  458. (defun todo-edit-item ()
  459. "Edit current TODO list entry."
  460. (interactive)
  461. (if (< (point-min) (point-max))
  462. (let ((item (todo-item-string)))
  463. (if (todo-string-multiline-p item)
  464. (todo-edit-multiline)
  465. (let ((new (read-from-minibuffer "Edit: " item)))
  466. (todo-remove-item)
  467. (insert new "\n")
  468. (todo-backward-item)
  469. (message ""))))
  470. (error "No TODO list entry to edit")))
  471. (defalias 'todo-cmd-edit 'todo-edit-item)
  472. (defun todo-edit-multiline ()
  473. "Set up a buffer for editing a multiline TODO list entry."
  474. (interactive)
  475. (let ((buffer-name (generate-new-buffer-name todo-edit-buffer)))
  476. (switch-to-buffer
  477. (make-indirect-buffer
  478. (file-name-nondirectory todo-file-do) buffer-name))
  479. (message "To exit, simply kill this buffer and return to list.")
  480. (todo-edit-mode)
  481. (narrow-to-region (todo-item-start) (todo-item-end))))
  482. ;;;###autoload
  483. (defun todo-add-category (&optional cat)
  484. "Add new category CAT to the TODO list."
  485. (interactive)
  486. (let ((buf (find-file-noselect todo-file-do t))
  487. (prompt "Category: "))
  488. (unless (zerop (buffer-size buf))
  489. (and (null todo-categories)
  490. (null todo-cats)
  491. (error "Error in %s: File is non-empty but contains no category"
  492. todo-file-do)))
  493. (unless cat (setq cat (read-from-minibuffer prompt)))
  494. (with-current-buffer buf
  495. ;; reject names that could induce bugs and confusion
  496. (while (and (cond ((string= "" cat)
  497. (setq prompt "Enter a non-empty category name: "))
  498. ((string-match "\\`\\s-+\\'" cat)
  499. (setq prompt "Enter a category name that is not only white space: "))
  500. ((member cat todo-categories)
  501. (setq prompt "Enter a non-existing category name: ")))
  502. (setq cat (read-from-minibuffer prompt))))
  503. ;; initialize a newly created Todo buffer for Todo mode
  504. (unless (file-exists-p todo-file-do) (todo-mode))
  505. (setq todo-categories (cons cat todo-categories))
  506. (widen)
  507. (goto-char (point-min))
  508. (if (search-forward "-*- mode: todo; " (+ (point-min) 16) t)
  509. (kill-line)
  510. (insert "-*- mode: todo; \n")
  511. (forward-char -1))
  512. (insert (format "todo-categories: %S; -*-" todo-categories))
  513. (forward-char 1)
  514. (insert (format "%s%s%s\n%s\n%s %s\n"
  515. todo-prefix todo-category-beg cat
  516. todo-category-end
  517. todo-prefix todo-category-sep))
  518. (if (called-interactively-p 'interactive)
  519. ;; properly display the newly added category
  520. (progn (setq todo-category-number 0) (todo-show))
  521. 0))))
  522. ;;;###autoload
  523. (defun todo-add-item-non-interactively (new-item category)
  524. "Insert NEW-ITEM in TODO list as a new entry in CATEGORY."
  525. (save-excursion
  526. (todo-show))
  527. (save-excursion
  528. (if (string= "" category)
  529. (setq category (nth todo-category-number todo-categories)))
  530. (let ((cat-exists (member category todo-categories)))
  531. (setq todo-category-number
  532. (if cat-exists
  533. (- (length todo-categories) (length cat-exists))
  534. (todo-add-category category))))
  535. (todo-show)
  536. (setq todo-previous-line 0)
  537. (let ((top 1)
  538. (bottom (1+ (count-lines (point-min) (point-max)))))
  539. (while (> (- bottom top) todo-insert-threshold)
  540. (let* ((current (/ (+ top bottom) 2))
  541. (answer (if (< current bottom)
  542. (todo-more-important-p current) nil)))
  543. (if answer
  544. (setq bottom current)
  545. (setq top (1+ current)))))
  546. (setq top (/ (+ top bottom) 2))
  547. ;; goto-line doesn't have the desired behavior in a narrowed buffer.
  548. (goto-char (point-min))
  549. (forward-line (1- top)))
  550. (insert new-item "\n")
  551. (todo-backward-item)
  552. (todo-save)
  553. (message "")))
  554. ;;;###autoload
  555. (defun todo-insert-item (arg)
  556. "Insert new TODO list entry.
  557. With a prefix argument ARG solicit the category, otherwise use the current
  558. category."
  559. (interactive "P")
  560. (save-excursion
  561. (if (not (derived-mode-p 'todo-mode)) (todo-show))
  562. (let* ((new-item (concat todo-prefix " "
  563. (read-from-minibuffer
  564. "New TODO entry: "
  565. (if todo-entry-prefix-function
  566. (funcall todo-entry-prefix-function)))))
  567. (current-category (nth todo-category-number todo-categories))
  568. (category (if arg (todo-completing-read) current-category)))
  569. (todo-add-item-non-interactively new-item category))))
  570. (defalias 'todo-cmd-inst 'todo-insert-item)
  571. (defun todo-insert-item-here ()
  572. "Insert a new TODO list entry directly above the entry at point.
  573. If point is on an empty line, insert the entry there."
  574. (interactive)
  575. (if (not (derived-mode-p 'todo-mode)) (todo-show))
  576. (let ((new-item (concat todo-prefix " "
  577. (read-from-minibuffer
  578. "New TODO entry: "
  579. (if todo-entry-prefix-function
  580. (funcall todo-entry-prefix-function))))))
  581. (unless (and (bolp) (eolp)) (todo-item-start))
  582. (insert (concat new-item "\n"))
  583. (backward-char)
  584. ;; put point at start of new entry
  585. (todo-item-start)))
  586. (defun todo-more-important-p (line)
  587. "Ask whether entry is more important than the one at LINE."
  588. (unless (equal todo-previous-line line)
  589. (setq todo-previous-line line)
  590. (goto-char (point-min))
  591. (forward-line (1- todo-previous-line))
  592. (let ((item (todo-item-string-start)))
  593. (setq todo-previous-answer
  594. (y-or-n-p (concat "More important than '" item "'? ")))))
  595. todo-previous-answer)
  596. (defalias 'todo-ask-p 'todo-more-important-p)
  597. (defun todo-delete-item ()
  598. "Delete current TODO list entry."
  599. (interactive)
  600. (if (> (count-lines (point-min) (point-max)) 0)
  601. (let* ((todo-entry (todo-item-string-start))
  602. (todo-answer (y-or-n-p (concat "Permanently remove '"
  603. todo-entry "'? "))))
  604. (when todo-answer
  605. (todo-remove-item)
  606. (todo-backward-item))
  607. (message ""))
  608. (error "No TODO list entry to delete")))
  609. (defalias 'todo-cmd-kill 'todo-delete-item)
  610. (defun todo-raise-item ()
  611. "Raise priority of current entry."
  612. (interactive)
  613. (if (> (count-lines (point-min) (point)) 0)
  614. (let ((item (todo-item-string)))
  615. (todo-remove-item)
  616. (todo-backward-item)
  617. (save-excursion
  618. (insert item "\n"))
  619. (message ""))
  620. (error "No TODO list entry to raise")))
  621. (defalias 'todo-cmd-rais 'todo-raise-item)
  622. (defun todo-lower-item ()
  623. "Lower priority of current entry."
  624. (interactive)
  625. (if (> (count-lines (point) (point-max)) 1)
  626. ;; Assume there is a final newline
  627. (let ((item (todo-item-string)))
  628. (todo-remove-item)
  629. (todo-forward-item)
  630. (save-excursion
  631. (insert item "\n"))
  632. (message ""))
  633. (error "No TODO list entry to lower")))
  634. (defalias 'todo-cmd-lowr 'todo-lower-item)
  635. (defun todo-file-item (&optional comment)
  636. "File the current TODO list entry away, annotated with an optional COMMENT."
  637. (interactive "sComment: ")
  638. (or (> (count-lines (point-min) (point-max)) 0)
  639. (error "No TODO list entry to file away"))
  640. (let ((time-stamp-format todo-time-string-format))
  641. (when (and comment (> (length comment) 0))
  642. (goto-char (todo-item-end))
  643. (insert
  644. (if (save-excursion (beginning-of-line)
  645. (looking-at (regexp-quote todo-prefix)))
  646. " "
  647. "\n\t")
  648. "(" comment ")"))
  649. (goto-char (todo-item-end))
  650. (insert " [" (nth todo-category-number todo-categories) "]")
  651. (todo-item-start)
  652. (let ((temp-point (point)))
  653. (if (looking-at (regexp-quote todo-prefix))
  654. (replace-match (time-stamp-string))
  655. ;; Standard prefix -> timestamp
  656. ;; Else prefix non-standard item start with timestamp
  657. (insert (time-stamp-string)))
  658. (append-to-file temp-point (todo-item-end 'include-sep) todo-file-done)
  659. (delete-region temp-point (todo-item-end 'include-sep)))
  660. (todo-backward-item)
  661. (message "")))
  662. ;; ---------------------------------------------------------------------------
  663. ;; Utility functions:
  664. ;;;###autoload
  665. (defun todo-top-priorities (&optional nof-priorities category-pr-page
  666. interactive)
  667. "List top priorities for each category.
  668. Number of entries for each category is given by NOF-PRIORITIES which
  669. defaults to `todo-show-priorities'.
  670. If CATEGORY-PR-PAGE is non-nil, a page separator \'^L\' is inserted
  671. between each category.
  672. INTERACTIVE should be non-nil if this function is called interactively."
  673. (interactive "P\ni\nP")
  674. (or nof-priorities (setq nof-priorities todo-show-priorities))
  675. (if (listp nof-priorities) ;universal argument
  676. (setq nof-priorities (car nof-priorities)))
  677. (let ((todo-print-buffer-name todo-tmp-buffer-name)
  678. ;;(todo-print-category-number 0)
  679. (todo-category-break (if category-pr-page " " ""))
  680. (cat-end
  681. (concat
  682. (if todo-remove-separator
  683. (concat todo-category-end "\n"
  684. (regexp-quote todo-prefix) " " todo-category-sep "\n")
  685. (concat todo-category-end "\n"))))
  686. beg end)
  687. (save-excursion
  688. (todo-show)
  689. (save-restriction
  690. (save-current-buffer
  691. (widen)
  692. (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
  693. (set-buffer todo-print-buffer-name)
  694. (goto-char (point-min))
  695. (when (re-search-forward (regexp-quote todo-header) nil t)
  696. (beginning-of-line 1)
  697. (delete-region (point) (line-end-position)))
  698. (while (re-search-forward ;Find category start
  699. (regexp-quote (concat todo-prefix todo-category-beg))
  700. nil t)
  701. (setq beg (+ (line-end-position) 1)) ;Start of first entry.
  702. (re-search-forward cat-end nil t)
  703. (setq end (match-beginning 0))
  704. (replace-match todo-category-break)
  705. (narrow-to-region beg end) ;In case we have too few entries.
  706. (goto-char (point-min))
  707. (if (zerop nof-priorities) ;Traverse entries.
  708. (goto-char end) ;All entries
  709. (todo-forward-item nof-priorities))
  710. (setq beg (point))
  711. (delete-region beg end)
  712. (widen))
  713. (and (looking-at " ") (replace-match "")) ;Remove trailing form-feed.
  714. (goto-char (point-min)) ;Due to display buffer
  715. )))
  716. (when interactive (display-buffer todo-print-buffer-name))
  717. (message "Type C-x 1 to remove %s window. M-C-v to scroll the help."
  718. todo-print-buffer-name)))
  719. (defun todo-save-top-priorities (&optional nof-priorities)
  720. "Save top priorities for each category in `todo-file-top'.
  721. Number of entries for each category is given by NOF-PRIORITIES which
  722. defaults to `todo-show-priorities'."
  723. (interactive "P")
  724. (save-window-excursion
  725. (save-excursion
  726. (save-restriction
  727. (todo-top-priorities nof-priorities)
  728. (set-buffer todo-tmp-buffer-name)
  729. (write-file todo-file-top)
  730. (kill-this-buffer)))))
  731. ;;;###autoload
  732. (defun todo-print (&optional category-pr-page)
  733. "Print todo summary using `todo-print-function'.
  734. If CATEGORY-PR-PAGE is non-nil, a page separator `^L' is inserted
  735. between each category.
  736. Number of entries for each category is given by `todo-print-priorities'."
  737. (interactive "P")
  738. (save-window-excursion
  739. (save-excursion
  740. (save-restriction
  741. (todo-top-priorities todo-print-priorities
  742. category-pr-page)
  743. (set-buffer todo-tmp-buffer-name)
  744. (and (funcall todo-print-function)
  745. (kill-this-buffer))
  746. (message "Todo printing done.")))))
  747. (defun todo-jump-to-category ()
  748. "Jump to a category. Default is previous category."
  749. (interactive)
  750. (let ((category (todo-completing-read)))
  751. (if (string= "" category)
  752. (setq category (nth todo-category-number todo-categories)))
  753. (setq todo-category-number
  754. (if (member category todo-categories)
  755. (- (length todo-categories)
  756. (length (member category todo-categories)))
  757. (todo-add-category category)))
  758. (todo-show)))
  759. (defun todo-line-string ()
  760. "Return current line in buffer as a string."
  761. (buffer-substring (line-beginning-position) (line-end-position)))
  762. (defun todo-item-string-start ()
  763. "Return the start of this TODO list entry as a string."
  764. ;; Suitable for putting in the minibuffer when asking the user
  765. (let ((item (todo-item-string)))
  766. (if (> (length item) 60)
  767. (setq item (concat (substring item 0 56) "...")))
  768. item))
  769. (defun todo-item-start ()
  770. "Go to start of current TODO list item and return point."
  771. (beginning-of-line)
  772. (if (not (looking-at (regexp-quote todo-prefix)))
  773. (search-backward-regexp
  774. (concat "^" (regexp-quote todo-prefix)) nil t))
  775. (point))
  776. (defun todo-item-end (&optional include-sep)
  777. "Return point at end of current TODO list item.
  778. If INCLUDE-SEP is non-nil, return point after the separator."
  779. (save-excursion
  780. (end-of-line)
  781. (if (search-forward-regexp
  782. (concat "^" (regexp-quote todo-prefix)) nil 'goto-end)
  783. (goto-char (match-beginning 0)))
  784. (unless include-sep (skip-chars-backward "\n"))
  785. (point)))
  786. (defun todo-remove-item ()
  787. "Delete the current entry from the TODO list."
  788. (delete-region (todo-item-start) (todo-item-end 'include-sep)))
  789. (defun todo-item-string ()
  790. "Return current TODO list entry as a string."
  791. (buffer-substring (todo-item-start) (todo-item-end)))
  792. (defun todo-string-count-lines (string)
  793. "Return the number of lines STRING spans."
  794. (length (split-string string "\n")))
  795. (defun todo-string-multiline-p (string)
  796. "Return non-nil if STRING spans several lines."
  797. (> (todo-string-count-lines string) 1))
  798. (defun todo-completing-read ()
  799. "Return a category name, with completion, for use in Todo mode."
  800. ;; make a copy of todo-categories in case history-delete-duplicates is
  801. ;; non-nil, which makes completing-read alter todo-categories
  802. (let* ((categories (copy-sequence todo-categories))
  803. (history (cons 'todo-categories (1+ todo-category-number)))
  804. (default (nth todo-category-number todo-categories))
  805. (category (completing-read
  806. (concat "Category [" default "]: ")
  807. todo-categories nil nil nil history default)))
  808. ;; restore the original value of todo-categories
  809. (setq todo-categories categories)
  810. category))
  811. ;; ---------------------------------------------------------------------------
  812. (easy-menu-define todo-menu todo-mode-map "Todo Menu"
  813. '("Todo"
  814. ["Next category" todo-forward-category t]
  815. ["Previous category" todo-backward-category t]
  816. ["Jump to category" todo-jump-to-category t]
  817. ["Show top priority items" todo-top-priorities t]
  818. ["Print categories" todo-print t]
  819. "---"
  820. ["Edit item" todo-edit-item t]
  821. ["File item" todo-file-item t]
  822. ["Insert new item" todo-insert-item t]
  823. ["Insert item here" todo-insert-item-here t]
  824. ["Kill item" todo-delete-item t]
  825. "---"
  826. ["Lower item priority" todo-lower-item t]
  827. ["Raise item priority" todo-raise-item t]
  828. "---"
  829. ["Next item" todo-forward-item t]
  830. ["Previous item" todo-backward-item t]
  831. "---"
  832. ["Save" todo-save t]
  833. ["Save Top Priorities" todo-save-top-priorities t]
  834. "---"
  835. ["Quit" todo-quit t]
  836. ))
  837. ;; As calendar reads .todo-do before todo-mode is loaded.
  838. ;;;###autoload
  839. (define-derived-mode todo-mode nil "TODO"
  840. "Major mode for editing TODO lists."
  841. (easy-menu-add todo-menu))
  842. (defvar date)
  843. (defvar entry)
  844. ;; t-c should be used from diary code, which requires calendar.
  845. (declare-function calendar-current-date "calendar" (&optional offset))
  846. ;; Read about this function in the setup instructions above!
  847. ;;;###autoload
  848. (defun todo-cp ()
  849. "Make a diary entry appear only in the current date's diary."
  850. (if (equal (calendar-current-date) date)
  851. entry))
  852. (define-derived-mode todo-edit-mode text-mode "TODO Edit"
  853. "Major mode for editing items in the TODO list.
  854. \\{todo-edit-mode-map}")
  855. ;;;###autoload
  856. (defun todo-show ()
  857. "Show TODO list."
  858. (interactive)
  859. ;; Call todo-initial-setup only if there is neither a Todo file nor
  860. ;; a corresponding unsaved buffer.
  861. (if (or (file-exists-p todo-file-do)
  862. (let* ((buf (get-buffer (file-name-nondirectory todo-file-do)))
  863. (bufname (buffer-file-name buf)))
  864. (equal (expand-file-name todo-file-do) bufname)))
  865. (find-file todo-file-do)
  866. (todo-initial-setup))
  867. (if (null todo-categories)
  868. (if (null todo-cats)
  869. (error "Error in %s: No categories in list `todo-categories'"
  870. todo-file-do)
  871. (goto-char (point-min))
  872. (and (search-forward "todo-cats:" nil t)
  873. (replace-match "todo-categories:"))
  874. (make-local-variable 'todo-categories)
  875. (setq todo-categories todo-cats)))
  876. (beginning-of-line)
  877. (todo-category-select))
  878. (defun todo-initial-setup ()
  879. "Set up things to work properly in TODO mode."
  880. (find-file todo-file-do)
  881. (erase-buffer)
  882. (todo-mode)
  883. (todo-add-category "Todo"))
  884. (provide 'todo-mode)
  885. ;;; todo-mode.el ends here