Home | History | Annotate | Line # | Download | only in ex
      1 /*	$NetBSD: ex_abbrev.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
      2 /*-
      3  * Copyright (c) 1992, 1993, 1994
      4  *	The Regents of the University of California.  All rights reserved.
      5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
      6  *	Keith Bostic.  All rights reserved.
      7  *
      8  * See the LICENSE file for redistribution information.
      9  */
     10 
     11 #include "config.h"
     12 
     13 #include <sys/cdefs.h>
     14 #if 0
     15 #ifndef lint
     16 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 ";
     17 #endif /* not lint */
     18 #else
     19 __RCSID("$NetBSD: ex_abbrev.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
     20 #endif
     21 
     22 #include <sys/types.h>
     23 #include <sys/queue.h>
     24 #include <sys/time.h>
     25 
     26 #include <bitstring.h>
     27 #include <ctype.h>
     28 #include <limits.h>
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 
     33 #include "../common/common.h"
     34 #include "../vi/vi.h"
     35 
     36 /*
     37  * ex_abbr -- :abbreviate [key replacement]
     38  *	Create an abbreviation or display abbreviations.
     39  *
     40  * PUBLIC: int ex_abbr __P((SCR *, EXCMD *));
     41  */
     42 int
     43 ex_abbr(SCR *sp, EXCMD *cmdp)
     44 {
     45 	CHAR_T *p;
     46 	size_t len;
     47 
     48 	switch (cmdp->argc) {
     49 	case 0:
     50 		if (seq_dump(sp, SEQ_ABBREV, 0) == 0)
     51 			msgq(sp, M_INFO, "105|No abbreviations to display");
     52 		return (0);
     53 	case 2:
     54 		break;
     55 	default:
     56 		abort();
     57 	}
     58 
     59 	/*
     60 	 * Check for illegal characters.
     61 	 *
     62 	 * !!!
     63 	 * Another fun one, historically.  See vi/v_ntext.c:txt_abbrev() for
     64 	 * details.  The bottom line is that all abbreviations have to end
     65 	 * with a "word" character, because it's the transition from word to
     66 	 * non-word characters that triggers the test for an abbreviation.  In
     67 	 * addition, because of the way the test is done, there can't be any
     68 	 * transitions from word to non-word character (or vice-versa) other
     69 	 * than between the next-to-last and last characters of the string,
     70 	 * and there can't be any <blank> characters.  Warn the user.
     71 	 */
     72 	if (!inword(cmdp->argv[0]->bp[cmdp->argv[0]->len - 1])) {
     73 		msgq(sp, M_ERR,
     74 		    "106|Abbreviations must end with a \"word\" character");
     75 			return (1);
     76 	}
     77 	for (p = cmdp->argv[0]->bp; *p != '\0'; ++p)
     78 		if (ISBLANK((UCHAR_T)p[0])) {
     79 			msgq(sp, M_ERR,
     80 			    "107|Abbreviations may not contain tabs or spaces");
     81 			return (1);
     82 		}
     83 	if (cmdp->argv[0]->len > 2)
     84 		for (p = cmdp->argv[0]->bp,
     85 		    len = cmdp->argv[0]->len - 2; len; --len, ++p)
     86 			if (inword(p[0]) != inword(p[1])) {
     87 				msgq(sp, M_ERR,
     88 "108|Abbreviations may not mix word/non-word characters, except at the end");
     89 				return (1);
     90 			}
     91 
     92 	if (seq_set(sp, NULL, 0, cmdp->argv[0]->bp, cmdp->argv[0]->len,
     93 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, SEQ_ABBREV, SEQ_USERDEF))
     94 		return (1);
     95 
     96 	F_SET(sp->gp, G_ABBREV);
     97 	return (0);
     98 }
     99 
    100 /*
    101  * ex_unabbr -- :unabbreviate key
    102  *      Delete an abbreviation.
    103  *
    104  * PUBLIC: int ex_unabbr __P((SCR *, EXCMD *));
    105  */
    106 int
    107 ex_unabbr(SCR *sp, EXCMD *cmdp)
    108 {
    109 	ARGS *ap;
    110 
    111 	ap = cmdp->argv[0];
    112 	if (!F_ISSET(sp->gp, G_ABBREV) ||
    113 	    seq_delete(sp, ap->bp, ap->len, SEQ_ABBREV)) {
    114 		msgq_wstr(sp, M_ERR, ap->bp,
    115 		    "109|\"%s\" is not an abbreviation");
    116 		return (1);
    117 	}
    118 	return (0);
    119 }
    120