2 # fangle - fully featured notangle replacement in awk
4 # Copyright (C) 2009-2010 Sam Liddicott <sam@liddicott.com>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # NOTE: Arnold Robbins public domain getopt for awk is also used:
19 # getopt.awk --- do C library getopt(3) function in awk
21 # Arnold Robbins, arnold@skeeve.com, Public Domain
23 # Initial version: March, 1991
26 function getopt
(argc, argv, options
, thisopt
, i
)
28 if (length(options
) ==
0) # no options given
30 if (argv[Optind
] ==
"--") { # all done
34 } else if (argv[Optind
] !~
/^
-[^
: \t\n\f\r\v\b]/) {
40 thisopt =
substr(argv[Optind
], _opti
, 1)
42 i =
index(options
, thisopt
)
45 printf("%c -- invalid option\n",
46 thisopt
) > "/dev/stderr"
47 if (_opti
>=
length(argv[Optind
])) {
54 if (substr(options
, i
+ 1, 1) ==
":") {
56 if (length(substr(argv[Optind
], _opti
+ 1)) > 0)
57 Optarg =
substr(argv[Optind
], _opti
+ 1)
59 Optarg =
argv[++Optind
]
63 if (_opti ==
0 || _opti
>=
length(argv[Optind
])) {
71 function error
(message
)
73 print "ERROR: " FILENAME ":" FNR " " message
> "/dev/stderr";
76 function warning
(message
)
78 print "WARNING: " FILENAME ":" FNR " " message
> "/dev/stderr";
81 function debug_log
(message
)
83 print "DEBUG: " FILENAME ":" FNR " " message
> "/dev/stderr";
85 function new_mode_tracker
(context
, language
, mode
) {
87 context
[0, "language"] = language
;
88 context
[0, "mode"] = mode
;
90 function push_mode_tracker
(context
, language
, mode
,
94 if (!
("" in context
)) {
96 new_mode_tracker
(context
, language
, mode
);
100 # if (context[top, "language"] == language && mode=="") mode = context[top, "mode"];
101 if (context
[top
, "language"] == language
&& context
[top
, "mode"] == mode
) return top
;
104 context
[top
, "language"] = language
;
105 context
[top
, "mode"] = mode
;
110 function dump_mode_tracker
(context
,
113 for(c=
0; c
<= context
[""]; c
++) {
114 printf(" %2d %s:%s\n", c
, context
[c
, "language"], context
[c
, "mode"]) > "/dev/stderr";
115 for(d=
1; ( (c
, "values", d
) in context
); d
++) {
116 printf(" %2d %s\n", d
, context
[c
, "values", d
]) > "/dev/stderr";
120 function pop_mode_tracker
(context
, context_origin
)
122 if ( (context_origin
) && ("" in context
) && context
[""] != (1+context_origin
) && context
[""] != context_origin
) return 0;
123 context
[""] = context_origin
;
126 function mode_tracker
(context
, text
, values
,
127 # optional parameters
129 mode
, submodes
, language
,
130 cindex
, c
, a
, part
, item
, name
, result
, new_values
, new_mode
,
131 delimiters
, terminators
)
133 cindex = context
[""] + 0;
134 mode = context
[cindex
, "mode"];
135 language = context
[cindex
, "language" ];
136 submodes=modes
[language
, mode
, "submodes"];
138 if ((language
, mode
, "delimiters") in modes
) {
139 delimiters = modes
[language
, mode
, "delimiters"];
140 if (length(submodes
)>0) submodes = submodes
"|";
141 submodes=submodes delimiters
;
142 } else delimiters=
"";
143 if ((language
, mode
, "terminators") in modes
) {
144 terminators = modes
[language
, mode
, "terminators"];
145 if (length(submodes
)>0) submodes = submodes
"|";
146 submodes=submodes terminators
;
147 } else terminators=
"";
148 if (!
length(submodes
)) return text
;
149 while((cindex
>=
0) && length(text
)) {
150 if (match(text
, "(" submodes
")", a
)) {
152 error
(sprintf("Internal error, matched zero length submode, should be impossible - likely regex computation error\n" \
153 "Language=%s\nmode=%s\nmatch=%s\n", language
, mode
, submodes
));
155 part =
substr(text
, 1, RSTART -1);
157 if (match(a
[1], "^" terminators
"$")) {
158 #printf("%2d EXIT MODE [%s] by [%s] [%s]\n", cindex, mode, a[1], text) > "/dev/stderr"
159 context
[cindex
, "values", ++context
[cindex
, "values"]] = item
;
160 delete context
[cindex
];
161 context
[""] =
--cindex
;
163 mode = context
[cindex
, "mode"];
164 language = context
[cindex
, "language"];
165 submodes=modes
[language
, mode
, "submodes"];
167 if ((language
, mode
, "delimiters") in modes
) {
168 delimiters = modes
[language
, mode
, "delimiters"];
169 if (length(submodes
)>0) submodes = submodes
"|";
170 submodes=submodes delimiters
;
171 } else delimiters=
"";
172 if ((language
, mode
, "terminators") in modes
) {
173 terminators = modes
[language
, mode
, "terminators"];
174 if (length(submodes
)>0) submodes = submodes
"|";
175 submodes=submodes terminators
;
176 } else terminators=
"";
177 if (!
length(submodes
)) return text
;
180 text =
substr(text
, 1 + length(part
) + length(a
[1]));
182 else if (match(a
[1], "^" delimiters
"$")) {
184 context
[cindex
, "values", ++context
[cindex
, "values"]] = item
;
189 text =
substr(text
, 1 + length(part
) + length(a
[1]));
191 else if ((language
, a
[1], "terminators") in modes
) {
192 #check if new_mode is defined
194 #printf("%2d ENTER MODE [%s] in [%s]\n", cindex, a[1], text) > "/dev/stderr"
195 text =
substr(text
, 1 + length(part
) + length(a
[1]));
196 context
[""] =
++cindex
;
197 context
[cindex
, "mode"] = a
[1];
198 context
[cindex
, "language"] = language
;
200 submodes=modes
[language
, mode
, "submodes"];
202 if ((language
, mode
, "delimiters") in modes
) {
203 delimiters = modes
[language
, mode
, "delimiters"];
204 if (length(submodes
)>0) submodes = submodes
"|";
205 submodes=submodes delimiters
;
206 } else delimiters=
"";
207 if ((language
, mode
, "terminators") in modes
) {
208 terminators = modes
[language
, mode
, "terminators"];
209 if (length(submodes
)>0) submodes = submodes
"|";
210 submodes=submodes terminators
;
211 } else terminators=
"";
212 if (!
length(submodes
)) return text
;
214 error
(sprintf("Submode '%s' set unknown mode in text: %s\nLanguage %s Mode %s\n", a
[1], text
, language
, mode
));
215 text =
substr(text
, 1 + length(part
) + length(a
[1]));
219 context
[cindex
, "values", ++context
[cindex
, "values"]] = item text
;
225 context
["item"] = item
;
227 if (length(item
)) context
[cindex
, "values", ++context
[cindex
, "values"]] = item
;
231 function untab
(text
) {
232 gsub("[[:space:]]*\xE2\x86\xA4","", text
);
235 function transform_escape
(context
, text
, top
,
238 for(c = top
; c
>=
0; c
--) {
239 if ( (context
[c
, "language"], context
[c
, "mode"]) in escapes
) {
240 cpl = escapes
[context
[c
, "language"], context
[c
, "mode"]];
241 for (cp =
1; cp
<= cpl
; cp
++) {
242 s = escapes
[context
[c
, "language"], context
[c
, "mode"], cp
, "s"];
243 r = escapes
[context
[c
, "language"], context
[c
, "mode"], cp
, "r"];
247 if ( (context
[c
, "language"], context
[c
, "mode"], cp
, "t") in escapes
) {
248 quotes
[src
, "t"] = escapes
[context
[c
, "language"], context
[c
, "mode"], cp
, "t"];
255 function dump_escaper
(quotes
, r
, cc
) {
256 for(cc=
1; cc
<=c
; cc
++) {
257 printf("%2d s[%s] r[%s]\n", cc
, quotes
[cc
, "s"], quotes
[cc
, "r"]) > "/dev/stderr"
260 function parse_chunk_args
(language
, text
, values
, mode
,
265 new_mode_tracker
(context
, language
, mode
);
266 rest = mode_tracker
(context
, text
, values
);
268 for(c=
1; c
<= context
[0, "values"]; c
++) {
269 values
[c
] = context
[0, "values", c
];
273 function new_chunk
(chunk_name
, opts
, args
,
277 # HACK WHILE WE CHANGE TO ( ) for PARAM CHUNKS
278 gsub("\\(\\)$", "", chunk_name
);
279 if (!
(chunk_name in chunk_names
)) {
280 if (debug
) print "New chunk " chunk_name
;
281 chunk_names
[chunk_name
];
283 chunks
[chunk_name
, p
] = opts
[p
];
284 if (debug
) print "chunks[" chunk_name
"," p
"] = " opts
[p
];
287 chunks
[chunk_name
, "params", p
] = args
[p
];
289 if ("append" in opts
) {
290 append=opts
["append"];
291 if (!
(append in chunk_names
)) {
292 warning
("Chunk " chunk_name
" is appended to chunk " append
" which is not defined yet");
295 chunk_include
(append
, chunk_name
);
296 chunk_line
(append
, ORS);
299 active_chunk = chunk_name
;
300 prime_chunk
(chunk_name
);
303 function prime_chunk
(chunk_name
)
305 chunks
[chunk_name
, "part", ++chunks
[chunk_name
, "part"] ] = \
306 chunk_name SUBSEP
"chunklet" SUBSEP
"" ++chunks
[chunk_name
, "chunklet"];
307 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "FILENAME"] =
FILENAME;
308 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "LINENO"] =
FNR + 1;
311 function chunk_line
(chunk_name
, line
){
312 chunks
[chunk_name
, "chunklet", chunks
[chunk_name
, "chunklet"],
313 ++chunks
[chunk_name
, "chunklet", chunks
[chunk_name
, "chunklet"], "line"] ] = line
;
316 function chunk_include
(chunk_name
, chunk_ref
, indent
, tail
)
318 chunks
[chunk_name
, "part", ++chunks
[chunk_name
, "part"] ] = chunk_ref
;
319 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "type" ] = part_type_chunk
;
320 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "indent" ] = indent_string
(indent
);
321 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "tail" ] = tail
;
322 prime_chunk
(chunk_name
);
325 function indent_string
(indent
) {
326 return sprintf("%" indent "s
", "");
328 function output_chunk_names( c, prefix, suffix)
334 for (c in chunk_names) {
335 print prefix c suffix "\n";
338 function output_chunks( a)
340 for (a in chunk_names) {
345 function output_chunk(chunk) {
347 lineno_needed = linenos;
352 function write_chunk(chunk_name) {
354 return write_chunk_r(chunk_name, context);
357 function write_chunk_r(chunk_name, context, indent, tail,
359 chunk_path, chunk_args,
362 chunk_params, part, max_part, part_line, frag, max_frag, text,
363 chunklet, only_part, call_chunk_args, new_context)
365 if (debug) debug_log("write_chunk_r
(" chunk_name ")");
366 if (match(chunk_name, "^
(.
*)\\[([0-9]*)\\]$
", chunk_name_parts)) {
367 chunk_name = chunk_name_parts[1];
368 only_part = chunk_name_parts[2];
370 context_origin = push_mode_tracker(context, chunks[chunk_name, "language
"], "");
371 split(chunks[chunk_name, "params
"], chunk_params, " *; *");
372 if (! (chunk_name in chunk_names)) {
373 error(sprintf(_"The root module
<<%s
>> was not defined.
\nUsed by
: %s
",\
374 chunk_name, chunk_path));
377 max_part = chunks[chunk_name, "part
"];
378 for(part = 1; part <= max_part; part++) {
379 if (! only_part || part == only_part) {
380 if (linenos && (chunk_name SUBSEP "part
" SUBSEP part SUBSEP "FILENAME" in chunks)) {
381 a_filename = chunks[chunk_name, "part
", part, "FILENAME"];
382 a_lineno = chunks[chunk_name, "part
", part, "LINENO
"];
383 if (a_filename != filename || a_lineno != lineno) {
388 chunklet = chunks[chunk_name, "part
", part];
389 if (chunks[chunk_name, "part
", part, "type
"] == part_type_chunk) {
390 if (match(chunklet, "^
([^
\\[\\(]*)\\((.
*)\\)$
", chunklet_parts)) {
391 chunklet = chunklet_parts[1];
393 gsub(sprintf("%c
",11), "", chunklet);
394 gsub(sprintf("%c
",11), "", chunklet_parts[2]);
395 parse_chunk_args("c
-like
", chunklet_parts[2], call_chunk_args, "(");
396 for (c in call_chunk_args) {
397 call_chunk_args[c] = expand_chunk_args(call_chunk_args[c], chunk_params, chunk_args);
400 split("", call_chunk_args);
403 write_chunk_r(chunklet, context,
404 chunks[chunk_name, "part
", part, "indent
"] indent,
405 chunks[chunk_name, "part
", part, "tail
"],
406 chunk_path "\n " chunk_name,
408 } else if (chunklet SUBSEP "line
" in chunks) {
409 max_frag = chunks[chunklet, "line
"];
410 for(frag = 1; frag <= max_frag; frag++) {
411 if (newline && lineno_needed && ! lineno_suppressed) {
412 filename = a_filename;
414 print "#line " lineno " \"" filename "\"\n"
418 text = chunks
[chunklet
, frag
];
421 text = expand_chunk_args
(text
, chunk_params
, chunk_args
);
425 if (part == max_part
&& frag == max_frag
&& length(chunk_path
)) {
431 } else if (length(text
) || length(tail
)) {
432 if (newline
) text = indent text
;
437 mode_tracker
(context
, text
);
438 print untab
(transform_escape
(context
, text
, context_origin
));
440 lineno_suppressed =
substr(lastline
, length(lastline
)) ==
"\\";
444 # empty last chunklet
448 if (! pop_mode_tracker
(context
, context_origin
)) {
449 dump_mode_tracker
(context
);
450 error
(sprintf(_
"Module %s did not close context properly.\nUsed by: %s\n", chunk_name
, chunk_path
));
453 function expand_chunk_args
(text
, params
, args
,
454 p
, text_array
, next_text
, v
, t
, l
)
456 if (split(text
, text_array
, "\\${")) {
458 v
[params
[p
]]=args
[p
];
461 for(t=
2; t in text_array
; t
++) {
462 if (match(text_array
[t
], "^([a-zA-Z_][a-zA-Z0-9_]*)}", l
) &&
465 text = text v
[l
[1]] substr(text_array
[t
], length(l
[1])+2);
467 text = text
"${" text_array
[t
];
476 ARG_SEPARATOR=
sprintf("%c", 11);
479 modes
["c-like", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
480 modes
["c-like", "", "delimiters"]=
" *, *";
481 modes
["c-like", "\\", "terminators"]=
".";
482 modes
["c-like", "\"", "submodes"]=
"\\\\";
483 modes
["c-like", "\"", "terminators"]=
"\"";
484 escapes
["c-like", "\"", ++escapes
["c-like", "\""], "s"]=
"\\\\";
485 escapes
["c-like", "\"", escapes
["c-like", "\""], "r"]=
"\\\\";
486 escapes
["c-like", "\"", ++escapes
["c-like", "\""], "s"]=
"\"";
487 escapes
["c-like", "\"", escapes
["c-like", "\""], "r"]=
"\\" "\"";
488 escapes
["c-like", "\"", ++escapes
["c-like", "\""], "s"]=
"\n";
489 escapes
["c-like", "\"", escapes
["c-like", "\""], "r"]=
"\\n";
490 modes
["c-like", "'", "submodes"]=
"\\\\";
491 modes
["c-like", "'", "terminators"]=
"'";
492 escapes
["c-like", "'", ++escapes
["c-like", "'"], "s"]=
"\\\\";
493 escapes
["c-like", "'", escapes
["c-like", "'"], "r"]=
"\\\\";
494 escapes
["c-like", "'", ++escapes
["c-like", "'"], "s"]=
"'";
495 escapes
["c-like", "'", escapes
["c-like", "'"], "r"]=
"\\" "'";
496 escapes
["c-like", "'", ++escapes
["c-like", "'"], "s"]=
"\n";
497 escapes
["c-like", "'", escapes
["c-like", "'"], "r"]=
"\\n";
498 modes
["c-like", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
499 modes
["c-like", "{", "delimiters"]=
" *, *";
500 modes
["c-like", "{", "terminators"]=
"}";
501 modes
["c-like", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
502 modes
["c-like", "[", "delimiters"]=
" *, *";
503 modes
["c-like", "[", "terminators"]=
"\\]";
504 modes
["c-like", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
505 modes
["c-like", "(", "delimiters"]=
" *, *";
506 modes
["c-like", "(", "terminators"]=
"\\)";
508 modes
["c", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
509 modes
["c", "", "delimiters"]=
" *, *";
510 modes
["c", "\\", "terminators"]=
".";
511 modes
["c", "\"", "submodes"]=
"\\\\";
512 modes
["c", "\"", "terminators"]=
"\"";
513 escapes
["c", "\"", ++escapes
["c", "\""], "s"]=
"\\\\";
514 escapes
["c", "\"", escapes
["c", "\""], "r"]=
"\\\\";
515 escapes
["c", "\"", ++escapes
["c", "\""], "s"]=
"\"";
516 escapes
["c", "\"", escapes
["c", "\""], "r"]=
"\\" "\"";
517 escapes
["c", "\"", ++escapes
["c", "\""], "s"]=
"\n";
518 escapes
["c", "\"", escapes
["c", "\""], "r"]=
"\\n";
519 modes
["c", "'", "submodes"]=
"\\\\";
520 modes
["c", "'", "terminators"]=
"'";
521 escapes
["c", "'", ++escapes
["c", "'"], "s"]=
"\\\\";
522 escapes
["c", "'", escapes
["c", "'"], "r"]=
"\\\\";
523 escapes
["c", "'", ++escapes
["c", "'"], "s"]=
"'";
524 escapes
["c", "'", escapes
["c", "'"], "r"]=
"\\" "'";
525 escapes
["c", "'", ++escapes
["c", "'"], "s"]=
"\n";
526 escapes
["c", "'", escapes
["c", "'"], "r"]=
"\\n";
527 modes
["c", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
528 modes
["c", "{", "delimiters"]=
" *, *";
529 modes
["c", "{", "terminators"]=
"}";
530 modes
["c", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
531 modes
["c", "[", "delimiters"]=
" *, *";
532 modes
["c", "[", "terminators"]=
"\\]";
533 modes
["c", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
534 modes
["c", "(", "delimiters"]=
" *, *";
535 modes
["c", "(", "terminators"]=
"\\)";
536 modes
["c", "", "submodes"] = modes
["c", "", "submodes"] "|" "/\\*";
537 modes
["c", "/*", "terminators"]=
"\\*/";
538 modes
["c", "", "submodes"] = modes
["c", "", "submodes"] "|" "//";
539 modes
["c", "//", "terminators"]=
"\n";
540 escapes
["c", "//", ++escapes
["c", "//"], "s"]=
"\n";
541 escapes
["c", "//", escapes
["c", "//"], "r"]=
"\n//";
542 modes
["c", "", "submodes"] = modes
["c", "", "submodes"] "|" "#";
543 modes
["c", "#", "submodes" ]=
"\\\\";
544 modes
["c", "#", "terminators"]=
"\n";
545 escapes
["c", "#", ++escapes
["c", "#"], "s"]=
"\n";
546 escapes
["c", "#", escapes
["c", "#"], "r"]=
"\\\\\n";
548 modes
["awk", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
549 modes
["awk", "", "delimiters"]=
" *, *";
550 modes
["awk", "\\", "terminators"]=
".";
551 modes
["awk", "\"", "submodes"]=
"\\\\";
552 modes
["awk", "\"", "terminators"]=
"\"";
553 escapes
["awk", "\"", ++escapes
["awk", "\""], "s"]=
"\\\\";
554 escapes
["awk", "\"", escapes
["awk", "\""], "r"]=
"\\\\";
555 escapes
["awk", "\"", ++escapes
["awk", "\""], "s"]=
"\"";
556 escapes
["awk", "\"", escapes
["awk", "\""], "r"]=
"\\" "\"";
557 escapes
["awk", "\"", ++escapes
["awk", "\""], "s"]=
"\n";
558 escapes
["awk", "\"", escapes
["awk", "\""], "r"]=
"\\n";
559 modes
["awk", "'", "submodes"]=
"\\\\";
560 modes
["awk", "'", "terminators"]=
"'";
561 escapes
["awk", "'", ++escapes
["awk", "'"], "s"]=
"\\\\";
562 escapes
["awk", "'", escapes
["awk", "'"], "r"]=
"\\\\";
563 escapes
["awk", "'", ++escapes
["awk", "'"], "s"]=
"'";
564 escapes
["awk", "'", escapes
["awk", "'"], "r"]=
"\\" "'";
565 escapes
["awk", "'", ++escapes
["awk", "'"], "s"]=
"\n";
566 escapes
["awk", "'", escapes
["awk", "'"], "r"]=
"\\n";
567 modes
["awk", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
568 modes
["awk", "{", "delimiters"]=
" *, *";
569 modes
["awk", "{", "terminators"]=
"}";
570 modes
["awk", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
571 modes
["awk", "[", "delimiters"]=
" *, *";
572 modes
["awk", "[", "terminators"]=
"\\]";
573 modes
["awk", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
574 modes
["awk", "(", "delimiters"]=
" *, *";
575 modes
["awk", "(", "terminators"]=
"\\)";
576 modes
["awk", "", "submodes"] = modes
["awk", "", "submodes"] "|" "#";
577 modes
["awk", "#", "terminators"]=
"\n";
578 escapes
["awk", "#", ++escapes
["awk", "#"], "s"]=
"\n";
579 escapes
["awk", "#", escapes
["awk", "#"], "r"]=
"\n#";
580 modes
["awk", "", "submodes"] = modes
["awk", "", "submodes"] "|" "/\\^";
581 modes
["awk", "/^", "terminators"]=
"/";
582 modes
["perl", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
583 modes
["perl", "", "delimiters"]=
" *, *";
584 modes
["perl", "\\", "terminators"]=
".";
585 modes
["perl", "\"", "submodes"]=
"\\\\";
586 modes
["perl", "\"", "terminators"]=
"\"";
587 escapes
["perl", "\"", ++escapes
["perl", "\""], "s"]=
"\\\\";
588 escapes
["perl", "\"", escapes
["perl", "\""], "r"]=
"\\\\";
589 escapes
["perl", "\"", ++escapes
["perl", "\""], "s"]=
"\"";
590 escapes
["perl", "\"", escapes
["perl", "\""], "r"]=
"\\" "\"";
591 escapes
["perl", "\"", ++escapes
["perl", "\""], "s"]=
"\n";
592 escapes
["perl", "\"", escapes
["perl", "\""], "r"]=
"\\n";
593 modes
["perl", "'", "submodes"]=
"\\\\";
594 modes
["perl", "'", "terminators"]=
"'";
595 escapes
["perl", "'", ++escapes
["perl", "'"], "s"]=
"\\\\";
596 escapes
["perl", "'", escapes
["perl", "'"], "r"]=
"\\\\";
597 escapes
["perl", "'", ++escapes
["perl", "'"], "s"]=
"'";
598 escapes
["perl", "'", escapes
["perl", "'"], "r"]=
"\\" "'";
599 escapes
["perl", "'", ++escapes
["perl", "'"], "s"]=
"\n";
600 escapes
["perl", "'", escapes
["perl", "'"], "r"]=
"\\n";
601 modes
["perl", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
602 modes
["perl", "{", "delimiters"]=
" *, *";
603 modes
["perl", "{", "terminators"]=
"}";
604 modes
["perl", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
605 modes
["perl", "[", "delimiters"]=
" *, *";
606 modes
["perl", "[", "terminators"]=
"\\]";
607 modes
["perl", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
608 modes
["perl", "(", "delimiters"]=
" *, *";
609 modes
["perl", "(", "terminators"]=
"\\)";
610 modes
["perl", "", "submodes"] = modes
["perl", "", "submodes"] "|" "/\\*";
611 modes
["perl", "/*", "terminators"]=
"\\*/";
612 modes
["perl", "", "submodes"] = modes
["perl", "", "submodes"] "|" "#";
613 modes
["perl", "#", "terminators"]=
"\n";
614 escapes
["perl", "#", ++escapes
["perl", "#"], "s"]=
"\n";
615 escapes
["perl", "#", escapes
["perl", "#"], "r"]=
"\n#";
616 modes
["sh", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[|\\$\\(";
617 modes
["sh", "\\", "terminators"]=
".";
619 modes
["sh", "\"", "submodes"]=
"\\\\|\\$\\(";
620 modes
["sh", "\"", "terminators"]=
"\"";
621 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\\\\";
622 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\\\";
623 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\"";
624 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\" "\"";
625 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\n";
626 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\n";
628 modes
["sh", "'", "terminators"]=
"'";
629 escapes
["sh", "'", ++escapes
["sh", "'"], "s"]=
"'";
630 escapes
["sh", "'", escapes
["sh", "'"], "r"]=
"'\\'" "'";
631 modes
["sh", "$(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
632 modes
["sh", "$(", "delimiters"]=
" *, *";
633 modes
["sh", "$(", "terminators"]=
"\\)";
634 escapes
["sh", "$(", ++escapes
["sh", "$("], "tunnel"]=
"";
635 modes
["sh", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
636 modes
["sh", "{", "delimiters"]=
" *, *";
637 modes
["sh", "{", "terminators"]=
"}";
638 modes
["sh", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
639 modes
["sh", "[", "delimiters"]=
" *, *";
640 modes
["sh", "[", "terminators"]=
"\\]";
641 modes
["sh", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
642 modes
["sh", "(", "delimiters"]=
" *, *";
643 modes
["sh", "(", "terminators"]=
"\\)";
644 modes
["sh", "", "submodes"] = modes
["sh", "", "submodes"] "|" "#";
645 modes
["sh", "#", "terminators"]=
"\n";
646 escapes
["sh", "#", ++escapes
["sh", "#"], "s"]=
"\n";
647 escapes
["sh", "#", escapes
["sh", "#"], "r"]=
"\n#";
648 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\\$";
649 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\$";
650 modes
["make", "", "submodes"]=
" ";
651 modes
["make", " ", "terminators"]=
"\\n";
652 escapes
["make", " ", ++escapes
["make", " "], "s"]=
"\\n";
653 escapes
["make", " ", escapes
["make", " "], "r"]=
" ;\\\n ";
654 escapes
["make", " ", ++escapes
["make", " "], "s"]=
"\\$";
655 escapes
["make", " ", escapes
["make", " "], "r"]=
"$$";
662 Optind =
1 # skip ARGV[0]
663 while(getopt
(ARGC, ARGV, "R:LdT:hr")!=-1) {
664 if (Optopt ==
"R") root = Optarg
;
665 else if (Optopt ==
"r") root=
"";
666 else if (Optopt ==
"L") linenos =
1;
667 else if (Optopt ==
"d") debug =
1;
668 else if (Optopt ==
"T") tabs = indent_string
(Optarg
+0);
669 else if (Optopt ==
"h") help
();
670 else if (Optopt ==
"?") help
();
672 for (i=
1; i
<Optind
; i
++) { ARGV[i
]=
""; }
681 gsub("\\xE2\\x86\\xA6", "\x09");
685 gsub("\\xE2\\x80\\x98", "`");
689 if (match($
0, "^ *([^[ ]* |)<([^[ ]*)\\[[0-9]*\\][(](.*)[)].*, lang=([^ ]*)>", line
)) {
690 next_chunk_name=line
[2];
691 get_texmacs_chunk_args
(line
[3], next_chunk_params
);
692 gsub(ARG_SEPARATOR
",? ?", ";", line
[3]);
693 params =
"params=" line
[3];
695 params = params
",language=" line
[4]
697 get_tex_chunk_args
(params
, next_chunk_opts
);
698 new_chunk
(next_chunk_name
, next_chunk_opts
, next_chunk_params
);
699 texmacs_chunking =
1;
701 # warning(sprintf("Unexpected chunk match: %s\n", $_))
706 if (match($
0, "^\\\\Chunk{ *([^ ,}]*),?(.*)}", line
)) {
707 next_chunk_name = line
[1];
708 get_tex_chunk_args
(line
[2], next_chunk_opts
);
712 /^
\\begin{lstlisting
}|^
\\begin{Chunk
}/ {
713 if (match($
0, "}.*[[,] *name= *{? *([^], }]*)", line
)) {
716 new_chunk
(next_chunk_name
, next_chunk_opts
);
721 /^
*\
|____________
*/ && texmacs_chunking
{
726 /^
*\
|\
/\\/ && texmacs_chunking
{
732 /^
*[1-9][0-9]* *\
| / {
733 if (texmacs_chunking
) {
736 gsub("^ *[1-9][0-9]* *\\| ", "")
739 /^
*\.\
/\\/ && texmacs_chunking
{
742 /^
*__
*$
/ && texmacs_chunking
{
746 if (! texmacs_chunk
) {
747 # must be a texmacs continued line
753 # texmacs_chunking=0;
757 if (match($
0, "^[<]<(.*)[>]>= *$", line
)) {
764 /^
\\[e
]nd
{lstlisting
}|^
\\[e
]nd
{Chunk
}/ {
774 length(active_chunk
) {
780 while(match(chunk
,"(\xC2\xAB)([^\xC2\xBB]*) [^\xC2\xBB]*\xC2\xBB", line
) ||
782 "([=]<\\\\chunkref{([^}>]*)}(\\(.*\\)|)>|<<([a-zA-Z_][-a-zA-Z0-9_]*)>>)",
785 chunklet =
substr(chunk
, 1, RSTART - 1);
786 indent
+=
length(chunklet
);
787 chunk_line
(active_chunk
, chunklet
);
788 chunk =
substr(chunk
, RSTART + RLENGTH);
789 if (substr(line
[1], 1, 1) ==
"=") {
792 gsub("\\\\#", "#", line
[3]);
793 gsub("\\\\textbackslash{}", "\\", line
[3]);
794 gsub("\\\\\\^", "^", line
[3]);
795 chunk_include
(active_chunk
, line
[2] line
[3], indent
);
796 } else if (substr(line
[1], 1, 1) ==
"<") {
797 chunk_include
(active_chunk
, line
[4], indent
);
798 } else if (line
[1] ==
"\xC2\xAB") {
799 chunk_include
(active_chunk
, line
[2], indent
);
801 error
("Unknown chunk fragment: " line
[1]);
804 chunk_line
(active_chunk
, chunk
);
805 chunk_line
(active_chunk
, "\n");
809 print "------ chunk names "
810 output_chunk_names
();
811 print "====== chunks"
815 print a
"=" chunks
[a
];
819 if (length(root
)) output_chunk
(root
);
820 else output_chunk_names
();
822 function get_texmacs_chunk_args
(text
, args
, a
, done
) {
823 split(text
, args
, ARG_SEPARATOR
);
826 for (a=
1; (a in args
); a
++) if (a
>1) {
827 if (args
[a
] ==
"" || substr(args
[a
], 1, 1) ==
")") done=
1;
833 if (substr(args
[a
], 1, 2) ==
", ") args
[a
]=
substr(args
[a
], 3);
834 else if (substr(args
[a
], 1, 1) ==
",") args
[a
]=
substr(args
[a
], 2);
837 function get_tex_chunk_args
(text
, values
,
838 # optional parameters
839 path
, # hierarchical precursors
844 while(length(text
)) {
845 if (match(text
, "^ *}(.*)", a
)) {
848 if (!
match(text
, " *([^,=]*[^,= ]) *(([,=]) *(([^,}]*) *,* *(.*))|)$", a
)) {
853 if (substr(a
[4],1,1) ==
"{") {
854 text = get_tex_chunk_args
(substr(a
[4],2), values
, path name SUBSEP
);
856 values
[path name
]=a
[5];
860 values
[path name
]=
"";