More MF.Fun WORD-RIGHT and WORD-LEFT 
Author Message
 More MF.Fun WORD-RIGHT and WORD-LEFT

Quote:

> : WORD-RIGHT ( -- )
>    RIGHT SPOT-TEXT-WS?
>    BEGIN
>            RIGHT SPOT-TEXT-WS? XOR
>            -DOC-END 0= OR
>    UNTIL

        DROP  \ or each word-right leaves an extra flag behind

Quote:
>    SPOT-TEXT-WS? 0= IF LEFT THEN
> ;

> : WORD-LEFT ( -- )
>    LEFT SPOT-TEXT-WS?
>    BEGIN
>            LEFT SPOT-TEXT-WS? XOR

>    UNTIL

        DROP \ or each word-left leaves a flag behind

Quote:
>    SPOT-TEXT-WS? 0= IF RIGHT THEN
> ;

        win32forth alerted me to this when I was checking this for
portability between gforth and win32forth.

Virtually,

Bruce R. McFarling, Newcastle, NSW



Fri, 07 Apr 2000 03:00:00 GMT  
 More MF.Fun WORD-RIGHT and WORD-LEFT

Quote:

> CREATE TEXT-WS BL , 'CR , HT , FF , 0 ,

> ( I know that this breaks if a char is bigger than a cell -- can a char be
> bigger than a cell?)


Quote:
> ... later that same MF.F file

> : SPOT-TEXT-WS? ( -- flag )

>         BEGIN

>                 /CELL +
>         REPEAT

> ;

Why not just

: SPOT-TEST-WS?  ( -- flag)  \ WHITE-SPOT?

/CELL + is CELL+

Consider make this true if not white space: -WHITE

Quote:

> : WORD-RIGHT ( -- )
>         RIGHT SPOT-TEXT-WS?
>         BEGIN
>                 RIGHT SPOT-TEXT-WS? XOR
>                 -DOC-END 0= OR
>         UNTIL
>         SPOT-TEXT-WS? 0= IF LEFT THEN
> ;

More likely: SPOT-TEXT-WS 0=  -DOC-END AND IF LEFT THEN

Quote:

> : WORD-LEFT ( -- )
>         LEFT SPOT-TEXT-WS?
>         BEGIN
>                 LEFT SPOT-TEXT-WS? XOR

>         UNTIL
>         SPOT-TEXT-WS? 0= IF RIGHT THEN
> ;


In WORD-LEFT and WORD-RIGHT ,

         ...
         BEGIN DUP
         ...
         UNTIL DROP
         ...

Leo Wong
--

http://www.albany.net/~hello/



Fri, 07 Apr 2000 03:00:00 GMT  
 More MF.Fun WORD-RIGHT and WORD-LEFT

        When we last looked in on our protagonist, yours truly, I was
looking to get a WORD-RIGHT and WORD-LEFT function.  I had this slated to
go into U and O as command keys.

        What I want is for the word right and left to jump from space to
space, and skip over the middle whitespace in strings of whitespace.  That
puts me right where I want to 'wedge', especially since it seems as if the
Win32Forth accept trims trailing blanks, so if I 'wedge' when sitting on
top of the start of a word, I get a run on.  I'm enough of a writer to
want my run-ons to be deliberate.
        This can be summarizes as:

(1) go once in the desired direction, and see if that's white space
(2) go again, and see if that is in the same class as the previous char
(3) repeat 2 if it was the same type (either white-space or not)
(4) if we ended up on a non-ws, (skimmed over a string of ws), go
backward on step

        And so this is what I implemented.

CREATE TEXT-WS BL , 'CR , HT , FF , 0 ,

( I know that this breaks if a char is bigger than a cell -- can a char be
bigger than a cell?)

... later that same MF.F file

: SPOT-TEXT-WS? ( -- flag )

        BEGIN

                /CELL +
        REPEAT

;

... still later, once RIGHT and LEFT have been defined

: WORD-RIGHT ( -- )
        RIGHT SPOT-TEXT-WS?
        BEGIN RIGHT SPOT-TEXT-WS? XOR UNTIL
        SPOT-TEXT-WS? 0= IF LEFT THEN
;

and similarly for WORD-LEFT.

        It worked OK, *except* when I added WORD-LEFT and then got up to
the front of the doc.  It went to never never land.  Control-C and look at
the source, it soon became obvious why -- LEFT and RIGHT *stall* when
they are attempted at the beginning and end of file, respectively.  SO
at that boundary condition, the status transition never takes place, and
we have an infinite loop.  So this has to be taken account of:

: WORD-RIGHT ( -- )
        RIGHT SPOT-TEXT-WS?
        BEGIN
                RIGHT SPOT-TEXT-WS? XOR
                -DOC-END 0= OR
        UNTIL
        SPOT-TEXT-WS? 0= IF LEFT THEN
;

: WORD-LEFT ( -- )
        LEFT SPOT-TEXT-WS?
        BEGIN
                LEFT SPOT-TEXT-WS? XOR

        UNTIL
        SPOT-TEXT-WS? 0= IF RIGHT THEN
;

        An advantage is that since they are reflections of each other, and
both stop at the "starting" white-sapce and, for multi-white-space
strings, the "end" of whitespace, you halt at the same spot from either
direction.

        If you want to halt at the first char and/or the first
