2 * Copyright (C) 1984-2021 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.
19 * Variables controlled by command line options.
21 public int quiet; /* Should we suppress the audible bell? */
22 public int how_search; /* Where should forward searches start? */
23 public int top_scroll; /* Repaint screen from top?
24 (alternative is scroll from bottom) */
25 public int pr_type; /* Type of prompt (short, medium, long) */
26 public int bs_mode; /* How to process backspaces */
27 public int know_dumb; /* Don't complain about dumb terminals */
28 public int quit_at_eof; /* Quit after hitting end of file twice */
29 public int quit_if_one_screen; /* Quit if EOF on first screen */
30 public int squeeze; /* Squeeze multiple blank lines into one */
31 public int tabstop; /* Tab settings */
32 public int back_scroll; /* Repaint screen on backwards movement */
33 public int forw_scroll; /* Repaint screen on forward movement */
34 public int caseless; /* Do "caseless" searches */
35 public int linenums; /* Use line numbers */
36 public int autobuf; /* Automatically allocate buffers as needed */
37 public int bufspace; /* Max buffer space per file (K) */
38 public int ctldisp; /* Send control chars to screen untranslated */
39 public int force_open; /* Open the file even if not regular file */
40 public int swindow; /* Size of scrolling window */
41 public int jump_sline; /* Screen line of "jump target" */
42 public long jump_sline_fraction = -1;
43 public long shift_count_fraction = -1;
44 public int chopline; /* Truncate displayed lines at screen width */
45 public int no_init; /* Disable sending ti/te termcap strings */
46 public int no_keypad; /* Disable sending ks/ke termcap strings */
47 public int twiddle; /* Show tildes after EOF */
48 public int show_attn; /* Hilite first unread line */
49 public int shift_count; /* Number of positions to shift horizontally */
50 public int status_col; /* Display a status column */
51 public int use_lessopen; /* Use the LESSOPEN filter */
52 public int quit_on_intr; /* Quit on interrupt */
53 public int follow_mode; /* F cmd Follows file desc or file name? */
54 public int oldbot; /* Old bottom of screen behavior {{REMOVE}} */
55 public int opt_use_backslash; /* Use backslash escaping in option parsing */
56 public char rscroll_char; /* Char which marks chopped lines with -S */
57 public int rscroll_attr; /* Attribute of rscroll_char */
58 public int no_hist_dups; /* Remove dups from history list */
59 public int mousecap; /* Allow mouse for scrolling */
60 public int wheel_lines; /* Number of lines to scroll on mouse wheel scroll */
61 public int perma_marks; /* Save marks in history file */
62 public int linenum_width; /* Width of line numbers */
63 public int status_col_width; /* Width of status column */
64 public int incr_search; /* Incremental search */
65 public int use_color; /* Use UI color */
66 public int want_filesize; /* */
68 public int hilite_search; /* Highlight matched search patterns? */
71 public int less_is_more = 0; /* Make compatible with POSIX more */
76 static struct optname a_optname = { "search-skip-screen", NULL };
77 static struct optname b_optname = { "buffers", NULL };
78 static struct optname B__optname = { "auto-buffers", NULL };
79 static struct optname c_optname = { "clear-screen", NULL };
80 static struct optname d_optname = { "dumb", NULL };
81 static struct optname D__optname = { "color", NULL };
82 static struct optname e_optname = { "quit-at-eof", NULL };
83 static struct optname f_optname = { "force", NULL };
84 static struct optname F__optname = { "quit-if-one-screen", NULL };
86 static struct optname g_optname = { "hilite-search", NULL };
88 static struct optname h_optname = { "max-back-scroll", NULL };
89 static struct optname i_optname = { "ignore-case", NULL };
90 static struct optname j_optname = { "jump-target", NULL };
91 static struct optname J__optname = { "status-column", NULL };
93 static struct optname k_optname = { "lesskey-file", NULL };
95 static struct optname ks_optname = { "lesskey-src", NULL };
96 #endif /* HAVE_LESSKEYSRC */
98 static struct optname K__optname = { "quit-on-intr", NULL };
99 static struct optname L__optname = { "no-lessopen", NULL };
100 static struct optname m_optname = { "long-prompt", NULL };
101 static struct optname n_optname = { "line-numbers", NULL };
103 static struct optname o_optname = { "log-file", NULL };
104 static struct optname O__optname = { "LOG-FILE", NULL };
106 static struct optname p_optname = { "pattern", NULL };
107 static struct optname P__optname = { "prompt", NULL };
108 static struct optname q2_optname = { "silent", NULL };
109 static struct optname q_optname = { "quiet", &q2_optname };
110 static struct optname r_optname = { "raw-control-chars", NULL };
111 static struct optname s_optname = { "squeeze-blank-lines", NULL };
112 static struct optname S__optname = { "chop-long-lines", NULL };
114 static struct optname t_optname = { "tag", NULL };
115 static struct optname T__optname = { "tag-file", NULL };
117 static struct optname u_optname = { "underline-special", NULL };
118 static struct optname V__optname = { "version", NULL };
119 static struct optname w_optname = { "hilite-unread", NULL };
120 static struct optname x_optname = { "tabs", NULL };
121 static struct optname X__optname = { "no-init", NULL };
122 static struct optname y_optname = { "max-forw-scroll", NULL };
123 static struct optname z_optname = { "window", NULL };
124 static struct optname quote_optname = { "quotes", NULL };
125 static struct optname tilde_optname = { "tilde", NULL };
126 static struct optname query_optname = { "help", NULL };
127 static struct optname pound_optname = { "shift", NULL };
128 static struct optname keypad_optname = { "no-keypad", NULL };
129 static struct optname oldbot_optname = { "old-bot", NULL };
130 static struct optname follow_optname = { "follow-name", NULL };
131 static struct optname use_backslash_optname = { "use-backslash", NULL };
132 static struct optname rscroll_optname = { "rscroll", NULL };
133 static struct optname nohistdups_optname = { "no-histdups", NULL };
134 static struct optname mousecap_optname = { "mouse", NULL };
135 static struct optname wheel_lines_optname = { "wheel-lines", NULL };
136 static struct optname perma_marks_optname = { "save-marks", NULL };
137 static struct optname linenum_width_optname = { "line-num-width", NULL };
138 static struct optname status_col_width_optname = { "status-col-width", NULL };
139 static struct optname incr_search_optname = { "incsearch", NULL };
140 static struct optname use_color_optname = { "use-color", NULL };
141 static struct optname want_filesize_optname = { "file-size", NULL };
143 static struct optname ttyin_name_optname = { "tty", NULL };
144 static struct optname rstat_optname = { "rstat", NULL };
149 * Table of all options and their semantics.
151 * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
152 * the description of the option when set to 0, 1 or 2, respectively.
153 * For NUMBER options, odesc[0] is the prompt to use when entering
154 * a new value, and odesc[1] is the description, which should contain
155 * one %d which is replaced by the value of the number.
156 * For STRING options, odesc[0] is the prompt to use when entering
157 * a new value, and odesc[1], if not NULL, is the set of characters
158 * that are valid in the string.
160 static struct loption option[] =
163 TRIPLE, OPT_ONPLUS, &how_search, NULL,
165 "Search includes displayed screen",
166 "Search skips displayed screen",
167 "Search includes all of displayed screen"
172 NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
174 "Max buffer space per file (K): ",
175 "Max buffer space per file: %dK",
180 BOOL, OPT_ON, &autobuf, NULL,
182 "Don't automatically allocate buffers",
183 "Automatically allocate buffers when needed",
188 TRIPLE, OPT_OFF, &top_scroll, NULL,
190 "Repaint by scrolling from bottom of screen",
191 "Repaint by painting from top of screen",
192 "Repaint by painting from top of screen"
196 BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
198 "Assume intelligent terminal",
199 "Assume dumb terminal",
204 STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
212 TRIPLE, OPT_OFF, &quit_at_eof, NULL,
214 "Don't quit at end-of-file",
215 "Quit at end-of-file",
216 "Quit immediately at end-of-file"
220 BOOL, OPT_OFF, &force_open, NULL,
222 "Open only regular files",
223 "Open even non-regular files",
228 BOOL, OPT_OFF, &quit_if_one_screen, NULL,
230 "Don't quit if end-of-file on first screen",
231 "Quit if end-of-file on first screen",
237 TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
239 "Don't highlight search matches",
240 "Highlight matches for previous search only",
241 "Highlight all matches for previous search pattern",
246 NUMBER, -1, &back_scroll, NULL,
248 "Backwards scroll limit: ",
249 "Backwards scroll limit is %d lines",
254 TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
256 "Case is significant in searches",
257 "Ignore case in searches",
258 "Ignore case in searches and in patterns"
262 STRING, 0, NULL, opt_j,
270 BOOL|REPAINT, OPT_OFF, &status_col, NULL,
272 "Don't display a status column",
273 "Display a status column",
279 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
283 { OLETTER_NONE, &ks_optname,
284 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_ks,
287 #endif /* HAVE_LESSKEYSRC */
290 BOOL, OPT_OFF, &quit_on_intr, NULL,
292 "Interrupt (ctrl-C) returns to prompt",
293 "Interrupt (ctrl-C) exits less",
298 BOOL, OPT_ON, &use_lessopen, NULL,
300 "Don't use the LESSOPEN filter",
301 "Use the LESSOPEN filter",
306 TRIPLE, OPT_OFF, &pr_type, NULL,
314 TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
316 "Don't use line numbers",
318 "Constantly display line numbers"
323 STRING, 0, NULL, opt_o,
324 { "log file: ", NULL, NULL }
327 STRING, 0, NULL, opt__O,
328 { "Log file: ", NULL, NULL }
332 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
336 STRING, 0, NULL, opt__P,
337 { "prompt: ", NULL, NULL }
340 TRIPLE, OPT_OFF, &quiet, NULL,
342 "Ring the bell for errors AND at eof/bof",
343 "Ring the bell for errors but not at eof/bof",
344 "Never ring the bell"
348 TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
350 "Display control characters as ^X",
351 "Display control characters directly (not recommended)",
352 "Display ANSI sequences directly, other control characters as ^X"
356 BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
358 "Display all blank lines",
359 "Squeeze multiple blank lines",
364 BOOL|REPAINT, OPT_OFF, &chopline, NULL,
373 STRING|NO_QUERY, 0, NULL, opt_t,
374 { "tag: ", NULL, NULL }
377 STRING, 0, NULL, opt__T,
378 { "tags file: ", NULL, NULL }
382 TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
384 "Display underlined text in underline mode",
385 "Backspaces cause overstrike",
386 "Print backspace as ^H"
390 NOVAR, 0, NULL, opt__V,
394 TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
396 "Don't highlight first unread line",
397 "Highlight first unread line after forward-screen",
398 "Highlight first unread line after any forward movement",
402 STRING|REPAINT, 0, NULL, opt_x,
410 BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
412 "Send init/deinit strings to terminal",
413 "Don't use init/deinit strings",
418 NUMBER, -1, &forw_scroll, NULL,
420 "Forward scroll limit: ",
421 "Forward scroll limit is %d lines",
426 NUMBER, -1, &swindow, NULL,
428 "Scroll window size: ",
429 "Scroll window size is %d lines",
433 { '"', "e_optname,
434 STRING, 0, NULL, opt_quote,
435 { "quotes: ", NULL, NULL }
437 { '~', &tilde_optname,
438 BOOL|REPAINT, OPT_ON, &twiddle, NULL,
440 "Don't show tildes after end of file",
441 "Show tildes after end of file",
445 { '?', &query_optname,
446 NOVAR, 0, NULL, opt_query,
449 { '#', £_optname,
450 STRING, 0, NULL, opt_shift,
452 "Horizontal shift: ",
457 { OLETTER_NONE, &keypad_optname,
458 BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
461 "Don't use keypad mode",
465 { OLETTER_NONE, &oldbot_optname,
466 BOOL, OPT_OFF, &oldbot, NULL,
468 "Use new bottom of screen behavior",
469 "Use old bottom of screen behavior",
473 { OLETTER_NONE, &follow_optname,
474 BOOL, FOLLOW_DESC, &follow_mode, NULL,
476 "F command follows file descriptor",
477 "F command follows file name",
481 { OLETTER_NONE, &use_backslash_optname,
482 BOOL, OPT_OFF, &opt_use_backslash, NULL,
484 "Use backslash escaping in command line parameters",
485 "Don't use backslash escaping in command line parameters",
489 { OLETTER_NONE, &rscroll_optname,
490 STRING|REPAINT|INIT_HANDLER, 0, NULL, opt_rscroll,
491 { "right scroll character: ", NULL, NULL }
493 { OLETTER_NONE, &nohistdups_optname,
494 BOOL, OPT_OFF, &no_hist_dups, NULL,
496 "Allow duplicates in history list",
497 "Remove duplicates from history list",
501 { OLETTER_NONE, &mousecap_optname,
502 TRIPLE, OPT_OFF, &mousecap, opt_mousecap,
504 "Ignore mouse input",
505 "Use the mouse for scrolling",
506 "Use the mouse for scrolling (reverse)"
509 { OLETTER_NONE, &wheel_lines_optname,
510 NUMBER|INIT_HANDLER, 0, &wheel_lines, opt_wheel_lines,
512 "Lines to scroll on mouse wheel: ",
513 "Scroll %d line(s) on mouse wheel",
517 { OLETTER_NONE, &perma_marks_optname,
518 BOOL, OPT_OFF, &perma_marks, NULL,
520 "Don't save marks in history file",
521 "Save marks in history file",
525 { OLETTER_NONE, &linenum_width_optname,
526 NUMBER|REPAINT, MIN_LINENUM_WIDTH, &linenum_width, opt_linenum_width,
528 "Line number width: ",
529 "Line number width is %d chars",
533 { OLETTER_NONE, &status_col_width_optname,
534 NUMBER|REPAINT, 2, &status_col_width, opt_status_col_width,
536 "Status column width: ",
537 "Status column width is %d chars",
541 { OLETTER_NONE, &incr_search_optname,
542 BOOL, OPT_OFF, &incr_search, NULL,
544 "Incremental search is off",
545 "Incremental search is on",
549 { OLETTER_NONE, &use_color_optname,
550 BOOL|REPAINT, OPT_OFF, &use_color, NULL,
557 { OLETTER_NONE, &want_filesize_optname,
558 BOOL|REPAINT, OPT_OFF, &want_filesize, opt_filesize,
560 "Don't get size of each file",
561 "Get size of each file",
566 { OLETTER_NONE, &ttyin_name_optname,
567 STRING|NO_TOGGLE, 0, NULL, opt_ttyin_name,
574 { OLETTER_NONE, &rstat_optname,
575 STRING|NO_TOGGLE, 0, NULL, opt_rstat,
583 { '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
588 * Initialize each option to its default value.
591 init_option(VOID_PARAM)
596 p = lgetenv("LESS_IS_MORE");
600 for (o = option; o->oletter != '\0'; o++)
603 * Set each variable to its default.
606 *(o->ovar) = o->odefault;
607 if (o->otype & INIT_HANDLER)
608 (*(o->ofunc))(INIT, (char *) NULL);
613 * Find an option in the option table, given its option letter.
615 public struct loption *
621 for (o = option; o->oletter != '\0'; o++)
625 if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
638 if (ASCII_IS_UPPER(c))
640 if (ASCII_IS_LOWER(c))
648 * Find an option in the option table, given its option name.
649 * p_optname is the (possibly partial) name to look for, and
650 * is updated to point after the matched name.
651 * p_oname if non-NULL is set to point to the full option name.
653 public struct loption *
654 findopt_name(p_optname, p_oname, p_err)
659 char *optname = *p_optname;
661 struct optname *oname;
664 struct loption *maxo = NULL;
665 struct optname *maxoname = NULL;
673 for (o = option; o->oletter != '\0'; o++)
676 * Check all names for this option.
678 for (oname = o->onames; oname != NULL; oname = oname->onext)
681 * Try normal match first (uppercase == 0),
682 * then, then if it's a TRIPLE option,
683 * try uppercase match (uppercase == 1).
685 for (uppercase = 0; uppercase <= 1; uppercase++)
687 len = sprefix(optname, oname->oname, uppercase);
688 if (len <= 0 || is_optchar(optname[len]))
691 * We didn't use all of the option name.
695 if (!exact && len == maxlen)
697 * Already had a partial match,
698 * and now there's another one that
699 * matches the same length.
702 else if (len > maxlen)
705 * Found a better match than
712 exact = (len == (int)strlen(oname->oname));
714 if (!(o->otype & TRIPLE))
722 * Name matched more than one option.
728 *p_optname = optname + maxlen;
730 *p_oname = maxoname == NULL ? NULL : maxoname->oname;