[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Groff] fetching a register's numeric value
From: |
Ted Harding |
Subject: |
Re: [Groff] fetching a register's numeric value |
Date: |
Fri, 31 Aug 2012 10:10:06 +0100 (BST) |
On 31-Aug-2012 06:25:26 Dave Kemper wrote:
> Hi,
> Is there a way to fetch the numeric value of a register regardless
> of what output format is currently associated with the register?
>
> The only method I've been able to figure out is:
>
> .ds reigning_format \g%
> .af % 0
> .nr page_number \n[%]
> .af % \*[reigning_format]
>
> to save the format, switch it to numeric, fetch the value, and then
> switch the format back to its saved status. Then I can use
> \n[page_number] when I need the numeric version of the page number
> (in calculations,for instance). This works, but is not very elegant.
>
> The existence of a better mechanism is implied by the .af documentation:
> "Changing the output format of a read-only register causes an error.
> It is necessary to first copy the register's value to a writeable
> register, then apply the `af' request to this other register."
> Unfortunately, no hint is provided on how one might copy the register's
> value in a case where the register's output format cannot be changed.
> The intuitive solution -- using .nr -- fails when the output format is
> "i", generating the message:
>
> warning: numeric expression expected (got `i')
>
> (or "got `v'" or "got `x'", depending on the register's current value)
Given that when groff enounters (e.g.) \n% it substitutes at that
point the value of \n% in its current ouitput format, I think that
your "not very elegantg" solution (or equivalent) is the way to do it.
However, you can sweep the inelegance under the carpet by defining
a macro. For example, the following:
.nr % 9
\n% \g% \" To display default format (Arabic)
.br
.af % i
\n% \g% \" To display new format (l.c. Roman)
.br
.de get% \" Defines macro "get%"
.ds rf \\g%
.af % 1
.nr \\$1 \\n%
.af % \\*[rf]
..
.get% X
\n[X] \g[X] \n% \g% \" To display final and original formats
produces the result:
9 0
ix i
9 0 ix i
And you could extend the macro to get the value from some register
other than \n%, using a second argument, e.g.
.de getnum
.ds rf \\g[\\$1]
.af \\$1 1
.nr \\$2 \\n[\\$1]
.af \\$1 \\*[rf]
..
so that ".getnum reg1 reg2" would put the numerical value of reg1
into reg2. Thus
.getnum % Y
\n[Y] \g[Y] \n% \g%
produces the same result
9 0 ix i
as before.
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Date: 31-Aug-2012 Time: 10:10:01
This message was sent by XFMail
-------------------------------------------------