Re: Tab has 8 spaces .... can it be more or less ? If so HOW? Please !

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

Pete Lomax wrote:
> 
> Les Rogers wrote:
> > .............. output is 
> > HiThere
> >         HiThere
> > Can I make output 
> > HiThere
> >    HiThere
> 
> I doubt there is any way to change what the console does when you
> puts(1,"\t"),
> but you can pre-process your output. Here is a routine I use in Edita to
> convert
> tabs to spaces:

Well there is, but requires advanced programming. It is simpler to get rid 
of tabs and convert them to spaces, whixh is what Pete's code does. I won't 
modify it, just put in some comments and blank lines.

The tab character is supposed to cause the next character(s) to display not 
from the current cursor  position, but from the next tabbing position. Hence,
you need to 
1/ provide the tabbing positions;
2/ know, whenever you encounter a tab, where the next tabbing position is.

For 1/, a simple approach consists in saying that the tabbing positions are 
regularly spaced starting from some column, which gives the spacing. By 
default, Windows sets this spacing to 8, and hence the tabbing positions to 
8,16,24,32,.... Since you wish to override this default, you have to provide 
the new spacing value. The code below assumes you defined an integer variable 
named "isTabWidth", which holds the desired value on calling ExpandTabs().
The code assumed you define d and initialised the variable properly.

For 2/, you know where you are if you know the starting point and how far 
you went from there. 
* The former is assumed to be column 1. This works if you start wriing after 
a carriage return. If yor text doesn't start there, the code won't behave 
exactly as you expect.
* The latter is simply the length of the text written so far. It will be 
processed assuming that no line wrap occurs, ie your text ends on the 
same line it started on.

Now the code which does the job, with the assumptions I mentioned:
constant SPACE=' ', TAB='\t' -- for clearer reading

global function ExpandTabs(sequence text)
-- Replace any tabs with spaces.
-- Fast (92,000 lines/second or better)
-- temp variable to hold the location of the tab character to process
integer tab 
    while 1 do  -- loop infinitely and determine when to stop ourselves
        tab=find(TAB,text)  -- any tab there?
        if not tab then exit end if  -- none, so we are done
-- Euphoria is a "simple" lnguage and defines only a minimal set of 
-- sequence handling routines. So it has no Mid$ and you have to 
-- code your own one as: 

-- <before_changed_section> & <new_value_for_section> & <after_changed_section>
-- The & operator concatenates sequences. Other languages call it + or !!.
        text=text[1..tab-1]& -- <before_changed_section>

--  <new_vlue_for_section>, which will replace the substring made of the tab
character
-- to process.
-- remainder(p,q) is the way Eu computes the remainder of the division of p 
-- by q. Most other languages use % for that.
-- To determine the next tabbing position, see how far ahead of- the 
-- previous one you are. That's the remainder() part. Then the gap to create
-- has length spacing-that distance.
-- And creating the gap of some length is achieved through writing a sequence 
-- of &s many spaces. reoeat() creates a sequence of copies of something.
             repeat(SPACE,isTabWidth-remainder(tab-1,isTabWidth))&

             text[tab+1..length(text)] -- <after_changed_section>
                                        -- can be shortened to "text[tab+1..$]"
    end while
-- no more tabs, so all of them were converted: return the result.
-- There is no predefined variable to store a function result.
    return text
end function

where isTabWidth needs to be set to/defined as 3 for the example you gave.

> You could of course make this a procedure, say Puts1, which displays to the
> console rather than 'return text'.
> 
> HTH,
> Pete

This means that he made a flexible version which translates input text to 
output text. You may wish to wrap this function into a procedure that
just displays the translated text.

HTH
CChris

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

Search



Quick Links

User menu

Not signed in.

Misc Menu