Update.
[elisp.git] / text-counters.el
1 ;; -*- mode: Emacs-Lisp; mode: rainbow; -*-
2
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; This program is free software; you can redistribute it and/or         ;;
5 ;; modify it under the terms of the GNU General Public License as        ;;
6 ;; published by the Free Software Foundation; either version 3, or (at   ;;
7 ;; your option) any later version.                                       ;;
8 ;;                                                                       ;;
9 ;; This program is distributed in the hope that it will be useful, but   ;;
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of            ;;
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      ;;
12 ;; General Public License for more details.                              ;;
13 ;;                                                                       ;;
14 ;; You should have received a copy of the GNU General Public License     ;;
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.  ;;
16 ;;                                                                       ;;
17 ;; Written by and Copyright (C) Francois Fleuret                         ;;
18 ;; Contact <francois@fleuret.org> for comments & bug reports             ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
21 ;; This script automatigally count the number of characters and words
22 ;; between "----" markers. To activate it automatically when switching
23 ;; to text-mode, just add to your emacs.el
24 ;;
25 ;; (add-hook 'text-mode-hook 'tc/add-text-counters-in-modeline)
26
27 (defface tc/modeline-face
28   '((((background light)) (:foreground "blue4"))
29     (((background dark)) (:foreground "cyan")))
30   "The face for the alarm-vc modeline message.")
31
32 ;; Counts the number of words and characters between the previous and
33 ;; the next line of '-' (with at least four '-' in each line)
34
35 (defun tc/text-counters-string () (interactive)
36   (let ((a (save-excursion
37              (and (re-search-backward "^--.*--*" nil t)
38                   (match-end 0))))
39         (b (save-excursion
40              (and (re-search-forward "^--.*--*" nil t)
41                   (match-beginning 0)))))
42
43     (when (and a b)
44       (propertize (format "%dw %dc " (count-words a b) (- b a))
45                   'face 'tc/modeline-face)
46       )))
47
48 ;; Add the said counters into the modeline
49
50 (defun tc/add-text-counters-in-modeline () (interactive)
51   (add-to-list 'mode-line-buffer-identification '(:eval (tc/text-counters-string)))
52   )