retire nonsymbolic rootdev, dev2name
[minix.git] / commands / elvis / vcmd.c
blob494f7e1f722bdb3b05045a741eb65b4b52e5a001
1 /* vcmd.c */
3 /* Author:
4 * Steve Kirkendall
5 * 14407 SW Teal Blvd. #C
6 * Beaverton, OR 97005
7 * kirkenda@cs.pdx.edu
8 */
11 /* This file contains the functions that handle VI commands */
14 #include "config.h"
15 #include "ctype.h"
16 #include "vi.h"
17 #if MSDOS
18 # include <process.h>
19 # include <string.h>
20 #endif
21 #if TOS
22 # include <osbind.h>
23 # include <string.h>
24 #endif
25 #if OSK
26 # include <stdio.h>
27 #endif
30 /* This function puts the editor in EX mode */
31 MARK v_quit()
33 move(LINES - 1, 0);
34 mode = MODE_EX;
35 return cursor;
38 /* This function causes the screen to be redrawn */
39 MARK v_redraw()
41 redraw(MARK_UNSET, FALSE);
42 return cursor;
45 /* This function executes a string of EX commands, and waits for a user keystroke
46 * before returning to the VI screen. If that keystroke is another ':', then
47 * another EX command is read and executed.
49 /*ARGSUSED*/
50 MARK v_1ex(m, text)
51 MARK m; /* the current line */
52 char *text; /* the first command to execute */
54 /* run the command. be careful about modes & output */
55 exwrote = (mode == MODE_COLON);
56 doexcmd(text);
57 exrefresh();
59 /* if mode is no longer MODE_VI, then we should quit right away! */
60 if (mode != MODE_VI && mode != MODE_COLON)
61 return cursor;
63 /* The command did some output. Wait for a keystoke. */
64 if (exwrote)
66 mode = MODE_VI;
67 msg("[Hit <RETURN> to continue]");
68 if (getkey(0) == ':')
69 { mode = MODE_COLON;
70 addch('\n');
72 else
73 redraw(MARK_UNSET, FALSE);
76 return cursor;
79 /* This function undoes the last change */
80 /*ARGSUSED*/
81 MARK v_undo(m)
82 MARK m; /* (ignored) */
84 if (undo())
86 redraw(MARK_UNSET, FALSE);
88 return cursor;
91 /* This function deletes the character(s) that the cursor is on */
92 MARK v_xchar(m, cnt, cmd)
93 MARK m; /* where to start deletions */
94 long cnt; /* number of chars to delete */
95 int cmd; /* either 'x' or 'X' */
97 DEFAULT(1);
99 /* for 'X', adjust so chars are deleted *BEFORE* cursor */
100 if (cmd == 'X')
102 if (markidx(m) < cnt)
103 return MARK_UNSET;
104 m -= cnt;
107 /* make sure we don't try to delete more thars than there are */
108 pfetch(markline(m));
109 if (markidx(m + cnt) > plen)
111 cnt = plen - markidx(m);
113 if (cnt == 0L)
115 return MARK_UNSET;
118 /* do it */
119 ChangeText
121 cut(m, m + cnt);
122 delete(m, m + cnt);
124 return m;
127 /* This function defines a mark */
128 /*ARGSUSED*/
129 MARK v_mark(m, count, key)
130 MARK m; /* where the mark will be */
131 long count; /* (ignored) */
132 int key; /* the ASCII label of the mark */
134 if (key < 'a' || key > 'z')
136 msg("Marks must be from a to z");
138 else
140 mark[key - 'a'] = m;
142 return m;
145 /* This function toggles upper & lower case letters */
146 MARK v_ulcase(m, cnt)
147 MARK m; /* where to make the change */
148 long cnt; /* number of chars to flip */
150 REG char *pos;
151 REG int j;
153 DEFAULT(1);
155 /* fetch the current version of the line */
156 pfetch(markline(m));
158 /* for each position in the line */
159 for (j = 0, pos = &ptext[markidx(m)]; j < cnt && *pos; j++, pos++)
161 if (isupper(*pos))
163 tmpblk.c[j] = tolower(*pos);
165 else
167 tmpblk.c[j] = toupper(*pos);
171 /* if the new text is different from the old, then change it */
172 if (strncmp(tmpblk.c, &ptext[markidx(m)], j))
174 ChangeText
176 tmpblk.c[j] = '\0';
177 change(m, m + j, tmpblk.c);
181 return m + j;
185 MARK v_replace(m, cnt, key)
186 MARK m; /* first char to be replaced */
187 long cnt; /* number of chars to replace */
188 int key; /* what to replace them with */
190 REG char *text;
191 REG int i;
193 DEFAULT(1);
195 /* map ^M to '\n' */
196 if (key == '\r')
198 key = '\n';
201 /* make sure the resulting line isn't too long */
202 if (cnt > BLKSIZE - 2 - markidx(m))
204 cnt = BLKSIZE - 2 - markidx(m);
207 /* build a string of the desired character with the desired length */
208 for (text = tmpblk.c, i = cnt; i > 0; i--)
210 *text++ = key;
212 *text = '\0';
214 /* make sure cnt doesn't extend past EOL */
215 pfetch(markline(m));
216 key = markidx(m);
217 if (key + cnt > plen)
219 cnt = plen - key;
222 /* do the replacement */
223 ChangeText
225 change(m, m + cnt, tmpblk.c);
228 if (*tmpblk.c == '\n')
230 return (m & ~(BLKSIZE - 1)) + cnt * BLKSIZE;
232 else
234 return m + cnt - 1;
238 MARK v_overtype(m)
239 MARK m; /* where to start overtyping */
241 MARK end; /* end of a substitution */
242 static long width; /* width of a single-line replace */
244 /* the "doingdot" version of replace is really a substitution */
245 if (doingdot)
247 /* was the last one really repeatable? */
248 if (width < 0)
250 msg("Can't repeat a multi-line overtype command");
251 return MARK_UNSET;
254 /* replacing nothing by nothing? Don't bother */
255 if (width == 0)
257 return m;
260 /* replace some chars by repeated text */
261 return v_subst(m, width);
264 /* Normally, we input starting here, in replace mode */
265 ChangeText
267 end = input(m, m, WHEN_VIREP, FALSE);
270 /* if we ended on the same line we started on, then this
271 * overtype is repeatable via the dot key.
273 if (markline(end) == markline(m) && end >= m - 1L)
275 width = end - m + 1L;
277 else /* it isn't repeatable */
279 width = -1L;
282 return end;
286 /* This function selects which cut buffer to use */
287 /*ARGSUSED*/
288 MARK v_selcut(m, cnt, key)
289 MARK m;
290 long cnt;
291 int key;
293 cutname(key);
294 return m;
297 /* This function pastes text from a cut buffer */
298 /*ARGSUSED*/
299 MARK v_paste(m, cnt, cmd)
300 MARK m; /* where to paste the text */
301 long cnt; /* (ignored) */
302 int cmd; /* either 'p' or 'P' */
304 MARK dest;
306 ChangeText
308 /* paste the text, and find out where it ends */
309 dest = paste(m, cmd == 'p', TRUE);
311 /* was that a line-mode paste? */
312 if (dest && markline(dest) != markline(m))
314 /* line-mode pastes leave the cursor at the front
315 * of the first pasted line.
317 dest = m;
318 if (cmd == 'p')
320 dest += BLKSIZE;
322 force_flags |= FRNT;
325 return dest;
328 /* This function yanks text into a cut buffer */
329 MARK v_yank(m, n)
330 MARK m, n; /* range of text to yank */
332 cut(m, n);
333 return m;
336 /* This function deletes a range of text */
337 MARK v_delete(m, n)
338 MARK m, n; /* range of text to delete */
340 /* illegal to try and delete nothing */
341 if (n <= m)
343 return MARK_UNSET;
346 /* Do it */
347 ChangeText
349 cut(m, n);
350 delete(m, n);
352 return m;
356 /* This starts input mode without deleting anything */
357 MARK v_insert(m, cnt, key)
358 MARK m; /* where to start (sort of) */
359 long cnt; /* repeat how many times? */
360 int key; /* what command is this for? {a,A,i,I,o,O} */
362 int wasdot;
363 long reps;
364 int above; /* boolean: new line going above old line? */
366 DEFAULT(1);
368 ChangeText
370 /* tweak the insertion point, based on command key */
371 above = FALSE;
372 switch (key)
374 case 'i':
375 break;
377 case 'a':
378 pfetch(markline(m));
379 if (plen > 0)
381 m++;
383 break;
385 case 'I':
386 m = m_front(m, 1L);
387 break;
389 case 'A':
390 pfetch(markline(m));
391 m = (m & ~(BLKSIZE - 1)) + plen;
392 break;
394 case 'O':
395 m &= ~(BLKSIZE - 1);
396 add(m, "\n");
397 above = TRUE;
398 break;
400 case 'o':
401 m = (m & ~(BLKSIZE - 1)) + BLKSIZE;
402 add(m, "\n");
403 break;
406 /* insert the same text once or more */
407 for (reps = cnt, wasdot = doingdot; reps > 0; reps--, doingdot = TRUE)
409 m = input(m, m, WHEN_VIINP, above) + 1;
411 if (markidx(m) > 0)
413 m--;
416 doingdot = wasdot;
419 #ifndef CRUNCH
420 # ifndef NO_EXTENSIONS
421 if (key == 'i' && *o_inputmode && mode == MODE_VI)
423 msg("Now in command mode! To return to input mode, hit <i>");
425 # endif
426 #endif
428 return m;
431 /* This starts input mode with some text deleted */
432 MARK v_change(m, n)
433 MARK m, n; /* the range of text to change */
435 int lnmode; /* is this a line-mode change? */
437 /* swap them if they're in reverse order */
438 if (m > n)
440 MARK tmp;
441 tmp = m;
442 m = n;
443 n = tmp;
446 /* for line mode, retain the last newline char */
447 lnmode = (markidx(m) == 0 && markidx(n) == 0 && m != n);
448 if (lnmode)
450 n -= BLKSIZE;
451 pfetch(markline(n));
452 n = (n & ~(BLKSIZE - 1)) + plen;
455 ChangeText
457 cut(m, n);
458 m = input(m, n, WHEN_VIINP, FALSE);
461 return m;
464 /* This function replaces a given number of characters with input */
465 MARK v_subst(m, cnt)
466 MARK m; /* where substitutions start */
467 long cnt; /* number of chars to replace */
469 DEFAULT(1);
471 /* make sure we don't try replacing past EOL */
472 pfetch(markline(m));
473 if (markidx(m) + cnt > plen)
475 cnt = plen - markidx(m);
478 /* Go for it! */
479 ChangeText
481 cut(m, m + cnt);
482 m = input(m, m + cnt, WHEN_VIINP, FALSE);
484 return m;
487 /* This calls the ex "join" command to join some lines together */
488 MARK v_join(m, cnt)
489 MARK m; /* the first line to be joined */
490 long cnt; /* number of other lines to join */
492 MARK joint; /* where the lines were joined */
494 DEFAULT(1);
496 /* figure out where the joint will be */
497 pfetch(markline(m));
498 joint = (m & ~(BLKSIZE - 1)) + plen;
500 /* join the lines */
501 cmd_join(m, m + MARK_AT_LINE(cnt), CMD_JOIN, 0, "");
503 /* the cursor should be left at the joint */
504 return joint;
508 /* This calls the ex "<" command to shift some lines left */
509 MARK v_lshift(m, n)
510 MARK m, n; /* range of lines to shift */
512 /* adjust for inclusive endmarks in ex */
513 n -= BLKSIZE;
515 cmd_shift(m, n, CMD_SHIFTL, FALSE, (char *)0);
517 return m;
520 /* This calls the ex ">" command to shift some lines right */
521 MARK v_rshift(m, n)
522 MARK m, n; /* range of lines to shift */
524 /* adjust for inclusive endmarks in ex */
525 n -= BLKSIZE;
527 cmd_shift(m, n, CMD_SHIFTR, FALSE, (char *)0);
529 return m;
532 /* This filters some lines through a preset program, to reformat them */
533 MARK v_reformat(m, n)
534 MARK m, n; /* range of lines to shift */
536 /* adjust for inclusive endmarks in ex */
537 n -= BLKSIZE;
539 /* run the filter command */
540 filter(m, n, o_equalprg, TRUE);
542 redraw(MARK_UNSET, FALSE);
543 return m;
547 /* This runs some lines through a filter program */
548 MARK v_filter(m, n)
549 MARK m, n; /* range of lines to shift */
551 char cmdln[150]; /* a shell command line */
553 /* adjust for inclusive endmarks in ex */
554 n -= BLKSIZE;
556 if (vgets('!', cmdln, sizeof(cmdln)) > 0)
558 filter(m, n, cmdln, TRUE);
561 redraw(MARK_UNSET, FALSE);
562 return m;
566 /* This function runs the ex "file" command to show the file's status */
567 MARK v_status()
569 cmd_file(cursor, cursor, CMD_FILE, 0, "");
570 return cursor;
574 /* This function runs the ":&" command to repeat the previous :s// */
575 MARK v_again(m, n)
576 MARK m, n;
578 cmd_substitute(m, n - BLKSIZE, CMD_SUBAGAIN, TRUE, "");
579 return cursor;
584 /* This function switches to the previous file, if possible */
585 MARK v_switch()
587 if (!*prevorig)
588 msg("No previous file");
589 else
590 { strcpy(tmpblk.c, prevorig);
591 cmd_edit(cursor, cursor, CMD_EDIT, 0, tmpblk.c);
593 return cursor;
596 /* This function does a tag search on a keyword */
597 /*ARGSUSED*/
598 MARK v_tag(keyword, m, cnt)
599 char *keyword;
600 MARK m;
601 long cnt;
603 /* move the cursor to the start of the tag name, where m is */
604 cursor = m;
606 /* perform the tag search */
607 cmd_tag(cursor, cursor, CMD_TAG, 0, keyword);
609 return cursor;
612 #ifndef NO_EXTENSIONS
613 /* This function looks up a keyword by calling the helpprog program */
614 /*ARGSUSED*/
615 MARK v_keyword(keyword, m, cnt)
616 char *keyword;
617 MARK m;
618 long cnt;
620 int waswarn;
621 char cmdline[130];
623 move(LINES - 1, 0);
624 addstr("---------------------------------------------------------\n");
625 clrtoeol();
626 refresh();
627 sprintf(cmdline, "%s %s", o_keywordprg, keyword);
628 waswarn = *o_warn;
629 *o_warn = FALSE;
630 suspend_curses();
631 if (system(cmdline))
633 addstr("<<< failed >>>\n");
635 resume_curses(FALSE);
636 mode = MODE_VI;
637 redraw(MARK_UNSET, FALSE);
638 *o_warn = waswarn;
640 return m;
645 MARK v_increment(keyword, m, cnt)
646 char *keyword;
647 MARK m;
648 long cnt;
650 static sign;
651 char newval[12];
652 long atol();
654 DEFAULT(1);
656 /* get one more keystroke, unless doingdot */
657 if (!doingdot)
659 sign = getkey(0);
662 /* adjust the number, based on that second keystroke */
663 switch (sign)
665 case '+':
666 case '#':
667 cnt = atol(keyword) + cnt;
668 break;
670 case '-':
671 cnt = atol(keyword) - cnt;
672 break;
674 case '=':
675 break;
677 default:
678 return MARK_UNSET;
680 sprintf(newval, "%ld", cnt);
682 ChangeText
684 change(m, m + strlen(keyword), newval);
687 return m;
689 #endif
692 /* This function acts like the EX command "xit" */
693 /*ARGSUSED*/
694 MARK v_xit(m, cnt, key)
695 MARK m; /* ignored */
696 long cnt; /* ignored */
697 int key; /* must be a second 'Z' */
699 /* if second char wasn't 'Z', fail */
700 if (key != 'Z')
702 return MARK_UNSET;
705 /* move the cursor to the bottom of the screen */
706 move(LINES - 1, 0);
707 clrtoeol();
709 /* do the xit command */
710 cmd_xit(m, m, CMD_XIT, FALSE, "");
712 /* return the cursor */
713 return m;
717 /* This function undoes changes to a single line, if possible */
718 MARK v_undoline(m)
719 MARK m; /* where we hope to undo the change */
721 /* make sure we have the right line in the buffer */
722 if (markline(m) != U_line)
724 return MARK_UNSET;
727 /* fix it */
728 ChangeText
730 strcat(U_text, "\n");
731 change(MARK_AT_LINE(U_line), MARK_AT_LINE(U_line + 1), U_text);
734 /* nothing in the buffer anymore */
735 U_line = -1L;
737 /* return, with the cursor at the front of the line */
738 return m & ~(BLKSIZE - 1);
742 #ifndef NO_ERRLIST
743 MARK v_errlist(m)
744 MARK m;
746 cmd_errlist(m, m, CMD_ERRLIST, FALSE, "");
747 return cursor;
749 #endif
752 #ifndef NO_AT
753 /*ARGSUSED*/
754 MARK v_at(m, cnt, key)
755 MARK m;
756 long cnt;
757 int key;
759 int size;
761 size = cb2str(key, tmpblk.c, BLKSIZE);
762 if (size <= 0 || size == BLKSIZE)
764 return MARK_UNSET;
767 execmap(0, tmpblk.c, FALSE);
768 return cursor;
770 #endif
773 #ifdef SIGTSTP
774 MARK v_suspend()
776 cmd_suspend(MARK_UNSET, MARK_UNSET, CMD_SUSPEND, FALSE, "");
777 return MARK_UNSET;
779 #endif
782 #ifndef NO_VISIBLE
783 /*ARGSUSED*/
784 MARK v_start(m, cnt, cmd)
785 MARK m; /* starting point for a v or V command */
786 long cnt; /* ignored */
787 int cmd; /* either 'v' or 'V' */
789 if (V_from)
791 V_from = MARK_UNSET;
793 else
795 V_from = m;
796 V_linemd = isupper(cmd);
798 return m;
800 #endif
802 #ifndef NO_POPUP
803 # define MENU_HEIGHT 11
804 # define MENU_WIDTH 23
805 MARK v_popup(m, n)
806 MARK m, n; /* the range of text to change */
808 int i;
809 int y, x; /* position where the window will pop up at */
810 int key; /* keystroke from the user */
811 int sel; /* index of the selected operation */
812 static int dfltsel;/* default value of sel */
813 static char *labels[11] =
815 "ESC cancel! ",
816 " d delete (cut) ",
817 " y yank (copy) ",
818 " p paste after ",
819 " P paste before ",
820 " > more indented ",
821 " < less indented ",
822 " = reformat ",
823 " ! external filter ",
824 " ZZ save & exit ",
825 " u undo previous "
828 /* try to position the menu near the cursor */
829 x = physcol - (MENU_WIDTH / 2);
830 if (x < 0)
831 x = 0;
832 else if (x + MENU_WIDTH + 2 > COLS)
833 x = COLS - MENU_WIDTH - 2;
834 if (markline(cursor) < topline || markline(cursor) > botline)
835 y = 0;
836 else if (markline(cursor) + 1L + MENU_HEIGHT > botline)
837 y = (int)(markline(cursor) - topline) - MENU_HEIGHT;
838 else
839 y = (int)(markline(cursor) - topline) + 1L;
841 /* draw the menu */
842 for (sel = 0; sel < MENU_HEIGHT; sel++)
844 move(y + sel, x);
845 do_POPUP();
846 if (sel == dfltsel)
847 qaddstr("-->");
848 else
849 qaddstr(" ");
850 qaddstr(labels[sel]);
851 do_SE();
854 /* get a selection */
855 move(y + dfltsel, x + 4);
856 for (sel = dfltsel; (key = getkey(WHEN_POPUP)) != '\\' && key != '\r'; )
858 /* erase the selection arrow */
859 move(y + sel, x);
860 do_POPUP();
861 qaddstr(" ");
862 qaddstr(labels[sel]);
863 do_SE();
865 /* process the user's keystroke */
866 if (key == 'j' && sel < MENU_HEIGHT - 1)
868 sel++;
870 else if (key == 'k' && sel > 0)
872 sel--;
874 else if (key == '\033')
876 sel = 0;
877 break;
879 else
881 for (i = 1; i < MENU_HEIGHT && labels[i][1] != key; i++)
884 if (i < MENU_HEIGHT)
886 sel = i;
887 break;
891 /* redraw the arrow, possibly in a new place */
892 move(y + sel, x);
893 do_POPUP();
894 qaddstr("-->");
895 qaddstr(labels[sel]);
896 do_SE();
897 move(y + sel, x + 4);
900 /* in most cases, the default selection is "paste after" */
901 dfltsel = 3;
903 /* perform the appropriate action */
904 switch (sel)
906 case 0:
907 m = cursor;
908 dfltsel = 0;
909 break;
911 case 1: /* delete (cut) */
912 m = v_delete(m, n);
913 break;
915 case 2: /* yank (copy) */
916 m = v_yank(m, n);
917 break;
919 case 3: /* paste after */
920 m = v_paste(n, 1L, 'P');
921 break;
923 case 4: /* paste before */
924 m = v_paste(m, 1L, 'P');
925 dfltsel = 4;
926 break;
928 case 5: /* more indented */
929 m = v_rshift(m, n);
930 dfltsel = 5;
931 break;
933 case 6: /* less indented */
934 m = v_lshift(m, n);
935 dfltsel = 6;
936 break;
938 case 7: /* reformat */
939 m = v_reformat(m, n);
940 dfltsel = 7;
941 break;
943 case 8: /* external filter */
944 m = v_filter(m, n);
945 dfltsel = 0;
946 break;
948 case 9: /* save & exit */
949 /* get confirmation first */
952 key = getkey(0);
953 } while (key != '\\' && key != 'Z' && key != '\r' /* good */
954 && key != '\033'); /* bad */
955 if (key != '\033')
957 m = v_xit(m, 0L, 'Z');
959 break;
961 case 10: /* undo previous */
962 m = v_undo(m);
963 dfltsel = 9;
964 break;
967 /* arrange for the menu to be erased (except that "chg from kbd"
968 * already erased it, and "save & exit" doesn't care)
970 if (sel != 5 && sel != 9)
971 redraw(MARK_UNSET, FALSE);
973 return m;
975 #endif /* undef NO_POPUP */