Skip to content

Implement font-lock for quasi quoted XML, HTML and JavaScript #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions haskell-compat.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ A process is considered alive if its status is `run', `open',
xref-prompt-for-identifier)))
(find-tag ident next-p))))

(unless (fboundp 'font-lock-ensure)
(defalias 'font-lock-ensure 'font-lock-fontify-buffer))

(provide 'haskell-compat)

;;; haskell-compat.el ends here
67 changes: 66 additions & 1 deletion haskell-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ This is the case if the \".\" is part of a \"forall <tvar> . <type>\"."
(string= " " (string (char-after start)))
(string= " " (string (char-before start))))))))

;;;###autoload
(defcustom haskell-font-lock-quasi-quote-modes
`(("hsx" . xml-mode)
("hamlet" . xml-mode)
("shamlet" . xml-mode)
("xmlQQ" . xml-mode)
("xml" . xml-mode)
("cmd" . shell-mode)
("sh_" . shell-mode)
("jmacro" . javascript-mode)
("jmacroE" . javascript-mode)
("r" . ess-mode)
("rChan" . ess-mode)
("sql" . sql-mode))
"Mapping from quasi quoter token to fontification mode.

If a quasi quote is seen in Haskell code its contents will have
font faces assigned as if respective mode was enabled."
:group 'haskell
:type '(repeat (cons string symbol)))

;;;###autoload
(defface haskell-keyword-face
'((t :inherit font-lock-keyword-face))
Expand Down Expand Up @@ -420,10 +441,54 @@ that should be commented under LaTeX-style literate scripts."
("^\\(\\\\\\)end{code}$" 1 "!"))
haskell-basic-syntactic-keywords))

(defun haskell-font-lock-fontify-block (lang-mode start end)
"Fontify a block as LANG-MODE."
(let ((string (buffer-substring-no-properties start end))
(modified (buffer-modified-p))
(org-buffer (current-buffer)) pos next)
(remove-text-properties start end '(face nil))
(with-current-buffer
(get-buffer-create
(concat " haskell-font-lock-fontify-block:" (symbol-name lang-mode)))
(delete-region (point-min) (point-max))
(insert string " ") ;; so there's a final property change
(unless (eq major-mode lang-mode) (funcall lang-mode))
(font-lock-ensure)
(setq pos (point-min))
(while (setq next (next-single-property-change pos 'face))
(put-text-property
(+ start (1- pos)) (1- (+ start next)) 'face
(get-text-property pos 'face) org-buffer)
(setq pos next)))
(add-text-properties
start end
'(font-lock-fontified t fontified t font-lock-multiline t))
(set-buffer-modified-p modified)))

(defun haskell-syntactic-face-function (state)
"`font-lock-syntactic-face-function' for Haskell."
(cond
((nth 3 state) 'font-lock-string-face) ; as normal
((nth 3 state)
(if (equal ?| (nth 3 state))
;; find out what kind of QuasiQuote is this
(let* ((qqname (save-excursion
(goto-char (nth 8 state))
(skip-syntax-backward "w._")
(buffer-substring-no-properties (point) (nth 8 state))))
(lang-mode (cdr (assoc qqname haskell-font-lock-quasi-quote-modes))))

(if (and lang-mode
(fboundp lang-mode))
(save-excursion
;; find the end of the QuasiQuote
(parse-partial-sexp (point) (point-max) nil nil state
'syntax-table)
(haskell-font-lock-fontify-block lang-mode (nth 8 state) (point))
;; must return nil here so that it is not fontified again as string
nil)
;; fontify normally as string because lang-mode is not present
'font-lock-string-face))
'font-lock-string-face))
;; Else comment. If it's from syntax table, use default face.
((or (eq 'syntax-table (nth 7 state))
(and (eq haskell-literate 'bird)
Expand Down