Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
;; A minor mode inspired by Gary Provost's notes on making sentence length music
;; via
;; The sentence at point gains its colour from how many words it has:
;; yellow - one or two words
;; pink - three or four words
;; red - five words
;; green - medium sentences
;; blue - long sentences
;; An overview mode colorizing the whole buffer by sentence length would be nice
;; too, but I will leave that as an exercise for people with a less rusty elisp.
;; And it'd probably be nicer if empty lines didn't colorize the sentence on the
;; most recent previous line.
;; (And I bet I am not doing ideal cleanup, or the most idiomatic code, either.)
(defun sentence-highlight-minor-mode ()
(make-local-variable 'sentence-highlight-mode)
(if sentence-highlight-mode
(setq sentence-highlight-mode nil)
(remove-hook 'post-command-hook 'sentence-highlight-current)
(move-overlay sentence-extent (point) (point) (current-buffer)))
(setq sentence-highlight-mode t)
(add-hook 'post-command-hook 'sentence-highlight-current)))
(setq sentence-end "[^.].[.?!]+\\([]\"')}]*\\|<[^>]+>\\)\\($\\| $\\|\t\\| \\)[ \t\n]*")
(setq sentence-color-lt3 "#f7f5ca") ; yellow - staccato sentences
(setq sentence-color-lt5 "#f5dcf6") ; pink - short sentences
(setq sentence-color-lt6 "#efbbb7") ; red - five-word sentences
(setq sentence-color-lt12 "#d3f3d0") ; green - medium sentences
(setq sentence-color-long "#b2f4f5") ; blue - long sentences
;; These probably ought to be done with defface and/or macros instead
(setq sentence-face-bg-lt3 (make-face 'sentence-face-bg-lt3))
(set-face-background sentence-face-bg-lt3 sentence-color-lt3)
(setq sentence-face-bg-lt5 (make-face 'sentence-face-bg-lt5))
(set-face-background sentence-face-bg-lt5 sentence-color-lt5)
(setq sentence-face-bg-lt6 (make-face 'sentence-face-bg-lt6))
(set-face-background sentence-face-bg-lt6 sentence-color-lt6)
(setq sentence-face-bg-lt12 (make-face 'sentence-face-bg-lt12))
(set-face-background sentence-face-bg-lt12 sentence-color-lt12)
(setq sentence-face-bg-long (make-face 'sentence-face-bg-long))
(set-face-background sentence-face-bg-long sentence-color-long)
(defun sentence-begin-pos () (save-excursion (unless (= (point) (point-max)) (forward-char)) (backward-sentence) (point)))
(defun sentence-end-pos () (save-excursion (unless (= (point) (point-max)) (forward-char)) (backward-sentence) (forward-sentence) (point)))
(setq sentence-highlight-mode nil)
(setq sentence-extent (make-overlay 0 0))
(defun sentence-highlight-current (&rest ignore)
"Highlight current sentence."
(and sentence-highlight-mode (> (buffer-size) 0)
(and (boundp 'sentence-extent)
((head (sentence-begin-pos))
(tail (sentence-end-pos))
(words (count-words head tail))
((< words 3) sentence-face-bg-lt3)
((< words 5) sentence-face-bg-lt5)
((< words 6) sentence-face-bg-lt6)
((< words 12) sentence-face-bg-lt12)
(t sentence-face-bg-long))))
(move-overlay sentence-extent head tail (current-buffer))
(overlay-put sentence-extent 'face face)))
;;;;; Now, enable this for Outline mode only:
;;; (add-hook 'outline-mode-hook (function (lambda ()
;;; ; .... any other outline mode tweaks or key definitions you may need
;;; (make-local-variable 'sentence-highlight-mode)
;;; (setq sentence-highlight-mode t)
;;; (add-hook 'post-command-hook 'sentence-highlight-current)
;;; )))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment