3 * Copyright (C) 1984-2007 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.
29 extern int so_s_width, so_e_width;
33 extern int jump_sline;
34 extern int less_is_more;
35 extern IFILE curr_ifile;
38 extern char *editproto;
42 * Prototypes for the three flavors of prompts.
43 * These strings are expanded by pr_expand().
45 static constant char s_proto[] =
46 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
47 static constant char m_proto[] =
48 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
49 static constant char M_proto[] =
50 "?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";
51 static constant char e_proto[] =
52 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
53 static constant char h_proto[] =
54 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
55 static constant char w_proto[] =
57 static constant char more_proto[] =
58 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
60 public char *prproto[3];
61 public char constant *eqproto = e_proto;
62 public char constant *hproto = h_proto;
63 public char constant *wproto = w_proto;
65 static char message[PROMPT_SIZE];
69 * Initialize the prompt prototype strings.
74 prproto[0] = save(s_proto);
75 prproto[1] = save(less_is_more ? more_proto : m_proto);
76 prproto[2] = save(M_proto);
77 eqproto = save(e_proto);
78 hproto = save(h_proto);
79 wproto = save(w_proto);
83 * Append a string to the end of the message.
92 if (mp + len >= message + PROMPT_SIZE)
93 len = message + PROMPT_SIZE - mp - 1;
100 * Append a character to the end of the message.
114 * Append a POSITION (as a decimal integer) to the end of the message.
120 char buf[INT_STRLEN_BOUND(pos) + 2];
127 * Append a line number to the end of the message.
133 char buf[INT_STRLEN_BOUND(linenum) + 2];
135 linenumtoa(linenum, buf);
140 * Append an integer to the end of the message.
146 char buf[INT_STRLEN_BOUND(num) + 2];
153 * Append a question mark to the end of the message.
162 * Return the "current" byte offset in the file.
170 pos = position(where);
171 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
172 pos = position(++where);
173 if (pos == NULL_POSITION)
179 * Return the value of a prototype conditional.
180 * A prototype string may include conditionals which consist of a
181 * question mark followed by a single letter.
182 * Here we decode that letter and return the appropriate boolean value.
193 case 'a': /* Anything in the message yet? */
194 return (mp > message);
195 case 'b': /* Current byte offset known? */
196 return (curr_byte(where) != NULL_POSITION);
198 return (hshift != 0);
199 case 'e': /* At end of file? */
201 case 'f': /* Filename known? */
202 return (strcmp(get_filename(curr_ifile), "-") != 0);
203 case 'l': /* Line number known? */
204 case 'd': /* Same as l */
206 case 'L': /* Final line number known? */
207 case 'D': /* Final page number known? */
208 return (linenums && ch_length() != NULL_POSITION);
209 case 'm': /* More than one file? */
211 return (ntags() ? (ntags() > 1) : (nifile() > 1));
213 return (nifile() > 1);
215 case 'n': /* First prompt in a new file? */
217 return (ntags() ? 1 : new_file);
221 case 'p': /* Percent into file (bytes) known? */
222 return (curr_byte(where) != NULL_POSITION &&
224 case 'P': /* Percent into file (lines) known? */
225 return (currline(where) != 0 &&
226 (len = ch_length()) > 0 &&
227 find_linenum(len) != 0);
228 case 's': /* Size of file known? */
230 return (ch_length() != NULL_POSITION);
231 case 'x': /* Is there a "next" file? */
236 return (next_ifile(curr_ifile) != NULL_IFILE);
242 * Decode a "percent" prototype character.
243 * A prototype string may include various "percent" escapes;
244 * that is, a percent sign followed by a single letter.
245 * Here we decode that letter and take the appropriate action,
246 * usually by appending something to the message being built.
249 protochar(c, where, iseditproto)
258 LINENUM last_linenum;
262 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
266 case 'b': /* Current byte offset */
267 pos = curr_byte(where);
268 if (pos != NULL_POSITION)
276 case 'd': /* Current page number */
277 linenum = currline(where);
278 if (linenum > 0 && sc_height > 1)
279 ap_linenum(PAGE_NUM(linenum));
283 case 'D': /* Final page number */
284 /* Find the page number of the last byte in the file (len-1). */
286 if (len == NULL_POSITION)
289 /* An empty file has no pages. */
293 linenum = find_linenum(len - 1);
297 ap_linenum(PAGE_NUM(linenum));
301 case 'E': /* Editor name */
305 case 'f': /* File name */
306 ap_str(get_filename(curr_ifile));
308 case 'i': /* Index into list of files */
314 ap_int(get_index(curr_ifile));
316 case 'l': /* Current line number */
317 linenum = currline(where);
323 case 'L': /* Final line number */
325 if (len == NULL_POSITION || len == ch_zero() ||
326 (linenum = find_linenum(len)) <= 0)
329 ap_linenum(linenum-1);
331 case 'm': /* Number of files */
340 case 'p': /* Percent into file (bytes) */
341 pos = curr_byte(where);
343 if (pos != NULL_POSITION && len > 0)
344 ap_int(percentage(pos,len));
348 case 'P': /* Percent into file (lines) */
349 linenum = currline(where);
351 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
352 (last_linenum = find_linenum(len)) <= 0)
355 ap_int(percentage(linenum, last_linenum));
357 case 's': /* Size of file */
360 if (len != NULL_POSITION)
365 case 't': /* Truncate trailing spaces in the message */
366 while (mp > message && mp[-1] == ' ')
369 case 'T': /* Type of list */
377 case 'x': /* Name of next file */
378 h = next_ifile(curr_ifile);
380 ap_str(get_filename(h));
388 * Skip a false conditional.
389 * When a false condition is found (either a false IF or the ELSE part
390 * of a true IF), this routine scans the prototype string to decide
391 * where to resume parsing the string.
392 * We must keep track of nested IFs and skip them properly.
398 register int iflevel;
401 * We came in here after processing a ? or :,
402 * so we start nested one level deep.
406 for (;;) switch (*++p)
410 * Start of a nested IF.
417 * If this matches the IF we came in here with,
426 * If this matches the IF we came in here with,
434 * Backslash escapes the next character.
440 * Whoops. Hit end of string.
441 * This is a malformed conditional, but just treat it
442 * as if all active conditionals ends here.
450 * Decode a char that represents a position on the screen.
459 case 'b': case 'd': case 'l': case 'p': case 'P':
462 case 't': *wp = TOP; break;
463 case 'm': *wp = MIDDLE; break;
464 case 'b': *wp = BOTTOM; break;
465 case 'B': *wp = BOTTOM_PLUS_ONE; break;
466 case 'j': *wp = adjsline(jump_sline); break;
467 default: *wp = TOP; p--; break;
474 * Construct a message based on a prototype string.
477 pr_expand(proto, maxwidth)
490 for (p = proto; *p != '\0'; p++)
494 default: /* Just put the character in the message */
497 case '\\': /* Backslash escapes the next character */
501 case '?': /* Conditional (IF) */
502 if ((c = *++p) == '\0')
507 p = wherechar(p, &where);
515 case '.': /* ENDIF */
517 case '%': /* Percent escape */
518 if ((c = *++p) == '\0')
523 p = wherechar(p, &where);
526 (proto == editproto));
538 if (maxwidth > 0 && mp >= message + maxwidth)
541 * Message is too long.
542 * Return just the final portion of it.
544 return (mp - maxwidth);
550 * Return a message suitable for printing by the "=" command.
555 return (pr_expand(eqproto, 0));
560 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
561 * If we can't come up with an appropriate prompt, return NULL
562 * and the caller will prompt with a colon.
570 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
571 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
572 hproto : prproto[type],
573 sc_width-so_s_width-so_e_width-2);
579 * Return a message suitable for printing while waiting in the F command.
584 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));