white-space, drop the IF ... THEN in the WORD-RIGHT, and replace it with
RIGHT in the WORD-LEFT.

Virtually,

Bruce R. McFarling, Newcastle, NSW



Fri, 07 Apr 2000 03:00:00 GMT  
 More MF.Fun WORD-RIGHT and WORD-LEFT


Quote:

> > CREATE TEXT-WS BL , 'CR , HT , FF , 0 ,

> > ( I know that this breaks if a char is bigger than a cell -- can a char be
> > bigger than a cell?)



        At the time I wrote it originally, I didn't have the pseudo
standard at hand, and I couldn't recall if C, was standard or just a
common extension.

Quote:

> > ... later that same MF.F file

> > : SPOT-TEXT-WS? ( -- flag )

> >         BEGIN

> >                 /CELL +
> >         REPEAT

> > ;

> Why not just

> : SPOT-TEST-WS?  ( -- flag)  \ WHITE-SPOT?


        Because (a) this works and (b) it continues working for whatever
whitespace definition is thrown at it.  I may *want* the cursor to stop at
an unprintable character that doesn't happen to be what I am thinking of
as whitespace at the time.  And I want it to be open to having whitespace
up in the area above the end of the standard printable character subset of
ASCII -- for example, I may or may not wish to slide over embbedded 8-bit
extended ANSI control codes,  and this leaves it open.

Quote:
> /CELL + is CEL

        Thanks.  Last time I wrote hunks of Forth at a time, CELL+ didn't
exist, so I was vague (again, writing in the shuttle van between campuses)
whether it was 1 CELLS + or CELLS +

Quote:
> Consider make this true if not white space: -WHITE

> > : WORD-RIGHT ( -- )
> >         RIGHT SPOT-TEXT-WS?
> >         BEGIN
> >                 RIGHT SPOT-TEXT-WS? XOR
> >                 -DOC-END 0= OR
> >         UNTIL
> >         SPOT-TEXT-WS? 0= IF LEFT THEN
> > ;
> More likely: SPOT-TEXT-WS 0=  -DOC-END AND IF LEFT THEN

        Yeah, I've noticed that in use.  I just can't figure out whether
its a bug or a feature.  I mean, if there is no 'next white space side of
the edge between white-space and non-white-space', as written it goes to
the *last* 'white-space side of an edge'.  Which makes it really
behavioral.  After all, if you want to go to the *end*, you can get there
any number of ways.
        Its a kind of an cute behavior, but I'll work with it a while to
see if it is a useful cute behavior, or an annoying one.

        BTW, just drop the last SPOT-WS? (I've shortened the name) after
the UNTIL: the TUCK already put it on the stack. DROP SPOT-WS? is
therefore busy work.

 > >

- Show quoted text -

Quote:
> > : WORD-LEFT ( -- )
> >         LEFT SPOT-TEXT-WS?
> >         BEGIN
> >                 LEFT SPOT-TEXT-WS? XOR

> >         UNTIL
> >         SPOT-TEXT-WS? 0= IF RIGHT THEN
> > ;


> In WORD-LEFT and WORD-RIGHT ,

>          ...
>          BEGIN DUP
>          ...
>          UNTIL DROP
>          ...

        So,
        BEGIN RIGHT SPOT-WS? TUCK XOR
                ...
        UNTIL 0= IF

and, like the amendment before (*but without the busy work*) it works for
longer than 2 strings, as well as not leaving a pile of debris behind on
the stack when used repeatedly. (oops!)

        Thanks, Leo

Virtually,

Bruce R. McFarling, Newcastle, NSW



Mon, 10 Apr 2000 03:00:00 GMT  
 More MF.Fun WORD-RIGHT and WORD-LEFT

        Here's a handy little function for MF when using it in
command mode -- note that this is from an earlier version, so
I'm using "IN-KEY" instead of the more recent vectored key handler.  
Modify to suit & your mileage may vary:

\ in definitions of command keys

CHAR ^ CONSTANT ^EmbedChar(1)
CHAR 6 CONSTANT ^EmbedChar(2)

\ just after the definitions of the "3 WAYS" functions, which includes
\ CHARACTER:

: EMBED-CHAR ( -- )
        IN-KEY CHARACTER
;

\ then in the COMMAND-HANDLER

        ^EmbedChar(1)  OF EMBED-CHAR ENDOF
        ^EmbedChar(2)  OF EMBED-CHAR ENDOF

And no more annoying "Wedge Character Return" for single character
tasks, like slipping a "\" in front of comments after adding more
detail.  Haven't thought through, yet, what to do about command
characters: whether to slip this over to another number and make
the "^" provide the ASCII command characters.

Virtually,

Bruce R. McFarling, Newcastle, NSW



Sun, 23 Apr 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. left / right long word roll with MMX?

2. LEAVING OUT WORDS

3. How to replace one or two words with one word with one line of awk code

4. unsigned word to signed word in RB

5. Word 97 and word 2000 - calling from clarion - HELP

6. Word by word cross reference

7. Class(y) 2.4b, with documentation for MS-Word 97, MS-Word 6.0 and WordPerfect 7.0

8. My final word on word size

9. Adding a signed word to unsigned word...

10. copy replacing part of words, instead of complete word

11. Using MF cobol with ole/api to access MS Word

12. Right, left, Right.

 

 
Powered by phpBB® Forum Software