summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2024-02-21 23:15:22 +0200
committerGravatar Uko Kokņevičs2024-02-21 23:15:22 +0200
commit2ea8a6e55bbbcd16081388787cce3476e6c882f2 (patch)
tree4634c4ab29305c26c639ef91f8351aa1cbecfb74 /src
parentAdd functionality for skipping paragraphs up and down (diff)
downloades-2ea8a6e55bbbcd16081388787cce3476e6c882f2.tar.gz
es-2ea8a6e55bbbcd16081388787cce3476e6c882f2.tar.xz
es-2ea8a6e55bbbcd16081388787cce3476e6c882f2.zip
Add functionality for skipping words left and right
Diffstat (limited to 'src')
-rw-r--r--src/Buffer.zig44
-rw-r--r--src/KeyMap.zig6
2 files changed, 50 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
113pub 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
113pub fn cleanWhiteSpace(self: *Buffer) !void { 133pub 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
327pub 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.
308pub fn goToLine(self: *Buffer, editor: *Editor) !void { 352pub 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| {
diff --git a/src/KeyMap.zig b/src/KeyMap.zig
index 90dcd01..8dca48d 100644
--- a/src/KeyMap.zig
+++ b/src/KeyMap.zig
@@ -113,7 +113,11 @@ pub fn defaultMap(allocator: Allocator) !KeyMap {
113 113
114 // M-<*> 114 // M-<*>
115 try map.bind(&.{Key.meta(Key.down)}, Buffer.forwardParagraph); 115 try map.bind(&.{Key.meta(Key.down)}, Buffer.forwardParagraph);
116 try map.bind(&.{Key.meta(Key.left)}, Buffer.backwardWord);
117 try map.bind(&.{Key.meta(Key.right)}, Buffer.forwardWord);
116 try map.bind(&.{Key.meta(Key.up)}, Buffer.backwardParagraph); 118 try map.bind(&.{Key.meta(Key.up)}, Buffer.backwardParagraph);
119 try map.bind(&.{Key.meta('b')}, Buffer.backwardWord);
120 try map.bind(&.{Key.meta('f')}, Buffer.forwardWord);
117 // M-g is taken 121 // M-g is taken
118 try map.bind(&.{Key.meta('n')}, Buffer.forwardParagraph); 122 try map.bind(&.{Key.meta('n')}, Buffer.forwardParagraph);
119 // M-O is taken 123 // M-O is taken
@@ -122,6 +126,8 @@ pub fn defaultMap(allocator: Allocator) !KeyMap {
122 126
123 // C-<*> 127 // C-<*>
124 try map.bind(&.{Key.ctrl(Key.down)}, Buffer.forwardParagraph); 128 try map.bind(&.{Key.ctrl(Key.down)}, Buffer.forwardParagraph);
129 try map.bind(&.{Key.ctrl(Key.left)}, Buffer.backwardWord);
130 try map.bind(&.{Key.ctrl(Key.right)}, Buffer.forwardWord);
125 try map.bind(&.{Key.ctrl(Key.up)}, Buffer.backwardParagraph); 131 try map.bind(&.{Key.ctrl(Key.up)}, Buffer.backwardParagraph);
126 try map.bind(&.{Key.ctrl('a')}, Buffer.moveBeginningOfLine); 132 try map.bind(&.{Key.ctrl('a')}, Buffer.moveBeginningOfLine);
127 try map.bind(&.{Key.ctrl('b')}, Buffer.backwardChar); 133 try map.bind(&.{Key.ctrl('b')}, Buffer.backwardChar);