├── tine.png ├── bigshot.png ├── parser.h ├── structs.h ├── mode.h ├── util.h ├── Makefile.darwin ├── Makefile ├── editor.h ├── buffer.h ├── util.c ├── README.md ├── tine.c ├── parser.c ├── mode.c ├── command.h ├── buffer.c ├── editor.c ├── tine.1 ├── COPYING └── command.c /tine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deadpixi/tine/HEAD/tine.png -------------------------------------------------------------------------------- /bigshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deadpixi/tine/HEAD/bigshot.png -------------------------------------------------------------------------------- /parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "editor.h" 9 | 10 | bool 11 | runextended(const wchar_t *c, size_t n, EDITOR *e); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /structs.h: -------------------------------------------------------------------------------- 1 | #ifndef STRUCTS_H 2 | #define STRUCTS_H 3 | 4 | typedef struct ARG ARG; 5 | typedef struct BUFFER BUFFER; 6 | typedef struct CMD CMD; 7 | typedef struct EDITOR EDITOR; 8 | typedef struct JOURNAL JOURNAL; 9 | typedef struct KEYSTROKE KEYSTROKE; 10 | typedef struct LINE LINE; 11 | typedef struct MODE MODE; 12 | typedef struct POS POS; 13 | typedef struct TAG TAG; 14 | typedef struct VIEW VIEW; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /mode.h: -------------------------------------------------------------------------------- 1 | #ifndef MODE_H 2 | #define MODE_H 3 | 4 | #include 5 | #include 6 | 7 | #include "structs.h" 8 | #include "command.h" 9 | 10 | typedef bool (*callback)(EDITOR *e, VIEW *v, const ARG *a); 11 | 12 | extern MODE *cmdmode; 13 | extern MODE *docmode; 14 | 15 | struct KEYSTROKE{ 16 | int o; 17 | wchar_t c; 18 | }; 19 | 20 | callback 21 | lookupkeystroke(const MODE *m, KEYSTROKE k, ARG *a); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | char *ellipsize(const char *s, size_t l, bool right); 9 | wchar_t *dupstr(const wchar_t *s, size_t n); 10 | wchar_t *stows(const char *s, size_t n); 11 | char *wstos(const wchar_t *s, size_t n); 12 | const char *trimleft(const char *s); 13 | bool readfile(const char *fn, 14 | bool (*cb)(const wchar_t *, size_t, void *), 15 | void *p); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Makefile.darwin: -------------------------------------------------------------------------------- 1 | CC := c99 2 | STANDARDS := -D_POSIX_C_SOURCE=200908L -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED 3 | CURSES := -DCURSES_INCLUDE="" 4 | CFLAGS := $(STANDARDS) $(CURSES) -Os 5 | LDFLAGS := -lcurses 6 | DESTDIR ?= /usr/local 7 | 8 | all: tine 9 | strip tine 10 | 11 | clean: 12 | rm -rf *.o tine 13 | 14 | tine: buffer.o command.o editor.o mode.o parser.o util.o 15 | 16 | install: all 17 | mkdir -p "$(DESTDIR)/bin" "$(DESTDIR)/share/man/man1" 18 | cp tine "$(DESTDIR)/bin/tine" 19 | cp tine.1 "$(DESTDIR)/share/man/man1/tine.1" 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC := c99 2 | STANDARDS := -D_POSIX_C_SOURCE=200908L -D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED 3 | CURSES := -DCURSES_INCLUDE="" 4 | CFLAGS := $(STANDARDS) $(CURSES) -Os 5 | LDFLAGS := -lncursesw 6 | DESTDIR ?= /usr/local 7 | 8 | all: tine 9 | strip tine 10 | 11 | clean: 12 | rm -rf *.o tine 13 | 14 | tine: buffer.o command.o editor.o mode.o parser.o util.o 15 | 16 | install: all 17 | mkdir -p "$(DESTDIR)/bin" "$(DESTDIR)/share/man/man1" 18 | cp tine "$(DESTDIR)/bin/tine" 19 | cp tine.1 "$(DESTDIR)/share/man/man1/tine.1" 20 | -------------------------------------------------------------------------------- /editor.h: -------------------------------------------------------------------------------- 1 | #ifndef EDITOR_H 2 | #define EDITOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include CURSES_INCLUDE 8 | 9 | #include "buffer.h" 10 | #include "mode.h" 11 | 12 | struct VIEW{ 13 | BUFFER *b; 14 | POS p, tos, gb; 15 | WINDOW *w; 16 | MODE *m; 17 | lineno bs, be; 18 | void (*statuscb)(EDITOR *e, VIEW *v); 19 | bool ex, uc, delay, et, q, ai, sm, se; 20 | size_t ph, ts, lm, rm, sd; 21 | wchar_t *dl; 22 | size_t dln; 23 | }; 24 | 25 | #define FUNC_MAX 10 26 | #define CTRL_MAX 32 27 | #define ERR_MAX 127 28 | #define BM_MAX 10 29 | struct EDITOR{ 30 | bool running, needsresize; 31 | VIEW cmdview, docview, *focusview; 32 | POS bm[BM_MAX], lc; 33 | wchar_t *funcs[FUNC_MAX]; 34 | char ctrlmap[CTRL_MAX]; 35 | char name[FILENAME_MAX + 1]; 36 | char err[ERR_MAX + 1]; 37 | wchar_t *find; 38 | size_t findn; 39 | }; 40 | 41 | EDITOR *openeditor(const char *name, WINDOW *docwin, WINDOW *cmdwin); 42 | void closeeditor(EDITOR *e); 43 | 44 | void dispatch(EDITOR *e, VIEW *v, KEYSTROKE k); 45 | void redisplay(VIEW *v); 46 | 47 | void hilight(VIEW *v, POS p1, POS p2); 48 | void clearhilight(VIEW *v); 49 | bool error(EDITOR *e, const char *s); 50 | 51 | KEYSTROKE getkeystroke(EDITOR *e, bool delay); 52 | void fixcursor(EDITOR *e); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /buffer.h: -------------------------------------------------------------------------------- 1 | #ifndef BUFFER_H 2 | #define BUFFER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define NONE ((size_t)-1) 9 | typedef size_t lineno; 10 | typedef size_t colno; 11 | struct POS{ 12 | lineno l; 13 | colno c; 14 | }; 15 | 16 | struct LINE{ 17 | size_t a, n; 18 | wchar_t *s; 19 | }; 20 | 21 | struct TAG{ 22 | POS p1; 23 | POS p2; 24 | int v; 25 | }; 26 | 27 | typedef enum{ /* note that these must be in descending order by priority */ 28 | VIRTCURS, 29 | HIGHLIGHT, 30 | BLOCK, 31 | TAG_MAX 32 | } tag; 33 | 34 | typedef uint64_t txn; 35 | struct BUFFER{ 36 | size_t a, n; 37 | LINE *l; 38 | 39 | bool canundo, dirty; 40 | int nbegin; 41 | txn t; 42 | JOURNAL *j; 43 | 44 | TAG tags[TAG_MAX]; 45 | }; 46 | 47 | BUFFER *openbuffer(void); 48 | void closebuffer(BUFFER *b); 49 | 50 | bool insertline(BUFFER *b, lineno l); 51 | bool inserttext(BUFFER *b, POS p, const wchar_t *s, size_t n); 52 | bool deleteline(BUFFER *b, lineno l); 53 | bool deletetext(BUFFER *b, POS p, size_t n); 54 | 55 | wint_t charat(const BUFFER *b, POS p); 56 | 57 | bool prev(const BUFFER *b, POS *p); 58 | bool next(const BUFFER *b, POS *p); 59 | bool attop(const BUFFER *b, POS p); 60 | bool atbot(const BUFFER *b, POS p); 61 | bool ateol(const BUFFER *b, POS p); 62 | 63 | POS pos(lineno l, colno c); 64 | 65 | bool settag(BUFFER *b, tag t, POS p1, POS p2, int v); 66 | void cleartag(BUFFER *b, tag t); 67 | 68 | void enableundo(BUFFER *b); 69 | bool mark(BUFFER *b); 70 | void begin(BUFFER *b); 71 | bool commit(BUFFER *b); 72 | bool undo(BUFFER *b, POS *p); 73 | void clearundo(BUFFER *b); 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "util.h" 13 | 14 | char * 15 | ellipsize(const char *s, size_t l, bool right) 16 | { 17 | size_t n = strlen(s); 18 | if (n <= l) 19 | return strdup(s); 20 | char *o = calloc(l + 3 + 1, sizeof(char)); 21 | if (!o) 22 | return NULL; 23 | 24 | if (!right){ 25 | strcat(o, "..."); 26 | strncat(o, s + n - l, l); 27 | } else{ 28 | strncat(o, s + n - l, l); 29 | strcat(o, "..."); 30 | } 31 | 32 | return o; 33 | } 34 | 35 | wchar_t * 36 | stows(const char *s, size_t n) 37 | { 38 | char *mbs = calloc(n + 1, sizeof(char)); 39 | if (!mbs) 40 | return NULL; 41 | strncpy(mbs, s, n); 42 | 43 | size_t wcn = mbstowcs(NULL, mbs, 0); 44 | wchar_t *wcs = calloc(wcn + 1, sizeof(wchar_t)); 45 | if (!wcs) 46 | return free(mbs), NULL; 47 | 48 | size_t wcr = mbstowcs(wcs, mbs, wcn); 49 | if (wcr == (size_t)-1) 50 | return free(mbs), free(wcs), NULL; 51 | 52 | free(mbs); 53 | return wcs; 54 | } 55 | 56 | char * 57 | wstos(const wchar_t *s, size_t n) 58 | { 59 | wchar_t *wcs = calloc(n + 1, sizeof(wchar_t)); 60 | if (!wcs) 61 | return NULL; 62 | wcsncpy(wcs, s, n); 63 | 64 | size_t mbn = wcstombs(NULL, wcs, 0); 65 | char *mbs = calloc(mbn + 1, sizeof(char)); 66 | if (!mbs) 67 | return free(wcs), NULL; 68 | 69 | size_t mbr = wcstombs(mbs, wcs, mbn); 70 | if (mbr == (size_t)-1) 71 | return free(mbs), free(wcs), NULL; 72 | 73 | free(wcs); 74 | return mbs; 75 | } 76 | 77 | bool 78 | readfile(const char *fn, bool (*cb)(const wchar_t *, size_t, void *), void *p) 79 | { 80 | struct stat s = {0}; 81 | if (stat(fn, &s) != 0) 82 | return false; 83 | if ((s.st_mode & S_IFMT) == S_IFDIR) 84 | return (errno = EISDIR), false; 85 | 86 | FILE *f = fopen(fn, "r"); 87 | if (!f) 88 | return false; 89 | 90 | char *l = NULL; 91 | size_t ln = 0; 92 | ssize_t n = 0; 93 | bool rc = true; 94 | while ((n = getline(&l, &ln, f)) != -1 && rc){ 95 | if (n && l[n - 1] == '\n') 96 | n--; 97 | if (n){ 98 | wchar_t *c = stows(l, n); 99 | rc = c && cb(c, wcslen(c), p); 100 | free(c); 101 | } else 102 | rc = cb(L"", 0, p); 103 | } 104 | free(l); 105 | fclose(f); 106 | return rc; 107 | } 108 | 109 | wchar_t * 110 | dupstr(const wchar_t *s, size_t n) 111 | { 112 | wchar_t *r = calloc(n + 1, sizeof(wchar_t)); 113 | if (!r) 114 | return r; 115 | wmemcpy(r, s, n); 116 | return r; 117 | } 118 | 119 | const char * 120 | trimleft(const char *s) 121 | { 122 | if (!s) 123 | return NULL; 124 | while (*s && isspace(*s)) 125 | s++; 126 | return s; 127 | } 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tine - tine is not ED 2 | 3 | ## Introduction 4 | tine (pronouced "tiny" and standing for "tine Is Not ED") is a modern-day 5 | clone of the AmigaDOS/TRIPOS ED display editor. 6 | 7 | ED was originally written by MetaComCo in the early 1980's as a display 8 | editor for the TRIPOS operating system. MetaComCo later ported TRIPOS 9 | to the Amiga, where it formed the core of AmigaDOS; as part of this 10 | port, ED was included as a display editor. 11 | 12 | ED was shipped as part of every release of AmigaDOS, from 1.0 to the latest 13 | 4.x series. It stands as the standard display editor for the operating 14 | system (in other words, "ED is the standard text editor"). 15 | 16 | (Note that tine is not endorsed by any owners of the Amiga intellectual property, 17 | MetaComCo, or anyone else.) 18 | 19 | ## Differences from ED 20 | tine is more than just an implementation of ED's extended command language, 21 | or a set of key-bindings on top of some other editor. 22 | tine attempts to be faithful to ED's somewhat unusual handling of whitespace and tabs, 23 | its rather anachronistic cursor position handling (in some ways ED feels more like a 24 | typewriter than a display editor), its simplicity, and its general quirkiness. 25 | 26 | In other words, tine tries to function as similarly to ED as possible, 27 | but there are a few differences: 28 | 29 | **The Good** 30 | - Unlimited line length (ED limits lines to 255 characters) 31 | - Full extended command editing and history (ED has only insert-and-delete-at-the-end 32 | editing for extended commands, and no history) 33 | - Smooth horizontal scrolling (ED only has chunky horizontal scrolling) 34 | - Unlimited undo (ED only has undo within the current line) 35 | - Numerous additional extended commands 36 | - A status line (ED has no status line of any kind, and in fact it's not actually possible to tell what line number you're on in ED except by counting from the top) 37 | - Much improved tab handling (ED does not allow the insertion of literal tabs) 38 | - Support for wide characters and combining characters (ED supports neither) 39 | - Up to ten bookmarks (there are no bookmarks in ED) 40 | - Various quality-of-life improvements (e.g. specifying a block start after a block end still works) 41 | 42 | **The Less Good** 43 | - Being a console-only application, tine does not support ED's extended menus or 44 | mouse-based cursor movement, similar to the pre-AmigaDOS 2 versions 45 | - Much like versions of ED shipped prior to AmigaDOS 2, tine does not support 46 | REXX macros 47 | 48 | All-in-all, tine functions similarly to ED circa AmigaDOS 1.3, with some features from 49 | AmigaDOS 2 and 3 and a few additional features. In any case, tine should be familiar and 50 | comfortable to ED users familiar with any version of the editor. 51 | 52 | ## Screenshots 53 | The font used in these screenshots is [IBM Plex](https://www.ibm.com/plex/), 54 | which is the only font with a dotted zero that I've ever really liked 55 | (usually it's slashed zero or nothing). 56 | 57 | ### Shot 1 58 | ![Screenshot of tine](tine.png) 59 | 60 | A screenshot of tine editing itself; 61 | the terminal has an excellent color scheme. 62 | 63 | ### Shot 2 64 | ![Another screenshot of tine](bigshot.png) 65 | 66 | Three instances of tine running under [mtm](https://github.com/deadpixi/mtm). 67 | 68 | The instance on the top left is showing the output of the `SH` command, 69 | while the isntance on the bottom left is showing the currently defined 70 | function keys (i.e. the output of the `DF` command). 71 | 72 | The instance on the right is editing text in multiple languages, 73 | displaying combining characters and other interesting features. 74 | 75 | ## The Future 76 | 77 | tine does not aim to be much more than an ED clone with a few improvements. 78 | However, the following changes are planned: 79 | 80 | - Removal of the ncurses dependency 81 | - Multi-window editing 82 | - Simple syntax highlighting 83 | - Buffer and screen management is fairly simplistic (though, given the expected use cases, perfectly adequate); 84 | they will be improved in the future 85 | 86 | ## A Work in Progress 87 | 88 | tine is very much a work in progress, 89 | but it's been stable enough to be my daily driver for a while now. 90 | 91 | Please report any bugs you may find, and patches would be happily accepted. 92 | Note that, for the most part, differences in behavior from AmigaDOS ED are considered bugs, 93 | so please report any that you may find. 94 | -------------------------------------------------------------------------------- /tine.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "structs.h" 11 | #include "editor.h" 12 | #include "util.h" 13 | 14 | static EDITOR *editor; 15 | static WINDOW *cmdwin; 16 | static bool reversed = false; 17 | 18 | static int 19 | quit(const char *m, int rc) 20 | { 21 | if (editor) 22 | closeeditor(editor); 23 | if (cmdwin) 24 | endwin(); 25 | if (m) 26 | fputs(m, stderr); 27 | exit(rc); 28 | } 29 | 30 | static int 31 | setupcommand(WINDOW *w, int i) 32 | { 33 | (void)i; 34 | cmdwin = w; 35 | if (!reversed) 36 | wbkgdset(w, A_REVERSE); 37 | werase(w); 38 | return OK; 39 | } 40 | 41 | static void 42 | initializescreen(int i) 43 | { 44 | ripoffline(i, setupcommand); 45 | if (!initscr()) 46 | quit("could not initialize screen", EXIT_FAILURE); 47 | raw(); 48 | noecho(); 49 | nonl(); 50 | if (reversed) 51 | wbkgdset(stdscr, A_REVERSE); 52 | keypad(stdscr, TRUE); 53 | keypad(cmdwin, TRUE); 54 | intrflush(stdscr, FALSE); 55 | } 56 | 57 | static void 58 | loadfile(const char *fn) 59 | { 60 | wchar_t *s = stows(fn, strlen(fn)); 61 | if (!s) 62 | quit("Out of memory\n", EXIT_FAILURE); 63 | ARG a = {.t = ARG_STRING, .s1 = s, .n1 = wcslen(s)}; 64 | errno = 0; 65 | cmd_if(editor, &editor->docview, &a); 66 | free(s); 67 | } 68 | 69 | static void 70 | runfile(const char *fn) 71 | { 72 | wchar_t *s = stows(fn, strlen(fn)); 73 | if (!s) 74 | quit("out of memory\n", EXIT_FAILURE); 75 | ARG a = {.t = ARG_STRING, .s1 = s, .n1 = wcslen(s)}; 76 | cmd_rf(editor, &editor->docview, &a); 77 | free(s); 78 | } 79 | 80 | static bool 81 | tryrun(const char *p, const char *n) 82 | { 83 | char fn[FILENAME_MAX + 1] = {0}; 84 | snprintf(fn, FILENAME_MAX, "%s/%s", p, n); 85 | if (access(fn, R_OK) == 0){ 86 | runfile(fn); 87 | return true; 88 | } 89 | return false; 90 | } 91 | 92 | static const char * 93 | getdef(const char *env, const char *def) 94 | { 95 | if (getenv(env)) 96 | return getenv(env); 97 | return def; 98 | } 99 | 100 | static void 101 | runstartup(const char *ext) 102 | { 103 | char def[FILENAME_MAX + 1] = {0}; 104 | char path[FILENAME_MAX + 1] = {0}; 105 | char *s = NULL; 106 | 107 | snprintf(def, FILENAME_MAX, "%s/.config", getdef("HOME", "/")); 108 | snprintf(path, FILENAME_MAX, "tine/%s", ext); 109 | if (tryrun(getdef("XDG_CONFIG_HOME", def), path)) 110 | return; 111 | if ((s = strdup(getdef("XDG_CONFIG_DIRS", "/etc/xdg")))){ 112 | for (char *n = strtok(s, ":"); n; n = strtok(NULL, ":")){ 113 | if (tryrun(n, path)) 114 | break; 115 | } 116 | free(s); 117 | return; 118 | } 119 | } 120 | 121 | static void 122 | runstartupfiles(const char *fn) 123 | { 124 | char path[FILENAME_MAX + 1] = {0}; 125 | snprintf(path, FILENAME_MAX, "tinerc.%s", strrchr(fn, '.')? strrchr(fn, '.') + 1 : fn); 126 | runstartup("tinerc"); 127 | runstartup(path); 128 | } 129 | 130 | static void 131 | fixloc(void) 132 | { 133 | int y, x; 134 | getyx(editor->focusview->w, y, x); 135 | wmove(editor->focusview->w, y, x); 136 | wrefresh(editor->focusview->w); 137 | } 138 | 139 | static void 140 | run(void) 141 | { 142 | if (editor->focusview->statuscb) 143 | editor->focusview->statuscb(editor, editor->focusview); 144 | redisplay(editor->focusview); 145 | 146 | while (editor->running){ 147 | fixloc(); 148 | KEYSTROKE k = getkeystroke(editor, true); 149 | if (k.o == ERR) 150 | continue; 151 | dispatch(editor, editor->focusview, k); 152 | if (editor->focusview->statuscb) 153 | editor->focusview->statuscb(editor, editor->focusview); 154 | redisplay(editor->focusview); 155 | } 156 | } 157 | 158 | #define USAGE "usage: tine [-rtn] FILE [CMDFILE|+LINE]...\n" 159 | int 160 | main(int argc, char **argv) 161 | { 162 | setlocale(LC_ALL, ""); 163 | 164 | int o = 0, toporbot = -1; 165 | bool runrc = true; 166 | while ((o = getopt(argc, argv, "rtn")) != -1){ 167 | switch (o){ 168 | case 'n': 169 | runrc = false; 170 | break; 171 | case 't': 172 | toporbot = 1; 173 | break; 174 | case 'r': 175 | reversed = true; 176 | break; 177 | 178 | default: 179 | quit(USAGE, EXIT_FAILURE); 180 | break; 181 | } 182 | } 183 | argc -= optind; 184 | argv += optind; 185 | 186 | if (argc < 1) 187 | quit(USAGE, EXIT_FAILURE); 188 | 189 | initializescreen(toporbot); 190 | if ((editor = openeditor(argv[0], stdscr, cmdwin)) == NULL) 191 | return fputs("out of memory", stderr), EXIT_FAILURE; 192 | loadfile(argv[0]); 193 | if (runrc) 194 | runstartupfiles(argv[0]); 195 | enableundo(editor->docview.b); 196 | editor->docview.b->dirty = false; 197 | 198 | for (int i = 1; i < argc; i++){ 199 | if (argv[i][0] == '+' && isdigit(argv[i][1])){ 200 | ARG a = {.t = ARG_NUMBER, .n1 = (size_t)atol(argv[i] + 1)}; 201 | cmd_m(editor, &editor->docview, &a); 202 | } else 203 | runfile(argv[i]); 204 | } 205 | 206 | run(); 207 | 208 | return quit(NULL, EXIT_SUCCESS); 209 | } 210 | -------------------------------------------------------------------------------- /parser.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "structs.h" 9 | #include "editor.h" 10 | #include "command.h" 11 | 12 | #define DEPTH_MAX 32 13 | typedef struct STATE STATE; 14 | struct STATE{ 15 | const wchar_t *s; 16 | size_t n, o, d; 17 | EDITOR *e; 18 | }; 19 | 20 | static bool chain(STATE *s); 21 | 22 | static void 23 | skipws(STATE *s) 24 | { 25 | while (s->o < s->n && iswspace(s->s[s->o])) 26 | s->o++; 27 | } 28 | 29 | static wint_t 30 | consume(STATE *s, bool skip) 31 | { 32 | if (skip) 33 | skipws(s); 34 | return s->o >= s->n? WEOF : (wint_t)s->s[s->o++]; 35 | } 36 | 37 | static wint_t 38 | consume1(STATE *s) 39 | { 40 | return consume(s, false); 41 | } 42 | 43 | static wint_t 44 | peek(STATE *s, bool skip) 45 | { 46 | if (skip) 47 | skipws(s); 48 | return s->o >= s->n? WEOF : (wint_t)s->s[s->o]; 49 | } 50 | 51 | static wint_t 52 | peek1(STATE *s) 53 | { 54 | return peek(s, false); 55 | } 56 | 57 | static bool 58 | name(STATE *s, wchar_t *b) 59 | { 60 | wmemset(b, 0, CMD_NAME_MAX); 61 | for (int i = 0; i < CMD_NAME_MAX && iswalpha(peek1(s)); i++) 62 | b[i] = towupper(consume1(s)); 63 | return b[0]; 64 | } 65 | 66 | static bool 67 | string(STATE *s, ARG *a) 68 | { 69 | wint_t d = consume(s, true); 70 | if (d == WEOF || iswalnum(d) || d == L';' || d == L'(' || d == L')') 71 | return error(s->e, "Invalid string delimiter"); 72 | 73 | a->s1 = s->s + s->o; 74 | wint_t c = consume1(s); 75 | while (c != WEOF && c != d){ 76 | a->n1++; 77 | c = consume1(s); 78 | } 79 | a->t = ARG_STRING; 80 | return true; 81 | } 82 | 83 | static bool 84 | exchange(STATE *s, ARG *a) 85 | { 86 | ARG a2 = {0}; 87 | if (!string(s, a)) 88 | return false; 89 | s->o--; 90 | if (!string(s, &a2)) 91 | return false; 92 | a->n2 = a2.n1; 93 | a->s2 = a2.s1; 94 | a->t = ARG_EXCHANGE; 95 | return true; 96 | } 97 | 98 | static size_t 99 | number(STATE *s) 100 | { 101 | size_t r = 0; 102 | if (!iswdigit(peek(s, true))) 103 | return error(s->e, "Number expected"), (size_t)-1; 104 | skipws(s); 105 | while (iswdigit(peek1(s))){ 106 | wint_t c = consume1(s); 107 | r *= 10; 108 | r += c - L'0'; 109 | } 110 | return r; 111 | } 112 | 113 | static bool 114 | numarg(STATE *s, ARG *a) 115 | { 116 | a->n1 = number(s); 117 | if (a->n1 == (size_t)-1) 118 | return false; 119 | a->t = ARG_NUMBER; 120 | return true; 121 | } 122 | 123 | static bool 124 | argavail(STATE *s) 125 | { 126 | wint_t c = peek(s, true); 127 | return c != WEOF && c != L';' && c != L'(' && c != L')'; 128 | } 129 | 130 | static bool 131 | simplecommand(STATE *s) 132 | { 133 | wchar_t n[CMD_NAME_MAX + 1] = {0}; 134 | if (!name(s, n)) 135 | return error(s->e, "Name expected"); 136 | const CMD *c = lookup(n); 137 | if (!c) 138 | return error(s->e, "Unknown command"); 139 | ARG a = {0}; 140 | if (argavail(s)){ 141 | switch (c->a){ 142 | case ARG_NONE: break; 143 | case ARG_STRING: if (!string(s, &a)) return false; break; 144 | case ARG_EXCHANGE: if (!exchange(s, &a)) return false; break; 145 | case ARG_NUMBER: if (!numarg(s, &a)) return false; break; 146 | } 147 | } else if (c->required && c->a != ARG_NONE) 148 | return error(s->e, "Argument expected"); 149 | return call(c, s->e, &s->e->docview, &a); 150 | } 151 | 152 | static size_t 153 | count(STATE *s) 154 | { 155 | if (iswdigit(peek(s, true))) 156 | return number(s); 157 | 158 | STATE t = *s; /* simulate extra lookahead */ 159 | wchar_t b[CMD_NAME_MAX + 1] = {0}; 160 | if (name(&t, b) && wcscmp(b, L"RP") == 0){ 161 | memcpy(s, &t, sizeof(t)); /* actually take the lookahead */ 162 | return SIZE_MAX; 163 | } 164 | return 1; 165 | } 166 | 167 | static bool 168 | command(STATE *s) 169 | { 170 | skipws(s); 171 | 172 | size_t n = count(s); 173 | STATE t = {0}; 174 | for (size_t i = 0; i < n; i++){ 175 | memcpy(&t, s, sizeof(t)); 176 | if (peek(&t, true) == L'('){ 177 | consume(&t, true); 178 | if (s->d++ > DEPTH_MAX) 179 | return error(s->e, "?FORMULA TOO COMPLEX"); 180 | if (!chain(&t)) 181 | return memcpy(s, &t, sizeof(t)), false; 182 | if (consume(&t, true) != L')') 183 | return memcpy(s, &t, sizeof(t)), error(s->e, "Unmatched();"); 184 | s->d--; 185 | } else if (!simplecommand(&t)) 186 | return memcpy(s, &t, sizeof(t)), false; 187 | 188 | redisplay(&s->e->docview); 189 | if (n > 1){ 190 | KEYSTROKE k = getkeystroke(s->e, false); 191 | if (k.o != ERR) 192 | return error(s->e, "Commands abandoned"); 193 | } 194 | } 195 | memcpy(s, &t, sizeof(t)); 196 | return true; 197 | } 198 | 199 | static bool 200 | chain(STATE *s) 201 | { 202 | if (!command(s)) 203 | return false; 204 | while (peek(s, true) == L';'){ 205 | consume(s, true); 206 | if (!command(s)) 207 | return false; 208 | } 209 | return true; 210 | } 211 | 212 | bool 213 | runextended(const wchar_t *c, size_t n, EDITOR *e) 214 | { 215 | STATE s = {.s = c, .n = n? n : wcslen(c), .o = 0, .d = 0, .e = e}; 216 | mark(e->docview.b); 217 | begin(e->docview.b); 218 | bool rc = chain(&s); 219 | commit(e->docview.b); 220 | if (!rc) 221 | return false; 222 | if (peek(&s, true) != WEOF) 223 | return error(e, "Extra input at end"); 224 | return true; 225 | } 226 | -------------------------------------------------------------------------------- /mode.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include CURSES_INCLUDE 4 | 5 | #include "structs.h" 6 | #include "mode.h" 7 | 8 | typedef struct MAP MAP; 9 | struct MAP{ 10 | KEYSTROKE k; 11 | callback f; 12 | ARG a; 13 | }; 14 | 15 | #define MAP_MAX 100 16 | struct MODE{ 17 | callback defcb; 18 | MAP map[MAP_MAX]; 19 | }; 20 | 21 | #define CTRL(x) ((x) & 0x1f) 22 | static MODE cmdmodetab ={ 23 | cmd_ty, 24 | { 25 | {{KEY_CODE_YES, KEY_BACKSPACE}, cmd_dl, {0}}, 26 | {{KEY_CODE_YES, KEY_DC}, cmd_dc, {0}}, 27 | {{KEY_CODE_YES, KEY_UP}, cmd_cu, {0}}, 28 | {{KEY_CODE_YES, KEY_DOWN}, cmd_cd, {0}}, 29 | {{KEY_CODE_YES, KEY_LEFT}, cmd_cl, {0}}, 30 | {{KEY_CODE_YES, KEY_RIGHT}, cmd_cr, {0}}, 31 | {{KEY_CODE_YES, KEY_SLEFT}, cmd_cs, {0}}, 32 | {{KEY_CODE_YES, KEY_SRIGHT}, cmd_ce, {0}}, 33 | {{KEY_CODE_YES, KEY_RESIZE}, cmd_vw, {0}}, 34 | {{KEY_CODE_YES, KEY_ENTER}, cmd_ru, {0}}, 35 | {{KEY_CODE_YES, KEY_BTAB}, cmd_bt, {0}}, 36 | {{OK, L'\n'}, cmd_rs, {0}}, 37 | {{OK, L'\r'}, cmd_ru, {0}}, 38 | {{OK, L'\t'}, cmd_tb, {0}}, 39 | {{OK, 0x7f}, cmd_dl, {0}}, 40 | {{OK, CTRL(L'[')}, cmd_rs, {0}}, 41 | {{OK, CTRL(L']')}, cmd_cj, {0}}, 42 | {{OK, CTRL(L'C')}, cmd_ca, {0}}, 43 | {{OK, CTRL(L'F')}, cmd_fc, {0}}, 44 | {{OK, CTRL(L'O')}, cmd_dw, {0}}, 45 | {{OK, CTRL(L'P')}, cmd_sm, {0}}, 46 | {{OK, CTRL(L'Q')}, cmd_qo, {0}}, 47 | {{OK, CTRL(L'R')}, cmd_wp, {0}}, 48 | {{OK, CTRL(L'T')}, cmd_wn, {0}}, 49 | {{OK, CTRL(L'V')}, cmd_vw, {0}}, 50 | {{OK, CTRL(L'W')}, cmd_dp, {0}}, 51 | {{OK, CTRL(L'Y')}, cmd_el, {0}}, 52 | {{KEY_CODE_YES, 0}, NULL, {0}} 53 | } 54 | }; 55 | MODE *cmdmode = &cmdmodetab; 56 | 57 | static MODE docmodetab ={ 58 | cmd_ty, 59 | { 60 | {{KEY_CODE_YES, KEY_BACKSPACE}, cmd_dl, {0}}, 61 | {{KEY_CODE_YES, KEY_IC}, cmd_i, {.t = ARG_STRING, .n1 = 0, .s1 = L""}}, 62 | {{KEY_CODE_YES, KEY_HOME}, cmd_t, {0}}, 63 | {{KEY_CODE_YES, KEY_END}, cmd_b, {0}}, 64 | {{KEY_CODE_YES, KEY_DC}, cmd_dc, {0}}, 65 | {{KEY_CODE_YES, KEY_UP}, cmd_cu, {0}}, 66 | {{KEY_CODE_YES, KEY_DOWN}, cmd_cd, {0}}, 67 | {{KEY_CODE_YES, KEY_LEFT}, cmd_cl, {0}}, 68 | {{KEY_CODE_YES, KEY_RIGHT}, cmd_cr, {0}}, 69 | {{KEY_CODE_YES, KEY_SLEFT}, cmd_cs, {0}}, 70 | {{KEY_CODE_YES, KEY_SRIGHT}, cmd_ce, {0}}, 71 | {{KEY_CODE_YES, KEY_RESIZE}, cmd_vw, {0}}, 72 | {{KEY_CODE_YES, KEY_ENTER}, cmd_s, {0}}, 73 | {{KEY_CODE_YES, KEY_PPAGE}, cmd_pu, {0}}, 74 | {{KEY_CODE_YES, KEY_NPAGE}, cmd_pd, {0}}, 75 | {{KEY_CODE_YES, KEY_BTAB}, cmd_bt, {0}}, 76 | {{KEY_CODE_YES, KEY_F(1)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 0}}, 77 | {{KEY_CODE_YES, KEY_F(2)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 1}}, 78 | {{KEY_CODE_YES, KEY_F(3)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 2}}, 79 | {{KEY_CODE_YES, KEY_F(4)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 3}}, 80 | {{KEY_CODE_YES, KEY_F(5)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 4}}, 81 | {{KEY_CODE_YES, KEY_F(6)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 5}}, 82 | {{KEY_CODE_YES, KEY_F(7)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 6}}, 83 | {{KEY_CODE_YES, KEY_F(8)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 7}}, 84 | {{KEY_CODE_YES, KEY_F(9)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 8}}, 85 | {{KEY_CODE_YES, KEY_F(10)}, cmd_cf, {.t = ARG_NUMBER, .n1 = 9}}, 86 | {{OK, L'\n'}, cmd_s, {0}}, 87 | {{OK, L'\r'}, cmd_s, {0}}, 88 | {{OK, L'\t'}, cmd_tb, {0}}, 89 | {{OK, 0x7f}, cmd_dl, {0}}, 90 | {{OK, CTRL(L'[')}, cmd_cm, {0}}, 91 | {{OK, CTRL(L']')}, cmd_cj, {0}}, 92 | {{OK, CTRL(L'A')}, cmd_se, {.t = ARG_STRING, .n1 = 0, .s1 = L""}}, 93 | {{OK, CTRL(L'B')}, cmd_d, {0}}, 94 | {{OK, CTRL(L'D')}, cmd_pd, {0}}, 95 | {{OK, CTRL(L'E')}, cmd_ep, {0}}, 96 | {{OK, CTRL(L'F')}, cmd_fc, {0}}, 97 | {{OK, CTRL(L'G')}, cmd_rp, {0}}, 98 | {{OK, CTRL(L'K')}, cmd_bb, {0}}, 99 | {{OK, CTRL(L'L')}, cmd_rd, {0}}, 100 | {{OK, CTRL(L'N')}, cmd_j, {0}}, 101 | {{OK, CTRL(L'O')}, cmd_dw, {0}}, 102 | {{OK, CTRL(L'P')}, cmd_sm, {0}}, 103 | {{OK, CTRL(L'Q')}, cmd_qo, {0}}, 104 | {{OK, CTRL(L'R')}, cmd_wp, {0}}, 105 | {{OK, CTRL(L'T')}, cmd_wn, {0}}, 106 | {{OK, CTRL(L'U')}, cmd_pu, {0}}, 107 | {{OK, CTRL(L'W')}, cmd_dp, {0}}, 108 | {{OK, CTRL(L'V')}, cmd_vw, {0}}, 109 | {{OK, CTRL(L'Y')}, cmd_el, {0}}, 110 | {{OK, CTRL(L'Z')}, cmd_gb, {0}}, 111 | {{OK, L'['}, cmd_hb, {0}}, 112 | {{OK, L']'}, cmd_hb, {0}}, 113 | {{OK, L'{'}, cmd_hb, {0}}, 114 | {{OK, L'}'}, cmd_hb, {0}}, 115 | {{OK, L'('}, cmd_hb, {0}}, 116 | {{OK, L')'}, cmd_hb, {0}}, 117 | {{KEY_CODE_YES, 0}, cmd_uk, {0}}, 118 | {{OK, 0}, NULL, {0}} 119 | } 120 | }; 121 | MODE *docmode = &docmodetab; 122 | 123 | callback 124 | lookupkeystroke(const MODE *m, KEYSTROKE k, ARG *a) 125 | { 126 | for (const MAP *i = m->map; i->f; i++){ 127 | if (i->k.o == k.o && (!i->k.c || i->k.c == k.c)){ 128 | if (i->a.t != ARG_NONE) 129 | memcpy(a, &i->a, sizeof(ARG)); 130 | return i->f; 131 | } 132 | } 133 | return m->defcb; 134 | } 135 | -------------------------------------------------------------------------------- /command.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMANDS_H 2 | #define COMMANDS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | #define CMD_NAME_MAX 2 10 | 11 | typedef enum{ 12 | ARG_NONE, 13 | ARG_STRING, 14 | ARG_EXCHANGE, 15 | ARG_NUMBER 16 | } argtype; 17 | 18 | struct ARG{ 19 | argtype t; 20 | size_t n1, n2; 21 | const wchar_t *s1, *s2; 22 | }; 23 | 24 | struct CMD{ 25 | const wchar_t *n; 26 | argtype a; 27 | bool required; 28 | bool (*f)(EDITOR *, VIEW *, const ARG *); 29 | }; 30 | 31 | const CMD *lookup(const wchar_t *s); 32 | bool call(const CMD *c, EDITOR *e, VIEW *v, const ARG *a); 33 | 34 | bool cmd_a(EDITOR *e, VIEW *v, const ARG *a); /* insert line after current */ 35 | bool cmd_ai(EDITOR *e, VIEW *v, const ARG *a); /* enable auto-indent */ 36 | bool cmd_b(EDITOR *e, VIEW *v, const ARG *a); /* move to bottom of file */ 37 | bool cmd_bb(EDITOR *e, VIEW *v, const ARG *a); /* smart mark of block */ 38 | bool cmd_be(EDITOR *e, VIEW *v, const ARG *a); /* block end at cursor line */ 39 | bool cmd_bf(EDITOR *e, VIEW *v, const ARG *a); /* backwards find */ 40 | bool cmd_bm(EDITOR *e, VIEW *v, const ARG *a); /* set bookmark */ 41 | bool cmd_bs(EDITOR *e, VIEW *v, const ARG *a); /* block start at cursor line */ 42 | bool cmd_bt(EDITOR *e, VIEW *v, const ARG *a); /* backwards tab */ 43 | bool cmd_ca(EDITOR *e, VIEW *v, const ARG *a); /* cancel command */ 44 | bool cmd_cb(EDITOR *e, VIEW *v, const ARG *a); /* clear block */ 45 | bool cmd_cd(EDITOR *e, VIEW *v, const ARG *a); /* cursor down, same column */ 46 | bool cmd_ce(EDITOR *e, VIEW *v, const ARG *a); /* cursor to end of line */ 47 | bool cmd_cf(EDITOR *e, VIEW *v, const ARG *a); /* call function key */ 48 | bool cmd_ch(EDITOR *e, VIEW *v, const ARG *a); /* clear highlight */ 49 | bool cmd_cj(EDITOR *e, VIEW *v, const ARG *a); /* cursor to end or beginning of line */ 50 | bool cmd_cm(EDITOR *e, VIEW *v, const ARG *a); /* change mode */ 51 | bool cmd_cl(EDITOR *e, VIEW *v, const ARG *a); /* cursor left */ 52 | bool cmd_cr(EDITOR *e, VIEW *v, const ARG *a); /* cursor right */ 53 | bool cmd_cs(EDITOR *e, VIEW *v, const ARG *a); /* cursor to start of line */ 54 | bool cmd_ct(EDITOR *e, VIEW *v, const ARG *a); /* collapse tabs */ 55 | bool cmd_cu(EDITOR *e, VIEW *v, const ARG *a); /* cursor up, same column */ 56 | bool cmd_d(EDITOR *e, VIEW *v, const ARG *a); /* delete line */ 57 | bool cmd_db(EDITOR *e, VIEW *v, const ARG *a); /* delete block */ 58 | bool cmd_dc(EDITOR *e, VIEW *v, const ARG *a); /* delete character at cursor */ 59 | bool cmd_df(EDITOR *e, VIEW *v, const ARG *a); /* display function definitions */ 60 | bool cmd_dl(EDITOR *e, VIEW *v, const ARG *a); /* delete character to left of cursor */ 61 | bool cmd_do(EDITOR *e, VIEW *v, const ARG *a); /* do a shell command */ 62 | bool cmd_dp(EDITOR *e, VIEW *v, const ARG *a); /* delete previous word */ 63 | bool cmd_dw(EDITOR *e, VIEW *v, const ARG *a); /* delete to end of current word */ 64 | bool cmd_e(EDITOR *e, VIEW *v, const ARG *a); /* exchange s/t */ 65 | bool cmd_el(EDITOR *e, VIEW *v, const ARG *a); /* erase to end of line */ 66 | bool cmd_ep(EDITOR *e, VIEW *v, const ARG *a); /* end/beginning of page */ 67 | bool cmd_eq(EDITOR *e, VIEW *v, const ARG *a); /* exchange s/t with query */ 68 | bool cmd_et(EDITOR *e, VIEW *v, const ARG *a); /* expand tabs */ 69 | bool cmd_ex(EDITOR *e, VIEW *v, const ARG *a); /* expand right margin */ 70 | bool cmd_f(EDITOR *e, VIEW *v, const ARG *a); /* find */ 71 | bool cmd_fb(EDITOR *e, VIEW *v, const ARG *a); /* filter block */ 72 | bool cmd_fc(EDITOR *e, VIEW *v, const ARG *a); /* flip case */ 73 | bool cmd_gb(EDITOR *e, VIEW *v, const ARG *a); /* go back */ 74 | bool cmd_gm(EDITOR *e, VIEW *v, const ARG *a); /* go to bookmark */ 75 | bool cmd_hb(EDITOR *e, VIEW *v, const ARG *a); /* handle bracket */ 76 | bool cmd_i(EDITOR *e, VIEW *v, const ARG *a); /* insert line before */ 77 | bool cmd_ib(EDITOR *e, VIEW *v, const ARG *a); /* insert block */ 78 | bool cmd_if(EDITOR *e, VIEW *v, const ARG *a); /* insert file */ 79 | bool cmd_im(EDITOR *e, VIEW *v, const ARG *a); /* ignore (don't show) matching braces */ 80 | bool cmd_j(EDITOR *e, VIEW *v, const ARG *a); /* join this line and next */ 81 | bool cmd_lc(EDITOR *e, VIEW *v, const ARG *a); /* case-sensitive searching */ 82 | bool cmd_m(EDITOR *e, VIEW *v, const ARG *a); /* move to line */ 83 | bool cmd_mc(EDITOR *e, VIEW *v, const ARG *a); /* remap control key */ 84 | bool cmd_ms(EDITOR *e, VIEW *v, const ARG *a); /* show matches */ 85 | bool cmd_n(EDITOR *e, VIEW *v, const ARG *a); /* move to beginning of next line */ 86 | bool cmd_ni(EDITOR *e, VIEW *v, const ARG *a); /* disable autoindent */ 87 | bool cmd_p(EDITOR *e, VIEW *v, const ARG *a); /* move to beginning of previous line */ 88 | bool cmd_pd(EDITOR *e, VIEW *v, const ARG *a); /* page down */ 89 | bool cmd_ph(EDITOR *e, VIEW *v, const ARG *a); /* define page hieght */ 90 | bool cmd_pu(EDITOR *e, VIEW *v, const ARG *a); /* page up */ 91 | bool cmd_q(EDITOR *e, VIEW *v, const ARG *a); /* quit without save */ 92 | bool cmd_qo(EDITOR *e, VIEW *v, const ARG *a); /* quote next */ 93 | bool cmd_qy(EDITOR *e, VIEW *v, const ARG *a); /* quit without save */ 94 | bool cmd_rd(EDITOR *e, VIEW *v, const ARG *a); /* restore deleted */ 95 | bool cmd_rf(EDITOR *e, VIEW *v, const ARG *a); /* run command file */ 96 | bool cmd_rm(EDITOR *e, VIEW *v, const ARG *a); /* reset margins */ 97 | bool cmd_rp(EDITOR *e, VIEW *v, const ARG *a); /* execute last extended command */ 98 | bool cmd_ru(EDITOR *e, VIEW *v, const ARG *a); /* run extended command */ 99 | bool cmd_rs(EDITOR *e, VIEW *v, const ARG *a); /* run extended command */ 100 | bool cmd_s(EDITOR *e, VIEW *v, const ARG *a); /* split line */ 101 | bool cmd_sa(EDITOR *e, VIEW *v, const ARG *a); /* save text to file */ 102 | bool cmd_sb(EDITOR *e, VIEW *v, const ARG *a); /* show block on screen */ 103 | bool cmd_sd(EDITOR *e, VIEW *v, const ARG *a); /* set show delay */ 104 | bool cmd_se(EDITOR *e, VIEW *v, const ARG *a); /* split line after moving to end */ 105 | bool cmd_sf(EDITOR *e, VIEW *v, const ARG *a); /* set function key */ 106 | bool cmd_sh(EDITOR *e, VIEW *v, const ARG *a); /* show information */ 107 | bool cmd_sl(EDITOR *e, VIEW *v, const ARG *a); /* set left margin */ 108 | bool cmd_sm(EDITOR *e, VIEW *v, const ARG *a); /* show matching brace */ 109 | bool cmd_sr(EDITOR *e, VIEW *v, const ARG *a); /* set right margin */ 110 | bool cmd_st(EDITOR *e, VIEW *v, const ARG *a); /* set tab distance */ 111 | bool cmd_t(EDITOR *e, VIEW *v, const ARG *a); /* move to top of file */ 112 | bool cmd_tb(EDITOR *e, VIEW *v, const ARG *a); /* move to next tabstop */ 113 | bool cmd_ty(EDITOR *e, VIEW *v, const ARG *a); /* type in characters */ 114 | bool cmd_u(EDITOR *e, VIEW *v, const ARG *a); /* undo */ 115 | bool cmd_uc(EDITOR *e, VIEW *v, const ARG *a); /* case-insensitive searching */ 116 | bool cmd_uk(EDITOR *e, VIEW *v, const ARG *a); /* unknown command */ 117 | bool cmd_vw(EDITOR *e, VIEW *v, const ARG *a); /* verify (redisplay) window */ 118 | bool cmd_wb(EDITOR *e, VIEW *v, const ARG *a); /* write block to file */ 119 | bool cmd_wn(EDITOR *e, VIEW *v, const ARG *a); /* next word */ 120 | bool cmd_wp(EDITOR *e, VIEW *v, const ARG *a); /* previous word */ 121 | bool cmd_x(EDITOR *e, VIEW *v, const ARG *a); /* exit with save */ 122 | 123 | #endif 124 | -------------------------------------------------------------------------------- /buffer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "structs.h" 5 | #include "buffer.h" 6 | 7 | #define SLACK 255 8 | 9 | typedef enum{MA, PO, IL, DL, IT, DT} action; 10 | struct JOURNAL{ 11 | JOURNAL *prev; 12 | action a; 13 | POS p; 14 | size_t n; 15 | wchar_t s[]; 16 | }; 17 | 18 | static bool 19 | merge(BUFFER *b, action a, POS p, const wchar_t *s, size_t n) 20 | { 21 | if (b->j && b->j->a == IT && a == IT && b->j->p.l == p.l && p.c == b->j->p.c + b->j->n){ 22 | JOURNAL *j = realloc(b->j, sizeof(JOURNAL) + (b->j->n + n + 1) * sizeof(wchar_t)); 23 | if (!j) 24 | return false; 25 | b->j = j; 26 | wmemcpy(b->j->s + b->j->n, s, n); 27 | b->j->n += n; 28 | b->j->s[b->j->n] = 0; 29 | return true; 30 | } 31 | 32 | if (b->j && b->j->a == MA && a == MA) 33 | return true; 34 | /* XXX - we can merge more things here */ 35 | 36 | return false; 37 | } 38 | 39 | static bool 40 | push(BUFFER *b, action a, POS p, const wchar_t *s, size_t n) 41 | { 42 | if (!b->canundo || merge(b, a, p, s, n)) 43 | return true; 44 | 45 | JOURNAL *j = calloc(1, sizeof(JOURNAL) + (n + 1) * sizeof(wchar_t)); 46 | if (!j) 47 | return false; 48 | j->prev = b->j; 49 | j->a = a; 50 | j->p = p; 51 | j->n = n; 52 | wmemcpy(j->s, s, n); 53 | b->j = j; 54 | return true; 55 | } 56 | 57 | static bool 58 | pop(BUFFER *b) 59 | { 60 | if (b->j){ 61 | JOURNAL *j = b->j; 62 | b->j = j->prev; 63 | free(j); 64 | return true; 65 | } 66 | return false; 67 | } 68 | 69 | BUFFER * 70 | openbuffer(void) 71 | { 72 | BUFFER *b = calloc(1, sizeof(BUFFER)); 73 | if (!b) 74 | return NULL; 75 | for (int i = 0; i < TAG_MAX; i++) 76 | b->tags[i].p1 = b->tags[i].p2 = pos(NONE, NONE); 77 | return b; 78 | } 79 | 80 | void 81 | closebuffer(BUFFER *b) 82 | { 83 | if (b){ 84 | while (b->j) 85 | pop(b); 86 | for (size_t i = 0; i < b->n; i++) 87 | free(b->l[i].s); 88 | free(b->l); 89 | free(b); 90 | } 91 | } 92 | 93 | static bool 94 | ensurelines(BUFFER *b, size_t n) 95 | { 96 | if (b->a >= n) 97 | return true; 98 | LINE *l = realloc(b->l, (n + SLACK) * sizeof(LINE)); 99 | if (!l) 100 | return false; 101 | b->l = l; 102 | b->a = n + SLACK; 103 | return true; 104 | } 105 | 106 | static bool 107 | doinsertline(BUFFER *b, lineno l) 108 | { 109 | if (!ensurelines(b, b->n + 1)) 110 | return false; 111 | 112 | memmove(b->l + l + 1, b->l + l, (b->n - l) * sizeof(LINE)); 113 | memset(b->l + l, 0, sizeof(LINE)); 114 | b->n++; 115 | 116 | return b->dirty = true; 117 | } 118 | 119 | static bool 120 | dodeleteline(BUFFER *b, lineno l) 121 | { 122 | free(b->l[l].s); 123 | memmove(b->l + l, b->l + l + 1, (b->n - l - 1) * sizeof(LINE)); 124 | b->n--; 125 | return b->dirty = true; 126 | } 127 | 128 | static bool 129 | ensureline(LINE *l, size_t n) 130 | { 131 | if (l->a >= n) 132 | return true; 133 | wchar_t *s = realloc(l->s, (n + SLACK) * sizeof(wchar_t)); 134 | if (!s) 135 | return false; 136 | l->s = s; 137 | l->a = n + SLACK; 138 | return true; 139 | } 140 | 141 | static bool 142 | doinserttext(BUFFER *b, POS p, const wchar_t *s, size_t n) 143 | { 144 | if (!n) 145 | return true; 146 | LINE *l = b->l + p.l; 147 | b->dirty = true; 148 | if (p.c > l->n){ 149 | if (!ensureline(l, p.c)) 150 | return false; 151 | wmemset(l->s + l->n, L' ', p.c - l->n); 152 | l->n = p.c; 153 | } 154 | 155 | if (!ensureline(l, l->n + n)) 156 | return false; 157 | wmemmove(l->s + p.c + n, l->s + p.c, l->n - p.c); 158 | wmemcpy(l->s + p.c, s, n); 159 | l->n += n; 160 | return true; 161 | } 162 | 163 | static bool 164 | dodeletetext(BUFFER *b, POS p, size_t n) 165 | { 166 | if (!n) 167 | return true; 168 | LINE *l = b->l + p.l; 169 | b->dirty = true; 170 | wmemmove(l->s + p.c, l->s + p.c + n, l->n - p.c - n); 171 | l->n -= n; 172 | return true; 173 | } 174 | 175 | bool 176 | insertline(BUFFER *b, lineno l) 177 | { 178 | if (!push(b, IL, pos(l, 0), L"", 0)) 179 | return false; 180 | if (!doinsertline(b, l)) 181 | return pop(b), false; 182 | return true; 183 | } 184 | 185 | bool 186 | deleteline(BUFFER *b, lineno l) 187 | { 188 | if (!push(b, DL, pos(l, 0), b->l[l].s, b->l[l].n)) 189 | return false; 190 | if (!dodeleteline(b, l)) 191 | return pop(b), false; 192 | return true; 193 | } 194 | 195 | bool 196 | inserttext(BUFFER *b, POS p, const wchar_t *s, size_t n) 197 | { 198 | if (!push(b, IT, p, s, n)) 199 | return false; 200 | if (!doinserttext(b, p, s, n)) 201 | return pop(b), false; 202 | return true; 203 | } 204 | 205 | bool 206 | deletetext(BUFFER *b, POS p, size_t n) 207 | { 208 | if (!push(b, DT, p, b->l[p.l].s + p.c, n)) 209 | return false; 210 | if (!dodeletetext(b, p, n)) 211 | return pop(b), false; 212 | return true; 213 | } 214 | 215 | wint_t 216 | charat(const BUFFER *b, POS p) 217 | { 218 | if (p.l >= b->n) 219 | return WEOF; 220 | if (p.c >= b->l[p.l].n) 221 | return L' '; 222 | return b->l[p.l].s[p.c]; 223 | } 224 | 225 | bool 226 | prev(const BUFFER *b, POS *p) 227 | { 228 | if (p->l == NONE || p->c == NONE || p->l >= b->n) 229 | return false; 230 | else if (p->c) 231 | p->c--; 232 | else if (p->l){ 233 | p->l--; 234 | p->c = b->l[p->l].n; 235 | } else 236 | return false; 237 | return true; 238 | } 239 | 240 | bool 241 | next(const BUFFER *b, POS *p) 242 | { 243 | if (p->l == NONE || p->c == NONE || p->l >= b->n) 244 | return false; 245 | else if (p->c < b->l[p->l].n) 246 | p->c++; 247 | else if (p->l < b->n){ 248 | p->l++; 249 | p->c = 0; 250 | } else 251 | return false; 252 | return true; 253 | } 254 | 255 | bool 256 | attop(const BUFFER *b, POS p) 257 | { 258 | return !b->n || (!p.l && !p.c); 259 | } 260 | 261 | bool 262 | atbot(const BUFFER *b, POS p) 263 | { 264 | return !b->n || (p.l >= b->n - 1 && p.c >= b->l[b->n - 1].n); 265 | } 266 | 267 | bool 268 | ateol(const BUFFER *b, POS p) 269 | { 270 | return !b->n || p.l >= b->n || p.c >= b->l[p.l].n; 271 | } 272 | 273 | POS 274 | pos(lineno l, colno c) 275 | { 276 | POS p = {l, c}; 277 | return p; 278 | } 279 | 280 | void 281 | begin(BUFFER *b) 282 | { 283 | b->nbegin++; 284 | } 285 | 286 | bool 287 | commit(BUFFER *b) 288 | { 289 | b->nbegin--; 290 | if (b->nbegin <= 0) 291 | b->nbegin = 0; 292 | return true; 293 | } 294 | 295 | bool 296 | mark(BUFFER *b) 297 | { 298 | if (b->nbegin) 299 | return true; 300 | return push(b, MA, b->j? b->j->p : pos(NONE, NONE), L"", 0); 301 | } 302 | 303 | bool 304 | undo(BUFFER *b, POS *p) 305 | { 306 | if (!b->j) 307 | return false; 308 | if (b->j->a == MA) 309 | pop(b); 310 | bool rc = true; 311 | while (b->j && b->j->a != MA && rc){ 312 | switch (b->j->a){ 313 | case IT: 314 | rc = dodeletetext(b, b->j->p, b->j->n); 315 | break; 316 | case DT: 317 | rc = doinserttext(b, b->j->p, b->j->s, b->j->n); 318 | break; 319 | case IL: 320 | rc = dodeleteline(b, b->j->p.l); 321 | break; 322 | case DL: 323 | rc = doinsertline(b, b->j->p.l) 324 | && doinserttext(b, b->j->p, b->j->s, b->j->n); 325 | break; 326 | case PO: 327 | break; /* just grabbing the position */ 328 | case MA: 329 | break; /* never reached */ 330 | } 331 | memcpy(p, &b->j->p, sizeof(POS)); 332 | pop(b); 333 | } 334 | if (b->j && b->j->a == MA) 335 | pop(b); 336 | return rc; 337 | } 338 | 339 | void 340 | enableundo(BUFFER *b) 341 | { 342 | b->canundo = true; 343 | } 344 | 345 | void 346 | clearundo(BUFFER *b) 347 | { 348 | while (b->j) 349 | pop(b); 350 | } 351 | 352 | bool 353 | settag(BUFFER *b, tag t, POS p1, POS p2, int v) 354 | { 355 | if (t >= TAG_MAX || p1.l == NONE || p1.c == NONE || p2.l == NONE || p2.c == NONE) 356 | return false; 357 | b->tags[t].p1 = p1; 358 | b->tags[t].p2 = p2; 359 | b->tags[t].v = v; 360 | return true; 361 | } 362 | 363 | void 364 | cleartag(BUFFER *b, tag t) 365 | { 366 | if (t < TAG_MAX){ 367 | b->tags[t].p1 = pos(NONE, NONE); 368 | b->tags[t].p2 = pos(NONE, NONE); 369 | } 370 | } 371 | -------------------------------------------------------------------------------- /editor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include CURSES_INCLUDE 5 | 6 | #include "structs.h" 7 | #include "editor.h" 8 | #include "mode.h" 9 | #include "util.h" 10 | 11 | /* The character used to fill an empty background. 12 | * A good choice might be ACS_CKBOARD. 13 | */ 14 | #ifndef EMPTY_BACKGROUND 15 | #define EMPTY_BACKGROUND ' ' 16 | #endif 17 | 18 | #define DEFAULT_TS 3 19 | #define DEFAULT_PH 12 20 | #define DEFAULT_SD 200 21 | static bool 22 | initview(VIEW *v, WINDOW *w, MODE *m, void (*statuscb)(EDITOR *e, VIEW *v)) 23 | { 24 | v->b = openbuffer(); 25 | if (!v->b) 26 | return false; 27 | v->w = w; 28 | v->m = m; 29 | v->bs = v->be = v->rm = NONE; 30 | v->gb = pos(NONE, NONE); 31 | v->lm = 0; 32 | v->ts = DEFAULT_TS; 33 | v->ph = DEFAULT_PH; 34 | v->sd = DEFAULT_SD; 35 | v->statuscb = statuscb; 36 | v->delay = true; 37 | return true; 38 | } 39 | 40 | static void 41 | freeview(VIEW *v) 42 | { 43 | if (v){ 44 | free(v->dl); 45 | closebuffer(v->b); 46 | if (v->w && v->w != stdscr) 47 | delwin(v->w); 48 | } 49 | } 50 | 51 | static void 52 | docstatus(EDITOR *e, VIEW *v) 53 | { 54 | WINDOW *w = e->cmdview.w; 55 | int lines, cols; 56 | 57 | cleartag(e->docview.b, VIRTCURS); 58 | 59 | getmaxyx(w, lines, cols); (void)lines; 60 | char buf[cols + 1]; 61 | memset(buf, 0, sizeof(buf)); 62 | 63 | char rm[25] = {0}, lm[25] = {0}; 64 | if (v->rm != NONE) 65 | snprintf(rm, sizeof(rm) - 1, "%zu", v->rm + 1); 66 | else 67 | snprintf(rm, sizeof(rm) - 1, "?"); 68 | 69 | if (v->ai) 70 | snprintf(lm, sizeof(lm) - 1, ">"); 71 | else 72 | snprintf(lm, sizeof(lm) - 1, "%zu", v->lm == NONE? 1 : v->lm + 1); 73 | 74 | if (e->err[0]) 75 | snprintf(buf, cols, "%s", e->err); 76 | else{ 77 | char *fn = ellipsize(basename(e->name), 12, false); 78 | snprintf(buf, cols, 79 | "%sFile=%-15s Line=%zu/%zu (%d%%) Col=%-5zu Block=%s%s %sTabs=%-2zu %sMargins=%s-%s", 80 | v->b->dirty? "*" : " ", 81 | fn? fn : basename(e->name), 82 | !v->b->n? 0 : v->p.l + 1, v->b->n, 83 | !v->b->n? 0 : (int)(100 * (((float)(v->p.l + 1)) / ((float)v->b->n))), 84 | v->p.c + 1, 85 | v->bs == NONE? "?" : "S", 86 | v->be == NONE? "?" : "E", 87 | v->et? "*" : " ", 88 | v->ts, 89 | v->ex? "*" : " ", 90 | lm, 91 | rm); 92 | free(fn); 93 | } 94 | 95 | werase(w); 96 | mvwprintw(w, 0, 0, "%s", buf); 97 | e->err[0] = 0; 98 | wrefresh(w); 99 | } 100 | 101 | static void 102 | cmdstatus(EDITOR *e, VIEW *v) 103 | { 104 | VIEW *dv = &e->docview; 105 | cleartag(dv->b, VIRTCURS); 106 | int s = dv->p.l >= dv->bs && dv->p.l <= dv->be? A_NORMAL : A_REVERSE; 107 | settag(dv->b, VIRTCURS, dv->p, pos(dv->p.l, dv->p.c + 1), s); 108 | redisplay(dv); 109 | } 110 | 111 | EDITOR * 112 | openeditor(const char *name, WINDOW *docwin, WINDOW *cmdwin) 113 | { 114 | EDITOR *e = calloc(1, sizeof(EDITOR)); 115 | if (!e 116 | || !initview(&e->cmdview, cmdwin, cmdmode, cmdstatus) 117 | || !initview(&e->docview, docwin, docmode, docstatus)){ 118 | closeeditor(e); 119 | return NULL; 120 | } 121 | 122 | e->docview.se = true; 123 | e->lc = pos(NONE, NONE); 124 | e->focusview = &e->docview; 125 | strncpy(e->name, name, FILENAME_MAX); 126 | for (int i = 0; i < CTRL_MAX; i++) 127 | e->ctrlmap[i] = i; 128 | for (int i = 0; i < BM_MAX; i++) 129 | e->bm[i] = pos(NONE, NONE); 130 | e->running = true; 131 | return e; 132 | } 133 | 134 | void 135 | closeeditor(EDITOR *e) 136 | { 137 | if (e){ 138 | for (size_t i = 0; i < FUNC_MAX; i++) 139 | free(e->funcs[i]); 140 | free(e->find); 141 | freeview(&e->cmdview); 142 | freeview(&e->docview); 143 | free(e); 144 | } 145 | } 146 | 147 | static KEYSTROKE 148 | remap(const EDITOR *e, KEYSTROKE k) 149 | { 150 | if (k.o == OK && k.c < CTRL_MAX) 151 | k.c = e->ctrlmap[k.c]; 152 | return k; 153 | } 154 | 155 | void 156 | dispatch(EDITOR *e, VIEW *v, KEYSTROKE i) 157 | { 158 | bool q = v->q && i.o == OK; 159 | KEYSTROKE k = remap(e, i); 160 | ARG a = {.t = ARG_STRING, .n1 = 1, .s1 = q? &i.c : &k.c}; 161 | v->q = false; 162 | callback c = q? cmd_ty : lookupkeystroke(v->m, k, &a); 163 | if (!c) 164 | return; 165 | c(e, v, &a); 166 | } 167 | 168 | static void 169 | reframe(VIEW *v) 170 | { 171 | size_t lines, cols; 172 | getmaxyx(v->w, lines, cols); 173 | 174 | if (v->p.l >= v->tos.l && v->p.l < v->tos.l + lines) 175 | ; /* do nothing */ 176 | else if (v->p.l < v->tos.l && v->tos.l - v->p.l < 5) 177 | v->tos.l -= (v->tos.l - v->p.l); 178 | else if (v->p.l < lines) 179 | v->tos.l = 0; 180 | else if (v->p.l > v->tos.l + (lines - 1) && v->p.l - (v->tos.l + lines - 1) < 5) 181 | v->tos.l += (v->p.l - (v->tos.l + lines - 1)); 182 | else 183 | v->tos.l = v->p.l - 0.33 * lines; 184 | 185 | if (v->p.c - v->tos.c < cols) 186 | ; /* do nothing */ 187 | else if (v->p.c < v->tos.c && v->tos.c - v->p.c < 5) 188 | v->tos.c -= (v->tos.c - v->p.c); 189 | else if (v->p.c < cols) 190 | v->tos.c = 0; 191 | else if (v->p.c > v->tos.c + (cols - 1) && v->p.c - (v->tos.c + cols - 1) < 5) 192 | v->tos.c += (v->p.c - (v->tos.c + cols - 1)); 193 | else 194 | v->tos.c = v->p.c - 0.33 * cols; 195 | } 196 | 197 | static bool 198 | between(POS p, POS a, POS b) 199 | { 200 | return (a.l != NONE && a.c != NONE && b.l != NONE && b.c != NONE) 201 | && (p.l > a.l || (p.l == a.l && p.c >= a.c)) 202 | && (p.l < b.l || (p.l == b.l && p.c < b.c)); 203 | } 204 | 205 | static int 206 | gettag(const BUFFER *b, POS p) 207 | { 208 | for (int i = 0; i < TAG_MAX; i++){ 209 | const TAG *t = b->tags + i; 210 | if (between(p, t->p1, t->p2)) 211 | return t->v; 212 | } 213 | return 0; 214 | } 215 | 216 | void 217 | redisplay(VIEW *v) 218 | { 219 | /* FIXME - this is a mess */ 220 | reframe(v); 221 | 222 | size_t lines, cols, y = 0, x = 0; 223 | getmaxyx(v->w, lines, cols); 224 | 225 | werase(v->w); 226 | size_t l = 0; 227 | for (l = 0; l < lines && v->tos.l + l < v->b->n; l++){ 228 | size_t c = 0, i = 0; 229 | while (c < cols){ 230 | wattrset(v->w, gettag(v->b, pos(v->tos.l + l, v->tos.c + i))); 231 | wmove(v->w, l, c); 232 | if (v->tos.l + l == v->p.l && v->tos.c + i == v->p.c) 233 | getyx(v->w, y, x); 234 | wchar_t w = charat(v->b, pos(v->tos.l + l, v->tos.c + i)); 235 | if (w == '\t'){ 236 | waddch(v->w, ' '); c++; 237 | while (c % v->ts){ 238 | waddch(v->w, ' '); 239 | c++; 240 | } 241 | } else{ 242 | int cw = wcwidth(w) > 0? wcwidth(w) : 0; 243 | wchar_t s[] = {w, 0, 0}; 244 | if (iswcntrl(w)){ 245 | s[0] = L'^'; 246 | s[1] = L'@' + w; 247 | if (wcwidth(s[1]) <= 0) 248 | s[1] = L'?'; 249 | cw = 1 + wcwidth(s[1]); 250 | } 251 | waddwstr(v->w, s); 252 | c+= cw; 253 | } 254 | i++; 255 | } 256 | } 257 | while (l < lines && v->se) 258 | mvwhline(v->w, l++, 0, ' ', cols); 259 | 260 | wmove(v->w, y, x); 261 | wrefresh(v->w); 262 | } 263 | 264 | bool 265 | error(EDITOR *e, const char *s) 266 | { 267 | strncpy(e->err, s, ERR_MAX); 268 | return false; 269 | } 270 | 271 | KEYSTROKE 272 | getkeystroke(EDITOR *e, bool delay) 273 | { 274 | if (delay != e->focusview->delay){ 275 | nodelay(e->focusview->w, !delay); 276 | e->focusview->delay = delay; 277 | } 278 | 279 | KEYSTROKE k = {0}; 280 | wint_t c = 0; 281 | int o = wget_wch(e->focusview->w, &c); 282 | while (o == KEY_CODE_YES && c == KEY_RESIZE){ 283 | redisplay(&e->docview); 284 | if (e->focusview->statuscb) 285 | e->focusview->statuscb(e, e->focusview); 286 | redisplay(e->focusview); 287 | o = wget_wch(e->focusview->w, &c); 288 | } 289 | k.o = c == WEOF? ERR : o; 290 | k.c = c; 291 | return k; 292 | } 293 | 294 | static void 295 | fixviewcursor(VIEW *v) 296 | { 297 | if (v->p.l >= v->b->n) 298 | v->p.l = v->b->n? v->b->n - 1 : 0; 299 | } 300 | 301 | void 302 | fixcursor(EDITOR *e) 303 | { 304 | fixviewcursor(&e->docview); 305 | fixviewcursor(&e->cmdview); 306 | } 307 | -------------------------------------------------------------------------------- /tine.1: -------------------------------------------------------------------------------- 1 | .Dd $Mdocdate$ 2 | .Dt TINE 1 3 | .Os 4 | .Sh NAME 5 | .Nm tine 6 | .Nd a simple text editor 7 | .Sh SYNOPSIS 8 | .Nm 9 | .Op Fl n 10 | .Op Fl r 11 | .Op Fl t 12 | .Ar 13 | .Sh DESCRIPTION 14 | .Nm 15 | is a full-screen text editor. 16 | It is modeled after the venerable 17 | .Em ED 18 | text editor from the TRIPOS and AmigaOS operating systems. 19 | .Pp 20 | .Nm 21 | requires at least one argument: 22 | the name of a file to edit. 23 | This file need not exist when 24 | .Nm 25 | starts; 26 | it will be created when it is saved. 27 | .Pp 28 | Subsequent arguments starting with 29 | .Li "+" 30 | followed by at least one decimal digit are treated as line numbers: 31 | .Nm 32 | will move to the specified line after starting up. 33 | Other arguments are treated as the names of command files 34 | .Pq "that is, files containing sequences of extended commands" 35 | and are executed in order. 36 | .Pp 37 | Note that it is possible to interleave line specifications and extended command files. 38 | .Sh EDITING 39 | .Ss "Quirks" 40 | .Nm 41 | is an attempt to clone the behavior of a much older editor. 42 | It often does not work like a more modern text editor: 43 | in particular, its handling of whitespace, tabs, and newlines is often markedly different. 44 | If you find yourself surprised by the behavior of the editor, 45 | consider it instead a small walk down memory lane. 46 | .Ss "The Display" 47 | .Nm 48 | divides the screen into two areas, 49 | the 50 | .Em file 51 | area and the 52 | .Em "status line" "." 53 | .Pp 54 | The file area displays a the contents of the file being edited. 55 | The file area acts as a 56 | .Dq window 57 | onto the file, 58 | showing a rectangular portion of it. 59 | If the portion of the file shown on screen does not reach the bottom of the file area, 60 | the portion beyond the end of the file is indicated by a gray or checkboard pattern. 61 | .Pp 62 | When editing the file, 63 | the file area is updated in real time to display the contents as they change. 64 | The file on disk is not modified until 65 | .Nm 66 | is told to save the file. 67 | .Pp 68 | Note that 69 | .Nm 70 | does not constrain the cursor to the text of a line. 71 | Text can be typed anywhere on a line, 72 | and 73 | .Nm 74 | will automatically insert spaces as appropriate. 75 | .Pp 76 | Also note that, 77 | by default, 78 | the 79 | .Em Tab 80 | key does not insert tab or space characters, but only advances the cursor. 81 | .Pp 82 | The status line shows information about the current editing session. 83 | It contains the following fields: 84 | .Bl -tag -width Ds 85 | .It "File=" 86 | The name of the file being edited. 87 | If there are changes that have not yet been saved, 88 | this field is prefixed with an asterisk. 89 | Long filenames will be ellipsized for display. 90 | .It "Line=" 91 | The line number of the file on which the cursor is positioned. 92 | .It "Col=" 93 | The column number of the file in which the cursor is positioned. 94 | .It "Block=" 95 | If no block is defined 96 | .Po 97 | see the 98 | .Ic BB "," 99 | .Ic BS "," 100 | and 101 | .Ic BE 102 | extended commands 103 | .Pc "," 104 | this field will display 105 | .Li "??" "." 106 | If the starting line of a block has been defined, 107 | the first question mark will be replaced by 108 | .Li "S" "." 109 | If the ending line of a block has been defined, 110 | the second question mark will be replaced by 111 | .Li "E" "." 112 | .It "Tabs=" 113 | Displays a number indicating how many spaces the tab key will advance the cursor. 114 | This number can be adjusted using the 115 | .Ic ST 116 | extended command. 117 | If the tab key has been configured to insert tabs rather than move the cursor 118 | .Po 119 | see the 120 | .Ic CT 121 | and 122 | .Ic ET 123 | extended commands 124 | .Pc "," 125 | this field will be prefixed with an asterisk. 126 | .It "Margins=" 127 | This field displays the current margins. 128 | The first number represents the left margin 129 | .Pq "which always has a value" "," 130 | and the second number represents the right margin. 131 | The left margin is 1 if it is not otherwise specified; 132 | the right margin is displayed as 133 | .Li "?" 134 | if not specified. 135 | The margins can be set via the 136 | .Ic SL 137 | and 138 | .Ic SR 139 | extended commands, 140 | and reset to their defaults using the 141 | .Ic RM 142 | command. 143 | .Pp 144 | If the right margin on the current line is currently ignored 145 | .Po 146 | see the 147 | .Ic EX 148 | command 149 | .Pc "," 150 | this field will be prefixed with an asterisk. 151 | .Pp 152 | If auto-indent is currently enabled 153 | .Po 154 | see the 155 | .Ic AI 156 | and 157 | .Ic NI 158 | commands 159 | .Pc "," 160 | the left margin is displayed as 161 | .Li ">" "." 162 | .El 163 | .Pp 164 | If an error occurs, the contents of the status line are replaced with an error message 165 | until the next keystroke. 166 | .Pp 167 | By default, 168 | the status line appears at the bottom of the display. 169 | By starting 170 | .Nm 171 | with the 172 | .Fl t 173 | flag, 174 | the status line will appear at the top. 175 | .Pp 176 | By default, 177 | the file area is displayed in normal video and the status line 178 | appears in reverse video. 179 | By starting 180 | .Nm 181 | with the 182 | .Fl r 183 | flag, 184 | this is reversed. 185 | .Ss "Word Wrap and Margins" 186 | .Nm 187 | maintains a left and right margin, 188 | settable with the 189 | .Ic SL 190 | and 191 | .Ic SR 192 | commands respectively. 193 | New lines will by default start offset to the left margin 194 | .Pq "though the cursor can be moved to before the left margin if desired" 195 | and words will wrap at the right margin. 196 | .Nm 197 | supports only simple wrapping: 198 | long words with no spaces that consume the entire width of a line between the margins will not be wrapped. 199 | .Ss "Startup Files" 200 | On startup, 201 | .Nm 202 | will execute the file 203 | .Pa "${XDG_CONFIG_HOME}/.tine/tinerc" 204 | if it exists, 205 | with 206 | .Ev "${XDG_CONFIG_HOME}" 207 | defaulting to 208 | .Pa "${HOME}/.config" "." 209 | If that file does not exist, 210 | the directories named in 211 | .Ev "${XDG_CONFIG_DIRS}" 212 | are inspected similarly, 213 | with 214 | .Ev "${XDG_CONFIG_DIRS}" 215 | defaulting to 216 | .Pa "/etc/xdg" "." 217 | .Pp 218 | A file of the form 219 | .Pa "tinerc.EXT" "," 220 | where 221 | .Em EXT 222 | is the 223 | .Dq "extension" 224 | of the filename 225 | .Pq "the part after the last '.' character" 226 | is also executed if it exists. 227 | If the named file has no extension, 228 | the whole filename is treated as the extension. 229 | .Pp 230 | In startup files, blank lines are ignored. 231 | Lines whose first non-whitespace character is a single quote are considered commentary. 232 | .Pp 233 | Calling 234 | .Nm 235 | with the 236 | .Fl n 237 | flag suppresses the automatic exection of these files. 238 | .Ss "The Block" 239 | Some contiguous sequence of lines in the file can be specially marked; 240 | these lines are referred to as 241 | .Dq "the block" "." 242 | The block can be manipulated in various ways, 243 | and several extended commands refer to it in some way. 244 | Note that the block is cleared 245 | .Pq "that is, lines are no longer specially marked" 246 | when a file is modified in certain ways. 247 | In general, any command that inserts or deletes a line will clear the block. 248 | An exception to this is the 249 | .Ic IB 250 | command, 251 | which inserts a copy of the block elsewhere in the file. 252 | .Pp 253 | Lines that are part of the block are displayed in reverse video. 254 | Note that it is not possible to have the block begin or end in the middle of a line. 255 | .Ss "Immediate Mode" 256 | When 257 | .Nm 258 | starts, 259 | it is in 260 | .Em immediate 261 | mode. 262 | In immediate mode, 263 | as the name implies, 264 | commands are executed immediately. 265 | Most of these 266 | .Dq "commands" 267 | are simply the normal actions of editing: 268 | inserting characters by typing them on the keyboard, 269 | moving the cursor in various directions via the cursor movement keys, 270 | and so on. 271 | .Pp 272 | Some commands, 273 | however, 274 | are entered by pressing the control key and one other key simultaneously. 275 | These commands perform more complicated manipulations on the file being edited. 276 | .Ss "Immediate Mode Commands" 277 | The following commands are available in immediate mode: 278 | .Bl -tag -width Ds 279 | .It Tab 280 | By default, advances the cursor to the next tab stop 281 | .Po 282 | it does 283 | .Em not 284 | insert a tab character 285 | .Pc "." 286 | This behavior can be modified by the 287 | .Ic ET 288 | command, 289 | which causes 290 | .Em Tab 291 | to insert actual tab characters. 292 | The width of tabstops is controlled by the 293 | .Ic ST 294 | command. 295 | .It Backspace 296 | Deletes the character to the left of the cursor. 297 | .It Insert 298 | Inserts a new line above the current line. 299 | .It Home 300 | Moves to the beginning of the first line. 301 | .It End 302 | Moves to the beginning of the last line. 303 | .It Up / Down / Left / Right 304 | Move one unit in the specified direction. 305 | .It Shift-Left / Shift-Right 306 | Move to the start/end of the current line. 307 | .It Enter 308 | Insert a new line at the cursor. 309 | .It Next / Previous Page 310 | Move up/down one page. 311 | .It F1 - F10 312 | Executes the extended command assigned to the given key. 313 | .It Escape 314 | Enters extended command mode. 315 | .It Ctrl-] 316 | Go to the end of the line or, if there, to the start. 317 | .It Ctrl-A 318 | Inserts a line below the current one. 319 | .It Ctrl-B 320 | Deletes the current line. 321 | .It Ctrl-D 322 | Moves down one page. 323 | .It Ctrl-E 324 | Moves to the top of the screen or, if there, to the bottom. 325 | .It Ctrl-F 326 | Flips the case of the character under the cursor. 327 | .It Ctrl-G 328 | Repeats the last extended mode command. 329 | .It Ctrl-K 330 | Sets a block mark. 331 | Executing this command twice will mark a block without needing to resort to extended commands. 332 | .It Ctrl-L 333 | Inserts a copy of the last deleted line. 334 | .It Ctrl-N 335 | Joins the current and next line. 336 | .It Ctrl-O 337 | Deletes the word/spaces immediately following the cursor. 338 | .It Ctrl-P 339 | If the cursor is on an opening or closing bracket, 340 | jump to the corresponding opening or closing bracket. 341 | .It Ctrl-Q 342 | Quote the next character typed. 343 | That is, insert the next character literally, even if it would normally be a command. 344 | .It Ctrl-R 345 | Move to the space following the previous word. 346 | .It Ctrl-T 347 | Move to the first character of the next word. 348 | .It Ctrl-U 349 | Move up one page. 350 | .It Ctrl-W 351 | Delete the previous word. 352 | .It Ctrl-V 353 | Redraw the screen. 354 | .It Ctrl-Y 355 | Delete from the cursor to the end of the current line. 356 | .It Ctrl-Z 357 | Go back to previous position before the last large cursor movement. 358 | .El 359 | .Pp 360 | Note that any of the sequences above that are prefixed with 361 | .Em Ctrl 362 | can be remapped using the 363 | .Ic MC 364 | extended command. 365 | .Sh "Extended Mode Commands" 366 | .Nm 367 | has a powerful editing command language. 368 | Extended mode commands can be entered while editing by pressing 369 | .Li Escape 370 | and then entering the command on the command line 371 | .Pq "which temporarily replaces the status line on the display" "." 372 | .Pp 373 | Most 374 | .Pq "but not all" 375 | of the immediate mode commands above can be used when editing an extended command. 376 | Pressing 377 | .Em Enter 378 | will execute the command line and return to immediate mode, 379 | while pressing 380 | .Em Escape 381 | or 382 | .Em Ctrl-J 383 | will execute the command line and remain in extended command mode. 384 | .Em Ctrl-C 385 | will cancel the current command and return to immediate mode. 386 | .Pp 387 | While in extended command mode, 388 | a virtual cursor is displayed in reverse video in the file area for reference. 389 | This is most useful when executing commands by pressing escape and remaining in extended command mode. 390 | .Pp 391 | .Nm 392 | maintains a history of extended commands executed, 393 | and this history can be browsed using the up and down arrow keys to move backwards and forwards in history. 394 | Earlier commands can be edited before executing them. 395 | .Ss "The Extended Mode Command Language" 396 | An extended command looks like: 397 | .Bd -literal -offset indent 398 | repeat-count command-name argument 399 | .Ed 400 | .Pp 401 | The 402 | .Li "repeat-count" 403 | is specified in decimal, 404 | and specifies how many times the following command should be repeated 405 | It is optional, 406 | the default is 1. 407 | A repeat count of 408 | .Li RP 409 | means that the command should be repeated until it fails. 410 | The 411 | .Li RP 412 | specification is not case-sensitive. 413 | .Pp 414 | The command name is required, and consists of one or two letters. 415 | Command names are not case-sensitive. 416 | A listing of extended mode commands is available below. 417 | .Pp 418 | Each command takes exactly zero or one argument of a given type, 419 | though some commands provide a useful default if it is not specified. 420 | Arguments can be strings, numbers, or two strings together. 421 | Strings can be delimited by any non-alphanumeric character except semicolon and parentheses. 422 | This allows the use of a delimiter character that does not appear in the string itself. 423 | .Pp 424 | Multiple commands can be specified on the same command line by separating them with semicolons. 425 | Multiple commands can be grouped into one logical command by enclosing them in parentheses. 426 | .Pp 427 | Pressing any key while an extended command is running will abort execution. 428 | .Pp 429 | Whitespace can be omitted where its absence does not result in any ambiguity. 430 | This includes whitespace between commands and arguments, 431 | or whitespace between numeric repeat counts and command names. 432 | .Pp 433 | Below is a list of what extended mode commands are available; 434 | in their descriptions, 435 | .Li n 436 | refers to a numeric argument; 437 | .Li "/s/" 438 | to a string argument with 439 | .Li "/" 440 | representing the delimiter character; 441 | and 442 | .Li "/s/t/" 443 | representing two strings together with 444 | .Li "/" 445 | again representing the delimiter. 446 | The final delimiter in a string or strings can be elided at the end of a command line. 447 | .Bl -tag -width Ds 448 | .It "A/s/" 449 | .Dq "After" 450 | Insert a line after the current line, containing the string 451 | .Ar s "." 452 | .It "AI" 453 | .Dq "Auto-Indent" 454 | Enable auto-indent mode. 455 | .It "B" 456 | .Dq "Bottom" 457 | Moves to the bottom of the file. 458 | .It "BB" 459 | .Dq "Block Both" 460 | Sets the current line to the beginning or end of the block, 461 | as appropriate. 462 | .It "BE" 463 | .Dq "Block End" 464 | Specifies that the block should end at the current line. 465 | .It "BF/s/" 466 | .Dq "Backwards Find" 467 | Searches backwards through the file for the given string. 468 | If unspecified, the last string used in a 469 | .Ic BF "," 470 | .Ic F "," 471 | .Ic E "," 472 | or 473 | .Ic "EQ" 474 | command is reused. 475 | .It "BM n" 476 | .Dq "BookMark" 477 | Set bookmark 478 | .Ar n 479 | to the current cursor position. 480 | .Ar n 481 | must be between one and ten, inclusive. 482 | .It "BS" 483 | .Dq "Block Start" 484 | Specifies that the block should start at the current line. 485 | .It "BT" 486 | .Dq "Back Tab" 487 | Move the cursor to the previous tab position. 488 | .It "CB" 489 | .Dq "Clear Block" 490 | Clears the block. 491 | .It "CD" 492 | .Dq "Cursor Down" 493 | Move the cursor down one line without changing its column. 494 | .It "CE" 495 | .Dq "Cursor End" 496 | Move the cursor to the end of the current line. 497 | .It "CF n" 498 | .Dq "Call Function" 499 | Call the extended command bound to function key 500 | .Ar n "." 501 | .Ar n 502 | must be between one and ten, inclusive. 503 | .It "CJ" 504 | .Dq "Cursor Jump 505 | Move the cursor to the end of the current line; 506 | if it is already there, move it to the beginning. 507 | .It "CL" 508 | .Dq "Cursor Left" 509 | Move the cursor left one screen position. 510 | .It "CR" 511 | .Dq "Cursor Right" 512 | Move the cursor right one screen position. 513 | .It "CS" 514 | .Dq "Cursor Start" 515 | Move the cursor to the start of the line. 516 | .It "CT" 517 | .Dq "Collapse Tabs" 518 | Cause the tab key to advance the cursor without inserting any characters. 519 | .It "CU" 520 | .Dq "Cursor Up" 521 | Move the cursor up one line without changing its column. 522 | .It "D" 523 | .Dq "Delete" 524 | Delete the current line. 525 | .It "DB" 526 | .Dq "Delete Block" 527 | Delete the block. 528 | .It "DC" 529 | .Dq "Delete Character" 530 | Delete the character under the cursor. 531 | .It "DF" 532 | .Dq "Display Functions" 533 | Display the extended commands bound to the function keys. 534 | .It "DL" 535 | .Dq "Delete Left" 536 | Delete the character to the left of the cursor. 537 | .It "DO/s/" 538 | .Dq "DO command" 539 | Temporarily suspend 540 | .Nm 541 | and execute 542 | .Ar s 543 | as an operating system command. 544 | .It "DP" 545 | .Dq "Delete Previous" 546 | Delete the word or spaces preceding the cursor. 547 | .It "DW" 548 | .Dq "Delete Word" 549 | Delete the word or spaces following the cursor. 550 | .It "E/s/t/" 551 | .Dq "Exchange" 552 | Exchange the next instance of 553 | .Ar s 554 | with 555 | .Ar t "." 556 | This is generally useful with a repetition count. 557 | .It "EL" 558 | .Dq "Erase in Line" 559 | Delete to the end of the line. 560 | .It "EP" 561 | .Dq "End Page" 562 | Move to the beginning of the text on the screen or, 563 | if already there, 564 | to the end of the text on the screen. 565 | .It "EQ" 566 | .Dq "Exchange with Query" 567 | Like 568 | .Ic 569 | but the user is prompted before each exchange action. 570 | Replying 571 | .Li n 572 | will not exchange the given instance. 573 | .It "ET" 574 | .Dq "Expand Tabs" 575 | Cause the tab key to insert literal tab characters. 576 | .It "EX" 577 | .Dq "EXpand margins" 578 | Ignore the right-hand margin for this line. 579 | This effect is canceled when the cursor leaves the current line. 580 | .It "F/s/" 581 | .Dq "Find" 582 | Search forwards through the file for string 583 | .Ar s "." 584 | If 585 | .Ar s 586 | is omitted, 587 | the last string searched for in a 588 | .Ic F "," 589 | .Ic BF "," 590 | .Ic E "," 591 | or 592 | .Ic EQ 593 | command is used. 594 | .It "FB/s/" 595 | .Dq "Filter Block" 596 | Filter block through operating system command 597 | .Ar s "." 598 | The existing block is passed as the command's standard input, 599 | and is replaced with the command's standard output. 600 | .It "FC" 601 | .Dq "Flip Case" 602 | Flip the case of the character under the cursor, 603 | and move one position to the right. 604 | .It "GB" 605 | .Dq "Go Back" 606 | Returns to the previous location, 607 | before any long-distance movement commands. 608 | .It "GM n" 609 | .Dq "Go to Mark" 610 | Go to bookmark 611 | .Ar n "." 612 | .It "I/s/" 613 | .Dq "Insert" 614 | Insert a line above the current line containing the string 615 | .Ar s "." 616 | .It "IB" 617 | .Dq "Insert Block" 618 | Insert a copy of the block at the current line. 619 | Unlike most actions that insert lines into the file, 620 | this does not clear the block. 621 | .It "IF/s/" 622 | .Dq "Insert File" 623 | Insert the contents of file 624 | .Ar s 625 | at the current cursor position. 626 | .It "IM" 627 | .Dq "Ignore Match" 628 | Disable 629 | .Dq "show matching brace" 630 | mode; see the 631 | .Ic "MS" 632 | command for details. 633 | .It "J" 634 | .Dq "Join" 635 | Join the current line and the next. 636 | .It "LC" 637 | .Dq "LowerCase" 638 | Cause subsequent 639 | .Ic F "," 640 | .Ic BF "," 641 | .Ic E "," 642 | and 643 | .Ic "EQ" 644 | commands to ignore case while searching. 645 | .It "M n" 646 | .Dq "Move" 647 | Move to line 648 | .Ar n "." 649 | .It "MC/s/t/" 650 | .Dq "Map Control" 651 | Cause 652 | .Nm 653 | to interpret the pressing of 654 | .Li Ctrl-s 655 | as if 656 | .Li Ctrl-t 657 | had been pressed. 658 | In this case, 659 | .Ar s 660 | and 661 | .Ar t 662 | must be single-character strings. 663 | .It "MS" 664 | .Dq "Match Show" 665 | Enable 666 | .Dq "show matching brace" 667 | mode. 668 | In this mode, 669 | when typing a brace character, 670 | the cursor will briefly move to the matching brace character if available, 671 | similar to if the 672 | .Ic "SB" 673 | command had been invoked. 674 | .It "N" 675 | .Dq "Next line" 676 | Move to the beginning of the next line. 677 | .It "NI" 678 | .Dq "Normal Indent" 679 | Disable auto-indent mode. 680 | .It "P" 681 | .Dq "Previous line" 682 | Move to the beginning of the previous line. 683 | .It "PD" 684 | .Dq "Page Down" 685 | Move down one page. 686 | .It "PH n" 687 | .Dq "Page Height" 688 | Set the number of lines in a page to 689 | .Ar n "." 690 | The default is 12. 691 | .It "PU" 692 | .Dq "Page Up" 693 | Move up one page. 694 | .It "Q" 695 | .Dq "Quit" 696 | Quit without saving. 697 | If the file has unsaved changes, the user is prompted to confirm. 698 | .It "QY" 699 | .Dq "Quit, answer Yes" 700 | Quit without saving. 701 | No prompting is done if there are unsaved changes. 702 | .It "RD" 703 | .Dq "Restore Deleted" 704 | Insert a copy of the last line deleted with the 705 | .Li "Ctrl-B" 706 | or 707 | .Ic "D" 708 | commands. 709 | .It "RF/s/" 710 | .Dq "Run File" 711 | Read file 712 | .Ar "s" 713 | and execute its contents as a sequence of 714 | .Nm 715 | extended commands. 716 | .It "RM" 717 | .Dq "Reset Margins" 718 | Reset the margins to their defaults 719 | .Pq "that is, 1 for the left margin and undefined for the right" "." 720 | .It "S" 721 | .Dq "Split" 722 | Split the current line at the cursor position. 723 | .It "SA/s/" 724 | .Dq "SAve" 725 | Save the file to the filename 726 | .Ar s "." 727 | If 728 | .Ar s 729 | is omitted, 730 | the name given to 731 | .Nm 732 | at startup is used. 733 | .It "SB" 734 | .Dq "Show Block" 735 | Move the display such that the first line of the block is visible on the screen. 736 | .It "SD n" 737 | .Dq "Set Delay" 738 | Set the time used to show matching brackets 739 | .Po 740 | see the 741 | .Ic "MS" 742 | command 743 | .Pc 744 | to 745 | .Ar n 746 | milliseconds. 747 | The default is 200. 748 | .It "SF/s/t/" 749 | .Dq "Set Function" 750 | Set function key 751 | .Ar s 752 | to the extended command 753 | .Ar t "." 754 | Note that 755 | .Ar s 756 | must be a decimal number between one and ten, inclusive. 757 | .It "SH" 758 | .Dq "SHow" 759 | Display some information about the current state of the editor. 760 | .It "SL n" 761 | .Dq "Set Left" 762 | Set the left margin to column 763 | .Ar n "." 764 | If 765 | .Ar n 766 | is omitted, use the current cursor column. 767 | .It "SM" 768 | .Dq "Show Matching" 769 | If the cursor is over a bracket character, 770 | move to the matching bracket character. 771 | .It "SR n" 772 | .Dq "Set Right" 773 | Set the right margin to column 774 | .Ar n "." 775 | If 776 | .Ar n 777 | is omitted, use the current cursor column. 778 | .It "ST n" 779 | .Dq "Set Tab" 780 | Set the tab distance to 781 | .Ar n 782 | columns. 783 | This is the number of columns advanced by the tab key when it is not configured to insert literal tabs, 784 | and the number of spaces literal tabs will take up when displayed on the screen. 785 | .It "T" 786 | .Dq "Top" 787 | Move to the top of the file. 788 | .It "TB" 789 | .Dq "TaB" 790 | Move to the next tabstop or, 791 | if the tab key is configured to insert literal tabs, 792 | insert a tab. 793 | .It "TY/s/" 794 | .Dq "TYpe" 795 | Insert the string 796 | .Ar s 797 | as if it had been typed at the keyboard. 798 | .It "U" 799 | .Dq "Undo" 800 | Undo the last file modification. 801 | .It "UC" 802 | .Dq "UpperCase" 803 | Cause subsequent 804 | .Ic F "," 805 | .Ic BF "," 806 | .Ic E "," 807 | and 808 | .Ic "EQ" 809 | commands to respect case while searching. 810 | .It "WB/s/" 811 | .Dq "Write Block" 812 | Write the contents of the block to the file 813 | .Ar s "." 814 | .It "WN" 815 | .Dq "Word Next" 816 | Move to the first character of the next word. 817 | .It "WP" 818 | .Dq "Word Previous" 819 | Move to the space following the last character of the previous word. 820 | .It "X" 821 | .Dq "eXit" 822 | Exit, saving any changes. 823 | No prompting is performed. 824 | .It "XQ" 825 | .Dq "eXit with Query" 826 | Exit, prompting to save first if the file has been changed. 827 | .El 828 | .Sh ENVIRONMENT 829 | .Bl -tag -width Ds 830 | .It Ev TERM 831 | Indciates the terminal type under which 832 | .Nm 833 | is running. 834 | .It Ev ESCDELAY 835 | This variable specifies the number of milliseconds 836 | .Nm 837 | will wait after seeing an escape character for a special character sequence to complete. 838 | By default, this is 1000 839 | .Pq "one second" "." 840 | .It Ev LC_CTYPE Ev LC_ALL Ev LANG 841 | These variables are consulted to determine the encoding used for textual data. 842 | .It Ev HOME Ev XDG_CONFIG_HOME Ev XDG_CONFIG_DIRS 843 | These variables are consulted to determine paths for startup files. 844 | .Sh FILES 845 | .Bl -tag -width Ds 846 | .It ".../.tine/tinerc" 847 | This file is automatically executed at startup. 848 | It is located using the XDG Specification for configuration files. 849 | .It ".../.tine/.tinerc.EXT" 850 | This file is automatically executed if the extension of the filename passed at startup matches 851 | .Li EXT "." 852 | If the passed filename has no extension, the whole filename is treated as the extension. 853 | .El 854 | .Sh EXAMPLES 855 | The following extended command will mimic the pre-AmigaDOS 2.0 meanings of the 856 | .Li Ctrl-U 857 | and 858 | .Li Ctrl-D 859 | commands: 860 | .Bd -literal 861 | MC/U/D/;MC/D/U/ 862 | .Ed 863 | .Pp 864 | The following extended command will move to the top of the file, 865 | make searches case-insensitive, 866 | and then find and exchange each instance of 867 | .Li foo 868 | with 869 | .Li bar "," 870 | prompting the user before each exchange, 871 | and then inserting the text 872 | .Li "baz" 873 | before the 874 | .Pq "possibly exchanged" 875 | text: 876 | .Bd -literal 877 | T;RP(EQ/foo/bar/;TY/baz/) 878 | .Ed 879 | .Pp 880 | This command might be useful to place in the 881 | .Pa "${XDG_CONFIG_HOME}/tine/tinerc.Makefile" 882 | file to set expanded tab mode automatically when editing makefiles: 883 | .Bd -literal 884 | ET 885 | .Ed 886 | .Pp 887 | This sets the F3 key to preview the file being edited as a man page: 888 | .Bd -literal 889 | SF%3%T;BS;B;BE;WB"/tmp/mtmp";CB;DO"man /tmp/mtmp;rm -f /tmp/mtmp";GB% 890 | .Ed 891 | .Pp 892 | This marks the whole file as the block by going to the top and setting the block start, 893 | going to the bottom and setting the block end, 894 | writing the block to a temporary file, 895 | clearing the block, 896 | calling the 897 | .Xr man 1 898 | command to display the man page and the 899 | .Xr rm 1 900 | command to delete the temporary file, 901 | and then finally returning the cursor to the remembered position before all this happend. 902 | .Sh HISTORY 903 | .Nm 904 | is a modern-day attempt to clone the 905 | .Em ED 906 | display editor from MetaComCo 907 | .Po 908 | .Do 909 | .Nm 910 | .Dc 911 | is a rather feeble attempt at a recursive acronym: 912 | .Dq "tine Is Not ED" 913 | .Pc "." 914 | .Pp 915 | .Em ED 916 | was written in the early 1980's as a display editor for the Cambridge TRIPOS operating system. 917 | TRIPOS later formed the core of AmigaDOS; 918 | .Em ED 919 | came along with it. 920 | .Pp 921 | .Nm 922 | shares no code with 923 | .Em ED "," 924 | nor does anyone involved with MetaComCo, the University of Cambridge, Amiga, or TRIPOS endorse or have anything to do with this project... 925 | though the author wishes to extend his heartfelt thanks to each of them for many years of enjoyable hacking. 926 | .Sh BUGS 927 | The only language in which output is produced and documentation is provided is English, 928 | regardless of the user's preferred language. 929 | .Pp 930 | While 931 | .Nm 932 | handles nonspacing and combining characters reasonably well, 933 | there is no support for right-to-left or bidirectional text, 934 | nor is there any support for more complex textual forms that are common in many languages. 935 | .Pp 936 | The screen update algorithm is wasteful of resources; 937 | a much more efficient mechanism should be used. 938 | .Pp 939 | There is no support for file locking and nothing prevents two instances of 940 | .Nm 941 | from modifying the same file concurrently. 942 | .Pp 943 | History browsing in the extended command line is a little nonintuitive. 944 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /command.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include CURSES_INCLUDE 14 | 15 | #include "structs.h" 16 | #include "buffer.h" 17 | #include "command.h" 18 | #include "editor.h" 19 | #include "parser.h" 20 | #include "util.h" 21 | 22 | /* UTILITY FUNCTIONS */ 23 | static bool 24 | prompt(EDITOR *e, const char *p) 25 | { 26 | werase(e->cmdview.w); 27 | mvwaddstr(e->cmdview.w, 0, 0, p); 28 | wrefresh(e->cmdview.w); 29 | KEYSTROKE k = getkeystroke(e, true); 30 | werase(e->cmdview.w); 31 | return k.c == L'y' || k.c == L'Y'; 32 | } 33 | 34 | static void 35 | fixblock(VIEW *v) 36 | { 37 | cleartag(v->b, BLOCK); 38 | if (v->bs == NONE || v->be == NONE) 39 | return; 40 | if (v->bs > v->be){ 41 | lineno l = v->bs; 42 | v->bs = v->be; 43 | v->be = l; 44 | } 45 | settag(v->b, BLOCK, pos(v->bs, 0), pos(v->be, SIZE_MAX - 1), A_REVERSE); 46 | } 47 | 48 | static bool 49 | setfind(EDITOR *e, const wchar_t *s, size_t n) 50 | { 51 | free(e->find); 52 | e->find = NULL; 53 | e->findn = 0; 54 | 55 | if (n){ 56 | e->find = dupstr(s, n); 57 | if (!e->find) 58 | return error(e, "Out of memory"); 59 | e->findn = n; 60 | } 61 | return true; 62 | } 63 | 64 | static wint_t 65 | identity(wint_t wc) 66 | { 67 | return wc; 68 | } 69 | 70 | static bool 71 | find(EDITOR *e, VIEW *v, POS op, bool r) 72 | { 73 | BUFFER *b = v->b; 74 | bool (*iter)(const BUFFER *, POS *) = r? prev : next; 75 | bool (*atend)(const BUFFER *b, POS) = r? attop : atbot; 76 | wint_t (*xform)(wint_t w) = v->uc? towupper : identity; 77 | if (!e->find || !e->findn) 78 | return error(e, "Empty target"); 79 | 80 | cleartag(b, HIGHLIGHT); 81 | do{ 82 | if (!iter(b, &op)) 83 | return error(e, "Search failed"); 84 | POS p = op; 85 | size_t i = 0; 86 | while (i < e->findn && xform((wint_t)e->find[i]) == xform(charat(b, p))){ 87 | i++; 88 | next(b, &p); 89 | } 90 | if (i == e->findn){ 91 | v->p = op; 92 | settag(b, HIGHLIGHT, op, p, A_UNDERLINE | A_BOLD); 93 | if (e->focusview == &e->cmdview) 94 | settag(b, VIRTCURS, op, pos(op.l, op.c + 1), A_REVERSE); 95 | redisplay(&e->docview); 96 | return true; 97 | } 98 | } while (!atend(b, op)); 99 | 100 | return error(e, "Search failed"); 101 | } 102 | 103 | static bool 104 | exchange(EDITOR *e, VIEW *v, const ARG *a, bool query) 105 | { 106 | if (a->n1 && !setfind(e, a->s1, a->n1)) 107 | return false; 108 | if (!find(e, v, v->p, false)) 109 | return false; 110 | if (!query || prompt(e, "Exchange?")){ 111 | if (!deletetext(v->b, v->p, a->n1) 112 | || !inserttext(v->b, v->p, a->s2, a->n2)) 113 | return false; 114 | } 115 | v->p.c += a->n2; 116 | return true; 117 | } 118 | 119 | static size_t 120 | trimlength(const LINE *l) 121 | { 122 | size_t n = l->n; 123 | while (n && iswspace(l->s[n - 1])) 124 | n--; 125 | return n; 126 | } 127 | 128 | static bool 129 | safewrite(int fd, const char *s, size_t n) 130 | { 131 | ssize_t nw = 0; 132 | while ((size_t)nw < n){ 133 | ssize_t w = write(fd, s + nw, n - nw); 134 | if (w < 0) 135 | return false; 136 | nw += w; 137 | } 138 | return true; 139 | } 140 | 141 | static bool 142 | writelines(int fd, EDITOR *e, const BUFFER *b, lineno ls, lineno le) 143 | { 144 | char *s = NULL; 145 | bool rc = true; 146 | for (lineno l = ls; rc && b->n && l <= le; l++){ 147 | if (b->l[l].n){ 148 | size_t n = trimlength(&b->l[l]); 149 | if (n){ 150 | s = wstos(b->l[l].s, n); 151 | if (!s) 152 | return close(fd), error(e, "Out of memory"); 153 | if (!(rc = safewrite(fd, s, strlen(s)))){ 154 | int err = errno; 155 | free(s); 156 | close(fd); 157 | return error(e, strerror(err)); 158 | } 159 | free(s); 160 | } 161 | } 162 | safewrite(fd, "\n", strlen("\n")); 163 | } 164 | close(fd); 165 | return true; 166 | } 167 | 168 | /* COMMAND DEFINITIONS */ 169 | enum{ 170 | NOFLAGS = 0, 171 | MARK = 1L<<0, 172 | CLEARSBLOCK = 1L<<1, 173 | NEEDSLINES = 1L<<2, 174 | NEEDSBLOCK = 1L<<3, 175 | SETSHILITE = 1L<<4, 176 | NOLOCATOR = 1L<<5 177 | }; 178 | 179 | #define COMMAND(name, fflags) \ 180 | bool \ 181 | cmd_ ## name (EDITOR *e, VIEW *v, const ARG *a) \ 182 | { \ 183 | long flags = fflags; \ 184 | BUFFER *b = v->b; (void)b; \ 185 | POS p = v->p; (void)p; \ 186 | lineno ol = p.l; \ 187 | bool haslines = v->b->n; (void)haslines; \ 188 | bool rc = true; \ 189 | if ((flags & NEEDSBLOCK) && (v->bs == NONE || v->be == NONE || !haslines)) \ 190 | ERROR("No block marked"); \ 191 | if (flags & MARK) \ 192 | mark(v->b); \ 193 | 194 | #define END \ 195 | goto endfunc; \ 196 | endfunc: \ 197 | fixblock(&e->docview); \ 198 | fixcursor(e); \ 199 | if (flags & CLEARSBLOCK){ \ 200 | cleartag(v->b, BLOCK); \ 201 | v->bs = v->be = NONE; \ 202 | } \ 203 | if (!(flags & SETSHILITE)) \ 204 | cleartag(v->b, HIGHLIGHT); \ 205 | if (v->p.l != ol) \ 206 | v->ex = false; \ 207 | if (!(flags & NOLOCATOR)) \ 208 | v->gb = v->p; \ 209 | return rc; \ 210 | } 211 | 212 | #define RETURN(x) do{rc = (x); goto endfunc;}while(false) 213 | #define ERROR(s) RETURN(error(e, s)) 214 | #define SUCCEED do{error(e, ""); RETURN(true);}while(false) 215 | #define FAIL RETURN(false) 216 | 217 | COMMAND(a, MARK | CLEARSBLOCK) /* insert line after current */ 218 | RETURN(insertline(b, p.l + 1) && inserttext(b, pos(p.l + 1, 0), a->s1, a->n1)); 219 | END 220 | 221 | COMMAND(ai, NOLOCATOR) /* autoindent */ 222 | v->ai = true; 223 | END 224 | 225 | COMMAND(b, MARK | NOLOCATOR) /* move to bottom of file */ 226 | v->p = pos(b->n? b->n - 1 : 0, 0); 227 | END 228 | 229 | COMMAND(bb, NOLOCATOR) /* smart mark of block */ 230 | if (v->bs == NONE) 231 | v->bs = p.l; 232 | else if (v->be == NONE) 233 | v->be = p.l; 234 | else{ 235 | v->be = NONE; 236 | v->bs = p.l; 237 | } 238 | END 239 | 240 | COMMAND(be, NOLOCATOR) /* block end at cursor line */ 241 | v->be = p.l; 242 | END 243 | 244 | COMMAND(bf, MARK | SETSHILITE | NOLOCATOR) /* backwards find */ 245 | RETURN((!a->n1 || setfind(e, a->s1, a->n1)) && find(e, v, p, true)); 246 | END 247 | 248 | COMMAND(bm, NOLOCATOR) /* set bookmark */ 249 | if (a->n1 == 0 || a->n1 > BM_MAX) 250 | ERROR("Invalid bookmark"); 251 | e->bm[a->n1 - 1] = p; 252 | END 253 | 254 | COMMAND(bs, NOLOCATOR) /* block start at cursor line */ 255 | v->bs = p.l; 256 | END 257 | 258 | COMMAND(bt, NOLOCATOR) /* backwards tab */ 259 | if (!cmd_cl(e, v, a)) 260 | FAIL; 261 | while (v->p.c > 0 && v->p.c % v->ts){ 262 | if (!cmd_cl(e, v,a )) 263 | FAIL; 264 | } 265 | END 266 | 267 | COMMAND(ca, NOFLAGS) /* cancel command mode */ 268 | cmd_cs(e, v, a); 269 | cmd_el(e, v, a); 270 | e->focusview = &e->docview; 271 | e->err[0] = 0; 272 | END 273 | 274 | COMMAND(cb, NEEDSBLOCK | CLEARSBLOCK | NOLOCATOR) /* clear block */ 275 | /* called for side effect */ 276 | END 277 | 278 | COMMAND(cd, MARK) /* cursor down, same column */ 279 | if (!haslines || p.l >= b->n - 1) 280 | ERROR("End of file"); 281 | v->p.l++; 282 | END 283 | 284 | COMMAND(ce, NOFLAGS) /* cursor to end of line */ 285 | v->p = pos(p.l, b->n? b->l[p.l].n : 0); 286 | END 287 | 288 | COMMAND(cf, NOLOCATOR) /* call a function key */ 289 | if (a->n1 >= FUNC_MAX || !e->funcs[a->n1]) 290 | ERROR("Invalid function number"); 291 | RETURN(runextended(e->funcs[a->n1], wcslen(e->funcs[a->n1]), e)); 292 | END 293 | 294 | COMMAND(cj, NOFLAGS) /* cursor jump to EOL/BOL */ 295 | if (!haslines) 296 | SUCCEED; 297 | LINE *l = &b->l[p.l]; 298 | v->p.c = (p.c == l->n)? 0 : l->n; 299 | END 300 | 301 | COMMAND(cl, NOFLAGS) /* cursor left */ 302 | colno c = v->p.c; 303 | if (v->p.c) 304 | v->p.c--; 305 | while (v->p.c && wcwidth(charat(b, v->p)) == 0) 306 | v->p.c--; 307 | if (c == v->p.c) 308 | ERROR("Beginning of line"); 309 | END 310 | 311 | COMMAND(cm, NOLOCATOR) /* enter command mode */ 312 | e->focusview = &e->cmdview; 313 | werase(e->cmdview.w); 314 | wrefresh(e->cmdview.w); 315 | refresh(); 316 | END 317 | 318 | COMMAND(cr, NOFLAGS) /* cursor right */ 319 | if (!haslines && !insertline(b, 0)) 320 | FAIL; 321 | if (p.c >= SIZE_MAX - 1) 322 | ERROR("End of line"); 323 | v->p.c++; 324 | while (v->p.c < SIZE_MAX - 1 && wcwidth(charat(b, v->p)) == 0) 325 | v->p.c++; 326 | END 327 | 328 | COMMAND(cs, NOFLAGS) /* cursor to start of line */ 329 | v->p.c = 0; 330 | END 331 | 332 | COMMAND(ct, NOLOCATOR) /* collapse tabs */ 333 | v->et = false; 334 | END 335 | 336 | COMMAND(cu, MARK) /* cursor up, same column */ 337 | if (!haslines || !p.l) 338 | ERROR("Top of file"); 339 | v->p.l--; 340 | END 341 | 342 | COMMAND(d, MARK | CLEARSBLOCK) /* delete line */ 343 | if (!haslines) 344 | SUCCEED; 345 | free(v->dl); 346 | v->dl = dupstr(b->l[p.l].s, b->l[p.l].n); 347 | v->dln = b->l[p.l].n; 348 | if (!v->dl) 349 | ERROR("Out of memory"); 350 | RETURN(deleteline(b, p.l)); 351 | END 352 | 353 | COMMAND(db, MARK | NEEDSBLOCK | CLEARSBLOCK) /* delete block */ 354 | size_t n = v->be - v->bs; 355 | for (size_t i = 0; i <= n && b->n; i++) 356 | deleteline(b, v->bs); 357 | END 358 | 359 | COMMAND(dc, NOFLAGS | NEEDSLINES) /* delete character at cursor */ 360 | if (b->n && p.c < b->l[p.l].n && b->l[p.l].n) 361 | RETURN(deletetext(b, p, 1)); 362 | END 363 | 364 | COMMAND(df, NOLOCATOR) /* display function definitions */ 365 | WINDOW *w = e->docview.w; 366 | WINDOW *c = e->cmdview.w; 367 | int oc = curs_set(0); 368 | wattron(w, A_BOLD); 369 | werase(w); 370 | for (int i = 0; i < FUNC_MAX; i++){ /* XXX handle narrow windows */ 371 | mvwprintw(w, i, 0, "Function key %2d: %ls", i + 1, 372 | e->funcs[i]? e->funcs[i] : L"Not set"); 373 | } 374 | mvwprintw(w, 12, 0, "Type any character to continue"); 375 | wattroff(w, A_BOLD); 376 | wrefresh(w); 377 | werase(c); 378 | mvwprintw(c, 0, 0, "Display Functions"); 379 | wrefresh(c); 380 | getkeystroke(e, true); 381 | if (oc != ERR) 382 | curs_set(oc); 383 | END 384 | 385 | COMMAND(dl, NOFLAGS) /* delete character to left */ 386 | if (!p.c) 387 | ERROR("Beginning of line"); 388 | v->p.c--; 389 | if (haslines && v->p.c < b->l[v->p.l].n) 390 | RETURN(deletetext(b, v->p, 1)); 391 | END 392 | 393 | COMMAND(do, NOLOCATOR) /* do a shell command */ 394 | if (!a->n1) 395 | ERROR("Empty command"); 396 | char *s = wstos(a->s1, a->n1); 397 | if (!s) 398 | ERROR("Out of memory"); 399 | endwin(); 400 | system(s); 401 | puts("\nPress any key to continue"); 402 | getkeystroke(e, true); 403 | refresh(); 404 | free(s); 405 | END 406 | 407 | COMMAND(dp, MARK) /* delete previous */ 408 | if (!v->p.c) 409 | ERROR("Beginning of line"); 410 | 411 | while (v->p.c && iswspace(charat(b, pos(v->p.l, v->p.c - 1))) && cmd_dl(e, v, a)) 412 | ; 413 | 414 | if (v->p.c && !iswspace(charat(b, pos(v->p.l, v->p.c - 1)))){ 415 | while (v->p.c && !iswspace(charat(b, pos(v->p.l, v->p.c - 1))) && cmd_dl(e, v, a)) 416 | ; 417 | } 418 | END 419 | 420 | COMMAND(dw, MARK) /* delete to end of current word */ 421 | bool r = true; 422 | size_t n = 0; 423 | POS start = v->p; 424 | 425 | if (ateol(v->b, v->p)) 426 | SUCCEED; 427 | if (iswspace(charat(b, v->p))){ 428 | while (iswspace(charat(b, v->p)) && !ateol(b, v->p) && r){ 429 | v->p.c++; 430 | n++; 431 | } 432 | } else while (!iswspace(charat(b, v->p)) && !ateol(b, v->p) && r){ 433 | v->p.c++; 434 | n++; 435 | } 436 | 437 | v->p = start; 438 | RETURN(deletetext(b, v->p, n)); 439 | END 440 | 441 | COMMAND(e, MARK | NOLOCATOR) /* exchange s/t */ 442 | RETURN(exchange(e, v, a, false)); 443 | END 444 | 445 | COMMAND(el, MARK) /* delete to EOL */ 446 | RETURN(!haslines || p.c >= b->l[p.l].n || deletetext(b, p, b->l[p.l].n - p.c)); 447 | END 448 | 449 | COMMAND(ep, MARK | NOLOCATOR) /* go to beginning or end of page */ 450 | size_t lines, cols; 451 | if (p.l >= b->n) 452 | SUCCEED; 453 | getmaxyx(v->w, lines, cols); 454 | (void)cols; 455 | lineno n = v->tos.l + lines - 1; 456 | if (v->tos.l + lines - 1 >= b->n) 457 | n = b->n - 1; 458 | LINE *l = &b->l[n]; 459 | if (p.l != v->tos.l || p.c > v->tos.c) 460 | v->p = v->tos; 461 | else 462 | v->p = pos(n, l->n); 463 | END 464 | 465 | COMMAND(eq, MARK | NOLOCATOR) /* exchange with query */ 466 | RETURN(exchange(e, v, a, true)); 467 | END 468 | 469 | COMMAND(et, NOLOCATOR) /* expand tabs */ 470 | v->et = true; 471 | END 472 | 473 | COMMAND(ex, NOLOCATOR) /* expand margins */ 474 | v->ex = true; 475 | END 476 | 477 | COMMAND(f, MARK | SETSHILITE | NOLOCATOR) /* find forward */ 478 | RETURN((!a->n1 || setfind(e, a->s1, a->n1)) && find(e, v, p, false)); 479 | END 480 | 481 | COMMAND(fb, MARK | NEEDSBLOCK | CLEARSBLOCK) /* filter block through command */ 482 | /* FIXME - we should do two pipes and a select instead of a temp file */ 483 | POS wp = pos(v->bs, 0); 484 | char tfn[] = "/tmp/tineXXXXXX"; 485 | int tfd = mkstemp(tfn); 486 | if (tfd < 0) 487 | ERROR("Could not open temporary file"); 488 | 489 | int tochild[2] = {-1, -1}; 490 | if (pipe(tochild) != 0){ 491 | close(tfd); 492 | unlink(tfn); 493 | ERROR("Could not open pipe"); 494 | } 495 | signal(SIGPIPE, SIG_IGN); 496 | 497 | char *s = wstos(a->s1, a->n1); 498 | if (!s){ 499 | close(tfd); close(tochild[0]); close(tochild[1]); unlink(tfn); 500 | ERROR("Out of memory"); 501 | } 502 | 503 | pid_t pid = fork(); 504 | if (pid == -1) 505 | ERROR("Could not fork"); 506 | else if (pid == 0){ 507 | close(tochild[1]); 508 | if (dup2(tochild[0], STDIN_FILENO) == -1) exit(EXIT_FAILURE); 509 | if (dup2(tfd, STDOUT_FILENO) == -1) exit(EXIT_FAILURE); 510 | if (dup2(tfd, STDERR_FILENO) == -1) exit(EXIT_FAILURE); 511 | execl("/bin/sh", "sh", "-c", s, NULL); 512 | } else{ 513 | free(s); 514 | close(tochild[0]); 515 | writelines(tochild[1], e, v->b, v->bs, v->be); 516 | while (waitpid(pid, NULL, WNOHANG) == 0){ 517 | if (getkeystroke(e, false).o != ERR){ 518 | kill(pid, SIGKILL); 519 | break; 520 | } 521 | } 522 | } 523 | close(tfd); 524 | close(tochild[1]); 525 | 526 | wchar_t *ws = stows(tfn, sizeof(tfn) - 1); 527 | if (!ws){ 528 | free(ws); unlink(tfn); 529 | ERROR("Out of memory"); 530 | } 531 | v->p = wp; 532 | ARG a1 = {.t = ARG_STRING, .s1 = ws, .n1 = wcslen(ws)}; 533 | bool r = cmd_db(e, v, a) && cmd_if(e, v, &a1); 534 | free(ws); unlink(tfn); 535 | RETURN(r); 536 | END 537 | 538 | COMMAND(fc, NOFLAGS) /* flip case */ 539 | wchar_t w = charat(b, p); 540 | bool r = true; 541 | w = iswupper(w)? towlower(w) : towupper(w); 542 | if (haslines && p.c < b->l[p.l].n) 543 | r = deletetext(b, p, 1) && inserttext(b, p, &w, 1); 544 | RETURN(r && cmd_cr(e, v, a)); 545 | END 546 | 547 | COMMAND(gb, MARK | NOLOCATOR) /* go back to previous position */ 548 | if (v->gb.l == NONE || v->gb.l >= b->n) 549 | ERROR("No previous position"); 550 | v->p = v->gb; 551 | END 552 | 553 | COMMAND(gm, MARK | NOLOCATOR) /* go to mark */ 554 | if (a->n1 == 0 || a->n1 > BM_MAX) 555 | ERROR("Invalid bookmark"); 556 | size_t n = a->n1 - 1; 557 | if (e->bm[n].l == NONE || e->bm[n].l >= b->n) 558 | ERROR("Bookmark not set"); 559 | v->p = e->bm[n]; 560 | END 561 | 562 | COMMAND(i, MARK | CLEARSBLOCK) /* insert line before */ 563 | RETURN(insertline(b, p.l) && inserttext(b, pos(p.l, 0), a->s1, a->n1)); 564 | END 565 | 566 | COMMAND(ib, MARK | NEEDSBLOCK) /* insert block */ 567 | if (p.l >= v->bs && p.l <= v->be) 568 | ERROR("Cursor inside block"); 569 | 570 | size_t n = v->be - v->bs; 571 | lineno bs = p.l < v->bs? v->bs + n + 1 : v->bs; 572 | for (size_t i = 0; i <= n; i++){ 573 | if (!insertline(v->b, v->p.l)) 574 | FAIL; 575 | } 576 | 577 | for (size_t i = 0; i <= n; i++){ 578 | const LINE *l = &v->b->l[bs + i]; 579 | if (!inserttext(b, pos(p.l + i, 0), l->s, l->n)) 580 | FAIL; 581 | } 582 | 583 | v->bs = bs; 584 | v->be = bs + n; 585 | END 586 | 587 | static bool 588 | cmd_if_cb(const wchar_t *s, size_t n, void *p) 589 | { 590 | VIEW *v = (VIEW *)p; 591 | v->p.c = 0; 592 | bool rc = insertline(v->b, v->p.l) && inserttext(v->b, v->p, s, n); 593 | v->p.l++; 594 | return rc; 595 | } 596 | 597 | COMMAND(if, MARK | CLEARSBLOCK) /* insert file */ 598 | char *fn = wstos(a->s1, a->n1); 599 | if (!fn) 600 | ERROR("Out of memory"); 601 | bool r = readfile(fn, cmd_if_cb, v); 602 | if (!r) 603 | snprintf(e->err, ERR_MAX, "Could not open file: %s", strerror(errno)); 604 | v->p = p; 605 | free(fn); 606 | RETURN(r); 607 | END 608 | 609 | COMMAND(im, NOLOCATOR) /* ignore (don't show) matching braces */ 610 | v->sm = false; 611 | END 612 | 613 | static bool 614 | squeezespace(BUFFER *b, POS p) 615 | { 616 | size_t n = 0; 617 | POS start = p; 618 | while (!ateol(b, p) && iswspace(charat(b, p)) && iswspace(charat(b, pos(p.l, p.c + 1)))){ 619 | p.c++; 620 | n++; 621 | } 622 | deletetext(b, start, n); 623 | return true; 624 | } 625 | 626 | COMMAND(j, MARK | CLEARSBLOCK) /* join this line and next */ 627 | if (b->n < 2 || p.l >= b->n - 1) 628 | ERROR("End of file"); 629 | LINE *l1 = &b->l[p.l]; 630 | LINE *l2 = &b->l[p.l + 1]; 631 | POS np = pos(p.l, l1->n); 632 | RETURN(inserttext(b, pos(p.l, l1->n), l2->s, l2->n) && deleteline(b, p.l + 1) && squeezespace(b, np)); 633 | END 634 | 635 | COMMAND(lc, NOLOCATOR) /* case-sensitive searching */ 636 | v->uc = false; 637 | END 638 | 639 | COMMAND(m, MARK | NOLOCATOR) /* move to line */ 640 | if (a->n1 == NONE || a->n1 == 0 || a->n1 - 1 >= b->n || !b->n) 641 | ERROR("Invalid line"); 642 | v->p.l = a->n1 - 1; 643 | END 644 | 645 | COMMAND(mc, NOLOCATOR) /* remap control key */ 646 | if (a->n1 != 1 || a->n2 != 1 || !a->s1 || !a->s2) 647 | ERROR("Invalid mapping specification"); 648 | wchar_t c1 = towupper(a->s1[0]) & 0x1f; 649 | wchar_t c2 = towupper(a->s2[0]) & 0x1f; 650 | if (!iswcntrl(c1) || !iswcntrl(c2) || c1 >= CTRL_MAX || c2 >= CTRL_MAX) 651 | ERROR("Invalid character specification"); 652 | e->ctrlmap[c1] = c2; 653 | END 654 | 655 | COMMAND(ms, NOLOCATOR) /* show matching braces */ 656 | v->sm = true; 657 | END 658 | 659 | COMMAND(n, MARK) /* move to beginning of next line */ 660 | if (!haslines || p.l >= b->n - 1) 661 | ERROR("End of file"); 662 | v->p = pos(p.l + 1, 0); 663 | END 664 | 665 | COMMAND(ni, NOLOCATOR) /* regular indent */ 666 | v->ai = false; 667 | END 668 | 669 | COMMAND(p, MARK) /* move to beginning of previous line */ 670 | if (!haslines || !p.l) 671 | ERROR("End of file"); 672 | v->p = pos(p.l - 1, 0); 673 | END 674 | 675 | COMMAND(pd, NOLOCATOR | MARK) /* page down */ 676 | if (b->n < v->ph || b->n - v->tos.l <= v->ph || b->n - p.l <= v->ph) 677 | ERROR("End of file"); 678 | v->tos.l += v->ph; 679 | v->p.l += v->ph; 680 | END 681 | 682 | COMMAND(ph, NOLOCATOR) /* define page height */ 683 | if (a->n1 == 0) 684 | ERROR("Invalid page height"); 685 | v->ph = a->n1; 686 | END 687 | 688 | COMMAND(pu, NOLOCATOR | MARK) /* page up */ 689 | if (v->tos.l < v->ph || v->p.l < v->ph) 690 | ERROR("Top of file"); 691 | v->tos.l -= v->ph; 692 | v->p.l -= v->ph; 693 | END 694 | 695 | COMMAND(q, NOLOCATOR) /* quit without save */ 696 | if (e->docview.b->dirty && !prompt(e, "File has changed. Lose changes?")) 697 | FAIL; 698 | e->running = false; 699 | END 700 | 701 | COMMAND(qo, NOFLAGS) /* quote next */ 702 | v->q = true; 703 | END 704 | 705 | COMMAND(qy, NOFLAGS) /* force quit without save */ 706 | e->running = false; 707 | END 708 | 709 | COMMAND(rd, CLEARSBLOCK) /* restore deleted */ 710 | if (!v->dl) 711 | ERROR("No saved line"); 712 | ARG a1 = {.t = ARG_STRING, .s1 = v->dl, .n1 = v->dln}; 713 | return cmd_i(e, v, &a1); 714 | END 715 | 716 | static bool 717 | iscomment(const wchar_t *s, size_t n) 718 | { 719 | if (!n) 720 | return true; 721 | size_t i = 0; 722 | while (i < n && iswspace(s[i])) 723 | i++; 724 | return s[i] == L'\'' || s[i] == 0; 725 | } 726 | 727 | static bool 728 | cmd_rf_cb(const wchar_t *s, size_t n, void *p) 729 | { 730 | if (!n || iscomment(s, n)) 731 | return true; 732 | return runextended(s, n, (EDITOR *)p); 733 | } 734 | 735 | COMMAND(rf, NOLOCATOR) /* run command file */ 736 | char *fn = wstos(a->s1, a->n1); 737 | if (!fn) 738 | ERROR("Out of memory"); 739 | bool r = readfile(fn, cmd_rf_cb, e); 740 | free(fn); 741 | RETURN(r); 742 | END 743 | 744 | COMMAND(rm, NOLOCATOR) /* reset margins */ 745 | v->rm = NONE; 746 | v->lm = 0; 747 | END 748 | 749 | static bool 750 | runcommand(EDITOR *e, VIEW *v, const ARG *a, bool stay) 751 | { 752 | if (!v->b->n || v->p.l >= v->b->n) 753 | return error(e, "Commands abandoned"); 754 | e->err[0] = 0; 755 | bool rc = true; 756 | if (trimlength(&v->b->l[v->p.l])){ 757 | rc = runextended(v->b->l[v->p.l].s, v->b->l[v->p.l].n, e); 758 | e->lc = v->p; 759 | if (v->p.l == v->b->n - 1) /* only add a newline if we're not repeating */ 760 | insertline(v->b, v->b->n); 761 | } 762 | v->p = pos(v->b->n - 1, 0); 763 | werase(v->w); 764 | if (!stay) 765 | e->focusview = &e->docview; 766 | return rc; 767 | } 768 | 769 | COMMAND(rp, MARK | NOLOCATOR) /* repeat last command */ 770 | if (e->lc.l == NONE) 771 | SUCCEED; 772 | e->cmdview.p = e->lc; 773 | RETURN(runcommand(e, &e->cmdview, a, false)); 774 | END 775 | 776 | COMMAND(rs, MARK | NOLOCATOR) /* run extended command and stay */ 777 | RETURN(runcommand(e, v, a, true)); 778 | END 779 | 780 | COMMAND(ru, MARK | NOLOCATOR) /* run extended command */ 781 | RETURN(runcommand(e, v, a, false)); 782 | END 783 | 784 | COMMAND(s, MARK | CLEARSBLOCK) /* split line */ 785 | if (!b->n && !insertline(b, p.l)) 786 | ERROR("Out of memory"); 787 | 788 | size_t lines, cols; 789 | getmaxyx(v->w, lines, cols); 790 | (void)cols; 791 | 792 | colno lm = v->lm == NONE? 0 : v->lm; 793 | if (v->ai && b->n && v->p.l){ 794 | const LINE *l = &b->l[v->p.l]; 795 | for (size_t i = 0; i < l->n; i++){ 796 | if (!iswspace(l->s[i])){ 797 | lm = i; 798 | break; 799 | } 800 | } 801 | } 802 | size_t ln = b->l[p.l].n; 803 | size_t n = p.c >= ln? 0 : ln - p.c; 804 | if (!insertline(v->b, p.l + 1) 805 | || !inserttext(v->b, pos(p.l + 1, lm), b->l[p.l].s + p.c, n) 806 | || !deletetext(v->b, pos(p.l, p.c), n)) 807 | ERROR("Out of memory"); 808 | v->p = pos(p.l + 1, lm); 809 | if (b->n - v->tos.l >= lines + 1) /* emulate a quirk of ED */ 810 | v->tos.l++; 811 | END 812 | 813 | COMMAND(sa, NOLOCATOR) /* save text to file */ 814 | char *fn = strdup(e->name); 815 | if (a->t == ARG_STRING && a->n1){ 816 | free(fn); 817 | fn = wstos(a->s1, a->n1); 818 | } 819 | if (!fn) 820 | ERROR("Out of memory"); 821 | 822 | int fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644); 823 | if (fd < 0){ 824 | free(fn); 825 | ERROR("Could not open file"); 826 | } 827 | bool r = writelines(fd, e, b, 0, b->n? b->n - 1 : 0); 828 | if (rc) 829 | e->docview.b->dirty = false; 830 | free(fn); 831 | RETURN(r); 832 | END 833 | 834 | COMMAND(sb, NOLOCATOR | MARK) /* show block on screen */ 835 | if (v->bs == NONE) 836 | ERROR("No block defined"); 837 | v->p.l = v->bs; 838 | END 839 | 840 | COMMAND(sd, NOLOCATOR) /* set show matching bracket delay */ 841 | if (a->n1 > 2000) 842 | ERROR("Invalid delay value"); 843 | v->sd = a->n1; 844 | END 845 | 846 | COMMAND(se, CLEARSBLOCK | MARK) /* split line after move to end */ 847 | RETURN(cmd_ce(e, v, a) && cmd_s(e, v, a)); 848 | END 849 | 850 | COMMAND(sf, NOLOCATOR) /* set function key */ 851 | if (!a->n1) 852 | ERROR("Invalid function number"); 853 | 854 | long long n = wcstoll(a->s1, NULL, 10) - 1; 855 | if (n < 0 || n >= FUNC_MAX) 856 | ERROR("Invalid function number"); 857 | 858 | free(e->funcs[n]); 859 | if ((e->funcs[n] = dupstr(a->s2, a->n2)) == NULL) 860 | ERROR("Out of memory"); 861 | END 862 | 863 | COMMAND(sh, NOLOCATOR) /* show information */ 864 | WINDOW *w = e->docview.w; 865 | WINDOW *c = e->cmdview.w; 866 | 867 | char *bs = NULL; 868 | char *be = NULL; 869 | int oc = curs_set(0); 870 | if (v->bs != NONE) 871 | bs = wstos(v->b->l[v->bs].s, v->b->l[v->bs].n); 872 | if (v->be != NONE) 873 | be = wstos(v->b->l[v->be].s, v->b->l[v->be].n); 874 | 875 | wattron(w, A_BOLD); 876 | werase(w); 877 | mvwprintw(w, 0, 0, "Editing file %s", basename(e->name)); 878 | mvwprintw(w, 1, 0, "In directory %-.61s", dirname(e->name)); 879 | mvwprintw(w, 2, 0, "Line count %zu", v->b->n); 880 | mvwprintw(w, 3, 0, "Tab distance %zu", v->ts); 881 | mvwprintw(w, 4, 0, "Page height %zu", v->ph); 882 | mvwprintw(w, 5, 0, "Left margin %zu", v->lm == NONE? 1 : v->lm + 1); 883 | if (v->rm == NONE) 884 | mvwprintw(w, 5, 0, "Right margin Not set"); 885 | else 886 | mvwprintw(w, 5, 0, "Right margin %zu", v->rm + 1); 887 | mvwprintw(w, 6, 0, "Block start %-.24s%s", bs? trimleft(bs) : "Not set", bs? "..." : ""); 888 | mvwprintw(w, 7, 0, "Block end %-.24s%s", be? trimleft(be) : "Not set", be? "..." : ""); 889 | mvwprintw(w, 9, 0, "Type any character to continue"); 890 | wattroff(w, A_BOLD); 891 | wrefresh(w); 892 | free(bs); 893 | free(be); 894 | 895 | werase(c); 896 | mvwprintw(c, 0, 0, 897 | "tine Copyright (C) 2019-2020 Rob King. See COPYING for details."); 898 | wrefresh(c); 899 | getkeystroke(e, true); 900 | if (oc != ERR) 901 | curs_set(oc); 902 | END 903 | 904 | COMMAND(sl, NOLOCATOR) /* set left margin */ 905 | if (v->rm != NONE && a->n1 >= v->rm) 906 | ERROR("Left margin would exceed right margin"); 907 | if (a->n1 >= SIZE_MAX - 1) 908 | ERROR("Invalid margin"); 909 | v->lm = a->n1 > 0? a->n1 - 1 : p.c; 910 | END 911 | 912 | static inline bool 913 | onscreen(const VIEW *v, POS p) 914 | { 915 | int y, x; 916 | getmaxyx(v->w, y, x); 917 | return p.l >= v->tos.l 918 | && p.l < v->tos.l + y 919 | && p.c >= v->tos.c 920 | && p.c < v->tos.c + x; 921 | } 922 | 923 | COMMAND(hb, MARK | NOLOCATOR) 924 | if (!cmd_ty(e, v, a)) 925 | RETURN(false); 926 | 927 | if (!v->sm) 928 | SUCCEED; 929 | 930 | POS op = v->p; 931 | cmd_cl(e, v, a); 932 | if (cmd_sm(e, v, a)){ 933 | if (onscreen(v, v->p)){ 934 | redisplay(&e->docview); 935 | napms(v->sd); 936 | } 937 | } 938 | v->p = op; 939 | SUCCEED; 940 | END 941 | 942 | COMMAND(sm, MARK | NOLOCATOR) /* show matching brace */ 943 | wint_t s = charat(b, p), m = 0; 944 | int c = 1; 945 | bool r = 0; 946 | switch (s){ 947 | case L'(': m = L')'; break; 948 | case L')': m = L'('; r = true; break; 949 | case L'{': m = L'}'; break; 950 | case L'}': m = L'{'; r = true; break; 951 | case L'[': m = L']'; break; 952 | case L']': m = L'['; r = true; break; 953 | case L'<': m = L'>'; break; 954 | case L'>': m = L'<'; r = true; break; 955 | default: ERROR("Search failed"); 956 | } 957 | 958 | bool (*iter)(const BUFFER *, POS *) = r? prev : next; 959 | bool (*atend)(const BUFFER *, POS) = r? attop : atbot; 960 | do{ 961 | if (!iter(b, &p)) 962 | ERROR("Search failed"); 963 | if (charat(b, p) == s) 964 | c++; 965 | else if (charat(b, p) == m) 966 | c--; 967 | if (c == 0){ 968 | v->p = p; 969 | SUCCEED; 970 | } 971 | } while (!atend(b, p)); 972 | ERROR("Search failed"); 973 | END 974 | 975 | COMMAND(sr, NOLOCATOR) /* set right margin */ 976 | if (v->lm != NONE && a->n1 <= v->lm) 977 | ERROR("Right margin would exceed left margin"); 978 | if (a->n1 >= SIZE_MAX - 1) 979 | ERROR("Invalid margin"); 980 | v->rm = a->n1 > 0? a->n1 - 1 : p.c; 981 | END 982 | 983 | COMMAND(st, NOLOCATOR) /* set tab distance */ 984 | if (a->n1 == 0 || a->n1 >= SIZE_MAX - 1) 985 | ERROR("Invalid tab width"); 986 | v->ts = a->n1; 987 | END 988 | 989 | COMMAND(t, MARK | NOLOCATOR) /* move to top of file */ 990 | v->p = pos(0, 0); 991 | END 992 | 993 | COMMAND(tb, NOFLAGS) /* move to next tabstop */ 994 | if (v->et){ 995 | ARG a2 = {.t = ARG_STRING, .n1 = 1, .s1 = L"\t"}; 996 | RETURN(cmd_ty(e, v, &a2)); 997 | } 998 | 999 | if (!cmd_cr(e, v, a)) 1000 | FAIL; 1001 | while (v->p.c % v->ts){ 1002 | if (!cmd_cr(e, v, a)) 1003 | FAIL; 1004 | } 1005 | END 1006 | 1007 | static void 1008 | wordwrap(EDITOR *e, VIEW *v, const ARG *a) 1009 | { 1010 | colno lm = v->lm == NONE? 0 : v->lm; 1011 | POS p = pos(v->p.l, v->p.c - 1); 1012 | while (!iswspace(charat(v->b, p)) && p.c > lm && p.c) 1013 | p.c--; 1014 | if (p.c > lm){ 1015 | 1016 | v->p = pos(p.l, p.c + 1); 1017 | cmd_s(e, v, a); 1018 | cmd_ce(e, v, a); 1019 | v->p.c = v->p.c < lm? lm : v->p.c; 1020 | } 1021 | } 1022 | 1023 | COMMAND(ty, NOFLAGS) /* type in characters */ 1024 | if (!haslines && !insertline(b, p.l)) 1025 | ERROR("Out of memory"); 1026 | 1027 | for (size_t i = 0; i < a->n1; i++){ 1028 | if (v->rm != NONE && v->p.c != NONE && v->p.c && v->p.c == v->rm && !v->ex){ 1029 | if (iswspace(a->s1[i])){ 1030 | cmd_s(e, v, a); 1031 | continue; 1032 | } 1033 | wordwrap(e, v, a); 1034 | } 1035 | if (!inserttext(b, v->p, a->s1 + i, 1)) 1036 | ERROR("Out of memory"); 1037 | v->p.c++; 1038 | } 1039 | END 1040 | 1041 | COMMAND(u, CLEARSBLOCK) /* undo */ 1042 | if (!b->j) 1043 | ERROR("Nothing to undo"); 1044 | if (!undo(b, &p)) 1045 | ERROR("Out of memory"); 1046 | v->p = p; 1047 | END 1048 | 1049 | COMMAND(uc, NOLOCATOR) /* case-insensitive searching */ 1050 | v->uc = true; 1051 | END 1052 | 1053 | COMMAND(uk, NOLOCATOR) /* unknown command */ 1054 | ERROR("Unknown command"); 1055 | END 1056 | 1057 | COMMAND(vw, NOLOCATOR) /* verify window */ 1058 | redisplay(&e->docview); 1059 | redisplay(&e->cmdview); 1060 | if (v->statuscb) 1061 | v->statuscb(e, v); 1062 | redrawwin(e->docview.w); 1063 | redrawwin(e->cmdview.w); 1064 | END 1065 | 1066 | COMMAND(wb, NOLOCATOR) /* write block */ 1067 | if (v->bs == NONE || v->be == NONE) 1068 | ERROR("No block defined"); 1069 | 1070 | char *fn = strdup(e->name); 1071 | if (a->t == ARG_STRING && a->n1){ 1072 | free(fn); 1073 | fn = wstos(a->s1, a->n1); 1074 | } 1075 | if (!fn) 1076 | ERROR("Out of memory"); 1077 | 1078 | int fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644); 1079 | if (fd < 0){ 1080 | free(fn); 1081 | ERROR("Could not open file"); 1082 | } 1083 | bool r = writelines(fd, e, v->b, v->bs, v->be); 1084 | free(fn); 1085 | RETURN(r); 1086 | END 1087 | 1088 | COMMAND(wn, MARK) /* next word */ 1089 | while (!iswspace(charat(b, v->p)) && !atbot(b, v->p)) 1090 | next(b, &v->p); 1091 | while (iswspace(charat(v->b, v->p)) && !atbot(v->b, v->p)) 1092 | next(b, &v->p); 1093 | if (atbot(b, v->p)) 1094 | RETURN(cmd_cr(e, v, a)); /* emulate a quirk in ED */ 1095 | END 1096 | 1097 | COMMAND(wp, MARK) /* previous word */ 1098 | /* no previous words */ 1099 | if (attop(b, p)) 1100 | ERROR("Top of file"); 1101 | 1102 | /* right after a word */ 1103 | if (iswspace(charat(b, v->p)) && prev(b, &v->p) && !attop(b, v->p)){ 1104 | while (!iswspace(charat(b, v->p)) && !attop(b, v->p)) 1105 | prev(b, &v->p); 1106 | while (iswspace(charat(b, v->p)) && !attop(b, v->p)) 1107 | prev(b, &v->p); 1108 | } else if (iswspace(charat(b, v->p))){ /* we're several spaces past a word */ 1109 | while (iswspace(charat(b, v->p)) && !attop(b, v->p)) 1110 | prev(b, &v->p); 1111 | } else{ /* we're inside a word */ 1112 | while (!iswspace(charat(b, v->p)) && !attop(b, v->p)) 1113 | prev(b, &v->p); 1114 | while (iswspace(charat(b, v->p)) && !attop(b, v->p)) 1115 | prev(b, &v->p); 1116 | } 1117 | if (!attop(b, v->p)) 1118 | cmd_cr(e, v, a); 1119 | END 1120 | 1121 | COMMAND(x, NOFLAGS) /* exit with save */ 1122 | RETURN(cmd_sa(e, v, a) && cmd_q(e, v, a)); 1123 | END 1124 | 1125 | COMMAND(xq, NOLOCATOR) /* exit with save and query */ 1126 | if (!e->docview.b->dirty) 1127 | RETURN(cmd_q(e, v, a)); 1128 | if (prompt(e, "File has been changed - Type Y to save and exit:")) 1129 | RETURN(cmd_x(e, v, a)); 1130 | FAIL; 1131 | END 1132 | 1133 | /* EXTENDED COMMAND LOOKUP */ 1134 | static CMD cmdtab[] ={ 1135 | /* nm argtype req'd func */ 1136 | {L"A", ARG_STRING, false, cmd_a}, 1137 | {L"AI", ARG_NONE, false, cmd_ai}, 1138 | {L"B", ARG_NONE, true, cmd_b}, 1139 | {L"BT", ARG_NONE, true, cmd_bt}, 1140 | {L"BE", ARG_NONE, true, cmd_be}, 1141 | {L"BF", ARG_STRING, false, cmd_bf}, 1142 | {L"BM", ARG_NUMBER, true, cmd_bm}, 1143 | {L"BS", ARG_NONE, true, cmd_bs}, 1144 | {L"CB", ARG_NONE, true, cmd_cb}, 1145 | {L"CD", ARG_NONE, true, cmd_cd}, 1146 | {L"CE", ARG_NONE, true, cmd_ce}, 1147 | {L"CF", ARG_NUMBER, true, cmd_cf}, 1148 | {L"CJ", ARG_NUMBER, true, cmd_cj}, 1149 | {L"CL", ARG_NONE, true, cmd_cl}, 1150 | {L"DP", ARG_NONE, true, cmd_dp}, 1151 | {L"CR", ARG_NONE, true, cmd_cr}, 1152 | {L"CS", ARG_NONE, true, cmd_cs}, 1153 | {L"CT", ARG_NONE, true, cmd_ct}, 1154 | {L"CU", ARG_NONE, true, cmd_cu}, 1155 | {L"D", ARG_NONE, true, cmd_d}, 1156 | {L"DB", ARG_NONE, true, cmd_db}, 1157 | {L"DC", ARG_NONE, true, cmd_dc}, 1158 | {L"DF", ARG_NONE, true, cmd_df}, 1159 | {L"DL", ARG_NONE, true, cmd_dl}, 1160 | {L"DO", ARG_STRING, true, cmd_do}, 1161 | {L"DW", ARG_NONE, true, cmd_dw}, 1162 | {L"E", ARG_EXCHANGE, true, cmd_e}, 1163 | {L"EL", ARG_NONE, true, cmd_el}, 1164 | {L"EP", ARG_NONE, true, cmd_ep}, 1165 | {L"EQ", ARG_EXCHANGE, true, cmd_eq}, 1166 | {L"ET", ARG_NONE, true, cmd_et}, 1167 | {L"EX", ARG_NONE, true, cmd_ex}, 1168 | {L"F", ARG_STRING, false, cmd_f}, 1169 | {L"FB", ARG_STRING, true, cmd_fb}, 1170 | {L"FC", ARG_NONE, false, cmd_fc}, 1171 | {L"GB", ARG_NONE, true, cmd_gb}, 1172 | {L"GM", ARG_NUMBER, true, cmd_gm}, 1173 | {L"I", ARG_STRING, false, cmd_i}, 1174 | {L"IB", ARG_NONE, true, cmd_ib}, 1175 | {L"IF", ARG_STRING, true, cmd_if}, 1176 | {L"IM", ARG_NONE, true, cmd_im}, 1177 | {L"J", ARG_NONE, true, cmd_j}, 1178 | {L"LC", ARG_NONE, true, cmd_lc}, 1179 | {L"M", ARG_NUMBER, true, cmd_m}, 1180 | {L"MC", ARG_EXCHANGE, true, cmd_mc}, 1181 | {L"MS", ARG_NONE, true, cmd_ms}, 1182 | {L"N", ARG_NONE, true, cmd_n}, 1183 | {L"NI", ARG_NONE, true, cmd_ni}, 1184 | {L"P", ARG_NONE, true, cmd_p}, 1185 | {L"PD", ARG_NONE, true, cmd_pd}, 1186 | {L"PH", ARG_NUMBER, true, cmd_ph}, 1187 | {L"PU", ARG_NONE, true, cmd_pu}, 1188 | {L"Q", ARG_STRING, false, cmd_q}, 1189 | {L"QY", ARG_STRING, false, cmd_qy}, 1190 | {L"RF", ARG_STRING, true, cmd_rf}, 1191 | {L"RM", ARG_NONE, true, cmd_rm}, 1192 | {L"SA", ARG_STRING, false, cmd_sa}, 1193 | {L"S", ARG_NONE, true, cmd_s}, 1194 | {L"SB", ARG_NONE, true, cmd_sb}, 1195 | {L"SD", ARG_NUMBER, true, cmd_sd}, 1196 | {L"SF", ARG_EXCHANGE, true, cmd_sf}, 1197 | {L"SH", ARG_NONE, true, cmd_sh}, 1198 | {L"SL", ARG_NUMBER, false, cmd_sl}, 1199 | {L"SM", ARG_NONE, true, cmd_sm}, 1200 | {L"SR", ARG_NUMBER, false, cmd_sr}, 1201 | {L"ST", ARG_NUMBER, true, cmd_st}, 1202 | {L"T", ARG_NONE, true, cmd_t}, 1203 | {L"TY", ARG_STRING, true, cmd_ty}, 1204 | {L"U", ARG_NONE, true, cmd_u}, 1205 | {L"UC", ARG_NONE, true, cmd_uc}, 1206 | {L"WB", ARG_STRING, true, cmd_wb}, 1207 | {L"WN", ARG_NONE, true, cmd_wn}, 1208 | {L"WP", ARG_NONE, true, cmd_wp}, 1209 | {L"X", ARG_NONE, true, cmd_x}, 1210 | {L"XQ", ARG_NONE, true, cmd_xq}, 1211 | {NULL, ARG_NONE, false, NULL} 1212 | }; 1213 | 1214 | 1215 | const CMD * 1216 | lookup(const wchar_t *s) 1217 | { 1218 | for (const CMD *c = cmdtab; c->n; c++){ 1219 | if (wcscmp(c->n, s) == 0) 1220 | return c; 1221 | } 1222 | return false; 1223 | } 1224 | 1225 | bool 1226 | call(const CMD *c, EDITOR *e, VIEW *v, const ARG *a) 1227 | { 1228 | ARG d = {0}; 1229 | if (!c->required && a->t == ARG_NONE){ 1230 | d.t = c->a; 1231 | a = &d; 1232 | } 1233 | if (a->t != c->a) 1234 | return error(e, "Invalid argument type"); 1235 | bool rc = c->f(e, v, a); 1236 | fixcursor(e); 1237 | return rc; 1238 | } 1239 | --------------------------------------------------------------------------------