chiark / gitweb /
el/dot-emacs.el: Hack quotes in configuration files.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 6 Jan 2012 10:54:53 +0000 (10:54 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 6 Jan 2012 10:54:53 +0000 (10:54 +0000)
`conf-mode' has a convenient tweak which makes quote characters not be
special, which you invoke using C-c C-q (M-x conf-quote-normal).
Unfortunately, there's no way to force this setting for a particular
file, which means that the display is wrong until you clobber it
manually.  Worse, the setting applies equally to both quote characters.

Institute a new variable for controlling whether quotes should be given
special syntax, and honour it in a hook function.  This turns out to be
fiddly: read the docstrings for details.

el/dot-emacs.el

index 0990d670b45d6236de7c31f46d37266295bf3846..c972adca0b1a82d16b18a2ea84fbff04a67af92b 100644 (file)
@@ -2179,6 +2179,48 @@ (defun mdw-sgml-mode ()
   (setq mode-name "[mdw] SGML")
   (run-hooks 'mdw-sgml-mode-hook))
 
+;;;--------------------------------------------------------------------------
+;;; Configuration files.
+
+(defvar mdw-conf-quote-normal nil
+  "*Control syntax category of quote characters `\"' and `''.
+If this is `t', consider quote characters to be normal
+punctuation, as for `conf-quote-normal'.  If this is `nil' then
+leave quote characters as quotes.  If this is a list, then
+consider the quote characters in the list to be normal
+punctuation.  If this is a single quote character, then consider
+that character only to be normal punctuation.")
+(defun mdw-conf-quote-normal-acceptable-value-p (value)
+  "Is the VALUE is an acceptable value for `mdw-conf-quote-normal'?"
+  (or (booleanp value)
+      (every (lambda (v) (memq v '(?\" ?')))
+            (if (listp value) value (list value)))))
+(put 'mdw-conf-quote-normal 'safe-local-variable '
+     mdw-conf-quote-normal-acceptable-value-p)
+
+(defun mdw-fix-up-quote ()
+  "Apply the setting of `mdw-conf-quote-normal'."
+  (let ((flag mdw-conf-quote-normal))
+    (cond ((eq flag t)
+          (conf-quote-normal t))
+         ((not flag)
+          nil)
+         (t
+          (let ((table (copy-syntax-table (syntax-table))))
+            (mapc (lambda (ch) (modify-syntax-entry ch "." table))
+                  (if (listp flag) flag (list flag)))
+            (set-syntax-table table)
+            (and font-lock-mode (font-lock-fontify-buffer)))))))
+(defun mdw-fix-up-quote-hack ()
+  "Unpleasant hack to call `mdw-fix-up-quote' at the right time.
+Annoyingly, `hack-local-variables' is done after `set-auto-mode'
+so we wouldn't see a local-variable setting of
+`mdw-conf-quote-normal' in `conf-mode-hook'.  Instead, wire
+ourselves onto `hack-local-variables-hook' here, and check the
+setting once it's actually been made."
+  (add-hook 'hack-local-variables-hook 'mdw-fix-up-quote t t))
+(add-hook 'conf-mode-hook 'mdw-fix-up-quote-hack t)
+
 ;;;--------------------------------------------------------------------------
 ;;; Shell scripts.