From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Reza Nikoopour Newsgroups: gmane.emacs.devel Subject: Help Understanding syntax-propertize-function Date: Mon, 15 Mar 2021 15:48:13 -0700 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="00000000000017488d05bd9b0c74" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30928"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Mar 15 23:49:07 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lLw1K-0007w2-6S for ged-emacs-devel@m.gmane-mx.org; Mon, 15 Mar 2021 23:49:06 +0100 Original-Received: from localhost ([::1]:34204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lLw1J-00059T-8j for ged-emacs-devel@m.gmane-mx.org; Mon, 15 Mar 2021 18:49:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53978) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lLw0j-0004fs-9P for emacs-devel@gnu.org; Mon, 15 Mar 2021 18:48:29 -0400 Original-Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]:33063) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lLw0g-00017F-Cr for emacs-devel@gnu.org; Mon, 15 Mar 2021 18:48:29 -0400 Original-Received: by mail-il1-x133.google.com with SMTP id r7so10976606ilb.0 for ; Mon, 15 Mar 2021 15:48:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=c17haMlxGR6n3yavWbgQgwET1Bjyui9KWrgXZFy/nJI=; b=fNljxXHBxlWkrAxTVKWwkIwB1Jw7trvbFr6j87hJuaZno89xzxGxfeJMBxWhAQwQm9 cD//mqL+bKK00BbnddOhFrbdQ753y4IbZKEk/FZkIs49eqyBQcxa7PQKL8brAz99YfIZ UagoDyS21XseCrW+yIN84gs5DgJ7FojUixqiq3SXxoTtRT1SYC/A5dD9sxOWMClnO0G7 hulT1PbZju8cvla4atq3SXKMlySyYbaoDsY/aDr1VzMhyu5iQztH+pkCJG0P4w+iW+8z mwe0GxVXAtf8K+zAHlp40tFzSAPi8xFBOgUZLkCXi40Jf4uhsjLuHNReqs3f6tNORbuJ Ylwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=c17haMlxGR6n3yavWbgQgwET1Bjyui9KWrgXZFy/nJI=; b=HYSkT4L7QSlyovL/e4HfAw/0hOL8l6L7NRp/68A3em/xe8tFxTqunERNqq9/2kg4zP bWKZyKyjrEh9+0omqrbnqRA1ExnkpOOtZ+w/Ezj6xcFzyBiNidjKGAw3oG7HO2PmjymV 0Rmr3wYerDpOKCsNjVJ32MRDsgxTgZgZlNJqOFJvevmd5FVs9J6nPoKJEpMkGeukYLpA 5PXAlj0zc2vYsZkP2pGDlYLn01JAp3q/Xa04LfQdoJmfQyGVb7ikI+vsi704Zk0Jl4Oq o4v9743VYulbAHj9Oq9uo44ZJQnMnQPj/cXPlNHI5HLNSibzvAmWG5Q6OSC5zUttJX03 Rdew== X-Gm-Message-State: AOAM532+eLFKE4QSUu1+L5KLCTp0yrFlPtnnBjr6MuIOoqctSmbpGQpV 6CzMqEAvz7rlfs1AL76y1UmbixLfqv8Q9Xfb/utQqKJasrI= X-Google-Smtp-Source: ABdhPJw6YaHTzS9y4UsNrYncrqCmCjCzbjm5EA+IdOwIrB+8DdyHHrOC5mxdN73YeLS1spCdtO6wJ86jUYkPcy85pHU= X-Received: by 2002:a92:d80f:: with SMTP id y15mr1415498ilm.96.1615848503984; Mon, 15 Mar 2021 15:48:23 -0700 (PDT) Received-SPF: pass client-ip=2607:f8b0:4864:20::133; envelope-from=rnikoopour@gmail.com; helo=mail-il1-x133.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:266487 Archived-At: --00000000000017488d05bd9b0c74 Content-Type: text/plain; charset="UTF-8" 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 --00000000000017488d05bd9b0c74 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello All,

I'm trying to implement here doc syn= tax highlighting.=C2=A0 I've reviewed the implementations in shell-scri= pt-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-Prop= erties.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)
=C2=A0 (let ((ppss (s= yntax-ppss)))
=C2=A0= =C2=A0 (when (eq t (nth 3 ppss))
=C2=A0 =C2=A0 =C2=A0 (let ((key (get-text-property (nth 8 pps= s) 'hcl-here-doc-marker))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (case-fold-search nil))=
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 (when (re-search-forward
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(con= cat "^\\(?:[ \t]*\\)" (regexp-quote key) "\\(\n\\)")
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0end 'move)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let ((eol = (match-beginning 1)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (put-text-property eol (1+ eol)=
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0'syntax-table (string-to-syntax "|"))))))= ))

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

(defun hc= l--syntax-propertize-function (start end)
=C2=A0 (goto-char start)
=C2=A0 (hcl--syntax-propertize-heredoc end)
=C2=A0 (funcall
=C2=A0 =C2=A0(syntax-propert= ize-rules
=C2=A0 =C2= =A0 (hcl--here-doc-beg-re
=C2=A0 =C2=A0 =C2=A0(2 (hcl--font-lock-open-heredoc
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(ma= tch-beginning 0) (match-string 1) (match-beginning 2))))
<= span style=3D"font-family:monospace">=C2=A0 =C2=A0 ("\\s|" (0 (pr= og1 nil (hcl--syntax-propertize-heredoc end)))))
=C2=A0 =C2=A0(point) end))

Cheers,
Reza
--00000000000017488d05bd9b0c74--