tled: ^P in insert mode appends the default yank buffer - neatvi - [fork] simple vi-type editor with UTF-8 support
(HTM) git clone git://src.adamsgaard.dk/neatvi
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit f57f8c4f0b99d1c293036dd028859fa16e03f847
(DIR) parent 322e61144baa724aee78a2e257647336c65b8f77
(HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
Date: Fri, 22 May 2015 21:57:33 +0430
led: ^P in insert mode appends the default yank buffer
Diffstat:
M led.c | 22 +++++++++++++++++-----
M uc.c | 6 ++++++
M vi.c | 16 +++-------------
M vi.h | 1 +
4 files changed, 27 insertions(+), 18 deletions(-)
---
(DIR) diff --git a/led.c b/led.c
t@@ -1,3 +1,4 @@
+#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
t@@ -147,7 +148,7 @@ static void led_printparts(char *ai, char *pref, char *main, char *post, char *k
}
sbuf_str(ln, post);
led_print(sbuf_buf(ln), -1);
- pos = ren_cursor(sbuf_buf(ln), ren_pos(sbuf_buf(ln), off - 1));
+ pos = ren_cursor(sbuf_buf(ln), ren_pos(sbuf_buf(ln), MAX(0, off - 1)));
term_pos(-1, led_pos(sbuf_buf(ln), pos + idir));
sbuf_free(ln);
}
t@@ -156,14 +157,14 @@ static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, ch
{
struct sbuf *sb;
int ai_len = strlen(ai);
- int c;
+ int c, lnmode;
sb = sbuf_make();
if (!pref)
pref = "";
if (!post)
post = "";
while (1) {
- led_printparts(ai, pref, sbuf_buf(sb), post, *kmap);
+ led_printparts(ai, pref, uc_lastline(sbuf_buf(sb)), post, *kmap);
c = term_read(-1);
switch (c) {
case TK_CTL('f'):
t@@ -190,11 +191,16 @@ static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, ch
case TK_CTL('t'):
if (ai_len < ai_max)
ai[ai_len++] = '\t';
+ ai[ai_len] = '\0';
break;
case TK_CTL('d'):
if (ai_len > 0)
ai[--ai_len] = '\0';
break;
+ case TK_CTL('p'):
+ if (reg_get(0, &lnmode))
+ sbuf_str(sb, reg_get(0, &lnmode));
+ break;
default:
if (c == '\n' || TK_INT(c))
break;
t@@ -224,7 +230,7 @@ char *led_input(char *pref, char *post, char *ai, int ai_max, char **kmap)
{
struct sbuf *sb = sbuf_make();
char *first_ai = NULL;
- int key;
+ int key, i, ai_len;
while (1) {
char *ln = led_line(pref, post, ai, ai_max, &key, kmap);
if (pref)
t@@ -234,11 +240,17 @@ char *led_input(char *pref, char *post, char *ai, int ai_max, char **kmap)
sbuf_str(sb, ln);
if (key == '\n')
sbuf_chr(sb, '\n');
- led_printparts(ai, pref ? pref : "", ln, key == '\n' ? "" : post, *kmap);
+ led_printparts(ai, pref ? pref : "", uc_lastline(ln),
+ key == '\n' ? "" : post, *kmap);
if (key == '\n')
term_chr('\n');
pref = NULL;
term_kill();
+ ai_len = ai_max ? strlen(ai) : 0;
+ for (i = 0; isspace((unsigned char) ln[i]); i++)
+ if (ai_len < ai_max)
+ ai[ai_len++] = ln[i];
+ ai[ai_len] = '\0';
free(ln);
if (key != '\n')
break;
(DIR) diff --git a/uc.c b/uc.c
t@@ -80,6 +80,12 @@ int uc_wid(char *s)
return 1;
}
+char *uc_lastline(char *s)
+{
+ char *r = strrchr(s, '\n');
+ return r ? r + 1 : s;
+}
+
/* allocate and return an array for the characters in s */
char **uc_chop(char *s, int *n)
{
(DIR) diff --git a/vi.c b/vi.c
t@@ -680,17 +680,6 @@ static void vi_delete(int r1, int c1, int r2, int c2, int lnmode, int closed)
free(post);
}
-static int lastline(char *str)
-{
- char *s = str;
- char *r = s;
- while (s && s[0]) {
- r = s;
- s = strchr(s + 1, '\n');
- }
- return r - str;
-}
-
static int linecount(char *s)
{
int n;
t@@ -712,7 +701,7 @@ static int indentscopy(char *d, char *s, int len)
static char *vi_input(char *pref, char *post, int *row, int *col)
{
char ai[64] = "";
- char *rep;
+ char *rep, *s;
struct sbuf *sb;
int last, off;
if (xai)
t@@ -724,7 +713,8 @@ static char *vi_input(char *pref, char *post, int *row, int *col)
sbuf_str(sb, ai);
sbuf_str(sb, pref);
sbuf_str(sb, rep);
- last = lastline(sbuf_buf(sb));
+ s = sbuf_buf(sb);
+ last = uc_lastline(s) - s;
off = uc_slen(sbuf_buf(sb) + last);
if (last)
while (xai && (post[0] == ' ' || post[0] == '\t'))
(DIR) diff --git a/vi.h b/vi.h
t@@ -86,6 +86,7 @@ char *uc_next(char *s);
char *uc_beg(char *beg, char *s);
char *uc_end(char *beg, char *s);
char *uc_shape(char *beg, char *s);
+char *uc_lastline(char *s);
/* managing the terminal */
#define xrows (term_rows() - 1)