2 * Copyright (C) 1984-2012 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, see the README file.
12 * Prompting and other messages.
13 * There are three flavors of prompts, SHORT, MEDIUM and LONG,
14 * selected by the -m/-M options.
15 * There is also the "equals message", printed by the = command.
16 * A prompt is a message composed of various pieces, such as the
17 * name of the file being viewed, the percentage into the file, etc.
26 extern int so_s_width, so_e_width;
30 extern int jump_sline;
31 extern int less_is_more;
32 extern IFILE curr_ifile;
35 extern char *editproto;
39 * Prototypes for the three flavors of prompts.
40 * These strings are expanded by pr_expand().
42 static constant char s_proto[] =
43 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x..%t";
44 static constant char m_proto[] =
45 "?n?f%f .?m(%T %i of %m) ..?e(END) ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t";
46 static constant char M_proto[] =
47 "?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";
48 static constant char e_proto[] =
49 "?f%f .?m(%T %i of %m) .?ltlines %lt-%lb?L/%L. .byte %bB?s/%s. ?e(END) :?pB%pB\\%..%t";
50 static constant char h_proto[] =
51 "HELP -- ?eEND -- Press g to see it again:Press RETURN for more., or q when done";
52 static constant char w_proto[] =
54 static constant char more_proto[] =
55 "--More--(?eEND ?x- Next\\: %x.:?pB%pB\\%:byte %bB?s/%s...%t)";
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(less_is_more ? more_proto : 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? */
197 return (eof_displayed());
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 'F': /* Last component of file name */
306 ap_str(last_component(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] == ' ')
370 case 'T': /* Type of list */
378 case 'x': /* Name of next file */
379 h = next_ifile(curr_ifile);
381 ap_str(get_filename(h));
389 * Skip a false conditional.
390 * When a false condition is found (either a false IF or the ELSE part
391 * of a true IF), this routine scans the prototype string to decide
392 * where to resume parsing the string.
393 * We must keep track of nested IFs and skip them properly.
395 static constant char *
397 register constant char *p;
399 register int iflevel;
402 * We came in here after processing a ? or :,
403 * so we start nested one level deep.
407 for (;;) switch (*++p)
411 * Start of a nested IF.
418 * If this matches the IF we came in here with,
427 * If this matches the IF we came in here with,
435 * Backslash escapes the next character.
441 * Whoops. Hit end of string.
442 * This is a malformed conditional, but just treat it
443 * as if all active conditionals ends here.
451 * Decode a char that represents a position on the screen.
453 static constant char *
460 case 'b': case 'd': case 'l': case 'p': case 'P':
463 case 't': *wp = TOP; break;
464 case 'm': *wp = MIDDLE; break;
465 case 'b': *wp = BOTTOM; break;
466 case 'B': *wp = BOTTOM_PLUS_ONE; break;
467 case 'j': *wp = adjsline(jump_sline); break;
468 default: *wp = TOP; p--; break;
475 * Construct a message based on a prototype string.
478 pr_expand(proto, maxwidth)
479 constant char *proto;
482 register constant char *p;
491 for (p = proto; *p != '\0'; p++)
495 default: /* Just put the character in the message */
498 case '\\': /* Backslash escapes the next character */
502 case '?': /* Conditional (IF) */
503 if ((c = *++p) == '\0')
508 p = wherechar(p, &where);
516 case '.': /* ENDIF */
518 case '%': /* Percent escape */
519 if ((c = *++p) == '\0')
524 p = wherechar(p, &where);
527 (proto == editproto));
539 if (maxwidth > 0 && mp >= message + maxwidth)
542 * Message is too long.
543 * Return just the final portion of it.
545 return (mp - maxwidth);
551 * Return a message suitable for printing by the "=" command.
556 return (pr_expand(eqproto, 0));
561 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
562 * If we can't come up with an appropriate prompt, return NULL
563 * and the caller will prompt with a colon.
571 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
572 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
573 hproto : prproto[type],
574 sc_width-so_s_width-so_e_width-2);
580 * Return a message suitable for printing while waiting in the F command.
585 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));