yatex
changeset 524:b1896ef49747 dev
Detection of region passed to filter modified.
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Tue, 23 Jan 2018 10:44:10 +0900 |
parents | 5bb46b7ab3de |
children | e0222b0ab207 |
files | yatexflt.el yatexlib.el |
diffstat | 2 files changed, 158 insertions(+), 93 deletions(-) [+] |
line diff
1.1 --- a/yatexflt.el Fri Jan 19 22:19:54 2018 +0900 1.2 +++ b/yatexflt.el Tue Jan 23 10:44:10 2018 +0900 1.3 @@ -1,7 +1,7 @@ 1.4 ;;; yatexflt.el --- YaTeX filter command utilizer -*- coding: sjis -*- 1.5 ;;; 1.6 ;;; (c)1993-2018 by HIROSE Yuuji.[yuuji@yatex.org] 1.7 -;;; Last modified Tue Jan 9 13:28:56 2018 on firestorm 1.8 +;;; Last modified Sun Jan 21 16:33:16 2018 on firestorm 1.9 ;;; $Id$ 1.10 1.11 ;;; Commentary: 1.12 @@ -136,13 +136,15 @@ 1.13 (save-excursion 1.14 (save-restriction 1.15 (widen) 1.16 - (let (r pl (list (overlays-in (point-min) (point-max)))) 1.17 + (let (r prop dest src pl (list (overlays-in (point-min) (point-max)))) 1.18 (while list 1.19 - (if (and (overlay-get (car list) 'filter-input) 1.20 - (setq pl (plist-member (overlay-properties (car list)) 1.21 - 'converted)) 1.22 - (not (plist-get pl 'converted))) 1.23 - (setq r (cons (car list) r))) 1.24 + (setq prop (overlay-properties (car list))) 1.25 + (if (setq dest (plist-get prop 'filter-output)) 1.26 + (if (if (setq src (plist-get prop 'filter-source)) 1.27 + (file-newer-than-file-p src dest) 1.28 + (and (setq pl (plist-member prop 'converted)) 1.29 + (not (plist-get pl 'converted)))) 1.30 + (setq r (cons (car list) r)))) 1.31 (setq list (cdr list))) 1.32 (nconc r) 1.33 r)))) 1.34 @@ -164,7 +166,7 @@ 1.35 (progn 1.36 (overlay-put ovl 'face 'YaTeX-on-the-fly-activated-face) 1.37 (message "Non-update source found: Update here: %s " 1.38 - "Y)es N)o A)bort") 1.39 + "Y)es N)o S)top-watching-Here A)bort") 1.40 (setq ans (read-char)) 1.41 (cond 1.42 ((memq ans '(?Y ?y)) 1.43 @@ -174,6 +176,7 @@ 1.44 (message "Waiting for conversion process to finish") 1.45 (sit-for 1))) 1.46 ((memq ans '(?A ?a)) (throw 'abort t)) 1.47 + ((memq ans '(?S ?s)) (delete-overlay ovl)) 1.48 (t nil))) 1.49 (overlay-put ovl 'face nil)) 1.50 (setq update-list (cdr update-list))))))) 1.51 @@ -221,99 +224,161 @@ 1.52 (goto-char (point-max)) 1.53 (insert (format "%s\n" (process-status proc)))))))) 1.54 1.55 +(defun YaTeX-filter-parse-filter-region (begend-info) 1.56 + "Return the list of SpecialFilter region. If not on, return nil. 1.57 +BEGEND-INFO is a value from the function YaTeX-in-BEGEND-p. 1.58 +Return the alist of: 1.59 +'((outfile $OutPutFileName) 1.60 + (source $InputFileName) ; or nil for embeded data source 1.61 + (cmdline $CommandLine) 1.62 + (begin $TextRegionBeginning) 1.63 + (end TextRegionEnd))" 1.64 + (if begend-info 1.65 + (let ((b (car begend-info)) (e (nth 1 begend-info)) 1.66 + delim (args (nth 2 begend-info)) 1.67 + (p (point)) openb closeb outfile source cmdline point-beg point-end) 1.68 + (save-excursion 1.69 + (and 1.70 + (string-match "FILTER" args) ;easy test 1.71 + (goto-char (car begend-info)) 1.72 + (re-search-forward 1.73 + "FILTER\\s *{\\([^}]+\\)}" e t) 1.74 + (setq outfile (YaTeX-match-string 1)) 1.75 + (goto-char (match-end 0)) 1.76 + (prog2 ;Step into the second brace 1.77 + (skip-chars-forward "\t ") 1.78 + (looking-at "{") ;Check if 2nd brace surely exists 1.79 + (skip-chars-forward "{") 1.80 + (skip-chars-forward "\t")) 1.81 + (setq openb (point)) 1.82 + (condition-case nil 1.83 + (progn (up-list 1) t) 1.84 + (error nil)) 1.85 + (setq closeb (1- (point)) 1.86 + cmdline (YaTeX-buffer-substring openb closeb)) 1.87 + (cond 1.88 + ((re-search-forward "^\\\\if0\\>" p t) ;; Embedded source 1.89 + (forward-line 1) 1.90 + (setq point-beg (if (looking-at "\\(.\\)\\1\\1") ;Triple chars 1.91 + (progn (setq delim (YaTeX-match-string 0)) 1.92 + (forward-line 1) 1.93 + (point)) 1.94 + (point))) 1.95 + (re-search-forward "^\\\\fi\\>" e t) 1.96 + (goto-char (match-beginning 0)) 1.97 + (setq point-end (if delim 1.98 + (progn 1.99 + (re-search-backward 1.100 + (concat "^" (regexp-quote delim)) 1.101 + (1+ point-beg) t) 1.102 + (match-beginning 0)) 1.103 + (point)))) 1.104 + ((re-search-forward "^\\s *%#SRC{\\(.*\\)}" e t) ; external file 1.105 + (setq source (YaTeX-match-string 1) 1.106 + point-beg (match-beginning 0) 1.107 + point-end (match-end 0))) 1.108 + (t ;; If source notation not found, 1.109 + (let ((ovl (overlays-in b e))) ;; clear all remaining overlays 1.110 + (while ovl 1.111 + (delete-overlay (car ovl)) 1.112 + (setq ovl (cdr ovl)))))) ;; Return nil 1.113 + 1.114 + ;; Then return all values 1.115 + (list (cons 'outfile outfile) 1.116 + (cons 'source source) 1.117 + (cons 'cmdline cmdline) 1.118 + (cons 'begin point-beg) 1.119 + (cons 'end point-end))))))) 1.120 + 1.121 +;;debug;; (YaTeX-filter-parse-filter-region (YaTeX-in-BEGEND-p)) 1.122 (defun YaTeX-filter-pass-to-filter (begend-info) 1.123 "Pass current BEGIN FILTER environment to external command." 1.124 (put 'YaTeX-filter-filter-sentinel 'outfile nil) 1.125 ;; begend-info is from YaTeX-in-BEGEND-p: (BEG END ARGS) 1.126 (let ((b (car begend-info)) (e (nth 1 begend-info)) 1.127 - (args (nth 2 begend-info)) 1.128 - (p (point)) openb closeb outfile cmdline point-if0 point-fi) 1.129 + (r (YaTeX-filter-parse-filter-region begend-info))) 1.130 (save-excursion 1.131 - (if (and begend-info 1.132 - (string-match "FILTER" args) ;easy test 1.133 - (goto-char (car begend-info)) 1.134 - (re-search-forward 1.135 - "FILTER\\s *{\\([^}]+\\)}" e t) 1.136 - (setq outfile (YaTeX-match-string 1)) 1.137 - (goto-char (match-end 0)) 1.138 - (prog2 ;Step into the second brace 1.139 - (skip-chars-forward "\t ") 1.140 - (looking-at "{") ;Check if 2nd brace surely exists 1.141 - (skip-chars-forward "{") 1.142 - (skip-chars-forward "\t")) 1.143 - (setq openb (point)) 1.144 - (condition-case nil 1.145 - (progn (up-list 1) t) 1.146 - (error nil)) 1.147 - (setq closeb (1- (point)) 1.148 - cmdline (YaTeX-buffer-substring openb closeb)) 1.149 - (re-search-forward "^\\\\if0\\>" p t) 1.150 - (setq point-if0 (point)) 1.151 - (re-search-forward "^\\\\fi\\>" e t) 1.152 - (setq point-fi (match-beginning 0))) 1.153 - (let*((case-fold-search t) 1.154 - (type (cond 1.155 - ((string-match "\\.png$" outfile) "png") 1.156 - ((string-match "\\.svg$" outfile) "svg") 1.157 - (t "pdf"))) 1.158 - (newcmdline (YaTeX-replace-formats 1.159 - cmdline 1.160 - (list (cons "t" type) 1.161 - (cons "o" outfile)))) 1.162 - (delim (progn (goto-char point-if0) 1.163 - (forward-line 1) 1.164 - (and (looking-at "\\(.\\)\\1\\1") ;Triple chars 1.165 - (prog1 1.166 - (YaTeX-match-string 0) 1.167 - (forward-line 1))))) 1.168 - (text-start (point)) 1.169 - (text-end (if (and delim 1.170 - (re-search-forward 1.171 - (concat "^" (regexp-quote delim)) 1.172 - point-fi t)) 1.173 - (match-beginning 0) 1.174 - point-fi)) 1.175 - (text (YaTeX-buffer-substring text-start text-end)) 1.176 - ;; 1.177 - ;; Now it's time to start filter process 1.178 - ;; 1.179 - (procbuf (YaTeX-system newcmdline "Filter" 'force)) 1.180 - (proc (get-buffer-process procbuf)) 1.181 - ;(procbuf (get-buffer-create " *Filter*")) 1.182 - (ovl (progn 1.183 - (remove-overlays text-start text-end) 1.184 - (make-overlay text-start text-end))) 1.185 - (ovlmodhook ;hook function to reset conv-success flag 1.186 - 'YaTeX-filter-filter-unset-conversion-flag)) 1.187 - (if proc 1.188 - (progn 1.189 - (overlay-put ovl 'filter-input outfile) 1.190 - (overlay-put ovl 'converted nil) 1.191 - (overlay-put ovl 'modification-hooks (list ovlmodhook)) 1.192 - (set-process-coding-system proc 'undecided 'utf-8) 1.193 - (set-process-sentinel proc 'YaTeX-filter-filter-sentinel) 1.194 - (YaTeX-showup-buffer procbuf) 1.195 - (set-buffer procbuf) 1.196 - (setq buffer-read-only nil) 1.197 - (erase-buffer) 1.198 - (insert (format "Starting process `%s'...\n" newcmdline)) 1.199 - (set-marker (process-mark proc) (point-max)) 1.200 - (process-send-string proc text) 1.201 - (process-send-string proc "\n") 1.202 - (process-send-eof proc) ;Notify stream chunk end 1.203 - (process-send-eof proc) ;Notify real EOF 1.204 - (put 'YaTeX-filter-filter-sentinel 'outfile outfile) 1.205 - (put 'YaTeX-filter-filter-sentinel 'overlay ovl)))))))) 1.206 + (if r (let*((case-fold-search t) 1.207 + (outfile (cdr (assq 'outfile r))) 1.208 + (source (cdr (assq 'source r))) 1.209 + (type (cond 1.210 + ((string-match "\\.png$" outfile) "png") 1.211 + ((string-match "\\.svg$" outfile) "svg") 1.212 + ((string-match "\\.tex$" outfile) "tex") 1.213 + (t "pdf"))) 1.214 + (newcmdline (YaTeX-replace-formats 1.215 + (cdr (assq 'cmdline r)) 1.216 + (list (cons "t" type) 1.217 + (cons "o" outfile) 1.218 + (cons "i" source)))) 1.219 + (text-start (cdr (assq 'begin r))) 1.220 + (text-end (cdr (assq 'end r))) 1.221 + (text (and (numberp text-start) 1.222 + (numberp text-end) 1.223 + (YaTeX-buffer-substring text-start text-end))) 1.224 + ;; 1.225 + ;; Now it's time to start filter process 1.226 + ;; 1.227 + (procbuf (YaTeX-system newcmdline "Filter" 'force)) 1.228 + (proc (get-buffer-process procbuf)) 1.229 + ;;(procbuf (get-buffer-create " *Filter*")) 1.230 + (ovl (progn 1.231 + (remove-overlays text-start text-end) 1.232 + (make-overlay text-start text-end))) 1.233 + (ovlmodhook ;hook function to reset conv-success flag 1.234 + 'YaTeX-filter-filter-unset-conversion-flag)) 1.235 + (if proc 1.236 + (progn 1.237 + (overlay-put ovl 'filter-output outfile) 1.238 + (overlay-put ovl 'filter-source source) 1.239 + (overlay-put ovl 'converted nil) 1.240 + (overlay-put ovl 'modification-hooks (list ovlmodhook)) 1.241 + (set-process-coding-system proc 'undecided 'utf-8) 1.242 + (set-process-sentinel proc 'YaTeX-filter-filter-sentinel) 1.243 + (YaTeX-showup-buffer procbuf) 1.244 + (set-buffer procbuf) 1.245 + (setq buffer-read-only nil) 1.246 + (erase-buffer) 1.247 + (insert (format "Starting process `%s'...\n" newcmdline)) 1.248 + (set-marker (process-mark proc) (point-max)) 1.249 + (cond 1.250 + (text 1.251 + (process-send-string proc text) 1.252 + (process-send-string proc "\n") 1.253 + (process-send-eof proc) ;Notify stream chunk end 1.254 + (process-send-eof proc))) ;Notify real EOF 1.255 + (put 'YaTeX-filter-filter-sentinel 'outfile outfile) 1.256 + (put 'YaTeX-filter-filter-sentinel 'overlay ovl)))))))) 1.257 1.258 (defun YaTeX-insert-filter-special (filter list &optional region-p) 1.259 - (let ((f (YaTeX-read-string-or-skip "Output file: "))) 1.260 + (let*((f (YaTeX-read-string-or-skip 1.261 + "Output file(Maybe *.(pdf|png|jpg|tex)): ")) 1.262 + (insert-default-directory) 1.263 + (cmdargs (car list)) 1.264 + (template-text (car (cdr list))) 1.265 + (ifile (read-file-name "Data source(Default: in this buffer): " nil)) 1.266 + (in-line (string= "" ifile))) 1.267 (if region-p 1.268 (if (< (point) (mark)) (exchange-point-and-mark))) 1.269 - (save-excursion (insert "===\n\\fi\n%#END\n")) 1.270 + (save-excursion 1.271 + (insert (if in-line "===\n\\fi\n" "") 1.272 + "%#END\n" 1.273 + (cond 1.274 + ((string-match "\\.tex$" f) 1.275 + (format "\\input{%s}\n" (substring f 0 (match-beginning 0)))) 1.276 + ((string-match "\\.\\(pdf\\|png\\|jpe?g\\|tiff?\\)$" f) 1.277 + (format "%%# \\includegraphics{%s}\n" f))))) 1.278 (and region-p (exchange-point-and-mark)) 1.279 - (insert (format "%%#BEGIN FILTER{%s}{%s}\n\\if0\n===\n" 1.280 - f (or (car list) ""))) 1.281 - (save-excursion (insert (or (car (cdr list)) "\n") "\n")))) 1.282 + (insert (format "%%#BEGIN FILTER{%s}{%s}\n%s" 1.283 + f (or cmdargs "") 1.284 + (if in-line "\\if0\n===\n" ""))) 1.285 + (save-excursion 1.286 + (insert (if in-line 1.287 + (cond (template-text 1.288 + (concat template-text 1.289 + (or (string-match "\n$" template-text) "\n"))) 1.290 + (t "\n")) 1.291 + (format "%%#SRC{%s}\n" ifile)))))) 1.292 1.293 (provide 'yatexflt) 1.294
2.1 --- a/yatexlib.el Fri Jan 19 22:19:54 2018 +0900 2.2 +++ b/yatexlib.el Tue Jan 23 10:44:10 2018 +0900 2.3 @@ -1,7 +1,7 @@ 2.4 ;;; yatexlib.el --- YaTeX and yahtml common libraries -*- coding: sjis -*- 2.5 ;;; 2.6 ;;; (c)1994-2018 by HIROSE Yuuji.[yuuji@yatex.org] 2.7 -;;; Last modified Thu Jan 4 13:58:20 2018 on firestorm 2.8 +;;; Last modified Sun Jan 21 16:29:01 2018 on firestorm 2.9 ;;; $Id$ 2.10 2.11 ;;; Code: 2.12 @@ -1224,8 +1224,8 @@ 2.13 ;;(if (eobp) nil (forward-char 1)) ;OUT 2015/1/5 2.14 )))) 2.15 2.16 -(defun YaTeX-in-BEGEND-p () 2.17 - "Check if the point is in a %#BEGIN...%#END region. 2.18 +(defun YaTeX-in-BEGEND-p (&optional pt) 2.19 + "Check if the point (or PT) is in a %#BEGIN...%#END region. 2.20 Return the list of beginning and ending point of the region and arg-string 2.21 if the point is in BEGEND. Otherwise nil." 2.22 (let ((b "%#BEGIN") bp args (e "%#END") (p (point)))