* calendar/todos.el (todos-insert-item): Fix last change to make
[bpt/emacs.git] / lisp / calendar / todos.el
index 13249f9..aab5b1a 100644 (file)
@@ -532,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
@@ -611,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."
@@ -1150,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)))
 
@@ -2186,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 ()
@@ -2620,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
@@ -2672,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))))
 
@@ -2717,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)
@@ -2861,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)
@@ -3020,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
@@ -3050,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)
@@ -3242,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.
@@ -4109,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
@@ -4522,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
@@ -4564,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: "))
@@ -4575,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))
@@ -4613,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
@@ -4636,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))))))
@@ -4807,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
@@ -5331,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))
@@ -5353,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)
@@ -5406,7 +5387,7 @@ meaning to raise or lower the item's priority by one."
        (and marked
             (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."
@@ -5596,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
@@ -5612,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)))
@@ -5636,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.
@@ -5673,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)))
@@ -5745,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)