Home   Archive   Permalink

Capturing REBOL help

Building on Nick's recent efforts with appbuilder, I want to write a little program to display function help in a text face in a window, instead of in the console. To do that, I imagine I would issue the 'help (function-name)' command in a script and capture the output. Capturing the output is the question.
I see through 'source help' that the 'help' function uses the 'print' function to display help. The only way I can think of to capture that output would be to redefine the 'print' function temporarily, as shown in the sample below. Is that the best way, or is there another trick for the redirection of output?
Thank you. Sample follows.
print: func [
] [
     FTEXT: copy ''
     FTEXT: copy PTEXT
;; -- Put FTEXT in a text face in a window...

posted by:   Steven White       13-Feb-2017/16:50:22-8:00

You can use the 'echo function to capture output in the console:
echo %file
help print
echo off
print read %file
But that's not the best way to do what you want.
I just posted the help.r script, which is included in appbuilder.r It has this code, to provide help for all functions in Rebol:
w: copy [] colors: copy [] foreach i copy first system/words [
    attempt [if any-function? get to-word i [append w i]]
view layout[text-list 150x440 data sort w [x: to-word value ? :x]]
I'll post again when I have some more time about how make your own version of the help function.
For now, you can play with the help function source:
editor mold :help

posted by:   Nick       13-Feb-2017/17:56:38-8:00

Thank you. I was using your code as a model. I have the function names in a text-list, and now I want to obtain the help on a selected word. In your code, it appears that the "? :x" obtains help for the contents of the word "x" which is the word selected from the list. But "?" opens a console window to display the help, and I am trying to intercept that output so I don't open a console window.    
Maybe instead of making a temporary "print" function I should make a temporary "help" function, and replace all "print" or "prin" with code to append to a text string.

posted by:   Steven White       13-Feb-2017/18:04:45-8:00

I wrote a HELP replacement function that returns a string instead of printing:
Here's the same script colourised:

posted by:   Chris       13-Feb-2017/18:29:49-8:00

A super quick hack that can be much improved, but you get the idea:
helptext: copy {}
help2: :help
foreach [orig new] [
    {print} {append helptext join newline } {prin} {append helptext join " "} {tab} {#"^-"}
][help2: do replace/all mold :help2 orig new]
help2 print
editor helptext

posted by:   Nick       13-Feb-2017/21:04:09-8:00

(newlines and spaces need to go after the printed values, use parse to do the replacement)

posted by:   Nick       13-Feb-2017/21:26:37-8:00

Or just use Chris' function, it's great.

posted by:   Nick       13-Feb-2017/22:14:14-8:00

Chris, thank you for that function. I actually did not use it because it looked complicated. So for the sake of my training I went ahead and extracted the source for "help" and modified it to return a string by changing all the PRIN*" functions to "append". THEN, I went back and looked at your function, and it all looked very familiar. A big benefit of the exercise was learning about the "collect" and "keep" functions which I never had heard of and which are NOT documented in the REBOL 2 function dictionary. Your solution is much smoother than my brute force attack.

posted by:   Steven White       15-Feb-2017/10:10:15-8:00

I used the Rebol function as a base. I'd started on the path to refactoring it with the print->accumulator in place, but only got so far.
COLLECT/KEEP are superlatively useful functions that help rationalise a whole bunch of data transformation operations. Here's a fun one:
     odds: [1 2 3 4 5 6 7 8]
     probe evens: collect [
         remove-each num odds [if even? num [keep num]]
     probe odds
It is documented as a Rebol 3 function though it is available from (I think) Rebol 2.7.7.

posted by:   Chris       15-Feb-2017/16:46:58-8:00

Steve, many of the new functions in R3 have been back-ported to the latest releases of R2 (in an effort called R2-forward). It can help to look at the R3 docs, or just browse through the help script above. Doing that, I found some new functions which I didn't know existed in R2.

posted by:   Nick       17-Feb-2017/11:18:50-8:00