diff options
Diffstat (limited to 'src/mouse.zig')
| -rw-r--r-- | src/mouse.zig | 302 |
1 files changed, 151 insertions, 151 deletions
diff --git a/src/mouse.zig b/src/mouse.zig index d15616b5..7d2df7e8 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -88,30 +88,6 @@ pub const HeaderSweep = struct { } }; -/// What a pointer at (mcol, mrow) is over in a column's tag, or the -/// workspace's for null: the header's text, the line, and the byte offset -/// in that text. `clamp` holds a held sweep to the header's rows and text, -/// as a pane tag's sweep stays on its tag. -fn headerPoint(p: *Pardes, column: ?usize, mcol: u16, mrow: u16, clamp: bool) ?struct { t: *panes.Text, text: []const u8, line: u16, at: usize } { - const v = p.view(); - const t = if (column) |c| &v.col_tags[c] else &p.global_tag; - const x = if (column) |c| v.col_x[c] + TAG_TEXT_INSET else TAG_TEXT_INSET; - const focused = v.header != null and p.headerColumn() == column; - const text = edit.flatSurface(p, t) catch return null; - const base = if (column != null) p.topBarHeight() else 0; - const rows = if (column != null) layout.columnBarHeight(p) else p.topBarHeight(); - const row: u16 = if (clamp) @min(mrow -| base, rows -| 1) else mrow -| base; - const top: usize = if (focused) v.header_top else 0; - const lines = modal.lineCount(text); - const line: u16 = if (clamp) @intCast(@min(row + top, lines -| 1)) else @intCast(row + top); - if (line > 0 and line >= lines) return null; - const bar = modal.lineSlice(text, line); - const scroll = if (focused) v.header_scroll else 0; - const kind: Layer.Kind = if (column != null) .column else .workspace; - const at = @min(bar.len, panes.File.rawAtDisplay(bar, (Layer.columnAt(p, kind, column orelse 0, v.pointer_tag_hit, clamp, row) orelse (mcol -| x)) + scroll)); - return .{ .t = t, .text = text, .line = line, .at = modal.lineStartOffset(text, line) + at }; -} - /// A header sweep's button came up: a click runs the word under it, or the /// header's selection when it is on that; a sweep runs what it swept. fn headerSweepRun(p: *Pardes, sweep: HeaderSweep) void { @@ -145,18 +121,6 @@ fn headerSweepRun(p: *Pardes, sweep: HeaderSweep) void { } else exec.runBuiltin(p, config.look_cmd, v.active, "", word); } -fn clampBorderCol(lx: u16, lw: u16, rw: u16, mcol: u16) u16 { - const lo = lx + config.MINW; - const hi = lx +| lw +| rw -| config.MINW; - return if (lo <= hi) std.math.clamp(mcol, lo, hi) else mcol; -} - -fn clampBorderRow(ay: u16, ah: u16, bh: u16, mrow: u16) u16 { - const lo = ay + BOX_H - 1; - const hi = ay +| ah +| bh -| (BOX_H + 1); - return if (lo <= hi) std.math.clamp(mrow, lo, hi) else mrow; -} - fn seamRowOf(p: *const Pardes, c: usize, k: usize) u16 { const r = p.view().rects[p.view().cols[c].panes[k]]; return r.y + r.h -| 1; @@ -229,11 +193,6 @@ pub fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clam return Layer.bodyAgain(mapped_hit); } -fn bodyPlace(hit: ?Mouse.BodyHit, pane: *const Pane) ?Layer.Place { - const h = hit orelse return null; - return if (h.serial == pane.serial) h.place else null; -} - pub fn sameBodyCell(a: ?Mouse.BodyHit, b: ?Mouse.BodyHit) bool { const first = a orelse return b == null; const second = b orelse return false; @@ -431,6 +390,145 @@ fn emulateButton(p: *Pardes, m: *Mouse) void { if (m.kind == .release) v.click_as = null; } +/// What a pointer at (mcol, mrow) is over in a column's tag, or the +/// workspace's for null: the header's text, the line, and the byte offset +/// in that text. `clamp` holds a held sweep to the header's rows and text, +/// as a pane tag's sweep stays on its tag. +fn headerPoint(p: *Pardes, column: ?usize, mcol: u16, mrow: u16, clamp: bool) ?struct { t: *panes.Text, text: []const u8, line: u16, at: usize } { + const v = p.view(); + const t = if (column) |c| &v.col_tags[c] else &p.global_tag; + const x = if (column) |c| v.col_x[c] + TAG_TEXT_INSET else TAG_TEXT_INSET; + const focused = v.header != null and p.headerColumn() == column; + const text = edit.flatSurface(p, t) catch return null; + const base = if (column != null) p.topBarHeight() else 0; + const rows = if (column != null) layout.columnBarHeight(p) else p.topBarHeight(); + const row: u16 = if (clamp) @min(mrow -| base, rows -| 1) else mrow -| base; + const top: usize = if (focused) v.header_top else 0; + const lines = modal.lineCount(text); + const line: u16 = if (clamp) @intCast(@min(row + top, lines -| 1)) else @intCast(row + top); + if (line > 0 and line >= lines) return null; + const bar = modal.lineSlice(text, line); + const scroll = if (focused) v.header_scroll else 0; + const kind: Layer.Kind = if (column != null) .column else .workspace; + const at = @min(bar.len, panes.File.rawAtDisplay(bar, (Layer.columnAt(p, kind, column orelse 0, v.pointer_tag_hit, clamp, row) orelse (mcol -| x)) + scroll)); + return .{ .t = t, .text = text, .line = line, .at = modal.lineStartOffset(text, line) + at }; +} + +fn bodyPlace(hit: ?Mouse.BodyHit, pane: *const Pane) ?Layer.Place { + const h = hit orelse return null; + return if (h.serial == pane.serial) h.place else null; +} + +fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { + const s = &p.view().drag.select; + const pane = p.panes[s.id] orelse return; + if (@min(pane.sel[sel_slot].r0, pane.sel[sel_slot].r1) < pane.tag_rows) { + // A sweep over the tag cuts from and pastes into the tag's own text; + // the part of it over the prefix is not the tag's to cut, so the + // sweep starts after it. + const t = &pane.tag; + if (!s.chorded) { + s.chorded = true; + const sl = pane.sel[sel_slot]; + pane.sel[sel_slot].state = .none; + const forward = sl.r0 < sl.r1 or (sl.r0 == sl.r1 and sl.c0 <= sl.c1); + const start = if (forward) .{ sl.r0, sl.c0 } else .{ sl.r1, sl.c1 }; + const end = if (forward) .{ sl.r1, sl.c1 } else .{ sl.r0, sl.c0 }; + const lead = pane.tag_lead; + const origin: modal.Cursor = .{ .row = 0, .col = lead }; + var from = tagline.tagOffsetAt(p, pane, @intCast(@max(0, start[0])), @intCast(@max(0, start[1]))) orelse origin; + var to = tagline.tagOffsetAt(p, pane, @intCast(@max(0, end[0])), @intCast(@max(0, end[1]))) orelse origin; + if (from.row == 0) from.col = @max(from.col, lead); + if (to.row == 0) to.col = @max(to.col, lead); + t.cur_row = @intCast(to.row); + t.cur_col = @intCast(to.col); + t.cur_pinned = true; + t.vsel = .{ .active = from.row != to.row or from.col != to.col, .row = @intCast(from.row), .col = @intCast(from.col), .explicit = false }; + t.nsel = 0; + } + if (cut) { + if (t.vsel.active) try edit.normalDelete(p, t, true); + } else if (t.vsel.active) { + try edit.chordReplace(p, t); + } else try edit.pasteText(p, t, p.registers.text(p.gpa, pardes.Registers.default) orelse return, true, 1); + return; + } + if (pane.body.mode == .tty) { + if (!s.chorded) { + s.chorded = true; + pane.sel[sel_slot].state = .none; // drop the sweep highlight + } + if (cut) return; + const y = p.registers.text(p.gpa, pardes.Registers.default) orelse return; + if (y.len == 0) return; + if (panes.terminal.reportsMouse(pane)) { + // A click where the text goes, so the program's cursor is there. + const col = @max(0, pane.sel[sel_slot].c1); + const row = @max(0, pane.sel[sel_slot].r1 - @as(i32, pane.tag_rows)); + var mb: [64]u8 = undefined; + for ([_]panes.terminal.MouseAction{ .press, .release }) |action| { + const bytes = panes.terminal.mouseBytes(pane, &mb, action, .left, .{}, col, row, action == .press); + if (bytes.len > 0) p.emitWrite(s.id, bytes); + } + } + edit.typeToTty(p, s.id, pane, y); + return; + } + if (!s.chorded) { + s.chorded = true; + // Everything done while B1 stays down is one undo step, as acme + // marks the file once per B1 hold (text.c:881 textselect, `state` + // and its one filemark): B1-B2 then B1-B3 is a copy. + edit.pushUndo(p, &pane.body); + p.view().chord_hold = true; + const sl = pane.sel[sel_slot]; + pane.sel[sel_slot].state = .none; + const pfx: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; + // both ends of the sweep through the same last-frame map, so a + // chord over wrapped rows cuts the text that was under it + const w0 = pane.wrapAt(@max(0, sl.r0 - @as(i32, pane.tag_rows))); + const w1 = pane.wrapAt(@max(0, sl.r1 - @as(i32, pane.tag_rows))); + const row0 = w0.line; + const row1 = w1.line; + const col0 = edit.paneByteAtDisplay(p, &pane.body, w0.line, w0.at, sl.c0 - pfx - w0.pad); + const col1 = edit.paneByteAtDisplay(p, &pane.body, w1.line, w1.at, sl.c1 - pfx - w1.pad); + pane.body.cur_row = row1; + pane.body.cur_col = col1; + pane.body.cur_pinned = true; + pane.body.normal.clear(); + if (!pane.isTerminal()) pane.body.mode = .normal; + pane.body.vsel = .{ .active = row0 != row1 or col0 != col1, .row = row0, .col = col0, .explicit = false }; + pane.body.nsel = 0; + } + if (cut) { + if (pane.body.vsel.active) try edit.normalDelete(p, &pane.body, true); + } else if (pane.body.vsel.active) { + try edit.chordReplace(p, &pane.body); + } else if (pane.file) |*f| { + // bare cursor: splice the register in literally (no linewise + // magic) so a paste right after a cut restores the text exactly + const y = p.registers.text(p.gpa, pardes.Registers.default) orelse return; + if (y.len == 0) return; + const at = modal.Cursor{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)) }; + edit.pushUndo(p, &pane.body); + const new = try modal.insertAt(p.gpa, f.content, at, y); + panes.File.setContent(p, f, new); + pane.body.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @intCast(at.row), .col = @intCast(at.col), .explicit = false }; + const end = modal.advanceBy(at, y); + if (end.col > 0) { + pane.body.cur_row = @intCast(end.row); + pane.body.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(f.content, end.row), end.col)); + } else { + // the register ended in '\n': the cursor lands ON that newline + pane.body.cur_row = @intCast(end.row -| 1); + pane.body.cur_col = @intCast(modal.lineSlice(f.content, end.row -| 1).len); + } + pane.body.cur_pinned = true; + pane.body.sticky_col = -1; + pane.ensureCursorVisible(); + } else try edit.pasteText(p, &pane.body, p.registers.text(p.gpa, pardes.Registers.default) orelse return, true, 1); +} + pub fn handleMouse(p: *Pardes, event: Mouse) void { const v = p.view(); var m = event; @@ -825,6 +923,18 @@ fn seamIdxAt(p: *const Pardes, c: usize, mrow: u16) ?usize { return null; } +fn clampBorderCol(lx: u16, lw: u16, rw: u16, mcol: u16) u16 { + const lo = lx + config.MINW; + const hi = lx +| lw +| rw -| config.MINW; + return if (lo <= hi) std.math.clamp(mcol, lo, hi) else mcol; +} + +fn clampBorderRow(ay: u16, ah: u16, bh: u16, mrow: u16) u16 { + const lo = ay + BOX_H - 1; + const hi = ay +| ah +| bh -| (BOX_H + 1); + return if (lo <= hi) std.math.clamp(mrow, lo, hi) else mrow; +} + pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) void { const v = p.view(); switch (v.drag) { @@ -1279,116 +1389,6 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { } } -fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { - const s = &p.view().drag.select; - const pane = p.panes[s.id] orelse return; - if (@min(pane.sel[sel_slot].r0, pane.sel[sel_slot].r1) < pane.tag_rows) { - // A sweep over the tag cuts from and pastes into the tag's own text; - // the part of it over the prefix is not the tag's to cut, so the - // sweep starts after it. - const t = &pane.tag; - if (!s.chorded) { - s.chorded = true; - const sl = pane.sel[sel_slot]; - pane.sel[sel_slot].state = .none; - const forward = sl.r0 < sl.r1 or (sl.r0 == sl.r1 and sl.c0 <= sl.c1); - const start = if (forward) .{ sl.r0, sl.c0 } else .{ sl.r1, sl.c1 }; - const end = if (forward) .{ sl.r1, sl.c1 } else .{ sl.r0, sl.c0 }; - const lead = pane.tag_lead; - const origin: modal.Cursor = .{ .row = 0, .col = lead }; - var from = tagline.tagOffsetAt(p, pane, @intCast(@max(0, start[0])), @intCast(@max(0, start[1]))) orelse origin; - var to = tagline.tagOffsetAt(p, pane, @intCast(@max(0, end[0])), @intCast(@max(0, end[1]))) orelse origin; - if (from.row == 0) from.col = @max(from.col, lead); - if (to.row == 0) to.col = @max(to.col, lead); - t.cur_row = @intCast(to.row); - t.cur_col = @intCast(to.col); - t.cur_pinned = true; - t.vsel = .{ .active = from.row != to.row or from.col != to.col, .row = @intCast(from.row), .col = @intCast(from.col), .explicit = false }; - t.nsel = 0; - } - if (cut) { - if (t.vsel.active) try edit.normalDelete(p, t, true); - } else if (t.vsel.active) { - try edit.chordReplace(p, t); - } else try edit.pasteText(p, t, p.registers.text(p.gpa, pardes.Registers.default) orelse return, true, 1); - return; - } - if (pane.body.mode == .tty) { - if (!s.chorded) { - s.chorded = true; - pane.sel[sel_slot].state = .none; // drop the sweep highlight - } - if (cut) return; - const y = p.registers.text(p.gpa, pardes.Registers.default) orelse return; - if (y.len == 0) return; - if (panes.terminal.reportsMouse(pane)) { - // A click where the text goes, so the program's cursor is there. - const col = @max(0, pane.sel[sel_slot].c1); - const row = @max(0, pane.sel[sel_slot].r1 - @as(i32, pane.tag_rows)); - var mb: [64]u8 = undefined; - for ([_]panes.terminal.MouseAction{ .press, .release }) |action| { - const bytes = panes.terminal.mouseBytes(pane, &mb, action, .left, .{}, col, row, action == .press); - if (bytes.len > 0) p.emitWrite(s.id, bytes); - } - } - edit.typeToTty(p, s.id, pane, y); - return; - } - if (!s.chorded) { - s.chorded = true; - // Everything done while B1 stays down is one undo step, as acme - // marks the file once per B1 hold (text.c:881 textselect, `state` - // and its one filemark): B1-B2 then B1-B3 is a copy. - edit.pushUndo(p, &pane.body); - p.view().chord_hold = true; - const sl = pane.sel[sel_slot]; - pane.sel[sel_slot].state = .none; - const pfx: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; - // both ends of the sweep through the same last-frame map, so a - // chord over wrapped rows cuts the text that was under it - const w0 = pane.wrapAt(@max(0, sl.r0 - @as(i32, pane.tag_rows))); - const w1 = pane.wrapAt(@max(0, sl.r1 - @as(i32, pane.tag_rows))); - const row0 = w0.line; - const row1 = w1.line; - const col0 = edit.paneByteAtDisplay(p, &pane.body, w0.line, w0.at, sl.c0 - pfx - w0.pad); - const col1 = edit.paneByteAtDisplay(p, &pane.body, w1.line, w1.at, sl.c1 - pfx - w1.pad); - pane.body.cur_row = row1; - pane.body.cur_col = col1; - pane.body.cur_pinned = true; - pane.body.normal.clear(); - if (!pane.isTerminal()) pane.body.mode = .normal; - pane.body.vsel = .{ .active = row0 != row1 or col0 != col1, .row = row0, .col = col0, .explicit = false }; - pane.body.nsel = 0; - } - if (cut) { - if (pane.body.vsel.active) try edit.normalDelete(p, &pane.body, true); - } else if (pane.body.vsel.active) { - try edit.chordReplace(p, &pane.body); - } else if (pane.file) |*f| { - // bare cursor: splice the register in literally (no linewise - // magic) so a paste right after a cut restores the text exactly - const y = p.registers.text(p.gpa, pardes.Registers.default) orelse return; - if (y.len == 0) return; - const at = modal.Cursor{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)) }; - edit.pushUndo(p, &pane.body); - const new = try modal.insertAt(p.gpa, f.content, at, y); - panes.File.setContent(p, f, new); - pane.body.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @intCast(at.row), .col = @intCast(at.col), .explicit = false }; - const end = modal.advanceBy(at, y); - if (end.col > 0) { - pane.body.cur_row = @intCast(end.row); - pane.body.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(f.content, end.row), end.col)); - } else { - // the register ended in '\n': the cursor lands ON that newline - pane.body.cur_row = @intCast(end.row -| 1); - pane.body.cur_col = @intCast(modal.lineSlice(f.content, end.row -| 1).len); - } - pane.body.cur_pinned = true; - pane.body.sticky_col = -1; - pane.ensureCursorVisible(); - } else try edit.pasteText(p, &pane.body, p.registers.text(p.gpa, pardes.Registers.default) orelse return, true, 1); -} - const test_metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 }; fn cornerFixture(gpa: std.mem.Allocator) !*Pardes { |
