2 * Copyright (C) 1984-2007 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Prompting and other messages.
14 * There are three flavors of prompts, SHORT, MEDIUM and LONG,
15 * selected by the -m/-M options.
16 * There is also the "equals message", printed by the = command.
17 * A prompt is a message composed of various pieces, such as the
18 * name of the file being viewed, the percentage into the file, etc.
28 extern int so_s_width
, so_e_width
;
32 extern int jump_sline
;
33 extern int less_is_more
;
34 extern IFILE curr_ifile
;
37 extern char *editproto
;
41 * Prototypes for the three flavors of prompts.
42 * These strings are expanded by pr_expand().
44 static constant
char s_proto
[] =
45 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
46 static constant
char m_proto
[] =
47 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
48 static constant
char M_proto
[] =
49 "?f%f .?n?m(%T %i of %m) ..?ltlines %lt-%lb?L/%L. :byte %bB?s/%s. .?e(END) ?x- Next\\: %x.:?pB%pB\\%..%t";
50 static constant
char e_proto
[] =
51 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
52 static constant
char h_proto
[] =
53 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
54 static constant
char w_proto
[] =
56 static constant
char more_proto
[] =
57 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
59 public char *prproto
[3];
60 public char constant
*eqproto
= e_proto
;
61 public char constant
*hproto
= h_proto
;
62 public char constant
*wproto
= w_proto
;
64 static char message
[PROMPT_SIZE
];
68 * Initialize the prompt prototype strings.
73 prproto
[0] = save(s_proto
);
74 prproto
[1] = save(less_is_more
? more_proto
: m_proto
);
75 prproto
[2] = save(M_proto
);
76 eqproto
= save(e_proto
);
77 hproto
= save(h_proto
);
78 wproto
= save(w_proto
);
82 * Append a string to the end of the message.
91 if (mp
+ len
>= message
+ PROMPT_SIZE
)
92 len
= message
+ PROMPT_SIZE
- mp
- 1;
99 * Append a character to the end of the message.
113 * Append a POSITION (as a decimal integer) to the end of the message.
119 char buf
[INT_STRLEN_BOUND(pos
) + 2];
126 * Append a line number to the end of the message.
132 char buf
[INT_STRLEN_BOUND(linenum
) + 2];
134 linenumtoa(linenum
, buf
);
139 * Append an integer to the end of the message.
145 char buf
[INT_STRLEN_BOUND(num
) + 2];
152 * Append a question mark to the end of the message.
161 * Return the "current" byte offset in the file.
169 pos
= position(where
);
170 while (pos
== NULL_POSITION
&& where
>= 0 && where
< sc_height
-1)
171 pos
= position(++where
);
172 if (pos
== NULL_POSITION
)
178 * Return the value of a prototype conditional.
179 * A prototype string may include conditionals which consist of a
180 * question mark followed by a single letter.
181 * Here we decode that letter and return the appropriate boolean value.
192 case 'a': /* Anything in the message yet? */
193 return (mp
> message
);
194 case 'b': /* Current byte offset known? */
195 return (curr_byte(where
) != NULL_POSITION
);
197 return (hshift
!= 0);
198 case 'e': /* At end of file? */
200 case 'f': /* Filename known? */
201 return (strcmp(get_filename(curr_ifile
), "-") != 0);
202 case 'l': /* Line number known? */
203 case 'd': /* Same as l */
205 case 'L': /* Final line number known? */
206 case 'D': /* Final page number known? */
207 return (linenums
&& ch_length() != NULL_POSITION
);
208 case 'm': /* More than one file? */
210 return (ntags() ? (ntags() > 1) : (nifile() > 1));
212 return (nifile() > 1);
214 case 'n': /* First prompt in a new file? */
216 return (ntags() ? 1 : new_file
);
220 case 'p': /* Percent into file (bytes) known? */
221 return (curr_byte(where
) != NULL_POSITION
&&
223 case 'P': /* Percent into file (lines) known? */
224 return (currline(where
) != 0 &&
225 (len
= ch_length()) > 0 &&
226 find_linenum(len
) != 0);
227 case 's': /* Size of file known? */
229 return (ch_length() != NULL_POSITION
);
230 case 'x': /* Is there a "next" file? */
235 return (next_ifile(curr_ifile
) != NULL_IFILE
);
241 * Decode a "percent" prototype character.
242 * A prototype string may include various "percent" escapes;
243 * that is, a percent sign followed by a single letter.
244 * Here we decode that letter and take the appropriate action,
245 * usually by appending something to the message being built.
248 protochar(c
, where
, iseditproto
)
257 LINENUM last_linenum
;
261 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
265 case 'b': /* Current byte offset */
266 pos
= curr_byte(where
);
267 if (pos
!= NULL_POSITION
)
275 case 'd': /* Current page number */
276 linenum
= currline(where
);
277 if (linenum
> 0 && sc_height
> 1)
278 ap_linenum(PAGE_NUM(linenum
));
282 case 'D': /* Final page number */
283 /* Find the page number of the last byte in the file (len-1). */
285 if (len
== NULL_POSITION
)
288 /* An empty file has no pages. */
292 linenum
= find_linenum(len
- 1);
296 ap_linenum(PAGE_NUM(linenum
));
300 case 'E': /* Editor name */
304 case 'f': /* File name */
305 ap_str(get_filename(curr_ifile
));
307 case 'i': /* Index into list of files */
313 ap_int(get_index(curr_ifile
));
315 case 'l': /* Current line number */
316 linenum
= currline(where
);
322 case 'L': /* Final line number */
324 if (len
== NULL_POSITION
|| len
== ch_zero() ||
325 (linenum
= find_linenum(len
)) <= 0)
328 ap_linenum(linenum
-1);
330 case 'm': /* Number of files */
339 case 'p': /* Percent into file (bytes) */
340 pos
= curr_byte(where
);
342 if (pos
!= NULL_POSITION
&& len
> 0)
343 ap_int(percentage(pos
,len
));
347 case 'P': /* Percent into file (lines) */
348 linenum
= currline(where
);
350 (len
= ch_length()) == NULL_POSITION
|| len
== ch_zero() ||
351 (last_linenum
= find_linenum(len
)) <= 0)
354 ap_int(percentage(linenum
, last_linenum
));
356 case 's': /* Size of file */
359 if (len
!= NULL_POSITION
)
364 case 't': /* Truncate trailing spaces in the message */
365 while (mp
> message
&& mp
[-1] == ' ')
368 case 'T': /* Type of list */
376 case 'x': /* Name of next file */
377 h
= next_ifile(curr_ifile
);
379 ap_str(get_filename(h
));
387 * Skip a false conditional.
388 * When a false condition is found (either a false IF or the ELSE part
389 * of a true IF), this routine scans the prototype string to decide
390 * where to resume parsing the string.
391 * We must keep track of nested IFs and skip them properly.
397 register int iflevel
;
400 * We came in here after processing a ? or :,
401 * so we start nested one level deep.
405 for (;;) switch (*++p
)
409 * Start of a nested IF.
416 * If this matches the IF we came in here with,
425 * If this matches the IF we came in here with,
433 * Backslash escapes the next character.
439 * Whoops. Hit end of string.
440 * This is a malformed conditional, but just treat it
441 * as if all active conditionals ends here.
449 * Decode a char that represents a position on the screen.
458 case 'b': case 'd': case 'l': case 'p': case 'P':
461 case 't': *wp
= TOP
; break;
462 case 'm': *wp
= MIDDLE
; break;
463 case 'b': *wp
= BOTTOM
; break;
464 case 'B': *wp
= BOTTOM_PLUS_ONE
; break;
465 case 'j': *wp
= adjsline(jump_sline
); break;
466 default: *wp
= TOP
; p
--; break;
473 * Construct a message based on a prototype string.
476 pr_expand(proto
, maxwidth
)
489 for (p
= proto
; *p
!= '\0'; p
++)
493 default: /* Just put the character in the message */
496 case '\\': /* Backslash escapes the next character */
500 case '?': /* Conditional (IF) */
501 if ((c
= *++p
) == '\0')
506 p
= wherechar(p
, &where
);
514 case '.': /* ENDIF */
516 case '%': /* Percent escape */
517 if ((c
= *++p
) == '\0')
522 p
= wherechar(p
, &where
);
525 (proto
== editproto
));
537 if (maxwidth
> 0 && mp
>= message
+ maxwidth
)
540 * Message is too long.
541 * Return just the final portion of it.
543 return (mp
- maxwidth
);
549 * Return a message suitable for printing by the "=" command.
554 return (pr_expand(eqproto
, 0));
559 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
560 * If we can't come up with an appropriate prompt, return NULL
561 * and the caller will prompt with a colon.
569 type
= (!less_is_more
) ? pr_type
: pr_type
? 0 : 1;
570 prompt
= pr_expand((ch_getflags() & CH_HELPFILE
) ?
571 hproto
: prproto
[type
],
572 sc_width
-so_s_width
-so_e_width
-2);
578 * Return a message suitable for printing while waiting in the F command.
583 return (pr_expand(wproto
, sc_width
-so_s_width
-so_e_width
-2));