Subject: bug#14688: 24.3; todo-show errors: wrong-type-argument and file-error

From: Stephen Berman <[email-masked]>
Date: Mon, 24 Jun 2013 00:18:52 +0200

On Fri 2013-06-21 23:58:09 +0200 I installed the new version of todo-mode.el.
On Sat, 22 Jun 2013 12:05:13 +0800 Leo Liu <[email-masked]> wrote:

> Make sure nothing is in ~/.emacs.d/todo
>
> 1. start emacs
> 2. todo-show and answer its queries and save the buffer
> 3. go to ~/.emacs.d/todo and delete files there
> 4. M-x todo-show
>
> you will get a couple of errors such as:
>
> Debugger entered--Lisp error: (wrong-type-argument stringp nil)
>   expand-file-name(nil)
>   find-file-noselect(nil)
>   todo-read-category("Enter a new category name: " add nil)
>   todo-add-category(nil "")
>   byte-code
>   todo-show(nil)
>   call-interactively(todo-show record nil)
>   command-execute(todo-show record)
>   execute-extended-command(nil "todo-show")
>   call-interactively(execute-extended-command nil nil)
>   command-execute(execute-extended-command)
>
> Debugger entered--Lisp error: (file-error "Removing old name" "no such file or
> directory" "/home/leo/.emacs.d/todo/TODO.todo")
>   delete-file("/home/leo/.emacs.d/todo/TODO.todo")
>   byte-code
>   byte-code
>   todo-show(nil)
>   call-interactively(todo-show record nil)
>   command-execute(todo-show record)
>   execute-extended-command(nil "todo-show")
>   call-interactively(execute-extended-command nil nil)
>   command-execute(execute-extended-command)

Well, it took all of six hours for the prediction I made[1] to come
true.  It's taken me quite a bit longer to come up with a fix, and I'm
not at all confident of its robustness.  Still, if you confirm that it
avoids the above errors, I'll probably commit it until I (or someone
else) come up with something better, or until you (or someone else) find
the next problem with it ;-).

The patch fixes some other problems beside those you reported, but which
arise similarly.  I found them when I tried to reproduce your recipe.
When I do exactly the above four steps in your recipe, I actually don't
get the errors you reported and the category is displayed normally.
However, if between steps 3 and 4 of your recipe I kill the buffer
visiting the todo file, then I do get the errors.  But if I don't kill
the buffer and also don't save it before trying to use other Todo mode
commands, other problems arise.  The patch tries to avoid these problems
as well as the ones you reported.

Steve Berman

Footnotes: 
[1] in this list on 11 June 2013:
>   since I'm the only one who's used the new version so far, there are
>   certain to be use cases and configurations I didn't test well enough
>   or even think of at all

