diff options
author | Vincent Labrecque <vincent@cvs.openbsd.org> | 2003-05-05 11:12:08 +0000 |
---|---|---|
committer | Vincent Labrecque <vincent@cvs.openbsd.org> | 2003-05-05 11:12:08 +0000 |
commit | fed26bfe553e958c2f6233b74adde58162be87e4 (patch) | |
tree | 3beecf26e81b5533b39cb5ce4d4acb186e5b6e79 /usr.bin/mg | |
parent | 0f165d5752212126bbe66b2206a2b526621a7846 (diff) |
add a mail-mode, to do automatic line-wrapping.
(at the same time, change the command line parsing to support a
-f <mode> that is slightly different from the one in gnu emacs,
we apply it to all buffers created from command line arguments,
so you can use "mg -f mail-mode" as your editor. manpage change
coming soon)
ok henning
Diffstat (limited to 'usr.bin/mg')
-rw-r--r-- | usr.bin/mg/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 5 | ||||
-rw-r--r-- | usr.bin/mg/mail.c | 98 | ||||
-rw-r--r-- | usr.bin/mg/main.c | 72 |
4 files changed, 154 insertions, 25 deletions
diff --git a/usr.bin/mg/Makefile b/usr.bin/mg/Makefile index 0a31fa89dba..631f55669fd 100644 --- a/usr.bin/mg/Makefile +++ b/usr.bin/mg/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.12 2002/05/29 12:41:42 vincent Exp $ +# $OpenBSD: Makefile,v 1.13 2003/05/05 11:12:07 vincent Exp $ PROG= mg @@ -27,6 +27,6 @@ SRCS= cinfo.c fileio.c spawn.c ttyio.c tty.c ttykbd.c \ # # More or less standalone extensions. # -SRCS+= grep.c theo.c +SRCS+= grep.c theo.c mail.c .include <bsd.prog.mk> diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index d4728ce7ece..8b54d117f21 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.45 2003/01/06 17:04:09 deraadt Exp $ */ +/* $OpenBSD: def.h,v 1.46 2003/05/05 11:12:07 vincent Exp $ */ #include <sys/queue.h> @@ -593,6 +593,9 @@ int auto_execute(int, int); PF *find_autoexec(const char *); int add_autoexec(const char *, const char *); +/* mail.c X */ +void mail_init(void); + /* * Externals. */ diff --git a/usr.bin/mg/mail.c b/usr.bin/mg/mail.c new file mode 100644 index 00000000000..67738b05c1e --- /dev/null +++ b/usr.bin/mg/mail.c @@ -0,0 +1,98 @@ +/* $OpenBSD: mail.c,v 1.1 2003/05/05 11:12:07 vincent Exp $ */ +/* + * This file is in the public domain. + * + * Author: Vincent Labrecque, April 2003 + */ +#include <ctype.h> + +#include "def.h" +#include "kbd.h" +#include "funmap.h" + +#define LIMIT 72 + +static int fake_self_insert(int, int); +static int mail(int, int); + +/* mappings for all "printable" characters ('-' -> '~') */ +static PF mail_fake[] = { + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, fake_self_insert, + fake_self_insert, fake_self_insert, fake_self_insert, +}; + +static struct KEYMAPE (1 + IMAPEXT) mailmap = { + 1, + 1 + IMAPEXT, + rescan, + { + { ' ', '~', mail_fake, NULL }, + } +}; + +void +mail_init(void) +{ + funmap_add(mail, "mail-mode"); + maps_add((KEYMAP *)&mailmap, "mail-mode"); +} + +static int +mail(int f, int n) +{ + curbp->b_modes[0] = name_mode("fundamental"); + curbp->b_modes[1] = name_mode("mail-mode"); + if (curbp->b_modes[1] == NULL) { + panic("can't happen"); + mail_init(); + curbp->b_modes[1] = name_mode("mail-mode"); + } + curbp->b_nmodes = 1; + return (TRUE); +} + +static int +fake_self_insert(int f, int n) +{ + if (curwp->w_doto >= LIMIT - 1) { + int save = curwp->w_doto; + + /* + * Find the last word boundary. + */ + while (curwp->w_doto > 0 && + !isspace(curwp->w_dotp->l_text[curwp->w_doto - 1])) + curwp->w_doto--; + /* + * handle lines without any spaces correctly! + */ + if (curwp->w_doto == 0 && !isspace(curwp->w_dotp->l_text[0])) + curwp->w_doto = save; + newline(FFRAND, 1); + gotoeol(0, 1); + } + selfinsert(f, n); + return (TRUE); +} diff --git a/usr.bin/mg/main.c b/usr.bin/mg/main.c index 15785a5a0d0..ec92f5ec22a 100644 --- a/usr.bin/mg/main.c +++ b/usr.bin/mg/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.23 2003/01/06 17:04:09 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.24 2003/05/05 11:12:07 vincent Exp $ */ /* * Mainline. @@ -12,6 +12,8 @@ #include "macro.h" #endif /* NO_MACRO */ +#include <err.h> + int thisflag; /* flags, this command */ int lastflag; /* flags, last command */ int curgoal; /* goal column */ @@ -22,21 +24,31 @@ MGWIN *curwp; /* current window */ MGWIN *wheadp; /* MGWIN listhead */ char pat[NPAT]; /* pattern */ -static void edinit(void); +static void edinit(PF); int main(int argc, char **argv) { - char *cp; + char *cp, *init_fcn_name = NULL; + PF init_fcn = NULL; + int o; + + while ((o = getopt(argc, argv, "f:")) != -1) + switch (o) { + case 'f': + if (init_fcn_name != NULL) + errx(1, "cannot specify more than one " + "initial function"); + init_fcn_name = optarg; + break; + default: + errx(1, "usage: mg [-f <mode>] [files...]"); + } + argc -= optind; + argv += optind; - vtinit(); /* Virtual terminal. */ -#ifndef NO_DIR - dirinit(); /* Get current directory. */ -#endif /* !NO_DIR */ - edinit(); /* Buffers, windows. */ maps_init(); /* Keymaps and modes. */ funmap_init(); /* Functions. */ - ttykeymapinit(); /* Symbols, bindings. */ /* * This is where we initialize standalone extensions that should @@ -48,8 +60,20 @@ main(int argc, char **argv) grep_init(); theo_init(); + mail_init(); } + if (init_fcn_name && + (init_fcn = name_function(init_fcn_name)) == NULL) + errx(1, "Unknown function `%s'", init_fcn_name); + + vtinit(); /* Virtual terminal. */ +#ifndef NO_DIR + dirinit(); /* Get current directory. */ +#endif /* !NO_DIR */ + edinit(init_fcn); /* Buffers, windows. */ + ttykeymapinit(); /* Symbols, bindings. */ + /* * doing update() before reading files causes the error messages from * the file I/O show up on the screen. (and also an extra display of @@ -62,9 +86,7 @@ main(int argc, char **argv) if ((cp = startupfile(NULL)) != NULL) (void)load(cp); #endif /* !NO_STARTUP */ - while (--argc > 0) { - argv++; - + while (argc > 0) { if (argv[0][0] == '+' && strlen(argv[0]) >= 2) { long lval; char *ep; @@ -78,16 +100,19 @@ main(int argc, char **argv) (lval > INT_MAX || lval < INT_MIN)) goto notnum; startrow = (int)lval; - continue; - } + } else { notnum: - - cp = adjustname(*argv); - if (cp != NULL) { - curbp = findbuffer(cp); - (void)showbuffer(curbp, curwp, 0); - (void)readin(cp); + cp = adjustname(*argv); + if (cp != NULL) { + curbp = findbuffer(cp); + (void)showbuffer(curbp, curwp, 0); + (void)readin(cp); + if (init_fcn_name) + init_fcn(); + } } + argc--; + argv++; } /* fake last flags */ @@ -125,7 +150,7 @@ notnum: * Initialize default buffer and window. */ static void -edinit(void) +edinit(PF init_fcn) { BUFFER *bp; MGWIN *wp; @@ -151,6 +176,9 @@ edinit(void) wp->w_ntrows = nrow - 2; /* 2 = mode, echo. */ wp->w_force = 0; wp->w_flag = WFMODE | WFHARD; /* Full. */ + + if (init_fcn) + init_fcn(); } /* @@ -184,5 +212,5 @@ quit(int f, int n) int ctrlg(int f, int n) { - return ABORT; + return (ABORT); } |