2 * Copyright (C) 1984-2023 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 int header_lines;
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.
69 public void init_prompt(void)
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.
82 static void ap_str(char *s)
86 len = (int) strlen(s);
87 if (mp + len >= message + PROMPT_SIZE)
88 len = (int) (message + PROMPT_SIZE - mp - 1);
95 * Append a character to the end of the message.
97 static void ap_char(char c)
107 * Append a POSITION (as a decimal integer) to the end of the message.
109 static void ap_pos(POSITION pos)
111 char buf[INT_STRLEN_BOUND(pos) + 2];
113 postoa(pos, buf, 10);
118 * Append a line number to the end of the message.
120 static void ap_linenum(LINENUM linenum)
122 char buf[INT_STRLEN_BOUND(linenum) + 2];
124 linenumtoa(linenum, buf, 10);
129 * Append an integer to the end of the message.
131 static void ap_int(int num)
133 char buf[INT_STRLEN_BOUND(num) + 2];
135 inttoa(num, buf, 10);
140 * Append a question mark to the end of the message.
142 static void ap_quest(void)
148 * Return the "current" byte offset in the file.
150 static POSITION curr_byte(int where)
154 pos = position(where);
155 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
156 pos = position(++where);
157 if (pos == NULL_POSITION)
163 * Return the value of a prototype conditional.
164 * A prototype string may include conditionals which consist of a
165 * question mark followed by a single letter.
166 * Here we decode that letter and return the appropriate boolean value.
168 static int cond(char c, int where)
174 case 'a': /* Anything in the message yet? */
175 return (mp > message);
176 case 'b': /* Current byte offset known? */
177 return (curr_byte(where) != NULL_POSITION);
179 return (hshift != 0);
180 case 'e': /* At end of file? */
181 return (eof_displayed());
182 case 'f': /* Filename known? */
184 return (strcmp(get_filename(curr_ifile), "-") != 0);
185 case 'l': /* Line number known? */
186 case 'd': /* Same as l */
189 return (currline(where) != 0);
190 case 'L': /* Final line number known? */
191 case 'D': /* Final page number known? */
192 return (linenums && ch_length() != NULL_POSITION);
193 case 'm': /* More than one file? */
195 return (ntags() ? (ntags() > 1) : (nifile() > 1));
197 return (nifile() > 1);
199 case 'n': /* First prompt in a new file? */
201 return (ntags() ? 1 : new_file);
205 case 'p': /* Percent into file (bytes) known? */
206 return (curr_byte(where) != NULL_POSITION &&
208 case 'P': /* Percent into file (lines) known? */
209 return (currline(where) != 0 &&
210 (len = ch_length()) > 0 &&
211 find_linenum(len) != 0);
212 case 's': /* Size of file known? */
214 return (ch_length() != NULL_POSITION);
215 case 'x': /* Is there a "next" file? */
220 return (next_ifile(curr_ifile) != NULL_IFILE);
226 * Decode a "percent" prototype character.
227 * A prototype string may include various "percent" escapes;
228 * that is, a percent sign followed by a single letter.
229 * Here we decode that letter and take the appropriate action,
230 * usually by appending something to the message being built.
232 static void protochar(int c, int where, int iseditproto)
238 LINENUM last_linenum;
243 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - header_lines - 1)) + 1)
247 case 'b': /* Current byte offset */
248 pos = curr_byte(where);
249 if (pos != NULL_POSITION)
257 case 'd': /* Current page number */
258 linenum = currline(where);
259 if (linenum > 0 && sc_height > header_lines + 1)
260 ap_linenum(PAGE_NUM(linenum));
264 case 'D': /* Final page number */
265 /* Find the page number of the last byte in the file (len-1). */
267 if (len == NULL_POSITION)
270 /* An empty file has no pages. */
274 linenum = find_linenum(len - 1);
278 ap_linenum(PAGE_NUM(linenum));
282 case 'E': /* Editor name */
286 case 'f': /* File name */
287 ap_str(get_filename(curr_ifile));
289 case 'F': /* Last component of file name */
290 ap_str(last_component(get_filename(curr_ifile)));
292 case 'g': /* Shell-escaped file name */
293 s = shell_quote(get_filename(curr_ifile));
297 case 'i': /* Index into list of files */
303 ap_int(get_index(curr_ifile));
305 case 'l': /* Current line number */
306 linenum = currline(where);
308 ap_linenum(vlinenum(linenum));
312 case 'L': /* Final line number */
314 if (len == NULL_POSITION || len == ch_zero() ||
315 (linenum = find_linenum(len)) <= 0)
318 ap_linenum(vlinenum(linenum-1));
320 case 'm': /* Number of files */
329 case 'p': /* Percent into file (bytes) */
330 pos = curr_byte(where);
332 if (pos != NULL_POSITION && len > 0)
333 ap_int(percentage(pos,len));
337 case 'P': /* Percent into file (lines) */
338 linenum = currline(where);
340 (len = ch_length()) == NULL_POSITION || len == ch_zero() ||
341 (last_linenum = find_linenum(len)) <= 0)
344 ap_int(percentage(linenum, last_linenum));
346 case 's': /* Size of file */
349 if (len != NULL_POSITION)
354 case 't': /* Truncate trailing spaces in the message */
355 while (mp > message && mp[-1] == ' ')
359 case 'T': /* Type of list */
367 case 'x': /* Name of next file */
368 h = next_ifile(curr_ifile);
370 ap_str(get_filename(h));
378 * Skip a false conditional.
379 * When a false condition is found (either a false IF or the ELSE part
380 * of a true IF), this routine scans the prototype string to decide
381 * where to resume parsing the string.
382 * We must keep track of nested IFs and skip them properly.
384 static constant char * skipcond(constant char *p)
389 * We came in here after processing a ? or :,
390 * so we start nested one level deep.
394 for (;;) switch (*++p)
398 * Start of a nested IF.
405 * If this matches the IF we came in here with,
414 * If this matches the IF we came in here with,
422 * Backslash escapes the next character.
429 * Whoops. Hit end of string.
430 * This is a malformed conditional, but just treat it
431 * as if all active conditionals ends here.
439 * Decode a char that represents a position on the screen.
441 static constant char * wherechar(char constant *p, int *wp)
445 case 'b': case 'd': case 'l': case 'p': case 'P':
448 case 't': *wp = TOP; break;
449 case 'm': *wp = MIDDLE; break;
450 case 'b': *wp = BOTTOM; break;
451 case 'B': *wp = BOTTOM_PLUS_ONE; break;
452 case 'j': *wp = sindex_from_sline(jump_sline); break;
453 default: *wp = TOP; p--; break;
460 * Construct a message based on a prototype string.
462 public char * pr_expand(constant char *proto)
473 for (p = proto; *p != '\0'; p++)
477 default: /* Just put the character in the message */
480 case '\\': /* Backslash escapes the next character */
484 case '?': /* Conditional (IF) */
485 if ((c = *++p) == '\0')
490 p = wherechar(p, &where);
498 case '.': /* ENDIF */
500 case '%': /* Percent escape */
501 if ((c = *++p) == '\0')
506 p = wherechar(p, &where);
509 (proto == editproto));
525 * Return a message suitable for printing by the "=" command.
527 public char * eq_message(void)
529 return (pr_expand(eqproto));
534 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
535 * If we can't come up with an appropriate prompt, return NULL
536 * and the caller will prompt with a colon.
538 public char * pr_string(void)
543 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
544 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
545 hproto : prproto[type]);
551 * Return a message suitable for printing while waiting in the F command.
553 public char * wait_message(void)
555 return (pr_expand(wproto));