-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo-variable-pitch.el
215 lines (192 loc) · 7.31 KB
/
info-variable-pitch.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
;;; info-variable-pitch.el --- Proportional fonts in Info only when appropriate -*- lexical-binding: t -*-
;; Author: Kisaragi Hiu
;; Version: 0.2.0
;; Package-Requires: ((emacs "25.1") (s "1.12.0"))
;; Homepage: https://github.com/kisaragi-hiu/info-variable-pitch
;; Keywords: faces
;; This file is not part of GNU Emacs
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Like org-variable-pitch but for Info.
;;; Code:
(require 'face-remap)
(require 's)
(defgroup info-variable-pitch nil
"Settings for info-variable-pitch."
:group 'info
:prefix "info-variable-pitch-")
(defcustom info-variable-pitch-want-upper-as-variable t
"Whether to fontify upper case as a variable.
This makes Emacs Lisp docs look nicer at the expense of
highlighting the likes of IEEE, GNU, REPL, FSF, etc. as if they
are variables.
This should be set before the package is loaded."
:group 'info-variable-pitch
:type 'boolean)
(defface info-variable-pitch-emphasis
`((t . (:inherit (bold italic))))
"Face used for emphasis like \"_this_\"."
:group 'info-variable-pitch)
(defvar-local info-variable-pitch--face-remap-entries nil)
;; A more robust way would be to literally parse the pages ourselves
;; instead of relying on font-lock to do the right thing per line.
;;
;; This is err... a bit of a mess. But it works well enough most of
;; the time.
(defvar info-variable-pitch--font-lock-keywords
`(;; Indented stuff
(,(rx bol (+ " ")
(or "|" "-" ; diagrams
"error→" ; error messages
"nil"
"$" ; shell
"#"
;; (elisp)Integer Basics
(seq " " (opt (any "+-")) digit (any ". "))
;; Lisp code blocks
(seq "("
;; If the first character is upper case, this is
;; more likely to be prose. Most callables don't
;; start with an upper case letter.
(not (any upper))
;; If this "form" is closed by the second
;; character, it's probably a bullet like (1).
(not ")")))
(* any))
(0
(let ((start (match-beginning 0))
(end (match-end 0)))
(put-text-property
start end
'face 'fixed-pitch))))
;; 8+ spaces must be a code block, I hope...
("^[ \t]\\{8,\\}\\(.*\\)"
(0
(let ((start (match-beginning 0))
(end (match-end 0)))
(unless (memq 'invisible (text-properties-at start))
(put-text-property
start end
'face 'fixed-pitch)))))
,@(when info-variable-pitch-want-upper-as-variable
;; Treat all UPPER-CASE to be variables
'(("[[:upper:]][[:upper:]][[:upper:][:digit:]-]*" . 'fixed-pitch)))
;; See (info "(elisp)Buffer Text Notation")
;; Point location indicator.
("★" . 'font-lock-constant-face)
;; (,(rx (or "&optional" "&rest" "&key"))
;; . 'font-lock-type-face)
;; Leading spaces or the " -- " for " -- Function: "
(,(rx bol (or " -- " (+ " "))) . 'fixed-pitch)
(,(rx bol " -- \\(?:Variable\\|User Option\\): "
(group (* any)))
(1 (put-text-property
(match-beginning 1)
(match-end 1)
'face '(fixed-pitch
font-lock-variable-name-face))))
(,(rx bol " -- "
(or "Function" "Macro" "Command" "Special Form")
": "
(group (* (not (any " "))))
(group (opt " " (* any))))
(1 (put-text-property
(match-beginning 1)
(match-end 1)
'face '(fixed-pitch
font-lock-function-name-face)))
(2
;; -- Function: foo integer1 &optional integer2 &rest integers
;; &optional and &rest are highlighted with
;; `font-lock-type-face', the rest is highlighted with info-variable-pitch-fixed-face.
(let ((start (match-beginning 2))
(end (match-end 2))
(str (match-string 2)))
(put-text-property
start end
'face 'fixed-pitch)
(dolist (pair (s-matched-positions-all
,(rx (or "&rest" "&optional"
"&key" "&allow-other-keys"))
str))
(put-text-property
(+ start (car pair))
(+ start (cdr pair))
'face '(fixed-pitch
font-lock-type-face))))))
;; Highlight _text-like-this_ as emphasis
;; Example: (info "(org)Exporting")
(,(rx (group "_")
(group (*? (any alnum "-")))
(group "_"))
(1
(let ((start (match-beginning 1))
(end (match-end 1)))
(put-text-property
start end
'invisible t)))
(2
(let ((start (match-beginning 2))
(end (match-end 2)))
(put-text-property
start end
'face 'info-variable-pitch-emphasis)))
(3
(let ((start (match-beginning 3))
(end (match-end 3)))
(put-text-property
start end
'invisible t))))))
(defun info-variable-pitch-mode--on ()
"Do the actual work when enabling `info-variable-pitch-mode'."
(variable-pitch-mode)
(font-lock-add-keywords nil info-variable-pitch--font-lock-keywords)
;; This doesn't disable case fold for whatever reason:
;; (setq-local font-lock-defaults
;; '(Info-mode-font-lock-keywords t nil))
;; Just... set it directly. Sigh.
(setq-local font-lock-keywords-case-fold-search nil)
(dolist (face '(font-lock-keyword-face
Info-quoted))
(when (facep face)
(push (face-remap-add-relative face 'fixed-pitch)
info-variable-pitch--face-remap-entries)))
(font-lock-flush))
(defun info-variable-pitch-mode--off ()
"Do the actual work when disabling `info-variable-pitch-mode'."
(variable-pitch-mode -1)
(setq-local font-lock-keywords-case-fold-search t)
(mapc #'face-remap-remove-relative info-variable-pitch--face-remap-entries)
(setq info-variable-pitch--face-remap-entries nil)
;; This is useful during development to allow
;; `info-variable-pitch--font-lock-keywords' to change without
;; making it impossible to undo our changes, but it's wrong as
;; Info's default keywords may not have stayed the same. Leaving it
;; here commented out for convenience.
;;
;; (setq font-lock-keywords
;; '(t
;; (("‘\\([‘’]\\|[^‘’]*\\)’"
;; (1 'Info-quoted)))
;; ("‘\\([‘’]\\|[^‘’]*\\)’"
;; (1 'Info-quoted))))
;;
(font-lock-remove-keywords nil info-variable-pitch--font-lock-keywords))
;;;###autoload
(define-minor-mode info-variable-pitch-mode
"Info variable-pitch mode."
:global nil :lighter IVP
(if info-variable-pitch-mode
(info-variable-pitch-mode--on)
(info-variable-pitch-mode--off)))
(provide 'info-variable-pitch)
;;; info-variable-pitch.el ends here