2013-05-20 01:16:08 -04:00
|
|
|
/* region.c -- implements region.h */
|
|
|
|
#include "region.h"
|
|
|
|
|
2010-07-10 00:46:27 -04:00
|
|
|
/* region.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
2010-11-14 21:10:03 -05:00
|
|
|
* The routines in this file deal with the region, that magic space
|
|
|
|
* between "." and mark. Some functions are commands. Some functions are
|
|
|
|
* just for internal use.
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
2010-11-14 21:10:03 -05:00
|
|
|
* Modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2010-11-14 21:10:03 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-05-28 02:57:03 -04:00
|
|
|
#include "display.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "estruct.h"
|
|
|
|
#include "edef.h"
|
|
|
|
#include "line.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Kill the region. Ask "getregion"
|
|
|
|
* to figure out the bounds of the region.
|
|
|
|
* Move "." to the start, and kill the characters.
|
|
|
|
* Bound to "C-W".
|
|
|
|
*/
|
2005-09-30 19:37:36 -04:00
|
|
|
int killregion(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int s;
|
2010-07-10 00:46:27 -04:00
|
|
|
struct region region;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
2010-08-29 06:03:55 -04:00
|
|
|
return rdonly(); /* we are in read only mode */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = getregion(®ion)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lastflag & CFKILL) == 0) /* This is a kill type */
|
|
|
|
kdelete(); /* command, so do magic */
|
|
|
|
thisflag |= CFKILL; /* kill buffer stuff. */
|
|
|
|
curwp->w_dotp = region.r_linep;
|
|
|
|
curwp->w_doto = region.r_offset;
|
2010-08-29 06:03:55 -04:00
|
|
|
return ldelete(region.r_size, TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy all of the characters in the
|
|
|
|
* region to the kill buffer. Don't move dot
|
|
|
|
* at all. This is a bit like a kill region followed
|
|
|
|
* by a yank. Bound to "M-W".
|
|
|
|
*/
|
2005-09-30 19:37:36 -04:00
|
|
|
int copyregion(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *linep;
|
2010-02-27 05:38:22 -05:00
|
|
|
int loffs;
|
|
|
|
int s;
|
2010-07-10 00:46:27 -04:00
|
|
|
struct region region;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = getregion(®ion)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lastflag & CFKILL) == 0) /* Kill type command. */
|
|
|
|
kdelete();
|
|
|
|
thisflag |= CFKILL;
|
|
|
|
linep = region.r_linep; /* Current line. */
|
|
|
|
loffs = region.r_offset; /* Current offset. */
|
|
|
|
while (region.r_size--) {
|
|
|
|
if (loffs == llength(linep)) { /* End of line. */
|
|
|
|
if ((s = kinsert('\n')) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
linep = lforw(linep);
|
|
|
|
loffs = 0;
|
|
|
|
} else { /* Middle of line. */
|
|
|
|
if ((s = kinsert(lgetc(linep, loffs))) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
++loffs;
|
|
|
|
}
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(region copied)");
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lower case region. Zap all of the upper
|
|
|
|
* case characters in the region to lower case. Use
|
|
|
|
* the region code to set the limits. Scan the buffer,
|
|
|
|
* doing the changes. Call "lchange" to ensure that
|
|
|
|
* redisplay is done in all buffers. Bound to
|
|
|
|
* "C-X C-L".
|
|
|
|
*/
|
2005-09-30 19:37:36 -04:00
|
|
|
int lowerregion(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *linep;
|
2010-02-27 05:38:22 -05:00
|
|
|
int loffs;
|
|
|
|
int c;
|
|
|
|
int s;
|
2010-07-10 00:46:27 -04:00
|
|
|
struct region region;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
2010-08-29 06:03:55 -04:00
|
|
|
return rdonly(); /* we are in read only mode */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = getregion(®ion)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
lchange(WFHARD);
|
|
|
|
linep = region.r_linep;
|
|
|
|
loffs = region.r_offset;
|
|
|
|
while (region.r_size--) {
|
|
|
|
if (loffs == llength(linep)) {
|
|
|
|
linep = lforw(linep);
|
|
|
|
loffs = 0;
|
|
|
|
} else {
|
|
|
|
c = lgetc(linep, loffs);
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
lputc(linep, loffs, c + 'a' - 'A');
|
|
|
|
++loffs;
|
|
|
|
}
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Upper case region. Zap all of the lower
|
|
|
|
* case characters in the region to upper case. Use
|
|
|
|
* the region code to set the limits. Scan the buffer,
|
|
|
|
* doing the changes. Call "lchange" to ensure that
|
|
|
|
* redisplay is done in all buffers. Bound to
|
|
|
|
* "C-X C-L".
|
|
|
|
*/
|
2005-09-30 19:37:36 -04:00
|
|
|
int upperregion(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *linep;
|
2010-02-27 05:38:22 -05:00
|
|
|
int loffs;
|
|
|
|
int c;
|
|
|
|
int s;
|
2010-07-10 00:46:27 -04:00
|
|
|
struct region region;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
|
2010-08-29 06:03:55 -04:00
|
|
|
return rdonly(); /* we are in read only mode */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((s = getregion(®ion)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return s;
|
2005-09-30 18:26:09 -04:00
|
|
|
lchange(WFHARD);
|
|
|
|
linep = region.r_linep;
|
|
|
|
loffs = region.r_offset;
|
|
|
|
while (region.r_size--) {
|
|
|
|
if (loffs == llength(linep)) {
|
|
|
|
linep = lforw(linep);
|
|
|
|
loffs = 0;
|
|
|
|
} else {
|
|
|
|
c = lgetc(linep, loffs);
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
lputc(linep, loffs, c - 'a' + 'A');
|
|
|
|
++loffs;
|
|
|
|
}
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine figures out the
|
|
|
|
* bounds of the region in the current window, and
|
2010-07-10 00:46:27 -04:00
|
|
|
* fills in the fields of the "struct region" structure pointed
|
2005-05-31 11:50:56 -04:00
|
|
|
* to by "rp". Because the dot and mark are usually very
|
|
|
|
* close together, we scan outward from dot looking for
|
|
|
|
* mark. This should save time. Return a standard code.
|
|
|
|
* Callers of this routine should be prepared to get
|
|
|
|
* an "ABORT" status; we might make this have the
|
|
|
|
* conform thing later.
|
|
|
|
*/
|
2010-07-10 00:46:27 -04:00
|
|
|
int getregion(struct region *rp)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *flp;
|
|
|
|
struct line *blp;
|
2005-09-30 18:26:09 -04:00
|
|
|
long fsize;
|
|
|
|
long bsize;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (curwp->w_markp == NULL) {
|
|
|
|
mlwrite("No mark set in this window");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
if (curwp->w_dotp == curwp->w_markp) {
|
|
|
|
rp->r_linep = curwp->w_dotp;
|
|
|
|
if (curwp->w_doto < curwp->w_marko) {
|
|
|
|
rp->r_offset = curwp->w_doto;
|
|
|
|
rp->r_size =
|
|
|
|
(long) (curwp->w_marko - curwp->w_doto);
|
|
|
|
} else {
|
|
|
|
rp->r_offset = curwp->w_marko;
|
|
|
|
rp->r_size =
|
|
|
|
(long) (curwp->w_doto - curwp->w_marko);
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
blp = curwp->w_dotp;
|
|
|
|
bsize = (long) curwp->w_doto;
|
|
|
|
flp = curwp->w_dotp;
|
|
|
|
fsize = (long) (llength(flp) - curwp->w_doto + 1);
|
|
|
|
while (flp != curbp->b_linep || lback(blp) != curbp->b_linep) {
|
|
|
|
if (flp != curbp->b_linep) {
|
|
|
|
flp = lforw(flp);
|
|
|
|
if (flp == curwp->w_markp) {
|
|
|
|
rp->r_linep = curwp->w_dotp;
|
|
|
|
rp->r_offset = curwp->w_doto;
|
|
|
|
rp->r_size = fsize + curwp->w_marko;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
fsize += llength(flp) + 1;
|
|
|
|
}
|
|
|
|
if (lback(blp) != curbp->b_linep) {
|
|
|
|
blp = lback(blp);
|
|
|
|
bsize += llength(blp) + 1;
|
|
|
|
if (blp == curwp->w_markp) {
|
|
|
|
rp->r_linep = blp;
|
|
|
|
rp->r_offset = curwp->w_marko;
|
|
|
|
rp->r_size = bsize - curwp->w_marko;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mlwrite("Bug: lost mark");
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|