Stephen Berman <stephen.berman at gmx.net>
[bpt/emacs.git] / lisp / calendar / todo-mode.el
index 2e246bf..1e06d4a 100644 (file)
@@ -1,10 +1,10 @@
 ;;; todo-mode.el --- major mode for editing TODO list files
 
 ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
-;;   2008  Free Software Foundation, Inc.
+;;   2008, 2009  Free Software Foundation, Inc.
 
-;; Author: Oliver Seidel <os10000@seidel-space.de>
-;;   [Not clear the above works, July 2000]
+;; Author: Oliver Seidel <privat@os10000.net>
+;; Maintainer: Stephen Berman <stephen.berman@gmx.net>
 ;; Created: 2 Aug 1997
 ;; Keywords: calendar, todo
 
@@ -282,11 +282,11 @@ show and mark todo entries for today, but may slow down processing of
 the diary file somewhat."
   :type 'string
   :group 'todo)
-(defcustom todo-file-do    "~/.todo-do"
+(defcustom todo-file-do    (convert-standard-filename "~/.todo-do")
   "TODO mode list file."
   :type 'file
   :group 'todo)
-(defcustom todo-file-done  "~/.todo-done"
+(defcustom todo-file-done  (convert-standard-filename "~/.todo-done")
   "TODO mode archive file."
   :type 'file
   :group 'todo)
@@ -316,7 +316,7 @@ window."
   :group 'todo)
 (defvar todo-edit-buffer " *TODO Edit*"
   "TODO Edit buffer name.")
-(defcustom todo-file-top "~/.todo-top"
+(defcustom todo-file-top (convert-standard-filename "~/.todo-top")
   "TODO mode top priorities file.
 
 Not in TODO format, but diary compatible.
@@ -505,14 +505,16 @@ Use `todo-categories' instead.")
 (defun todo-edit-item ()
   "Edit current TODO list entry."
   (interactive)
-  (let ((item (todo-item-string)))
-    (if (todo-string-multiline-p item)
-        (todo-edit-multiline)
-      (let ((new (read-from-minibuffer "Edit: " item)))
-        (todo-remove-item)
-        (insert new "\n")
-        (todo-backward-item)
-        (message "")))))
+  (if (< (point-min) (point-max))
+      (let ((item (todo-item-string)))
+       (if (todo-string-multiline-p item)
+           (todo-edit-multiline)
+         (let ((new (read-from-minibuffer "Edit: " item)))
+           (todo-remove-item)
+           (insert new "\n")
+           (todo-backward-item)
+           (message ""))))
+    (error "No TODO list entry to edit")))
 (defalias 'todo-cmd-edit 'todo-edit-item)
 
 (defun todo-edit-multiline ()
@@ -527,28 +529,45 @@ Use `todo-categories' instead.")
     (narrow-to-region (todo-item-start) (todo-item-end))))
 
 ;;;###autoload
-(defun todo-add-category (cat)
+(defun todo-add-category (&optional cat)
   "Add new category CAT to the TODO list."
-  (interactive "sCategory: ")
-  (save-window-excursion
-    (setq todo-categories (cons cat todo-categories))
-    (find-file todo-file-do)
-    (widen)
-    (goto-char (point-min))
-    (let ((posn (search-forward "-*- mode: todo; " 17 t)))
-      (if posn
-          (progn
-            (goto-char posn)
-            (kill-line))
-        (insert "-*- mode: todo; \n")
-        (forward-char -1)))
-    (insert (format "todo-categories: %S; -*-" todo-categories))
-    (forward-char 1)
-    (insert (format "%s%s%s\n%s\n%s %s\n"
-                    todo-prefix todo-category-beg cat
-                    todo-category-end
-                    todo-prefix todo-category-sep)))
-  0)
+  (interactive)
+  (let ((buf (find-file-noselect todo-file-do t))
+       (prompt "Category: "))
+    (unless (zerop (buffer-size buf))
+      (and (null todo-categories)
+          (null todo-cats)
+          (error "Error in %s: File is non-empty but contains no category" 
+                 todo-file-do)))
+    (unless cat (setq cat (read-from-minibuffer prompt)))
+    (with-current-buffer buf
+      ;; reject names that could induce bugs and confusion
+      (while (and (cond ((string= "" cat)
+                        (setq prompt "Enter a non-empty category name: "))
+                       ((string-match "\\`\\s-+\\'" cat)
+                        (setq prompt "Enter a category name that is not only white space: "))
+                       ((member cat todo-categories)
+                        (setq prompt "Enter a non-existing category name: ")))
+                 (setq cat (read-from-minibuffer prompt))))
+      ;; initialize a newly created Todo buffer for Todo mode
+      (unless (file-exists-p todo-file-do) (todo-mode))
+      (setq todo-categories (cons cat todo-categories))
+      (widen)
+      (goto-char (point-min))
+      (if (search-forward "-*- mode: todo; " 17 t)
+         (kill-line)
+       (insert "-*- mode: todo; \n")
+       (forward-char -1))
+      (insert (format "todo-categories: %S; -*-" todo-categories))
+      (forward-char 1)
+      (insert (format "%s%s%s\n%s\n%s %s\n"
+                     todo-prefix todo-category-beg cat
+                     todo-category-end
+                     todo-prefix todo-category-sep))
+      (if (called-interactively-p 'interactive)
+         ;; properly display the newly added category
+         (progn (setq todo-category-number 0) (todo-show))
+       0))))
 
 ;;;###autoload
 (defun todo-add-item-non-interactively (new-item category)
@@ -596,30 +615,27 @@ category."
                              "New TODO entry: "
                              (if todo-entry-prefix-function
                                  (funcall todo-entry-prefix-function)))))
-          (categories todo-categories)
-          (history (cons 'categories (1+ todo-category-number)))
           (current-category (nth todo-category-number todo-categories))
-          (category
-           (if arg
-               current-category
-             (completing-read (concat "Category [" current-category "]: ")
-                              (todo-category-alist) nil nil nil
-                              history current-category))))
+          (category (if arg (todo-completing-read) current-category)))
       (todo-add-item-non-interactively new-item category))))
 
 (defalias 'todo-cmd-inst 'todo-insert-item)
 
 (defun todo-insert-item-here ()
-  "Insert new TODO list entry under the cursor."
-  (interactive "")
-  (save-excursion
-    (if (not (derived-mode-p 'todo-mode)) (todo-show))
-    (let* ((new-item (concat todo-prefix " "
-                            (read-from-minibuffer
-                             "New TODO entry: "
-                             (if todo-entry-prefix-function
-                                 (funcall todo-entry-prefix-function))))))
-      (insert (concat new-item "\n")))))
+  "Insert a new TODO list entry directly above the entry at point.
+If point is on an empty line, insert the entry there."
+  (interactive)
+  (if (not (derived-mode-p 'todo-mode)) (todo-show))
+  (let ((new-item (concat todo-prefix " "
+                         (read-from-minibuffer
+                          "New TODO entry: "
+                          (if todo-entry-prefix-function
+                              (funcall todo-entry-prefix-function))))))
+    (unless (and (bolp) (eolp)) (goto-char (todo-item-start)))
+    (insert (concat new-item "\n"))
+    (backward-char)
+    ;; put point at start of new entry
+    (goto-char (todo-item-start))))
 
 (defun todo-more-important-p (line)
   "Ask whether entry is more important than the one at LINE."
@@ -731,34 +747,35 @@ between each category."
                       (regexp-quote todo-prefix) " " todo-category-sep "\n")
             (concat todo-category-end "\n"))))
         beg end)
-    (todo-show)
     (save-excursion
+      (todo-show)
       (save-restriction
-        (widen)
-        (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
-        (set-buffer todo-print-buffer-name)
-        (goto-char (point-min))
-        (when (re-search-forward (regexp-quote todo-header) nil t)
-         (beginning-of-line 1)
-         (delete-region (point) (line-end-position)))
-        (while (re-search-forward       ;Find category start
-                (regexp-quote (concat todo-prefix todo-category-beg))
-                nil t)
-          (setq beg (+ (line-end-position) 1)) ;Start of first entry.
-          (re-search-forward cat-end nil t)
-          (setq end (match-beginning 0))
-          (replace-match todo-category-break)
-          (narrow-to-region beg end)    ;In case we have too few entries.
-          (goto-char (point-min))
-          (if (zerop nof-priorities)      ;Traverse entries.
-              (goto-char end)            ;All entries
-            (todo-forward-item nof-priorities))
-          (setq beg (point))
-          (delete-region beg end)
-          (widen))
-        (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
-        (goto-char (point-min))         ;Due to display buffer
-        ))
+       (save-current-buffer
+         (widen)
+         (copy-to-buffer todo-print-buffer-name (point-min) (point-max))
+         (set-buffer todo-print-buffer-name)
+         (goto-char (point-min))
+         (when (re-search-forward (regexp-quote todo-header) nil t)
+           (beginning-of-line 1)
+           (delete-region (point) (line-end-position)))
+         (while (re-search-forward       ;Find category start
+                 (regexp-quote (concat todo-prefix todo-category-beg))
+                 nil t)
+           (setq beg (+ (line-end-position) 1)) ;Start of first entry.
+           (re-search-forward cat-end nil t)
+           (setq end (match-beginning 0))
+           (replace-match todo-category-break)
+           (narrow-to-region beg end)    ;In case we have too few entries.
+           (goto-char (point-min))
+           (if (zerop nof-priorities)      ;Traverse entries.
+               (goto-char end)            ;All entries
+             (todo-forward-item nof-priorities))
+           (setq beg (point))
+           (delete-region beg end)
+           (widen))
+         (and (looking-at "\f") (replace-match "")) ;Remove trailing form-feed.
+         (goto-char (point-min))         ;Due to display buffer
+         )))
     ;; Could have used switch-to-buffer as it has a norecord argument,
     ;; which is nice when we are called from e.g. todo-print.
     ;; Else we could have used pop-to-buffer.
@@ -801,12 +818,7 @@ Number of entries for each category is given by `todo-print-priorities'."
 (defun todo-jump-to-category ()
   "Jump to a category.  Default is previous category."
   (interactive)
-  (let* ((categories todo-categories)
-         (history (cons 'categories (1+ todo-category-number)))
-        (default (nth todo-category-number todo-categories))
-        (category (completing-read
-                    (concat "Category [" default "]: ")
-                    (todo-category-alist) nil nil nil history default)))
+  (let ((category (todo-completing-read)))
     (if (string= "" category)
         (setq category (nth todo-category-number todo-categories)))
     (setq todo-category-number
@@ -861,9 +873,19 @@ Number of entries for each category is given by `todo-print-priorities'."
   "Return non-nil if STRING spans several lines."
   (> (todo-string-count-lines string) 1))
 
