help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: replacing a function with another one


From: Stefan Monnier
Subject: Re: replacing a function with another one
Date: Mon, 10 Mar 2014 08:44:38 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

> (defadvice hi-lock-set-file-patterns (after my-test-obtain-patterns)
>   ...
>   (ad-deactivate 'hi-lock-set-file-patterns)
[...]
> (defun my-test ()
>   ...
>   (ad-activate 'hi-lock-set-file-patterns)

I recommend you don't use ad-activate and ad-deactivate (among other
reasons because they activate/deactivate all advices applied to the
function rather than only the one you know about; but also because they
hide the existence of an advice).

Instead, use an auxiliary variable, e.g.

  (defvar my-set-patterns-in-original-buffer nil)
  (defadvice hi-lock-set-file-patterns (after my-test-obtain-patterns)
    (when my-set-patterns-in-original-buffer
      (setq my-set-patterns-in-original-buffer nil)
      ...))
  (defun my-test ()
    (setq my-set-patterns-in-original-buffer t)
    ...)
or
  (defvar my-set-patterns-in-original-buffer nil)
  (defadvice hi-lock-set-file-patterns (after my-test-obtain-patterns)
    (when my-set-patterns-in-original-buffer
      ...))
  (defun my-test ()
    (let ((my-set-patterns-in-original-buffer t))
      ...))

BTW, I recommend you then merge my-set-patterns-in-original-buffer and
original-buffer, so the advice is always active but only does something
if original-buffer is non-nil.  Also, use with-current-buffer rather
than pop-to-buffer (pop-to-buffer has *many* side-effects, so using it
in an advice is a recipe for disaster):

  (defvar my-set-patterns-original-buffer nil)
  (defadvice hi-lock-set-file-patterns (after my-test-obtain-patterns)
    (when my-set-patterns-original-buffer
      (with-current-buffer my-set-patterns-original-buffer
        (hi-lock-set-file-patterns (ad-get-arg 0)))))
  (defun my-test ()
    (save-excursion
      (save-restriction
        (widen)
        (goto-char (point-min))
        (when (re-search-forward "^// ext-fontify-defs: " nil t)
          (message "found marker")
          (let ((filename (thing-at-point 'filename)))
            (message "filename at %d: %s/%s for buffer %s" (point) filename 
(thing-at-point 'filename) (buffer-name))
            (when filename
              (when (file-exists-p filename)
              (let ((my-set-patterns-original-buffer (current-buffer)))
                ;; FIXME: usually from Lisp we should call find-file-noselect,
                ;; but I don't know what's the intention here.
                (find-file filename)
                (hi-lock-mode 1)))))))))

-- Stefan




reply via email to

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