diff options
| author | 2024-02-21 23:15:22 +0200 | |
|---|---|---|
| committer | 2024-02-21 23:15:22 +0200 | |
| commit | 2ea8a6e55bbbcd16081388787cce3476e6c882f2 (patch) | |
| tree | 4634c4ab29305c26c639ef91f8351aa1cbecfb74 /src/Buffer.zig | |
| parent | Add functionality for skipping paragraphs up and down (diff) | |
| download | es-2ea8a6e55bbbcd16081388787cce3476e6c882f2.tar.gz es-2ea8a6e55bbbcd16081388787cce3476e6c882f2.tar.xz es-2ea8a6e55bbbcd16081388787cce3476e6c882f2.zip | |
Add functionality for skipping words left and right
Diffstat (limited to 'src/Buffer.zig')
| -rw-r--r-- | src/Buffer.zig | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Buffer.zig b/src/Buffer.zig index bb6d3ce..26f9a93 100644 --- a/src/Buffer.zig +++ b/src/Buffer.zig | |||
| @@ -110,6 +110,26 @@ pub fn backwardParagraph(self: *Buffer) void { | |||
| 110 | self.cx = 0; | 110 | self.cx = 0; |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | pub fn backwardWord(self: *Buffer) void { | ||
| 114 | if (self.cx == 0) { | ||
| 115 | return self.backwardChar(); | ||
| 116 | } | ||
| 117 | |||
| 118 | const chars = self.rows.items[self.cy].data.items; | ||
| 119 | // First we skip non-word | ||
| 120 | while (self.cx > 0) : (self.cx -= 1) { | ||
| 121 | if (std.ascii.isAlphanumeric(chars[self.cx - 1])) { | ||
| 122 | break; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | // Then we skip word | ||
| 126 | while (self.cx > 0) : (self.cx -= 1) { | ||
| 127 | if (!std.ascii.isAlphanumeric(chars[self.cx - 1])) { | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 113 | pub fn cleanWhiteSpace(self: *Buffer) !void { | 133 | pub fn cleanWhiteSpace(self: *Buffer) !void { |
| 114 | for (self.rows.items) |*row| { | 134 | for (self.rows.items) |*row| { |
| 115 | try row.cleanWhiteSpace(self); | 135 | try row.cleanWhiteSpace(self); |
| @@ -304,6 +324,30 @@ pub fn forwardParagraph(self: *Buffer) void { | |||
| 304 | self.cx = 0; | 324 | self.cx = 0; |
| 305 | } | 325 | } |
| 306 | 326 | ||
| 327 | pub fn forwardWord(self: *Buffer) void { | ||
| 328 | if (self.cy == self.rows.items.len) { | ||
| 329 | return; | ||
| 330 | } | ||
| 331 | |||
| 332 | const chars = self.rows.items[self.cy].data.items; | ||
| 333 | if (self.cx == chars.len) { | ||
| 334 | return self.forwardChar(); | ||
| 335 | } | ||
| 336 | |||
| 337 | // First we skip non-word | ||
| 338 | while (self.cx < chars.len) : (self.cx += 1) { | ||
| 339 | if (std.ascii.isAlphanumeric(chars[self.cx])) { | ||
| 340 | break; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | // Then we skip word | ||
| 344 | while (self.cx < chars.len) : (self.cx += 1) { | ||
| 345 | if (!std.ascii.isAlphanumeric(chars[self.cx])) { | ||
| 346 | break; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 307 | // TODO: Make use of the callback feature to go to the final line while typing in. | 351 | // TODO: Make use of the callback feature to go to the final line while typing in. |
| 308 | pub fn goToLine(self: *Buffer, editor: *Editor) !void { | 352 | pub fn goToLine(self: *Buffer, editor: *Editor) !void { |
| 309 | if (try editor.prompt(self.allocator, "Goto line")) |line_str| { | 353 | if (try editor.prompt(self.allocator, "Goto line")) |line_str| { |