otodo-mode.el 35 KB

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