summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkii/m-pdfsnc.mkii
blob: 12e2e2d68bae5bb6064e5026b0e9310ab14ec124 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
%D \module
%D   [       file=m-pdfsnc,
%D        version=2003.12.23,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Editor Synchronization,
%D         author={Hans Hagen \& Ton Otten},
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% \enablemode[demo]

% This file is a variation of the file pdfsync4context.tex that ships
% with the pdfsync package (used by iTeXMac and TeXShop) by Piero
% D'Ancona and Jérôme Laurens. I made the macros a bit more efficient
% and added some basic tracing options as well as a few more options.
% I made this one while playing a bit with the Mac and TeX). Maybe I'll
% add a bit of support to the kernel in in order to get rid of redundant
% markers. Also, this had better be rewritten in a bit less \pdf
% dependent way so that it can also be supported by dvipdfmx. I could
% write a generic file as well, but since there is already support for
% other packages I'll not do that (now).

% compatible commands:
%
%  \pdfsyncstart \pdfsyncstop \pdfsync
%
% extra commands:
%
%  \enablepdfsync \disablepdfsync \pdfsynctext \pdfsyncnode \pdfsynctracemode

\ifx\pdfoutput\undefined
  \donefalse
\else\ifcase\pdfoutput
  \donefalse
\else\ifx\pdfsavepos\undefined
  \donefalse
\else
  \donetrue
\fi\fi\fi

\chardef \pdfsyncstate     \zerocount
\chardef \pdfsynctracemode \zerocount

\ifdone

  \writestatus{pdfsync}{loading and enabling synchronization support}

\else

  \writestatus{pdfsync}{synchronization is only available with pdftex}

  \let \pdfsyncstart   \relax % brr, not per se symmetrically used, so
  \let \pdfsyncstop    \relax % enable and disable are better names
  \let \pdfsync        \relax

  \let \pdfsynctext    \gobbleoneargument
  \let \pdfsyncnode    \gobbleoneargument

  \let \enablepdfsync  \pdfsyncstart
  \let \disablepdfsync \pdfsyncstop

  \expandafter\endinput
\fi

\unprotect % not really needed

\newcount \pdfsynccounter
\newwrite \pdfsyncwrite

\def\pdfsyncstart{\chardef\pdfsyncstate\plusone}   \let\enablepdfsync \pdfsyncstart
\def\pdfsyncstop {\chardef\pdfsyncstate\zerocount} \let\disablepdfsync\pdfsyncstop

\def\dostartpdfsync
  {\immediate\openout\pdfsyncwrite\jobname.pdfsync
   \immediate\write\pdfsyncwrite{\jobname}%
   \immediate\write\pdfsyncwrite{version 0}}

\def\dostoppdfsync
  {\immediate\closeout\pdfsyncwrite}

\def\doregisterpdfsyncpage
  {\immediate\write\pdfsyncwrite{s\space\realfolio}}

\def\dopdfsyncopenfile
  {\immediate\write\pdfsyncwrite{(\space\readfilename}}

\def\dopdfsyncclosefile
  {\immediate\write\pdfsyncwrite{)}}

\def\doregisterpdfsyncnode#1%
  {\ifcase\pdfsyncstate\else
     \pdfsavepos
     \immediate\write\pdfsyncwrite{l\space\the\pdfsynccounter\space\the\inputlineno}%
     \expanded{\write\pdfsyncwrite{p\ifnum#1=\plusone*\fi\space\the\pdfsynccounter\space\noexpand\the\pdflastxpos\space\noexpand\the\pdflastypos}}%
     \global\advance\pdfsynccounter\plusone
   \fi}

\let\pdfsynctracer\gobbleoneargument

\def\pdfsync    {\doregisterpdfsyncnode\plusone  \pdfsynctracer*}
\def\pdfsyncnode{\doregisterpdfsyncnode\zerocount\pdfsynctracer}
\def\pdfsynctext{\doregisterpdfsyncnode\plusone  \pdfsynctracer}

\appendtoks \dostartpdfsync        \to \everystarttext
%appendtoks \dostoppdfsync         \to \everystoptext
\appendtoks \doregisterpdfsyncpage \to \everyshipout
\appendtoks \dopdfsyncopenfile     \to \everybeforereadfile
\appendtoks \dopdfsyncclosefile    \to \everyafterreadfile

\appendtoks \enablepdfsync         \to \everystarttext
\appendtoks \disablepdfsync        \to \everypagebody

% beware, adding nodes this way will interfere with the typesetting

\appendtoks \pdfsyncnode p\to \everypar
\appendtoks \pdfsyncnode m\to \everymath
\appendtoks \pdfsyncnode h\to \everyhbox

% just for fun

\def\pdfsynctracer#1%
  {\ifcase\pdfsynctracemode\else
     \ifcase\pdfsyncstate\else
       \begingroup
       \forgetall
       \disablepdfsync
       \ifcase\pdfsynctracemode\or % could be done more efficient, box around ifcase
         \setbox\scratchbox\hbox to \zeropoint{\hss\infofont#1\hss}%   1
       \else
         \setbox\scratchbox\hbox to \zeropoint{\hss\traceboxplacementtrue\boxcursor\hss}% 2/3/4
       \fi
       \smashbox\scratchbox\box\scratchbox
       \ifcase\pdfsynctracemode\or\or\or
         \setbox\scratchbox\hbox to \zeropoint{\hss\raise1.25ex\hbox{\infofont#1}\hss}%  3
         \smashbox\scratchbox\box\scratchbox
       \or
         \setbox\scratchbox\hbox to \zeropoint{\hss\lower1.25ex\hbox{\infofont#1}\hss}%  4
         \smashbox\scratchbox\box\scratchbox
       \fi
       \endgroup
     \fi
   \fi}

% \def\pdfsynctracer#1% more efficient but unreadable
%   {\ifcase\pdfsynctracemode\else
%      \ifcase\pdfsyncstate\else
%        \begingroup
%        \forgetall
%        \disablepdfsync
%        \setbox\scratchbox\hbox to \zeropoint
%           {\hss
%            \ifcase\pdfsynctracemode\or
%              \infofont#1%   1
%            \else
%              \traceboxplacementtrue\boxcursor% 2/3/4
%            \fi
%            \hss}%
%        \smashbox\scratchbox\box\scratchbox
%        \setbox\scratchbox\hbox to \zeropoint
%          {\hss
%           \ifcase\pdfsynctracemode\or\or\or
%             \raise1.25ex\hbox{\infofont#1}%  3
%           \or
%             \lower1.25ex\hbox{\infofont#1}%  4
%           \fi
%           \hss}%
%        \smashbox\scratchbox\box\scratchbox
%        \endgroup
%      \fi
%    \fi}

\protect

\doifnotmode{demo}{\endinput}

\chardef\pdfsynctracemode=3

\starttext

\chapter{Test}

\processfile{tufte}

\startitemize
\item first
\item second
\stopitemize

\processfile{tufte}

\startlines
some local \pdfsync sync and \pdfsynctext{here}a marked point
some local \pdfsync sync and \pdfsyncnode{here}a marked point
\stoplines

\stoptext