-(defun todo-category-alist ()
-  "Generate an alist for use in `completing-read' from `todo-categories'."
-  (mapcar #'list todo-categories))
+(defun todo-completing-read ()
+  "Return a category name, with completion, for use in Todo mode."
+  ;; make a copy of todo-categories in case history-delete-duplicates is
+  ;; non-nil, which makes completing-read alter todo-categories
+  (let* ((categories (copy-sequence todo-categories))
+        (history (cons 'todo-categories (1+ todo-category-number)))
+        (default (nth todo-category-number todo-categories))
+        (category (completing-read
+                   (concat "Category [" default "]: ")
+                   todo-categories nil nil nil history default)))
+    ;; restore the original value of todo-categories
+    (setq todo-categories categories)
+    category))
 
 ;; ---------------------------------------------------------------------------
 
@@ -911,7 +933,7 @@ Number of entries for each category is given by `todo-print-priorities'."
 (defvar entry)
 
 ;; t-c should be used from diary code, which requires calendar.
-(declare-function calendar-current-date "calendar" nil)
+(declare-function calendar-current-date "calendar" (&optional offset))
 
 ;; Read about this function in the setup instructions above!
 ;;;###autoload
@@ -929,7 +951,12 @@ Number of entries for each category is given by `todo-print-priorities'."
 (defun todo-show ()
   "Show TODO list."
   (interactive)
-  (if (file-exists-p todo-file-do)
+  ;; Call todo-initial-setup only if there is neither a Todo file nor
+  ;; a corresponding unsaved buffer.
+  (if (or (file-exists-p todo-file-do)
+         (let* ((buf (get-buffer (file-name-nondirectory todo-file-do)))
+                (bufname (buffer-file-name buf)))
+           (equal (expand-file-name todo-file-do) bufname)))
       (find-file todo-file-do)
     (todo-initial-setup))
   (if (null todo-categories)