2 * Copyright (C) 1984-2009 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.
27 extern int so_s_width, so_e_width;
31 extern int jump_sline;
32 extern int less_is_more;
33 extern IFILE curr_ifile;
36 extern char *editproto;
40 * Prototypes for the three flavors of prompts.
41 * These strings are expanded by pr_expand().
43 static constant char s_proto[] =
44 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
45 static constant char m_proto[] =
46 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
47 static constant char M_proto[] =
48 "?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";
49 static constant char e_proto[] =
50 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
51 static constant char h_proto[] =
52 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
53 static constant char w_proto[] =
55 static constant char more_proto[] =
56 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
58 public char *prproto[3];
59 public char constant *eqproto = e_proto;
60 public char constant *hproto = h_proto;
61 public char constant *wproto = w_proto;
63 static char message[PROMPT_SIZE];
67 * Initialize the prompt prototype strings.
72 prproto[0] = save(s_proto);
73 prproto[1] = save(less_is_more ? more_proto : m_proto);
74 prproto[2] = save(M_proto);
75 eqproto = save(e_proto);
76 hproto = save(h_proto);
77 wproto = save(w_proto);
81 * Append a string to the end of the message.
90 if (mp + len >= message + PROMPT_SIZE)
91 len = message + PROMPT_SIZE - mp - 1;
98 * Append a character to the end of the message.
112 * Append a POSITION (as a decimal integer) to the end of the message.
118 char buf[INT_STRLEN_BOUND(pos) + 2];
125 * Append a line number to the end of the message.
131 char buf[INT_STRLEN_BOUND(linenum) + 2];
133 linenumtoa(linenum, buf);
138 * Append an integer to the end of the message.
144 char buf[INT_STRLEN_BOUND(num) + 2];
151 * Append a question mark to the end of the message.
160 * Return the "current" byte offset in the file.
168 pos = position(where);
169 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
170 pos = position(++where);
171 if (pos == NULL_POSITION)
177 * Return the value of a prototype conditional.
178 * A prototype string may include conditionals which consist of a
179 * question mark followed by a single letter.
180 * Here we decode that letter and return the appropriate boolean value.
191 case 'a': /* Anything in the message yet? */
192 return (mp > message);
193 case 'b': /* Current byte offset known? */
194 return (curr_byte(where) != NULL_POSITION);
196 return (hshift != 0);
197 case 'e': /* At end of file? */
198 return (eof_displayed());
199 case 'f': /* Filename known? */
200 return (strcmp(get_filename(curr_ifile), "-") != 0);
201 case 'l': /* Line number known? */
202 case 'd': /* Same as l */
204 case 'L': /* Final line number known? */
205 case 'D': /* Final page number known? */
206 return (linenums && ch_length() != NULL_POSITION);
207 case 'm': /* More than one file? */
209 return (ntags() ? (ntags() > 1) : (nifile() > 1));
211 return (nifile() > 1);
213 case 'n': /* First prompt in a new file? */
215 return (ntags() ? 1 : new_file);
219 case 'p': /* Percent into file (bytes) known? */
220 return (curr_byte(where) != NULL_POSITION &&
222 case 'P': /* Percent into file (lines) known? */
223 return (currline(where) != 0 &&
224 (len = ch_length()) > 0 &&
225 find_linenum(len) != 0);
226 case 's': /* Size of file known? */
228 return (ch_length() != NULL_POSITION);
229 case 'x': /* Is there a "next" file? */
234 return (next_ifile(curr_ifile) != NULL_IFILE);
240 * Decode a "percent" prototype character.
241 * A prototype string may include various "percent" escapes;
242 * that is, a percent sign followed by a single letter.
243 * Here we decode that letter and take the appropriate action,
244 * usually by appending something to the message being built.
247 protochar(c, where, iseditproto)
256 LINENUM last_linenum;
260 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
264 case 'b': /* Current byte offset */
265 pos = curr_byte(where);
266 if (pos != NULL_POSITION)
274 case 'd': /* Current page number */
275 linenum = currline(where);
276 if (linenum > 0 && sc_height > 1)
277 ap_linenum(PAGE_NUM(linenum));
281 case 'D': /* Final page number */
282 /* Find the page number of the last byte in the file (len-1). */
284 if (len == NULL_POSITION)
287 /* An empty file has no pages. */
291 linenum = find_linenum(len - 1);
295 ap_linenum(PAGE_NUM(linenum));
299 case 'E': /* Editor name */
303 case 'f': /* File name */
304 ap_str(get_filename(curr_ifile));
306 case 'i': /* Index into list of files */
312 ap_int(get_index(curr_ifile));
314 case 'l': /* Current line number */
315 linenum = currline(where);
321 case 'L': /* Final line number */
323 if (len == NULL_POSITION || len == ch_zero() ||
324 (linenum = find_linenum(len)) <= 0)
327 ap_linenum(linenum-1);
329 case 'm': /* Number of files */
338 case 'p': /* Percent into file (bytes) */
339 pos = curr_byte(where);
341 if (pos != NULL_POSITION && len > 0)
342 ap_int(percentage(pos,len));
346 case 'P': /* Percent into file (lines) */
347 linenum = currline(where);
349 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
350 (last_linenum = find_linenum(len)) <= 0)
353 ap_int(percentage(linenum, last_linenum));
355 case 's': /* Size of file */
358 if (len != NULL_POSITION)
363 case 't': /* Truncate trailing spaces in the message */
364 while (mp > message && mp[-1] == ' ')
367 case 'T': /* Type of list */
375 case 'x': /* Name of next file */
376 h = next_ifile(curr_ifile);
378 ap_str(get_filename(h));
386 * Skip a false conditional.
387 * When a false condition is found (either a false IF or the ELSE part
388 * of a true IF), this routine scans the prototype string to decide
389 * where to resume parsing the string.
390 * We must keep track of nested IFs and skip them properly.
396 register int iflevel;
399 * We came in here after processing a ? or :,
400 * so we start nested one level deep.
404 for (;;) switch (*++p)
408 * Start of a nested IF.
415 * If this matches the IF we came in here with,
424 * If this matches the IF we came in here with,
432 * Backslash escapes the next character.
438 * Whoops. Hit end of string.
439 * This is a malformed conditional, but just treat it
440 * as if all active conditionals ends here.
448 * Decode a char that represents a position on the screen.
457 case 'b': case 'd': case 'l': case 'p': case 'P':
460 case 't': *wp = TOP; break;
461 case 'm': *wp = MIDDLE; break;
462 case 'b': *wp = BOTTOM; break;
463 case 'B': *wp = BOTTOM_PLUS_ONE; break;
464 case 'j': *wp = adjsline(jump_sline); break;
465 default: *wp = TOP; p--; break;
472 * Construct a message based on a prototype string.
475 pr_expand(proto, maxwidth)
488 for (p = proto; *p != '\0'; p++)
492 default: /* Just put the character in the message */
495 case '\\': /* Backslash escapes the next character */
499 case '?': /* Conditional (IF) */
500 if ((c = *++p) == '\0')
505 p = wherechar(p, &where);
513 case '.': /* ENDIF */
515 case '%': /* Percent escape */
516 if ((c = *++p) == '\0')
521 p = wherechar(p, &where);
524 (proto == editproto));
536 if (maxwidth > 0 && mp >= message + maxwidth)
539 * Message is too long.
540 * Return just the final portion of it.
542 return (mp - maxwidth);
548 * Return a message suitable for printing by the "=" command.
553 return (pr_expand(eqproto, 0));
558 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
559 * If we can't come up with an appropriate prompt, return NULL
560 * and the caller will prompt with a colon.
568 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
569 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
570 hproto : prproto[type],
571 sc_width-so_s_width-so_e_width-2);
577 * Return a message suitable for printing while waiting in the F command.
582 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));