2013-05-20 01:16:08 -04:00
|
|
|
/* word.c -- implements word.h */
|
|
|
|
#include "word.h"
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* The routines in this file implement commands that work word or a
|
|
|
|
paragraph at a time. There are all sorts of word mode commands. If I
|
|
|
|
do any sentence mode commands, they are likely to be put in this file.
|
|
|
|
|
|
|
|
Modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2021-07-19 03:39:00 -04:00
|
|
|
#include <assert.h>
|
2021-07-16 00:24:13 -04:00
|
|
|
#include <stdlib.h> /* malloc, free */
|
|
|
|
#include <string.h> /* memcpy */
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2013-05-28 03:10:16 -04:00
|
|
|
#include "basic.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "buffer.h"
|
2021-08-18 04:54:35 -04:00
|
|
|
#include "defines.h"
|
2019-07-25 07:13:40 -04:00
|
|
|
#include "isa.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2015-02-12 23:31:59 -05:00
|
|
|
#include "mlout.h"
|
2013-05-28 00:37:04 -04:00
|
|
|
#include "random.h"
|
2013-05-28 01:18:47 -04:00
|
|
|
#include "region.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-07-16 00:24:13 -04:00
|
|
|
#define ALLOCSZ 32
|
2013-10-10 23:20:06 -04:00
|
|
|
#define TAB 0x09 /* a tab character */
|
|
|
|
|
2013-09-29 01:26:48 -04:00
|
|
|
static int justflag = FALSE ; /* justify, don't fill */
|
|
|
|
|
2013-09-25 03:09:09 -04:00
|
|
|
static int inword( void) ;
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* Word wrap on n-spaces. Back-over whatever precedes the point on the
|
|
|
|
current line and stop on the first word-break or the beginning of the
|
|
|
|
line. If we reach the beginning of the line, jump back to the end of
|
|
|
|
the word and start a new line. Otherwise, break the line at the
|
|
|
|
word-break, eat it, and jump back to the end of the word.
|
|
|
|
|
|
|
|
Returns TRUE on success, FALSE on errors.
|
|
|
|
|
|
|
|
@f: default flag.
|
|
|
|
@n: numeric argument.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( wrapword) {
|
2010-02-27 05:38:22 -05:00
|
|
|
int cnt; /* size of word wrapped to next line */
|
|
|
|
int c; /* charector temporary */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* backup from the <NL> 1 char */
|
|
|
|
if (!backchar(0, 1))
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* back up until we aren't in a word,
|
|
|
|
make sure there is a break in the line */
|
|
|
|
cnt = 0;
|
|
|
|
while (((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ')
|
2005-09-30 18:26:09 -04:00
|
|
|
&& (c != '\t')) {
|
2005-05-31 11:50:56 -04:00
|
|
|
cnt++;
|
|
|
|
if (!backchar(0, 1))
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if we make it to the beginning, start a new line */
|
|
|
|
if (curwp->w_doto == 0) {
|
|
|
|
gotoeol(FALSE, 0);
|
2010-08-29 06:03:55 -04:00
|
|
|
return lnewline();
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete the forward white space */
|
|
|
|
if (!forwdel(0, 1))
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* put in a end of line */
|
|
|
|
if (!lnewline())
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and past the first word */
|
|
|
|
while (cnt-- > 0) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Move the cursor backward by "n" words. All of the details of motion are
|
|
|
|
performed by the "backchar" and "forwchar" routines. Error if you try
|
|
|
|
to move beyond the buffers.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( backword) {
|
2019-08-02 23:05:16 -04:00
|
|
|
if( n < 0)
|
|
|
|
return forwword( f, -n) ;
|
|
|
|
|
|
|
|
if( backchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
while( n--) {
|
|
|
|
while( !inword())
|
|
|
|
if( backchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if( backchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
} while( inword()) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2019-08-02 23:05:16 -04:00
|
|
|
|
|
|
|
return forwchar( FALSE, 1) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Move the cursor forward by the specified number of words. All of the
|
|
|
|
motion is done by "forwchar". Error if you try and move beyond the
|
|
|
|
buffer's end.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( forwword) {
|
2019-08-02 23:05:16 -04:00
|
|
|
if( n < 0)
|
|
|
|
return backword( f, -n) ;
|
|
|
|
|
|
|
|
while( n--) {
|
|
|
|
while( inword())
|
|
|
|
if( forwchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if( forwchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
} while( !inword()) ;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2019-08-02 23:05:16 -04:00
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Word capitalize, to upper and to lower
|
|
|
|
*/
|
|
|
|
static boolean uniflip( boolean toupper_f) { /* flip unicode case and forward */
|
|
|
|
unicode_t c ;
|
|
|
|
int len ;
|
|
|
|
|
|
|
|
len = lgetchar( &c) ; /* len => unicode or extended ASCII */
|
|
|
|
if( (c <= 255) && ( toupper_f ? islower( c) : isupper( c))) {
|
|
|
|
c = flipcase( c) ;
|
|
|
|
ldelchar( 1, FALSE) ;
|
|
|
|
if( len == 1)
|
|
|
|
linsert_byte( 1, c) ;
|
|
|
|
else
|
|
|
|
linsert( 1, c) ;
|
|
|
|
|
|
|
|
lchange( WFHARD) ;
|
|
|
|
} else
|
|
|
|
if( forwchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean capcapword( int n, boolean first_f, boolean rest_f) {
|
2021-07-19 08:50:32 -04:00
|
|
|
assert( !(curbp->b_mode & MDVIEW)) ;
|
2019-08-02 23:05:16 -04:00
|
|
|
|
|
|
|
if( n < 0)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
while( n--) {
|
|
|
|
while( !inword())
|
|
|
|
if( forwchar( FALSE, 1) == FALSE)
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
if( !uniflip( first_f))
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
while( inword())
|
|
|
|
if( !uniflip( rest_f))
|
|
|
|
return FALSE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2019-08-02 23:05:16 -04:00
|
|
|
|
|
|
|
return TRUE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Move the cursor forward by the specified number of words. As you move,
|
|
|
|
convert any characters to upper case. Error if you try and move beyond
|
|
|
|
the end of the buffer. Bound to "M-U".
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( upperword) {
|
2019-08-02 23:05:16 -04:00
|
|
|
return capcapword( n, TRUE, TRUE) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2019-08-02 23:05:16 -04:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* Move the cursor forward by the specified number of words. As you move
|
|
|
|
convert characters to lower case. Error if you try and move over the
|
|
|
|
end of the buffer. Bound to "M-L".
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( lowerword) {
|
2019-08-02 23:05:16 -04:00
|
|
|
return capcapword( n, FALSE, FALSE) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Move the cursor forward by the specified number of words. As you move
|
|
|
|
convert the first character of the word to upper case, and subsequent
|
|
|
|
characters to lower case. Error if you try and move past the end of the
|
|
|
|
buffer. Bound to "M-C".
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( capword) {
|
2019-08-02 23:05:16 -04:00
|
|
|
return capcapword( n, TRUE, FALSE) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Kill forward by "n" words. Remember the location of dot. Move forward
|
|
|
|
by the right number of words. Put dot back where it was and issue the
|
|
|
|
kill command for the right number of characters. With a zero argument,
|
|
|
|
just kill one word and no whitespace. Bound to "M-D".
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( delfword) {
|
2021-08-03 01:37:06 -04:00
|
|
|
line_p dotp; /* original cursor line */
|
2010-02-27 05:38:22 -05:00
|
|
|
int doto; /* and row */
|
|
|
|
int c; /* temp char */
|
2021-07-19 08:50:32 -04:00
|
|
|
|
|
|
|
assert( !(curbp->b_mode & MDVIEW)) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* ignore the command if there is a negative argument */
|
|
|
|
if (n < 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Clear the kill buffer if last command wasn't a kill */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lastflag & CFKILL) == 0)
|
2005-05-31 11:50:56 -04:00
|
|
|
kdelete();
|
|
|
|
thisflag |= CFKILL; /* this command is a kill */
|
|
|
|
|
|
|
|
/* save the current cursor position */
|
|
|
|
dotp = curwp->w_dotp;
|
|
|
|
doto = curwp->w_doto;
|
|
|
|
|
|
|
|
/* figure out how many characters to give the axe */
|
2021-08-03 01:37:06 -04:00
|
|
|
long size = 0 ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* get us into a word.... */
|
|
|
|
while (inword() == FALSE) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
++size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
/* skip one word, no whitespace! */
|
|
|
|
while (inword() == TRUE) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
++size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* skip n words.... */
|
|
|
|
while (n--) {
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if we are at EOL; skip to the beginning of the next */
|
|
|
|
while (curwp->w_doto == llength(curwp->w_dotp)) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
++size;
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* move forward till we are at the end of the word */
|
|
|
|
while (inword() == TRUE) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
++size;
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if there are more words, skip the interword stuff */
|
|
|
|
if (n != 0)
|
|
|
|
while (inword() == FALSE) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
++size;
|
|
|
|
}
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* skip whitespace and newlines */
|
|
|
|
while ((curwp->w_doto == llength(curwp->w_dotp)) ||
|
2005-09-30 18:26:09 -04:00
|
|
|
((c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ')
|
|
|
|
|| (c == '\t')) {
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
|
|
|
break;
|
|
|
|
++size;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the original position and delete the words */
|
|
|
|
curwp->w_dotp = dotp;
|
|
|
|
curwp->w_doto = doto;
|
2010-08-29 06:03:55 -04:00
|
|
|
return ldelete(size, TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Kill backwards by "n" words. Move backwards by the desired number of
|
|
|
|
words, counting the characters. When dot is finally moved to its
|
|
|
|
resting place, fire off the kill command. Bound to "M-Rubout" and to
|
|
|
|
"M-Backspace".
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( delbword) {
|
2021-07-19 08:50:32 -04:00
|
|
|
assert( !(curbp->b_mode & MDVIEW)) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* ignore the command if there is a nonpositive argument */
|
|
|
|
if (n <= 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* Clear the kill buffer if last command wasn't a kill */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lastflag & CFKILL) == 0)
|
2005-05-31 11:50:56 -04:00
|
|
|
kdelete();
|
|
|
|
thisflag |= CFKILL; /* this command is a kill */
|
|
|
|
|
|
|
|
if (backchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2021-08-03 01:37:06 -04:00
|
|
|
|
|
|
|
long size = 0 ;
|
2005-05-31 11:50:56 -04:00
|
|
|
while (n--) {
|
|
|
|
while (inword() == FALSE) {
|
|
|
|
if (backchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
++size;
|
|
|
|
}
|
|
|
|
while (inword() != FALSE) {
|
|
|
|
++size;
|
|
|
|
if (backchar(FALSE, 1) == FALSE)
|
|
|
|
goto bckdel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (forwchar(FALSE, 1) == FALSE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2012-07-11 13:43:16 -04:00
|
|
|
bckdel:return ldelchar(size, TRUE);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return TRUE if the character at dot is a character that is considered to be
|
|
|
|
* part of a word. The word character list is hard coded. Should be setable.
|
|
|
|
*/
|
2019-07-25 07:13:40 -04:00
|
|
|
static int inword( void) {
|
2010-02-27 05:38:22 -05:00
|
|
|
int c;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2019-07-25 07:13:40 -04:00
|
|
|
if( curwp->w_doto == llength( curwp->w_dotp))
|
|
|
|
return FALSE ;
|
|
|
|
|
|
|
|
c = lgetc( curwp->w_dotp, curwp->w_doto) ;
|
|
|
|
return isletter( c) || ( c >= '0' && c <= '9') ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:57:10 -04:00
|
|
|
static int parafillnjustify( int f, int n, int justify_f) {
|
2021-07-16 00:24:13 -04:00
|
|
|
unicode_t c; /* current char during scan */
|
2021-07-16 22:57:10 -04:00
|
|
|
unicode_t *wbuf ; /* buffer for current word */
|
2021-07-16 00:24:13 -04:00
|
|
|
int wbufsize ;
|
2010-02-27 05:38:22 -05:00
|
|
|
int wordlen; /* length of current word */
|
|
|
|
int clength; /* position on line during fill */
|
|
|
|
int eopflag; /* Are we at the End-Of-Paragraph? */
|
2021-07-17 22:02:49 -04:00
|
|
|
int firstflag = TRUE ; /* first word? (needs no space) */
|
2021-08-03 01:37:06 -04:00
|
|
|
line_p eopline; /* pointer to line just past EOP */
|
2021-07-16 22:57:10 -04:00
|
|
|
int dotflag = 0 ; /* was the last char a period? */
|
|
|
|
int leftmarg = 0 ; /* left marginal */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2021-07-19 03:39:00 -04:00
|
|
|
assert( !(curbp->b_mode & MDVIEW)) ;
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
if( fillcol == 0) /* no fill column set */
|
|
|
|
return mloutfail( "No fill column set") ;
|
2021-07-16 22:57:10 -04:00
|
|
|
|
|
|
|
if( justify_f) {
|
|
|
|
leftmarg = getccol( FALSE) ;
|
2021-08-11 05:02:19 -04:00
|
|
|
if( leftmarg + 10 > fillcol)
|
|
|
|
return mloutfail( "Column too narrow") ;
|
2021-07-16 22:57:10 -04:00
|
|
|
|
|
|
|
justflag = justify_f ;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-07-16 00:24:13 -04:00
|
|
|
wbufsize = ALLOCSZ ;
|
|
|
|
wbuf = malloc( ALLOCSZ * sizeof *wbuf) ;
|
2021-07-16 22:57:10 -04:00
|
|
|
if( NULL == wbuf) {
|
|
|
|
justflag = FALSE ;
|
2021-07-16 00:24:13 -04:00
|
|
|
return FALSE ;
|
2021-07-16 22:57:10 -04:00
|
|
|
}
|
2021-07-16 00:24:13 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* record the pointer to the line just past the EOP */
|
|
|
|
gotoeop(FALSE, 1);
|
|
|
|
eopline = lforw(curwp->w_dotp);
|
|
|
|
|
2021-07-17 23:15:18 -04:00
|
|
|
/* and back to the beginning of the paragraph */
|
2005-05-31 11:50:56 -04:00
|
|
|
gotobop(FALSE, 1);
|
|
|
|
|
|
|
|
/* initialize various info */
|
2021-07-17 22:02:49 -04:00
|
|
|
if( justflag && leftmarg < llength(curwp->w_dotp)) {
|
2021-07-16 22:57:10 -04:00
|
|
|
setccol( leftmarg) ;
|
2021-07-17 22:02:49 -04:00
|
|
|
lgetchar( &c) ;
|
|
|
|
if( c == ' ' || c == '\t')
|
|
|
|
/* on a space */
|
|
|
|
if( getccol( TRUE) < getccol( FALSE))
|
|
|
|
/* first non space before current position */
|
|
|
|
firstflag = FALSE ;
|
|
|
|
}
|
2021-07-16 22:57:10 -04:00
|
|
|
|
2021-07-16 00:24:13 -04:00
|
|
|
clength = getccol( FALSE) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
wordlen = 0;
|
|
|
|
|
|
|
|
/* scan through lines, filling words */
|
|
|
|
eopflag = FALSE;
|
|
|
|
while (!eopflag) {
|
2012-07-11 13:43:16 -04:00
|
|
|
int bytes = 1;
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* get the next character in the paragraph */
|
|
|
|
if (curwp->w_doto == llength(curwp->w_dotp)) {
|
|
|
|
c = ' ';
|
|
|
|
if (lforw(curwp->w_dotp) == eopline)
|
|
|
|
eopflag = TRUE;
|
|
|
|
} else
|
2012-07-11 13:43:16 -04:00
|
|
|
bytes = lgetchar(&c);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and then delete it */
|
2012-07-11 13:43:16 -04:00
|
|
|
ldelete(bytes, FALSE);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if not a separator, just add it in */
|
|
|
|
if (c != ' ' && c != '\t') {
|
2021-07-16 00:24:13 -04:00
|
|
|
if (wordlen < wbufsize)
|
2005-05-31 11:50:56 -04:00
|
|
|
wbuf[wordlen++] = c;
|
2021-07-16 00:24:13 -04:00
|
|
|
else {
|
|
|
|
/* overflow */
|
|
|
|
unicode_t *newptr ;
|
|
|
|
|
|
|
|
newptr = realloc( wbuf, (wbufsize + ALLOCSZ) * sizeof *wbuf) ;
|
|
|
|
if( newptr != NULL) {
|
|
|
|
wbuf = newptr ;
|
|
|
|
wbufsize += ALLOCSZ ;
|
|
|
|
wbuf[ wordlen++] = c ;
|
|
|
|
} /* else the word is truncated silently */
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
} else if (wordlen) {
|
2021-07-16 22:57:10 -04:00
|
|
|
/* at a word break with a word waiting */
|
|
|
|
if( firstflag)
|
|
|
|
firstflag = FALSE ;
|
|
|
|
else {
|
2021-07-04 23:02:34 -04:00
|
|
|
/* calculate tentative new length with word added */
|
2021-07-16 22:57:10 -04:00
|
|
|
if( fillcol > clength + dotflag + 1 + wordlen) {
|
2005-05-31 11:50:56 -04:00
|
|
|
/* add word to current line */
|
2021-07-16 22:57:10 -04:00
|
|
|
linsert( dotflag + 1, ' ') ; /* the space */
|
|
|
|
clength += dotflag + 1 ;
|
|
|
|
} else {
|
|
|
|
/* start a new line */
|
|
|
|
lnewline();
|
|
|
|
linsert( leftmarg, ' ') ;
|
|
|
|
clength = leftmarg;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and add the word in in either case */
|
2021-07-16 22:57:10 -04:00
|
|
|
for( int i = 0 ; i < wordlen ; i++) {
|
2021-07-16 00:24:13 -04:00
|
|
|
c = wbuf[ i] ;
|
2021-07-16 22:57:10 -04:00
|
|
|
linsert( 1, c) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
++clength;
|
|
|
|
}
|
2021-07-16 00:24:13 -04:00
|
|
|
|
2021-07-16 22:57:10 -04:00
|
|
|
dotflag = c == '.' ; /* was the last char a period? */
|
2005-05-31 11:50:56 -04:00
|
|
|
wordlen = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 23:15:18 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* and add a last newline for the end of our new paragraph */
|
2021-07-17 23:15:18 -04:00
|
|
|
if( eopline == curbp->b_linep) /* at EOF? */
|
|
|
|
forwchar( FALSE, 1) ;
|
|
|
|
else
|
|
|
|
lnewline() ;
|
2021-07-16 22:57:10 -04:00
|
|
|
|
|
|
|
if( justflag) {
|
|
|
|
forwword(FALSE, 1);
|
|
|
|
setccol( leftmarg) ;
|
|
|
|
justflag = FALSE;
|
|
|
|
}
|
|
|
|
|
2021-07-16 00:24:13 -04:00
|
|
|
free( wbuf) ;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Fill the current paragraph according to the current
|
2021-07-16 22:57:10 -04:00
|
|
|
* fill column
|
|
|
|
*
|
|
|
|
* f and n - deFault flag and Numeric argument
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( fillpara) {
|
2021-07-16 22:57:10 -04:00
|
|
|
return parafillnjustify( f, n, FALSE) ;
|
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
2005-09-30 19:34:11 -04:00
|
|
|
/* Fill the current paragraph according to the current
|
|
|
|
* fill column and cursor position
|
|
|
|
*
|
|
|
|
* int f, n; deFault flag and Numeric argument
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( justpara) {
|
2021-07-16 22:57:10 -04:00
|
|
|
return parafillnjustify( f, n, TRUE) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* delete n paragraphs starting with the current one
|
2005-09-30 19:34:11 -04:00
|
|
|
*
|
|
|
|
* int f default flag
|
|
|
|
* int n # of paras to delete
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( killpara) {
|
2005-05-31 11:50:56 -04:00
|
|
|
while (n--) { /* for each paragraph to delete */
|
|
|
|
|
|
|
|
/* mark out the end and beginning of the para to delete */
|
|
|
|
gotoeop(FALSE, 1);
|
|
|
|
|
|
|
|
/* set the mark here */
|
|
|
|
curwp->w_markp = curwp->w_dotp;
|
|
|
|
curwp->w_marko = curwp->w_doto;
|
|
|
|
|
|
|
|
/* go to the beginning of the paragraph */
|
|
|
|
gotobop(FALSE, 1);
|
|
|
|
curwp->w_doto = 0; /* force us to the beginning of line */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* and delete it */
|
2021-07-19 03:39:00 -04:00
|
|
|
int status = killregion( FALSE, 1) ;
|
|
|
|
if( status != TRUE)
|
|
|
|
return status ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and clean up the 2 extra lines */
|
|
|
|
ldelete(2L, TRUE);
|
|
|
|
}
|
2021-07-19 03:39:00 -04:00
|
|
|
|
|
|
|
return TRUE ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
/* wordcount: count the # of words in the marked region,
|
2005-09-30 19:34:11 -04:00
|
|
|
* along with average word sizes, # of chars, etc,
|
|
|
|
* and report on them.
|
|
|
|
*
|
|
|
|
* int f, n; ignored numeric arguments
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( wordcount) {
|
2021-08-03 01:37:06 -04:00
|
|
|
line_p lp; /* current line to scan */
|
2010-02-27 05:38:22 -05:00
|
|
|
int offset; /* current char to scan */
|
2005-05-31 11:50:56 -04:00
|
|
|
long size; /* size of region left to count */
|
2010-02-27 05:38:22 -05:00
|
|
|
int ch; /* current character to scan */
|
|
|
|
int wordflag; /* are we in a word now? */
|
|
|
|
int lastword; /* were we just in a word? */
|
2005-05-31 11:50:56 -04:00
|
|
|
long nwords; /* total # of words */
|
|
|
|
long nchars; /* total number of chars */
|
|
|
|
int nlines; /* total number of lines in region */
|
|
|
|
int avgch; /* average number of chars/word */
|
|
|
|
int status; /* status return code */
|
2021-08-03 01:37:06 -04:00
|
|
|
region_t region ; /* region to look at */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* make sure we have a region to count */
|
2021-08-03 01:37:06 -04:00
|
|
|
if( (status = getregion( ®ion)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
lp = region.r_linep;
|
|
|
|
offset = region.r_offset;
|
|
|
|
size = region.r_size;
|
|
|
|
|
|
|
|
/* count up things */
|
|
|
|
lastword = FALSE;
|
|
|
|
nchars = 0L;
|
|
|
|
nwords = 0L;
|
|
|
|
nlines = 0;
|
|
|
|
while (size--) {
|
|
|
|
|
|
|
|
/* get the current character */
|
|
|
|
if (offset == llength(lp)) { /* end of line */
|
|
|
|
ch = '\n';
|
|
|
|
lp = lforw(lp);
|
|
|
|
offset = 0;
|
|
|
|
++nlines;
|
|
|
|
} else {
|
|
|
|
ch = lgetc(lp, offset);
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and tabulate it */
|
2019-07-25 07:13:40 -04:00
|
|
|
wordflag = isletter( ch) || (ch >= '0' && ch <= '9') ;
|
2005-05-31 11:50:56 -04:00
|
|
|
if (wordflag == TRUE && lastword == FALSE)
|
|
|
|
++nwords;
|
|
|
|
lastword = wordflag;
|
|
|
|
++nchars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and report on the info */
|
|
|
|
if (nwords > 0L)
|
2005-09-30 18:26:09 -04:00
|
|
|
avgch = (int) ((100L * nchars) / nwords);
|
2005-05-31 11:50:56 -04:00
|
|
|
else
|
|
|
|
avgch = 0;
|
|
|
|
|
2015-02-12 23:31:59 -05:00
|
|
|
mloutfmt( "Words %D Chars %D Lines %d Avg chars/word %f",
|
2015-01-21 08:30:01 -05:00
|
|
|
nwords, nchars, nlines + 1, avgch) ;
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2013-09-25 03:09:09 -04:00
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* go back to the beginning of the current paragraph
|
2013-09-25 03:09:09 -04:00
|
|
|
* here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
|
|
|
|
* combination to delimit the beginning of a paragraph
|
|
|
|
*
|
|
|
|
* int f, n; default Flag & Numeric argument
|
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( gotobop) {
|
2013-09-25 03:09:09 -04:00
|
|
|
if (n < 0) /* the other way... */
|
|
|
|
return gotoeop(f, -n);
|
|
|
|
|
|
|
|
while (n-- > 0) { /* for each one asked for */
|
|
|
|
|
|
|
|
/* first scan back until we are in a word */
|
2021-07-16 00:24:13 -04:00
|
|
|
while( backchar( FALSE, 1) && !inword()) ;
|
|
|
|
|
2013-09-25 03:09:09 -04:00
|
|
|
curwp->w_doto = 0; /* and go to the B-O-Line */
|
|
|
|
|
|
|
|
/* and scan back until we hit a <NL><NL> or <NL><TAB>
|
|
|
|
or a <NL><SPACE> */
|
|
|
|
while (lback(curwp->w_dotp) != curbp->b_linep)
|
|
|
|
if (llength(curwp->w_dotp) != 0 &&
|
|
|
|
((justflag == TRUE) ||
|
|
|
|
(lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
|
|
|
|
lgetc(curwp->w_dotp, curwp->w_doto) != ' '))
|
|
|
|
)
|
|
|
|
curwp->w_dotp = lback(curwp->w_dotp);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* and then forward until we are in a word */
|
2021-07-16 00:24:13 -04:00
|
|
|
while( !inword() && forwchar( FALSE, 1)) ;
|
2013-09-25 03:09:09 -04:00
|
|
|
}
|
|
|
|
curwp->w_flag |= WFMOVE; /* force screen update */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-08-11 05:02:19 -04:00
|
|
|
|
|
|
|
/* Go forward to the end of the current paragraph here we look for a
|
|
|
|
<NL><NL> or <NL><TAB> or <NL><SPACE> combination to delimit the
|
|
|
|
beginning of a paragraph
|
|
|
|
|
|
|
|
int f, n; default Flag & Numeric argument
|
2013-09-25 03:09:09 -04:00
|
|
|
*/
|
2021-08-11 05:02:19 -04:00
|
|
|
BINDABLE( gotoeop) {
|
2013-09-25 03:09:09 -04:00
|
|
|
if (n < 0) /* the other way... */
|
|
|
|
return gotobop(f, -n);
|
|
|
|
|
|
|
|
while (n-- > 0) { /* for each one asked for */
|
|
|
|
/* first scan forward until we are in a word */
|
2021-07-16 00:24:13 -04:00
|
|
|
while( !inword() && forwchar( FALSE, 1)) ;
|
|
|
|
curwp->w_doto = 0 ; /* and go to the B-O-Line */
|
|
|
|
if( curwp->w_dotp != curbp->b_linep) /* of next line if not at EOF */
|
|
|
|
curwp->w_dotp = lforw( curwp->w_dotp) ;
|
2013-09-25 03:09:09 -04:00
|
|
|
|
2021-07-16 00:24:13 -04:00
|
|
|
/* and scan forward until we hit a <NL><NL> or <NL><TAB>
|
2013-09-25 03:09:09 -04:00
|
|
|
or a <NL><SPACE> */
|
|
|
|
while (curwp->w_dotp != curbp->b_linep) {
|
|
|
|
if (llength(curwp->w_dotp) != 0 &&
|
|
|
|
((justflag == TRUE) ||
|
|
|
|
(lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
|
|
|
|
lgetc(curwp->w_dotp, curwp->w_doto) != ' '))
|
|
|
|
)
|
|
|
|
curwp->w_dotp = lforw(curwp->w_dotp);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and then backward until we are in a word */
|
2021-07-16 00:24:13 -04:00
|
|
|
while( backchar( FALSE, 1) && !inword()) ;
|
|
|
|
|
2013-09-25 03:09:09 -04:00
|
|
|
curwp->w_doto = llength(curwp->w_dotp); /* and to the EOL */
|
|
|
|
}
|
|
|
|
curwp->w_flag |= WFMOVE; /* force screen update */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2021-07-16 22:57:10 -04:00
|
|
|
|
|
|
|
/* end of word.c */
|