aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2009-01-28 03:56:18 +0000
committerChong Yidong2009-01-28 03:56:18 +0000
commit685b54793282db07cba9a78ee918070983f89227 (patch)
treead6241c61d2b1af66ade43123dea5f21336c8e0a
parent943ff2103422b101eaf2b8708075afc91263483b (diff)
downloademacs-685b54793282db07cba9a78ee918070983f89227.tar.gz
emacs-685b54793282db07cba9a78ee918070983f89227.zip
(todo-insert-item-here): Prevent insertion
of a new entry inside of an existing entry. Minor code cleanup. (todo-add-category): Change the interactive spec. Signal an error if the Todo file is non-empty but contains no category. Reject category names that could induce bugs and confusion. Call todo-mode if the Todo file is new and unsaved. Simplify handling of local variables cookie. Properly display the newly added category in Todo mode. (todo-show): Call todo-initial-setup only if there is neither a Todo file nor a corresponding unsaved buffer. (todo-category-alist): Delete function. (todo-completing-read): New function. (todo-insert-item, todo-jump-to-category): Use it. (todo-insert-item): Make the use of the prefix argument conform to the doc string.
-rw-r--r--lisp/calendar/todo-mode.el122
1 files changed, 73 insertions, 49 deletions
diff --git a/lisp/calendar/todo-mode.el b/lisp/calendar/todo-mode.el
index bfefc23ff18..3d859305aa9 100644
--- a/lisp/calendar/todo-mode.el
+++ b/lisp/calendar/todo-mode.el
@@ -527,28 +527,45 @@ Use `todo-categories' instead.")
527 (narrow-to-region (todo-item-start) (todo-item-end)))) 527 (narrow-to-region (todo-item-start) (todo-item-end))))
528 528
529;;;###autoload 529;;;###autoload
530(defun todo-add-category (cat) 530(defun todo-add-category (&optional cat)
531 "Add new category CAT to the TODO list." 531 "Add new category CAT to the TODO list."
532 (interactive "sCategory: ") 532 (interactive)
533 (save-window-excursion 533 (let ((buf (find-file-noselect todo-file-do t))
534 (setq todo-categories (cons cat todo-categories)) 534 (prompt "Category: "))
535 (find-file todo-file-do) 535 (unless (zerop (buffer-size buf))
536 (widen) 536 (and (null todo-categories)
537 (goto-char (point-min)) 537 (null todo-cats)
538 (let ((posn (search-forward "-*- mode: todo; " 17 t))) 538 (error "Error in %s: File is non-empty but contains no category"
539 (if posn 539 todo-file-do)))
540 (progn 540 (unless cat (setq cat (read-from-minibuffer prompt)))
541 (goto-char posn) 541 (with-current-buffer buf
542 (kill-line)) 542 ;; reject names that could induce bugs and confusion
543 (insert "-*- mode: todo; \n") 543 (while (and (cond ((string= "" cat)
544 (forward-char -1))) 544 (setq prompt "Enter a non-empty category name: "))
545 (insert (format "todo-categories: %S; -*-" todo-categories)) 545 ((string-match "\\`\\s-+\\'" cat)
546 (forward-char 1) 546 (setq prompt "Enter a category name that is not only white space: "))
547 (insert (format "%s%s%s\n%s\n%s %s\n" 547 ((member cat todo-categories)
548 todo-prefix todo-category-beg cat 548 (setq prompt "Enter a non-existing category name: ")))
549 todo-category-end 549 (setq cat (read-from-minibuffer prompt))))
550 todo-prefix todo-category-sep))) 550 ;; initialize a newly created Todo buffer for Todo mode
551 0) 551 (unless (file-exists-p todo-file-do) (todo-mode))
552 (setq todo-categories (cons cat todo-categories))
553 (widen)
554 (goto-char (point-min))
555 (if (search-forward "-*- mode: todo; " 17 t)
556 (kill-line)
557 (insert "-*- mode: todo; \n")
558 (forward-char -1))
559 (insert (format "todo-categories: %S; -*-" todo-categories))
560 (forward-char 1)
561 (insert (format "%s%s%s\n%s\n%s %s\n"
562 todo-prefix todo-category-beg cat
563 todo-category-end
564 todo-prefix todo-category-sep))
565 (if (interactive-p)
566 ;; properly display the newly added category
567 (progn (setq todo-category-number 0) (todo-show))
568 0))))
552 569
553;;;###autoload 570;;;###autoload
554(defun todo-add-item-non-interactively (new-item category) 571(defun todo-add-item-non-interactively (new-item category)
@@ -596,30 +613,27 @@ category."
596 "New TODO entry: " 613 "New TODO entry: "
597 (if todo-entry-prefix-function 614 (if todo-entry-prefix-function
598 (funcall todo-entry-prefix-function))))) 615 (funcall todo-entry-prefix-function)))))
599 (categories todo-categories)
600 (history (cons 'categories (1+ todo-category-number)))
601 (current-category (nth todo-category-number todo-categories)) 616 (current-category (nth todo-category-number todo-categories))
602 (category 617 (category (if arg (todo-completing-read) current-category)))
603 (if arg
604 current-category
605 (completing-read (concat "Category [" current-category "]: ")
606 (todo-category-alist) nil nil nil
607 history current-category))))
608 (todo-add-item-non-interactively new-item category)))) 618 (todo-add-item-non-interactively new-item category))))
609 619
610(defalias 'todo-cmd-inst 'todo-insert-item) 620(defalias 'todo-cmd-inst 'todo-insert-item)
611 621
612(defun todo-insert-item-here () 622(defun todo-insert-item-here ()
613 "Insert new TODO list entry under the cursor." 623 "Insert a new TODO list entry directly above the entry at point.
614 (interactive "") 624If point is on an empty line, insert the entry there."
615 (save-excursion 625 (interactive)
616 (if (not (derived-mode-p 'todo-mode)) (todo-show)) 626 (if (not (derived-mode-p 'todo-mode)) (todo-show))
617 (let* ((new-item (concat todo-prefix " " 627 (let ((new-item (concat todo-prefix " "
618 (read-from-minibuffer 628 (read-from-minibuffer
619 "New TODO entry: " 629 "New TODO entry: "
620 (if todo-entry-prefix-function 630 (if todo-entry-prefix-function
621 (funcall todo-entry-prefix-function)))))) 631 (funcall todo-entry-prefix-function))))))
622 (insert (concat new-item "\n"))))) 632 (unless (and (bolp) (eolp)) (goto-char (todo-item-start)))
633 (insert (concat new-item "\n"))
634 (backward-char)
635 ;; put point at start of new entry
636 (goto-char (todo-item-start))))
623 637
624(defun todo-more-important-p (line) 638(defun todo-more-important-p (line)
625 "Ask whether entry is more important than the one at LINE." 639 "Ask whether entry is more important than the one at LINE."
@@ -801,12 +815,7 @@ Number of entries for each category is given by `todo-print-priorities'."
801(defun todo-jump-to-category () 815(defun todo-jump-to-category ()
802 "Jump to a category. Default is previous category." 816 "Jump to a category. Default is previous category."
803 (interactive) 817 (interactive)
804 (let* ((categories todo-categories) 818 (let ((category (todo-completing-read)))
805 (history (cons 'categories (1+ todo-category-number)))
806 (default (nth todo-category-number todo-categories))
807 (category (completing-read
808 (concat "Category [" default "]: ")
809 (todo-category-alist) nil nil nil history default)))
810 (if (string= "" category) 819 (if (string= "" category)
811 (setq category (nth todo-category-number todo-categories))) 820 (setq category (nth todo-category-number todo-categories)))
812 (setq todo-category-number 821 (setq todo-category-number
@@ -861,9 +870,19 @@ Number of entries for each category is given by `todo-print-priorities'."
861 "Return non-nil if STRING spans several lines." 870 "Return non-nil if STRING spans several lines."
862 (> (todo-string-count-lines string) 1)) 871 (> (todo-string-count-lines string) 1))
863 872
864(defun todo-category-alist () 873(defun todo-completing-read ()
865 "Generate an alist for use in `completing-read' from `todo-categories'." 874 "Return a category name, with completion, for use in Todo mode."
866 (mapcar #'list todo-categories)) 875 ;; make a copy of todo-categories in case history-delete-duplicates is
876 ;; non-nil, which makes completing-read alter todo-categories
877 (let* ((categories (copy-sequence todo-categories))
878 (history (cons 'todo-categories (1+ todo-category-number)))
879 (default (nth todo-category-number todo-categories))
880 (category (completing-read
881 (concat "Category [" default "]: ")
882 todo-categories nil nil nil history default)))
883 ;; restore the original value of todo-categories
884 (setq todo-categories categories)
885 category))
867 886
868;; --------------------------------------------------------------------------- 887;; ---------------------------------------------------------------------------
869 888
@@ -929,7 +948,12 @@ Number of entries for each category is given by `todo-print-priorities'."
929(defun todo-show () 948(defun todo-show ()
930 "Show TODO list." 949 "Show TODO list."
931 (interactive) 950 (interactive)
932 (if (file-exists-p todo-file-do) 951 ;; Call todo-initial-setup only if there is neither a Todo file nor
952 ;; a corresponding unsaved buffer.
953 (if (or (file-exists-p todo-file-do)
954 (let* ((buf (get-buffer (file-name-nondirectory todo-file-do)))
955 (bufname (buffer-file-name buf)))
956 (equal (expand-file-name todo-file-do) bufname)))
933 (find-file todo-file-do) 957 (find-file todo-file-do)
934 (todo-initial-setup)) 958 (todo-initial-setup))
935 (if (null todo-categories) 959 (if (null todo-categories)