From 6cb52f8b2fc7fd42bce655735fd7723fe1d66842 Mon Sep 17 00:00:00 2001 Message-Id: <6cb52f8b2fc7fd42bce655735fd7723fe1d66842.1717748560.git.mdw@distorted.org.uk> From: Mark Wooding Date: Fri, 6 Jan 2012 10:54:53 +0000 Subject: [PATCH] el/dot-emacs.el: Hack quotes in configuration files. Organization: Straylight/Edgeware From: Mark Wooding `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 | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/el/dot-emacs.el b/el/dot-emacs.el index 0990d67..c972adc 100644 --- a/el/dot-emacs.el +++ b/el/dot-emacs.el @@ -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. -- [mdw]