yatex

view yatexflt.el @ 569:e3209b30680c

[prefix] bq produces <blockquote></blockquote>
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 09 Mar 2019 22:25:22 +0900
parents ea6956f10ce7
children
line source
1 ;;; yatexflt.el --- YaTeX filter command utilizer -*- coding: sjis -*-
2 ;;;
3 ;;; (c)1993-2018 by HIROSE Yuuji.[yuuji@yatex.org]
4 ;;; Last modified Sat Jun 2 18:12:41 2018 on firestorm
5 ;;; $Id$
7 ;;; Commentary:
8 ;;;
9 ;;; This lisp enables passing inline text to some external filter
10 ;;; command to generate files such as graphic files.
11 ;;;
12 ;;; Typical situation is using blockdiag/dot(graphviz) command to
13 ;;; generate png/pdf file.
14 ;;;
15 ;;; Example:
16 ;;;
17 ;;; [[LaTeX Source]]
18 ;;; %#BEGIN FILTER{foo.pdf}{dot -T %t -o o}
19 ;;; \if0
20 ;;; ===
21 ;;; digraph {
22 ;;; A -> B;
23 ;;; B -> C;
24 ;;; }
25 ;;; ===
26 ;;; \fi
27 ;;; %#END
28 ;;; \includegraphics{foo.pdf}
29 ;;;
30 ;;; In this case above, when you type `[prefix] t e' between two
31 ;;; `===' lines, the content in a region are fed to dot command as
32 ;;; follows:
33 ;;;
34 ;;; echo TEXT | dot -T pdf -o foo.pdf
35 ;;;
36 ;;; Then foo.pdf file will be generated and the image (as PNG) will
37 ;;; be displayed in the next window.
40 ;;; Code:
41 (require 'yatexlib)
42 (defvar YaTeX-filter-special-env-alist-default
43 '((".blockdiag"
44 "blockdiag -T %t -o %o -"
45 "blockdiag {
46 default_fontsize = 32;
47 A -> B;
48 }")
49 (".seqdiag" "seqdiag -T %t -o %o -"
50 "seqdiag {
51 client -> server [label = \"SYN\"];
52 client <- server [label = \"SYN/ACK\"];
53 client -> server [label = \"ACK\"];}")
54 (".actdiag" "actdiag -T %t -o %o -"
55 "actdiag {
56 sayHo -> ho -> hohoho
57 lane dj {
58 label = \"DJ\"
59 sayHo [label = \"Say Ho\"]; hohoho [label = \"Ho Ho Ho!\"]; }
60 lane mc { label = \"MC\"; ho [label = \"Hooooh!\"]}}")
61 (".nwdiag" "nwdiag -T %t -o %o -"
62 "nwdiag {
63 network ext {
64 address = \"10.1.2.0/24\"
65 router [address = \"10.1.2.1\"]
66 }
67 network int {
68 address = \"192.168.22.0/24\"
69 router [address = \"192.168.22.1\"]
70 websrv [address = \"192.168.22.80\"]
71 cli-1; cli-2
72 }
73 }")
74 (".rackdiag" "rackdiag -T %t -o %o -"
75 "rackdiag {
76 16U;
77 1: UPS [4U]; 5: Storage [3U]; 8: PC [2U]; 8: PC [2U];
78 }")
79 (".dot"
80 "dot -T %t -o %o"
81 "digraph {
82 graph [charset=\"utf-8\"]
83 A -> B
84 }"
85 )))
87 ;;;###autoload
88 (defun YaTeX-filter-goto-source (file other-win)
89 "Go to corresponding text source of the graphic file"
90 (cond
91 ((file-exists-p file)
92 (let ((buf (find-file-noselect file)))
93 (funcall (cond (other-win 'YaTeX-switch-to-buffer-other-window)
94 ((get-buffer-window buf) 'goto-buffer-window)
95 (t 'YaTeX-switch-to-buffer))
96 buf)))))
98 (defvar YaTeX-filter-special-env-alist-private nil)
99 (defvar YaTeX-filter-special-env-alist
100 (append YaTeX-filter-special-env-alist-private
101 YaTeX-filter-special-env-alist-default))
103 (defun YaTeX-filter-filter-set-conversion-flag ()
104 (let ((ovl (get 'YaTeX-filter-filter-sentinel 'overlay)))
105 (if ovl ;; When successful conversion met,
106 (progn ;; (1)Set conversion complete flag
107 (add-hook ;; (2)Add hook of seim-automatic
108 'write-file-hooks ;; update of convert to write-
109 'YaTeX-filter-update-all) ;; file hook.
110 (overlay-put ovl 'converted t)))))
112 (defun YaTeX-filter-filter-unset-conversion-flag
113 (ovl after beg end &optional length)
114 (if after (overlay-put ovl 'converted nil)))
117 (defun YaTeX-filter-pngify-sentinel (proc msg)
118 (save-excursion
119 (let ((b (process-buffer proc)) (selw (selected-window))
120 img)
121 (set-buffer b)
122 (cond
123 ((eq (process-status proc) 'run)
124 (put-text-property (point-min) (point-max) 'invisible t))
125 ((eq (process-status proc) 'exit)
126 (set-buffer b)
127 (YaTeX-popup-image
128 (YaTeX-buffer-substring
129 (get 'YaTeX-filter-pngify-sentinel 'start) (point-max))
130 b)
131 (YaTeX-filter-filter-set-conversion-flag))
132 (t
133 (set-buffer b)
134 (remove-text-properties (point-min) (point-max) '(invisible t))
135 (insert "\nProcess aborted %s\n" msg))))))
137 (defvar YaTeX-filter-pdf2png-stdout
138 (cond
139 ((YaTeX-executable-find "convert") "convert -trim %s PNG:-")
140 (t
141 "gs -dNOPAUSE -sDEVICE=png256 -sOutputFile=- -dBATCH -q -r75 %s"))
142 "Command line syntax to convert PDF file to PNG stream")
144 (defun YaTeX-filter-modified-BEGEND-regions ()
145 "Return the list of overlays which contains un-converted text."
146 (save-excursion
147 (save-restriction
148 (widen)
149 (let (r prop dest src pl (list (overlays-in (point-min) (point-max))))
150 (while list
151 (setq prop (overlay-properties (car list)))
152 (if (setq dest (plist-get prop 'filter-output))
153 (if (if (setq src (plist-get prop 'filter-source))
154 (file-newer-than-file-p src dest)
155 (and (setq pl (plist-member prop 'converted))
156 (not (plist-get pl 'converted))))
157 (setq r (cons (car list) r))))
158 (setq list (cdr list)))
159 (nconc r)
160 r))))
162 (defun YaTeX-filter-update-all ()
163 "Update all destination files from built-in source text."
164 (interactive)
165 (let ((timeout 4)
166 ans ovl (update-list (YaTeX-filter-modified-BEGEND-regions)))
167 (if update-list
168 (save-excursion
169 (save-window-excursion
170 (catch 'abort
171 (while update-list
172 (goto-char (overlay-start (setq ovl (car update-list))))
173 (or (pos-visible-in-window-p)
174 (set-window-start nil (point)))
175 (unwind-protect
176 (progn
177 (overlay-put ovl 'face 'YaTeX-on-the-fly-activated-face)
178 (message "Non-update source found: Update here: %s "
179 "Y)es N)o S)top-watching-Here A)bort")
180 (setq ans (read-char))
181 (cond
182 ((memq ans '(?Y ?y))
183 (YaTeX-filter-BEGEND)
184 (while (and (> (setq timeout (1- timeout)))
185 (eq (process-status "Filter") 'run))
186 (message "Waiting for conversion process to finish")
187 (sit-for 1)))
188 ((memq ans '(?A ?a)) (throw 'abort t))
189 ((memq ans '(?S ?s)) (delete-overlay ovl))
190 (t nil)))
191 (overlay-put ovl 'face nil))
192 (setq update-list (cdr update-list)))))))
193 ;; Write file hook should return nil
194 nil))
196 (defun YaTeX-filter-filter-sentinel (proc msg)
197 (put 'YaTeX-filter-pngify-sentinel 'start nil)
198 (let ((b (process-buffer proc))
199 (imagefile (get 'YaTeX-filter-filter-sentinel 'outfile))
200 ovl
201 (selw (selected-window)))
202 (save-excursion
203 (cond
204 ((eq (process-status proc) 'run))
205 ((eq (process-status proc) 'exit)
206 (set-buffer b)
207 (remove-images (point-min) (point-max))
208 (if (and (file-regular-p imagefile)
209 (file-readable-p imagefile))
210 (save-excursion
211 (setq buffer-read-only nil)
212 (cond
213 ((string-match "\\.\\(jpg\\|png\\)" imagefile)
214 (erase-buffer)
215 (YaTeX-popup-image imagefile b)
216 (YaTeX-filter-filter-set-conversion-flag))
217 (t ;Convert again to PNG file
218 (goto-char (point-max))
219 (insert "\nConvert Again to PNG file...\n")
220 (put 'YaTeX-filter-pngify-sentinel 'start (point))
221 (set-process-sentinel
222 (start-process
223 "Filter" b ;Safe to reuse
224 shell-file-name YaTeX-shell-command-option
225 (format YaTeX-filter-pdf2png-stdout imagefile))
226 'YaTeX-filter-pngify-sentinel)
227 (set-buffer-multibyte nil)
228 ))
229 (select-window selw)))
230 (YaTeX-preview-image-mode)
231 )
232 (t ;Other status might be an error
233 (set-buffer b)
234 (goto-char (point-max))
235 (insert (format "%s\n" (process-status proc))))))))
237 (defvar YaTeX-filter-block-marker "==="
238 "Begining and Ending marker for contents for external filter program")
239 (defvar YaTeX-filter-src "#SRC"
240 "Keyword for input filename for external filter program")
242 (defun YaTeX-filter-parse-filter-region (begend-info)
243 "Return the list of SpecialFilter region. If not on, return nil.
244 BEGEND-INFO is a value from the function YaTeX-in-BEGEND-p.
245 Return the alist of:
246 '((outfile $OutPutFileName)
247 (source $InputFileName) ; or nil for embeded data source
248 (cmdline $CommandLine)
249 (begin $TextRegionBeginning)
250 (end TextRegionEnd))"
251 (if begend-info
252 (let ((b (car begend-info)) (e (nth 1 begend-info))
253 delim (args (nth 2 begend-info))
254 (p (point)) openb closeb outfile source cmdline point-beg point-end
255 (src-ptn (format "^\\s *%s%s"
256 (regexp-quote comment-start)
257 (regexp-quote YaTeX-filter-src))))
258 (save-excursion
259 (and
260 (string-match "FILTER" args) ;easy test
261 (goto-char (car begend-info))
262 (re-search-forward
263 "FILTER\\s *{\\([^}]+\\)}" e t)
264 (setq outfile (YaTeX-match-string 1))
265 (goto-char (match-end 0))
266 (prog2 ;Step into the second brace
267 (skip-chars-forward "\t ")
268 (looking-at "{") ;Check if 2nd brace surely exists
269 (skip-chars-forward "{")
270 (skip-chars-forward "\t"))
271 (setq openb (point))
272 (condition-case nil
273 (progn (up-list 1) t)
274 (error nil))
275 (setq closeb (1- (point))
276 cmdline (YaTeX-buffer-substring openb closeb))
277 (cond
278 ((re-search-forward "^\\\\if0\\>" p t) ;; Embedded source
279 (forward-line 1)
280 (setq point-beg (if (looking-at YaTeX-filter-block-marker)
281 (progn (setq delim (YaTeX-match-string 0))
282 (forward-line 1)
283 (point))
284 (point)))
285 (re-search-forward "^\\\\fi\\>" e t)
286 (goto-char (match-beginning 0))
287 (setq point-end (if delim
288 (progn
289 (re-search-backward
290 (concat "^" (regexp-quote delim))
291 (1+ point-beg) t)
292 (match-beginning 0))
293 (point))))
294 ((re-search-forward
295 (format "%s{\\(.*\\)}" src-ptn) e t) ; external file
296 (setq source (YaTeX-match-string 1)
297 point-beg (match-beginning 0)
298 point-end (match-end 0)))
299 (t ;; If source notation not found,
300 (let ((ovl (overlays-in b e))) ;; clear all remaining overlays
301 (while ovl
302 (delete-overlay (car ovl))
303 (setq ovl (cdr ovl)))))) ;; Return nil
305 ;; Then return all values
306 (list (cons 'outfile outfile)
307 (cons 'source source)
308 (cons 'cmdline cmdline)
309 (cons 'begin point-beg)
310 (cons 'end point-end)))))))
312 ;;debug;; (YaTeX-filter-parse-filter-region (YaTeX-in-BEGEND-p))
313 (defun YaTeX-filter-pass-to-filter (begend-info)
314 "Pass current BEGIN FILTER environment to external command."
315 (put 'YaTeX-filter-filter-sentinel 'outfile nil)
316 ;; begend-info is from YaTeX-in-BEGEND-p: (BEG END ARGS)
317 (let ((b (car begend-info)) (e (nth 1 begend-info))
318 (r (YaTeX-filter-parse-filter-region begend-info))
319 insmark)
320 (save-excursion
321 (if r (let*((case-fold-search t)
322 (outfile (cdr (assq 'outfile r)))
323 (source (cdr (assq 'source r)))
324 (type (cond
325 ((string-match "\\.png$" outfile) "png")
326 ((string-match "\\.svg$" outfile) "svg")
327 ((string-match "\\.tex$" outfile) "tex")
328 (t "pdf")))
329 (newcmdline (YaTeX-replace-formats
330 (cdr (assq 'cmdline r))
331 (list (cons "t" type)
332 (cons "o" outfile)
333 (cons "i" source))))
334 (text-start (cdr (assq 'begin r)))
335 (text-end (cdr (assq 'end r)))
336 (text (and (numberp text-start)
337 (numberp text-end)
338 (YaTeX-buffer-substring text-start text-end)))
339 ;;
340 ;; Now it's time to start filter process
341 ;;
342 (procbuf (YaTeX-system newcmdline "Filter" 'force))
343 (proc (get-buffer-process procbuf))
344 ;;(procbuf (get-buffer-create " *Filter*"))
345 (ovl (progn
346 (remove-overlays text-start text-end)
347 (make-overlay text-start text-end)))
348 (ovlmodhook ;hook function to reset conv-success flag
349 'YaTeX-filter-filter-unset-conversion-flag))
350 (if proc
351 (progn
352 (overlay-put ovl 'filter-output outfile)
353 (overlay-put ovl 'filter-source source)
354 (overlay-put ovl 'converted nil)
355 (overlay-put ovl 'modification-hooks (list ovlmodhook))
356 (set-process-coding-system proc 'undecided 'utf-8)
357 (set-process-sentinel proc 'YaTeX-filter-filter-sentinel)
358 (YaTeX-showup-buffer procbuf)
359 (set-buffer procbuf)
360 (setq buffer-read-only nil)
361 (erase-buffer)
362 (insert (format "Starting process `%s'...\n" newcmdline))
363 (set-marker (process-mark proc) (point-max))
364 (setq insmark (point-max))
365 (cond
366 (text
367 (process-send-string
368 proc
369 (if source
370 (progn
371 (insert-file-contents-literally source)
372 (YaTeX-buffer-substring insmark (point-max)))
373 text))
374 (process-send-string proc "\n")
375 (process-send-eof proc) ;Notify stream chunk end
376 (process-send-eof proc))) ;Notify real EOF
377 (put 'YaTeX-filter-filter-sentinel 'outfile outfile)
378 (put 'YaTeX-filter-filter-sentinel 'overlay ovl))))))))
380 (defun YaTeX-insert-filter-special (filter list &optional region-p)
381 (let*((f (YaTeX-read-string-or-skip
382 "Output file(Maybe *.(pdf|png|jpg|tex)): "))
383 (insert-default-directory)
384 (cmdargs (car list))
385 (template-text (car (cdr list)))
386 (ifile (read-file-name "Data source(Default: in this buffer): " nil))
387 (in-line (string= "" ifile)))
388 (if region-p
389 (if (< (point) (mark)) (exchange-point-and-mark)))
390 (save-excursion
391 (insert (if in-line "===\n\\fi\n" "")
392 "%#END\n"
393 (cond
394 ((string-match "\\.tex$" f)
395 (format "\\input{%s}\n" (substring f 0 (match-beginning 0))))
396 ((string-match "\\.\\(pdf\\|png\\|jpe?g\\|tiff?\\)$" f)
397 (format "%%# \\includegraphics{%s}\n" f)))))
398 (and region-p (exchange-point-and-mark))
399 (insert (format "%%#BEGIN FILTER{%s}{%s}\n%s%s"
400 f (or cmdargs "")
401 (format "%%#%% If you call program in yatex, type `%se'\n"
402 (key-description
403 (car (where-is-internal 'YaTeX-typeset-menu))))
404 (if in-line "\\if0\n===\n" "")))
405 (save-excursion
406 (insert
407 (if in-line
408 (cond (template-text
409 (concat template-text
410 (or (string-match "\n$" template-text) "\n")))
411 (t "\n"))
412 (format "%%#SRC{%s}\n" ifile))))))
414 (provide 'yatexflt)
416 ; Local variables:
417 ; fill-prefix: ";;; "
418 ; paragraph-start: "^$\\| \\|;;;$"
419 ; paragraph-separate: "^$\\| \\|;;;$"
420 ; End: