Debian Codegraph @

texk/web2c/synctexdir/synctex_parser.c

raw
1 /*
2 Copyright (c) 2008-2017 jerome DOT laurens AT u-bourgogne DOT fr
3
4 This file is part of the __SyncTeX__ package.
5
6 [//]: # (Latest Revision: Sun Oct 15 15:09:55 UTC 2017)
7 [//]: # (Version: 1.21)
8
9 See `synctex_parser_readme.md` for more details
10
11 ## License
12
13 Permission is hereby granted, free of charge, to any person
14 obtaining a copy of this software and associated documentation
15 files (the "Software"), to deal in the Software without
16 restriction, including without limitation the rights to use,
17 copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the
19 Software is furnished to do so, subject to the following
20 conditions:
21
22 The above copyright notice and this permission notice shall be
23 included in all copies or substantial portions of the Software.
24
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
27 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
29 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 OTHER DEALINGS IN THE SOFTWARE
33
34 Except as contained in this notice, the name of the copyright holder
35 shall not be used in advertising or otherwise to promote the sale,
36 use or other dealings in this Software without prior written
37 authorization from the copyright holder.
38
39 Acknowledgments:
40 ----------------
41 The author received useful remarks from the pdfTeX developers, especially Hahn The Thanh,
42 and significant help from XeTeX developer Jonathan Kew
43
44 Nota Bene:
45 ----------
46 If you include or use a significant part of the synctex package into a software,
47 I would appreciate to be listed as contributor and see "SyncTeX" highlighted.
48
49 */
50
51 /* We assume that high level application like pdf viewers will want
52 * to embed this code as is. We assume that they also have locale.h and setlocale.
53 * For other tools such as TeXLive tools, you must define SYNCTEX_USE_LOCAL_HEADER,
54 * when building. You also have to create and customize synctex_parser_local.h to fit your system.
55 * In particular, the HAVE_LOCALE_H and HAVE_SETLOCALE macros should be properly defined.
56 * With this design, you should not need to edit this file. */
57
58 /**
59 * \file synctex_parser.c
60 * \brief SyncTeX file parser and controller.
61 * - author: Jérôme LAURENS
62 * \version 1.21
63 * \date Sun Oct 15 15:09:55 UTC 2017
64 *
65 * Reads and parse *.synctex[.gz] files,
66 * performs edit and display queries.
67 *
68 * See
69 * - synctex_scanner_new_with_output_file
70 * - synctex_scanner_parse
71 * - synctex_scanner_free
72 * - synctex_display_query
73 * - synctex_edit_query
74 * - synctex_scanner_next_result
75 * - synctex_scanner_reset_result
76 *
77 * The data is organized in a graph with multiple entries.
78 * The root object is a scanner, it is created with the contents on a synctex file.
79 * Each node of the tree is a synctex_node_t object.
80 * There are 3 subtrees, two of them sharing the same leaves.
81 * The first tree is the list of input records, where input file names are associated with tags.
82 * The second tree is the box tree as given by TeX when shipping pages out.
83 * First level objects are sheets and forms, containing boxes, glues, kerns...
84 * The third tree allows to browse leaves according to tag and line.
85 */
86 /* Declare _GNU_SOURCE for accessing vasprintf. For MSC compiler, vasprintf is
87 * defined in this file
88 */
89 #define _GNU_SOURCE
90
91 # if defined(SYNCTEX_USE_LOCAL_HEADER)
92 # include "synctex_parser_local.h"
93 # else
94 # define HAVE_LOCALE_H 1
95 # define HAVE_SETLOCALE 1
96 # if defined(_MSC_VER)
97 # define SYNCTEX_INLINE __inline
98 # else
99 # define SYNCTEX_INLINE inline
100 # endif
101 # endif
102
103 #include <stdlib.h>
104 #include <stdarg.h>
105 #include <stdio.h>
106 #include <string.h>
107 #include <errno.h>
108 #include <limits.h>
109
110 #if defined(HAVE_LOCALE_H)
111 #include <locale.h>
112 #endif
113
114 /* Mark unused parameters, so that there will be no compile warnings. */
115 #ifdef __DARWIN_UNIX03
116 # define SYNCTEX_UNUSED(x) SYNCTEX_PRAGMA(unused(x))
117 # define SYNCTEX_PRAGMA(x) _Pragma ( #x )
118 #else
119 # define SYNCTEX_UNUSED(x) (void)(x);
120 #endif
121
122 #include "synctex_parser_advanced.h"
123
124 SYNCTEX_INLINE static int _synctex_abs(int x) {
125 return x>0? x: -x;
126 }
127 /* These are the possible extensions of the synctex file */
128 const char * synctex_suffix = ".synctex";
129 const char * synctex_suffix_gz = ".gz";
130
131 typedef synctex_node_p(*synctex_node_new_f)(synctex_scanner_p);
132 typedef void(*synctex_node_fld_f)(synctex_node_p);
133 typedef char *(*synctex_node_str_f)(synctex_node_p);
134
135 /**
136 * Pseudo class.
137 * - author: J. Laurens
138 *
139 * Each nodes has a class, it is therefore called an object.
140 * Each class has a unique scanner.
141 * Each class has a type which is a unique identifier.
142 * The class points to various methods,
143 * each of them vary amongst objects.
144 * Each class has a data model which stores node's attributes.
145 * Each class has an tree model which stores children and parent.
146 * Inspectors give access to data and tree elements.
147 */
148
149 /* 8 fields + size: spcflnat */
150 typedef struct synctex_tree_model_t {
151 int sibling;
152 int parent;
153 int child;
154 int friend;
155 int last;
156 int next_hbox;
157 int arg_sibling;
158 int target;
159 int size;
160 } synctex_tree_model_s;
161 typedef const synctex_tree_model_s * synctex_tree_model_p;
162
163 typedef struct synctex_data_model_t {
164 int tag;
165 int line;
166 int column;
167 int h;
168 int v;
169 int width;
170 int height;
171 int depth;
172 int mean_line;
173 int weight;
174 int h_V;
175 int v_V;
176 int width_V;
177 int height_V;
178 int depth_V;
179 int name;
180 int page;
181 int size;
182 } synctex_data_model_s;
183
184 typedef const synctex_data_model_s * synctex_data_model_p;
185
186 typedef int (*synctex_int_getter_f)(synctex_node_p);
187 typedef struct synctex_tlcpector_t {
188 synctex_int_getter_f tag;
189 synctex_int_getter_f line;
190 synctex_int_getter_f column;
191 } synctex_tlcpector_s;
192 typedef const synctex_tlcpector_s * synctex_tlcpector_p;
193 static int _synctex_int_none(synctex_node_p node) {
194 SYNCTEX_UNUSED(node)
195 return 0;
196 }
197 static const synctex_tlcpector_s synctex_tlcpector_none = {
198 &_synctex_int_none, /* tag */
199 &_synctex_int_none, /* line */
200 &_synctex_int_none, /* column */
201 };
202
203 typedef struct synctex_inspector_t {
204 synctex_int_getter_f h;
205 synctex_int_getter_f v;
206 synctex_int_getter_f width;
207 synctex_int_getter_f height;
208 synctex_int_getter_f depth;
209 } synctex_inspector_s;
210 typedef const synctex_inspector_s * synctex_inspector_p;
211 static const synctex_inspector_s synctex_inspector_none = {
212 &_synctex_int_none, /* h */
213 &_synctex_int_none, /* v */
214 &_synctex_int_none, /* width */
215 &_synctex_int_none, /* height */
216 &_synctex_int_none, /* depth */
217 };
218
219 typedef float (*synctex_float_getter_f)(synctex_node_p);
220 typedef struct synctex_vispector_t {
221 synctex_float_getter_f h;
222 synctex_float_getter_f v;
223 synctex_float_getter_f width;
224 synctex_float_getter_f height;
225 synctex_float_getter_f depth;
226 } synctex_vispector_s;
227 static float _synctex_float_none(synctex_node_p node) {
228 SYNCTEX_UNUSED(node)
229 return 0;
230 }
231 static const synctex_vispector_s synctex_vispector_none = {
232 &_synctex_float_none, /* h */
233 &_synctex_float_none, /* v */
234 &_synctex_float_none, /* width */
235 &_synctex_float_none, /* height */
236 &_synctex_float_none, /* depth */
237 };
238 typedef const synctex_vispector_s * synctex_vispector_p;
239
240 struct synctex_class_t {
241 synctex_scanner_p scanner;
242 synctex_node_type_t type;
243 synctex_node_new_f new;
244 synctex_node_fld_f free;
245 synctex_node_fld_f log;
246 synctex_node_fld_f display;
247 synctex_node_str_f abstract;
248 synctex_tree_model_p navigator;
249 synctex_data_model_p modelator;
250 synctex_tlcpector_p tlcpector;
251 synctex_inspector_p inspector;
252 synctex_vispector_p vispector;
253 };
254
255 /**
256 * Nota bene: naming convention.
257 * For static API, when the name contains "proxy", it applies to proxies.
258 * When the name contains "noxy", it applies to non proxies only.
259 * When the name contains "node", well it depends...
260 */
261
262 typedef synctex_node_p synctex_proxy_p;
263 typedef synctex_node_p synctex_noxy_p;
264
265 # ifdef SYNCTEX_NOTHING
266 # pragma mark -
267 # pragma mark Abstract OBJECTS and METHODS
268 # endif
269
270 /**
271 * \def SYNCTEX_MSG_SEND
272 * \brief Takes care of sending the given message if possible.
273 * - parameter NODE: of type synctex_node_p
274 * - parameter SELECTOR: one of the class_ pointer properties
275 */
276 # define SYNCTEX_MSG_SEND(NODE,SELECTOR) do {\
277 synctex_node_p N__ = NODE;\
278 if (N__ && N__->class_->SELECTOR) {\
279 (*(N__->class_->SELECTOR))(N__);\
280 }\
281 } while (synctex_NO)
282
283 /**
284 * Free the given node by sending the free message.
285 * - parameter NODE: of type synctex_node_p
286 */
287 static void synctex_node_free(synctex_node_p node) {
288 SYNCTEX_MSG_SEND(node,free);
289 }
290 # if defined(SYNCTEX_TESTING)
291 # if !defined(SYNCTEX_USE_HANDLE)
292 # define SYNCTEX_USE_HANDLE 1
293 # endif
294 # if !defined(SYNCTEX_USE_CHARINDEX)
295 # define SYNCTEX_USE_CHARINDEX 1
296 # endif
297 # endif
298 SYNCTEX_INLINE static synctex_node_p _synctex_new_handle_with_target(synctex_node_p target);
299 # if defined(SYNCTEX_USE_HANDLE)
300 # define SYNCTEX_SCANNER_FREE_HANDLE(SCANR) \
301 __synctex_scanner_free_handle(SCANR)
302 # define SYNCTEX_SCANNER_REMOVE_HANDLE_TO(WHAT) \
303 __synctex_scanner_remove_handle_to(WHAT)
304 # define SYNCTEX_REGISTER_HANDLE_TO(NODE) \
305 __synctex_scanner_register_handle_to(NODE)
306 # else
307 # define SYNCTEX_SCANNER_FREE_HANDLE(SCANR)
308 # define SYNCTEX_SCANNER_REMOVE_HANDLE_TO(WHAT)
309 # define SYNCTEX_REGISTER_HANDLE_TO(NODE)
310 # endif
311
312 # if defined(SYNCTEX_USE_CHARINDEX)
313 # define SYNCTEX_CHARINDEX(NODE) (NODE->char_index)
314 # define SYNCTEX_LINEINDEX(NODE) (NODE->line_index)
315 # define SYNCTEX_PRINT_CHARINDEX_FMT "#%i"
316 # define SYNCTEX_PRINT_CHARINDEX_WHAT ,SYNCTEX_CHARINDEX(node)
317 # define SYNCTEX_PRINT_CHARINDEX \
318 printf(SYNCTEX_PRINT_CHARINDEX_FMT SYNCTEX_PRINT_CHARINDEX_WHAT)
319 # define SYNCTEX_PRINT_LINEINDEX_FMT "L#%i"
320 # define SYNCTEX_PRINT_LINEINDEX_WHAT ,SYNCTEX_LINEINDEX(node)
321 # define SYNCTEX_PRINT_LINEINDEX \
322 printf(SYNCTEX_PRINT_LINEINDEX_FMT SYNCTEX_PRINT_LINEINDEX_WHAT)
323 # define SYNCTEX_PRINT_CHARINDEX_NL \
324 printf(SYNCTEX_PRINT_CHARINDEX_FMT "\n" SYNCTEX_PRINT_CHARINDEX_WHAT)
325 # define SYNCTEX_PRINT_LINEINDEX_NL \
326 printf(SYNCTEX_PRINT_CHARINDEX_FMT "\n"SYNCTEX_PRINT_LINEINDEX_WHAT)
327 # define SYNCTEX_IMPLEMENT_CHARINDEX(NODE,CORRECTION)\
328 NODE->char_index = (synctex_charindex_t)(scanner->reader->charindex_offset+SYNCTEX_CUR-SYNCTEX_START+(CORRECTION)); \
329 NODE->line_index = scanner->reader->line_number;
330 # else
331 # define SYNCTEX_CHARINDEX(NODE) 0
332 # define SYNCTEX_LINEINDEX(NODE) 0
333 # define SYNCTEX_PRINT_CHARINDEX_FMT
334 # define SYNCTEX_PRINT_CHARINDEX_WHAT
335 # define SYNCTEX_PRINT_CHARINDEX
336 # define SYNCTEX_PRINT_CHARINDEX
337 # define SYNCTEX_PRINT_LINEINDEX_FMT
338 # define SYNCTEX_PRINT_LINEINDEX_WHAT
339 # define SYNCTEX_PRINT_LINEINDEX
340 # define SYNCTEX_PRINT_CHARINDEX_NL printf("\n")
341 # define SYNCTEX_PRINT_LINEINDEX_NL printf("\n")
342 # define SYNCTEX_IMPLEMENT_CHARINDEX(NODE,CORRECTION)
343 # endif
344
345 /**
346 * The next macros are used to access the node tree info
347 * SYNCTEX_DATA(node) points to the first synctex integer or pointer data of node
348 * SYNCTEX_DATA(node)[index] is the information at index
349 * for example, the page of a sheet is stored in SYNCTEX_DATA(sheet)[_synctex_data_page_idx]
350 * - parameter NODE: of type synctex_node_p
351 * If the name starts with "__", the argument is nonullable
352 */
353 # ifdef SYNCTEX_NOTHING
354 # pragma mark -
355 # pragma mark Tree SETGET
356 # endif
357
358 #if SYNCTEX_DEBUG > 1000
359 #define SYNCTEX_PARAMETER_ASSERT(WHAT) \
360 do { \
361 if (!(WHAT)) { \
362 printf("! Parameter failure: %s\n",#WHAT); \
363 } \
364 } while (synctex_NO)
365 #define DEFINE_SYNCTEX_TREE_HAS(WHAT)\
366 static synctex_bool_t _synctex_tree_has_##WHAT(synctex_node_p node) {\
367 if (node) {\
368 if (node->class_->navigator->WHAT>=0) {\
369 return synctex_YES; \
370 } else {\
371 printf("WARNING: NO tree %s for %s\n", #WHAT, synctex_node_isa(node));\
372 }\
373 }\
374 return synctex_NO;\
375 }
376 #else
377 # define SYNCTEX_PARAMETER_ASSERT(WHAT)
378 # define DEFINE_SYNCTEX_TREE_HAS(WHAT) \
379 SYNCTEX_INLINE static synctex_bool_t _synctex_tree_has_##WHAT(synctex_node_p node) {\
380 return (node && (node->class_->navigator->WHAT>=0));\
381 }
382 #endif
383
384 # define DEFINE_SYNCTEX_TREE__GET(WHAT) \
385 SYNCTEX_INLINE static synctex_node_p __synctex_tree_##WHAT(synctex_non_null_node_p node) {\
386 return node->data[node->class_->navigator->WHAT].as_node;\
387 }
388 # define DEFINE_SYNCTEX_TREE_GET(WHAT) \
389 DEFINE_SYNCTEX_TREE__GET(WHAT) \
390 SYNCTEX_INLINE static synctex_node_p _synctex_tree_##WHAT(synctex_node_p node) {\
391 if (_synctex_tree_has_##WHAT(node)) {\
392 return __synctex_tree_##WHAT(node);\
393 }\
394 return 0;\
395 }
396 # define DEFINE_SYNCTEX_TREE__RESET(WHAT) \
397 SYNCTEX_INLINE static synctex_node_p __synctex_tree_reset_##WHAT(synctex_non_null_node_p node) {\
398 synctex_node_p old = node->data[node->class_->navigator->WHAT].as_node;\
399 node->data[node->class_->navigator->WHAT].as_node=NULL;\
400 return old;\
401 }
402 # define DEFINE_SYNCTEX_TREE_RESET(WHAT) \
403 DEFINE_SYNCTEX_TREE__RESET(WHAT) \
404 SYNCTEX_INLINE static synctex_node_p _synctex_tree_reset_##WHAT(synctex_node_p node) {\
405 return _synctex_tree_has_##WHAT(node)? \
406 __synctex_tree_reset_##WHAT(node): NULL; \
407 }
408 # define DEFINE_SYNCTEX_TREE__SET(WHAT) \
409 SYNCTEX_INLINE static synctex_node_p __synctex_tree_set_##WHAT(synctex_non_null_node_p node, synctex_node_p new_value) {\
410 synctex_node_p old = __synctex_tree_##WHAT(node);\
411 node->data[node->class_->navigator->WHAT].as_node=new_value;\
412 return old;\
413 }
414 # define DEFINE_SYNCTEX_TREE_SET(WHAT) \
415 DEFINE_SYNCTEX_TREE__SET(WHAT) \
416 SYNCTEX_INLINE static synctex_node_p _synctex_tree_set_##WHAT(synctex_node_p node, synctex_node_p new_value) {\
417 return _synctex_tree_has_##WHAT(node)?\
418 __synctex_tree_set_##WHAT(node,new_value):NULL;\
419 }
420 # define DEFINE_SYNCTEX_TREE__GETSETRESET(WHAT) \
421 DEFINE_SYNCTEX_TREE__GET(WHAT) \
422 DEFINE_SYNCTEX_TREE__SET(WHAT) \
423 DEFINE_SYNCTEX_TREE__RESET(WHAT)
424
425 # define DEFINE_SYNCTEX_TREE_GETSET(WHAT) \
426 DEFINE_SYNCTEX_TREE_HAS(WHAT) \
427 DEFINE_SYNCTEX_TREE_GET(WHAT) \
428 DEFINE_SYNCTEX_TREE_SET(WHAT)
429
430 # define DEFINE_SYNCTEX_TREE_GETRESET(WHAT) \
431 DEFINE_SYNCTEX_TREE_HAS(WHAT) \
432 DEFINE_SYNCTEX_TREE_GET(WHAT) \
433 DEFINE_SYNCTEX_TREE_RESET(WHAT)
434
435 # define DEFINE_SYNCTEX_TREE_GETSETRESET(WHAT) \
436 DEFINE_SYNCTEX_TREE_HAS(WHAT) \
437 DEFINE_SYNCTEX_TREE_GET(WHAT) \
438 DEFINE_SYNCTEX_TREE_SET(WHAT) \
439 DEFINE_SYNCTEX_TREE_RESET(WHAT)
440
441 /*
442 * _synctex_tree_set_... methods return the old value.
443 * The return value of _synctex_tree_set_child and
444 * _synctex_tree_set_sibling must be released somehow.
445 */
446 /* The next macro call creates:
447 SYNCTEX_INLINE static synctex_node_p __synctex_tree_sibling(synctex_node_p node)
448 SYNCTEX_INLINE static synctex_node_p __synctex_tree_set_sibling(synctex_node_p node, synctex_node_p new_value)
449 SYNCTEX_INLINE static synctex_node_p __synctex_tree_reset_sibling(synctex_node_p node)
450 */
451 DEFINE_SYNCTEX_TREE__GETSETRESET(sibling)
452 /* The next macro call creates:
453 SYNCTEX_INLINE static synctex_bool_t _synctex_tree_has_parent(synctex_node_p node);
454 SYNCTEX_INLINE static synctex_node_p __synctex_tree_parent(synctex_non_null_node_p node);
455 SYNCTEX_INLINE static synctex_node_p _synctex_tree_parent(synctex_node_p node);
456 SYNCTEX_INLINE static synctex_node_p __synctex_tree_set_parent(synctex_node_p node, synctex_node_p new_value);
457 SYNCTEX_INLINE static synctex_node_p _synctex_tree_set_parent(synctex_node_p node, synctex_node_p new_value);
458 SYNCTEX_INLINE static synctex_node_p __synctex_tree_reset_parent(synctex_node_p node);
459 SYNCTEX_INLINE static synctex_node_p _synctex_tree_reset_parent(synctex_node_p node);
460 */
461 DEFINE_SYNCTEX_TREE_GETSETRESET(parent)
462 DEFINE_SYNCTEX_TREE_GETSETRESET(child)
463 DEFINE_SYNCTEX_TREE_GETSETRESET(friend)
464 /* The next macro call creates:
465 SYNCTEX_INLINE static synctex_bool_t _synctex_tree_has_last(synctex_node_p node);
466 SYNCTEX_INLINE static synctex_node_p __synctex_tree_last(synctex_non_null_node_p node);
467 SYNCTEX_INLINE static synctex_node_p _synctex_tree_last(synctex_node_p node);
468 SYNCTEX_INLINE static synctex_node_p __synctex_tree_set_last(synctex_node_p node, synctex_node_p new_value);
469 SYNCTEX_INLINE static synctex_node_p _synctex_tree_set_last(synctex_node_p node, synctex_node_p new_value);
470 */
471 DEFINE_SYNCTEX_TREE_GETSET(last)
472 DEFINE_SYNCTEX_TREE_GETSET(next_hbox)
473 DEFINE_SYNCTEX_TREE_GETSET(arg_sibling)
474 DEFINE_SYNCTEX_TREE_GETSETRESET(target)
475
476 #if SYNCTEX_DEBUG>1000
477 # undef SYNCTEX_USE_NODE_COUNT
478 # define SYNCTEX_USE_NODE_COUNT 1
479 #endif
480 #if SYNCTEX_USE_NODE_COUNT>0
481 # define SYNCTEX_DECLARE_NODE_COUNT int node_count;
482 # define SYNCTEX_INIT_NODE_COUNT \
483 do { node_count = 0; } while(synctex_NO)
484 #else
485 # define SYNCTEX_DECLARE_NODE_COUNT
486 # define SYNCTEX_INIT_NODE_COUNT
487 #endif
488
489 #if SYNCTEX_USE_NODE_COUNT>10
490 # define SYNCTEX_DID_NEW(N) _synctex_did_new(N)
491 # define SYNCTEX_WILL_FREE(N) _synctex_will_free(N)
492 #else
493 # define SYNCTEX_DID_NEW(N)
494 # define SYNCTEX_WILL_FREE(N)
495 #endif
496
497 #define SYNCTEX_HAS_CHILDREN(NODE) (NODE && _synctex_tree_child(NODE))
498 # ifdef __SYNCTEX_WORK__
499 # include "/usr/local/include/node/zlib.h"
500 # else
501 # include <zlib.h>
502 # endif
503
504 # ifdef SYNCTEX_NOTHING
505 # pragma mark -
506 # pragma mark STATUS
507 # endif
508 /* When the end of the synctex file has been reached: */
509 # define SYNCTEX_STATUS_EOF 0
510 /* When the function could not return the value it was asked for: */
511 # define SYNCTEX_STATUS_NOT_OK (SYNCTEX_STATUS_EOF+1)
512 /* When the function returns the value it was asked for:
513 It must be the biggest one */
514 # define SYNCTEX_STATUS_OK (SYNCTEX_STATUS_NOT_OK+1)
515 /* Generic error: */
516 # define SYNCTEX_STATUS_ERROR (SYNCTEX_STATUS_EOF-1)
517 /* Parameter error: */
518 # define SYNCTEX_STATUS_BAD_ARGUMENT (SYNCTEX_STATUS_ERROR-1)
519
520 # ifdef SYNCTEX_NOTHING
521 # pragma mark -
522 # pragma mark File reader
523 # endif
524
525 /* We ensure that SYNCTEX_BUFFER_SIZE < UINT_MAX, I don't know if it makes sense... */
526 /* Actually, the minimum buffer size is driven by integer and float parsing, including the unit.
527 * ±0.123456789e123??
528 */
529 # define SYNCTEX_BUFFER_MIN_SIZE 32
530 # define SYNCTEX_BUFFER_SIZE 32768
531
532 #if SYNCTEX_BUFFER_SIZE >= UINT_MAX
533 # error BAD BUFFER SIZE(1)
534 #endif
535 #if SYNCTEX_BUFFER_SIZE < SYNCTEX_BUFFER_MIN_SIZE
536 # error BAD BUFFER SIZE(2)
537 #endif
538
539 typedef struct synctex_reader_t {
540 gzFile file; /* The (possibly compressed) file */
541 char * output;
542 char * synctex;
543 char * current; /* current location in the buffer */
544 char * start; /* start of the buffer */
545 char * end; /* end of the buffer */
546 size_t min_size;
547 size_t size;
548 int lastv;
549 int line_number;
550 SYNCTEX_DECLARE_CHAR_OFFSET
551 } synctex_reader_s;
552
553 typedef synctex_reader_s * synctex_reader_p;
554
555 typedef struct {
556 synctex_status_t status;
557 char * synctex;
558 gzFile file;
559 synctex_io_mode_t io_mode;
560 } synctex_open_s;
561
562 /* This functions opens the file at the "output" given location.
563 * It manages the problem of quoted filenames that appear with pdftex and filenames containing the space character.
564 * In TeXLive 2008, the synctex file created with pdftex did contain unexpected quotes.
565 * This function will remove them if possible.
566 * All the reference arguments will take a value on return. They must be non NULL.
567 * - returns: an open structure which status is
568 * SYNCTEX_STATUS_OK on success,
569 * SYNCTEX_STATUS_ERROR on failure.
570 * - note: on success, the caller is the owner
571 * of the fields of the returned open structure.
572 */
573 static synctex_open_s __synctex_open_v2(const char * output, synctex_io_mode_t io_mode, synctex_bool_t add_quotes) {
574 synctex_open_s open = {SYNCTEX_STATUS_ERROR, NULL, NULL, io_mode};
575 char * quoteless_synctex_name = NULL;
576 const char * mode = _synctex_get_io_mode_name(open.io_mode);
577 size_t size = strlen(output)+strlen(synctex_suffix)+strlen(synctex_suffix_gz)+1;
578 if (NULL == (open.synctex = (char *)malloc(size))) {
579 _synctex_error("! __synctex_open_v2: Memory problem (1)\n");
580 return open;
581 }
582 /* we have reserved for synctex enough memory to copy output (including its 2 eventual quotes), both suffices,
583 * including the terminating character. size is free now. */
584 if (open.synctex != strcpy(open.synctex,output)) {
585 _synctex_error("! __synctex_open_v2: Copy problem\n");
586 return_on_error:
587 free(open.synctex);
588 open.synctex = NULL;
589 free(quoteless_synctex_name);/* We MUST have quoteless_synctex_name<>synctex_name */
590 return open;
591 }
592 /* remove the last path extension if any */
593 _synctex_strip_last_path_extension(open.synctex);
594 if (!strlen(open.synctex)) {
595 goto return_on_error;
596 }
597 /* now insert quotes. */
598 if (add_quotes) {
599 char * quoted = NULL;
600 if (_synctex_copy_with_quoting_last_path_component(open.synctex,&quoted,size) || quoted == NULL) {
601 /* There was an error or quoting does not make sense: */
602 goto return_on_error;
603 }
604 quoteless_synctex_name = open.synctex;
605 open.synctex = quoted;
606 }
607 /* Now add to open.synctex the first path extension. */
608 if (open.synctex != strcat(open.synctex,synctex_suffix)){
609 _synctex_error("! __synctex_open_v2: Concatenation problem (can't add suffix '%s')\n",synctex_suffix);
610 goto return_on_error;
611 }
612 /* Add to quoteless_synctex_name as well, if relevant. */
613 if (quoteless_synctex_name && (quoteless_synctex_name != strcat(quoteless_synctex_name,synctex_suffix))){
614 free(quoteless_synctex_name);
615 quoteless_synctex_name = NULL;
616 }
617 if (NULL == (open.file = gzopen(open.synctex,mode))) {
618 /* Could not open this file */
619 if (errno != ENOENT) {
620 /* The file does exist, this is a lower level error, I can't do anything. */
621 _synctex_error("could not open %s, error %i\n",open.synctex,errno);
622 goto return_on_error;
623 }
624 /* Apparently, there is no uncompressed synctex file. Try the compressed version */
625 if (open.synctex != strcat(open.synctex,synctex_suffix_gz)){
626 _synctex_error("! __synctex_open_v2: Concatenation problem (can't add suffix '%s')\n",synctex_suffix_gz);
627 goto return_on_error;
628 }
629 open.io_mode |= synctex_io_gz_mask;
630 mode = _synctex_get_io_mode_name(open.io_mode); /* the file is a compressed and is a binary file, this caused errors on Windows */
631 /* Add the suffix to the quoteless_synctex_name as well. */
632 if (quoteless_synctex_name && (quoteless_synctex_name != strcat(quoteless_synctex_name,synctex_suffix_gz))){
633 free(quoteless_synctex_name);
634 quoteless_synctex_name = NULL;
635 }
636 if (NULL == (open.file = gzopen(open.synctex,mode))) {
637 /* Could not open this file */
638 if (errno != ENOENT) {
639 /* The file does exist, this is a lower level error, I can't do anything. */
640 _synctex_error("Could not open %s, error %i\n",open.synctex,errno);
641 }
642 goto return_on_error;
643 }
644 }
645 /* At this point, the file is properly open.
646 * If we are in the add_quotes mode, we change the file name by removing the quotes. */
647 if (quoteless_synctex_name) {
648 gzclose(open.file);
649 if (rename(open.synctex,quoteless_synctex_name)) {
650 _synctex_error("Could not rename %s to %s, error %i\n",open.synctex,quoteless_synctex_name,errno);
651 /* We could not rename, reopen the file with the quoted name. */
652 if (NULL == (open.file = gzopen(open.synctex,mode))) {
653 /* No luck, could not re open this file, something has happened meanwhile */
654 if (errno != ENOENT) {
655 /* The file does not exist any more, it has certainly be removed somehow
656 * this is a lower level error, I can't do anything. */
657 _synctex_error("Could not open again %s, error %i\n",open.synctex,errno);
658 }
659 goto return_on_error;
660 }
661 } else {
662 /* The file has been successfully renamed */
663 if (NULL == (open.file = gzopen(quoteless_synctex_name,mode))) {
664 /* Could not open this file */
665 if (errno != ENOENT) {
666 /* The file does exist, this is a lower level error, I can't do anything. */
667 _synctex_error("Could not open renamed %s, error %i\n",quoteless_synctex_name,errno);
668 }
669 goto return_on_error;
670 }
671 /* The quote free file name should replace the old one:*/
672 free(open.synctex);
673 open.synctex = quoteless_synctex_name;
674 quoteless_synctex_name = NULL;
675 }
676 }
677 /* The operation is successful, return the arguments by value. */
678 open.status = SYNCTEX_STATUS_OK;
679 return open;
680 }
681
682 /* Opens the output file, taking into account the eventual build_directory.
683 * - returns: an open structure which status is
684 * SYNCTEX_STATUS_OK on success,
685 * SYNCTEX_STATUS_ERROR on failure.
686 * - note: on success, the caller is the owner
687 * of the fields of the returned open structure.
688 */
689 static synctex_open_s _synctex_open_v2(const char * output, const char * build_directory, synctex_io_mode_t io_mode, synctex_bool_t add_quotes) {
690 synctex_open_s open = __synctex_open_v2(output,io_mode,add_quotes);
691 if (open.status == SYNCTEX_STATUS_OK) {
692 return open;
693 }
694 if (build_directory && strlen(build_directory)) {
695 char * build_output;
696 const char *lpc;
697 size_t size;
698 synctex_bool_t is_absolute;
699 build_output = NULL;
700 lpc = _synctex_last_path_component(output);
701 size = strlen(build_directory)+strlen(lpc)+2; /* One for the '/' and one for the '\0'. */
702 is_absolute = _synctex_path_is_absolute(build_directory);
703 if (!is_absolute) {
704 size += strlen(output);
705 }
706 if ((build_output = (char *)_synctex_malloc(size))) {
707 if (is_absolute) {
708 build_output[0] = '\0';
709 } else {
710 if (build_output != strcpy(build_output,output)) {
711 _synctex_free(build_output);
712 return open;
713 }
714 build_output[lpc-output]='\0';
715 }
716 if (build_output == strcat(build_output,build_directory)) {
717 /* Append a path separator if necessary. */
718 if (!SYNCTEX_IS_PATH_SEPARATOR(build_output[strlen(build_directory)-1])) {
719 if (build_output != strcat(build_output,"/")) {
720 _synctex_free(build_output);
721 return open;
722 }
723 }
724 /* Append the last path component of the output. */
725 if (build_output != strcat(build_output,lpc)) {
726 _synctex_free(build_output);
727 return open;
728 }
729 open = __synctex_open_v2(build_output,io_mode,add_quotes);
730 }
731 _synctex_free(build_output);
732 } /* if ((build_output... */
733 } /* if (build_directory...) */
734 return open;
735 }
736 static void synctex_reader_free(synctex_reader_p reader) {
737 if (reader) {
738 _synctex_free(reader->output);
739 _synctex_free(reader->synctex);
740 _synctex_free(reader->start);
741 gzclose(reader->file);
742 _synctex_free(reader);
743 }
744 }
745 /*
746 * Return reader on success.
747 * Deallocate reader and return NULL on failure.
748 */
749 static synctex_reader_p synctex_reader_init_with_output_file(synctex_reader_p reader, const char * output, const char * build_directory) {
750 if (reader) {
751 /* now open the synctex file */
752 synctex_open_s open = _synctex_open_v2(output,build_directory,0,synctex_ADD_QUOTES);
753 if (open.status<SYNCTEX_STATUS_OK) {
754 open = _synctex_open_v2(output,build_directory,0,synctex_DONT_ADD_QUOTES);
755 if (open.status<SYNCTEX_STATUS_OK) {
756 return NULL;
757 }
758 }
759 reader->synctex = open.synctex;
760 reader->file = open.file;
761 /* make a private copy of output */
762 if (NULL == (reader->output = (char *)_synctex_malloc(strlen(output)+1))){
763 _synctex_error("! synctex_scanner_new_with_output_file: Memory problem (2), reader's output is not reliable.");
764 } else if (reader->output != strcpy(reader->output,output)) {
765 _synctex_free(reader->output);
766 reader->output = NULL;
767 _synctex_error("! synctex_scanner_new_with_output_file: Copy problem, reader's output is not reliable.");
768 }
769 reader->start = reader->end = reader->current = NULL;
770 reader->min_size = SYNCTEX_BUFFER_MIN_SIZE;
771 reader->size = SYNCTEX_BUFFER_SIZE;
772 reader->start = reader->current =
773 (char *)_synctex_malloc(reader->size+1); /* one more character for null termination */
774 if (NULL == reader->start) {
775 _synctex_error("! malloc error in synctex_reader_init_with_output_file.");
776 #ifdef SYNCTEX_DEBUG
777 return reader;
778 #else
779 synctex_reader_free(reader);
780 return NULL;
781 #endif
782 }
783 reader->end = reader->start+reader->size;
784 /* reader->end always points to a null terminating character.
785 * Maybe there is another null terminating character between reader->current and reader->end-1.
786 * At least, we are sure that reader->current points to a string covering a valid part of the memory. */
787 # if defined(SYNCTEX_USE_CHARINDEX)
788 reader->charindex_offset = -reader->size;
789 # endif
790 }
791 return reader;
792 }
793
794 # if defined(SYNCTEX_USE_HANDLE)
795 # define SYNCTEX_DECLARE_HANDLE synctex_node_p handle;
796 # else
797 # define SYNCTEX_DECLARE_HANDLE
798 # endif
799
800 # ifdef SYNCTEX_NOTHING
801 # pragma mark -
802 # pragma mark SCANNER
803 # endif
804 /**
805 * The synctex scanner is the root object.
806 * Is is initialized with the contents of a text file or a gzipped file.
807 * The buffer_.* are first used to parse the text.
808 */
809 struct synctex_scanner_t {
810 synctex_reader_p reader;
811 SYNCTEX_DECLARE_NODE_COUNT
812 SYNCTEX_DECLARE_HANDLE
813 char * output_fmt; /* dvi or pdf, not yet used */
814 synctex_iterator_p iterator;/* result iterator */
815 int version; /* 1, not yet used */
816 struct {
817 unsigned has_parsed:1; /* Whether the scanner has parsed its underlying synctex file. */
818 unsigned postamble:1; /* Whether the scanner has parsed its underlying synctex file. */
819 unsigned reserved:sizeof(unsigned)-2; /* alignment */
820 } flags;
821 int pre_magnification; /* magnification from the synctex preamble */
822 int pre_unit; /* unit from the synctex preamble */
823 int pre_x_offset; /* X offset from the synctex preamble */
824 int pre_y_offset; /* Y offset from the synctex preamble */
825 int count; /* Number of records, from the synctex postamble */
826 float unit; /* real unit, from synctex preamble or post scriptum */
827 float x_offset; /* X offset, from synctex preamble or post scriptum */
828 float y_offset; /* Y Offset, from synctex preamble or post scriptum */
829 synctex_node_p input; /* The first input node, its siblings are the other input nodes */
830 synctex_node_p sheet; /* The first sheet node, its siblings are the other sheet nodes */
831 synctex_node_p form; /* The first form, its siblings are the other forms */
832 synctex_node_p ref_in_sheet; /* The first form ref node in sheet, its friends are the other form ref nodes */
833 synctex_node_p ref_in_form; /* The first form ref node, its friends are the other form ref nodes in sheet */
834 int number_of_lists; /* The number of friend lists */
835 synctex_node_r lists_of_friends;/* The friend lists */
836 synctex_class_s class_[synctex_node_number_of_types]; /* The classes of the nodes of the scanner */
837 int display_switcher;
838 char * display_prompt;
839 };
840
841 /**
842 * Create a new node of the given type.
843 * - parameter scanner: of type synctex_node_p
844 * - parameter type: a type, the client is responsible
845 * to ask for an acceptable type.
846 */
847 synctex_node_p synctex_node_new(synctex_scanner_p scanner, synctex_node_type_t type) {
848 return scanner? scanner->class_[type].new(scanner):NULL;
849 }
850 # if defined(SYNCTEX_USE_HANDLE)
851 SYNCTEX_INLINE static void __synctex_scanner_free_handle(synctex_scanner_p scanner) {
852 synctex_node_free(scanner->handle);
853 }
854 SYNCTEX_INLINE static void __synctex_scanner_remove_handle_to(synctex_node_p node) {
855 synctex_node_p arg_sibling = NULL;
856 synctex_node_p handle = node->class_->scanner->handle;
857 while (handle) {
858 synctex_node_p sibling;
859 if (node == _synctex_tree_target(handle)) {
860 sibling = __synctex_tree_reset_sibling(handle);
861 if (arg_sibling) {
862 __synctex_tree_set_sibling(arg_sibling, sibling);
863 } else {
864 node->class_->scanner->handle = sibling;
865 }
866 synctex_node_free(handle);
867 break;
868 } else {
869 sibling = __synctex_tree_sibling(handle);
870 }
871 arg_sibling = handle;
872 handle = sibling;
873 }
874 }
875 SYNCTEX_INLINE static void __synctex_scanner_register_handle_to(synctex_node_p node) {
876 synctex_node_p NNN = _synctex_new_handle_with_target(node);
877 __synctex_tree_set_sibling(NNN,node->class_->scanner->handle);
878 node->class_->scanner->handle = NNN;
879 }
880 #endif
881 #if SYNCTEX_USE_NODE_COUNT>10
882 SYNCTEX_INLINE static void _synctex_did_new(synctex_node_p node) {
883 printf("NODE CREATED # %i, %s, %p\n",
884 (node->class_->scanner->node_count)++,
885 synctex_node_isa(node),
886 node);
887 }
888 SYNCTEX_INLINE static void _synctex_will_free(synctex_node_p node) {
889 printf("NODE DELETED # %i, %s, %p\n",
890 --(node->class_->scanner->node_count),
891 synctex_node_isa(node),
892 node);
893 }
894 #endif
895
896 /**
897 * Free the given node.
898 * - parameter node: of type synctex_node_p
899 * - note: a node is meant to own its child and sibling.
900 * It is not owned by its parent, unless it is its first child.
901 * This destructor is for all nodes with children.
902 */
903 static void _synctex_free_node(synctex_node_p node) {
904 if (node) {
905 synctex_node_p sibling;
906 synctex_node_p child;
907 SYNCTEX_SCANNER_REMOVE_HANDLE_TO(node);
908 SYNCTEX_WILL_FREE(node);
909 sibling = __synctex_tree_reset_sibling(node);
910 child = _synctex_tree_reset_child(node);
911 synctex_node_free(sibling);
912 synctex_node_free(child);
913 _synctex_free(node);
914 }
915 return;
916 }
917 /**
918 * Free the given handle.
919 * - parameter node: of type synctex_node_p
920 * - note: a node is meant to own its child and sibling.
921 * It is not owned by its parent, unless it is its first child.
922 * This destructor is for all handles.
923 */
924 /*
925 static void _synctex_free_handle_old(synctex_node_p handle) {
926 if (handle) {
927 _synctex_free_handle_old(__synctex_tree_sibling(handle));
928 _synctex_free_handle_old(_synctex_tree_child(handle));
929 _synctex_free(handle);
930 }
931 return;
932 }
933 */
934 static void _synctex_free_handle(synctex_node_p handle) {
935 if (handle) {
936 synctex_node_p n = handle;
937 synctex_node_p nn;
938 __synctex_tree_set_parent(n, NULL);
939 down:
940 while ((nn = _synctex_tree_child(n))) {
941 __synctex_tree_set_parent(nn, n);
942 n = nn;
943 };
944 right:
945 nn = __synctex_tree_sibling(n);
946 if (nn) {
947 _synctex_free(n);
948 n = nn;
949 goto down;
950 }
951 nn = __synctex_tree_parent(n);
952 _synctex_free(n);
953 if (nn) {
954 n = nn;
955 goto right;
956 }
957 }
958 return;
959 }
960
961 /**
962 * Free the given leaf node.
963 * - parameter node: of type synctex_node_p, with no child nor sibling.
964 * - note: a node is meant to own its child and sibling.
965 * It is not owned by its parent, unless it is its first child.
966 * This destructor is for all nodes with no children.
967 */
968 static void _synctex_free_leaf(synctex_node_p node) {
969 if (node) {
970 synctex_node_p sibling;
971 SYNCTEX_SCANNER_REMOVE_HANDLE_TO(node);
972 SYNCTEX_WILL_FREE(node);
973 sibling = __synctex_tree_reset_sibling(node);
974 synctex_node_free(sibling);
975 _synctex_free(node);
976 }
977 return;
978 }
979
980 /**
981 SYNCTEX_CUR, SYNCTEX_START and SYNCTEX_END are convenient shortcuts
982 */
983 # define SYNCTEX_CUR (scanner->reader->current)
984 # define SYNCTEX_START (scanner->reader->start)
985 # define SYNCTEX_END (scanner->reader->end)
986
987 /* Here are gathered all the possible status that the next scanning functions will return.
988 * All these functions return a status, and pass their result through pointers.
989 * Negative values correspond to errors.
990 * The management of the buffer is causing some significant overhead.
991 * Every function that may access the buffer returns a status related to the buffer and file state.
992 * status >= SYNCTEX_STATUS_OK means the function worked as expected
993 * status < SYNCTEX_STATUS_OK means the function did not work as expected
994 * status == SYNCTEX_STATUS_NOT_OK means the function did not work as expected but there is still some material to parse.
995 * status == SYNCTEX_STATUS_EOF means the function did not work as expected and there is no more material.
996 * status<SYNCTEX_STATUS_EOF means an error
997 */
998 #if defined(SYNCTEX_USE_CHARINDEX)
999 synctex_node_p synctex_scanner_handle(synctex_scanner_p scanner) {
1000 return scanner? scanner->handle:NULL;
1001 }
1002 #endif
1003
1004 # ifdef SYNCTEX_NOTHING
1005 # pragma mark -
1006 # pragma mark Decoding prototypes
1007 # endif
1008
1009 typedef struct {
1010 int integer;
1011 synctex_status_t status;
1012 } synctex_is_s;
1013
1014 static synctex_is_s _synctex_decode_int(synctex_scanner_p scanner);
1015 static synctex_is_s _synctex_decode_int_opt(synctex_scanner_p scanner, int default_value);
1016 static synctex_is_s _synctex_decode_int_v(synctex_scanner_p scanner);
1017
1018 typedef struct {
1019 char * string;
1020 synctex_status_t status;
1021 } synctex_ss_s;
1022
1023 static synctex_ss_s _synctex_decode_string(synctex_scanner_p scanner);
1024
1025 # ifdef SYNCTEX_NOTHING
1026 # pragma mark -
1027 # pragma mark Data SETGET
1028 # endif
1029
1030 /**
1031 * The next macros are used to access the node data info
1032 * through the class modelator integer fields.
1033 * - parameter NODE: of type synctex_node_p
1034 */
1035 # define SYNCTEX_DATA(NODE) ((*((((NODE)->class_))->info))(NODE))
1036 #if defined SYNCTEX_DEBUG > 1000
1037 # define DEFINE_SYNCTEX_DATA_HAS(WHAT) \
1038 SYNCTEX_INLINE static synctex_bool_t __synctex_data_has_##WHAT(synctex_node_p node) {\
1039 return (node && (node->class_->modelator->WHAT>=0));\
1040 }\
1041 SYNCTEX_INLINE static synctex_bool_t _synctex_data_has_##WHAT(synctex_node_p node) {\
1042 if (node && (node->class_->modelator->WHAT<0)) {\
1043 printf("WARNING: NO %s for %s\n", #WHAT, synctex_node_isa(node));\
1044 }\
1045 return __synctex_data_has_##WHAT(node);\
1046 }
1047 #else
1048 # define DEFINE_SYNCTEX_DATA_HAS(WHAT) \
1049 SYNCTEX_INLINE static synctex_bool_t __synctex_data_has_##WHAT(synctex_node_p node) {\
1050 return (node && (node->class_->modelator->WHAT>=0));\
1051 }\
1052 SYNCTEX_INLINE static synctex_bool_t _synctex_data_has_##WHAT(synctex_node_p node) {\
1053 return __synctex_data_has_##WHAT(node);\
1054 }
1055 #endif
1056
1057 SYNCTEX_INLINE static synctex_data_p __synctex_data(synctex_node_p node) {
1058 return node->data+node->class_->navigator->size;
1059 }
1060 # define DEFINE_SYNCTEX_DATA_INT_GETSET(WHAT) \
1061 DEFINE_SYNCTEX_DATA_HAS(WHAT)\
1062 static int _synctex_data_##WHAT(synctex_node_p node) {\
1063 if (_synctex_data_has_##WHAT(node)) {\
1064 return __synctex_data(node)[node->class_->modelator->WHAT].as_integer;\
1065 }\
1066 return 0;\
1067 }\
1068 static int _synctex_data_set_##WHAT(synctex_node_p node, int new_value) {\
1069 int old = 0;\
1070 if (_synctex_data_has_##WHAT(node)) {\
1071 old = __synctex_data(node)[node->class_->modelator->WHAT].as_integer;\
1072 __synctex_data(node)[node->class_->modelator->WHAT].as_integer=new_value;\
1073 }\
1074 return old;\
1075 }
1076 #define DEFINE_SYNCTEX_DATA_INT_DECODE(WHAT) \
1077 static synctex_status_t _synctex_data_decode_##WHAT(synctex_node_p node) {\
1078 if (_synctex_data_has_##WHAT(node)) {\
1079 synctex_is_s is = _synctex_decode_int(node->class_->scanner);\
1080 if (is.status == SYNCTEX_STATUS_OK) {\
1081 _synctex_data_set_##WHAT(node,is.integer);\
1082 } \
1083 return is.status;\
1084 }\
1085 return SYNCTEX_STATUS_BAD_ARGUMENT;\
1086 }
1087 # define DEFINE_SYNCTEX_DATA_INT_DECODE_v(WHAT) \
1088 static synctex_status_t _synctex_data_decode_##WHAT##_v(synctex_node_p node) {\
1089 if (_synctex_data_has_##WHAT(node)) {\
1090 synctex_is_s is = _synctex_decode_int_v(node->class_->scanner);\
1091 if (is.status == SYNCTEX_STATUS_OK) {\
1092 _synctex_data_set_##WHAT(node,is.integer);\
1093 } \
1094 return is.status;\
1095 }\
1096 return SYNCTEX_STATUS_BAD_ARGUMENT;\
1097 }
1098 #define DEFINE_SYNCTEX_DATA_STR_GETSET(WHAT) \
1099 DEFINE_SYNCTEX_DATA_HAS(WHAT)\
1100 static char * _synctex_data_##WHAT(synctex_node_p node) {\
1101 if (_synctex_data_has_##WHAT(node)) {\
1102 return node->data[node->class_->navigator->size+node->class_->modelator->WHAT].as_string;\
1103 }\
1104 return NULL;\
1105 }\
1106 static char * _synctex_data_set_##WHAT(synctex_node_p node, char * new_value) {\
1107 char * old = "";\
1108 if (_synctex_data_has_##WHAT(node)) {\
1109 old = node->data[node->class_->navigator->size+node->class_->modelator->WHAT].as_string;\
1110 node->data[node->class_->navigator->size+node->class_->modelator->WHAT].as_string =new_value;\
1111 }\
1112 return old;\
1113 }
1114 #define DEFINE_SYNCTEX_DATA_STR_DECODE(WHAT) \
1115 static synctex_status_t _synctex_data_decode_##WHAT(synctex_node_p node) {\
1116 if (_synctex_data_has_##WHAT(node)) {\
1117 synctex_ss_s ss = _synctex_decode_string(node->class_->scanner);\
1118 if (ss.status == SYNCTEX_STATUS_OK) {\
1119 _synctex_data_set_##WHAT(node,ss.string);\
1120 } \
1121 return ss.status;\
1122 }\
1123 return SYNCTEX_STATUS_BAD_ARGUMENT;\
1124 }
1125 #define DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(WHAT) \
1126 DEFINE_SYNCTEX_DATA_INT_GETSET(WHAT) \
1127 DEFINE_SYNCTEX_DATA_INT_DECODE(WHAT)
1128 #define DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE_v(WHAT) \
1129 DEFINE_SYNCTEX_DATA_INT_GETSET(WHAT) \
1130 DEFINE_SYNCTEX_DATA_INT_DECODE_v(WHAT)
1131 #define DEFINE_SYNCTEX_DATA_STR_GETSET_DECODE(WHAT) \
1132 DEFINE_SYNCTEX_DATA_STR_GETSET(WHAT) \
1133 DEFINE_SYNCTEX_DATA_STR_DECODE(WHAT)
1134
1135 # ifdef SYNCTEX_NOTHING
1136 # pragma mark -
1137 # pragma mark OBJECTS, their creators and destructors.
1138 # endif
1139
1140 # ifdef SYNCTEX_NOTHING
1141 # pragma mark input.
1142 # endif
1143
1144 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(tag)
1145 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(line)
1146 DEFINE_SYNCTEX_DATA_STR_GETSET_DECODE(name)
1147
1148 /* Input nodes only know about their sibling, which is another input node.
1149 * The synctex information is the _synctex_data_tag and _synctex_data_name
1150 * note: the input owns its name. */
1151
1152 # define SYNCTEX_INPUT_MARK "Input:"
1153
1154 static const synctex_tree_model_s synctex_tree_model_input = {
1155 synctex_tree_sibling_idx, /* sibling */
1156 -1, /* parent */
1157 -1, /* child */
1158 -1, /* friend */
1159 -1, /* last */
1160 -1, /* next_hbox */
1161 -1, /* arg_sibling */
1162 -1, /* target */
1163 synctex_tree_s_input_max
1164 };
1165 static const synctex_data_model_s synctex_data_model_input = {
1166 synctex_data_input_tag_idx, /* tag */
1167 synctex_data_input_line_idx,/* line */
1168 -1, /* column */
1169 -1, /* h */
1170 -1, /* v */
1171 -1, /* width */
1172 -1, /* height */
1173 -1, /* depth */
1174 -1, /* mean_line */
1175 -1, /* weight */
1176 -1, /* h_V */
1177 -1, /* v_V */
1178 -1, /* width_V */
1179 -1, /* height_V */
1180 -1, /* depth_V */
1181 synctex_data_input_name_idx, /* name */
1182 -1, /* page */
1183 synctex_data_input_tln_max
1184 };
1185
1186 #define SYNCTEX_INSPECTOR_GETTER_F(WHAT)\
1187 &_synctex_data_##WHAT, &_synctex_data_set_##WHAT
1188
1189 static synctex_node_p _synctex_new_input(synctex_scanner_p scanner);
1190 static void _synctex_free_input(synctex_node_p node);
1191 static void _synctex_log_input(synctex_node_p node);
1192 static char * _synctex_abstract_input(synctex_node_p node);
1193 static void _synctex_display_input(synctex_node_p node);
1194
1195 static const synctex_tlcpector_s synctex_tlcpector_input = {
1196 &_synctex_data_tag, /* tag */
1197 &_synctex_int_none, /* line */
1198 &_synctex_int_none, /* column */
1199 };
1200
1201 static synctex_class_s synctex_class_input = {
1202 NULL, /* No scanner yet */
1203 synctex_node_type_input, /* Node type */
1204 &_synctex_new_input, /* creator */
1205 &_synctex_free_input, /* destructor */
1206 &_synctex_log_input, /* log */
1207 &_synctex_display_input, /* display */
1208 &_synctex_abstract_input, /* abstract */
1209 &synctex_tree_model_input, /* tree model */
1210 &synctex_data_model_input, /* data model */
1211 &synctex_tlcpector_input, /* inspector */
1212 &synctex_inspector_none, /* inspector */
1213 &synctex_vispector_none, /* vispector */
1214 };
1215
1216 typedef struct {
1217 SYNCTEX_DECLARE_CHARINDEX
1218 synctex_class_p class_;
1219 synctex_data_u data[synctex_tree_s_input_max+synctex_data_input_tln_max];
1220 } synctex_input_s;
1221
1222 static synctex_node_p _synctex_new_input(synctex_scanner_p scanner) {
1223 if (scanner) {
1224 synctex_node_p node = _synctex_malloc(sizeof(synctex_input_s));
1225 if (node) {
1226 node->class_ = scanner->class_+synctex_node_type_input;
1227 SYNCTEX_DID_NEW(node);
1228 SYNCTEX_IMPLEMENT_CHARINDEX(node,0);
1229 SYNCTEX_REGISTER_HANDLE_TO(node);
1230 }
1231 return node;
1232 }
1233 return NULL;
1234 }
1235
1236 static void _synctex_free_input(synctex_node_p node){
1237 if (node) {
1238 synctex_node_p sibling;
1239 SYNCTEX_SCANNER_REMOVE_HANDLE_TO(node);
1240 SYNCTEX_WILL_FREE(node);
1241 sibling = __synctex_tree_reset_sibling(node);
1242 synctex_node_free(sibling);
1243 _synctex_free(_synctex_data_name(node));
1244 _synctex_free(node);
1245 }
1246 }
1247
1248 /* The sheet is a first level node.
1249 * It has no parent (the owner is the scanner itself)
1250 * Its sibling points to another sheet.
1251 * Its child points to its first child, in general a box.
1252 * A sheet node contains only one synctex information: the page.
1253 * This is the 1 based page index as given by TeX.
1254 */
1255
1256 # ifdef SYNCTEX_NOTHING
1257 # pragma mark sheet.
1258 # endif
1259 /**
1260 * Every node has the same structure, but not the same size.
1261 */
1262
1263 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(page)
1264
1265 typedef struct {
1266 SYNCTEX_DECLARE_CHARINDEX
1267 synctex_class_p class_;
1268 synctex_data_u data[synctex_tree_scn_sheet_max+synctex_data_p_sheet_max];
1269 } synctex_node_sheet_s;
1270
1271 /* sheet node creator */
1272
1273 #define DEFINE_synctex_new_scanned_NODE(NAME)\
1274 static synctex_node_p _synctex_new_##NAME(synctex_scanner_p scanner) {\
1275 if (scanner) {\
1276 ++SYNCTEX_CUR;\
1277 synctex_node_p node = _synctex_malloc(sizeof(synctex_node_##NAME##_s));\
1278 if (node) {\
1279 node->class_ = scanner->class_+synctex_node_type_##NAME;\
1280 SYNCTEX_DID_NEW(node); \
1281 SYNCTEX_IMPLEMENT_CHARINDEX(node,-1);\
1282 SYNCTEX_REGISTER_HANDLE_TO(node); \
1283 }\
1284 return node;\
1285 }\
1286 return NULL;\
1287 }
1288 /* NB: -1 in SYNCTEX_IMPLEMENT_CHARINDEX above because
1289 * the first char of the line has been scanned
1290 */
1291 DEFINE_synctex_new_scanned_NODE(sheet)
1292 static void _synctex_log_sheet(synctex_node_p node);
1293 static char * _synctex_abstract_sheet(synctex_node_p node);
1294 static void _synctex_display_sheet(synctex_node_p node);
1295
1296 static const synctex_tree_model_s synctex_tree_model_sheet = {
1297 synctex_tree_sibling_idx, /* sibling */
1298 -1, /* parent */
1299 synctex_tree_s_child_idx, /* child */
1300 -1, /* friend */
1301 -1, /* last */
1302 synctex_tree_sc_next_hbox_idx, /* next_hbox */
1303 -1, /* arg_sibling */
1304 -1, /* target */
1305 synctex_tree_scn_sheet_max
1306 };
1307 static const synctex_data_model_s synctex_data_model_sheet = {
1308 -1, /* tag */
1309 -1, /* line */
1310 -1, /* column */
1311 -1, /* h */
1312 -1, /* v */
1313 -1, /* width */
1314 -1, /* height */
1315 -1, /* depth */
1316 -1, /* mean_line */
1317 -1, /* weight */
1318 -1, /* h_V */
1319 -1, /* v_V */
1320 -1, /* width_V */
1321 -1, /* height_V */
1322 -1, /* depth_V */
1323 -1, /* name */
1324 synctex_data_sheet_page_idx, /* page */
1325 synctex_data_p_sheet_max
1326 };
1327 static synctex_class_s synctex_class_sheet = {
1328 NULL, /* No scanner yet */
1329 synctex_node_type_sheet, /* Node type */
1330 &_synctex_new_sheet, /* creator */
1331 &_synctex_free_node, /* destructor */
1332 &_synctex_log_sheet, /* log */
1333 &_synctex_display_sheet, /* display */
1334 &_synctex_abstract_sheet, /* abstract */
1335 &synctex_tree_model_sheet, /* tree model */
1336 &synctex_data_model_sheet, /* data model */
1337 &synctex_tlcpector_none, /* tlcpector */
1338 &synctex_inspector_none, /* inspector */
1339 &synctex_vispector_none, /* vispector */
1340 };
1341
1342 # ifdef SYNCTEX_NOTHING
1343 # pragma mark form.
1344 # endif
1345 /**
1346 * Every node has the same structure, but not the same size.
1347 */
1348 typedef struct {
1349 SYNCTEX_DECLARE_CHARINDEX
1350 synctex_class_p class_;
1351 synctex_data_u data[synctex_tree_sct_form_max+synctex_data_t_form_max];
1352 } synctex_node_form_s;
1353
1354 DEFINE_synctex_new_scanned_NODE(form)
1355
1356 static char * _synctex_abstract_form(synctex_node_p node);
1357 static void _synctex_display_form(synctex_node_p node);
1358 static void _synctex_log_form(synctex_node_p node);
1359
1360 static const synctex_tree_model_s synctex_tree_model_form = {
1361 synctex_tree_sibling_idx, /* sibling */
1362 -1, /* parent */
1363 synctex_tree_s_child_idx, /* child */
1364 -1, /* friend */
1365 -1, /* last */
1366 -1, /* next_hbox */
1367 -1, /* arg_sibling */
1368 synctex_tree_sc_target_idx, /* target */
1369 synctex_tree_sct_form_max
1370 };
1371 static const synctex_data_model_s synctex_data_model_form = {
1372 synctex_data_form_tag_idx, /* tag */
1373 -1, /* line */
1374 -1, /* column */
1375 -1, /* h */
1376 -1, /* v */
1377 -1, /* width */
1378 -1, /* height */
1379 -1, /* depth */
1380 -1, /* mean_line */
1381 -1, /* weight */
1382 -1, /* h_V */
1383 -1, /* v_V */
1384 -1, /* width_V */
1385 -1, /* height_V */
1386 -1, /* depth_V */
1387 -1, /* name */
1388 -1, /* page */
1389 synctex_data_t_form_max
1390 };
1391 static synctex_class_s synctex_class_form = {
1392 NULL, /* No scanner yet */
1393 synctex_node_type_form, /* Node type */
1394 &_synctex_new_form, /* creator */
1395 &_synctex_free_node, /* destructor */
1396 &_synctex_log_form, /* log */
1397 &_synctex_display_form, /* display */
1398 &_synctex_abstract_form, /* abstract */
1399 &synctex_tree_model_form, /* tree model */
1400 &synctex_data_model_form, /* data model */
1401 &synctex_tlcpector_none, /* tlcpector */
1402 &synctex_inspector_none, /* inspector */
1403 &synctex_vispector_none, /* vispector */
1404 };
1405
1406 # ifdef SYNCTEX_NOTHING
1407 # pragma mark vbox.
1408 # endif
1409
1410 /* A box node contains navigation and synctex information
1411 * There are different kinds of boxes.
1412 * Only horizontal boxes are treated differently because of their visible size.
1413 */
1414 typedef struct {
1415 SYNCTEX_DECLARE_CHARINDEX
1416 synctex_class_p class_;
1417 synctex_data_u data[synctex_tree_spcfl_vbox_max+synctex_data_box_max];
1418 } synctex_node_vbox_s;
1419
1420 /* vertical box node creator */
1421 DEFINE_synctex_new_scanned_NODE(vbox)
1422
1423 static char * _synctex_abstract_vbox(synctex_node_p node);
1424 static void _synctex_display_vbox(synctex_node_p node);
1425 static void _synctex_log_vbox(synctex_node_p node);
1426
1427 static const synctex_tree_model_s synctex_tree_model_vbox = {
1428 synctex_tree_sibling_idx, /* sibling */
1429 synctex_tree_s_parent_idx, /* parent */
1430 synctex_tree_sp_child_idx, /* child */
1431 synctex_tree_spc_friend_idx, /* friend */
1432 synctex_tree_spcf_last_idx, /* last */
1433 -1, /* next_hbox */
1434 -1, /* arg_sibling */
1435 -1, /* target */
1436 synctex_tree_spcfl_vbox_max
1437 };
1438
1439 #define SYNCTEX_DFLT_COLUMN -1
1440
1441 DEFINE_SYNCTEX_DATA_INT_GETSET(column)
1442 static synctex_status_t _synctex_data_decode_column(synctex_node_p node) {
1443 if (_synctex_data_has_column(node)) {
1444 synctex_is_s is = _synctex_decode_int_opt(node->class_->scanner,
1445 SYNCTEX_DFLT_COLUMN);
1446 if (is.status == SYNCTEX_STATUS_OK) {
1447 _synctex_data_set_column(node,is.integer);
1448 }
1449 return is.status;
1450 }
1451 return SYNCTEX_STATUS_BAD_ARGUMENT;
1452 }
1453 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(h)
1454 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE_v(v)
1455 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(width)
1456 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(height)
1457 DEFINE_SYNCTEX_DATA_INT_GETSET_DECODE(depth)
1458
1459 SYNCTEX_INLINE static void _synctex_data_set_tlc(synctex_node_p node, synctex_node_p model) {
1460 _synctex_data_set_tag(node, _synctex_data_tag(model));
1461 _synctex_data_set_line(node, _synctex_data_line(model));
1462 _synctex_data_set_column(node, _synctex_data_column(model));
1463 }
1464 SYNCTEX_INLINE static void _synctex_data_set_tlchv(synctex_node_p node, synctex_node_p model) {
1465 _synctex_data_set_tlc(node,model);
1466 _synctex_data_set_h(node, _synctex_data_h(model));
1467 _synctex_data_set_v(node, _synctex_data_v(model));
1468 }
1469
1470 static const synctex_data_model_s synctex_data_model_box = {
1471 synctex_data_tag_idx, /* tag */
1472 synctex_data_line_idx, /* line */
1473 synctex_data_column_idx,/* column */
1474 synctex_data_h_idx, /* h */
1475 synctex_data_v_idx, /* v */
1476 synctex_data_width_idx, /* width */
1477 synctex_data_height_idx,/* height */
1478 synctex_data_depth_idx, /* depth */
1479 -1, /* mean_line */
1480 -1, /* weight */
1481 -1, /* h_V */
1482 -1, /* v_V */
1483 -1, /* width_V */
1484 -1, /* height_V */
1485 -1, /* depth_V */
1486 -1, /* name */
1487 -1, /* page */
1488 synctex_data_box_max
1489 };
1490 static const synctex_tlcpector_s synctex_tlcpector_default = {
1491 &_synctex_data_tag, /* tag */
1492 &_synctex_data_line, /* line */
1493 &_synctex_data_column, /* column */
1494 };
1495 static const synctex_inspector_s synctex_inspector_box = {
1496 &_synctex_data_h,
1497 &_synctex_data_v,
1498 &_synctex_data_width,
1499 &_synctex_data_height,
1500 &_synctex_data_depth,
1501 };
1502 static float __synctex_node_visible_h(synctex_node_p node);
1503 static float __synctex_node_visible_v(synctex_node_p node);
1504 static float __synctex_node_visible_width(synctex_node_p node);
1505 static float __synctex_node_visible_height(synctex_node_p node);
1506 static float __synctex_node_visible_depth(synctex_node_p node);
1507 static synctex_vispector_s synctex_vispector_box = {
1508 &__synctex_node_visible_h,
1509 &__synctex_node_visible_v,
1510 &__synctex_node_visible_width,
1511 &__synctex_node_visible_height,
1512 &__synctex_node_visible_depth,
1513 };
1514 /* These are static class objects, each scanner will make a copy of them and setup the scanner field.
1515 */
1516 static synctex_class_s synctex_class_vbox = {
1517 NULL, /* No scanner yet */
1518 synctex_node_type_vbox, /* Node type */
1519 &_synctex_new_vbox, /* creator */
1520 &_synctex_free_node, /* destructor */
1521 &_synctex_log_vbox, /* log */
1522 &_synctex_display_vbox, /* display */
1523 &_synctex_abstract_vbox, /* abstract */
1524 &synctex_tree_model_vbox, /* tree model */
1525 &synctex_data_model_box, /* data model */
1526 &synctex_tlcpector_default, /* tlcpector */
1527 &synctex_inspector_box, /* inspector */
1528 &synctex_vispector_box, /* vispector */
1529 };
1530
1531 # ifdef SYNCTEX_NOTHING
1532 # pragma mark hbox.
1533 # endif
1534
1535 /* Horizontal boxes must contain visible size, because 0 width does not mean emptiness.
1536 * They also contain an average of the line numbers of the containing nodes. */
1537
1538 static const synctex_tree_model_s synctex_tree_model_hbox = {
1539 synctex_tree_sibling_idx, /* sibling */
1540 synctex_tree_s_parent_idx, /* parent */
1541 synctex_tree_sp_child_idx, /* child */
1542 synctex_tree_spc_friend_idx, /* friend */
1543 synctex_tree_spcf_last_idx, /* last */
1544 synctex_tree_spcfl_next_hbox_idx, /* next_hbox */
1545 -1, /* arg_sibling */
1546 -1, /* target */
1547 synctex_tree_spcfln_hbox_max
1548 };
1549
1550 DEFINE_SYNCTEX_DATA_INT_GETSET(mean_line)
1551 DEFINE_SYNCTEX_DATA_INT_GETSET(weight)
1552 DEFINE_SYNCTEX_DATA_INT_GETSET(h_V)
1553 DEFINE_SYNCTEX_DATA_INT_GETSET(v_V)
1554 DEFINE_SYNCTEX_DATA_INT_GETSET(width_V)
1555 DEFINE_SYNCTEX_DATA_INT_GETSET(height_V)
1556 DEFINE_SYNCTEX_DATA_INT_GETSET(depth_V)
1557
1558 /**
1559 * The hbox model.
1560 * It contains V variants of geometrical information.
1561 * It happens that hboxes contain material that is not used to compute
1562 * the bounding box. Some letters may appear out of the box given by TeX.
1563 * In such a situation, the visible bouding box is bigger ence the V variant.
1564 * Only hboxes have such variant. It does not make sense for void boxes
1565 * and it is not used here for vboxes.
1566 * - author: JL
1567 */
1568
1569 static const synctex_data_model_s synctex_data_model_hbox = {
1570 synctex_data_tag_idx, /* tag */
1571 synctex_data_line_idx, /* line */
1572 synctex_data_column_idx,/* column */
1573 synctex_data_h_idx, /* h */
1574 synctex_data_v_idx, /* v */
1575 synctex_data_width_idx, /* width */
1576 synctex_data_height_idx,/* height */
1577 synctex_data_depth_idx, /* depth */
1578 synctex_data_mean_line_idx, /* mean_line */
1579 synctex_data_weight_idx, /* weight */
1580 synctex_data_h_V_idx, /* h_V */
1581 synctex_data_v_V_idx, /* v_V */
1582 synctex_data_width_V_idx, /* width_V */
1583 synctex_data_height_V_idx, /* height_V */
1584 synctex_data_depth_V_idx, /* depth_V */
1585 -1, /* name */
1586 -1, /* page */
1587 synctex_data_hbox_max
1588 };
1589
1590 typedef struct {
1591 SYNCTEX_DECLARE_CHARINDEX
1592 synctex_class_p class_;
1593 synctex_data_u data[synctex_tree_spcfln_hbox_max+synctex_data_hbox_max];
1594 } synctex_node_hbox_s;
1595
1596 /* horizontal box node creator */
1597 DEFINE_synctex_new_scanned_NODE(hbox)
1598
1599 static void _synctex_log_hbox(synctex_node_p node);
1600 static char * _synctex_abstract_hbox(synctex_node_p node);
1601 static void _synctex_display_hbox(synctex_node_p node);
1602
1603 static synctex_class_s synctex_class_hbox = {
1604 NULL, /* No scanner yet */
1605 synctex_node_type_hbox, /* Node type */
1606 &_synctex_new_hbox, /* creator */
1607 &_synctex_free_node, /* destructor */
1608 &_synctex_log_hbox, /* log */
1609 &_synctex_display_hbox, /* display */
1610 &_synctex_abstract_hbox, /* abstract */
1611 &synctex_tree_model_hbox, /* tree model */
1612 &synctex_data_model_hbox, /* data model */
1613 &synctex_tlcpector_default, /* tlcpector */
1614 &synctex_inspector_box, /* inspector */
1615 &synctex_vispector_box, /* vispector */
1616 };
1617
1618 # ifdef SYNCTEX_NOTHING
1619 # pragma mark void vbox.
1620 # endif
1621
1622 /* This void box node implementation is either horizontal or vertical
1623 * It does not contain a child field.
1624 */
1625 static const synctex_tree_model_s synctex_tree_model_spf = {
1626 synctex_tree_sibling_idx, /* sibling */
1627 synctex_tree_s_parent_idx, /* parent */
1628 -1, /* child */
1629 synctex_tree_sp_friend_idx, /* friend */
1630 -1, /* last */
1631 -1, /* next_hbox */
1632 -1, /* arg_sibling */
1633 -1, /* target */
1634 synctex_tree_spf_max
1635 };
1636 typedef struct {
1637 SYNCTEX_DECLARE_CHARINDEX
1638 synctex_class_p class_;
1639 synctex_data_u data[synctex_tree_spf_max+synctex_data_box_max];
1640 } synctex_node_void_vbox_s;
1641
1642 /* vertical void box node creator */
1643 DEFINE_synctex_new_scanned_NODE(void_vbox)
1644
1645 static void _synctex_log_void_box(synctex_node_p node);
1646 static char * _synctex_abstract_void_vbox(synctex_node_p node);
1647 static void _synctex_display_void_vbox(synctex_node_p node);
1648
1649 static synctex_class_s synctex_class_void_vbox = {
1650 NULL, /* No scanner yet */
1651 synctex_node_type_void_vbox,/* Node type */
1652 &_synctex_new_void_vbox, /* creator */
1653 &_synctex_free_leaf, /* destructor */
1654 &_synctex_log_void_box, /* log */
1655 &_synctex_display_void_vbox,/* display */
1656 &_synctex_abstract_void_vbox,/* abstract */
1657 &synctex_tree_model_spf, /* tree model */
1658 &synctex_data_model_box, /* data model */
1659 &synctex_tlcpector_default, /* tlcpector */
1660 &synctex_inspector_box, /* inspector */
1661 &synctex_vispector_box, /* vispector */
1662 };
1663
1664 # ifdef SYNCTEX_NOTHING
1665 # pragma mark void hbox.
1666 # endif
1667
1668 typedef synctex_node_void_vbox_s synctex_node_void_hbox_s;
1669
1670 /* horizontal void box node creator */
1671 DEFINE_synctex_new_scanned_NODE(void_hbox)
1672
1673 static char * _synctex_abstract_void_hbox(synctex_node_p node);
1674 static void _synctex_display_void_hbox(synctex_node_p node);
1675
1676 static synctex_class_s synctex_class_void_hbox = {
1677 NULL, /* No scanner yet */
1678 synctex_node_type_void_hbox,/* Node type */
1679 &_synctex_new_void_hbox, /* creator */
1680 &_synctex_free_leaf, /* destructor */
1681 &_synctex_log_void_box, /* log */
1682 &_synctex_display_void_hbox,/* display */
1683 &_synctex_abstract_void_hbox,/* abstract */
1684 &synctex_tree_model_spf, /* tree model */
1685 &synctex_data_model_box, /* data model */
1686 &synctex_tlcpector_default, /* tlcpector */
1687 &synctex_inspector_box, /* inspector */
1688 &synctex_vispector_box, /* vispector */
1689 };
1690
1691 # ifdef SYNCTEX_NOTHING
1692 # pragma mark form ref.
1693 # endif
1694
1695 /* The form ref node. */
1696 typedef struct {
1697 SYNCTEX_DECLARE_CHARINDEX
1698 synctex_class_p class_;
1699 synctex_data_u data[synctex_tree_spfa_max+synctex_data_ref_thv_max];
1700 } synctex_node_ref_s;
1701
1702 /* form ref node creator */
1703 DEFINE_synctex_new_scanned_NODE(ref)
1704
1705 static void _synctex_log_ref(synctex_node_p node);
1706 static char * _synctex_abstract_ref(synctex_node_p node);
1707 static void _synctex_display_ref(synctex_node_p node);
1708
1709 static const synctex_tree_model_s synctex_tree_model_spfa = {
1710 synctex_tree_sibling_idx, /* sibling */
1711 synctex_tree_s_parent_idx, /* parent */
1712 -1, /* child */
1713 synctex_tree_sp_friend_idx, /* friend */
1714 -1, /* last */
1715 -1, /* next_hbox */
1716 synctex_tree_spf_arg_sibling_idx, /* arg_sibling */
1717 -1, /* target */
1718 synctex_tree_spfa_max
1719 };
1720 static const synctex_data_model_s synctex_data_model_ref = {
1721 synctex_data_tag_idx, /* tag */
1722 -1, /* line */
1723 -1, /* column */
1724 synctex_data_ref_h_idx, /* h */
1725 synctex_data_ref_v_idx, /* v */
1726 -1, /* width */
1727 -1, /* height */
1728 -1, /* depth */
1729 -1, /* mean_line */
1730 -1, /* weight */
1731 -1, /* h_V */
1732 -1, /* v_V */
1733 -1, /* width_V */
1734 -1, /* height_V */
1735 -1, /* depth_V */
1736 -1, /* name */
1737 -1, /* page */
1738 synctex_data_ref_thv_max /* size */
1739 };
1740 static synctex_class_s synctex_class_ref = {
1741 NULL, /* No scanner yet */
1742 synctex_node_type_ref, /* Node type */
1743 &_synctex_new_ref, /* creator */
1744 &_synctex_free_leaf, /* destructor */
1745 &_synctex_log_ref, /* log */
1746 &_synctex_display_ref, /* display */
1747 &_synctex_abstract_ref, /* abstract */
1748 &synctex_tree_model_spfa, /* navigator */
1749 &synctex_data_model_ref, /* data model */
1750 &synctex_tlcpector_none, /* tlcpector */
1751 &synctex_inspector_none, /* inspector */
1752 &synctex_vispector_none, /* vispector */
1753 };
1754 # ifdef SYNCTEX_NOTHING
1755 # pragma mark small node.
1756 # endif
1757
1758 /* The small nodes correspond to glue, penalty, math and boundary nodes. */
1759 static const synctex_data_model_s synctex_data_model_tlchv = {
1760 synctex_data_tag_idx, /* tag */
1761 synctex_data_line_idx, /* line */
1762 synctex_data_column_idx, /* column */
1763 synctex_data_h_idx, /* h */
1764 synctex_data_v_idx, /* v */
1765 -1, /* width */
1766 -1, /* height */
1767 -1, /* depth */
1768 -1, /* mean_line */
1769 -1, /* weight */
1770 -1, /* h_V */
1771 -1, /* v_V */
1772 -1, /* width_V */
1773 -1, /* height_V */
1774 -1, /* depth_V */
1775 -1, /* name */
1776 -1, /* page */
1777 synctex_data_tlchv_max
1778 };
1779
1780 typedef struct {
1781 SYNCTEX_DECLARE_CHARINDEX
1782 synctex_class_p class_;
1783 synctex_data_u data[synctex_tree_spf_max+synctex_data_tlchv_max];
1784 } synctex_node_tlchv_s;
1785
1786 static void _synctex_log_tlchv_node(synctex_node_p node);
1787
1788 # ifdef SYNCTEX_NOTHING
1789 # pragma mark math.
1790 # endif
1791
1792 typedef synctex_node_tlchv_s synctex_node_math_s;
1793
1794 /* math node creator */
1795 DEFINE_synctex_new_scanned_NODE(math)
1796
1797 static char * _synctex_abstract_math(synctex_node_p node);
1798 static void _synctex_display_math(synctex_node_p node);
1799 static synctex_inspector_s synctex_inspector_hv = {
1800 &_synctex_data_h,
1801 &_synctex_data_v,
1802 &_synctex_int_none,
1803 &_synctex_int_none,
1804 &_synctex_int_none,
1805 };
1806 static synctex_vispector_s synctex_vispector_hv = {
1807 &__synctex_node_visible_h,
1808 &__synctex_node_visible_v,
1809 &_synctex_float_none,
1810 &_synctex_float_none,
1811 &_synctex_float_none,
1812 };
1813
1814 static synctex_class_s synctex_class_math = {
1815 NULL, /* No scanner yet */
1816 synctex_node_type_math, /* Node type */
1817 &_synctex_new_math, /* creator */
1818 &_synctex_free_leaf, /* destructor */
1819 &_synctex_log_tlchv_node, /* log */
1820 &_synctex_display_math, /* display */
1821 &_synctex_abstract_math, /* abstract */
1822 &synctex_tree_model_spf, /* tree model */
1823 &synctex_data_model_tlchv, /* data model */
1824 &synctex_tlcpector_default, /* tlcpector */
1825 &synctex_inspector_hv, /* inspector */
1826 &synctex_vispector_hv, /* vispector */
1827 };
1828
1829 # ifdef SYNCTEX_NOTHING
1830 # pragma mark kern node.
1831 # endif
1832
1833 static const synctex_data_model_s synctex_data_model_tlchvw = {
1834 synctex_data_tag_idx, /* tag */
1835 synctex_data_line_idx, /* line */
1836 synctex_data_column_idx,/* column */
1837 synctex_data_h_idx, /* h */
1838 synctex_data_v_idx, /* v */
1839 synctex_data_width_idx, /* width */
1840 -1, /* height */
1841 -1, /* depth */
1842 -1, /* mean_line */
1843 -1, /* weight */
1844 -1, /* h_V */
1845 -1, /* v_V */
1846 -1, /* width_V */
1847 -1, /* height_V */
1848 -1, /* depth_V */
1849 -1, /* name */
1850 -1, /* page */
1851 synctex_data_tlchvw_max
1852 };
1853 typedef struct {
1854 SYNCTEX_DECLARE_CHARINDEX
1855 synctex_class_p class_;
1856 synctex_data_u data[synctex_tree_spf_max+synctex_data_tlchvw_max];
1857 } synctex_node_kern_s;
1858
1859 /* kern node creator */
1860 DEFINE_synctex_new_scanned_NODE(kern)
1861
1862 static void _synctex_log_kern_node(synctex_node_p node);
1863 static char * _synctex_abstract_kern(synctex_node_p node);
1864 static void _synctex_display_kern(synctex_node_p node);
1865
1866 static synctex_inspector_s synctex_inspector_kern = {
1867 &_synctex_data_h,
1868 &_synctex_data_v,
1869 &_synctex_data_width,
1870 &_synctex_int_none,
1871 &_synctex_int_none,
1872 };
1873 static float __synctex_kern_visible_h(synctex_node_p node);
1874 static float __synctex_kern_visible_width(synctex_node_p node);
1875 static synctex_vispector_s synctex_vispector_kern = {
1876 &__synctex_kern_visible_h,
1877 &__synctex_node_visible_v,
1878 &__synctex_kern_visible_width,
1879 &_synctex_float_none,
1880 &_synctex_float_none,
1881 };
1882
1883 static synctex_class_s synctex_class_kern = {
1884 NULL, /* No scanner yet */
1885 synctex_node_type_kern, /* Node type */
1886 &_synctex_new_kern, /* creator */
1887 &_synctex_free_leaf, /* destructor */
1888 &_synctex_log_kern_node, /* log */
1889 &_synctex_display_kern, /* display */
1890 &_synctex_abstract_kern, /* abstract */
1891 &synctex_tree_model_spf, /* tree model */
1892 &synctex_data_model_tlchvw, /* data model */
1893 &synctex_tlcpector_default, /* tlcpector */
1894 &synctex_inspector_kern, /* inspector */
1895 &synctex_vispector_kern, /* vispector */
1896 };
1897
1898 # ifdef SYNCTEX_NOTHING
1899 # pragma mark glue.
1900 # endif
1901
1902 /* glue node creator */
1903 typedef synctex_node_tlchv_s synctex_node_glue_s;
1904 DEFINE_synctex_new_scanned_NODE(glue)
1905
1906 static char * _synctex_abstract_glue(synctex_node_p node);
1907 static void _synctex_display_glue(synctex_node_p node);
1908
1909 static synctex_class_s synctex_class_glue = {
1910 NULL, /* No scanner yet */
1911 synctex_node_type_glue, /* Node type */
1912 &_synctex_new_glue, /* creator */
1913 &_synctex_free_leaf, /* destructor */
1914 &_synctex_log_tlchv_node, /* log */
1915 &_synctex_display_glue, /* display */
1916 &_synctex_abstract_glue, /* abstract */
1917 &synctex_tree_model_spf, /* tree model */
1918 &synctex_data_model_tlchv, /* data model */
1919 &synctex_tlcpector_default, /* tlcpector */
1920 &synctex_inspector_hv, /* inspector */
1921 &synctex_vispector_hv, /* vispector */
1922 };
1923
1924 /* The small nodes correspond to glue and boundary nodes. */
1925
1926 # ifdef SYNCTEX_NOTHING
1927 # pragma mark rule.
1928 # endif
1929
1930 typedef struct {
1931 SYNCTEX_DECLARE_CHARINDEX
1932 synctex_class_p class_;
1933 synctex_data_u data[synctex_tree_spf_max+synctex_data_box_max];
1934 } synctex_node_rule_s;
1935
1936 DEFINE_synctex_new_scanned_NODE(rule)
1937
1938 static void _synctex_log_rule(synctex_node_p node);
1939 static char * _synctex_abstract_rule(synctex_node_p node);
1940 static void _synctex_display_rule(synctex_node_p node);
1941
1942 static float __synctex_rule_visible_h(synctex_node_p node);
1943 static float __synctex_rule_visible_v(synctex_node_p node);
1944 static float __synctex_rule_visible_width(synctex_node_p node);
1945 static float __synctex_rule_visible_height(synctex_node_p node);
1946 static float __synctex_rule_visible_depth(synctex_node_p node);
1947 static synctex_vispector_s synctex_vispector_rule = {
1948 &__synctex_rule_visible_h,
1949 &__synctex_rule_visible_v,
1950 &__synctex_rule_visible_width,
1951 &__synctex_rule_visible_height,
1952 &__synctex_rule_visible_depth,
1953 };
1954
1955 static synctex_class_s synctex_class_rule = {
1956 NULL, /* No scanner yet */
1957 synctex_node_type_rule, /* Node type */
1958 &_synctex_new_rule, /* creator */
1959 &_synctex_free_leaf, /* destructor */
1960 &_synctex_log_rule, /* log */
1961 &_synctex_display_rule, /* display */
1962 &_synctex_abstract_rule, /* abstract */
1963 &synctex_tree_model_spf, /* tree model */
1964 &synctex_data_model_box, /* data model */
1965 &synctex_tlcpector_default, /* tlcpector */
1966 &synctex_inspector_box, /* inspector */
1967 &synctex_vispector_rule, /* vispector */
1968 };
1969
1970 # ifdef SYNCTEX_NOTHING
1971 # pragma mark boundary.
1972 # endif
1973
1974 /* boundary node creator */
1975 typedef synctex_node_tlchv_s synctex_node_boundary_s;
1976 DEFINE_synctex_new_scanned_NODE(boundary)
1977
1978 static char * _synctex_abstract_boundary(synctex_node_p node);
1979 static void _synctex_display_boundary(synctex_node_p node);
1980
1981 static synctex_class_s synctex_class_boundary = {
1982 NULL, /* No scanner yet */
1983 synctex_node_type_boundary, /* Node type */
1984 &_synctex_new_boundary, /* creator */
1985 &_synctex_free_leaf, /* destructor */
1986 &_synctex_log_tlchv_node, /* log */
1987 &_synctex_display_boundary, /* display */
1988 &_synctex_abstract_boundary,/* abstract */
1989 &synctex_tree_model_spf, /* tree model */
1990 &synctex_data_model_tlchv, /* data model */
1991 &synctex_tlcpector_default, /* tlcpector */
1992 &synctex_inspector_hv, /* inspector */
1993 &synctex_vispector_hv, /* vispector */
1994 };
1995
1996 # ifdef SYNCTEX_NOTHING
1997 # pragma mark box boundary.
1998 # endif
1999
2000 typedef struct {
2001 SYNCTEX_DECLARE_CHARINDEX
2002 synctex_class_p class_;
2003 synctex_data_u data[synctex_tree_spfa_max+synctex_data_tlchv_max];
2004 } synctex_node_box_bdry_s;
2005
2006 #define DEFINE_synctex_new_unscanned_NODE(NAME)\
2007 SYNCTEX_INLINE static synctex_node_p _synctex_new_##NAME(synctex_scanner_p scanner) {\
2008 if (scanner) {\
2009 synctex_node_p node = _synctex_malloc(sizeof(synctex_node_##NAME##_s));\
2010 if (node) {\
2011 node->class_ = scanner->class_+synctex_node_type_##NAME;\
2012 SYNCTEX_DID_NEW(node); \
2013 }\
2014 return node;\
2015 }\
2016 return NULL;\
2017 }
2018 DEFINE_synctex_new_unscanned_NODE(box_bdry)
2019
2020 static char * _synctex_abstract_box_bdry(synctex_node_p node);
2021 static void _synctex_display_box_bdry(synctex_node_p node);
2022
2023 static synctex_class_s synctex_class_box_bdry = {
2024 NULL, /* No scanner yet */
2025 synctex_node_type_box_bdry, /* Node type */
2026 &_synctex_new_box_bdry, /* creator */
2027 &_synctex_free_leaf, /* destructor */
2028 &_synctex_log_tlchv_node, /* log */
2029 &_synctex_display_box_bdry, /* display */
2030 &_synctex_abstract_box_bdry,/* display */
2031 &synctex_tree_model_spfa, /* tree model */
2032 &synctex_data_model_tlchv, /* data model */
2033 &synctex_tlcpector_default, /* tlcpector */
2034 &synctex_inspector_hv, /* inspector */
2035 &synctex_vispector_hv, /* vispector */
2036 };
2037
2038 # ifdef SYNCTEX_NOTHING
2039 # pragma mark hbox proxy.
2040 # endif
2041
2042 /**
2043 * Standard nodes refer to TeX nodes: math, kern, boxes...
2044 * Proxy nodes are used to support forms.
2045 * A form is parsed as a tree of standard nodes starting
2046 * at the top left position.
2047 * When a reference is used, the form is duplicated
2048 * to the location specified by the reference.
2049 * As the same form can be duplicated at different locations,
2050 * the geometrical information is relative to its own top left point.
2051 * As we need absolute locations, we use proxy nodes.
2052 * A proxy node records an offset and the target node.
2053 * The target partly acts as a delegate.
2054 * The h and v position of the proxy node is the h and v
2055 * position of the target shifted by the proxy's offset.
2056 * The width, height and depth are not sensitive to offsets.
2057 * When are proxies created ?
2058 * 1) when the synctex file has been parsed, all the form refs
2059 * are replaced by proxies to the content of a form.
2060 * This content is a node with siblings (actually none).
2061 * Those root proxies have the parent of the ref they replace,
2062 * so their parents exist and are no proxy.
2063 * Moreover, if they have no sibling, it means that their target have no
2064 * sibling as well.
2065 * Such nodes are called root proxies.
2066 * 2) On the fly, when a proxy is asked for its child
2067 * (or sibling) and has none, a proxy to its target's child
2068 * (or sibling) is created if any. There are only 2 possible situations:
2069 * either the newly created proxy is the child of a proxy,
2070 * or it is the sibling of a proxy created on the fly.
2071 * In both cases, the parent is a proxy with children.
2072 * Such nodes are called child proxies.
2073 * How to compute the offset of a proxy ?
2074 * The offset of root proxy objects is exactly
2075 * the offset of the ref they replace.
2076 * The offset of other proxies is their owner's,
2077 * except when pointing to a root proxy.
2078 * What happens for cascading forms ?
2079 * Here is an example diagram
2080 *
2081 * At parse time, the arrow means "owns":
2082 * sheet0 -> ref_to1
2083 *
2084 * target1 -> ref_to2
2085 *
2086 * target2 -> child22
2087 *
2088 * After replacing the refs:
2089 * sheet0 -> proxy00 -> proxy01 -> proxy02
2090 * | | |
2091 * target1 -> proxy11 -> proxy12
2092 * | |
2093 * target2 -> proxy22
2094 *
2095 * proxy00, proxy11 and proxy22 are root proxies.
2096 * Their offset is the one of the ref they replace
2097 * proxy01, proxy02 and proxy12 are child proxies.
2098 * Their proxy is the one of their parent.
2099 * Optimization.
2100 * After all the refs are replaced, there are only root nodes
2101 * targeting standard node. We make sure that each child proxy
2102 * also targets a standard node.
2103 * It is possible for a proxy to have a standard sibling
2104 * whereas its target has no sibling at all. Root proxies
2105 * are such nodes, and are the only ones.
2106 * The consequence is that proxies created on the fly
2107 * must take into account this situation.
2108 */
2109
2110 /* A proxy to a hbox.
2111 * A proxy do have a target, which can be a proxy
2112 */
2113
2114 static const synctex_tree_model_s synctex_tree_model_proxy_hbox = {
2115 synctex_tree_sibling_idx, /* sibling */
2116 synctex_tree_s_parent_idx, /* parent */
2117 synctex_tree_sp_child_idx, /* child */
2118 synctex_tree_spc_friend_idx, /* friend */
2119 synctex_tree_spcf_last_idx, /* last */
2120 synctex_tree_spcfl_next_hbox_idx, /* next_hbox */
2121 -1, /* arg_sibling */
2122 synctex_tree_spcfln_target_idx, /* target */
2123 synctex_tree_spcflnt_proxy_hbox_max
2124 };
2125 static const synctex_data_model_s synctex_data_model_proxy = {
2126 -1, /* tag */
2127 -1, /* line */
2128 -1, /* column */
2129 synctex_data_proxy_h_idx, /* h */
2130 synctex_data_proxy_v_idx, /* v */
2131 -1, /* width */
2132 -1, /* height */
2133 -1, /* depth */
2134 -1, /* mean_line */
2135 -1, /* weight */
2136 -1, /* h_V */
2137 -1, /* v_V */
2138 -1, /* width_V */
2139 -1, /* height_V */
2140 -1, /* depth_V */
2141 -1, /* name */
2142 -1, /* page */
2143 synctex_data_proxy_hv_max
2144 };
2145 typedef struct {
2146 SYNCTEX_DECLARE_CHARINDEX
2147 synctex_class_p class_;
2148 synctex_data_u data[synctex_tree_spcflnt_proxy_hbox_max+synctex_data_proxy_hv_max];
2149 } synctex_node_proxy_hbox_s;
2150
2151 /* box proxy node creator */
2152 DEFINE_synctex_new_unscanned_NODE(proxy_hbox)
2153
2154 static void _synctex_log_proxy(synctex_node_p node);
2155 static char * _synctex_abstract_proxy_hbox(synctex_node_p node);
2156 static void _synctex_display_proxy_hbox(synctex_node_p node);
2157
2158 static int _synctex_proxy_tag(synctex_node_p);
2159 static int _synctex_proxy_line(synctex_node_p);
2160 static int _synctex_proxy_column(synctex_node_p);
2161
2162 static synctex_tlcpector_s synctex_tlcpector_proxy = {
2163 &_synctex_proxy_tag,
2164 &_synctex_proxy_line,
2165 &_synctex_proxy_column,
2166 };
2167 static int _synctex_proxy_h(synctex_node_p);
2168 static int _synctex_proxy_v(synctex_node_p);
2169 static int _synctex_proxy_width(synctex_node_p);
2170 static int _synctex_proxy_height(synctex_node_p);
2171 static int _synctex_proxy_depth(synctex_node_p);
2172 static synctex_inspector_s synctex_inspector_proxy_box = {
2173 &_synctex_proxy_h,
2174 &_synctex_proxy_v,
2175 &_synctex_proxy_width,
2176 &_synctex_proxy_height,
2177 &_synctex_proxy_depth,
2178 };
2179
2180 static float __synctex_proxy_visible_h(synctex_node_p);
2181 static float __synctex_proxy_visible_v(synctex_node_p);
2182 static float __synctex_proxy_visible_width(synctex_node_p);
2183 static float __synctex_proxy_visible_height(synctex_node_p);
2184 static float __synctex_proxy_visible_depth(synctex_node_p);
2185
2186 static synctex_vispector_s synctex_vispector_proxy_box = {
2187 &__synctex_proxy_visible_h,
2188 &__synctex_proxy_visible_v,
2189 &__synctex_proxy_visible_width,
2190 &__synctex_proxy_visible_height,
2191 &__synctex_proxy_visible_depth,
2192 };
2193
2194 static synctex_class_s synctex_class_proxy_hbox = {
2195 NULL, /* No scanner yet */
2196 synctex_node_type_proxy_hbox, /* Node type */
2197 &_synctex_new_proxy_hbox, /* creator */
2198 &_synctex_free_node, /* destructor */
2199 &_synctex_log_proxy, /* log */
2200 &_synctex_display_proxy_hbox, /* display */
2201 &_synctex_abstract_proxy_hbox, /* abstract */
2202 &synctex_tree_model_proxy_hbox, /* tree model */
2203 &synctex_data_model_proxy, /* data model */
2204 &synctex_tlcpector_proxy, /* tlcpector */
2205 &synctex_inspector_proxy_box, /* inspector */
2206 &synctex_vispector_proxy_box, /* vispector */
2207 };
2208
2209 # ifdef SYNCTEX_NOTHING
2210 # pragma mark vbox proxy.
2211 # endif
2212
2213 /* A proxy to a vbox. */
2214
2215 static const synctex_tree_model_s synctex_tree_model_proxy_vbox = {
2216 synctex_tree_sibling_idx, /* sibling */
2217 synctex_tree_s_parent_idx, /* parent */
2218 synctex_tree_sp_child_idx, /* child */
2219 synctex_tree_spc_friend_idx, /* friend */
2220 synctex_tree_spcf_last_idx, /* last */
2221 -1, /* next_hbox */
2222 -1, /* arg_sibling */
2223 synctex_tree_spcfl_target_idx, /* target */
2224 synctex_tree_spcflt_proxy_vbox_max
2225 };
2226
2227 typedef struct {
2228 SYNCTEX_DECLARE_CHARINDEX
2229 synctex_class_p class_;
2230 synctex_data_u data[synctex_tree_spcflt_proxy_vbox_max+synctex_data_proxy_hv_max];
2231 } synctex_node_proxy_vbox_s;
2232
2233 /* box proxy node creator */
2234 DEFINE_synctex_new_unscanned_NODE(proxy_vbox)
2235
2236 static void _synctex_log_proxy(synctex_node_p node);
2237 static char * _synctex_abstract_proxy_vbox(synctex_node_p node);
2238 static void _synctex_display_proxy_vbox(synctex_node_p node);
2239
2240 static synctex_class_s synctex_class_proxy_vbox = {
2241 NULL, /* No scanner yet */
2242 synctex_node_type_proxy_vbox, /* Node type */
2243 &_synctex_new_proxy_vbox, /* creator */
2244 &_synctex_free_node, /* destructor */
2245 &_synctex_log_proxy, /* log */
2246 &_synctex_display_proxy_vbox, /* display */
2247 &_synctex_abstract_proxy_vbox, /* abstract */
2248 &synctex_tree_model_proxy_vbox, /* tree model */
2249 &synctex_data_model_proxy, /* data model */
2250 &synctex_tlcpector_proxy, /* tlcpector */
2251 &synctex_inspector_proxy_box, /* inspector */
2252 &synctex_vispector_proxy_box, /* vispector */
2253 };
2254
2255 # ifdef SYNCTEX_NOTHING
2256 # pragma mark proxy.
2257 # endif
2258
2259 /**
2260 * A proxy to a node but a box.
2261 */
2262
2263 static const synctex_tree_model_s synctex_tree_model_proxy = {
2264 synctex_tree_sibling_idx, /* sibling */
2265 synctex_tree_s_parent_idx, /* parent */
2266 -1, /* child */
2267 synctex_tree_sp_friend_idx, /* friend */
2268 -1, /* last */
2269 -1, /* next_hbox */
2270 -1, /* arg_sibling */
2271 synctex_tree_spf_target_idx,/* target */
2272 synctex_tree_spft_proxy_max
2273 };
2274
2275 typedef struct {
2276 SYNCTEX_DECLARE_CHARINDEX
2277 synctex_class_p class_;
2278 synctex_data_u data[synctex_tree_spft_proxy_max+synctex_data_proxy_hv_max];
2279 } synctex_node_proxy_s;
2280
2281 /* proxy node creator */
2282 DEFINE_synctex_new_unscanned_NODE(proxy)
2283
2284 static void _synctex_log_proxy(synctex_node_p node);
2285 static char * _synctex_abstract_proxy(synctex_node_p node);
2286 static void _synctex_display_proxy(synctex_node_p node);
2287
2288 static synctex_vispector_s synctex_vispector_proxy = {
2289 &__synctex_proxy_visible_h,
2290 &__synctex_proxy_visible_v,
2291 &__synctex_proxy_visible_width,
2292 &_synctex_float_none,
2293 &_synctex_float_none,
2294 };
2295
2296 static synctex_class_s synctex_class_proxy = {
2297 NULL, /* No scanner yet */
2298 synctex_node_type_proxy, /* Node type */
2299 &_synctex_new_proxy, /* creator */
2300 &_synctex_free_leaf, /* destructor */
2301 &_synctex_log_proxy, /* log */
2302 &_synctex_display_proxy, /* display */
2303 &_synctex_abstract_proxy, /* abstract */
2304 &synctex_tree_model_proxy, /* tree model */
2305 &synctex_data_model_proxy, /* data model */
2306 &synctex_tlcpector_proxy, /* tlcpector */
2307 &synctex_inspector_proxy_box, /* inspector */
2308 &synctex_vispector_proxy, /* vispector */
2309 };
2310
2311 # ifdef SYNCTEX_NOTHING
2312 # pragma mark last proxy.
2313 # endif
2314
2315 /**
2316 * A proxy to the last proxy/box boundary.
2317 */
2318
2319 static const synctex_tree_model_s synctex_tree_model_proxy_last = {
2320 synctex_tree_sibling_idx, /* sibling */
2321 synctex_tree_s_parent_idx, /* parent */
2322 -1, /* child */
2323 synctex_tree_sp_friend_idx, /* friend */
2324 -1, /* last */
2325 -1, /* next_hbox */
2326 synctex_tree_spf_arg_sibling_idx, /* arg_sibling */
2327 synctex_tree_spfa_target_idx, /* target */
2328 synctex_tree_spfat_proxy_last_max
2329 };
2330
2331 typedef struct {
2332 SYNCTEX_DECLARE_CHARINDEX
2333 synctex_class_p class_;
2334 synctex_data_u data[synctex_tree_spfat_proxy_last_max+synctex_data_proxy_hv_max];
2335 } synctex_node_proxy_last_s;
2336
2337 /* proxy node creator */
2338 DEFINE_synctex_new_unscanned_NODE(proxy_last)
2339
2340 static void _synctex_log_proxy(synctex_node_p node);
2341 static char * _synctex_abstract_proxy(synctex_node_p node);
2342 static void _synctex_display_proxy(synctex_node_p node);
2343
2344 static synctex_class_s synctex_class_proxy_last = {
2345 NULL, /* No scanner yet */
2346 synctex_node_type_proxy_last, /* Node type */
2347 &_synctex_new_proxy, /* creator */
2348 &_synctex_free_leaf, /* destructor */
2349 &_synctex_log_proxy, /* log */
2350 &_synctex_display_proxy, /* display */
2351 &_synctex_abstract_proxy, /* abstract */
2352 &synctex_tree_model_proxy_last, /* tree model */
2353 &synctex_data_model_proxy, /* data model */
2354 &synctex_tlcpector_proxy, /* tlcpector */
2355 &synctex_inspector_proxy_box, /* inspector */
2356 &synctex_vispector_proxy, /* vispector */
2357 };
2358
2359 # ifdef SYNCTEX_NOTHING
2360 # pragma mark handle.
2361 # endif
2362
2363 /**
2364 * A handle node.
2365 * A handle is never the target of a proxy
2366 * or another handle.
2367 * The child of a handle is always a handle if any.
2368 * The sibling of a handle is always a handle if any.
2369 * The parent of a handle is always a handle if any.
2370 */
2371
2372 static const synctex_tree_model_s synctex_tree_model_handle = {
2373 synctex_tree_sibling_idx, /* sibling */
2374 synctex_tree_s_parent_idx, /* parent */
2375 synctex_tree_sp_child_idx, /* child */
2376 -1, /* friend */
2377 -1, /* last */
2378 -1, /* next_hbox */
2379 -1, /* arg_sibling */
2380 synctex_tree_spc_target_idx,/* target */
2381 synctex_tree_spct_handle_max
2382 };
2383
2384 static const synctex_data_model_s synctex_data_model_handle = {
2385 -1, /* tag */
2386 -1, /* line */
2387 -1, /* column */
2388 -1, /* h */
2389 -1, /* v */
2390 -1, /* width */
2391 -1, /* height */
2392 -1, /* depth */
2393 -1, /* mean_line */
2394 synctex_data_handle_w_idx, /* weight */
2395 -1, /* h_V */
2396 -1, /* v_V */
2397 -1, /* width_V */
2398 -1, /* height_V */
2399 -1, /* depth_V */
2400 -1, /* name */
2401 -1, /* page */
2402 synctex_data_handle_w_max
2403 };
2404
2405 typedef struct {
2406 SYNCTEX_DECLARE_CHARINDEX
2407 synctex_class_p class_;
2408 synctex_data_u data[synctex_tree_spct_handle_max+synctex_data_handle_w_max];
2409 } synctex_node_handle_s;
2410
2411 /* handle node creator */
2412 DEFINE_synctex_new_unscanned_NODE(handle)
2413
2414 static void _synctex_log_handle(synctex_node_p node);
2415 static char * _synctex_abstract_handle(synctex_node_p node);
2416 static void _synctex_display_handle(synctex_node_p node);
2417
2418 static synctex_class_s synctex_class_handle = {
2419 NULL, /* No scanner yet */
2420 synctex_node_type_handle, /* Node type */
2421 &_synctex_new_handle, /* creator */
2422 &_synctex_free_handle, /* destructor */
2423 &_synctex_log_handle, /* log */
2424 &_synctex_display_handle, /* display */
2425 &_synctex_abstract_handle, /* abstract */
2426 &synctex_tree_model_handle, /* tree model */
2427 &synctex_data_model_handle, /* data model */
2428 &synctex_tlcpector_proxy, /* tlcpector */
2429 &synctex_inspector_proxy_box, /* inspector */
2430 &synctex_vispector_proxy_box, /* vispector */
2431 };
2432
2433 SYNCTEX_INLINE static synctex_node_p _synctex_new_handle_with_target(synctex_node_p target) {
2434 if (target) {
2435 synctex_node_p result = _synctex_new_handle(target->class_->scanner);
2436 if (result) {
2437 _synctex_tree_set_target(result,target);
2438 return result;
2439 }
2440 }
2441 return NULL;
2442 }
2443 SYNCTEX_INLINE static synctex_node_p _synctex_new_handle_with_child(synctex_node_p child) {
2444 if (child) {
2445 synctex_node_p result = _synctex_new_handle(child->class_->scanner);
2446 if (result) {
2447 _synctex_tree_set_child(result,child);
2448 return result;
2449 }
2450 }
2451 return NULL;
2452 }
2453
2454 # ifdef SYNCTEX_NOTHING
2455 # pragma mark -
2456 # pragma mark Navigation
2457 # endif
2458 synctex_node_p synctex_node_parent(synctex_node_p node)
2459 {
2460 return _synctex_tree_parent(node);
2461 }
2462 synctex_node_p synctex_node_parent_sheet(synctex_node_p node)
2463 {
2464 while(node && synctex_node_type(node) != synctex_node_type_sheet) {
2465 node = _synctex_tree_parent(node);
2466 }
2467 /* exit the while loop either when node is NULL or node is a sheet */
2468 return node;
2469 }
2470 synctex_node_p synctex_node_parent_form(synctex_node_p node)
2471 {
2472 while(node && synctex_node_type(node) != synctex_node_type_form) {
2473 node = _synctex_tree_parent(node);
2474 }
2475 /* exit the while loop either when node is NULL or node is a form */
2476 return node;
2477 }
2478
2479 /**
2480 * The returned proxy will be the child or a sibling of source.
2481 * The returned proxy has no parent, child nor sibling.
2482 * Used only by __synctex_replace_ref.
2483 * argument to_node: a box, not a proxy nor anything else.
2484 */
2485 SYNCTEX_INLINE static synctex_node_p __synctex_new_proxy_from_ref_to(synctex_node_p ref, synctex_node_p to_node) {
2486 synctex_node_p proxy = NULL;
2487 if (!ref || !to_node) {
2488 return NULL;
2489 }
2490 switch(synctex_node_type(to_node)) {
2491 case synctex_node_type_vbox:
2492 proxy = _synctex_new_proxy_vbox(ref->class_->scanner);
2493 break;
2494 case synctex_node_type_hbox:
2495 proxy = _synctex_new_proxy_hbox(ref->class_->scanner);
2496 break;
2497 default:
2498 _synctex_error("! __synctex_new_proxy_from_ref_to. Unexpected form child (%s). Please report.", synctex_node_isa(to_node));
2499 return NULL;
2500 }
2501 if (!proxy) {
2502 _synctex_error("! __synctex_new_proxy_from_ref_to. Internal error. Please report.");
2503 return NULL;
2504 }
2505 _synctex_data_set_h(proxy, _synctex_data_h(ref));
2506 _synctex_data_set_v(proxy, _synctex_data_v(ref)-_synctex_data_height(to_node));
2507 _synctex_tree_set_target(proxy,to_node);
2508 # if defined(SYNCTEX_USE_CHARINDEX)
2509 proxy->line_index=to_node?to_node->line_index:0;
2510 proxy->char_index=to_node?to_node->char_index:0;
2511 # endif
2512 return proxy;
2513 }
2514 /**
2515 * The returned proxy will be the child or a sibling of owning_proxy.
2516 * The returned proxy has no parent, nor child.
2517 * Used only by synctex_node_child and synctex_node_sibling
2518 * to create proxies on the fly.
2519 * If the to_node has an already computed sibling,
2520 * then the returned proxy has itself a sibling
2521 * pointing to that already computed sibling.
2522 */
2523 SYNCTEX_INLINE static synctex_node_p __synctex_new_child_proxy_to(synctex_node_p owner, synctex_node_p to_node) {
2524 synctex_node_p proxy = NULL;
2525 synctex_node_p target = to_node;
2526 if (!owner) {
2527 return NULL;
2528 }
2529 switch(synctex_node_type(target)) {
2530 case synctex_node_type_vbox:
2531 if ((proxy = _synctex_new_proxy_vbox(owner->class_->scanner))) {
2532 exit_standard:
2533 _synctex_data_set_h(proxy, _synctex_data_h(owner));
2534 _synctex_data_set_v(proxy, _synctex_data_v(owner));
2535 exit0:
2536 _synctex_tree_set_target(proxy,target);
2537 # if defined(SYNCTEX_USE_CHARINDEX)
2538 proxy->line_index=to_node?to_node->line_index:0;
2539 proxy->char_index=to_node?to_node->char_index:0;
2540 # endif
2541 return proxy;
2542 };
2543 break;
2544 case synctex_node_type_proxy_vbox:
2545 if ((proxy = _synctex_new_proxy_vbox(owner->class_->scanner))) {
2546 exit_proxy:
2547 target = _synctex_tree_target(to_node);
2548 _synctex_data_set_h(proxy, _synctex_data_h(owner)+_synctex_data_h(to_node));
2549 _synctex_data_set_v(proxy, _synctex_data_v(owner)+_synctex_data_v(to_node));
2550 goto exit0;
2551 };
2552 break;
2553 case synctex_node_type_hbox:
2554 if ((proxy = _synctex_new_proxy_hbox(owner->class_->scanner))) {
2555 goto exit_standard;
2556 };
2557 break;
2558 case synctex_node_type_proxy_hbox:
2559 if ((proxy = _synctex_new_proxy_hbox(owner->class_->scanner))) {
2560 goto exit_proxy;
2561 };
2562 break;
2563 case synctex_node_type_proxy:
2564 case synctex_node_type_proxy_last:
2565 if ((proxy = _synctex_new_proxy(owner->class_->scanner))) {
2566 goto exit_proxy;
2567 };
2568 break;
2569 default:
2570 if ((proxy = _synctex_new_proxy(owner->class_->scanner))) {
2571 goto exit_standard;
2572 };
2573 break;
2574 }
2575 _synctex_error("! __synctex_new_child_proxy_to. "
2576 "Internal error. "
2577 "Please report.");
2578 return NULL;
2579 }
2580 SYNCTEX_INLINE static synctex_node_p _synctex_tree_set_sibling(synctex_node_p node, synctex_node_p new_sibling);
2581 typedef struct synctex_nns_t {
2582 synctex_node_p first;
2583 synctex_node_p last;
2584 synctex_status_t status;
2585 } synctex_nns_s;
2586 /**
2587 * Given a target node, create a list of proxies.
2588 * The first proxy points to the target node,
2589 * its sibling points to the target's sibling and so on.
2590 * Returns the first created proxy, the last one and
2591 * an error status.
2592 */
2593 SYNCTEX_INLINE static synctex_nns_s _synctex_new_child_proxies_to(synctex_node_p owner, synctex_node_p to_node) {
2594 synctex_nns_s nns = {NULL,NULL,SYNCTEX_STATUS_OK};
2595 if ((nns.first = nns.last = __synctex_new_child_proxy_to(owner,to_node))) {
2596 synctex_node_p to_next_sibling = __synctex_tree_sibling(to_node);
2597 synctex_node_p to_sibling;
2598 while ((to_sibling = to_next_sibling)) {
2599 synctex_node_p sibling;
2600 if ((to_next_sibling = __synctex_tree_sibling(to_sibling))) {
2601 /* This is not the last sibling */
2602 if((sibling = __synctex_new_child_proxy_to(owner,to_sibling))) {
2603 _synctex_tree_set_sibling(nns.last,sibling);
2604 nns.last = sibling;
2605 continue;
2606 } else {
2607 _synctex_error("! _synctex_new_child_proxy_to. "
2608 "Internal error (1). "
2609 "Please report.");
2610 nns.status = SYNCTEX_STATUS_ERROR;
2611 }
2612 } else if((sibling = _synctex_new_proxy_last(owner->class_->scanner))) {
2613 _synctex_tree_set_sibling(nns.last,sibling);
2614 nns.last = sibling;
2615 _synctex_data_set_h(nns.last, _synctex_data_h(nns.first));
2616 _synctex_data_set_v(nns.last, _synctex_data_v(nns.first));
2617 _synctex_tree_set_target(nns.last,to_sibling);
2618 # if defined(SYNCTEX_USE_CHARINDEX)
2619 nns.last->line_index=to_sibling->line_index;
2620 nns.last->char_index=to_sibling->char_index;
2621 # endif
2622 } else {
2623 _synctex_error("! _synctex_new_child_proxy_to. "
2624 "Internal error (2). "
2625 "Please report.");
2626 nns.status = SYNCTEX_STATUS_ERROR;
2627 }
2628 break;
2629 }
2630 }
2631 return nns;
2632 }
2633 static char * _synctex_node_abstract(synctex_node_p node);
2634 SYNCTEX_INLINE static synctex_node_p synctex_tree_set_friend(synctex_node_p node,synctex_node_p new_friend) {
2635 #if SYNCTEX_DEBUG
2636 synctex_node_p F = new_friend;
2637 while (F) {
2638 if (node == F) {
2639 printf("THIS IS AN ERROR\n");
2640 F = new_friend;
2641 while (F) {
2642 printf("%s\n",_synctex_node_abstract(F));
2643 if (node == F) {
2644 return NULL;
2645 }
2646 F = _synctex_tree_friend(F);
2647 }
2648 return NULL;
2649 }
2650 F = _synctex_tree_friend(F);
2651 }
2652 #endif
2653 return new_friend?_synctex_tree_set_friend(node,new_friend):_synctex_tree_reset_friend(node);
2654 }
2655 /**
2656 *
2657 */
2658 SYNCTEX_INLINE static synctex_node_p __synctex_node_make_friend(synctex_node_p node, int i) {
2659 synctex_node_p old = NULL;
2660 if (i>=0) {
2661 i = i%(node->class_->scanner->number_of_lists);
2662 old = synctex_tree_set_friend(node,(node->class_->scanner->lists_of_friends)[i]);
2663 (node->class_->scanner->lists_of_friends)[i] = node;
2664 #if SYNCTEX_DEBUG>500
2665 printf("tl(%i)=>",i);
2666 synctex_node_log(node);
2667 if (synctex_node_parent_form(node)) {
2668 printf("! ERROR. No registration expected!\n");
2669 }
2670 #endif
2671 }
2672 return old;
2673 }
2674 /**
2675 * All proxies have tlc attributes, on behalf of their target.
2676 * The purpose is to register all af them.
2677 * - argument node: is the proxy, must not be NULL
2678 */
2679 SYNCTEX_INLINE static synctex_node_p __synctex_proxy_make_friend_and_next_hbox(synctex_node_p node) {
2680 synctex_node_p old = NULL;
2681 synctex_node_p target = _synctex_tree_target(node);
2682 if (target) {
2683 int i = _synctex_data_tag(target)+_synctex_data_line(target);
2684 old = __synctex_node_make_friend(node,i);
2685 } else {
2686 old = __synctex_tree_reset_friend(node);
2687 }
2688 if (synctex_node_type(node) == synctex_node_type_proxy_hbox) {
2689 synctex_node_p sheet = synctex_node_parent_sheet(node);
2690 if (sheet) {
2691 _synctex_tree_set_next_hbox(node,_synctex_tree_next_hbox(sheet));
2692 _synctex_tree_set_next_hbox(sheet,node);
2693 }
2694 }
2695 return old;
2696 }
2697 /**
2698 * Register a node which have tag, line and column.
2699 * - argument node: the node
2700 */
2701 SYNCTEX_INLINE static synctex_node_p __synctex_node_make_friend_tlc(synctex_node_p node) {
2702 int i = synctex_node_tag(node)+synctex_node_line(node);
2703 return __synctex_node_make_friend(node,i);
2704 }
2705 /**
2706 * Register a node which have tag, line and column.
2707 * Does nothing if the argument is NULL.
2708 * Calls __synctex_node_make_friend_tlc.
2709 * - argument node: the node
2710 */
2711 SYNCTEX_INLINE static void _synctex_node_make_friend_tlc(synctex_node_p node) {
2712 if (node) {
2713 __synctex_node_make_friend_tlc(node);
2714 }
2715 }
2716 static synctex_node_p _synctex_node_set_child(synctex_node_p node, synctex_node_p new_child);
2717 /**
2718 * The (first) child of the node, if any, NULL otherwise.
2719 * At parse time, non void box nodes have children.
2720 * All other nodes have no children.
2721 * In order to support pdf forms, proxies are created
2722 * to place form nodes at real locations.
2723 * Ref nodes are replaced by root proxies targeting
2724 * form contents. If root proxies have no children,
2725 * they are created on the fly as proxies to the
2726 * children of the targeted box.
2727 * As such, proxies created here are targeting a
2728 * node that belongs to a form.
2729 * This is the only place where child proxies are created.
2730 */
2731 synctex_node_p synctex_node_child(synctex_node_p node) {
2732 synctex_node_p child = NULL;
2733 synctex_node_p target = NULL;
2734 if ((child = _synctex_tree_child(node))) {
2735 return child;
2736 } else if ((target = _synctex_tree_target(node))) {
2737 if ((child = synctex_node_child(target))) {
2738 /* This is a proxy with no child
2739 * which target does have a child. */
2740 synctex_nns_s nns = _synctex_new_child_proxies_to(node, child);
2741 if (nns.first) {
2742 _synctex_node_set_child(node,nns.first);
2743 return nns.first;
2744 } else {
2745 _synctex_error("! synctex_node_child. Internal inconsistency. Please report.");
2746 }
2747 }
2748 }
2749 return NULL;
2750 }
2751 /*
2752 * Set the parent/child bound.
2753 * Things get complicated when new_child has siblings.
2754 * The caller is responsible for releasing the returned value.
2755 */
2756 static synctex_node_p _synctex_node_set_child(synctex_node_p parent, synctex_node_p new_child) {
2757 if (parent) {
2758 synctex_node_p old = _synctex_tree_set_child(parent,new_child);
2759 synctex_node_p last_child = NULL;
2760 synctex_node_p child;
2761 if ((child = old)) {
2762 do {
2763 _synctex_tree_reset_parent(child);
2764 } while ((child = __synctex_tree_sibling(child)));
2765 }
2766 if ((child = new_child)) {
2767 do {
2768 _synctex_tree_set_parent(child,parent);
2769 last_child = child;
2770 } while ((child = __synctex_tree_sibling(child)));
2771 }
2772 _synctex_tree_set_last(parent,last_child);
2773 return old;
2774 }
2775 return NULL;
2776 }
2777
2778 /* The last child of the given node, or NULL.
2779 */
2780 synctex_node_p synctex_node_last_child(synctex_node_p node) {
2781 return _synctex_tree_last(node);
2782 }
2783 /**
2784 * All nodes siblings are properly set up at parse time
2785 * except for non root proxies.
2786 */
2787 synctex_node_p synctex_node_sibling(synctex_node_p node) {
2788 return node? __synctex_tree_sibling(node): NULL;
2789 }
2790 /**
2791 * All the _synctex_tree_... methods refer to the tree model.
2792 * __synctex_tree_... methods are low level.
2793 */
2794 /**
2795 * Replace the sibling.
2796 * Connect to the arg_sibling of the new_sibling if relevant.
2797 * - returns the old sibling.
2798 * The caller is responsible for releasing the old sibling.
2799 * The bound to the parent is managed below.
2800 */
2801 SYNCTEX_INLINE static synctex_node_p _synctex_tree_set_sibling(synctex_node_p node, synctex_node_p new_sibling) {
2802 if (node == new_sibling) {
2803 printf("BOF\n");
2804 }
2805 synctex_node_p old = node? __synctex_tree_set_sibling(node,new_sibling): NULL;
2806 _synctex_tree_set_arg_sibling(new_sibling,node);
2807 return old;
2808 }
2809 /**
2810 * Replace the sibling.
2811 * Set the parent of the new sibling (and further siblings)
2812 * to the parent of the receiver.
2813 * Also set the last sibling of parent.
2814 * - argument new_sibling: must not be NULL.
2815 * - returns the old sibling.
2816 * The caller is responsible for releasing the old sibling.
2817 */
2818 static synctex_node_p _synctex_node_set_sibling(synctex_node_p node, synctex_node_p new_sibling) {
2819 if (node && new_sibling) {
2820 synctex_node_p old = _synctex_tree_set_sibling(node,new_sibling);
2821 if (_synctex_tree_has_parent(node)) {
2822 synctex_node_p parent = __synctex_tree_parent(node);
2823 if (parent) {
2824 synctex_node_p N = new_sibling;
2825 while (synctex_YES) {
2826 if (_synctex_tree_has_parent(N)) {
2827 __synctex_tree_set_parent(N,parent);
2828 _synctex_tree_set_last(parent,N);
2829 N = __synctex_tree_sibling(N);
2830 continue;
2831 } else if (N) {
2832 _synctex_error("! synctex_node_sibling. "
2833 "Internal inconsistency. "
2834 "Please report.");
2835 }
2836 break;
2837 }
2838 }
2839 }
2840 return old;
2841 }
2842 return NULL;
2843 }
2844 /**
2845 * The last sibling of the given node, or NULL with node.
2846 */
2847 synctex_node_p synctex_node_last_sibling(synctex_node_p node) {
2848 synctex_node_p sibling;
2849 do {
2850 sibling = node;
2851 } while((node = synctex_node_sibling(node)));
2852 return sibling;
2853 }
2854 /**
2855 * The next nodes corresponds to a deep first tree traversal.
2856 * Does not create child proxies as side effect contrary to
2857 * the synctex_node_next method above.
2858 * May loop infinitely many times if the tree
2859 * is not properly built (contains loops).
2860 */
2861 SYNCTEX_INLINE static synctex_node_p _synctex_node_sibling_or_parents(synctex_node_p node) {
2862 while (node) {
2863 synctex_node_p N;
2864 if ((N = __synctex_tree_sibling(node))) {
2865 return N;
2866 } else if ((node = _synctex_tree_parent(node))) {
2867 if (synctex_node_type(node) == synctex_node_type_sheet) {/* EXC_BAD_ACCESS? */
2868 return NULL;
2869 } else if (synctex_node_type(node) == synctex_node_type_form) {
2870 return NULL;
2871 }
2872 } else {
2873 return NULL;
2874 }
2875 }
2876 return NULL;
2877 }
2878 /**
2879 * The next nodes corresponds to a deep first tree traversal.
2880 * Creates child proxies as side effect.
2881 * May loop infinitely many times if the tree
2882 * is not properly built (contains loops).
2883 */
2884 synctex_node_p synctex_node_next(synctex_node_p node) {
2885 synctex_node_p N = synctex_node_child(node);
2886 if (N) {
2887 return N;
2888 }
2889 return _synctex_node_sibling_or_parents(node);
2890 }
2891 /**
2892 * The node which argument is the sibling.
2893 * - return: NULL if the argument has no parent or
2894 * is the first child of its parent.
2895 * - Input nodes have no arg siblings
2896 */
2897 synctex_node_p synctex_node_arg_sibling(synctex_node_p node) {
2898 #if 1
2899 return _synctex_tree_arg_sibling(node);
2900 #else
2901 synctex_node_p N = _synctex_tree_parent(node);
2902 if ((N = _synctex_tree_child(N))) {
2903 do {
2904 synctex_node_p NN = __synctex_tree_sibling(N);
2905 if (NN == node) {
2906 return N;
2907 }
2908 N = NN;
2909 } while (N);
2910 }
2911 return N;
2912 #endif
2913 }
2914 # ifdef SYNCTEX_NOTHING
2915 # pragma mark -
2916 # pragma mark CLASS
2917 # endif
2918
2919 /* Public node accessor: the type */
2920 synctex_node_type_t synctex_node_type(synctex_node_p node) {
2921 return node? node->class_->type: synctex_node_type_none;
2922 }
2923
2924 /* Public node accessor: the type */
2925 synctex_node_type_t synctex_node_target_type(synctex_node_p node) {
2926 synctex_node_p target = _synctex_tree_target(node);
2927 if (target) {
2928 return (((target)->class_))->type;
2929 } else if (node) {
2930 return (((node)->class_))->type;
2931 }
2932 return synctex_node_type_none;
2933 }
2934
2935 /* Public node accessor: the human readable type */
2936 const char * synctex_node_isa(synctex_node_p node) {
2937 static const char * isa[synctex_node_number_of_types] =
2938 {"Not a node",
2939 "input",
2940 "sheet",
2941 "form",
2942 "ref",
2943 "vbox",
2944 "void vbox",
2945 "hbox",
2946 "void hbox",
2947 "kern",
2948 "glue",
2949 "rule",
2950 "math",
2951 "boundary",
2952 "box_bdry",
2953 "proxy",
2954 "last proxy",
2955 "vbox proxy",
2956 "hbox proxy",
2957 "handle"};
2958 return isa[synctex_node_type(node)];
2959 }
2960
2961 # ifdef SYNCTEX_NOTHING
2962 # pragma mark -
2963 # pragma mark LOG
2964 # endif
2965
2966 /* Public node logger */
2967 void synctex_node_log(synctex_node_p node) {
2968 SYNCTEX_MSG_SEND(node,log);
2969 }
2970
2971 static void _synctex_log_input(synctex_node_p node) {
2972 if (node) {
2973 printf("%s:%i,%s(%i)\n",synctex_node_isa(node),
2974 _synctex_data_tag(node),
2975 _synctex_data_name(node),
2976 _synctex_data_line(node));
2977 printf("SELF:%p\n",(void *)node);
2978 printf(" SIBLING:%p\n",
2979 (void *)__synctex_tree_sibling(node));
2980 }
2981 }
2982
2983 static void _synctex_log_sheet(synctex_node_p node) {
2984 if (node) {
2985 printf("%s:%i",synctex_node_isa(node),_synctex_data_page(node));
2986 SYNCTEX_PRINT_CHARINDEX_NL;
2987 printf("SELF:%p\n",(void *)node);
2988 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
2989 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
2990 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
2991 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
2992 printf(" NEXT_hbox:%p\n",(void *)_synctex_tree_next_hbox(node));
2993 }
2994 }
2995
2996 static void _synctex_log_form(synctex_node_p node) {
2997 if (node) {
2998 printf("%s:%i",synctex_node_isa(node),_synctex_data_tag(node));
2999 SYNCTEX_PRINT_CHARINDEX_NL;
3000 printf("SELF:%p\n",(void *)node);
3001 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3002 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3003 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
3004 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3005 }
3006 }
3007
3008 static void _synctex_log_ref(synctex_node_p node) {
3009 if (node) {
3010 printf("%s:%i:%i,%i",
3011 synctex_node_isa(node),
3012 _synctex_data_tag(node),
3013 _synctex_data_h(node),
3014 _synctex_data_v(node));
3015 SYNCTEX_PRINT_CHARINDEX_NL;
3016 printf("SELF:%p\n",(void *)node);
3017 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3018 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3019 }
3020 }
3021
3022 static void _synctex_log_tlchv_node(synctex_node_p node) {
3023 if (node) {
3024 printf("%s:%i,%i,%i:%i,%i",
3025 synctex_node_isa(node),
3026 _synctex_data_tag(node),
3027 _synctex_data_line(node),
3028 _synctex_data_column(node),
3029 _synctex_data_h(node),
3030 _synctex_data_v(node));
3031 SYNCTEX_PRINT_CHARINDEX_NL;
3032 printf("SELF:%p\n",(void *)node);
3033 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3034 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3035 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
3036 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3037 }
3038 }
3039
3040 static void _synctex_log_kern_node(synctex_node_p node) {
3041 if (node) {
3042 printf("%s:%i,%i,%i:%i,%i:%i",
3043 synctex_node_isa(node),
3044 _synctex_data_tag(node),
3045 _synctex_data_line(node),
3046 _synctex_data_column(node),
3047 _synctex_data_h(node),
3048 _synctex_data_v(node),
3049 _synctex_data_width(node));
3050 SYNCTEX_PRINT_CHARINDEX_NL;
3051 printf("SELF:%p\n",(void *)node);
3052 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3053 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3054 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
3055 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3056 }
3057 }
3058
3059 static void _synctex_log_rule(synctex_node_p node) {
3060 if (node) {
3061 printf("%s:%i,%i,%i:%i,%i",
3062 synctex_node_isa(node),
3063 _synctex_data_tag(node),
3064 _synctex_data_line(node),
3065 _synctex_data_column(node),
3066 _synctex_data_h(node),
3067 _synctex_data_v(node));
3068 printf(":%i",_synctex_data_width(node));
3069 printf(",%i",_synctex_data_height(node));
3070 printf(",%i",_synctex_data_depth(node));
3071 SYNCTEX_PRINT_CHARINDEX_NL;
3072 printf("SELF:%p\n",(void *)node);
3073 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3074 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3075 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3076 }
3077 }
3078
3079 static void _synctex_log_void_box(synctex_node_p node) {
3080 if (node) {
3081 printf("%s",synctex_node_isa(node));
3082 printf(":%i",_synctex_data_tag(node));
3083 printf(",%i",_synctex_data_line(node));
3084 printf(",%i",_synctex_data_column(node));
3085 printf(":%i",_synctex_data_h(node));
3086 printf(",%i",_synctex_data_v(node));
3087 printf(":%i",_synctex_data_width(node));
3088 printf(",%i",_synctex_data_height(node));
3089 printf(",%i",_synctex_data_depth(node));
3090 SYNCTEX_PRINT_CHARINDEX_NL;
3091 printf("SELF:%p\n",(void *)node);
3092 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3093 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3094 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
3095 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3096 }
3097 }
3098
3099 static void _synctex_log_vbox(synctex_node_p node) {
3100 if (node) {
3101 printf("%s",synctex_node_isa(node));
3102 printf(":%i",_synctex_data_tag(node));
3103 printf(",%i",_synctex_data_line(node));
3104 printf(",%i",_synctex_data_column(node));
3105 printf(":%i",_synctex_data_h(node));
3106 printf(",%i",_synctex_data_v(node));
3107 printf(":%i",_synctex_data_width(node));
3108 printf(",%i",_synctex_data_height(node));
3109 printf(",%i",_synctex_data_depth(node));
3110 SYNCTEX_PRINT_CHARINDEX_NL;
3111 printf("SELF:%p\n",(void *)node);
3112 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3113 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3114 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
3115 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3116 printf(" NEXT_hbox:%p\n",(void *)_synctex_tree_next_hbox(node));
3117 }
3118 }
3119
3120 static void _synctex_log_hbox(synctex_node_p node) {
3121 if (node) {
3122 printf("%s",synctex_node_isa(node));
3123 printf(":%i",_synctex_data_tag(node));
3124 printf(",%i~%i*%i",_synctex_data_line(node),_synctex_data_mean_line(node),_synctex_data_weight(node));
3125 printf(",%i",_synctex_data_column(node));
3126 printf(":%i",_synctex_data_h(node));
3127 printf(",%i",_synctex_data_v(node));
3128 printf(":%i",_synctex_data_width(node));
3129 printf(",%i",_synctex_data_height(node));
3130 printf(",%i",_synctex_data_depth(node));
3131 printf("/%i",_synctex_data_h_V(node));
3132 printf(",%i",_synctex_data_v_V(node));
3133 printf(":%i",_synctex_data_width_V(node));
3134 printf(",%i",_synctex_data_height_V(node));
3135 printf(",%i",_synctex_data_depth_V(node));
3136 SYNCTEX_PRINT_CHARINDEX_NL;
3137 printf("SELF:%p\n",(void *)node);
3138 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3139 printf(" PARENT:%p\n",(void *)_synctex_tree_parent(node));
3140 printf(" CHILD:%p\n",(void *)_synctex_tree_child(node));
3141 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3142 printf(" NEXT_hbox:%p\n",(void *)_synctex_tree_next_hbox(node));
3143 }
3144 }
3145 static void _synctex_log_proxy(synctex_node_p node) {
3146 if (node) {
3147 synctex_node_p N = _synctex_tree_target(node);
3148 printf("%s",synctex_node_isa(node));
3149 printf(":%i",_synctex_data_h(node));
3150 printf(",%i",_synctex_data_v(node));
3151 SYNCTEX_PRINT_CHARINDEX_NL;
3152 printf("SELF:%p\n",(void *)node);
3153 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3154 printf(" LEFT:%p\n",(void *)_synctex_tree_friend(node));
3155 printf(" ->%s\n",_synctex_node_abstract(N));
3156 }
3157 }
3158 static void _synctex_log_handle(synctex_node_p node) {
3159 if (node) {
3160 synctex_node_p N = _synctex_tree_target(node);
3161 printf("%s",synctex_node_isa(node));
3162 SYNCTEX_PRINT_CHARINDEX_NL;
3163 printf("SELF:%p\n",(void *)node);
3164 printf(" SIBLING:%p\n",(void *)__synctex_tree_sibling(node));
3165 printf(" ->%s\n",_synctex_node_abstract(N));
3166 }
3167 }
3168
3169 # ifdef SYNCTEX_NOTHING
3170 # pragma mark -
3171 # pragma mark SYNCTEX_DISPLAY
3172 # endif
3173
3174 int synctex_scanner_display_switcher(synctex_scanner_p scanR) {
3175 return scanR->display_switcher;
3176 }
3177 void synctex_scanner_set_display_switcher(synctex_scanner_p scanR, int switcher) {
3178 scanR->display_switcher = switcher;
3179 }
3180 static const char * const _synctex_display_prompt = "................................";
3181
3182 static char * _synctex_scanner_display_prompt_down(synctex_scanner_p scanR) {
3183 if (scanR->display_prompt>_synctex_display_prompt) {
3184 --scanR->display_prompt;
3185 }
3186 return scanR->display_prompt;
3187 }
3188 static char * _synctex_scanner_display_prompt_up(synctex_scanner_p scanR) {
3189 if (scanR->display_prompt+1<_synctex_display_prompt+strlen(_synctex_display_prompt)) {
3190 ++scanR->display_prompt;
3191 }
3192 return scanR->display_prompt;
3193 }
3194
3195 void synctex_node_display(synctex_node_p node) {
3196 if (node) {
3197 synctex_scanner_p scanR = node->class_->scanner;
3198 if (scanR) {
3199 if (scanR->display_switcher<0) {
3200 SYNCTEX_MSG_SEND(node, display);
3201 } else if (scanR->display_switcher>0 && --scanR->display_switcher>0) {
3202 SYNCTEX_MSG_SEND(node, display);
3203 } else if (scanR->display_switcher-->=0) {
3204 printf("%s Next display skipped. Reset display switcher.\n",node->class_->scanner->display_prompt);
3205 }
3206 } else {
3207 SYNCTEX_MSG_SEND(node, display);
3208 }
3209 }
3210 }
3211 static char * _synctex_node_abstract(synctex_node_p node) {
3212 SYNCTEX_PARAMETER_ASSERT(node || node->class_);
3213 return (node && node->class_->abstract)? node->class_->abstract(node):"none";
3214 }
3215
3216 SYNCTEX_INLINE static void _synctex_display_child(synctex_node_p node) {
3217 synctex_node_p N = _synctex_tree_child(node);
3218 if (N) {
3219 _synctex_scanner_display_prompt_down(N->class_->scanner);
3220 synctex_node_display(N);
3221 _synctex_scanner_display_prompt_up(N->class_->scanner);
3222 }
3223 }
3224
3225 SYNCTEX_INLINE static void _synctex_display_sibling(synctex_node_p node) {
3226 synctex_node_display(__synctex_tree_sibling(node));
3227 }
3228 #define SYNCTEX_ABSTRACT_MAX 128
3229 static char * _synctex_abstract_input(synctex_node_p node) {
3230 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3231 if (node) {
3232 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"Input:%i:%s(%i)" SYNCTEX_PRINT_CHARINDEX_FMT,
3233 _synctex_data_tag(node),
3234 _synctex_data_name(node),
3235 _synctex_data_line(node)
3236 SYNCTEX_PRINT_CHARINDEX_WHAT);
3237 }
3238 return abstract;
3239 }
3240
3241 static void _synctex_display_input(synctex_node_p node) {
3242 if (node) {
3243 printf("Input:%i:%s(%i)"
3244 SYNCTEX_PRINT_CHARINDEX_FMT
3245 "\n",
3246 _synctex_data_tag(node),
3247 _synctex_data_name(node),
3248 _synctex_data_line(node)
3249 SYNCTEX_PRINT_CHARINDEX_WHAT);
3250 synctex_node_display(__synctex_tree_sibling(node));
3251 }
3252 }
3253
3254 static char * _synctex_abstract_sheet(synctex_node_p node) {
3255 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3256 if (node) {
3257 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"{%i...}" SYNCTEX_PRINT_CHARINDEX_FMT,
3258 _synctex_data_page(node)
3259 SYNCTEX_PRINT_CHARINDEX_WHAT);
3260 }
3261 return abstract;
3262 }
3263
3264 static void _synctex_display_sheet(synctex_node_p node) {
3265 if (node) {
3266 printf("%s{%i"
3267 SYNCTEX_PRINT_CHARINDEX_FMT
3268 "\n",
3269 node->class_->scanner->display_prompt,
3270 _synctex_data_page(node)
3271 SYNCTEX_PRINT_CHARINDEX_WHAT);
3272 _synctex_display_child(node);
3273 printf("%s}\n",node->class_->scanner->display_prompt);
3274 _synctex_display_sibling(node);
3275 }
3276 }
3277
3278 static char * _synctex_abstract_form(synctex_node_p node) {
3279 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3280 if (node) {
3281 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"<%i...>" SYNCTEX_PRINT_CHARINDEX_FMT,
3282 _synctex_data_tag(node)
3283 SYNCTEX_PRINT_CHARINDEX_WHAT);
3284 SYNCTEX_PRINT_CHARINDEX;
3285 }
3286 return abstract;
3287 }
3288
3289 static void _synctex_display_form(synctex_node_p node) {
3290 if (node) {
3291 printf("%s<%i"
3292 SYNCTEX_PRINT_CHARINDEX_FMT
3293 "\n",
3294 node->class_->scanner->display_prompt,
3295 _synctex_data_tag(node)
3296 SYNCTEX_PRINT_CHARINDEX_WHAT);
3297 _synctex_display_child(node);
3298 printf("%s>\n",node->class_->scanner->display_prompt);
3299 _synctex_display_sibling(node);
3300 }
3301 }
3302
3303 static char * _synctex_abstract_vbox(synctex_node_p node) {
3304 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3305 if (node) {
3306 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"[%i,%i:%i,%i:%i,%i,%i...]"
3307 SYNCTEX_PRINT_CHARINDEX_FMT,
3308 _synctex_data_tag(node),
3309 _synctex_data_line(node),
3310 _synctex_data_h(node),
3311 _synctex_data_v(node),
3312 _synctex_data_width(node),
3313 _synctex_data_height(node),
3314 _synctex_data_depth(node)
3315 SYNCTEX_PRINT_CHARINDEX_WHAT);
3316 }
3317 return abstract;
3318 }
3319
3320 static void _synctex_display_vbox(synctex_node_p node) {
3321 if (node) {
3322 printf("%s[%i,%i:%i,%i:%i,%i,%i"
3323 SYNCTEX_PRINT_CHARINDEX_FMT
3324 "\n",
3325 node->class_->scanner->display_prompt,
3326 _synctex_data_tag(node),
3327 _synctex_data_line(node),
3328 _synctex_data_h(node),
3329 _synctex_data_v(node),
3330 _synctex_data_width(node),
3331 _synctex_data_height(node),
3332 _synctex_data_depth(node)
3333 SYNCTEX_PRINT_CHARINDEX_WHAT);
3334 _synctex_display_child(node);
3335 printf("%s]\n%slast:%s\n",
3336 node->class_->scanner->display_prompt,
3337 node->class_->scanner->display_prompt,
3338 _synctex_node_abstract(_synctex_tree_last(node)));
3339 _synctex_display_sibling(node);
3340 }
3341 }
3342
3343 static char * _synctex_abstract_hbox(synctex_node_p node) {
3344 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3345 if (node) {
3346 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"(%i,%i~%i*%i:%i,%i:%i,%i,%i...)"
3347 SYNCTEX_PRINT_CHARINDEX_FMT,
3348 _synctex_data_tag(node),
3349 _synctex_data_line(node),
3350 _synctex_data_mean_line(node),
3351 _synctex_data_weight(node),
3352 _synctex_data_h(node),
3353 _synctex_data_v(node),
3354 _synctex_data_width(node),
3355 _synctex_data_height(node),
3356 _synctex_data_depth(node)
3357 SYNCTEX_PRINT_CHARINDEX_WHAT);
3358 }
3359 return abstract;
3360 }
3361
3362 static void _synctex_display_hbox(synctex_node_p node) {
3363 if (node) {
3364 printf("%s(%i,%i~%i*%i:%i,%i:%i,%i,%i"
3365 SYNCTEX_PRINT_CHARINDEX_FMT
3366 "\n",
3367 node->class_->scanner->display_prompt,
3368 _synctex_data_tag(node),
3369 _synctex_data_line(node),
3370 _synctex_data_mean_line(node),
3371 _synctex_data_weight(node),
3372 _synctex_data_h(node),
3373 _synctex_data_v(node),
3374 _synctex_data_width(node),
3375 _synctex_data_height(node),
3376 _synctex_data_depth(node)
3377 SYNCTEX_PRINT_CHARINDEX_WHAT);
3378 _synctex_display_child(node);
3379 printf("%s)\n%slast:%s\n",
3380 node->class_->scanner->display_prompt,
3381 node->class_->scanner->display_prompt,
3382 _synctex_node_abstract(_synctex_tree_last(node)));
3383 _synctex_display_sibling(node);
3384 }
3385 }
3386
3387 static char * _synctex_abstract_void_vbox(synctex_node_p node) {
3388 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3389 if (node) {
3390 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"v%i,%i;%i,%i:%i,%i,%i"
3391 SYNCTEX_PRINT_CHARINDEX_FMT
3392 "\n",
3393 _synctex_data_tag(node),
3394 _synctex_data_line(node),
3395 _synctex_data_h(node),
3396 _synctex_data_v(node),
3397 _synctex_data_width(node),
3398 _synctex_data_height(node),
3399 _synctex_data_depth(node)
3400 SYNCTEX_PRINT_CHARINDEX_WHAT);
3401 }
3402 return abstract;
3403 }
3404
3405 static void _synctex_display_void_vbox(synctex_node_p node) {
3406 if (node) {
3407 printf("%sv%i,%i;%i,%i:%i,%i,%i"
3408 SYNCTEX_PRINT_CHARINDEX_FMT
3409 "\n",
3410 node->class_->scanner->display_prompt,
3411 _synctex_data_tag(node),
3412 _synctex_data_line(node),
3413 _synctex_data_h(node),
3414 _synctex_data_v(node),
3415 _synctex_data_width(node),
3416 _synctex_data_height(node),
3417 _synctex_data_depth(node)
3418 SYNCTEX_PRINT_CHARINDEX_WHAT);
3419 _synctex_display_sibling(node);
3420 }
3421 }
3422
3423 static char * _synctex_abstract_void_hbox(synctex_node_p node) {
3424 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3425 if (node) {
3426 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"h%i,%i:%i,%i:%i,%i,%i"
3427 SYNCTEX_PRINT_CHARINDEX_FMT,
3428 _synctex_data_tag(node),
3429 _synctex_data_line(node),
3430 _synctex_data_h(node),
3431 _synctex_data_v(node),
3432 _synctex_data_width(node),
3433 _synctex_data_height(node),
3434 _synctex_data_depth(node)
3435 SYNCTEX_PRINT_CHARINDEX_WHAT);
3436 }
3437 return abstract;
3438 }
3439
3440 static void _synctex_display_void_hbox(synctex_node_p node) {
3441 if (node) {
3442 printf("%sh%i,%i:%i,%i:%i,%i,%i"
3443 SYNCTEX_PRINT_CHARINDEX_FMT
3444 "\n",
3445 node->class_->scanner->display_prompt,
3446 _synctex_data_tag(node),
3447 _synctex_data_line(node),
3448 _synctex_data_h(node),
3449 _synctex_data_v(node),
3450 _synctex_data_width(node),
3451 _synctex_data_height(node),
3452 _synctex_data_depth(node)
3453 SYNCTEX_PRINT_CHARINDEX_WHAT);
3454 _synctex_display_sibling(node);
3455 }
3456 }
3457
3458 static char * _synctex_abstract_glue(synctex_node_p node) {
3459 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3460 if (node) {
3461 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"glue:%i,%i:%i,%i"
3462 SYNCTEX_PRINT_CHARINDEX_FMT,
3463 _synctex_data_tag(node),
3464 _synctex_data_line(node),
3465 _synctex_data_h(node),
3466 _synctex_data_v(node)
3467 SYNCTEX_PRINT_CHARINDEX_WHAT);
3468 }
3469 return abstract;
3470 }
3471
3472 static void _synctex_display_glue(synctex_node_p node) {
3473 if (node) {
3474 printf("%sglue:%i,%i:%i,%i"
3475 SYNCTEX_PRINT_CHARINDEX_FMT
3476 "\n",
3477 node->class_->scanner->display_prompt,
3478 _synctex_data_tag(node),
3479 _synctex_data_line(node),
3480 _synctex_data_h(node),
3481 _synctex_data_v(node)
3482 SYNCTEX_PRINT_CHARINDEX_WHAT);
3483 _synctex_display_sibling(node);
3484 }
3485 }
3486
3487 static char * _synctex_abstract_rule(synctex_node_p node) {
3488 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3489 if (node) {
3490 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"rule:%i,%i:%i,%i:%i,%i,%i"
3491 SYNCTEX_PRINT_CHARINDEX_FMT,
3492 _synctex_data_tag(node),
3493 _synctex_data_line(node),
3494 _synctex_data_h(node),
3495 _synctex_data_v(node),
3496 _synctex_data_width(node),
3497 _synctex_data_height(node),
3498 _synctex_data_depth(node)
3499 SYNCTEX_PRINT_CHARINDEX_WHAT);
3500 }
3501 return abstract;
3502 }
3503
3504 static void _synctex_display_rule(synctex_node_p node) {
3505 if (node) {
3506 printf("%srule:%i,%i:%i,%i:%i,%i,%i"
3507 SYNCTEX_PRINT_CHARINDEX_FMT
3508 "\n",
3509 node->class_->scanner->display_prompt,
3510 _synctex_data_tag(node),
3511 _synctex_data_line(node),
3512 _synctex_data_h(node),
3513 _synctex_data_v(node),
3514 _synctex_data_width(node),
3515 _synctex_data_height(node),
3516 _synctex_data_depth(node)
3517 SYNCTEX_PRINT_CHARINDEX_WHAT);
3518 _synctex_display_sibling(node);
3519 }
3520 }
3521
3522 static char * _synctex_abstract_math(synctex_node_p node) {
3523 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3524 if (node) {
3525 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"math:%i,%i:%i,%i"
3526 SYNCTEX_PRINT_CHARINDEX_FMT,
3527 _synctex_data_tag(node),
3528 _synctex_data_line(node),
3529 _synctex_data_h(node),
3530 _synctex_data_v(node)
3531 SYNCTEX_PRINT_CHARINDEX_WHAT);
3532 }
3533 return abstract;
3534 }
3535
3536 static void _synctex_display_math(synctex_node_p node) {
3537 if (node) {
3538 printf("%smath:%i,%i:%i,%i"
3539 SYNCTEX_PRINT_CHARINDEX_FMT
3540 "\n",
3541 node->class_->scanner->display_prompt,
3542 _synctex_data_tag(node),
3543 _synctex_data_line(node),
3544 _synctex_data_h(node),
3545 _synctex_data_v(node)
3546 SYNCTEX_PRINT_CHARINDEX_WHAT);
3547 _synctex_display_sibling(node);
3548 }
3549 }
3550
3551 static char * _synctex_abstract_kern(synctex_node_p node) {
3552 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3553 if (node) {
3554 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"kern:%i,%i:%i,%i:%i"
3555 SYNCTEX_PRINT_CHARINDEX_FMT,
3556 _synctex_data_tag(node),
3557 _synctex_data_line(node),
3558 _synctex_data_h(node),
3559 _synctex_data_v(node),
3560 _synctex_data_width(node)
3561 SYNCTEX_PRINT_CHARINDEX_WHAT);
3562 }
3563 return abstract;
3564 }
3565
3566 static void _synctex_display_kern(synctex_node_p node) {
3567 if (node) {
3568 printf("%skern:%i,%i:%i,%i:%i"
3569 SYNCTEX_PRINT_CHARINDEX_FMT
3570 "\n",
3571 node->class_->scanner->display_prompt,
3572 _synctex_data_tag(node),
3573 _synctex_data_line(node),
3574 _synctex_data_h(node),
3575 _synctex_data_v(node),
3576 _synctex_data_width(node)
3577 SYNCTEX_PRINT_CHARINDEX_WHAT);
3578 _synctex_display_sibling(node);
3579 }
3580 }
3581
3582 static char * _synctex_abstract_boundary(synctex_node_p node) {
3583 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3584 if (node) {
3585 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"boundary:%i,%i:%i,%i"
3586 SYNCTEX_PRINT_CHARINDEX_FMT,
3587 _synctex_data_tag(node),
3588 _synctex_data_line(node),
3589 _synctex_data_h(node),
3590 _synctex_data_v(node)
3591 SYNCTEX_PRINT_CHARINDEX_WHAT);
3592 }
3593 return abstract;
3594 }
3595
3596 static void _synctex_display_boundary(synctex_node_p node) {
3597 if (node) {
3598 printf("%sboundary:%i,%i:%i,%i"
3599 SYNCTEX_PRINT_CHARINDEX_FMT
3600 "\n",
3601 node->class_->scanner->display_prompt,
3602 _synctex_data_tag(node),
3603 _synctex_data_line(node),
3604 _synctex_data_h(node),
3605 _synctex_data_v(node)
3606 SYNCTEX_PRINT_CHARINDEX_WHAT);
3607 _synctex_display_sibling(node);
3608 }
3609 }
3610
3611 static char * _synctex_abstract_box_bdry(synctex_node_p node) {
3612 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3613 if (node) {
3614 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"box bdry:%i,%i:%i,%i" SYNCTEX_PRINT_CHARINDEX_FMT,
3615 _synctex_data_tag(node),
3616 _synctex_data_line(node),
3617 _synctex_data_h(node),
3618 _synctex_data_v(node)
3619 SYNCTEX_PRINT_CHARINDEX_WHAT);
3620 }
3621 return abstract;
3622 }
3623
3624 static void _synctex_display_box_bdry(synctex_node_p node) {
3625 if (node) {
3626 printf("%sbox bdry:%i,%i:%i,%i",
3627 node->class_->scanner->display_prompt,
3628 _synctex_data_tag(node),
3629 _synctex_data_line(node),
3630 _synctex_data_h(node),
3631 _synctex_data_v(node));
3632 SYNCTEX_PRINT_CHARINDEX_NL;
3633 _synctex_display_sibling(node);
3634 }
3635 }
3636
3637 static char * _synctex_abstract_ref(synctex_node_p node) {
3638 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3639 if (node) {
3640 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"form ref:%i:%i,%i" SYNCTEX_PRINT_CHARINDEX_FMT,
3641 _synctex_data_tag(node),
3642 _synctex_data_h(node),
3643 _synctex_data_v(node)
3644 SYNCTEX_PRINT_CHARINDEX_WHAT);
3645 }
3646 return abstract;
3647 }
3648
3649 static void _synctex_display_ref(synctex_node_p node) {
3650 if (node) {
3651 printf("%sform ref:%i:%i,%i",
3652 node->class_->scanner->display_prompt,
3653 _synctex_data_tag(node),
3654 _synctex_data_h(node),
3655 _synctex_data_v(node));
3656 SYNCTEX_PRINT_CHARINDEX_NL;
3657 _synctex_display_sibling(node);
3658 }
3659 }
3660 static char * _synctex_abstract_proxy(synctex_node_p node) {
3661 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3662 if (node) {
3663 synctex_node_p N = _synctex_tree_target(node);
3664 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"%s:%i,%i:%i,%i/%p%s",
3665 synctex_node_isa(node),
3666 synctex_node_tag(node),
3667 synctex_node_line(node),
3668 _synctex_data_h(node),
3669 _synctex_data_v(node),
3670 (void*)node, // Fix GCC warning: %p expects a void* according to POSIX
3671 _synctex_node_abstract(N));
3672 }
3673 return abstract;
3674 }
3675 static void _synctex_display_proxy(synctex_node_p node) {
3676 if (node) {
3677 synctex_node_p N = _synctex_tree_target(node);
3678 printf("%s%s:%i,%i:%i,%i",
3679 node->class_->scanner->display_prompt,
3680 synctex_node_isa(node),
3681 synctex_node_tag(node),
3682 synctex_node_line(node),
3683 _synctex_data_h(node),
3684 _synctex_data_v(node));
3685 if (N) {
3686 printf("=%i,%i:%i,%i,%i->%s",
3687 synctex_node_h(node),
3688 synctex_node_v(node),
3689 synctex_node_width(node),
3690 synctex_node_height(node),
3691 synctex_node_depth(node),
3692 _synctex_node_abstract(N));
3693 }
3694 printf("\n");
3695 _synctex_display_child(node);
3696 _synctex_display_sibling(node);
3697 }
3698 }
3699 static char * _synctex_abstract_proxy_vbox(synctex_node_p node) {
3700 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3701 if (node) {
3702 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,
3703 "[*%i,%i:%i,%i:%i,%i,%i...*]"
3704 SYNCTEX_PRINT_CHARINDEX_FMT,
3705 synctex_node_tag(node),
3706 synctex_node_line(node),
3707 synctex_node_h(node),
3708 synctex_node_v(node),
3709 synctex_node_width(node),
3710 synctex_node_height(node),
3711 synctex_node_depth(node)
3712 SYNCTEX_PRINT_CHARINDEX_WHAT);
3713 }
3714 return abstract;
3715 }
3716
3717 static void _synctex_display_proxy_vbox(synctex_node_p node) {
3718 if (node) {
3719 printf("%s[*%i,%i:%i,%i:%i,%i,%i"
3720 SYNCTEX_PRINT_CHARINDEX_FMT
3721 "\n",
3722 node->class_->scanner->display_prompt,
3723 synctex_node_tag(node),
3724 synctex_node_line(node),
3725 synctex_node_h(node),
3726 synctex_node_v(node),
3727 synctex_node_width(node),
3728 synctex_node_height(node),
3729 synctex_node_depth(node)
3730 SYNCTEX_PRINT_CHARINDEX_WHAT);
3731 _synctex_display_child(node);
3732 printf("%s*]\n%slast:%s\n",
3733 node->class_->scanner->display_prompt,
3734 node->class_->scanner->display_prompt,
3735 _synctex_node_abstract(_synctex_tree_last(node)));
3736 _synctex_display_sibling(node);
3737 }
3738 }
3739
3740 static char * _synctex_abstract_proxy_hbox(synctex_node_p node) {
3741 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3742 if (node) {
3743 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"(*%i,%i~%i*%i:%i,%i:%i,%i,%i...*)/%p"
3744 SYNCTEX_PRINT_CHARINDEX_FMT,
3745 synctex_node_tag(node),
3746 synctex_node_line(node),
3747 synctex_node_mean_line(node),
3748 synctex_node_weight(node),
3749 synctex_node_h(node),
3750 synctex_node_v(node),
3751 synctex_node_width(node),
3752 synctex_node_height(node),
3753 synctex_node_depth(node),
3754 (void*)node // Fix GCC warning: %p expects a void* according to POSIX
3755 SYNCTEX_PRINT_CHARINDEX_WHAT);
3756 }
3757 return abstract;
3758 }
3759
3760 static void _synctex_display_proxy_hbox(synctex_node_p node) {
3761 if (node) {
3762 printf("%s(*%i,%i~%i*%i:%i,%i:%i,%i,%i"
3763 SYNCTEX_PRINT_CHARINDEX_FMT
3764 "\n",
3765 node->class_->scanner->display_prompt,
3766 synctex_node_tag(node),
3767 synctex_node_line(node),
3768 synctex_node_mean_line(node),
3769 synctex_node_weight(node),
3770 synctex_node_h(node),
3771 synctex_node_v(node),
3772 synctex_node_width(node),
3773 synctex_node_height(node),
3774 synctex_node_depth(node)
3775 SYNCTEX_PRINT_CHARINDEX_WHAT);
3776 _synctex_display_child(node);
3777 printf("%s*)\n%slast:%s\n",
3778 node->class_->scanner->display_prompt,
3779 node->class_->scanner->display_prompt,
3780 _synctex_node_abstract(_synctex_tree_last(node)));
3781 _synctex_display_sibling(node);
3782 }
3783 }
3784
3785 static char * _synctex_abstract_handle(synctex_node_p node) {
3786 static char abstract[SYNCTEX_ABSTRACT_MAX] = "none";
3787 if (node) {
3788 synctex_node_p N = _synctex_tree_target(node);
3789 if (N && !N->class_) {
3790 exit(1);
3791 }
3792 snprintf(abstract,SYNCTEX_ABSTRACT_MAX,"%s:%s",
3793 synctex_node_isa(node),
3794 (N?_synctex_node_abstract(N):""));
3795 }
3796 return abstract;
3797 }
3798 static void _synctex_display_handle(synctex_node_p node) {
3799 if (node) {
3800 synctex_node_p N = _synctex_tree_target(node);
3801 printf("%s%s(%i):->%s\n",
3802 node->class_->scanner->display_prompt,
3803 synctex_node_isa(node),
3804 _synctex_data_weight(N),
3805 _synctex_node_abstract(N));
3806 _synctex_display_child(node);
3807 _synctex_display_sibling(node);
3808 }
3809 }
3810 # ifdef SYNCTEX_NOTHING
3811 # pragma mark -
3812 # pragma mark STATUS
3813 # endif
3814
3815 # ifdef SYNCTEX_NOTHING
3816 # pragma mark -
3817 # pragma mark Prototypes
3818 # endif
3819 typedef struct {
3820 size_t size;
3821 synctex_status_t status;
3822 } synctex_zs_s;
3823 static synctex_zs_s _synctex_buffer_get_available_size(synctex_scanner_p scanner, size_t size);
3824 static synctex_status_t _synctex_next_line(synctex_scanner_p scanner);
3825 static synctex_status_t _synctex_match_string(synctex_scanner_p scanner, const char * the_string);
3826
3827 typedef struct synctex_ns_t {
3828 synctex_node_p node;
3829 synctex_status_t status;
3830 } synctex_ns_s;
3831 static synctex_ns_s __synctex_parse_new_input(synctex_scanner_p scanner);
3832 static synctex_status_t _synctex_scan_preamble(synctex_scanner_p scanner);
3833 typedef struct {
3834 float value;
3835 synctex_status_t status;
3836 } synctex_fs_s;
3837 static synctex_fs_s _synctex_scan_float_and_dimension(synctex_scanner_p scanner);
3838 static synctex_status_t _synctex_scan_post_scriptum(synctex_scanner_p scanner);
3839 static synctex_status_t _synctex_scan_postamble(synctex_scanner_p scanner);
3840 static synctex_status_t _synctex_setup_visible_hbox(synctex_node_p box);
3841 static synctex_status_t _synctex_scan_content(synctex_scanner_p scanner);
3842 int synctex_scanner_pre_x_offset(synctex_scanner_p scanner);
3843 int synctex_scanner_pre_y_offset(synctex_scanner_p scanner);
3844 const char * synctex_scanner_get_output_fmt(synctex_scanner_p scanner);
3845
3846 # ifdef SYNCTEX_NOTHING
3847 # pragma mark -
3848 # pragma mark SCANNER UTILITIES
3849 # endif
3850
3851 # define SYNCTEX_FILE (scanner->reader->file)
3852
3853 /**
3854 * Try to ensure that the buffer contains at least size bytes.
3855 * Passing a huge size argument means the whole buffer length.
3856 * Passing a 0 size argument means return the available buffer length, without reading the file.
3857 * In that case, the return status is always SYNCTEX_STATUS_OK unless the given scanner is NULL.
3858 * The size_t value returned is the number of bytes now available in the buffer. This is a nonnegative integer, it may take the value 0.
3859 * It is the responsibility of the caller to test whether this size is conforming to its needs.
3860 * Negative values may return in case of error, actually
3861 * when there was an error reading the synctex file.
3862 * - parameter scanner: The owning scanner. When NULL, returns SYNCTEX_STATUS_BAD_ARGUMENT.
3863 * - parameter expected: expected number of bytes.
3864 * - returns: a size and a status.
3865 */
3866 static synctex_zs_s _synctex_buffer_get_available_size(synctex_scanner_p scanner, size_t expected) {
3867 size_t size = 0;
3868 if (NULL == scanner) {
3869 return (synctex_zs_s){0,SYNCTEX_STATUS_BAD_ARGUMENT};
3870 }
3871 if (expected>scanner->reader->size){
3872 expected = scanner->reader->size;
3873 }
3874 size = SYNCTEX_END - SYNCTEX_CUR; /* available is the number of unparsed chars in the buffer */
3875 if (expected<=size) {
3876 /* There are already sufficiently many characters in the buffer */
3877 return (synctex_zs_s){size,SYNCTEX_STATUS_OK};
3878 }
3879 if (SYNCTEX_FILE) {
3880 /* Copy the remaining part of the buffer to the beginning,
3881 * then read the next part of the file */
3882 int already_read = 0;
3883 # if defined(SYNCTEX_USE_CHARINDEX)
3884 scanner->reader->charindex_offset += SYNCTEX_CUR - SYNCTEX_START;
3885 # endif
3886 if (size) {
3887 memmove(SYNCTEX_START, SYNCTEX_CUR, size);
3888 }
3889 SYNCTEX_CUR = SYNCTEX_START + size; /* the next character after the move, will change. */
3890 /* Fill the buffer up to its end */
3891 already_read = gzread(SYNCTEX_FILE,(void *)SYNCTEX_CUR,(int)(SYNCTEX_BUFFER_SIZE - size));
3892 if (already_read>0) {
3893 /* We assume that 0<already_read<=SYNCTEX_BUFFER_SIZE - size, such that
3894 * SYNCTEX_CUR + already_read = SYNCTEX_START + size + already_read <= SYNCTEX_START + SYNCTEX_BUFFER_SIZE */
3895 SYNCTEX_END = SYNCTEX_CUR + already_read;
3896 /* If the end of the file was reached, all the required SYNCTEX_BUFFER_SIZE - available
3897 * may not be filled with values from the file.
3898 * In that case, the buffer should stop properly after already_read characters. */
3899 * SYNCTEX_END = '\0'; /* there is enough room */
3900 SYNCTEX_CUR = SYNCTEX_START;
3901 /* May be available is less than size, the caller will have to test. */
3902 return (synctex_zs_s){SYNCTEX_END - SYNCTEX_CUR,SYNCTEX_STATUS_OK};
3903 } else if (0>already_read) {
3904 /* There is a possible error in reading the file */
3905 int errnum = 0;
3906 const char * error_string = gzerror(SYNCTEX_FILE, &errnum);
3907 if (Z_ERRNO == errnum) {
3908 /* There is an error in zlib caused by the file system */
3909 _synctex_error("gzread error from the file system (%i)",errno);
3910 return (synctex_zs_s){0,SYNCTEX_STATUS_ERROR};
3911 } else if (errnum) {
3912 _synctex_error("gzread error (%i:%i,%s)",already_read,errnum,error_string);
3913 return (synctex_zs_s){0,SYNCTEX_STATUS_ERROR};
3914 }
3915 }
3916 /* Nothing was read, we are at the end of the file. */
3917 gzclose(SYNCTEX_FILE);
3918 SYNCTEX_FILE = NULL;
3919 SYNCTEX_END = SYNCTEX_CUR;
3920 SYNCTEX_CUR = SYNCTEX_START;
3921 * SYNCTEX_END = '\0';/* Terminate the string properly.*/
3922 /* there might be a bit of text left */
3923 return (synctex_zs_s){SYNCTEX_END - SYNCTEX_CUR,SYNCTEX_STATUS_EOF};
3924 }
3925 /* We cannot enlarge the buffer because the end of the file was reached. */
3926 return (synctex_zs_s){size,SYNCTEX_STATUS_EOF};
3927 }
3928
3929 /* Used when parsing the synctex file.
3930 * Advance to the next character starting a line.
3931 * Actually, only '\n' is recognized as end of line marker.
3932 * On normal completion, the returned value is the number of unparsed characters available in the buffer.
3933 * In general, it is a positive value, 0 meaning that the end of file was reached.
3934 * -1 is returned in case of error, actually because there was an error while feeding the buffer.
3935 * When the function returns with no error, SYNCTEX_CUR points to the first character of the next line, if any.
3936 * J. Laurens: Sat May 10 07:52:31 UTC 2008
3937 */
3938 static synctex_status_t _synctex_next_line(synctex_scanner_p scanner) {
3939 synctex_status_t status = SYNCTEX_STATUS_OK;
3940 if (NULL == scanner) {
3941 return SYNCTEX_STATUS_BAD_ARGUMENT;
3942 }
3943 infinite_loop:
3944 while(SYNCTEX_CUR<SYNCTEX_END) {
3945 if (*SYNCTEX_CUR == '\n') {
3946 ++SYNCTEX_CUR;
3947 ++scanner->reader->line_number;
3948 return _synctex_buffer_get_available_size(scanner, 1).status;
3949 }
3950 ++SYNCTEX_CUR;
3951 }
3952 /* Here, we have SYNCTEX_CUR == SYNCTEX_END, such that the next call to _synctex_buffer_get_available_size
3953 * will read another bunch of synctex file. Little by little, we advance to the end of the file. */
3954 status = _synctex_buffer_get_available_size(scanner, 1).status;
3955 if (status<=SYNCTEX_STATUS_EOF) {
3956 return status;
3957 }
3958 goto infinite_loop;
3959 }
3960
3961 /* Scan the given string.
3962 * Both scanner and the_string must not be NULL, and the_string must not be 0 length.
3963 * SYNCTEX_STATUS_OK is returned if the string is found,
3964 * SYNCTEX_STATUS_EOF is returned when the EOF is reached,
3965 * SYNCTEX_STATUS_NOT_OK is returned is the string is not found,
3966 * an error status is returned otherwise.
3967 * This is a critical method because buffering renders things more difficult.
3968 * The given string might be as long as the maximum size_t value.
3969 * As side effect, the buffer state may have changed if the given argument string can't fit into the buffer.
3970 */
3971 static synctex_status_t _synctex_match_string(synctex_scanner_p scanner, const char * the_string) {
3972 size_t tested_len = 0; /* the number of characters at the beginning of the_string that match */
3973 size_t remaining_len = 0; /* the number of remaining characters of the_string that should match */
3974 size_t available = 0;
3975 synctex_zs_s zs = {0,0};
3976 if (NULL == scanner || NULL == the_string) {
3977 return SYNCTEX_STATUS_BAD_ARGUMENT;
3978 }
3979 remaining_len = strlen(the_string); /* All the_string should match */
3980 if (0 == remaining_len) {
3981 return SYNCTEX_STATUS_BAD_ARGUMENT;
3982 }
3983 /* How many characters available in the buffer? */
3984 zs = _synctex_buffer_get_available_size(scanner,remaining_len);
3985 if (zs.status<SYNCTEX_STATUS_EOF) {
3986 return zs.status;
3987 }
3988 /* Maybe we have less characters than expected because the buffer is too small. */
3989 if (zs.size>=remaining_len) {
3990 /* The buffer is sufficiently big to hold the expected number of characters. */
3991 if (strncmp((char *)SYNCTEX_CUR,the_string,remaining_len)) {
3992 return SYNCTEX_STATUS_NOT_OK;
3993 }
3994 return_OK:
3995 /* Advance SYNCTEX_CUR to the next character after the_string. */
3996 SYNCTEX_CUR += remaining_len;
3997 return SYNCTEX_STATUS_OK;
3998 } else if (strncmp((char *)SYNCTEX_CUR,the_string,zs.size)) {
3999 /* No need to go further, this is not the expected string in the buffer. */
4000 return SYNCTEX_STATUS_NOT_OK;
4001 } else if (SYNCTEX_FILE) {
4002 /* The buffer was too small to contain remaining_len characters.
4003 * We have to cut the string into pieces. */
4004 z_off_t offset = 0L;
4005 /* the first part of the string is found, advance the_string to the next untested character. */
4006 the_string += zs.size;
4007 /* update the remaining length and the parsed length. */
4008 remaining_len -= zs.size;
4009 tested_len += zs.size;
4010 SYNCTEX_CUR += zs.size; /* We validate the tested characters. */
4011 if (0 == remaining_len) {
4012 /* Nothing left to test, we have found the given string. */
4013 return SYNCTEX_STATUS_OK;
4014 }
4015 /* We also have to record the current state of the file cursor because
4016 * if the_string does not match, all this should be a totally blank operation,
4017 * for which the file and buffer states should not be modified at all.
4018 * In fact, the states of the buffer before and after this function are in general different
4019 * but they are totally equivalent as long as the values of the buffer before SYNCTEX_CUR
4020 * can be safely discarded. */
4021 offset = gztell(SYNCTEX_FILE);
4022 /* offset now corresponds to the first character of the file that was not buffered. */
4023 /* SYNCTEX_CUR - SYNCTEX_START is the number of chars that where already buffered and
4024 * that match the head of the_string. If in fine the_string does not match, all these chars must be recovered
4025 * because the whole buffer contents is replaced in _synctex_buffer_get_available_size.
4026 * They were buffered from offset-len location in the file. */
4027 offset -= SYNCTEX_CUR - SYNCTEX_START;
4028 more_characters:
4029 /* There is still some work to be done, so read another bunch of file.
4030 * This is the second call to _synctex_buffer_get_available_size,
4031 * which means that the actual contents of the buffer will be discarded.
4032 * We will definitely have to recover the previous state in case we do not find the expected string. */
4033 zs = _synctex_buffer_get_available_size(scanner,remaining_len);
4034 if (zs.status<SYNCTEX_STATUS_EOF) {
4035 return zs.status; /* This is an error, no need to go further. */
4036 }
4037 if (zs.size==0) {
4038 /* Missing characters: recover the initial state of the file and return. */
4039 return_NOT_OK:
4040 if (offset != gzseek(SYNCTEX_FILE,offset,SEEK_SET)) {
4041 /* This is a critical error, we could not recover the previous state. */
4042 _synctex_error("Can't seek file");
4043 return SYNCTEX_STATUS_ERROR;
4044 }
4045 /* Next time we are asked to fill the buffer,
4046 * we will read a complete bunch of text from the file. */
4047 SYNCTEX_CUR = SYNCTEX_END;
4048 return SYNCTEX_STATUS_NOT_OK;
4049 }
4050 if (zs.size<remaining_len) {
4051 /* We'll have to loop one more time. */
4052 if (strncmp((char *)SYNCTEX_CUR,the_string,zs.size)) {
4053 /* This is not the expected string, recover the previous state and return. */
4054 goto return_NOT_OK;
4055 }
4056 /* Advance the_string to the first untested character. */
4057 the_string += available;
4058 /* update the remaining length and the parsed length. */
4059 remaining_len -= zs.size;
4060 tested_len += zs.size;
4061 SYNCTEX_CUR += zs.size; /* We validate the tested characters. */
4062 goto more_characters;
4063 }
4064 /* This is the last step. */
4065 if (strncmp((char *)SYNCTEX_CUR,the_string,remaining_len)) {
4066 /* This is not the expected string, recover the previous state and return. */
4067 goto return_NOT_OK;
4068 }
4069 goto return_OK;
4070 } else {
4071 /* The buffer can't contain the given string argument, and the EOF was reached */
4072 return SYNCTEX_STATUS_EOF;
4073 }
4074 }
4075
4076 /* Used when parsing the synctex file.
4077 * Decode an integer.
4078 * First, field separators, namely ':' and ',' characters are skipped
4079 * The returned value is negative if there is an unrecoverable error.
4080 * It is SYNCTEX_STATUS_NOT_OK if an integer could not be parsed, for example
4081 * if the characters at the current cursor position are not digits or
4082 * if the end of the file has been reached.
4083 * It is SYNCTEX_STATUS_OK if an int has been successfully parsed.
4084 * The given scanner argument must not be NULL, on the contrary, value_ref may be NULL.
4085 */
4086 static synctex_is_s _synctex_decode_int(synctex_scanner_p scanner) {
4087 char * ptr = NULL;
4088 char * end = NULL;
4089 synctex_zs_s zs = {0,0};
4090 int result;
4091 if (NULL == scanner) {
4092 return (synctex_is_s){0, SYNCTEX_STATUS_BAD_ARGUMENT};
4093 }
4094 zs = _synctex_buffer_get_available_size(scanner, SYNCTEX_BUFFER_MIN_SIZE);
4095 if (zs.status<SYNCTEX_STATUS_EOF) {
4096 return (synctex_is_s){0,zs.status};
4097 }
4098 if (zs.size==0) {
4099 return (synctex_is_s){0,SYNCTEX_STATUS_NOT_OK};
4100 }
4101 ptr = SYNCTEX_CUR;
4102 /* Optionally parse the separator */
4103 if (*ptr==':' || *ptr==',') {
4104 ++ptr;
4105 --zs.size;
4106 if (zs.size==0) {
4107 return (synctex_is_s){0,SYNCTEX_STATUS_NOT_OK};
4108 }
4109 }
4110 result = (int)strtol(ptr, &end, 10);
4111 if (end>ptr) {
4112 SYNCTEX_CUR = end;
4113 return (synctex_is_s){result,SYNCTEX_STATUS_OK};
4114 }
4115 return (synctex_is_s){result,SYNCTEX_STATUS_NOT_OK};
4116 }
4117 static synctex_is_s _synctex_decode_int_opt(synctex_scanner_p scanner, int default_value) {
4118 char * ptr = NULL;
4119 char * end = NULL;
4120 synctex_zs_s zs = {0, 0};
4121 if (NULL == scanner) {
4122 return (synctex_is_s){default_value, SYNCTEX_STATUS_BAD_ARGUMENT};
4123 }
4124 zs = _synctex_buffer_get_available_size(scanner, SYNCTEX_BUFFER_MIN_SIZE);
4125 if (zs.status<SYNCTEX_STATUS_EOF) {
4126 return (synctex_is_s){default_value,zs.status};
4127 }
4128 if (zs.size==0) {
4129 return (synctex_is_s){default_value,SYNCTEX_STATUS_OK};
4130 }
4131 ptr = SYNCTEX_CUR;
4132 /* Comma separator required */
4133 if (*ptr==',') {
4134 int result;
4135 ++ptr;
4136 --zs.size;
4137 if (zs.size==0) {
4138 return (synctex_is_s){default_value,SYNCTEX_STATUS_NOT_OK};
4139 }
4140 result = (int)strtol(ptr, &end, 10);
4141 if (end>ptr) {
4142 SYNCTEX_CUR = end;
4143 return (synctex_is_s){result,SYNCTEX_STATUS_OK};
4144 }
4145 return (synctex_is_s){default_value,SYNCTEX_STATUS_NOT_OK};
4146 }
4147 return (synctex_is_s){default_value,SYNCTEX_STATUS_OK};
4148 }
4149 /* Used when parsing the synctex file.
4150 * Decode an integer for a v field.
4151 * Try the _synctex_decode_int version and set the last v field scanned.
4152 * If it does not succeed, tries to match an '=' sign,
4153 * which is a shortcut for the last v field scanned.
4154 */
4155 # define SYNCTEX_INPUT_COMEQUALS ",="
4156 static synctex_is_s _synctex_decode_int_v(synctex_scanner_p scanner) {
4157 synctex_is_s is = _synctex_decode_int(scanner);
4158 if (SYNCTEX_STATUS_OK == is.status) {
4159 scanner->reader->lastv = is.integer;
4160 return is;
4161 }
4162 is.status = _synctex_match_string(scanner,SYNCTEX_INPUT_COMEQUALS);
4163 if (is.status<SYNCTEX_STATUS_OK) {
4164 return is;
4165 }
4166 is.integer = scanner->reader->lastv;
4167 return is;
4168 }
4169
4170 /* The purpose of this function is to read a string.
4171 * A string is an array of characters from the current parser location
4172 * and before the next '\n' character.
4173 * If a string was properly decoded, it is returned in value_ref and
4174 * the cursor points to the new line marker.
4175 * The returned string was alloced on the heap, the caller is the owner and
4176 * is responsible to free it in due time,
4177 * unless it transfers the ownership to another object.
4178 * If no string is parsed, * value_ref is undefined.
4179 * The maximum length of a string that a scanner can decode is platform dependent, namely UINT_MAX.
4180 * If you just want to blindly parse the file up to the end of the current line,
4181 * use _synctex_next_line instead.
4182 * On return, the scanner cursor is unchanged if a string could not be scanned or
4183 * points to the terminating '\n' character otherwise. As a consequence,
4184 * _synctex_next_line is necessary after.
4185 * If either scanner or value_ref is NULL, it is considered as an error and
4186 * SYNCTEX_STATUS_BAD_ARGUMENT is returned.
4187 */
4188 static synctex_ss_s _synctex_decode_string(synctex_scanner_p scanner) {
4189 char * end = NULL;
4190 size_t len = 0;/* The number of bytes to copy */
4191 size_t already_len = 0;
4192 synctex_zs_s zs = {0,0};
4193 char * string = NULL;
4194 if (NULL == scanner) {
4195 return (synctex_ss_s){NULL,SYNCTEX_STATUS_BAD_ARGUMENT};
4196 }
4197 /* The buffer must at least contain one character: the '\n' end of line marker */
4198 if (SYNCTEX_CUR>=SYNCTEX_END) {
4199 more_characters:
4200 zs = _synctex_buffer_get_available_size(scanner,1);
4201 if (zs.status < SYNCTEX_STATUS_EOF) {
4202 return (synctex_ss_s){NULL,zs.status};
4203 } else if (0 == zs.size) {
4204 return (synctex_ss_s){NULL,SYNCTEX_STATUS_EOF};
4205 }
4206 }
4207 /* Now we are sure that there is at least one available character, either because
4208 * SYNCTEX_CUR was already < SYNCTEX_END, or because the buffer has been properly filled. */
4209 /* end will point to the next unparsed '\n' character in the file, when mapped to the buffer. */
4210 end = SYNCTEX_CUR;
4211 /* We scan all the characters up to the next '\n' */
4212 while (end<SYNCTEX_END && *end != '\n') {
4213 ++end;
4214 }
4215 /* OK, we found where to stop:
4216 * either end == SYNCTEX_END
4217 * or *end == '\n' */
4218 len = end - SYNCTEX_CUR;
4219 if (len<UINT_MAX-already_len) {
4220 if ((string = realloc(string,len+already_len+1)) != NULL) {
4221 if (memcpy(string+already_len,SYNCTEX_CUR,len)) {
4222 already_len += len;
4223 string[already_len]='\0'; /* Terminate the string */
4224 SYNCTEX_CUR += len;/* Eventually advance to the terminating '\n' */
4225 if (SYNCTEX_CUR==SYNCTEX_END) {
4226 /* No \n found*/
4227 goto more_characters;
4228 }
4229 /* trim the trailing whites */
4230 len = already_len;
4231 while (len>0) {
4232 already_len = len--;
4233 if (string[len]!=' ') {
4234 break;
4235 }
4236 }
4237 string[already_len] = '\0';
4238 return (synctex_ss_s){string,SYNCTEX_STATUS_OK};
4239 }
4240 free(string);
4241 _synctex_error("could not copy memory (1).");
4242 return (synctex_ss_s){NULL,SYNCTEX_STATUS_ERROR};
4243 }
4244 }
4245 _synctex_error("could not (re)allocate memory (1).");
4246 return (synctex_ss_s){NULL,SYNCTEX_STATUS_ERROR};
4247 }
4248
4249 /* Used when parsing the synctex file.
4250 * Read an Input record.
4251 * - parameter scanner: non NULL scanner
4252 * - returns SYNCTEX_STATUS_OK on successful completions, others values otherwise.
4253 */
4254 static synctex_ns_s __synctex_parse_new_input(synctex_scanner_p scanner) {
4255 synctex_node_p input = NULL;
4256 synctex_status_t status = SYNCTEX_STATUS_BAD_ARGUMENT;
4257 synctex_zs_s zs = {0,0};
4258 if (NULL == scanner) {
4259 return (synctex_ns_s){NULL,status};
4260 }
4261 if ((status=_synctex_match_string(scanner,SYNCTEX_INPUT_MARK))<SYNCTEX_STATUS_OK) {
4262 return (synctex_ns_s){NULL,status};
4263 }
4264 /* Create a node */
4265 if (NULL == (input = _synctex_new_input(scanner))) {
4266 _synctex_error("Could not create an input node.");
4267 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4268 }
4269 /* Decode the tag */
4270 if ((status=_synctex_data_decode_tag(input))<SYNCTEX_STATUS_OK) {
4271 _synctex_error("Bad format of input node.");
4272 synctex_node_free(input);
4273 return (synctex_ns_s){NULL,status};
4274 }
4275 /* The next character is a field separator, we expect one character in the buffer. */
4276 zs = _synctex_buffer_get_available_size(scanner, 1);
4277 if (zs.status<=SYNCTEX_STATUS_ERROR) {
4278 return (synctex_ns_s){NULL,status};
4279 }
4280 if (0 == zs.size) {
4281 return (synctex_ns_s){NULL,SYNCTEX_STATUS_EOF};
4282 }
4283 /* We can now safely advance to the next character, stepping over the field separator. */
4284 ++SYNCTEX_CUR;
4285 --zs.size;
4286 /* Then we scan the file name */
4287 if ((status=_synctex_data_decode_name(input))<SYNCTEX_STATUS_OK) {
4288 synctex_node_free(input);
4289 _synctex_next_line(scanner);/* Ignore this whole line */
4290 return (synctex_ns_s){NULL,status};
4291 }
4292 /* Prepend this input node to the input linked list of the scanner */
4293 __synctex_tree_set_sibling(input,scanner->input);/* input has no parent */
4294 scanner->input = input;
4295 # if SYNCTEX_VERBOSE
4296 synctex_node_log(input);
4297 # endif
4298 return (synctex_ns_s){input,_synctex_next_line(scanner)};/* read the line termination character, if any */
4299 }
4300
4301 typedef synctex_is_s (*synctex_decoder_t)(synctex_scanner_p);
4302
4303 /* Used when parsing the synctex file.
4304 * Read one of the settings.
4305 * On normal completion, returns SYNCTEX_STATUS_OK.
4306 * On error, returns SYNCTEX_STATUS_ERROR.
4307 * Both arguments must not be NULL.
4308 * On return, the scanner points to the next character after the decoded object whatever it is.
4309 * It is the responsibility of the caller to prepare the scanner for the next line.
4310 */
4311 static synctex_status_t _synctex_scan_named(synctex_scanner_p scanner,const char * name) {
4312 synctex_status_t status = 0;
4313 if (NULL == scanner || NULL == name) {
4314 return SYNCTEX_STATUS_BAD_ARGUMENT;
4315 }
4316 not_found:
4317 status = _synctex_match_string(scanner,name);
4318 if (status<SYNCTEX_STATUS_NOT_OK) {
4319 return status;
4320 } else if (status == SYNCTEX_STATUS_NOT_OK) {
4321 status = _synctex_next_line(scanner);
4322 if (status<SYNCTEX_STATUS_OK) {
4323 return status;
4324 }
4325 goto not_found;
4326 }
4327 return SYNCTEX_STATUS_OK;
4328 }
4329
4330 /* Used when parsing the synctex file.
4331 * Read the preamble.
4332 */
4333 static synctex_status_t _synctex_scan_preamble(synctex_scanner_p scanner) {
4334 synctex_status_t status = 0;
4335 synctex_is_s is = {0,0};
4336 synctex_ss_s ss = {NULL,0};
4337 if (NULL == scanner) {
4338 return SYNCTEX_STATUS_BAD_ARGUMENT;
4339 }
4340 status = _synctex_scan_named(scanner,"SyncTeX Version:");
4341 if (status<SYNCTEX_STATUS_OK) {
4342 return status;
4343 }
4344 is = _synctex_decode_int(scanner);
4345 if (is.status<SYNCTEX_STATUS_OK) {
4346 return is.status;
4347 }
4348 status = _synctex_next_line(scanner);
4349 if (status<SYNCTEX_STATUS_OK) {
4350 return status;
4351 }
4352 scanner->version = is.integer;
4353 /* Read all the input records */
4354 do {
4355 status = __synctex_parse_new_input(scanner).status;
4356 if (status<SYNCTEX_STATUS_NOT_OK) {
4357 return status;
4358 }
4359 } while(status == SYNCTEX_STATUS_OK);
4360 /* the loop exits when status == SYNCTEX_STATUS_NOT_OK */
4361 /* Now read all the required settings. */
4362 if ((status=_synctex_scan_named(scanner,"Output:"))<SYNCTEX_STATUS_OK) {
4363 return status;
4364 }
4365 if ((ss=_synctex_decode_string(scanner)).status<SYNCTEX_STATUS_OK) {
4366 return is.status;
4367 }
4368 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4369 return status;
4370 }
4371 scanner->output_fmt = ss.string;
4372 if ((status=_synctex_scan_named(scanner,"Magnification:"))<SYNCTEX_STATUS_OK) {
4373 return status;
4374 }
4375 if ((is=_synctex_decode_int(scanner)).status<SYNCTEX_STATUS_OK) {
4376 return is.status;
4377 }
4378 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4379 return status;
4380 }
4381 scanner->pre_magnification = is.integer;
4382 if ((status=_synctex_scan_named(scanner,"Unit:"))<SYNCTEX_STATUS_OK) {
4383 return status;
4384 }
4385 if ((is=_synctex_decode_int(scanner)).status<SYNCTEX_STATUS_OK) {
4386 return is.status;
4387 }
4388 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4389 return status;
4390 }
4391 scanner->pre_unit = is.integer;
4392 if ((status=_synctex_scan_named(scanner,"X Offset:"))<SYNCTEX_STATUS_OK) {
4393 return status;
4394 }
4395 if ((is=_synctex_decode_int(scanner)).status<SYNCTEX_STATUS_OK) {
4396 return is.status;
4397 }
4398 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4399 return status;
4400 }
4401 scanner->pre_x_offset = is.integer;
4402 if ((status=_synctex_scan_named(scanner,"Y Offset:"))<SYNCTEX_STATUS_OK) {
4403 return status;
4404 }
4405 if ((is=_synctex_decode_int(scanner)).status<SYNCTEX_STATUS_OK) {
4406 return is.status;
4407 }
4408 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4409 return status;
4410 }
4411 scanner->pre_y_offset = is.integer;
4412 return SYNCTEX_STATUS_OK;
4413 }
4414
4415 /* parse a float with a dimension */
4416 static synctex_fs_s _synctex_scan_float_and_dimension(synctex_scanner_p scanner) {
4417 synctex_fs_s fs = {0,0};
4418 synctex_zs_s zs = {0,0};
4419 char * endptr = NULL;
4420 #ifdef HAVE_SETLOCALE
4421 char * loc = setlocale(LC_NUMERIC, NULL);
4422 #endif
4423 if (NULL == scanner) {
4424 return (synctex_fs_s){0,SYNCTEX_STATUS_BAD_ARGUMENT};
4425 }
4426 zs = _synctex_buffer_get_available_size(scanner, SYNCTEX_BUFFER_MIN_SIZE);
4427 if (zs.status<SYNCTEX_STATUS_EOF) {
4428 _synctex_error("Problem with float.");
4429 return (synctex_fs_s){0,zs.status};
4430 }
4431 #ifdef HAVE_SETLOCALE
4432 setlocale(LC_NUMERIC, "C");
4433 #endif
4434 fs.value = strtod(SYNCTEX_CUR,&endptr);
4435 #ifdef HAVE_SETLOCALE
4436 setlocale(LC_NUMERIC, loc);
4437 #endif
4438 if (endptr == SYNCTEX_CUR) {
4439 _synctex_error("A float was expected.");
4440 return (synctex_fs_s){0,SYNCTEX_STATUS_ERROR};
4441 }
4442 SYNCTEX_CUR = endptr;
4443 if ((fs.status = _synctex_match_string(scanner,"in")) >= SYNCTEX_STATUS_OK) {
4444 fs.value *= 72.27f*65536;
4445 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4446 report_unit_error:
4447 _synctex_error("problem with unit.");
4448 return fs;
4449 } else if ((fs.status = _synctex_match_string(scanner,"cm")) >= SYNCTEX_STATUS_OK) {
4450 fs.value *= 72.27f*65536/2.54f;
4451 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4452 goto report_unit_error;
4453 } else if ((fs.status = _synctex_match_string(scanner,"mm")) >= SYNCTEX_STATUS_OK) {
4454 fs.value *= 72.27f*65536/25.4f;
4455 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4456 goto report_unit_error;
4457 } else if ((fs.status = _synctex_match_string(scanner,"pt")) >= SYNCTEX_STATUS_OK) {
4458 fs.value *= 65536.0f;
4459 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4460 goto report_unit_error;
4461 } else if ((fs.status = _synctex_match_string(scanner,"bp")) >= SYNCTEX_STATUS_OK) {
4462 fs.value *= 72.27f/72*65536.0f;
4463 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4464 goto report_unit_error;
4465 } else if ((fs.status = _synctex_match_string(scanner,"pc")) >= SYNCTEX_STATUS_OK) {
4466 fs.value *= 12.0*65536.0f;
4467 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4468 goto report_unit_error;
4469 } else if ((fs.status = _synctex_match_string(scanner,"sp")) >= SYNCTEX_STATUS_OK) {
4470 fs.value *= 1.0f;
4471 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4472 goto report_unit_error;
4473 } else if ((fs.status = _synctex_match_string(scanner,"dd")) >= SYNCTEX_STATUS_OK) {
4474 fs.value *= 1238.0f/1157*65536.0f;
4475 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4476 goto report_unit_error;
4477 } else if ((fs.status = _synctex_match_string(scanner,"cc")) >= SYNCTEX_STATUS_OK) {
4478 fs.value *= 14856.0f/1157*65536;
4479 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4480 goto report_unit_error;
4481 } else if ((fs.status = _synctex_match_string(scanner,"nd")) >= SYNCTEX_STATUS_OK) {
4482 fs.value *= 685.0f/642*65536;
4483 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4484 goto report_unit_error;
4485 } else if ((fs.status = _synctex_match_string(scanner,"nc")) >= SYNCTEX_STATUS_OK) {
4486 fs.value *= 1370.0f/107*65536;
4487 } else if (fs.status<SYNCTEX_STATUS_EOF) {
4488 goto report_unit_error;
4489 }
4490 return fs;
4491 }
4492
4493 /* parse the post scriptum
4494 * SYNCTEX_STATUS_OK is returned on completion
4495 * a negative error is returned otherwise */
4496 static synctex_status_t _synctex_scan_post_scriptum(synctex_scanner_p scanner) {
4497 synctex_status_t status = 0;
4498 synctex_fs_s fs = {0,0};
4499 char * endptr = NULL;
4500 #ifdef HAVE_SETLOCALE
4501 char * loc = setlocale(LC_NUMERIC, NULL);
4502 #endif
4503 if (NULL == scanner) {
4504 return SYNCTEX_STATUS_BAD_ARGUMENT;
4505 }
4506 /* Scan the file until a post scriptum line is found */
4507 post_scriptum_not_found:
4508 status = _synctex_match_string(scanner,"Post scriptum:");
4509 if (status<SYNCTEX_STATUS_NOT_OK) {
4510 return status;
4511 }
4512 if (status == SYNCTEX_STATUS_NOT_OK) {
4513 status = _synctex_next_line(scanner);
4514 if (status<SYNCTEX_STATUS_EOF) {
4515 return status;
4516 } else if (status<SYNCTEX_STATUS_OK) {
4517 return SYNCTEX_STATUS_OK;/* The EOF is found, we have properly scanned the file */
4518 }
4519 goto post_scriptum_not_found;
4520 }
4521 /* We found the name, advance to the next line. */
4522 next_line:
4523 status = _synctex_next_line(scanner);
4524 if (status<SYNCTEX_STATUS_EOF) {
4525 return status;
4526 } else if (status<SYNCTEX_STATUS_OK) {
4527 return SYNCTEX_STATUS_OK;/* The EOF is found, we have properly scanned the file */
4528 }
4529 /* Scanning the information */
4530 status = _synctex_match_string(scanner,"Magnification:");
4531 if (status == SYNCTEX_STATUS_OK ) {
4532 #ifdef HAVE_SETLOCALE
4533 setlocale(LC_NUMERIC, "C");
4534 #endif
4535 scanner->unit = strtod(SYNCTEX_CUR,&endptr);
4536 #ifdef HAVE_SETLOCALE
4537 setlocale(LC_NUMERIC, loc);
4538 #endif
4539 if (endptr == SYNCTEX_CUR) {
4540 _synctex_error("bad magnification in the post scriptum, a float was expected.");
4541 return SYNCTEX_STATUS_ERROR;
4542 }
4543 if (scanner->unit<=0) {
4544 _synctex_error("bad magnification in the post scriptum, a positive float was expected.");
4545 return SYNCTEX_STATUS_ERROR;
4546 }
4547 SYNCTEX_CUR = endptr;
4548 goto next_line;
4549 }
4550 if (status<SYNCTEX_STATUS_EOF){
4551 report_record_problem:
4552 _synctex_error("Problem reading the Post Scriptum records");
4553 return status; /* echo the error. */
4554 }
4555 status = _synctex_match_string(scanner,"X Offset:");
4556 if (status == SYNCTEX_STATUS_OK) {
4557 fs = _synctex_scan_float_and_dimension(scanner);
4558 if (fs.status<SYNCTEX_STATUS_OK) {
4559 _synctex_error("Problem with X offset in the Post Scriptum.");
4560 return fs.status;
4561 }
4562 scanner->x_offset = fs.value;
4563 goto next_line;
4564 } else if (status<SYNCTEX_STATUS_EOF){
4565 goto report_record_problem;
4566 }
4567 status = _synctex_match_string(scanner,"Y Offset:");
4568 if (status==SYNCTEX_STATUS_OK) {
4569 fs = _synctex_scan_float_and_dimension(scanner);
4570 if (fs.status<SYNCTEX_STATUS_OK) {
4571 _synctex_error("Problem with Y offset in the Post Scriptum.");
4572 return fs.status;
4573 }
4574 scanner->y_offset = fs.value;
4575 goto next_line;
4576 } else if (status<SYNCTEX_STATUS_EOF){
4577 goto report_record_problem;
4578 }
4579 goto next_line;
4580 }
4581
4582 /* SYNCTEX_STATUS_OK is returned if the postamble is read
4583 * SYNCTEX_STATUS_NOT_OK is returned if the postamble is not at the current location
4584 * a negative error otherwise
4585 * The postamble comprises the post scriptum section.
4586 */
4587 static synctex_status_t _synctex_scan_postamble(synctex_scanner_p scanner) {
4588 synctex_status_t status = 0;
4589 synctex_is_s is = {0,0};
4590 if (NULL == scanner) {
4591 return SYNCTEX_STATUS_BAD_ARGUMENT;
4592 }
4593 if (!scanner->flags.postamble && (status=_synctex_match_string(scanner,"Postamble:"))<SYNCTEX_STATUS_OK) {
4594 return status;
4595 }
4596 count_again:
4597 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4598 return status;
4599 }
4600 if ((status=_synctex_scan_named(scanner,"Count:"))< SYNCTEX_STATUS_EOF) {
4601 return status; /* forward the error */
4602 } else if (status < SYNCTEX_STATUS_OK) { /* No Count record found */
4603 goto count_again;
4604 }
4605 if ((is=_synctex_decode_int(scanner)).status<SYNCTEX_STATUS_OK) {
4606 return is.status;
4607 }
4608 if ((status=_synctex_next_line(scanner))<SYNCTEX_STATUS_OK) {
4609 return status;
4610 }
4611 scanner->count = is.integer;
4612 /* Now we scan the last part of the SyncTeX file: the Post Scriptum section. */
4613 return _synctex_scan_post_scriptum(scanner);
4614 }
4615
4616 /* Horizontal boxes also have visible size.
4617 * Visible size are bigger than real size.
4618 * For example 0 width boxes may contain text.
4619 * At creation time, the visible size is set to the values of the real size.
4620 */
4621 static synctex_status_t _synctex_setup_visible_hbox(synctex_node_p box) {
4622 if (box) {
4623 switch(synctex_node_type(box)) {
4624 case synctex_node_type_hbox:
4625 _synctex_data_set_h_V(box,_synctex_data_h(box));
4626 _synctex_data_set_v_V(box,_synctex_data_v(box));
4627 _synctex_data_set_width_V(box,_synctex_data_width(box));
4628 _synctex_data_set_height_V(box,_synctex_data_height(box));
4629 _synctex_data_set_depth_V(box,_synctex_data_depth(box));
4630 return SYNCTEX_STATUS_OK;
4631 default:
4632 break;
4633 }
4634 }
4635 return SYNCTEX_STATUS_BAD_ARGUMENT;
4636 }
4637
4638 /* This method is sent to an horizontal box to setup the visible size
4639 * Some box have 0 width but do contain text material.
4640 * With this method, one can enlarge the box to contain the given point (h,v).
4641 */
4642 static synctex_status_t _synctex_make_hbox_contain_point(synctex_node_p node,synctex_point_s point) {
4643 int min, max, n;
4644 if (NULL == node || synctex_node_type(node) != synctex_node_type_hbox) {
4645 return SYNCTEX_STATUS_BAD_ARGUMENT;
4646 }
4647 if ((n = _synctex_data_width_V(node))<0) {
4648 max = _synctex_data_h_V(node);
4649 min = max+n;
4650 if (point.h<min) {
4651 _synctex_data_set_width_V(node,point.h-max);
4652 } else if (point.h>max) {
4653 _synctex_data_set_h_V(node,point.h);
4654 _synctex_data_set_width_V(node,min-point.h);
4655 }
4656 } else {
4657 min = _synctex_data_h_V(node);
4658 max = min+n;
4659 if (point.h<min) {
4660 _synctex_data_set_h_V(node,point.h);
4661 _synctex_data_set_width_V(node,max - point.h);
4662 } else if (point.h>max) {
4663 _synctex_data_set_width_V(node,point.h - min);
4664 }
4665 }
4666 n = _synctex_data_v_V(node);
4667 min = n - _synctex_data_height_V(node);
4668 max = n + _synctex_data_depth_V(node);
4669 if (point.v<min) {
4670 _synctex_data_set_height_V(node,n-point.v);
4671 } else if (point.v>max) {
4672 _synctex_data_set_depth_V(node,point.v-n);
4673 }
4674 return SYNCTEX_STATUS_OK;
4675 }
4676 static synctex_status_t _synctex_make_hbox_contain_box(synctex_node_p node,synctex_box_s box) {
4677 int min, max, n;
4678 if (NULL == node || synctex_node_type(node) != synctex_node_type_hbox) {
4679 return SYNCTEX_STATUS_BAD_ARGUMENT;
4680 }
4681 if ((n = _synctex_data_width_V(node))<0) {
4682 max = _synctex_data_h_V(node);
4683 min = max+n;
4684 if (box.min.h <min) {
4685 _synctex_data_set_width_V(node,box.min.h-max);
4686 } else if (box.max.h>max) {
4687 _synctex_data_set_h_V(node,box.max.h);
4688 _synctex_data_set_width_V(node,min-box.max.h);
4689 }
4690 } else {
4691 min = _synctex_data_h_V(node);
4692 max = min+n;
4693 if (box.min.h<min) {
4694 _synctex_data_set_h_V(node,box.min.h);
4695 _synctex_data_set_width_V(node,max - box.min.h);
4696 } else if (box.max.h>max) {
4697 _synctex_data_set_width_V(node,box.max.h - min);
4698 }
4699 }
4700 n = _synctex_data_v_V(node);
4701 min = n - _synctex_data_height_V(node);
4702 max = n + _synctex_data_depth_V(node);
4703 if (box.min.v<min) {
4704 _synctex_data_set_height_V(node,n-box.min.v);
4705 } else if (box.max.v>max) {
4706 _synctex_data_set_depth_V(node,box.max.v-n);
4707 }
4708 return SYNCTEX_STATUS_OK;
4709 }
4710 # ifdef SYNCTEX_NOTHING
4711 # pragma mark -
4712 # pragma mark SPECIAL CHARACTERS
4713 # endif
4714
4715
4716 /* Here are the control characters that strat each line of the synctex output file.
4717 * Their values define the meaning of the line.
4718 */
4719 # define SYNCTEX_CHAR_BEGIN_SHEET '{'
4720 # define SYNCTEX_CHAR_END_SHEET '}'
4721 # define SYNCTEX_CHAR_BEGIN_FORM '<'
4722 # define SYNCTEX_CHAR_END_FORM '>'
4723 # define SYNCTEX_CHAR_BEGIN_VBOX '['
4724 # define SYNCTEX_CHAR_END_VBOX ']'
4725 # define SYNCTEX_CHAR_BEGIN_HBOX '('
4726 # define SYNCTEX_CHAR_END_HBOX ')'
4727 # define SYNCTEX_CHAR_ANCHOR '!'
4728 # define SYNCTEX_CHAR_VOID_VBOX 'v'
4729 # define SYNCTEX_CHAR_VOID_HBOX 'h'
4730 # define SYNCTEX_CHAR_KERN 'k'
4731 # define SYNCTEX_CHAR_GLUE 'g'
4732 # define SYNCTEX_CHAR_RULE 'r'
4733 # define SYNCTEX_CHAR_MATH '$'
4734 # define SYNCTEX_CHAR_FORM_REF 'f'
4735 # define SYNCTEX_CHAR_BOUNDARY 'x'
4736 # define SYNCTEX_CHAR_CHARACTER 'c'
4737 # define SYNCTEX_CHAR_COMMENT '%'
4738
4739 # ifdef SYNCTEX_NOTHING
4740 # pragma mark -
4741 # pragma mark SCANNERS & PARSERS
4742 # endif
4743
4744 # define SYNCTEX_DECODE_FAILED(NODE,WHAT) \
4745 (_synctex_data_decode_##WHAT(NODE)<SYNCTEX_STATUS_OK)
4746 # define SYNCTEX_DECODE_FAILED_V(NODE,WHAT) \
4747 (_synctex_data_decode_##WHAT##_v(NODE)<SYNCTEX_STATUS_OK)
4748
4749 #define SYNCTEX_NS_NULL (synctex_ns_s){NULL,SYNCTEX_STATUS_NOT_OK}
4750 static synctex_ns_s _synctex_parse_new_sheet(synctex_scanner_p scanner) {
4751 synctex_node_p node;
4752 if ((node = _synctex_new_sheet(scanner))) {
4753 if (
4754 SYNCTEX_DECODE_FAILED(node,page)) {
4755 _synctex_error("Bad sheet record.");
4756 } else if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4757 _synctex_error("Missing end of sheet.");
4758 } else {
4759 /* Now set the owner */
4760 if (scanner->sheet) {
4761 synctex_node_p last_sheet = scanner->sheet;
4762 synctex_node_p next_sheet = NULL;
4763 while ((next_sheet = __synctex_tree_sibling(last_sheet))) {
4764 last_sheet = next_sheet;
4765 }
4766 /* sheets have no parent */
4767 __synctex_tree_set_sibling(last_sheet,node);
4768 } else {
4769 scanner->sheet = node;
4770 }
4771 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4772 }
4773 _synctex_free_node(node);
4774 }
4775 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4776 }
4777 /**
4778 * - requirement: scanner != NULL
4779 */
4780 static synctex_ns_s _synctex_parse_new_form(synctex_scanner_p scanner) {
4781 synctex_node_p node;
4782 if ((node = _synctex_new_form(scanner))) {
4783 if (
4784 SYNCTEX_DECODE_FAILED(node,tag)) {
4785 _synctex_error("Bad sheet record.");
4786 } else if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4787 _synctex_error("Missing end of form.");
4788 } else {
4789 /* Now set the owner */
4790 if (scanner->form) {
4791 synctex_node_p last_form = scanner->form;
4792 synctex_node_p next_form = NULL;
4793 while ((next_form = __synctex_tree_sibling(last_form))) {
4794 last_form = next_form;
4795 }
4796 __synctex_tree_set_sibling(last_form,node);
4797 } else {
4798 scanner->form = node;
4799 }
4800 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4801 }
4802 _synctex_free_node(node);
4803 }
4804 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4805 }
4806 # define SYNCTEX_SHOULD_DECODE_FAILED(NODE,WHAT) \
4807 (_synctex_data_has_##WHAT(NODE) &&(_synctex_data_decode_##WHAT(NODE)<SYNCTEX_STATUS_OK))
4808 # define SYNCTEX_SHOULD_DECODE_FAILED_V(NODE,WHAT) \
4809 (_synctex_data_has_##WHAT(NODE) &&(_synctex_data_decode_##WHAT##_v(NODE)<SYNCTEX_STATUS_OK))
4810
4811 static synctex_status_t _synctex_data_decode_tlchvwhd(synctex_node_p node) {
4812 return SYNCTEX_SHOULD_DECODE_FAILED(node,tag)
4813 || SYNCTEX_SHOULD_DECODE_FAILED(node,line)
4814 || SYNCTEX_SHOULD_DECODE_FAILED(node,column)
4815 || SYNCTEX_SHOULD_DECODE_FAILED(node,h)
4816 || SYNCTEX_SHOULD_DECODE_FAILED_V(node,v)
4817 || SYNCTEX_SHOULD_DECODE_FAILED(node,width)
4818 || SYNCTEX_SHOULD_DECODE_FAILED(node,height)
4819 || SYNCTEX_SHOULD_DECODE_FAILED(node,depth);
4820 }
4821 static synctex_ns_s _synctex_parse_new_vbox(synctex_scanner_p scanner) {
4822 synctex_node_p node;
4823 if ((node = _synctex_new_vbox(scanner))) {
4824 if (_synctex_data_decode_tlchvwhd(node)) {
4825 _synctex_error("Bad vbox record.");
4826 _synctex_next_line(scanner);
4827 out:
4828 _synctex_free_node(node);
4829 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4830 }
4831 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4832 _synctex_error("Missing end of vbox.");
4833 goto out;
4834 }
4835 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4836 }
4837 _synctex_next_line(scanner);
4838 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4839 }
4840 SYNCTEX_INLINE static synctex_node_p __synctex_node_make_friend_tlc(synctex_node_p node);
4841 static synctex_ns_s _synctex_parse_new_hbox(synctex_scanner_p scanner) {
4842 synctex_node_p node;
4843 if ((node = _synctex_new_hbox(scanner))) {
4844 if (_synctex_data_decode_tlchvwhd(node)) {
4845 _synctex_error("Bad hbox record.");
4846 _synctex_next_line(scanner);
4847 out:
4848 _synctex_free_node(node);
4849 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4850 }
4851 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4852 _synctex_error("Missing end of hbox.");
4853 goto out;
4854 }
4855 if (_synctex_setup_visible_hbox(node)<SYNCTEX_STATUS_OK) {
4856 _synctex_error("Unexpected error (_synctex_parse_new_hbox).");
4857 goto out;
4858 }
4859 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4860 }
4861 _synctex_next_line(scanner);
4862 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4863 }
4864 static synctex_ns_s _synctex_parse_new_void_vbox(synctex_scanner_p scanner) {
4865 synctex_node_p node;
4866 if ((node = _synctex_new_void_vbox(scanner))) {
4867 if (_synctex_data_decode_tlchvwhd(node)) {
4868 _synctex_error("Bad void vbox record.");
4869 _synctex_next_line(scanner);
4870 out:
4871 _synctex_free_node(node);
4872 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4873 }
4874 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4875 _synctex_error("Missing end of container.");
4876 goto out;
4877 }
4878 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4879 }
4880 _synctex_next_line(scanner);
4881 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4882 }
4883 static synctex_ns_s _synctex_parse_new_void_hbox(synctex_scanner_p scanner) {
4884 synctex_node_p node;
4885 if ((node = _synctex_new_void_hbox(scanner))) {
4886 if (_synctex_data_decode_tlchvwhd(node)) {
4887 _synctex_error("Bad void hbox record.");
4888 _synctex_next_line(scanner);
4889 out:
4890 _synctex_free_node(node);
4891 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4892 }
4893 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4894 _synctex_error("Missing end of container.");
4895 goto out;
4896 }
4897 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4898 }
4899 _synctex_next_line(scanner);
4900 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4901 }
4902 static synctex_ns_s _synctex_parse_new_kern(synctex_scanner_p scanner) {
4903 synctex_node_p node;
4904 if ((node = _synctex_new_kern(scanner))) {
4905 if (_synctex_data_decode_tlchvwhd(node)) {
4906 _synctex_error("Bad kern record.");
4907 _synctex_next_line(scanner);
4908 out:
4909 _synctex_free_node(node);
4910 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4911 }
4912 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4913 _synctex_error("Missing end of container.");
4914 goto out;
4915 }
4916 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4917 }
4918 _synctex_next_line(scanner);
4919 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4920 }
4921 static synctex_ns_s _synctex_parse_new_glue(synctex_scanner_p scanner) {
4922 synctex_node_p node;
4923 if ((node = _synctex_new_glue(scanner))) {
4924 if (_synctex_data_decode_tlchvwhd(node)) {
4925 _synctex_error("Bad glue record.");
4926 _synctex_next_line(scanner);
4927 out:
4928 _synctex_free_node(node);
4929 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4930 }
4931 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4932 _synctex_error("Missing end of container.");
4933 goto out;
4934 }
4935 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4936 }
4937 _synctex_next_line(scanner);
4938 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4939 }
4940 static synctex_ns_s _synctex_parse_new_rule(synctex_scanner_p scanner) {
4941 synctex_node_p node;
4942 if ((node = _synctex_new_rule(scanner))) {
4943 if (_synctex_data_decode_tlchvwhd(node)) {
4944 _synctex_error("Bad rule record.");
4945 _synctex_next_line(scanner);
4946 out:
4947 _synctex_free_node(node);
4948 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4949 }
4950 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4951 _synctex_error("Missing end of container.");
4952 goto out;
4953 }
4954 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4955 }
4956 _synctex_next_line(scanner);
4957 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4958 }
4959 static synctex_ns_s _synctex_parse_new_math(synctex_scanner_p scanner) {
4960 synctex_node_p node;
4961 if ((node = _synctex_new_math(scanner))) {
4962 if (_synctex_data_decode_tlchvwhd(node)) {
4963 _synctex_error("Bad math record.");
4964 _synctex_next_line(scanner);
4965 out:
4966 _synctex_free_node(node);
4967 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4968 }
4969 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4970 _synctex_error("Missing end of container.");
4971 goto out;
4972 }
4973 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4974 }
4975 _synctex_next_line(scanner);
4976 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4977 }
4978 static synctex_ns_s _synctex_parse_new_boundary(synctex_scanner_p scanner) {
4979 synctex_node_p node;
4980 if ((node = _synctex_new_boundary(scanner))) {
4981 if (_synctex_data_decode_tlchvwhd(node)) {
4982 _synctex_error("Bad boundary record.");
4983 _synctex_next_line(scanner);
4984 out:
4985 _synctex_free_node(node);
4986 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4987 }
4988 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
4989 _synctex_error("Missing end of container.");
4990 goto out;
4991 }
4992 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
4993 }
4994 _synctex_next_line(scanner);
4995 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
4996 }
4997 SYNCTEX_INLINE static synctex_ns_s _synctex_parse_new_ref(synctex_scanner_p scanner) {
4998 synctex_node_p node;
4999 if ((node = _synctex_new_ref(scanner))) {
5000 if (SYNCTEX_DECODE_FAILED(node,tag)
5001 || SYNCTEX_DECODE_FAILED(node,h)
5002 || SYNCTEX_DECODE_FAILED_V(node,v)) {
5003 _synctex_error("Bad form ref record.");
5004 _synctex_next_line(scanner);
5005 out:
5006 _synctex_free_node(node);
5007 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
5008 }
5009 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5010 _synctex_error("Missing end of container.");
5011 goto out;
5012 }
5013 return (synctex_ns_s){node,SYNCTEX_STATUS_OK};
5014 }
5015 _synctex_next_line(scanner);
5016 return (synctex_ns_s){NULL,SYNCTEX_STATUS_ERROR};
5017 }
5018 # undef SYNCTEX_DECODE_FAILED
5019 # undef SYNCTEX_DECODE_FAILED_V
5020
5021 SYNCTEX_INLINE static synctex_point_s _synctex_data_point(synctex_node_p node);
5022 SYNCTEX_INLINE static synctex_point_s _synctex_data_point_V(synctex_node_p node);
5023 SYNCTEX_INLINE static synctex_point_s _synctex_data_set_point(synctex_node_p node, synctex_point_s point);
5024 SYNCTEX_INLINE static synctex_box_s _synctex_data_box(synctex_node_p node);
5025 SYNCTEX_INLINE static synctex_box_s _synctex_data_xob(synctex_node_p node);
5026 SYNCTEX_INLINE static synctex_box_s _synctex_data_box_V(synctex_node_p node);
5027
5028 SYNCTEX_INLINE static synctex_node_p _synctex_input_register_line(synctex_node_p input,synctex_node_p node) {
5029 if (node && _synctex_data_tag(input) != _synctex_data_tag(node)) {
5030 input = synctex_scanner_input_with_tag(node->class_->scanner,_synctex_data_tag(node));
5031 }
5032 if (_synctex_data_line(node)>_synctex_data_line(input)) {
5033 _synctex_data_set_line(input,_synctex_data_line(node));
5034 }
5035 return input;
5036 }
5037 /**
5038 * Free node and its siblings and return its detached child.
5039 */
5040 SYNCTEX_INLINE static synctex_node_p _synctex_handle_pop_child(synctex_node_p handle) {
5041 synctex_node_p child = _synctex_tree_reset_child(handle);
5042 synctex_node_free(handle);
5043 return child;
5044 }
5045 /**
5046 * Set the tlc of all the x nodes that are targets of
5047 * x_handle and its sibling.
5048 * Reset the target of x_handle and deletes its siblings.
5049 * child is a node that has just been parsed and is not a boundary node.
5050 */
5051 SYNCTEX_INLINE static void _synctex_handle_set_tlc(synctex_node_p x_handle, synctex_node_p child, synctex_bool_t make_friend) {
5052 if (x_handle) {
5053 synctex_node_p sibling = x_handle;
5054 if (child) {
5055 synctex_node_p target;
5056 while ((target = synctex_node_target(sibling))) {
5057 _synctex_data_set_tlc(target,child);
5058 if (make_friend) {
5059 _synctex_node_make_friend_tlc(target);
5060 }
5061 if ((sibling = __synctex_tree_sibling(sibling))) {
5062 continue;
5063 } else {
5064 break;
5065 }
5066 }
5067 }
5068 _synctex_tree_reset_target(x_handle);
5069 sibling = __synctex_tree_reset_sibling(x_handle);
5070 synctex_node_free(sibling);
5071 }
5072 }
5073 /**
5074 * When we have parsed a box, we must register
5075 * all the contained heading boundary nodes
5076 * that have not yet been registered.
5077 * Those handles will be deleted when poping.
5078 */
5079 SYNCTEX_INLINE static void _synctex_handle_make_friend_tlc(synctex_node_p node) {
5080 while (node) {
5081 synctex_node_p target = _synctex_tree_reset_target(node);
5082 _synctex_node_make_friend_tlc(target);
5083 node = __synctex_tree_sibling(node);
5084 }
5085 }
5086 /**
5087 * Scan sheets, forms and input records.
5088 * - parameter scanner: owning scanner
5089 * - returns: status
5090 */
5091 static synctex_status_t __synctex_parse_sfi(synctex_scanner_p scanner) {
5092 synctex_status_t status = SYNCTEX_STATUS_OK;
5093 synctex_zs_s zs = {0,0};
5094 synctex_ns_s input = SYNCTEX_NS_NULL;
5095 synctex_node_p sheet = NULL;
5096 synctex_node_p form = NULL;
5097 synctex_node_p parent = NULL;
5098 synctex_node_p child = NULL;
5099 /*
5100 * Experimentations lead to the forthcoming conclusion:
5101 * Sometimes, the first nodes of a box have the wrong line number.
5102 * These are only boundary (x) nodes.
5103 * We observed that boundary nodes do have the proper line number
5104 * if they follow a node with a different type.
5105 * We keep track of these leading x nodes in a handle tree.
5106 */
5107 synctex_node_p x_handle = NULL;
5108 # define SYNCTEX_RETURN(STATUS) \
5109 synctex_node_free(x_handle);\
5110 return STATUS
5111 synctex_node_p last_k = NULL;
5112 synctex_node_p last_g = NULL;
5113 synctex_ns_s ns = SYNCTEX_NS_NULL;
5114 int form_depth = 0;
5115 int ignored_form_depth = 0;
5116 synctex_bool_t try_input = synctex_YES;
5117 if (!(x_handle = _synctex_new_handle(scanner))) {
5118 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5119 }
5120 # ifdef SYNCTEX_NOTHING
5121 # pragma mark MAIN LOOP
5122 # endif
5123 main_loop:
5124 status = SYNCTEX_STATUS_OK;
5125 sheet = form = parent = child = NULL;
5126 # define SYNCTEX_START_SCAN(WHAT)\
5127 (*SYNCTEX_CUR == SYNCTEX_CHAR_##WHAT)
5128 if (SYNCTEX_CUR<SYNCTEX_END) {
5129 if (SYNCTEX_START_SCAN(BEGIN_FORM)) {
5130 # ifdef SYNCTEX_NOTHING
5131 # pragma mark + SCAN FORM
5132 # endif
5133 scan_form:
5134 ns = _synctex_parse_new_form(scanner);
5135 if (ns.status == SYNCTEX_STATUS_OK) {
5136 ++form_depth;
5137 if (_synctex_tree_parent(form)) {
5138 /* This form is already being parsed */
5139 ++ignored_form_depth;
5140 goto ignore_loop;
5141 }
5142 _synctex_tree_set_parent(ns.node,form);
5143 form = ns.node;
5144 parent = form;
5145 child = NULL;
5146 last_k = last_g = NULL;
5147 goto content_loop;
5148 }
5149 if (form || sheet) {
5150 last_k = last_g = NULL;
5151 goto content_loop;
5152 }
5153 try_input = synctex_YES;
5154 goto main_loop;
5155 } else if (SYNCTEX_START_SCAN(BEGIN_SHEET)) {
5156 # ifdef SYNCTEX_NOTHING
5157 # pragma mark + SCAN SHEET
5158 # endif
5159 try_input = synctex_YES;
5160 ns = _synctex_parse_new_sheet(scanner);
5161 if (ns.status == SYNCTEX_STATUS_OK) {
5162 sheet = ns.node;
5163 parent = sheet;
5164 last_k = last_g = NULL;
5165 goto content_loop;
5166 }
5167 goto main_loop;
5168 } else if (SYNCTEX_START_SCAN(ANCHOR)) {
5169 # ifdef SYNCTEX_NOTHING
5170 # pragma mark + SCAN ANCHOR
5171 # endif
5172 scan_anchor:
5173 ++SYNCTEX_CUR;
5174 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5175 _synctex_error("Missing anchor.");
5176 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5177 }
5178 if (form || sheet) {
5179 last_k = last_g = NULL;
5180 goto content_loop;
5181 }
5182 try_input = synctex_YES;
5183 goto main_loop;
5184 } else if (SYNCTEX_START_SCAN(ANCHOR)) {
5185 # ifdef SYNCTEX_NOTHING
5186 # pragma mark + SCAN COMMENT
5187 # endif
5188 ++SYNCTEX_CUR;
5189 _synctex_next_line(scanner);
5190 try_input = synctex_YES;
5191 goto main_loop;
5192 } else if (try_input) {
5193 # ifdef SYNCTEX_NOTHING
5194 # pragma mark + SCAN INPUT
5195 # endif
5196 try_input = synctex_NO;
5197 do {
5198 input = __synctex_parse_new_input(scanner);
5199 } while (input.status == SYNCTEX_STATUS_OK);
5200 goto main_loop;
5201 }
5202 status = _synctex_match_string(scanner,"Postamble:");
5203 if (status==SYNCTEX_STATUS_OK) {
5204 scanner->flags.postamble = 1;
5205 SYNCTEX_RETURN(status);
5206 }
5207 status = _synctex_next_line(scanner);
5208 if (status<SYNCTEX_STATUS_OK) {
5209 SYNCTEX_RETURN(status);
5210 }
5211 }
5212 /* At least 1 more character */
5213 zs = _synctex_buffer_get_available_size(scanner,1);
5214 if (zs.size == 0){
5215 _synctex_error("Incomplete synctex file, postamble missing.");
5216 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5217 }
5218 goto main_loop;
5219 /* Unreachable. */
5220 # ifdef SYNCTEX_NOTHING
5221 # pragma mark IGNORE LOOP
5222 # endif
5223 ignore_loop:
5224 ns = SYNCTEX_NS_NULL;
5225 if (SYNCTEX_CUR<SYNCTEX_END) {
5226 if (SYNCTEX_START_SCAN(BEGIN_FORM)) {
5227 ++ignored_form_depth;
5228 } else if (SYNCTEX_START_SCAN(END_FORM)) {
5229 --ignored_form_depth;
5230 }
5231 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5232 _synctex_error("Incomplete container.");
5233 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5234 }
5235 } else {
5236 zs = _synctex_buffer_get_available_size(scanner,1);
5237 if (zs.size == 0){
5238 _synctex_error("Incomplete synctex file, postamble missing.");
5239 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5240 }
5241 }
5242 if (ignored_form_depth) {
5243 goto ignore_loop;
5244 } else {
5245 last_k = last_g = NULL;
5246 goto content_loop;
5247 }
5248
5249 # ifdef SYNCTEX_NOTHING
5250 # pragma mark CONTENT LOOP
5251 # endif
5252 content_loop:
5253 /* Either in a form, a sheet or a box.
5254 * - in a sheet, "{" is not possible, only boxes and "}" at top level.
5255 * - in a form, "{" is not possible, only boxes, "<" and ">" at top level.
5256 * - in a box, the unique possibility is '<', '[', '(' or ">".
5257 * We still keep the '(' for a sheet, because that dos not cost too much.
5258 * We must also consider void boxes as children.
5259 */
5260 /* forms are everywhere */
5261 ns = SYNCTEX_NS_NULL;
5262 #if SYNCTEX_VERBOSE
5263 synctex_scanner_set_display_switcher(scanner,-1);
5264 printf("NEW CONTENT LOOP\n");
5265 #if SYNCTEX_DEBUG>500
5266 synctex_node_display(sheet);
5267 #endif
5268 #endif
5269 if (SYNCTEX_CUR<SYNCTEX_END) {
5270 if (SYNCTEX_START_SCAN(BEGIN_FORM)) {
5271 goto scan_form;
5272 } else if (SYNCTEX_START_SCAN(BEGIN_VBOX)) {
5273 # ifdef SYNCTEX_NOTHING
5274 # pragma mark + SCAN VBOX
5275 # endif
5276 ns = _synctex_parse_new_vbox(scanner);
5277 if (ns.status == SYNCTEX_STATUS_OK) {
5278 x_handle = _synctex_new_handle_with_child(x_handle);
5279 if (child) {
5280 _synctex_node_set_sibling(child,ns.node);
5281 } else {
5282 _synctex_node_set_child(parent,ns.node);
5283 }
5284 parent = ns.node;
5285 child = _synctex_tree_last(parent);
5286 # if SYNCTEX_VERBOSE
5287 synctex_node_log(parent);
5288 # endif
5289 input.node = _synctex_input_register_line(input.node,parent);
5290 last_k = last_g = NULL;
5291 goto content_loop;
5292 }
5293 } else if (SYNCTEX_START_SCAN(END_VBOX)) {
5294 if (synctex_node_type(parent) == synctex_node_type_vbox) {
5295 # ifdef SYNCTEX_NOTHING
5296 # pragma mark + SCAN XOBV
5297 # endif
5298 ++SYNCTEX_CUR;
5299 if (NULL == _synctex_tree_child(parent) && !form) {
5300 /* only void v boxes are friends */
5301 _synctex_node_make_friend_tlc(parent);
5302 }
5303 child = parent;
5304 parent = _synctex_tree_parent(child);
5305 if (!form) {
5306 _synctex_handle_make_friend_tlc(x_handle);
5307 }
5308 x_handle = _synctex_handle_pop_child(x_handle);
5309 _synctex_handle_set_tlc(x_handle,child,!form);
5310 # if SYNCTEX_VERBOSE
5311 synctex_node_log(child);
5312 # endif
5313 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5314 _synctex_error("Incomplete container.");
5315 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5316 }
5317 last_k = last_g = NULL;
5318 goto content_loop;
5319 }
5320 } else if (SYNCTEX_START_SCAN(BEGIN_HBOX)) {
5321 # ifdef SYNCTEX_NOTHING
5322 # pragma mark + SCAN HBOX
5323 # endif
5324 # if defined(SYNCTEX_USE_CHARINDEX)
5325 synctex_charindex_t char_index = (synctex_charindex_t)(scanner->reader->charindex_offset+SYNCTEX_CUR-SYNCTEX_START);
5326 synctex_lineindex_t line_index = scanner->reader->line_number;
5327 # endif
5328 ns = _synctex_parse_new_hbox(scanner);
5329 if (ns.status == SYNCTEX_STATUS_OK) {
5330 x_handle = _synctex_new_handle_with_child(x_handle);
5331 if (child) {
5332 _synctex_node_set_sibling(child,ns.node);
5333 } else {
5334 _synctex_node_set_child(parent,ns.node);
5335 }
5336 parent = ns.node;
5337 /* add a box boundary node at the start */
5338 if ((child = _synctex_new_box_bdry(scanner))) {
5339 # if defined(SYNCTEX_USE_CHARINDEX)
5340 child->line_index=line_index;
5341 child->char_index=char_index;
5342 # endif
5343 _synctex_node_set_child(parent,child);
5344 _synctex_data_set_tlchv(child,parent);
5345 if (!form) {
5346 __synctex_node_make_friend_tlc(child);
5347 }
5348 } else {
5349 _synctex_error("Can't create box bdry record.");
5350 }
5351 # if SYNCTEX_VERBOSE
5352 synctex_node_log(parent);
5353 # endif
5354 input.node = _synctex_input_register_line(input.node,parent);
5355 last_k = last_g = NULL;
5356 goto content_loop;
5357 }
5358 } else if (SYNCTEX_START_SCAN(END_HBOX)) {
5359 if (synctex_node_type(parent) == synctex_node_type_hbox) {
5360 # ifdef SYNCTEX_NOTHING
5361 # pragma mark + SCAN XOBH
5362 # endif
5363 ++SYNCTEX_CUR;
5364 /* setting the next horizontal box at the end ensures
5365 * that a child is recorded before any of its ancestors.
5366 */
5367 if (form == NULL /* && sheet != NULL*/ ) {
5368 _synctex_tree_set_next_hbox(parent,_synctex_tree_next_hbox(sheet));
5369 _synctex_tree_set_next_hbox(sheet,parent);
5370 }
5371 {
5372 /* Update the mean line number */
5373 synctex_node_p node = _synctex_tree_child(parent);
5374 synctex_node_p sibling = NULL;
5375 /* Ignore the first node (a box_bdry) */
5376 if (node && (sibling = __synctex_tree_sibling(node))) {
5377 unsigned int node_weight = 0;
5378 unsigned int cumulated_line_numbers = 0;
5379 _synctex_data_set_line(node, _synctex_data_line(sibling));
5380 node = sibling;
5381 do {
5382 if (synctex_node_type(node)==synctex_node_type_hbox) {
5383 if (_synctex_data_weight(node)) {
5384 node_weight += _synctex_data_weight(node);
5385 cumulated_line_numbers += _synctex_data_mean_line(node)*_synctex_data_weight(node);
5386 } else {
5387 ++node_weight;
5388 cumulated_line_numbers += _synctex_data_mean_line(node);
5389 }
5390 } else {
5391 ++node_weight;
5392 cumulated_line_numbers += synctex_node_line(node);
5393 }
5394 } while ((node = __synctex_tree_sibling(node)));
5395 _synctex_data_set_mean_line(parent,(cumulated_line_numbers + node_weight/2)/node_weight);
5396 _synctex_data_set_weight(parent,node_weight);
5397 } else {
5398 _synctex_data_set_mean_line(parent,_synctex_data_line(parent));
5399 _synctex_data_set_weight(parent,1);
5400 }
5401 if ((sibling = _synctex_new_box_bdry(scanner))) {
5402 # if defined(SYNCTEX_USE_CHARINDEX)
5403 sibling->line_index=child->line_index;
5404 sibling->char_index=child->char_index;
5405 # endif
5406 _synctex_node_set_sibling(child,sibling);
5407 {
5408 synctex_node_p N = child;
5409 while (synctex_node_type(N) == synctex_node_type_ref) {
5410 N = _synctex_tree_arg_sibling(N);
5411 }
5412 _synctex_data_set_tlc(sibling,N);
5413 }
5414 _synctex_data_set_h(sibling,_synctex_data_h_V(parent)+_synctex_data_width_V(parent));
5415 _synctex_data_set_v(sibling,_synctex_data_v_V(parent));
5416 child = sibling;
5417 } else {
5418 _synctex_error("Can't create box bdry record.");
5419 }
5420 sibling = _synctex_tree_child(parent);
5421 _synctex_data_set_point(sibling,_synctex_data_point_V(parent));
5422 if (last_k && last_g && (child = synctex_node_child(parent))) {
5423 /* Find the node preceding last_k */
5424 synctex_node_p next;
5425 while ((next = __synctex_tree_sibling(child))) {
5426 if (next == last_k) {
5427 _synctex_data_set_tlc(last_k,child);
5428 _synctex_data_set_tlc(last_g,child);
5429 break;
5430 }
5431 child = next;
5432 }
5433 }
5434 child = parent;
5435 parent = _synctex_tree_parent(child);
5436 if (!form) {
5437 _synctex_handle_make_friend_tlc(x_handle);
5438 }
5439 x_handle = _synctex_handle_pop_child(x_handle);
5440 _synctex_handle_set_tlc(x_handle,child,!form);
5441 _synctex_make_hbox_contain_box(parent, _synctex_data_box_V(child));
5442 # if SYNCTEX_VERBOSE
5443 synctex_node_log(child);
5444 # endif
5445 }
5446 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5447 _synctex_error("Incomplete container.");
5448 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5449 }
5450 last_k = last_g = NULL;
5451 goto content_loop;
5452 }
5453 } else if (SYNCTEX_START_SCAN(VOID_VBOX)) {
5454 # ifdef SYNCTEX_NOTHING
5455 # pragma mark + SCAN VOID VBOX
5456 # endif
5457 ns = _synctex_parse_new_void_vbox(scanner);
5458 if (ns.status == SYNCTEX_STATUS_OK) {
5459 if (child) {
5460 _synctex_node_set_sibling(child,ns.node);
5461 } else {
5462 _synctex_node_set_child(parent,ns.node);
5463 }
5464 child = ns.node;
5465 _synctex_handle_set_tlc(x_handle, child,!form);
5466 # if SYNCTEX_VERBOSE
5467 synctex_node_log(child);
5468 # endif
5469 input.node = _synctex_input_register_line(input.node,child);
5470 last_k = last_g = NULL;
5471 goto content_loop;
5472 }
5473 } else if (SYNCTEX_START_SCAN(VOID_HBOX)) {
5474 # ifdef SYNCTEX_NOTHING
5475 # pragma mark + SCAN VOID HBOX
5476 # endif
5477 ns = _synctex_parse_new_void_hbox(scanner);
5478 if (ns.status == SYNCTEX_STATUS_OK) {
5479 if (_synctex_data_width(ns.node)<0) {
5480 printf("Negative width\n");
5481 }
5482 if (child) {
5483 _synctex_node_set_sibling(child,ns.node);
5484 } else {
5485 _synctex_node_set_child(parent,ns.node);
5486 }
5487 child = ns.node;
5488 _synctex_handle_set_tlc(x_handle, child,!form);
5489 _synctex_make_hbox_contain_box(parent,_synctex_data_box(child));
5490 # if SYNCTEX_VERBOSE
5491 synctex_node_log(child);
5492 # endif
5493 input.node = _synctex_input_register_line(input.node,child);
5494 last_k = last_g = NULL;
5495 goto content_loop;
5496 }
5497 } else if (SYNCTEX_START_SCAN(KERN)) {
5498 # ifdef SYNCTEX_NOTHING
5499 # pragma mark + SCAN KERN
5500 # endif
5501 ns = _synctex_parse_new_kern(scanner);
5502 if (ns.status == SYNCTEX_STATUS_OK) {
5503 if (child) {
5504 _synctex_node_set_sibling(child,ns.node);
5505 } else {
5506 _synctex_node_set_child(parent,ns.node);
5507 }
5508 child = ns.node;
5509 if (!form) {
5510 __synctex_node_make_friend_tlc(child);
5511 }
5512 _synctex_handle_set_tlc(x_handle, child,!form);
5513 _synctex_make_hbox_contain_box(parent,_synctex_data_xob(child));
5514 # if SYNCTEX_VERBOSE
5515 synctex_node_log(child);
5516 # endif
5517 input.node = _synctex_input_register_line(input.node,child);
5518 last_k = child;
5519 last_g = NULL;
5520 goto content_loop;
5521 }
5522 } else if (SYNCTEX_START_SCAN(GLUE)) {
5523 # ifdef SYNCTEX_NOTHING
5524 # pragma mark + SCAN GLUE
5525 # endif
5526 ns = _synctex_parse_new_glue(scanner);
5527 if (ns.status == SYNCTEX_STATUS_OK) {
5528 if (child) {
5529 _synctex_node_set_sibling(child,ns.node);
5530 } else {
5531 _synctex_node_set_child(parent,ns.node);
5532 }
5533 child = ns.node;
5534 if (!form) {
5535 __synctex_node_make_friend_tlc(child);
5536 }
5537 _synctex_handle_set_tlc(x_handle, child,!form);
5538 _synctex_make_hbox_contain_point(parent,_synctex_data_point(child));
5539 # if SYNCTEX_VERBOSE
5540 synctex_node_log(child);
5541 # endif
5542 input.node = _synctex_input_register_line(input.node,child);
5543 if (last_k) {
5544 last_g = child;
5545 } else {
5546 last_k = last_g = NULL;
5547 }
5548 goto content_loop;
5549 }
5550 } else if (SYNCTEX_START_SCAN(RULE)) {
5551 # ifdef SYNCTEX_NOTHING
5552 # pragma mark + SCAN RULE
5553 # endif
5554 ns = _synctex_parse_new_rule(scanner);
5555 if (ns.status == SYNCTEX_STATUS_OK) {
5556 if (child) {
5557 _synctex_node_set_sibling(child,ns.node);
5558 } else {
5559 _synctex_node_set_child(parent,ns.node);
5560 }
5561 child = ns.node;
5562 if (!form) {
5563 __synctex_node_make_friend_tlc(child);
5564 }
5565 _synctex_handle_set_tlc(x_handle, child,!form);
5566 /* Rules are sometimes far too big
5567 _synctex_make_hbox_contain_box(parent,_synctex_data_box(child));
5568 */
5569 # if SYNCTEX_VERBOSE
5570 synctex_node_log(child);
5571 # endif
5572 input.node = _synctex_input_register_line(input.node,child);
5573 last_k = last_g = NULL;
5574 goto content_loop;
5575 }
5576 } else if (SYNCTEX_START_SCAN(MATH)) {
5577 # ifdef SYNCTEX_NOTHING
5578 # pragma mark + SCAN MATH
5579 # endif
5580 ns = _synctex_parse_new_math(scanner);
5581 if (ns.status == SYNCTEX_STATUS_OK) {
5582 if (child) {
5583 _synctex_node_set_sibling(child,ns.node);
5584 } else {
5585 _synctex_node_set_child(parent,ns.node);
5586 }
5587 child = ns.node;
5588 if (!form) {
5589 __synctex_node_make_friend_tlc(child);
5590 }
5591 _synctex_handle_set_tlc(x_handle, child,!form);
5592 _synctex_make_hbox_contain_point(parent,_synctex_data_point(child));
5593 # if SYNCTEX_VERBOSE
5594 synctex_node_log(child);
5595 # endif
5596 input.node = _synctex_input_register_line(input.node,child);
5597 last_k = last_g = NULL;
5598 goto content_loop;
5599 }
5600 } else if (SYNCTEX_START_SCAN(FORM_REF)) {
5601 # ifdef SYNCTEX_NOTHING
5602 # pragma mark + SCAN FORM REF
5603 # endif
5604 #if SYNCTEX_DEBUG>500
5605 synctex_node_display(parent);
5606 synctex_node_display(child);
5607 #endif
5608 ns = _synctex_parse_new_ref(scanner);
5609 if (ns.status == SYNCTEX_STATUS_OK) {
5610 if (child) {
5611 _synctex_node_set_sibling(child,ns.node);
5612 } else {
5613 _synctex_node_set_child(parent,ns.node);
5614 }
5615 child = ns.node;
5616 if (form) {
5617 if (scanner->ref_in_form) {
5618 synctex_tree_set_friend(child,scanner->ref_in_form);
5619 }
5620 scanner->ref_in_form = child;
5621 } else {
5622 if (scanner->ref_in_sheet) {
5623 synctex_tree_set_friend(child,scanner->ref_in_sheet);
5624 }
5625 scanner->ref_in_sheet = child;
5626 }
5627 # if SYNCTEX_VERBOSE
5628 synctex_node_log(child);
5629 # endif
5630 last_k = last_g = NULL;
5631 goto content_loop;
5632 }
5633 } else if (SYNCTEX_START_SCAN(BOUNDARY)) {
5634 # ifdef SYNCTEX_NOTHING
5635 # pragma mark + SCAN BOUNDARY
5636 # endif
5637 ns = _synctex_parse_new_boundary(scanner);
5638 if (ns.status == SYNCTEX_STATUS_OK) {
5639 if (child) {
5640 _synctex_node_set_sibling(child,ns.node);
5641 } else {
5642 _synctex_node_set_child(parent,ns.node);
5643 }
5644 if (synctex_node_type(child)==synctex_node_type_box_bdry
5645 || _synctex_tree_target(x_handle)) {
5646 child = _synctex_tree_reset_child(x_handle);
5647 child = _synctex_new_handle_with_child(child);
5648 __synctex_tree_set_sibling(child, x_handle);
5649 x_handle = child;
5650 _synctex_tree_set_target(x_handle,ns.node);
5651 } else if (!form) {
5652 __synctex_node_make_friend_tlc(ns.node);
5653 }
5654 child = ns.node;
5655 _synctex_make_hbox_contain_point(parent,_synctex_data_point(child));
5656 # if SYNCTEX_VERBOSE
5657 synctex_node_log(child);
5658 # endif
5659 input.node = _synctex_input_register_line(input.node,child);
5660 last_k = last_g = NULL;
5661 goto content_loop;
5662 }
5663 } else if (SYNCTEX_START_SCAN(CHARACTER)) {
5664 # ifdef SYNCTEX_NOTHING
5665 # pragma mark + SCAN CHARACTER
5666 # endif
5667 ++SYNCTEX_CUR;
5668 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5669 _synctex_error("Missing end of container.");
5670 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5671 }
5672 last_k = last_g = NULL;
5673 goto content_loop;
5674 } else if (SYNCTEX_START_SCAN(ANCHOR)) {
5675 # ifdef SYNCTEX_NOTHING
5676 # pragma mark + SCAN ANCHOR
5677 # endif
5678 goto scan_anchor;
5679 } else if (SYNCTEX_START_SCAN(END_SHEET)) {
5680 if (sheet && parent == sheet) {
5681 # ifdef SYNCTEX_NOTHING
5682 # pragma mark + SCAN TEEHS
5683 # endif
5684 ++SYNCTEX_CUR;
5685 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5686 _synctex_error("Missing anchor.");
5687 }
5688 parent = sheet = NULL;
5689 goto main_loop;
5690 }
5691 } else if (SYNCTEX_START_SCAN(END_FORM)) {
5692 if (parent == form && form_depth > 0) {
5693 # ifdef SYNCTEX_NOTHING
5694 # pragma mark + SCAN MROF
5695 # endif
5696 ++SYNCTEX_CUR;
5697 --form_depth;
5698 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK
5699 && (form_depth || sheet)) {
5700 _synctex_error("Missing end of container.");
5701 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5702 }
5703 if ((parent = _synctex_tree_parent(form))) {
5704 _synctex_tree_reset_parent(form);
5705 child = form;
5706 form = parent;
5707 goto content_loop;
5708 } else if (sheet) {
5709 form = NULL;
5710 parent = sheet;
5711 child = synctex_node_last_sibling(child);
5712 goto content_loop;
5713 }
5714 goto main_loop;
5715 }
5716 }
5717 _synctex_error("Ignored record <%.20s...>(line %i)\n",SYNCTEX_CUR, scanner->reader->line_number+1);
5718 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
5719 _synctex_error("Missing end of sheet/form.");
5720 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5721 }
5722 last_k = last_g = NULL;
5723 goto content_loop;
5724 }
5725 zs = _synctex_buffer_get_available_size(scanner,1);
5726 if (zs.size == 0){
5727 _synctex_error("Incomplete synctex file, postamble missing.");
5728 SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
5729 }
5730 last_k = last_g = NULL;
5731 goto content_loop;
5732 }
5733 #undef SYNCTEX_RETURN
5734 /**
5735 * Replace ref in its tree hierarchy by a single box
5736 * proxy to the contents of the associated form.
5737 * - argument ref: a ref node with no friend
5738 * - return the proxy created.
5739 * - note: Does nothing if ref is not owned.
5740 * - note: On return, ref will have no parent nor sibling.
5741 * The caller is responsible for releasing ref.
5742 * - note: this is where root proxies are created.
5743 * - note: the target of the root proxy is the content
5744 * of a form.
5745 */
5746 SYNCTEX_INLINE static synctex_ns_s __synctex_replace_ref(synctex_node_p ref) {
5747 synctex_ns_s ns = {NULL,SYNCTEX_STATUS_OK};
5748 synctex_node_p parent;
5749 if ((parent = _synctex_tree_parent(ref))) {
5750 synctex_node_p sibling = __synctex_tree_reset_sibling(ref);
5751 synctex_node_p arg_sibling = synctex_node_arg_sibling(ref);
5752 /* arg_sibling != NULL because the child of a box
5753 * is always a box boundary, not a ref. */
5754 synctex_node_p target = synctex_form_content(ref->class_->scanner, _synctex_data_tag(ref));
5755 /* The target is a single node (box)
5756 * with children and no siblings. */
5757 if ((ns.node = __synctex_new_proxy_from_ref_to(ref, target))) {
5758 /* Insert this proxy instead of ref. */
5759 _synctex_node_set_sibling(arg_sibling,ns.node);
5760 /* Then append the original sibling of ref. */
5761 _synctex_node_set_sibling(ns.node,sibling);
5762 # if defined(SYNCTEX_USE_CHARINDEX)
5763 if (synctex_node_type(sibling) == synctex_node_type_box_bdry) {
5764 /* The sibling is the last box boundary
5765 * which may have a less accurate information */
5766 sibling->char_index = arg_sibling->char_index;
5767 sibling->line_index = arg_sibling->line_index;
5768 }
5769 #endif
5770 #if SYNCTEX_DEBUG>500
5771 printf("! Ref replacement:\n");
5772 synctex_node_log(ref);
5773 synctex_node_display(synctex_node_sibling(ref));
5774 #endif
5775 } else /* simply remove ref */ {
5776 _synctex_tree_set_sibling(arg_sibling,sibling);
5777 }
5778 __synctex_tree_reset_parent(ref);
5779 } else {
5780 _synctex_error("! Missing parent in __synctex_replace_ref. "
5781 "Please report.");
5782 ns.status = SYNCTEX_STATUS_BAD_ARGUMENT;
5783 }
5784 return ns;
5785 }
5786 /**
5787 * - argument ref: is the starting point of a linked list
5788 * of refs. The link is made through the friend field.
5789 * - returns: the status and the list of all the proxies
5790 * created. The link is made through the friend field.
5791 * - note: All refs are freed
5792 */
5793 SYNCTEX_INLINE static synctex_ns_s _synctex_post_process_ref(synctex_node_p ref) {
5794 synctex_ns_s ns = {NULL, SYNCTEX_STATUS_OK};
5795 while (ref) {
5796 synctex_node_p next_ref = _synctex_tree_reset_friend(ref);
5797 synctex_ns_s sub_ns = __synctex_replace_ref(ref);
5798 if (sub_ns.status < ns.status) {
5799 ns.status = sub_ns.status;
5800 } else {
5801 /* Insert all the created proxies in the list
5802 * sub_ns.node is the last friend,
5803 */
5804 synctex_tree_set_friend(sub_ns.node,ns.node);
5805 ns.node = sub_ns.node;
5806 }
5807 /* Ensure ref is fully detached before freeing,
5808 * even if __synctex_replace_ref did not detach it
5809 * (e.g. because the ref had no parent).
5810 * Otherwise _synctex_free_leaf would chase the
5811 * stale sibling pointer into the live tree. */
5812 __synctex_tree_reset_sibling(ref);
5813 __synctex_tree_reset_parent(ref);
5814 synctex_node_free(ref);
5815 ref = next_ref;
5816 }
5817 return ns;
5818 }
5819 typedef synctex_node_p (* synctex_processor_f)(synctex_node_p node);
5820 /**
5821 * Apply the processor f to the tree hierarchy rooted at proxy.
5822 * proxy has replaced a form ref, no children yet.
5823 * As a side effect all the hierarchy of nodes will be created.
5824 */
5825 SYNCTEX_INLINE static synctex_status_t _synctex_post_process_proxy(synctex_node_p proxy, synctex_processor_f f) {
5826 while(proxy) {
5827 synctex_node_p next_proxy = _synctex_tree_friend(proxy);
5828 synctex_node_p halt = __synctex_tree_sibling(proxy);
5829 /* if proxy is the last sibling, halt is NULL.
5830 * Find what should be a next node,
5831 * without creating new nodes. */
5832 if (!halt) {
5833 synctex_node_p parent = _synctex_tree_parent(proxy);
5834 halt = __synctex_tree_sibling(parent);
5835 while (!halt && parent) {
5836 parent = _synctex_tree_parent(parent);
5837 halt = __synctex_tree_sibling(parent);
5838 }
5839 }
5840 do {
5841 #if SYNCTEX_DEBUG>500
5842 printf("POST PROCESSING %s\n",_synctex_node_abstract(proxy));
5843 {
5844 int i,j = 0;
5845 for (i=0;i<proxy->class_->scanner->number_of_lists;++i) {
5846 synctex_node_p N = proxy->class_->scanner->lists_of_friends[i];
5847 do {
5848 if (N==proxy) {
5849 ++j;
5850 printf("%s",_synctex_node_abstract(N));
5851 }
5852 } while ((N = _synctex_tree_friend(N)));
5853 }
5854 if (j) {
5855 printf("\nBeforehand %i match\n",j);
5856 }
5857 }
5858 #endif
5859 f(proxy);
5860 #if SYNCTEX_DEBUG>500
5861 {
5862 int i,j = 0;
5863 for (i=0;i<proxy->class_->scanner->number_of_lists;++i) {
5864 synctex_node_p N = proxy->class_->scanner->lists_of_friends[i];
5865 do {
5866 if (N==proxy) {
5867 ++j;
5868 printf("%s",_synctex_node_abstract(N));
5869 }
5870 } while ((N = _synctex_tree_friend(N)));
5871 }
5872 if (j) {
5873 printf("\n%i match\n",j);
5874 }
5875 }
5876 #endif
5877 /* Side effect: create the hierarchy on the fly */
5878 proxy = synctex_node_next(proxy); /* Change is here */
5879 #if SYNCTEX_DEBUG>500
5880 if (proxy) {
5881 int i,j = 0;
5882 for (i=0;i<proxy->class_->scanner->number_of_lists;++i) {
5883 synctex_node_p N = proxy->class_->scanner->lists_of_friends[i];
5884 do {
5885 if (N==proxy) {
5886 ++j;
5887 printf("%s",_synctex_node_abstract(N));
5888 }
5889 } while ((N = _synctex_tree_friend(N)));
5890 }
5891 if (j) {
5892 printf("\nnext %i match\n",j);
5893 }
5894 }
5895 #endif
5896 } while (proxy && proxy != halt);
5897 proxy = next_proxy;
5898 }
5899 return SYNCTEX_STATUS_OK;
5900 }
5901 /**
5902 * Replace all the form refs by root box proxies.
5903 * Create the node hierarchy and update the friends.
5904 * On entry, the refs are collected as a friend list
5905 * in either a form or a sheet
5906 * - parameter: the owning scanner
5907 */
5908 SYNCTEX_INLINE static synctex_status_t _synctex_post_process(synctex_scanner_p scanner) {
5909 synctex_status_t status = SYNCTEX_STATUS_OK;
5910 synctex_ns_s ns = {NULL,SYNCTEX_STATUS_NOT_OK};
5911 #if SYNCTEX_DEBUG>500
5912 printf("! entering _synctex_post_process.\n");
5913 synctex_node_display(scanner->sheet);
5914 synctex_node_display(scanner->form);
5915 #endif
5916 /* replace form refs inside forms by box proxies */
5917 ns = _synctex_post_process_ref(scanner->ref_in_form);
5918 scanner->ref_in_form = NULL;/* it was just released */
5919 if (ns.status<status) {
5920 status = ns.status;
5921 }
5922 #if SYNCTEX_DEBUG>500
5923 printf("! ref replaced in form _synctex_post_process.\n");
5924 synctex_node_display(scanner->form);
5925 #endif
5926 /* Create all the form proxy nodes on the fly.
5927 * ns.node is the root of the list of
5928 * newly created proxies.
5929 * There might be a problem with cascading proxies.
5930 * In order to be properly managed, the data must
5931 * be organized in the right way.
5932 * The inserted form must be defined before
5933 * the inserting one. *TeX will take care of that. */
5934 ns.status = _synctex_post_process_proxy(ns.node,&_synctex_tree_reset_friend);
5935 if (ns.status<status) {
5936 status = ns.status;
5937 }
5938 /* replace form refs inside sheets by box proxies */
5939 ns = _synctex_post_process_ref(scanner->ref_in_sheet);
5940 if (ns.status<status) {
5941 status = ns.status;
5942 }
5943 scanner->ref_in_sheet = NULL;
5944 #if SYNCTEX_DEBUG>500
5945 printf("! ref replaced in sheet _synctex_post_process.\n");
5946 synctex_node_display(scanner->sheet);
5947 #endif
5948 #if 0
5949 {
5950 int i;
5951 for (i=0;i<scanner->number_of_lists;++i) {
5952 synctex_node_p P = ns.node;
5953 do {
5954 synctex_node_p N = scanner->lists_of_friends[i];
5955 do {
5956 if (P == N) {
5957 printf("Already registered.\n");
5958 synctex_node_display(N);
5959 break;
5960 }
5961 } while ((N = _synctex_tree_friend(N)));
5962 } while((P = _synctex_tree_friend(P)));
5963 }
5964 }
5965 #endif
5966 #if SYNCTEX_DEBUG>10000
5967 {
5968 int i;
5969 for (i=0;i<scanner->number_of_lists;++i) {
5970 synctex_node_p P = scanner->lists_of_friends[i];
5971 int j = 0;
5972 while (P) {
5973 ++j;
5974 synctex_node_log(P);
5975 P = _synctex_tree_friend(P);
5976 }
5977 if (j) {
5978 printf("friends %i -> # %i\n",i,j);
5979 }
5980 }
5981 }
5982 #endif
5983 ns.status = _synctex_post_process_proxy(ns.node,&__synctex_proxy_make_friend_and_next_hbox);
5984 if (ns.status<status) {
5985 status = ns.status;
5986 }
5987 #if SYNCTEX_DEBUG>500
5988 printf("! exiting _synctex_post_process.\n");
5989 synctex_node_display(scanner->sheet);
5990 synctex_node_display(scanner->form);
5991 printf("! display all.\n");
5992 synctex_node_display(scanner->sheet);
5993 synctex_node_display(scanner->form);
5994 #endif
5995 return status;
5996 }
5997 /* Used when parsing the synctex file
5998 */
5999 static synctex_status_t _synctex_scan_content(synctex_scanner_p scanner) {
6000 if (NULL == scanner) {
6001 return SYNCTEX_STATUS_BAD_ARGUMENT;
6002 }
6003 scanner->reader->lastv = -1;
6004 synctex_status_t status = 0;
6005 /* Find where this section starts */
6006 content_not_found:
6007 status = _synctex_match_string(scanner,"Content:");
6008 if (status<SYNCTEX_STATUS_EOF) {
6009 return status;
6010 }
6011 if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
6012 _synctex_error("Incomplete Content.");
6013 return SYNCTEX_STATUS_ERROR;
6014 }
6015 if (status == SYNCTEX_STATUS_NOT_OK) {
6016 goto content_not_found;
6017 }
6018 status = __synctex_parse_sfi(scanner);
6019 if (status == SYNCTEX_STATUS_OK) {
6020 status = _synctex_post_process(scanner);
6021 }
6022 return status;
6023 }
6024 synctex_scanner_p synctex_scanner_new() {
6025 synctex_scanner_p scanner =(synctex_scanner_p)_synctex_malloc(sizeof(synctex_scanner_s));
6026 if (scanner) {
6027 if (!(scanner->reader = _synctex_malloc(sizeof(synctex_reader_s)))) {
6028 _synctex_free(scanner);
6029 return NULL;
6030 }
6031 # ifdef SYNCTEX_NOTHING
6032 # pragma mark -
6033 # endif
6034 # define DEFINE_synctex_scanner_class(NAME)\
6035 scanner->class_[synctex_node_type_##NAME] = synctex_class_##NAME;\
6036 (scanner->class_[synctex_node_type_##NAME]).scanner = scanner
6037 DEFINE_synctex_scanner_class(input);
6038 DEFINE_synctex_scanner_class(sheet);
6039 DEFINE_synctex_scanner_class(form);
6040 DEFINE_synctex_scanner_class(hbox);
6041 DEFINE_synctex_scanner_class(void_hbox);
6042 DEFINE_synctex_scanner_class(vbox);
6043 DEFINE_synctex_scanner_class(void_vbox);
6044 DEFINE_synctex_scanner_class(kern);
6045 DEFINE_synctex_scanner_class(glue);
6046 DEFINE_synctex_scanner_class(rule);
6047 DEFINE_synctex_scanner_class(math);
6048 DEFINE_synctex_scanner_class(boundary);
6049 DEFINE_synctex_scanner_class(box_bdry);
6050 DEFINE_synctex_scanner_class(ref);
6051 DEFINE_synctex_scanner_class(proxy_hbox);
6052 DEFINE_synctex_scanner_class(proxy_vbox);
6053 DEFINE_synctex_scanner_class(proxy);
6054 DEFINE_synctex_scanner_class(proxy_last);
6055 DEFINE_synctex_scanner_class(handle);
6056 /* set up the lists of friends */
6057 scanner->number_of_lists = 1024;
6058 scanner->lists_of_friends = (synctex_node_r)_synctex_malloc(scanner->number_of_lists*sizeof(synctex_node_p));
6059 if (NULL == scanner->lists_of_friends) {
6060 synctex_scanner_free(scanner);
6061 _synctex_error("malloc:2");
6062 return NULL;
6063 }
6064 scanner->display_switcher = 100;
6065 scanner->display_prompt = (char *)_synctex_display_prompt+strlen(_synctex_display_prompt)-1;
6066 }
6067 return scanner;
6068 }
6069 /* Where the synctex scanner is created. */
6070 synctex_scanner_p synctex_scanner_new_with_output_file(const char * output, const char * build_directory, int parse) {
6071 synctex_scanner_p scanner = synctex_scanner_new();
6072 if (NULL == scanner) {
6073 _synctex_error("malloc problem");
6074 return NULL;
6075 }
6076 if (synctex_reader_init_with_output_file(scanner->reader, output, build_directory)) {
6077 return parse? synctex_scanner_parse(scanner):scanner;
6078 }
6079 // don't warn to terminal if no file is present, this is a library.
6080 // _synctex_error("No file?");
6081 synctex_scanner_free(scanner);
6082 return NULL;
6083 }
6084
6085 /* The scanner destructor
6086 */
6087 int synctex_scanner_free(synctex_scanner_p scanner) {
6088 int node_count = 0;
6089 if (scanner) {
6090 synctex_node_free(scanner->sheet);
6091 synctex_node_free(scanner->form);
6092 synctex_node_free(scanner->input);
6093 synctex_reader_free(scanner->reader);
6094 SYNCTEX_SCANNER_FREE_HANDLE(scanner);
6095 synctex_iterator_free(scanner->iterator);
6096 free(scanner->output_fmt);
6097 free(scanner->lists_of_friends);
6098 #if SYNCTEX_USE_NODE_COUNT>0
6099 node_count = scanner->node_count;
6100 #endif
6101 free(scanner);
6102 }
6103 return node_count;
6104 }
6105
6106 /* Where the synctex scanner parses the contents of the file. */
6107 synctex_scanner_p synctex_scanner_parse(synctex_scanner_p scanner) {
6108 synctex_status_t status = 0;
6109 if (!scanner || scanner->flags.has_parsed) {
6110 return scanner;
6111 }
6112 scanner->flags.has_parsed=1;
6113 scanner->pre_magnification = 1000;
6114 scanner->pre_unit = 8192;
6115 scanner->pre_x_offset = scanner->pre_y_offset = 578;
6116 /* initialize the offset with a fake improbable value,
6117 * If there is a post scriptum section, this value will be overridden by the real life value */
6118 scanner->x_offset = scanner->y_offset = 6.027e23f;
6119 scanner->reader->line_number = 1;
6120
6121 synctex_scanner_set_display_switcher(scanner, 1000);
6122 SYNCTEX_END = SYNCTEX_START+SYNCTEX_BUFFER_SIZE;
6123 /* SYNCTEX_END always points to a null terminating character.
6124 * Maybe there is another null terminating character between SYNCTEX_CUR and SYNCTEX_END-1.
6125 * At least, we are sure that SYNCTEX_CUR points to a string covering a valid part of the memory. */
6126 *SYNCTEX_END = '\0';
6127 SYNCTEX_CUR = SYNCTEX_END;
6128 # if defined(SYNCTEX_USE_CHARINDEX)
6129 scanner->reader->charindex_offset = -SYNCTEX_BUFFER_SIZE;
6130 # endif
6131 status = _synctex_scan_preamble(scanner);
6132 if (status<SYNCTEX_STATUS_OK) {
6133 _synctex_error("Bad preamble\n");
6134 bailey:
6135 #ifdef SYNCTEX_DEBUG
6136 return scanner;
6137 #else
6138 synctex_scanner_free(scanner);
6139 return NULL;
6140 #endif
6141 }
6142 status = _synctex_scan_content(scanner);
6143 if (status<SYNCTEX_STATUS_OK) {
6144 _synctex_error("Bad content\n");
6145 goto bailey;
6146 }
6147 status = _synctex_scan_postamble(scanner);
6148 if (status<SYNCTEX_STATUS_OK) {
6149 _synctex_error("Bad postamble. Ignored\n");
6150 }
6151 #if SYNCTEX_DEBUG>500
6152 synctex_scanner_set_display_switcher(scanner, 100);
6153 synctex_node_display(scanner->sheet);
6154 synctex_node_display(scanner->form);
6155 #endif
6156 synctex_scanner_set_display_switcher(scanner, 1000);
6157 /* Everything is finished, free the buffer, close the file */
6158 free((void *)SYNCTEX_START);
6159 SYNCTEX_START = SYNCTEX_CUR = SYNCTEX_END = NULL;
6160 gzclose(SYNCTEX_FILE);
6161 SYNCTEX_FILE = NULL;
6162 /* Final tuning: set the default values for various parameters */
6163 /* 1 pre_unit = (scanner->pre_unit)/65536 pt = (scanner->pre_unit)/65781.76 bp
6164 * 1 pt = 65536 sp */
6165 if (scanner->pre_unit<=0) {
6166 scanner->pre_unit = 8192;
6167 }
6168 if (scanner->pre_magnification<=0) {
6169 scanner->pre_magnification = 1000;
6170 }
6171 if (scanner->unit <= 0) {
6172 /* no post magnification */
6173 scanner->unit = scanner->pre_unit / 65781.76;/* 65781.76 or 65536.0*/
6174 } else {
6175 /* post magnification */
6176 scanner->unit *= scanner->pre_unit / 65781.76;
6177 }
6178 scanner->unit *= scanner->pre_magnification / 1000.0;
6179 if (scanner->x_offset > 6e23) {
6180 /* no post offset */
6181 scanner->x_offset = scanner->pre_x_offset * (scanner->pre_unit / 65781.76);
6182 scanner->y_offset = scanner->pre_y_offset * (scanner->pre_unit / 65781.76);
6183 } else {
6184 /* post offset */
6185 scanner->x_offset /= 65781.76f;
6186 scanner->y_offset /= 65781.76f;
6187 }
6188 return scanner;
6189 #undef SYNCTEX_FILE
6190 }
6191
6192 /* Scanner accessors.
6193 */
6194 int synctex_scanner_pre_x_offset(synctex_scanner_p scanner){
6195 return scanner?scanner->pre_x_offset:0;
6196 }
6197 int synctex_scanner_pre_y_offset(synctex_scanner_p scanner){
6198 return scanner?scanner->pre_y_offset:0;
6199 }
6200 int synctex_scanner_x_offset(synctex_scanner_p scanner){
6201 return scanner?scanner->x_offset:0;
6202 }
6203 int synctex_scanner_y_offset(synctex_scanner_p scanner){
6204 return scanner?scanner->y_offset:0;
6205 }
6206 float synctex_scanner_magnification(synctex_scanner_p scanner){
6207 return scanner?scanner->unit:1;
6208 }
6209 void synctex_scanner_display(synctex_scanner_p scanner) {
6210 if (NULL == scanner) {
6211 return;
6212 }
6213 printf("The scanner:\noutput:%s\noutput_fmt:%s\nversion:%i\n",scanner->reader->output,scanner->output_fmt,scanner->version);
6214 printf("pre_unit:%i\nx_offset:%i\ny_offset:%i\n",scanner->pre_unit,scanner->pre_x_offset,scanner->pre_y_offset);
6215 printf("count:%i\npost_magnification:%f\npost_x_offset:%f\npost_y_offset:%f\n",
6216 scanner->count,scanner->unit,scanner->x_offset,scanner->y_offset);
6217 printf("The input:\n");
6218 synctex_node_display(scanner->input);
6219 if (scanner->count<1000) {
6220 printf("The sheets:\n");
6221 synctex_node_display(scanner->sheet);
6222 printf("The friends:\n");
6223 if (scanner->lists_of_friends) {
6224 int i = scanner->number_of_lists;
6225 synctex_node_p node;
6226 while(i--) {
6227 printf("Friend index:%i\n",i);
6228 node = (scanner->lists_of_friends)[i];
6229 while(node) {
6230 printf("%s:%i,%i\n",
6231 synctex_node_isa(node),
6232 _synctex_data_tag(node),
6233 _synctex_data_line(node)
6234 );
6235 node = _synctex_tree_friend(node);
6236 }
6237 }
6238 }
6239 } else {
6240 printf("SyncTeX Warning: Too many objects\n");
6241 }
6242 }
6243 /* Public */
6244 const char * synctex_scanner_get_name(synctex_scanner_p scanner,int tag) {
6245 synctex_node_p input = NULL;
6246 if (NULL == scanner) {
6247 return NULL;
6248 }
6249 if ((input = scanner->input)) {;
6250 do {
6251 if (tag == _synctex_data_tag(input)) {
6252 return (_synctex_data_name(input));
6253 }
6254 } while((input = __synctex_tree_sibling(input)));
6255 }
6256 return NULL;
6257 }
6258 const char * synctex_node_get_name(synctex_node_p node) {
6259 if (node) {
6260 return synctex_scanner_get_name(node->class_->scanner,_synctex_data_tag(node));
6261 }
6262 return NULL;
6263 }
6264
6265 static int _synctex_scanner_get_tag(synctex_scanner_p scanner,const char * name);
6266 static int _synctex_scanner_get_tag(synctex_scanner_p scanner,const char * name) {
6267 synctex_node_p input = NULL;
6268 if (NULL == scanner) {
6269 return 0;
6270 }
6271 if ((input = scanner->input)) {
6272 do {
6273 if (_synctex_is_equivalent_file_name(name,(_synctex_data_name(input)))) {
6274 return _synctex_data_tag(input);
6275 }
6276 } while((input = __synctex_tree_sibling(input)));
6277 }
6278 // 2011 version
6279 name = _synctex_base_name(name);
6280 if ((input = scanner->input)) {
6281 do {
6282 if (_synctex_is_equivalent_file_name(name,_synctex_base_name(_synctex_data_name(input)))) {
6283 synctex_node_p other_input = input;
6284 while((other_input = __synctex_tree_sibling(other_input))) {
6285 if (_synctex_is_equivalent_file_name(name,_synctex_base_name(_synctex_data_name(other_input)))
6286 && (strlen(_synctex_data_name(input))!=strlen(_synctex_data_name(other_input))
6287 || strncmp(_synctex_data_name(other_input),_synctex_data_name(input),strlen(_synctex_data_name(input))))) {
6288 // There is a second possible candidate
6289 return 0;
6290 }
6291 }
6292 return _synctex_data_tag(input);
6293 }
6294 } while((input = __synctex_tree_sibling(input)));
6295 }
6296 return 0;
6297 }
6298
6299 int synctex_scanner_get_tag(synctex_scanner_p scanner,const char * name) {
6300 size_t char_index = strlen(name);
6301 if ((scanner = synctex_scanner_parse(scanner)) && (0 < char_index)) {
6302 /* the name is not void */
6303 char_index -= 1;
6304 if (!SYNCTEX_IS_PATH_SEPARATOR(name[char_index])) {
6305 /* the last character of name is not a path separator */
6306 int result = _synctex_scanner_get_tag(scanner,name);
6307 if (result) {
6308 return result;
6309 } else {
6310 /* the given name was not the one known by TeX
6311 * try a name relative to the enclosing directory of the scanner->output file */
6312 const char * relative = name;
6313 const char * ptr = scanner->reader->output;
6314 while((strlen(relative) > 0) && (strlen(ptr) > 0) && (*relative == *ptr))
6315 {
6316 relative += 1;
6317 ptr += 1;
6318 }
6319 /* Find the last path separator before relative */
6320 while(relative > name) {
6321 if (SYNCTEX_IS_PATH_SEPARATOR(*(relative-1))) {
6322 break;
6323 }
6324 relative -= 1;
6325 }
6326 if ((relative > name) && (result = _synctex_scanner_get_tag(scanner,relative))) {
6327 return result;
6328 }
6329 if (SYNCTEX_IS_PATH_SEPARATOR(name[0])) {
6330 /* No tag found for the given absolute name,
6331 * Try each relative path starting from the shortest one */
6332 while(0<char_index) {
6333 char_index -= 1;
6334 if (SYNCTEX_IS_PATH_SEPARATOR(name[char_index])
6335 && (result = _synctex_scanner_get_tag(scanner,name+char_index+1))) {
6336 return result;
6337 }
6338 }
6339 }
6340 }
6341 return result;
6342 }
6343 }
6344 return 0;
6345 }
6346 synctex_node_p synctex_scanner_input(synctex_scanner_p scanner) {
6347 return scanner?scanner->input:NULL;
6348 }
6349 synctex_node_p synctex_scanner_input_with_tag(synctex_scanner_p scanner, int tag) {
6350 synctex_node_p input = scanner?scanner->input:NULL;
6351 while (_synctex_data_tag(input)!=tag) {
6352 if ((input = __synctex_tree_sibling(input))) {
6353 continue;
6354 }
6355 break;
6356 }
6357 return input;
6358 }
6359 const char * synctex_scanner_get_output_fmt(synctex_scanner_p scanner) {
6360 return NULL != scanner && scanner->output_fmt?scanner->output_fmt:"";
6361 }
6362 const char * synctex_scanner_get_output(synctex_scanner_p scanner) {
6363 return NULL != scanner && scanner->reader->output?scanner->reader->output:"";
6364 }
6365 const char * synctex_scanner_get_synctex(synctex_scanner_p scanner) {
6366 return NULL != scanner && scanner->reader->synctex?scanner->reader->synctex:"";
6367 }
6368 # ifdef SYNCTEX_NOTHING
6369 # pragma mark -
6370 # pragma mark Public node attributes
6371 # endif
6372
6373 # define SYNCTEX_DEFINE_NODE_HVWHD(WHAT) \
6374 int synctex_node_##WHAT(synctex_node_p node) { \
6375 return (node && node->class_->inspector->WHAT)? \
6376 node->class_->inspector->WHAT(node): 0; \
6377 }
6378 # define SYNCTEX_DEFINE_PROXY_HV(WHAT) \
6379 static int _synctex_proxy_##WHAT(synctex_proxy_p proxy) { \
6380 synctex_node_p target = _synctex_tree_target(proxy); \
6381 if (target) { \
6382 return _synctex_data_##WHAT(proxy)+synctex_node_##WHAT(target); \
6383 } else { \
6384 return proxy? _synctex_data_##WHAT(proxy): 0; \
6385 } \
6386 }
6387 #define SYNCTEX_DEFINE_PROXY_TLCWVD(WHAT) \
6388 static int _synctex_proxy_##WHAT(synctex_proxy_p proxy) { \
6389 synctex_node_p target = _synctex_tree_target(proxy); \
6390 return target? synctex_node_##WHAT(target): 0; \
6391 }
6392
6393 /**
6394 * The horizontal location of the node.
6395 * Idem for v, width, height and depth.
6396 * - parameter node: a node with geometrical information.
6397 * - returns: an integer.
6398 * - requires: every proxy node has a target.
6399 * - note: recursive call if the parameter has a proxy.
6400 * - author: JL
6401 */
6402 SYNCTEX_DEFINE_NODE_HVWHD(h)
6403 SYNCTEX_DEFINE_NODE_HVWHD(v)
6404 SYNCTEX_DEFINE_NODE_HVWHD(width)
6405 SYNCTEX_DEFINE_NODE_HVWHD(height)
6406 SYNCTEX_DEFINE_NODE_HVWHD(depth)
6407 SYNCTEX_DEFINE_PROXY_TLCWVD(tag)
6408 SYNCTEX_DEFINE_PROXY_TLCWVD(line)
6409 SYNCTEX_DEFINE_PROXY_TLCWVD(column)
6410 SYNCTEX_DEFINE_PROXY_HV(h)
6411 SYNCTEX_DEFINE_PROXY_HV(v)
6412 SYNCTEX_DEFINE_PROXY_TLCWVD(width)
6413 SYNCTEX_DEFINE_PROXY_TLCWVD(height)
6414 SYNCTEX_DEFINE_PROXY_TLCWVD(depth)
6415
6416 /**
6417 * Whether the argument is a box,
6418 * either vertical or horizontal,
6419 * either void or not,
6420 * or a proxy to such a box.
6421 * - parameter NODE: of type synctex_node_p
6422 * - returns: yorn
6423 */
6424
6425 SYNCTEX_INLINE static synctex_bool_t _synctex_node_is_box(synctex_node_p node) {
6426 return node &&
6427 (node->class_->type == synctex_node_type_hbox
6428 || node->class_->type == synctex_node_type_void_hbox
6429 || node->class_->type == synctex_node_type_vbox
6430 || node->class_->type == synctex_node_type_void_vbox
6431 || _synctex_node_is_box(_synctex_tree_target(node)));
6432 }
6433
6434 /**
6435 * Whether the argument is a handle.
6436 * Handles are similar to proxies because they have a target.
6437 * They are used for query results.
6438 * - parameter NODE: of type synctex_node_p
6439 * - returns: yorn
6440 */
6441
6442 SYNCTEX_INLINE static synctex_bool_t _synctex_node_is_handle(synctex_node_p node) {
6443 return node &&
6444 (node->class_->type == synctex_node_type_handle);
6445 }
6446
6447 /**
6448 * Resolves handle indirection.
6449 * - parameter node: of type synctex_node_p
6450 * - returns: node if it is not a handle,
6451 * its target otherwise.
6452 */
6453
6454 SYNCTEX_INLINE static synctex_node_p _synctex_node_or_handle_target(synctex_node_p node) {
6455 return _synctex_node_is_handle(node)?
6456 _synctex_tree_target(node):node;
6457 }
6458
6459 /**
6460 * Whether the argument is an hbox.
6461 * - parameter NODE: of type synctex_node_p
6462 * - returns: yorn
6463 */
6464
6465 SYNCTEX_INLINE static synctex_bool_t _synctex_node_is_hbox(synctex_node_p node) {
6466 return node &&
6467 (node->class_->type == synctex_node_type_hbox
6468 || node->class_->type == synctex_node_type_void_hbox
6469 || _synctex_node_is_hbox(_synctex_tree_target(node)));
6470 }
6471
6472 /**
6473 * The horizontal location of the first box enclosing node.
6474 * - parameter node: a node with geometrical information.
6475 * - returns: an integer.
6476 * - author: JL
6477 */
6478 int synctex_node_box_h(synctex_node_p node) {
6479 if (_synctex_node_is_box(node) || (node = _synctex_tree_parent(node))) {
6480 return synctex_node_h(node);
6481 }
6482 return 0;
6483 }
6484 /**
6485 * The vertical location of the first box enclosing node.
6486 * - parameter node: a node with geometrical information.
6487 * - returns: an integer.
6488 * - author: JL
6489 */
6490 int synctex_node_box_v(synctex_node_p node) {
6491 if (_synctex_node_is_box(node) || (node = _synctex_tree_parent(node))) {
6492 return synctex_node_v(node);
6493 }
6494 return 0;
6495 }
6496 /**
6497 * The width of the first box enclosing node.
6498 * - parameter node: a node with geometrical information.
6499 * - returns: an integer.
6500 * - author: JL
6501 */
6502 int synctex_node_box_width(synctex_node_p node) {
6503 if (_synctex_node_is_box(node) || (node = _synctex_tree_parent(node))) {
6504 return synctex_node_width(node);
6505 }
6506 return 0;
6507 }
6508 /**
6509 * The height of the first box enclosing node.
6510 * - parameter node: a node with geometrical information.
6511 * - returns: an integer.
6512 * - author: JL
6513 */
6514 int synctex_node_box_height(synctex_node_p node) {
6515 if (_synctex_node_is_box(node) || (node = _synctex_tree_parent(node))) {
6516 return synctex_node_height(node);
6517 }
6518 return 0;
6519 }
6520 /**
6521 * The depth of the first box enclosing node.
6522 * - parameter node: a node with geometrical information.
6523 * - returns: an integer.
6524 * - author: JL
6525 */
6526 int synctex_node_box_depth(synctex_node_p node) {
6527 if (_synctex_node_is_box(node) || (node = _synctex_tree_parent(node))) {
6528 return synctex_node_depth(node);
6529 }
6530 return 0;
6531 }
6532 /**
6533 * The horizontal location of an hbox, corrected with contents.
6534 * - parameter node: an hbox node.
6535 * - returns: an integer, 0 if node is not an hbox or an hbox proxy.
6536 * - note: recursive call when node is an hbox proxy.
6537 * - author: JL
6538 */
6539 int synctex_node_hbox_h(synctex_node_p node) {
6540 switch(synctex_node_type(node)) {
6541 case synctex_node_type_hbox:
6542 return _synctex_data_h_V(node);
6543 case synctex_node_type_proxy_hbox:
6544 return _synctex_data_h(node)+synctex_node_hbox_h(_synctex_tree_target(node));
6545 default:
6546 return 0;
6547 }
6548 }
6549 /**
6550 * The vertical location of an hbox, corrected with contents.
6551 * - parameter node: an hbox node.
6552 * - returns: an integer, 0 if node is not an hbox or an hbox proxy.
6553 * - note: recursive call when node is an hbox proxy.
6554 * - author: JL
6555 */
6556 int synctex_node_hbox_v(synctex_node_p node) {
6557 switch(synctex_node_type(node)) {
6558 case synctex_node_type_hbox:
6559 return _synctex_data_v_V(node);
6560 case synctex_node_type_proxy_hbox:
6561 return _synctex_data_v(node)+synctex_node_hbox_v(_synctex_tree_target(node));
6562 default:
6563 return 0;
6564 }
6565 }
6566 /**
6567 * The width of an hbox, corrected with contents.
6568 * - parameter node: an hbox node, 0 if node is not an hbox or an hbox proxy.
6569 * - returns: an integer.
6570 * - author: JL
6571 */
6572 int synctex_node_hbox_width(synctex_node_p node) {
6573 synctex_node_p target = _synctex_tree_target(node);
6574 if (target) {
6575 node = target;
6576 }
6577 return synctex_node_type(node) == synctex_node_type_hbox?
6578 _synctex_data_width_V(node): 0;
6579 }
6580 /**
6581 * The height of an hbox, corrected with contents.
6582 * - parameter node: an hbox node.
6583 * - returns: an integer, 0 if node is not an hbox or an hbox proxy.
6584 * - author: JL
6585 */
6586 int synctex_node_hbox_height(synctex_node_p node) {
6587 synctex_node_p target = _synctex_tree_target(node);
6588 if (target) {
6589 node = target;
6590 }
6591 return synctex_node_type(node) == synctex_node_type_hbox?
6592 _synctex_data_height_V(node): 0;
6593 }
6594 /**
6595 * The depth of an hbox, corrected with contents.
6596 * - parameter node: an hbox node.
6597 * - returns: an integer, 0 if node is not an hbox or an hbox proxy.
6598 * - note: recursive call when node is an hbox proxy.
6599 * - author: JL
6600 */
6601 int synctex_node_hbox_depth(synctex_node_p node) {
6602 synctex_node_p target = _synctex_tree_target(node);
6603 if (target) {
6604 node = target;
6605 }
6606 return synctex_node_type(node) == synctex_node_type_hbox?
6607 _synctex_data_depth_V(node): 0;
6608 }
6609 # ifdef SYNCTEX_NOTHING
6610 # pragma mark -
6611 # pragma mark Public node visible attributes
6612 # endif
6613
6614 #define SYNCTEX_VISIBLE_SIZE(node,s) \
6615 (s)*node->class_->scanner->unit
6616 #define SYNCTEX_VISIBLE_DISTANCE_h(node,d) \
6617 ((d)*node->class_->scanner->unit+node->class_->scanner->x_offset)
6618 #define SYNCTEX_VISIBLE_DISTANCE_v(node,d) \
6619 ((d)*node->class_->scanner->unit+node->class_->scanner->y_offset)
6620 static float __synctex_node_visible_h(synctex_node_p node) {
6621 return SYNCTEX_VISIBLE_DISTANCE_h(node,synctex_node_h(node));
6622 }
6623 static float __synctex_node_visible_v(synctex_node_p node) {
6624 return SYNCTEX_VISIBLE_DISTANCE_v(node,synctex_node_v(node));
6625 }
6626 static float __synctex_node_visible_width(synctex_node_p node) {
6627 return SYNCTEX_VISIBLE_SIZE(node,synctex_node_width(node));
6628 }
6629 static float __synctex_node_visible_height(synctex_node_p node) {
6630 return SYNCTEX_VISIBLE_SIZE(node,synctex_node_height(node));
6631 }
6632 static float __synctex_node_visible_depth(synctex_node_p node) {
6633 return SYNCTEX_VISIBLE_SIZE(node,synctex_node_depth(node));
6634 }
6635 static float __synctex_proxy_visible_h(synctex_node_p node) {
6636 return SYNCTEX_VISIBLE_DISTANCE_h(node,synctex_node_h(node));
6637 }
6638 static float __synctex_proxy_visible_v(synctex_node_p node) {
6639 return SYNCTEX_VISIBLE_DISTANCE_v(node,synctex_node_v(node));
6640 }
6641 static float __synctex_proxy_visible_width(synctex_node_p node) {
6642 synctex_node_p target = _synctex_tree_target(node);
6643 return __synctex_node_visible_width(target);
6644 }
6645 static float __synctex_proxy_visible_height(synctex_node_p node) {
6646 synctex_node_p target = _synctex_tree_target(node);
6647 return __synctex_node_visible_height(target);
6648 }
6649 static float __synctex_proxy_visible_depth(synctex_node_p node) {
6650 synctex_node_p target = _synctex_tree_target(node);
6651 return __synctex_node_visible_depth(target);
6652 }
6653 static float __synctex_kern_visible_h(synctex_noxy_p noxy) {
6654 int h = _synctex_data_h(noxy);
6655 int width = _synctex_data_width(noxy);
6656 return SYNCTEX_VISIBLE_DISTANCE_h(noxy, width>0?h-width:h);
6657 }
6658 static float __synctex_kern_visible_width(synctex_noxy_p noxy) {
6659 int width = _synctex_data_width(noxy);
6660 return SYNCTEX_VISIBLE_SIZE(noxy, width>0?width:-width);
6661 }
6662 static float __synctex_rule_visible_h(synctex_noxy_p noxy) {
6663 int h = _synctex_data_h(noxy);
6664 int width = _synctex_data_width(noxy);
6665 return SYNCTEX_VISIBLE_DISTANCE_h(noxy, width>0?h:h-width);
6666 }
6667 static float __synctex_rule_visible_width(synctex_noxy_p noxy) {
6668 int width = _synctex_data_width(noxy);
6669 return SYNCTEX_VISIBLE_SIZE(noxy, width>0?width:-width);
6670 }
6671 static float __synctex_rule_visible_v(synctex_noxy_p noxy) {
6672 return __synctex_node_visible_v(noxy);
6673 }
6674 static float __synctex_rule_visible_height(synctex_noxy_p noxy) {
6675 return __synctex_node_visible_height(noxy);
6676 }
6677 static float __synctex_rule_visible_depth(synctex_noxy_p noxy) {
6678 return __synctex_node_visible_depth(noxy);
6679 }
6680
6681 /**
6682 * The horizontal location of node, in page coordinates.
6683 * - parameter node: a node.
6684 * - returns: a float.
6685 * - author: JL
6686 */
6687 float synctex_node_visible_h(synctex_node_p node){
6688 return node? node->class_->vispector->h(node): 0;
6689 }
6690 /**
6691 * The vertical location of node, in page coordinates.
6692 * - parameter node: a node.
6693 * - returns: a float.
6694 * - author: JL
6695 */
6696 float synctex_node_visible_v(synctex_node_p node){
6697 return node? node->class_->vispector->v(node): 0;
6698 }
6699 /**
6700 * The width of node, in page coordinates.
6701 * - parameter node: a node.
6702 * - returns: a float.
6703 * - author: JL
6704 */
6705 float synctex_node_visible_width(synctex_node_p node){
6706 return node? node->class_->vispector->width(node): 0;
6707 }
6708 /**
6709 * The height of node, in page coordinates.
6710 * - parameter node: a node.
6711 * - returns: a float.
6712 * - author: JL
6713 */
6714 float synctex_node_visible_height(synctex_node_p node){
6715 return node? node->class_->vispector->height(node): 0;
6716 }
6717 /**
6718 * The depth of node, in page coordinates.
6719 * - parameter node: a node.
6720 * - returns: a float.
6721 * - author: JL
6722 */
6723 float synctex_node_visible_depth(synctex_node_p node){
6724 return node? node->class_->vispector->depth(node): 0;
6725 }
6726
6727 /**
6728 * The V variant of geometrical information.
6729 * - parameter node: a node.
6730 * - returns: an integer.
6731 * - author: JL
6732 */
6733 #define SYNCTEX_DEFINE_V(WHAT)\
6734 SYNCTEX_INLINE static int _synctex_node_##WHAT##_V(synctex_node_p node) { \
6735 synctex_node_p target = _synctex_tree_target(node); \
6736 if (target) { \
6737 return _synctex_data_##WHAT(node)+_synctex_node_##WHAT##_V(target); \
6738 } else if (_synctex_data_has_##WHAT##_V(node)) { \
6739 return _synctex_data_##WHAT##_V(node); \
6740 } else { \
6741 return _synctex_data_##WHAT(node); \
6742 } \
6743 }
6744 SYNCTEX_DEFINE_V(h)
6745 SYNCTEX_DEFINE_V(v)
6746 SYNCTEX_DEFINE_V(width)
6747 SYNCTEX_DEFINE_V(height)
6748 SYNCTEX_DEFINE_V(depth)
6749
6750 SYNCTEX_INLINE static synctex_point_s _synctex_data_point(synctex_node_p node) {
6751 return (synctex_point_s){synctex_node_h(node),synctex_node_v(node)};
6752 }
6753 SYNCTEX_INLINE static synctex_point_s _synctex_data_point_V(synctex_node_p node) {
6754 return (synctex_point_s){_synctex_node_h_V(node),_synctex_node_v_V(node)};
6755 }
6756 SYNCTEX_INLINE static synctex_point_s _synctex_data_set_point(synctex_node_p node, synctex_point_s point) {
6757 synctex_point_s old = _synctex_data_point(node);
6758 _synctex_data_set_h(node,point.h);
6759 _synctex_data_set_v(node,point.v);
6760 return old;
6761 }
6762 SYNCTEX_INLINE static synctex_box_s _synctex_data_box(synctex_node_p node) {
6763 synctex_box_s box = {{0,0},{0,0}};
6764 int n;
6765 n = synctex_node_width(node);
6766 if (n<0) {
6767 box.max.h = synctex_node_h(node);
6768 box.min.h = box.max.h + n;
6769 } else {
6770 box.min.h = synctex_node_h(node);
6771 box.max.h = box.min.h + n;
6772 }
6773 n = synctex_node_v(node);
6774 box.min.v = n - synctex_node_height(node);
6775 box.max.v = n + synctex_node_depth(node);
6776 return box;
6777 }
6778 SYNCTEX_INLINE static synctex_box_s _synctex_data_xob(synctex_node_p node) {
6779 synctex_box_s box = {{0,0},{0,0}};
6780 int n;
6781 n = synctex_node_width(node);
6782 if (n>0) {
6783 box.max.h = synctex_node_h(node);
6784 box.min.h = box.max.h - n;
6785 } else {
6786 box.min.h = synctex_node_h(node);
6787 box.max.h = box.min.h - n;
6788 }
6789 n = synctex_node_v(node);
6790 box.min.v = n - synctex_node_height(node);
6791 box.max.v = n + synctex_node_depth(node);
6792 return box;
6793 }
6794 SYNCTEX_INLINE static synctex_box_s _synctex_data_box_V(synctex_node_p node) {
6795 synctex_box_s box = {{0,0},{0,0}};
6796 int n;
6797 n = _synctex_node_width_V(node);
6798 if (n<0) {
6799 box.max.h = _synctex_node_h_V(node);
6800 box.min.h = box.max.h + n;
6801 } else {
6802 box.min.h = _synctex_node_h_V(node);
6803 box.max.h = box.min.h + n;
6804 }
6805 n = _synctex_node_v_V(node);
6806 box.min.v = n - _synctex_node_height_V(node);
6807 box.max.v = n + _synctex_node_depth_V(node);
6808 return box;
6809 }
6810
6811 /**
6812 * The higher box node in the parent hierarchy which
6813 * mean line number is the one of node ±1.
6814 * This enclosing box is computed as follows
6815 * 1) get the first hbox in the parent linked list
6816 * starting at node.
6817 * If there is none, simply return the parent of node.
6818 * 2) compute the mean line number
6819 * 3) scans up the tree for the higher hbox with
6820 * the same mean line number, ±1 eventually
6821 * - parameter node: a node.
6822 * - returns: a (proxy to a) box node.
6823 * - author: JL
6824 */
6825 static synctex_node_p _synctex_node_box_visible(synctex_node_p node) {
6826 if ((node = _synctex_node_or_handle_target(node))) {
6827 int mean = 0;
6828 int bound = 1500000/(node->class_->scanner->pre_magnification/1000.0);
6829 synctex_node_p parent = NULL;
6830 /* get the first enclosing parent
6831 * then get the highest enclosing parent with the same mean line ±1 */
6832 node = _synctex_node_or_handle_target(node);
6833 if (!_synctex_node_is_box(node)) {
6834 if ((parent = _synctex_tree_parent(node))) {
6835 node = parent;
6836 } else if ((node = _synctex_tree_target(node))) {
6837 if (!_synctex_node_is_box(node)) {
6838 if ((parent = _synctex_tree_parent(node))) {
6839 node = parent;
6840 } else {
6841 return NULL;
6842 }
6843 }
6844 }
6845 }
6846 parent = node;
6847 mean = synctex_node_mean_line(node);
6848 while ((parent = _synctex_tree_parent(parent))) {
6849 if (_synctex_node_is_hbox(parent)) {
6850 if (_synctex_abs(mean-synctex_node_mean_line(parent))>1) {
6851 return node;
6852 } else if (synctex_node_width(parent)>bound) {
6853 return parent;
6854 } else if (synctex_node_height(parent)+synctex_node_depth(parent)>bound) {
6855 return parent;
6856 }
6857 node = parent;
6858 }
6859 }
6860 }
6861 return node;
6862 }
6863 /**
6864 * The horizontal location of the first box enclosing node, in page coordinates.
6865 * - parameter node: a node.
6866 * - returns: a float.
6867 * - author: JL
6868 */
6869 float synctex_node_box_visible_h(synctex_node_p node) {
6870 return SYNCTEX_VISIBLE_DISTANCE_h(node,_synctex_node_h_V(_synctex_node_box_visible(node)));
6871 }
6872 /**
6873 * The vertical location of the first box enclosing node, in page coordinates.
6874 * - parameter node: a node.
6875 * - returns: a float.
6876 * - author: JL
6877 */
6878 float synctex_node_box_visible_v(synctex_node_p node) {
6879 return SYNCTEX_VISIBLE_DISTANCE_v(node,_synctex_node_v_V(_synctex_node_box_visible(node)));
6880 }
6881 /**
6882 * The width of the first box enclosing node, in page coordinates.
6883 * - parameter node: a node.
6884 * - returns: a float.
6885 * - author: JL
6886 */
6887 float synctex_node_box_visible_width(synctex_node_p node) {
6888 return SYNCTEX_VISIBLE_SIZE(node,_synctex_node_width_V(_synctex_node_box_visible(node)));
6889 }
6890 /**
6891 * The height of the first box enclosing node, in page coordinates.
6892 * - parameter node: a node.
6893 * - returns: a float.
6894 * - author: JL
6895 */
6896 float synctex_node_box_visible_height(synctex_node_p node) {
6897 return SYNCTEX_VISIBLE_SIZE(node,_synctex_node_height_V(_synctex_node_box_visible(node)));
6898 }
6899 /**
6900 * The depth of the first box enclosing node, in page coordinates.
6901 * - parameter node: a node.
6902 * - returns: a float.
6903 * - author: JL
6904 */
6905 float synctex_node_box_visible_depth(synctex_node_p node) {
6906 return SYNCTEX_VISIBLE_SIZE(node,_synctex_node_depth_V(_synctex_node_box_visible(node)));
6907 }
6908 # ifdef SYNCTEX_NOTHING
6909 # pragma mark -
6910 # pragma mark Other public node attributes
6911 # endif
6912
6913 /**
6914 * The page number of the sheet enclosing node.
6915 * - parameter node: a node.
6916 * - returns: the page number or -1 if node does not belong to a sheet tree.
6917 * - note: a proxy target does not belong to a sheet
6918 * but a form, its page number is always -1.
6919 * - note: a handles does not belong to a sheet not a form.
6920 * its page number is -1.
6921 * - author: JL
6922 */
6923 int synctex_node_page(synctex_node_p node){
6924 synctex_node_p parent = NULL;
6925 while((parent = _synctex_tree_parent(node))) {
6926 node = parent;
6927 }
6928 if (synctex_node_type(node) == synctex_node_type_sheet) {
6929 return _synctex_data_page(node);
6930 }
6931 return -1;
6932 }
6933 /**
6934 * The page number of the target.
6935 * - author: JL
6936 */
6937 SYNCTEX_INLINE static int _synctex_node_target_page(synctex_node_p node){
6938 return synctex_node_page(_synctex_tree_target(node));
6939 }
6940
6941 #if defined (SYNCTEX_USE_CHARINDEX)
6942 synctex_charindex_t synctex_node_charindex(synctex_node_p node) {
6943 synctex_node_p target = _synctex_tree_target(node);
6944 return target? SYNCTEX_CHARINDEX(target):(node?SYNCTEX_CHARINDEX(node):0);
6945 }
6946 #endif
6947
6948 /**
6949 * The tag of the node.
6950 * - parameter node: a node.
6951 * - returns: the tag or -1 if node is NULL.
6952 * - author: JL
6953 */
6954 int synctex_node_tag(synctex_node_p node) {
6955 return node? node->class_->tlcpector->tag(node): -1;
6956 }
6957 /**
6958 * The line of the node.
6959 * - parameter node: a node.
6960 * - returns: the line or -1 if node is NULL.
6961 * - author: JL
6962 */
6963 int synctex_node_line(synctex_node_p node) {
6964 return node? node->class_->tlcpector->line(node): -1;
6965 }
6966 /**
6967 * The column of the node.
6968 * - parameter node: a node.
6969 * - returns: the column or -1 if node is NULL.
6970 * - author: JL
6971 */
6972 int synctex_node_column(synctex_node_p node) {
6973 return node? node->class_->tlcpector->column(node): -1;
6974 }
6975 /**
6976 * The mean line number of the node.
6977 * - parameter node: a node.
6978 * - returns: the mean line or -1 if node is NULL.
6979 * - author: JL
6980 */
6981 int synctex_node_mean_line(synctex_node_p node) {
6982 synctex_node_p other = _synctex_tree_target(node);
6983 if (other) {
6984 node = other;
6985 }
6986 if (_synctex_data_has_mean_line(node)) {
6987 return _synctex_data_mean_line(node);
6988 }
6989 if ((other = synctex_node_parent(node))) {
6990 if (_synctex_data_has_mean_line(other)) {
6991 return _synctex_data_mean_line(other);
6992 }
6993 }
6994 return synctex_node_line(node);
6995 }
6996 /**
6997 * The weight of the node.
6998 * - parameter node: a node.
6999 * - returns: the weight or -1 if node is NULL.
7000 * - author: JL
7001 */
7002 int synctex_node_weight(synctex_node_p node) {
7003 synctex_node_p target = _synctex_tree_target(node);
7004 if (target) {
7005 node = target;
7006 }
7007 return node?(synctex_node_type(node)==synctex_node_type_hbox?_synctex_data_weight(node):0):-1;
7008 }
7009 /**
7010 * The number of children of the node.
7011 * - parameter node: a node.
7012 * - returns: the count or -1 if node is NULL.
7013 * - author: JL
7014 */
7015 int synctex_node_child_count(synctex_node_p node) {
7016 synctex_node_p target = _synctex_tree_target(node);
7017 if (target) {
7018 node = target;
7019 }
7020 return node?(synctex_node_type(node)==synctex_node_type_hbox?_synctex_data_weight(node):0):-1;
7021 }
7022 # ifdef SYNCTEX_NOTHING
7023 # pragma mark -
7024 # pragma mark Sheet & Form
7025 # endif
7026
7027 /**
7028 * The sheet of the scanner with a given page number.
7029 * - parameter scanner: a scanner.
7030 * - parameter page: a 1 based page number.
7031 * If page == 0, returns the first sheet.
7032 * - returns: a sheet or NULL.
7033 * - author: JL
7034 */
7035 synctex_node_p synctex_sheet(synctex_scanner_p scanner,int page) {
7036 if (scanner) {
7037 synctex_node_p sheet = scanner->sheet;
7038 while(sheet) {
7039 if (page == _synctex_data_page(sheet)) {
7040 return sheet;
7041 }
7042 sheet = __synctex_tree_sibling(sheet);
7043 }
7044 if (page == 0) {
7045 return scanner->sheet;
7046 }
7047 }
7048 return NULL;
7049 }
7050 /**
7051 * The form of the scanner with a given tag.
7052 * - parameter scanner: a scanner.
7053 * - parameter tag: an integer identifier.
7054 * If tag == 0, returns the first form.
7055 * - returns: a form.
7056 * - author: JL
7057 */
7058 synctex_node_p synctex_form(synctex_scanner_p scanner,int tag) {
7059 if (scanner) {
7060 synctex_node_p form = scanner->form;
7061 while(form) {
7062 if (tag == _synctex_data_tag(form)) {
7063 return form;
7064 }
7065 form = __synctex_tree_sibling(form);
7066 }
7067 if (tag == 0) {
7068 return scanner->form;
7069 }
7070 }
7071 return NULL;
7072 }
7073
7074 /**
7075 * The content of the sheet with given page number.
7076 * - parameter scanner: a scanner.
7077 * - parameter page: a 1 based page number.
7078 * - returns: a (vertical) box node.
7079 * - author: JL
7080 */
7081 synctex_node_p synctex_sheet_content(synctex_scanner_p scanner,int page) {
7082 if (scanner) {
7083 return _synctex_tree_child(synctex_sheet(scanner,page));
7084 }
7085 return NULL;
7086 }
7087
7088 /**
7089 * The content of the sheet with given page number.
7090 * - parameter scanner: a scanner.
7091 * - parameter tag: an integer identifier.
7092 * - returns: a box node.
7093 * - author: JL
7094 */
7095 synctex_node_p synctex_form_content(synctex_scanner_p scanner,int tag) {
7096 if (scanner) {
7097 return _synctex_tree_child(synctex_form(scanner,tag));
7098 }
7099 return NULL;
7100 }
7101
7102 SYNCTEX_INLINE static synctex_node_p _synctex_scanner_friend(synctex_scanner_p scanner,int i) {
7103 if (i>=0) {
7104 i = _synctex_abs(i)%(scanner->number_of_lists);
7105 return (scanner->lists_of_friends)[i];
7106 }
7107 return NULL;
7108 }
7109 SYNCTEX_INLINE static synctex_bool_t _synctex_nodes_are_friend(synctex_node_p left, synctex_node_p right) {
7110 return synctex_node_tag(left) == synctex_node_tag(right) && synctex_node_line(left) == synctex_node_line(right);
7111 }
7112 /**
7113 * The sibling argument is a parent/child list of nodes of the same page.
7114 */
7115 typedef struct {
7116 int count;
7117 synctex_node_p node;
7118 } synctex_counted_node_s;
7119
7120 SYNCTEX_INLINE static synctex_counted_node_s _synctex_vertically_sorted_v2(synctex_node_p sibling) {
7121 /* Clean the weights of the parents */
7122 synctex_counted_node_s result = {0, NULL};
7123 synctex_node_p h = NULL;
7124 synctex_node_p next_h = NULL;
7125 synctex_node_p parent = NULL;
7126 int weight = 0;
7127 synctex_node_p N = NULL;
7128 h = sibling;
7129 do {
7130 N = _synctex_tree_target(h);
7131 parent = _synctex_tree_parent(N);
7132 _synctex_data_set_weight(parent, 0);
7133 } while((h = _synctex_tree_child(h)));
7134 /* Compute the weights of the nodes */
7135 h = sibling;
7136 do {
7137 N = _synctex_tree_target(h);
7138 parent = _synctex_tree_parent(N);
7139 weight = _synctex_data_weight(parent);
7140 if (weight==0) {
7141 N = _synctex_tree_child(parent);
7142 do {
7143 if (_synctex_nodes_are_friend(N,sibling)) {
7144 ++ weight;
7145 }
7146 } while ((N = __synctex_tree_sibling(N)));
7147 _synctex_data_set_weight(h,weight);
7148 _synctex_data_set_weight(parent,weight);
7149 }
7150 } while((h = _synctex_tree_child(h)));
7151 /* Order handle nodes according to the weight */
7152 h = _synctex_tree_reset_child(sibling);
7153 result.node = sibling;
7154 weight = 0;
7155 while((h)) {
7156 N = result.node;
7157 if (_synctex_data_weight(h)>_synctex_data_weight(N)) {
7158 next_h = _synctex_tree_set_child(h,N);
7159 result.node = h;
7160 } else if (_synctex_data_weight(h) == 0) {
7161 ++ weight;
7162 next_h = _synctex_tree_reset_child(h);
7163 synctex_node_free(h);
7164 } else {
7165 synctex_node_p next_N = NULL;
7166 while((next_N = _synctex_tree_child(N))) {
7167 N = next_N;
7168 if (_synctex_data_weight(h)<_synctex_data_weight(next_N)) {
7169 continue;
7170 }
7171 break;
7172 }
7173 next_h = _synctex_tree_set_child(h,_synctex_tree_set_child(N,h));
7174 }
7175 h = next_h;
7176 };
7177 h = result.node;
7178 weight = 0;
7179 do {
7180 ++weight;
7181 } while((h = _synctex_tree_child(h)));
7182 result.count = 1;
7183 h = result.node;
7184 while((next_h = _synctex_tree_child(h))) {
7185 if (_synctex_data_weight(next_h)==0) {
7186 _synctex_tree_reset_child(h);
7187 weight = 1;
7188 h = next_h;
7189 while((h = _synctex_tree_child(h))) {
7190 ++weight;
7191 }
7192 synctex_node_free(next_h);
7193 break;
7194 }
7195 ++result.count;
7196 h = next_h;
7197 }
7198 return result;
7199 }
7200
7201 SYNCTEX_INLINE static synctex_bool_t _synctex_point_in_box_v2(synctex_point_p hitP, synctex_node_p node);
7202
7203 /* This struct records distances, the left one is non negative and the right one is non positive.
7204 * When comparing the locations of 2 different graphical objects on the page, we will have to also record the
7205 * horizontal distance as signed to keep track of the typesetting order.*/
7206
7207 typedef struct {
7208 synctex_node_p node;
7209 int distance;
7210 } synctex_nd_s;
7211
7212 #define SYNCTEX_ND_0 (synctex_nd_s){NULL,INT_MAX}
7213
7214 typedef synctex_nd_s * synctex_nd_p;
7215
7216 typedef struct {
7217 synctex_nd_s l;
7218 synctex_nd_s r;
7219 } synctex_nd_lr_s;
7220
7221 /* The best container is the deeper box that contains the hit point (H,V).
7222 * _synctex_eq_deepest_container_v2 starts with node whereas
7223 * _synctex_box_child_deepest starts with node's children, if any
7224 * if node is not a box, or a void box, NULL is returned.
7225 * We traverse the node tree in a deep first manner and stop as soon as a result is found. */
7226 static synctex_node_p _synctex_eq_deepest_container_v2(synctex_point_p hitP, synctex_node_p node);
7227
7228 SYNCTEX_INLINE static synctex_nd_lr_s _synctex_eq_get_closest_children_in_box_v2(synctex_point_p hitP, synctex_node_p node);
7229
7230 /* Closest child, recursive. */
7231 static synctex_nd_s __synctex_closest_deep_child_v2(synctex_point_p hitP, synctex_node_p node);
7232
7233 /* The smallest container between two has the smallest width or height.
7234 * This comparison is used when there are 2 overlapping boxes that contain the hit point.
7235 * For ConTeXt, the problem appears at each page.
7236 * The chosen box is the one with the smallest height, then the smallest width. */
7237 SYNCTEX_INLINE static synctex_node_p _synctex_smallest_container_v2(synctex_node_p node, synctex_node_p other_node);
7238
7239 /* Returns the distance between the hit point hit point=(H,V) and the given node. */
7240
7241 static int _synctex_point_node_distance_v2(synctex_point_p hitP, synctex_node_p node);
7242
7243 /* The closest container is the box that is the one closest to the given point.
7244 * The "visible" version takes into account the visible dimensions instead of the real ones given by TeX. */
7245 static synctex_nd_s _synctex_eq_closest_child_v2(synctex_point_p hitP, synctex_node_p node);
7246
7247 # ifdef SYNCTEX_NOTHING
7248 # pragma mark -
7249 # pragma mark Queries
7250 # endif
7251
7252 /**
7253 * iterator for a deep first tree traversal.
7254 */
7255 struct synctex_iterator_t {
7256 synctex_node_p seed;
7257 synctex_node_p top;
7258 synctex_node_p next;
7259 int count0;
7260 int count;
7261 };
7262
7263 SYNCTEX_INLINE static synctex_iterator_p _synctex_iterator_new(synctex_node_p result, int count) {
7264 synctex_iterator_p iterator;
7265 if ((iterator = _synctex_malloc(sizeof(synctex_iterator_s)))) {
7266 iterator->seed = iterator->top = iterator->next = result;
7267 iterator->count0 = iterator->count = count;
7268 }
7269 return iterator;
7270 }
7271
7272 void synctex_iterator_free(synctex_iterator_p iterator) {
7273 if (iterator) {
7274 synctex_node_free(iterator->seed);
7275 _synctex_free(iterator);
7276 }
7277 }
7278 synctex_bool_t synctex_iterator_has_next(synctex_iterator_p iterator) {
7279 return iterator?iterator->count>0:0;
7280 }
7281 int synctex_iterator_count(synctex_iterator_p iterator) {
7282 return iterator? iterator->count: 0;
7283 }
7284
7285 /**
7286 * The next result of the iterator.
7287 * Internally, the iterator stores handles to nodes.
7288 * Externally, it returns the targets,
7289 * such that the caller only sees nodes.
7290 */
7291 synctex_node_p synctex_iterator_next_result(synctex_iterator_p iterator) {
7292 if (iterator && iterator->count>0) {
7293 synctex_node_p N = iterator->next;
7294 if(!(iterator->next = _synctex_tree_child(N))) {
7295 iterator->next = iterator->top = __synctex_tree_sibling(iterator->top);
7296 }
7297 --iterator->count;
7298 return _synctex_tree_target(N);
7299 }
7300 return NULL;
7301 }
7302 int synctex_iterator_reset(synctex_iterator_p iterator) {
7303 if (iterator) {
7304 iterator->next = iterator->top = iterator->seed;
7305 return iterator->count = iterator->count0;
7306 }
7307 return 0;
7308 }
7309
7310 synctex_iterator_p synctex_iterator_new_edit(synctex_scanner_p scanner,int page,float h,float v){
7311 if (scanner) {
7312 synctex_node_p sheet = NULL;
7313 synctex_point_s hit;
7314 synctex_node_p node = NULL;
7315 synctex_nd_lr_s nds = {{NULL,0},{NULL,0}};
7316 if (NULL == (scanner = synctex_scanner_parse(scanner)) || 0 >= scanner->unit) {/* scanner->unit must be >0 */
7317 return NULL;
7318 }
7319 /* Find the proper sheet */
7320 sheet = synctex_sheet(scanner,page);
7321 if (NULL == sheet) {
7322 return NULL;
7323 }
7324 /* Now sheet points to the sheet node with proper page number. */
7325 /* Now that scanner has been initialized, we can convert
7326 * the given point to scanner integer coordinates */
7327 hit = (synctex_point_s)
7328 {(h-scanner->x_offset)/scanner->unit,
7329 (v-scanner->y_offset)/scanner->unit};
7330 /* At first, we browse all the horizontal boxes of the sheet
7331 * until we find one containing the hit point. */
7332 if ((node = _synctex_tree_next_hbox(sheet))) {
7333 do {
7334 if (_synctex_point_in_box_v2(&hit,node)) {
7335 /* Maybe the hit point belongs to a contained vertical box.
7336 * This is the most likely situation.
7337 */
7338 synctex_node_p next = node;
7339 #if defined(SYNCTEX_DEBUG)
7340 printf("--- We are lucky\n");
7341 #endif
7342 /* This trick is for catching overlapping boxes */
7343 while ((next = _synctex_tree_next_hbox(next))) {
7344 if (_synctex_point_in_box_v2(&hit,next)) {
7345 node = _synctex_smallest_container_v2(next,node);
7346 }
7347 }
7348 /* node is the smallest horizontal box that contains hit,
7349 * unless there is no hbox at all.
7350 */
7351 node = _synctex_eq_deepest_container_v2(&hit, node);
7352 nds = _synctex_eq_get_closest_children_in_box_v2(&hit, node);
7353 end:
7354 if (nds.r.node && nds.l.node) {
7355 if ((_synctex_data_tag(nds.r.node)!=_synctex_data_tag(nds.l.node))
7356 || (_synctex_data_line(nds.r.node)!=_synctex_data_line(nds.l.node))
7357 || (_synctex_data_column(nds.r.node)!=_synctex_data_column(nds.l.node))) {
7358 if (_synctex_data_line(nds.r.node)<_synctex_data_line(nds.l.node)) {
7359 node = nds.r.node;
7360 nds.r.node = nds.l.node;
7361 nds.l.node = node;
7362 } else if (_synctex_data_line(nds.r.node)==_synctex_data_line(nds.l.node)) {
7363 if (nds.l.distance>nds.r.distance) {
7364 node = nds.r.node;
7365 nds.r.node = nds.l.node;
7366 nds.l.node = node;
7367 }
7368 }
7369 if((node = _synctex_new_handle_with_target(nds.l.node))) {
7370 synctex_node_p other_handle;
7371 if((other_handle = _synctex_new_handle_with_target(nds.r.node))) {
7372 _synctex_tree_set_sibling(node,other_handle);
7373 return _synctex_iterator_new(node,2);
7374 }
7375 return _synctex_iterator_new(node,1);
7376 }
7377 return NULL;
7378 }
7379 /* both nodes have the same input coordinates
7380 * We choose the one closest to the hit point */
7381 if (nds.l.distance>nds.r.distance) {
7382 nds.l.node = nds.r.node;
7383 }
7384 nds.r.node = NULL;
7385 } else if (nds.r.node) {
7386 nds.l = nds.r;
7387 } else if (!nds.l.node) {
7388 nds.l.node = node;
7389 }
7390 if((node = _synctex_new_handle_with_target(nds.l.node))) {
7391 return _synctex_iterator_new(node,1);
7392 }
7393 return 0;
7394 }
7395 } while ((node = _synctex_tree_next_hbox(node)));
7396 /* All the horizontal boxes have been tested,
7397 * None of them contains the hit point.
7398 */
7399 }
7400 /* We are not lucky,
7401 * we test absolutely all the node
7402 * to find the closest... */
7403 if ((node = _synctex_tree_child(sheet))) {
7404 #if defined(SYNCTEX_DEBUG)
7405 printf("--- We are not lucky\n");
7406 #endif
7407 nds.l = __synctex_closest_deep_child_v2(&hit, node);
7408 #if defined(SYNCTEX_DEBUG)
7409 printf("Edit query best: %i\n", nds.l.distance);
7410 #endif
7411 goto end;
7412 }
7413 }
7414 return NULL;
7415 }
7416
7417 /**
7418 * Loop the candidate friendly list to find the ones with the proper
7419 * tag and line.
7420 * Returns a tree of results targeting the found candidates.
7421 * At the top level each sibling has its own page number.
7422 * All the results with the same page number are linked by child/parent entry.
7423 * - parameter candidate: a friendly list of candidates
7424 */
7425 static synctex_node_p _synctex_display_query_v2(synctex_node_p target, int tag, int line, synctex_bool_t exclude_box) {
7426 synctex_node_p first_handle = NULL;
7427 /* Search the first match */
7428 if (target == NULL) {
7429 return first_handle;
7430 }
7431 do {
7432 int page;
7433 if ((exclude_box
7434 && _synctex_node_is_box(target))
7435 || (tag != synctex_node_tag(target))
7436 || (line != synctex_node_line(target))) {
7437 continue;
7438 }
7439 /* We found a first match, create
7440 * a result handle targeting that candidate. */
7441 first_handle = _synctex_new_handle_with_target(target);
7442 if (first_handle == NULL) {
7443 return first_handle;
7444 }
7445 /* target is either a node,
7446 * or a proxy to some node, in which case,
7447 * the target's target belongs to a form,
7448 * not a sheet. */
7449 page = synctex_node_page(target);
7450 /* Now create all the other results */
7451 while ((target = _synctex_tree_friend(target))) {
7452 synctex_node_p result = NULL;
7453 if ((exclude_box
7454 && _synctex_node_is_box(target))
7455 || (tag != synctex_node_tag(target))
7456 || (line != synctex_node_line(target))) {
7457 continue;
7458 }
7459 /* Another match, same page number ? */
7460 result = _synctex_new_handle_with_target(target);
7461 if (NULL == result ) {
7462 return first_handle;
7463 }
7464 /* is it the same page number ? */
7465 if (synctex_node_page(target) == page) {
7466 __synctex_tree_set_child(result, first_handle);
7467 first_handle = result;
7468 } else {
7469 /* We have 2 page numbers involved */
7470 __synctex_tree_set_sibling(first_handle, result);
7471 while ((target = _synctex_tree_friend(target))) {
7472 synctex_node_p same_page_node;
7473 if ((exclude_box
7474 && _synctex_node_is_box(target))
7475 || (tag != synctex_node_tag(target))
7476 || (line != synctex_node_line(target))) {
7477 continue;
7478 }
7479 /* New match found, which page? */
7480 result = _synctex_new_handle_with_target(target);
7481 if (NULL == result) {
7482 return first_handle;
7483 }
7484 same_page_node = first_handle;
7485 page = synctex_node_page(target);
7486 /* Find a result with the same page number */;
7487 do {
7488 if (_synctex_node_target_page(same_page_node) == page) {
7489 /* Insert result between same_page_node and its child */
7490 _synctex_tree_set_child(result,_synctex_tree_set_child(same_page_node,result));
7491 } else if ((same_page_node = __synctex_tree_sibling(same_page_node))) {
7492 continue;
7493 } else {
7494 /* This is a new page number */
7495 __synctex_tree_set_sibling(result,first_handle);
7496 first_handle = result;
7497 }
7498 break;
7499 } while (synctex_YES);
7500 }
7501 return first_handle;
7502 }
7503 }
7504 } while ((target = _synctex_tree_friend(target)));
7505 return first_handle;
7506 }
7507 synctex_iterator_p synctex_iterator_new_display(synctex_scanner_p scanner,const char * name,int line,int column, int page_hint) {
7508 SYNCTEX_UNUSED(column)
7509 if (scanner) {
7510 int tag = synctex_scanner_get_tag(scanner,name);/* parse if necessary */
7511 int max_line = 0;
7512 int line_offset = 1;
7513 int try_count = 100;
7514 synctex_node_p node = NULL;
7515 synctex_node_p result = NULL;
7516 if (tag == 0) {
7517 printf("SyncTeX Warning: No tag for %s\n",name);
7518 return NULL;
7519 }
7520 node = synctex_scanner_input_with_tag(scanner, tag);
7521 max_line = _synctex_data_line(node);
7522 /* node = NULL; */
7523 if (line>max_line) {
7524 line = max_line;
7525 }
7526 while(try_count--) {
7527 if (line<=max_line) {
7528 /* This loop will only be performed once for advanced viewers */
7529 synctex_node_p friend = _synctex_scanner_friend(scanner,tag+line);
7530 if ((node = friend)) {
7531 result = _synctex_display_query_v2(node,tag,line,synctex_YES);
7532 if (!result) {
7533 /* We did not find any matching boundary, retry including boxes */
7534 node = friend;/* no need to test it again, already done */
7535 result = _synctex_display_query_v2(node,tag,line,synctex_NO);
7536 }
7537 /* Now reverse the order to have nodes in display order, and then keep just a few nodes.
7538 * Order first the best node. */
7539 /* The result is a tree. At the root level, all nodes
7540 * correspond to different page numbers.
7541 * Each node has a child which corresponds to the same
7542 * page number if relevant.
7543 * Then reorder the nodes to put first the one which fits best.
7544 * The idea is to count the number of nodes
7545 * with the same tag and line number in the parents
7546 * and choose the ones with the biggest count.
7547 */
7548 if (result) {
7549 /* navigate through siblings, then children */
7550 synctex_node_p next_sibling = __synctex_tree_reset_sibling(result);
7551 int best_match = abs(page_hint-_synctex_node_target_page(result));
7552 synctex_node_p sibling;
7553 int match;
7554 synctex_counted_node_s cn = _synctex_vertically_sorted_v2(result);
7555 int count = cn.count;
7556 result = cn.node;
7557 while((sibling = next_sibling)) {
7558 /* What is next? Do not miss that step! */
7559 next_sibling = __synctex_tree_reset_sibling(sibling);
7560 cn = _synctex_vertically_sorted_v2(sibling);
7561 count += cn.count;
7562 sibling = cn.node;
7563 match = abs(page_hint-_synctex_node_target_page(sibling));
7564 if (match<best_match) {
7565 /* Order this node first */
7566 __synctex_tree_set_sibling(sibling,result);
7567 result = sibling;
7568 best_match = match;
7569 } else /*if (match>=best_match)*/ {
7570 __synctex_tree_set_sibling(sibling,__synctex_tree_sibling(result));
7571 __synctex_tree_set_sibling(result,sibling);
7572 }
7573 }
7574 return _synctex_iterator_new(result,count);
7575 }
7576 }
7577 # if defined(__SYNCTEX_STRONG_DISPLAY_QUERY__)
7578 break;
7579 # else
7580 line += line_offset;
7581 line_offset=line_offset<0?-(line_offset-1):-(line_offset+1);
7582 if (line <= 0) {
7583 line += line_offset;
7584 line_offset=line_offset<0?-(line_offset-1):-(line_offset+1);
7585 }
7586 # endif
7587 }
7588 }
7589 }
7590 return NULL;
7591 }
7592 synctex_status_t synctex_display_query(synctex_scanner_p scanner,const char * name,int line,int column, int page_hint) {
7593 if (scanner) {
7594 synctex_iterator_free(scanner->iterator);
7595 scanner->iterator = synctex_iterator_new_display(scanner, name,line,column, page_hint);
7596 return synctex_iterator_count(scanner->iterator);
7597 }
7598 return SYNCTEX_STATUS_ERROR;
7599 }
7600 synctex_status_t synctex_edit_query(synctex_scanner_p scanner,int page,float h,float v) {
7601 if (scanner) {
7602 synctex_iterator_free(scanner->iterator);
7603 scanner->iterator = synctex_iterator_new_edit(scanner, page, h, v);
7604 return synctex_iterator_count(scanner->iterator);
7605 }
7606 return SYNCTEX_STATUS_ERROR;
7607 }
7608 /**
7609 * The next result of a query.
7610 */
7611 synctex_node_p synctex_scanner_next_result(synctex_scanner_p scanner) {
7612 return scanner? synctex_iterator_next_result(scanner->iterator): NULL;
7613 }
7614 synctex_status_t synctex_scanner_reset_result(synctex_scanner_p scanner) {
7615 return scanner? synctex_iterator_reset(scanner->iterator): SYNCTEX_STATUS_ERROR;
7616 }
7617
7618 synctex_node_p synctex_node_target(synctex_node_p node) {
7619 return _synctex_tree_target(node);
7620 }
7621
7622 # ifdef SYNCTEX_NOTHING
7623 # pragma mark -
7624 # pragma mark Geometric utilities
7625 # endif
7626
7627 /** Rougly speaking, this is:
7628 * node's h coordinate - hit point's h coordinate.
7629 * If node is to the right of the hit point, then this distance is positive,
7630 * if node is to the left of the hit point, this distance is negative.
7631 * If the argument is a pdf form reference, then the child is used and returned instead.
7632 * Last Revision: Mon Apr 24 07:05:27 UTC 2017
7633 */
7634 static synctex_nd_s _synctex_point_h_ordered_distance_v2
7635 (synctex_point_p hit, synctex_node_p node) {
7636 synctex_nd_s nd = {node,INT_MAX};
7637 if (node) {
7638 int min,med,max,width;
7639 switch(synctex_node_type(node)) {
7640 /* The distance between a point and a box is special.
7641 * It is not the euclidian distance, nor something similar.
7642 * We have to take into account the particular layout,
7643 * and the box hierarchy.
7644 * Given a box, there are 9 regions delimited by the lines of the edges of the box.
7645 * The origin being at the top left corner of the page,
7646 * we also give names to the vertices of the box.
7647 *
7648 * 1 | 2 | 3
7649 * ---A---B--->
7650 * 4 | 5 | 6
7651 * ---C---D--->
7652 * 7 | 8 | 9
7653 * v v
7654 */
7655 case synctex_node_type_vbox:
7656 case synctex_node_type_void_vbox:
7657 case synctex_node_type_void_hbox:
7658 /* getting the box bounds, taking into account negative width, height and depth. */
7659 width = _synctex_data_width(node);
7660 min = _synctex_data_h(node);
7661 max = min + (width>0?width:-width);
7662 /* We always have min <= max */
7663 if (hit->h<min) {
7664 nd.distance = min - hit->h; /* regions 1+4+7, result is > 0 */
7665 } else if (hit->h>max) {
7666 nd.distance = max - hit->h; /* regions 3+6+9, result is < 0 */
7667 } else {
7668 nd.distance = 0; /* regions 2+5+8, inside the box, except for vertical coordinates */
7669 }
7670 break;
7671 case synctex_node_type_proxy_vbox:
7672 /* getting the box bounds, taking into account negative width, height and depth. */
7673 width = synctex_node_width(node);
7674 min = synctex_node_h(node);
7675 max = min + (width>0?width:-width);
7676 /* We always have min <= max */
7677 if (hit->h<min) {
7678 nd.distance = min - hit->h; /* regions 1+4+7, result is > 0 */
7679 } else if (hit->h>max) {
7680 nd.distance = max - hit->h; /* regions 3+6+9, result is < 0 */
7681 } else {
7682 nd.distance = 0; /* regions 2+5+8, inside the box, except for vertical coordinates */
7683 }
7684 break;
7685 case synctex_node_type_hbox:
7686 case synctex_node_type_proxy_hbox:
7687 /* getting the box bounds, taking into account negative width, height and depth. */
7688 width = synctex_node_hbox_width(node);
7689 min = synctex_node_hbox_h(node);
7690 max = min + (width>0?width:-width);
7691 /* We always have min <= max */
7692 if (hit->h<min) {
7693 nd.distance = min - hit->h; /* regions 1+4+7, result is > 0 */
7694 } else if (hit->h>max) {
7695 nd.distance = max - hit->h; /* regions 3+6+9, result is < 0 */
7696 } else {
7697 nd.distance = 0; /* regions 2+5+8, inside the box, except for vertical coordinates */
7698 }
7699 break;
7700 case synctex_node_type_kern:
7701 /* IMPORTANT NOTICE: the location of the kern is recorded AFTER the move.
7702 * The distance to the kern is very special,
7703 * in general, there is no text material in the kern,
7704 * this is why we compute the offset relative to the closest edge of the kern.*/
7705 max = _synctex_data_width(node);
7706 if (max<0) {
7707 min = _synctex_data_h(node);
7708 max = min - max;
7709 } else {
7710 min = -max;
7711 max = _synctex_data_h(node);
7712 min += max;
7713 }
7714 med = (min+max)/2;
7715 /* positive kern: '.' means text, '>' means kern offset
7716 * .............
7717 * min>>>>med>>>>max
7718 * ...............
7719 * negative kern: '.' means text, '<' means kern offset
7720 * ............................
7721 * min<<<<med<<<<max
7722 * .................................
7723 * Actually, we do not take into account negative widths.
7724 * There is a problem for such situation when there is effectively overlapping text.
7725 * But this should be extremely rare. I guess that in that case, many different choices
7726 * could be made, one being in contradiction with the other.
7727 * It means that the best choice should be made according to the situation that occurs
7728 * most frequently.
7729 */
7730 if (hit->h<min) {
7731 nd.distance = min - hit->h + 1; /* penalty to ensure other nodes are chosen first in case of overlapping ones */
7732 } else if (hit->h>max) {
7733 nd.distance = max - hit->h - 1; /* same kind of penalty */
7734 } else if (hit->h>med) {
7735 /* do things like if the node had 0 width and was placed at the max edge + 1*/
7736 nd.distance = max - hit->h + 1; /* positive, the kern is to the right of the hit point */
7737 } else {
7738 nd.distance = min - hit->h - 1; /* negative, the kern is to the left of the hit point */
7739 }
7740 break;
7741 case synctex_node_type_rule:/* to do: special management */
7742 case synctex_node_type_glue:
7743 case synctex_node_type_math:
7744 case synctex_node_type_boundary:
7745 case synctex_node_type_box_bdry:
7746 nd.distance = _synctex_data_h(node) - hit->h;
7747 break;
7748 case synctex_node_type_ref:
7749 nd.node = synctex_node_child(node);
7750 nd = _synctex_point_h_ordered_distance_v2(hit,nd.node);
7751 break;
7752 case synctex_node_type_proxy:
7753 case synctex_node_type_proxy_last:
7754 {
7755 /* shift the hit point to be relative to the proxy origin,
7756 * then compute the distance to the target
7757 */
7758 synctex_point_s otherHit = *hit;
7759 otherHit.h -= _synctex_data_h(node);
7760 otherHit.v -= _synctex_data_v(node);
7761 nd.node = _synctex_tree_target(node);
7762 nd = _synctex_point_h_ordered_distance_v2(&otherHit,nd.node);
7763 nd.node = node;
7764 }
7765 default:
7766 break;
7767 }
7768 }
7769 return nd;
7770 }
7771 /** Rougly speaking, this is:
7772 * node's v coordinate - hit point's v coordinate.
7773 * If node is at the top of the hit point, then this distance is positive,
7774 * if node is at the bottom of the hit point, this distance is negative.
7775 */
7776 static synctex_nd_s _synctex_point_v_ordered_distance_v2
7777 (synctex_point_p hit, synctex_node_p node) {
7778 synctex_nd_s nd = {node, INT_MAX};
7779 int min,max,depth,height;
7780 switch(synctex_node_type(node)) {
7781 /* The distance between a point and a box is special.
7782 * It is not the euclidian distance, nor something similar.
7783 * We have to take into account the particular layout,
7784 * and the box hierarchy.
7785 * Given a box, there are 9 regions delimited by the lines of the edges of the box.
7786 * The origin being at the top left corner of the page,
7787 * we also give names to the vertices of the box.
7788 *
7789 * 1 | 2 | 3
7790 * ---A---B--->
7791 * 4 | 5 | 6
7792 * ---C---D--->
7793 * 7 | 8 | 9
7794 * v v
7795 */
7796 case synctex_node_type_vbox:
7797 case synctex_node_type_void_vbox:
7798 case synctex_node_type_void_hbox:
7799 /* getting the box bounds, taking into account negative width, height and depth. */
7800 min = synctex_node_v(node);
7801 max = min + _synctex_abs(_synctex_data_depth(node));
7802 min -= _synctex_abs(_synctex_data_height(node));
7803 /* We always have min <= max */
7804 if (hit->v<min) {
7805 nd.distance = min - hit->v; /* regions 1+2+3, result is > 0 */
7806 } else if (hit->v>max) {
7807 nd.distance = max - hit->v; /* regions 7+8+9, result is < 0 */
7808 } else {
7809 nd.distance = 0; /* regions 4.5.6, inside the box, except for horizontal coordinates */
7810 }
7811 break;
7812 case synctex_node_type_proxy_vbox:
7813 /* getting the box bounds, taking into account negative width, height and depth. */
7814 min = synctex_node_v(node);
7815 max = min + _synctex_abs(synctex_node_depth(node));
7816 min -= _synctex_abs(synctex_node_height(node));
7817 /* We always have min <= max */
7818 if (hit->v<min) {
7819 nd.distance = min - hit->v; /* regions 1+2+3, result is > 0 */
7820 } else if (hit->v>max) {
7821 nd.distance = max - hit->v; /* regions 7+8+9, result is < 0 */
7822 } else {
7823 nd.distance = 0; /* regions 4.5.6, inside the box, except for horizontal coordinates */
7824 }
7825 break;
7826 case synctex_node_type_hbox:
7827 case synctex_node_type_proxy_hbox:
7828 /* getting the box bounds, taking into account negative height and depth. */
7829 min = synctex_node_hbox_v(node);
7830 depth = synctex_node_hbox_depth(node);
7831 max = min + (depth>0?depth:-depth);
7832 height = synctex_node_hbox_height(node);
7833 min -= (height>0?height:-height);
7834 /* We always have min <= max */
7835 if (hit->v<min) {
7836 nd.distance = min - hit->v; /* regions 1+2+3, result is > 0 */
7837 } else if (hit->v>max) {
7838 nd.distance = max - hit->v; /* regions 7+8+9, result is < 0 */
7839 } else {
7840 nd.distance = 0; /* regions 4.5.6, inside the box, except for horizontal coordinates */
7841 }
7842 break;
7843 case synctex_node_type_rule:/* to do: special management */
7844 case synctex_node_type_kern:
7845 case synctex_node_type_glue:
7846 case synctex_node_type_math:
7847 min = _synctex_data_v(node);
7848 max = min + _synctex_abs(_synctex_data_depth(_synctex_tree_parent(node)));
7849 min -= _synctex_abs(_synctex_data_height(_synctex_tree_parent(node)));
7850 /* We always have min <= max */
7851 if (hit->v<min) {
7852 nd.distance = min - hit->v; /* regions 1+2+3, result is > 0 */
7853 } else if (hit->v>max) {
7854 nd.distance = max - hit->v; /* regions 7+8+9, result is < 0 */
7855 } else {
7856 nd.distance = 0; /* regions 4.5.6, inside the box, except for horizontal coordinates */
7857 }
7858 break;
7859 case synctex_node_type_ref:
7860 nd.node = synctex_node_child(node);
7861 nd = _synctex_point_v_ordered_distance_v2(hit,nd.node);
7862 break;
7863 case synctex_node_type_proxy:
7864 case synctex_node_type_proxy_last:
7865 {
7866 synctex_point_s otherHit = *hit;
7867 otherHit.h -= _synctex_data_h(node);
7868 otherHit.v -= _synctex_data_v(node);
7869 nd.node = _synctex_tree_target(node);
7870 nd = _synctex_point_v_ordered_distance_v2(&otherHit,nd.node);
7871 nd.node = node;
7872 }
7873 default: break;
7874 }
7875 return nd;
7876 }
7877 /**
7878 * The best is the one with the smallest area.
7879 * The area is width*height where width and height may be big.
7880 * So there is a real risk of overflow if we stick with ints.
7881 */
7882 SYNCTEX_INLINE static synctex_node_p _synctex_smallest_container_v2(synctex_node_p node, synctex_node_p other_node) {
7883 long total_height, other_total_height;
7884 unsigned long area, other_area;
7885 long width = synctex_node_hbox_width(node);
7886 long other_width = synctex_node_hbox_width(other_node);
7887 if (width<0) {
7888 width = -width;
7889 }
7890 if (other_width<0) {
7891 other_width = -other_width;
7892 }
7893 total_height = _synctex_abs(synctex_node_hbox_depth(node)) + _synctex_abs(synctex_node_hbox_height(node));
7894 other_total_height = _synctex_abs(synctex_node_hbox_depth(other_node)) + _synctex_abs(synctex_node_hbox_height(other_node));
7895 area = total_height*width;
7896 other_area = other_total_height*other_width;
7897 if (area<other_area) {
7898 return node;
7899 }
7900 if (area>other_area) {
7901 return other_node;
7902 }
7903 if (_synctex_abs(_synctex_data_width(node))>_synctex_abs(_synctex_data_width(other_node))) {
7904 return node;
7905 }
7906 if (_synctex_abs(_synctex_data_width(node))<_synctex_abs(_synctex_data_width(other_node))) {
7907 return other_node;
7908 }
7909 if (total_height<other_total_height) {
7910 return node;
7911 }
7912 if (total_height>other_total_height) {
7913 return other_node;
7914 }
7915 return node;
7916 }
7917
7918 SYNCTEX_INLINE static synctex_bool_t _synctex_point_in_box_v2(synctex_point_p hit, synctex_node_p node) {
7919 if (node) {
7920 if (0 == _synctex_point_h_ordered_distance_v2(hit,node).distance
7921 && 0 == _synctex_point_v_ordered_distance_v2(hit,node).distance) {
7922 return synctex_YES;
7923 }
7924 }
7925 return synctex_NO;
7926 }
7927
7928 static int _synctex_distance_to_box_v2(synctex_point_p hit,synctex_box_p box) {
7929 /* The distance between a point and a box is special.
7930 * It is not the euclidian distance, nor something similar.
7931 * We have to take into account the particular layout,
7932 * and the box hierarchy.
7933 * Given a box, there are 9 regions delimited by the lines of the edges of the box.
7934 * The origin being at the top left corner of the page,
7935 * we also give names to the vertices of the box.
7936 *
7937 * 1 | 2 | 3
7938 * ---A---B--->
7939 * 4 | 5 | 6
7940 * ---C---D--->
7941 * 7 | 8 | 9
7942 * v v
7943 * In each region, there is a different formula.
7944 * In the end we have a continuous distance which may not be a mathematical distance but who cares. */
7945 if (hit->v<box->min.v) {
7946 /* Regions 1, 2 or 3 */
7947 if (hit->h<box->min.h) {
7948 /* This is region 1. The distance to the box is the L1 distance PA. */
7949 return box->min.v - hit->v + box->min.h - hit->h;/* Integer overflow? probability epsilon */
7950 } else if (hit->h<=box->max.h) {
7951 /* This is region 2. The distance to the box is the geometrical distance to the top edge. */
7952 return box->min.v - hit->v;
7953 } else {
7954 /* This is region 3. The distance to the box is the L1 distance PB. */
7955 return box->min.v - hit->v + hit->h - box->max.h;
7956 }
7957 } else if (hit->v<=box->max.v) {
7958 /* Regions 4, 5 or 6 */
7959 if (hit->h<box->min.h) {
7960 /* This is region 4. The distance to the box is the geometrical distance to the left edge. */
7961 return box->min.h - hit->h;
7962 } else if (hit->h<=box->max.h) {
7963 /* This is region 5. We are inside the box. */
7964 return 0;
7965 } else {
7966 /* This is region 6. The distance to the box is the geometrical distance to the right edge. */
7967 return hit->h - box->max.h;
7968 }
7969 } else {
7970 /* Regions 7, 8 or 9 */
7971 if (hit->h<box->min.h) {
7972 /* This is region 7. The distance to the box is the L1 distance PC. */
7973 return hit->v - box->max.v + box->min.h - hit->h;
7974 } else if (hit->h<=box->max.h) {
7975 /* This is region 8. The distance to the box is the geometrical distance to the top edge. */
7976 return hit->v - box->max.v;
7977 } else {
7978 /* This is region 9. The distance to the box is the L1 distance PD. */
7979 return hit->v - box->max.v + hit->h - box->max.h;
7980 }
7981 }
7982 }
7983
7984 /**
7985 * The distance from the hit point to the node.
7986 */
7987 static int _synctex_point_node_distance_v2(synctex_point_p hit, synctex_node_p node) {
7988 int d = INT_MAX;
7989 if (node) {
7990 synctex_box_s box = {{0,0},{0,0}};
7991 int dd = INT_MAX;
7992 switch(synctex_node_type(node)) {
7993 case synctex_node_type_vbox:
7994 box.min.h = _synctex_data_h(node);
7995 box.max.h = box.min.h + _synctex_abs(_synctex_data_width(node));
7996 box.min.v = synctex_node_v(node);
7997 box.max.v = box.min.v + _synctex_abs(_synctex_data_depth(node));
7998 box.min.v -= _synctex_abs(_synctex_data_height(node));
7999 return _synctex_distance_to_box_v2(hit,&box);
8000 case synctex_node_type_proxy_vbox:
8001 box.min.h = synctex_node_h(node);
8002 box.max.h = box.min.h + _synctex_abs(synctex_node_width(node));
8003 box.min.v = synctex_node_v(node);
8004 box.max.v = box.min.v + _synctex_abs(synctex_node_depth(node));
8005 box.min.v -= _synctex_abs(synctex_node_height(node));
8006 return _synctex_distance_to_box_v2(hit,&box);
8007 case synctex_node_type_hbox:
8008 case synctex_node_type_proxy_hbox:
8009 box.min.h = synctex_node_hbox_h(node);
8010 box.max.h = box.min.h + _synctex_abs(synctex_node_hbox_width(node));
8011 box.min.v = synctex_node_hbox_v(node);
8012 box.max.v = box.min.v + _synctex_abs(synctex_node_hbox_depth(node));
8013 box.min.v -= _synctex_abs(synctex_node_hbox_height(node));
8014 return _synctex_distance_to_box_v2(hit,&box);
8015 case synctex_node_type_void_vbox:
8016 case synctex_node_type_void_hbox:
8017 /* best of distances from the left edge and right edge*/
8018 box.min.h = _synctex_data_h(node);
8019 box.max.h = box.min.h;
8020 box.min.v = _synctex_data_v(node);
8021 box.max.v = box.min.v + _synctex_abs(_synctex_data_depth(node));
8022 box.min.v -= _synctex_abs(_synctex_data_height(node));
8023 d = _synctex_distance_to_box_v2(hit,&box);
8024 box.min.h = box.min.h + _synctex_abs(_synctex_data_width(node));
8025 box.max.h = box.min.h;
8026 dd = _synctex_distance_to_box_v2(hit,&box);
8027 return d<dd ? d:dd;
8028 case synctex_node_type_kern:
8029 box.min.h = _synctex_data_h(node);
8030 box.max.h = box.min.h;
8031 box.max.v = _synctex_data_v(node);
8032 box.min.v = box.max.v - _synctex_abs(_synctex_data_height(_synctex_tree_parent(node)));
8033 d = _synctex_distance_to_box_v2(hit,&box);
8034 box.min.h -= _synctex_data_width(node);
8035 box.max.h = box.min.h;
8036 dd = _synctex_distance_to_box_v2(hit,&box);
8037 return d<dd ? d:dd;
8038 case synctex_node_type_glue:
8039 case synctex_node_type_math:
8040 case synctex_node_type_boundary:
8041 case synctex_node_type_box_bdry:
8042 box.min.h = _synctex_data_h(node);
8043 box.max.h = box.min.h;
8044 box.max.v = _synctex_data_v(node);
8045 box.min.v = box.max.v - _synctex_abs(_synctex_data_height(_synctex_tree_parent(node)));
8046 return _synctex_distance_to_box_v2(hit,&box);
8047 case synctex_node_type_proxy:
8048 case synctex_node_type_proxy_last:
8049 {
8050 synctex_point_s otherHit = *hit;
8051 otherHit.h -= _synctex_data_h(node);
8052 otherHit.v -= _synctex_data_v(node);
8053 return _synctex_point_node_distance_v2(&otherHit, _synctex_tree_target(node));
8054 }
8055 default: break;
8056 }
8057 }
8058 return d;
8059 }
8060 static synctex_node_p _synctex_eq_deepest_container_v2(synctex_point_p hit, synctex_node_p node) {
8061 if (node) {
8062 /**/
8063 synctex_node_p child;
8064 if ((child = synctex_node_child(node))) {
8065 /* Non void hbox or vbox, form ref or proxy */
8066 /* We go deep first because some boxes have 0 dimensions
8067 * despite they do contain some black material.
8068 */
8069 do {
8070 if ((_synctex_point_in_box_v2(hit,child))) {
8071 synctex_node_p deep = _synctex_eq_deepest_container_v2(hit,child);
8072 if (deep) {
8073 /* One of the children contains the hit. */
8074 return deep;
8075 }
8076 }
8077 } while((child = synctex_node_sibling(child)));
8078 /* is the hit point inside the box? */
8079 if (synctex_node_type(node) == synctex_node_type_vbox
8080 || synctex_node_type(node) == synctex_node_type_proxy_vbox) {
8081 /* For vboxes we try to use some node inside.
8082 * Walk through the list of siblings until we find the closest one.
8083 * Only consider siblings with children inside. */
8084 if ((child = _synctex_tree_child(node))) {
8085 synctex_nd_s best = SYNCTEX_ND_0;
8086 do {
8087 if (_synctex_tree_child(child)) {
8088 int d = _synctex_point_node_distance_v2(hit,child);
8089 if (d <= best.distance) {
8090 best = (synctex_nd_s){child, d};
8091 }
8092 }
8093 } while((child = __synctex_tree_sibling(child)));
8094 if (best.node) {
8095 return best.node;
8096 }
8097 }
8098 }
8099 if (_synctex_point_in_box_v2(hit,node)) {
8100 return node;
8101 }
8102 }
8103 }
8104 return NULL;
8105 }
8106 static synctex_nd_s _synctex_eq_deepest_container_v3(synctex_point_p hit, synctex_node_p node) {
8107 if (node) {
8108 synctex_node_p child = NULL;
8109 if ((child = synctex_node_child(node))) {
8110 /* Non void hbox, vbox, box proxy or form ref */
8111 /* We go deep first because some boxes have 0 dimensions
8112 * despite they do contain some black material.
8113 */
8114 do {
8115 synctex_nd_s deep = _synctex_eq_deepest_container_v3(hit, child);
8116 if (deep.node) {
8117 /* One of the children contains the hit-> */
8118 return deep;
8119 }
8120 } while((child = synctex_node_sibling(child)));
8121 /* For vboxes we try to use some node inside.
8122 * Walk through the list of siblings until we find the closest one.
8123 * Only consider siblings with children inside. */
8124 if (synctex_node_type(node) == synctex_node_type_vbox
8125 || synctex_node_type(node) == synctex_node_type_proxy_vbox) {
8126 if ((child = synctex_node_child(node))) {
8127 synctex_nd_s best = SYNCTEX_ND_0;
8128 do {
8129 if (synctex_node_child(child)) {
8130 int d = _synctex_point_node_distance_v2(hit,child);
8131 if (d < best.distance) {
8132 best = (synctex_nd_s){child,d};
8133 }
8134 }
8135 } while((child = synctex_node_sibling(child)));
8136 if (best.node) {
8137 return best;
8138 }
8139 }
8140 }
8141 /* is the hit point inside the box? */
8142 if (_synctex_point_in_box_v2(hit,node)) {
8143 return (synctex_nd_s){node, 0};
8144 }
8145 }
8146 }
8147 return SYNCTEX_ND_0;
8148 }
8149
8150 /* Compares the locations of the hit point with the locations of
8151 * the various nodes contained in the box.
8152 * As it is an horizontal box, we only compare horizontal coordinates.
8153 */
8154 SYNCTEX_INLINE static synctex_nd_lr_s __synctex_eq_get_closest_children_in_hbox_v2(synctex_point_p hitP, synctex_node_p node) {
8155 synctex_nd_s childd = SYNCTEX_ND_0;
8156 synctex_nd_lr_s nds = {SYNCTEX_ND_0,SYNCTEX_ND_0};
8157 if ((childd.node = synctex_node_child(node))) {
8158 synctex_nd_s nd = SYNCTEX_ND_0;
8159 do {
8160 childd = _synctex_point_h_ordered_distance_v2(hitP,childd.node);
8161 if (childd.distance > 0) {
8162 /* node is to the right of the hit point.
8163 * We compare node and the previously recorded one, through the recorded distance.
8164 * If the nodes have the same tag, prefer the one with the smallest line number,
8165 * if the nodes also have the same line number, prefer the one with the smallest column. */
8166 if (nds.r.distance > childd.distance) {
8167 nds.r = childd;
8168 } else if (nds.r.distance == childd.distance && nds.r.node) {
8169 if (_synctex_data_tag(nds.r.node) == _synctex_data_tag(childd.node)
8170 && (_synctex_data_line(nds.r.node) > _synctex_data_line(childd.node)
8171 || (_synctex_data_line(nds.r.node) == _synctex_data_line(childd.node)
8172 && _synctex_data_column(nds.r.node) > _synctex_data_column(childd.node)))) {
8173 nds.r = childd;
8174 }
8175 }
8176 } else if (childd.distance == 0) {
8177 /* hit point is inside node. */
8178 if (_synctex_tree_child(childd.node)) {
8179 return _synctex_eq_get_closest_children_in_box_v2(hitP, childd.node);
8180 }
8181 nds.l = childd;
8182 } else { /* here childd.distance < 0, the hit point is to the right of node */
8183 childd.distance = -childd.distance;
8184 if (nds.l.distance > childd.distance) {
8185 nds.l = childd;
8186 } else if (nds.l.distance == childd.distance && nds.l.node) {
8187 if (_synctex_data_tag(nds.l.node) == _synctex_data_tag(childd.node)
8188 && (_synctex_data_line(nds.l.node) > _synctex_data_line(childd.node)
8189 || (_synctex_data_line(nds.l.node) == _synctex_data_line(childd.node)
8190 && _synctex_data_column(nds.l.node) > _synctex_data_column(childd.node)))) {
8191 nds.l = childd;
8192 }
8193 }
8194 }
8195 } while((childd.node = synctex_node_sibling(childd.node)));
8196 if (nds.l.node) {
8197 /* the left node is new, try to narrow the result */
8198 if ((nd = _synctex_eq_deepest_container_v3(hitP,nds.l.node)).node) {
8199 nds.l = nd;
8200 }
8201 if((nd = __synctex_closest_deep_child_v2(hitP,nds.l.node)).node) {
8202 nds.l.node = nd.node;
8203 }
8204 }
8205 if (nds.r.node) {
8206 /* the right node is new, try to narrow the result */
8207 if ((nd = _synctex_eq_deepest_container_v3(hitP,nds.r.node)).node) {
8208 nds.r = nd;
8209 }
8210 if((nd = __synctex_closest_deep_child_v2(hitP,nds.r.node)).node) {
8211 nds.r.node = nd.node;
8212 }
8213 }
8214 }
8215 return nds;
8216 }
8217
8218 #if 0
8219 SYNCTEX_INLINE static synctex_nd_lr_s __synctex_eq_get_closest_children_in_hbox_v3(synctex_point_p hitP, synctex_node_p nodeP) {
8220 synctex_nd_s nd = SYNCTEX_ND_0;
8221 synctex_nd_lr_s nds = {SYNCTEX_ND_0,SYNCTEX_ND_0};
8222 if ((nd.node = _synctex_tree_child(nodeP))) {
8223 do {
8224 nd = _synctex_point_h_ordered_distance_v2(hitP,nd.node);
8225 if (nd.distance > 0) {
8226 /* node is to the right of the hit point.
8227 * We compare node and the previously recorded one, through the recorded distance.
8228 * If the nodes have the same tag, prefer the one with the smallest line number,
8229 * if the nodes also have the same line number, prefer the one with the smallest column. */
8230 if (nds.r.distance > nd.distance) {
8231 nds.r = nd;
8232 } else if (nds.r.distance == nd.distance && nds.r.node) {
8233 if (_synctex_data_tag(nds.r.node) == _synctex_data_tag(nd.node)
8234 && (_synctex_data_line(nds.r.node) > _synctex_data_line(nd.node)
8235 || (_synctex_data_line(nds.r.node) == _synctex_data_line(nd.node)
8236 && _synctex_data_column(nds.r.node) > _synctex_data_column(nd.node)))) {
8237 nds.r = nd;
8238 }
8239 }
8240 } else if (nd.distance == 0) {
8241 /* hit point is inside node. */
8242 nds.l = nd;
8243 } else { /* here nd.d < 0, the hit point is to the right of node */
8244 nd.distance = -nd.distance;
8245 if (nds.l.distance > nd.distance) {
8246 nds.l = nd;
8247 } else if (nds.l.distance == nd.distance && nds.l.node) {
8248 if (_synctex_data_tag(nds.l.node) == _synctex_data_tag(nd.node)
8249 && (_synctex_data_line(nds.l.node) > _synctex_data_line(nd.node)
8250 || (_synctex_data_line(nds.l.node) == _synctex_data_line(nd.node)
8251 && _synctex_data_column(nds.l.node) > _synctex_data_column(nd.node)))) {
8252 nds.l = nd;
8253 }
8254 }
8255 }
8256 } while((nd.node = __synctex_tree_sibling(nd.node)));
8257 if (nds.l.node) {
8258 /* the left node is new, try to narrow the result */
8259 if ((nd.node = _synctex_eq_deepest_container_v2(hitP,nds.l.node))) {
8260 nds.l.node = nd.node;
8261 }
8262 if((nd = _synctex_eq_closest_child_v2(hitP,nds.l.node)).node) {
8263 nds.l.node = nd.node;
8264 }
8265 }
8266 if (nds.r.node) {
8267 /* the right node is new, try to narrow the result */
8268 if ((nd.node = _synctex_eq_deepest_container_v2(hitP,nds.r.node))) {
8269 nds.r.node = nd.node;
8270 }
8271 if((nd = _synctex_eq_closest_child_v2(hitP,nds.r.node)).node) {
8272 nds.r.node = nd.node;
8273 }
8274 }
8275 }
8276 return nds;
8277 }
8278 #endif
8279 SYNCTEX_INLINE static synctex_nd_lr_s __synctex_eq_get_closest_children_in_vbox_v2(synctex_point_p hitP, synctex_node_p nodeP) {
8280 SYNCTEX_UNUSED(nodeP)
8281 synctex_nd_lr_s nds = {SYNCTEX_ND_0,SYNCTEX_ND_0};
8282 synctex_nd_s nd = SYNCTEX_ND_0;
8283 if ((nd.node = synctex_node_child(nd.node))) {
8284 do {
8285 nd = _synctex_point_v_ordered_distance_v2(hitP,nd.node);
8286 /* this is what makes the difference with the h version above */
8287 if (nd.distance > 0) {
8288 /* node is to the top of the hit point (below because TeX is oriented from top to bottom.
8289 * We compare node and the previously recorded one, through the recorded distance.
8290 * If the nodes have the same tag, prefer the one with the smallest line number,
8291 * if the nodes also have the same line number, prefer the one with the smallest column. */
8292 if (nds.r.distance > nd.distance) {
8293 nds.r = nd;
8294 } else if (nds.r.distance == nd.distance && nds.r.node) {
8295 if (_synctex_data_tag(nds.r.node) == _synctex_data_tag(nd.node)
8296 && (_synctex_data_line(nds.r.node) > _synctex_data_line(nd.node)
8297 || (_synctex_data_line(nds.r.node) == _synctex_data_line(nd.node)
8298 && _synctex_data_column(nds.r.node) > _synctex_data_column(nd.node)))) {
8299 nds.r = nd;
8300 }
8301 }
8302 } else if (nd.distance == 0) {
8303 nds.l = nd;
8304 } else { /* here nd < 0 */
8305 nd.distance = -nd.distance;
8306 if (nds.l.distance > nd.distance) {
8307 nds.l = nd;
8308 } else if (nds.l.distance == nd.distance && nds.l.node) {
8309 if (_synctex_data_tag(nds.l.node) == _synctex_data_tag(nd.node)
8310 && (_synctex_data_line(nds.l.node) > _synctex_data_line(nd.node)
8311 || (_synctex_data_line(nds.l.node) == _synctex_data_line(nd.node)
8312 && _synctex_data_column(nds.l.node) > _synctex_data_column(nd.node)))) {
8313 nds.l = nd;
8314 }
8315 }
8316 }
8317 } while((nd.node = synctex_node_sibling(nd.node)));
8318 if (nds.l.node) {
8319 if ((nd.node = _synctex_eq_deepest_container_v2(hitP,nds.l.node))) {
8320 nds.l.node = nd.node;
8321 }
8322 if((nd = _synctex_eq_closest_child_v2(hitP,nds.l.node)).node) {
8323 nds.l.node = nd.node;
8324 }
8325 }
8326 if (nds.r.node) {
8327 if ((nd.node = _synctex_eq_deepest_container_v2(hitP,nds.r.node))) {
8328 nds.r.node = nd.node;
8329 }
8330 if((nd = _synctex_eq_closest_child_v2(hitP,nds.r.node)).node) {
8331 nds.r.node = nd.node;
8332 }
8333 }
8334 }
8335 return nds;
8336 }
8337
8338 /**
8339 * Get the child closest to the hit point.
8340 * - parameter: hit point
8341 * - parameter: containing node
8342 * - returns: the child and the distance to the hit point.
8343 * SYNCTEX_ND_0 if the parameter node has no children.
8344 * - note: recursive call.
8345 */
8346 static synctex_nd_s __synctex_closest_deep_child_v2(synctex_point_p hitP, synctex_node_p node) {
8347 synctex_nd_s best = SYNCTEX_ND_0;
8348 synctex_node_p child = NULL;
8349 if ((child = synctex_node_child(node))) {
8350 #if defined(SYNCTEX_DEBUG)
8351 printf("Closest deep child on box at line %i\n",
8352 SYNCTEX_LINEINDEX(node));
8353 #endif
8354 do {
8355 #if SYNCTEX_DEBUG>500
8356 synctex_node_display(child);
8357 #endif
8358 synctex_nd_s nd = SYNCTEX_ND_0;
8359 if (_synctex_node_is_box(child)) {
8360 nd = __synctex_closest_deep_child_v2(hitP,child);
8361 } else {
8362 nd = (synctex_nd_s) {child, _synctex_point_node_distance_v2(hitP,child)};
8363 }
8364 if (nd.distance < best.distance ||(nd.distance == best.distance
8365 && synctex_node_type(nd.node) != synctex_node_type_kern)) {
8366 #if defined(SYNCTEX_DEBUG)
8367 if(nd.node) {
8368 printf("New best %i<=%i line %i\n",nd.distance,
8369 best.distance,SYNCTEX_LINEINDEX(nd.node));
8370 }
8371 #endif
8372 best = nd;
8373 }
8374 } while((child = synctex_node_sibling(child)));
8375 #if defined(SYNCTEX_DEBUG)
8376 if(best.node) {
8377 printf("Found new best %i line %i\n",best.distance,SYNCTEX_LINEINDEX(best.node));
8378 }
8379 #endif
8380 }
8381 return best;
8382 }
8383
8384 /**
8385 * Return the closest child.
8386 * - parameter: a pointer to the hit point,
8387 * - parameter: the container
8388 * - return: SYNCTEX_ND_0 if node has no child,
8389 * the __synctex_closest_deep_child_v2 otherwise.
8390 */
8391 static synctex_nd_s _synctex_eq_closest_child_v2(synctex_point_p hitP, synctex_node_p node) {
8392 synctex_nd_s nd = SYNCTEX_ND_0;
8393 if (_synctex_node_is_box(node)) {
8394 nd = __synctex_closest_deep_child_v2(hitP, node);
8395 if (_synctex_node_is_box(nd.node)) {
8396 synctex_node_p child = NULL;
8397 if ((child = synctex_node_child(nd.node))) {
8398 synctex_nd_s best = {child,_synctex_point_node_distance_v2(hitP,child)};
8399 while((child = synctex_node_sibling(child))) {
8400 int d = _synctex_point_node_distance_v2(hitP,child);
8401 if (d < best.distance) {
8402 best = (synctex_nd_s){child,d};
8403 } else if (d == best.distance && synctex_node_type(child) != synctex_node_type_kern) {
8404 best.node = child;
8405 }
8406 }
8407 return best;
8408 }
8409 }
8410 return nd;
8411 }
8412 return SYNCTEX_ND_0;
8413 }
8414 SYNCTEX_INLINE static synctex_nd_lr_s _synctex_eq_get_closest_children_in_box_v2(synctex_point_p hitP, synctex_node_p node) {
8415 synctex_nd_lr_s nds = {SYNCTEX_ND_0,SYNCTEX_ND_0};
8416 if(_synctex_tree_has_child(node)) { /* node != NULL */
8417 if (node->class_->type==synctex_node_type_hbox ||
8418 node->class_->type==synctex_node_type_proxy_hbox) {
8419 return __synctex_eq_get_closest_children_in_hbox_v2(hitP,node);
8420 } else {
8421 return __synctex_eq_get_closest_children_in_vbox_v2(hitP,node);
8422 }
8423 }
8424 return nds;
8425 }
8426
8427 #ifndef SYNCTEX_NO_UPDATER
8428
8429 # ifdef SYNCTEX_NOTHING
8430 # pragma mark -
8431 # pragma mark Updater
8432 # endif
8433
8434 typedef int (*synctex_print_f)(synctex_updater_p, const char * , ...); /* print formatted to either FILE * or gzFile */
8435 typedef void (*synctex_close_f)(synctex_updater_p); /* close FILE * or gzFile */
8436
8437 # define SYNCTEX_BITS_PER_BYTE 8
8438
8439 typedef union {
8440 gzFile as_gzFile;
8441 FILE * as_FILE_p;
8442 void * as_ptr;
8443 } synctex_file_u;
8444
8445 struct synctex_updater_t {
8446 synctex_file_u file;
8447 synctex_print_f print;
8448 synctex_close_f close;
8449 int length; /* the number of chars appended */
8450 };
8451
8452 SYNCTEX_ATTRIBUTE_FORMAT_PRINTF(2, 3)
8453 static int _synctex_updater_print(synctex_updater_p updater, const char * format, ...) {
8454 int result = 0;
8455 if (updater) {
8456 va_list va;
8457 va_start(va, format);
8458 result = vfprintf(updater->file.as_FILE_p,
8459 format,
8460 va);
8461 va_end(va);
8462 }
8463 return result;
8464 }
8465 #if defined(_MSC_VER)
8466 #include <stdio.h>
8467 #include <stdlib.h>
8468 #include <stdarg.h>
8469
8470 static int vasprintf(char **ret,
8471 const char *format,
8472 va_list ap)
8473 {
8474 int len;
8475 len = _vsnprintf(NULL, 0, format, ap);
8476 if (len < 0) return -1;
8477 *ret = malloc(len + 1);
8478 if (!*ret) return -1;
8479 _vsnprintf(*ret, len+1, format, ap);
8480 (*ret)[len] = '\0';
8481 return len;
8482 }
8483
8484 #endif
8485
8486 /**
8487 * gzvprintf is not available until OSX 10.10
8488 */
8489 SYNCTEX_ATTRIBUTE_FORMAT_PRINTF(2, 3)
8490 static int _synctex_updater_print_gz(synctex_updater_p updater, const char * format, ...) {
8491 int result = 0;
8492 if (updater) {
8493 char * buffer;
8494 va_list va;
8495 va_start(va, format);
8496 if (vasprintf(&buffer, format, va) < 0) {
8497 _synctex_error("Out of memory...");
8498 } else if ((result = (int)strlen(buffer))) {
8499 result = gzwrite(updater->file.as_gzFile, buffer, (unsigned)result);
8500 }
8501 va_end(va);
8502 free(buffer);
8503 }
8504 return result;
8505 }
8506
8507 static void _synctex_updater_close(synctex_updater_p updater) {
8508 if (updater) {
8509 fclose(updater->file.as_FILE_p);
8510 }
8511 }
8512
8513 static void _synctex_updater_close_gz(synctex_updater_p updater) {
8514 if (updater) {
8515 gzclose(updater->file.as_gzFile);
8516 }
8517 }
8518
8519 synctex_updater_p synctex_updater_new_with_output_file(const char * output, const char * build_directory) {
8520 synctex_updater_p updater = NULL;
8521 const char * mode = NULL;
8522 synctex_open_s open;
8523 /* prepare the updater, the memory is the only one dynamically allocated */
8524 updater = (synctex_updater_p)_synctex_malloc(sizeof(synctex_updater_s));
8525 if (NULL == updater) {
8526 _synctex_error("! synctex_updater_new_with_file: malloc problem");
8527 return NULL;
8528 }
8529 open = _synctex_open_v2(output,build_directory,0,synctex_ADD_QUOTES);
8530 if (open.status < SYNCTEX_STATUS_OK) {
8531 open = _synctex_open_v2(output,build_directory,0,synctex_DONT_ADD_QUOTES);
8532 if (open.status < SYNCTEX_STATUS_OK) {
8533 return_on_error:
8534 _synctex_free(updater);
8535 return updater = NULL;
8536 }
8537 }
8538 /* OK, the file exists, we close it and reopen it with the correct mode.
8539 * The receiver is now the owner of the "synctex" variable. */
8540 gzclose(open.file);
8541 updater->file.as_ptr = NULL;
8542 mode = _synctex_get_io_mode_name(open.io_mode|synctex_io_append_mask);/* either "a" or "ab", depending on the file extension */
8543 if (open.io_mode&synctex_io_gz_mask) {
8544 if (NULL == (updater->file.as_FILE_p = fopen(open.synctex,mode))) {
8545 no_write_error:
8546 _synctex_error("! synctex_updater_new_with_file: Can't append to %s",open.synctex);
8547 free(open.synctex);
8548 goto return_on_error;
8549 }
8550 updater->print = &_synctex_updater_print;
8551 updater->close = &_synctex_updater_close;
8552 } else {
8553 if (NULL == (updater->file.as_gzFile = gzopen(open.synctex,mode))) {
8554 goto no_write_error;
8555 }
8556 updater->print = &_synctex_updater_print_gz;
8557 updater->close = &_synctex_updater_close_gz;
8558 }
8559 printf("SyncTeX: updating %s...",open.synctex);
8560 _synctex_free(open.synctex);
8561 return updater;
8562 }
8563
8564 void synctex_updater_append_magnification(synctex_updater_p updater, char * magnification){
8565 if (NULL==updater) {
8566 return;
8567 }
8568 if (magnification && strlen(magnification)) {
8569 updater->length +=
8570 updater->print(updater,"Magnification:%s\n",magnification);
8571 }
8572 }
8573
8574 void synctex_updater_append_x_offset(synctex_updater_p updater, char * x_offset){
8575 if (NULL==updater) {
8576 return;
8577 }
8578 if (x_offset && strlen(x_offset)) {
8579 updater->length += updater->print(updater,"X Offset:%s\n",x_offset);
8580 }
8581 }
8582
8583 void synctex_updater_append_y_offset(synctex_updater_p updater, char * y_offset){
8584 if (NULL==updater) {
8585 return;
8586 }
8587 if (y_offset && strlen(y_offset)) {
8588 updater->length += updater->print(updater,"Y Offset:%s\n",y_offset);
8589 }
8590 }
8591
8592 void synctex_updater_free(synctex_updater_p updater){
8593 if (NULL==updater) {
8594 return;
8595 }
8596 if (updater->length>0) {
8597 updater->print(updater,"!%i\n",updater->length);
8598 }
8599 updater->close(updater);
8600 _synctex_free(updater);
8601 printf("... done.\n");
8602 return;
8603 }
8604 #endif
8605
8606 #if defined(SYNCTEX_TESTING)
8607 # ifdef SYNCTEX_NOTHING
8608 # pragma mark -
8609 # pragma mark Testers
8610 # endif
8611 /**
8612 * The next nodes corresponds to a deep first tree traversal.
8613 * Does not create child proxies as side effect contrary to
8614 * the synctex_node_next method above.
8615 * May loop infinitely many times if the tree
8616 * is not properly built (contains loops).
8617 */
8618 static synctex_node_p _synctex_node_next(synctex_node_p node) {
8619 synctex_node_p N = _synctex_tree_child(node);
8620 if (N) {
8621 return N;
8622 }
8623 return _synctex_node_sibling_or_parents(node);
8624 }
8625 static int _synctex_input_copy_name(synctex_node_p input, char * name) {
8626 char * copy = _synctex_malloc(strlen(name)+1);
8627 memcpy(copy,name,strlen(name)+1);
8628 _synctex_data_set_name(input,copy);
8629 return 0;
8630 }
8631 int synctex_test_setup_scanner_sheets_421(synctex_scanner_p scanner) {
8632 int TC = 0;
8633 synctex_node_p sheet = synctex_node_new(scanner,synctex_node_type_sheet);
8634 _synctex_data_set_page(sheet,4);
8635 SYNCTEX_TEST_BODY(TC, _synctex_data_page(sheet)==4,"");
8636 synctex_node_free(scanner->sheet);
8637 scanner->sheet = sheet;
8638 sheet = synctex_node_new(scanner,synctex_node_type_sheet);
8639 _synctex_data_set_page(sheet,2);
8640 SYNCTEX_TEST_BODY(TC, _synctex_data_page(sheet)==2,"");
8641 __synctex_tree_set_sibling(sheet, scanner->sheet);
8642 scanner->sheet = sheet;
8643 sheet = synctex_node_new(scanner,synctex_node_type_sheet);
8644 _synctex_data_set_page(sheet,1);
8645 SYNCTEX_TEST_BODY(TC, _synctex_data_page(sheet)==1,"");
8646 __synctex_tree_set_sibling(sheet, scanner->sheet);
8647 scanner->sheet = sheet;
8648 return TC;
8649 }
8650 int synctex_test_input(synctex_scanner_p scanner) {
8651 int TC = 0;
8652 synctex_node_p input = synctex_node_new(scanner,synctex_node_type_input);
8653 _synctex_data_set_tag(input,421);
8654 SYNCTEX_TEST_BODY(TC, _synctex_data_tag(input)==421,"");
8655 _synctex_data_set_tag(input,124);
8656 SYNCTEX_TEST_BODY(TC, _synctex_data_tag(input)==124,"");
8657 _synctex_data_set_line(input,421);
8658 SYNCTEX_TEST_BODY(TC, _synctex_data_line(input)==421,"");
8659 _synctex_data_set_line(input,214);
8660 SYNCTEX_TEST_BODY(TC, _synctex_data_line(input)==214,"");
8661 _synctex_data_set_line(input,214);
8662 SYNCTEX_TEST_BODY(TC, _synctex_data_line(input)==214,"");
8663 _synctex_input_copy_name(input,"214");
8664 SYNCTEX_TEST_BODY(TC, 0==memcmp(_synctex_data_name(input),"214",4),"");
8665 _synctex_input_copy_name(input,"421421");
8666
8667 SYNCTEX_TEST_BODY(TC,
8668 0==memcmp(_synctex_data_name(input),
8669 "421421",
8670 4),
8671 "");
8672 synctex_node_free(input);
8673 return TC;
8674 }
8675 int synctex_test_proxy(synctex_scanner_p scanner) {
8676 int TC = 0;
8677 synctex_node_p proxy = synctex_node_new(scanner,synctex_node_type_proxy);
8678 synctex_node_p target = synctex_node_new(scanner,synctex_node_type_rule);
8679 _synctex_tree_set_target(proxy,target);
8680 _synctex_data_set_tag(target,421);
8681 SYNCTEX_TEST_BODY(TC, _synctex_data_tag(target)==421,"");
8682 SYNCTEX_TEST_BODY(TC, synctex_node_tag(target)==421,"");
8683 SYNCTEX_TEST_BODY(TC, synctex_node_tag(proxy)==421,"");
8684 synctex_node_free(proxy);
8685 synctex_node_free(target);
8686 return TC;
8687 }
8688 int synctex_test_handle(synctex_scanner_p scanner) {
8689 int TC = 0;
8690 synctex_node_p handle = synctex_node_new(scanner,synctex_node_type_handle);
8691 synctex_node_p proxy = synctex_node_new(scanner, synctex_node_type_proxy);
8692 synctex_node_p target = synctex_node_new(scanner,synctex_node_type_rule);
8693 _synctex_tree_set_target(handle,target);
8694 _synctex_data_set_tag(target,421);
8695 SYNCTEX_TEST_BODY(TC, _synctex_data_tag(target)==421,"");
8696 SYNCTEX_TEST_BODY(TC, synctex_node_tag(target)==421,"");
8697 SYNCTEX_TEST_BODY(TC, synctex_node_tag(handle)==421,"");
8698 _synctex_data_set_line(target,214);
8699 SYNCTEX_TEST_BODY(TC, _synctex_data_line(target)==214,"");
8700 SYNCTEX_TEST_BODY(TC, synctex_node_line(target)==214,"");
8701 SYNCTEX_TEST_BODY(TC, synctex_node_line(handle)==214,"");
8702 _synctex_data_set_column(target,142);
8703 SYNCTEX_TEST_BODY(TC, _synctex_data_column(target)==142,"");
8704 SYNCTEX_TEST_BODY(TC, synctex_node_column(target)==142,"");
8705 SYNCTEX_TEST_BODY(TC, synctex_node_column(handle)==142,"");
8706 _synctex_tree_set_target(proxy,target);
8707 _synctex_tree_set_target(handle,proxy);
8708 _synctex_data_set_tag(target,412);
8709 SYNCTEX_TEST_BODY(TC, _synctex_data_tag(target)==412,"");
8710 SYNCTEX_TEST_BODY(TC, synctex_node_tag(target)==412,"");
8711 SYNCTEX_TEST_BODY(TC, synctex_node_tag(handle)==412,"");
8712 _synctex_data_set_line(target,124);
8713 SYNCTEX_TEST_BODY(TC, _synctex_data_line(target)==124,"");
8714 SYNCTEX_TEST_BODY(TC, synctex_node_line(target)==124,"");
8715 SYNCTEX_TEST_BODY(TC, synctex_node_line(handle)==124,"");
8716 _synctex_data_set_column(target,241);
8717 SYNCTEX_TEST_BODY(TC, _synctex_data_column(target)==241,"");
8718 SYNCTEX_TEST_BODY(TC, synctex_node_column(target)==241,"");
8719 SYNCTEX_TEST_BODY(TC, synctex_node_column(handle)==241,"");
8720 synctex_node_free(handle);
8721 synctex_node_free(proxy);
8722 synctex_node_free(target);
8723 return TC;
8724 }
8725 int synctex_test_setup_scanner_input(synctex_scanner_p scanner) {
8726 int TC = 0;
8727 synctex_node_p input = synctex_node_new(scanner,synctex_node_type_input);
8728 _synctex_data_set_tag(input,4);
8729 _synctex_input_copy_name(input,"21");
8730 _synctex_data_set_line(input,421);
8731 synctex_node_free(scanner->input);
8732 scanner->input = input;
8733 SYNCTEX_TEST_BODY(TC, _synctex_data_tag(input)==4,"");
8734 SYNCTEX_TEST_BODY(TC, strcmp(_synctex_data_name(input),"21")==0,"");
8735 SYNCTEX_TEST_BODY(TC, _synctex_data_line(input)==421,"");
8736 return TC;
8737 }
8738 int synctex_test_setup_nodes(synctex_scanner_p scanner, synctex_node_r nodes) {
8739 int TC = 0;
8740 int n;
8741 for (n=0;n<synctex_node_number_of_types;++n) {
8742 nodes[n] = synctex_node_new(scanner,n);
8743 SYNCTEX_TEST_BODY(TC, nodes[n]!=NULL,"");
8744 }
8745 return TC;
8746 }
8747 int synctex_test_teardown_nodes(synctex_scanner_p scanner, synctex_node_r nodes) {
8748 int n;
8749 for (n=0;n<synctex_node_number_of_types;++n) {
8750 synctex_node_free(nodes[n]);
8751 nodes[n]=NULL;
8752 }
8753 return 1;
8754 }
8755 int synctex_test_tree(synctex_scanner_p scanner) {
8756 int TC = 0;
8757 synctex_node_p nodes1[synctex_node_number_of_types];
8758 synctex_node_p nodes2[synctex_node_number_of_types];
8759 synctex_node_p nodes3[synctex_node_number_of_types];
8760 int i,j;
8761 TC += synctex_test_setup_nodes(scanner,nodes1);
8762 TC += synctex_test_setup_nodes(scanner,nodes2);
8763 TC += synctex_test_setup_nodes(scanner,nodes3);
8764 /* Every node has a sibling */
8765 for (i=0;i<synctex_node_number_of_types;++i) {
8766 for (j=0;j<synctex_node_number_of_types;++j) {
8767 _synctex_tree_set_sibling(nodes1[i],nodes2[i]);
8768 SYNCTEX_TEST_BODY(TC, nodes2[i]==synctex_node_sibling(nodes1[i]),"");
8769 }
8770 }
8771 synctex_test_teardown_nodes(scanner,nodes3);
8772 synctex_test_teardown_nodes(scanner,nodes2);
8773 synctex_test_teardown_nodes(scanner,nodes1);
8774 return TC;
8775 }
8776 int synctex_test_page(synctex_scanner_p scanner) {
8777 int TC = synctex_test_setup_scanner_sheets_421(scanner);
8778 synctex_node_p sheet = scanner->sheet;
8779 synctex_node_p node = synctex_node_new(scanner,synctex_node_type_rule);
8780 _synctex_data_set_tag(node,4);
8781 _synctex_data_set_line(node,21);
8782 synctex_node_free(_synctex_node_set_child(sheet,node));
8783 SYNCTEX_TEST_BODY(TC, synctex_node_page(node)==synctex_node_page(sheet),"");
8784 return TC;
8785 }
8786 int synctex_test_display_query(synctex_scanner_p scanner) {
8787 int TC = synctex_test_setup_scanner_sheets_421(scanner);
8788 synctex_node_p sheet = scanner->sheet;
8789 synctex_node_p node = synctex_node_new(scanner,synctex_node_type_rule);
8790 _synctex_data_set_tag(node,4);
8791 _synctex_data_set_line(node,21);
8792 synctex_node_free(_synctex_node_set_child(sheet,node));
8793 SYNCTEX_TEST_BODY(TC, node==synctex_node_child(sheet),"");
8794 __synctex_node_make_friend_tlc(node);
8795 SYNCTEX_TEST_BODY(TC, _synctex_scanner_friend(scanner, 25)==node,"");
8796 sheet = __synctex_tree_sibling(sheet);
8797 node = synctex_node_new(scanner,synctex_node_type_rule);
8798 _synctex_data_set_tag(node,4);
8799 _synctex_data_set_line(node,21);
8800 synctex_node_free(_synctex_node_set_child(sheet,node));
8801 SYNCTEX_TEST_BODY(TC, node==synctex_node_child(sheet),"");
8802 __synctex_node_make_friend_tlc(node);
8803 SYNCTEX_TEST_BODY(TC, _synctex_scanner_friend(scanner, 25)==node,"");
8804 sheet = __synctex_tree_sibling(sheet);
8805 node = synctex_node_new(scanner,synctex_node_type_rule);
8806 _synctex_data_set_tag(node,4);
8807 _synctex_data_set_line(node,21);
8808 synctex_node_free(_synctex_node_set_child(sheet,node));
8809 SYNCTEX_TEST_BODY(TC, node==synctex_node_child(sheet),"");
8810 __synctex_node_make_friend_tlc(node);
8811 SYNCTEX_TEST_BODY(TC, (_synctex_scanner_friend(scanner, 25)==node),"");
8812 synctex_test_setup_scanner_input(scanner);
8813 scanner->flags.has_parsed = synctex_YES;
8814 #if 1
8815 SYNCTEX_TEST_BODY(TC, (synctex_display_query(scanner,"21",21,4,-1)==3),"");
8816 #endif
8817 return TC;
8818 }
8819 typedef struct {
8820 int s; /* status */
8821 char n[25]; /* name */
8822 } synctex_test_sn_s;
8823
8824 synctex_test_sn_s synctex_test_tmp_sn(char * content) {
8825 synctex_test_sn_s sn = {0, "/tmp/test.XXXXXX.synctex"};
8826 FILE *sfp;
8827 int fd = mkstemps(sn.n,8);
8828 if (fd < 0) {
8829 fprintf(stderr, "%s: %s\n", sn.n, strerror(errno));
8830 sn.s = -1;
8831 return sn;
8832 }
8833 if ((sfp = fdopen(fd, "w+")) == NULL) {
8834 unlink(sn.n);
8835 close(fd);
8836 fprintf(stderr, "%s: %s\n", sn.n, strerror(errno));
8837 sn.s = -2;
8838 return sn;
8839 }
8840 sn.s = fputs(content,sfp);
8841 printf("temp:%s\n%i\n",sn.n,sn.s);
8842 fclose(sfp);
8843 if (sn.s==0) {
8844 sn.s = -2;
8845 unlink(sn.n);
8846 }
8847 return sn;
8848 }
8849 int synctex_test_sheet_1() {
8850 int TC = 0;
8851 char * content =
8852 "SyncTeX Version:1 \n" /*00-19*/
8853 "Input:1:./1.tex \n" /*20-39*/
8854 "Output:pdf \n" /*40-59*/
8855 "Magnification:100000000 \n" /*60-89*/
8856 "Unit:1 \n" /*90-99*/
8857 "X Offset:0 \n" /*00-19*/
8858 "Y Offset:0 \n" /*20-39*/
8859 "Content: \n" /*40-49*/
8860 "{1 \n" /*50-59*/
8861 "[1,10:20,350:330,330,0 \n" /*60-89*/
8862 "] \n" /*90-99*/
8863 "} \n" /*00-09*/
8864 "Postamble:\n";
8865 synctex_test_sn_s sn = synctex_test_tmp_sn(content);
8866 if (sn.s>0) {
8867 synctex_scanner_p scanner = synctex_scanner_new_with_output_file(sn.n, NULL, synctex_YES);
8868 synctex_node_p node = synctex_scanner_handle(scanner);
8869 printf("Created nodes:\n");
8870 while (node) {
8871 printf("%s\n",_synctex_node_abstract(node));
8872 node = synctex_node_next(node);
8873 }
8874 synctex_scanner_free(scanner);
8875 unlink(sn.n);
8876 } else {
8877 ++TC;
8878 }
8879 return TC;
8880 }
8881 int synctex_test_sheet_2() {
8882 int TC = 0;
8883 char * content =
8884 "SyncTeX Version:1 \n" /*00-19*/
8885 "Input:1:./1.tex \n" /*20-39*/
8886 "Output:pdf \n" /*40-59*/
8887 "Magnification:100000000 \n" /*60-89*/
8888 "Unit:1 \n" /*90-99*/
8889 "X Offset:0 \n" /*00-19*/
8890 "Y Offset:0 \n" /*20-39*/
8891 "Content: \n" /*40-49*/
8892 "{1 \n" /*50-59*/
8893 "(1,10:20,350:330,330,0 \n" /*60-89*/
8894 ") \n" /*90-99*/
8895 "} \n" /*00-09*/
8896 "Postamble:\n";
8897 synctex_test_sn_s sn = synctex_test_tmp_sn(content);
8898 if (sn.s>0) {
8899 synctex_scanner_p scanner = synctex_scanner_new_with_output_file(sn.n, NULL, synctex_YES);
8900 synctex_node_p node = synctex_scanner_handle(scanner);
8901 printf("Created nodes:\n");
8902 while (node) {
8903 printf("%s\n",_synctex_node_abstract(node));
8904 node = _synctex_node_next(node);
8905 }
8906 TC += synctex_scanner_free(scanner);
8907 unlink(sn.n);
8908 } else {
8909 ++TC;
8910 }
8911 return TC;
8912 }
8913 int synctex_test_charindex() {
8914 int TC = 0;
8915 char * content =
8916 "SyncTeX Version:1 \n" /*00-19*/
8917 "Input:1:./1.tex \n" /*20-39*/
8918 "Output:pdf \n" /*40-59*/
8919 "Magnification:100000000 \n" /*60-89*/
8920 "Unit:1 \n" /*90-99*/
8921 "X Offset:0 \n" /*00-19*/
8922 "Y Offset:0 \n" /*20-39*/
8923 "Content: \n" /*40-49*/
8924 "{1 \n" /*50-59*/
8925 "[1,10:20,350:330,330,0 \n" /*60-89*/
8926 "(1,58:20,100:250,10,5 \n" /*90-119*/
8927 "f1000:50,100 \n" /*20-39*/
8928 ") \n" /*40-49*/
8929 "] \n" /*50-59*/
8930 "} \n" /*60-69*/
8931 "Postamble:\n";
8932 synctex_test_sn_s sn = synctex_test_tmp_sn(content);
8933 if (sn.s>0) {
8934 synctex_scanner_p scanner = synctex_scanner_new_with_output_file(sn.n, NULL, synctex_YES);
8935 synctex_node_p node = synctex_scanner_handle(scanner);
8936 printf("Created nodes:\n");
8937 while (node) {
8938 printf("%s\n",_synctex_node_abstract(node));
8939 node = synctex_node_next(node);
8940 }
8941 TC += synctex_scanner_free(scanner);
8942 unlink(sn.n);
8943 } else {
8944 ++TC;
8945 }
8946 return TC;
8947 }
8948 int synctex_test_form() {
8949 int TC = 0;
8950 char * content =
8951 "SyncTeX Version:1 \n" /*00-19*/
8952 "Input:1:./1.tex \n" /*20-39*/
8953 "Output:pdf \n" /*40-59*/
8954 "Magnification:100000000 \n" /*60-89*/
8955 "Unit:1 \n" /*90-99*/
8956 "X Offset:0 \n" /*00-19*/
8957 "Y Offset:0 \n" /*20-39*/
8958 "Content: \n" /*40-49*/
8959 "{1 \n" /*50-59*/
8960 "[1,10:20,350:330,330,0 \n" /*60-89*/
8961 "(1,58:20,100:250,10,5 \n" /*90-119*/
8962 "f1000:50,100 \n" /*20-39*/
8963 ") \n" /*40-49*/
8964 "] \n" /*50-59*/
8965 "} \n" /*60-69*/
8966 "<1000 \n" /*70-79*/
8967 "(1,63:0,0:100,8,3 \n" /*80-99*/
8968 ") \n" /*00-09*/
8969 "> \n" /*10-19*/
8970 "Postamble:\n";
8971 synctex_test_sn_s sn = synctex_test_tmp_sn(content);
8972 if (sn.s>0) {
8973 synctex_scanner_p scanner = synctex_scanner_new_with_output_file(sn.n, NULL, synctex_YES);
8974 synctex_node_p node = synctex_scanner_handle(scanner);
8975 while (node) {
8976 printf("%s\n",_synctex_node_abstract(node));
8977 node = _synctex_node_next(node);
8978 }
8979 TC += synctex_scanner_free(scanner);
8980 unlink(sn.n);
8981 } else {
8982 ++TC;
8983 }
8984 return TC;
8985 }
8986 #endif