=== modified file 'lisp/calendar/todo-mode.el'
*** lisp/calendar/todo-mode.el	2013-06-21 14:07:46 +0000
--- lisp/calendar/todo-mode.el	2013-06-23 22:09:25 +0000
***************
*** 578,588 ****
      (file-name-sans-extension (file-name-nondirectory file))))
  
  (defcustom todo-default-todo-file (todo-short-file-name
! 				     (car (funcall todo-files-function)))
    "Todo file visited by first session invocation of `todo-show'."
!   :type `(radio ,@(mapcar (lambda (f) (list 'const f))
! 			  (mapcar 'todo-short-file-name
! 				  (funcall todo-files-function))))
    :group 'todo)
  
  (defcustom todo-show-current-file t
--- 578,589 ----
      (file-name-sans-extension (file-name-nondirectory file))))
  
  (defcustom todo-default-todo-file (todo-short-file-name
! 				   (car (funcall todo-files-function)))
    "Todo file visited by first session invocation of `todo-show'."
!   :type (when todo-files
! 	  `(radio ,@(mapcar (lambda (f) (list 'const f))
! 			    (mapcar 'todo-short-file-name
! 				    (funcall todo-files-function)))))
    :group 'todo)
  
  (defcustom todo-show-current-file t
***************
*** 669,674 ****
--- 670,696 ----
  Invoking this command in Todo Archive mode visits the
  corresponding todo file, displaying the corresponding category."
    (interactive "P")
+   ;; If the user deletes the default todo file while its content is in
+   ;; a buffer and then tries to show it, ask whether to restore the
+   ;; file, else delete the buffer and cleanup to avoid errors and
+   ;; reinitialize a new default file.
+   (let* ((tdtf (todo-absolute-file-name todo-default-todo-file))
+ 	 (buf (and tdtf (find-buffer-visiting tdtf)))
+ 	 ans)
+     (when (and tdtf (not (file-exists-p tdtf)))
+       (if (and buf (y-or-n-p
+ 		    (concat
+ 		     (format (concat "Todo file \"%s\" has been deleted "
+ 				     "but its content is still in a buffer!\n")
+ 			     todo-default-todo-file)
+ 		     "Save that buffer and restore the todo file? ")))
+ 	  (with-current-buffer buf (save-buffer))
+ 	(setq todo-default-todo-file nil
+ 	      todo-files (todo-files))
+ 	(setq todo-default-todo-file (todo-short-file-name (car todo-files)))
+ 	(todo-reevaluate-default-file-defcustom)
+ 	(setq todo-global-current-todo-file nil) ; For todo-read-file-name.
+ 	(kill-buffer buf))))
    (catch 'shown
      ;; If there is a legacy todo file but no todo file in the current
      ;; format, offer to convert the legacy file and show it.
***************
*** 681,784 ****
  				     "old todo file to the new format? ")))
  	  (when (todo-convert-legacy-files)
  	    (throw 'shown nil)))))
!     (let* ((cat)
! 	   (show-first todo-show-first)
! 	   (file (cond ((or solicit-file
! 			    (and (called-interactively-p 'any)
! 				 (memq major-mode '(todo-mode
! 						    todo-archive-mode
! 						    todo-filtered-items-mode))))
! 			(if (funcall todo-files-function)
! 			    (todo-read-file-name "Choose a todo file to visit: "
! 						  nil t)
! 			  (user-error "There are no todo files")))
! 		       ((and (eq major-mode 'todo-archive-mode)
! 			     ;; Called noninteractively via todo-quit
! 			     ;; to jump to corresponding category in
! 			     ;; todo file.
! 			     (not (called-interactively-p 'any)))
! 			(setq cat (todo-current-category))
! 			(concat (file-name-sans-extension
! 				 todo-current-todo-file) ".todo"))
! 		       (t
! 			(or todo-current-todo-file
! 			    (and todo-show-current-file
! 				 todo-global-current-todo-file)
! 			    (todo-absolute-file-name todo-default-todo-file)
! 			    (todo-add-file)))))
! 	   add-item first-file)
!       (unless todo-default-todo-file
! 	;; We just initialized the first todo file, so make it the default.
! 	(setq todo-default-todo-file (todo-short-file-name file)
! 	      first-file t)
! 	(todo-reevaluate-default-file-defcustom))
!       (unless (member file todo-visited)
! 	;; Can't setq t-c-t-f here, otherwise wrong file shown when
! 	;; todo-show is called from todo-show-categories-table.
! 	(let ((todo-current-todo-file file))
! 	  (cond ((eq todo-show-first 'table)
! 		 (todo-show-categories-table))
! 		((memq todo-show-first '(top diary regexp))
! 		 (let* ((shortf (todo-short-file-name file))
! 			(fi-file (todo-absolute-file-name
! 				  shortf todo-show-first)))
! 		   (when (eq todo-show-first 'regexp)
! 		     (let ((rxfiles (directory-files todo-directory t
! 						     ".*\\.todr$" t)))
! 		       (when (and rxfiles (> (length rxfiles) 1))
! 			 (let ((rxf (mapcar 'todo-short-file-name rxfiles)))
! 			   (setq fi-file (todo-absolute-file-name
! 					  (completing-read
! 					   "Choose a regexp items file: "
! 					   rxf) 'regexp))))))
! 		   (if (file-exists-p fi-file)
! 		       (set-window-buffer
! 			(selected-window)
! 			(set-buffer (find-file-noselect fi-file 'nowarn)))
! 		     (message "There is no %s file for %s"
! 			      (cond ((eq todo-show-first 'top)
! 				     "top priorities")
! 				    ((eq todo-show-first 'diary)
! 				     "diary items")
! 				    ((eq todo-show-first 'regexp)
! 				     "regexp items"))
! 			      shortf)
! 		     (setq todo-show-first 'first)))))))
!       (when (or (member file todo-visited)
! 		(eq todo-show-first 'first))
! 	(set-window-buffer (selected-window)
! 			   (set-buffer (find-file-noselect file 'nowarn)))
! 	;; When quitting an archive file, show the corresponding
! 	;; category in the corresponding todo file, if it exists.
! 	(when (assoc cat todo-categories)
! 	  (setq todo-category-number (todo-category-number cat)))
! 	;; If this is a new todo file, add its first category.
! 	(when (zerop (buffer-size))
! 	  (let (cat-added)
! 	    (unwind-protect
! 		(setq todo-category-number
! 		      (todo-add-category todo-current-todo-file "")
! 		      add-item todo-add-item-if-new-category
! 		      cat-added t)
! 	      (if cat-added
! 		  ;; If the category was added, save the file now, so we
! 		  ;; don't risk having an empty todo file, which would
! 		  ;; signal an error if we tried to visit it later,
! 		  ;; since doing that looks for category boundaries.
! 		  (save-buffer 0)
! 		;; If user cancels before adding the category, clean up
! 		;; and exit, so we have a fresh slate the next time.
! 		(delete-file file)
! 		(setq todo-files (delete file todo-files))
! 		(when first-file
! 		  (setq todo-default-todo-file nil
! 			todo-current-todo-file nil))
! 		(kill-buffer)
! 		(keyboard-quit)))))
! 	(save-excursion (todo-category-select))
! 	(when add-item (todo-basic-insert-item)))
!       (setq todo-show-first show-first)
!       (add-to-list 'todo-visited file))))
  
  (defun todo-save ()
    "Save the current todo file."
--- 703,827 ----
  				     "old todo file to the new format? ")))
  	  (when (todo-convert-legacy-files)
  	    (throw 'shown nil)))))
!     (catch 'end
!       (let* ((cat)
! 	     (show-first todo-show-first)
! 	     (file (cond ((or solicit-file
! 			      (and (called-interactively-p 'any)
! 				   (memq major-mode '(todo-mode
! 						      todo-archive-mode
! 						      todo-filtered-items-mode))))
! 			  (if (funcall todo-files-function)
! 			      (todo-read-file-name "Choose a todo file to visit: "
! 						    nil t)
! 			    (user-error "There are no todo files")))
! 			 ((and (eq major-mode 'todo-archive-mode)
! 			       ;; Called noninteractively via todo-quit
! 			       ;; to jump to corresponding category in
! 			       ;; todo file.
! 			       (not (called-interactively-p 'any)))
! 			  (setq cat (todo-current-category))
! 			  (concat (file-name-sans-extension
! 				   todo-current-todo-file) ".todo"))
! 			 (t
! 			  (or todo-current-todo-file
! 			      (and todo-show-current-file
! 				   todo-global-current-todo-file)
! 			      (todo-absolute-file-name todo-default-todo-file)
! 			      (todo-add-file)))))
! 	     add-item first-file)
! 	(unless todo-default-todo-file
! 	  ;; We just initialized the first todo file, so make it the default.
! 	  (setq todo-default-todo-file (todo-short-file-name file)
! 		first-file t)
! 	  (todo-reevaluate-default-file-defcustom))
! 	(unless (member file todo-visited)
! 	  ;; Can't setq t-c-t-f here, otherwise wrong file shown when
! 	  ;; todo-show is called from todo-show-categories-table.
! 	  (let ((todo-current-todo-file file))
! 	    (cond ((eq todo-show-first 'table)
! 		   (todo-show-categories-table))
! 		  ((memq todo-show-first '(top diary regexp))
! 		   (let* ((shortf (todo-short-file-name file))
! 			  (fi-file (todo-absolute-file-name
! 				    shortf todo-show-first)))
! 		     (when (eq todo-show-first 'regexp)
! 		       (let ((rxfiles (directory-files todo-directory t
! 						       ".*\\.todr$" t)))
! 			 (when (and rxfiles (> (length rxfiles) 1))
! 			   (let ((rxf (mapcar 'todo-short-file-name rxfiles)))
! 			     (setq fi-file (todo-absolute-file-name
! 					    (completing-read
! 					     "Choose a regexp items file: "
! 					     rxf) 'regexp))))))
! 		     (if (file-exists-p fi-file)
! 			 (set-window-buffer
! 			  (selected-window)
! 			  (set-buffer (find-file-noselect fi-file 'nowarn)))
! 		       (message "There is no %s file for %s"
! 				(cond ((eq todo-show-first 'top)
! 				       "top priorities")
! 				      ((eq todo-show-first 'diary)
! 				       "diary items")
! 				      ((eq todo-show-first 'regexp)
! 				       "regexp items"))
! 				shortf)
! 		       (setq todo-show-first 'first)))))))
! 	(when (or (member file todo-visited)
! 		  (eq todo-show-first 'first))
! 	  ;; If the user deletes a file while its content is in a
! 	  ;; buffer and then tries to show it, ask whether to restore
! 	  ;; the file, else delete the buffer and cleanup to avoid
! 	  ;; errors.
! 	  (when (not (file-exists-p file))
! 	    (let ((buf (find-buffer-visiting file)))
! 	      (if (and buf
! 		       (y-or-n-p
! 			(concat
! 			 (format (concat "Todo file \"%s\" has been deleted but "
! 					 "its content is still in a buffer!\n")
! 				 (todo-short-file-name file))
! 			 "Save that buffer and restore the todo file? ")))
! 		  (with-current-buffer buf (save-buffer))
! 		(setq todo-current-todo-file nil
! 		      todo-global-current-todo-file nil
! 		      todo-files (todo-files))
! 		(kill-buffer buf)
! 		(throw 'end nil))))
! 	  (set-window-buffer (selected-window)
! 			     (set-buffer (find-file-noselect file 'nowarn)))
! 	  ;; When quitting an archive file, show the corresponding
! 	  ;; category in the corresponding todo file, if it exists.
! 	  (when (assoc cat todo-categories)
! 	    (setq todo-category-number (todo-category-number cat)))
! 	  ;; If this is a new todo file, add its first category.
! 	  (when (zerop (buffer-size))
! 	    (let (cat-added)
! 	      (unwind-protect
! 		  (setq todo-category-number
! 			(todo-add-category todo-current-todo-file "")
! 			add-item todo-add-item-if-new-category
! 			cat-added t)
! 		(if cat-added
! 		    ;; If the category was added, save the file now, so we
! 		    ;; don't risk having an empty todo file, which would
! 		    ;; signal an error if we tried to visit it later,
! 		    ;; since doing that looks for category boundaries.
! 		    (save-buffer 0)
! 		  ;; If user cancels before adding the category, clean up
! 		  ;; and exit, so we have a fresh slate the next time.
! 		  (delete-file file)
! 		  (setq todo-files (delete file todo-files))
! 		  (when first-file
! 		    (setq todo-default-todo-file nil
! 			  todo-current-todo-file nil)
! 		    (todo-reevaluate-default-file-defcustom))
! 		  (kill-buffer)
! 		  (keyboard-quit)))))
! 	  (save-excursion (todo-category-select))
! 	  (when add-item (todo-basic-insert-item)))
! 	(setq todo-show-first show-first)
! 	(add-to-list 'todo-visited file)))))
  
  (defun todo-save ()
    "Save the current todo file."
***************
*** 4678,4684 ****
  	  (unless todo-default-todo-file
  	    ;; We just initialized the first todo file, so make it the
  	    ;; default now to avoid an infinite recursion with todo-show.
! 	    (setq todo-default-todo-file (todo-short-file-name file)))
  	  (todo-show))))))
  
  ;; -----------------------------------------------------------------------------
--- 4721,4728 ----
  	  (unless todo-default-todo-file
  	    ;; We just initialized the first todo file, so make it the
  	    ;; default now to avoid an infinite recursion with todo-show.
! 	    (setq todo-default-todo-file (todo-short-file-name file))
! 	    (todo-reevaluate-default-file-defcustom))
  	  (todo-show))))))
  
  ;; -----------------------------------------------------------------------------
***************
*** 5868,5879 ****
  (defun todo-reevaluate-default-file-defcustom ()
    "Reevaluate defcustom of `todo-default-todo-file'.
  Called after adding or deleting a todo file."
!   (eval (defcustom todo-default-todo-file (car (funcall todo-files-function))
! 	  "Todo file visited by first session invocation of `todo-show'."
! 	  :type `(radio ,@(mapcar (lambda (f) (list 'const f))
! 				  (mapcar 'todo-short-file-name
! 					  (funcall todo-files-function))))
! 	  :group 'todo)))
  
  (defun todo-reevaluate-category-completions-files-defcustom ()
    "Reevaluate defcustom of `todo-category-completions-files'.
--- 5912,5926 ----
  (defun todo-reevaluate-default-file-defcustom ()
    "Reevaluate defcustom of `todo-default-todo-file'.
  Called after adding or deleting a todo file."
!   (eval
!    (defcustom todo-default-todo-file (todo-short-file-name
! 				      (car (funcall todo-files-function)))
!      "Todo file visited by first session invocation of `todo-show'."
!      :type (when todo-files
! 	     `(radio ,@(mapcar (lambda (f) (list 'const f))
! 			       (mapcar 'todo-short-file-name
! 				       (funcall todo-files-function)))))
!      :group 'todo)))
  
  (defun todo-reevaluate-category-completions-files-defcustom ()
    "Reevaluate defcustom of `todo-category-completions-files'.