Search code examples
emacsmultilinefont-lockfont-lock-mode

Emacs: font-lock-fontify-region and multiline


I am writing a minor mode for php/html files. I use a function (cf. font-lock-keywords) to fontify <?php ?> blocs.

In order to fontify multilined blocs, I need to set font-lock-multiline to t.

Everything is running quite nicely. Their is just a problem in this case : When I have a multiline bloc and a delete the closing tag (?>) the bloc is unfontified. When I put the tag back, the block is not fontified again.

I have three questions :

1/ is there a simple solution to this problem

if not 2/ is there any way to trigger font-lock-fontify-buffer each time I type those two chars : '?''>'

3/ better, is there a way to trigger this kind a fonction : when I type ?> I find the opening tag <?php and force a font-lock-fontify-region on this bloc.


Solution

  • This is a basic approach, and the logic is insufficient, but it demonstrates one option:

    (defvar foo-minor-mode-map (make-keymap) "foo-minor-mode keymap.")
    (define-key foo-minor-mode-map (kbd ">") 'foo-electric-gt)
    
    (defun foo-electric-gt (&optional arg)
      (interactive "*p")
      (when (looking-back "\\?$")
        (save-excursion
          (let ((end (- (point) 1))
                (beg (+ (search-backward "<?php") 5)))
            (font-lock-fontify-region beg end))))
      (insert-char ?> arg))
    
    (define-minor-mode foo-minor-mode
      "foo mode.
    
    \\{foo-minor-mode-map}"
      :keymap 'foo-minor-mode-map)