]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/miscellany/less-177/line.c
2 * Routines to manipulate the "line buffer".
3 * The line buffer holds a line of output as it is being built
4 * in preparation for output to the screen.
9 static char linebuf
[1024]; /* Buffer which holds the current output line */
10 static char attr
[1024]; /* Extension of linebuf to hold attributes */
11 static int curr
; /* Index into linebuf */
12 static int column
; /* Printable length, accounting for
14 static int overstrike
; /* Next char should overstrike previous char */
15 static int is_null_line
; /* There is no current line */
18 static int do_append();
26 extern int auto_wrap
, ignaw
;
27 extern int bo_s_width
, bo_e_width
;
28 extern int ul_s_width
, ul_e_width
;
29 extern int bl_s_width
, bl_e_width
;
30 extern int sc_width
, sc_height
;
33 * Rewind the line buffer.
46 * Insert the line number (of the given position) into the line buffer.
57 * We display the line number at the start of each line
58 * only if the -N option is set.
64 * Get the line number and put it in the current line.
65 * {{ Note: since find_linenum calls forw_raw_line,
66 * it may seek in the input file, requiring the caller
67 * of plinenum to re-seek if necessary. }}
69 lno
= find_linenum(pos
);
71 sprintf(&linebuf
[curr
], "%6d", lno
);
72 n
= strlen(&linebuf
[curr
]);
74 for (i
= 0; i
< n
; i
++)
78 * Append enough spaces to bring us to the next tab stop.
79 * {{ We could avoid this at the cost of adding some
80 * complication to the tab stop logic in pappend(). }}
87 } while ((column
% tabstop
) != 0);
91 * Return the printing width of the start (enter) sequence
92 * for a given character attribute.
100 case BOLD
: return (bo_s_width
);
101 case UNDERLINE
: return (ul_s_width
);
102 case BLINK
: return (bl_s_width
);
108 * Return the printing width of the end (exit) sequence
109 * for a given character attribute.
117 case BOLD
: return (bo_e_width
);
118 case UNDERLINE
: return (ul_e_width
);
119 case BLINK
: return (bl_e_width
);
125 * Return the printing width of a given character and attribute,
126 * if the character were added to the current position in the line buffer.
127 * Adding a character with a given attribute may cause an enter or exit
128 * attribute sequence to be inserted, so this must be taken into account.
139 * Backspace moves backwards one position.
145 * Control characters do unpredicatable things,
146 * so we don't even try to guess; say it doesn't move.
147 * This can only happen if the -r flag is in effect.
152 * Other characters take one space,
153 * plus the width of any attribute enter/exit sequence.
156 if (curr
> 0 && attr
[curr
-1] != a
)
157 w
+= attr_ewidth(attr
[curr
-1]);
158 if (a
&& (curr
== 0 || attr
[curr
-1] != a
))
164 * Delete the previous character in the line buffer.
170 column
-= pwidth(linebuf
[curr
], attr
[curr
]);
174 * Append a character and attribute to the line buffer.
184 if (ctldisp
> 0 && column
+ w
+ attr_ewidth(a
) > sc_width
)
186 * Won't fit on screen.
190 if (curr
>= sizeof(linebuf
)-2)
192 * Won't fit in line buffer.
197 * Special handling for "magic cookie" terminals.
198 * If an attribute enter/exit sequence has a printing width > 0,
199 * and the sequence is adjacent to a space, delete the space.
200 * We just mark the space as invisible, to avoid having too
201 * many spaces deleted.
202 * {{ Note that even if the attribute width is > 1, we
203 * delete only one space. It's not worth trying to do more.
204 * It's hardly worth doing this much. }}
206 if (curr
> 0 && a
!= NORMAL
&&
207 linebuf
[curr
-1] == ' ' && attr
[curr
-1] == NORMAL
&&
211 * We are about to append an enter-attribute sequence
212 * just after a space. Delete the space.
214 attr
[curr
-1] = INVIS
;
216 } else if (curr
> 0 && attr
[curr
-1] != NORMAL
&&
217 attr
[curr
-1] != INVIS
&& c
== ' ' && a
== NORMAL
&&
218 attr_ewidth(attr
[curr
-1]) > 0)
221 * We are about to append a space just after an
222 * exit-attribute sequence. Delete the space.
227 /* End of magic cookie handling. */
236 * Append a character to the line buffer.
237 * Expand tabs into spaces, handle underlining, boldfacing, etc.
238 * Returns 0 if ok, 1 if couldn't fit in buffer.
246 if (do_append(pendc
))
248 * Oops. We've probably lost the char which
249 * was in pendc, since caller won't back up.
255 if (c
== '\r' && bs_mode
== BS_SPECIAL
)
258 * Don't put the CR into the buffer until we see
259 * the next char. If the next char is a newline,
266 return (do_append(c
));
276 #define STOREC(c,a) if (storec((c),(a))) return (1); else curr++
281 * Overstrike the character at the current position
282 * in the line buffer. This will cause either
283 * underline (if a "_" is overstruck),
284 * bold (if an identical character is overstruck),
285 * or just deletion of the character in the buffer.
288 if (c
== linebuf
[curr
])
289 STOREC(linebuf
[curr
], BOLD
);
291 STOREC(linebuf
[curr
], UNDERLINE
);
292 else if (linebuf
[curr
] == '_')
293 STOREC(c
, UNDERLINE
);
294 else if (control_char(c
))
295 goto do_control_char
;
298 } else if (c
== '\b')
306 goto do_control_char
;
314 } else if (c
== '\t')
317 * Expand a tab into spaces.
322 } while ((column
% tabstop
) != 0);
323 } else if (control_char(c
))
329 * Output as a normal character.
335 * Output in the (blinking) ^X format.
341 * Make sure we can get the entire representation
342 * the character on this line.
344 if (column
+ strlen(s
) +
345 attr_swidth(a
) + attr_ewidth(a
) > sc_width
)
348 for ( ; *s
!= 0; s
++)
360 * Terminate the line in the line buffer.
366 if (pendc
&& (pendc
!= '\r' || !endline
))
368 * If we had a pending character, put it in the buffer.
369 * But discard a pending CR if we are at end of line
370 * (that is, discard the CR in a CR/LF sequence).
372 (void) do_append(pendc
);
375 * Add a newline if necessary,
376 * and append a '\0' to the end of the line.
378 if (column
< sc_width
|| !auto_wrap
|| ignaw
|| ctldisp
== 0)
380 linebuf
[curr
] = '\n';
384 linebuf
[curr
] = '\0';
389 * Get a character from the current line.
390 * Return the character as the function return value,
391 * and the character attribute in *ap.
401 * If there is no current line, we pretend the line is
402 * either "~" or "", depending on the "twiddle" flag.
411 return (linebuf
[i
] & 0377);
415 * Indicate that there is no current line.
424 * Analogous to forw_line(), but deals with "raw lines":
425 * lines which are not split for screen width.
426 * {{ This is supposed to be more efficient than forw_line(). }}
429 forw_raw_line(curr_pos
, linep
)
437 if (curr_pos
== NULL_POSITION
|| ch_seek(curr_pos
) ||
438 (c
= ch_forw_get()) == EOI
)
439 return (NULL_POSITION
);
445 if (c
== '\n' || c
== EOI
)
450 if (p
>= &linebuf
[sizeof(linebuf
)-1])
453 * Overflowed the input buffer.
454 * Pretend the line ended here.
455 * {{ The line buffer is supposed to be big
456 * enough that this never happens. }}
458 new_pos
= ch_tell() - 1;
471 * Analogous to back_line(), but deals with "raw lines".
472 * {{ This is supposed to be more efficient than back_line(). }}
475 back_raw_line(curr_pos
, linep
)
483 if (curr_pos
== NULL_POSITION
|| curr_pos
<= ch_zero() ||
485 return (NULL_POSITION
);
487 p
= &linebuf
[sizeof(linebuf
)];
496 * This is the newline ending the previous line.
497 * We have hit the beginning of the line.
499 new_pos
= ch_tell() + 1;
505 * We have hit the beginning of the file.
506 * This must be the first line in the file.
507 * This must, of course, be the beginning of the line.
515 * Overflowed the input buffer.
516 * Pretend the line ended here.
518 new_pos
= ch_tell() + 1;