forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-misc-links.el
304 lines (267 loc) · 9.56 KB
/
org-ref-misc-links.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
;;; org-ref-misc-links.el --- Miscellaneous links -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021 John Kitchin
;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
;; Keywords: convenience
;; 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/>.
;;
;; * Miscellaneous links
;;; Commentary:
;;
;;; Code:
;;** List of figures
;;;###autoload
(defun org-ref-list-of-figures (&optional _arg)
"Generate buffer with list of figures in them.
ARG does nothing.
Ignore figures in COMMENTED sections."
(interactive)
(save-excursion
(widen)
(let* ((c-b (buffer-name))
(counter 0)
(list-of-figures
(org-element-map (org-ref-parse-buffer) 'link
(lambda (link)
"create a link for to the figure"
(when
(and (string= (org-element-property :type link) "file")
(string-match-p
"[^.]*\\.\\(png\\|jpg\\|eps\\|pdf\\|svg\\)$"
(org-element-property :path link))
;; ignore commented sections
(save-excursion
(goto-char (org-element-property :begin link))
(not (or (org-in-commented-heading-p)
(org-at-comment-p)
(-intersection (org-get-tags) org-export-exclude-tags)))))
(cl-incf counter)
(let* ((start (org-element-property :begin link))
(linenum (progn (goto-char start) (line-number-at-pos)))
(fname (org-element-property :path link))
(parent (car (cdr
(org-element-property :parent link))))
(caption (cl-caaar (plist-get parent :caption)))
(name (plist-get parent :name)))
(if caption
(format "[[file:%s::%s][Figure %s:]] %s\n" c-b linenum counter caption)
;; if it has no caption, try the name
;; if it has no name, use the file name
(cond (name
(format "[[file:%s::%s][Figure %s:]] %s\n" c-b linenum counter name))
(fname
(format "[[file:%s::%s][Figure %s:]] %s\n"
c-b linenum counter fname))))))))))
(switch-to-buffer "*List of Figures*")
(setq buffer-read-only nil)
(org-mode)
(erase-buffer)
(insert (mapconcat 'identity list-of-figures ""))
(goto-char (point-min))
;; open links in the same window
(setq-local org-link-frame-setup
'((file . find-file)))
(setq buffer-read-only t)
(use-local-map (copy-keymap org-mode-map))
(local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
(org-link-set-parameters "list-of-figures"
:follow #'org-ref-list-of-figures
:export (lambda (_path _desc format)
(cond
((eq format 'latex)
(format "\\listoffigures")))))
;;** List of tables
;;;###autoload
(defun org-ref-list-of-tables (&optional _arg)
"Generate a buffer with a list of tables.
ARG does nothing."
(interactive)
(save-excursion
(widen)
(let* ((c-b (buffer-name))
(counter 0)
(list-of-tables
(org-element-map (org-ref-parse-buffer 'element) 'table
(lambda (table)
"create a link for to the table"
(save-excursion
(goto-char (org-element-property :begin table))
(when
;; ignore commented sections
(not (or (org-in-commented-heading-p)
(-intersection (org-get-tags) org-export-exclude-tags)
(looking-at "#\\+RESULTS:") ))
(cl-incf counter)
(let* ((start (org-element-property :begin table))
(linenum (progn (goto-char start) (line-number-at-pos)))
(caption (cl-caaar (org-element-property :caption table)))
(name (org-element-property :name table)))
(if caption
(format "[[file:%s::%s][Table %s:]] %s\n" c-b linenum counter caption)
;; if it has no caption, try the name
;; if it has no name, use generic name
(cond (name
(format "[[file:%s::%s][Table %s:]] %s\n"
c-b linenum counter name))
(t
(format "[[file:%s::%s][Table %s:]] No caption\n"
c-b linenum counter)))))))))))
(switch-to-buffer "*List of Tables*")
(setq buffer-read-only nil)
(org-mode)
(erase-buffer)
(insert (mapconcat 'identity list-of-tables ""))
(goto-char (point-min))
;; open links in the same window
(setq-local org-link-frame-setup
'((file . find-file)))
(setq buffer-read-only t)
(use-local-map (copy-keymap org-mode-map))
(local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
(org-link-set-parameters "list-of-tables"
:follow #'org-ref-list-of-tables
:export (lambda (_path _desc format)
(cond
((eq format 'latex)
(format "\\listoftables")))))
;;* Index link
;;
;; You need these lines in the header
;;
;; #+latex_header: \usepackage{makeidx}
;; #+latex_header: \makeindex
(org-link-set-parameters "index"
:follow (lambda (path)
(occur path))
:export (lambda (path _desc format)
(cond
((eq format 'latex)
(format "\\index{%s}" path)))))
;; this will generate a temporary index of entries in the file when clicked on.
;;;###autoload
(defun org-ref-index (&optional _path)
"Open an *index* buffer with links to index entries.
PATH is required for the org-link, but it does nothing here."
(interactive)
(let ((*index-links* '())
(*initial-letters* '()))
;; get links
(org-element-map (org-ref-parse-buffer) 'link
(lambda (link)
(let ((plist (nth 1 link)))
(when (equal (plist-get plist ':type) "index")
(add-to-list
'*index-links*
(cons (plist-get plist :path)
(format
"[[elisp:(progn (switch-to-buffer \"%s\") (goto-char %s) (org-cycle '(64)))][%s]] "
(current-buffer)
(plist-get plist :begin) ;; position of link
;; grab a description
(save-excursion
(goto-char (plist-get plist :begin))
(if (thing-at-point 'sentence)
;; get a sentence
(let ((s (thing-at-point 'sentence)))
(cl-loop for char in '("[" "]" "\n")
do
(setq s (replace-regexp-in-string
(regexp-quote char) " " s)))
(concat s " "))
;; or call it a link
"link")))))))))
;; sort the links
(setq *index-links* (cl-sort *index-links* 'string-lessp :key 'car))
;; now separate out into chunks first letters
(dolist (link *index-links*)
(push (substring (car link) 0 1) *initial-letters*))
(setq *initial-letters* (reverse *initial-letters*))
;; now create the index
(switch-to-buffer (get-buffer-create "*index*"))
(org-mode)
(erase-buffer)
(insert "#+TITLE: Index\n\n")
(dolist (letter *initial-letters*)
(insert (format "* %s\n" (upcase letter)))
;; now process the links
(while (and
*index-links*
(string= letter (substring (car (car *index-links*)) 0 1)))
(let ((link (pop *index-links*)))
(insert (format "%s %s\n\n" (car link) (cdr link))))))
(switch-to-buffer "*index*")))
(org-link-set-parameters "printindex"
:follow #'org-ref-index
:export (lambda (_path _desc format)
(cond
((eq format 'latex)
(format "\\printindex")))))
(defun org-ref-idxproc (_backend)
"Preprocess index entries.
Each entry is replaced by a radio target. The printindex is
replaced by links to them."
(let* ((index-links (reverse (org-element-map (org-element-parse-buffer) 'link
(lambda (lnk)
(when (string= "index" (org-element-property :type lnk))
lnk)))))
(sorted-groups (seq-sort-by
(lambda (x)
"Alphabetically sort groups"
(car x))
#'string-lessp
(seq-group-by
(lambda (lnk)
(org-element-property :path lnk))
index-links)))
(link-replacements '()))
;; Sort within each group
(cl-loop for (key . links) in sorted-groups do
(setf (cdr (assoc key sorted-groups))
(sort links (lambda (a b)
(< (org-element-property :begin a)
(org-element-property :begin b))))))
;; Compute replacements for each index link.
(cl-loop for (key . links) in sorted-groups do
(cl-loop for i from 0 for lnk in links do
(cl-pushnew (cons
(org-element-property :begin lnk)
(format "<<%s-%s>>" key i))
link-replacements)))
(cl-loop for il in index-links collect
(setf (buffer-substring (org-element-property :begin il)
(org-element-property :end il))
(cdr (assoc (org-element-property :begin il) link-replacements))))
;; Now we replace the printindex link
(org-element-map (org-element-parse-buffer) 'link
(lambda (lnk)
(when (string= "printindex" (org-element-property :type lnk))
(setf (buffer-substring (org-element-property :begin lnk)
(org-element-property :end lnk))
;; If sorted-groups is empty, we should do nothing I think.
(if sorted-groups
(format "*Index*\n\n%s"
(string-join
(cl-loop for (key . links) in sorted-groups collect
(format "%s: %s"
key
(string-join
(cl-loop for i from 0 for lnk in links collect
(format "[[%s-%s][%s-%s]] "
(org-element-property :path lnk)
i
(org-element-property :path lnk)
i))
", ")))
"\n\n"))
"")))))))
(provide 'org-ref-misc-links)
;;; org-ref-misc-links.el ends here