2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
24 #include "../common/common.h"
27 static CHAR_T * const fmt[] = {
40 static void inc_err(SCR *, enum nresult);
43 * v_increment -- [count]#[#+-]
44 * Increment/decrement a keyword number.
46 * PUBLIC: int v_increment(SCR *, VICMD *);
49 v_increment(SCR *sp, VICMD *vp)
53 long change, ltmp, lval;
54 size_t beg, blen, end, len, nlen, wlen;
55 int base, isempty, rval;
56 CHAR_T *ntype, nbuf[100];
59 /* Validate the operator. */
60 if (vp->character == '#')
62 if (vp->character != '+' && vp->character != '-') {
63 v_emsg(sp, vp->kp->usage, VIM_USAGE);
67 /* If new value set, save it off, but it has to fit in a long. */
68 if (F_ISSET(vp, VC_C1SET)) {
69 if (vp->count > LONG_MAX) {
70 inc_err(sp, NUM_OVER);
78 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
85 * Skip any leading space before the number. Getting a cursor word
86 * implies moving the cursor to its beginning, if we moved, refresh
89 for (beg = vp->m_start.cno; beg < len && ISSPACE(p[beg]); ++beg);
92 if (beg != vp->m_start.cno) {
94 (void)vs_refresh(sp, 0);
98 #define ishex(c) (ISXDIGIT(c))
100 #define isoctal(c) ((c) >= '0' && (c) <= '7')
103 * Look for 0[Xx], or leading + or - signs, guess at the base.
104 * The character after that must be a number. Wlen is set to
105 * the remaining characters in the line that could be part of
109 if (p[beg] == '0' && wlen > 2 &&
110 (p[beg + 1] == 'X' || p[beg + 1] == 'x')) {
115 ntype = p[beg + 1] == 'X' ? fmt[HEXC] : fmt[HEXL];
116 } else if (p[beg] == '0' && wlen > 1) {
119 if (!isoctal(p[end]))
122 } else if (wlen >= 1 && (p[beg] == '+' || p[beg] == '-')) {
126 if (!isdigit(p[end]))
132 if (!isdigit(p[end])) {
133 nonum: msgq(sp, M_ERR, "181|Cursor not in a number");
138 /* Find the end of the word, possibly correcting the base. */
139 while (++end < len) {
144 if (p[end] == '8' || p[end] == '9') {
168 * If the line was at the end of the buffer, we have to copy it
169 * so we can guarantee that it's NULL-terminated. We make the
170 * buffer big enough to fit the line changes as well, and only
173 GET_SPACE_RETW(sp, bp, blen, len + 50);
175 MEMMOVE(bp, &p[beg], wlen);
182 * Octal or hex deal in unsigned longs, everything else is done
186 if ((nret = nget_slong(&lval, t, NULL, 10)) != NUM_OK)
188 ltmp = vp->character == '-' ? -change : change;
189 if (lval > 0 && ltmp > 0 && !NPFITS(LONG_MAX, lval, ltmp)) {
193 if (lval < 0 && ltmp < 0 && !NNFITS(LONG_MIN, lval, ltmp)) {
198 /* If we cross 0, signed numbers lose their sign. */
199 if (lval == 0 && ntype == fmt[SDEC])
201 nlen = SPRINTF(nbuf, sizeof(nbuf), ntype, lval);
203 if ((nret = nget_uslong(&ulval, t, NULL, base)) != NUM_OK)
205 if (vp->character == '+') {
206 if (!NPFITS(ULONG_MAX, ulval, change)) {
212 if (ulval < change) {
219 /* Correct for literal "0[Xx]" in format. */
223 nlen = SPRINTF(nbuf, sizeof(nbuf), ntype, wlen, ulval);
226 /* Build the new line. */
228 MEMMOVE(bp + beg, nbuf, nlen);
229 MEMMOVE(bp + beg + nlen, p + end, len - beg - (end - beg));
230 len = beg + nlen + (len - beg - (end - beg));
233 rval = db_set(sp, vp->m_start.lno, bp, len);
240 FREE_SPACEW(sp, bp, blen);
245 inc_err(SCR *sp, enum nresult nret)
254 msgq(sp, M_ERR, "182|Resulting number too large");
257 msgq(sp, M_ERR, "183|Resulting number too small");