yatex

view yatexflt.el @ 624:0018833906a1

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