aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--text.c21
-rw-r--r--text.h3
-rw-r--r--vis.c11
3 files changed, 15 insertions, 20 deletions
diff --git a/text.c b/text.c
index 93e79d6..189cf2f 100644
--- a/text.c
+++ b/text.c
@@ -665,15 +665,10 @@ bool text_vprintf(Text *txt, size_t pos, const char *format, va_list ap) {
return ret;
}
-bool text_insert_newline(Text *txt, size_t pos) {
- switch (text_newline_type(txt)) {
- case TEXT_NEWLINE_NL:
- return text_insert(txt, pos, "\n", 1);
- case TEXT_NEWLINE_CRNL:
- return text_insert(txt, pos, "\r\n", 2);
- default:
- return false;
- }
+size_t text_insert_newline(Text *txt, size_t pos) {
+ const char *data = text_newline_char(txt);
+ size_t len = strlen(data);
+ return text_insert(txt, pos, data, len) ? len : 0;
}
static size_t action_undo(Text *txt, Action *a) {
@@ -1265,6 +1260,14 @@ enum TextNewLine text_newline_type(Text *txt){
return txt->newlines;
}
+const char *text_newline_char(Text *txt) {
+ static const char *types[] = {
+ [TEXT_NEWLINE_NL] = "\n",
+ [TEXT_NEWLINE_CRNL] = "\r\n",
+ };
+ return types[text_newline_type(txt)];
+}
+
static bool text_iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) {
*it = (Iterator){
.pos = pos,
diff --git a/text.h b/text.h
index 90da900..db71173 100644
--- a/text.h
+++ b/text.h
@@ -41,7 +41,7 @@ bool text_appendf(Text*, const char *format, ...);
bool text_printf(Text*, size_t pos, const char *format, ...);
bool text_vprintf(Text*, size_t pos, const char *format, va_list ap);
/* inserts a line ending character (depending on file type) */
-bool text_insert_newline(Text*, size_t pos);
+size_t text_insert_newline(Text*, size_t pos);
/* insert `len' bytes starting from `data' at `pos' which has to be
* in the interval [0, text_size(txt)] */
bool text_insert(Text*, size_t pos, const char *data, size_t len);
@@ -125,6 +125,7 @@ enum TextNewLine {
};
enum TextNewLine text_newline_type(Text*);
+const char *text_newline_char(Text*);
/* save the whole text to the given `filename'. Return true if succesful.
* In which case an implicit snapshot is taken. The save might associate a
diff --git a/vis.c b/vis.c
index 0d4fd9d..1707dab 100644
--- a/vis.c
+++ b/vis.c
@@ -1103,16 +1103,7 @@ static void copy_indent_from_previous_line(Win *win) {
}
void vis_insert_nl(Vis *vis) {
- const char *nl;
- switch (text_newline_type(vis->win->file->text)) {
- case TEXT_NEWLINE_CRNL:
- nl = "\r\n";
- break;
- default:
- nl = "\n";
- break;
- }
-
+ const char *nl = text_newline_char(vis->win->file->text);
vis_insert_key(vis, nl, strlen(nl));
if (vis->autoindent)