summaryrefslogtreecommitdiff
path: root/src/edit.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/edit.zig')
-rw-r--r--src/edit.zig189
1 files changed, 38 insertions, 151 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 93a6f308..964cda79 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -62,7 +62,6 @@ pub fn registerValue(p: *Pardes, t: *Text, name: u21) ?[]const u8 {
/// the text of the range under the cursor: the selection, or the cursor's
/// own character
fn selText(p: *Pardes, t: *Text) []const u8 {
- if (t.msel.active) return yankRows(p, t, @min(t.msel.r0, t.msel.r1), @max(t.msel.r0, t.msel.r1));
const save = t.vsel;
defer t.vsel = save;
if (!t.vsel.active) t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = false };
@@ -160,14 +159,14 @@ fn pasteInto(p: *Pardes, t: *Text, bytes: []const u8) void {
if (bytes.len == 0) return;
const typed: pardes.Key = .{ .cp = bytes[0], .text = bytes };
if (t.what == .tag and t.mode == .insert) if (t.pane()) |pane| if (tagline.typedIntoName(p, pane, typed)) return;
- const kept = .{ t.cur_row, t.cur_col, t.vsel, t.msel };
+ const kept = .{ t.cur_row, t.cur_col, t.vsel };
var kept_sels: [Text.max_selections - 1]Text.SelRange = undefined;
const kept_n = t.nsel;
@memcpy(kept_sels[0..kept_n], t.sels[0..kept_n]);
t.refused = false;
if (t.mode == .insert) insertKey(p, t, typed) else pasteText(p, t, bytes, false, 1);
if (t.refused) {
- t.cur_row, t.cur_col, t.vsel, t.msel = kept;
+ t.cur_row, t.cur_col, t.vsel = kept;
t.restoreSels(kept_sels[0..kept_n]);
}
}
@@ -349,25 +348,23 @@ pub fn spanHas(r: i32, c: i32, ar: i32, ac: i32, br: i32, bc: i32) bool {
return (r > sr or (r == sr and c >= sc)) and (r < er or (r == er and c <= ec));
}
-/// the active modal selection as text (v range or x lines), if any
+/// the active modal selection as text, if any
pub fn currentSelText(p: *Pardes, pane: *Pane) ?[]const u8 {
if (comptime pdf_enabled) if (p.native_images) if (pane.pdf) |pv|
if (pv.selection != null and pv.selection_text.len > 0) return pv.selection_text;
- if (pane.body.vsel.active) return vselText(p, &pane.body);
- if (!pane.body.msel.active) return null;
- return yankRows(p, &pane.body, @min(pane.body.msel.r0, pane.body.msel.r1), @max(pane.body.msel.r0, pane.body.msel.r1));
+ if (!pane.body.vsel.active) return null;
+ return vselText(p, &pane.body);
}
pub fn exitInsert(p: *Pardes, t: *Text) void {
t.mode = .normal;
t.reg_wait = false;
- t.msel.active = false;
t.normal.clear();
if (!t.restore_cursor) return;
t.restore_cursor = false;
// helix enter_normal_mode: every range, from its start to the char
// before its end when it points forward, else to its end
- const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active;
+ const expl = t.vsel.active and t.vsel.explicit;
const text = flatSurface(p, t) catch return;
var rs: [Text.max_selections]modal.Selection = undefined;
const got = t.ranges(text, 0, &rs);
@@ -584,7 +581,6 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) void {
t.sticky_col = -1;
const text = flatSurface(p, t) catch {
t.mode = .insert;
- t.msel.active = false;
t.vsel.active = false;
t.normal.clear();
return;
@@ -681,7 +677,6 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) void {
},
}
t.mode = .insert;
- t.msel.active = false;
t.normal.clear();
t.show();
}
@@ -1063,80 +1058,28 @@ fn vselText(p: *Pardes, t: *Text) []const u8 {
return modal.rangeText(arena, text, .{ .row = @intCast(@max(0, b.lo_row)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row)), .col = @intCast(@max(0, b.hi_col)) }) catch "";
}
-/// join surface rows [r0, r1] (absolute) with '\n'; scratch-owned
-fn yankRows(p: *Pardes, t: *Text, r0: i32, r1: i32) []const u8 {
- const arena = p.scratch.allocator();
- const lines = paneCursorLines(p, t) catch return "";
- const rows_count: usize = @intCast(@max(0, r1 - r0 + 1));
- var total: usize = rows_count -| 1;
- var i = r0;
- while (i <= r1) : (i += 1) {
- if (i >= 0 and @as(usize, @intCast(i)) < lines.len)
- total += lines[@intCast(i)].len;
- }
- const out = arena.alloc(u8, total) catch return "";
- var at: usize = 0;
- i = r0;
- while (i <= r1) : (i += 1) {
- if (i > r0) {
- out[at] = '\n';
- at += 1;
- }
- if (i >= 0 and @as(usize, @intCast(i)) < lines.len) {
- const line = lines[@intCast(i)];
- @memcpy(out[at..][0..line.len], line);
- at += line.len;
- }
- }
- return out;
-}
-
pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) void {
t.select = false; // helix delete exits select mode
t.sticky_col = -1;
// A bare cursor is the 1-wide selection on its cell (helix), a '\n'
// cell too, where the delete joins the lines.
- if (!t.vsel.active and !t.msel.active) t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = false };
- if (t.vsel.active) {
- const b = vselBounds(t);
- if (yank) setYank(p, vselText(p, t)); // read the range before the move
- pushUndo(p, t);
- const eb = editTextEol(p, t, b) orelse {
- t.vsel.active = false;
- return;
- };
- const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch {
- t.vsel.active = false;
- return;
- };
- setEditText(p, t, d.content);
- p.gpa.free(d.deleted);
- t.cur_row = b.lo_row;
- t.cur_col = b.lo_col;
- t.clampCursor(d.content, eb.row0);
- return;
- }
- const r0 = @min(t.msel.r0, t.msel.r1);
- const r1 = @max(t.msel.r0, t.msel.r1);
+ if (!t.vsel.active) t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = false };
+ const b = vselBounds(t);
+ if (yank) setYank(p, vselText(p, t)); // read the range before the move
pushUndo(p, t);
- // linewise: a line's newline lives on the row below it
- const eb = editText(p, t, r0, r1 + 1, -1) orelse {
- t.msel.active = false;
+ const eb = editTextEol(p, t, b) orelse {
+ t.vsel.active = false;
return;
};
- const d = modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, r0 - eb.row0)), @intCast(@max(0, r1 - eb.row0))) catch {
- t.msel.active = false;
+ const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch {
+ t.vsel.active = false;
return;
};
setEditText(p, t, d.content);
- if (yank) setYank(p, d.deleted);
p.gpa.free(d.deleted);
- const n = modal.lineCount(d.content);
- t.cur_row = @as(i32, @intCast(@min(@as(usize, @intCast(@max(0, r0 - eb.row0))), if (n == 0) 0 else n - 1))) + eb.row0;
- t.cur_col = 0;
- t.msel.active = false;
- t.cur_pinned = true;
- t.show();
+ t.cur_row = b.lo_row;
+ t.cur_col = b.lo_col;
+ t.clampCursor(d.content, eb.row0);
}
pub fn normalYank(p: *Pardes, t: *Text) void {
@@ -1209,7 +1152,6 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz
t.cur_col = @intCast(modal.lineSlice(out, end.row -| 1).len);
}
}
- t.msel.active = false;
t.cur_pinned = true;
t.show();
}
@@ -1237,7 +1179,6 @@ pub fn normalChange(p: *Pardes, t: *Text) void {
t.cur_row = @as(i32, @intCast(row)) + eb.row0;
t.cur_col = @intCast(ind.len);
t.vsel.active = false;
- t.msel.active = false;
t.cur_pinned = true;
t.mode = .insert;
t.normal.clear();
@@ -1259,54 +1200,21 @@ pub fn normalChange(p: *Pardes, t: *Text) void {
t.normal.clear();
return;
}
- if (t.msel.active) {
- const r0 = @min(t.msel.r0, t.msel.r1);
- const r1 = @max(t.msel.r0, t.msel.r1);
- pushUndo(p, t);
- const eb = editText(p, t, r0, r1 + 1, -1) orelse return;
- const lo: usize = @intCast(@max(0, r0 - eb.row0));
- const hi: usize = @intCast(@max(0, r1 - eb.row0));
- // the lines are adjacent in the text already, their newlines and all
- setYank(p, eb.text[modal.lineStartOffset(eb.text, lo)..modal.lineEndOffset(eb.text, hi)]);
- var body: []u8 = eb.text;
- if (hi > lo) {
- const d = modal.deleteLines(p.gpa, body, lo + 1, hi) catch {
- t.msel.active = false;
- return;
- };
- setEditText(p, t, d.content);
- p.gpa.free(d.deleted);
- body = d.content;
- }
- const cl = modal.clearLine(p.gpa, body, lo) catch {
- t.msel.active = false;
- return;
- };
- setEditText(p, t, cl);
- t.cur_row = r0;
- t.cur_col = 0;
- t.msel.active = false;
- t.cur_pinned = true;
- t.mode = .insert;
- t.normal.clear();
- t.show();
- } else {
- pushUndo(p, t);
- const eb = editTextEol(p, t, .{ .lo_row = t.cur_row, .lo_col = t.cur_col, .hi_row = t.cur_row, .hi_col = t.cur_col }) orelse return;
- const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) };
- const arena = p.scratch.allocator();
- setYank(p, modal.rangeText(arena, eb.text, c, c) catch return);
- const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return;
- setEditText(p, t, d.content);
- p.gpa.free(d.deleted);
- const llen = modal.lineSlice(d.content, c.row).len;
- t.cur_col = @min(t.cur_col, @as(i32, @intCast(llen)));
- t.mode = .insert;
- t.normal.clear();
- }
+ pushUndo(p, t);
+ const eb = editTextEol(p, t, .{ .lo_row = t.cur_row, .lo_col = t.cur_col, .hi_row = t.cur_row, .hi_col = t.cur_col }) orelse return;
+ const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) };
+ const arena = p.scratch.allocator();
+ setYank(p, modal.rangeText(arena, eb.text, c, c) catch return);
+ const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return;
+ setEditText(p, t, d.content);
+ p.gpa.free(d.deleted);
+ const llen = modal.lineSlice(d.content, c.row).len;
+ t.cur_col = @min(t.cur_col, @as(i32, @intCast(llen)));
+ t.mode = .insert;
+ t.normal.clear();
}
-fn selRange(t: *Text, content: []const u8, row0: i32) modal.Range {
+fn selRange(t: *Text, row0: i32) modal.Range {
if (t.vsel.active) {
const b = vselBounds(t);
return .{
@@ -1314,12 +1222,6 @@ fn selRange(t: *Text, content: []const u8, row0: i32) modal.Range {
.b = .{ .row = @intCast(@max(0, b.hi_row - row0)), .col = @intCast(@max(0, b.hi_col)) },
};
}
- if (t.msel.active) {
- const r0: usize = @intCast(@max(0, @min(t.msel.r0, t.msel.r1) - row0));
- const r1: usize = @intCast(@max(0, @max(t.msel.r0, t.msel.r1) - row0));
- const llen = modal.lineSlice(content, r1).len;
- return .{ .a = .{ .row = r0, .col = 0 }, .b = .{ .row = r1, .col = if (llen == 0) 0 else modal.prevGrapheme(modal.lineSlice(content, r1), llen) } };
- }
const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - row0)), .col = @intCast(@max(0, t.cur_col)) };
return .{ .a = c, .b = c };
}
@@ -1327,12 +1229,6 @@ fn selRange(t: *Text, content: []const u8, row0: i32) modal.Range {
/// the rows an op's selection spans, absolute (what editText must cover)
fn selRows(t: *Text) Bounds {
if (t.vsel.active) return vselBounds(t);
- if (t.msel.active) return .{
- .lo_row = @min(t.msel.r0, t.msel.r1),
- .lo_col = 0,
- .hi_row = @max(t.msel.r0, t.msel.r1),
- .hi_col = 0,
- };
return .{ .lo_row = t.cur_row, .lo_col = t.cur_col, .hi_row = t.cur_row, .hi_col = t.cur_col };
}
@@ -1376,7 +1272,7 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) void {
while (at < hi) : (graphemes += 1) at = modal.nextGrapheme(eb.text, at);
var encoded: [4]u8 = undefined;
const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return;
- const r = selRange(t, eb.text, eb.row0);
+ const r = selRange(t, eb.row0);
pushUndo(p, t);
const new = modal.replaceChars(p.gpa, eb.text, r.a, r.b, ch) catch return;
setEditText(p, t, new);
@@ -1402,12 +1298,11 @@ fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) void {
if (y.len == 0) return;
t.select = false;
const eb = editTextEol(p, t, selRows(t)) orelse return;
- const r = selRange(t, eb.text, eb.row0);
+ const r = selRange(t, eb.row0);
const back = t.vsel.active and (t.cur_row < t.vsel.row or (t.cur_row == t.vsel.row and t.cur_col < t.vsel.col));
pushUndo(p, t);
const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, y) catch return;
setEditText(p, t, new);
- t.msel.active = false;
const end = modal.advanceBy(r.a, y);
var last_row: i32 = undefined;
var last_col: i32 = undefined;
@@ -1439,7 +1334,7 @@ fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) void {
pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) void {
t.select = false;
const eb = editTextEol(p, t, selRows(t)) orelse return;
- const r = selRange(t, eb.text, eb.row0);
+ const r = selRange(t, eb.row0);
pushUndo(p, t);
const new = modal.changeCase(p.gpa, eb.text, r.a, r.b, op) catch return;
setEditText(p, t, new);
@@ -1466,7 +1361,7 @@ fn mapThroughChanges(chs: []const sam.Op, pos: usize) usize {
/// become one space, or nothing when that line is blank, and a line two
/// ranges share is joined once. `Alt-J` then selects the spaces it put in.
pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
- const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active;
+ const expl = t.vsel.active and t.vsel.explicit;
// a join always eats the newline of its last line, so the buffer has
// to reach one row PAST every range
const b = allSelRows(t);
@@ -1535,7 +1430,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
/// Each range comes back over the text it now holds, in its own direction,
/// and the primary moves with the text it had.
pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) void {
- const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active;
+ const expl = t.vsel.active and t.vsel.explicit;
const eb = editTextEol(p, t, allSelRows(t)) orelse return;
const text = eb.text;
var rs: [Text.max_selections]modal.Selection = undefined;
@@ -1570,7 +1465,7 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) voi
/// before each range so its head lands on the column's widest head, measured
/// in display cells. A range over several lines aligns nothing.
pub fn normalAlign(p: *Pardes, t: *Text) void {
- const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active;
+ const expl = t.vsel.active and t.vsel.explicit;
const eb = editTextEol(p, t, allSelRows(t)) orelse return;
const text = eb.text;
var rs: [Text.max_selections]modal.Selection = undefined;
@@ -1692,14 +1587,13 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void {
t.vsel.row = @as(i32, @intCast(ac.row)) + eb.row0;
t.vsel.col = @intCast(ac.col);
}
- t.msel.active = false;
t.cur_pinned = true;
t.sticky_col = -1;
t.show();
}
pub fn normalToggleComment(p: *Pardes, t: *Text) void {
- const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active;
+ const expl = t.vsel.active and t.vsel.explicit;
t.select = false; // helix exit_select_mode
// the buffer must cover every cursor's rows, not just the primary's
const b = allSelRows(t);
@@ -1780,7 +1674,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void {
pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) void {
const eb = editTextEol(p, t, selRows(t)) orelse return;
- const r = selRange(t, eb.text, eb.row0);
+ const r = selRange(t, eb.row0);
const arena = p.scratch.allocator();
const frag = modal.rangeText(arena, eb.text, r.a, r.b) catch return;
const rep = (modal.incrementDecimal(arena, frag, delta) catch null) orelse return;
@@ -1791,7 +1685,6 @@ pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) void {
const start = modal.lineStartOffset(new, r.a.row) + r.a.col;
const cc = modal.positionAt(new, modal.prevGrapheme(new, start + rep.len));
t.vsel = .{ .active = modal.nextGrapheme(rep, 0) < rep.len, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false };
- t.msel.active = false;
t.cur_row = @as(i32, @intCast(cc.row)) + eb.row0;
t.cur_col = @intCast(cc.col);
t.cur_pinned = true;
@@ -1819,10 +1712,6 @@ pub fn addNewline(p: *Pardes, t: *Text, below: bool, cnt: usize) void {
const n: i32 = @intCast(cnt);
t.cur_row += n;
if (t.vsel.active) t.vsel.row += n;
- if (t.msel.active) {
- t.msel.r0 += n;
- t.msel.r1 += n;
- }
}
t.cur_pinned = true;
t.show();
@@ -1900,7 +1789,7 @@ pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) void {
const pr = pairFor(ch) orelse return;
t.select = false;
const eb = editTextEol(p, t, selRows(t)) orelse return;
- const r = selRange(t, eb.text, eb.row0);
+ const r = selRange(t, eb.row0);
// the wrapped range keeps the direction it had (helix with_direction)
const back = t.vsel.active and (t.cur_row < t.vsel.row or (t.cur_row == t.vsel.row and t.cur_col < t.vsel.col));
pushUndo(p, t);
@@ -1913,7 +1802,6 @@ pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) void {
const open_col: i32 = @intCast(r.a.col);
const close_row = @as(i32, @intCast(r.b.row)) + eb.row0;
const close_at: i32 = @intCast(close_col + @as(usize, if (r.a.row == r.b.row) 1 else 0));
- t.msel.active = false;
t.vsel = .{ .active = true, .row = if (back) close_row else open_row, .col = if (back) close_at else open_col, .explicit = false };
t.cur_row = if (back) open_row else close_row;
t.cur_col = if (back) open_col else close_at;
@@ -1954,7 +1842,6 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) void {
t.cur_row = @as(i32, @intCast(cc.row)) + eb.row0;
t.cur_col = @intCast(cc.col);
t.vsel.active = false;
- t.msel.active = false;
t.cur_pinned = true;
t.show();
}