Skip to content

Commit

Permalink
Add --mouse option.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Oct 29, 2018
1 parent 55b5b97 commit 726d77f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.VER
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

* Add --nohistdups option.

* Add --mouse option.

* Support PCRE2 regular expression library.

* Redraw screen on SIGWINCH even if screen size doesn't change.
Expand Down
1 change: 1 addition & 0 deletions cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#define A_RRSHIFT 59
#define A_CLRMARK 62
#define A_SETMARKBOT 63
#define A_MOUSE_IN 64

#define A_INVALID 100
#define A_NOACTION 101
Expand Down
22 changes: 22 additions & 0 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

extern int erase_char, erase2_char, kill_char;
extern int secure;
extern int screen_trashed;

#define SK(k) \
SK_SPECIAL_KEY, (k), 6, 1, 1, 1
Expand Down Expand Up @@ -58,6 +59,7 @@ static unsigned char cmdtable[] =
CONTROL('D'),0, A_F_SCROLL,
'u',0, A_B_SCROLL,
CONTROL('U'),0, A_B_SCROLL,
'\e','[','M',0, A_MOUSE_IN,
' ',0, A_F_SCREEN,
'f',0, A_F_SCREEN,
CONTROL('F'),0, A_F_SCREEN,
Expand Down Expand Up @@ -457,6 +459,26 @@ cmd_search(cmd, table, endtable, sp)
*sp = ++p;
a &= ~A_EXTRA;
}
if (a == A_MOUSE_IN)
{
int b = getcc();
int x = getcc() - 0x21;
int y = getcc() - 0x21;
switch (b) {
default: a = A_NOACTION; break;
case 0x61: a = A_F_LINE; break;
case 0x60: a = A_B_LINE; break;
case 0x23: /* button release */
/*
* {{ It would be better to do this in commands()
* but it's nontrival to pass y to it. }}
*/
setmark('#', y);
screen_trashed = 1;
a = A_NOACTION;
break;
}
}
return (a);
}
} else if (*q == '\0')
Expand Down
5 changes: 5 additions & 0 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,11 @@ file name is typed in, and the same string is already in the history list,
the existing copy is removed from the history list before the new one is added.
Thus, a given string will appear only once in the history list.
Normally, a string may appear multiple times.
.IP "\-\-mouse"
Enables mouse input:
the scroll wheel can be used to scroll within the file,
and clicking the mouse will set the "#" mark to the line on which the cursor sits.
Mouse input works only on terminals which support X11 mouse reporting, and on Windows systems.
.IP \-\-
A command line argument of "\-\-" marks the end of option arguments.
Any arguments following this are interpreted as filenames.
Expand Down
21 changes: 17 additions & 4 deletions mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ extern int jump_sline;
* Each mark is identified by a lowercase or uppercase letter.
* The final one is lmark, for the "last mark"; addressed by the apostrophe.
*/
#define NMARKS ((2*26)+1) /* a-z, A-Z, lastmark */
#define NMARKS ((2*26)+2) /* a-z, A-Z, lastmark, mousemark */
#define MOUSEMARK (NMARKS-2)
#define LASTMARK (NMARKS-1)
static struct mark marks[NMARKS];

Expand Down Expand Up @@ -42,6 +43,9 @@ getumark(c)
if (c >= 'A' && c <= 'Z')
return (&marks[c-'A'+26]);

if (c == '#')
return (&marks[MOUSEMARK]);

error("Invalid mark letter", NULL_PARG);
return (NULL);
}
Expand Down Expand Up @@ -97,6 +101,9 @@ getmark(c)
*/
m = &marks[LASTMARK];
break;
case '#':
m = &marks[MOUSEMARK];
break;
default:
/*
* Must be a user-defined mark.
Expand Down Expand Up @@ -139,6 +146,11 @@ setmark(c, where)
if (m == NULL)
return;
get_scrpos(&scrpos, where);
if (scrpos.pos == NULL_POSITION)
{
bell();
return;
}
m->m_scrpos = scrpos;
m->m_ifile = curr_ifile;
}
Expand Down Expand Up @@ -253,13 +265,14 @@ posmark(pos)
{
int i;

/* Only lower case and upper case letters */
for (i = 0; i < 26*2; i++)
/* Only user marks */
for (i = 0; i < 26*2 + 1; i++)
{
if (marks[i].m_ifile == curr_ifile && marks[i].m_scrpos.pos == pos)
{
if (i < 26) return 'a' + i;
return 'A' + i - 26;
if (i < 26*2) return 'A' + (i - 26);
return '#';
}
}
return 0;
Expand Down
10 changes: 10 additions & 0 deletions opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public int opt_use_backslash; /* Use backslash escaping in option parsing */
public char rscroll_char; /* Char which marks chopped lines with -S */
public int rscroll_attr; /* Attribute of rscroll_char */
public int no_hist_dups; /* Remove dups from history list */
public int mousecap; /* Allow mouse for scrolling */
#if HILITE_SEARCH
public int hilite_search; /* Highlight matched search patterns? */
#endif
Expand Down Expand Up @@ -115,6 +116,7 @@ static struct optname follow_optname = { "follow-name", NULL };
static struct optname use_backslash_optname = { "use-backslash", NULL };
static struct optname rscroll_optname = { "rscroll", NULL };
static struct optname nohistdups_optname = { "nohistdups", NULL };
static struct optname mousecap_optname = { "mouse", NULL };


