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: Jambunathan K
Subject: Re: replacing a function with another one
Date: Wed, 12 Mar 2014 18:46:26 +0530
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Hello OP,

It seems you don't mind digging in to elisp code.

1. Put the following snippet (see below) in to your .emacs.
2. Restart emacs
3. C-x C-f somefile.txt
4. M-x add-file-local-variable RET hi-lock-patterns-file RET "somefile.patterns"
5. C-x C-v
6. Highlight some strings here and there
7. Make some modifications to the file
8. C-x C-s

Now,

1. C-x C-f somefile.txt.
2. You should see the highlights.

When you highlight some text, the buffer is considered as *not*
modified.  So after-save-hook will not be called if there are no actual
modifications in the current session. In that case, you can force a
write of the patterns file with

   M-x hi-lock-file-save-patterns

See the attached sample files for what I mean.

----------------------------------------------------------------

(add-hook 'find-file-hook 'hi-lock-find-file-hook)
(add-hook 'after-save-hook 'hi-lock-file-save-patterns)

(defun hi-lock-find-file-hook ()
  (when (and (boundp 'hi-lock-patterns-file)
             (file-readable-p hi-lock-patterns-file))
    (let ((patterns (with-current-buffer (find-file-noselect 
hi-lock-patterns-file)
                      (goto-char (point-min))
                      (prog1 (ignore-errors (read (current-buffer)))
                        (kill-buffer)))))
      (setq hi-lock-interactive-patterns patterns)
      (font-lock-add-keywords nil hi-lock-interactive-patterns t))))

(defun hi-lock-file-save-patterns ()
  (interactive)
  (when (boundp 'hi-lock-patterns-file)
    (let ((patterns (append hi-lock-file-patterns 
hi-lock-interactive-patterns)))
      (with-current-buffer (find-file-noselect hi-lock-patterns-file)
        (erase-buffer)
        (insert (pp-to-string patterns))
        (save-buffer 0)))))

----------------------------------------------------------------

Attachment: somefile.patterns
Description: Binary data

Attachment: somefile.txt
Description: Text document


reply via email to

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