Subject: Re: bug#17482: args-out-of-range when visiting foo.todo

From: Stephen Berman <[email-masked]>
Date: Tue, 13 May 2014 17:57:04 +0200

On Tue, 13 May 2014 02:22:35 -0400 Glenn Morris <[email-masked]> wrote:

> Package: emacs
> Version: 24.3.91
> Severity: important
>
> emacs -Q
> C-x C-f /tmp/foo.todo
>   -> args-out-of-range error
>
> (.todo is a pretty generic extension for me. I don't want it to open a
> specialized Emacs mode. I don't feel strongly about it though.
> Perhaps the NEWS file should say that .todo files now open in todo-mode.
> They didn't before.)

Perhaps it was presumptuous of me to appropriate the .todo extension for
Todo mode.  I'm not aware of any policy about this, and I do see that a
number of modes add an extension to auto-mode-alist unconditionally,
while others merely suggest doing that in the user's init file.
However, I've come up with the following fix, which keeps automatic
recognition of Todo mode files located in `todo-directory', which is
where all Todo mode files are stored, but allows you to create and visit
foo.todo in the default major mode anywhere else.  If this works for you
and it's not an unacceptable abuse of auto-mode-alist, I'll commit it to
emacs-24.  Thanks for the report.

Steve Berman

=== modified file 'lisp/calendar/todo-mode.el'
*** lisp/calendar/todo-mode.el	2014-05-09 07:50:42 +0000
--- lisp/calendar/todo-mode.el	2014-05-13 15:37:39 +0000
***************
*** 6401,6411 ****
  
  (defun todo-display-as-todo-file ()
    "Show todo files correctly when visited from outside of Todo mode.
! Added to `find-file-hook' in Todo mode and Todo Archive mode."
!   (and (member this-command todo-visit-files-commands)
!        (= (- (point-max) (point-min)) (buffer-size))
!        (member major-mode '(todo-mode todo-archive-mode))
!        (todo-category-select)))
  
  (defun todo-add-to-buffer-list ()
    "Add name of just visited todo file to `todo-file-buffers'.
--- 6401,6425 ----
  
  (defun todo-display-as-todo-file ()
    "Show todo files correctly when visited from outside of Todo mode.
! Added to `find-file-hook' in Todo mode, Todo Archive mode and
! Todo Filtered Items mode.
! 
! This is intended only for visiting an existing todo, archive, or
! filtered items file, and signals an error if you try to create a
! new one using, e.g., `find-file'."
!   (if (equal (file-truename todo-directory)
! 	     (file-name-directory (file-truename (buffer-file-name))))
!       (when (member this-command todo-visit-files-commands)
! 	(if (> (buffer-size) 0)
! 	    (and (= (- (point-max) (point-min)) (buffer-size))
! 		 (memq major-mode '(todo-mode todo-archive-mode))
! 		 (todo-category-select))
! 	  (kill-buffer)
! 	  (user-error "To add a new todo file, type %s in Todo mode"
! 		      (substitute-command-keys "\\[todo-add-file]"))))
!     (kill-all-local-variables)
!     (setq buffer-read-only nil)
!     (set-buffer-major-mode (current-buffer))))
  
  (defun todo-add-to-buffer-list ()
    "Add name of just visited todo file to `todo-file-buffers'.
***************
*** 6454,6459 ****
--- 6468,6474 ----
    "Make some settings that apply to multiple Todo modes."
    (add-to-invisibility-spec 'todo)
    (setq buffer-read-only t)
+   (add-hook 'find-file-hook 'todo-display-as-todo-file nil t)
    (when (and (boundp 'desktop-save-mode) desktop-save-mode)
      (setq-local desktop-save-buffer 'todo-desktop-save-buffer))
    (when (boundp 'hl-line-range-function)
***************
*** 6466,6473 ****
  (defun todo-modes-set-3 ()
    "Make some settings that apply to multiple Todo modes."
    (setq-local todo-categories (todo-set-categories))
!   (setq-local todo-category-number 1)
!   (add-hook 'find-file-hook 'todo-display-as-todo-file nil t))
  
  (put 'todo-mode 'mode-class 'special)
  
--- 6481,6487 ----
  (defun todo-modes-set-3 ()
    "Make some settings that apply to multiple Todo modes."
    (setq-local todo-categories (todo-set-categories))
!   (setq-local todo-category-number 1))
  
  (put 'todo-mode 'mode-class 'special)
  
***************
*** 6555,6566 ****
    (todo-modes-set-1)
    (todo-modes-set-2))
  
  ;;;###autoload
! (add-to-list 'auto-mode-alist '("\\.todo\\'" . todo-mode))
  ;;;###autoload
! (add-to-list 'auto-mode-alist '("\\.toda\\'" . todo-archive-mode))
  ;;;###autoload
! (add-to-list 'auto-mode-alist '("\\.tod[tyr]\\'" . todo-filtered-items-mode))
  
  ;; -----------------------------------------------------------------------------
  (provide 'todo-mode)
--- 6569,6607 ----
    (todo-modes-set-1)
    (todo-modes-set-2))
  
+ ;; All Todo mode files reside in `todo-directory', but maybe the user
+ ;; wants to be able to have files with these extensions in other modes
+ ;; elsewhere (bug#17482).  So we conditionalize automatic major mode
+ ;; selection on the file's location.
+ 
+ ;;;###autoload
+ (defun todo-maybe-todo-mode ()
+   (if (equal (file-truename todo-directory)
+ 	     (file-name-directory (file-truename (buffer-file-name))))
+       (todo-mode)
+     (set-buffer-major-mode (current-buffer))))
+ 
+ ;;;###autoload
+ (defun todo-maybe-todo-archive-mode ()
+   (if (equal (file-truename todo-directory)
+ 	     (file-name-directory (file-truename (buffer-file-name))))
+       (todo-archive-mode)
+     (set-buffer-major-mode (current-buffer))))
+ 
+ ;;;###autoload
+ (defun todo-maybe-todo-filtered-items-mode ()
+   (if (equal (file-truename todo-directory)
+ 	     (file-name-directory (file-truename (buffer-file-name))))
+       (todo-filtered-items-mode)
+     (set-buffer-major-mode (current-buffer))))
+ 
  ;;;###autoload
! (add-to-list 'auto-mode-alist '("\\.todo\\'" . todo-maybe-todo-mode))
  ;;;###autoload
! (add-to-list 'auto-mode-alist '("\\.toda\\'" . todo-maybe-todo-archive-mode))
  ;;;###autoload
! (add-to-list 'auto-mode-alist
! 	     '("\\.tod[tyr]\\'" . todo-maybe-todo-filtered-items-mode))
  
  ;; -----------------------------------------------------------------------------
  (provide 'todo-mode)