led: map cursor position to terminal column number with led_pos()
[neatvi.git] / led.c
blob9b4725d8cc0c8cae05e1ed9d15337006326b64de
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include "vi.h"
6 #include "kmap.h"
8 static char **led_kmap = kmap_def;
10 static char *keymap(char **kmap, int c)
12 static char cs[4];
13 cs[0] = c;
14 return kmap[c] ? kmap[c] : cs;
17 /* map cursor horizontal position to terminal column number */
18 int led_pos(char *s, int pos)
20 return dir_context(s) >= 0 ? pos : xcols - pos - 1;
23 char *led_keymap(int c)
25 return c >= 0 ? keymap(led_kmap, c) : NULL;
28 static char *led_render(char *s0)
30 int n, maxcol = 0;
31 int *pos; /* pos[i]: the screen position of the i-th character */
32 int *off; /* off[i]: the character at screen position i */
33 char **chrs; /* chrs[i]: the i-th character in s1 */
34 char *s1;
35 struct sbuf *out;
36 int i;
37 s1 = ren_translate(s0 ? s0 : "");
38 chrs = uc_chop(s1, &n);
39 pos = ren_position(s0);
40 off = malloc(xcols * sizeof(off[0]));
41 memset(off, 0xff, xcols * sizeof(off[0]));
42 for (i = 0; i < n; i++) {
43 int curpos = led_pos(s0, pos[i]);
44 if (curpos >= 0 && curpos < xcols) {
45 off[curpos] = i;
46 if (curpos > maxcol)
47 maxcol = curpos;
50 out = sbuf_make();
51 for (i = 0; i <= maxcol; i++) {
52 if (off[i] >= 0 && uc_isprint(chrs[off[i]]))
53 sbuf_mem(out, chrs[off[i]], uc_len(chrs[off[i]]));
54 else
55 sbuf_chr(out, ' ');
57 free(pos);
58 free(off);
59 free(chrs);
60 free(s1);
61 return sbuf_done(out);
64 void led_print(char *s, int row)
66 char *r = led_render(s);
67 term_pos(row, 0);
68 term_kill();
69 term_str(r);
70 free(r);
73 static int led_lastchar(char *s)
75 char *r = *s ? strchr(s, '\0') : s;
76 if (r != s)
77 r = uc_beg(s, r - 1);
78 return r - s;
81 static int led_lastword(char *s)
83 char *r = *s ? uc_beg(s, strchr(s, '\0') - 1) : s;
84 int kind;
85 while (r > s && uc_isspace(r))
86 r = uc_beg(s, r - 1);
87 kind = r > s ? uc_kind(r) : 0;
88 while (r > s && uc_kind(uc_beg(s, r - 1)) == kind)
89 r = uc_beg(s, r - 1);
90 return r - s;
93 /* the position of the cursor for inserting another character */
94 static int led_insertionpos(struct sbuf *sb)
96 int len = sbuf_len(sb);
97 char *chr = keymap(led_kmap, 'a');
98 int col;
99 sbuf_str(sb, chr);
100 col = ren_cursor(sbuf_buf(sb),
101 ren_pos(sbuf_buf(sb), uc_slen(sbuf_buf(sb)) - 1));
102 sbuf_cut(sb, len);
103 return col;
106 static void led_printparts(char *pref, char *main, char *post)
108 struct sbuf *ln;
109 int col;
110 ln = sbuf_make();
111 sbuf_str(ln, pref);
112 sbuf_str(ln, main);
113 col = led_insertionpos(ln);
114 sbuf_str(ln, post);
115 led_print(sbuf_buf(ln), -1);
116 term_pos(-1, led_pos(sbuf_buf(ln), col));
117 sbuf_free(ln);
120 static char *led_line(char *pref, char *post, int *key, char ***kmap)
122 struct sbuf *sb;
123 int c;
124 sb = sbuf_make();
125 if (!pref)
126 pref = "";
127 if (!post)
128 post = "";
129 while (1) {
130 led_printparts(pref, sbuf_buf(sb), post);
131 c = term_read(-1);
132 switch (c) {
133 case TERMCTRL('f'):
134 *kmap = kmap_farsi;
135 continue;
136 case TERMCTRL('e'):
137 *kmap = kmap_def;
138 continue;
139 case TERMCTRL('h'):
140 case 127:
141 if (sbuf_len(sb))
142 sbuf_cut(sb, led_lastchar(sbuf_buf(sb)));
143 break;
144 case TERMCTRL('u'):
145 sbuf_cut(sb, 0);
146 break;
147 case TERMCTRL('v'):
148 sbuf_chr(sb, term_read(-1));
149 break;
150 case TERMCTRL('w'):
151 if (sbuf_len(sb))
152 sbuf_cut(sb, led_lastword(sbuf_buf(sb)));
153 break;
154 default:
155 if (c == '\n' || c == TERMESC || c < 0)
156 break;
157 sbuf_str(sb, keymap(*kmap, c));
159 if (c == '\n' || c == TERMESC || c < 0)
160 break;
162 *key = c;
163 return sbuf_done(sb);
166 /* read an ex command */
167 char *led_prompt(char *pref, char *post)
169 char **kmap = kmap_def;
170 char *s;
171 int key;
172 s = led_line(pref, post, &key, &kmap);
173 if (key == '\n')
174 return s;
175 free(s);
176 return NULL;
179 /* read visual command input */
180 char *led_input(char *pref, char *post, int *row, int *col)
182 struct sbuf *sb = sbuf_make();
183 int key;
184 *row = 0;
185 while (1) {
186 char *ln = led_line(pref, post, &key, &led_kmap);
187 if (pref)
188 sbuf_str(sb, pref);
189 sbuf_str(sb, ln);
190 if (key == '\n')
191 sbuf_chr(sb, '\n');
192 *col = ren_last(pref ? sbuf_buf(sb) : ln);
193 led_printparts(pref ? pref : "", ln, key == '\n' ? "" : post);
194 if (key == '\n')
195 term_chr('\n');
196 pref = NULL;
197 term_kill();
198 free(ln);
199 if (key != '\n')
200 break;
201 (*row)++;
203 sbuf_str(sb, post);
204 if (key == TERMESC)
205 return sbuf_done(sb);
206 sbuf_free(sb);
207 return NULL;