3 * Copyright (C) 1984-2004 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 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[] =
57 public char *prproto[3];
58 public char constant *eqproto = e_proto;
59 public char constant *hproto = h_proto;
60 public char constant *wproto = w_proto;
62 static char message[PROMPT_SIZE];
66 * Initialize the prompt prototype strings.
71 prproto[0] = save(s_proto);
72 prproto[1] = save(m_proto);
73 prproto[2] = save(M_proto);
74 eqproto = save(e_proto);
75 hproto = save(h_proto);
76 wproto = save(w_proto);
80 * Append a string to the end of the message.
89 if (mp + len >= message + PROMPT_SIZE)
90 len = message + PROMPT_SIZE - mp - 1;
97 * Append a character to the end of the message.
111 * Append a POSITION (as a decimal integer) to the end of the message.
117 char buf[INT_STRLEN_BOUND(pos) + 2];
124 * Append a line number to the end of the message.
130 char buf[INT_STRLEN_BOUND(linenum) + 2];
132 linenumtoa(linenum, buf);
137 * Append an integer to the end of the message.
143 char buf[INT_STRLEN_BOUND(num) + 2];
150 * Append a question mark to the end of the message.
159 * Return the "current" byte offset in the file.
167 pos = position(where);
168 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
169 pos = position(++where);
170 if (pos == NULL_POSITION)
176 * Return the value of a prototype conditional.
177 * A prototype string may include conditionals which consist of a
178 * question mark followed by a single letter.
179 * Here we decode that letter and return the appropriate boolean value.
190 case 'a': /* Anything in the message yet? */
191 return (mp > message);
192 case 'b': /* Current byte offset known? */
193 return (curr_byte(where) != NULL_POSITION);
195 return (hshift != 0);
196 case 'e': /* At end of file? */
198 case 'f': /* Filename known? */
199 return (strcmp(get_filename(curr_ifile), "-") != 0);
200 case 'l': /* Line number known? */
201 case 'd': /* Same as l */
203 case 'L': /* Final line number known? */
204 case 'D': /* Final page number known? */
205 return (linenums && ch_length() != NULL_POSITION);
206 case 'm': /* More than one file? */
208 return (ntags() ? (ntags() > 1) : (nifile() > 1));
210 return (nifile() > 1);
212 case 'n': /* First prompt in a new file? */
214 return (ntags() ? 1 : new_file);
218 case 'p': /* Percent into file (bytes) known? */
219 return (curr_byte(where) != NULL_POSITION &&
221 case 'P': /* Percent into file (lines) known? */
222 return (currline(where) != 0 &&
223 (len = ch_length()) > 0 &&
224 find_linenum(len) != 0);
225 case 's': /* Size of file known? */
227 return (ch_length() != NULL_POSITION);
228 case 'x': /* Is there a "next" file? */
233 return (next_ifile(curr_ifile) != NULL_IFILE);
239 * Decode a "percent" prototype character.
240 * A prototype string may include various "percent" escapes;
241 * that is, a percent sign followed by a single letter.
242 * Here we decode that letter and take the appropriate action,
243 * usually by appending something to the message being built.
246 protochar(c, where, iseditproto)
255 LINENUM last_linenum;
259 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
263 case 'b': /* Current byte offset */
264 pos = curr_byte(where);
265 if (pos != NULL_POSITION)
273 case 'd': /* Current page number */
274 linenum = currline(where);
275 if (linenum > 0 && sc_height > 1)
276 ap_linenum(PAGE_NUM(linenum));
280 case 'D': /* Final page number */
281 /* Find the page number of the last byte in the file (len-1). */
283 if (len == NULL_POSITION)
286 /* An empty file has no pages. */
290 linenum = find_linenum(len - 1);
294 ap_linenum(PAGE_NUM(linenum));
298 case 'E': /* Editor name */
302 case 'f': /* File name */
303 ap_str(get_filename(curr_ifile));
305 case 'i': /* Index into list of files */
311 ap_int(get_index(curr_ifile));
313 case 'l': /* Current line number */
314 linenum = currline(where);
320 case 'L': /* Final line number */
322 if (len == NULL_POSITION || len == ch_zero() ||
323 (linenum = find_linenum(len)) <= 0)
326 ap_linenum(linenum-1);
328 case 'm': /* Number of files */
337 case 'p': /* Percent into file (bytes) */
338 pos = curr_byte(where);
340 if (pos != NULL_POSITION && len > 0)
341 ap_int(percentage(pos,len));
345 case 'P': /* Percent into file (lines) */
346 linenum = currline(where);
348 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
349 (last_linenum = find_linenum(len)) <= 0)
352 ap_int(percentage(linenum, last_linenum));
354 case 's': /* Size of file */
357 if (len != NULL_POSITION)
362 case 't': /* Truncate trailing spaces in the message */
363 while (mp > message && mp[-1] == ' ')
366 case 'T': /* Type of list */
374 case 'x': /* Name of next file */
375 h = next_ifile(curr_ifile);
377 ap_str(get_filename(h));
385 * Skip a false conditional.
386 * When a false condition is found (either a false IF or the ELSE part
387 * of a true IF), this routine scans the prototype string to decide
388 * where to resume parsing the string.
389 * We must keep track of nested IFs and skip them properly.
395 register int iflevel;
398 * We came in here after processing a ? or :,
399 * so we start nested one level deep.
403 for (;;) switch (*++p)
407 * Start of a nested IF.
414 * If this matches the IF we came in here with,
423 * If this matches the IF we came in here with,
431 * Backslash escapes the next character.
437 * Whoops. Hit end of string.
438 * This is a malformed conditional, but just treat it
439 * as if all active conditionals ends here.
447 * Decode a char that represents a position on the screen.
456 case 'b': case 'd': case 'l': case 'p': case 'P':
459 case 't': *wp = TOP; break;
460 case 'm': *wp = MIDDLE; break;
461 case 'b': *wp = BOTTOM; break;
462 case 'B': *wp = BOTTOM_PLUS_ONE; break;
463 case 'j': *wp = adjsline(jump_sline); break;
464 default: *wp = TOP; p--; break;
471 * Construct a message based on a prototype string.
474 pr_expand(proto, maxwidth)
487 for (p = proto; *p != '\0'; p++)
491 default: /* Just put the character in the message */
494 case '\\': /* Backslash escapes the next character */
498 case '?': /* Conditional (IF) */
499 if ((c = *++p) == '\0')
504 p = wherechar(p, &where);
512 case '.': /* ENDIF */
514 case '%': /* Percent escape */
515 if ((c = *++p) == '\0')
520 p = wherechar(p, &where);
523 (proto == editproto));
535 if (maxwidth > 0 && mp >= message + maxwidth)
538 * Message is too long.
539 * Return just the final portion of it.
541 return (mp - maxwidth);
547 * Return a message suitable for printing by the "=" command.
552 return (pr_expand(eqproto, 0));
557 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
558 * If we can't come up with an appropriate prompt, return NULL
559 * and the caller will prompt with a colon.
566 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
567 hproto : prproto[pr_type],
568 sc_width-so_s_width-so_e_width-2);
574 * Return a message suitable for printing while waiting in the F command.
579 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));