From 71ea99d4c8b847766e48883219b38aa3bca74367 Mon Sep 17 00:00:00 2001 From: wl Date: Sat, 14 Feb 2009 17:21:35 +0000 Subject: Implement a leading spaces macro request, `lsm', in analogy to `blm'. Implement two new number registers, \n[lsn] and \n[lss], which hold the number of spaces and the horizontal space, respectively, which would be inserted if the macro registered by `lsm' wasn't called. * src/roff/troff/input.cpp (leading_spaces_macro_name, leading_spaces_number, leading_spaces_space): New global variables. (leading_spaces_macro): New function. (process_input_stack) : Handle `lsm'. (init_input_requests): Register `lsm', \n[lsn], and \n[lss]. * NEWS, man/groff_diff.man, man/groff.man, doc/groff.texinfo (Leading Spaces Traps): Document new requests and registers. --- ChangeLog | 18 ++++++++++++++++++ NEWS | 12 ++++++++++++ doc/groff.texinfo | 34 +++++++++++++++++++++++++++++++--- man/groff.man | 18 ++++++++++++++++++ man/groff_diff.man | 23 ++++++++++++++++++++++- src/roff/troff/input.cpp | 32 ++++++++++++++++++++++++++------ 6 files changed, 127 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 71846a97..76f8fe23 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2009-02-14 Werner LEMBERG + + Implement a leading spaces macro request, `lsm', in analogy to + `blm'. + Implement two new number registers, \n[lsn] and \n[lss], which hold + the number of spaces and the horizontal space, respectively, which + would be inserted if the macro registered by `lsm' wasn't called. + + * src/roff/troff/input.cpp (leading_spaces_macro_name, + leading_spaces_number, leading_spaces_space): New global + variables. + (leading_spaces_macro): New function. + (process_input_stack) : Handle `lsm'. + (init_input_requests): Register `lsm', \n[lsn], and \n[lss]. + + * NEWS, man/groff_diff.man, man/groff.man, doc/groff.texinfo + (Leading Spaces Traps): Document new requests and registers. + 2009-02-10 Denis M. Wilson * man/groff.man: Document missing number registers (`$$', `.b', diff --git a/NEWS b/NEWS index e2f2d660..6b8d432d 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,18 @@ This file describes recent user-visible changes in groff. Bug fixes are not described. There are more details in the man and info pages. +VERSION 1.20.2 +============== + +Troff +----- + +o The new `lsm' request specifies a macro to be invoked when leading spaces + in an input line are encountered (which are removed then). Number + registers `lsn' and `lss' hold the number of removed leading spaces and + the corresponding horizontal space, respectively. + + VERSION 1.20.1 ============== diff --git a/doc/groff.texinfo b/doc/groff.texinfo index 1ff314c2..a6b26cc8 100644 --- a/doc/groff.texinfo +++ b/doc/groff.texinfo @@ -4751,9 +4751,11 @@ with the blank line macro request @code{blm}. @xref{Blank Line Traps}. @cindex fill mode @cindex mode, fill +@cindex leading spaces macro (@code{lsm}) A line that begins with a space causes a break and the space is output at the beginning of the next line. Note that this space isn't adjusted, -even in fill mode. +even in fill mode; however, the behaviour can be modified with the +leading spaces macro request @code{lsm}. @xref{Leading Spaces Traps}. The end of file also causes a break -- otherwise the last line of the document may vanish! @@ -12043,6 +12045,7 @@ executed. * Diversion Traps:: * Input Line Traps:: * Blank Line Traps:: +* Leading Spaces Traps:: * End-of-input Traps:: @end menu @@ -12338,7 +12341,7 @@ of already processed lines. @c --------------------------------------------------------------------- -@node Blank Line Traps, End-of-input Traps, Input Line Traps, Traps +@node Blank Line Traps, Leading Spaces Traps, Input Line Traps, Traps @subsection Blank Line Traps @cindex blank line traps @cindex traps, blank line @@ -12351,7 +12354,32 @@ encounters a blank line in the input file. @c --------------------------------------------------------------------- -@node End-of-input Traps, , Blank Line Traps, Traps +@node Leading Spaces Traps, End-of-input Traps, Blank Line Traps, Traps +@subsection Leading Spaces Traps +@cindex leading spaces traps +@cindex traps, leading spaces + +@DefreqList {lsm, macro} +@DefregItem {lsn} +@DefregListEnd {lss} +@cindex leading spaces macro (@code{lsm}) +Set a leading spaces trap. @code{gtroff} executes @var{macro} when it +encounters leading spaces in an input line; the implicit line break +which normally happens in this case is suppressed. A line consisting +of spaces only, however, is treated as an empty line, possibly subject +to an empty line macro set with the @code{blm} request. + +Leading spaces are removed from the input line before calling the +leading spaces macro. The number of removed spaces is stored in +register @code{lsn}; the horizontal space which would be emitted if +there was no leading space macro is stored in register @code{lss}. +Note that @code{lsn} and @code{lss} are available even if no leading +space macro has been set. +@endDefreq + +@c --------------------------------------------------------------------- + +@node End-of-input Traps, , Leading Spaces Traps, Traps @subsection End-of-input Traps @cindex end-of-input traps @cindex traps, end-of-input diff --git a/man/groff.man b/man/groff.man index 4e59c25b..a63961ca 100644 --- a/man/groff.man +++ b/man/groff.man @@ -1873,6 +1873,15 @@ default scaling indicator\~\c .scaleindicator m ). . .TPx +.REQ .lsm +Unset the leading spaces macro. +. +.TPx +.REQ .lsm "macro" +Set the leading spaces macro to +.IR macro . +. +.TPx .REQ .ls Change to the previous value of additional intra-line skip. . @@ -3902,6 +3911,15 @@ image (set by Output line number. . .TPx +.REG lsn +The number of leading spaces of an input line. +. +.TPx +.REG lss +The horizontal space corresponding to the leading spaces of an input +line. +. +.TPx .REG minutes The number of minutes after the hour. . diff --git a/man/groff_diff.man b/man/groff_diff.man index 7f39a50a..dcd70a0d 100644 --- a/man/groff_diff.man +++ b/man/groff_diff.man @@ -1767,6 +1767,19 @@ read-only number register is set to\~1 if in line-tabs mode, and 0 otherwise. . .TP +.BI .lsm\ xx +Set the leading spaces macro to +.IR xx . +If there are leading spaces in an input line, it is invoked instead of +the usual troff behaviour; the leading spaces are removed. +Registers +.B \[rs]n[lsn] +and +.B \[rs]n[lss] +hold the number of removed leading spaces and the corresponding +horizontal space, respectively. +. +.TP .BI .mso\ file The same as the .B so @@ -2900,7 +2913,7 @@ Zero if no magnification. .B \[rs]n[urx] .TQ .B \[rs]n[ury] -These four registers are set by the +These four read/\:write registers are set by the .B psbb request and contain the bounding box values (in PostScript units) of a given PostScript image. @@ -2952,6 +2965,14 @@ Initialized at start-up. The current horizontal position at input line. . .TP +.B \[rs]n[lsn] +.TQ +.B \[rs]n[lss] +If there are leading spaces in an input line, these registers +hold the number of leading spaces and the corresponding +horizontal space, respectively. +. +.TP .B \[rs]n[minutes] The number of minutes after the hour. . diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp index 8038f70f..b3243f5a 100644 --- a/src/roff/troff/input.cpp +++ b/src/roff/troff/input.cpp @@ -101,6 +101,7 @@ static void disable_warning(const char *); static int escape_char = '\\'; static symbol end_macro_name; static symbol blank_line_macro_name; +static symbol leading_spaces_macro_name; static int compatible_flag = 0; int ascii_output_flag = 0; int suppress_output_flag = 0; @@ -120,7 +121,7 @@ double spread_limit = -3.0 - 1.0; // negative means deactivated double warn_scale; char warn_scaling_indicator; -int debug_state = 0; // turns on debugging of the html troff state +int debug_state = 0; // turns on debugging of the html troff state search_path *mac_path = &safer_macro_path; @@ -2608,6 +2609,12 @@ void blank_line_macro() skip_line(); } +void leading_spaces_macro() +{ + leading_spaces_macro_name = get_name(); + skip_line(); +} + static void trapping_blank_line() { if (!blank_line_macro_name.is_null()) @@ -2773,6 +2780,9 @@ int hmotion_node::reread(int *) return 0; } +static int leading_spaces_number = 0; +static int leading_spaces_space = 0; + void process_input_stack() { int_stack trap_bol_stack; @@ -2853,11 +2863,12 @@ void process_input_stack() do { node *n; cc = get_copy(&n); - if (cc != EOF) + if (cc != EOF) { if (cc != '\0') curdiv->transparent_output(transparent_translate(cc)); else curdiv->transparent_output(n); + } } while (cc != '\n' && cc != EOF); if (cc == EOF) curdiv->transparent_output('\n'); @@ -2919,9 +2930,15 @@ void process_input_stack() trapping_blank_line(); else { push_token(tok); - curenv->do_break(); - curenv->add_node(new hmotion_node(space_width * nspaces, - curenv->get_fill_color())); + leading_spaces_number = nspaces; + leading_spaces_space = space_width.to_units() * nspaces; + if (!leading_spaces_macro_name.is_null()) + spring_trap(leading_spaces_macro_name); + else { + curenv->do_break(); + curenv->add_node(new hmotion_node(space_width * nspaces, + curenv->get_fill_color())); + } bol = 0; } } @@ -4080,7 +4097,7 @@ void spring_trap(symbol nm) postponed_trap = nm; return; } - static char buf[2] = { BEGIN_TRAP, 0 }; + static char buf[2] = { BEGIN_TRAP, '\0' }; static char buf2[2] = { END_TRAP, '\0' }; input_stack::push(make_temp_iterator(buf2)); request_or_macro *p = lookup_request(nm); @@ -7812,6 +7829,7 @@ void init_input_requests() init_request("ig", ignore); init_request("length", length_request); init_request("lf", line_file); + init_request("lsm", leading_spaces_macro); init_request("mso", macro_source); init_request("nop", nop_request); init_request("nroff", nroff_request); @@ -7879,6 +7897,8 @@ void init_input_requests() number_reg_dictionary.define("c.", new writable_lineno_reg); number_reg_dictionary.define("llx", new variable_reg(&llx_reg_contents)); number_reg_dictionary.define("lly", new variable_reg(&lly_reg_contents)); + number_reg_dictionary.define("lsn", new variable_reg(&leading_spaces_number)); + number_reg_dictionary.define("lss", new variable_reg(&leading_spaces_space)); number_reg_dictionary.define("opmaxx", new variable_reg(&output_reg_maxx_contents)); number_reg_dictionary.define("opmaxy", -- cgit v1.2.1