1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   |  
 
 (defun my-mode () "my-mode" (interactive)
   (kill-all-local-variables)
   (make-local-variable 'font-lock-defaults)
   (setq font-lock-defaults  '(my-mode-font-lock-keywords t))
   (make-local-variable 'font-lock-extend-region-functions)
   (add-hook 'font-lock-extend-region-functions
	     'my-font-lock-extend-region))
 
 
(defun my-font-lock-extend-region ()
   (save-excursion
     (goto-char font-lock-beg)
	 ;; search for beginning of a macro
     (let ((found-point (re-search-backward "^#begin" nil t)))
       (if found-point
		   (progn
			 (goto-char font-lock-end)
			 (if (re-search-forward "[^\\]$" nil t)
				 (progn
				   (end-of-line)				   
				   (setq font-lock-end (point))))
	    (setq font-lock-beg found-point))))))
 
 (defvar my-mode-font-lock-keywords nil
   "Keywords/Regexp for fontlocking of my-mode")
 
 (setq my-mode-font-lock-keywords
	  (list
       '(my-font-lock-matcher
		 ;; number_identifying_region type_of_enlighting
		 (0 'font-lock-type-face)
		 )))
 
 ;;  Original idea from font-latex-match-math-env command in font-latex.el.
 (defun my-font-lock-matcher (limit)
   "
#begin \
 macro continues \
 and again \
 the last line enlighted
 this one no
 "
  ;; search for the begin of the first region
  (when (re-search-forward "^#begin" limit t)
    (let ((beg (match-beginning 0)) end ; 1st Region
		  )
	  ;; search for end of region 
	  ;; return to the beginning of the first line of
	  ;; the macro in order to check if it's a one line macro
	  (beginning-of-line)
          ;; end of region is defined by the first line not ended by a backslash
	  (if (re-search-forward "[^\\]$" limit t)
		  (progn
			(setq end (match-end 0 ))
			))
	  (end-of-line)
	  (setq end (point))
	  ;; save the regions
	  (store-match-data (list beg end))
      t))) | 
Partager