4 * Copyright (C) 2015-2017 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_printmsg(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(&xkmap
);
124 static char *vi_prompt(char *msg
, int *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
, "", &xkmap
);
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
, &xkmap
);
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;
341 *row
= MIN(xtop
+ cnt
- 1, lbuf_len(xb
) - 1);
344 *row
= MIN(xtop
+ xrows
- 1 - cnt
+ 1, lbuf_len(xb
) - 1);
347 *row
= MIN(xtop
+ xrows
/ 2, lbuf_len(xb
) - 1);
351 *row
= MIN(*row
+ cnt
- 1, lbuf_len(xb
) - 1);
354 if (c
== '%' && (vi_arg1
|| vi_arg2
)) {
357 *row
= MAX(0, lbuf_len(xb
) - 1) * cnt
/ 100;
368 static char *vi_curword(struct lbuf
*lb
, int row
, int off
)
371 char *ln
= lbuf_get(lb
, row
);
375 beg
= uc_chr(ln
, ren_noeol(ln
, off
));
377 while (*end
&& uc_kind(end
) == 1)
379 while (beg
> ln
&& uc_kind(uc_beg(ln
, beg
- 1)) == 1)
380 beg
= uc_beg(ln
, beg
- 1);
385 sbuf_mem(sb
, beg
, end
- beg
);
387 return sbuf_done(sb
);
391 static int vi_motion(int *row
, int *off
)
393 int cnt
= (vi_arg1
? vi_arg1
: 1) * (vi_arg2
? vi_arg2
: 1);
394 char *ln
= lbuf_get(xb
, *row
);
395 int dir
= dir_context(ln
? ln
: "");
396 int mark
, mark_row
, mark_off
;
400 if ((mv
= vi_motionln(row
, 0))) {
407 if (!(cs
= vi_char()))
409 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
413 if (!(cs
= vi_char()))
415 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
421 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, cnt
, row
, off
))
427 if (vi_findchar(xb
, vi_charlast
, vi_charcmd
, -cnt
, row
, off
))
431 for (i
= 0; i
< cnt
; i
++)
432 if (vi_nextcol(xb
, -1 * dir
, row
, off
))
436 for (i
= 0; i
< cnt
; i
++)
437 if (vi_nextcol(xb
, +1 * dir
, row
, off
))
441 if (!(cs
= vi_char()))
443 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
447 if (!(cs
= vi_char()))
449 if (vi_findchar(xb
, cs
, mv
, cnt
, row
, off
))
453 for (i
= 0; i
< cnt
; i
++)
454 if (lbuf_wordend(xb
, 1, -1, row
, off
))
458 for (i
= 0; i
< cnt
; i
++)
459 if (lbuf_wordend(xb
, 1, +1, row
, off
))
463 for (i
= 0; i
< cnt
; i
++)
464 if (lbuf_wordbeg(xb
, 1, +1, row
, off
))
468 for (i
= 0; i
< cnt
; i
++)
469 if (lbuf_wordend(xb
, 0, -1, row
, off
))
473 for (i
= 0; i
< cnt
; i
++)
474 if (lbuf_wordend(xb
, 0, +1, row
, off
))
478 for (i
= 0; i
< cnt
; i
++)
479 if (lbuf_wordbeg(xb
, 0, +1, row
, off
))
483 for (i
= 0; i
< cnt
; i
++)
484 if (lbuf_paragraphbeg(xb
, -1, row
, off
))
488 for (i
= 0; i
< cnt
; i
++)
489 if (lbuf_paragraphbeg(xb
, +1, row
, off
))
493 if (vi_read() != '[')
495 for (i
= 0; i
< cnt
; i
++)
496 if (lbuf_sectionbeg(xb
, -1, row
, off
))
500 if (vi_read() != ']')
502 for (i
= 0; i
< cnt
; i
++)
503 if (lbuf_sectionbeg(xb
, +1, row
, off
))
510 *off
= lbuf_indents(xb
, *row
);
513 *off
= lbuf_eol(xb
, *row
);
516 *off
= vi_col2off(xb
, *row
, cnt
- 1);
520 if (vi_search(mv
, cnt
, row
, off
))
524 if (vi_search(mv
, cnt
, row
, off
))
528 if (vi_search(mv
, cnt
, row
, off
))
532 if (vi_search(mv
, cnt
, row
, off
))
536 if (!(cs
= vi_curword(xb
, *row
, *off
)))
540 if (vi_search('n', cnt
, row
, off
))
544 for (i
= 0; i
< cnt
; i
++)
545 if (vi_nextoff(xb
, +1, row
, off
))
550 for (i
= 0; i
< cnt
; i
++)
551 if (vi_nextoff(xb
, -1, row
, off
))
555 if ((mark
= vi_read()) <= 0)
557 if (lbuf_jump(xb
, mark
, &mark_row
, &mark_off
))
563 if (lbuf_pair(xb
, row
, off
))
573 static void swap(int *a
, int *b
)
580 static char *lbuf_region(struct lbuf
*lb
, int r1
, int o1
, int r2
, int o2
)
585 return uc_sub(lbuf_get(lb
, r1
), o1
, o2
);
587 s1
= uc_sub(lbuf_get(lb
, r1
), o1
, -1);
588 s3
= uc_sub(lbuf_get(lb
, r2
), 0, o2
);
589 s2
= lbuf_cp(lb
, r1
+ 1, r2
);
596 return sbuf_done(sb
);
599 static void vi_yank(int r1
, int o1
, int r2
, int o2
, int lnmode
)
602 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
603 reg_put(vi_ybuf
, region
, lnmode
);
606 xoff
= lnmode
? xoff
: o1
;
609 static void vi_delete(int r1
, int o1
, int r2
, int o2
, int lnmode
)
613 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
614 reg_put(vi_ybuf
, region
, lnmode
);
616 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
617 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
619 struct sbuf
*sb
= sbuf_make();
622 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
625 lbuf_edit(xb
, NULL
, r1
, r2
+ 1);
628 xoff
= lnmode
? lbuf_indents(xb
, xrow
) : o1
;
633 static int linecount(char *s
)
637 if ((s
= strchr(s
, '\n')))
642 static int charcount(char *text
, char *post
)
644 int tlen
= strlen(text
);
645 int plen
= strlen(post
);
650 for (i
= 0; i
< tlen
- plen
; i
++)
653 return uc_slen(nl
) - uc_slen(post
);
656 static char *vi_input(char *pref
, char *post
, int *row
, int *off
)
658 char *rep
= led_input(pref
, post
, &xkmap
);
661 *row
= linecount(rep
) - 1;
662 *off
= charcount(rep
, post
) - 1;
668 static char *vi_indents(char *ln
)
670 struct sbuf
*sb
= sbuf_make();
671 while (xai
&& ln
&& (*ln
== ' ' || *ln
== '\t'))
673 return sbuf_done(sb
);
676 static void vi_change(int r1
, int o1
, int r2
, int o2
, int lnmode
)
682 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
683 reg_put(vi_ybuf
, region
, lnmode
);
685 pref
= lnmode
? vi_indents(lbuf_get(xb
, r1
)) : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
686 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
687 vi_drawrm(r1
, r2
, 0);
688 rep
= vi_input(pref
, post
, &row
, &off
);
690 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
699 static void vi_case(int r1
, int o1
, int r2
, int o2
, int lnmode
, int cmd
)
703 region
= lbuf_region(xb
, r1
, lnmode
? 0 : o1
, r2
, lnmode
? -1 : o2
);
706 int c
= (unsigned char) s
[0];
713 s
[0] = islower(c
) ? toupper(c
) : tolower(c
);
717 pref
= lnmode
? uc_dup("") : uc_sub(lbuf_get(xb
, r1
), 0, o1
);
718 post
= lnmode
? uc_dup("\n") : uc_sub(lbuf_get(xb
, r2
), o2
, -1);
720 struct sbuf
*sb
= sbuf_make();
722 sbuf_str(sb
, region
);
724 lbuf_edit(xb
, sbuf_buf(sb
), r1
, r2
+ 1);
727 lbuf_edit(xb
, region
, r1
, r2
+ 1);
730 xoff
= lnmode
? lbuf_indents(xb
, r2
) : o2
;
736 static void vi_pipe(int r1
, int r2
)
741 char *cmd
= vi_prompt("!", &kmap
);
744 text
= lbuf_cp(xb
, r1
, r2
+ 1);
745 rep
= cmd_pipe(cmd
, text
, 1, 1);
747 lbuf_edit(xb
, rep
, r1
, r2
+ 1);
753 static void vi_shift(int r1
, int r2
, int dir
)
758 for (i
= r1
; i
<= r2
; i
++) {
759 if (!(ln
= lbuf_get(xb
, i
)))
765 ln
= ln
[0] == ' ' || ln
[0] == '\t' ? ln
+ 1 : ln
;
767 lbuf_edit(xb
, sbuf_buf(sb
), i
, i
+ 1);
771 xoff
= lbuf_indents(xb
, xrow
);
774 static int vc_motion(int cmd
)
776 int r1
= xrow
, r2
= xrow
; /* region rows */
777 int o1
= xoff
, o2
= xoff
; /* visual region columns */
778 int lnmode
= 0; /* line-based region */
780 vi_arg2
= vi_prefix();
783 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
785 if ((mv
= vi_motionln(&r2
, cmd
))) {
787 } else if (!(mv
= vi_motion(&r2
, &o2
))) {
796 o2
= lbuf_eol(xb
, r2
);
802 if (r1
== r2
&& o1
> o2
)
804 o1
= ren_noeol(lbuf_get(xb
, r1
), o1
);
805 if (!lnmode
&& strchr("fFtTeE%", mv
))
806 if (o2
< lbuf_eol(xb
, r2
))
807 o2
= ren_noeol(lbuf_get(xb
, r2
), o2
) + 1;
809 vi_yank(r1
, o1
, r2
, o2
, lnmode
);
811 vi_delete(r1
, o1
, r2
, o2
, lnmode
);
813 vi_change(r1
, o1
, r2
, o2
, lnmode
);
814 if (cmd
== '~' || cmd
== 'u' || cmd
== 'U')
815 vi_case(r1
, o1
, r2
, o2
, lnmode
, cmd
);
818 if (cmd
== '>' || cmd
== '<')
819 vi_shift(r1
, r2
, cmd
== '>' ? +1 : -1);
823 static int vc_insert(int cmd
)
826 char *ln
= lbuf_get(xb
, xrow
);
830 xoff
= lbuf_indents(xb
, xrow
);
832 xoff
= lbuf_eol(xb
, xrow
);
833 xoff
= ren_noeol(ln
, xoff
);
836 if (cmd
== 'i' || cmd
== 'I')
838 if (cmd
== 'a' || cmd
== 'A')
840 if (ln
&& ln
[0] == '\n')
842 pref
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, 0, off
) : vi_indents(ln
);
843 post
= ln
&& cmd
!= 'o' && cmd
!= 'O' ? uc_sub(ln
, off
, -1) : uc_dup("\n");
844 vi_drawrm(xrow
, xrow
, cmd
== 'o' || cmd
== 'O');
845 rep
= vi_input(pref
, post
, &row
, &off
);
846 if ((cmd
== 'o' || cmd
== 'O') && !lbuf_len(xb
))
847 lbuf_edit(xb
, "\n", 0, 0);
849 lbuf_edit(xb
, rep
, xrow
, xrow
+ (cmd
!= 'o' && cmd
!= 'O'));
859 static int vc_put(int cmd
)
861 int cnt
= MAX(1, vi_arg1
);
863 char *buf
= reg_get(vi_ybuf
, &lnmode
);
866 snprintf(vi_msg
, sizeof(vi_msg
), "yank buffer empty\n");
870 struct sbuf
*sb
= sbuf_make();
871 for (i
= 0; i
< cnt
; i
++)
874 lbuf_edit(xb
, "\n", 0, 0);
877 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
);
878 xoff
= lbuf_indents(xb
, xrow
);
881 struct sbuf
*sb
= sbuf_make();
882 char *ln
= xrow
< lbuf_len(xb
) ? lbuf_get(xb
, xrow
) : "\n";
883 int off
= ren_noeol(ln
, xoff
) + (ln
[0] != '\n' && cmd
== 'p');
884 char *s
= uc_sub(ln
, 0, off
);
887 for (i
= 0; i
< cnt
; i
++)
889 s
= uc_sub(ln
, off
, -1);
892 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
893 xoff
= off
+ uc_slen(buf
) * cnt
- 1;
899 static int join_spaces(char *prev
, char *next
)
901 int prevlen
= strlen(prev
);
904 if (prev
[prevlen
- 1] == ' ' || next
[0] == ')')
906 return prev
[prevlen
- 1] == '.' ? 2 : 1;
909 static int vc_join(void)
912 int cnt
= vi_arg1
<= 1 ? 2 : vi_arg1
;
914 int end
= xrow
+ cnt
;
917 if (!lbuf_get(xb
, beg
) || !lbuf_get(xb
, end
- 1))
920 for (i
= beg
; i
< end
; i
++) {
921 char *ln
= lbuf_get(xb
, i
);
922 char *lnend
= strchr(ln
, '\n');
925 while (ln
[0] == ' ' || ln
[0] == '\t')
927 spaces
= i
> beg
? join_spaces(sbuf_buf(sb
), ln
) : 0;
928 off
= uc_slen(sbuf_buf(sb
));
931 sbuf_mem(sb
, ln
, lnend
- ln
);
934 lbuf_edit(xb
, sbuf_buf(sb
), beg
, end
);
940 static int vi_scrollforeward(int cnt
)
942 if (xtop
>= lbuf_len(xb
) - 1)
944 xtop
= MIN(lbuf_len(xb
) - 1, xtop
+ cnt
);
945 xrow
= MAX(xrow
, xtop
);
949 static int vi_scrollbackward(int cnt
)
953 xtop
= MAX(0, xtop
- cnt
);
954 xrow
= MIN(xrow
, xtop
+ xrows
- 1);
958 static void vc_status(void)
960 int col
= vi_off2col(xb
, xrow
, xoff
);
961 snprintf(vi_msg
, sizeof(vi_msg
),
962 "\"%s\"%c %d lines L%d C%d\n",
963 ex_path()[0] ? ex_path() : "unnamed",
964 lbuf_modified(xb
) ? '*' : ' ',
965 lbuf_len(xb
), xrow
+ 1,
966 ren_cursor(lbuf_get(xb
, xrow
), col
) + 1);
969 static void vc_charinfo(void)
971 char *c
= uc_chr(lbuf_get(xb
, xrow
), xoff
);
974 memcpy(cbuf
, c
, uc_len(c
));
975 snprintf(vi_msg
, sizeof(vi_msg
), "<%s> %04x\n", cbuf
, uc_code(c
));
979 static int vc_replace(void)
981 int cnt
= MAX(1, vi_arg1
);
982 char *cs
= vi_char();
983 char *ln
= lbuf_get(xb
, xrow
);
990 off
= ren_noeol(ln
, xoff
);
992 for (i
= 0; s
[0] != '\n' && i
< cnt
; i
++)
996 pref
= uc_sub(ln
, 0, off
);
997 post
= uc_sub(ln
, off
+ cnt
, -1);
1000 for (i
= 0; i
< cnt
; i
++)
1003 lbuf_edit(xb
, sbuf_buf(sb
), xrow
, xrow
+ 1);
1012 static char rep_cmd
[4096]; /* the last command */
1015 static void vc_repeat(void)
1018 for (i
= 0; i
< MAX(1, vi_arg1
); i
++)
1019 term_push(rep_cmd
, rep_len
);
1022 static void vc_execute(void)
1024 static int exec_buf
;
1034 buf
= reg_get(exec_buf
, &lnmode
);
1036 for (i
= 0; i
< MAX(1, vi_arg1
); i
++)
1037 term_push(buf
, strlen(buf
));
1040 static void vi(void)
1046 xtop
= MAX(0, xrow
- xrows
/ 2);
1048 xcol
= vi_off2col(xb
, xrow
, xoff
);
1049 vi_drawagain(xcol
, 0);
1050 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
), xcol
));
1052 int mod
= 0; /* screen should be redrawn (1: current line, 2: the whole screen */
1054 int noff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1060 vi_ybuf
= vi_yankbuf();
1061 vi_arg1
= vi_prefix();
1063 vi_ybuf
= vi_yankbuf();
1064 mv
= vi_motion(&nrow
, &noff
);
1066 if (strchr("\'`GHML/?{}[]nN", mv
) ||
1067 (mv
== '%' && noff
< 0)) {
1068 lbuf_mark(xb
, '\'', xrow
, xoff
);
1069 lbuf_mark(xb
, '`', xrow
, xoff
);
1072 if (noff
< 0 && !strchr("jk", mv
))
1073 noff
= lbuf_indents(xb
, xrow
);
1074 if (strchr("jk", mv
))
1075 noff
= vi_col2off(xb
, xrow
, xcol
);
1076 xoff
= ren_noeol(lbuf_get(xb
, xrow
), noff
);
1077 if (!strchr("|jk", mv
))
1078 xcol
= vi_off2col(xb
, xrow
, xoff
);
1081 } else if (mv
== 0) {
1087 lbuf_mark(xb
, '*', xrow
, xoff
);
1090 if (vi_scrollbackward(MAX(1, vi_arg1
) * (xrows
- 1)))
1092 xoff
= lbuf_indents(xb
, xrow
);
1096 if (vi_scrollforeward(MAX(1, vi_arg1
) * (xrows
- 1)))
1098 xoff
= lbuf_indents(xb
, xrow
);
1102 if (vi_scrollforeward(MAX(1, vi_arg1
)))
1106 if (vi_scrollbackward(MAX(1, vi_arg1
)))
1113 vi_scroll
= vi_arg1
;
1114 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1115 xrow
= MAX(0, xrow
- n
);
1117 xtop
= MAX(0, xtop
- n
);
1118 xoff
= lbuf_indents(xb
, xrow
);
1122 if (xrow
== lbuf_len(xb
) - 1)
1125 vi_scroll
= vi_arg1
;
1126 n
= vi_scroll
? vi_scroll
: xrows
/ 2;
1127 xrow
= MIN(MAX(0, lbuf_len(xb
) - 1), xrow
+ n
);
1128 if (xtop
< lbuf_len(xb
) - xrows
)
1129 xtop
= MIN(lbuf_len(xb
) - xrows
, xtop
+ n
);
1130 xoff
= lbuf_indents(xb
, xrow
);
1139 if (!lbuf_undo(xb
)) {
1140 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1143 snprintf(vi_msg
, sizeof(vi_msg
), "undo failed\n");
1147 if (!lbuf_redo(xb
)) {
1148 lbuf_jump(xb
, '*', &xrow
, &xoff
);
1151 snprintf(vi_msg
, sizeof(vi_msg
), "redo failed\n");
1162 ln
= vi_prompt(":", &kmap
);
1199 if ((mark
= vi_read()) > 0 && islower(mark
))
1200 lbuf_mark(xb
, mark
, xrow
, xoff
);
1211 xtop
= vi_arg1
? vi_arg1
: xrow
;
1214 n
= vi_arg1
? vi_arg1
: xrow
;
1215 xtop
= MAX(0, n
- xrows
/ 2);
1218 n
= vi_arg1
? vi_arg1
: xrow
;
1219 xtop
= MAX(0, n
- xrows
+ 1);
1223 xtd
= k
== 'r' ? -1 : +1;
1227 xtd
= k
== 'R' ? -2 : +2;
1231 xkmap
= k
== 'e' ? 0 : xkmap_alt
;
1238 if (k
== '~' || k
== 'u' || k
== 'U')
1246 if (!vc_motion('d'))
1250 vi_back(TK_CTL('h'));
1251 if (!vc_motion('d'))
1256 if (!vc_motion('c'))
1261 if (!vc_motion('d'))
1270 if (!vc_motion('c'))
1275 if (!vc_motion('c'))
1289 if (!vc_motion('~'))
1302 if (strchr("!<>ACDIJOPRSXYacdioprsxy~", c
) ||
1303 c
== 'g' && strchr("uU~", k
)) {
1304 if (n
< sizeof(rep_cmd
)) {
1305 memcpy(rep_cmd
, cmd
, n
);
1310 if (xrow
< 0 || xrow
>= lbuf_len(xb
))
1311 xrow
= lbuf_len(xb
) ? lbuf_len(xb
) - 1 : 0;
1313 xtop
= xtop
- xrows
/ 2 > xrow
?
1314 MAX(0, xrow
- xrows
/ 2) : xrow
;
1315 if (xtop
+ xrows
<= xrow
)
1316 xtop
= xtop
+ xrows
+ xrows
/ 2 <= xrow
?
1317 xrow
- xrows
/ 2 : xrow
- xrows
+ 1;
1318 xoff
= ren_noeol(lbuf_get(xb
, xrow
), xoff
);
1320 xcol
= vi_off2col(xb
, xrow
, xoff
);
1321 if (xcol
>= xleft
+ xcols
)
1322 xleft
= xcol
- xcols
/ 2;
1324 xleft
= xcol
< xcols
? 0 : xcol
- xcols
/ 2;
1326 if (mod
|| xleft
!= oleft
)
1327 vi_drawagain(xcol
, mod
== 2 && xleft
== oleft
);
1328 else if (xtop
!= otop
)
1329 vi_drawupdate(xcol
, otop
);
1332 term_pos(xrow
- xtop
, led_pos(lbuf_get(xb
, xrow
),
1333 ren_cursor(lbuf_get(xb
, xrow
), xcol
)));
1340 int main(int argc
, char *argv
[])
1343 char *prog
= strchr(argv
[0], '/') ? strrchr(argv
[0], '/') + 1 : argv
[0];
1344 xvis
= strcmp("ex", prog
) && strcmp("neatex", prog
);
1345 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1346 if (argv
[i
][1] == 's')
1348 if (argv
[i
][1] == 'e')
1350 if (argv
[i
][1] == 'v')
1357 if (!ex_init(argv
+ i
)) {