4 * Copyright (C) 2015 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
15 char xpath
[PATHLEN
]; /* current file */
16 char xpath_alt
[PATHLEN
]; /* alternate file */
17 char xmsg
[512]; /* current message */
18 struct lbuf
*xb
; /* current buffer */
19 int xrow
, xcol
, xtop
; /* current row, column, and top row */
20 int xrow_alt
; /* alternate row, column, and top row */
21 int xled
= 1; /* use the line editor */
22 int xdir
= 'L'; /* current direction context */
23 int xvis
; /* visual mode */
26 static char vi_findlast
[256]; /* the last searched keyword */
27 static int vi_finddir
; /* the last search direction */
28 static char vi_charlast
[8]; /* the last character searched via f, t, F, or T */
29 static int vi_charcmd
; /* the character finding command */
30 static int vi_arg1
, vi_arg2
; /* the first and second arguments */
31 static int vi_ybuf
; /* current yank buffer */
33 static void vi_drawmsg(void)
35 led_print(xmsg
, xrows
);
39 static void vi_draw(void)
43 for (i
= xtop
; i
< xtop
+ xrows
; i
++) {
44 char *s
= lbuf_get(xb
, i
);
45 led_print(s
? s
: "~", i
- xtop
);
48 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
52 static int vi_buf
[128];
55 static int vi_read(void)
57 return vi_buflen
? vi_buf
[--vi_buflen
] : term_read(1000);
60 static void vi_back(int c
)
62 if (vi_buflen
< sizeof(vi_buf
))
63 vi_buf
[vi_buflen
++] = c
;
66 static char *vi_char(void)
69 return TK_INT(key
) ? NULL
: led_keymap(key
);
72 static char *vi_prompt(char *msg
)
74 term_pos(xrows
, led_pos(msg
, 0));
76 return led_prompt(msg
, "");
79 char *ex_read(char *msg
)
84 char *s
= led_prompt(msg
, "");
90 while ((c
= getchar()) != EOF
&& c
!= '\n')
99 void ex_show(char *msg
)
102 snprintf(xmsg
, sizeof(xmsg
), "%s", msg
);
111 static int vi_yankbuf(void)
120 static int vi_prefix(void)
124 if ((c
>= '1' && c
<= '9')) {
126 n
= n
* 10 + c
- '0';
134 static int lbuf_lnnext(struct lbuf
*lb
, int *r
, int *c
, int dir
)
136 char *ln
= lbuf_get(lb
, *r
);
137 int col
= ln
? ren_next(ln
, *c
, dir
) : -1;
144 static void lbuf_eol(struct lbuf
*lb
, int *r
, int *c
, int dir
)
146 char *ln
= lbuf_get(lb
, *r
);
147 *c
= dir
< 0 ? 0 : MAX(0, ren_wid(ln
? ln
: "") - 1);
150 static int lbuf_next(struct lbuf
*lb
, int *r
, int *c
, int dir
)
152 if (dir
< 0 && *r
>= lbuf_len(lb
))
153 *r
= MAX(0, lbuf_len(lb
) - 1);
154 if (lbuf_lnnext(lb
, r
, c
, dir
)) {
155 if (!lbuf_get(lb
, *r
+ dir
))
158 lbuf_eol(lb
, r
, c
, -dir
);
164 /* return a pointer to the character at visual position c of line r */
165 static char *lbuf_chr(struct lbuf
*lb
, int r
, int c
)
167 char *ln
= lbuf_get(lb
, r
);
168 return ln
? uc_chr(ln
, ren_off(ln
, c
)) : "";
171 static void lbuf_postindents(struct lbuf
*lb
, int *r
, int *c
)
173 lbuf_eol(lb
, r
, c
, -1);
174 while (uc_isspace(lbuf_chr(lb
, *r
, *c
)))
175 if (lbuf_lnnext(lb
, r
, c
, +1))
179 static int lbuf_findchar(struct lbuf
*lb
, int *row
, int *col
, char *cs
, int cmd
, int n
)
181 int dir
= (cmd
== 'f' || cmd
== 't') ? +1 : -1;
187 strcpy(vi_charlast
, cs
);
189 while (n
> 0 && !lbuf_lnnext(lb
, row
, &c
, dir
))
190 if (uc_code(lbuf_chr(lb
, *row
, c
)) == uc_code(cs
))
194 if (!n
&& (cmd
== 't' || cmd
== 'T'))
195 lbuf_lnnext(lb
, row
, col
, -dir
);
199 static int lbuf_search(struct lbuf
*lb
, char *kw
, int dir
, int *r
, int *c
, int *len
)
203 int row
= *r
, col
= *c
;
205 struct rset
*re
= rset_make(1, &kw
, 0);
208 for (i
= row
; !found
&& i
>= 0 && i
< lbuf_len(lb
); i
+= dir
) {
209 char *s
= lbuf_get(lb
, i
);
210 int off
= dir
> 0 && row
== i
? uc_chr(s
, col
+ 1) - s
: 0;
211 int flg
= off
? RE_NOTBOL
: 0;
212 while (rset_find(re
, s
+ off
, 1, offs
, flg
) >= 0) {
213 if (dir
< 0 && row
== i
&& off
+ offs
[0] >= col
)
216 *c
= uc_off(s
, off
+ offs
[0]);
218 *len
= offs
[1] - offs
[0];
228 static int vi_search(int cmd
, int cnt
, int *row
, int *col
)
236 if (cmd
== '/' || cmd
== '?') {
237 char sign
[4] = {cmd
};
238 char *kw
= vi_prompt(sign
);
241 vi_finddir
= cmd
== '/' ? +1 : -1;
243 snprintf(vi_findlast
, sizeof(vi_findlast
), "%s", kw
);
244 if (strchr(vi_findlast
, cmd
)) {
245 off
= strchr(vi_findlast
, cmd
) + 1;
246 *strchr(vi_findlast
, cmd
) = '\0';
250 dir
= cmd
== 'N' ? -vi_finddir
: vi_finddir
;
251 if (!vi_findlast
[0] || !lbuf_len(xb
))
253 c
= ren_off(lbuf_get(xb
, *row
), *col
);
254 for (i
= 0; i
< cnt
; i
++) {
255 if (lbuf_search(xb
, vi_findlast
, dir
, &r
, &c
, &len
)) {
259 if (i
+ 1 < cnt
&& cmd
== '/')
264 *col
= ren_pos(lbuf_get(xb
, r
), c
);
265 while (off
[0] && isspace((unsigned char) off
[0]))
269 if (*row
+ atoi(off
) < 0 || *row
+ atoi(off
) >= lbuf_len(xb
))
276 snprintf(xmsg
, sizeof(xmsg
), "\"%s\" not found\n", vi_findlast
);
280 /* move to the last character of the word */
281 static int lbuf_wordlast(struct lbuf
*lb
, int *row
, int *col
, int kind
, int dir
)
283 if (!kind
|| !(uc_kind(lbuf_chr(lb
, *row
, *col
)) & kind
))
285 while (uc_kind(lbuf_chr(lb
, *row
, *col
)) & kind
)
286 if (lbuf_next(lb
, row
, col
, dir
))
288 if (!(uc_kind(lbuf_chr(lb
, *row
, *col
)) & kind
))
289 lbuf_next(lb
, row
, col
, -dir
);
293 static int lbuf_wordbeg(struct lbuf
*lb
, int *row
, int *col
, int big
, int dir
)
296 lbuf_wordlast(lb
, row
, col
, big
? 3 : uc_kind(lbuf_chr(lb
, *row
, *col
)), dir
);
297 if (lbuf_next(lb
, row
, col
, dir
))
299 while (uc_isspace(lbuf_chr(lb
, *row
, *col
))) {
300 nl
= uc_code(lbuf_chr(lb
, *row
, *col
)) == '\n' ? nl
+ 1 : 0;
303 if (lbuf_next(lb
, row
, col
, dir
))
309 static int lbuf_wordend(struct lbuf
*lb
, int *row
, int *col
, int big
, int dir
)
311 int nl
= uc_code(lbuf_chr(lb
, *row
, *col
)) == '\n' ? -1 : 0;
312 if (!uc_isspace(lbuf_chr(lb
, *row
, *col
)))
313 if (lbuf_next(lb
, row
, col
, dir
))
315 while (uc_isspace(lbuf_chr(lb
, *row
, *col
))) {
316 nl
= uc_code(lbuf_chr(lb
, *row
, *col
)) == '\n' ? nl
+ 1 : 0;
319 lbuf_next(lb
, row
, col
, -dir
);
322 if (lbuf_next(lb
, row
, col
, dir
))
325 if (lbuf_wordlast(lb
, row
, col
, big
? 3 : uc_kind(lbuf_chr(lb
, *row
, *col
)), dir
))
330 static int lbuf_paragraphbeg(struct lbuf
*lb
, int *row
, int *col
, int dir
)
332 while (*row
>= 0 && *row
< lbuf_len(lb
) && !strcmp("\n", lbuf_get(lb
, *row
)))
334 while (*row
>= 0 && *row
< lbuf_len(lb
) && strcmp("\n", lbuf_get(lb
, *row
)))
336 *row
= MAX(0, MIN(*row
, lbuf_len(lb
) - 1));
337 lbuf_eol(lb
, row
, col
, -1);
341 static int lbuf_sectionbeg(struct lbuf
*lb
, int *row
, int *col
, int dir
)
344 while (*row
>= 0 && *row
< lbuf_len(lb
) && lbuf_get(lb
, *row
)[0] != '{')
346 *row
= MAX(0, MIN(*row
, lbuf_len(lb
) - 1));
347 lbuf_eol(lb
, row
, col
, -1);
351 /* read a line motion */
352 static int vi_motionln(int *row
, int cmd
)
354 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
360 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
363 *row
= MAX(*row
- cnt
, 0);
366 *row
= MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1);
369 if ((mark
= vi_read()) > 0 && (isalpha(mark
) || mark
== '\''))
370 if (lbuf_markpos(xb
, mark
) >= 0)
371 *row
= lbuf_markpos(xb
, mark
);
374 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
377 *row
= MAX(*row
- cnt
, 0);
380 *row
= (vi_arg1
|| vi_arg2
) ? cnt
- 1 : lbuf_len(xb
) - 1;
384 *row
= MIN(xtop
+ cnt
- 1, lbuf_len(xb
) - 1);
390 *row
= MIN(xtop
+ xrows
- 1 - cnt
+ 1, lbuf_len(xb
) - 1);
396 *row
= MIN(xtop
+ xrows
/ 2, lbuf_len(xb
) - 1);
402 *row
= MAX(0, MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1));
412 static int vi_motion(int *row
, int *col
)
414 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
415 char *ln
= lbuf_get(xb
, *row
);
416 int dir
= dir_context(ln
? ln
: "");
420 if ((mv
= vi_motionln(row
, 0))) {
427 for (i
= 0; i
< cnt
; i
++)
428 if (lbuf_lnnext(xb
, row
, col
, 1))
432 if ((cs
= vi_char()))
433 if (lbuf_findchar(xb
, row
, col
, cs
, mv
, cnt
))
437 if ((cs
= vi_char()))
438 if (lbuf_findchar(xb
, row
, col
, cs
, mv
, cnt
))
443 if (lbuf_findchar(xb
, row
, col
, vi_charlast
, vi_charcmd
, cnt
))
448 if (lbuf_findchar(xb
, row
, col
, vi_charlast
, vi_charcmd
, -cnt
))
452 for (i
= 0; i
< cnt
; i
++)
453 if (lbuf_lnnext(xb
, row
, col
, -1 * dir
))
457 for (i
= 0; i
< cnt
; i
++)
458 if (lbuf_lnnext(xb
, row
, col
, +1 * dir
))
462 if ((cs
= vi_char()))
463 if (lbuf_findchar(xb
, row
, col
, cs
, mv
, cnt
))
467 if ((cs
= vi_char()))
468 if (lbuf_findchar(xb
, row
, col
, cs
, mv
, cnt
))
472 for (i
= 0; i
< cnt
; i
++)
473 if (lbuf_wordend(xb
, row
, col
, 1, -1))
477 for (i
= 0; i
< cnt
; i
++)
478 if (lbuf_wordend(xb
, row
, col
, 1, +1))
482 for (i
= 0; i
< cnt
; i
++)
483 if (lbuf_wordbeg(xb
, row
, col
, 1, +1))
487 for (i
= 0; i
< cnt
; i
++)
488 if (lbuf_wordend(xb
, row
, col
, 0, -1))
492 for (i
= 0; i
< cnt
; i
++)
493 if (lbuf_wordend(xb
, row
, col
, 0, +1))
497 for (i
= 0; i
< cnt
; i
++)
498 if (lbuf_wordbeg(xb
, row
, col
, 0, +1))
502 for (i
= 0; i
< cnt
; i
++)
503 if (lbuf_paragraphbeg(xb
, row
, col
, -1))
507 for (i
= 0; i
< cnt
; i
++)
508 if (lbuf_paragraphbeg(xb
, row
, col
, +1))
512 if (vi_read() != '[')
514 for (i
= 0; i
< cnt
; i
++)
515 if (lbuf_sectionbeg(xb
, row
, col
, -1))
519 if (vi_read() != ']')
521 for (i
= 0; i
< cnt
; i
++)
522 if (lbuf_sectionbeg(xb
, row
, col
, +1))
526 lbuf_eol(xb
, row
, col
, -1);
529 lbuf_postindents(xb
, row
, col
);
532 lbuf_eol(xb
, row
, col
, +1);
533 lbuf_lnnext(xb
, row
, col
, -1);
539 if (vi_search(mv
, cnt
, row
, col
))
543 if (vi_search(mv
, cnt
, row
, col
))
547 if (vi_search(mv
, cnt
, row
, col
))
551 if (vi_search(mv
, cnt
, row
, col
))
556 for (i
= 0; i
< cnt
; i
++)
557 if (lbuf_lnnext(xb
, row
, col
, -1))
567 static void swap(int *a
, int *b
)
574 static char *lbuf_region(struct lbuf
*lb
, int r1
, int l1
, int r2
, int l2
)
579 return uc_sub(lbuf_get(lb
, r1
), l1
, l2
);
581 s1
= uc_sub(lbuf_get(lb
, r1
), l1
, -1);
582 s3
= uc_sub(lbuf_get(lb
, r2
), 0, l2
);
583 s2
= lbuf_cp(lb
, r1
+ 1, r2
);
590 return sbuf_done(sb
);
593 /* insertion offset before or after the given visual position */
594 static int vi_insertionoffset(char *s
, int c1
, int before
)
600 return before
|| s
[l
] == '\n' ? l
: l
+ 1;
603 static void vi_commandregion(int *r1
, int *r2
, int *c1
, int *c2
, int *l1
, int *l2
, int closed
)
605 if (*r2
< *r1
|| (*r2
== *r1
&& *c2
< *c1
)) {
609 *l1
= vi_insertionoffset(lbuf_get(xb
, *r1
), *c1
, 1);
610 *l2
= vi_insertionoffset(lbuf_get(xb
, *r2
), *c2
, !closed
);
611 if (*r1
== *r2
&& lbuf_get(xb
, *r1
))
612 ren_region(lbuf_get(xb
, *r1
), *c1
, *c2
, l1
, l2
, closed
);
613 if (*r1
== *r2
&& *l2
< *l1
)
617 static void vi_yank(int r1
, int c1
, int r2
, int c2
, int lnmode
, int closed
)
621 vi_commandregion(&r1
, &r2
, &c1
, &c2
, &l1
, &l2
, closed
);
622 region
= lbuf_region(xb
, r1
, lnmode
? 0 : l1
, r2
, lnmode
? -1 : l2
);
623 reg_put(vi_ybuf
, region
, lnmode
);
626 xcol
= lnmode
? xcol
: c1
;
629 static void vi_delete(int r1
, int c1
, int r2
, int c2
, int lnmode
, int closed
)
634 vi_commandregion(&r1
, &r2
, &c1
, &c2
, &l1
, &l2
, closed
);
635 region
= lbuf_region(xb
, r1
, lnmode
? 0 : l1
, r2
, lnmode
? -1 : l2
);
636 reg_put(vi_ybuf
, region
, lnmode
);
638 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, l1
);
639 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), l2
, -1);
640 lbuf_rm(xb
, r1
, r2
+ 1);
642 struct sbuf
*sb
= sbuf_make();
645 lbuf_put(xb
, r1
, sbuf_buf(sb
));
651 lbuf_postindents(xb
, &xrow
, &xcol
);
656 static int lastline(char *str
)
662 s
= strchr(s
== str
? s
: s
+ 1, '\n');
667 static int linecount(char *s
)
671 if ((s
= strchr(s
, '\n')))
676 static int indentscopy(char *d
, char *s
, int len
)
679 for (i
= 0; i
< len
- 1 && (s
[i
] == ' ' || s
[i
] == '\t'); i
++)
685 static char *vi_input(char *pref
, char *post
, int *row
, int *col
)
692 pref
+= indentscopy(ai
, pref
, sizeof(ai
));
693 rep
= led_input(pref
, post
, ai
, xautoindent
? sizeof(ai
) - 1 : 0);
700 last
= lastline(sbuf_buf(sb
));
701 off
= uc_slen(sbuf_buf(sb
) + last
);
703 while (xautoindent
&& (post
[0] == ' ' || post
[0] == '\t'))
706 *row
= linecount(sbuf_buf(sb
)) - 1;
707 *col
= ren_pos(sbuf_buf(sb
) + last
, MAX(0, off
- 1));
709 return sbuf_done(sb
);
712 static char *vi_indents(char *ln
)
714 struct sbuf
*sb
= sbuf_make();
715 while (xautoindent
&& ln
&& (*ln
== ' ' || *ln
== '\t'))
717 return sbuf_done(sb
);
720 static void vi_change(int r1
, int c1
, int r2
, int c2
, int lnmode
, int closed
)
727 vi_commandregion(&r1
, &r2
, &c1
, &c2
, &l1
, &l2
, closed
);
728 region
= lbuf_region(xb
, r1
, lnmode
? 0 : l1
, r2
, lnmode
? -1 : l2
);
729 reg_put(vi_ybuf
, region
, lnmode
);
731 pref
= lnmode
? vi_indents(lbuf_get(xb
, r1
)) : uc_sub(lbuf_get(xb
, r1
), 0, l1
);
732 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), l2
, -1);
733 rep
= vi_input(pref
, post
, &row
, &col
);
735 lbuf_rm(xb
, r1
, r2
+ 1);
736 lbuf_put(xb
, r1
, rep
);
745 static void vi_pipe(int r1
, int r2
)
749 char *cmd
= vi_prompt("!");
754 text
= lbuf_cp(xb
, r1
, r2
+ 1);
755 rep
= cmd_pipe(cmd
, text
);
757 lbuf_rm(xb
, r1
, r2
+ 1);
758 lbuf_put(xb
, r1
, rep
);
765 static void vi_shift(int r1
, int r2
, int dir
)
772 for (i
= r1
; i
<= r2
; i
++) {
773 if (!(ln
= lbuf_get(xb
, i
)))
779 ln
= ln
[0] == ' ' || ln
[0] == '\t' ? ln
+ 1 : ln
;
781 lbuf_rm(xb
, i
, i
+ 1);
782 lbuf_put(xb
, i
, sbuf_buf(sb
));
786 lbuf_postindents(xb
, &xrow
, &xcol
);
789 static int vc_motion(int cmd
)
791 int r1
= xrow
, r2
= xrow
; /* region rows */
792 int c1
= xcol
, c2
= xcol
; /* visual region columns */
793 int lnmode
= 0; /* line-based region */
794 int closed
= 1; /* include the last character */
796 vi_arg2
= vi_prefix();
799 if ((mv
= vi_motionln(&r2
, cmd
))) {
801 } else if (!(mv
= vi_motion(&r2
, &c2
))) {
807 if (!strchr("fFtTeE$", mv
))
811 lbuf_eol(xb
, &r1
, &c1
, -1);
812 lbuf_eol(xb
, &r2
, &c2
, +1);
815 vi_yank(r1
, c1
, r2
, c2
, lnmode
, closed
);
817 vi_delete(r1
, c1
, r2
, c2
, lnmode
, closed
);
819 vi_change(r1
, c1
, r2
, c2
, lnmode
, closed
);
822 if (cmd
== '>' || cmd
== '<')
823 vi_shift(r1
, r2
, cmd
== '>' ? +1 : -1);
827 static int vc_insert(int cmd
)
830 char *ln
= lbuf_get(xb
, xrow
);
831 int row
, col
, off
= 0;
834 lbuf_postindents(xb
, &xrow
, &xcol
);
836 lbuf_eol(xb
, &xrow
, &xcol
, +1);
837 lbuf_lnnext(xb
, &xrow
, &xcol
, -1);
841 if (cmd
== 'i' || cmd
== 'I')
842 off
= vi_insertionoffset(ln
, xcol
, 1);
843 if (cmd
== 'a' || cmd
== 'A')
844 off
= vi_insertionoffset(ln
, xcol
, 0);
845 pref
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, 0, off
) : vi_indents(ln
);
846 post
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, off
, -1) : uc_dup("\n");
847 rep
= vi_input(pref
, post
, &row
, &col
);
848 if ((cmd
== 'o' || cmd
== 'O') && !lbuf_len(xb
))
849 lbuf_put(xb
, 0, "\n");
851 if (cmd
!= 'o' && cmd
!= 'O')
852 lbuf_rm(xb
, xrow
, xrow
+ 1);
853 lbuf_put(xb
, xrow
, rep
);
863 static int vc_put(int cmd
)
865 int cnt
= MAX(1, vi_arg1
);
868 char *buf
= reg_get(vi_ybuf
, &lnmode
);
874 ln
= lnmode
? NULL
: lbuf_get(xb
, xrow
);
875 off
= vi_insertionoffset(ln
, xcol
, cmd
== 'P');
876 if (cmd
== 'p' && !ln
)
880 char *s
= uc_sub(ln
, 0, off
);
884 for (i
= 0; i
< cnt
; i
++)
887 char *s
= uc_sub(ln
, off
, -1);
891 if (!ln
&& !lbuf_len(xb
))
892 lbuf_put(xb
, 0, "\n");
894 lbuf_rm(xb
, xrow
, xrow
+ 1);
895 lbuf_put(xb
, xrow
, sbuf_buf(sb
));
897 xcol
= ren_pos(lbuf_get(xb
, xrow
), off
+ uc_slen(buf
) * cnt
- 1);
899 lbuf_postindents(xb
, &xrow
, &xcol
);
904 static int join_spaces(char *prev
, char *next
)
906 int prevlen
= strlen(prev
);
909 if (prev
[prevlen
- 1] == ' ' || next
[0] == ')')
911 return prev
[prevlen
- 1] == '.' ? 2 : 1;
914 static int vc_join(void)
917 int cnt
= vi_arg1
<= 1 ? 2 : vi_arg1
;
919 int end
= xrow
+ cnt
;
922 if (!lbuf_get(xb
, beg
) || !lbuf_get(xb
, end
- 1))
925 for (i
= beg
; i
< end
; i
++) {
926 char *ln
= lbuf_get(xb
, i
);
927 char *lnend
= strchr(ln
, '\n');
930 while (ln
[0] == ' ' || ln
[0] == '\t')
932 spaces
= i
> beg
? join_spaces(sbuf_buf(sb
), ln
) : 0;
933 off
= uc_slen(sbuf_buf(sb
));
936 sbuf_mem(sb
, ln
, lnend
- ln
);
939 lbuf_rm(xb
, beg
, end
);
940 lbuf_put(xb
, beg
, sbuf_buf(sb
));
941 xcol
= ren_pos(sbuf_buf(sb
), off
);
946 static int vi_scrollforeward(int cnt
)
948 if (xtop
>= lbuf_len(xb
) - 1)
950 xtop
= MIN(lbuf_len(xb
) - 1, xtop
+ cnt
);
951 xrow
= MAX(xrow
, xtop
);
955 static int vi_scrollbackward(int cnt
)
959 xtop
= MAX(0, xtop
- cnt
);
960 xrow
= MIN(xrow
, xtop
+ xrows
- 1);
964 static void vc_status(void)
966 snprintf(xmsg
, sizeof(xmsg
), "\"%s\" line %d of %d, col %d\n",
967 xpath
[0] ? xpath
: "unnamed", xrow
+ 1, lbuf_len(xb
), xcol
+ 1);
970 static int vc_replace(void)
972 int cnt
= MAX(1, vi_arg1
);
973 char *cs
= vi_char();
974 char *ln
= lbuf_get(xb
, xrow
);
981 off
= ren_off(ln
, xcol
);
983 for (i
= 0; s
[0] != '\n' && i
< cnt
; i
++)
987 pref
= uc_sub(ln
, 0, off
);
988 post
= uc_sub(ln
, off
+ cnt
, -1);
991 for (i
= 0; i
< cnt
; i
++)
994 lbuf_rm(xb
, xrow
, xrow
+ 1);
995 lbuf_put(xb
, xrow
, sbuf_buf(sb
));
997 xcol
= ren_pos(sbuf_buf(sb
), off
);
1004 static void vi(void)
1011 lbuf_eol(xb
, &xrow
, &xcol
, -1);
1013 term_pos(xrow
, led_pos(lbuf_get(xb
, xrow
), xcol
));
1020 vi_ybuf
= vi_yankbuf();
1021 vi_arg1
= vi_prefix();
1023 vi_ybuf
= vi_yankbuf();
1024 mv
= vi_motion(&xrow
, &xcol
);
1026 if (strchr("\'GHML/?", mv
))
1027 lbuf_mark(xb
, '\'', orow
);
1029 if (strchr("jk", mv
))
1032 lbuf_postindents(xb
, &xrow
, &xcol
);
1034 } else if (mv
== 0) {
1041 if (vi_scrollbackward(MAX(1, vi_arg1
) * (xrows
- 1)))
1043 lbuf_postindents(xb
, &xrow
, &xcol
);
1047 if (vi_scrollforeward(MAX(1, vi_arg1
) * (xrows
- 1)))
1049 lbuf_postindents(xb
, &xrow
, &xcol
);
1053 if (vi_scrollforeward(MAX(1, vi_arg1
)))
1058 if (vi_scrollbackward(MAX(1, vi_arg1
)))
1078 ln
= vi_prompt(":");
1110 if ((mark
= vi_read()) > 0 && isalpha(mark
))
1111 lbuf_mark(xb
, mark
, xrow
);
1122 xtop
= vi_arg1
? vi_arg1
: xrow
;
1125 n
= vi_arg1
? vi_arg1
: xrow
;
1126 xtop
= MAX(0, n
- xrows
/ 2);
1129 n
= vi_arg1
? vi_arg1
: xrow
;
1130 xtop
= MAX(0, n
- xrows
+ 1);
1143 if (!vc_motion('d'))
1147 vi_back(TK_CTL('h'));
1148 if (!vc_motion('d'))
1153 if (!vc_motion('c'))
1158 if (!vc_motion('d'))
1167 if (!vc_motion('c'))
1172 if (!vc_motion('c'))
1177 if (!vc_motion('y'))
1184 if (xrow
< 0 || xrow
>= lbuf_len(xb
))
1185 xrow
= lbuf_len(xb
) ? lbuf_len(xb
) - 1 : 0;
1186 if (xrow
< xtop
|| xrow
>= xtop
+ xrows
) {
1187 xtop
= xrow
< xtop
? xrow
: MAX(0, xrow
- xrows
+ 1);
1194 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
),
1195 ren_cursor(lbuf_get(xb
, xrow
), xcol
)));
1203 int main(int argc
, char *argv
[])
1209 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1210 if (argv
[i
][1] == 's')
1212 if (argv
[i
][1] == 'e')
1214 if (argv
[i
][1] == 'v')
1219 snprintf(ecmd
, PATHLEN
, "e %s", argv
[i
]);