3 * Copyright (C) 1984-2011 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information about less, or for information on how to
9 * contact the author, see the README file.
14 * Prompting and other messages.
15 * There are three flavors of prompts, SHORT, MEDIUM and LONG,
16 * selected by the -m/-M options.
17 * There is also the "equals message", printed by the = command.
18 * A prompt is a message composed of various pieces, such as the
19 * 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? */
199 return (eof_displayed());
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 'F': /* Last component of file name */
308 ap_str(last_component(get_filename(curr_ifile)));
310 case 'i': /* Index into list of files */
316 ap_int(get_index(curr_ifile));
318 case 'l': /* Current line number */
319 linenum = currline(where);
325 case 'L': /* Final line number */
327 if (len == NULL_POSITION || len == ch_zero() ||
328 (linenum = find_linenum(len)) <= 0)
331 ap_linenum(linenum-1);
333 case 'm': /* Number of files */
342 case 'p': /* Percent into file (bytes) */
343 pos = curr_byte(where);
345 if (pos != NULL_POSITION && len > 0)
346 ap_int(percentage(pos,len));
350 case 'P': /* Percent into file (lines) */
351 linenum = currline(where);
353 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
354 (last_linenum = find_linenum(len)) <= 0)
357 ap_int(percentage(linenum, last_linenum));
359 case 's': /* Size of file */
362 if (len != NULL_POSITION)
367 case 't': /* Truncate trailing spaces in the message */
368 while (mp > message && mp[-1] == ' ')
372 case 'T': /* Type of list */
380 case 'x': /* Name of next file */
381 h = next_ifile(curr_ifile);
383 ap_str(get_filename(h));
391 * Skip a false conditional.
392 * When a false condition is found (either a false IF or the ELSE part
393 * of a true IF), this routine scans the prototype string to decide
394 * where to resume parsing the string.
395 * We must keep track of nested IFs and skip them properly.
401 register int iflevel;
404 * We came in here after processing a ? or :,
405 * so we start nested one level deep.
409 for (;;) switch (*++p)
413 * Start of a nested IF.
420 * If this matches the IF we came in here with,
429 * If this matches the IF we came in here with,
437 * Backslash escapes the next character.
443 * Whoops. Hit end of string.
444 * This is a malformed conditional, but just treat it
445 * as if all active conditionals ends here.
453 * Decode a char that represents a position on the screen.
462 case 'b': case 'd': case 'l': case 'p': case 'P':
465 case 't': *wp = TOP; break;
466 case 'm': *wp = MIDDLE; break;
467 case 'b': *wp = BOTTOM; break;
468 case 'B': *wp = BOTTOM_PLUS_ONE; break;
469 case 'j': *wp = adjsline(jump_sline); break;
470 default: *wp = TOP; p--; break;
477 * Construct a message based on a prototype string.
480 pr_expand(proto, maxwidth)
493 for (p = proto; *p != '\0'; p++)
497 default: /* Just put the character in the message */
500 case '\\': /* Backslash escapes the next character */
504 case '?': /* Conditional (IF) */
505 if ((c = *++p) == '\0')
510 p = wherechar(p, &where);
518 case '.': /* ENDIF */
520 case '%': /* Percent escape */
521 if ((c = *++p) == '\0')
526 p = wherechar(p, &where);
529 (proto == editproto));
541 if (maxwidth > 0 && mp >= message + maxwidth)
544 * Message is too long.
545 * Return just the final portion of it.
547 return (mp - maxwidth);
553 * Return a message suitable for printing by the "=" command.
558 return (pr_expand(eqproto, 0));
563 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
564 * If we can't come up with an appropriate prompt, return NULL
565 * and the caller will prompt with a colon.
573 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
574 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
575 hproto : prproto[type],
576 sc_width-so_s_width-so_e_width-2);
582 * Return a message suitable for printing while waiting in the F command.
587 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));