summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/rescansync/rescansync.sty
blob: 197a7cad8994de071b3fea7fee49a48574623464 (plain)
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
% File: rescansync.sty
% Copyright 2022 user202729
%
% This work  may be  distributed and/or  modified under  the conditions  of the
% LaTeX Project Public License (LPPL),  either version 1.3c  of this license or
% (at your option) any later version.  The latest version of this license is in
% the file:
%
%   http://www.latex-project.org/lppl.txt
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is user202729.

\ProvidesExplPackage{rescansync}{2022/07/09}{0.0.0}{Re-scan tokens with synctex information}

\cs_generate_variant:Nn \regex_replace_all:nnN {nxN}
\cs_generate_variant:Nn \tl_replace_all:Nnn {Nxn}

\msg_new:nnn {rescansync} {currfile-package-not-loaded}
	{currfile~package~is~needed~for~this~functionality}

\msg_new:nnn {rescansync} {saveenv-package-not-loaded}
	{saveenv~package~is~needed~for~this~functionality}

\makeatletter

\sys_if_engine_luatex:TF {
	\let \_RS_optionaldirectlua \directlua
	\cs_new:Npn \rescansync_gettag: {
		\directlua{
			tex.sprint(tex.get_synctex_tag())
		}
	}
}
{
	\let \_RS_optionaldirectlua \@gobble
	\let \rescansync_gettag: \empty
}



%\let \rescansyncTMPfixsync \undefined

\tl_gset:Nn \_RS_count {0}
\edef \_RS_j_tl {\char_generate:nn {`\^^J} {12}}

\iow_new:N \_RS_file

% #1: the content (will be detokenized)
% #2: the number (line offset)
% #3: the file name
% #4: the synctex tag (get from \rescansync_gettag:)
%
% content is an expl3 string with ^^J represent line break -- same format as expected by iow_now
%
% NOTE: because filecontentsdef make character >= 128 in pdftex active character and
% they cannot be \write safely, we detokenize it then replace ^^M later.
\cs_new_protected:Npn \rescansync:nnnn #1 #2 #3 #4 {
	\tl_gset:Nx \_RS_count {\int_eval:n {\_RS_count+1}}
	\tl_set:Nx \_RS_filename {RS\_RS_count-#3}

	\begingroup  % will be closed by the active `~` later
		\iow_open:Nn \_RS_file {\_RS_filename}

		\iow_now:Nx \_RS_file {
			% print some initial padding newlines first
			\prg_replicate:nV {#2} \_RS_j_tl

			% prepend some content to the start of the line...
			\c_backslash_str a \c_backslash_str _RS_optionaldirectlua{tex.set_synctex_tag( #4 )} \c_tilde_str   % this one works
			% note that the directlua must be directly inside the file, not indirectly invoked through some macro

			% print the content
			\detokenize{#1}
		}

		\iow_close:N \_RS_file

		\int_step_inline:nnn {33} {125} {\catcode ##1=11~}  % set all to letter
		\catcode `\~ \active
		\catcode `\\ 0
		\catcode `\{ 1
		\catcode `\} 2~

		% just so that the content '\_RS_optionaldirectlua{tex.set_synctex_tag( ... )}~' is interpreted correctly...


		\char_set_active_eq:NN \~ \endgroup
		\exp_args:NNNo \expandafter \_RS_skip_initial_endlinechars \@@input {\_RS_filename}
}

\cs_new_protected:Npn \rescansync:nnn #1 #2 #3 {
	\rescansync:nnnn {#1} {#2} {#3} {\rescansync_gettag:}  % #4 will be expanded inside
}

\cs_new_protected:Npn \_RS_skip_initial_endlinechars #1 \a {}  % #1: tokens generated by endlinechar

\NewDocumentEnvironment {rescansyncSaveenvPacked} {m} {
	\edef \_RS_lineno {\the\inputlineno}
	\cs_if_free:NT \saveenv {
		\msg_error:nn {rescansync} {saveenv-package-not-loaded}
	}
	\cs_if_free:NT \currfilename {
		\msg_error:nn {rescansync} {currfile-package-not-loaded}
	}
	\saveenv \_RS_body
} {
	\endsaveenv
	\global\edef #1 { {\_RS_body} {\_RS_lineno} {\currfilename} }
	%\pretty:V #1
}

\NewDocumentEnvironment {rescansyncSaveenvghostPacked} {m} {
	\edef \_RS_lineno {\the\inputlineno}
	\cs_if_free:NT \saveenv {
		\msg_error:nn {rescansync} {saveenv-package-not-loaded}
	}
	\cs_if_free:NT \currfilename {
		\msg_error:nn {rescansync} {currfile-package-not-loaded}
	}
	\saveenvghost \_RS_body
} {
	\endsaveenvghost
	\global\edef #1 { {\_RS_body} {\_RS_lineno} {\currfilename} }
	%\pretty:V #1
}

\NewDocumentCommand \rescansyncPacked {m} {
	%\pretty:V {#1}
	\exp_last_unbraced:Nx \rescansync:nnn {#1}
}



\cs_generate_variant:Nn \iow_now:Nn {No}
\cs_generate_variant:Nn \prg_replicate:nn {nV}

% #1: the macro that contain the content, #2: the number (line offset)
\cs_new_protected:Npn \rescansync:nn #1 #2 {
	\cs_if_free:NT \currfilename {
		\msg_error:nn {rescansync} {currfile-package-not-loaded}
	}
	\rescansync:nnn {#1} {#2} {\currfilename}
}