4 * Copyright (C) 2015 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 char *vi_kmap
; /* current insertion keymap */
21 static int vi_pcol
; /* the column requested by | command */
22 static int vi_printed
; /* ex_print() calls since the last command */
23 static int vi_scroll
; /* scroll amount for ^f and ^d*/
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 /* redraw the screen */
44 static void vi_draw(int xcol
)
48 for (i
= xtop
; i
< xtop
+ xrows
; i
++) {
49 char *s
= lbuf_get(xb
, i
);
50 led_print(s
? s
: (i
? "~" : ""), i
- xtop
);
53 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
57 /* update the screen by removing lines r1 to r2 before an input command */
58 static void vi_drawrm(int r1
, int r2
, int newln
)
60 r1
= MIN(MAX(r1
, xtop
), xtop
+ xrows
);
61 r2
= MIN(MAX(r2
, xtop
), xtop
+ xrows
);
62 term_pos(r1
- xtop
, 0);
63 term_room(r1
- r2
+ newln
);
66 static int vi_buf
[128];
69 static int vi_read(void)
71 return vi_buflen
? vi_buf
[--vi_buflen
] : term_read();
74 static void vi_back(int c
)
76 if (vi_buflen
< sizeof(vi_buf
))
77 vi_buf
[vi_buflen
++] = c
;
80 static char *vi_char(void)
82 return led_read(&vi_kmap
);
85 static char *vi_prompt(char *msg
, char **kmap
)
87 term_pos(xrows
, led_pos(msg
, 0));
89 return led_prompt(msg
, "", kmap
);
92 /* read an ex input line */
93 char *ex_read(char *msg
)
98 char *s
= led_prompt(msg
, "", &vi_kmap
);
104 while ((c
= getchar()) != EOF
&& c
!= '\n')
110 return sbuf_done(sb
);
113 /* show an ex message */
114 void ex_show(char *msg
)
117 snprintf(vi_msg
, sizeof(vi_msg
), "%s", msg
);
126 /* print an ex output line */
127 void ex_print(char *line
)
130 vi_printed
+= line
? 1 : 2;
132 snprintf(vi_msg
, sizeof(vi_msg
), "%s", line
);
142 static int vi_yankbuf(void)
151 static int vi_prefix(void)
155 if ((c
>= '1' && c
<= '9')) {
157 n
= n
* 10 + c
- '0';
165 static int vi_col2off(struct lbuf
*lb
, int row
, int col
)
167 char *ln
= lbuf_get(lb
, row
);
168 return ln
? ren_off(ln
, col
) : 0;
171 static int vi_off2col(struct lbuf
*lb
, int row
, int off
)
173 char *ln
= lbuf_get(lb
, row
);
174 return ln
? ren_pos(ln
, off
) : 0;
177 static int vi_nextoff(struct lbuf
*lb
, int dir
, int *row
, int *off
)
180 if (o
< 0 || !lbuf_get(lb
, *row
) || o
>= uc_slen(lbuf_get(lb
, *row
)))
186 static int vi_nextcol(struct lbuf
*lb
, int dir
, int *row
, int *off
)
188 char *ln
= lbuf_get(lb
, *row
);
189 int col
= ln
? ren_pos(ln
, *off
) : 0;
190 int o
= ln
? ren_next(ln
, col
, dir
) : -1;
193 *off
= ren_off(ln
, o
);
197 static int vi_findchar(struct lbuf
*lb
, char *cs
, int cmd
, int n
, int *row
, int *off
)
199 strcpy(vi_charlast
, cs
);
201 return lbuf_findchar(lb
, cs
, cmd
, n
, row
, off
);
204 static int vi_search(int cmd
, int cnt
, int *row
, int *off
)
212 if (cmd
== '/' || cmd
== '?') {
213 char sign
[4] = {cmd
};
214 char *kw
= vi_prompt(sign
, &vi_kmap
);
217 xfinddir
= cmd
== '/' ? +1 : -1;
219 snprintf(xfindkwd
, sizeof(xfindkwd
), "%s", kw
);
220 if (strchr(xfindkwd
, cmd
)) {
221 soff
= strchr(xfindkwd
, cmd
) + 1;
222 *strchr(xfindkwd
, cmd
) = '\0';
226 dir
= cmd
== 'N' ? -xfinddir
: xfinddir
;
227 if (!xfindkwd
[0] || !lbuf_len(xb
))
230 for (i
= 0; i
< cnt
; i
++) {
231 if (lbuf_search(xb
, xfindkwd
, dir
, &r
, &o
, &len
)) {
235 if (i
+ 1 < cnt
&& cmd
== '/')
241 while (soff
[0] && isspace((unsigned char) soff
[0]))
245 if (*row
+ atoi(soff
) < 0 || *row
+ atoi(soff
) >= lbuf_len(xb
))
252 snprintf(vi_msg
, sizeof(vi_msg
), "\"%s\" not found\n", xfindkwd
);
256 /* read a line motion */
257 static int vi_motionln(int *row
, int cmd
)
259 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
261 int mark
, mark_row
, mark_off
;
265 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
268 *row
= MAX(*row
- cnt
, 0);
271 *row
= MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1);
274 if ((mark
= vi_read()) <= 0)
276 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
281 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
284 *row
= MAX(*row
- cnt
, 0);
287 *row
= (vi_arg1
|| vi_arg2
) ? cnt
- 1 : lbuf_len(xb
) - 1;
291 *row
= MIN(xtop
+ cnt
- 1, lbuf_len(xb
) - 1);
297 *row
= MIN(xtop
+ xrows
- 1 - cnt
+ 1, lbuf_len(xb
) - 1);
303 *row
= MIN(xtop
+ xrows
/ 2, lbuf_len(xb
) - 1);
309 *row
= MAX(0, MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1));
312 if (c
== '%' && (vi_arg1
|| vi_arg2
)) {
315 *row
= MAX(0, lbuf_len(xb
) - 1) * cnt
/ 100;
324 static char *vi_curword(struct lbuf
*lb
, int row
, int off
)
327 char *ln
= lbuf_get(lb
, row
);
331 beg
= uc_chr(ln
, ren_noeol(ln
, off
));
333 while (*end
&& uc_kind(end
) == 1)
335 while (beg
> ln
&& uc_kind(uc_beg(ln
, beg
- 1)) == 1)
336 beg
= uc_beg(ln
, beg
- 1);
341 sbuf_mem(sb
, beg
, end
- beg
);
343 return sbuf_done(sb
);
347 static int vi_motion(int *row
, int *off
)
349 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
350 char *ln
= lbuf_get(xb
, *row
);
351 int dir
= dir_context(ln
? ln
: "");
352 int mark
, mark_row
, mark_off
;
356 if ((mv
= vi_motionln(row
, 0))) {
363 if (!(cs
= vi_char()))
365 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
369 if (!(cs
= vi_char()))
371 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
377 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, cnt
, row
, off
))
383 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, -cnt
, row
, off
))
387 for (i
= 0; i
< cnt
; i
++)
388 if (vi_nextcol(xb
, -1 * dir
, row
, off
))
392 for (i
= 0; i
< cnt
; i
++)
393 if (vi_nextcol(xb
, +1 * dir
, row
, off
))
397 if (!(cs
= vi_char()))
399 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
403 if (!(cs
= vi_char()))
405 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
409 for (i
= 0; i
< cnt
; i
++)
410 if (lbuf_wordend(xb
, 1, -1, row
, off
))
414 for (i
= 0; i
< cnt
; i
++)
415 if (lbuf_wordend(xb
, 1, +1, row
, off
))
419 for (i
= 0; i
< cnt
; i
++)
420 if (lbuf_wordbeg(xb
, 1, +1, row
, off
))
424 for (i
= 0; i
< cnt
; i
++)
425 if (lbuf_wordend(xb
, 0, -1, row
, off
))
429 for (i
= 0; i
< cnt
; i
++)
430 if (lbuf_wordend(xb
, 0, +1, row
, off
))
434 for (i
= 0; i
< cnt
; i
++)
435 if (lbuf_wordbeg(xb
, 0, +1, row
, off
))
439 for (i
= 0; i
< cnt
; i
++)
440 if (lbuf_paragraphbeg(xb
, -1, row
, off
))
444 for (i
= 0; i
< cnt
; i
++)
445 if (lbuf_paragraphbeg(xb
, +1, row
, off
))
449 if (vi_read() != '[')
451 for (i
= 0; i
< cnt
; i
++)
452 if (lbuf_sectionbeg(xb
, -1, row
, off
))
456 if (vi_read() != ']')
458 for (i
= 0; i
< cnt
; i
++)
459 if (lbuf_sectionbeg(xb
, +1, row
, off
))
466 *off
= lbuf_indents(xb
, *row
);
469 *off
= lbuf_eol(xb
, *row
);
472 *off
= vi_col2off(xb
, *row
, cnt
- 1);
476 if (vi_search(mv
, cnt
, row
, off
))
480 if (vi_search(mv
, cnt
, row
, off
))
484 if (vi_search(mv
, cnt
, row
, off
))
488 if (vi_search(mv
, cnt
, row
, off
))
492 if (!(cs
= vi_curword(xb
, *row
, *off
)))
494 strcpy(xfindkwd
, cs
);
497 if (vi_search('n', cnt
, row
, off
))
501 for (i
= 0; i
< cnt
; i
++)
502 if (vi_nextoff(xb
, +1, row
, off
))
507 for (i
= 0; i
< cnt
; i
++)
508 if (vi_nextoff(xb
, -1, row
, off
))
512 if ((mark
= vi_read()) <= 0)
514 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
520 if (lbuf_pair(xb
, row
, off
))
530 static void swap(int *a
, int *b
)
537 static char *lbuf_region(struct lbuf
*lb
, int r1
, int o1
, int r2
, int o2
)
542 return uc_sub(lbuf_get(lb
, r1
), o1
, o2
);
544 s1
= uc_sub(lbuf_get(lb
, r1
), o1
, -1);
545 s3
= uc_sub(lbuf_get(lb
, r2
), 0, o2
);
546 s2
= lbuf_cp(lb
, r1
+ 1, r2
);
553 return sbuf_done(sb
);
556 static void vi_yank(int r1
, int o1
, int r2
, int o2
, int lnmode
)
559 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
560 reg_put(vi_ybuf
, region
, lnmode
);
563 xoff
= lnmode
? xoff
: o1
;
566 static void vi_delete(int r1
, int o1
, int r2
, int o2
, int lnmode
)
570 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
571 reg_put(vi_ybuf
, region
, lnmode
);
573 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
574 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
576 struct sbuf
*sb
= sbuf_make();
579 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
582 lbuf_edit(xb
, NULL
, r1
, r2
+ 1);
585 xoff
= lnmode
? lbuf_indents(xb
, xrow
) : o1
;
590 static int linecount(char *s
)
594 if ((s
= strchr(s
, '\n')))
599 static int indentscopy(char *d
, char *s
, int len
)
602 for (i
= 0; i
< len
- 1 && (s
[i
] == ' ' || s
[i
] == '\t'); i
++)
608 static char *vi_input(char *pref
, char *post
, int *row
, int *off
)
615 pref
+= indentscopy(ai
, pref
, sizeof(ai
));
616 rep
= led_input(pref
, post
, ai
, xai
? sizeof(ai
) - 1 : 0, &vi_kmap
);
624 last
= uc_lastline(s
) - s
;
625 *off
= MAX(0, uc_slen(sbuf_buf(sb
) + last
) - 1);
627 while (xai
&& (post
[0] == ' ' || post
[0] == '\t'))
630 *row
= linecount(sbuf_buf(sb
)) - 1;
632 return sbuf_done(sb
);
635 static char *vi_indents(char *ln
)
637 struct sbuf
*sb
= sbuf_make();
638 while (xai
&& ln
&& (*ln
== ' ' || *ln
== '\t'))
640 return sbuf_done(sb
);
643 static void vi_change(int r1
, int o1
, int r2
, int o2
, int lnmode
)
649 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
650 reg_put(vi_ybuf
, region
, lnmode
);
652 pref
= lnmode
? vi_indents(lbuf_get(xb
, r1
)) : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
653 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
654 vi_drawrm(r1
, r2
, 0);
655 rep
= vi_input(pref
, post
, &row
, &off
);
657 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
666 static void vi_case(int r1
, int o1
, int r2
, int o2
, int lnmode
, int cmd
)
670 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
673 int c
= (unsigned char) s
[0];
680 s
[0] = islower(c
) ? toupper(c
) : tolower(c
);
684 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
685 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
687 struct sbuf
*sb
= sbuf_make();
689 sbuf_str(sb
, region
);
691 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
694 lbuf_edit(xb
, region
, r1
, r2
+ 1);
697 xoff
= lnmode
? lbuf_indents(xb
, r2
) : o2
;
703 static void vi_pipe(int r1
, int r2
)
708 char *cmd
= vi_prompt("!", &kmap
);
711 text
= lbuf_cp(xb
, r1
, r2
+ 1);
712 rep
= cmd_pipe(cmd
, text
, 1, 1);
714 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
720 static void vi_shift(int r1
, int r2
, int dir
)
725 for (i
= r1
; i
<= r2
; i
++) {
726 if (!(ln
= lbuf_get(xb
, i
)))
732 ln
= ln
[0] == ' ' || ln
[0] == '\t' ? ln
+ 1 : ln
;
734 lbuf_edit(xb
, sbuf_buf(sb
), i
, i
+ 1);
738 xoff
= lbuf_indents(xb
, xrow
);
741 static int vc_motion(int cmd
)
743 int r1
= xrow
, r2
= xrow
; /* region rows */
744 int o1
= xoff
, o2
= xoff
; /* visual region columns */
745 int lnmode
= 0; /* line-based region */
747 vi_arg2
= vi_prefix();
750 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
752 if ((mv
= vi_motionln(&r2
, cmd
))) {
754 } else if (!(mv
= vi_motion(&r2
, &o2
))) {
763 o2
= lbuf_eol(xb
, r2
);
769 if (r1
== r2
&& o1
> o2
)
771 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
772 if (!lnmode
&& strchr("fFtTeE%", mv
))
773 if (o2
< lbuf_eol(xb
, r2
))
774 o2
= ren_noeol(lbuf_get(xb
, r2
), o2
) + 1;
776 vi_yank(r1
, o1
, r2
, o2
, lnmode
);
778 vi_delete(r1
, o1
, r2
, o2
, lnmode
);
780 vi_change(r1
, o1
, r2
, o2
, lnmode
);
781 if (cmd
== '~' || cmd
== 'u' || cmd
== 'U')
782 vi_case(r1
, o1
, r2
, o2
, lnmode
, cmd
);
785 if (cmd
== '>' || cmd
== '<')
786 vi_shift(r1
, r2
, cmd
== '>' ? +1 : -1);
790 static int vc_insert(int cmd
)
793 char *ln
= lbuf_get(xb
, xrow
);
797 xoff
= lbuf_indents(xb
, xrow
);
799 xoff
= lbuf_eol(xb
, xrow
);
800 xoff
= ren_noeol(ln
, xoff
);
803 if (cmd
== 'i' || cmd
== 'I')
805 if (cmd
== 'a' || cmd
== 'A')
807 pref
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, 0, off
) : vi_indents(ln
);
808 post
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, off
, -1) : uc_dup("\n");
809 vi_drawrm(xrow
, xrow
, cmd
== 'o' || cmd
== 'O');
810 rep
= vi_input(pref
, post
, &row
, &off
);
811 if ((cmd
== 'o' || cmd
== 'O') && !lbuf_len(xb
))
812 lbuf_edit(xb
, "\n", 0, 0);
814 lbuf_edit(xb
, rep
, xrow
, xrow
+ (cmd
!= 'o' && cmd
!= 'O'));
824 static int vc_put(int cmd
)
826 int cnt
= MAX(1, vi_arg1
);
828 char *buf
= reg_get(vi_ybuf
, &lnmode
);
831 snprintf(vi_msg
, sizeof(vi_msg
), "yank buffer empty\n");
835 struct sbuf
*sb
= sbuf_make();
836 for (i
= 0; i
< cnt
; i
++)
839 lbuf_edit(xb
, "\n", 0, 0);
842 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
);
843 xoff
= lbuf_indents(xb
, xrow
);
846 struct sbuf
*sb
= sbuf_make();
847 char *ln
= xrow
< lbuf_len(xb
) ? lbuf_get(xb
, xrow
) : "\n";
848 int off
= ren_noeol(ln
, xoff
) + (ln
[0] != '\n' && cmd
== 'p');
849 char *s
= uc_sub(ln
, 0, off
);
852 for (i
= 0; i
< cnt
; i
++)
854 s
= uc_sub(ln
, off
, -1);
857 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
858 xoff
= off
+ uc_slen(buf
) * cnt
- 1;
864 static int join_spaces(char *prev
, char *next
)
866 int prevlen
= strlen(prev
);
869 if (prev
[prevlen
- 1] == ' ' || next
[0] == ')')
871 return prev
[prevlen
- 1] == '.' ? 2 : 1;
874 static int vc_join(void)
877 int cnt
= vi_arg1
<= 1 ? 2 : vi_arg1
;
879 int end
= xrow
+ cnt
;
882 if (!lbuf_get(xb
, beg
) || !lbuf_get(xb
, end
- 1))
885 for (i
= beg
; i
< end
; i
++) {
886 char *ln
= lbuf_get(xb
, i
);
887 char *lnend
= strchr(ln
, '\n');
890 while (ln
[0] == ' ' || ln
[0] == '\t')
892 spaces
= i
> beg
? join_spaces(sbuf_buf(sb
), ln
) : 0;
893 off
= uc_slen(sbuf_buf(sb
));
896 sbuf_mem(sb
, ln
, lnend
- ln
);
899 lbuf_edit(xb
, sbuf_buf(sb
), beg
, end
);
905 static int vi_scrollforeward(int cnt
)
907 if (xtop
>= lbuf_len(xb
) - 1)
909 xtop
= MIN(lbuf_len(xb
) - 1, xtop
+ cnt
);
910 xrow
= MAX(xrow
, xtop
);
914 static int vi_scrollbackward(int cnt
)
918 xtop
= MAX(0, xtop
- cnt
);
919 xrow
= MIN(xrow
, xtop
+ xrows
- 1);
923 static void vc_status(void)
925 int col
= vi_off2col(xb
, xrow
, xoff
);
926 snprintf(vi_msg
, sizeof(vi_msg
),
927 "\"%s\"%c %d lines L%d C%d\n",
928 ex_path()[0] ? ex_path() : "unnamed",
929 lbuf_modified(xb
) ? '*' : ' ',
930 lbuf_len(xb
), xrow
+ 1,
931 ren_cursor(lbuf_get(xb
, xrow
), col
) + 1);
934 static int vc_replace(void)
936 int cnt
= MAX(1, vi_arg1
);
937 char *cs
= vi_char();
938 char *ln
= lbuf_get(xb
, xrow
);
945 off
= ren_noeol(ln
, xoff
);
947 for (i
= 0; s
[0] != '\n' && i
< cnt
; i
++)
951 pref
= uc_sub(ln
, 0, off
);
952 post
= uc_sub(ln
, off
+ cnt
, -1);
955 for (i
= 0; i
< cnt
; i
++)
958 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
967 static char rep_cmd
[4096]; /* the last command */
970 static void vc_repeat(void)
972 term_push(rep_cmd
, rep_len
);
975 static void vc_execute(void)
986 buf
= reg_get(exec_buf
, &lnmode
);
988 term_push(buf
, strlen(buf
));
997 xtop
= MAX(0, xrow
- xrows
/ 2);
999 xcol
= vi_off2col(xb
, xrow
, xoff
);
1001 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
), xcol
));
1005 int noff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1011 vi_ybuf
= vi_yankbuf();
1012 vi_arg1
= vi_prefix();
1014 vi_ybuf
= vi_yankbuf();
1015 mv
= vi_motion(&nrow
, &noff
);
1017 if (strchr("\'`GHML/?{}[]nN", mv
) ||
1018 (mv
== '%' && noff
< 0)) {
1019 lbuf_mark(xb
, '\'', xrow
, xoff
);
1020 lbuf_mark(xb
, '`', xrow
, xoff
);
1023 if (noff
< 0 && !strchr("jk", mv
))
1024 noff
= lbuf_indents(xb
, xrow
);
1025 if (strchr("jk", mv
))
1026 noff
= vi_col2off(xb
, xrow
, xcol
);
1027 xoff
= ren_noeol(lbuf_get(xb
, xrow
), noff
);
1028 if (!strchr("|jk", mv
))
1029 xcol
= vi_off2col(xb
, xrow
, xoff
);
1032 } else if (mv
== 0) {
1038 lbuf_mark(xb
, '*', xrow
, xoff
);
1041 if (vi_scrollbackward(MAX(1, vi_arg1
) * (xrows
- 1)))
1043 xoff
= lbuf_indents(xb
, xrow
);
1047 if (vi_scrollforeward(MAX(1, vi_arg1
) * (xrows
- 1)))
1049 xoff
= lbuf_indents(xb
, xrow
);
1053 if (vi_scrollforeward(MAX(1, vi_arg1
)))
1058 if (vi_scrollbackward(MAX(1, vi_arg1
)))
1066 vi_scroll
= vi_arg1
;
1067 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1068 xrow
= MAX(0, xrow
- n
);
1070 xtop
= MAX(0, xtop
- n
);
1072 xoff
= lbuf_indents(xb
, xrow
);
1075 if (xrow
== lbuf_len(xb
) - 1)
1078 vi_scroll
= vi_arg1
;
1079 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1080 xrow
= MIN(MAX(0, lbuf_len(xb
) - 1), xrow
+ n
);
1081 if (xtop
< lbuf_len(xb
) - xrows
)
1082 xtop
= MIN(lbuf_len(xb
) - xrows
, xtop
+ n
);
1084 xoff
= lbuf_indents(xb
, xrow
);
1092 if (!lbuf_undo(xb
)) {
1093 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1096 snprintf(vi_msg
, sizeof(vi_msg
), "undo failed\n");
1100 if (!lbuf_redo(xb
)) {
1101 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1104 snprintf(vi_msg
, sizeof(vi_msg
), "redo failed\n");
1115 ln
= vi_prompt(":", &kmap
);
1150 if ((mark
= vi_read()) > 0 && islower(mark
))
1151 lbuf_mark(xb
, mark
, xrow
, xoff
);
1162 xtop
= vi_arg1
? vi_arg1
: xrow
;
1165 n
= vi_arg1
? vi_arg1
: xrow
;
1166 xtop
= MAX(0, n
- xrows
/ 2);
1169 n
= vi_arg1
? vi_arg1
: xrow
;
1170 xtop
= MAX(0, n
- xrows
+ 1);
1174 xdir
= k
== 'r' ? -1 : +1;
1178 xdir
= k
== 'R' ? -2 : +2;
1185 if (k
== '~' || k
== 'u' || k
== 'U')
1191 if (!vc_motion('d'))
1195 vi_back(TK_CTL('h'));
1196 if (!vc_motion('d'))
1201 if (!vc_motion('c'))
1206 if (!vc_motion('d'))
1215 if (!vc_motion('c'))
1220 if (!vc_motion('c'))
1225 if (!vc_motion('y'))
1235 if (!vc_motion('~'))
1248 if (strchr("!<>ACDIJOPRSXYacdioprsxy~", c
)) {
1249 if (n
< sizeof(rep_cmd
)) {
1250 memcpy(rep_cmd
, cmd
, n
);
1255 if (xrow
< 0 || xrow
>= lbuf_len(xb
))
1256 xrow
= lbuf_len(xb
) ? lbuf_len(xb
) - 1 : 0;
1258 xtop
= xtop
- xrows
/ 2 > xrow
?
1259 MAX(0, xrow
- xrows
/ 2) : xrow
;
1260 if (xtop
+ xrows
<= xrow
)
1261 xtop
= xtop
+ xrows
+ xrows
/ 2 <= xrow
?
1262 xrow
- xrows
/ 2 : xrow
- xrows
+ 1;
1263 xoff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1265 xcol
= vi_off2col(xb
, xrow
, xoff
);
1266 if (xcol
>= xleft
+ xcols
)
1267 xleft
= xcol
- xcols
/ 2;
1269 xleft
= xcol
< xcols
? 0 : xcol
- xcols
/ 2;
1271 if (redraw
|| xtop
!= otop
|| xleft
!= oleft
)
1275 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
),
1276 ren_cursor(lbuf_get(xb
, xrow
), xcol
)));
1283 int main(int argc
, char *argv
[])
1287 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1288 if (argv
[i
][1] == 's')
1290 if (argv
[i
][1] == 'e')
1292 if (argv
[i
][1] == 'v')
1299 if (!ex_init(argv
+ i
)) {