unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Help Understanding syntax-propertize-function
@ 2021-03-15 22:48 Reza Nikoopour
  2021-03-15 23:30 ` Stefan Monnier
  0 siblings, 1 reply; 10+ messages in thread
From: Reza Nikoopour @ 2021-03-15 22:48 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1720 bytes --]

Hello All,

I'm trying to implement here doc syntax highlighting.  I've reviewed the
implementations in shell-script-mode and hcl-mode but I don't really
understand what is happening. I've read the documentation for
syntax-propertize-function (
https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Properties.html)
but that didn't help me understand what's going on.

Could someone help explain the following code:

(defun hcl--syntax-propertize-heredoc (end)
  (let ((ppss (syntax-ppss)))
    (when (eq t (nth 3 ppss))
      (let ((key (get-text-property (nth 8 ppss) 'hcl-here-doc-marker))
            (case-fold-search nil))
        (when (re-search-forward
               (concat "^\\(?:[ \t]*\\)" (regexp-quote key) "\\(\n\\)")
               end 'move)
          (let ((eol (match-beginning 1)))
            (put-text-property eol (1+ eol)
                               'syntax-table (string-to-syntax "|"))))))))

(defun hcl--font-lock-open-heredoc (start string eol)
  (unless (or (memq (char-before start) '(?< ?>))
     (save-excursion
                (goto-char start)
                (hcl--in-string-or-comment-p)))
    (let ((str (replace-regexp-in-string "['\"]" "" string)))
      (put-text-property eol (1+ eol) 'hcl-here-doc-marker str)
      (prog1 (string-to-syntax "|")
        (goto-char (+ 2 start))))))

(defun hcl--syntax-propertize-function (start end)
  (goto-char start)
  (hcl--syntax-propertize-heredoc end)
  (funcall
   (syntax-propertize-rules
    (hcl--here-doc-beg-re
     (2 (hcl--font-lock-open-heredoc
         (match-beginning 0) (match-string 1) (match-beginning 2))))
    ("\\s|" (0 (prog1 nil (hcl--syntax-propertize-heredoc end)))))
   (point) end))


Cheers,
Reza

[-- Attachment #2: Type: text/html, Size: 3864 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-03-24 22:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-15 22:48 Help Understanding syntax-propertize-function Reza Nikoopour
2021-03-15 23:30 ` Stefan Monnier
2021-03-15 23:58   ` Reza Nikoopour
2021-03-16  1:42     ` Stefan Monnier
2021-03-16 15:37       ` Reza Nikoopour
2021-03-16 17:29         ` Stefan Monnier
2021-03-16 17:50           ` Reza Nikoopour
2021-03-24 20:29   ` Filipp Gunbin
2021-03-24 22:10     ` Stefan Monnier
2021-03-24 22:54       ` Filipp Gunbin

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).