yatex

view yatexflt.el @ 544:ab6c176c676a

Move all dot-filter definitions to yatexflt.el
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 02 Jun 2018 10:39:58 +0900
parents b1896ef49747
children ea6956f10ce7
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 Fri Jun 1 08:28:40 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 }
84 bigraph {
85 graph [charset=\"utf-8\"]}"
86 )))
88 ;;;###autoload
89 (defun YaTeX-filter-goto-source (file other-win)
90 "Go to corresponding text source of the graphic file"
91 (cond
92 ((file-exists-p file)
93 (let ((buf (find-file-noselect file)))
94 (funcall (cond (other-win 'YaTeX-switch-to-buffer-other-window)
95 ((get-buffer-window buf) 'goto-buffer-window)
96 (t 'YaTeX-switch-to-buffer))
97 buf)))))
99 (defvar YaTeX-filter-special-env-alist-private nil)
100 (defvar YaTeX-filter-special-env-alist
101 (append YaTeX-filter-special-env-alist-private
102 YaTeX-filter-special-env-alist-default))
104 (defun YaTeX-filter-filter-set-conversion-flag ()
105 (let ((ovl (get 'YaTeX-filter-filter-sentinel 'overlay)))
106 (if ovl ;; When successful conversion met,
107 (progn ;; (1)Set conversion complete flag
108 (add-hook ;; (2)Add hook of seim-automatic
109 'write-file-hooks ;; update of convert to write-
110 'YaTeX-filter-update-all) ;; file hook.
111 (overlay-put ovl 'converted t)))))
113 (defun YaTeX-filter-filter-unset-conversion-flag
114 (ovl after beg end &optional length)
115 (if after (overlay-put ovl 'converted nil)))
118 (defun YaTeX-filter-pngify-sentinel (proc msg)
119 (save-excursion
120 (let ((b (process-buffer proc)) (selw (selected-window))
121 img)
122 (set-buffer b)
123 (cond
124 ((eq (process-status proc) 'run)
125 (put-text-property (point-min) (point-max) 'invisible t))
126 ((eq (process-status proc) 'exit)
127 (set-buffer b)
128 (YaTeX-popup-image
129 (YaTeX-buffer-substring
130 (get 'YaTeX-filter-pngify-sentinel 'start) (point-max))
131 b)
132 (YaTeX-filter-filter-set-conversion-flag))
133 (t
134 (set-buffer b)
135 (remove-text-properties (point-min) (point-max) '(invisible t))
136 (insert "\nProcess aborted %s\n" msg))))))
138 (defvar YaTeX-filter-pdf2png-stdout
139 (cond
140 ((YaTeX-executable-find "convert") "convert -trim %s PNG:-")
141 (t
142 "gs -dNOPAUSE -sDEVICE=png256 -sOutputFile=- -dBATCH -q -r75 %s"))
143 "Command line syntax to convert PDF file to PNG stream")
145 (defun YaTeX-filter-modified-BEGEND-regions ()
146 "Return the list of overlays which contains un-converted text."
147 (save-excursion
148 (save-restriction
149 (widen)
150 (let (r prop dest src pl (list (overlays-in (point-min) (point-max))))
151 (while list
152 (setq prop (overlay-properties (car list)))
153 (if (setq dest (plist-get prop 'filter-output))
154 (if (if (setq src (plist-get prop 'filter-source))
155 (file-newer-than-file-p src dest)
156 (and (setq pl (plist-member prop 'converted))
157 (not (plist-get pl 'converted))))
158 (setq r (cons (car list) r))))
159 (setq list (cdr list)))
160 (nconc r)
161 r))))
163 (defun YaTeX-filter-update-all ()
164 "Update all destination files from built-in source text."
165 (interactive)
166 (let ((timeout 4)
167 ans ovl (update-list (YaTeX-filter-modified-BEGEND-regions)))
168 (if update-list
169 (save-excursion
170 (save-window-excursion
171 (catch 'abort
172 (while update-list
173 (goto-char (overlay-start (setq ovl (car update-list))))
174 (or (pos-visible-in-window-p)
175 (set-window-start nil (point)))
176 (unwind-protect
177 (progn
178 (overlay-put ovl 'face 'YaTeX-on-the-fly-activated-face)
179 (message "Non-update source found: Update here: %s "
180 "Y)es N)o S)top-watching-Here A)bort")
181 (setq ans (read-char))
182 (cond
183 ((memq ans '(?Y ?y))
184 (YaTeX-filter-BEGEND)
185 (while (and (> (setq timeout (1- timeout)))
186 (eq (process-status "Filter") 'run))
187 (message "Waiting for conversion process to finish")
188 (sit-for 1)))
189 ((memq ans '(?A ?a)) (throw 'abort t))
190 ((memq ans '(?S ?s)) (delete-overlay ovl))
191 (t nil)))
192 (overlay-put ovl 'face nil))
193 (setq update-list (cdr update-list)))))))
194 ;; Write file hook should return nil
195 nil))
197 (defun YaTeX-filter-filter-sentinel (proc msg)
198 (put 'YaTeX-filter-pngify-sentinel 'start nil)
199 (let ((b (process-buffer proc))
200 (imagefile (get 'YaTeX-filter-filter-sentinel 'outfile))
201 ovl
202 (selw (selected-window)))
203 (save-excursion
204 (cond
205 ((eq (process-status proc) 'run))
206 ((eq (process-status proc) 'exit)
207 (set-buffer b)
208 (remove-images (point-min) (point-max))
209 (if (and (file-regular-p imagefile)
210 (file-readable-p imagefile))
211 (save-excursion
212 (setq buffer-read-only nil)
213 (cond
214 ((string-match "\\.\\(jpg\\|png\\)" imagefile)
215 (erase-buffer)
216 (YaTeX-popup-image imagefile b)
217 (YaTeX-filter-filter-set-conversion-flag))
218 (t ;Convert again to PNG file
219 (goto-char (point-max))
220 (insert "\nConvert Again to PNG file...\n")
221 (put 'YaTeX-filter-pngify-sentinel 'start (point))
222 (set-process-sentinel
223 (start-process
224 "Filter" b ;Safe to reuse
225 shell-file-name YaTeX-shell-command-option
226 (format YaTeX-filter-pdf2png-stdout imagefile))
227 'YaTeX-filter-pngify-sentinel)
228 (set-buffer-multibyte nil)
229 ))
230 (select-window selw)))
231 (YaTeX-preview-image-mode)
232 )
233 (t ;Other status might be an error
234 (set-buffer b)
235 (goto-char (point-max))
236 (insert (format "%s\n" (process-status proc))))))))
238 (defun YaTeX-filter-parse-filter-region (begend-info)
239 "Return the list of SpecialFilter region. If not on, return nil.
240 BEGEND-INFO is a value from the function YaTeX-in-BEGEND-p.
241 Return the alist of:
242 '((outfile $OutPutFileName)
243 (source $InputFileName) ; or nil for embeded data source
244 (cmdline $CommandLine)
245 (begin $TextRegionBeginning)
246 (end TextRegionEnd))"
247 (if begend-info
248 (let ((b (car begend-info)) (e (nth 1 begend-info))
249 delim (args (nth 2 begend-info))
250 (p (point)) openb closeb outfile source cmdline point-beg point-end)
251 (save-excursion
252 (and
253 (string-match "FILTER" args) ;easy test
254 (goto-char (car begend-info))
255 (re-search-forward
256 "FILTER\\s *{\\([^}]+\\)}" e t)
257 (setq outfile (YaTeX-match-string 1))
258 (goto-char (match-end 0))
259 (prog2 ;Step into the second brace
260 (skip-chars-forward "\t ")
261 (looking-at "{") ;Check if 2nd brace surely exists
262 (skip-chars-forward "{")
263 (skip-chars-forward "\t"))
264 (setq openb (point))
265 (condition-case nil
266 (progn (up-list 1) t)
267 (error nil))
268 (setq closeb (1- (point))
269 cmdline (YaTeX-buffer-substring openb closeb))
270 (cond
271 ((re-search-forward "^\\\\if0\\>" p t) ;; Embedded source
272 (forward-line 1)
273 (setq point-beg (if (looking-at "\\(.\\)\\1\\1") ;Triple chars
274 (progn (setq delim (YaTeX-match-string 0))
275 (forward-line 1)
276 (point))
277 (point)))
278 (re-search-forward "^\\\\fi\\>" e t)
279 (goto-char (match-beginning 0))
280 (setq point-end (if delim
281 (progn
282 (re-search-backward
283 (concat "^" (regexp-quote delim))
284 (1+ point-beg) t)
285 (match-beginning 0))
286 (point))))
287 ((re-search-forward "^\\s *%#SRC{\\(.*\\)}" e t) ; external file
288 (setq source (YaTeX-match-string 1)
289 point-beg (match-beginning 0)
290 point-end (match-end 0)))
291 (t ;; If source notation not found,
292 (let ((ovl (overlays-in b e))) ;; clear all remaining overlays
293 (while ovl
294 (delete-overlay (car ovl))
295 (setq ovl (cdr ovl)))))) ;; Return nil
297 ;; Then return all values
298 (list (cons 'outfile outfile)
299 (cons 'source source)
300 (cons 'cmdline cmdline)
301 (cons 'begin point-beg)
302 (cons 'end point-end)))))))
304 ;;debug;; (YaTeX-filter-parse-filter-region (YaTeX-in-BEGEND-p))
305 (defun YaTeX-filter-pass-to-filter (begend-info)
306 "Pass current BEGIN FILTER environment to external command."
307 (put 'YaTeX-filter-filter-sentinel 'outfile nil)
308 ;; begend-info is from YaTeX-in-BEGEND-p: (BEG END ARGS)
309 (let ((b (car begend-info)) (e (nth 1 begend-info))
310 (r (YaTeX-filter-parse-filter-region begend-info)))
311 (save-excursion
312 (if r (let*((case-fold-search t)
313 (outfile (cdr (assq 'outfile r)))
314 (source (cdr (assq 'source r)))
315 (type (cond
316 ((string-match "\\.png$" outfile) "png")
317 ((string-match "\\.svg$" outfile) "svg")
318 ((string-match "\\.tex$" outfile) "tex")
319 (t "pdf")))
320 (newcmdline (YaTeX-replace-formats
321 (cdr (assq 'cmdline r))
322 (list (cons "t" type)
323 (cons "o" outfile)
324 (cons "i" source))))
325 (text-start (cdr (assq 'begin r)))
326 (text-end (cdr (assq 'end r)))
327 (text (and (numberp text-start)
328 (numberp text-end)
329 (YaTeX-buffer-substring text-start text-end)))
330 ;;
331 ;; Now it's time to start filter process
332 ;;
333 (procbuf (YaTeX-system newcmdline "Filter" 'force))
334 (proc (get-buffer-process procbuf))
335 ;;(procbuf (get-buffer-create " *Filter*"))
336 (ovl (progn
337 (remove-overlays text-start text-end)
338 (make-overlay text-start text-end)))
339 (ovlmodhook ;hook function to reset conv-success flag
340 'YaTeX-filter-filter-unset-conversion-flag))
341 (if proc
342 (progn
343 (overlay-put ovl 'filter-output outfile)
344 (overlay-put ovl 'filter-source source)
345 (overlay-put ovl 'converted nil)
346 (overlay-put ovl 'modification-hooks (list ovlmodhook))
347 (set-process-coding-system proc 'undecided 'utf-8)
348 (set-process-sentinel proc 'YaTeX-filter-filter-sentinel)
349 (YaTeX-showup-buffer procbuf)
350 (set-buffer procbuf)
351 (setq buffer-read-only nil)
352 (erase-buffer)
353 (insert (format "Starting process `%s'...\n" newcmdline))
354 (set-marker (process-mark proc) (point-max))
355 (cond
356 (text
357 (process-send-string proc text)
358 (process-send-string proc "\n")
359 (process-send-eof proc) ;Notify stream chunk end
360 (process-send-eof proc))) ;Notify real EOF
361 (put 'YaTeX-filter-filter-sentinel 'outfile outfile)
362 (put 'YaTeX-filter-filter-sentinel 'overlay ovl))))))))
364 (defun YaTeX-insert-filter-special (filter list &optional region-p)
365 (let*((f (YaTeX-read-string-or-skip
366 "Output file(Maybe *.(pdf|png|jpg|tex)): "))
367 (insert-default-directory)
368 (cmdargs (car list))
369 (template-text (car (cdr list)))
370 (ifile (read-file-name "Data source(Default: in this buffer): " nil))
371 (in-line (string= "" ifile)))
372 (if region-p
373 (if (< (point) (mark)) (exchange-point-and-mark)))
374 (save-excursion
375 (insert (if in-line "===\n\\fi\n" "")
376 "%#END\n"
377 (cond
378 ((string-match "\\.tex$" f)
379 (format "\\input{%s}\n" (substring f 0 (match-beginning 0))))
380 ((string-match "\\.\\(pdf\\|png\\|jpe?g\\|tiff?\\)$" f)
381 (format "%%# \\includegraphics{%s}\n" f)))))
382 (and region-p (exchange-point-and-mark))
383 (insert (format "%%#BEGIN FILTER{%s}{%s}\n%s"
384 f (or cmdargs "")
385 (if in-line "\\if0\n===\n" "")))
386 (save-excursion
387 (insert (if in-line
388 (cond (template-text
389 (concat template-text
390 (or (string-match "\n$" template-text) "\n")))
391 (t "\n"))
392 (format "%%#SRC{%s}\n" ifile))))))
394 (provide 'yatexflt)
396 ; Local variables:
397 ; fill-prefix: ";;; "
398 ; paragraph-start: "^$\\| \\|;;;$"
399 ; paragraph-separate: "^$\\| \\|;;;$"
400 ; End: