From 8d56586d0a2ec4af423fe6fb0b2d344a04371ff8 Mon Sep 17 00:00:00 2001
From: Stefan Monnier <monnier@iro.umontreal.ca>
Date: Wed, 15 Oct 2014 11:28:35 -0400
Subject: [PATCH] * lisp/progmodes/m4-mode.el (m4-syntax-propertize): New var.
 (m4-mode): Use it. (m4--quoted-p): New function. (m4-font-lock-keywords):
 Don't handle #..\n comments any more. (m4-mode-syntax-table): Use punctuation
 syntax (according to m4 manual) for most special characters.

---
 lisp/ChangeLog            |  7 +++++++
 lisp/progmodes/m4-mode.el | 33 ++++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 9b6d532decb..610cf6f8670 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,12 @@
 2014-10-15  Stefan Monnier  <monnier@iro.umontreal.ca>
 
+	* progmodes/m4-mode.el (m4-syntax-propertize): New var.
+	(m4-mode): Use it.
+	(m4--quoted-p): New function.
+	(m4-font-lock-keywords): Don't handle #..\n comments any more.
+	(m4-mode-syntax-table): Use punctuation syntax (according to m4 manual)
+	for most special characters.
+
 	* progmodes/compile.el (compilation--previous-directory): Simplify.
 	(compilation-next-error): Ensure the parse before we look at
 	compilation-message property.
diff --git a/lisp/progmodes/m4-mode.el b/lisp/progmodes/m4-mode.el
index b795b35a8ea..573acf4445d 100644
--- a/lisp/progmodes/m4-mode.el
+++ b/lisp/progmodes/m4-mode.el
@@ -65,14 +65,13 @@ If m4 is not in your PATH, set this to an absolute file name."
 
 (defvar m4-font-lock-keywords
   `(
-    ("\\(\\b\\(m4_\\)?dnl\\b\\|^\\#\\).*$" . font-lock-comment-face)
-;    ("\\(\\bdnl\\b\\|\\bm4_dnl\\b\\|^\\#\\).*$" . font-lock-comment-face)
+    ("\\(\\_<\\(m4_\\)?dnl\\_>\\).*$" . font-lock-comment-face)
     ("\\$[*#@0-9]" . font-lock-variable-name-face)
     ("\\\$\\\@" . font-lock-variable-name-face)
     ("\\\$\\\*" . font-lock-variable-name-face)
     ("\\b\\(builtin\\|change\\(com\\|quote\\|word\\)\\|d\\(e\\(bug\\(file\\|mode\\)\\|cr\\|f\\(ine\\|n\\)\\)\\|iv\\(ert\\|num\\)\\|nl\\|umpdef\\)\\|e\\(rrprint\\|syscmd\\|val\\)\\|f\\(ile\\|ormat\\)\\|gnu\\|i\\(f\\(def\\|else\\)\\|n\\(c\\(lude\\|r\\)\\|d\\(ex\\|ir\\)\\)\\)\\|l\\(en\\|ine\\)\\|m\\(4\\(exit\\|wrap\\)\\|aketemp\\)\\|p\\(atsubst\\|opdef\\|ushdef\\)\\|regexp\\|s\\(hift\\|include\\|ubstr\\|ys\\(cmd\\|val\\)\\)\\|tra\\(ceo\\(ff\\|n\\)\\|nslit\\)\\|un\\(d\\(efine\\|ivert\\)\\|ix\\)\\)\\b" . font-lock-keyword-face)
     ("\\b\\(m4_\\(builtin\\|change\\(com\\|quote\\|word\\)\\|d\\(e\\(bug\\(file\\|mode\\)\\|cr\\|f\\(ine\\|n\\)\\)\\|iv\\(ert\\|num\\)\\|nl\\|umpdef\\)\\|e\\(rrprint\\|syscmd\\|val\\)\\|f\\(ile\\|ormat\\)\\|i\\(f\\(def\\|else\\)\\|n\\(c\\(lude\\|r\\)\\|d\\(ex\\|ir\\)\\)\\)\\|l\\(en\\|ine\\)\\|m\\(4\\(_undefine\\|exit\\|wrap\\)\\|aketemp\\)\\|p\\(atsubst\\|opdef\\|ushdef\\)\\|regexp\\|s\\(hift\\|include\\|ubstr\\|ys\\(cmd\\|val\\)\\)\\|tra\\(ceo\\(ff\\|n\\)\\|nslit\\)\\|undivert\\)\\)\\b" . font-lock-keyword-face))
-  "Default font-lock-keywords for `m4 mode'.")
+  "Default `font-lock-keywords' for M4 mode.")
 
 (defcustom m4-mode-hook nil
   "Hook called by `m4-mode'."
@@ -86,19 +85,26 @@ If m4 is not in your PATH, set this to an absolute file name."
     (modify-syntax-entry ?' ")`" table)
     (modify-syntax-entry ?# "<\n" table)
     (modify-syntax-entry ?\n ">#" table)
-    (modify-syntax-entry ?{  "_" table)
-    (modify-syntax-entry ?}  "_" table)
-    ;; FIXME: This symbol syntax for underscore looks OK on its own, but it's
-    ;; odd that it should have the same syntax as { and } are these really
-    ;; valid in m4 symbols?
+    (modify-syntax-entry ?{  "." table)
+    (modify-syntax-entry ?}  "." table)
     (modify-syntax-entry ?_  "_" table)
-    ;; FIXME: These three chars with word syntax look wrong.
-    (modify-syntax-entry ?*  "w" table)
-    (modify-syntax-entry ?\"  "w" table)
-    (modify-syntax-entry ?\"  "w" table)
+    (modify-syntax-entry ?*  "." table)
+    (modify-syntax-entry ?\"  "." table)
     table)
   "Syntax table used while in `m4-mode'.")
 
+(defun m4--quoted-p (pos)
+  "Return non-nil if POS is inside a quoted string."
+  (let ((quoted nil))
+    (dolist (o (nth 9 (save-excursion (syntax-ppss pos))))
+      (if (eq (char-after o) ?\`) (setq quoted t)))
+    quoted))
+
+(defconst m4-syntax-propertize
+  (syntax-propertize-rules
+   ("#" (0 (when (m4--quoted-p (match-beginning 0))
+             (string-to-syntax "."))))))
+
 (defvar m4-mode-map
   (let ((map (make-sparse-keymap))
 	(menu-map (make-sparse-keymap)))
@@ -148,7 +154,8 @@ If m4 is not in your PATH, set this to an absolute file name."
   (setq-local comment-start "#")
   (setq-local parse-sexp-ignore-comments t)
   (setq-local add-log-current-defun-function #'m4-current-defun-name)
-  (setq font-lock-defaults '(m4-font-lock-keywords nil)))
+  (setq-local syntax-propertize-function m4-syntax-propertize)
+  (setq-local font-lock-defaults '(m4-font-lock-keywords nil)))
 
 (provide 'm4-mode)
 ;;stuff to play with for debugging
-- 
2.39.5