In my comment I suggested to use highlight-regexp
. But meanwhile I discovered that that method does not keep the highlighting correctly updated (it actually should).
So I wrote up a minor mode hlds-mode
that does the job.
I assume that with this minor mode the variable sentence-end-double-space
becomes obsolete.
You just only hook the minor mode into those major modes where you want the highlighting and you can even switch on or off the minor mode with M-x hlds-mode
if you decide late that you do like or not like the highlighting for that specific buffer.
(defface hlds
'((((class color) (background dark))
:background "grey" :foreground "darkgray")
(((class color) (background light))
:background "yellow" :foreground "lightgray")
(t :inverse-video t))
"Face used to mark missing sentence separating double spaces."
:group 'hlds)
(defvar hlds-re "\\.\\( \\)[^[:space:]]"
"Regular expression for marking missing double spaces as sentence separators.")
(defun hlds-clear (start end)
"Clear hlds fontification in region from START to END."
(with-silent-modifications
(cl-loop for int being the intervals property 'face from start to end
if (eq (get-text-property (car int) 'face) 'hlds)
do (remove-text-properties (car int) (cdr int) '(face hdls)))))
(defun hlds-jitlock-handler (start end)
"Mark double spaces in region from START to END."
(save-excursion
(with-silent-modifications
(hlds-clear start end)
(goto-char (max (- start 2) 1))
(when (< end (point-max))
(cl-incf end))
(while (re-search-forward hlds-re end t)
(put-text-property (match-beginning 1) (match-end 1) 'face 'hlds)))))
(defvar-local hlds-font-lock-keywords nil
"Additional `font-lock-keywors' entries for hdls-mode.")
(define-minor-mode hlds-mode
"Highlight missing double-spaces that should separate sentences."
nil
" ds"
nil
(if hlds-mode
(if font-lock-mode
(progn
(font-lock-add-keywords
nil
(setq hlds-font-lock-keywords `((,hlds-re 1 'hlds))))
(save-restriction
(widen)
(font-lock-flush)
(font-lock-ensure)))
(unless jit-lock-mode (jit-lock-mode t))
(jit-lock-register #'hlds-jitlock-handler)
(jit-lock-fontify-now (point-min) (point-max)))
(when font-lock-mode
(font-lock-remove-keywords nil hlds-font-lock-keywords)
(save-restriction
(widen)
(font-lock-flush (point-min) (point-max))))
(when jit-lock-mode
(save-restriction
(widen)
(hlds-clear (point-min) (point-max)))
(jit-lock-unregister #'hlds-jitlock-handler))))
For obtaining the effect that the file-local variable sentence-end-double-space
controls the activation of hlds-mode
you can define a corresponding globalized mode. The TURN-ON function hlds-turn-on
turns on hlds-mode
if the automagically selected major mode for the file buffer is registered in sentence-end-double-space
. If you annihilate the value of sentence-end-double-space
file-locally hlds-mode
is not switched on in the corresponding file buffer.
(defcustom hlds-major-mode-list '(text-mode fundamental-mode)
"Major modes with `hlds-mode' turned on by default."
:type '(repeat :tag "Major modes" symbol)
:group 'hlds)
(defun hlds-turn-on ()
"Turn on `hlds-mode' in the current buffer."
(when (and (apply #'derived-mode-p hlds-major-mode-list)
sentence-end-double-space)
(hlds-mode)))
(define-globalized-minor-mode global-hlds-mode hlds-mode hlds-turn-on)
(global-hlds-mode) ;; Just turn the global mode on.
Tested with emacs -Q
with code in *scratch*
buffer. One file with file-local setting sentence-end-double-space: nil
and one without.
Emacs-Version: GNU Emacs 26.1 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2018-05-29
flycheck-mode
does that in elisp doc-strings. Otherwise you can usehighlight-regexp
with regular expression\. [^[:space:]]
. You can even put(highlight-regexp "\\. [^[:space:]]")
into the hook of the major mode you use for your documents.whitespace-style
might be useful, but it would need to be limited to relevant modes, such astext-mode
. Maybe a separatewhitespace-mode
option would be appropriate, to allowwhitespace-style
to continue to specify more general behavior. If you want to request such an enhancement, useM-x report-emacs-bug
.