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"];
103 context
[top
, "language"] = language
;
104 context
[top
, "mode"] = mode
;
109 function dump_mode_tracker
(context
,
112 for(c=
0; c
<= context
[""]; c
++) {
113 printf(" %2d %s:%s\n", c
, context
[c
, "language"], context
[c
, "mode"]) > "/dev/stderr";
114 for(d=
1; ( (c
, "values", d
) in context
); d
++) {
115 printf(" %2d %s\n", d
, context
[c
, "values", d
]) > "/dev/stderr";
119 function pop_mode_tracker
(context
, context_origin
)
121 if ( (context_origin
) && ("" in context
) && context
[""] != context_origin
) return 0;
125 function mode_tracker
(context
, text
, values
,
126 # optional parameters
128 mode
, submodes
, language
,
129 cindex
, c
, a
, part
, item
, name
, result
, new_values
, new_mode
,
130 delimiters
, terminators
)
132 cindex = context
[""] + 0;
133 mode = context
[cindex
, "mode"];
134 language = context
[cindex
, "language" ];
135 submodes=modes
[language
, mode
, "submodes"];
137 if ((language
, mode
, "delimiters") in modes
) {
138 delimiters = modes
[language
, mode
, "delimiters"];
139 if (length(submodes
)>0) submodes = submodes
"|";
140 submodes=submodes delimiters
;
141 } else delimiters=
"";
142 if ((language
, mode
, "terminators") in modes
) {
143 terminators = modes
[language
, mode
, "terminators"];
144 if (length(submodes
)>0) submodes = submodes
"|";
145 submodes=submodes terminators
;
146 } else terminators=
"";
147 if (!
length(submodes
)) return text
;
148 while((cindex
>=
0) && length(text
)) {
149 if (match(text
, "(" submodes
")", a
)) {
151 error
(sprintf("Internal error, matched zero length submode, should be impossible - likely regex computation error\n" \
152 "Language=%s\nmode=%s\nmatch=%s\n", language
, mode
, submodes
));
154 part =
substr(text
, 1, RSTART -1);
156 if (match(a
[1], "^" terminators
"$")) {
157 #printf("%2d EXIT MODE [%s] by [%s] [%s]\n", cindex, mode, a[1], text) > "/dev/stderr"
158 context
[cindex
, "values", ++context
[cindex
, "values"]] = item
;
159 delete context
[cindex
];
160 context
[""] =
--cindex
;
162 mode = context
[cindex
, "mode"];
163 language = context
[cindex
, "language"];
164 submodes=modes
[language
, mode
, "submodes"];
166 if ((language
, mode
, "delimiters") in modes
) {
167 delimiters = modes
[language
, mode
, "delimiters"];
168 if (length(submodes
)>0) submodes = submodes
"|";
169 submodes=submodes delimiters
;
170 } else delimiters=
"";
171 if ((language
, mode
, "terminators") in modes
) {
172 terminators = modes
[language
, mode
, "terminators"];
173 if (length(submodes
)>0) submodes = submodes
"|";
174 submodes=submodes terminators
;
175 } else terminators=
"";
176 if (!
length(submodes
)) return text
;
179 text =
substr(text
, 1 + length(part
) + length(a
[1]));
181 else if (match(a
[1], "^" delimiters
"$")) {
183 context
[cindex
, "values", ++context
[cindex
, "values"]] = item
;
188 text =
substr(text
, 1 + length(part
) + length(a
[1]));
190 else if ((language
, a
[1], "terminators") in modes
) {
191 #check if new_mode is defined
193 #printf("%2d ENTER MODE [%s] in [%s]\n", cindex, a[1], text) > "/dev/stderr"
194 text =
substr(text
, 1 + length(part
) + length(a
[1]));
195 context
[""] =
++cindex
;
196 context
[cindex
, "mode"] = a
[1];
197 context
[cindex
, "language"] = language
;
199 submodes=modes
[language
, mode
, "submodes"];
201 if ((language
, mode
, "delimiters") in modes
) {
202 delimiters = modes
[language
, mode
, "delimiters"];
203 if (length(submodes
)>0) submodes = submodes
"|";
204 submodes=submodes delimiters
;
205 } else delimiters=
"";
206 if ((language
, mode
, "terminators") in modes
) {
207 terminators = modes
[language
, mode
, "terminators"];
208 if (length(submodes
)>0) submodes = submodes
"|";
209 submodes=submodes terminators
;
210 } else terminators=
"";
211 if (!
length(submodes
)) return text
;
213 error
(sprintf("Submode '%s' set unknown mode in text: %s\nLanguage %s Mode %s\n", a
[1], text
, language
, mode
));
214 text =
substr(text
, 1 + length(part
) + length(a
[1]));
218 context
[cindex
, "values", ++context
[cindex
, "values"]] = item text
;
224 context
["item"] = item
;
226 if (length(item
)) context
[cindex
, "values", ++context
[cindex
, "values"]] = item
;
230 function untab
(text
) {
231 gsub("[[:space:]]*\xE2\x86\xA4","", text
);
234 function transform_escape
(context
, text
, top
,
237 for(c = top
; c
>=
0; c
--) {
238 if ( (context
[c
, "language"], context
[c
, "mode"]) in escapes
) {
239 cpl = escapes
[context
[c
, "language"], context
[c
, "mode"]];
240 for (cp =
1; cp
<= cpl
; cp
++) {
241 s = escapes
[context
[c
, "language"], context
[c
, "mode"], cp
, "s"];
242 r = escapes
[context
[c
, "language"], context
[c
, "mode"], cp
, "r"];
246 if ( (context
[c
, "language"], context
[c
, "mode"], cp
, "t") in escapes
) {
247 quotes
[src
, "t"] = escapes
[context
[c
, "language"], context
[c
, "mode"], cp
, "t"];
254 function dump_escaper
(quotes
, r
, cc
) {
255 for(cc=
1; cc
<=c
; cc
++) {
256 printf("%2d s[%s] r[%s]\n", cc
, quotes
[cc
, "s"], quotes
[cc
, "r"]) > "/dev/stderr"
259 function parse_chunk_args
(language
, text
, values
, mode
,
264 new_mode_tracker
(context
, language
, mode
);
265 rest = mode_tracker
(context
, text
, values
);
267 for(c=
1; c
<= context
[0, "values"]; c
++) {
268 values
[c
] = context
[0, "values", c
];
272 function new_chunk
(chunk_name
, opts
, args
,
276 # HACK WHILE WE CHANGE TO ( ) for PARAM CHUNKS
277 gsub("\\(\\)$", "", chunk_name
);
278 if (!
(chunk_name in chunk_names
)) {
279 if (debug
) print "New chunk " chunk_name
;
280 chunk_names
[chunk_name
];
282 chunks
[chunk_name
, p
] = opts
[p
];
283 if (debug
) print "chunks[" chunk_name
"," p
"] = " opts
[p
];
286 chunks
[chunk_name
, "params", p
] = args
[p
];
288 if ("append" in opts
) {
289 append=opts
["append"];
290 if (!
(append in chunk_names
)) {
291 warning
("Chunk " chunk_name
" is appended to chunk " append
" which is not defined yet");
294 chunk_include
(append
, chunk_name
);
295 chunk_line
(append
, ORS);
298 active_chunk = chunk_name
;
299 prime_chunk
(chunk_name
);
302 function prime_chunk
(chunk_name
)
304 chunks
[chunk_name
, "part", ++chunks
[chunk_name
, "part"] ] = \
305 chunk_name SUBSEP
"chunklet" SUBSEP
"" ++chunks
[chunk_name
, "chunklet"];
306 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "FILENAME"] =
FILENAME;
307 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "LINENO"] =
FNR + 1;
310 function chunk_line
(chunk_name
, line
){
311 chunks
[chunk_name
, "chunklet", chunks
[chunk_name
, "chunklet"],
312 ++chunks
[chunk_name
, "chunklet", chunks
[chunk_name
, "chunklet"], "line"] ] = line
;
315 function chunk_include
(chunk_name
, chunk_ref
, indent
, tail
)
317 chunks
[chunk_name
, "part", ++chunks
[chunk_name
, "part"] ] = chunk_ref
;
318 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "type" ] = part_type_chunk
;
319 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "indent" ] = indent_string
(indent
);
320 chunks
[chunk_name
, "part", chunks
[chunk_name
, "part"], "tail" ] = tail
;
321 prime_chunk
(chunk_name
);
324 function indent_string
(indent
) {
325 return sprintf("%" indent "s
", "");
327 function output_chunk_names( c, prefix, suffix)
333 for (c in chunk_names) {
334 print prefix c suffix "\n";
337 function output_chunks( a)
339 for (a in chunk_names) {
344 function output_chunk(chunk) {
346 lineno_needed = linenos;
351 function write_chunk(chunk_name) {
353 return write_chunk_r(chunk_name, context);
356 function write_chunk_r(chunk_name, context, indent, tail,
358 chunk_path, chunk_args,
361 chunk_params, part, max_part, part_line, frag, max_frag, text,
362 chunklet, only_part, call_chunk_args, new_context)
364 if (debug) debug_log("write_chunk_r
(" chunk_name ")");
365 if (match(chunk_name, "^
(.
*)\\[([0-9]*)\\]$
", chunk_name_parts)) {
366 chunk_name = chunk_name_parts[1];
367 only_part = chunk_name_parts[2];
369 context_origin = push_mode_tracker(context, chunks[chunk_name, "language
"], "");
370 split(chunks[chunk_name, "params
"], chunk_params, " *; *");
371 if (! (chunk_name in chunk_names)) {
372 error(sprintf(_"The root module
<<%s
>> was not defined.
\nUsed by
: %s
",\
373 chunk_name, chunk_path));
376 max_part = chunks[chunk_name, "part
"];
377 for(part = 1; part <= max_part; part++) {
378 if (! only_part || part == only_part) {
379 if (linenos && (chunk_name SUBSEP "part
" SUBSEP part SUBSEP "FILENAME" in chunks)) {
380 a_filename = chunks[chunk_name, "part
", part, "FILENAME"];
381 a_lineno = chunks[chunk_name, "part
", part, "LINENO
"];
382 if (a_filename != filename || a_lineno != lineno) {
387 chunklet = chunks[chunk_name, "part
", part];
388 if (chunks[chunk_name, "part
", part, "type
"] == part_type_chunk) {
389 if (match(chunklet, "^
([^
\\[\\(]*)\\((.
*)\\)$
", chunklet_parts)) {
390 chunklet = chunklet_parts[1];
392 gsub(sprintf("%c
",11), "", chunklet);
393 gsub(sprintf("%c
",11), "", chunklet_parts[2]);
394 parse_chunk_args("c
-like
", chunklet_parts[2], call_chunk_args, "(");
395 for (c in call_chunk_args) {
396 call_chunk_args[c] = expand_chunk_args(call_chunk_args[c], chunk_params, chunk_args);
399 split("", call_chunk_args);
402 write_chunk_r(chunklet, context,
403 chunks[chunk_name, "part
", part, "indent
"] indent,
404 chunks[chunk_name, "part
", part, "tail
"],
405 chunk_path "\n " chunk_name,
407 } else if (chunklet SUBSEP "line
" in chunks) {
408 max_frag = chunks[chunklet, "line
"];
409 for(frag = 1; frag <= max_frag; frag++) {
410 if (newline && lineno_needed && ! lineno_suppressed) {
411 filename = a_filename;
413 print "#line " lineno " \"" filename "\"\n"
417 text = chunks
[chunklet
, frag
];
420 text = expand_chunk_args
(text
, chunk_params
, chunk_args
);
424 if (part == max_part
&& frag == max_frag
&& length(chunk_path
)) {
430 } else if (length(text
) || length(tail
)) {
431 if (newline
) text = indent text
;
436 mode_tracker
(context
, text
);
437 print untab
(transform_escape
(context
, text
, context_origin
));
439 lineno_suppressed =
substr(lastline
, length(lastline
)) ==
"\\";
443 # empty last chunklet
447 if (! pop_mode_tracker
(context
, context_origin
)) {
448 dump_mode_tracker
(context
);
449 error
(sprintf(_
"Module %s did not close context properly.\nUsed by: %s\n", chunk_name
, chunk_path
));
452 function expand_chunk_args
(text
, params
, args
,
453 p
, text_array
, next_text
, v
, t
, l
)
455 if (split(text
, text_array
, "\\${")) {
457 v
[params
[p
]]=args
[p
];
460 for(t=
2; t in text_array
; t
++) {
461 if (match(text_array
[t
], "^([a-zA-Z_][a-zA-Z0-9_]*)}", l
) &&
464 text = text v
[l
[1]] substr(text_array
[t
], length(l
[1])+2);
466 text = text
"${" text_array
[t
];
475 ARG_SEPARATOR=
sprintf("%c", 11);
478 modes
["c-like", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
479 modes
["c-like", "", "delimiters"]=
" *, *";
480 modes
["c-like", "\\", "terminators"]=
".";
481 modes
["c-like", "\"", "submodes"]=
"\\\\";
482 modes
["c-like", "\"", "terminators"]=
"\"";
483 escapes
["c-like", "\"", ++escapes
["c-like", "\""], "s"]=
"\\\\";
484 escapes
["c-like", "\"", escapes
["c-like", "\""], "r"]=
"\\\\";
485 escapes
["c-like", "\"", ++escapes
["c-like", "\""], "s"]=
"\"";
486 escapes
["c-like", "\"", escapes
["c-like", "\""], "r"]=
"\\" "\"";
487 escapes
["c-like", "\"", ++escapes
["c-like", "\""], "s"]=
"\n";
488 escapes
["c-like", "\"", escapes
["c-like", "\""], "r"]=
"\\n";
489 modes
["c-like", "'", "submodes"]=
"\\\\";
490 modes
["c-like", "'", "terminators"]=
"'";
491 escapes
["c-like", "'", ++escapes
["c-like", "'"], "s"]=
"\\\\";
492 escapes
["c-like", "'", escapes
["c-like", "'"], "r"]=
"\\\\";
493 escapes
["c-like", "'", ++escapes
["c-like", "'"], "s"]=
"'";
494 escapes
["c-like", "'", escapes
["c-like", "'"], "r"]=
"\\" "'";
495 escapes
["c-like", "'", ++escapes
["c-like", "'"], "s"]=
"\n";
496 escapes
["c-like", "'", escapes
["c-like", "'"], "r"]=
"\\n";
497 modes
["c-like", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
498 modes
["c-like", "{", "delimiters"]=
" *, *";
499 modes
["c-like", "{", "terminators"]=
"}";
500 modes
["c-like", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
501 modes
["c-like", "[", "delimiters"]=
" *, *";
502 modes
["c-like", "[", "terminators"]=
"\\]";
503 modes
["c-like", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
504 modes
["c-like", "(", "delimiters"]=
" *, *";
505 modes
["c-like", "(", "terminators"]=
"\\)";
507 modes
["c", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
508 modes
["c", "", "delimiters"]=
" *, *";
509 modes
["c", "\\", "terminators"]=
".";
510 modes
["c", "\"", "submodes"]=
"\\\\";
511 modes
["c", "\"", "terminators"]=
"\"";
512 escapes
["c", "\"", ++escapes
["c", "\""], "s"]=
"\\\\";
513 escapes
["c", "\"", escapes
["c", "\""], "r"]=
"\\\\";
514 escapes
["c", "\"", ++escapes
["c", "\""], "s"]=
"\"";
515 escapes
["c", "\"", escapes
["c", "\""], "r"]=
"\\" "\"";
516 escapes
["c", "\"", ++escapes
["c", "\""], "s"]=
"\n";
517 escapes
["c", "\"", escapes
["c", "\""], "r"]=
"\\n";
518 modes
["c", "'", "submodes"]=
"\\\\";
519 modes
["c", "'", "terminators"]=
"'";
520 escapes
["c", "'", ++escapes
["c", "'"], "s"]=
"\\\\";
521 escapes
["c", "'", escapes
["c", "'"], "r"]=
"\\\\";
522 escapes
["c", "'", ++escapes
["c", "'"], "s"]=
"'";
523 escapes
["c", "'", escapes
["c", "'"], "r"]=
"\\" "'";
524 escapes
["c", "'", ++escapes
["c", "'"], "s"]=
"\n";
525 escapes
["c", "'", escapes
["c", "'"], "r"]=
"\\n";
526 modes
["c", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
527 modes
["c", "{", "delimiters"]=
" *, *";
528 modes
["c", "{", "terminators"]=
"}";
529 modes
["c", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
530 modes
["c", "[", "delimiters"]=
" *, *";
531 modes
["c", "[", "terminators"]=
"\\]";
532 modes
["c", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
533 modes
["c", "(", "delimiters"]=
" *, *";
534 modes
["c", "(", "terminators"]=
"\\)";
535 modes
["c", "", "submodes"] = modes
["c", "", "submodes"] "|" "/\\*";
536 modes
["c", "/*", "terminators"]=
"\\*/";
537 modes
["c", "", "submodes"] = modes
["c", "", "submodes"] "|" "//";
538 modes
["c", "//", "terminators"]=
"\n";
539 escapes
["c", "//", ++escapes
["c", "//"], "s"]=
"\n";
540 escapes
["c", "//", escapes
["c", "//"], "r"]=
"\n//";
541 modes
["c", "", "submodes"] = modes
["c", "", "submodes"] "|" "#";
542 modes
["c", "#", "submodes" ]=
"\\\\";
543 modes
["c", "#", "terminators"]=
"\n";
544 escapes
["c", "#", ++escapes
["c", "#"], "s"]=
"\n";
545 escapes
["c", "#", escapes
["c", "#"], "r"]=
"\\\\\n";
547 modes
["awk", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
548 modes
["awk", "", "delimiters"]=
" *, *";
549 modes
["awk", "\\", "terminators"]=
".";
550 modes
["awk", "\"", "submodes"]=
"\\\\";
551 modes
["awk", "\"", "terminators"]=
"\"";
552 escapes
["awk", "\"", ++escapes
["awk", "\""], "s"]=
"\\\\";
553 escapes
["awk", "\"", escapes
["awk", "\""], "r"]=
"\\\\";
554 escapes
["awk", "\"", ++escapes
["awk", "\""], "s"]=
"\"";
555 escapes
["awk", "\"", escapes
["awk", "\""], "r"]=
"\\" "\"";
556 escapes
["awk", "\"", ++escapes
["awk", "\""], "s"]=
"\n";
557 escapes
["awk", "\"", escapes
["awk", "\""], "r"]=
"\\n";
558 modes
["awk", "'", "submodes"]=
"\\\\";
559 modes
["awk", "'", "terminators"]=
"'";
560 escapes
["awk", "'", ++escapes
["awk", "'"], "s"]=
"\\\\";
561 escapes
["awk", "'", escapes
["awk", "'"], "r"]=
"\\\\";
562 escapes
["awk", "'", ++escapes
["awk", "'"], "s"]=
"'";
563 escapes
["awk", "'", escapes
["awk", "'"], "r"]=
"\\" "'";
564 escapes
["awk", "'", ++escapes
["awk", "'"], "s"]=
"\n";
565 escapes
["awk", "'", escapes
["awk", "'"], "r"]=
"\\n";
566 modes
["awk", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
567 modes
["awk", "{", "delimiters"]=
" *, *";
568 modes
["awk", "{", "terminators"]=
"}";
569 modes
["awk", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
570 modes
["awk", "[", "delimiters"]=
" *, *";
571 modes
["awk", "[", "terminators"]=
"\\]";
572 modes
["awk", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
573 modes
["awk", "(", "delimiters"]=
" *, *";
574 modes
["awk", "(", "terminators"]=
"\\)";
575 modes
["awk", "", "submodes"] = modes
["awk", "", "submodes"] "|" "#";
576 modes
["awk", "#", "terminators"]=
"\n";
577 escapes
["awk", "#", ++escapes
["awk", "#"], "s"]=
"\n";
578 escapes
["awk", "#", escapes
["awk", "#"], "r"]=
"\n#";
579 modes
["awk", "", "submodes"] = modes
["awk", "", "submodes"] "|" "/\\^";
580 modes
["awk", "/^", "terminators"]=
"/";
581 modes
["perl", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[";
582 modes
["perl", "", "delimiters"]=
" *, *";
583 modes
["perl", "\\", "terminators"]=
".";
584 modes
["perl", "\"", "submodes"]=
"\\\\";
585 modes
["perl", "\"", "terminators"]=
"\"";
586 escapes
["perl", "\"", ++escapes
["perl", "\""], "s"]=
"\\\\";
587 escapes
["perl", "\"", escapes
["perl", "\""], "r"]=
"\\\\";
588 escapes
["perl", "\"", ++escapes
["perl", "\""], "s"]=
"\"";
589 escapes
["perl", "\"", escapes
["perl", "\""], "r"]=
"\\" "\"";
590 escapes
["perl", "\"", ++escapes
["perl", "\""], "s"]=
"\n";
591 escapes
["perl", "\"", escapes
["perl", "\""], "r"]=
"\\n";
592 modes
["perl", "'", "submodes"]=
"\\\\";
593 modes
["perl", "'", "terminators"]=
"'";
594 escapes
["perl", "'", ++escapes
["perl", "'"], "s"]=
"\\\\";
595 escapes
["perl", "'", escapes
["perl", "'"], "r"]=
"\\\\";
596 escapes
["perl", "'", ++escapes
["perl", "'"], "s"]=
"'";
597 escapes
["perl", "'", escapes
["perl", "'"], "r"]=
"\\" "'";
598 escapes
["perl", "'", ++escapes
["perl", "'"], "s"]=
"\n";
599 escapes
["perl", "'", escapes
["perl", "'"], "r"]=
"\\n";
600 modes
["perl", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
601 modes
["perl", "{", "delimiters"]=
" *, *";
602 modes
["perl", "{", "terminators"]=
"}";
603 modes
["perl", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
604 modes
["perl", "[", "delimiters"]=
" *, *";
605 modes
["perl", "[", "terminators"]=
"\\]";
606 modes
["perl", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
607 modes
["perl", "(", "delimiters"]=
" *, *";
608 modes
["perl", "(", "terminators"]=
"\\)";
609 modes
["perl", "", "submodes"] = modes
["perl", "", "submodes"] "|" "/\\*";
610 modes
["perl", "/*", "terminators"]=
"\\*/";
611 modes
["perl", "", "submodes"] = modes
["perl", "", "submodes"] "|" "#";
612 modes
["perl", "#", "terminators"]=
"\n";
613 escapes
["perl", "#", ++escapes
["perl", "#"], "s"]=
"\n";
614 escapes
["perl", "#", escapes
["perl", "#"], "r"]=
"\n#";
615 modes
["sh", "", "submodes"]=
"\\\\|\"|'|{|\\(|\\[|\\$\\(";
616 modes
["sh", "\\", "terminators"]=
".";
618 modes
["sh", "\"", "submodes"]=
"\\\\|\\$\\(";
619 modes
["sh", "\"", "terminators"]=
"\"";
620 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\\\\";
621 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\\\";
622 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\"";
623 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\" "\"";
624 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\n";
625 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\n";
627 modes
["sh", "'", "terminators"]=
"'";
628 escapes
["sh", "'", ++escapes
["sh", "'"], "s"]=
"'";
629 escapes
["sh", "'", escapes
["sh", "'"], "r"]=
"'\\'" "'";
630 modes
["sh", "$(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
631 modes
["sh", "$(", "delimiters"]=
" *, *";
632 modes
["sh", "$(", "terminators"]=
"\\)";
633 escapes
["sh", "$(", ++escapes
["sh", "$("], "tunnel"]=
"";
634 modes
["sh", "{", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
635 modes
["sh", "{", "delimiters"]=
" *, *";
636 modes
["sh", "{", "terminators"]=
"}";
637 modes
["sh", "[", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
638 modes
["sh", "[", "delimiters"]=
" *, *";
639 modes
["sh", "[", "terminators"]=
"\\]";
640 modes
["sh", "(", "submodes" ]=
"\\\\|\"|{|\\(|\\[|'|/\\*";
641 modes
["sh", "(", "delimiters"]=
" *, *";
642 modes
["sh", "(", "terminators"]=
"\\)";
643 modes
["sh", "", "submodes"] = modes
["sh", "", "submodes"] "|" "#";
644 modes
["sh", "#", "terminators"]=
"\n";
645 escapes
["sh", "#", ++escapes
["sh", "#"], "s"]=
"\n";
646 escapes
["sh", "#", escapes
["sh", "#"], "r"]=
"\n#";
647 escapes
["sh", "\"", ++escapes
["sh", "\""], "s"]=
"\\$";
648 escapes
["sh", "\"", escapes
["sh", "\""], "r"]=
"\\$";
649 modes
["make", "", "submodes"]=
" ";
650 escapes
["make", "", ++escapes
["make", ""], "s"]=
"\\$";
651 escapes
["make", "", ++escapes
["make", ""], "r"]=
"$$";
652 modes
["make", " ", "terminators"]=
"\\n";
653 escapes
["make", " ", ++escapes
["make", " "], "s"]=
"\\n";
654 escapes
["make", " ", ++escapes
["make", " "], "r"]=
"\n ";
661 Optind =
1 # skip ARGV[0]
662 while(getopt
(ARGC, ARGV, "R:LdT:hr")!=-1) {
663 if (Optopt ==
"R") root = Optarg
;
664 else if (Optopt ==
"r") root=
"";
665 else if (Optopt ==
"L") linenos =
1;
666 else if (Optopt ==
"d") debug =
1;
667 else if (Optopt ==
"T") tabs = indent_string
(Optarg
+0);
668 else if (Optopt ==
"h") help
();
669 else if (Optopt ==
"?") help
();
671 for (i=
1; i
<Optind
; i
++) { ARGV[i
]=
""; }
680 gsub("\\xE2\\x86\\xA6", "\x09");
684 gsub("\\xE2\\x80\\x98", "`");
688 if (match($
0, "^ *([^[ ]* |)<([^[ ]*)\\[[0-9]*\\][(](.*)[)].*, lang=([^ ]*)>", line
)) {
689 next_chunk_name=line
[2];
690 get_texmacs_chunk_args
(line
[3], next_chunk_params
);
691 gsub(ARG_SEPARATOR
",? ?", ";", line
[3]);
692 params =
"params=" line
[3];
694 params = params
",language=" line
[4]
696 get_tex_chunk_args
(params
, next_chunk_opts
);
697 new_chunk
(next_chunk_name
, next_chunk_opts
, next_chunk_params
);
698 texmacs_chunking =
1;
700 # warning(sprintf("Unexpected chunk match: %s\n", $_))
705 if (match($
0, "^\\\\Chunk{ *([^ ,}]*),?(.*)}", line
)) {
706 next_chunk_name = line
[1];
707 get_tex_chunk_args
(line
[2], next_chunk_opts
);
711 /^
\\begin{lstlisting
}|^
\\begin{Chunk
}/ {
712 if (match($
0, "}.*[[,] *name= *{? *([^], }]*)", line
)) {
715 new_chunk
(next_chunk_name
, next_chunk_opts
);
720 /^
*\
|____________
*/ && texmacs_chunking
{
725 /^
*\
|\
/\\/ && texmacs_chunking
{
731 /^
*[1-9][0-9]* *\
| / {
732 if (texmacs_chunking
) {
735 gsub("^ *[1-9][0-9]* *\\| ", "")
738 /^
*\.\
/\\/ && texmacs_chunking
{
741 /^
*__
*$
/ && texmacs_chunking
{
745 if (! texmacs_chunk
) {
746 # must be a texmacs continued line
752 # texmacs_chunking=0;
756 if (match($
0, "^[<]<(.*)[>]>= *$", line
)) {
763 /^
\\[e
]nd
{lstlisting
}|^
\\[e
]nd
{Chunk
}/ {
773 length(active_chunk
) {
779 while(match(chunk
,"(\xC2\xAB)([^\xC2\xBB]*) [^\xC2\xBB]*\xC2\xBB", line
) ||
781 "([=]<\\\\chunkref{([^}>]*)}(\\(.*\\)|)>|<<([a-zA-Z_][-a-zA-Z0-9_]*)>>)",
784 chunklet =
substr(chunk
, 1, RSTART - 1);
785 indent
+=
length(chunklet
);
786 chunk_line
(active_chunk
, chunklet
);
787 chunk =
substr(chunk
, RSTART + RLENGTH);
788 if (substr(line
[1], 1, 1) ==
"=") {
791 gsub("\\\\#", "#", line
[3]);
792 gsub("\\\\textbackslash{}", "\\", line
[3]);
793 gsub("\\\\\\^", "^", line
[3]);
794 chunk_include
(active_chunk
, line
[2] line
[3], indent
);
795 } else if (substr(line
[1], 1, 1) ==
"<") {
796 chunk_include
(active_chunk
, line
[4], indent
);
797 } else if (line
[1] ==
"\xC2\xAB") {
798 chunk_include
(active_chunk
, line
[2], indent
);
800 error
("Unknown chunk fragment: " line
[1]);
803 chunk_line
(active_chunk
, chunk
);
804 chunk_line
(active_chunk
, "\n");
808 print "------ chunk names "
809 output_chunk_names
();
810 print "====== chunks"
814 print a
"=" chunks
[a
];
818 if (length(root
)) output_chunk
(root
);
819 else output_chunk_names
();
821 function get_texmacs_chunk_args
(text
, args
, a
, done
) {
822 split(text
, args
, ARG_SEPARATOR
);
825 for (a=
1; (a in args
); a
++) if (a
>1) {
826 if (args
[a
] ==
"" || substr(args
[a
], 1, 1) ==
")") done=
1;
832 if (substr(args
[a
], 1, 2) ==
", ") args
[a
]=
substr(args
[a
], 3);
833 else if (substr(args
[a
], 1, 1) ==
",") args
[a
]=
substr(args
[a
], 2);
836 function get_tex_chunk_args
(text
, values
,
837 # optional parameters
838 path
, # hierarchical precursors
843 while(length(text
)) {
844 if (match(text
, "^ *}(.*)", a
)) {
847 if (!
match(text
, " *([^,=]*[^,= ]) *(([,=]) *(([^,}]*) *,* *(.*))|)$", a
)) {
852 if (substr(a
[4],1,1) ==
"{") {
853 text = get_tex_chunk_args
(substr(a
[4],2), values
, path name SUBSEP
);
855 values
[path name
]=a
[5];
859 values
[path name
]=
"";