home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume41
/
vim
/
part15
< prev
next >
Wrap
Text File
|
1993-12-21
|
35KB
|
1,555 lines
Newsgroups: comp.sources.misc
From: mool@oce.nl (Bram Moolenaar)
Subject: v41i065: vim - Vi IMitation editor, v2.0, Part15/25
Message-ID: <1993Dec21.172340.1130@sparky.sterling.com>
X-Md4-Signature: aa30a595ebc088b885e76028f4c2934e
Keywords: utility, editor, vi, vim
Sender: kent@sparky.sterling.com (Kent Landfield)
Organization: Sterling Software
Date: Tue, 21 Dec 1993 17:23:40 GMT
Approved: kent@sparky.sterling.com
Submitted-by: mool@oce.nl (Bram Moolenaar)
Posting-number: Volume 41, Issue 65
Archive-name: vim/part15
Environment: UNIX, AMIGA, MS-DOS
Supersedes: vim: Volume 37, Issue 1-24
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of archive 15 (of 25)."
# Contents: vim/src/ops.c
# Wrapped by mool@oce-rd2 on Wed Dec 15 09:50:07 1993
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'vim/src/ops.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'vim/src/ops.c'\"
else
echo shar: Extracting \"'vim/src/ops.c'\" \(31254 characters\)
sed "s/^X//" >'vim/src/ops.c' <<'END_OF_FILE'
X/* vi:ts=4:sw=4
X *
X * VIM - Vi IMproved
X *
X * Code Contributions By: Bram Moolenaar mool@oce.nl
X * Tim Thompson twitch!tjt
X * Tony Andrews onecom!wldrdg!tony
X * G. R. (Fred) Walter watmath!watcgl!grwalter
X */
X
X/*
X * ops.c: implementation of various operators: doshift, dodelete, dotilde,
X * dochange, doyank, doput, dojoin
X */
X
X#include "vim.h"
X#include "globals.h"
X#include "proto.h"
X#include "param.h"
X#include "ops.h"
X
X/*
X * We have one yank buffer for normal yanks and puts, nine yank buffers for
X * deletes and 26 yank buffers for use by name.
X * Each yank buffer is an array of pointers to lines.
X */
Xstatic struct yankbuf
X{
X char **y_array; /* pointer to array of line pointers */
X linenr_t y_size; /* number of lines in y_array */
X char y_type; /* MLINE, MCHAR or MBLOCK */
X} y_buf[36]; /* 0..9 = number buffers, 10..35 = char buffers */
X
Xstatic struct yankbuf *y_current; /* ptr to current yank buffer */
Xstatic int yankappend; /* TRUE when appending */
Xstatic struct yankbuf *y_previous = NULL; /* ptr to last written yank buffer */
X
Xstatic void get_yank_buffer __ARGS((int));
Xstatic int stuff_yank __ARGS((int, char *));
Xstatic void free_yank __ARGS((long));
Xstatic void free_yank_all __ARGS((void));
Xstatic void block_prep __ARGS((linenr_t, int));
X
X/* variables use by block_prep, dodelete and doyank */
Xstatic int startspaces;
Xstatic int endspaces;
Xstatic int textlen;
Xstatic char *textstart;
Xstatic colnr_t textcol;
X
X/*
X * doshift - handle a shift operation
X */
X void
Xdoshift(op)
X int op;
X{
X register long i;
X
X if (!u_save((linenr_t)(Curpos.lnum - 1), (linenr_t)(Curpos.lnum + nlines)))
X return;
X
X Curpos.lnum += nlines; /* start with last line, leave cursor on first */
X for (i = nlines; --i >= 0; )
X if (lineempty(--Curpos.lnum))
X Curpos.col = 0;
X else
X {
X /* if (Visual_block)
X shift the block, not the whole line
X else */
X shift_line(op == LSHIFT, p_sr);
X }
X
X updateScreen(CURSUPD);
X
X if (nlines > p_report)
X smsg("%ld line%s %ced", nlines, plural(nlines),
X (op == RSHIFT) ? '>' : '<');
X}
X
X/*
X * shift the current line one shiftwidth left (if left != 0) or right
X * leaves cursor on first blank in the line
X */
X void
Xshift_line(left, round)
X int left;
X int round;
X{
X register int count;
X register int i, j;
X
X count = get_indent(); /* get current indent */
X
X if (round) /* round off indent */
X {
X i = count / (int)p_sw; /* compute new indent */
X j = count % (int)p_sw;
X if (j)
X {
X if (!left)
X ++i;
X }
X else if (left)
X {
X if (i)
X --i;
X }
X else
X ++i;
X count = i * (int)p_sw;
X }
X else /* original vi indent */
X {
X if (left)
X {
X count -= (int)p_sw;
X if (count < 0)
X count = 0;
X }
X else
X count += (int)p_sw;
X }
X set_indent(count, TRUE); /* set new indent */
X}
X
X/*
X * Set y_current and yankappend, according to the value of yankbuffer.
X */
X static void
Xget_yank_buffer(writing)
X int writing;
X{
X register int i;
X
X yankappend = FALSE;
X if (((yankbuffer == 0 && !writing) || yankbuffer == '"') && y_previous != NULL)
X {
X y_current = y_previous;
X return;
X }
X i = yankbuffer;
X if (isdigit(i))
X i -= '0';
X else if (islower(i))
X i -= 'a' - 10;
X else if (isupper(i))
X {
X i -= 'A' - 10;
X yankappend = TRUE;
X }
X else /* not 0-9, a-z or A-Z: use buffer 0 */
X i = 0;
X y_current = &(y_buf[i]);
X if (writing) /* remember the buffer we write into for doput() */
X y_previous = y_current;
X}
X
X/*
X * (stop) recording into a yank buffer
X */
X int
Xdorecord(c)
X int c;
X{
X char *p;
X static int bufname;
X
X if (Recording == FALSE) /* start recording */
X {
X if (!isalnum(c) && c != '"') /* registers 0-9, a-z and " are allowed */
X return FALSE;
X Recording = TRUE;
X showmode();
X bufname = c;
X return TRUE;
X }
X else /* stop recording */
X {
X Recording = FALSE;
X msg("");
X /* the trailing 'q' command will not have been put in the buffer */
X p = (char *)get_recorded();
X if (p == NULL)
X return FALSE;
X return (stuff_yank(bufname, p));
X }
X}
X
X/*
X * stuff string 'p' into yank buffer 'bufname' (append if uppercase)
X * 'p' is assumed to be alloced.
X */
X static int
Xstuff_yank(bufname, p)
X int bufname;
X char *p;
X{
X char *lp;
X char **pp;
X
X yankbuffer = bufname;
X if (yankbuffer == '.' || yankbuffer == '%') /* read-only buffer */
X return FALSE;
X get_yank_buffer(TRUE);
X if (yankappend && y_current->y_array != NULL)
X {
X pp = &(y_current->y_array[y_current->y_size - 1]);
X lp = lalloc((u_long)(strlen(*pp) + strlen(p) + 1), TRUE);
X if (lp == NULL)
X {
X free(p);
X return FALSE;
X }
X strcpy(lp, *pp);
X strcat(lp, p);
X free(p);
X free(*pp);
X *pp = lp;
X }
X else
X {
X free_yank_all();
X if ((y_current->y_array = (char **)alloc((unsigned)sizeof(char *))) == NULL)
X {
X free(p);
X return FALSE;
X }
X y_current->y_array[0] = p;
X y_current->y_size = 1;
X y_current->y_type = MCHAR; /* used to be MLINE, why? */
X }
X return TRUE;
X}
X
X/*
X * execute a yank buffer (register): copy it into the stuff buffer
X */
X int
Xdoexecbuf(c)
X int c;
X{
X static int lastc = NUL;
X long i;
X
X if (c == '@') /* repeat previous one */
X c = lastc;
X
X lastc = c;
X if (!isalnum(c) && c != '"') /* registers 0-9, a-z and " are allowed */
X return FALSE;
X
X yankbuffer = c;
X get_yank_buffer(FALSE);
X if (y_current->y_array == NULL)
X return FALSE;
X
X for (i = y_current->y_size; --i >= 0; )
X {
X /* insert newline between lines and after last line if type is MLINE */
X if (y_current->y_type == MLINE || i < y_current->y_size - 1)
X {
X if (ins_typestr("\n", FALSE) < 0)
X return FALSE;
X }
X if (ins_typestr(y_current->y_array[i], FALSE) < 0)
X return FALSE;
X }
X Exec_reg = TRUE; /* disable the 'q' command */
X return TRUE;
X}
X
X/*
X * insert a yank buffer: copy it into the Read buffer
X */
X int
Xinsertbuf(c)
X int c;
X{
X long i;
X
X if (c == '%') /* insert file name */
X {
X if (check_fname())
X return FALSE;
X stuffReadbuff(xFilename);
X return TRUE;
X }
X
X if (!isalnum(c) && c != '"') /* registers 0-9, a-z and " are allowed */
X return FALSE;
X
X yankbuffer = c;
X get_yank_buffer(FALSE);
X if (y_current->y_array == NULL)
X return FALSE;
X
X for (i = 0; i < y_current->y_size; ++i)
X {
X stuffReadbuff(y_current->y_array[i]);
X /* insert newline between lines and after last line if type is MLINE */
X if (y_current->y_type == MLINE || i < y_current->y_size - 1)
X stuffReadbuff("\n");
X }
X return TRUE;
X}
X
X/*
X * dodelete - handle a delete operation
X */
X void
Xdodelete()
X{
X register int n;
X linenr_t lnum;
X char *ptr;
X linenr_t old_lcount = line_count;
X
X /*
X * Imitate the strange Vi behaviour: If the delete spans more than one line
X * and mtype == MCHAR and the result is a blank line, make the delete
X * linewise. Don't do this for the change command.
X */
X if (mtype == MCHAR && nlines > 1 && operator == DELETE)
X {
X ptr = nr2ptr(endop.lnum) + endop.col + mincl;
X skipspace(&ptr);
X if (*ptr == NUL && startinmargin())
X mtype = MLINE;
X }
X
X /*
X * Shift number buffers if there is no yankbuffer defined and we do a
X * delete that contains a line break.
X */
X if (yankbuffer == 0 && (mtype == MLINE || nlines > 1))
X {
X y_current = &y_buf[9];
X free_yank_all(); /* free buffer nine */
X for (n = 9; n > 1; --n)
X y_buf[n] = y_buf[n - 1];
X y_previous = y_current = &y_buf[1];
X y_buf[1].y_array = NULL; /* set buffer one to empty */
X }
X else if (yankbuffer == '.' || yankbuffer == '%') /* read-only buffer */
X {
X beep();
X return;
X }
X else /* yank into specified buffer */
X get_yank_buffer(TRUE);
X
X /*
X * Do a yank of whatever we're about to delete. If there's too much stuff
X * to fit in the yank buffer, then get a confirmation before doing the
X * delete. This is crude, but simple. And it avoids doing a delete of
X * something we can't put back if we want.
X */
X if (!doyank(TRUE))
X {
X if (ask_yesno("cannot yank; delete anyway") != 'y')
X {
X emsg(e_abort);
X return;
X }
X }
X
X/*
X * block mode
X */
X if (Visual_block)
X {
X if (!u_save((linenr_t)(startop.lnum - 1), (linenr_t)(endop.lnum + 1)))
X return;
X
X for (lnum = Curpos.lnum; Curpos.lnum <= endop.lnum; ++Curpos.lnum)
X {
X block_prep(Curpos.lnum, TRUE);
X if (textlen == 0) /* nothing to delete */
X continue;
X
X /*
X * If we delete a TAB, it may be replaced by several characters.
X * Thus the number of characters may increase!
X */
X n = textlen - startspaces - endspaces;
X /* number of characters increases - make room */
X if (n < 0 && !canincrease(-n))
X continue;
X ptr = nr2ptr(Curpos.lnum) + textcol;
X /* copy the part after the deleted part */
X memmove(ptr + startspaces + endspaces, ptr + textlen, strlen(ptr + textlen) + 1);
X /* insert spaces */
X copy_spaces(ptr, (size_t)(startspaces + endspaces));
X if (n > 0)
X canincrease(0);
X }
X Curpos.lnum = lnum;
X CHANGED;
X updateScreen(VALID_TO_CURSCHAR);
X nlines = 0; /* no lines deleted */
X }
X else if (mtype == MLINE)
X {
X if (operator == CHANGE)
X {
X dellines((long)(nlines - 1), TRUE, TRUE);
X if (!u_saveCurpos())
X return;
X while (delchar(TRUE)); /* slow but simple */
X }
X else
X {
X dellines(nlines, TRUE, TRUE);
X }
X u_clearline(); /* "U" command should not be possible after "dd" */
X }
X else if (nlines == 1) /* delete characters within one line */
X {
X if (!u_saveCurpos())
X return;
X n = endop.col - startop.col + 1 - !mincl;
X while (n-- > 0)
X if (!delchar(TRUE))
X break;
X }
X else /* delete characters between lines */
X {
X if (!u_saveCurpos()) /* save first line for undo */
X return;
X n = Curpos.col;
X while (Curpos.col >= n) /* delete from cursor to end of line */
X if (!delchar(TRUE))
X break;
X
X startop = Curpos; /* remember Curpos */
X ++Curpos.lnum;
X dellines((long)(nlines - 2), TRUE, TRUE); /* includes save for undo */
X
X if (!u_saveCurpos()) /* save last line for undo */
X return;
X n = endop.col - !mincl;
X Curpos.col = 0;
X while (n-- >= 0) /* delete from start of line until endop */
X if (!delchar(TRUE))
X break;
X Curpos = startop; /* restore Curpos */
X dojoin(FALSE, TRUE);
X }
X
X if ((mtype == MCHAR && nlines == 1) || operator == CHANGE)
X {
X cursupdate();
X updateline();
X }
X else
X updateScreen(CURSUPD);
X
X msgmore(line_count - old_lcount);
X
X /* correct endop for deleted text (for "']" command) */
X if (Visual_block)
X endop.col = startop.col;
X else
X endop = startop;
X}
X
X/*
X * dotilde - handle the (non-standard vi) tilde operator
X */
X void
Xdotilde()
X{
X FPOS pos;
X
X if (!u_save((linenr_t)(startop.lnum - 1), (linenr_t)(endop.lnum + 1)))
X return;
X
X pos = startop;
X if (Visual_block) /* block mode */
X {
X for (; pos.lnum <= endop.lnum; ++pos.lnum)
X {
X block_prep(pos.lnum, FALSE);
X pos.col = textcol;
X while (--textlen >= 0)
X {
X swapchar(&pos);
X if (inc(&pos) == -1) /* at end of file */
X break;
X }
X }
X }
X else /* not block mode */
X {
X if (mtype == MLINE)
X {
X pos.col = 0;
X endop.col = strlen(nr2ptr(endop.lnum));
X if (endop.col)
X --endop.col;
X }
X else if (!mincl)
X dec(&endop);
X
X while (ltoreq(pos, endop))
X {
X swapchar(&pos);
X if (inc(&pos) == -1) /* at end of file */
X break;
X }
X }
X
X if (mtype == MCHAR && nlines == 1 && !Visual_block)
X {
X cursupdate();
X updateline();
X }
X else
X updateScreen(CURSUPD);
X
X if (nlines > p_report)
X smsg("%ld line%s ~ed", nlines, plural(nlines));
X}
X
X/*
X * If operator == UPPER: make uppercase,
X * if operator == LOWER: make lowercase,
X * else swap case of character at 'pos'
X */
X void
Xswapchar(pos)
X FPOS *pos;
X{
X int c;
X
X c = gchar(pos);
X if (islower(c) && operator != LOWER)
X {
X pchar(*pos, toupper(c));
X CHANGED;
X }
X else if (isupper(c) && operator != UPPER)
X {
X pchar(*pos, tolower(c));
X CHANGED;
X }
X}
X
X/*
X * dochange - handle a change operation
X */
X void
Xdochange()
X{
X register colnr_t l;
X
X l = startop.col;
X
X if (!no_op)
X dodelete();
X
X if ((l > Curpos.col) && !lineempty(Curpos.lnum))
X incCurpos();
X
X startinsert(NUL, FALSE, (linenr_t)1);
X}
X
X/*
X * set all the yank buffers to empty (called from main())
X */
X void
Xinit_yank()
X{
X register int i;
X
X for (i = 0; i < 36; ++i)
X y_buf[i].y_array = NULL;
X}
X
X/*
X * Free "n" lines from the current yank buffer.
X * Called for normal freeing and in case of error.
X */
X static void
Xfree_yank(n)
X long n;
X{
X if (y_current->y_array != NULL)
X {
X register long i;
X
X for (i = n; --i >= 0; )
X {
X if (i % 1000 == 999) /* this may take a while */
X smsg("freeing %ld lines", i + 1);
X free(y_current->y_array[i]);
X }
X free((char *)y_current->y_array);
X y_current->y_array = NULL;
X if (n >= 1000)
X msg("");
X }
X}
X
X static void
Xfree_yank_all()
X{
X free_yank(y_current->y_size);
X}
X
X/*
X * Yank the text between Curpos and startpos into a yank buffer.
X * If we are to append ("uppercase), we first yank into a new yank buffer and
X * then concatenate the old and the new one (so we keep the old one in case
X * of out-of-memory).
X */
X int
Xdoyank(deleting)
X int deleting;
X{
X long i; /* index in y_array[] */
X struct yankbuf *curr; /* copy of y_current */
X struct yankbuf new; /* new yank buffer when appending */
X char **new_ptr;
X register linenr_t lnum; /* current line number */
X long j;
X int yanktype = mtype;
X long yanklines = nlines;
X linenr_t yankendlnum = endop.lnum;
X
X char *pnew;
X
X if (yankbuffer == '.' || yankbuffer == '%') /* read-only buffer */
X {
X beep();
X return FALSE;
X }
X if (!deleting) /* dodelete() already set y_current */
X get_yank_buffer(TRUE);
X
X curr = y_current;
X if (yankappend && y_current->y_array != NULL) /* append to existing contents */
X y_current = &new;
X else
X free_yank_all(); /* free previously yanked lines */
X
X/*
X * If the cursor was in column 1 before and after the movement, the
X * yank is always linewise.
X */
X if (mtype == MCHAR && startop.col == 0 && endop.col == 0 && nlines > 1)
X {
X yanktype = MLINE;
X if (mincl == FALSE && yankendlnum > startop.lnum)
X {
X --yankendlnum;
X --yanklines;
X }
X }
X
X y_current->y_size = yanklines;
X y_current->y_type = yanktype; /* set the yank buffer type */
X y_current->y_array = (char **)lalloc((u_long)(sizeof(char *) * yanklines), TRUE);
X
X if (y_current->y_array == NULL)
X {
X y_current = curr;
X return FALSE;
X }
X
X i = 0;
X lnum = startop.lnum;
X
X if (Visual_block)
X {
X/*
X * block mode
X */
X y_current->y_type = MBLOCK; /* set the yank buffer type */
X for ( ; lnum <= yankendlnum; ++lnum)
X {
X block_prep(lnum, FALSE);
X if ((pnew = alloc(startspaces + endspaces + textlen + 1)) == NULL)
X goto fail;
X y_current->y_array[i++] = pnew;
X copy_spaces(pnew, (size_t)startspaces);
X pnew += startspaces;
X strncpy(pnew, textstart, (size_t)textlen);
X pnew += textlen;
X copy_spaces(pnew, (size_t)endspaces);
X pnew += endspaces;
X *pnew = NUL;
X }
X }
X else
X {
X/*
X * there are three parts for non-block mode:
X * 1. if yanktype != MLINE yank last part of the top line
X * 2. yank the lines between startop and endop, inclusive when yanktype == MLINE
X * 3. if yanktype != MLINE yank first part of the bot line
X */
X if (yanktype != MLINE)
X {
X if (yanklines == 1) /* startop and endop on same line */
X {
X j = endop.col - startop.col + 1 - !mincl;
X if ((y_current->y_array[0] = strnsave(nr2ptr(lnum) + startop.col, (int)j)) == NULL)
X {
X fail:
X free_yank(i); /* free the lines that we allocated */
X y_current = curr;
X return FALSE;
X }
X goto success;
X }
X if ((y_current->y_array[0] = strsave(nr2ptr(lnum++) + startop.col)) == NULL)
X goto fail;
X ++i;
X }
X
X while (yanktype == MLINE ? (lnum <= yankendlnum) : (lnum < yankendlnum))
X {
X if ((y_current->y_array[i] = strsave(nr2ptr(lnum++))) == NULL)
X goto fail;
X ++i;
X }
X if (yanktype != MLINE)
X {
X if ((y_current->y_array[i] = strnsave(nr2ptr(yankendlnum), endop.col + 1 - !mincl)) == NULL)
X goto fail;
X }
X }
X
Xsuccess:
X if (curr != y_current) /* append the new block to the old block */
X {
X new_ptr = (char **)lalloc((u_long)(sizeof(char *) * (curr->y_size + y_current->y_size)), TRUE);
X if (new_ptr == NULL)
X goto fail;
X for (j = 0; j < curr->y_size; ++j)
X new_ptr[j] = curr->y_array[j];
X free(curr->y_array);
X curr->y_array = new_ptr;
X
X if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
X curr->y_type = MLINE;
X if (curr->y_type == MCHAR) /* concatenate the last line of the old
X block with the first line of the new block */
X {
X new_ptr = (char **)lalloc((u_long)(strlen(curr->y_array[curr->y_size - 1]) + strlen(y_current->y_array[0]) + 1), TRUE);
X if (new_ptr == NULL)
X {
X i = y_current->y_size - 1;
X goto fail;
X }
X strcpy((char *)new_ptr, curr->y_array[--j]);
X strcat((char *)new_ptr, y_current->y_array[0]);
X free(curr->y_array[j]);
X free(y_current->y_array[0]);
X curr->y_array[j++] = (char *)new_ptr;
X i = 1;
X }
X else
X i = 0;
X while (i < y_current->y_size)
X curr->y_array[j++] = y_current->y_array[i++];
X curr->y_size = j;
X free(y_current->y_array);
X y_current = curr;
X }
X if (operator == YANK) /* don't do this when deleting */
X {
X if (yanktype == MCHAR && !Visual_block)
X --yanklines;
X if (yanklines > p_report)
X {
X cursupdate(); /* redisplay now, so message is not deleted */
X smsg("%ld line%s yanked", yanklines, plural(yanklines));
X }
X }
X
X return TRUE;
X}
X
X void
Xdoput(dir, count)
X int dir;
X long count;
X{
X char *ptr, *ep;
X int newlen;
X int totlen = 0; /* init for gcc */
X linenr_t lnum;
X int col;
X long i; /* index in y_array[] */
X int y_type;
X long y_size;
X char **y_array;
X long nlines = 0;
X int vcol;
X int delchar;
X int incr = 0;
X long j;
X FPOS newCurpos;
X int commandchar;
X char temp[2];
X
X startop = Curpos; /* default for "'[" command */
X if (dir == FORWARD)
X startop.col++;
X endop = Curpos; /* default for "']" command */
X commandchar = (dir == FORWARD ? (count == -1 ? 'o' : 'a') : (count == -1 ? 'O' : 'i'));
X if (yankbuffer == '.') /* use inserted text */
X {
X stuff_inserted(commandchar, count, FALSE);
X return;
X }
X else if (yankbuffer == '%') /* use file name */
X {
X if (!check_fname())
X {
X stuffcharReadbuff(commandchar);
X stuffReadbuff(xFilename);
X stuffcharReadbuff(ESC);
X }
X return;
X }
X get_yank_buffer(FALSE);
X
X y_type = y_current->y_type;
X y_size = y_current->y_size;
X y_array = y_current->y_array;
X
X if (count == -1) /* :put command */
X {
X y_type = MLINE;
X count = 1;
X }
X
X if (y_size == 0 || y_array == NULL)
X {
X temp[0] = yankbuffer;
X temp[1] = NUL;
X emsg2("Nothing in register %s", temp);
X return;
X }
X
X if (y_type == MBLOCK)
X {
X lnum = Curpos.lnum + y_size + 1;
X if (lnum > line_count)
X lnum = line_count + 1;
X if (!u_save(Curpos.lnum - 1, lnum))
X return;
X }
X else if (!u_saveCurpos())
X return;
X
X newlen = strlen(y_array[0]);
X CHANGED;
X
X lnum = Curpos.lnum;
X col = Curpos.col;
X
X/*
X * block mode
X */
X if (y_type == MBLOCK)
X {
X if (dir == FORWARD && gcharCurpos() != NUL)
X {
X col = getvcol(&Curpos, 3) + 1;
X ++Curpos.col;
X }
X else
X col = getvcol(&Curpos, 2);
X for (i = 0; i < y_size; ++i)
X {
X startspaces = 0;
X endspaces = 0;
X textcol = 0;
X vcol = 0;
X delchar = 0;
X
X /* add a new line */
X if (Curpos.lnum > line_count)
X {
X ep = alloc_line(0);
X if (ep == NULL)
X goto error;
X appendline(line_count, ep);
X ++nlines;
X }
X ptr = nr2ptr(Curpos.lnum);
X while (vcol < col && *ptr)
X {
X /* Count a tab for what it's worth (if list mode not on) */
X incr = chartabsize(*ptr, vcol);
X vcol += incr;
X ++ptr;
X ++textcol;
X }
X if (vcol < col) /* line too short, padd with spaces */
X {
X startspaces = col - vcol;
X }
X else if (vcol > col)
X {
X endspaces = vcol - col;
X startspaces = incr - endspaces;
X --textcol;
X delchar = 1;
X }
X newlen = strlen(y_array[i]);
X totlen = count * newlen + startspaces + endspaces;
X if (!canincrease(totlen))
X break;
X ptr = nr2ptr(Curpos.lnum) + textcol;
X
X /* move the text after the cursor to the end of the line. */
X memmove(ptr + totlen - delchar, ptr, strlen(ptr) + 1);
X /* may insert some spaces before the new text */
X copy_spaces(ptr, (size_t)startspaces);
X ptr += startspaces;
X /* insert the new text */
X for (j = 0; j < count; ++j)
X {
X strncpy(ptr, y_array[i], (size_t)newlen);
X ptr += newlen;
X }
X /* may insert some spaces after the new text */
X copy_spaces(ptr, (size_t)endspaces);
X
X ++Curpos.lnum;
X if (i == 0)
X Curpos.col += startspaces;
X }
X endop.lnum = Curpos.lnum - 1; /* for "']" command */
X endop.col = textcol + totlen - 1;
X Curpos.lnum = lnum;
X cursupdate();
X updateScreen(VALID_TO_CURSCHAR);
X }
X else /* not block mode */
X {
X if (y_type == MCHAR)
X {
X /* if type is MCHAR, FORWARD is the same as BACKWARD on the next character */
X if (dir == FORWARD && gcharCurpos() != NUL)
X {
X ++col;
X if (newlen)
X {
X ++Curpos.col;
X ++endop.col;
X }
X }
X newCurpos = Curpos;
X }
X else if (dir == BACKWARD)
X /* if type is MLINE, BACKWARD is the same as FORWARD on the previous line */
X --lnum;
X else /* type == MLINE, dir == FORWARD */
X {
X startop.col = 0;
X startop.lnum++;
X }
X
X/*
X * simple case: insert into current line
X */
X if (y_type == MCHAR && y_size == 1)
X {
X i = count * newlen;
X if (i)
X {
X if (!canincrease((int)i))
X return; /* alloc() will give error message */
X ep = nr2ptr(lnum) + col;
X memmove(ep + i, ep, strlen(ep) + 1);
X Curpos.col += (colnr_t)(i - 1); /* put cursor on last putted char */
X for (i = 0; i < count; ++i)
X {
X strncpy(ep, y_array[0], (size_t)newlen);
X ep += newlen;
X }
X }
X endop = Curpos;
X updateline();
X }
X else
X {
X if (y_type == MCHAR)
X --y_size;
X while (--count >= 0)
X {
X i = 0;
X if (y_type == MCHAR)
X {
X /*
X * Split the current line in two at the insert position.
X * Append y_array[0] to first line.
X * Insert y_array[size - 1] in front of second line.
X */
X ptr = nr2ptr(lnum) + col;
X col = strlen(y_array[y_size]);
X ep = alloc_line((unsigned)(strlen(ptr) + col));
X if (ep == NULL)
X goto error;
X strcpy(ep, y_array[y_size]);
X strcat(ep, ptr);
X appendline(lnum, ep); /* insert in second line */
X ++nlines;
X *ptr = NUL;
X Curpos.lnum = lnum;
X if (!canincrease(newlen)) /* lnum == Curpos.lnum! */
X goto error;
X strcat(nr2ptr(lnum), y_array[0]);/* append to first line */
X i = 1;
X }
X
X while (i < y_size)
X {
X ep = save_line(y_array[i++]);
X if (ep == NULL)
X goto error;
X appendline(lnum++, ep);
X ++nlines;
X }
X if (y_type == MCHAR)
X ++lnum; /* lnum is now number of line below inserted lines */
X }
X
X endop.lnum = lnum; /* for "']" command */
X if (y_type == MLINE)
X {
X Curpos.col = 0;
X endop.col = 0;
X if (dir == FORWARD)
X {
X updateScreen(NOT_VALID); /* recompute Botline */
X ++Curpos.lnum;
X }
X /* put cursor on first non-blank in last inserted line */
X beginline(TRUE);
X }
X else /* put cursor on first inserted character */
X {
X if (col > 1)
X endop.col = col - 1;
X else
X endop.col = 0;
X Curpos = newCurpos;
X }
X
Xerror:
X updateScreen(CURSUPD);
X }
X }
X
X msgmore(nlines);
X set_want_col = TRUE;
X}
X
X/*
X * display the contents of the yank buffers
X */
X void
Xdodis()
X{
X register int i, n;
X register long j;
X register char *p;
X register struct yankbuf *yb;
X
X#ifdef AMIGA
X settmode(0); /* set cooked mode so output can be halted */
X#endif
X for (i = -1; i < 36; ++i)
X {
X if (i == -1)
X {
X if (y_previous != NULL)
X yb = y_previous;
X else
X yb = &(y_buf[0]);
X }
X else
X yb = &(y_buf[i]);
X if (yb->y_array != NULL)
X {
X if (i == -1)
X outstrn("\"\"");
X else
X {
X outchar('"');
X if (i < 10)
X outchar(i + '0');
X else
X outchar(i + 'a' - 10);
X }
X outchar(' ');
X
X n = (int)Columns - 4;
X for (j = 0; j < yb->y_size && n > 0; ++j)
X {
X if (j)
X {
X outstrn("^J");
X n -= 2;
X }
X for (p = yb->y_array[j]; *p && n > 0; ++p)
X {
X outstrn(transchar(*p));
X n -= charsize(*p);
X }
X }
X outchar('\n');
X flushbuf();
X }
X }
X#ifdef AMIGA
X settmode(1);
X#endif
X wait_return(TRUE);
X}
X
X/*
X * join 'count' lines (minimal 2), including u_save()
X */
X void
Xdodojoin(count, insert_space, redraw)
X long count;
X int insert_space;
X int redraw;
X{
X if (!u_save((linenr_t)(Curpos.lnum - 1), (linenr_t)(Curpos.lnum + count)))
X return;
X
X while (--count > 0)
X if (!dojoin(insert_space, redraw))
X {
X beep();
X break;
X }
X
X if (redraw)
X updateScreen(VALID_TO_CURSCHAR);
X}
X
X int
Xdojoin(insert_space, redraw)
X int insert_space;
X int redraw;
X{
X char *curr;
X char *next;
X char *endcurr;
X int currsize; /* size of the current line */
X int nextsize; /* size of the next line */
X int spaces; /* number of spaces to insert */
X int rows_to_del; /* number of rows on screen to delete */
X linenr_t t;
X
X if (Curpos.lnum == line_count) /* on last line */
X return FALSE;
X
X rows_to_del = plines_m(Curpos.lnum, Curpos.lnum + 1);
X curr = nr2ptr(Curpos.lnum);
X currsize = strlen(curr);
X next = nr2ptr((linenr_t)(Curpos.lnum + 1));
X spaces = 0;
X if (insert_space)
X {
X skipspace(&next);
X spaces = 1;
X if (*next == ')' || currsize == 0)
X spaces = 0;
X else
X {
X endcurr = curr + currsize - 1;
X if (*endcurr == ' ' || *endcurr == TAB)
X {
X spaces = 0;
X if (currsize > 1)
X --endcurr;
X }
X if (p_js && strchr(".!?", *endcurr) != NULL)
X spaces = 2;
X }
X }
X nextsize = strlen(next);
X if (!canincrease(nextsize + spaces))
X return FALSE;
X
X /*
X * Append the spaces and the next line. Curr has to be obtained again,
X * because canincrease() will have changed the pointer.
X */
X curr = nr2ptr(Curpos.lnum) + currsize;
X while (spaces--)
X *curr++ = ' ';
X strcpy(curr, next);
X
X /*
X * Delete the following line. To do this we move the cursor there
X * briefly, and then move it back. After dellines() the cursor may
X * have moved up (last line deleted), so the current lnum is kept in t.
X */
X t = Curpos.lnum;
X ++Curpos.lnum;
X dellines(1L, FALSE, FALSE);
X Curpos.lnum = t;
X
X /*
X * the number of rows on the screen is reduced by the difference
X * in number of rows of the two old lines and the one new line
X */
X if (redraw)
X {
X rows_to_del -= plines(Curpos.lnum);
X if (rows_to_del > 0)
X s_del(Cursrow, rows_to_del, TRUE);
X }
X
X /*
X * go to first character of the joined line
X */
X if (currsize == 0)
X Curpos.col = 0;
X else
X {
X Curpos.col = currsize - 1;
X oneright();
X }
X CHANGED;
X
X return TRUE;
X}
X
X/*
X * implementation of the format operator 'Q'
X */
X void
Xdoformat()
X{
X /* prepare undo and join the lines */
X dodojoin((long)nlines, TRUE, FALSE);
X
X /* put cursor on last non-space */
X coladvance(MAXCOL);
X while (Curpos.col && isspace(gcharCurpos()))
X decCurpos();
X curs_columns(FALSE); /* update Cursvcol */
X
X /* do the formatting */
X State = INSERT; /* for Opencmd() */
X insertchar(NUL);
X State = NORMAL;
X updateScreen(NOT_VALID);
X}
X
X void
Xstartinsert(initstr, startln, count)
X int initstr;
X int startln; /* if set, insert at start of line */
X long count;
X{
X Insstart = Curpos;
X if (startln)
X Insstart.col = 0;
X
X if (initstr != NUL)
X {
X ResetRedobuff();
X AppendNumberToRedobuff(count);
X AppendCharToRedobuff(initstr);
X }
X
X if (initstr == 'R')
X State = REPLACE;
X else
X State = INSERT;
X
X if (p_smd)
X showmode();
X
X change_warning(); /* give a warning if readonly */
X edit(count);
X}
X
X/*
X * prepare a few things for block mode yank/delete/tilde
X *
X * for delete:
X * - textlen includes the first/last char to be (partly) deleted
X * - start/endspaces is the number of columns that are taken by the
X * first/last deleted char minus the number of columns that have to be deleted.
X * for yank and tilde:
X * - textlen includes the first/last char to be wholly yanked
X * - start/endspaces is the number of columns of the first/last yanked char
X * that are to be yanked.
X */
X static void
Xblock_prep(lnum, delete)
X linenr_t lnum;
X int delete;
X{
X int vcol;
X int incr = 0;
X char *pend;
X
X startspaces = 0;
X endspaces = 0;
X textlen = 0;
X textcol = 0;
X vcol = 0;
X textstart = nr2ptr(lnum);
X while (vcol < startvcol && *textstart)
X {
X /* Count a tab for what it's worth (if list mode not on) */
X incr = chartabsize(*textstart, vcol);
X vcol += incr;
X ++textstart;
X ++textcol;
X }
X if (vcol < startvcol) /* line too short */
X {
X if (!delete)
X endspaces = endvcol - startvcol + 1;
X }
X else /* vcol >= startvcol */
X {
X startspaces = vcol - startvcol;
X if (delete && vcol > startvcol)
X startspaces = incr - startspaces;
X pend = textstart;
X while (vcol <= endvcol && *pend)
X {
X /* Count a tab for what it's worth (if list mode not on) */
X incr = chartabsize(*pend, vcol);
X vcol += incr;
X ++pend;
X }
X if (vcol < endvcol && !delete) /* line too short */
X {
X endspaces = endvcol - vcol;
X }
X else if (vcol > endvcol)
X {
X if (delete)
X endspaces = vcol - endvcol - 1;
X else if (pend != textstart)
X {
X endspaces = incr - (vcol - endvcol);
X if (endspaces)
X --pend;
X }
X }
X if (delete && startspaces)
X {
X --textstart;
X --textcol;
X }
X textlen = (int)(pend - textstart);
X }
X}
X
X int
Xdoaddsub(c, Prenum1)
X int c;
X linenr_t Prenum1;
X{
X register int col;
X char buf[30];
X int hex; /* 'x' or 'X': hexadecimal; '0': octal */
X static int hexupper = FALSE; /* 0xABC */
X long n;
X char *ptr;
X
X ptr = nr2ptr(Curpos.lnum);
X col = Curpos.col;
X
X /* first check if we are on a hexadecimal number */
X while (col > 0 && isxdigit(ptr[col]))
X --col;
X if (col > 0 && (ptr[col] == 'X' || ptr[col] == 'x') &&
X ptr[col - 1] == '0' && isxdigit(ptr[col + 1]))
X --col; /* found hexadecimal number */
X else
X {
X /* first search forward and then backward for start of number */
X col = Curpos.col;
X
X while (ptr[col] != NUL && !isdigit(ptr[col]))
X ++col;
X
X while (col > 0 && isdigit(ptr[col - 1]))
X --col;
X }
X
X if (isdigit(ptr[col]) && u_saveCurpos())
X {
X set_want_col = TRUE;
X
X if (ptr[col] != '0')
X hex = 0; /* decimal */
X else
X {
X hex = TO_UPPER(ptr[col + 1]); /* assume hexadecimal */
X if (hex != 'X' || !isxdigit(ptr[col + 2]))
X {
X if (isdigit(hex))
X hex = '0'; /* octal */
X else
X hex = 0; /* 0 by itself is decimal */
X }
X }
X
X if (!hex && col > 0 && ptr[col - 1] == '-')
X --col;
X
X ptr += col;
X if (hex == '0')
X sscanf(ptr, "%lo", &n);
X else if (hex)
X sscanf(ptr, "%lx", &n); /* "%X" doesn't work! */
X else
X n = atol(ptr);
X
X if (c == Ctrl('A'))
X n += Prenum1;
X else
X n -= Prenum1;
X
X if (hex == 'X') /* skip the '0x' */
X col += 2;
X Curpos.col = col;
X do /* delete the old number */
X {
X if (isalpha(c))
X {
X if (isupper(c))
X hexupper = TRUE;
X else
X hexupper = FALSE;
X }
X delchar(FALSE);
X c = gcharCurpos();
X }
X while (hex ? (hex == '0' ? c >= '0' && c <= '7' : isxdigit(c)) : isdigit(c));
X
X if (hex == '0')
X sprintf(buf, "0%lo", n);
X else if (hexupper)
X sprintf(buf, "%lX", n);
X else if (hex)
X sprintf(buf, "%lx", n);
X else
X sprintf(buf, "%ld", n);
X insstr(buf); /* insert the new number */
X --Curpos.col;
X updateline();
X return TRUE;
X }
X else
X {
X beep();
X return FALSE;
X }
X}
X
X/*
X * Return TRUE if startop is on or before the first non-blank character in the line
X */
X int
Xstartinmargin()
X{
X int n;
X char *ptr;
X
X n = 0;
X for (ptr = nr2ptr(startop.lnum); *ptr && isspace(*ptr); ++ptr)
X ++n;
X return (n >= startop.col);
X}
END_OF_FILE
if test 31254 -ne `wc -c <'vim/src/ops.c'`; then
echo shar: \"'vim/src/ops.c'\" unpacked with wrong size!
fi
chmod +x 'vim/src/ops.c'
# end of 'vim/src/ops.c'
fi
echo shar: End of archive 15 \(of 25\).
cp /dev/null ark15isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ; do
if test ! -f ark${I}isdone ; then
MISSING="${MISSING} ${I}"
fi
done
if test "${MISSING}" = "" ; then
echo You have unpacked all 25 archives.
rm -f ark[1-9]isdone ark[1-9][0-9]isdone
else
echo You still need to unpack the following archives:
echo " " ${MISSING}
fi
## End of shell archive.
exit 0
===============================================================================
Bram Moolenaar | DISCLAIMER: This note does not
Oce Nederland B.V., Research & Development | necessarily represent the position
p.o. box 101, 5900 MA Venlo | of Oce-Nederland B.V. Therefore
The Netherlands phone +31 77 594077 | no liability or responsibility for
UUCP: mool@oce.nl fax +31 77 595473 | whatever will be accepted.
exit 0 # Just in case...