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.
13 static const char sccsid[] = "$Id: v_increment.c,v 10.17 2011/12/02 01:17:53 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
28 #include "../common/common.h"
31 static CHAR_T * const fmt[] = {
44 static void inc_err(SCR *, enum nresult);
47 * v_increment -- [count]#[#+-]
48 * Increment/decrement a keyword number.
50 * PUBLIC: int v_increment(SCR *, VICMD *);
53 v_increment(SCR *sp, VICMD *vp)
57 long change, ltmp, lval;
58 size_t beg, blen, end, len, nlen, wlen;
59 int base, isempty, rval;
60 CHAR_T *ntype, nbuf[100];
63 /* Validate the operator. */
64 if (vp->character == '#')
66 if (vp->character != '+' && vp->character != '-') {
67 v_emsg(sp, vp->kp->usage, VIM_USAGE);
71 /* If new value set, save it off, but it has to fit in a long. */
72 if (F_ISSET(vp, VC_C1SET)) {
73 if (vp->count > LONG_MAX) {
74 inc_err(sp, NUM_OVER);
82 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
89 * Skip any leading space before the number. Getting a cursor word
90 * implies moving the cursor to its beginning, if we moved, refresh
93 for (beg = vp->m_start.cno; beg < len && ISSPACE(p[beg]); ++beg);
96 if (beg != vp->m_start.cno) {
98 (void)vs_refresh(sp, 0);
102 #define ishex(c) (ISXDIGIT(c))
104 #define isoctal(c) ((c) >= '0' && (c) <= '7')
107 * Look for 0[Xx], or leading + or - signs, guess at the base.
108 * The character after that must be a number. Wlen is set to
109 * the remaining characters in the line that could be part of
113 if (p[beg] == '0' && wlen > 2 &&
114 (p[beg + 1] == 'X' || p[beg + 1] == 'x')) {
119 ntype = p[beg + 1] == 'X' ? fmt[HEXC] : fmt[HEXL];
120 } else if (p[beg] == '0' && wlen > 1) {
123 if (!isoctal(p[end]))
126 } else if (wlen >= 1 && (p[beg] == '+' || p[beg] == '-')) {
130 if (!isdigit(p[end]))
136 if (!isdigit(p[end])) {
137 nonum: msgq(sp, M_ERR, "181|Cursor not in a number");
142 /* Find the end of the word, possibly correcting the base. */
143 while (++end < len) {
148 if (p[end] == '8' || p[end] == '9') {
172 * If the line was at the end of the buffer, we have to copy it
173 * so we can guarantee that it's NULL-terminated. We make the
174 * buffer big enough to fit the line changes as well, and only
177 GET_SPACE_RETW(sp, bp, blen, len + 50);
179 MEMMOVE(bp, &p[beg], wlen);
186 * Octal or hex deal in unsigned longs, everything else is done
190 if ((nret = nget_slong(&lval, t, NULL, 10)) != NUM_OK)
192 ltmp = vp->character == '-' ? -change : change;
193 if (lval > 0 && ltmp > 0 && !NPFITS(LONG_MAX, lval, ltmp)) {
197 if (lval < 0 && ltmp < 0 && !NNFITS(LONG_MIN, lval, ltmp)) {
202 /* If we cross 0, signed numbers lose their sign. */
203 if (lval == 0 && ntype == fmt[SDEC])
205 nlen = SPRINTF(nbuf, sizeof(nbuf), ntype, lval);
207 if ((nret = nget_uslong(&ulval, t, NULL, base)) != NUM_OK)
209 if (vp->character == '+') {
210 if (!NPFITS(ULONG_MAX, ulval, change)) {
216 if (ulval < change) {
223 /* Correct for literal "0[Xx]" in format. */
227 nlen = SPRINTF(nbuf, sizeof(nbuf), ntype, wlen, ulval);
230 /* Build the new line. */
232 MEMMOVE(bp + beg, nbuf, nlen);
233 MEMMOVE(bp + beg + nlen, p + end, len - beg - (end - beg));
234 len = beg + nlen + (len - beg - (end - beg));
237 rval = db_set(sp, vp->m_start.lno, bp, len);
244 FREE_SPACEW(sp, bp, blen);
249 inc_err(SCR *sp, enum nresult nret)
258 msgq(sp, M_ERR, "182|Resulting number too large");
261 msgq(sp, M_ERR, "183|Resulting number too small");