From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: lee Newsgroups: gmane.emacs.help Subject: Re: replacing a function with another one Date: Tue, 11 Mar 2014 05:11:18 +0100 Organization: my virtual residence Message-ID: <87siqpsdgs.fsf@yun.yagibdah.de> 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> <87vbvleiey.fsf@yun.yagibdah.de> <87vbvlbm8p.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1394511221 6040 80.91.229.3 (11 Mar 2014 04:13:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 11 Mar 2014 04:13:41 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 11 05:13:50 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 1WNE4b-0000tr-SZ for geh-help-gnu-emacs@m.gmane.org; Tue, 11 Mar 2014 05:13:50 +0100 Original-Received: from localhost ([::1]:52272 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNE4b-000149-EB for geh-help-gnu-emacs@m.gmane.org; Tue, 11 Mar 2014 00:13:49 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52178) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNE4K-00013K-LV for help-gnu-emacs@gnu.org; Tue, 11 Mar 2014 00:13:37 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNE4F-00014X-Ij for help-gnu-emacs@gnu.org; Tue, 11 Mar 2014 00:13:32 -0400 Original-Received: from client-194-42-186-216.muenet.net ([194.42.186.216]:52065 helo=yun.yagibdah.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNE4F-0000z7-5Q for help-gnu-emacs@gnu.org; Tue, 11 Mar 2014 00:13:27 -0400 Original-Received: from lee by yun.yagibdah.de with local (Exim 4.80.1) (envelope-from ) id 1WNE4C-0000oA-VM for help-gnu-emacs@gnu.org; Tue, 11 Mar 2014 05:13:24 +0100 In-Reply-To: <87vbvlbm8p.fsf@web.de> (Michael Heerdegen's message of "Tue, 11 Mar 2014 01:41:10 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) Mail-Followup-To: help-gnu-emacs@gnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 194.42.186.216 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:96421 Archived-At: Michael Heerdegen writes: > lee writes: > >> > Instead, use an auxiliary variable, e.g. >> >> Hm, I have experimented with variables and found that things don=C2=B4t = work >> when the variables are local. But then, I can=C2=B4t get add-advice to = work, >> not even with global variables. It=C2=B4s too complicated, and the >> documentation is not understandable at all. >> >> It also seems that it is not possible at all to make a mode on top of >> hi-lock-mode which does what I want. Variables have to be local per >> buffer, and with that, it=C2=B4s not possible to read patterns from diff= erent >> buffers because the variables change because buffers must be switched to >> read the patterns. > > Please don't get confused. Use global auxiliary variables for things > that reflect buffer independent states. But these things are not global. There is a combination of buffer A and buffer B, buffer C, ..., buffer N. The buffers B..N can all contain patterns that need to be read and used to for highlighting with buffer A. To keep it simple, I have assumed that there is only buffer A and buffer B. Support for multiple buffers might be added later. Now for buffer A, it must be remembered that its patters are in buffer B. That goes for multiple combinations of buffers A and B. When the variables used to remember this are global, there can only be a single combination of buffers. I have experimented with defvar-local: (defvar-local test nil) (defun foo (setq test (current-buffer)) (message "test-1 %s" (buffer-name test)) (find-file "some.file") (message "test-2 %s" (buffer-name test))) When you call foo, the value of test will change according to the current buffer. So you cannot use a local variable. Use a global one and it doesn=C2=B4t change, but then you don=C2=B4t know anymore from which= buffer patterns were used for which buffer when you do it with multiple files. > And you can always get the buffer local binding of a variable foo in > any buffer b with > > (with-current-buffer b foo) > > There is no limitation from the Emacs' side. But then I don=C2=B4t have the variables from the buffer I=C2=B4m doing thi= s from. > Mmh, ok, let's start from the beginning and simple. Let's add some > mechanism that allows to specify additional, directory local hi-lock > patterns. That should be simple as this: > > (defvar hi-lock-dir-patterns nil) ;; use as dir local variable >=20=20=20=20=20 > (advice-add > 'hi-lock-find-patterns :after > (lambda (&rest _) > (hi-lock-set-file-patterns > (append hi-lock-file-patterns > hi-lock-dir-patterns)))) > I can=C2=B4t get advice-add to work. The documentation doesn=C2=B4t explai= n it, and google doesn=C2=B4t find any examples. It either doesn=C2=B4t work, or= I=C2=B4m getting weird error messages that don=C2=B4t tell me anything. Code that works fine when used in a function suddenly gives an error message that says that setq is an invalid function. Remove the (setq something tosomething) and suddenly all the rest of the function is invalid for no reason. > The advice just merges in the additional patterns. Well, it can only be used :before or :after. Using it :before didn=C2=B4t work because I had to switch buffers in the prepended part, and apparently after that part was completed, the buffer was switched back. > Note that using directory local variables has the advantage that me > must not explicitly look up the patterns, because the directory local > variable is automatically bound when the file is opened. ? > This should be all to do as long as you don't want to automatically save > additional patterns That is one of the next steps. With add-advice, It=C2=B4s not working at a= ll atm. > - you have to add them yourself in the .dir-locals > file. > > If you want such automatic saving, we would just have to substitute the > dir local variable with a mechanism that loads and saves explicitly. Is > this what you want? I simply want to store the patterns in one (and at some time multiple) different buffers rather than in the buffer they are applied to. I want that to work transparently, for saving, loading and keeping the patterns up to date (i. e. when something that used to be highlighted is completely removed, the pattern for it needs to be removed, too), without duplicates. To assign a file A with patterns to another file B, I just want to put a line into file B so that the patterns in file A are used to highlight file B. File A can than be used with many files B. Once that works, the files A could have multiple lines in them to use patterns from multiple files B. Files B do not need to have patterns in themselves, though it would be nice if they could. Sigh ... Any idea why the following does not work: (defvar ext-hi-lock-original-buffer nil "Remember the buffer for which hi-lock highlighting-patterns that are being read from another buffer are to be applied to --- that is the original buffer.") (defvar ext-hi-lock-patterns-buffer nil) (defvar ext-hi-lock-disable-all nil) (defun my-test-unless () (interactive) (unless ext-hi-lock-disable-all (message "enabled"))) ;; ;; functions modifying functions in hi-lock.el ;; (defun ext-hi-lock-find-other-patterns () "See if there is a line in the current buffer specifying that hi-lock highlighting-patterns should be read from another file. When the line is found, visit the other file and read the patterns from its buffer, then apply them to the current buffer." (unless ext-hi-lock-disable-all ;; (when (not ext-hi-lock-original-buffer) (setq ext-hi-lock-original-buffer (current-buffer)) (save-excursion (save-restriction (widen) (goto-char (point-min)) (when (re-search-forward "^// ext-fontify-defs: " nil t) (let ((filename (thing-at-point 'filename))) (when filename (when (file-exists-p filename) (message "start: original-buffer: %s, current-b: %s" (buffer-name ext-hi-lock-original-buffer) (buffer-name (current-buffer))) (setq ext-hi-lock-patterns-buffer (find-file-noselect filename)) (message "found file: original-buffer: %s, current-b: %s" (buffer-name ext-hi-lock-original-buffer) (buffer-name (current-buffer))) (when ext-hi-lock-patterns-buffer (message "get patterns from %s" (buffer-name ext-hi-lock-patterns-buffer)) (set-buffer ext-hi-lock-patterns-buffer) (setq ext-hi-lock-disable-all t) (set-auto-mode-0 'hi-lock-mode t) (setq ext-hi-lock-disable-all nil) (message "buffer set: original-buffer: %s, current-b: %s" (buffer-name ext-hi-lock-original-buffer) (buffer-name (current-buffer))) ;; search the whole buffer for patterns (setq hi-lock-file-patterns-range (point-max))))))))))) (advice-add 'hi-lock-find-patterns :before 'ext-hi-lock-find-other-patterns) (advice-add 'hi-lock-set-file-patterns :before (lambda (patterns) (my-test-unless) (unless ext-hi-lock-disable-all (message "set on buffer: %s" (buffer-name (current-buffer))) (set-buffer ext-hi-lock-original-buffer) (message "set now buffer: %s --- should be buffer %s" (buffer-name (current-buffer)) (buffer-name ext-hi-lock-original-buffer)) (setq ext-hi-lock-disable-all t) (hi-lock-set-file-patterns patterns) (setq ext-hi-lock-disable-all nil) )) '((name . ext-hi-lock-switch-to-original-buffer))) (defun ext-hi-lock-message (unused) (message "patterns probably from %s for %s" ext-hi-lock-patterns-buffer ext-hi-lock-original-buffer)) (setq hi-lock-file-patterns-policy 'ext-hi-lock-message) It just doesn=C2=B4t apply the patterns. For unknown reasons, hi-lock-set-file-patterns is not called when the original buffer has a line in it to pull in patterns from another buffer. --=20 Knowledge is volatile and fluid. Software is power.