forked from aaptel/preview-latex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx.el
216 lines (181 loc) · 6.87 KB
/
px.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
;;; px.el --- preview inline latex in any mode
;; Copyright (C) 2024 Yury Tarasievich <yurytch@github>
;; Copyright (C) 2014 Aurélien Aptel <aurelien.aptel@gmail.com>
;; Copyright (C) 2013 Rüdiger Sonderfeld <ruediger@c-plusplus.de>
;; Author: Aurélien Aptel <aurelien.aptel@gmail.com>
;; URL: http://github.com/aaptel/preview-latex
;; Version: 1.1
;;; Commentary:
;; Provides functions to preview LaTeX codes like $x^2$ in any
;; buffer/mode.
;; Use `px-preview-region' to preview LaTeX codes delimited by $ pairs
;; in the region.
;; Use `px-preview' to process the whole buffer.
;; Use `px-remove' to remove all images and restore the text back.
;; Use `px-toggle' to toggle between images and text on the whole
;; buffer.
;; Most of this code comes from weechat-latex.el which in turn uses
;; org-mode previewer.
;;; License:
;; 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/>.
;;; Code:
(require 'org)
(defvar px-temp-file-prefix "px-"
"Prefix for temporary files.")
(defvar px-temp-directory-prefix "px-"
"Prefix for temporary directory.")
(defvar px-image-program org-latex-create-formula-image-program
"Program to convert LaTeX fragments.
See `org-latex-create-formula-image-program'")
(defvar px-temp-dir nil
"The temporary directory used for preview images.")
(defvar px--active nil)
(make-variable-buffer-local 'px--active)
(defvar px--xelatex-png-mod-active nil)
(make-variable-buffer-local 'px--xelatex-mod-png-active)
(defun px--xelatex-png-dpi ()
"Calculate DPI for conversion from XeLaTeX-generated PDF to PNG.
{(14/10)? * emacs_font_height_px / ((10/72.0) * 72)} * xetex_pdf_dpi "
(ceiling
(* 72.0 ; standard DPI in XeTeX PDF
(/ (aref (font-info (face-font 'default)) 3) ; total emacs font height in px
(* 72.0 (/ 10.0 72.0 )) ; pixels in 10 pt (default for XeTeX?) glyph height
)
)
)
)
(defvar px--xelatex-png-image-converter
(concat "pdftoppm -png -singlefile "
"-r __DPI__ "
"-W -1 -H -1 %o/%b.pdf %o/1 "
"&& convert %o/1.png -trim %O && rm %o/1.png " ) )
(defun px--adjust-xelatex-png-image-converter ()
"Return xelatex-png image converter description string with DPI adjusted."
(replace-regexp-in-string
"__DPI__" (number-to-string (px--xelatex-png-dpi)) px--xelatex-png-image-converter)
)
(defun px--xelatex-png-init ()
"Init and insert modifications of ORG structures."
(unless px--xelatex-png-mod-active
(setq org-preview-latex-process-alist--xelatex-png
(list
'xelatex-png
':programs '("xelatex" "pdftoppm" "convert")
':description "pdf > png"
':message "you need to install packages for: xetex, poppler and imagemagick."
':image-input-type "pdf"
':image-output-type "png"
':image-size-adjust '(1.0 . 1.0)
':latex-compiler '("xelatex -interaction nonstopmode -output-directory %o %f")
':image-converter (list px--xelatex-png-image-converter)
)
)
(setq org-preview-latex-process-alist
(cons
org-preview-latex-process-alist--xelatex-png
org-preview-latex-process-alist
)
)
(setq org-latex-create-formula-image-program 'xelatex-png)
; alias, obsolete in org 9.*
(setq org-preview-latex-default-process 'xelatex-png)
; separate defvar in px
(setq px-image-program 'xelatex-png)
(setq px--xelatex-png-mod-active t)
) ; end of unless
)
(defun px--create-preview (&optional beg end)
"Wrapper for `org-format-latex'.
The parameter AT should be nil or in (TYPE . POINT) format. With TYPE being a
string showing the matched LaTeX statement (e.g., ``$'') and POINT being the
POINT to replace. If AT is nil replace statements everywhere."
(setcdr (assq 'xelatex-png org-preview-latex-process-alist)
(plist-put (alist-get 'xelatex-png org-preview-latex-process-alist)
:image-converter
(list (px--adjust-xelatex-png-image-converter))
)
)
(if (version< "9" org-version)
(org-format-latex px-temp-file-prefix
beg end
temporary-file-directory
'overlays
"Creating images...%s"
'forbuffer
px-image-program)
(condition-case e
(org-format-latex px-temp-file-prefix
px-temp-dir
'overlays
"Creating images...%s"
(if beg (cons "$" beg) nil)
'forbuffer
px-image-program)
;; if wrong arity, try with one less argument (cf. issue #1)
(wrong-number-of-arguments
(org-format-latex px-temp-file-prefix
px-temp-dir
'overlays
"Creating images...%s"
'forbuffer
px-image-program)))))
(defun px--set-temp-dir ()
"Set `px-temp-dir' unless it is already set."
(unless px-temp-dir
(setq px-temp-dir
(make-temp-file px-temp-directory-prefix
'directory))))
;;;###autoload
(defun px-preview ()
"Preview LaTeX fragments in the current buffer."
(interactive)
(save-excursion
(let ((inhibit-read-only t))
(px--xelatex-png-init)
(px--set-temp-dir)
(px-remove)
(px--create-preview)
(setq px--active t))))
;;;###autoload
(defun px-preview-region (beg end)
"Preview LaTeX fragments in region."
(interactive "r")
(let* ((math-regex (assoc "$" org-latex-regexps))
(regex (nth 1 math-regex))
(n (nth 2 math-regex))
matches)
(save-excursion
(goto-char beg)
(while (re-search-forward regex end t)
(setq matches (cons (cons (match-beginning n) (match-end n)) matches)))
(let ((inhibit-read-only t))
(px--set-temp-dir)
(dolist (i matches)
(px--create-preview i))))))
;;;###autoload
(defun px-remove ()
"Remove LaTeX preview images in current buffer."
(interactive)
(let ((inhibit-read-only t))
(if (version< "9" org-version)
(delete-all-overlays)
(org-remove-latex-fragment-image-overlays)))
(setq px--active nil))
;;;###autoload
(defun px-toggle ()
"Toggle display of LaTeX preview in the current buffer."
(interactive)
(if px--active
(px-remove)
(px-preview)))
(provide 'px)
;;; px.el ends here