Re: word wrap?

new topic     » goto parent     » topic index » view thread      » older message » newer message

The wordwrap function in the attached include takes into account the
initial cursor position. If the input string is too long, it is
scanned to find the position of the last space character and a new
newline is inserted just past that point. If there are no spaces in
the preceding string segment, the newline character is simply inserted
at the right margin. Embedded newlines as well as carriage returns are
also considered.

I have done virtually no testing on this, so please be carefull. jiri

-- snip ------------------------------------------------------------

--  file  : wordwrap.e
--  author: jiri babor
--  date  : 99-01-28
--  email : jbabor at paradise.net.nz
--  topic : wordwrap function

integer right_margin

global procedure set_right_margin(integer i)
    right_margin=i
end procedure

global function wordwrap(sequence text)
    -- if input string is longer
    integer b,c,i,max_length
    sequence pos,s
    pos=machine_func(25,0)  -- get_position() - don't include graphic.e
    max_length=right_margin-pos[2]+1
    s=""
    b=0                     -- last blank position
    i=1                     -- search position in string
    while length(text)>max_length do
        c=text[i]
        if i>max_length then
            if not b then b=i-1 end if
            s&=text[1..b]&'\n'
            text=text[b+1..length(text)]
            b=0
            i=1
            max_length=right_margin
        elsif c='\n' or c='\r' then
            s&=text[1..i]
            text=text[i+1..length(text)]
            b=0
            i=1
            max_length=right_margin
        else
            if c=' ' then b=i end if
            i+=1
        end if
    end while
    return s & text
end function

right_margin=80 -- default

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu