#include #include #include #include #include "buf.h" int opt_ai = 1; /* Autoindent */ int opt_sm = 1; /* Show matching character */ static int * get_opt(char *s) { if (strncmp(s, "ai", 2) == 0) return (&opt_ai); else if (strncmp(s, "sm", 2) == 0) return (&opt_sm); else return (NULL); } static int ex_set(struct buf *b) { char *p, *v; int args, *opt; p = minib; /* Skip first word */ v = strsep(&p, " "); args = 0; while (p) { v = strsep(&p, " "); if (v && *v != '\0') { args++; if (strncmp(v, "no", 2) == 0) { opt = get_opt(v + 2); if (opt == NULL) { printerr("set: no '%s' option", v + 2); return (1); } *opt = 0; } else { opt = get_opt(v); if (opt == NULL) { printerr("set: no '%s' option", v); return (1); } *opt = 1; } } } clear_mini(); /* XXX show all set options */ if (args == 0) printd("%s%s", opt_ai ? "ai " : "", opt_sm ? "sm ": ""); return (0); } static void quit(int e) { endwin(); exit(e); } static int ex_quit(struct buf *b) { if ((!b->dirty) || (minib[1] == '!')) quit(b->dirty); printerr("File modified since last complete write; write or use ! to" " override."); return (1); } static int ex_write(struct buf *b) { int err; /* XXX support ":w!" and ":w file" */ if ((err = file_store(&file, b)) != 0) { printerr("write failed: %s\n", strerror(err)); return (1); } if (minib[1] == 'q') quit(0); printd("%s: %d lines", file.path, b->num_lines); return (0); } int cmd_ex(struct cmd *c, struct buf *b) { int k; minipos = 0; mvaddch(maxy - 1, 0, ':'); while (1) { k = getch(); if (k == ESC) { minipos = 0; clear_mini(); return (1); } else if (k == BKSPC) { minipos--; if (minipos < 0) minipos = 0; move(maxy - 1, minipos + 1); continue; } else if (k == 13) { if (k == 13) { minib[minipos] = '\0'; minipos = 0; /* XXX search up to first space */ if (strncmp(minib, "w", 1) == 0) { return (ex_write(b)); } else if (strncmp(minib, "se", 2) == 0) return (ex_set(b)); else if (strncmp(minib, "q", 1) == 0) return (ex_quit(b)); break; } } minib[minipos++] = k; /* XXX bounds check */ mvaddch(maxy - 1, minipos, k); } return (0); }