2 * Copyright (C) 1984-2022 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.
70 init_prompt(VOID_PARAM
)
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.
89 len
= (int) strlen(s
);
90 if (mp
+ len
>= message
+ PROMPT_SIZE
)
91 len
= (int) (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? */
201 return (strcmp(get_filename(curr_ifile
), "-") != 0);
202 case 'l': /* Line number known? */
203 case 'd': /* Same as l */
206 return (currline(where
) != 0);
207 case 'L': /* Final line number known? */
208 case 'D': /* Final page number known? */
209 return (linenums
&& ch_length() != NULL_POSITION
);
210 case 'm': /* More than one file? */
212 return (ntags() ? (ntags() > 1) : (nifile() > 1));
214 return (nifile() > 1);
216 case 'n': /* First prompt in a new file? */
218 return (ntags() ? 1 : new_file
);
222 case 'p': /* Percent into file (bytes) known? */
223 return (curr_byte(where
) != NULL_POSITION
&&
225 case 'P': /* Percent into file (lines) known? */
226 return (currline(where
) != 0 &&
227 (len
= ch_length()) > 0 &&
228 find_linenum(len
) != 0);
229 case 's': /* Size of file known? */
231 return (ch_length() != NULL_POSITION
);
232 case 'x': /* Is there a "next" file? */
237 return (next_ifile(curr_ifile
) != NULL_IFILE
);
243 * Decode a "percent" prototype character.
244 * A prototype string may include various "percent" escapes;
245 * that is, a percent sign followed by a single letter.
246 * Here we decode that letter and take the appropriate action,
247 * usually by appending something to the message being built.
250 protochar(c
, where
, iseditproto
)
259 LINENUM last_linenum
;
264 #define PAGE_NUM(linenum) ((((linenum) - 1) / (sc_height - header_lines - 1)) + 1)
268 case 'b': /* Current byte offset */
269 pos
= curr_byte(where
);
270 if (pos
!= NULL_POSITION
)
278 case 'd': /* Current page number */
279 linenum
= currline(where
);
280 if (linenum
> 0 && sc_height
> header_lines
+ 1)
281 ap_linenum(PAGE_NUM(linenum
));
285 case 'D': /* Final page number */
286 /* Find the page number of the last byte in the file (len-1). */
288 if (len
== NULL_POSITION
)
291 /* An empty file has no pages. */
295 linenum
= find_linenum(len
- 1);
299 ap_linenum(PAGE_NUM(linenum
));
303 case 'E': /* Editor name */
307 case 'f': /* File name */
308 ap_str(get_filename(curr_ifile
));
310 case 'F': /* Last component of file name */
311 ap_str(last_component(get_filename(curr_ifile
)));
313 case 'g': /* Shell-escaped file name */
314 s
= shell_quote(get_filename(curr_ifile
));
318 case 'i': /* Index into list of files */
324 ap_int(get_index(curr_ifile
));
326 case 'l': /* Current line number */
327 linenum
= currline(where
);
329 ap_linenum(vlinenum(linenum
));
333 case 'L': /* Final line number */
335 if (len
== NULL_POSITION
|| len
== ch_zero() ||
336 (linenum
= find_linenum(len
)) <= 0)
339 ap_linenum(vlinenum(linenum
-1));
341 case 'm': /* Number of files */
350 case 'p': /* Percent into file (bytes) */
351 pos
= curr_byte(where
);
353 if (pos
!= NULL_POSITION
&& len
> 0)
354 ap_int(percentage(pos
,len
));
358 case 'P': /* Percent into file (lines) */
359 linenum
= currline(where
);
361 (len
= ch_length()) == NULL_POSITION
|| len
== ch_zero() ||
362 (last_linenum
= find_linenum(len
)) <= 0)
365 ap_int(percentage(linenum
, last_linenum
));
367 case 's': /* Size of file */
370 if (len
!= NULL_POSITION
)
375 case 't': /* Truncate trailing spaces in the message */
376 while (mp
> message
&& mp
[-1] == ' ')
380 case 'T': /* Type of list */
388 case 'x': /* Name of next file */
389 h
= next_ifile(curr_ifile
);
391 ap_str(get_filename(h
));
399 * Skip a false conditional.
400 * When a false condition is found (either a false IF or the ELSE part
401 * of a true IF), this routine scans the prototype string to decide
402 * where to resume parsing the string.
403 * We must keep track of nested IFs and skip them properly.
405 static constant
char *
412 * We came in here after processing a ? or :,
413 * so we start nested one level deep.
417 for (;;) switch (*++p
)
421 * Start of a nested IF.
428 * If this matches the IF we came in here with,
437 * If this matches the IF we came in here with,
445 * Backslash escapes the next character.
451 * Whoops. Hit end of string.
452 * This is a malformed conditional, but just treat it
453 * as if all active conditionals ends here.
461 * Decode a char that represents a position on the screen.
463 static constant
char *
470 case 'b': case 'd': case 'l': case 'p': case 'P':
473 case 't': *wp
= TOP
; break;
474 case 'm': *wp
= MIDDLE
; break;
475 case 'b': *wp
= BOTTOM
; break;
476 case 'B': *wp
= BOTTOM_PLUS_ONE
; break;
477 case 'j': *wp
= sindex_from_sline(jump_sline
); break;
478 default: *wp
= TOP
; p
--; break;
485 * Construct a message based on a prototype string.
489 constant
char *proto
;
500 for (p
= proto
; *p
!= '\0'; p
++)
504 default: /* Just put the character in the message */
507 case '\\': /* Backslash escapes the next character */
511 case '?': /* Conditional (IF) */
512 if ((c
= *++p
) == '\0')
517 p
= wherechar(p
, &where
);
525 case '.': /* ENDIF */
527 case '%': /* Percent escape */
528 if ((c
= *++p
) == '\0')
533 p
= wherechar(p
, &where
);
536 (proto
== editproto
));
552 * Return a message suitable for printing by the "=" command.
555 eq_message(VOID_PARAM
)
557 return (pr_expand(eqproto
));
562 * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
563 * If we can't come up with an appropriate prompt, return NULL
564 * and the caller will prompt with a colon.
567 pr_string(VOID_PARAM
)
572 type
= (!less_is_more
) ? pr_type
: pr_type
? 0 : 1;
573 prompt
= pr_expand((ch_getflags() & CH_HELPFILE
) ?
574 hproto
: prproto
[type
]);
580 * Return a message suitable for printing while waiting in the F command.
583 wait_message(VOID_PARAM
)
585 return (pr_expand(wproto
));