summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/File.zig2
-rw-r--r--src/Pipe.zig6
-rw-r--r--src/Terminal.zig63
-rw-r--r--src/Text.zig79
-rw-r--r--src/body_layer.zig5
-rw-r--r--src/colors.zig2
-rw-r--r--src/dump.zig2
-rw-r--r--src/edit.zig41
-rw-r--r--src/exec.zig7
-rw-r--r--src/look.zig3
-rw-r--r--src/mouse.zig82
-rw-r--r--src/ninep/pane.zig44
-rw-r--r--src/normal.zig20
-rw-r--r--src/panes.zig66
-rw-r--r--src/pardes.zig633
15 files changed, 493 insertions, 562 deletions
diff --git a/src/File.zig b/src/File.zig
index bf4bf08c..2bf9f57d 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -1129,7 +1129,7 @@ pub fn drawGutter(p: *Pardes, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, bod
const goff = pane.scroll();
const gcur = Terminal.gridCursor(pane);
const gcrow = if (pane.body.cur_pinned) pane.body.cur_row else @as(i32, gcur.y) + goff;
- const typing_body = !pane.tag_edit and (pane.prompt == .none or pane.prompt == .del_side);
+ const typing_body = pane.focus == .body and (pane.prompt == .none or pane.prompt == .del_side);
const cur_line: i32 = if (active and typing_body) gcrow else std.math.minInt(i32);
// the body's first row, the way renderPane derives it (Tagbottom)
const body_y = p.bodyTop(r);
diff --git a/src/Pipe.zig b/src/Pipe.zig
index 3e6752a8..4cabfd88 100644
--- a/src/Pipe.zig
+++ b/src/Pipe.zig
@@ -294,12 +294,12 @@ test "selection pipe prompt submits exact request and Escape cancels" {
pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true };
p.update(.{ .key = .{ .cp = '|' } });
- try std.testing.expect(!pane.tag_edit and pane.prompt == .pipe);
+ try std.testing.expect(pane.focus == .body and pane.prompt == .pipe);
try std.testing.expectEqualStrings(config.pipe_marker, pane.promptText().?);
try std.testing.expect(nextPipeEffect(p) == null);
p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } });
p.update(.{ .key = .{ .cp = Key.enter } });
- try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe and pane.input.own == null);
+ try std.testing.expect(pane.focus == .body and pane.prompt != .pipe and pane.input.own == null);
const id = nextPipeEffect(p) orelse return error.MissingPipeEffect;
const request = p.pipe.pipeRequest(id) orelse return error.MissingPipeRequest;
try std.testing.expectEqualStrings("tr a-z A-Z", request.command);
@@ -311,7 +311,7 @@ test "selection pipe prompt submits exact request and Escape cancels" {
p.update(.{ .key = .{ .cp = '|' } });
p.update(.{ .key = .{ .cp = 'c', .text = "cat" } });
p.update(.{ .key = .{ .cp = Key.escape } });
- try std.testing.expect(!pane.tag_edit and pane.prompt != .pipe);
+ try std.testing.expect(pane.focus == .body and pane.prompt != .pipe);
try std.testing.expectEqualSlices(u8, before, pane.file.?.content);
try std.testing.expect(nextPipeEffect(p) == null);
}
diff --git a/src/Terminal.zig b/src/Terminal.zig
index fbe7bfc0..7b5af696 100644
--- a/src/Terminal.zig
+++ b/src/Terminal.zig
@@ -686,8 +686,7 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void {
const at_bottom = atBottom(pane);
if (has_positions) ingestWithPositions(pane, bytes) else ingest(pane, bytes);
p.shell_rows.markStale(pane);
- const body_mode = if (pane.tag_edit) pane.tag_mode else pane.body.mode;
- if (body_mode == .tty and at_bottom) followOutput(pane);
+ if (pane.body.mode == .tty and at_bottom) followOutput(pane);
}
const RowPin = struct { pin: ?*ghostty_vt.Pin = null, offset: i32 = 0 };
@@ -2463,64 +2462,22 @@ pub fn padOutputBelowEdits(p: *Pardes, id: usize) void {
while (i < pad) : (i += 1) p.emitWrite(id, "\r");
}
-/// the snapshot takes ownership of a COPY of the edit buffer's text
-pub fn snap(p: *Pardes, pane: *Pane) ?Snapshot {
- var ovl: ?EditBuffer = null;
- if (pane.ovl) |o| ovl = .{ .row = o.row, .rows = o.rows, .text = p.gpa.dupe(u8, o.text) catch return null };
- return .{ .ovl = ovl, .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel };
-}
-
-/// undo/redo restores the selection recorded with the snapshot (helix
-/// keeps selections in its history transactions)
-pub fn restoreSnap(p: *Pardes, pane: *Pane, s: Snapshot) void {
- if (pane.ovl) |o| p.gpa.free(o.text);
- pane.ovl = s.ovl;
- pane.body.cur_row = s.cur_row;
- pane.body.cur_col = s.cur_col;
- pane.body.cur_pinned = true;
- pane.body.vsel = s.vsel;
- pane.body.msel.active = false;
- pane.ensureCursorVisible();
-}
-
-fn pushHistory(gpa: std.mem.Allocator, slots: []Snapshot, len: *usize, value: Snapshot) void {
- if (len.* == slots.len) {
- if (slots[0].ovl) |overlay| gpa.free(overlay.text);
- std.mem.copyForwards(Snapshot, slots[0 .. slots.len - 1], slots[1..]);
- len.* -= 1;
- }
- slots[len.*] = value;
- len.* += 1;
-}
-
pub fn pushUndo(p: *Pardes, pane: *Pane) void {
- const current = pane.ovl orelse EditBuffer{ .rows = 0 };
- if (pane.body.ed_undo_len > 0) {
- const top = pane.body.ed_undo[pane.body.ed_undo_len - 1];
- const same = if (top.ovl) |overlay| pane.ovl != null and overlay.row == current.row and
- overlay.rows == current.rows and std.mem.eql(u8, overlay.text, current.text) else pane.ovl == null;
- if (same) return;
- }
- const value = snap(p, pane) orelse return;
- pushHistory(p.gpa, &pane.body.ed_undo, &pane.body.ed_undo_len, value);
- for (pane.body.ed_redo[0..pane.body.ed_redo_len]) |item| if (item.ovl) |overlay| p.gpa.free(overlay.text);
- pane.body.ed_redo_len = 0;
+ pane.body.remember(p.gpa, pane.ovl);
}
pub fn undo(p: *Pardes, pane: *Pane) void {
- if (pane.body.ed_undo_len == 0) return;
- const current = snap(p, pane) orelse return;
- pushHistory(p.gpa, &pane.body.ed_redo, &pane.body.ed_redo_len, current);
- pane.body.ed_undo_len -= 1;
- restoreSnap(p, pane, pane.body.ed_undo[pane.body.ed_undo_len]);
+ const back_to = pane.body.step(p.gpa, pane.ovl, true) orelse return;
+ if (pane.ovl) |o| p.gpa.free(o.text);
+ pane.ovl = back_to.ovl;
+ pane.ensureCursorVisible();
}
pub fn redo(p: *Pardes, pane: *Pane) void {
- if (pane.body.ed_redo_len == 0) return;
- const current = snap(p, pane) orelse return;
- pushHistory(p.gpa, &pane.body.ed_undo, &pane.body.ed_undo_len, current);
- pane.body.ed_redo_len -= 1;
- restoreSnap(p, pane, pane.body.ed_redo[pane.body.ed_redo_len]);
+ const forward_to = pane.body.step(p.gpa, pane.ovl, false) orelse return;
+ if (pane.ovl) |o| p.gpa.free(o.text);
+ pane.ovl = forward_to.ovl;
+ pane.ensureCursorVisible();
}
pub fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: [:0]const u8) void {
diff --git a/src/Text.zig b/src/Text.zig
index aaa6dc88..3e48a42e 100644
--- a/src/Text.zig
+++ b/src/Text.zig
@@ -1,11 +1,11 @@
//! One editable text, acme's Text (editors/acme/dat.h:171-190, text.c): the
//! cursor, the selections and the modal state that edit it, and the undo of
-//! an edit buffer. A pane's body is one, and so is the answer typed into a
-//! pane's prompt. Where a body's characters come from -- a file's content, a
-//! terminal's edit overlay, a PDF's text -- is the window's business
-//! (edit.zig's editText and flatSurface), the way acme's Text reads its File;
-//! every other text owns its characters. Everything here works on the flat
-//! text it is handed.
+//! an edit buffer. A pane's body is one, its tag is another, and so is the
+//! answer typed into a pane's prompt. Where a body's characters come from --
+//! a file's content, a terminal's edit overlay, a PDF's text -- is the
+//! window's business (edit.zig's editText and flatSurface), the way acme's
+//! Text reads its File; every other text owns its characters. Everything
+//! here works on the flat text it is handed.
const std = @import("std");
const panes = @import("panes.zig");
const modal = @import("modal.zig");
@@ -19,7 +19,7 @@ pub const Mode = enum { normal, insert, tty };
/// Which text of its window this is, acme's `what`. It says where the
/// characters live and whether moving the cursor scrolls a body.
-pub const What = enum { body, input };
+pub const What = enum { body, tag, input };
/// A modal line selection (helix `x`): whole rows [r0, r1], absolute.
pub const LineSel = struct {
@@ -49,7 +49,8 @@ pub const SelRange = struct {
what: What = .body,
/// The characters of a text that holds its own (gpa-owned): everything but
-/// a body, which reads its pane's.
+/// a body, which reads its pane's. A tag nobody has changed has none yet and
+/// shows the default its pane's kind starts with.
own: ?[]u8 = null,
mode: Mode = .normal,
cur_pinned: bool = false,
@@ -77,12 +78,14 @@ ed_redo_len: usize = 0,
pub fn pane(t: *Text) *Pane {
return switch (t.what) {
.body => @alignCast(@fieldParentPtr("body", t)),
+ .tag => @alignCast(@fieldParentPtr("tag", t)),
.input => @alignCast(@fieldParentPtr("input", t)),
};
}
/// Bring the cursor into view (acme's textshow). Only a body scrolls to it;
-/// a prompt's answer is drawn whole.
+/// the tagline follows its caret where it is drawn, and a prompt's answer is
+/// drawn whole.
pub fn show(t: *Text) void {
if (t.what == .body) t.pane().ensureCursorVisible();
}
@@ -273,8 +276,8 @@ pub fn multiSelAction(t: *Text, text: []const u8, kind: modal.Normal.Multi, cnt:
return setRanges(t, text, out[0..m], &.{}, @min(got.pri, m - 1), expl);
}
if (kind == .rotate_forward or kind == .rotate_backward) {
- const step = cnt % n;
- const pri = if (kind == .rotate_forward) (got.pri + step) % n else (got.pri + (n - step)) % n;
+ const turn = cnt % n;
+ const pri = if (kind == .rotate_forward) (got.pri + turn) % n else (got.pri + (n - turn)) % n;
return setRanges(t, text, rs[0..n], &.{}, pri, expl);
}
if (kind == .merge) {
@@ -510,6 +513,8 @@ pub fn visualMove(
/// Ctrl-d/u: scroll half a page AND move the cursor by the same rows
pub fn halfPageMove(t: *Text, text: []const u8, range: modal.Selection, down: bool) void {
+ // a view to move through is a body's; a tag or an answer is all shown
+ if (t.what != .body) return;
const half: i32 = @max(1, @divTrunc(@as(i32, t.pane().rows), 2));
t.pane().scrollBy(if (down) half else -half);
verticalMove(t, text, range, down, @intCast(half));
@@ -518,6 +523,8 @@ pub fn halfPageMove(t: *Text, text: []const u8, range: modal.Selection, down: bo
/// helix `scroll` without cursor sync (Ctrl-f/b, PgUp/PgDn, zj/zk): shift
/// the view, then snap a fallen-out cursor to the near scrolloff edge, col 0
pub fn scrollViewMove(t: *Text, text: []const u8, range: modal.Selection, delta: i32) void {
+ // a view to move through is a body's; a tag or an answer is all shown
+ if (t.what != .body) return;
const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, t.pane().rows) - 1, 2));
t.pane().scrollBy(delta);
const top = t.pane().scroll();
@@ -540,6 +547,8 @@ pub fn scrollViewMove(t: *Text, text: []const u8, range: modal.Selection, delta:
/// gt/gc/gb: view-relative rows, col 0, scrolloff clamped (helix goto_window)
pub fn gotoWindow(t: *Text, text: []const u8, range: modal.Selection, which: enum { top, center, bottom }, cnt: usize) void {
+ // a view to move through is a body's; a tag or an answer is all shown
+ if (t.what != .body) return;
const margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, t.pane().rows) - 1, 2));
const top = t.pane().scroll();
const last_row: i32 = @intCast(File.textLineCount(t, text) - 1);
@@ -623,3 +632,51 @@ pub fn clampCursor(t: *Text, text: []const u8, row0: i32) void {
t.msel.active = false;
t.show();
}
+
+fn pushHistory(gpa: std.mem.Allocator, slots: []Terminal.Snapshot, len: *usize, value: Terminal.Snapshot) void {
+ if (len.* == slots.len) {
+ if (slots[0].ovl) |overlay| gpa.free(overlay.text);
+ std.mem.copyForwards(Terminal.Snapshot, slots[0 .. slots.len - 1], slots[1..]);
+ len.* -= 1;
+ }
+ slots[len.*] = value;
+ len.* += 1;
+}
+
+/// Record an edit buffer (a terminal's overlay, a tag's own text) as it
+/// stands before an edit, once per change of it, and forget what could have
+/// been redone. Null is a buffer not made yet.
+pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?Terminal.EditBuffer) void {
+ if (t.ed_undo_len > 0) {
+ const top = t.ed_undo[t.ed_undo_len - 1];
+ const same = if (top.ovl) |overlay| if (current) |now| overlay.row == now.row and
+ overlay.rows == now.rows and std.mem.eql(u8, overlay.text, now.text) else false else current == null;
+ if (same) return;
+ }
+ const copy: ?Terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = gpa.dupe(u8, now.text) catch return } else null;
+ pushHistory(gpa, &t.ed_undo, &t.ed_undo_len, .{ .ovl = copy, .cur_row = t.cur_row, .cur_col = t.cur_col, .vsel = t.vsel });
+ for (t.ed_redo[0..t.ed_redo_len]) |item| if (item.ovl) |overlay| gpa.free(overlay.text);
+ t.ed_redo_len = 0;
+}
+
+/// Undo (back) or redo one edit of an edit buffer: `current` goes on the
+/// other history and the state to return to comes back, its cursor and
+/// selection already restored here. The caller installs its buffer, which
+/// it then owns. Null when there is nothing to step to.
+pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?Terminal.EditBuffer, back: bool) ?Terminal.Snapshot {
+ const from, const from_len, const to, const to_len = if (back)
+ .{ &t.ed_undo, &t.ed_undo_len, &t.ed_redo, &t.ed_redo_len }
+ else
+ .{ &t.ed_redo, &t.ed_redo_len, &t.ed_undo, &t.ed_undo_len };
+ if (from_len.* == 0) return null;
+ const copy: ?Terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = gpa.dupe(u8, now.text) catch return null } else null;
+ pushHistory(gpa, to, to_len, .{ .ovl = copy, .cur_row = t.cur_row, .cur_col = t.cur_col, .vsel = t.vsel });
+ from_len.* -= 1;
+ const back_to = from[from_len.*];
+ t.cur_row = back_to.cur_row;
+ t.cur_col = back_to.cur_col;
+ t.cur_pinned = true;
+ t.vsel = back_to.vsel;
+ t.msel.active = false;
+ return back_to;
+}
diff --git a/src/body_layer.zig b/src/body_layer.zig
index 1f210220..e85e10f1 100644
--- a/src/body_layer.zig
+++ b/src/body_layer.zig
@@ -363,6 +363,9 @@ pub fn renderBody(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, r: Rect, id
for (pane.sel, 0..) |sl, b| {
if (sl.state == .none) continue;
if (sl.state == .done and b != 0) continue;
+ // A select sweep over the tag is the tag's selection, painted there
+ // in the tag's own selection colours.
+ if (b == pardes.sel_slot and @min(sl.r0, sl.r1) < BOX_H) continue;
paintPointerSelection(p, pane, r, tx, tw, tag_y, body_y, sl, sel_btn[b], th.sel_fg);
}
// modal line selection (helix `x`): whole body rows, normal mode only
@@ -461,7 +464,7 @@ pub fn renderBody(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, r: Rect, id
// cursor: tracks the shell cursor until pinned by a click or a key
// (the tag's or a prompt's cursor wins while that is being typed into)
- if (active and !pane.tag_edit and (pane.prompt == .none or pane.prompt == .del_side)) {
+ if (active and pane.focus == .body and (pane.prompt == .none or pane.prompt == .del_side)) {
if (pane.body.mode != .tty) {
const cur = panes.Terminal.gridCursor(pane);
const goff = panes.Terminal.gridOffset(pane);
diff --git a/src/colors.zig b/src/colors.zig
index 741187c6..f92f67b1 100644
--- a/src/colors.zig
+++ b/src/colors.zig
@@ -602,7 +602,7 @@ test "line numbers stay quiet without a current-line tag background" {
const moved = try p.render(p.scratch.allocator());
try std.testing.expect(!moved.at(x, y).style.bold);
try std.testing.expect(moved.at(x, y + 1).style.bold);
- p.enterTagEdit(pane, -1);
+ p.enterTag(pane);
const tag_edit = try p.render(p.scratch.allocator());
try std.testing.expect(!tag_edit.at(x, y + 1).style.bold);
}
diff --git a/src/dump.zig b/src/dump.zig
index 2fb9ce80..7112e1f0 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -621,7 +621,7 @@ pub fn dumpState(p: *Pardes) !void {
try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, scroll, iv.path, iv.raw)
else
try pardes.panes.Terminal.dumpPane(pane, arena, tag, body, scroll);
- dp.tag_tail = if (pane.tag_init) pane.tagSlice() else null;
+ dp.tag_tail = pane.tag.own;
dump_panes[panes_len] = dp;
panes_len += 1;
}
diff --git a/src/edit.zig b/src/edit.zig
index 5249a64c..02e138e8 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -96,18 +96,12 @@ pub fn applyPaste(p: *Pardes, bytes: []const u8) void {
if (normal.selRegexArmed(pane)) |a| normal.applySelRegex(p, pane, a.pat, a.split);
return;
}
- if (pane.tag_edit) {
- const text = p.tagText(p.scratch.allocator(), pane) catch return;
- const range: @TypeOf(Pardes.tagSelBounds(pane)) = if (pane.tag_sel) Pardes.tagSelBounds(pane) else .{ .lo = pane.tag_col, .hi = pane.tag_col };
- const hi = if (pane.tag_sel) modal.nextGrapheme(text, range.hi) else range.hi;
- _ = p.replaceTagText(pane, range.lo, hi, bytes);
- return;
- }
- if (pane.isTerminal() and pane.body.mode == .tty) return typeToTty(p, id, pane, bytes);
+ const t = if (pane.focus == .tag) &pane.tag else &pane.body;
+ if (t.what == .body and pane.isTerminal() and pane.body.mode == .tty) return typeToTty(p, id, pane, bytes);
switch (if (req) |r| r.mode else .after) {
- .after => pasteText(p, &pane.body, bytes, false, 1),
- .before => pasteText(p, &pane.body, bytes, true, 1),
- .replace => replaceWithText(p, &pane.body, bytes),
+ .after => pasteText(p, t, bytes, false, 1),
+ .before => pasteText(p, t, bytes, true, 1),
+ .replace => replaceWithText(p, t, bytes),
}
}
@@ -301,7 +295,7 @@ pub fn exitInsert(p: *Pardes, t: *Text) void {
pub fn paneCursorLines(p: *Pardes, t: *Text) ![]const []const u8 {
const arena = p.scratch.allocator();
if (t.what != .body) {
- const text = t.own orelse "";
+ const text = try flatSurface(p, t);
const lines = try arena.alloc([]const u8, std.mem.count(u8, text, "\n") + 1);
var it = std.mem.splitScalar(u8, text, '\n');
for (lines) |*line| line.* = it.next().?;
@@ -346,6 +340,7 @@ pub fn pinPaneCursor(p: *Pardes, t: *Text) void {
}
pub fn flatSurface(p: *Pardes, t: *Text) ![]const u8 {
+ if (t.what == .tag) return Pardes.curTail(t.pane());
if (t.what != .body) return t.own orelse "";
const pane = t.pane();
if (pane.file) |f| return f.content;
@@ -367,6 +362,10 @@ pub fn paneWrapWidth(p: *const Pardes, t: *Text) usize {
/// Route shared edit operations to the characters `t` edits: its own, or
/// for a body its file's content or its terminal's overlay.
fn editText(p: *Pardes, t: *Text, lo: i32, hi: i32, col: i32) ?panes.EditText {
+ // A tag nobody has changed shows its default; the first edit makes that
+ // default its own text.
+ if (t.what == .tag and t.own == null)
+ t.own = p.gpa.dupe(u8, Pardes.defaultTail(t.pane())) catch return null;
if (t.what != .body) return .{ .text = t.own orelse &.{}, .row0 = 0 };
const pane = t.pane();
if (pane.file) |f| return .{ .text = f.content, .row0 = 0 };
@@ -1544,14 +1543,19 @@ pub fn surroundReplace(p: *Pardes, t: *Text, from: u21, to: u21) void {
pub fn pushUndo(p: *Pardes, t: *Text) void {
// one keystroke, one undo step — even when it edited at ten cursors.
if (p.multi_on and !p.multi_first) return;
- // a prompt's answer is typed once and gone; it keeps no history
- if (t.what != .body) return;
+ switch (t.what) {
+ .body => {},
+ .tag => return t.remember(p.gpa, if (t.own) |own| .{ .text = own } else null),
+ // a prompt's answer is typed once and gone; it keeps no history
+ .input => return,
+ }
const pane = t.pane();
if (pane.file != null) return panes.File.pushUndo(p, pane);
panes.Terminal.pushUndo(p, pane);
}
pub fn doUndo(p: *Pardes, t: *Text) void {
+ if (t.what == .tag) return stepOwn(p, t, true);
if (t.what != .body) return;
const pane = t.pane();
if (pane.file != null) return panes.File.undo(p, pane);
@@ -1559,12 +1563,21 @@ pub fn doUndo(p: *Pardes, t: *Text) void {
}
pub fn doRedo(p: *Pardes, t: *Text) void {
+ if (t.what == .tag) return stepOwn(p, t, false);
if (t.what != .body) return;
const pane = t.pane();
if (pane.file != null) return panes.File.redo(p, pane);
panes.Terminal.redo(p, pane);
}
+/// Undo or redo an edit of a text's own characters; stepping back past its
+/// first edit gives a tag its default again.
+fn stepOwn(p: *Pardes, t: *Text, back: bool) void {
+ const to = t.step(p.gpa, if (t.own) |own| .{ .text = own } else null, back) orelse return;
+ if (t.own) |own| p.gpa.free(own);
+ t.own = if (to.ovl) |buffer| buffer.text else null;
+}
+
const PointerTextSelection = struct {
sel: Pane.Sel,
on_tag: bool,
diff --git a/src/exec.zig b/src/exec.zig
index a2dbac7c..82b3da4a 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -260,8 +260,9 @@ pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void {
f.output = null; // an ordinary file pane from here on
f.watch_after_save = true;
pane.clearCwd();
- pane.tag_init = false; // re-derive the tag as a plain file
- pane.tag_tail_len = 0;
+ // re-derive the tag as a plain file's
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = null;
p.emit(.{ .save_file = .{ .pane = @intCast(id) } });
return;
}
@@ -351,7 +352,7 @@ pub fn replaceStillborn(p: *Pardes) void {
const out = file.output orelse return;
if (out.from != .cmd or out.from.cmd != .New) return;
if (file.revision != 0 or file.content.len != 0) return;
- if (old.tag_init and !std.mem.eql(u8, old.tagSlice(), Pardes.defaultTail(old))) return;
+ if (old.tag.own) |own| if (!std.mem.eql(u8, own, Pardes.defaultTail(old))) return;
const was_active = p.active == old_id;
layout.compute(p); // the joiner may have no rect yet; absorb snaps to rows
p.removePane(old_id, new_id) catch |err| return p.reportError(old_id, "close", err);
diff --git a/src/look.zig b/src/look.zig
index efc3f312..6f9f8c6a 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -1150,8 +1150,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void {
const found = resolveLookTarget(p, id, txt, &realbuf);
switch (found) {
.none => {
- const bmode = if (pane.tag_edit) pane.tag_mode else pane.body.mode;
- if (pane.image != null or bmode == .tty) return;
+ if (pane.image != null or pane.body.mode == .tty) return;
runSearch(p, id, trimmed, .text, .cursor) catch |err| {
p.reportError(id, "search", err);
return;
diff --git a/src/mouse.zig b/src/mouse.zig
index 5f77634b..ae047c58 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -39,7 +39,6 @@ pub const Drag = union(enum) {
/// `grab`: how far into the grip it was pressed, so the column's left
/// edge lands at `cur_x - grab`. A release still on the grip moves nothing.
column_move: struct { serial: u32, cur_x: u16, grab: u16 = 0 },
- tag: struct { id: usize },
/// chorded: a 1-2/1-3 cut/paste chord fired during this left drag —
/// the drag's own release is then inert
select: struct {
@@ -55,7 +54,7 @@ pub const Drag = union(enum) {
fn button(drag: Drag) ?Mouse.Button {
return switch (drag) {
.none => null,
- .border_v, .border_h, .move, .column_move, .tag => config.select_button,
+ .border_v, .border_h, .move, .column_move => config.select_button,
.select => |selection| selection.button,
};
}
@@ -228,7 +227,7 @@ pub fn handleMouse(p: *Pardes, m: Mouse) void {
const previous_tag_hit = p.pointer_tag_hit;
p.pointer_tag_hit = if (m.tag_hit) |point| p.reprojectTagHit(point) else null;
if (m.tag_hit != null and p.pointer_tag_hit == null and m.kind == .press) return;
- if (m.tag_hit == null and (m.kind == .drag or m.kind == .release) and (p.drag == .tag or p.drag == .select or p.header_drag)) {
+ if (m.tag_hit == null and (m.kind == .drag or m.kind == .release) and (p.drag == .select or p.header_drag)) {
if (previous_tag_hit) |previous| {
var continued = previous;
continued.pixel_x = (@as(f32, @floatFromInt(raw_col)) + 0.5) * @as(f32, @floatFromInt(previous.metrics.body_w));
@@ -236,7 +235,7 @@ pub fn handleMouse(p: *Pardes, m: Mouse) void {
p.pointer_tag_hit = continued;
}
}
- if ((m.kind == .drag or m.kind == .release) and (p.drag == .tag or p.drag == .select or p.header_drag)) {
+ if ((m.kind == .drag or m.kind == .release) and (p.drag == .select or p.header_drag)) {
if (previous_tag_hit) |previous| if (p.pointer_tag_hit) |*current| {
current.kind = previous.kind;
current.id = previous.id;
@@ -303,7 +302,7 @@ pub fn handleMouse(p: *Pardes, m: Mouse) void {
(m.button == config.select_button or m.button == config.exec_button or m.button == config.look_button))
{
if (m.button == config.select_button) if (p.panes[p.active]) |pane| {
- if (pane.tag_edit) Pardes.exitTagEdit(pane);
+ pane.focus = .body;
pane.closePrompt();
};
var column: ?usize = null;
@@ -439,7 +438,7 @@ pub fn handleMouse(p: *Pardes, m: Mouse) void {
const r = p.rects[id];
const pane = p.panes[id] orelse return;
if (id != p.active) if (p.panes[p.active]) |previous| {
- if (previous.tag_edit) Pardes.exitTagEdit(previous);
+ previous.focus = .body;
previous.closePrompt();
};
const tag_y = if (p.settings.tag_bottom) r.y + r.h -| BOX_H else r.y;
@@ -448,20 +447,29 @@ pub fn handleMouse(p: *Pardes, m: Mouse) void {
// gutter scrollbar: a left click scrolls UP to that row
p.active = id;
pane.scrollBy(-(@as(i32, mrow) - @as(i32, body_y)));
- } else if (mrow >= tag_y and mrow < tag_y + BOX_H) {
- if (mcol < r.x + TAG_TEXT_INSET) return;
- p.active = id;
- pane.closePrompt();
- p.enterTagEdit(pane, if (tagColumn(p, .pane, id, p.pointer_tag_hit, false)) |value| @as(i32, value) else @as(i32, mcol) - @as(i32, r.x + TAG_TEXT_INSET));
- pane.tag_anchor = pane.tag_col;
- p.drag = .{ .tag = .{ .id = id } };
} else {
+ const on_tag = mrow >= tag_y and mrow < tag_y + BOX_H;
+ if (on_tag and mcol < r.x + TAG_TEXT_INSET) return;
p.active = id;
- if (pane.tag_edit) Pardes.exitTagEdit(pane);
pane.closePrompt();
const pointed = edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, p.pointer_tag_hit) orelse return;
pane.clearPointerSelection(sel_slot);
pane.sel[sel_slot] = pointed.sel;
+ // A clicked tag is typed straight into, from where it was
+ // clicked; `:` is the door to its normal mode.
+ pane.focus = if (on_tag) .tag else .body;
+ if (on_tag) {
+ // a count typed before the click was meant for the body
+ pane.body.normal.clear();
+ pane.tag.mode = .insert;
+ pane.tag.normal.clear();
+ pane.tag.vsel.active = false;
+ pane.tag.msel.active = false;
+ pane.tag.nsel = 0;
+ pane.tag.cur_pinned = true;
+ pane.tag.cur_row = 0;
+ pane.tag.cur_col = @intCast(p.tagOffsetAt(pane, @intCast(@max(0, pointed.sel.c0))) orelse 0);
+ }
p.drag = .{ .select = .{
.id = id,
.button = config.select_button,
@@ -472,7 +480,7 @@ pub fn handleMouse(p: *Pardes, m: Mouse) void {
config.select_button,
mcol,
mrow,
- false,
+ pointed.on_tag,
),
} };
}
@@ -617,13 +625,6 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo
panes.Pdf.pointerUpdate(&s.pdf, p, pane, mcol, mrow);
}
},
- .tag => |d| {
- const pane = p.panes[d.id] orelse return;
- const c = if (tagColumn(p, .pane, d.id, p.pointer_tag_hit, true)) |value| @as(i32, value) else @as(i32, mcol) - @as(i32, p.rects[d.id].x + TAG_TEXT_INSET);
- const text = p.tagText(p.scratch.allocator(), pane) catch return;
- pane.tag_col = @intCast(@min(text.len, panes.File.rawAtDisplay(text, @as(usize, @intCast(@max(0, c))) + pane.tag_scroll)));
- pane.tag_sel = pane.tag_col != pane.tag_anchor;
- },
.none => {},
}
}
@@ -816,6 +817,16 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void {
const sl = pane.sel[sel_slot];
const dragged = sl.c0 != sl.c1 or sl.r0 != sl.r1;
if (!dragged) pane.sel[sel_slot].state = .none;
+ if (sl.r0 < BOX_H) {
+ // On the tag the caret goes where the button came up. A
+ // click on the path, if it can be renamed, drafts a name.
+ const at = p.tagOffsetAt(pane, @intCast(@max(0, sl.c1)));
+ if (at) |col| {
+ pane.tag.cur_row = 0;
+ pane.tag.cur_col = @intCast(col);
+ } else if (!dragged) p.draftName(pane, @intCast(@max(0, sl.c1)));
+ return;
+ }
if (dragged) edit.capturePointerSelection(p, pane, sel_slot) catch {};
if (dragged and pane.body.mode == .tty) mirrorTtySelection(p, pane);
const body_vis = sl.r1 - @as(i32, BOX_H);
@@ -869,7 +880,7 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void {
}
const txt = operand.text;
const cmd = if (s.button == config.look_button) config.look_cmd else config.exec_cmd;
- if (clk.r0 < BOX_H and s.button == config.exec_button and pane.tag_name_len != null) {
+ if (clk.r0 < BOX_H and s.button == config.exec_button and pane.prompt == .name) {
const prefix_width = panes.File.displayWidth(p.tagPrefix(pane) catch return);
const on_name = @as(usize, @intCast(@max(0, clk.c0))) < prefix_width;
if (!p.commitTagName(pane)) return;
@@ -895,7 +906,6 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void {
});
}
},
- .tag => {}, // dragUpdate already left the tag cursor + selection set
.none => {},
}
}
@@ -913,6 +923,30 @@ fn chordCutPaste(p: *Pardes, cut: bool) void {
if (cut and pv.selection_text.len > 0) edit.setYank(p, pv.selection_text);
return;
};
+ if (@min(pane.sel[sel_slot].r0, pane.sel[sel_slot].r1) < BOX_H) {
+ // 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.
+ const t = &pane.tag;
+ if (!s.chorded) {
+ s.chorded = true;
+ const sl = pane.sel[sel_slot];
+ pane.sel[sel_slot].state = .none;
+ const from = p.tagOffsetAt(pane, @intCast(@max(0, @min(sl.c0, sl.c1)))) orelse 0;
+ const to = p.tagOffsetAt(pane, @intCast(@max(0, @max(sl.c0, sl.c1)))) orelse 0;
+ t.cur_row = 0;
+ t.cur_col = @intCast(to);
+ t.cur_pinned = true;
+ t.vsel = .{ .active = from != to, .row = 0, .col = @intCast(from), .explicit = false };
+ t.msel.active = false;
+ t.nsel = 0;
+ }
+ if (cut) {
+ if (t.vsel.active) edit.normalDelete(p, t, true);
+ } else if (t.vsel.active) {
+ edit.normalReplaceYank(p, t);
+ } else edit.pasteText(p, t, p.yank orelse return, true, 1);
+ return;
+ }
if (pane.body.mode == .tty) {
if (!s.chorded) {
s.chorded = true;
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index 7e9cf7e7..282ae473 100644
--- a/src/ninep/pane.zig
+++ b/src/ninep/pane.zig
@@ -15,6 +15,7 @@ const addressing = @import("addr.zig");
const ctl = @import("ctl.zig");
const pty = @import("pty.zig");
const screen = @import("screen.zig");
+const limits = @import("../memory.zig").limits;
const Pardes = pardes.Pardes;
const Pane = pardes.Pane;
@@ -384,7 +385,7 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply
return switch (file) {
.name => writeName(p, req, id, pane),
.body => writeBody(p, req, id, pane),
- .tag => writeTag(p, req, pane),
+ .tag => writeTag(req, pane),
.ctl => ctl.writePane(p, req, pane),
.addr, .dot, .limit => writeRange(req, pane, file),
.data, .xdata => writeData(p, req, pane),
@@ -417,15 +418,15 @@ fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
return .{ .tag = req.tag, .written = @intCast(take) };
}
-fn writeTag(p: *Pardes, req: Req, pane: *Pane) Reply {
+fn writeTag(req: Req, pane: *Pane) Reply {
if (req.data.len == 0) return .{ .tag = req.tag, .written = 0 };
- p.seedTail(pane);
- const room = pane.tag_tail.len - pane.tag_tail_len;
+ // The tag's own text grows by what is written, newlines and all: a tag
+ // is a text like any other (acme's tag file appends the same way).
+ const had = Pardes.curTail(pane).len;
+ const room = limits.max_tag_tail -| had;
if (room == 0) return Reply.fail(req.tag, E.NOSPC);
const take = wholeUtf8(req.data[0..@min(req.data.len, room)]);
- @memcpy(pane.tag_tail[pane.tag_tail_len..][0..take], req.data[0..take]);
- pane.tag_tail_len += take;
- pane.tag_init = true;
+ pane.appendTag(req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
return .{ .tag = req.tag, .written = @intCast(take) };
}
@@ -553,8 +554,8 @@ pub fn nameBuffer(p: *Pardes, id: usize, full: []const u8, preserve_tag: bool) !
f.output = null;
pane.clearCwd();
if (!preserve_tag) {
- pane.tag_init = false;
- pane.tag_tail_len = 0;
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = null;
}
f.saved_revision = f.revision -% 1;
f.watch_after_save = fs.localPath(full) != null;
@@ -606,8 +607,13 @@ pub fn truncate(p: *Pardes, pane: *Pane, file: PaneFile) tree.Status {
setDot(pane, .{});
},
.tag => {
- pane.tag_tail_len = 0;
- pane.tag_init = true;
+ const empty = p.gpa.alloc(u8, 0) catch return .err;
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = empty;
+ pane.tag.cur_row = 0;
+ pane.tag.cur_col = 0;
+ pane.tag.vsel.active = false;
+ pane.tag.nsel = 0;
},
.addr => pf.addr = .{},
.limit => pf.limit = null,
@@ -740,12 +746,20 @@ test "tag reads the whole tag and writes append to the editable tail" {
const before = rd(p, node, 0, 4096).bytes.len;
const w = wr(p, node, " Mine");
try testing.expectEqual(@as(u32, 5), w.reply.written);
- try testing.expect(std.mem.endsWith(u8, pane.tag_tail[0..pane.tag_tail_len], " Mine"));
+ try testing.expect(std.mem.endsWith(u8, Pardes.curTail(pane), " Mine"));
const after = rd(p, node, 0, 4096);
try testing.expectEqual(before + 5, after.bytes.len);
try testing.expect(std.mem.endsWith(u8, after.bytes, " Mine"));
- pane.tag_tail_len = pane.tag_tail.len;
+ // A tag is a text like the body: a newline written to it stays one, and
+ // the tag reads back taller by it.
+ try testing.expectEqual(@as(u32, 10), wr(p, node, "\nNext Line").reply.written);
+ try testing.expect(std.mem.endsWith(u8, Pardes.curTail(pane), " Mine\nNext Line"));
+ try testing.expect(std.mem.endsWith(u8, rd(p, node, 0, 4096).bytes, " Mine\nNext Line"));
+
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = try p.gpa.alloc(u8, limits.max_tag_tail);
+ @memset(pane.tag.own.?, 'x');
try testing.expectEqual(E.NOSPC, wr(p, node, "x").errno());
}
@@ -873,10 +887,10 @@ test "truncating the tag clears its editable tail" {
const pane = p.panes[0].?;
const tag = Node.of(pane.serial, .tag);
_ = wr(p, tag, " Mine");
- try testing.expect(pane.tag_tail_len > 0);
+ try testing.expect(Pardes.curTail(pane).len > 0);
const cleared = call(p, .{ .tag = 1, .op = .setattr, .node = tag, .truncate = true });
try testing.expectEqual(Status.ok, cleared.reply.status);
- try testing.expectEqual(@as(usize, 0), pane.tag_tail_len);
+ try testing.expectEqualStrings("", pane.tag.own.?);
try testing.expect(std.mem.indexOf(u8, rd(p, tag, 0, 4096).bytes, " Mine") == null);
}
diff --git a/src/normal.zig b/src/normal.zig
index a77f27a7..e4fa075d 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -257,7 +257,7 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void {
if (p.multi_stop) {
p.multi_stop = false;
const pn = p.panes[id] orelse return;
- if (pn.serial == serial) pn.body.nsel = 0;
+ if (pn.serial == serial) t.nsel = 0;
return;
}
const text = edit.flatSurface(p, t) catch return;
@@ -285,7 +285,7 @@ pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void {
fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void {
const pane = t.pane();
- const raw_selection = pane.rawPointerText(pane.sel[sel_slot]) != null;
+ const raw_selection = t.what == .body and pane.rawPointerText(pane.sel[sel_slot]) != null;
const before = .{ t.cur_row, t.cur_col, t.vsel, t.msel, t.select, t.nsel, t.mode };
defer if (raw_selection and !std.meta.eql(before, .{ t.cur_row, t.cur_col, t.vsel, t.msel, t.select, t.nsel, t.mode })) {
pane.clearPointerSelection(sel_slot);
@@ -334,7 +334,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void
.view_center => return t.gotoWindow(text, range, .center, go.count),
.view_bottom => return t.gotoWindow(text, range, .bottom, go.count),
},
- .view => |view| switch (view) {
+ .view => |view| if (t.what == .body) switch (view) {
.top => {
pane.scrollBy(t.cur_row - pane.scroll());
pane.ensureCursorVisible();
@@ -545,10 +545,11 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void
p.leader_on = true;
p.leader_n = 0;
},
- .command_line => {
- p.enterTagEdit(pane, -1);
- if (pane.tag_edit) t.mode = .normal;
- },
+ // `:` is the one key a tag and a body do not share: it moves the
+ // keyboard between them.
+ .command_line => if (t.what == .tag) {
+ pane.focus = .body;
+ } else p.enterTag(pane),
.pipe_selection => |how| return p.startPrompt(pane, .{ .pipe = how }),
.search => return p.startPrompt(pane, .{ .search = config.search_marker }),
.search_step => |direction| return look.lookWalk(
@@ -590,10 +591,7 @@ pub fn handlePdfNormal(p: *Pardes, pane: *Pane, key: Key) void {
p.leader_on = true;
p.leader_n = 0;
},
- .command_line => {
- p.enterTagEdit(pane, -1);
- if (pane.tag_edit) pane.body.mode = .normal;
- },
+ .command_line => p.enterTag(pane),
.search => p.startPrompt(pane, .{ .search = config.search_marker }),
.search_forward => look.lookWalk(p, 1),
.search_backward => look.lookWalk(p, -1),
diff --git a/src/panes.zig b/src/panes.zig
index c26065ca..720f655f 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -69,9 +69,14 @@ pub const Pane = struct {
/// Del from the keyboard with open panes both above and below: which
/// one takes the rows. Nothing is typed; one key answers.
del_side,
+ /// A new name for the file, drafted in place of the path on the
+ /// tagline after a click there; Enter commits it, as a buffer name
+ /// and never a disk rename.
+ name,
};
pub const Cwd = union(enum) { none, inherited: *Pane, owned: []u8 };
+ pub const Focus = enum { body, tag };
terminal: ?*Terminal.State = null,
gpa: std.mem.Allocator,
serial: u32 = 0,
@@ -97,8 +102,9 @@ pub const Pane = struct {
pdf: PdfSlot = if (Pdf.enabled) null else {},
prompt: Prompt = .none,
/// The answer to an open prompt: its marker and what is typed after it,
- /// edited in insert mode and shown on the notice band. It is its own text
- /// so a prompt never touches the tag.
+ /// edited in insert mode and shown on the notice band, or a name draft
+ /// shown on the tagline in place of the path. It is its own text so a
+ /// prompt never touches the tag.
input: Text = .{ .what = .input },
search_pane: ?usize = null,
search_row: ?usize = null,
@@ -107,23 +113,12 @@ pub const Pane = struct {
nsel_snap: u8 = 0,
sel_snap_pri: u8 = 0,
sel_snap_expl: bool = false,
- /// the editable tag tail: a bounded one-line command buffer. Input that
- /// does not fit is refused atomically.
- tag_tail: [limits.max_tag_tail]u8 = undefined,
- tag_tail_len: usize = 0,
- tag_init: bool = false,
- tag_edit: bool = false,
- tag_sel: bool = false,
- /// the body mode a tag edit hijacked (tags are always insert); terminals
- /// restore it on exit so clicking the tag never changes the pane's mode
- tag_mode: Text.Mode = .normal,
- tag_col: u16 = 0,
- tag_saved_col: ?u16 = null,
- tag_anchor: u16 = 0,
- /// Filename edits are staged separately from the live buffer identity.
- /// Enter commits the name; leaving tag editing discards the draft.
- tag_name: [limits.host_path_cap]u8 = undefined,
- tag_name_len: ?u16 = null,
+ /// The tag: what the user keeps after the computed prefix (the path, the
+ /// dirty marker, a PDF's page), its cursor, selections and undo. The
+ /// prefix is never stored in it, so it never moves the tag's cursor.
+ tag: Text = .{ .what = .tag },
+ /// Which of the pane's texts the keyboard types into.
+ focus: Focus = .body,
/// Display columns hidden to reveal the tag caret; tag addresses stay whole.
tag_scroll: u16 = 0,
cwd: Cwd = .none,
@@ -297,9 +292,15 @@ pub const Pane = struct {
return null;
}
- pub fn tagSlice(p: *const Pane) []const u8 {
- return p.tag_tail[0..p.tag_tail_len];
+ /// Add `text` to the end of the tag's own text, which starts out as the
+ /// default a pane of its kind shows.
+ pub fn appendTag(pane: *Pane, text: []const u8) !void {
+ const had = pane.tag.own orelse pardes.Pardes.defaultTail(pane);
+ const grown = try std.mem.concat(pane.gpa, u8, &.{ had, text });
+ if (pane.tag.own) |old| pane.gpa.free(old);
+ pane.tag.own = grown;
}
+
/// Close an open prompt, answered or not. Its answer goes with it, and an
/// s/S preview keeps whatever it last previewed.
pub fn closePrompt(pane: *Pane) void {
@@ -313,33 +314,12 @@ pub const Pane = struct {
/// painter of the band reads.
pub fn promptText(p: *const Pane) ?[]const u8 {
return switch (p.prompt) {
- .none => null,
+ .none, .name => null,
.del_side => config.del_question,
else => p.input.own orelse "",
};
}
- pub fn appendTag(p: *Pane, text: []const u8) bool {
- if (text.len > p.tag_tail.len - p.tag_tail_len) return false;
- @memcpy(p.tag_tail[p.tag_tail_len..][0..text.len], text);
- p.tag_tail_len += text.len;
- return true;
- }
-
- pub fn insertTagByte(p: *Pane, at: usize, byte: u8) bool {
- if (at > p.tag_tail_len or p.tag_tail_len == p.tag_tail.len) return false;
- std.mem.copyBackwards(u8, p.tag_tail[at + 1 .. p.tag_tail_len + 1], p.tag_tail[at..p.tag_tail_len]);
- p.tag_tail[at] = byte;
- p.tag_tail_len += 1;
- return true;
- }
-
- pub fn removeTagByte(p: *Pane, at: usize) void {
- if (at >= p.tag_tail_len) return;
- std.mem.copyForwards(u8, p.tag_tail[at .. p.tag_tail_len - 1], p.tag_tail[at + 1 .. p.tag_tail_len]);
- p.tag_tail_len -= 1;
- }
-
pub fn cwdSlice(p: *const Pane) []const u8 {
return switch (p.cwd) {
.none => "",
diff --git a/src/pardes.zig b/src/pardes.zig
index a29973e7..ce7c7065 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -687,9 +687,8 @@ test "pane-tag Exec prefers Tty and argument builtins before shell fallback" {
// Argument-taking builtins use the same gesture path: a tag sweep is the
// complete command line, and Theme consumes its tail without a PTY write.
- pane.tag_tail_len = 0;
- try std.testing.expect(pane.appendTag(" Theme dark"));
- pane.tag_init = true;
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = try p.gpa.dupe(u8, " Theme dark");
p.update(.{ .mouse = .{ .button = config.exec_button, .kind = .press, .col = tag_x + 1, .row = tag_y } });
p.update(.{ .mouse = .{ .button = config.exec_button, .kind = .drag, .col = tag_x + 10, .row = tag_y } });
p.update(.{ .mouse = .{ .button = config.exec_button, .kind = .release, .col = tag_x + 10, .row = tag_y } });
@@ -707,8 +706,8 @@ test "pane-tag Exec prefers Tty and argument builtins before shell fallback" {
// A word absent from the builtin vocabulary takes the fallback, including
// the terminating carriage return expected by the shell prompt.
- pane.tag_tail_len = 0;
- try std.testing.expect(pane.appendTag(" DefinitelyNotABuiltin"));
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = try p.gpa.dupe(u8, " DefinitelyNotABuiltin");
p.update(.{ .mouse = .{ .button = config.exec_button, .kind = .press, .col = tag_x + 1, .row = tag_y } });
p.update(.{ .mouse = .{ .button = config.exec_button, .kind = .release, .col = tag_x + 1, .row = tag_y } });
var sent: [256]u8 = undefined;
@@ -743,20 +742,20 @@ test "Collapse builtin keeps content and restores the pane from its default tag"
try std.testing.expectEqual(revision, pane.file.?.revision);
try std.testing.expectEqualStrings("keep this text\n", pane.file.?.content);
try std.testing.expect((try p.render(p.scratch.allocator())).cursor == null);
- p.enterTagEdit(pane, -1);
+ p.enterTag(pane);
const tag_cursor = (try p.render(p.scratch.allocator())).cursor orelse return error.MissingTagCursor;
try std.testing.expectEqual(p.rects[0].y, tag_cursor.y);
- Pardes.exitTagEdit(pane);
+ pane.focus = .body;
try std.testing.expect(p.executeBuiltinLine(0, "Collapse"));
p.sync();
try std.testing.expect(!pane.collapsed);
try std.testing.expectEqual(height, p.rects[0].h);
// Unedited defaults from the preceding version gain the new command.
- pane.tag_init = false;
- pane.tag_tail_len = 0;
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = null;
p.restoreTailAt(pane, pre_collapse_file_pane_tail, "", .file);
- try std.testing.expect(!pane.tag_init);
+ try std.testing.expect(pane.tag.own == null);
try std.testing.expectEqualStrings(file_pane_tail, Pardes.curTail(pane));
}
@@ -859,8 +858,8 @@ test "TreeContext command and custom tag survive a workspace round trip" {
try std.testing.expect(pane.file.?.tree_context);
try std.testing.expect(p.executeBuiltinLine(0, "TreeContext invalid"));
try std.testing.expect(pane.file.?.tree_context);
- try std.testing.expect(pane.appendTag(" Save Keep Del"));
- pane.tag_init = true;
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = try p.gpa.dupe(u8, " Save Keep Del");
try dump.dumpState(p);
const restored = try dump.initFromDump(p.gpa, .{ .tty_only = true }, p.dump_out.?);
defer restored.deinit();
@@ -874,22 +873,23 @@ test "Togglettymode builtin and keyboard chord share mode transitions including
defer p.deinit();
const pane = p.panes[0].?;
try std.testing.expect(std.mem.indexOf(u8, Pardes.curTail(pane), " Mode ") != null);
- p.seedTail(pane);
- try std.testing.expect(pane.appendTag(" Togglettymode"));
+ try pane.appendTag(" Togglettymode");
try std.testing.expectEqual(Text.Mode.tty, pane.body.mode);
try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode"));
try std.testing.expectEqual(Text.Mode.normal, pane.body.mode);
p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } });
try std.testing.expectEqual(Text.Mode.tty, pane.body.mode);
- p.enterTagEdit(pane, -1);
- try std.testing.expectEqual(Text.Mode.insert, pane.body.mode);
+ // The tag has its own mode: the body keeps tty while the tag is typed into.
+ p.enterTag(pane);
+ try std.testing.expect(pane.focus == .tag);
+ try std.testing.expectEqual(Text.Mode.tty, pane.body.mode);
try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode"));
try std.testing.expectEqual(Text.Mode.normal, pane.body.mode);
- try std.testing.expect(!pane.tag_edit);
- p.enterTagEdit(pane, -1);
+ try std.testing.expect(pane.focus == .body);
+ p.enterTag(pane);
p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } });
try std.testing.expectEqual(Text.Mode.tty, pane.body.mode);
- try std.testing.expect(!pane.tag_edit);
+ try std.testing.expect(pane.focus == .body);
p.acknowledgePanelPresentation(&.{});
const tag = try p.tagText(p.scratch.allocator(), pane);
@@ -974,10 +974,8 @@ test "compact tag commands stay beside their own prefix across layout changes" {
try std.testing.expectEqualStrings(above_tag, try p.tagText(p.scratch.allocator(), above));
p.update(.{ .resize = .{ .cols = 100, .rows = 30 } });
while (p.nextEffect()) |_| {}
-
- p.seedTail(below);
- try std.testing.expectEqualStrings(tail, below.tagSlice());
- try std.testing.expect(below.appendTag(" lots and lots of typing out here"));
+ try std.testing.expectEqualStrings(tail, Pardes.curTail(below));
+ try below.appendTag(" lots and lots of typing out here");
try std.testing.expectEqual(above_tag.len, (try p.tagText(p.scratch.allocator(), above)).len);
p.update(.{ .resize = .{ .cols = 100, .rows = 2 } });
@@ -997,14 +995,14 @@ test "legacy default tag tails upgrade while custom tails remain owned" {
legacy_pane_tail,
});
p.restoreTail(terminal, terminal_old);
- try std.testing.expect(!terminal.tag_init);
+ try std.testing.expect(terminal.tag.own == null);
try std.testing.expectEqualStrings(terminal_pane_tail, Pardes.curTail(terminal));
const terminal_previous = try std.fmt.allocPrint(p.scratch.allocator(), "{s} New Del", .{
try p.tagPrefix(terminal),
});
p.restoreTail(terminal, terminal_previous);
- try std.testing.expect(!terminal.tag_init);
+ try std.testing.expect(terminal.tag.own == null);
try std.testing.expectEqualStrings(terminal_pane_tail, Pardes.curTail(terminal));
const terminal_prefilter = try std.fmt.allocPrint(p.scratch.allocator(), "{s}{s}", .{
@@ -1012,60 +1010,60 @@ test "legacy default tag tails upgrade while custom tails remain owned" {
prev_terminal_pane_tail,
});
p.restoreTail(terminal, terminal_prefilter);
- try std.testing.expect(!terminal.tag_init);
+ try std.testing.expect(terminal.tag.own == null);
try std.testing.expectEqualStrings(terminal_pane_tail, Pardes.curTail(terminal));
for ([_][]const u8{ pre_mode_terminal_pane_tail, pre_tty_first_terminal_pane_tail, pre_collapse_terminal_pane_tail }) |old_tail| {
const old = try std.fmt.allocPrint(p.scratch.allocator(), "{s}{s}", .{ try p.tagPrefix(terminal), old_tail });
p.restoreTail(terminal, old);
- try std.testing.expect(!terminal.tag_init);
+ try std.testing.expect(terminal.tag.own == null);
try std.testing.expectEqualStrings(terminal_pane_tail, Pardes.curTail(terminal));
}
const custom_mode = try std.fmt.allocPrint(p.scratch.allocator(), "{s}{s} Keep", .{ try p.tagPrefix(terminal), pre_mode_terminal_pane_tail });
p.restoreTail(terminal, custom_mode);
- try std.testing.expect(terminal.tag_init);
+ try std.testing.expect(terminal.tag.own != null);
try std.testing.expectEqualStrings(pre_mode_terminal_pane_tail ++ " Keep", Pardes.curTail(terminal));
- terminal.tag_tail_len = 0;
- terminal.tag_init = false;
+ if (terminal.tag.own) |own| p.gpa.free(own);
+ terminal.tag.own = null;
const terminal_custom = try std.fmt.allocPrint(p.scratch.allocator(), "{s} Keep Del", .{
try p.tagPrefix(terminal),
});
p.restoreTail(terminal, terminal_custom);
- try std.testing.expect(terminal.tag_init);
+ try std.testing.expect(terminal.tag.own != null);
try std.testing.expectEqualStrings(" Keep Del", Pardes.curTail(terminal));
- terminal.tag_tail_len = 0;
- terminal.tag_init = false;
+ if (terminal.tag.own) |own| p.gpa.free(own);
+ terminal.tag.own = null;
p.restoreDumpTail(terminal, .{
.kind = .terminal,
.tag = "TTY /historical/cwd Keep Del",
.body = "",
.terminal = .{ .cwd = "/historical/cwd" },
});
- try std.testing.expect(terminal.tag_init);
+ try std.testing.expect(terminal.tag.own != null);
try std.testing.expectEqualStrings(" Keep Del", Pardes.curTail(terminal));
- terminal.tag_tail_len = 0;
- terminal.tag_init = false;
+ if (terminal.tag.own) |own| p.gpa.free(own);
+ terminal.tag.own = null;
p.restoreDumpTail(terminal, .{
.kind = .terminal,
.tag = "TTY /historical/cwd New Del",
.body = "",
.terminal = .{ .cwd = "/historical/cwd" },
});
- try std.testing.expect(!terminal.tag_init);
+ try std.testing.expect(terminal.tag.own == null);
try std.testing.expectEqualStrings(terminal_pane_tail, Pardes.curTail(terminal));
// This happens to be the historical FILE default, but on a terminal it is
// user-owned text and must not be swallowed by migration.
- terminal.tag_tail_len = 0;
- terminal.tag_init = false;
+ if (terminal.tag.own) |own| p.gpa.free(own);
+ terminal.tag.own = null;
const terminal_fileish = try std.fmt.allocPrint(p.scratch.allocator(), "{s} Save Del", .{
try p.tagPrefix(terminal),
});
p.restoreTail(terminal, terminal_fileish);
- try std.testing.expect(terminal.tag_init);
+ try std.testing.expect(terminal.tag.own != null);
try std.testing.expectEqualStrings(" Save Del", Pardes.curTail(terminal));
// A savable file has a distinct old default. Save remains first after the
@@ -1076,25 +1074,25 @@ test "legacy default tag tails upgrade while custom tails remain owned" {
legacy_file_pane_tail,
});
p.restoreTail(file, file_old);
- try std.testing.expect(!file.tag_init);
+ try std.testing.expect(file.tag.own == null);
try std.testing.expectEqualStrings(" Save Tty Collapse Del", Pardes.curTail(file));
const file_custom = try std.fmt.allocPrint(p.scratch.allocator(), "{s} Save Mine Del", .{
try p.tagPrefix(file),
});
p.restoreTail(file, file_custom);
- try std.testing.expect(file.tag_init);
+ try std.testing.expect(file.tag.own != null);
try std.testing.expectEqualStrings(" Save Mine Del", Pardes.curTail(file));
// Conversely the generic pane's current default is a custom edit on a
// savable file. Legacy recognition is scoped to the source pane class.
- file.tag_tail_len = 0;
- file.tag_init = false;
+ if (file.tag.own) |own| p.gpa.free(own);
+ file.tag.own = null;
const file_generic = try std.fmt.allocPrint(p.scratch.allocator(), "{s} New Del", .{
try p.tagPrefix(file),
});
p.restoreTail(file, file_generic);
- try std.testing.expect(file.tag_init);
+ try std.testing.expect(file.tag.own != null);
try std.testing.expectEqualStrings(" New Del", Pardes.curTail(file));
const image_doc = try panes.Image.create(p, 1, "/tmp/legacy image.ppm", &.{});
@@ -1105,8 +1103,8 @@ test "legacy default tag tails upgrade while custom tails remain owned" {
.body = "",
.image = .{ .path = "/tmp/legacy image.ppm" },
});
- try std.testing.expect(image_doc.tag_init);
- try std.testing.expectEqualStrings(" Keep Del", image_doc.tagSlice());
+ try std.testing.expect(image_doc.tag.own != null);
+ try std.testing.expectEqualStrings(" Keep Del", Pardes.curTail(image_doc));
}
test "Joincol folds the active column into its right neighbor, keeping its panes" {
@@ -1247,11 +1245,11 @@ test "pane defaults keep creation of scratch panes in column tags" {
try std.testing.expectEqualStrings("New Tty Find Grep Joincol Delcol", p.columnTag(0));
const terminal = p.panes[0].?;
p.restoreTailAt(terminal, pre_column_terminal_pane_tail, "", .terminal);
- try std.testing.expect(!terminal.tag_init);
+ try std.testing.expect(terminal.tag.own == null);
try std.testing.expectEqualStrings(terminal_pane_tail, Pardes.curTail(terminal));
const file = try p.setTestFile("unchanged\n");
p.restoreTailAt(file, pre_column_file_pane_tail, "", .file);
- try std.testing.expect(!file.tag_init);
+ try std.testing.expect(file.tag.own == null);
try std.testing.expectEqualStrings(file_pane_tail, Pardes.curTail(file));
}
@@ -1398,8 +1396,8 @@ test "image dump restores source bytes renderer choices and exact custom tail" {
pane.image.?.glyph_art = true;
pane.image.?.pmode = .terminal;
pane.image.?.ascii = false;
- try std.testing.expect(pane.appendTag(" Keep Del"));
- pane.tag_init = true;
+ if (pane.tag.own) |own| p.gpa.free(own);
+ pane.tag.own = try p.gpa.dupe(u8, " Keep Del");
try dump.dumpState(p);
var first_dump = try dump.readZon(gpa, p.dump_out.?, "image-first-dump");
@@ -1416,8 +1414,8 @@ test "image dump restores source bytes renderer choices and exact custom tail" {
try std.testing.expect(restored_pane.image.?.glyph_art);
try std.testing.expectEqual(image.PaletteMode.terminal, restored_pane.image.?.pmode);
try std.testing.expect(!restored_pane.image.?.ascii);
- try std.testing.expect(restored_pane.tag_init);
- try std.testing.expectEqualStrings(" Keep Del", restored_pane.tagSlice());
+ try std.testing.expect(restored_pane.tag.own != null);
+ try std.testing.expectEqualStrings(" Keep Del", Pardes.curTail(restored_pane));
try std.testing.expectEqualSlices(u8, source, restored_pane.image.?.raw);
try dump.dumpState(restored);
@@ -2095,20 +2093,20 @@ test "Save reaches every tagline with text behind it and no other" {
pane_tail,
});
p.restoreTail(out, dumped);
- try std.testing.expect(!out.tag_init);
+ try std.testing.expect(out.tag.own == null);
try std.testing.expectEqualStrings(locations_pane_tail, Pardes.curTail(out));
// A scratch is an output buffer that has worn the FILE tails all along,
// so its own older default upgrades from that family too.
out.file.?.output = .{ .from = .{ .cmd = std.meta.stringToEnum(Builtin, "New").? } };
- out.tag_tail_len = 0;
- out.tag_init = false;
+ if (out.tag.own) |own| p.gpa.free(own);
+ out.tag.own = null;
const scratch_dumped = try std.fmt.allocPrint(p.scratch.allocator(), "{s}{s}", .{
try p.tagPrefix(out),
prev_file_pane_tail,
});
p.restoreTail(out, scratch_dumped);
- try std.testing.expect(!out.tag_init);
+ try std.testing.expect(out.tag.own == null);
try std.testing.expectEqualStrings(" Save Tty Collapse Del", Pardes.curTail(out));
// An image's bytes on disk already are what they are: nothing of the
@@ -2629,24 +2627,25 @@ test "Unicode display cells map back to body and tag byte cursors" {
p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = text_x + 2, .row = body_y } });
try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col);
- // A click in the wide path glyph likewise becomes a byte cursor at the
+ // A click in the wide path glyph drafts a name with its caret at the
// grapheme start; arrow motion then advances by the full UTF-8 cluster.
- p.enterTagEdit(pane, 1);
- try std.testing.expectEqual(@as(u16, 0), pane.tag_col);
- p.tagInsertKey(pane, .{ .cp = Key.right });
- try std.testing.expectEqual(@as(u16, 3), pane.tag_col);
- p.tagInsertKey(pane, .{ .cp = Key.right });
- try std.testing.expectEqual(@as(u16, 6), pane.tag_col);
+ p.draftName(pane, 1);
+ try std.testing.expectEqual(@as(i32, 0), pane.input.cur_col);
+ edit.insertKey(p, &pane.input, .{ .cp = Key.right });
+ try std.testing.expectEqual(@as(i32, 3), pane.input.cur_col);
+ edit.insertKey(p, &pane.input, .{ .cp = Key.right });
+ try std.testing.expectEqual(@as(i32, 6), pane.input.cur_col);
+ pane.closePrompt();
- const before = try gpa.dupe(u8, pane.tagSlice());
+ const before = try gpa.dupe(u8, Pardes.curTail(pane));
defer gpa.free(before);
- p.enterTagEdit(pane, -1);
- const insertion = pane.tag_col;
- p.tagInsertKey(pane, .{ .cp = 'λ', .text = "λ" });
- try std.testing.expectEqual(insertion + 2, pane.tag_col);
- p.tagInsertKey(pane, .{ .cp = Key.backspace });
- try std.testing.expectEqual(insertion, pane.tag_col);
- try std.testing.expectEqualStrings(before, pane.tagSlice());
+ p.enterTag(pane);
+ const insertion = pane.tag.cur_col;
+ edit.insertKey(p, &pane.tag, .{ .cp = 'λ', .text = "λ" });
+ try std.testing.expectEqual(insertion + 2, pane.tag.cur_col);
+ edit.insertKey(p, &pane.tag, .{ .cp = Key.backspace });
+ try std.testing.expectEqual(insertion, pane.tag.cur_col);
+ try std.testing.expectEqualStrings(before, Pardes.curTail(pane));
}
test "tabbed file aligns syntax cursor and mouse at the nearest insertion gap" {
@@ -4110,6 +4109,9 @@ pub const Pardes = struct {
if (pane.file) |*file| panes.File.deinit(p, pane, file);
if (pane.ovl) |o| p.gpa.free(o.text);
if (pane.input.own) |text| p.gpa.free(text);
+ if (pane.tag.own) |text| p.gpa.free(text);
+ for (pane.tag.ed_undo[0..pane.tag.ed_undo_len]) |sn| if (sn.ovl) |o| p.gpa.free(o.text);
+ for (pane.tag.ed_redo[0..pane.tag.ed_redo_len]) |sn| if (sn.ovl) |o| p.gpa.free(o.text);
for (pane.body.ed_undo[0..pane.body.ed_undo_len]) |sn| if (sn.ovl) |o| p.gpa.free(o.text);
for (pane.body.ed_redo[0..pane.body.ed_redo_len]) |sn| if (sn.ovl) |o| p.gpa.free(o.text);
panes.Terminal.deinitEmulator(pane, p.gpa);
@@ -4762,7 +4764,7 @@ pub const Pardes = struct {
);
if (pane.image) |*state| return panes.Image.tagPrefix(arena, state);
if (pane.file) |f| {
- const name = if (pane.tag_name_len) |len| pane.tag_name[0..len] else f.path;
+ const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path;
if (panes.Output.fileTraits(f.output).saves and f.revision != f.saved_revision)
return std.fmt.allocPrint(arena, "{s}{s}", .{ name, dirty_marker });
return arena.dupe(u8, name);
@@ -4770,10 +4772,10 @@ pub const Pardes = struct {
return arena.dupe(u8, pane.cwdSlice());
}
- /// the editable tail: the user's edited buffer once touched, else defaults
- fn curTail(pane: *Pane) []const u8 {
- if (pane.tag_init) return pane.tagSlice();
- return defaultTail(pane);
+ /// The tag's own text: what the user made of it, or until then the
+ /// default a pane of its kind starts with.
+ pub fn curTail(pane: *Pane) []const u8 {
+ return pane.tag.own orelse defaultTail(pane);
}
pub fn defaultTail(pane: *const Pane) []const u8 {
@@ -4793,6 +4795,8 @@ pub const Pardes = struct {
return null;
}
+ /// The tag as it is shown, read and pointed at: its computed prefix and
+ /// its own text after it.
pub fn tagText(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]u8 {
const prefix = try p.tagPrefix(pane);
const tail = curTail(pane);
@@ -4814,7 +4818,7 @@ pub const Pardes = struct {
}
var start: usize = 0;
const name = if (pane.file) |file|
- if (pane.tag_name_len) |len| pane.tag_name[0..len] else file.path
+ if (pane.prompt == .name) pane.input.own orelse file.path else file.path
else if (pane.image) |state| blk: {
// Image tags put renderer controls before the path.
start = prefix_len -| state.path.len;
@@ -4827,88 +4831,76 @@ pub const Pardes = struct {
return .{ .lo = start + basename, .hi = start + name.len };
}
- pub fn seedTail(_: *Pardes, pane: *Pane) void {
- if (pane.tag_init) return;
+ /// Give the keyboard to the tag in normal mode (`:`), its cursor where
+ /// the tag last had it. The first time there is nothing to remember, and
+ /// it starts on Save, so that `:` and the execute key write the file.
+ pub fn enterTag(p: *Pardes, pane: *Pane) void {
+ const t = &pane.tag;
const tail = curTail(pane);
- if (tail.len > pane.tag_tail.len) return;
- @memcpy(pane.tag_tail[0..tail.len], tail);
- pane.tag_tail_len = tail.len;
- pane.tag_init = true;
- }
-
- pub fn enterTagEdit(p: *Pardes, pane: *Pane, col: i32) void {
- if (pane.tag_edit) pane.tag_saved_col = pane.tag_col;
- const edit0: i32 = @intCast((p.tagPrefix(pane) catch return).len);
- p.seedTail(pane);
- if (!pane.tag_init) return;
- if (!pane.tag_edit) pane.tag_mode = pane.body.mode;
- pane.tag_edit = true;
- pane.tag_sel = false;
- // a one-line tag has no use for normal mode: always insert
- pane.body.mode = .insert;
- pane.body.normal.clear();
- const end = edit0 + @as(i32, @intCast(pane.tag_tail_len));
- const tail = pane.tagSlice();
- const lead: i32 = @intCast(tail.len - std.mem.trimStart(u8, tail, " ").len);
- if (col < 0) {
- const text = p.tagText(p.scratch.allocator(), pane) catch return;
- if (pane.tag_saved_col) |saved| {
- pane.tag_col = @intCast(modal.graphemeStart(text, @min(saved, text.len)));
- } else {
- pane.tag_col = @intCast(@min(edit0 + lead, end));
- }
- if (pane.tag_saved_col == null) if (p.paneIdOf(pane)) |id| {
+ if (!t.cur_pinned) {
+ var words = std.mem.tokenizeAny(u8, tail, " \t\n");
+ const at = while (words.next()) |word| {
+ if (std.mem.eql(u8, word, "Save")) break @intFromPtr(word.ptr) - @intFromPtr(tail.ptr);
+ } else tail.len - std.mem.trimStart(u8, tail, " ").len;
+ const cursor = modal.positionAt(tail, @min(at, tail.len));
+ t.cur_row = @intCast(cursor.row);
+ t.cur_col = @intCast(cursor.col);
+ t.cur_pinned = true;
+ // Reveal the suffix together with its commands, not merely the
+ // first command letter at the viewport's far right edge.
+ if (p.paneIdOf(pane)) |id| {
+ const tag = p.tagText(p.scratch.allocator(), pane) catch return;
+ const line = tag[0 .. std.mem.indexOfScalar(u8, tag, '\n') orelse tag.len];
const width = p.tagCapacity(p.rects[id].w -| TAG_TEXT_INSET);
- // Reveal the suffix together with its commands, not merely the
- // first command letter at the viewport's far right edge.
- pane.tag_scroll = @intCast(panes.File.displayWidth(text) + 1 -| width);
- };
- } else {
- const text = p.tagText(p.scratch.allocator(), pane) catch return;
- pane.tag_col = @intCast(@min(text.len, panes.File.rawAtDisplay(text, @as(usize, @intCast(col)) + pane.tag_scroll)));
+ pane.tag_scroll = @intCast(panes.File.displayWidth(line) + 1 -| width);
+ }
}
+ // The text may have changed since (a 9P write, a rename), so the
+ // remembered cursor is pulled back inside it. It is the cursor that
+ // is remembered, not a selection.
+ const rows = modal.cursorLineCount(tail);
+ t.cur_row = @intCast(@min(@as(usize, @intCast(@max(0, t.cur_row))), rows - 1));
+ const line = modal.lineSlice(tail, @intCast(t.cur_row));
+ t.cur_col = @intCast(modal.graphemeStart(line, @min(line.len, @as(usize, @intCast(@max(0, t.cur_col))))));
+ t.vsel.active = false;
+ t.msel.active = false;
+ t.nsel = 0;
+ t.select = false;
+ t.append_at = null;
+ t.sticky_col = -1;
+ t.mode = .normal;
+ t.normal.clear();
+ pane.focus = .tag;
}
- pub fn exitTagEdit(pane: *Pane) void {
- if (pane.tag_edit) pane.tag_saved_col = pane.tag_col;
- if (pane.isTerminal()) {
- if (pane.tag_edit) pane.body.mode = pane.tag_mode;
- } else pane.body.mode = .normal;
- pane.tag_edit = false;
- pane.tag_sel = false;
- pane.tag_name_len = null;
- pane.tag_scroll = 0;
- }
-
- pub fn tagSelBounds(pane: *Pane) struct { lo: usize, hi: usize } {
- const a: usize = pane.tag_anchor;
- const c: usize = pane.tag_col;
- return .{ .lo = @min(a, c), .hi = @max(a, c) };
- }
-
- fn tagChordText(p: *Pardes, pane: *Pane) ?[]const u8 {
- const text = p.tagText(p.scratch.allocator(), pane) catch return null;
- if (pane.tag_sel) {
- const b = tagSelBounds(pane);
- const hi = modal.nextGrapheme(text, b.hi);
- return if (hi > b.lo) text[b.lo..hi] else null;
- }
- const b = config.wordBounds(text, @min(@as(usize, pane.tag_col), text.len));
- return if (b.hi > b.lo) text[b.lo..b.hi] else null;
+ /// Where display column `col` of the tagline (tag scroll included) lands
+ /// in the tag's own text, or null when it is on the prefix.
+ pub fn tagOffsetAt(p: *Pardes, pane: *Pane, col: usize) ?usize {
+ const prefix = p.tagPrefix(pane) catch return null;
+ const width = panes.File.displayWidth(prefix);
+ if (col < width) return null;
+ const tail = curTail(pane);
+ const line = tail[0 .. std.mem.indexOfScalar(u8, tail, '\n') orelse tail.len];
+ return modal.graphemeStart(line, panes.File.rawAtDisplay(line, col - width));
}
- fn tagNameEnd(pane: *const Pane) ?usize {
- const f = pane.file orelse return null;
- if (!panes.Output.fileTraits(f.output).saves or f.path.len > pane.tag_name.len) return null;
- return if (pane.tag_name_len) |len| len else f.path.len;
+ /// Start a name draft for a file that can take one, its caret at display
+ /// column `col` of the path. Nothing changes until it is typed into.
+ pub fn draftName(p: *Pardes, pane: *Pane, col: usize) void {
+ const f = pane.file orelse return;
+ if (!panes.Output.fileTraits(f.output).saves) return;
+ const name = p.gpa.dupe(u8, f.path) catch return;
+ pane.closePrompt();
+ pane.prompt = .name;
+ pane.input = .{ .what = .input, .own = name, .mode = .insert, .cur_pinned = true, .cur_col = @intCast(@min(name.len, panes.File.rawAtDisplay(name, col))) };
}
- /// Commit a staged buffer name, never a disk rename or write. Keep the
+ /// Commit a drafted buffer name, never a disk rename or write. Keep the
/// draft visible on failure so the user can correct it or cancel with Esc.
pub fn commitTagName(p: *Pardes, pane: *Pane) bool {
- const len = pane.tag_name_len orelse return true;
+ if (pane.prompt != .name) return true;
const id = p.paneIdOf(pane) orelse return false;
- const name = pane.tag_name[0..len];
+ const name = pane.input.own orelse "";
if (name.len == 0 or name[name.len - 1] == '/' or !std.unicode.utf8ValidateSlice(name)) {
p.reportError(id, "name", error.InvalidName);
return false;
@@ -4917,8 +4909,6 @@ pub const Pardes = struct {
p.reportError(id, "name", error.InvalidName);
return false;
};
- const old_prefix = (p.tagPrefix(pane) catch return false).len;
- const tail_col = @as(usize, pane.tag_col) -| old_prefix;
const full = std.fs.path.resolvePosix(p.scratch.allocator(), &.{ paneDir(pane), name }) catch |err| {
p.reportError(id, "name", err);
return false;
@@ -4927,181 +4917,10 @@ pub const Pardes = struct {
p.reportError(id, "name", err);
return false;
};
- pane.tag_name_len = null;
- const prefix = (p.tagPrefix(pane) catch return false).len;
- pane.tag_col = @intCast(prefix + @min(tail_col, pane.tag_tail_len));
- pane.tag_sel = false;
- return true;
- }
-
- /// Replace one range in the rendered tag. The file name and command tail
- /// retain separate identities even when a selection crosses their boundary.
- pub fn replaceTagText(p: *Pardes, pane: *Pane, start: usize, finish: usize, input: []const u8) bool {
- const prefix_len = (p.tagPrefix(pane) catch return false).len;
- const end = prefix_len + pane.tag_tail_len;
- const lo = @min(start, end);
- const hi = @max(lo, @min(finish, end));
- const name_end = tagNameEnd(pane);
- const editing_name = if (name_end) |n| lo <= n else false;
- if (!editing_name and lo < prefix_len) return false;
- var bytes: [limits.max_tag_tail]u8 = undefined;
- var count: usize = 0;
- for (input) |byte| {
- if (byte == '\n' or byte == '\r') continue;
- if (count == bytes.len) return false;
- bytes[count] = byte;
- count += 1;
- }
- const tail_lo = lo -| prefix_len;
- const tail_hi = hi -| prefix_len;
- const tail_insert = if (editing_name) 0 else count;
- const tail_len = pane.tag_tail_len - (tail_hi - tail_lo) + tail_insert;
- if (tail_len > pane.tag_tail.len) return false;
- if (editing_name) {
- const n = name_end.?;
- const name_hi = @min(hi, n);
- const name_len = n - (name_hi - lo) + count;
- if (name_len > pane.tag_name.len) return false;
- if (pane.tag_name_len == null) @memcpy(pane.tag_name[0..n], pane.file.?.path);
- if (count > name_hi - lo)
- std.mem.copyBackwards(u8, pane.tag_name[lo + count .. name_len], pane.tag_name[name_hi..n])
- else
- std.mem.copyForwards(u8, pane.tag_name[lo + count .. name_len], pane.tag_name[name_hi..n]);
- @memcpy(pane.tag_name[lo..][0..count], bytes[0..count]);
- pane.tag_name_len = @intCast(name_len);
- }
- if (tail_insert > tail_hi - tail_lo)
- std.mem.copyBackwards(u8, pane.tag_tail[tail_lo + tail_insert .. tail_len], pane.tag_tail[tail_hi..pane.tag_tail_len])
- else
- std.mem.copyForwards(u8, pane.tag_tail[tail_lo + tail_insert .. tail_len], pane.tag_tail[tail_hi..pane.tag_tail_len]);
- if (!editing_name) @memcpy(pane.tag_tail[tail_lo..][0..count], bytes[0..count]);
- pane.tag_tail_len = tail_len;
- pane.tag_col = @intCast(lo + count);
- pane.tag_sel = false;
+ pane.closePrompt();
return true;
}
- fn tagInsertKey(p: *Pardes, pane: *Pane, key: Key) void {
- if (hit(key, config.escape)) {
- exitTagEdit(pane); // the tag is ALWAYS insert; Esc leaves it
- return;
- }
- // the acme chords on the selection or the word at the cursor
- if (hit(key, config.look_key) or hit(key, config.exec_key)) {
- if (pane.tag_name_len != null) {
- const in_name = pane.tag_col < (p.tagPrefix(pane) catch return).len;
- if (!p.commitTagName(pane)) return;
- if (hit(key, config.look_key) or in_name) {
- exitTagEdit(pane);
- return;
- }
- }
- if (p.tagChordText(pane)) |txt| {
- pane.tag_sel = false;
- exec.runBuiltin(p, if (hit(key, config.look_key)) config.look_cmd else config.exec_cmd, p.active, "", txt);
- }
- return;
- }
- const text = p.tagText(p.scratch.allocator(), pane) catch return;
- const end = text.len;
- if (key.text.len > 0) {
- const range: @TypeOf(tagSelBounds(pane)) = if (pane.tag_sel) tagSelBounds(pane) else .{ .lo = pane.tag_col, .hi = pane.tag_col };
- const hi = if (pane.tag_sel) modal.nextGrapheme(text, range.hi) else range.hi;
- _ = p.replaceTagText(pane, range.lo, hi, key.text);
- return;
- }
- switch (key.cp) {
- Key.backspace, Key.delete => {
- if (pane.tag_sel) {
- const range = tagSelBounds(pane);
- _ = p.replaceTagText(pane, range.lo, modal.nextGrapheme(text, range.hi), "");
- } else if (key.cp == Key.backspace and pane.tag_col > 0) {
- _ = p.replaceTagText(pane, modal.prevGrapheme(text, pane.tag_col), pane.tag_col, "");
- } else if (key.cp == Key.delete and pane.tag_col < end) {
- _ = p.replaceTagText(pane, pane.tag_col, modal.nextGrapheme(text, pane.tag_col), "");
- }
- },
- Key.left => if (pane.tag_col > 0) {
- pane.tag_col = @intCast(modal.prevGrapheme(text, pane.tag_col));
- },
- Key.right => if (pane.tag_col < end) {
- pane.tag_col = @intCast(modal.nextGrapheme(text, pane.tag_col));
- },
- Key.home => pane.tag_col = 0,
- Key.end => pane.tag_col = @intCast(end),
- else => {},
- }
- }
-
- /// write a helix range back onto the tag cursor + selection: the rendered
- /// tag's one-line mirror of setPaneRange.
- fn setTagRange(pane: *Pane, text: []const u8, r: modal.Selection) void {
- const lo = @min(r.anchor, r.head);
- const hi = @max(r.anchor, r.head);
- pane.tag_col = @intCast(modal.selectionCursor(text, r));
- pane.tag_sel = modal.nextGrapheme(text, lo) < hi; // one grapheme IS the block cursor
- if (pane.tag_sel) pane.tag_anchor = @intCast(if (r.head > r.anchor) lo else modal.prevGrapheme(text, hi));
- }
-
- fn tagNormalKey(p: *Pardes, pane: *Pane, key: Key) void {
- const text = p.tagText(p.scratch.allocator(), pane) catch return;
- const cur: usize = @min(@as(usize, pane.tag_col), text.len);
- // Esc abandons the command line: back to the body, tail kept as text
- if (hit(key, config.escape) or hit(key, &.{.{ .cp = 'j' }})) return exitTagEdit(pane);
- if (hit(key, config.look_key) or hit(key, config.exec_key)) {
- const cmd = if (hit(key, config.look_key)) config.look_cmd else config.exec_cmd;
- const txt = p.tagChordText(pane) orelse return;
- const id = p.active;
- const body_mode = pane.tag_mode;
- exitTagEdit(pane); // leave the tag FIRST: Del frees the pane
- pane.body.mode = body_mode;
- exec.runBuiltin(p, cmd, id, "", txt);
- return;
- }
- if (hit(key, config.tag_yank)) {
- if (p.tagChordText(pane)) |txt| edit.setClipboard(p, txt);
- return;
- }
- if (hit(key, config.insert) or hit(key, config.append) or
- hit(key, config.insert_line_start) or hit(key, config.insert_line_end))
- {
- const edit0: usize = if (tagNameEnd(pane) != null) 0 else (p.tagPrefix(pane) catch return).len;
- pane.tag_col = @intCast(@max(edit0, if (hit(key, config.append))
- modal.nextGrapheme(text, cur)
- else if (hit(key, config.insert_line_start))
- 0
- else if (hit(key, config.insert_line_end))
- text.len
- else
- cur));
- pane.tag_sel = false;
- pane.body.mode = .insert;
- return;
- }
- const dir: ?Builtin = for (config.window_keys) |wk| {
- var letter = wk.letter;
- letter.cp = std.ascii.toUpper(@intCast(letter.cp));
- if (hit(key, &.{letter})) break wk.cmd;
- } else null;
- if (dir) |d| {
- const from = p.active;
- exec.runBuiltin(p, d, from, "", null);
- if (p.active == from) {
- if (d == .Up) {
- exitTagEdit(pane);
- p.enterHeader(if (p.columnBarHeight() > 0) (layout.findPane(p, from) orelse return).col else null);
- }
- return;
- }
- const dest = p.panes[p.active] orelse return;
- exitTagEdit(pane); // the pane we left is fully out of its tag...
- p.enterTagEdit(dest, -1);
- if (dest.tag_edit) dest.body.mode = .normal;
- return;
- }
- if (lineMotion(text, cur, key)) |r| setTagRange(pane, text, r);
- }
-
fn lineMotion(text: []const u8, cur: usize, key: Key) ?modal.Selection {
const target: ?usize = if (hit(key, config.line_move_left))
modal.prevGrapheme(text, cur)
@@ -5243,8 +5062,7 @@ pub const Pardes = struct {
const pane = p.panes[d].?;
p.exitHeader();
p.active = d;
- p.enterTagEdit(pane, -1);
- if (pane.tag_edit) pane.body.mode = .normal;
+ p.enterTag(pane);
return;
}
if (hit(key, config.topbar_up)) {
@@ -5268,12 +5086,11 @@ pub const Pardes = struct {
pub fn cycleMode(p: *Pardes, id: usize) void {
const pane = p.panes[id] orelse return;
if (pane.file == null and !pane.isTerminal()) return;
- const previous = if (pane.tag_edit) pane.tag_mode else pane.body.mode;
+ const previous = pane.body.mode;
p.active = id;
p.exitHeader();
- if (pane.tag_edit) exitTagEdit(pane);
+ pane.focus = .body;
pane.closePrompt();
- pane.body.mode = previous;
switch (previous) {
.tty => {
pane.body.mode = .normal;
@@ -5293,7 +5110,7 @@ pub const Pardes = struct {
if (!pane.isTerminal()) return;
p.active = id;
p.exitHeader();
- if (pane.tag_edit) exitTagEdit(pane);
+ pane.focus = .body;
pane.closePrompt();
if (pane.body.mode == .tty) {
pane.body.mode = .normal;
@@ -5314,7 +5131,7 @@ pub const Pardes = struct {
}
if (p.topbar_col != null) return p.topbarKey(key);
const pane = p.panes[p.active] orelse return;
- if (pane.isTerminal() and pane.body.mode == .tty and !pane.tag_edit) {
+ if (pane.isTerminal() and pane.body.mode == .tty and pane.focus == .body and pane.prompt == .none) {
p.leader_on = false;
p.ctrl_w_pending = false;
if (key.ctrl and !key.alt and key.cp == p.opts.tty_toggle)
@@ -5352,7 +5169,8 @@ pub const Pardes = struct {
}
// insert mode owns Ctrl-w (delete word backward, helix); the focus
// prefix applies outside raw tty mode
- if (hit(key, config.window_prefix) and pane.body.mode != .insert and pane.prompt == .none) {
+ const t = if (pane.focus == .tag) &pane.tag else &pane.body;
+ if (hit(key, config.window_prefix) and t.mode != .insert and pane.prompt == .none) {
p.ctrl_w_pending = true;
return;
}
@@ -5385,6 +5203,21 @@ pub const Pardes = struct {
if (pane.isTerminal() and tty_toggle) {
return p.toggleTty(p.active);
}
+ if (pane.prompt == .name) {
+ // Enter or the execute key commits the draft and the keyboard
+ // goes back to the body; Esc drops it.
+ if (hit(key, config.look_key) or hit(key, config.exec_key)) {
+ if (p.commitTagName(pane)) pane.focus = .body;
+ return;
+ }
+ if (hit(key, config.escape)) {
+ pane.closePrompt();
+ pane.focus = .body;
+ return;
+ }
+ edit.handleInsert(p, &pane.input, key);
+ return;
+ }
if (pane.prompt != .none) {
if (hit(key, config.search_submit) or hit(key, config.escape)) {
const submit = hit(key, config.search_submit);
@@ -5395,7 +5228,7 @@ pub const Pardes = struct {
look.submitSearch(p, p.active)
else if (normal.selRegexArmed(pane)) |_|
normal.applySelRegex(p, pane, "", false),
- .none, .del_side => unreachable,
+ .none, .del_side, .name => unreachable,
}
pane.closePrompt();
pane.body.mode = .normal;
@@ -5406,12 +5239,27 @@ pub const Pardes = struct {
if (normal.selRegexArmed(pane)) |a| normal.applySelRegex(p, pane, a.pat, a.split);
return;
}
- if (pane.tag_edit) {
- if (pane.body.mode == .normal) p.tagNormalKey(pane, key) else p.tagInsertKey(pane, key);
- return;
- }
- if (pane.body.mode == .normal and (hit(key, config.look_key) or hit(key, config.exec_key))) {
+ if (t.mode == .normal and (hit(key, config.look_key) or hit(key, config.exec_key))) {
const cmd = if (hit(key, config.look_key)) config.look_cmd else config.exec_cmd;
+ if (pane.focus == .tag) {
+ // An explicit selection, else the word under the cursor in the
+ // tag as it is shown: the prefix is part of what a word is.
+ const tag = p.tagText(p.scratch.allocator(), pane) catch return;
+ const tail = curTail(pane);
+ const range = t.primaryRange(tail, 0);
+ const lo = @min(range.anchor, range.head);
+ const hi = @max(range.anchor, range.head);
+ const txt = if ((t.vsel.active and t.vsel.explicit) or t.msel.active)
+ tail[lo..hi]
+ else word: {
+ const b = config.wordBounds(tag, tag.len - tail.len + modal.selectionCursor(tail, range));
+ break :word tag[b.lo..b.hi];
+ };
+ // Out of the tag FIRST: Del frees the pane.
+ pane.focus = .body;
+ if (txt.len > 0) exec.runBuiltin(p, cmd, p.active, "", txt);
+ return;
+ }
edit.pinPaneCursor(p, &pane.body);
const explicit = (p.native_images and pane.hasPdfSelection()) or
(pane.body.vsel.active and pane.body.vsel.explicit) or pane.body.msel.active;
@@ -5430,14 +5278,12 @@ pub const Pardes = struct {
exec.runBuiltin(p, cmd, p.active, "", word);
return;
}
- if (pane.body.mode == .normal and pane.hasPdf()) return normal.handlePdfNormal(p, pane, key);
- switch (pane.body.mode) {
- .normal => {
- normal.handleNormal(p, &pane.body, key);
- },
+ if (t.mode == .normal and t.what == .body and pane.hasPdf()) return normal.handlePdfNormal(p, pane, key);
+ switch (t.mode) {
+ .normal => normal.handleNormal(p, t, key),
.insert => {
- if (hit(key, config.escape)) return edit.exitInsert(p, &pane.body);
- edit.handleInsert(p, &pane.body, key);
+ if (hit(key, config.escape)) return edit.exitInsert(p, t);
+ edit.handleInsert(p, t, key);
},
.tty => panes.Terminal.forwardKey(p, p.active, key),
}
@@ -5507,7 +5353,7 @@ pub const Pardes = struct {
const dir = if (request == .save) paneDir(pane) else "";
const slash: []const u8 = if (request == .save and (dir.len == 0 or dir[dir.len - 1] != '/')) "/" else "";
const answer = std.mem.concat(p.gpa, u8, &.{ marker, dir, slash }) catch return;
- if (pane.tag_edit) exitTagEdit(pane);
+ pane.focus = .body;
// Not closePrompt: an s/S snapshot was taken for this very prompt.
if (pane.input.own) |old| p.gpa.free(old);
pane.prompt = switch (request) {
@@ -5806,8 +5652,7 @@ pub const Pardes = struct {
pub fn restoreDumpTail(p: *Pardes, pane: *Pane, src: dump.Pane) void {
if (src.tag_tail) |tail| {
- if (!pane.appendTag(tail)) return;
- pane.tag_init = true;
+ pane.tag.own = p.gpa.dupe(u8, tail) catch return;
return;
}
const tail_class = tailClass(pane);
@@ -5855,7 +5700,7 @@ pub const Pardes = struct {
}
fn restoreTailAt(
- _: *Pardes,
+ p: *Pardes,
pane: *Pane,
saved_tag: []const u8,
pfx: []const u8,
@@ -5886,8 +5731,7 @@ pub const Pardes = struct {
};
for (defaults) |default|
if (std.mem.eql(u8, said, std.mem.trimStart(u8, default, " "))) return;
- if (!pane.appendTag(rest)) return;
- pane.tag_init = true;
+ pane.tag.own = p.gpa.dupe(u8, rest) catch return;
}
/// Close a pane; its rows go to `to`, or where layout.absorbVWeight
@@ -6053,8 +5897,18 @@ pub const Pardes = struct {
p.trackJump();
for (&p.panes, 0..) |*slot, id| {
const pane = slot.* orelse continue;
- if (pane.tag_edit and (id != p.active or p.topbar_col != null))
- exitTagEdit(pane);
+ // The keyboard is on one pane, or on a header: a tag it has left
+ // hands it back to its body, and a name drafted there is dropped.
+ if (id != p.active or p.topbar_col != null) {
+ pane.focus = .body;
+ if (pane.prompt == .name) pane.closePrompt();
+ }
+ // A sweep over the tag is the tag's while it has the keyboard.
+ const swept = pane.sel[sel_slot];
+ if (pane.focus == .body and swept.state == .done and @min(swept.r0, swept.r1) < BOX_H) {
+ pane.clearPointerSelection(sel_slot);
+ pane.sel[sel_slot].state = .none;
+ }
if (comptime terminal_panes) if (pane.terminal) |state| {
var off: u16 = 0;
while (off < state.reply_len) {
@@ -6430,7 +6284,7 @@ pub const Pardes = struct {
s.set(d.cur_x, placement.row, "▌", .{ .fg = .{ .rgb = chrome.lineno } });
}
},
- .none, .select, .tag => if (p.pointer_inside) {
+ .none, .select => if (p.pointer_inside) {
// hover hints on a pane's own trailing edge
var c: usize = 0;
while (c + 1 < p.ncol) : (c += 1) {
@@ -6801,8 +6655,7 @@ pub const Pardes = struct {
.font_role = .tagline,
};
s.fill(r.x, tag_y, config.GUTTER, BOX_H, box_style);
- const box_mode = if (pane.tag_edit) pane.tag_mode else pane.body.mode;
- s.set(r.x, tag_y, switch (box_mode) {
+ s.set(r.x, tag_y, switch (pane.body.mode) {
.normal => config.tag_normal,
.insert => config.tag_insert,
.tty => config.tag_tty,
@@ -6856,9 +6709,24 @@ pub const Pardes = struct {
.bg = .{ .rgb = tag_bg },
.font_role = .tagline,
});
- const tag = try p.tagText(arena, pane);
- if (active and pane.tag_edit) {
- const caret = panes.File.rawDisplayCol(tag, pane.tag_col);
+ const whole = try p.tagText(arena, pane);
+ const tail = curTail(pane);
+ const prefix_len = whole.len - tail.len;
+ // The tagline shows the tag's first line: its prefix and the first
+ // line of its own text.
+ const tag = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len];
+ // The keyboard's caret on it, as a byte offset: in its own text when
+ // the tag has the keyboard, in the path while a name is drafted.
+ const caret_at: ?usize = if (!active)
+ null
+ else if (pane.prompt == .name)
+ @min(tag.len, @as(usize, @intCast(@max(0, pane.input.cur_col))))
+ else if (pane.focus == .tag and pane.tag.cur_row == 0)
+ @min(tag.len, prefix_len + @as(usize, @intCast(@max(0, pane.tag.cur_col))))
+ else
+ null;
+ if (caret_at) |at| {
+ const caret = panes.File.rawDisplayCol(tag, at);
var scroll: usize = pane.tag_scroll;
if (caret < scroll) scroll = caret;
if (caret >= scroll + capacity) scroll = caret - capacity + 1;
@@ -6869,10 +6737,10 @@ pub const Pardes = struct {
scroll = panes.File.rawDisplayCol(tag, raw);
}
pane.tag_scroll = @intCast(scroll);
- } else pane.tag_scroll = 0;
+ } else if (pane.focus != .tag) pane.tag_scroll = 0;
const tag_start = panes.File.rawAtDisplay(tag, pane.tag_scroll);
_ = s.print(tx, tag_y, tw, tag[tag_start..], tag_style);
- if (tagTintRange(pane, tag, tag.len - curTail(pane).len)) |name| {
+ if (tagTintRange(pane, tag, prefix_len)) |name| {
const lo = @max(name.lo, tag_start);
const hi = @min(name.hi, tag.len);
if (lo < hi) {
@@ -6889,23 +6757,31 @@ pub const Pardes = struct {
body_layer.paintPointerSelection(p, pane, r, tx, tw, tag_y, body_y, sel, preview_bg, null);
};
}
- if (pane.tag_edit and pane.tag_sel) {
- const b = tagSelBounds(pane);
- var col = panes.File.rawDisplayCol(tag, b.lo) -| pane.tag_scroll;
- const hi = modal.nextGrapheme(tag, b.hi);
- const end = panes.File.rawDisplayCol(tag, hi);
- while (col + pane.tag_scroll < end and col < tw) : (col += 1) {
- const cell = s.at(tx + @as(u16, @intCast(col)), tag_y);
- cell.default = false;
- cell.style.bg = .{ .rgb = th.sel_bg };
- cell.style.fg = .{ .rgb = th.sel_fg };
+ // The tag's own selections, while it has the keyboard. A range of one
+ // grapheme is the block cursor, not a selection.
+ if (pane.focus == .tag) {
+ var ranges: [Text.max_selections]modal.Selection = undefined;
+ const got = pane.tag.ranges(tail, 0, &ranges);
+ const shown = tag.len - prefix_len;
+ for (ranges[0..got.n]) |range| {
+ const lo = @min(range.anchor, range.head);
+ const hi = @max(range.anchor, range.head);
+ if (modal.nextGrapheme(tail, lo) >= hi or lo >= shown) continue;
+ var col = panes.File.rawDisplayCol(tag, prefix_len + lo) -| pane.tag_scroll;
+ const end = panes.File.rawDisplayCol(tag, prefix_len + @min(hi, shown));
+ while (col + pane.tag_scroll < end and col < tw) : (col += 1) {
+ const cell = s.at(tx + @as(u16, @intCast(col)), tag_y);
+ cell.default = false;
+ cell.style.bg = .{ .rgb = th.sel_bg };
+ cell.style.fg = .{ .rgb = th.sel_fg };
+ }
}
}
- // cursor while editing the tag: byte offset mapped to its display cell
- if (active and pane.tag_edit) {
- // bar while typing, block for `:` normal mode (same rule as a body)
- const col = panes.File.rawDisplayCol(tag, pane.tag_col) -| pane.tag_scroll;
- if (col < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(col)), .y = tag_y, .bar = pane.body.mode == .insert };
+ // the caret: bar while typing, block in normal mode (same rule as a body)
+ if (caret_at) |at| {
+ const col = panes.File.rawDisplayCol(tag, at) -| pane.tag_scroll;
+ const mode = if (pane.prompt == .name) pane.input.mode else pane.tag.mode;
+ if (col < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(col)), .y = tag_y, .bar = mode == .insert };
}
for (pane.sel, 0..) |selection, button| {
@@ -7034,10 +6910,10 @@ test "raw terminal cursor obeys visibility without hiding modal and tag cursors"
const modal_cursor = (try p.render(frame.allocator())).cursor orelse return error.MissingCursor;
try std.testing.expectEqual(rect.x + config.GUTTER + 1, modal_cursor.x);
try std.testing.expectEqual(body_y, modal_cursor.y);
- p.enterTagEdit(pane, 0);
+ p.enterTag(pane);
const tag_cursor = (try p.render(frame.allocator())).cursor orelse return error.MissingCursor;
try std.testing.expectEqual(if (p.settings.tag_bottom) rect.y + rect.h - BOX_H else rect.y, tag_cursor.y);
- Pardes.exitTagEdit(pane);
+ pane.focus = .body;
pane.body.mode = .tty;
try std.testing.expect((try p.render(frame.allocator())).cursor == null);
@@ -7193,7 +7069,7 @@ test "a prompt is typed on its own line in the notice band and leaves the tag ro
p.update(.{ .key = .{ .cp = '/' } });
p.update(.{ .key = .{ .cp = 't', .text = "tw" } });
p.update(.{ .key = .{ .cp = Key.left } });
- try std.testing.expect(pane.prompt == .search and !pane.tag_edit);
+ try std.testing.expect(pane.prompt == .search and pane.focus == .body);
try std.testing.expectEqualStrings(config.search_marker ++ "tw", pane.promptText().?);
_ = frame.reset(.retain_capacity);
_ = try p.render(frame.allocator());
@@ -7243,8 +7119,7 @@ test "a stillborn pane gives its column to the first pane that joins it" {
try std.testing.expect(p.executeBuiltinLine(shell, "Newcol"));
const tagged = p.active;
p.sync();
- p.seedTail(p.panes[tagged].?);
- try std.testing.expect(p.panes[tagged].?.appendTag(" make"));
+ try p.panes[tagged].?.appendTag(" make");
try std.testing.expect(p.executeBuiltinLine(tagged, "Tty"));
p.update(.tick);
try std.testing.expect(p.panes[tagged] != null);