-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinfo-colors.el
174 lines (146 loc) · 6.4 KB
/
info-colors.el
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;;; info-colors.el --- Extra colors for Info-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Tuấn-Anh Nguyễn
;; Author: Tuấn-Anh Nguyễn <ubolonton@gmail.com>
;; Author: Drew Adams
;; Keywords: faces
;; URL: https://github.com/ubolonton/info-colors
;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
;; Package-Version: 0.2.2
;; Package-X-Original-Version: 0
;; 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 3 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This is a modern adaption of the extra coloring provided by Drew
;; Adams' `info+' package.
;; To enable this:
;; (add-hook 'Info-selection-hook 'info-colors-fontify-node)
;;; Code:
(require 'info)
(require 'cl-lib)
;;;###autoload
(defgroup info-colors nil
"Extra font lock rules for Info-mode."
:group 'info)
(defface info-colors-ref-item-type
'((t (:inherit font-lock-type-face)))
"Face for type of info's reference items."
:group 'info-colors)
(defface info-colors-ref-item-syntax-class
'((t (:inherit font-lock-preprocessor-face)))
"Face for names of `Syntax class' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-constant
'((t (:inherit font-lock-constant-face)))
"Face for names of `Constant' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-variable
'((t (:inherit font-lock-variable-name-face)))
"Face for names of `Variable' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-user-option
'((t (:inherit font-lock-variable-name-face)))
"Face for names of `User Option' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-special-form
'((t (:inherit font-lock-keyword-face)))
"Face for names of `Special Form' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-macro
'((t (:inherit font-lock-keyword-face)))
"Face for names of `Macro' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-function
'((t (:inherit font-lock-function-name-face)))
"Face for names of `Function' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-command
'((t (:inherit font-lock-function-name-face)))
"Face for names of `Command' reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-ref-item-other
'((t (:inherit font-lock-variable-name-face)))
"Face for the remaining parts of reference items in `info' nodes."
:group 'info-colors)
(defface info-colors-lisp-code-block
'((t (:inherit fixed-pitch)))
"Face for Lisp code blocks in `info' nodes."
:group 'info-colors)
;;; TODO: Don't fontify indents.
;;; TODO: Fontify as Lisp code.
;;;###autoload
(defun info-colors-fontify-lisp-code-blocks ()
"Fontify Lisp code blocks in an `info' node."
(goto-char (point-min))
;; @nerton: Find code blocks by looking for two newlines followed by at least 5 spaces and
;; check if the line before the two newlines (the previous paragraph) has a 5 space less indent
;; than the found line. See https://github.com/ubolonton/info-colors/issues/2
(while (re-search-forward
"\\(.+\\)\n\n\\( \\{5,\\}\\)"
nil t)
(let* ((prev-line (match-string 1))
(start (match-beginning 2))
(indent (length (match-string 2)))
(prev-indent (if (string-match "^ +" prev-line)
(length (match-string 0 prev-line))
0))
;; Example: ‘C-x l’ (‘transient-set-level’)
(prev-is-kb-desc? (string-prefix-p "‘" prev-line))
(small-indent-change? (< (- indent prev-indent) 5)))
(if (or prev-is-kb-desc? small-indent-change?)
;; Not the begin of a code block. Continue.
(beginning-of-line)
;; Look for the end of the code block.
(while (progn
(re-search-forward "\n\n")
(and (not (eobp))
(looking-at " *")
(= (length (match-string 0)) indent))))
(put-text-property (1- start) (point)
'font-lock-face 'info-colors-lisp-code-block)))))
;;; TODO: Use syntax table or something?
;;;###autoload
(defun info-colors-fontify-reference-items ()
"Fontify reference items in an `info' node."
(goto-char (point-min))
(while (re-search-forward
"^ --? \
\\(Command\\|Constant\\|Function\\|Macro\\|Special Form\\|Syntax class\\|User Option\\|Variable\\):\
*\\(\\S-+\\)\
\\(\\( .*\\)?\\([\n] \\{8\\}.*\\)*\\)"
nil t)
(let ((sym (intern (match-string 1))))
(put-text-property (match-beginning 1) (match-end 1)
'font-lock-face 'info-colors-ref-item-type)
(put-text-property
(match-beginning 2) (match-end 2)
'font-lock-face (cl-case sym
(Constant 'info-colors-ref-item-constant)
(Variable 'info-colors-ref-item-variable)
(User\ Option 'info-colors-ref-item-user-option)
(Special\ Form 'info-colors-ref-item-special-form)
(Macro 'info-colors-ref-item-macro)
(Function 'info-colors-ref-item-function)
(Command 'info-colors-ref-item-command)
(Syntax\ class 'info-colors-ref-item-syntax-class)))
(when (match-beginning 3)
(put-text-property (match-beginning 3) (match-end 3)
'font-lock-face 'info-colors-ref-item-other)))))
;;;###autoload
(defun info-colors-fontify-node ()
"Fontify an `info' node."
(save-excursion
(let* ((inhibit-read-only t)
(case-fold-search t))
(info-colors-fontify-lisp-code-blocks)
(info-colors-fontify-reference-items)
(set-buffer-modified-p nil))))
(provide 'info-colors)
;;; info-colors.el ends here