summaryrefslogtreecommitdiff
path: root/src/dged
diff options
context:
space:
mode:
Diffstat (limited to 'src/dged')
-rw-r--r--src/dged/buffer.c19
-rw-r--r--src/dged/buffer.h11
-rw-r--r--src/dged/buffer_view.c17
-rw-r--r--src/dged/buffer_view.h3
4 files changed, 36 insertions, 14 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c
index a512c60..749dba7 100644
--- a/src/dged/buffer.c
+++ b/src/dged/buffer.c
@@ -328,6 +328,11 @@ static struct match_result find_prev_in_line(struct buffer *buffer,
--bytei;
}
+ // first byte on line can also be a match
+ if (predicate(line.text[bytei])) {
+ found = true;
+ }
+
uint32_t target_col = text_byteindex_to_col(buffer->text, start.line, bytei);
return (struct match_result){
.at = (struct location){.line = start.line, .col = target_col},
@@ -595,6 +600,20 @@ struct location buffer_next_char(struct buffer *buffer, struct location dot) {
return dot;
}
+struct region buffer_word_at(struct buffer *buffer, struct location at) {
+ struct match_result prev_word_break =
+ find_prev_in_line(buffer, at, is_word_break);
+ struct match_result next_word_break =
+ find_next_in_line(buffer, at, is_word_break);
+
+ if (prev_word_break.at.col != next_word_break.at.col &&
+ prev_word_break.found) {
+ moveh(buffer, 1, &prev_word_break.at);
+ }
+
+ return region_new(prev_word_break.at, next_word_break.at);
+}
+
struct location buffer_next_word(struct buffer *buffer, struct location dot) {
struct match_result res = find_next_in_line(buffer, dot, is_word_break);
if (!res.found) {
diff --git a/src/dged/buffer.h b/src/dged/buffer.h
index 2e71fb3..3cb8d03 100644
--- a/src/dged/buffer.h
+++ b/src/dged/buffer.h
@@ -245,6 +245,17 @@ struct location buffer_next_word(struct buffer *buffer, struct location dot);
struct location buffer_next_line(struct buffer *buffer, struct location dot);
/**
+ * Get the extents of the word located at @ref at.
+ *
+ * @param [in] buffer The buffer to look in.
+ * @param [in] at The location to start from.
+ *
+ * @returns The extent of the closest word as a region. If
+ * there is no word, the region will be zero-sized.
+ */
+struct region buffer_word_at(struct buffer *buffer, struct location at);
+
+/**
* Clamp a buffer position to the boundaries of the buffer.
*
* Note that both @ref line and @ref col can be negative or bigger than the
diff --git a/src/dged/buffer_view.c b/src/dged/buffer_view.c
index aeee2f6..15aa812 100644
--- a/src/dged/buffer_view.c
+++ b/src/dged/buffer_view.c
@@ -186,24 +186,17 @@ void buffer_view_backward_delete_char(struct buffer_view *view) {
region_new(buffer_previous_char(view->buffer, view->dot), view->dot));
}
-void buffer_view_forward_delete_word(struct buffer_view *view) {
+void buffer_view_delete_word(struct buffer_view *view) {
if (maybe_delete_region(view)) {
return;
}
- view->dot = buffer_delete(
- view->buffer,
- region_new(view->dot, buffer_next_word(view->buffer, view->dot)));
-}
+ struct region word = buffer_word_at(view->buffer, view->dot);
-void buffer_view_backward_delete_word(struct buffer_view *view) {
- if (maybe_delete_region(view)) {
- return;
+ if (region_has_size(word)) {
+ buffer_delete(view->buffer, word);
+ view->dot = word.begin;
}
-
- view->dot = buffer_delete(
- view->buffer,
- region_new(buffer_previous_word(view->buffer, view->dot), view->dot));
}
void buffer_view_kill_line(struct buffer_view *view) {
diff --git a/src/dged/buffer_view.h b/src/dged/buffer_view.h
index 1c8fa6a..620c261 100644
--- a/src/dged/buffer_view.h
+++ b/src/dged/buffer_view.h
@@ -62,8 +62,7 @@ void buffer_view_backward_nlines(struct buffer_view *view, uint32_t nlines);
void buffer_view_forward_delete_char(struct buffer_view *view);
void buffer_view_backward_delete_char(struct buffer_view *view);
-void buffer_view_forward_delete_word(struct buffer_view *view);
-void buffer_view_backward_delete_word(struct buffer_view *view);
+void buffer_view_delete_word(struct buffer_view *view);
void buffer_view_kill_line(struct buffer_view *view);