Printing topic text in PunyInform

I’d like to print the topic for Read/Consult, but can’t figure out how to do this.

There was a post about this at Ask: with several words , but either that solution no longer works or I’m doing something wrong in my program.

Here’s my code:

Object -> MonkGuide "The History of the Monks of Our Lady of Thorns",
  class AdjObject,
  with
    adj_name 'monks',
    noun_name 'history' 'book',
    description "A small book listing the monks who have served in the priory,
      along with some information on them.",
    before [ i ;
      Consult:
        if (second == nothing) {
          print "cf=", consult_from, ", cw=", consult_Words, "^";
          print "You find no entry for ~";
          for(i = 0 : i < consult_words : i++) {
            print (address) WordValue(consult_from + i), " ";
          }
          "~ in ", (the) self, ".";
        }
        print_ret (string) second.history_text, "^";
    ],
  has
    proper;

The debug for cw= and cf= print sane values given the input “read about foo bar baz in book”, but it prints total junk.

Any thoughts would be much appreciated

(further playing: that solution does work if the topic is made up of dictionary words, but I’d it to work for the raw text of the topic)

Instead, do this:

last_word = consult_from + consult_words - 1;
end = WordAddress(last_word) + WordLength(last_word);
for(i = WordAddress(consult_from) : i < end : i++)
  print (char) i->0;
1 Like

Works perfectly. Thanks, @fredrik !

1 Like

Another way could be to define your own printing routine, which checks for dictionary words and spells them out when unknown. For example:

[ raw_word pos wd len;
    wd = WordValue (pos);
    if (wd == 0) {
        len = WordLength (pos);
        pos = WordAddress (pos);
        for ( : len > 0 : len--, pos++)
            print (char) pos->0;
    } else {
        print (address) wd;
    }
];

[ TestSub i;
    print ">> ";
    for (i = 0 : i < consult_words : i++)
        print (raw_word) consult_from + i, " ";
    print "<< ";
];

Verb 'test'
    * topic -> Test
;

The drawback is that any dictionary words are then truncated to the first n characters, where n is between 1 and 9, depending on Z-code version, custom alphabet table (if any), and the specific characters of the word.

Oh yeah, completely forgot about that.

1 Like