emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New version of todo-mode.el (announcement + user guide)


From: Stephen Berman
Subject: Re: New version of todo-mode.el (announcement + user guide)
Date: Thu, 13 Jun 2013 22:53:53 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

On Wed, 12 Jun 2013 21:18:29 -0400 Stefan Monnier <address@hidden> wrote:

>> Thanks for the go-ahead.  I do agree with you that a hook would be
>> better and in fact gave it some thought, but couldn't see how to do it
>> using an existing hook.  I didn't consider adding a new hook, and I'm
>> not sure I what that should be.  The problem is, after the button is
>> clicked in the Fancy Diary display, if the todo file is in a buffer, it
>> will probably be narrowed, so it has to be widened before going to the
>> position of the marker, and then it has to be narrowed again afterwards
>> to get the proper display.  So either there have to be two hooks
>> sandwiching the goto-char for the marker, or the latter has to be passed
>> to the hook function.  Neither really seems much better than the ad hoc
>> patch I posted, but it's not unlikely I'm overlooking a better
>> alternative.  If you have any ideas, I'm all ears.
>
> I think a way to do this is with a diary-goto-marker-function hook.
> The default value could be `goto-char'.  And your todo-mode can change
> it via something like:
>
>     (add-function :around diary-goto-marker-function
>                   (lambda (orig-fun &rest args)
>                     (when (derived-mode-p 'todo-mode) (widen))
>                     (apply orig-fun args)
>                     (todos-diary-goto-entry)))

Thank you for this excellent "advice" ;-).  It works very nicely --
except for one wrinkle: after loading todos.el (which requires
'diary-lib) I have to eval diary-goto-entry (or load diary-lib) again in
order for the advice to take effect.  Without doing that it's as if the
advice were not activated.  But I see nothing about activating advice in
nadvice.el.  Here's what I added to diary-lib.el:

(defvar diary-goto-location-function 'goto-char
  "Function called by `diary-goto-entry' to jump to a diary entry.
Major modes that require special handling of the source file of
the diary entry can assign a suitable function to this variable.")

(defun diary-goto-entry (button)
  "Jump to the diary entry for the BUTTON at point."
  (let* ((locator (button-get button 'locator))
         (marker (car locator))
         markbuf file)
    ;; If marker pointing to diary location is valid, use that.
    (if (and marker (setq markbuf (marker-buffer marker)))
        (progn
          (pop-to-buffer markbuf)
          (funcall diary-goto-location-function (marker-position marker)))
      ;; Marker is invalid (eg buffer has been killed).
      (or (and (setq file (cadr locator))
               (file-exists-p file)
               (find-file-other-window file)
               (progn
                 (when (eq major-mode (default-value 'major-mode)) (diary-mode))
                 (goto-char (point-min))
                 (if (re-search-forward (format "%s.*\\(%s\\)"
                                                (regexp-quote (nth 2 locator))
                                                (regexp-quote (nth 3 locator)))
                                        nil t)
                     (funcall diary-goto-location-function (match-beginning 
1)))))
          (message "Unable to locate this diary entry")))))

and here's what I added to todos-mode:

(define-derived-mode todos-mode special-mode "Todos"
  "Major mode for displaying, navigating and editing Todo lists.

\\{todos-mode-map}"
;; other initializations ...
  (add-function :around diary-goto-location-function
                (lambda (orig-fun &rest args)
                  (when (derived-mode-p 'todos-mode) (widen))
                  (apply orig-fun args)
                  (todos-diary-goto-entry))))

What am I doing wrong?

Steve Berman



reply via email to

[Prev in Thread] Current Thread [Next in Thread]