forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-glossary.el
571 lines (503 loc) · 17 KB
/
org-ref-glossary.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
;;; org-ref-glossary.el --- glossary support in org-ref -*- lexical-binding: t; -*-
;; Copyright (C) 2016 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Keywords:
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Provides Some glossary support for org-mode. Only export to LaTeX is
;; supported. The functionality is based on the LaTeX glossaries package. See
;; https://en.wikibooks.org/wiki/LaTeX/Glossary and
;; http://ctan.math.washington.edu/tex-archive/macros/latex/contrib/glossaries/glossaries-user.pdf
;; Put something like this in your org-file.
;; #+latex_header: \usepackage{glossaries}
;; #+latex_header: \makeglossaries
;; Put this where you want the glossaries to appear in your org-file.
;; \printglossaries
;; Add new glossary entries to your org-file like this. Enclose strings
;; containing a comma in {}. Multiline entries are supported.
;; #+latex_header_extra: \newglossaryentry{computer}{name=computer,description={A machine, that computes}}
;; #+latex_header_extra: \newglossaryentry{tree}{name=tree,description=a big plant}
;; #+latex_header_extra: \newglossaryentry{naiive}
;; #+latex_header_extra: {
;; #+latex_header_extra: name=na\"{\i}ve,
;; #+latex_header_extra: description={is a French loanword (adjective, form of naïf)
;; #+latex_header_extra: indicating having or showing a lack of experience,
;; #+latex_header_extra: understanding or sophistication}
;; #+latex_header_extra: }
;; Here is an example acronym definition
;; #+latex_header_extra: \newacronym{lvm}{LVM}{Logical Volume Manager}
;; New links defined:
;; gls:name A reference to the glossary entry NAME.
;; glspl:name The plural version of the entry
;; Gls:name Capitalized glossary entry
;; Glspl: Capitalized, plural glossary entry
;; [[gslink:name][alternate text]]
;; glssymbol:name Outputs the symbol value of the glossary entry settings.
;; glsdesc:name The description of name
;; The links export to LaTeX. You can click on the link and jump to the
;; definition. The links have tooltips for the definitions.
;; Acronym links
;; acrshort:label
;; acrfull:label
;; acrlong:label
;; ac:label (exports to \gls{label})
;; Ac:label (exports to \Gls{label})
;; acp:label (exports to \glspl{label})
;; Acp:label (exports to \Glspl{label})
(require 'org-element)
(require 'org-ref-utils)
;;; Code:
(defgroup org-ref-glossary nil
"Customization group for org-ref-glossary."
:tag "Org Ref glossary"
:group 'org)
(defcustom org-ref-glossary-color "Mediumpurple3"
"Color for glossary links."
:type 'string
:group 'org-ref)
(defcustom org-ref-acronym-color "Darkorange2"
"Color for acronym links."
:type 'string
:group 'org-ref)
(defun or-find-closing-curly-bracket (&optional limit)
"Find closing bracket for the bracket at point and move point to it.
Go up to LIMIT or `point-max'. This is a parsing function. I
wrote this because using `forward-list' does not always work if
there is an escaped \" for example. This seems pretty robust."
(unless (looking-at "{") (error "Not at a curley bracket"))
(let ((level 1))
(while (and (not (= 0 level))
(not (eobp))
(< (point) (or limit (point-max))))
(forward-char)
(when (and (looking-at "{")
(not (looking-back "\\\\" (- (point) 2))))
(incf level))
(when (and (looking-at "}")
(not (looking-back "\\\\" (- (point) 2))))
(decf level)))
(point)))
;;* Glossary
(defun or-parse-glossary-entry (entry)
"Parse glossary ENTRY definition to a p-list of key=value.
Typically:
(:name name :description description)
but there could be other :key value pairs."
(save-excursion
(let (end-of-entry
data
key value p1 p2)
(goto-char (point-min))
;; We may not find an entry if it is defined as an acronym
(when (re-search-forward
(format "\\newglossaryentry{%s}" entry) nil t)
(re-search-forward "{")
(save-excursion
(backward-char)
(or-find-closing-curly-bracket)
(setq end-of-entry (point)))
(while (re-search-forward "\\(\\w+?\\)=" end-of-entry t)
(setq key (match-string 1))
;; get value
(goto-char (+ 1 (match-end 1)))
(setq p1 (point))
(if (looking-at "{")
;; value is wrapped in {}
(progn
(or-find-closing-curly-bracket)
(setq p2 (point)
value (buffer-substring (+ 1 p1) p2)))
;; value is up to the next comma
(re-search-forward "," end-of-entry 'mv)
(setq value (buffer-substring p1 (- (point) 1))))
;; remove #+latex_header_extra:
(setq value (replace-regexp-in-string
"#\\+latex_header_extra: " "" value))
(setq value (replace-regexp-in-string
"\n +" " " value))
(setq data (append data
(list (intern (format ":%s" key)))
(list value))))
data))))
;;;###autoload
(defun org-ref-add-glossary-entry (label name description)
"Insert a new glossary entry.
LABEL is how you refer to it with links.
NAME is the name of the entry to be defined.
DESCRIPTION is the definition of the entry.
Entry gets added after the last #+latex_header line."
(interactive "sLabel: \nsName: \nsDescription: ")
(save-excursion
(re-search-backward "#\\+latex_header" nil t)
(forward-line)
(when (not (looking-at "^$"))
(beginning-of-line)
(insert "\n")
(forward-line -1))
(insert (format "#+latex_header_extra: \\newglossaryentry{%s}{name={%s},description={%s}}\n"
label name description))))
;;** Glossary links
(defun or-follow-glossary (entry)
"Goto beginning of the glossary ENTRY."
(org-mark-ring-push)
(goto-char (point-min))
(re-search-forward (format "\\newglossaryentry{%s}" entry))
(goto-char (match-beginning 0)))
;; link to glossary entry
(org-ref-link-set-parameters "gls"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\gls{%s}" path)))))
;; plural
(org-ref-link-set-parameters "glspl"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\glspl{%s}" path)))))
;; capitalized link
(org-ref-link-set-parameters "Gls"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\Gls{%s}" path)))))
;; capitalized plural link
(org-ref-link-set-parameters "Glspl"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\Glspl{%s}" path)))))
(org-ref-link-set-parameters "glslink"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path desc format)
(cond
((eq format 'latex)
(format "\\glslink{%s}{%s}" path desc)))))
(org-ref-link-set-parameters "glssymbol"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path _desc format)
(cond
((eq format 'latex)
(format "\\glssymbol{%s}" path)))))
(org-ref-link-set-parameters "glsdesc"
:follow #'or-follow-glossary
:face 'org-ref-glossary-face
:help-echo 'or-glossary-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\glsdesc{%s}" path)))))
;;** Tooltips on glossary entries
(defface org-ref-glossary-face
`((t (:inherit org-link :foreground ,org-ref-glossary-color)))
"Face for glossary links.")
(defun or-glossary-tooltip (_window _object position)
"Return tooltip for the glossary entry.
The entry is in WINDOW and OBJECT at POSITION.
Used in fontification."
(save-excursion
(goto-char position)
(let* ((label (org-element-property :path (org-element-context)))
(data (or (or-parse-glossary-entry label)
(or-parse-acronym-entry label)))
(name (or (plist-get data :name)
(plist-get data :abbrv)))
(description (or (plist-get data :description)
(plist-get data :full))))
(format
"%s: %s"
name
(with-temp-buffer
(insert (concat description "."))
(fill-paragraph)
(buffer-string))))))
(unless (fboundp 'org-link-set-parameters)
(defun or-next-glossary-link (limit)
"Search to next glossary link up to LIMIT.
Adds a tooltip to the link that is found."
(when (and (re-search-forward
(concat
(regexp-opt '("gls" "glspl"
"Gls" "Glspl"
"glslink"
"glssymbol"
"glsdesc"))
":[a-zA-Z]\\{2,\\}")
limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
(forward-char -2)
(let ((next-link (org-element-context)))
(if next-link
(progn
(set-match-data (list (org-element-property :begin next-link)
(- (org-element-property :end next-link)
(org-element-property :post-blank next-link))))
(add-text-properties
(org-element-property :begin next-link)
(- (org-element-property :end next-link)
(org-element-property :post-blank next-link))
(list
'help-echo 'or-glossary-tooltip))
(goto-char (org-element-property :end next-link)))
(goto-char limit)
nil)))))
;;* Acronyms
;;;###autoload
(defun org-ref-add-acronym-entry (label abbrv full)
"Add an acronym entry with LABEL.
ABBRV is the abbreviated form.
FULL is the expanded acronym."
(interactive "sLabel: \nsAcronym: \nsFull name: ")
(save-excursion
(re-search-backward "#\\+latex_header" nil t)
(forward-line)
(when (not (looking-at "^$"))
(beginning-of-line)
(insert "\n")
(forward-line -1))
(insert (format "#+latex_header_extra: \\newacronym{%s}{%s}{%s}\n"
label abbrv full))))
(defun or-parse-acronym-entry (label)
"Parse an acronym entry LABEL to a plist.
\(:abbrv abbrv :full full)
\newacronym{<label>}{<abbrv>}{<full>}"
(save-excursion
(let (abbrv full p1)
(goto-char (point-min))
(when
(re-search-forward (format "\\newacronym{%s}" label) nil t)
(setq p1 (+ 1 (point)))
(forward-list)
(setq abbrv (buffer-substring p1 (- (point) 1)))
(setq p1 (+ 1 (point)))
(forward-list)
(setq full (buffer-substring p1 (- (point) 1)))
(list :abbrv abbrv :full full)))))
;;** Acronym links
(defun or-follow-acronym (label)
"Go to the definition of the acronym LABEL."
(org-mark-ring-push)
(goto-char (point-min))
(re-search-forward (format "\\\\newacronym{%s}" label))
(goto-char (match-beginning 0)))
(org-ref-link-set-parameters "acrshort"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\acrshort{%s}" path)))))
(org-ref-link-set-parameters "acrlong"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\acrlong{%s}" path)))))
(org-ref-link-set-parameters "acrfull"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\acrfull{%s}" path)))))
;; Shortcuts
(org-ref-link-set-parameters "ac"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\gls{%s}" path)))))
(org-ref-link-set-parameters "Ac"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\Gls{%s}" path)))))
(org-ref-link-set-parameters "acp"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\glspl{%s}" path)))))
(org-ref-link-set-parameters "Acp"
:follow #'or-follow-acronym
:face 'org-ref-acronym-face
:help-echo 'or-acronym-tooltip
:export (lambda (path _ format)
(cond
((eq format 'latex)
(format "\\Glspl{%s}" path)))))
;;** Tooltips on acronyms
(defface org-ref-acronym-face
`((t (:inherit org-link :foreground ,org-ref-acronym-color)))
"Face for acronym links.")
(defun or-acronym-tooltip (_window _object position)
"Return tooltip for the acronym entry.
The entry is in WINDOW and OBJECT at POSITION.
Used in fontification.
WINDOW and OBJECT are ignored."
(save-excursion
(goto-char position)
(let* ((label (org-element-property :path (org-element-context)))
(acronym-data (or-parse-acronym-entry label))
(abbrv (plist-get acronym-data :abbrv))
(full (plist-get acronym-data :full)))
(if acronym-data
(format
"%s: %s"
abbrv full)
(format "%s is not defined in this file." label)))))
;; We use search instead of a regexp to match links with descriptions. These are
;; hard to do with regexps.
(unless (fboundp 'org-link-set-parameters)
(defun or-next-acronym-link (limit)
"Search to next acronym link up to LIMIT and add a tooltip."
(when (and (re-search-forward
(concat
(regexp-opt '("acrshort" "acrfull" "acrlong" "ac" "Ac" "acp" "Acp"))
":[a-zA-Z]\\{2,\\}")
limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
(save-excursion
(forward-char -2)
(let ((next-link (org-element-context)))
(if next-link
(progn
(set-match-data
(list (org-element-property :begin next-link)
(- (org-element-property :end next-link)
(org-element-property :post-blank next-link))))
(add-text-properties
(org-element-property :begin next-link)
(- (org-element-property :end next-link)
(org-element-property :post-blank next-link))
(list
'help-echo 'or-acronym-tooltip))
(goto-char (org-element-property :end next-link)))
(goto-char limit)
nil))))))
;; * Helm command to insert entries
;;;###autoload
(defun org-ref-insert-glossary-link ()
"Helm command to insert glossary and acronym entries as links."
(interactive)
;; gather entries
(let ((glossary-candidates '())
(acronym-candidates '())
key
entry)
(save-excursion
(goto-char (point-min))
(while (re-search-forward
"\\\\newglossaryentry{\\([[:ascii:]]+?\\)}" nil t)
(setq key (match-string 1)
entry (or-parse-glossary-entry key))
(setq glossary-candidates
(append
glossary-candidates
(list
(cons
;; for helm
(format "%s: %s."
(plist-get entry :name)
(plist-get entry :description))
;; the returned candidate
(list key
(plist-get entry :name))))))))
;; acronym candidates
(save-excursion
(goto-char (point-min))
(while (re-search-forward
"\\\\newacronym{\\([[:ascii:]]+?\\)}" nil t)
(setq key (match-string 1)
entry (or-parse-acronym-entry key))
(setq acronym-candidates
(append
acronym-candidates
(list
(cons
;; for helm
(format "%s (%s)."
(plist-get entry :full)
(plist-get entry :abbrv))
;; the returned candidate
(list key
(plist-get entry :abbrv))))))))
(helm :sources
`(,(helm-build-sync-source "Insert glossary term"
:candidates glossary-candidates
:action (lambda (candidate)
(insert (format
"[[%s:%s][%s]]"
(completing-read "Type: "
'("gls"
"glspl"
"Gls"
"Glspl"
"glssymbol"
"glsdesc")
nil t
"gls")
(nth 0 candidate)
(nth 1 candidate)))))
,(helm-build-sync-source "Insert acronym term"
:candidates acronym-candidates
:action (lambda (candidate)
(insert (format
"[[%s:%s][%s]]"
(completing-read "Type: "
'("acrshort"
"acrlong"
"acrfull"
"ac"
"Ac"
"acp"
"Acp")
nil t
"ac")
(nth 0 candidate)
(nth 1 candidate)))))
,(helm-build-sync-source "Add new term"
:candidates '(("Add glossary term" . org-ref-add-glossary-entry)
("Add acronym term" . org-ref-add-acronym-entry))
:action (lambda (x)
(call-interactively x)))))))
(provide 'org-ref-glossary)
;;; org-ref-glossary.el ends here