-
Notifications
You must be signed in to change notification settings - Fork 4
/
px.el
150 lines (120 loc) · 4.57 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
;;; px.el --- preview inline latex in any mode
;; Copyright (C) 2014 Aurélien Aptel <[email protected]>
;; Copyright (C) 2013 Rüdiger Sonderfeld <[email protected]>
;; Author: Aurélien Aptel <[email protected]>
;; 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)
(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."
(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--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