Skip to content

Commit

Permalink
keyboard selection mode (mintty#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
mintty committed Feb 8, 2019
1 parent 963ed87 commit 77fda46
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
22 changes: 21 additions & 1 deletion docs/mintty.1
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ with option \fBClearSelectionOnInput=false\fP.
The current selection size can optionally been indicated with a popup,
enabled with option \fBSelectionShowSize\fP.

Selection can also be managed using the keyboard. Shift+middle-keypad-key
(Shift+"5") enters keyboard selecting mode (modifier configurable).

.TP
\fBElastic text selection\fP
The traditional selection behaviour of cell-based terminals is that a
Expand Down Expand Up @@ -638,7 +641,7 @@ the most significant non-zero byte first.
An overview of all the keyboard shortcuts.

.TP
\fBScrollback\fP
\fBScrollback and Selection via keyboard\fP
.br
\(en \fBShift+Up\fP: Line up
.br
Expand All @@ -657,6 +660,23 @@ An overview of all the keyboard shortcuts.
\(en \fBShift+cursor-left\fP: Go to previous scroll marker (e.g. in prompt)
.br
\(en \fBShift+cursor-right\fP: Go to next scroll marker (e.g. in prompt)
.br
\(en \fBShift+middle-keypad-key\fP: Enter keyboard selecting mode

Note: The modifier can be configured with setting \fBScrollMod\fP.

Keyboard selecting mode: \fBAlt\fP sets rectangular selection.
Once keyboard selecting mode is entered, the following keys are applied:
.br
\(en \fBUp\fP, \fBDown\fP, \fBUp\fP, \fBUp\fP: Modify selection
.br
\(en \fBPgUp\fP, \fBPgDn\fP, \fBHome\fP, \fBEnd\fP: Scroll/Modify selection
.br
\(en \fB[Alt+]middle-keypad-key\fP: Restart [rectangular] selection
.br
\(en \fBInsert\fP or \fBEnter\fP: Copy selection and exit selection mode
.br
\(en \fBDelete\fP or \fBESC\fP: Exit selection mode

.TP
\fBCopy and paste\fP
Expand Down
118 changes: 117 additions & 1 deletion src/wininput.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static bool newwin_shifted = false;
static bool newwin_home = false;
static int newwin_monix = 0, newwin_moniy = 0;
static int transparency_pending = 0;
static bool selection_pending = false;
bool kb_input = false;
uint kb_trace = 0;

Expand Down Expand Up @@ -1775,6 +1776,112 @@ win_key_down(WPARAM wp, LPARAM lp)
return true;
}
}
if (selection_pending) {
bool sel_adjust = false;
//WPARAM scroll = 0;
int sbtop = -sblines();
int sbbot = term_last_nonempty_line();
int oldisptop = term.disptop;
//printf("y %d disptop %d sb %d..%d\n", term.sel_pos.y, term.disptop, sbtop, sbbot);
switch (key) {
when VK_CLEAR: // recalibrate
term.sel_anchor = term.sel_pos;
term.sel_start = term.sel_pos;
term.sel_end = term.sel_pos;
term.sel_rect = mods & MDK_ALT;
sel_adjust = true;
when VK_LEFT:
if (term.sel_pos.x > 0)
term.sel_pos.x--;
sel_adjust = true;
when VK_RIGHT:
if (term.sel_pos.x < term.cols)
term.sel_pos.x++;
sel_adjust = true;
when VK_UP:
if (term.sel_pos.y > sbtop) {
if (term.sel_pos.y <= term.disptop)
term_scroll(0, -1);
term.sel_pos.y--;
sel_adjust = true;
}
when VK_DOWN:
if (term.sel_pos.y < sbbot) {
if (term.sel_pos.y + 1 >= term.disptop + term.rows)
term_scroll(0, +1);
term.sel_pos.y++;
sel_adjust = true;
}
when VK_PRIOR:
//scroll = SB_PAGEUP;
term_scroll(0, -max(1, term.rows - 1));
term.sel_pos.y += term.disptop - oldisptop;
sel_adjust = true;
when VK_NEXT:
//scroll = SB_PAGEDOWN;
term_scroll(0, +max(1, term.rows - 1));
term.sel_pos.y += term.disptop - oldisptop;
sel_adjust = true;
when VK_HOME:
//scroll = SB_TOP;
term_scroll(+1, 0);
term.sel_pos.y += term.disptop - oldisptop;
term.sel_pos.y = sbtop;
term.sel_pos.x = 0;
sel_adjust = true;
when VK_END:
//scroll = SB_BOTTOM;
term_scroll(-1, 0);
term.sel_pos.y += term.disptop - oldisptop;
term.sel_pos.y = sbbot;
if (sbbot < term.rows) {
termline *line = term.lines[sbbot];
if (line)
for (int j = line->cols - 1; j > 0; j--) {
term.sel_pos.x = j + 1;
if (!termchars_equal(&line->chars[j], &term.erase_char))
break;
}
}
sel_adjust = true;
when VK_INSERT or VK_RETURN: // copy
term_copy();
selection_pending = false;
when VK_DELETE or VK_ESCAPE: // abort
selection_pending = false;
otherwise:
//selection_pending = false;
win_bell(&cfg);
}
//if (scroll) {
// SendMessage(wnd, WM_VSCROLL, scroll, 0);
// sel_adjust = true;
//}
if (sel_adjust) {
if (term.sel_rect) {
term.sel_start.y = min(term.sel_anchor.y, term.sel_pos.y);
term.sel_start.x = min(term.sel_anchor.x, term.sel_pos.x);
term.sel_end.y = max(term.sel_anchor.y, term.sel_pos.y);
term.sel_end.x = max(term.sel_anchor.x, term.sel_pos.x);
}
else if (posle(term.sel_anchor, term.sel_pos)) {
term.sel_start = term.sel_anchor;
term.sel_end = term.sel_pos;
}
else {
term.sel_start = term.sel_pos;
term.sel_end = term.sel_anchor;
}
//printf("->sel %d:%d .. %d:%d\n", term.sel_start.y, term.sel_start.x, term.sel_end.y, term.sel_end.x);
term.selected = true;
win_update(true);
}
if (selection_pending)
return true;
else
term.selected = false;
return true;
}

bool allow_shortcut = true;

Expand Down Expand Up @@ -1987,7 +2094,7 @@ win_key_down(WPARAM wp, LPARAM lp)
return true;
}

// Scrollback
// Scrollback and Selection via keyboard
if (!term.on_alt_screen || term.show_other_screen) {
mod_keys scroll_mod = cfg.scroll_mod ?: 128;
if (cfg.pgupdn_scroll && (key == VK_PRIOR || key == VK_NEXT) &&
Expand All @@ -2004,6 +2111,15 @@ win_key_down(WPARAM wp, LPARAM lp)
when VK_DOWN: scroll = SB_LINEDOWN;
when VK_LEFT: scroll = SB_PRIOR;
when VK_RIGHT: scroll = SB_NEXT;
when VK_CLEAR:
term.sel_pos = (pos){.y = term.curs.y, .x = term.curs.x, .r = 0};
term.sel_anchor = term.sel_pos;
term.sel_start = term.sel_pos;
term.sel_end = term.sel_pos;
term.sel_rect = mods & MDK_ALT;
selection_pending = true;
//printf("selection_pending = true\n");
return true;
otherwise: goto not_scroll;
}
SendMessage(wnd, WM_VSCROLL, scroll, 0);
Expand Down

0 comments on commit 77fda46

Please sign in to comment.