summaryrefslogtreecommitdiff
path: root/.emacs.d/site-lisp/highlight-80+.el
blob: f80e9288c50118d0da264b1ce4c6de5aa7a51993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;;; highlight-80+.el --- highlight characters beyond column 80
;;
;; Copyright (C) 2008 Nikolaj Schumacher
;;
;; Author: Nikolaj Schumacher <bugs * nschum de>
;; Version: 1.0
;; Keywords: faces
;; URL: http://nschum.de/src/emacs/highlight-tabs/
;; Compatibility: GNU Emacs 22.x
;;
;; This file is NOT part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; This mode highlights all characters that cross the 80 character line limit.
;;
;;; Change Log:
;;
;; 2008-08-11 (1.0)
;;    Initial release.
;;
;;; Code:

(defgroup highlight-80+ nil
  "Highlight characters beyond column 80."
  :group 'faces)

(defcustom highlight-80+-columns 80
  "*Number of columns to allow in lines."
  :group 'highlight-80+
  :type 'integer)

(defface highlight-80+-line
  nil
  "*Face for showing lines with over `highlight-80+-columns'."
  :group 'highlight-80+-line)

(defface highlight-80+
  '((((background dark)) (:background "blue"))
    (((background light)) (:background "dark gray")))
  "*Face for showing characters beyond column `highlight-80+-columns'."
  :group 'highlight-80+-line)

(defface highlight-80+-first
  nil
  "*Face for showing the first character beyond `highlight-80+-columns'."
  :group 'highlight-80+-line)

(defconst highlight-80+-keywords
  `((highlight-80+-matcher (0 'highlight-80+-line prepend)
                           (1 'highlight-80+ prepend)
                           (2 'highlight-80+-first prepend))))

(defsubst highlight-80+-format ()
  (if (< tab-width 2)
      "^\\(\\)\\([^\n]\\)\\{80,\\}$"
    (concat (format "^\\(?:[^\t\n]\\{%d\\}\\|[^\t\n]\\{,%d\\}\t\\)\\{%d\\}"
                    tab-width (- tab-width 1)
                    (/ highlight-80+-columns tab-width))
            (let ((remainder (mod highlight-80+-columns tab-width)))
              (when remainder
                (format "\\(?:[^\t\n]\\{%d\\}\\|\t\\)" remainder)))
            "\\(\\(.\\).*\\)$")))

(defvar highlight-80+-last-width 0)
(make-variable-buffer-local 'highlight-80+-last-width)

(defvar highlight-80+-last-keywords "")
(make-variable-buffer-local 'highlight-80+-last-keywords)

(defun highlight-80+-matcher (limit)
  ;; Update search when `tab-width' has changed.
  (unless (equal highlight-80+-last-width tab-width)
    (setq highlight-80+-last-keywords (highlight-80+-format)
          highlight-80+-last-width tab-width)
    ;; The rest of the buffer can't be right, either.
    (let ((font-lock-keywords))
      (font-lock-fontify-buffer)))
  ;; re-search-forward is C and much faster checking columns ourselves
  (re-search-forward highlight-80+-last-keywords nil t))

;;;###autoload
(define-minor-mode highlight-80+-mode
  "Highlight the portions of lines longer than 80 characters."
  nil " 80+" nil
  (if highlight-80+-mode
      (font-lock-add-keywords nil highlight-80+-keywords t)
    (font-lock-remove-keywords nil highlight-80+-keywords)
    (kill-local-variable 'highlight-80+-last-keywords)
    (kill-local-variable 'highlight-80+-last-width))
  (font-lock-fontify-buffer))

(provide 'highlight-80+)
;;; highlight-80+.el ends here