aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-01-28 21:13:29 +1000
committerDavid Walter Seikel2014-01-28 21:13:29 +1000
commit471001441d81710c7d14943ea08630453d2d702d (patch)
tree89ac0a886b8ae9aca9d0d58c04a76bc020268040
parentRemove the event structure stuff, not actually using it. (diff)
downloadboxes-471001441d81710c7d14943ea08630453d2d702d.zip
boxes-471001441d81710c7d14943ea08630453d2d702d.tar.gz
boxes-471001441d81710c7d14943ea08630453d2d702d.tar.bz2
boxes-471001441d81710c7d14943ea08630453d2d702d.tar.xz
Reorganise doCommand() and friends a bit.
-rw-r--r--boxes.c85
1 files changed, 48 insertions, 37 deletions
diff --git a/boxes.c b/boxes.c
index 82f7535..622aef3 100644
--- a/boxes.c
+++ b/boxes.c
@@ -662,23 +662,6 @@ static box *currentBox;
662static view *commandLine; 662static view *commandLine;
663static int commandMode; 663static int commandMode;
664 664
665void doCommand(struct function *functions, char *command, view *view)
666{
667 if (command)
668 {
669 int i;
670
671 for (i = 0; functions[i].name; i++)
672 {
673 if (strcmp(functions[i].name, command) == 0)
674 {
675 if (functions[i].handler);
676 functions[i].handler(view);
677 break;
678 }
679 }
680 }
681}
682 665
683#define MEM_SIZE 128 // Chunk size for line memory allocation. 666#define MEM_SIZE 128 // Chunk size for line memory allocation.
684 667
@@ -1085,6 +1068,43 @@ void drawContentLine(view *view, int y, int start, int end, char *left, char *in
1085 drawLine(y, start, end, left, internal, &(temp[offset]), right, current); 1068 drawLine(y, start, end, left, internal, &(temp[offset]), right, current);
1086} 1069}
1087 1070
1071void doCommand(view *view, char *command)
1072{
1073 if (command)
1074 {
1075 struct function *functions = view->content->context->commands;
1076 int i;
1077
1078 for (i = 0; functions[i].name; i++)
1079 {
1080 if (strcmp(functions[i].name, command) == 0)
1081 {
1082 if (functions[i].handler);
1083 {
1084 int y, len;
1085
1086 functions[i].handler(view);
1087
1088 // Coz things might change out from under us, find the current view. Again.
1089 if (commandMode) view = commandLine;
1090 else view = currentBox->view;
1091
1092 // TODO - When doing scripts and such, might want to turn off the line update until finished.
1093 // Draw the prompt and the current line.
1094 y = view->Y + (view->cY - view->offsetY);
1095 len = strlen(view->prompt);
1096 drawLine(y, view->X, view->X + view->W, "", " ", view->prompt, "", 0);
1097 drawContentLine(view, y, view->X + len, view->X + view->W, "", " ", view->line->line, "", 1);
1098 // Move the cursor.
1099 printf("\x1B[%d;%dH", y + 1, view->X + len + (view->cX - view->offsetX) + 1);
1100 fflush(stdout);
1101 }
1102 break;
1103 }
1104 }
1105 }
1106}
1107
1088int moveCursorAbsolute(view *view, long cX, long cY, long sX, long sY) 1108int moveCursorAbsolute(view *view, long cX, long cY, long sX, long sY)
1089{ 1109{
1090 struct line *newLine = view->line; 1110 struct line *newLine = view->line;
@@ -1717,7 +1737,7 @@ void executeLine(view *view)
1717 // Don't bother doing much if there's nothing on this line. 1737 // Don't bother doing much if there's nothing on this line.
1718 if (result->line[0]) 1738 if (result->line[0])
1719 { 1739 {
1720 doCommand(currentBox->view->content->context->commands, result->line, currentBox->view); 1740 doCommand(currentBox->view, result->line);
1721 // If we are not at the end of the history contents. 1741 // If we are not at the end of the history contents.
1722 if (&(view->content->lines) != result->next) 1742 if (&(view->content->lines) != result->next)
1723 { 1743 {
@@ -1756,7 +1776,7 @@ void quit(view *view)
1756 TT.stillRunning = 0; 1776 TT.stillRunning = 0;
1757} 1777}
1758 1778
1759void nop(box *box) 1779void nop(view *view)
1760{ 1780{
1761 // 'tis a nop, don't actually do anything. 1781 // 'tis a nop, don't actually do anything.
1762} 1782}
@@ -1777,30 +1797,15 @@ static void lineChar(long extra, char *buffer)
1777 moveCursorRelative(view, strlen(buffer), 0, 0, 0); 1797 moveCursorRelative(view, strlen(buffer), 0, 0, 0);
1778} 1798}
1779 1799
1780// TODO - should merge this and the real one. Note that when doing scripts and such, might want to turn off the line update until finished.
1781static struct keyCommand * lineCommand(long extra, char *command) 1800static struct keyCommand * lineCommand(long extra, char *command)
1782{ 1801{
1783 struct _view *view = (struct _view *) extra; // Though we pretty much stomp on this straight away. 1802 struct _view *view = (struct _view *) extra; // Though we pretty much stomp on this straight away.
1784 int y, len;
1785 1803
1786 // Coz things might change out from under us, find the current view. 1804 // Coz things might change out from under us, find the current view.
1787 if (commandMode) view = commandLine; 1805 if (commandMode) view = commandLine;
1788 else view = currentBox->view; 1806 else view = currentBox->view;
1789 1807
1790 doCommand(view->content->context->commands, command, view); 1808 doCommand(view, command);
1791
1792 // Coz things might change out from under us, find the current view. Again
1793 if (commandMode) view = commandLine;
1794 else view = currentBox->view;
1795
1796 // Draw the prompt and the current line.
1797 y = view->Y + (view->cY - view->offsetY);
1798 len = strlen(view->prompt);
1799 drawLine(y, view->X, view->X + view->W, "", " ", view->prompt, "", 0);
1800 drawContentLine(view, y, view->X + len, view->X + view->W, "", " ", view->line->line, "", 1);
1801 // Move the cursor.
1802 printf("\x1B[%d;%dH", y + 1, view->X + len + (view->cX - view->offsetX) + 1);
1803 fflush(stdout);
1804 1809
1805 // This is using the currentBox instead of view, coz the command line keys are part of the box context now, not separate. 1810 // This is using the currentBox instead of view, coz the command line keys are part of the box context now, not separate.
1806 return currentBox->view->content->context->modes[currentBox->view->mode].keys; 1811 return currentBox->view->content->context->modes[currentBox->view->mode].keys;
@@ -1825,8 +1830,8 @@ The response from a terminal size check command includes a prefix, a suffix, wit
1825void editLine(long extra, void (*lineChar)(long extra, char *buffer), struct keyCommand * (*lineCommand)(long extra, char *command)) 1830void editLine(long extra, void (*lineChar)(long extra, char *buffer), struct keyCommand * (*lineCommand)(long extra, char *command))
1826{ 1831{
1827 struct pollfd pollfds[1]; 1832 struct pollfd pollfds[1];
1828 // Get the initial command set, and trigger the first cursor move. 1833 // Get the initial command set, and trigger the first cursor move. Assumes the command set has a nop that otherwise does nothing.
1829 struct keyCommand *ourKeys = lineCommand(extra, ""); 1834 struct keyCommand *ourKeys = lineCommand(extra, "nop");
1830 char buffer[20]; 1835 char buffer[20];
1831 char command[20]; 1836 char command[20];
1832 int pollcount = 1; 1837 int pollcount = 1;
@@ -1983,6 +1988,7 @@ struct function simpleEditCommands[] =
1983 {"endOfLine", "Go to end of line.", 0, {endOfLine}}, 1988 {"endOfLine", "Go to end of line.", 0, {endOfLine}},
1984 {"executeLine", "Execute a line as a script.", 0, {executeLine}}, 1989 {"executeLine", "Execute a line as a script.", 0, {executeLine}},
1985 {"leftChar", "Move cursor left one character.", 0, {leftChar}}, 1990 {"leftChar", "Move cursor left one character.", 0, {leftChar}},
1991 {"nop", "Do nothing.", 0, {nop}},
1986 {"quit", "Quit the application.", 0, {quit}}, 1992 {"quit", "Quit the application.", 0, {quit}},
1987 {"rightChar", "Move cursor right one character.", 0, {rightChar}}, 1993 {"rightChar", "Move cursor right one character.", 0, {rightChar}},
1988 {"save", "Save.", 0, {saveContent}}, 1994 {"save", "Save.", 0, {saveContent}},
@@ -2029,6 +2035,7 @@ struct keyCommand simpleCommandKeys[] =
2029// readline uses these same commands, and defaults to emacs keystrokes. 2035// readline uses these same commands, and defaults to emacs keystrokes.
2030struct function simpleEmacsCommands[] = 2036struct function simpleEmacsCommands[] =
2031{ 2037{
2038 {"nop", "Do nothing.", 0, {nop}},
2032 {"delete-backward-char", "Back space last character.", 0, {backSpaceChar}}, 2039 {"delete-backward-char", "Back space last character.", 0, {backSpaceChar}},
2033 {"delete-window", "Delete a box.", 0, {deleteBox}}, 2040 {"delete-window", "Delete a box.", 0, {deleteBox}},
2034 {"delete-char", "Delete current character.", 0, {deleteChar}}, 2041 {"delete-char", "Delete current character.", 0, {deleteChar}},
@@ -2135,6 +2142,7 @@ struct context simpleEmacs =
2135// TODO - Some of these might be wrong. Just going by the inadequate joe docs for now. 2142// TODO - Some of these might be wrong. Just going by the inadequate joe docs for now.
2136struct function simpleJoeCommands[] = 2143struct function simpleJoeCommands[] =
2137{ 2144{
2145 {"nop", "Do nothing.", 0, {nop}},
2138 {"backs", "Back space last character.", 0, {backSpaceChar}}, 2146 {"backs", "Back space last character.", 0, {backSpaceChar}},
2139 {"abort", "Delete a box.", 0, {deleteBox}}, 2147 {"abort", "Delete a box.", 0, {deleteBox}},
2140 {"delch", "Delete current character.", 0, {deleteChar}}, 2148 {"delch", "Delete current character.", 0, {deleteChar}},
@@ -2363,6 +2371,7 @@ struct context simpleMcedit =
2363 2371
2364struct function simpleNanoCommands[] = 2372struct function simpleNanoCommands[] =
2365{ 2373{
2374 {"nop", "Do nothing.", 0, {nop}},
2366 {"backSpaceChar", "Back space last character.", 0, {backSpaceChar}}, 2375 {"backSpaceChar", "Back space last character.", 0, {backSpaceChar}},
2367 {"delete", "Delete current character.", 0, {deleteChar}}, 2376 {"delete", "Delete current character.", 0, {deleteChar}},
2368 {"down", "Move cursor down one line.", 0, {downLine}}, 2377 {"down", "Move cursor down one line.", 0, {downLine}},
@@ -2497,6 +2506,8 @@ void viStartOfNextLine(view *view)
2497// TODO - ex uses "shortest unique string" to match commands, should implement that, and do it for the other contexts to. 2506// TODO - ex uses "shortest unique string" to match commands, should implement that, and do it for the other contexts to.
2498struct function simpleViCommands[] = 2507struct function simpleViCommands[] =
2499{ 2508{
2509 {"nop", "Do nothing.", 0, {nop}},
2510
2500 // These are actual ex commands. 2511 // These are actual ex commands.
2501 {"insert", "Switch to insert mode.", 0, {viInsertMode}}, 2512 {"insert", "Switch to insert mode.", 0, {viInsertMode}},
2502 {"quit", "Quit the application.", 0, {quit}}, 2513 {"quit", "Quit the application.", 0, {quit}},