views:

109

answers:

2

K&R C Programming Language: pg. 105

Extend entab and detab to accept the shorthand

entab -m +n

to mean tab stops every n columns, starting at column m.

entab replaces a number of spaces with a tab character and detab does the opposite. The question I have concerns the tab stops and entab. I figure that for detab it's pretty easy to determine the number of spaces needed to reach the next tab stop, so no worries there. With entab, replacing spaces with tabs is slightly more difficult since I cannot for sure know how large the tab character goes to its own tab stop (unless there is a way to know for sure).

Am I even thinking about this thing properly?

A: 

"tab stops every n columns, starting at column m" tells you how large each tab stop is, at least by my reading: it's just n. Only the first tab stop is different; that one is m.

Plynx
So don't worry about `entab` because `\t` will go to whatever tabstop it wants to?
marsol0x
@marsol0x For the case you're thinking of, I think you're meant to *input* the length of `\t` as *n*, where *m* = 0. With *m* and *n* you are specifying to `entab` what the tab stop behavior of your system is.
Plynx
+1  A: 

entab needs to work out when runs of spaces reach a tabstop - then that run of spaces can be replaced by a tab character.

For example, the following line of text (the ruller is there for reference):

           1   1   2   2   2
1      8   2   6   0   4   8
-------+---+---+---+---+---+
          this     is a line

should look like the following after entab -8 +4:

\t  this \tis a line

(note that it might be reasonable for there to be two tab characters following 'this' in the line, since either a space or a tab would reach that particular tab stop).

Michael Burr