* calendar/todos.el (todos-insert-item): Fix last change to make
[bpt/emacs.git] / lisp / calendar / todos.el
index a67a98e..aab5b1a 100644 (file)
@@ -212,21 +212,14 @@ These reflect the priorities of the items in each category."
 (defun todos-reset-prefix (symbol value)
   "The :set function for `todos-prefix' and `todos-number-priorities'."
   (let ((oldvalue (symbol-value symbol))
-       (files (append todos-files todos-archives)))
+       (files todos-file-buffers))
     (custom-set-default symbol value)
     (when (not (equal value oldvalue))
       (dolist (f files)
        (with-current-buffer (find-file-noselect f)
-         (save-window-excursion
-           (todos-show)
-           (save-excursion
-             (widen)
-             (goto-char (point-min))
-             (while (not (eobp))
-               (remove-overlays (point) (point)); 'before-string prefix)
-               (forward-line)))
-           ;; Activate the new setting (save-restriction does not help).
-           (save-excursion (todos-category-select))))))))
+         (remove-overlays 1 (1+ (buffer-size)) 'todos 'prefix)
+         ;; Activate the new setting in the current category.
+         (save-excursion (todos-category-select)))))))
 
 (defcustom todos-item-mark "*"
   "String used to mark items.
@@ -539,6 +532,8 @@ items in that category, which overrides NUM."
   :type 'sexp
   :group 'todos-filtered)
 
+;; FIXME: rename to todos-top-priorities AFTER renaming command
+;; todos-top-priorities to todos-filter-top-priorities
 (defcustom todos-show-priorities 1
   "Default number of top priorities shown by `todos-top-priorities'."
   :type 'integer
@@ -618,7 +613,7 @@ categories display according to priority."
   :group 'todos-categories)
 
 ;; ---------------------------------------------------------------------------
-;;; Faces and font-lock matcher functions
+;;; Faces and font locking
 
 (defgroup todos-faces nil
   "Faces for the Todos modes."
@@ -1157,11 +1152,14 @@ number as its value."
   "Return string used as value of variable `todos-done-separator'."
   (let ((sep todos-done-separator-string))
     (propertize (if (= 1 (length sep))
-                   ;; If separator's length is window-width, an
-                   ;; indented empty line appears between the
-                   ;; separator and the first done item.
-                   ;; FIXME: should this be customizable?
+                   ;; If separator's length is window-width, then
+                   ;; with non-nil todos-wrap-lines and
+                   ;; todos-wrap-and-indent as value of
+                   ;; todos-line-wrapping-function, an indented empty
+                   ;; line appears between the separator and the
+                   ;; first done item.
                    (make-string (1- (window-width)) (string-to-char sep))
+                   ;; (make-string (window-width) (string-to-char sep))
                  todos-done-separator-string)
                'face 'todos-done-sep)))
 
@@ -1181,14 +1179,16 @@ done items are shown.  Its value is determined by user option
              (concat "\n\\(" (regexp-quote todos-category-done) "\\)") nil t)
        (let* ((beg (match-beginning 1))
               (end (match-end 0))
-              (ovs (overlays-at beg))
-              (ov (when ovs (car ovs)))
+              (ov (progn (goto-char beg)
+                         (todos-get-overlay 'separator)))
               (old-sep (when ov (overlay-get ov 'display)))
               new-ov)
-         (when (string= old-sep sep)
-           (setq new-ov (make-overlay beg end))
-           (overlay-put new-ov 'display todos-done-separator)
-           (delete-overlay ov)))))))
+         (when old-sep
+           (unless (string= old-sep sep)
+             (setq new-ov (make-overlay beg end))
+             (overlay-put new-ov 'todos 'separator)
+             (overlay-put new-ov 'display todos-done-separator)
+             (delete-overlay ov))))))))
 
 (defun todos-category-completions ()
   "Return a list of completions for `todos-read-category'.
@@ -1259,17 +1259,12 @@ Todos files named in `todos-category-completions-files'."
        ;; Make display overlay for done items separator string, unless there
        ;; already is one.
        (let* ((done-sep todos-done-separator)
-              (ovs (overlays-at done-sep-start))
-              ;; ov-sep0 ov-sep1)
-              ov-sep)
-         ;; There should never be more than one overlay here, so car suffices.
-         (unless (and ovs (string= (overlay-get (car ovs) 'display) done-sep))
-           (setq ov-sep (make-overlay done-sep-start done-end))
-           (overlay-put ov-sep 'display done-sep))))
-           ;; (setq ov-sep0 (make-overlay done-sep-start done-end))
-           ;; (setq ov-sep1 (make-overlay done-end done-end))
-           ;; (overlay-put ov-sep0 'invisible t)
-           ;; (overlay-put ov-sep1 'after-string done-sep)))
+              (ov (progn (goto-char done-sep-start)
+                         (todos-get-overlay 'separator))))
+         (unless ov
+           (setq ov (make-overlay done-sep-start done-end))
+           (overlay-put ov 'todos 'separator)
+           (overlay-put ov 'display done-sep))))
       (narrow-to-region (point-min) done-start)
       ;; Loading this from todos-mode, or adding it to the mode hook, causes
       ;; Emacs to hang in todos-item-start, at (looking-at todos-item-start).
@@ -1517,11 +1512,10 @@ The final element is \"*\", indicating an unspecified month.")
 
 (defun todos-remove-item ()
   "Internal function called in editing, deleting or moving items."
-  (let* ((beg (todos-item-start))
-        (end (progn (todos-item-end) (1+ (point))))
-        (ovs (overlays-in beg beg)))
-    ;; There can be both prefix/number and mark overlays.
-    (while ovs (delete-overlay (car ovs)) (pop ovs))
+  (let* ((end (progn (todos-item-end) (1+ (point))))
+        (beg (todos-item-start))
+        (ov (todos-get-overlay 'prefix)))
+    (when ov (delete-overlay ov))
     (delete-region beg end)))
 
 (defun todos-diary-item-p ()
@@ -1545,20 +1539,24 @@ The final element is \"*\", indicating an unspecified month.")
        (progn (goto-char (point-min))
               (looking-at todos-done-string-start)))))
 
-(defun todos-prefix-overlay ()
-  "Return this item's prefix overlay."
-  ;; Why doesn't this work?
-  ;; (get-char-property-and-overlay lbp 'before-string)
-  (let* ((lbp (line-beginning-position))
-        (ovs (overlays-in lbp lbp)))
-    (car ovs)))
+(defun todos-get-overlay (val)
+  "Return the overlay at point whose `todos' property has value VAL."
+  ;; Use overlays-in to find prefix overlays and check over two
+  ;; positions to find done separator overlay.
+  (let ((ovs (overlays-in (point) (1+ (point))))
+       ov)
+    (catch 'done
+      (while ovs
+       (setq ov (pop ovs))
+       (when (eq (overlay-get ov 'todos) val)
+         (throw 'done ov))))))
 
 (defun todos-marked-item-p ()
   "Non-nil if this item begins with `todos-item-mark'.
  In that case, return the item's prefix overlay."
   ;; If a todos-item-insert command is called on a Todos file before
   ;; it is visited, it has no prefix overlays, so conditionalize:
-  (let* ((ov (todos-prefix-overlay))
+  (let* ((ov (todos-get-overlay 'prefix))
         (pref (when ov (overlay-get ov 'before-string)))
         (marked (when pref
                   (string-match (concat "^" (regexp-quote todos-item-mark))
@@ -1571,7 +1569,7 @@ The final element is \"*\", indicating an unspecified month.")
   ;; Insertion pushes item down but not its prefix overlay.  When the
   ;; overlay includes a mark, this would now mark the inserted ITEM,
   ;; so move it to the pushed down item.
-  (let ((ov (todos-prefix-overlay))
+  (let ((ov (todos-get-overlay 'prefix))
        (marked (todos-marked-item-p)))
     (insert item "\n")
     (when marked (move-overlay ov (point) (point))))
@@ -1585,41 +1583,45 @@ The overlay's value is the string `todos-prefix' or with non-nil
 the number of todo or done items in the category indicating the
 item's priority.  Todo and done items are numbered independently
 of each other."
-  (let ((prefix (propertize (concat todos-prefix " ")
-                           'face 'todos-prefix-string))
-       (num 0)
+  (let ((num 0)
        (cat-tp (or (cdr (assoc-string
                          (todos-current-category)
                          (nth 2 (assoc-string todos-current-todos-file
                                               todos-priorities-rules))))
                    todos-show-priorities))
-       done)
+       done prefix)
     (save-excursion
       (goto-char (point-min))
       (while (not (eobp))
        (when (or (todos-date-string-matcher (line-end-position))
                  (todos-done-string-matcher (line-end-position)))
          (goto-char (match-beginning 0))
-         (when todos-number-priorities
-           (setq num (1+ num))
-           ;; Reset number to 1 for first done item.
-           (when (and (looking-at todos-done-string-start)
-                      (looking-back (concat "^"
-                                            (regexp-quote todos-category-done)
-                                            "\n")))
-             (setq num 1
-                   done t))
-           (setq prefix (propertize (concat (number-to-string num) " ")
-                                    'face
-                                    ;; Numbers of top priorities have
-                                    ;; a distinct face in Todos mode.
-                                    (if (and (not done) (<= num cat-tp)
-                                             (eq major-mode 'todos-mode))
-                                        'todos-top-priority
-                                      'todos-prefix-string))))
-         (let ((ov (todos-prefix-overlay))
+         (setq num (1+ num))
+         ;; Reset number to 1 for first done item.
+         (when (and (looking-at todos-done-string-start)
+                    (looking-back (concat "^"
+                                          (regexp-quote todos-category-done)
+                                          "\n")))
+           (setq num 1
+                 done t))
+         (setq prefix (concat (propertize
+                               (if todos-number-priorities
+                                   (number-to-string num)
+                                 todos-prefix)
+                               'face
+                               ;; Prefix of top priority items has a
+                               ;; distinct face in Todos mode.
+                               (if (and (not done) (<= num cat-tp)
+                                        (eq major-mode 'todos-mode))
+                                   'todos-top-priority
+                                 'todos-prefix-string))
+                              " "))
+         (let ((ov (todos-get-overlay 'prefix))
                (marked (todos-marked-item-p)))
+           ;; Prefix overlay must be at a single position so its
+           ;; bounds aren't changed when (re)moving an item.
            (unless ov (setq ov (make-overlay (point) (point))))
+           (overlay-put ov 'todos 'prefix)
            (overlay-put ov 'before-string (if marked
                                               (concat todos-item-mark prefix)
                                             prefix))))
@@ -2189,41 +2191,43 @@ priority has changed or its text was truncated or augmented, and
                   todos-global-current-todos-file)))
     (find-file-noselect file)
     (with-current-buffer (find-buffer-visiting file)
-      (widen)
-      (goto-char (point-min))
-      (let ((beg (re-search-forward
-                 (concat "^" (regexp-quote (concat todos-category-beg cat)) "$")
-                 nil t))
-           (done (save-excursion
-                   (re-search-forward
-                    (concat "^" (regexp-quote todos-category-done)) nil t)))
-           (end (save-excursion
-                  (or (re-search-forward
-                       (concat "^" (regexp-quote todos-category-beg))
-                       nil t)
-                      (point-max)))))
-       (setq found (when (search-forward str end t)
-                     (goto-char (match-beginning 0))))
-       (when found
-          (setq found
-                (cons found (if (> (point) done)
-                                'done
-                              (let ((cpriority 1))
-                                (save-excursion
-                                  ;; Not top item in category.
-                                  (while (> (point) (1+ beg))
-                                    (let ((opoint (point)))
-                                      (todos-backward-item)
-                                      ;; Can't move backward beyond
-                                      ;; first item in file.
-                                      (unless (= (point) opoint)
-                                        (setq cpriority (1+ cpriority))))))
-                                (if (and (= tpriority cpriority)
-                                         ;; Proper substring is not the same.
-                                         (string= (todos-item-string)
-                                                  str))
-                                    'same
-                                  'changed))))))))
+      (save-restriction
+       (widen)
+       (goto-char (point-min))
+       (let ((beg (re-search-forward
+                   (concat "^" (regexp-quote (concat todos-category-beg cat))
+                           "$")
+                   nil t))
+             (done (save-excursion
+                     (re-search-forward
+                      (concat "^" (regexp-quote todos-category-done)) nil t)))
+             (end (save-excursion
+                    (or (re-search-forward
+                         (concat "^" (regexp-quote todos-category-beg))
+                         nil t)
+                        (point-max)))))
+         (setq found (when (search-forward str end t)
+                       (goto-char (match-beginning 0))))
+         (when found
+           (setq found
+                 (cons found (if (> (point) done)
+                                 'done
+                               (let ((cpriority 1))
+                                 (save-excursion
+                                   ;; Not top item in category.
+                                   (while (> (point) (1+ beg))
+                                     (let ((opoint (point)))
+                                       (todos-backward-item)
+                                       ;; Can't move backward beyond
+                                       ;; first item in file.
+                                       (unless (= (point) opoint)
+                                         (setq cpriority (1+ cpriority))))))
+                                 (if (and (= tpriority cpriority)
+                                          ;; Proper substring is not the same.
+                                          (string= (todos-item-string)
+                                                   str))
+                                     'same
+                                   'changed)))))))))
       (list found file cat)))
 
 (defun todos-check-top-priorities ()
@@ -2623,6 +2627,7 @@ which is the value of the user option
    (replace-regexp-in-string
     "-+" "-"
     (concat "todos-item-insert-"
+    ;; (concat "todos-insert-item-"
            (mapconcat (lambda (e) (if e (symbol-name e))) arglist "-")))))
 
 (defvar todos-insertion-commands-names
@@ -2675,6 +2680,7 @@ which is the value of the user option
                    (setq key (concat key key1)))))
            todos-insertion-commands-arg-key-list)
       (if (string-match (concat (regexp-quote "todos-item-insert") "\\_>") cname)
+      ;; (if (string-match (concat (regexp-quote "todos-insert-item") "\\_>") cname)
          (setq key (concat key "i")))
       (define-key map key c))))
 
@@ -2720,6 +2726,7 @@ which is the value of the user option
     ;;               navigation                        
     ("f"            . todos-forward-category)
     ("b"            . todos-backward-category)
+    ("t"             . todos-show)
     ("j"            . todos-jump-to-category)
     ("n"            . todos-forward-item)
     ("p"            . todos-backward-item)
@@ -2864,17 +2871,19 @@ which is the value of the user option
 (defvar todos-categories-mode-map
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map t)
+    ;; (define-key map "c" 'todos-display-categories-numberically-or-alphabetically)
     (define-key map "c" 'todos-display-categories-alphabetically-or-by-priority)
     (define-key map "t" 'todos-display-categories-sorted-by-todo)
     (define-key map "y" 'todos-display-categories-sorted-by-diary)
     (define-key map "d" 'todos-display-categories-sorted-by-done)
     (define-key map "a" 'todos-display-categories-sorted-by-archived)
+    (define-key map "#" 'todos-set-category-priority)
     (define-key map "l" 'todos-lower-category-priority)
     (define-key map "+" 'todos-lower-category-priority)
     (define-key map "r" 'todos-raise-category-priority)
     (define-key map "-" 'todos-raise-category-priority)
-    (define-key map "n" 'todos-forward-button)
-    (define-key map "p" 'todos-backward-button)
+    (define-key map "n" 'todos-forward-button) ; todos-next-button
+    (define-key map "p" 'todos-backward-button) ; todos-previous-button
     (define-key map [tab] 'todos-forward-button)
     (define-key map [backtab] 'todos-backward-button)
     (define-key map "q" 'todos-quit)
@@ -3023,11 +3032,14 @@ which is the value of the user option
 ;;;###autoload
 (defun todos-show (&optional solicit-file)
   "Visit a Todos file and display one of its categories.
-With non-nil prefix argument SOLICIT-FILE prompt for which todo
-file to visit; otherwise visit `todos-default-todos-file'.
-Subsequent invocations from outside of Todos mode revisit this
-file or, with option `todos-show-current-file' non-nil (the
-default), whichever Todos file was last visited.
+
+When invoked in Todos mode, prompt for which todo file to visit.
+When invoked outside of Todos mode with non-nil prefix argument
+SOLICIT-FILE prompt for which todo file to visit; otherwise visit
+`todos-default-todos-file'.  Subsequent invocations from outside
+of Todos mode revisit this file or, with option
+`todos-show-current-file' non-nil (the default), whichever Todos
+file was last visited.
 
 Calling this command before any Todos file exists prompts for a
 file name and an initial category (defaulting to
@@ -3053,7 +3065,8 @@ corresponding Todos file, displaying the corresponding category."
   (interactive "P")
   (let* ((cat)
         (show-first todos-show-first)
-        (file (cond (solicit-file
+        (file (cond ((or (eq major-mode 'todos-mode)
+                         solicit-file)
                      (if (funcall todos-files-function)
                          (todos-read-file-name "Choose a Todos file to visit: "
                                                nil t)
@@ -3245,7 +3258,7 @@ buries it and restores state as needed."
           (kill-buffer buf)))
        ((eq major-mode 'todos-filtered-items-mode)
         (kill-buffer)
-        (todos-show))
+        (unless (eq major-mode 'todos-mode) (todos-show)))
        ((member major-mode (list 'todos-mode 'todos-archive-mode))
         ;; Have to write previously nonexistant archives to file, and might
         ;; as well save Todos file also.
@@ -3764,26 +3777,21 @@ face."
   (save-excursion
     (save-restriction
       (goto-char (point-min))
-      (let ((ovs (overlays-in (point) (1+ (point))))
-           ov hidden)
-       (while ovs
-         (setq ov (pop ovs))
-         (if (equal (overlay-get ov 'display) "")
-             (setq ovs nil hidden t)))
+      (if (todos-get-overlay 'header)
+         (remove-overlays 1 (1+ (buffer-size)) 'todos 'header)
        (widen)
        (goto-char (point-min))
-       (if hidden
-           (remove-overlays (point-min) (point-max) 'display "")
-         (while (not (eobp))
-           (when (re-search-forward
-                  (concat todos-date-string-start todos-date-pattern
-                          "\\( " diary-time-regexp "\\)?"
-                          (regexp-quote todos-nondiary-end) "? ")
-                  nil t)
-             (unless (save-match-data (todos-done-item-p))
-               (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
-               (overlay-put ov 'display "")))
-           (todos-forward-item)))))))
+       (while (not (eobp))
+         (when (re-search-forward
+                (concat todos-date-string-start todos-date-pattern
+                        "\\( " diary-time-regexp "\\)?"
+                        (regexp-quote todos-nondiary-end) "? ")
+                nil t)
+           (unless (save-match-data (todos-done-item-p))
+             (setq ov (make-overlay (match-beginning 0) (match-end 0) nil t))
+             (overlay-put ov 'todos 'header)
+             (overlay-put ov 'display "")))
+         (todos-forward-item))))))
 
 (defun todos-mark-unmark-item (&optional n)
   "Mark item with `todos-item-mark' if unmarked, otherwise unmark it.
@@ -3794,7 +3802,7 @@ marking of the next N items."
   (dotimes (i n)
     (let* ((cat (todos-current-category))
           (marks (assoc cat todos-categories-with-marks))
-          (ov (todos-prefix-overlay))
+          (ov (todos-get-overlay 'prefix))
           (pref (overlay-get ov 'before-string)))
       (if (todos-marked-item-p)
          (progn
@@ -3817,7 +3825,7 @@ marking of the next N items."
     (while (not (eobp))
       (let* ((cat (todos-current-category))
             (marks (assoc cat todos-categories-with-marks))
-            (ov (todos-prefix-overlay))
+            (ov (todos-get-overlay 'prefix))
             (pref (overlay-get ov 'before-string)))
        (unless (todos-marked-item-p)
          (overlay-put ov 'before-string (concat todos-item-mark pref))
@@ -3834,7 +3842,7 @@ marking of the next N items."
     (while (not (eobp))
       (let* ((cat (todos-current-category))
             (marks (assoc cat todos-categories-with-marks))
-            (ov (todos-prefix-overlay))
+            (ov (todos-get-overlay 'prefix))
             (pref (overlay-get ov 'before-string)))
        (when (todos-marked-item-p)
          (overlay-put ov 'before-string (substring pref 1))
@@ -4117,7 +4125,7 @@ i.e. including all existing todo and done items."
                                     "the archived category will remain\n"
                                     "after deleting the todo category.  "
                                     "Do you still want to delete it\n"
-                                    "(see 'todos-skip-archived-categories' "
+                                    "(see `todos-skip-archived-categories' "
                                     "for another option)? ")))
                  (t
                   (y-or-n-p (concat "Permanently remove category \"" cat
@@ -4530,7 +4538,9 @@ the new item:
   category, insert it directly above the todo item at
   point (hence lowering the priority of the remaining items), or
   if point is on the empty line below the last todo item, insert
-  the new item there.  If the command with `here' is invoked
+  the new item there.  If point is in the done items section of
+  the category, insert the new item as the first todo item in the
+  category.  Likewise, if the command with `here' is invoked
   outside of the current category, jump to the chosen category
   and insert the new item as the first item in the category.
 - If REGION-OR-HERE is the symbol `region', use the region of the
@@ -4572,8 +4582,9 @@ the priority is not given by HERE but by prompting."
        (let (use-empty-active-region)
          (unless (and todos-use-only-highlighted-region (use-region-p))
            (error "There is no active region"))))
-      (let* ((buf (current-buffer))
+      (let* ((obuf (current-buffer))
             (ocat (todos-current-category))
+            (opoint (point))
             (todos-mm (eq major-mode 'todos-mode))
             (cat+file (cond ((equal arg '(4))
                              (todos-read-category "Insert in category: "))
@@ -4583,6 +4594,8 @@ the priority is not given by HERE but by prompting."
                             (t
                              (cons (todos-current-category)
                                    (or todos-current-todos-file
+                                       (and todos-show-current-file
+                                            todos-global-current-todos-file)
                                        (todos-absolute-file-name
                                         todos-default-todos-file))))))
             (cat (car cat+file))
@@ -4621,10 +4634,6 @@ the priority is not given by HERE but by prompting."
        (setq todos-current-todos-file file)
        (unless todos-global-current-todos-file
          (setq todos-global-current-todos-file todos-current-todos-file))
-       ;; These are not needed here, since they are called in
-       ;; todos-set-item-priority.
-       ;; (todos-category-number cat)
-       ;; (todos-category-select)
        (let ((buffer-read-only nil)
              done-only item-added)
          (setq new-item
@@ -4644,37 +4653,50 @@ the priority is not given by HERE but by prompting."
                          "\\(\n\\)[^[:blank:]]"
                          (concat "\n" (make-string todos-indent-to-here 32))
                          new-item nil nil 1))
-         (if here
-             (if (or (todos-done-item-p) (todos-done-item-section-p))
-                 (error "Cannot insert item in done items section")
-               (unless (and todos-mm (equal cat ocat))
-                 (todos-category-number cat)
-                 (todos-category-select)
-                 (goto-char (point-min)))
-               (todos-insert-with-overlays new-item))
-           (unwind-protect
-               (progn
-                 ;; If only done items are displayed in category,
-                 ;; toggle to todo items.
-                 (when (and (goto-char (point-min))
-                              (looking-at todos-done-string-start))
-                   (setq done-only t)
-                   (todos-show-done-only))
-                 (todos-set-item-priority new-item cat t)
-                 (setq item-added t))
-             ;; If user cancels before setting priority, restore
-             ;; display.
-             (unless item-added
-               (and done-only (todos-show-done-only)))
-             ;; If todos section is not visible when insertion
-             ;; command is called (either because only done items
-             ;; were shown or because category was not in current
-             ;; buffer), then if item is inserted at end of category,
-             ;; point is at eob and eob at window-start, so that that
-             ;; higher priority todo items are out of view.  So we
-             ;; recenter to make sure the todo items are displayed in
-             ;; the window.
-             (when item-added (recenter))))
+         (unwind-protect
+             (progn
+               ;; If only done items are displayed in category,
+               ;; toggle to todo items before inserting new item.
+               (when (save-excursion
+                       (goto-char (point-min))
+                       (looking-at todos-done-string-start))
+                 (setq done-only t)
+                 (todos-show-done-only))
+               (if here
+                   (progn
+                     ;; If command was invoked with point in done
+                     ;; items section or outside of the current
+                     ;; category, can't insert "here", so to be
+                     ;; useful give new item top priority.
+                     (when (or (todos-done-item-section-p)
+                               (prog1 (not (and todos-mm (equal cat ocat)))
+                                 (todos-category-number cat)
+                                 (todos-category-select))
+                               done-only)
+                       (goto-char (point-min)))
+                     (todos-insert-with-overlays new-item))
+                 (todos-set-item-priority new-item cat t))
+               (setq item-added t))
+           ;; If user cancels before setting priority, restore
+           ;; display.
+           (unless item-added
+             (if ocat
+                 (progn
+                   (unless (equal cat ocat)
+                     (todos-category-number ocat)
+                     (todos-category-select))
+                   (and done-only (todos-show-done-only)))
+               (set-window-buffer (selected-window) (set-buffer obuf)))
+             (goto-char opoint))
+           ;; If todos section is not visible when insertion
+           ;; command is called (either because only done items
+           ;; were shown or because category was not in current
+           ;; buffer), then if item is inserted at end of category,
+           ;; point is at eob and eob at window-start, so that that
+           ;; higher priority todo items are out of view.  So we
+           ;; recenter to make sure the todo items are displayed in
+           ;; the window.
+           (when item-added (recenter)))
          (todos-update-count 'todo 1)
          (if (or diary todos-include-in-diary) (todos-update-count 'diary 1))
          (todos-update-categories-sexp))))))
@@ -4815,55 +4837,6 @@ minibuffer; otherwise, edit it in Todos Edit mode."
          (todos-insert-with-overlays new)
          (move-to-column item-beg))))))
 
-;; (defun todos-edit-multiline-item ()
-;;   "Edit current Todo item in Todos Edit mode.
-;; Use of newlines invokes `todos-indent' to insure compliance with
-;; the format of Diary entries."
-;;   (interactive)
-;;   (todos-edit-multiline t))
-
-;; (defun todos-edit-multiline (&optional item) ;FIXME: not item editing command
-;;   ""                                        ;FIXME
-;;   (interactive)
-;;   (let ((buffer-name todos-edit-buffer))
-;;     (set-window-buffer
-;;      (selected-window)
-;;      (set-buffer (make-indirect-buffer
-;;               (file-name-nondirectory todos-current-todos-file)
-;;               buffer-name)))
-;;     (if item
-;;     (narrow-to-region (todos-item-start) (todos-item-end))
-;;       (widen))
-;;     (todos-edit-mode)
-;;     (message "%s" (substitute-command-keys
-;;                (concat "Type \\[todos-edit-quit] to check file format "
-;;                        "validity and return to Todos mode.\n")))))
-
-;; (defun todos-edit-quit ()
-;;   "Return from Todos Edit mode to Todos mode.
-;; If the item contains hard line breaks, make sure the following
-;; lines are indented by `todos-indent-to-here' to conform to diary
-;; format.
-
-;; If the whole file was in Todos Edit mode, check before returning
-;; whether the file is still a valid Todos file and if so, also
-;; recalculate the Todos categories sexp, in case changes were made
-;; in the number or names of categories."
-;;   (interactive)
-;;   (if (eq (buffer-size) (- (point-max) (point-min)))
-;;       (when (todos-check-format)
-;;     (todos-repair-categories-sexp))
-;;     ;; Ensure lines following hard newlines are indented.
-;;     (let ((item (replace-regexp-in-string
-;;              "\\(\n\\)[^[:blank:]]"
-;;              (concat "\n" (make-string todos-indent-to-here 32))
-;;              (buffer-string) nil nil 1)))
-;;       (delete-region (point-min) (point-max))
-;;       (insert item)))
-;;   (kill-buffer)
-;;   ;; In case next buffer is not the one holding todos-current-todos-file.
-;;   (todos-show))
-
 (defun todos-edit-multiline-item ()
   "Edit current Todo item in Todos Edit mode.
 Use of newlines invokes `todos-indent' to insure compliance with
@@ -4882,9 +4855,8 @@ the format of Diary entries."
   ""                                   ;FIXME
   (interactive)
   (widen)
-  (rename-buffer todos-edit-buffer)
   (todos-edit-mode)
-  (remove-overlays) ; nil nil 'before-string)
+  (remove-overlays)
   (message "%s" (substitute-command-keys
                 (concat "Type \\[todos-edit-quit] to check file format "
                         "validity and return to Todos mode.\n"))))
@@ -4919,7 +4891,6 @@ in the number or names of categories."
        ;; (todos-repair-categories-sexp)
        ;; Compare (todos-make-categories-list t) with sexp and if
        ;; different ask (todos-update-categories-sexp) ?
-       (rename-buffer (file-name-nondirectory todos-current-todos-file))
        (todos-mode)
        (let* ((cat-beg (concat "^" (regexp-quote todos-category-beg)
                                "\\(.*\\)$"))
@@ -5341,7 +5312,7 @@ meaning to raise or lower the item's priority by one."
                        (todos-get-count 'todo cat))))
           (maxnum (if new (1+ todo) todo))
           (prompt (format "Set item priority (1-%d): " maxnum))
-          (priority (cond ((numberp current-prefix-arg)
+          (priority (cond ((and (not arg) (numberp current-prefix-arg))
                            current-prefix-arg)
                           ((and (eq arg 'raise) (>= curnum 1))
                            (1- curnum))
@@ -5363,7 +5334,7 @@ meaning to raise or lower the item's priority by one."
                (goto-char (point-min))
                (setq done (re-search-forward todos-done-string-start nil t))))
            (let ((todos-show-with-done done))
-             (todos-category-select)))))
+             (todos-category-select))))
        ;; Prompt for priority only when the category has at least one todo item.
        (when (> maxnum 1)
          (while (not priority)
@@ -5414,9 +5385,9 @@ meaning to raise or lower the item's priority by one."
        (todos-insert-with-overlays item)
        ;; If item was marked, restore the mark.
        (and marked
-            (let* ((ov (todos-prefix-overlay))
+            (let* ((ov (todos-get-overlay 'prefix))
                    (pref (overlay-get ov 'before-string)))
-              (overlay-put ov 'before-string (concat todos-item-mark pref)))))))
+              (overlay-put ov 'before-string (concat todos-item-mark pref))))))))
 
 (defun todos-raise-item-priority ()
   "Raise priority of current item by moving it up by one item."
@@ -5606,14 +5577,17 @@ visible."
                            ""))
             (done-prefix (concat "[" todos-done-string date-string time-string
                                  "] "))
-            (comment (and arg (not marked) (read-string "Enter a comment: ")))
+            (comment (and arg (read-string "Enter a comment: ")))
             (item-count 0)
             (diary-count 0)
             (show-done (save-excursion
                          (goto-char (point-min))
                          (re-search-forward todos-done-string-start nil t)))
-            item done-item
-            (buffer-read-only))
+            (buffer-read-only nil)
+            item done-item opoint)
+       ;; Don't add empty comment to done item.
+       (setq comment (unless (zerop (length comment))
+                       (concat " [" todos-comment-string ": " comment "]")))
        (and marked (goto-char (point-min)))
        (catch 'done
          ;; Stop looping when we hit the empty line below the last
@@ -5622,14 +5596,8 @@ visible."
            (if (or (not marked) (and marked (todos-marked-item-p)))
                (progn
                  (setq item (todos-item-string))
-                 (setq done-item (cond (marked
-                                        (concat done-item done-prefix item "\n"))
-                                       (comment
-                                        (concat done-prefix item " ["
-                                                todos-comment-string
-                                                ": " comment "]"))
-                                       (t
-                                        (concat done-prefix item))))
+                 (setq done-item (concat done-item done-prefix item
+                                         comment (and marked "\n")))
                  (setq item-count (1+ item-count))
                  (when (todos-diary-item-p)
                    (setq diary-count (1+ diary-count)))
@@ -5646,13 +5614,16 @@ visible."
          (re-search-forward
           (concat "^" (regexp-quote todos-category-done)) nil t)
          (forward-char)
+         (when show-done (setq opoint (point)))
          (insert done-item "\n"))
        (todos-update-count 'todo (- item-count))
        (todos-update-count 'done item-count)
        (todos-update-count 'diary (- diary-count))
        (todos-update-categories-sexp)
        (let ((todos-show-with-done show-done))
-         (todos-category-select))))))
+         (todos-category-select)
+         ;; When done items are shown, put cursor on first just done item.
+         (when opoint (goto-char opoint)))))))
 
 (defun todos-done-item-add-edit-or-delete-comment (&optional arg)
   "Add a comment to this done item or edit an existing comment.
@@ -5683,7 +5654,7 @@ With prefix ARG delete an existing comment."
 (defun todos-item-undo ()
   "Restore this done item to the todo section of this category.
 If done item has a comment, ask whether to omit the comment from
-the restored item."
+the restored item."                    ;FIXME: marked done items
   (interactive)
   (let* ((cat (todos-current-category))
         (marked (assoc cat todos-categories-with-marks)))
@@ -5755,13 +5726,16 @@ the restored item."
          (unwind-protect
              (progn
                (todos-set-item-priority item (todos-current-category) t)
-               (setq undone t)
+               (setq undone t
+                     opoint (point))
                (todos-update-count 'todo 1)
                (todos-update-count 'done -1)
                (and (todos-diary-item-p) (todos-update-count 'diary 1))
                (todos-update-categories-sexp)
                (let ((todos-show-with-done (> (todos-get-count 'done) 0)))
-                 (todos-category-select)))
+                 (todos-category-select)
+                 ;; Put the cursor on the undone item.
+                 (goto-char opoint)))
            (unless undone
              (let ((todos-show-with-done t))
                (widen)