/*
Expand Down Expand Up @@ -466,6 +468,14 @@ static struct loption option[] =
NULL
}
},
{ OLETTER_NONE, &mousecap_optname,
BOOL, OPT_OFF, &mousecap, NULL,
{
"Ignore mouse input",
"Use the mouse for scrolling",
NULL
}
},
{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
};

Expand Down
15 changes: 13 additions & 2 deletions position.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,19 @@ get_scrpos(scrpos, where)

switch (where)
{
case TOP: i = 0; dir = +1; last = sc_height-2; break;
default: i = sc_height-2; dir = -1; last = 0; break;
case TOP:
i = 0; dir = +1; last = sc_height-2;
break;
case BOTTOM: case BOTTOM_PLUS_ONE:
i = sc_height-2; dir = -1; last = 0;
break;
default:
i = where;
if (table[i] == NULL_POSITION) {
scrpos->pos = NULL_POSITION;
return;
}
break;
}

/*
Expand Down
21 changes: 21 additions & 0 deletions screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ static char
*sc_backspace, /* Backspace cursor */
*sc_s_keypad, /* Start keypad mode */
*sc_e_keypad, /* End keypad mode */
*sc_s_mousecap, /* Start mouse capture mode */
*sc_e_mousecap, /* End mouse capture mode */
*sc_init, /* Startup terminal initialization */
*sc_deinit; /* Exit terminal de-initialization */
#endif
Expand Down Expand Up @@ -235,6 +237,7 @@ extern int screen_trashed;
extern int top_scroll;
extern int quit_if_one_screen;
extern int oldbot;
extern int mousecap;
#if HILITE_SEARCH
extern int hilite_search;
#endif
Expand Down Expand Up @@ -1216,6 +1219,9 @@ get_term()
sc_e_keypad = "";
kent = ltgetstr("@8", &sp);

sc_s_mousecap = "\e[?1000h";
sc_e_mousecap = "\e[?1000l";

sc_init = ltgetstr("ti", &sp);
if (sc_init == NULL)
sc_init = "";
Expand Down Expand Up @@ -1559,6 +1565,8 @@ init()
tputs(sc_init, sc_height, putchr);
if (!no_keypad)
tputs(sc_s_keypad, sc_height, putchr);
if (mousecap)
tputs(sc_s_mousecap, sc_height, putchr);
if (top_scroll)
{
int i;
Expand Down Expand Up @@ -1593,6 +1601,8 @@ deinit()
if (!init_done)
return;
#if !MSDOS_COMPILER
if (mousecap)
tputs(sc_e_mousecap, sc_height, putchr);
if (!no_keypad)
tputs(sc_e_keypad, sc_height, putchr);
if (!no_init && !quit_if_one_screen)
Expand Down Expand Up @@ -2408,6 +2418,17 @@ win32_kbhit()
if (read == 0)
return (FALSE);
ReadConsoleInput(tty, &ip, 1, &read);
/* read mouse wheel and fake up/down arrow key presses */
if (mousecap && ip.EventType == MOUSE_EVENT &&
ip.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
{
/* {{ This does not support setmark('#') like the X11 version does.
* Also fails if user redefines the arrow actions. }} */
currentKey.scan = ((int)ip.Event.MouseEvent.dwButtonState < 0) ? PCK_DOWN : PCK_UP;
currentKey.ascii = 0;
keyCount = 1;
return (TRUE);
}
} while (ip.EventType != KEY_EVENT ||
ip.Event.KeyEvent.bKeyDown != TRUE ||
ip.Event.KeyEvent.wVirtualScanCode == 0 ||
Expand Down
4 changes: 2 additions & 2 deletions ttyin.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ open_getchr()
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
tty = CreateFile("CONIN$", GENERIC_READ,
tty = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, &sa,
OPEN_EXISTING, 0L, NULL);
GetConsoleMode(tty, &console_mode);
/* Make sure we get Ctrl+C events. */
SetConsoleMode(tty, ENABLE_PROCESSED_INPUT);
SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
#else
#if MSDOS_COMPILER
extern int fd0;
Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ v536 8/31/18 Use descriptive error messages for PCRE2.
v537 8/31/18 Support mingw build system (thanks to Mike Soyka).
v538 9/3/18 Clean up some WIN32 code.
v539 9/13/18 Fix spurious input on Windows with CAPSLOCK.
v540 10/29/18 Add --mouse option.
*/

char version[] = "539";
char version[] = "540";

0 comments on commit 726d77f

Please sign in to comment.