2 * Copyright (C) 1984-2015 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.
87 len = (int) strlen(s);
88 if (mp + len >= message + PROMPT_SIZE)
89 len = (int) (message + PROMPT_SIZE - mp - 1);
96 * Append a character to the end of the message.
109 * Append a POSITION (as a decimal integer) to the end of the message.
114 char buf[INT_STRLEN_BOUND(pos) + 2];
121 * Append a line number to the end of the message.
124 ap_linenum(LINENUM linenum)
126 char buf[INT_STRLEN_BOUND(linenum) + 2];
128 linenumtoa(linenum, buf);
133 * Append an integer to the end of the message.
138 char buf[INT_STRLEN_BOUND(num) + 2];
145 * Append a question mark to the end of the message.
154 * Return the "current" byte offset in the file.
161 pos = position(where);
162 while (pos == NULL_POSITION && where >= 0 && where < sc_height-1)
163 pos = position(++where);
164 if (pos == NULL_POSITION)
170 * Return the value of a prototype conditional.
171 * A prototype string may include conditionals which consist of a
172 * question mark followed by a single letter.
173 * Here we decode that letter and return the appropriate boolean value.
176 cond(char c, int where)
182 case 'a': /* Anything in the message yet? */
183 return (mp > message);
184 case 'b': /* Current byte offset known? */
185 return (curr_byte(where) != NULL_POSITION);
187 return (hshift != 0);
188 case 'e': /* At end of file? */
189 return (eof_displayed());
190 case 'f': /* Filename known? */
191 return (strcmp(get_filename(curr_ifile), "-") != 0);
192 case 'l': /* Line number known? */
193 case 'd': /* Same as l */
195 case 'L': /* Final line number known? */
196 case 'D': /* Final page number known? */
197 return (linenums && ch_length() != NULL_POSITION);
198 case 'm': /* More than one file? */
200 return (ntags() ? (ntags() > 1) : (nifile() > 1));
202 return (nifile() > 1);
204 case 'n': /* First prompt in a new file? */
206 return (ntags() ? 1 : new_file);
210 case 'p': /* Percent into file (bytes) known? */
211 return (curr_byte(where) != NULL_POSITION &&
213 case 'P': /* Percent into file (lines) known? */
214 return (currline(where) != 0 &&
215 (len = ch_length()) > 0 &&
216 find_linenum(len) != 0);
217 case 's': /* Size of file known? */
219 return (ch_length() != NULL_POSITION);
220 case 'x': /* Is there a "next" file? */
225 return (next_ifile(curr_ifile) != NULL_IFILE);
231 * Decode a "percent" prototype character.
232 * A prototype string may include various "percent" escapes;
233 * that is, a percent sign followed by a single letter.
234 * Here we decode that letter and take the appropriate action,
235 * usually by appending something to the message being built.
238 protochar(int c, int where, int iseditproto)
244 LINENUM last_linenum;
248 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - 1)) + 1)
252 case 'b': /* Current byte offset */
253 pos = curr_byte(where);
254 if (pos != NULL_POSITION)
262 case 'd': /* Current page number */
263 linenum = currline(where);
264 if (linenum > 0 && sc_height > 1)
265 ap_linenum(PAGE_NUM(linenum));
269 case 'D': /* Final page number */
270 /* Find the page number of the last byte in the file (len-1). */
272 if (len == NULL_POSITION)
275 /* An empty file has no pages. */
279 linenum = find_linenum(len - 1);
283 ap_linenum(PAGE_NUM(linenum));
287 case 'E': /* Editor name */
291 case 'f': /* File name */
292 ap_str(get_filename(curr_ifile));
294 case 'F': /* Last component of file name */
295 ap_str(last_component(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);
312 case 'L': /* Final line number */
314 if (len == NULL_POSITION || len == ch_zero() ||
315 (linenum = find_linenum(len)) <= 0)
318 ap_linenum(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 *
385 skipcond(constant char *p)
390 * We came in here after processing a ? or :,
391 * so we start nested one level deep.
395 for (;;) switch (*++p)
399 * Start of a nested IF.
406 * If this matches the IF we came in here with,
415 * If this matches the IF we came in here with,
423 * 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 *
442 wherechar(char constant *p, int *wp)
446 case 'b': case 'd': case 'l': case 'p': case 'P':
449 case 't': *wp = TOP; break;
450 case 'm': *wp = MIDDLE; break;
451 case 'b': *wp = BOTTOM; break;
452 case 'B': *wp = BOTTOM_PLUS_ONE; break;
453 case 'j': *wp = adjsline(jump_sline); break;
454 default: *wp = TOP; p--; break;
461 * Construct a message based on a prototype string.
464 pr_expand(constant char *proto, int maxwidth)
475 for (p = proto; *p != '\0'; p++)
479 default: /* Just put the character in the message */
482 case '\\': /* Backslash escapes the next character */
486 case '?': /* Conditional (IF) */
487 if ((c = *++p) == '\0')
492 p = wherechar(p, &where);
500 case '.': /* ENDIF */
502 case '%': /* Percent escape */
503 if ((c = *++p) == '\0')
508 p = wherechar(p, &where);
511 (proto == editproto));
523 if (maxwidth > 0 && mp >= message + maxwidth)
526 * Message is too long.
527 * Return just the final portion of it.
529 return (mp - maxwidth);
535 * Return a message suitable for printing by the "=" command.
540 return (pr_expand(eqproto, 0));
545 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
546 * If we can't come up with an appropriate prompt, return NULL
547 * and the caller will prompt with a colon.
555 type = (!less_is_more) ? pr_type : pr_type ? 0 : 1;
556 prompt = pr_expand((ch_getflags() & CH_HELPFILE) ?
557 hproto : prproto[type],
558 sc_width-so_s_width-so_e_width-2);
564 * Return a message suitable for printing while waiting in the F command.
569 return (pr_expand(wproto, sc_width-so_s_width-so_e_width-2));