4 * Copyright (C) 2015-2016 Ali Gholami Rudi <ali at rudi dot ir>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 static char vi_msg
[EXLEN
]; /* current message */
26 static char vi_charlast
[8]; /* the last character searched via f, t, F, or T */
27 static int vi_charcmd
; /* the character finding command */
28 static int vi_arg1
, vi_arg2
; /* the first and second arguments */
29 static int vi_ybuf
; /* current yank buffer */
30 static int vi_pcol
; /* the column requested by | command */
31 static int vi_printed
; /* ex_print() calls since the last command */
32 static int vi_scroll
; /* scroll amount for ^f and ^d*/
33 static int vi_soset
, vi_so
; /* search offset; 1 in "/kw/1" */
35 static void vi_wait(void)
38 free(ex_read("[enter to continue]"));
44 static void vi_drawmsg(void)
48 led_print(vi_msg
, xrows
);
53 static void vi_drawrow(int row
)
55 char *s
= lbuf_get(xb
, row
);
56 led_print(s
? s
: (row
? "~" : ""), row
- xtop
);
59 /* redraw the screen */
60 static void vi_drawagain(int xcol
, int lineonly
)
64 for (i
= xtop
; i
< xtop
+ xrows
; i
++)
65 if (!lineonly
|| i
== xrow
)
68 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
72 /* update the screen */
73 static void vi_drawupdate(int xcol
, int otop
)
79 term_room(otop
- xtop
);
81 int n
= MIN(xtop
- otop
, xrows
);
82 for (i
= 0; i
< n
; i
++)
83 vi_drawrow(xtop
+ xrows
- n
+ i
);
85 int n
= MIN(otop
- xtop
, xrows
);
86 for (i
= 0; i
< n
; i
++)
89 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
93 term_pos(xrow
, led_pos(lbuf_get(xb
, i
), xcol
));
96 /* update the screen by removing lines r1 to r2 before an input command */
97 static void vi_drawrm(int r1
, int r2
, int newln
)
99 r1
= MIN(MAX(r1
, xtop
), xtop
+ xrows
);
100 r2
= MIN(MAX(r2
, xtop
), xtop
+ xrows
);
101 term_pos(r1
- xtop
, 0);
102 term_room(r1
- r2
+ newln
);
105 static int vi_buf
[128];
106 static int vi_buflen
;
108 static int vi_read(void)
110 return vi_buflen
? vi_buf
[--vi_buflen
] : term_read();
113 static void vi_back(int c
)
115 if (vi_buflen
< sizeof(vi_buf
))
116 vi_buf
[vi_buflen
++] = c
;
119 static char *vi_char(void)
121 return led_read(ex_kmap());
124 static char *vi_prompt(char *msg
, char **kmap
)
127 term_pos(xrows
, led_pos(msg
, 0));
129 s
= led_prompt(msg
, "", kmap
);
132 r
= uc_dup(strlen(s
) >= strlen(msg
) ? s
+ strlen(msg
) : s
);
137 /* read an ex input line */
138 char *ex_read(char *msg
)
143 char *s
= led_prompt(msg
, "", ex_kmap());
149 while ((c
= getchar()) != EOF
&& c
!= '\n')
155 return sbuf_done(sb
);
158 /* show an ex message */
159 void ex_show(char *msg
)
162 snprintf(vi_msg
, sizeof(vi_msg
), "%s", msg
);
171 /* print an ex output line */
172 void ex_print(char *line
)
175 vi_printed
+= line
? 1 : 2;
177 snprintf(vi_msg
, sizeof(vi_msg
), "%s", line
);
187 static int vi_yankbuf(void)
196 static int vi_prefix(void)
200 if ((c
>= '1' && c
<= '9')) {
202 n
= n
* 10 + c
- '0';
210 static int vi_col2off(struct lbuf
*lb
, int row
, int col
)
212 char *ln
= lbuf_get(lb
, row
);
213 return ln
? ren_off(ln
, col
) : 0;
216 static int vi_off2col(struct lbuf
*lb
, int row
, int off
)
218 char *ln
= lbuf_get(lb
, row
);
219 return ln
? ren_pos(ln
, off
) : 0;
222 static int vi_nextoff(struct lbuf
*lb
, int dir
, int *row
, int *off
)
225 if (o
< 0 || !lbuf_get(lb
, *row
) || o
>= uc_slen(lbuf_get(lb
, *row
)))
231 static int vi_nextcol(struct lbuf
*lb
, int dir
, int *row
, int *off
)
233 char *ln
= lbuf_get(lb
, *row
);
234 int col
= ln
? ren_pos(ln
, *off
) : 0;
235 int o
= ln
? ren_next(ln
, col
, dir
) : -1;
238 *off
= ren_off(ln
, o
);
242 static int vi_findchar(struct lbuf
*lb
, char *cs
, int cmd
, int n
, int *row
, int *off
)
244 strcpy(vi_charlast
, cs
);
246 return lbuf_findchar(lb
, cs
, cmd
, n
, row
, off
);
249 static int vi_search(int cmd
, int cnt
, int *row
, int *off
)
257 if (cmd
== '/' || cmd
== '?') {
258 char sign
[4] = {cmd
};
260 char *kw
= vi_prompt(sign
, ex_kmap());
269 if ((re
= re_read(&kw
))) {
270 ex_kwdset(re
[0] ? re
: NULL
, cmd
== '/' ? +1 : -1);
279 if (!lbuf_len(xb
) || ex_kwd(&kwd
, &dir
))
281 dir
= cmd
== 'N' ? -dir
: dir
;
283 for (i
= 0; i
< cnt
; i
++) {
284 if (lbuf_search(xb
, kwd
, dir
, &r
, &o
, &len
)) {
288 if (i
+ 1 < cnt
&& cmd
== '/')
296 if (*row
+ vi_so
< 0 || *row
+ vi_so
>= lbuf_len(xb
))
303 snprintf(vi_msg
, sizeof(vi_msg
), "\"%s\" not found\n", kwd
);
307 /* read a line motion */
308 static int vi_motionln(int *row
, int cmd
)
310 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
312 int mark
, mark_row
, mark_off
;
316 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
319 *row
= MAX(*row
- cnt
, 0);
322 *row
= MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1);
325 if ((mark
= vi_read()) <= 0)
327 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
332 *row
= MIN(*row
+ cnt
, lbuf_len(xb
) - 1);
335 *row
= MAX(*row
- cnt
, 0);
338 *row
= (vi_arg1
|| vi_arg2
) ? cnt
- 1 : lbuf_len(xb
) - 1;
342 *row
= MIN(xtop
+ cnt
- 1, lbuf_len(xb
) - 1);
348 *row
= MIN(xtop
+ xrows
- 1 - cnt
+ 1, lbuf_len(xb
) - 1);
354 *row
= MIN(xtop
+ xrows
/ 2, lbuf_len(xb
) - 1);
360 *row
= MAX(0, MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1));
363 if (c
== '%' && (vi_arg1
|| vi_arg2
)) {
366 *row
= MAX(0, lbuf_len(xb
) - 1) * cnt
/ 100;
375 static char *vi_curword(struct lbuf
*lb
, int row
, int off
)
378 char *ln
= lbuf_get(lb
, row
);
382 beg
= uc_chr(ln
, ren_noeol(ln
, off
));
384 while (*end
&& uc_kind(end
) == 1)
386 while (beg
> ln
&& uc_kind(uc_beg(ln
, beg
- 1)) == 1)
387 beg
= uc_beg(ln
, beg
- 1);
392 sbuf_mem(sb
, beg
, end
- beg
);
394 return sbuf_done(sb
);
398 static int vi_motion(int *row
, int *off
)
400 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
401 char *ln
= lbuf_get(xb
, *row
);
402 int dir
= dir_context(ln
? ln
: "");
403 int mark
, mark_row
, mark_off
;
407 if ((mv
= vi_motionln(row
, 0))) {
414 if (!(cs
= vi_char()))
416 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
420 if (!(cs
= vi_char()))
422 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
428 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, cnt
, row
, off
))
434 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, -cnt
, row
, off
))
438 for (i
= 0; i
< cnt
; i
++)
439 if (vi_nextcol(xb
, -1 * dir
, row
, off
))
443 for (i
= 0; i
< cnt
; i
++)
444 if (vi_nextcol(xb
, +1 * dir
, row
, off
))
448 if (!(cs
= vi_char()))
450 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
454 if (!(cs
= vi_char()))
456 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
460 for (i
= 0; i
< cnt
; i
++)
461 if (lbuf_wordend(xb
, 1, -1, row
, off
))
465 for (i
= 0; i
< cnt
; i
++)
466 if (lbuf_wordend(xb
, 1, +1, row
, off
))
470 for (i
= 0; i
< cnt
; i
++)
471 if (lbuf_wordbeg(xb
, 1, +1, row
, off
))
475 for (i
= 0; i
< cnt
; i
++)
476 if (lbuf_wordend(xb
, 0, -1, row
, off
))
480 for (i
= 0; i
< cnt
; i
++)
481 if (lbuf_wordend(xb
, 0, +1, row
, off
))
485 for (i
= 0; i
< cnt
; i
++)
486 if (lbuf_wordbeg(xb
, 0, +1, row
, off
))
490 for (i
= 0; i
< cnt
; i
++)
491 if (lbuf_paragraphbeg(xb
, -1, row
, off
))
495 for (i
= 0; i
< cnt
; i
++)
496 if (lbuf_paragraphbeg(xb
, +1, row
, off
))
500 if (vi_read() != '[')
502 for (i
= 0; i
< cnt
; i
++)
503 if (lbuf_sectionbeg(xb
, -1, row
, off
))
507 if (vi_read() != ']')
509 for (i
= 0; i
< cnt
; i
++)
510 if (lbuf_sectionbeg(xb
, +1, row
, off
))
517 *off
= lbuf_indents(xb
, *row
);
520 *off
= lbuf_eol(xb
, *row
);
523 *off
= vi_col2off(xb
, *row
, cnt
- 1);
527 if (vi_search(mv
, cnt
, row
, off
))
531 if (vi_search(mv
, cnt
, row
, off
))
535 if (vi_search(mv
, cnt
, row
, off
))
539 if (vi_search(mv
, cnt
, row
, off
))
543 if (!(cs
= vi_curword(xb
, *row
, *off
)))
547 if (vi_search('n', cnt
, row
, off
))
551 for (i
= 0; i
< cnt
; i
++)
552 if (vi_nextoff(xb
, +1, row
, off
))
557 for (i
= 0; i
< cnt
; i
++)
558 if (vi_nextoff(xb
, -1, row
, off
))
562 if ((mark
= vi_read()) <= 0)
564 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
570 if (lbuf_pair(xb
, row
, off
))
580 static void swap(int *a
, int *b
)
587 static char *lbuf_region(struct lbuf
*lb
, int r1
, int o1
, int r2
, int o2
)
592 return uc_sub(lbuf_get(lb
, r1
), o1
, o2
);
594 s1
= uc_sub(lbuf_get(lb
, r1
), o1
, -1);
595 s3
= uc_sub(lbuf_get(lb
, r2
), 0, o2
);
596 s2
= lbuf_cp(lb
, r1
+ 1, r2
);
603 return sbuf_done(sb
);
606 static void vi_yank(int r1
, int o1
, int r2
, int o2
, int lnmode
)
609 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
610 reg_put(vi_ybuf
, region
, lnmode
);
613 xoff
= lnmode
? xoff
: o1
;
616 static void vi_delete(int r1
, int o1
, int r2
, int o2
, int lnmode
)
620 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
621 reg_put(vi_ybuf
, region
, lnmode
);
623 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
624 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
626 struct sbuf
*sb
= sbuf_make();
629 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
632 lbuf_edit(xb
, NULL
, r1
, r2
+ 1);
635 xoff
= lnmode
? lbuf_indents(xb
, xrow
) : o1
;
640 static int linecount(char *s
)
644 if ((s
= strchr(s
, '\n')))
649 static int charcount(char *text
, char *post
)
651 int tlen
= strlen(text
);
652 int plen
= strlen(post
);
657 for (i
= 0; i
< tlen
- plen
; i
++)
660 return uc_slen(nl
) - uc_slen(post
);
663 static char *vi_input(char *pref
, char *post
, int *row
, int *off
)
665 char *rep
= led_input(pref
, post
, ex_kmap());
668 *row
= linecount(rep
) - 1;
669 *off
= charcount(rep
, post
) - 1;
675 static char *vi_indents(char *ln
)
677 struct sbuf
*sb
= sbuf_make();
678 while (xai
&& ln
&& (*ln
== ' ' || *ln
== '\t'))
680 return sbuf_done(sb
);
683 static void vi_change(int r1
, int o1
, int r2
, int o2
, int lnmode
)
689 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
690 reg_put(vi_ybuf
, region
, lnmode
);
692 pref
= lnmode
? vi_indents(lbuf_get(xb
, r1
)) : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
693 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
694 vi_drawrm(r1
, r2
, 0);
695 rep
= vi_input(pref
, post
, &row
, &off
);
697 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
706 static void vi_case(int r1
, int o1
, int r2
, int o2
, int lnmode
, int cmd
)
710 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
713 int c
= (unsigned char) s
[0];
720 s
[0] = islower(c
) ? toupper(c
) : tolower(c
);
724 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
725 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
727 struct sbuf
*sb
= sbuf_make();
729 sbuf_str(sb
, region
);
731 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
734 lbuf_edit(xb
, region
, r1
, r2
+ 1);
737 xoff
= lnmode
? lbuf_indents(xb
, r2
) : o2
;
743 static void vi_pipe(int r1
, int r2
)
748 char *cmd
= vi_prompt("!", &kmap
);
751 text
= lbuf_cp(xb
, r1
, r2
+ 1);
752 rep
= cmd_pipe(cmd
, text
, 1, 1);
754 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
760 static void vi_shift(int r1
, int r2
, int dir
)
765 for (i
= r1
; i
<= r2
; i
++) {
766 if (!(ln
= lbuf_get(xb
, i
)))
772 ln
= ln
[0] == ' ' || ln
[0] == '\t' ? ln
+ 1 : ln
;
774 lbuf_edit(xb
, sbuf_buf(sb
), i
, i
+ 1);
778 xoff
= lbuf_indents(xb
, xrow
);
781 static int vc_motion(int cmd
)
783 int r1
= xrow
, r2
= xrow
; /* region rows */
784 int o1
= xoff
, o2
= xoff
; /* visual region columns */
785 int lnmode
= 0; /* line-based region */
787 vi_arg2
= vi_prefix();
790 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
792 if ((mv
= vi_motionln(&r2
, cmd
))) {
794 } else if (!(mv
= vi_motion(&r2
, &o2
))) {
803 o2
= lbuf_eol(xb
, r2
);
809 if (r1
== r2
&& o1
> o2
)
811 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
812 if (!lnmode
&& strchr("fFtTeE%", mv
))
813 if (o2
< lbuf_eol(xb
, r2
))
814 o2
= ren_noeol(lbuf_get(xb
, r2
), o2
) + 1;
816 vi_yank(r1
, o1
, r2
, o2
, lnmode
);
818 vi_delete(r1
, o1
, r2
, o2
, lnmode
);
820 vi_change(r1
, o1
, r2
, o2
, lnmode
);
821 if (cmd
== '~' || cmd
== 'u' || cmd
== 'U')
822 vi_case(r1
, o1
, r2
, o2
, lnmode
, cmd
);
825 if (cmd
== '>' || cmd
== '<')
826 vi_shift(r1
, r2
, cmd
== '>' ? +1 : -1);
830 static int vc_insert(int cmd
)
833 char *ln
= lbuf_get(xb
, xrow
);
837 xoff
= lbuf_indents(xb
, xrow
);
839 xoff
= lbuf_eol(xb
, xrow
);
840 xoff
= ren_noeol(ln
, xoff
);
843 if (cmd
== 'i' || cmd
== 'I')
845 if (cmd
== 'a' || cmd
== 'A')
847 if (ln
&& ln
[0] == '\n')
849 pref
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, 0, off
) : vi_indents(ln
);
850 post
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, off
, -1) : uc_dup("\n");
851 vi_drawrm(xrow
, xrow
, cmd
== 'o' || cmd
== 'O');
852 rep
= vi_input(pref
, post
, &row
, &off
);
853 if ((cmd
== 'o' || cmd
== 'O') && !lbuf_len(xb
))
854 lbuf_edit(xb
, "\n", 0, 0);
856 lbuf_edit(xb
, rep
, xrow
, xrow
+ (cmd
!= 'o' && cmd
!= 'O'));
866 static int vc_put(int cmd
)
868 int cnt
= MAX(1, vi_arg1
);
870 char *buf
= reg_get(vi_ybuf
, &lnmode
);
873 snprintf(vi_msg
, sizeof(vi_msg
), "yank buffer empty\n");
877 struct sbuf
*sb
= sbuf_make();
878 for (i
= 0; i
< cnt
; i
++)
881 lbuf_edit(xb
, "\n", 0, 0);
884 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
);
885 xoff
= lbuf_indents(xb
, xrow
);
888 struct sbuf
*sb
= sbuf_make();
889 char *ln
= xrow
< lbuf_len(xb
) ? lbuf_get(xb
, xrow
) : "\n";
890 int off
= ren_noeol(ln
, xoff
) + (ln
[0] != '\n' && cmd
== 'p');
891 char *s
= uc_sub(ln
, 0, off
);
894 for (i
= 0; i
< cnt
; i
++)
896 s
= uc_sub(ln
, off
, -1);
899 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
900 xoff
= off
+ uc_slen(buf
) * cnt
- 1;
906 static int join_spaces(char *prev
, char *next
)
908 int prevlen
= strlen(prev
);
911 if (prev
[prevlen
- 1] == ' ' || next
[0] == ')')
913 return prev
[prevlen
- 1] == '.' ? 2 : 1;
916 static int vc_join(void)
919 int cnt
= vi_arg1
<= 1 ? 2 : vi_arg1
;
921 int end
= xrow
+ cnt
;
924 if (!lbuf_get(xb
, beg
) || !lbuf_get(xb
, end
- 1))
927 for (i
= beg
; i
< end
; i
++) {
928 char *ln
= lbuf_get(xb
, i
);
929 char *lnend
= strchr(ln
, '\n');
932 while (ln
[0] == ' ' || ln
[0] == '\t')
934 spaces
= i
> beg
? join_spaces(sbuf_buf(sb
), ln
) : 0;
935 off
= uc_slen(sbuf_buf(sb
));
938 sbuf_mem(sb
, ln
, lnend
- ln
);
941 lbuf_edit(xb
, sbuf_buf(sb
), beg
, end
);
947 static int vi_scrollforeward(int cnt
)
949 if (xtop
>= lbuf_len(xb
) - 1)
951 xtop
= MIN(lbuf_len(xb
) - 1, xtop
+ cnt
);
952 xrow
= MAX(xrow
, xtop
);
956 static int vi_scrollbackward(int cnt
)
960 xtop
= MAX(0, xtop
- cnt
);
961 xrow
= MIN(xrow
, xtop
+ xrows
- 1);
965 static void vc_status(void)
967 int col
= vi_off2col(xb
, xrow
, xoff
);
968 snprintf(vi_msg
, sizeof(vi_msg
),
969 "\"%s\"%c %d lines L%d C%d\n",
970 ex_path()[0] ? ex_path() : "unnamed",
971 lbuf_modified(xb
) ? '*' : ' ',
972 lbuf_len(xb
), xrow
+ 1,
973 ren_cursor(lbuf_get(xb
, xrow
), col
) + 1);
976 static void vc_charinfo(void)
978 char *c
= uc_chr(lbuf_get(xb
, xrow
), xoff
);
981 memcpy(cbuf
, c
, uc_len(c
));
982 snprintf(vi_msg
, sizeof(vi_msg
), "<%s> %04x\n", cbuf
, uc_code(c
));
986 static int vc_replace(void)
988 int cnt
= MAX(1, vi_arg1
);
989 char *cs
= vi_char();
990 char *ln
= lbuf_get(xb
, xrow
);
997 off
= ren_noeol(ln
, xoff
);
999 for (i
= 0; s
[0] != '\n' && i
< cnt
; i
++)
1003 pref
= uc_sub(ln
, 0, off
);
1004 post
= uc_sub(ln
, off
+ cnt
, -1);
1007 for (i
= 0; i
< cnt
; i
++)
1010 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
1019 static char rep_cmd
[4096]; /* the last command */
1022 static void vc_repeat(void)
1025 for (i
= 0; i
< MAX(1, vi_arg1
); i
++)
1026 term_push(rep_cmd
, rep_len
);
1029 static void vc_execute(void)
1031 static int exec_buf
;
1041 buf
= reg_get(exec_buf
, &lnmode
);
1043 for (i
= 0; i
< MAX(1, vi_arg1
); i
++)
1044 term_push(buf
, strlen(buf
));
1047 static void vi(void)
1053 xtop
= MAX(0, xrow
- xrows
/ 2);
1055 xcol
= vi_off2col(xb
, xrow
, xoff
);
1056 vi_drawagain(xcol
, 0);
1057 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
), xcol
));
1059 int mod
= 0; /* screen should be redrawn */
1061 int noff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1067 vi_ybuf
= vi_yankbuf();
1068 vi_arg1
= vi_prefix();
1070 vi_ybuf
= vi_yankbuf();
1071 mv
= vi_motion(&nrow
, &noff
);
1073 if (strchr("\'`GHML/?{}[]nN", mv
) ||
1074 (mv
== '%' && noff
< 0)) {
1075 lbuf_mark(xb
, '\'', xrow
, xoff
);
1076 lbuf_mark(xb
, '`', xrow
, xoff
);
1079 if (noff
< 0 && !strchr("jk", mv
))
1080 noff
= lbuf_indents(xb
, xrow
);
1081 if (strchr("jk", mv
))
1082 noff
= vi_col2off(xb
, xrow
, xcol
);
1083 xoff
= ren_noeol(lbuf_get(xb
, xrow
), noff
);
1084 if (!strchr("|jk", mv
))
1085 xcol
= vi_off2col(xb
, xrow
, xoff
);
1088 } else if (mv
== 0) {
1094 lbuf_mark(xb
, '*', xrow
, xoff
);
1097 if (vi_scrollbackward(MAX(1, vi_arg1
) * (xrows
- 1)))
1099 xoff
= lbuf_indents(xb
, xrow
);
1102 if (vi_scrollforeward(MAX(1, vi_arg1
) * (xrows
- 1)))
1104 xoff
= lbuf_indents(xb
, xrow
);
1107 if (vi_scrollforeward(MAX(1, vi_arg1
)))
1111 if (vi_scrollbackward(MAX(1, vi_arg1
)))
1118 vi_scroll
= vi_arg1
;
1119 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1120 xrow
= MAX(0, xrow
- n
);
1122 xtop
= MAX(0, xtop
- n
);
1123 xoff
= lbuf_indents(xb
, xrow
);
1126 if (xrow
== lbuf_len(xb
) - 1)
1129 vi_scroll
= vi_arg1
;
1130 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1131 xrow
= MIN(MAX(0, lbuf_len(xb
) - 1), xrow
+ n
);
1132 if (xtop
< lbuf_len(xb
) - xrows
)
1133 xtop
= MIN(lbuf_len(xb
) - xrows
, xtop
+ n
);
1134 xoff
= lbuf_indents(xb
, xrow
);
1142 if (!lbuf_undo(xb
)) {
1143 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1146 snprintf(vi_msg
, sizeof(vi_msg
), "undo failed\n");
1150 if (!lbuf_redo(xb
)) {
1151 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1154 snprintf(vi_msg
, sizeof(vi_msg
), "redo failed\n");
1165 ln
= vi_prompt(":", &kmap
);
1202 if ((mark
= vi_read()) > 0 && islower(mark
))
1203 lbuf_mark(xb
, mark
, xrow
, xoff
);
1214 xtop
= vi_arg1
? vi_arg1
: xrow
;
1217 n
= vi_arg1
? vi_arg1
: xrow
;
1218 xtop
= MAX(0, n
- xrows
/ 2);
1221 n
= vi_arg1
? vi_arg1
: xrow
;
1222 xtop
= MAX(0, n
- xrows
+ 1);
1226 xdir
= k
== 'r' ? -1 : +1;
1230 xdir
= k
== 'R' ? -2 : +2;
1237 if (k
== '~' || k
== 'u' || k
== 'U')
1245 if (!vc_motion('d'))
1249 vi_back(TK_CTL('h'));
1250 if (!vc_motion('d'))
1255 if (!vc_motion('c'))
1260 if (!vc_motion('d'))
1269 if (!vc_motion('c'))
1274 if (!vc_motion('c'))
1288 if (!vc_motion('~'))
1301 if (strchr("!<>ACDIJOPRSXYacdioprsxy~", c
)) {
1302 if (n
< sizeof(rep_cmd
)) {
1303 memcpy(rep_cmd
, cmd
, n
);
1308 if (xrow
< 0 || xrow
>= lbuf_len(xb
))
1309 xrow
= lbuf_len(xb
) ? lbuf_len(xb
) - 1 : 0;
1311 xtop
= xtop
- xrows
/ 2 > xrow
?
1312 MAX(0, xrow
- xrows
/ 2) : xrow
;
1313 if (xtop
+ xrows
<= xrow
)
1314 xtop
= xtop
+ xrows
+ xrows
/ 2 <= xrow
?
1315 xrow
- xrows
/ 2 : xrow
- xrows
+ 1;
1316 xoff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1318 xcol
= vi_off2col(xb
, xrow
, xoff
);
1319 if (xcol
>= xleft
+ xcols
)
1320 xleft
= xcol
- xcols
/ 2;
1322 xleft
= xcol
< xcols
? 0 : xcol
- xcols
/ 2;
1324 if (mod
|| xleft
!= oleft
)
1325 vi_drawagain(xcol
, mod
== 2 && xleft
== oleft
);
1326 else if (xtop
!= otop
)
1327 vi_drawupdate(xcol
, otop
);
1330 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
),
1331 ren_cursor(lbuf_get(xb
, xrow
), xcol
)));
1338 int main(int argc
, char *argv
[])
1341 char *prog
= strchr(argv
[0], '/') ? strrchr(argv
[0], '/') + 1 : argv
[0];
1342 xvis
= strcmp("ex", prog
) && strcmp("neatex", prog
);
1343 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1344 if (argv
[i
][1] == 's')
1346 if (argv
[i
][1] == 'e')
1348 if (argv
[i
][1] == 'v')
1355 if (!ex_init(argv
+ i
)) {