4 * Copyright (C) 2015-2016 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
15 static char vi_msg
[EXLEN
]; /* current message */
16 static char vi_charlast
[8]; /* the last character searched via f, t, F, or T */
17 static int vi_charcmd
; /* the character finding command */
18 static int vi_arg1
, vi_arg2
; /* the first and second arguments */
19 static int vi_ybuf
; /* current yank buffer */
20 static int vi_pcol
; /* the column requested by | command */
21 static int vi_printed
; /* ex_print() calls since the last command */
22 static int vi_scroll
; /* scroll amount for ^f and ^d*/
23 static int vi_soset
, vi_so
; /* search offset; 1 in "/kw/1" */
25 static void vi_wait(void)
28 free(ex_read("[enter to continue]"));
34 static void vi_drawmsg(void)
38 led_print(vi_msg
, xrows
);
43 static void vi_drawrow(int row
)
45 char *s
= lbuf_get(xb
, row
);
46 led_print(s
? s
: (row
? "~" : ""), row
- xtop
);
49 /* redraw the screen */
50 static void vi_drawagain(int xcol
, int lineonly
)
54 for (i
= xtop
; i
< xtop
+ xrows
; i
++)
55 if (!lineonly
|| i
== xrow
)
58 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
62 /* update the screen */
63 static void vi_drawupdate(int xcol
, int otop
)
69 term_room(otop
- xtop
);
71 int n
= MIN(xtop
- otop
, xrows
);
72 for (i
= 0; i
< n
; i
++)
73 vi_drawrow(xtop
+ xrows
- n
+ i
);
75 int n
= MIN(otop
- xtop
, xrows
);
76 for (i
= 0; i
< n
; i
++)
79 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
83 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
86 /* update the screen by removing lines r1 to r2 before an input command */
87 static void vi_drawrm(int r1
, int r2
, int newln
)
89 r1
= MIN(MAX(r1
, xtop
), xtop
+ xrows
);
90 r2
= MIN(MAX(r2
, xtop
), xtop
+ xrows
);
91 term_pos(r1
- xtop
, 0);
92 term_room(r1
- r2
+ newln
);
95 static int vi_buf
[128];
98 static int vi_read(void)
100 return vi_buflen
? vi_buf
[--vi_buflen
] : term_read();
103 static void vi_back(int c
)
105 if (vi_buflen
< sizeof(vi_buf
))
106 vi_buf
[vi_buflen
++] = c
;
109 static char *vi_char(void)
111 return led_read(ex_kmap());
114 static char *vi_prompt(char *msg
, char **kmap
)
117 term_pos(xrows
, led_pos(msg
, 0));
119 s
= led_prompt(msg
, "", kmap
);
122 r
= uc_dup(strlen(s
) >= strlen(msg
) ? s
+ strlen(msg
) : s
);
127 /* read an ex input line */
128 char *ex_read(char *msg
)
133 char *s
= led_prompt(msg
, "", ex_kmap());
139 while ((c
= getchar()) != EOF
&& c
!= '\n')
145 return sbuf_done(sb
);
148 /* show an ex message */
149 void ex_show(char *msg
)
152 snprintf(vi_msg
, sizeof(vi_msg
), "%s", msg
);
161 /* print an ex output line */
162 void ex_print(char *line
)
165 vi_printed
+= line
? 1 : 2;
167 snprintf(vi_msg
, sizeof(vi_msg
), "%s", line
);
177 static int vi_yankbuf(void)
186 static int vi_prefix(void)
190 if ((c
>= '1' && c
<= '9')) {
192 n
= n
* 10 + c
- '0';
200 static int vi_col2off(struct lbuf
*lb
, int row
, int col
)
202 char *ln
= lbuf_get(lb
, row
);
203 return ln
? ren_off(ln
, col
) : 0;
206 static int vi_off2col(struct lbuf
*lb
, int row
, int off
)
208 char *ln
= lbuf_get(lb
, row
);
209 return ln
? ren_pos(ln
, off
) : 0;
212 static int vi_nextoff(struct lbuf
*lb
, int dir
, int *row
, int *off
)
215 if (o
< 0 || !lbuf_get(lb
, *row
) || o
>= uc_slen(lbuf_get(lb
, *row
)))
221 static int vi_nextcol(struct lbuf
*lb
, int dir
, int *row
, int *off
)
223 char *ln
= lbuf_get(lb
, *row
);
224 int col
= ln
? ren_pos(ln
, *off
) : 0;
225 int o
= ln
? ren_next(ln
, col
, dir
) : -1;
228 *off
= ren_off(ln
, o
);
232 static int vi_findchar(struct lbuf
*lb
, char *cs
, int cmd
, int n
, int *row
, int *off
)
234 strcpy(vi_charlast
, cs
);
236 return lbuf_findchar(lb
, cs
, cmd
, n
, row
, off
);
239 static int vi_search(int cmd
, int cnt
, int *row
, int *off
)
247 if (cmd
== '/' || cmd
== '?') {
248 char sign
[4] = {cmd
};
250 char *kw
= vi_prompt(sign
, ex_kmap());
259 if ((re
= re_read(&kw
))) {
260 ex_kwdset(re
[0] ? re
: NULL
, cmd
== '/' ? +1 : -1);
269 if (!lbuf_len(xb
) || ex_kwd(&kwd
, &dir
))
271 dir
= cmd
== 'N' ? -dir
: dir
;
273 for (i
= 0; i
< cnt
; i
++) {
274 if (lbuf_search(xb
, kwd
, dir
, &r
, &o
, &len
)) {
278 if (i
+ 1 < cnt
&& cmd
== '/')
286 if (*row
+ vi_so
< 0 || *row
+ vi_so
>= lbuf_len(xb
))
293 snprintf(vi_msg
, sizeof(vi_msg
), "\"%s\" not found\n", kwd
);
297 /* read a line motion */
298 static int vi_motionln(int *row
, int cmd
)
300 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
302 int mark
, mark_row
, mark_off
;
306 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
309 *row
= MAX(*row
- cnt
, 0);
312 *row
= MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1);
315 if ((mark
= vi_read()) <= 0)
317 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
322 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
325 *row
= MAX(*row
- cnt
, 0);
328 *row
= (vi_arg1
|| vi_arg2
) ? cnt
- 1 : lbuf_len(xb
) - 1;
332 *row
= MIN(xtop
+ cnt
- 1, lbuf_len(xb
) - 1);
338 *row
= MIN(xtop
+ xrows
- 1 - cnt
+ 1, lbuf_len(xb
) - 1);
344 *row
= MIN(xtop
+ xrows
/ 2, lbuf_len(xb
) - 1);
350 *row
= MAX(0, MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1));
353 if (c
== '%' && (vi_arg1
|| vi_arg2
)) {
356 *row
= MAX(0, lbuf_len(xb
) - 1) * cnt
/ 100;
365 static char *vi_curword(struct lbuf
*lb
, int row
, int off
)
368 char *ln
= lbuf_get(lb
, row
);
372 beg
= uc_chr(ln
, ren_noeol(ln
, off
));
374 while (*end
&& uc_kind(end
) == 1)
376 while (beg
> ln
&& uc_kind(uc_beg(ln
, beg
- 1)) == 1)
377 beg
= uc_beg(ln
, beg
- 1);
382 sbuf_mem(sb
, beg
, end
- beg
);
384 return sbuf_done(sb
);
388 static int vi_motion(int *row
, int *off
)
390 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
391 char *ln
= lbuf_get(xb
, *row
);
392 int dir
= dir_context(ln
? ln
: "");
393 int mark
, mark_row
, mark_off
;
397 if ((mv
= vi_motionln(row
, 0))) {
404 if (!(cs
= vi_char()))
406 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
410 if (!(cs
= vi_char()))
412 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
418 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, cnt
, row
, off
))
424 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, -cnt
, row
, off
))
428 for (i
= 0; i
< cnt
; i
++)
429 if (vi_nextcol(xb
, -1 * dir
, row
, off
))
433 for (i
= 0; i
< cnt
; i
++)
434 if (vi_nextcol(xb
, +1 * dir
, row
, off
))
438 if (!(cs
= vi_char()))
440 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
444 if (!(cs
= vi_char()))
446 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
450 for (i
= 0; i
< cnt
; i
++)
451 if (lbuf_wordend(xb
, 1, -1, row
, off
))
455 for (i
= 0; i
< cnt
; i
++)
456 if (lbuf_wordend(xb
, 1, +1, row
, off
))
460 for (i
= 0; i
< cnt
; i
++)
461 if (lbuf_wordbeg(xb
, 1, +1, row
, off
))
465 for (i
= 0; i
< cnt
; i
++)
466 if (lbuf_wordend(xb
, 0, -1, row
, off
))
470 for (i
= 0; i
< cnt
; i
++)
471 if (lbuf_wordend(xb
, 0, +1, row
, off
))
475 for (i
= 0; i
< cnt
; i
++)
476 if (lbuf_wordbeg(xb
, 0, +1, row
, off
))
480 for (i
= 0; i
< cnt
; i
++)
481 if (lbuf_paragraphbeg(xb
, -1, row
, off
))
485 for (i
= 0; i
< cnt
; i
++)
486 if (lbuf_paragraphbeg(xb
, +1, row
, off
))
490 if (vi_read() != '[')
492 for (i
= 0; i
< cnt
; i
++)
493 if (lbuf_sectionbeg(xb
, -1, row
, off
))
497 if (vi_read() != ']')
499 for (i
= 0; i
< cnt
; i
++)
500 if (lbuf_sectionbeg(xb
, +1, row
, off
))
507 *off
= lbuf_indents(xb
, *row
);
510 *off
= lbuf_eol(xb
, *row
);
513 *off
= vi_col2off(xb
, *row
, cnt
- 1);
517 if (vi_search(mv
, cnt
, row
, off
))
521 if (vi_search(mv
, cnt
, row
, off
))
525 if (vi_search(mv
, cnt
, row
, off
))
529 if (vi_search(mv
, cnt
, row
, off
))
533 if (!(cs
= vi_curword(xb
, *row
, *off
)))
537 if (vi_search('n', cnt
, row
, off
))
541 for (i
= 0; i
< cnt
; i
++)
542 if (vi_nextoff(xb
, +1, row
, off
))
547 for (i
= 0; i
< cnt
; i
++)
548 if (vi_nextoff(xb
, -1, row
, off
))
552 if ((mark
= vi_read()) <= 0)
554 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
560 if (lbuf_pair(xb
, row
, off
))
570 static void swap(int *a
, int *b
)
577 static char *lbuf_region(struct lbuf
*lb
, int r1
, int o1
, int r2
, int o2
)
582 return uc_sub(lbuf_get(lb
, r1
), o1
, o2
);
584 s1
= uc_sub(lbuf_get(lb
, r1
), o1
, -1);
585 s3
= uc_sub(lbuf_get(lb
, r2
), 0, o2
);
586 s2
= lbuf_cp(lb
, r1
+ 1, r2
);
593 return sbuf_done(sb
);
596 static void vi_yank(int r1
, int o1
, int r2
, int o2
, int lnmode
)
599 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
600 reg_put(vi_ybuf
, region
, lnmode
);
603 xoff
= lnmode
? xoff
: o1
;
606 static void vi_delete(int r1
, int o1
, int r2
, int o2
, int lnmode
)
610 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
611 reg_put(vi_ybuf
, region
, lnmode
);
613 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
614 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
616 struct sbuf
*sb
= sbuf_make();
619 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
622 lbuf_edit(xb
, NULL
, r1
, r2
+ 1);
625 xoff
= lnmode
? lbuf_indents(xb
, xrow
) : o1
;
630 static int linecount(char *s
)
634 if ((s
= strchr(s
, '\n')))
639 static int charcount(char *text
, char *post
)
641 int tlen
= strlen(text
);
642 int plen
= strlen(post
);
647 for (i
= 0; i
< tlen
- plen
; i
++)
650 return uc_slen(nl
) - uc_slen(post
);
653 static char *vi_input(char *pref
, char *post
, int *row
, int *off
)
655 char *rep
= led_input(pref
, post
, ex_kmap());
658 *row
= linecount(rep
) - 1;
659 *off
= charcount(rep
, post
) - 1;
665 static char *vi_indents(char *ln
)
667 struct sbuf
*sb
= sbuf_make();
668 while (xai
&& ln
&& (*ln
== ' ' || *ln
== '\t'))
670 return sbuf_done(sb
);
673 static void vi_change(int r1
, int o1
, int r2
, int o2
, int lnmode
)
679 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
680 reg_put(vi_ybuf
, region
, lnmode
);
682 pref
= lnmode
? vi_indents(lbuf_get(xb
, r1
)) : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
683 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
684 vi_drawrm(r1
, r2
, 0);
685 rep
= vi_input(pref
, post
, &row
, &off
);
687 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
696 static void vi_case(int r1
, int o1
, int r2
, int o2
, int lnmode
, int cmd
)
700 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
703 int c
= (unsigned char) s
[0];
710 s
[0] = islower(c
) ? toupper(c
) : tolower(c
);
714 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
715 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
717 struct sbuf
*sb
= sbuf_make();
719 sbuf_str(sb
, region
);
721 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
724 lbuf_edit(xb
, region
, r1
, r2
+ 1);
727 xoff
= lnmode
? lbuf_indents(xb
, r2
) : o2
;
733 static void vi_pipe(int r1
, int r2
)
738 char *cmd
= vi_prompt("!", &kmap
);
741 text
= lbuf_cp(xb
, r1
, r2
+ 1);
742 rep
= cmd_pipe(cmd
, text
, 1, 1);
744 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
750 static void vi_shift(int r1
, int r2
, int dir
)
755 for (i
= r1
; i
<= r2
; i
++) {
756 if (!(ln
= lbuf_get(xb
, i
)))
762 ln
= ln
[0] == ' ' || ln
[0] == '\t' ? ln
+ 1 : ln
;
764 lbuf_edit(xb
, sbuf_buf(sb
), i
, i
+ 1);
768 xoff
= lbuf_indents(xb
, xrow
);
771 static int vc_motion(int cmd
)
773 int r1
= xrow
, r2
= xrow
; /* region rows */
774 int o1
= xoff
, o2
= xoff
; /* visual region columns */
775 int lnmode
= 0; /* line-based region */
777 vi_arg2
= vi_prefix();
780 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
782 if ((mv
= vi_motionln(&r2
, cmd
))) {
784 } else if (!(mv
= vi_motion(&r2
, &o2
))) {
793 o2
= lbuf_eol(xb
, r2
);
799 if (r1
== r2
&& o1
> o2
)
801 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
802 if (!lnmode
&& strchr("fFtTeE%", mv
))
803 if (o2
< lbuf_eol(xb
, r2
))
804 o2
= ren_noeol(lbuf_get(xb
, r2
), o2
) + 1;
806 vi_yank(r1
, o1
, r2
, o2
, lnmode
);
808 vi_delete(r1
, o1
, r2
, o2
, lnmode
);
810 vi_change(r1
, o1
, r2
, o2
, lnmode
);
811 if (cmd
== '~' || cmd
== 'u' || cmd
== 'U')
812 vi_case(r1
, o1
, r2
, o2
, lnmode
, cmd
);
815 if (cmd
== '>' || cmd
== '<')
816 vi_shift(r1
, r2
, cmd
== '>' ? +1 : -1);
820 static int vc_insert(int cmd
)
823 char *ln
= lbuf_get(xb
, xrow
);
827 xoff
= lbuf_indents(xb
, xrow
);
829 xoff
= lbuf_eol(xb
, xrow
);
830 xoff
= ren_noeol(ln
, xoff
);
833 if (cmd
== 'i' || cmd
== 'I')
835 if (cmd
== 'a' || cmd
== 'A')
837 if (ln
&& ln
[0] == '\n')
839 pref
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, 0, off
) : vi_indents(ln
);
840 post
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, off
, -1) : uc_dup("\n");
841 vi_drawrm(xrow
, xrow
, cmd
== 'o' || cmd
== 'O');
842 rep
= vi_input(pref
, post
, &row
, &off
);
843 if ((cmd
== 'o' || cmd
== 'O') && !lbuf_len(xb
))
844 lbuf_edit(xb
, "\n", 0, 0);
846 lbuf_edit(xb
, rep
, xrow
, xrow
+ (cmd
!= 'o' && cmd
!= 'O'));
856 static int vc_put(int cmd
)
858 int cnt
= MAX(1, vi_arg1
);
860 char *buf
= reg_get(vi_ybuf
, &lnmode
);
863 snprintf(vi_msg
, sizeof(vi_msg
), "yank buffer empty\n");
867 struct sbuf
*sb
= sbuf_make();
868 for (i
= 0; i
< cnt
; i
++)
871 lbuf_edit(xb
, "\n", 0, 0);
874 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
);
875 xoff
= lbuf_indents(xb
, xrow
);
878 struct sbuf
*sb
= sbuf_make();
879 char *ln
= xrow
< lbuf_len(xb
) ? lbuf_get(xb
, xrow
) : "\n";
880 int off
= ren_noeol(ln
, xoff
) + (ln
[0] != '\n' && cmd
== 'p');
881 char *s
= uc_sub(ln
, 0, off
);
884 for (i
= 0; i
< cnt
; i
++)
886 s
= uc_sub(ln
, off
, -1);
889 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
890 xoff
= off
+ uc_slen(buf
) * cnt
- 1;
896 static int join_spaces(char *prev
, char *next
)
898 int prevlen
= strlen(prev
);
901 if (prev
[prevlen
- 1] == ' ' || next
[0] == ')')
903 return prev
[prevlen
- 1] == '.' ? 2 : 1;
906 static int vc_join(void)
909 int cnt
= vi_arg1
<= 1 ? 2 : vi_arg1
;
911 int end
= xrow
+ cnt
;
914 if (!lbuf_get(xb
, beg
) || !lbuf_get(xb
, end
- 1))
917 for (i
= beg
; i
< end
; i
++) {
918 char *ln
= lbuf_get(xb
, i
);
919 char *lnend
= strchr(ln
, '\n');
922 while (ln
[0] == ' ' || ln
[0] == '\t')
924 spaces
= i
> beg
? join_spaces(sbuf_buf(sb
), ln
) : 0;
925 off
= uc_slen(sbuf_buf(sb
));
928 sbuf_mem(sb
, ln
, lnend
- ln
);
931 lbuf_edit(xb
, sbuf_buf(sb
), beg
, end
);
937 static int vi_scrollforeward(int cnt
)
939 if (xtop
>= lbuf_len(xb
) - 1)
941 xtop
= MIN(lbuf_len(xb
) - 1, xtop
+ cnt
);
942 xrow
= MAX(xrow
, xtop
);
946 static int vi_scrollbackward(int cnt
)
950 xtop
= MAX(0, xtop
- cnt
);
951 xrow
= MIN(xrow
, xtop
+ xrows
- 1);
955 static void vc_status(void)
957 int col
= vi_off2col(xb
, xrow
, xoff
);
958 snprintf(vi_msg
, sizeof(vi_msg
),
959 "\"%s\"%c %d lines L%d C%d\n",
960 ex_path()[0] ? ex_path() : "unnamed",
961 lbuf_modified(xb
) ? '*' : ' ',
962 lbuf_len(xb
), xrow
+ 1,
963 ren_cursor(lbuf_get(xb
, xrow
), col
) + 1);
966 static int vc_replace(void)
968 int cnt
= MAX(1, vi_arg1
);
969 char *cs
= vi_char();
970 char *ln
= lbuf_get(xb
, xrow
);
977 off
= ren_noeol(ln
, xoff
);
979 for (i
= 0; s
[0] != '\n' && i
< cnt
; i
++)
983 pref
= uc_sub(ln
, 0, off
);
984 post
= uc_sub(ln
, off
+ cnt
, -1);
987 for (i
= 0; i
< cnt
; i
++)
990 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
999 static char rep_cmd
[4096]; /* the last command */
1002 static void vc_repeat(void)
1004 term_push(rep_cmd
, rep_len
);
1007 static void vc_execute(void)
1009 static int exec_buf
;
1018 buf
= reg_get(exec_buf
, &lnmode
);
1020 term_push(buf
, strlen(buf
));
1023 static void vi(void)
1029 xtop
= MAX(0, xrow
- xrows
/ 2);
1031 xcol
= vi_off2col(xb
, xrow
, xoff
);
1032 vi_drawagain(xcol
, 0);
1033 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
), xcol
));
1035 int mod
= 0; /* screen should be redrawn */
1037 int noff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1043 vi_ybuf
= vi_yankbuf();
1044 vi_arg1
= vi_prefix();
1046 vi_ybuf
= vi_yankbuf();
1047 mv
= vi_motion(&nrow
, &noff
);
1049 if (strchr("\'`GHML/?{}[]nN", mv
) ||
1050 (mv
== '%' && noff
< 0)) {
1051 lbuf_mark(xb
, '\'', xrow
, xoff
);
1052 lbuf_mark(xb
, '`', xrow
, xoff
);
1055 if (noff
< 0 && !strchr("jk", mv
))
1056 noff
= lbuf_indents(xb
, xrow
);
1057 if (strchr("jk", mv
))
1058 noff
= vi_col2off(xb
, xrow
, xcol
);
1059 xoff
= ren_noeol(lbuf_get(xb
, xrow
), noff
);
1060 if (!strchr("|jk", mv
))
1061 xcol
= vi_off2col(xb
, xrow
, xoff
);
1064 } else if (mv
== 0) {
1070 lbuf_mark(xb
, '*', xrow
, xoff
);
1073 if (vi_scrollbackward(MAX(1, vi_arg1
) * (xrows
- 1)))
1075 xoff
= lbuf_indents(xb
, xrow
);
1078 if (vi_scrollforeward(MAX(1, vi_arg1
) * (xrows
- 1)))
1080 xoff
= lbuf_indents(xb
, xrow
);
1083 if (vi_scrollforeward(MAX(1, vi_arg1
)))
1087 if (vi_scrollbackward(MAX(1, vi_arg1
)))
1094 vi_scroll
= vi_arg1
;
1095 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1096 xrow
= MAX(0, xrow
- n
);
1098 xtop
= MAX(0, xtop
- n
);
1099 xoff
= lbuf_indents(xb
, xrow
);
1102 if (xrow
== lbuf_len(xb
) - 1)
1105 vi_scroll
= vi_arg1
;
1106 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1107 xrow
= MIN(MAX(0, lbuf_len(xb
) - 1), xrow
+ n
);
1108 if (xtop
< lbuf_len(xb
) - xrows
)
1109 xtop
= MIN(lbuf_len(xb
) - xrows
, xtop
+ n
);
1110 xoff
= lbuf_indents(xb
, xrow
);
1118 if (!lbuf_undo(xb
)) {
1119 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1122 snprintf(vi_msg
, sizeof(vi_msg
), "undo failed\n");
1126 if (!lbuf_redo(xb
)) {
1127 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1130 snprintf(vi_msg
, sizeof(vi_msg
), "redo failed\n");
1141 ln
= vi_prompt(":", &kmap
);
1176 if ((mark
= vi_read()) > 0 && islower(mark
))
1177 lbuf_mark(xb
, mark
, xrow
, xoff
);
1188 xtop
= vi_arg1
? vi_arg1
: xrow
;
1191 n
= vi_arg1
? vi_arg1
: xrow
;
1192 xtop
= MAX(0, n
- xrows
/ 2);
1195 n
= vi_arg1
? vi_arg1
: xrow
;
1196 xtop
= MAX(0, n
- xrows
+ 1);
1200 xdir
= k
== 'r' ? -1 : +1;
1204 xdir
= k
== 'R' ? -2 : +2;
1211 if (k
== '~' || k
== 'u' || k
== 'U')
1217 if (!vc_motion('d'))
1221 vi_back(TK_CTL('h'));
1222 if (!vc_motion('d'))
1227 if (!vc_motion('c'))
1232 if (!vc_motion('d'))
1241 if (!vc_motion('c'))
1246 if (!vc_motion('c'))
1260 if (!vc_motion('~'))
1273 if (strchr("!<>ACDIJOPRSXYacdioprsxy~", c
)) {
1274 if (n
< sizeof(rep_cmd
)) {
1275 memcpy(rep_cmd
, cmd
, n
);
1280 if (xrow
< 0 || xrow
>= lbuf_len(xb
))
1281 xrow
= lbuf_len(xb
) ? lbuf_len(xb
) - 1 : 0;
1283 xtop
= xtop
- xrows
/ 2 > xrow
?
1284 MAX(0, xrow
- xrows
/ 2) : xrow
;
1285 if (xtop
+ xrows
<= xrow
)
1286 xtop
= xtop
+ xrows
+ xrows
/ 2 <= xrow
?
1287 xrow
- xrows
/ 2 : xrow
- xrows
+ 1;
1288 xoff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1290 xcol
= vi_off2col(xb
, xrow
, xoff
);
1291 if (xcol
>= xleft
+ xcols
)
1292 xleft
= xcol
- xcols
/ 2;
1294 xleft
= xcol
< xcols
? 0 : xcol
- xcols
/ 2;
1296 if (mod
|| xleft
!= oleft
)
1297 vi_drawagain(xcol
, mod
== 2);
1298 else if (xtop
!= otop
)
1299 vi_drawupdate(xcol
, otop
);
1302 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
),
1303 ren_cursor(lbuf_get(xb
, xrow
), xcol
)));
1310 int main(int argc
, char *argv
[])
1314 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1315 if (argv
[i
][1] == 's')
1317 if (argv
[i
][1] == 'e')
1319 if (argv
[i
][1] == 'v')
1326 if (!ex_init(argv
+ i
)) {