From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: replacing a function with another one Date: Mon, 10 Mar 2014 08:44:38 -0400 Message-ID: References: <87vbvofsi6.fsf@yun.yagibdah.de> <87bnxgs4r9.fsf@web.de> <87lhwj1cfz.fsf@yun.yagibdah.de> <87zjkz6vd5.fsf@web.de> <8738ir161u.fsf@yun.yagibdah.de> <87eh2b6nfm.fsf@web.de> <87r46anab5.fsf@yun.yagibdah.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1394455513 6076 80.91.229.3 (10 Mar 2014 12:45:13 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 10 Mar 2014 12:45:13 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Mar 10 13:45:21 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WMza5-0005OT-CX for geh-help-gnu-emacs@m.gmane.org; Mon, 10 Mar 2014 13:45:21 +0100 Original-Received: from localhost ([::1]:48512 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WMza5-00076v-2H for geh-help-gnu-emacs@m.gmane.org; Mon, 10 Mar 2014 08:45:21 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47115) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WMzZn-0006wh-9u for help-gnu-emacs@gnu.org; Mon, 10 Mar 2014 08:45:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WMzZg-0007aZ-0R for help-gnu-emacs@gnu.org; Mon, 10 Mar 2014 08:45:03 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:49565) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WMzZf-0007aP-Q1 for help-gnu-emacs@gnu.org; Mon, 10 Mar 2014 08:44:55 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WMzZc-0004tn-6K for help-gnu-emacs@gnu.org; Mon, 10 Mar 2014 13:44:52 +0100 Original-Received: from 76-10-154-114.dsl.teksavvy.com ([76.10.154.114]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 10 Mar 2014 13:44:52 +0100 Original-Received: from monnier by 76-10-154-114.dsl.teksavvy.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 10 Mar 2014 13:44:52 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 61 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 76-10-154-114.dsl.teksavvy.com User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) Cancel-Lock: sha1:i39trZ9RIKzb/JtkqTnA2vVcL7c= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:96394 Archived-At: > (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