From bba85a010c42f4aa0a954bc8425e206916bd9873 Mon Sep 17 00:00:00 2001 From: Sam Liddicott Date: Thu, 3 Dec 2009 20:38:53 +0000 Subject: [PATCH] Add support for append= on chunk definitions It saves you having to keep declaring a 1 line of a chunk merely to add another line --- newfangle | 27 +++++++--- newfangle.lyx | 161 +++++++++++++++++++++++++++++++++++++++++++++++-------- newfangle.module | 1 + 3 files changed, 161 insertions(+), 28 deletions(-) diff --git a/newfangle b/newfangle index 88fce2f..4bbd91a 100644 --- a/newfangle +++ b/newfangle @@ -71,8 +71,12 @@ function getopt(argc, argv, options, thisopt, i) function error(message) { - print message > "/dev/stderr"; + print "ERROR: " FILENAME ":" FNR " " message > "/dev/stderr"; exit 1; +}function warning(message) +{ + print "WARNING: " FILENAME ":" FNR " " message > "/dev/stderr"; + warnings++; } function new_mode(language, mode) { mode["language"] = language; @@ -80,11 +84,10 @@ function new_mode(language, mode) { } function new_chunk(chunk_name, params, # local vars - p ) + p, append ) { # HACK WHILE WE CHANGE TO ( ) for PARAM CHUNKS gsub("\\(\\)$", "", chunk_name); - active_chunk = chunk_name; if (! (chunk_name in chunk_names)) { if (debug) print "New chunk " chunk_name; chunk_names[chunk_name]; @@ -92,6 +95,15 @@ function new_chunk(chunk_name, params, chunks[chunk_name, p] = params[p]; } } + if ("append" in params) { + append=params["append"]; + if (! (append in chunk_names)) { + warning("Chunk " chunk_name " is appended to chunk " append " which is not defined yet"); + new_chunk(append); + } + chunk_include(append, chunk_name); + } + active_chunk = chunk_name; prime_chunk(chunk_name); } @@ -173,7 +185,7 @@ function write_chunk(chunk_name, indent, tail, lineno_needed++; } } - + chunklet = chunks[chunk_name, "part", part]; if (chunks[chunk_name, "part", part, "type"] == part_type_chunk) { if (match(chunklet, "^([^\\[]*)\\((.*)\\)$", chunklet_parts)) { @@ -201,12 +213,12 @@ function write_chunk(chunk_name, indent, tail, print "#line " lineno " \"" filename "\"\n" lineno_needed = 0; } - + text = chunks[chunklet, frag]; /* check params */ text = expand_chunk_args(text, chunk_params, chunk_args); - + if (text == "\n") { lineno++; if (part == max_part && frag == max_frag && length(chunk_path)) { @@ -219,7 +231,7 @@ function write_chunk(chunk_name, indent, tail, if (newline) text = indent text; newline = 0; } - + text = text tail; # track_mode(mode, text); print text; @@ -355,6 +367,7 @@ function get_chunk_args(text, values, # local vars a, name) { + split("", next_chunk_args); while(length(text)) { if (match(text, "^ *}(.*)", a)) { return a[1]; diff --git a/newfangle.lyx b/newfangle.lyx index 3c39a79..a76535f 100644 --- a/newfangle.lyx +++ b/newfangle.lyx @@ -10,7 +10,8 @@ basicstyle=\footnotesize\ttfamily, %keywordstyle=\color{darkgreen}, numberstyle=\tiny,language=C,columns=fullflexible, -numberfirstline=true} +numberfirstline=true +} \end_preamble \use_default_options true \begin_modules @@ -166,8 +167,8 @@ Noweb \noun default generally brought literate programming to the masses through removing some of the complexity of the original literate programming, but this would - be of no advantage to me if the LyX --- LaTeX combination brought more - complications in their place. + be of no advantage to me if the LyX / LaTeX combination brought more complicati +ons in their place. \end_layout \begin_layout Standard @@ -177,7 +178,7 @@ Newfangle \noun default was thus born --- as an awk replacement for notangle, adding some important features, like better integration with LyX and LaTeX, multiple output format - conversions, and fixing notangle bugs like indenting when using -L for + conversions, and fixing notangle bugs like indentation when using -L for line numbers. \end_layout @@ -249,7 +250,7 @@ when a parameterized chunk is included as well as the #line emission, say \begin_layout Enumerate with 2 macro expansions on one line ${} ${} the first is too greedy and - looks to the wrong } + looks to the final } \end_layout \begin_layout Enumerate @@ -3098,7 +3099,7 @@ Fatal errors are issued with the error function: \end_layout \begin_layout Chunk -error() +error(),append=helper-functions \end_layout \begin_layout Standard @@ -3118,7 +3119,7 @@ function error(message) \begin_layout Plain Layout - print message > "/dev/stderr"; + print "ERROR: " FILENAME ":" FNR " " message > "/dev/stderr"; \end_layout \begin_layout Plain Layout @@ -3136,12 +3137,8 @@ function error(message) \end_layout -\begin_layout Standard -This is one of the helper functions. -\end_layout - \begin_layout Chunk -helper-functions +warning(),append=helper-functions \end_layout \begin_layout Standard @@ -3151,9 +3148,27 @@ status open \begin_layout Plain Layout -=< -\backslash -chunkref{error()}> +function warning(message) +\end_layout + +\begin_layout Plain Layout + +{ +\end_layout + +\begin_layout Plain Layout + + print "WARNING: " FILENAME ":" FNR " " message > "/dev/stderr"; +\end_layout + +\begin_layout Plain Layout + + warnings++; +\end_layout + +\begin_layout Plain Layout + +} \end_layout \end_inset @@ -3534,6 +3549,11 @@ status open \begin_layout Plain Layout + split("", next_chunk_args); +\end_layout + +\begin_layout Plain Layout + while(length(text)) { \end_layout @@ -8823,6 +8843,24 @@ params \end_layout \begin_layout Chunk +helper-functionsx +\end_layout + +\begin_layout Standard +\begin_inset listings +inline false +status open + +\begin_layout Plain Layout + +\end_layout + +\end_inset + + +\end_layout + +\begin_layout Chunk chunk-storage-functions \end_layout @@ -8843,7 +8881,7 @@ function new_chunk(chunk_name, params, \begin_layout Plain Layout - p ) + p, append ) \end_layout \begin_layout Plain Layout @@ -8871,11 +8909,6 @@ function new_chunk(chunk_name, params, \begin_layout Plain Layout - active_chunk = chunk_name; -\end_layout - -\begin_layout Plain Layout - if (! (chunk_name in chunk_names)) { \end_layout @@ -8911,6 +8944,52 @@ function new_chunk(chunk_name, params, \begin_layout Plain Layout + if ("append" in params) { +\end_layout + +\begin_layout Plain Layout + + append=params["append"]; +\end_layout + +\begin_layout Plain Layout + + if (! (append in chunk_names)) { +\end_layout + +\begin_layout Plain Layout + + warning("Chunk " chunk_name " is appended to chunk " append " which + is not defined yet"); +\end_layout + +\begin_layout Plain Layout + + new_chunk(append); +\end_layout + +\begin_layout Plain Layout + + } +\end_layout + +\begin_layout Plain Layout + + chunk_include(append, chunk_name); +\end_layout + +\begin_layout Plain Layout + + } +\end_layout + +\begin_layout Plain Layout + + active_chunk = chunk_name; +\end_layout + +\begin_layout Plain Layout + prime_chunk(chunk_name); \end_layout @@ -10784,6 +10863,46 @@ newfangle@chunk@params{#1}}% \end_layout +\begin_layout Standard +As it is common to define a chunk which then needs appending to another + chunk, and annoying to have to declare a single line chunk to manage the + include, we support an +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +append= +\end_layout + +\end_inset + + option. + +\end_layout + +\begin_layout Standard +\begin_inset listings +inline false +status open + +\begin_layout Plain Layout + + +\backslash +lst@Key{append} +\backslash +relax{ +\backslash +def +\backslash +newfangle@chunk@params{#1}}% +\end_layout + +\end_inset + + +\end_layout + \begin_layout Subsection The noweb styled caption \end_layout diff --git a/newfangle.module b/newfangle.module index 52f7bd8..b02573a 100644 --- a/newfangle.module +++ b/newfangle.module @@ -31,6 +31,7 @@ AddToPreamble }% \def\lst@chunkname{\empty}% \lst@Key{params}\relax{\def\newfangle@chunk@params{#1}}% +\lst@Key{append}\relax{\def\newfangle@chunk@params{#1}}% \def\newfanglecaption{\protect\newfangle@caption}% \newcounter{newfangle@chunkcounter}% \def\newfangle@caption{% -- 2.11.4.GIT