xref: /minix3/external/bsd/nvi/dist/ex/ex_abbrev.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_abbrev.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_abbrev.c,v 10.10 2001/12/16 18:18:54 skimo Exp  (Berkeley) Date: 2001/12/16 18:18:54 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_abbrev.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/time.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <ctype.h>
2884d9c625SLionel Sambuc #include <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc 
3384d9c625SLionel Sambuc #include "../common/common.h"
3484d9c625SLionel Sambuc #include "../vi/vi.h"
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc /*
3784d9c625SLionel Sambuc  * ex_abbr -- :abbreviate [key replacement]
3884d9c625SLionel Sambuc  *	Create an abbreviation or display abbreviations.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * PUBLIC: int ex_abbr __P((SCR *, EXCMD *));
4184d9c625SLionel Sambuc  */
4284d9c625SLionel Sambuc int
ex_abbr(SCR * sp,EXCMD * cmdp)4384d9c625SLionel Sambuc ex_abbr(SCR *sp, EXCMD *cmdp)
4484d9c625SLionel Sambuc {
4584d9c625SLionel Sambuc 	CHAR_T *p;
4684d9c625SLionel Sambuc 	size_t len;
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc 	switch (cmdp->argc) {
4984d9c625SLionel Sambuc 	case 0:
5084d9c625SLionel Sambuc 		if (seq_dump(sp, SEQ_ABBREV, 0) == 0)
5184d9c625SLionel Sambuc 			msgq(sp, M_INFO, "105|No abbreviations to display");
5284d9c625SLionel Sambuc 		return (0);
5384d9c625SLionel Sambuc 	case 2:
5484d9c625SLionel Sambuc 		break;
5584d9c625SLionel Sambuc 	default:
5684d9c625SLionel Sambuc 		abort();
5784d9c625SLionel Sambuc 	}
5884d9c625SLionel Sambuc 
5984d9c625SLionel Sambuc 	/*
6084d9c625SLionel Sambuc 	 * Check for illegal characters.
6184d9c625SLionel Sambuc 	 *
6284d9c625SLionel Sambuc 	 * !!!
6384d9c625SLionel Sambuc 	 * Another fun one, historically.  See vi/v_ntext.c:txt_abbrev() for
6484d9c625SLionel Sambuc 	 * details.  The bottom line is that all abbreviations have to end
6584d9c625SLionel Sambuc 	 * with a "word" character, because it's the transition from word to
6684d9c625SLionel Sambuc 	 * non-word characters that triggers the test for an abbreviation.  In
6784d9c625SLionel Sambuc 	 * addition, because of the way the test is done, there can't be any
6884d9c625SLionel Sambuc 	 * transitions from word to non-word character (or vice-versa) other
6984d9c625SLionel Sambuc 	 * than between the next-to-last and last characters of the string,
7084d9c625SLionel Sambuc 	 * and there can't be any <blank> characters.  Warn the user.
7184d9c625SLionel Sambuc 	 */
7284d9c625SLionel Sambuc 	if (!inword(cmdp->argv[0]->bp[cmdp->argv[0]->len - 1])) {
7384d9c625SLionel Sambuc 		msgq(sp, M_ERR,
7484d9c625SLionel Sambuc 		    "106|Abbreviations must end with a \"word\" character");
7584d9c625SLionel Sambuc 			return (1);
7684d9c625SLionel Sambuc 	}
7784d9c625SLionel Sambuc 	for (p = cmdp->argv[0]->bp; *p != '\0'; ++p)
7884d9c625SLionel Sambuc 		if (ISBLANK((UCHAR_T)p[0])) {
7984d9c625SLionel Sambuc 			msgq(sp, M_ERR,
8084d9c625SLionel Sambuc 			    "107|Abbreviations may not contain tabs or spaces");
8184d9c625SLionel Sambuc 			return (1);
8284d9c625SLionel Sambuc 		}
8384d9c625SLionel Sambuc 	if (cmdp->argv[0]->len > 2)
8484d9c625SLionel Sambuc 		for (p = cmdp->argv[0]->bp,
8584d9c625SLionel Sambuc 		    len = cmdp->argv[0]->len - 2; len; --len, ++p)
8684d9c625SLionel Sambuc 			if (inword(p[0]) != inword(p[1])) {
8784d9c625SLionel Sambuc 				msgq(sp, M_ERR,
8884d9c625SLionel Sambuc "108|Abbreviations may not mix word/non-word characters, except at the end");
8984d9c625SLionel Sambuc 				return (1);
9084d9c625SLionel Sambuc 			}
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc 	if (seq_set(sp, NULL, 0, cmdp->argv[0]->bp, cmdp->argv[0]->len,
9384d9c625SLionel Sambuc 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, SEQ_ABBREV, SEQ_USERDEF))
9484d9c625SLionel Sambuc 		return (1);
9584d9c625SLionel Sambuc 
9684d9c625SLionel Sambuc 	F_SET(sp->gp, G_ABBREV);
9784d9c625SLionel Sambuc 	return (0);
9884d9c625SLionel Sambuc }
9984d9c625SLionel Sambuc 
10084d9c625SLionel Sambuc /*
10184d9c625SLionel Sambuc  * ex_unabbr -- :unabbreviate key
10284d9c625SLionel Sambuc  *      Delete an abbreviation.
10384d9c625SLionel Sambuc  *
10484d9c625SLionel Sambuc  * PUBLIC: int ex_unabbr __P((SCR *, EXCMD *));
10584d9c625SLionel Sambuc  */
10684d9c625SLionel Sambuc int
ex_unabbr(SCR * sp,EXCMD * cmdp)10784d9c625SLionel Sambuc ex_unabbr(SCR *sp, EXCMD *cmdp)
10884d9c625SLionel Sambuc {
10984d9c625SLionel Sambuc 	ARGS *ap;
11084d9c625SLionel Sambuc 
11184d9c625SLionel Sambuc 	ap = cmdp->argv[0];
11284d9c625SLionel Sambuc 	if (!F_ISSET(sp->gp, G_ABBREV) ||
11384d9c625SLionel Sambuc 	    seq_delete(sp, ap->bp, ap->len, SEQ_ABBREV)) {
11484d9c625SLionel Sambuc 		msgq_wstr(sp, M_ERR, ap->bp,
11584d9c625SLionel Sambuc 		    "109|\"%s\" is not an abbreviation");
11684d9c625SLionel Sambuc 		return (1);
11784d9c625SLionel Sambuc 	}
11884d9c625SLionel Sambuc 	return (0);
11984d9c625SLionel Sambuc }
120