2013-05-18 20:13:48 -04:00
|
|
|
/* basic.c -- implements basic.h */
|
|
|
|
|
|
|
|
#include "basic.h"
|
|
|
|
|
2013-10-11 02:09:29 -04:00
|
|
|
#define CVMVAS 1 /* arguments to page forward/back in pages */
|
|
|
|
|
2010-08-20 22:53:23 -04:00
|
|
|
/* basic.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
|
|
|
* The routines in this file move the cursor around on the screen. They
|
|
|
|
* compute a new value for the cursor, then adjust ".". The display code
|
|
|
|
* always updates the cursor location, so only moves between lines, or
|
|
|
|
* functions that adjust the top line in the window and invalidate the
|
|
|
|
* framing, are hard.
|
|
|
|
*
|
|
|
|
* modified by Petri Kutvonen
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:53:23 -04:00
|
|
|
#include <stdio.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <stdlib.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "buffer.h"
|
2013-05-28 02:57:03 -04:00
|
|
|
#include "display.h"
|
2010-08-20 22:53:23 -04:00
|
|
|
#include "estruct.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2013-05-28 00:37:04 -04:00
|
|
|
#include "random.h"
|
2013-09-25 09:45:05 -04:00
|
|
|
#include "terminal.h"
|
2012-07-10 19:40:36 -04:00
|
|
|
#include "utf8.h"
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-10-09 02:38:55 -04:00
|
|
|
|
2013-10-09 23:49:04 -04:00
|
|
|
int overlap = 0 ; /* line overlap in forw/back page */
|
|
|
|
int curgoal ; /* Goal for C-P, C-N */
|
2013-10-09 02:38:55 -04:00
|
|
|
|
|
|
|
|
2010-08-26 12:20:25 -04:00
|
|
|
/*
|
|
|
|
* This routine, given a pointer to a struct line, and the current cursor goal
|
|
|
|
* column, return the best choice for the offset. The offset is returned.
|
|
|
|
* Used by "C-N" and "C-P".
|
|
|
|
*/
|
|
|
|
static int getgoal(struct line *dlp)
|
|
|
|
{
|
|
|
|
int col;
|
|
|
|
int newcol;
|
|
|
|
int dbo;
|
2012-07-15 17:36:38 -04:00
|
|
|
int len = llength(dlp);
|
2010-08-26 12:20:25 -04:00
|
|
|
|
|
|
|
col = 0;
|
|
|
|
dbo = 0;
|
2012-07-15 17:36:38 -04:00
|
|
|
while (dbo != len) {
|
|
|
|
unicode_t c;
|
|
|
|
int width = utf8_to_unicode(dlp->l_text, dbo, len, &c);
|
2010-08-26 12:20:25 -04:00
|
|
|
newcol = col;
|
2012-07-15 17:36:38 -04:00
|
|
|
|
|
|
|
/* Take tabs, ^X and \xx hex characters into account */
|
2010-08-26 12:20:25 -04:00
|
|
|
if (c == '\t')
|
|
|
|
newcol |= tabmask;
|
|
|
|
else if (c < 0x20 || c == 0x7F)
|
|
|
|
++newcol;
|
2012-07-15 17:36:38 -04:00
|
|
|
else if (c >= 0x80 && c <= 0xa0)
|
|
|
|
newcol += 2;
|
|
|
|
|
2010-08-26 12:20:25 -04:00
|
|
|
++newcol;
|
|
|
|
if (newcol > curgoal)
|
|
|
|
break;
|
|
|
|
col = newcol;
|
2012-07-15 17:36:38 -04:00
|
|
|
dbo += width;
|
2010-08-26 12:20:25 -04:00
|
|
|
}
|
|
|
|
return dbo;
|
|
|
|
}
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/*
|
2010-08-20 22:53:23 -04:00
|
|
|
* Move the cursor to the beginning of the current line.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int gotobol(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_doto = 0;
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the cursor to the end of the current line. Trivial. No errors.
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int gotoeol(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_doto = llength(curwp->w_dotp);
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 19:34:11 -04:00
|
|
|
/*
|
2010-08-20 22:53:23 -04:00
|
|
|
* Move to a particular line.
|
2005-09-30 19:34:11 -04:00
|
|
|
*
|
2010-08-20 22:53:23 -04:00
|
|
|
* @n: The specified line position at the current buffer.
|
2005-09-30 19:34:11 -04:00
|
|
|
*/
|
|
|
|
int gotoline(int f, int n)
|
|
|
|
{
|
2010-08-20 22:53:23 -04:00
|
|
|
int status;
|
|
|
|
char arg[NSTRING]; /* Buffer to hold argument. */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-08-20 22:53:23 -04:00
|
|
|
/* Get an argument if one doesnt exist. */
|
2005-05-31 11:50:56 -04:00
|
|
|
if (f == FALSE) {
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((status =
|
|
|
|
mlreply("Line to GOTO: ", arg, NSTRING)) != TRUE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
mlwrite("(Aborted)");
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
n = atoi(arg);
|
|
|
|
}
|
2010-08-20 22:53:23 -04:00
|
|
|
/* Handle the case where the user may be passed something like this:
|
|
|
|
* em filename +
|
|
|
|
* In this case we just go to the end of the buffer.
|
|
|
|
*/
|
|
|
|
if (n == 0)
|
|
|
|
return gotoeob(f, n);
|
|
|
|
|
|
|
|
/* If a bogus argument was passed, then returns false. */
|
|
|
|
if (n < 0)
|
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-08-20 22:53:23 -04:00
|
|
|
/* First, we go to the begin of the buffer. */
|
|
|
|
gotobob(f, n);
|
|
|
|
return forwline(f, n - 1);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Goto the beginning of the buffer. Massive adjustment of dot. This is
|
|
|
|
* considered to be hard motion; it really isn't if the original value of dot
|
|
|
|
* is the same as the new value of dot. Normally bound to "M-<".
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int gotobob(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_dotp = lforw(curbp->b_linep);
|
|
|
|
curwp->w_doto = 0;
|
|
|
|
curwp->w_flag |= WFHARD;
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move to the end of the buffer. Dot is always put at the end of the file
|
|
|
|
* (ZJ). The standard screen code does most of the hard parts of update.
|
|
|
|
* Bound to "M->".
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int gotoeob(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_dotp = curbp->b_linep;
|
|
|
|
curwp->w_doto = 0;
|
|
|
|
curwp->w_flag |= WFHARD;
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move forward by full lines. If the number of lines to move is less than
|
|
|
|
* zero, call the backward line function to actually do it. The last command
|
|
|
|
* controls how the goal column is set. Bound to "C-N". No errors are
|
|
|
|
* possible.
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int forwline(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *dlp;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (n < 0)
|
2010-08-20 22:53:23 -04:00
|
|
|
return backline(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if we are on the last line as we start....fail the command */
|
|
|
|
if (curwp->w_dotp == curbp->b_linep)
|
2010-08-20 22:53:23 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if the last command was not note a line move,
|
|
|
|
reset the goal column */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lastflag & CFCPCN) == 0)
|
|
|
|
curgoal = getccol(FALSE);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* flag this command as a line move */
|
2005-09-30 18:26:09 -04:00
|
|
|
thisflag |= CFCPCN;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and move the point down */
|
2005-09-30 18:26:09 -04:00
|
|
|
dlp = curwp->w_dotp;
|
|
|
|
while (n-- && dlp != curbp->b_linep)
|
|
|
|
dlp = lforw(dlp);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* reseting the current position */
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_dotp = dlp;
|
|
|
|
curwp->w_doto = getgoal(dlp);
|
|
|
|
curwp->w_flag |= WFMOVE;
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is like "forwline", but goes backwards. The scheme is exactly
|
|
|
|
* the same. Check for arguments that are less than zero and call your
|
|
|
|
* alternate. Figure out the new line and call "movedot" to perform the
|
|
|
|
* motion. No errors are possible. Bound to "C-P".
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int backline(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *dlp;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (n < 0)
|
2010-08-20 22:53:23 -04:00
|
|
|
return forwline(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if we are on the last line as we start....fail the command */
|
|
|
|
if (lback(curwp->w_dotp) == curbp->b_linep)
|
2010-08-20 22:53:23 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if the last command was not note a line move,
|
|
|
|
reset the goal column */
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((lastflag & CFCPCN) == 0)
|
|
|
|
curgoal = getccol(FALSE);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* flag this command as a line move */
|
2005-09-30 18:26:09 -04:00
|
|
|
thisflag |= CFCPCN;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and move the point up */
|
2005-09-30 18:26:09 -04:00
|
|
|
dlp = curwp->w_dotp;
|
|
|
|
while (n-- && lback(dlp) != curbp->b_linep)
|
|
|
|
dlp = lback(dlp);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* reseting the current position */
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_dotp = dlp;
|
|
|
|
curwp->w_doto = getgoal(dlp);
|
|
|
|
curwp->w_flag |= WFMOVE;
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scroll forward by a specified number of lines, or by a full page if no
|
|
|
|
* argument. Bound to "C-V". The "2" in the arithmetic on the window size is
|
|
|
|
* the overlap; this value is the default overlap value in ITS EMACS. Because
|
|
|
|
* this zaps the top line in the display window, we have to do a hard update.
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int forwpage(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (f == FALSE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#if SCROLLCODE
|
|
|
|
if (term.t_scroll != NULL)
|
|
|
|
if (overlap == 0)
|
|
|
|
n = curwp->w_ntrows / 3 * 2;
|
|
|
|
else
|
|
|
|
n = curwp->w_ntrows - overlap;
|
|
|
|
else
|
|
|
|
#endif
|
2010-08-20 22:53:23 -04:00
|
|
|
n = curwp->w_ntrows - 2; /* Default scroll. */
|
|
|
|
if (n <= 0) /* Forget the overlap. */
|
|
|
|
n = 1; /* If tiny window. */
|
2005-09-30 18:26:09 -04:00
|
|
|
} else if (n < 0)
|
2010-08-26 12:20:25 -04:00
|
|
|
return backpage(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
#if CVMVAS
|
2010-08-20 22:53:23 -04:00
|
|
|
else /* Convert from pages. */
|
|
|
|
n *= curwp->w_ntrows; /* To lines. */
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
lp = curwp->w_linep;
|
|
|
|
while (n-- && lp != curbp->b_linep)
|
|
|
|
lp = lforw(lp);
|
|
|
|
curwp->w_linep = lp;
|
|
|
|
curwp->w_dotp = lp;
|
|
|
|
curwp->w_doto = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
#if SCROLLCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_flag |= WFHARD | WFKILLS;
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
|
|
|
curwp->w_flag |= WFHARD;
|
|
|
|
#endif
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This command is like "forwpage", but it goes backwards. The "2", like
|
|
|
|
* above, is the overlap between the two windows. The value is from the ITS
|
|
|
|
* EMACS manual. Bound to "M-V". We do a hard update for exactly the same
|
|
|
|
* reason.
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int backpage(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *lp;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
if (f == FALSE) {
|
2005-05-31 11:50:56 -04:00
|
|
|
#if SCROLLCODE
|
|
|
|
if (term.t_scroll != NULL)
|
|
|
|
if (overlap == 0)
|
|
|
|
n = curwp->w_ntrows / 3 * 2;
|
|
|
|
else
|
|
|
|
n = curwp->w_ntrows - overlap;
|
|
|
|
else
|
|
|
|
#endif
|
2010-08-20 22:53:23 -04:00
|
|
|
n = curwp->w_ntrows - 2; /* Default scroll. */
|
|
|
|
if (n <= 0) /* Don't blow up if the. */
|
|
|
|
n = 1; /* Window is tiny. */
|
2005-09-30 18:26:09 -04:00
|
|
|
} else if (n < 0)
|
2010-08-26 12:20:25 -04:00
|
|
|
return forwpage(f, -n);
|
2005-05-31 11:50:56 -04:00
|
|
|
#if CVMVAS
|
2010-08-26 12:20:25 -04:00
|
|
|
else /* Convert from pages. */
|
|
|
|
n *= curwp->w_ntrows; /* To lines. */
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
lp = curwp->w_linep;
|
|
|
|
while (n-- && lback(lp) != curbp->b_linep)
|
|
|
|
lp = lback(lp);
|
|
|
|
curwp->w_linep = lp;
|
|
|
|
curwp->w_dotp = lp;
|
|
|
|
curwp->w_doto = 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
#if SCROLLCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_flag |= WFHARD | WFINS;
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_flag |= WFHARD;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the mark in the current window to the value of "." in the window. No
|
|
|
|
* errors are possible. Bound to "M-.".
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int setmark(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
curwp->w_markp = curwp->w_dotp;
|
|
|
|
curwp->w_marko = curwp->w_doto;
|
|
|
|
mlwrite("(Mark set)");
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swap the values of "." and "mark" in the current window. This is pretty
|
|
|
|
* easy, bacause all of the hard work gets done by the standard routine
|
|
|
|
* that moves the mark about. The only possible error is "no mark". Bound to
|
|
|
|
* "C-X C-X".
|
|
|
|
*/
|
2005-09-30 19:34:11 -04:00
|
|
|
int swapmark(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-04-17 19:38:09 -04:00
|
|
|
struct line *odotp;
|
2010-02-27 05:38:22 -05:00
|
|
|
int odoto;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
if (curwp->w_markp == NULL) {
|
|
|
|
mlwrite("No mark in this window");
|
2010-08-20 22:53:23 -04:00
|
|
|
return FALSE;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
odotp = curwp->w_dotp;
|
|
|
|
odoto = curwp->w_doto;
|
|
|
|
curwp->w_dotp = curwp->w_markp;
|
|
|
|
curwp->w_doto = curwp->w_marko;
|
|
|
|
curwp->w_markp = odotp;
|
|
|
|
curwp->w_marko = odoto;
|
|
|
|
curwp->w_flag |= WFMOVE;
|
2010-08-20 22:53:23 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|