hilit19 for Python; can handle unbalanced quotes in comments!

Michael Scharf (scharf@EMBL-Heidelberg.DE)
08 Jun 1994 18:46:54 GMT

This is a hilit for Python. I tested it for emacs-19.24. The function
hilit-python-string-find can handle unbalanced quotes and double quotes in
comments. as in:

# - It's ....

One could easyly adopt hilit-python-string-find for Perl and C++ // comments as
well. I tested it for emacs-19.24.

The function hilit-python-string-find might not be elegant at all - but it
seems to work. I program emacs-lisp by analogy (I don't really understand lisp
yet ;-)

Michael

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; if you don't know how to set the colors for hilit19 (as I did'nt :-)
;; put this in your .emacs file and modify it as you like

;; (cond (window-system
;; (setq hilit-mode-enable-list '(not text-mode)
;; hilit-background-mode 'light
;; hilit-inhibit-hooks nil
;; hilit-inhibit-rebinding nil)
;;
;; (require 'hilit19)
;; (setq hilit-face-translation-table
;; (append '(
;; (string . chocolate3)
;; (comment . blue-italic)
;; (keyword . purple)
;; (include . ForestGreen-bold)
;; (funccall . gold4)
;; (backtick . red3)
;; (assignment . red2)
;; (defun . bold)
;; (char . bold)
;; )
;; hilit-face-translation-table
;; )
;; )
;;
;; ))

(defun hilit-python-string-find (qchar)
"looks for a string and returns (start . end) or NIL. Finds strings
delimited by QCHAR. It handles the case correct when a unmatched QCHAR appears
in a comment. The first QCHAR may not be preceded by a backslash and the
closing QCHAR may not be preceded by an odd number of backslashes."
(let (st en pp)
(while (and (search-forward qchar nil t)
(or (save-excursion
;; look for the line of the prevoius comment
(setq st (match-beginning 0))
(goto-char (point))
(save-excursion
(skip-chars-backward "^#")
(skip-chars-backward "^\n")
(setq pp (point)))
;; in which line are we?
(skip-chars-backward "^\n")
;; is the prevoius comment in the same line as we are?
(eq pp (point)))
(eq ?\\ (char-after (1- st))))
))
(while (and (search-forward qchar nil t)
(save-excursion
(setq en (point))
(forward-char -1)
(skip-chars-backward "\\\\")
(forward-char 1)
(not (zerop (% (- en (point)) 2))))))
(and en (cons st en))))

;; here are the patterns...

(hilit-set-mode-patterns
'python-mode
'(
(hilit-python-string-find "\"" string)
(hilit-python-string-find "\'" string)
(hilit-python-string-find "\`" backtick)
("\\s #.*$" nil comment)
("^#.*$" nil comment)
("\\<import\\>[A-Za-z_*, \t]+" nil include)
("\\<from[ \t]+\\w+" nil include)
("^[ \t]*\\(def\\|class\\)\\>[^:]*" nil defun)
("\\<\\(and\\|del\\|for\\|in\\|print\\|break\\|elif\\|is\\|raise\\|else\\|global\\|not\\|return\\|continue\\|except\\|if\\|or\\|try\\|finally\\|pass\\|while\\)\\>" nil keyword)
;; ("[A-Za-z_]+[ \t]*(" ")" funccall)
;; ("^.*=" nil assignment)
;; ("\\>[ \t]*(" ")" funccall)
;; ("[()]" nil funccall)
;; ("\\([-+{}%/\\.[()=<>]\\|\\]\\)" nil char)
;; ("=" nil assignment)
)
nil 'case-insensitive)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

--
         __________________________________________________________
   ****    ___   _  _   ___   _    
  ******  | __) | \/ | |   ) | |   Michael Scharf
 ******** | _)  |    | | -<  | |_  Tel: +49 6221 387 305 Fax: 517
  * ****  |___) |_||_| |___) |___) EMail: scharf@EMBL-Heidelberg.de
   ****   __________________________________________________________