2000-06-19 04:22:15 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* move.c -- This file is part of GNU nano. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2020-01-15 10:42:38 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2020 Free Software Foundation, Inc. *
|
2018-06-01 08:17:42 +00:00
|
|
|
* Copyright (C) 2014-2018 Benno Schulenberg *
|
2016-08-29 13:14:18 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty *
|
|
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU General Public License for more details. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2020-06-20 10:09:31 +00:00
|
|
|
#include "prototypes.h"
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Move to the first line of the file. */
|
2017-12-26 13:15:49 +00:00
|
|
|
void to_first_line(void)
|
2004-05-23 21:33:23 +00:00
|
|
|
{
|
2019-03-21 16:23:49 +00:00
|
|
|
openfile->current = openfile->filetop;
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = 0;
|
|
|
|
openfile->placewewant = 0;
|
2005-07-14 18:01:08 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
2004-05-23 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Move to the last line of the file. */
|
2017-12-26 13:15:49 +00:00
|
|
|
void to_last_line(void)
|
2004-05-23 21:33:23 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current = openfile->filebot;
|
2020-03-13 18:30:04 +00:00
|
|
|
openfile->current_x = (inhelp) ? 0 : strlen(openfile->filebot->data);
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
2016-10-12 19:07:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Set the last line of the screen as the target for the cursor. */
|
|
|
|
openfile->current_y = editwinrows - 1;
|
2005-07-14 18:01:08 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
|
|
|
focusing = FALSE;
|
2004-05-23 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 08:27:49 +00:00
|
|
|
/* Determine the actual current chunk and the target column. */
|
|
|
|
void get_edge_and_target(size_t *leftedge, size_t *target_column)
|
|
|
|
{
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP)) {
|
|
|
|
size_t shim = editwincols * (1 + (tabsize / editwincols));
|
2017-08-22 13:59:20 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
*leftedge = leftedge_for(xplustabs(), openfile->current);
|
|
|
|
*target_column = (openfile->placewewant + shim - *leftedge) % editwincols;
|
|
|
|
} else
|
2017-03-29 08:27:49 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
{
|
|
|
|
*leftedge = 0;
|
|
|
|
*target_column = openfile->placewewant;
|
|
|
|
}
|
2017-03-29 08:27:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 20:03:07 +00:00
|
|
|
/* Return the index in line->data that corresponds to the given column on the
|
2017-09-30 19:48:05 +00:00
|
|
|
* chunk that starts at the given leftedge. If the target column has landed
|
|
|
|
* on a tab, prevent the cursor from falling back a row when moving forward,
|
|
|
|
* or from skipping a row when moving backward, by incrementing the index. */
|
2019-03-21 16:08:52 +00:00
|
|
|
size_t proper_x(linestruct *line, size_t *leftedge, bool forward,
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t column, bool *shifted)
|
2017-08-20 13:20:20 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t index = actual_x(line->data, column);
|
2017-08-20 13:20:20 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP) && line->data[index] == '\t' &&
|
2019-04-24 07:08:23 +00:00
|
|
|
((forward && wideness(line->data, index) < *leftedge) ||
|
2017-12-29 18:27:33 +00:00
|
|
|
(!forward && column / tabsize == (*leftedge - 1) / tabsize &&
|
|
|
|
column / tabsize < (*leftedge + editwincols - 1) / tabsize))) {
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (shifted != NULL)
|
|
|
|
*shifted = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ISSET(SOFTWRAP))
|
2019-04-24 07:08:23 +00:00
|
|
|
*leftedge = leftedge_for(wideness(line->data, index), line);
|
2017-08-20 13:20:20 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return index;
|
2017-08-20 13:20:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 20:03:07 +00:00
|
|
|
/* Adjust the values for current_x and placewewant in case we have landed in
|
|
|
|
* the middle of a tab that crosses a row boundary. */
|
|
|
|
void set_proper_index_and_pww(size_t *leftedge, size_t target, bool forward)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
bool shifted = FALSE;
|
|
|
|
size_t was_edge = *leftedge;
|
2017-08-24 20:03:07 +00:00
|
|
|
|
2017-10-20 18:00:12 +00:00
|
|
|
openfile->current_x = proper_x(openfile->current, leftedge, forward,
|
2017-12-29 18:27:33 +00:00
|
|
|
actual_last_column(*leftedge, target), &shifted);
|
|
|
|
|
|
|
|
/* If the index was incremented, try going to the target column. */
|
|
|
|
if (shifted || *leftedge < was_edge)
|
|
|
|
openfile->current_x = proper_x(openfile->current, leftedge, forward,
|
|
|
|
actual_last_column(*leftedge, target), &shifted);
|
2017-08-24 20:03:07 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->placewewant = *leftedge + target;
|
2017-08-24 20:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 15:19:29 +00:00
|
|
|
/* Move up almost one screenful. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_page_up(void)
|
2000-07-03 04:24:39 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
int mustmove = (editwinrows < 3) ? 1 : editwinrows - 2;
|
|
|
|
size_t leftedge, target_column;
|
|
|
|
|
2020-09-21 15:04:10 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're not in smooth scrolling mode, put the cursor at the
|
|
|
|
* beginning of the top line of the edit window, as Pico does. */
|
2019-01-29 19:23:13 +00:00
|
|
|
if (ISSET(JUMPY_SCROLLING)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current = openfile->edittop;
|
|
|
|
leftedge = openfile->firstcolumn;
|
|
|
|
openfile->current_y = 0;
|
|
|
|
target_column = 0;
|
|
|
|
} else
|
2020-09-21 15:04:10 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
get_edge_and_target(&leftedge, &target_column);
|
|
|
|
|
|
|
|
/* Move up the required number of lines or chunks. If we can't, we're
|
|
|
|
* at the top of the file, so put the cursor there and get out. */
|
|
|
|
if (go_back_chunks(mustmove, &openfile->current, &leftedge) > 0) {
|
|
|
|
to_first_line();
|
|
|
|
return;
|
|
|
|
}
|
2005-07-14 22:15:09 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
set_proper_index_and_pww(&leftedge, target_column, FALSE);
|
2005-07-14 22:15:09 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Move the viewport so that the cursor stays immobile, if possible. */
|
|
|
|
adjust_viewport(STATIONARY);
|
|
|
|
refresh_needed = TRUE;
|
2000-07-03 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 15:19:29 +00:00
|
|
|
/* Move down almost one screenful. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_page_down(void)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
int mustmove = (editwinrows < 3) ? 1 : editwinrows - 2;
|
|
|
|
size_t leftedge, target_column;
|
|
|
|
|
2020-09-21 15:04:10 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're not in smooth scrolling mode, put the cursor at the
|
|
|
|
* beginning of the top line of the edit window, as Pico does. */
|
2019-01-29 19:23:13 +00:00
|
|
|
if (ISSET(JUMPY_SCROLLING)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current = openfile->edittop;
|
|
|
|
leftedge = openfile->firstcolumn;
|
|
|
|
openfile->current_y = 0;
|
|
|
|
target_column = 0;
|
|
|
|
} else
|
2020-09-21 15:04:10 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
get_edge_and_target(&leftedge, &target_column);
|
|
|
|
|
|
|
|
/* Move down the required number of lines or chunks. If we can't, we're
|
|
|
|
* at the bottom of the file, so put the cursor there and get out. */
|
|
|
|
if (go_forward_chunks(mustmove, &openfile->current, &leftedge) > 0) {
|
|
|
|
to_last_line();
|
|
|
|
return;
|
|
|
|
}
|
2005-07-14 22:15:09 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
set_proper_index_and_pww(&leftedge, target_column, TRUE);
|
2005-07-14 22:15:09 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Move the viewport so that the cursor stays immobile, if possible. */
|
|
|
|
adjust_viewport(STATIONARY);
|
|
|
|
refresh_needed = TRUE;
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 16:34:07 +00:00
|
|
|
#ifdef ENABLE_JUSTIFY
|
2018-11-22 19:00:18 +00:00
|
|
|
/* Move to the first beginning of a paragraph before the current line. */
|
2019-03-21 16:08:52 +00:00
|
|
|
void do_para_begin(linestruct **line)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2018-08-27 23:56:52 +00:00
|
|
|
if ((*line)->prev != NULL)
|
|
|
|
*line = (*line)->prev;
|
2017-08-30 19:57:35 +00:00
|
|
|
|
2018-08-27 23:56:52 +00:00
|
|
|
while (!begpar(*line, 0))
|
|
|
|
*line = (*line)->prev;
|
2005-07-20 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 18:24:08 +00:00
|
|
|
/* Move down to the last line of the first found paragraph. */
|
2019-03-21 16:08:52 +00:00
|
|
|
void do_para_end(linestruct **line)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2018-11-22 19:00:18 +00:00
|
|
|
while ((*line)->next != NULL && !inpar(*line))
|
2018-08-27 23:56:52 +00:00
|
|
|
*line = (*line)->next;
|
|
|
|
|
2018-11-22 19:00:18 +00:00
|
|
|
while ((*line)->next != NULL && inpar((*line)->next) &&
|
|
|
|
!begpar((*line)->next, 0))
|
2018-08-27 23:56:52 +00:00
|
|
|
*line = (*line)->next;
|
2005-07-20 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 20:17:05 +00:00
|
|
|
/* Move up to first start of a paragraph before the current line. */
|
2020-03-11 18:47:21 +00:00
|
|
|
void to_para_begin(void)
|
2017-08-18 20:17:05 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2018-09-09 18:33:27 +00:00
|
|
|
|
2018-08-27 23:56:52 +00:00
|
|
|
do_para_begin(&openfile->current);
|
|
|
|
openfile->current_x = 0;
|
2018-09-09 18:33:27 +00:00
|
|
|
|
|
|
|
edit_redraw(was_current, CENTERING);
|
2017-08-18 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 18:24:08 +00:00
|
|
|
/* Move down to just after the first found end of a paragraph. */
|
2020-03-11 18:47:21 +00:00
|
|
|
void to_para_end(void)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2018-09-09 18:33:27 +00:00
|
|
|
|
2018-11-25 18:24:08 +00:00
|
|
|
do_para_end(&openfile->current);
|
|
|
|
|
|
|
|
/* Step beyond the last line of the paragraph, if possible;
|
|
|
|
* otherwise, move to the end of the line. */
|
|
|
|
if (openfile->current->next != NULL) {
|
|
|
|
openfile->current = openfile->current->next;
|
2018-08-27 23:56:52 +00:00
|
|
|
openfile->current_x = 0;
|
2018-11-25 18:24:08 +00:00
|
|
|
} else
|
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
2018-09-09 18:33:27 +00:00
|
|
|
|
|
|
|
edit_redraw(was_current, CENTERING);
|
2005-07-20 19:24:11 +00:00
|
|
|
}
|
2017-10-31 16:34:07 +00:00
|
|
|
#endif /* ENABLE_JUSTIFY */
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-08-30 19:57:35 +00:00
|
|
|
/* Move to the preceding block of text. */
|
2020-03-11 18:45:06 +00:00
|
|
|
void to_prev_block(void)
|
2016-06-25 13:16:52 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
bool is_text = FALSE, seen_text = FALSE;
|
|
|
|
|
|
|
|
/* Skip backward until first blank line after some nonblank line(s). */
|
|
|
|
while (openfile->current->prev != NULL && (!seen_text || is_text)) {
|
|
|
|
openfile->current = openfile->current->prev;
|
|
|
|
is_text = !white_string(openfile->current->data);
|
|
|
|
seen_text = seen_text || is_text;
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:49:24 +00:00
|
|
|
/* Step forward one line again if we passed text but this line is blank. */
|
|
|
|
if (seen_text && openfile->current->next != NULL &&
|
2017-12-29 18:27:33 +00:00
|
|
|
white_string(openfile->current->data))
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
|
|
|
|
openfile->current_x = 0;
|
|
|
|
edit_redraw(was_current, CENTERING);
|
2016-06-25 13:16:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:57:35 +00:00
|
|
|
/* Move to the next block of text. */
|
2020-03-11 18:45:06 +00:00
|
|
|
void to_next_block(void)
|
2016-06-25 13:16:52 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
bool is_white = white_string(openfile->current->data);
|
|
|
|
bool seen_white = is_white;
|
|
|
|
|
|
|
|
/* Skip forward until first nonblank line after some blank line(s). */
|
|
|
|
while (openfile->current->next != NULL && (!seen_white || is_white)) {
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
is_white = white_string(openfile->current->data);
|
|
|
|
seen_white = seen_white || is_white;
|
|
|
|
}
|
|
|
|
|
|
|
|
openfile->current_x = 0;
|
|
|
|
edit_redraw(was_current, CENTERING);
|
2016-06-25 13:16:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:57:35 +00:00
|
|
|
/* Move to the previous word. If allow_punct is TRUE, treat punctuation
|
2018-09-09 18:33:27 +00:00
|
|
|
* as part of a word. */
|
2018-09-09 19:01:09 +00:00
|
|
|
void do_prev_word(bool allow_punct)
|
2005-07-20 21:31:19 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
bool seen_a_word = FALSE, step_forward = FALSE;
|
|
|
|
|
|
|
|
/* Move backward until we pass over the start of a word. */
|
|
|
|
while (TRUE) {
|
|
|
|
/* If at the head of a line, move to the end of the preceding one. */
|
|
|
|
if (openfile->current_x == 0) {
|
|
|
|
if (openfile->current->prev == NULL)
|
|
|
|
break;
|
|
|
|
openfile->current = openfile->current->prev;
|
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step back one character. */
|
2019-06-09 17:37:56 +00:00
|
|
|
openfile->current_x = step_left(openfile->current->data,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x);
|
|
|
|
|
2020-03-12 11:38:01 +00:00
|
|
|
if (is_word_char(openfile->current->data + openfile->current_x,
|
2017-12-29 18:27:33 +00:00
|
|
|
allow_punct)) {
|
|
|
|
seen_a_word = TRUE;
|
|
|
|
/* If at the head of a line now, this surely is a word start. */
|
|
|
|
if (openfile->current_x == 0)
|
|
|
|
break;
|
|
|
|
} else if (seen_a_word) {
|
|
|
|
/* This is space now: we've overshot the start of the word. */
|
|
|
|
step_forward = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2005-07-20 21:31:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (step_forward)
|
|
|
|
/* Move one character forward again to sit on the start of the word. */
|
2019-06-09 17:37:56 +00:00
|
|
|
openfile->current_x = step_right(openfile->current->data,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x);
|
2005-07-20 21:31:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 20:10:27 +00:00
|
|
|
/* Move to the next word. If after_ends is TRUE, stop at the ends of words
|
2018-09-09 18:33:27 +00:00
|
|
|
* instead of their beginnings. If allow_punct is TRUE, treat punctuation as
|
|
|
|
* part of a word. Return TRUE if we started on a word, and FALSE otherwise. */
|
2018-09-09 19:01:09 +00:00
|
|
|
bool do_next_word(bool after_ends, bool allow_punct)
|
2016-02-22 12:49:08 +00:00
|
|
|
{
|
2020-03-12 11:38:01 +00:00
|
|
|
bool started_on_word = is_word_char(openfile->current->data +
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x, allow_punct);
|
|
|
|
bool seen_space = !started_on_word;
|
2018-05-23 10:00:43 +00:00
|
|
|
#ifndef NANO_TINY
|
2018-04-30 20:10:27 +00:00
|
|
|
bool seen_word = started_on_word;
|
2018-05-23 10:00:43 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* Move forward until we reach the start of a word. */
|
|
|
|
while (TRUE) {
|
|
|
|
/* If at the end of a line, move to the beginning of the next one. */
|
|
|
|
if (openfile->current->data[openfile->current_x] == '\0') {
|
|
|
|
/* When at end of file, stop. */
|
|
|
|
if (openfile->current->next == NULL)
|
|
|
|
break;
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
seen_space = TRUE;
|
|
|
|
} else {
|
|
|
|
/* Step forward one character. */
|
2019-06-09 17:37:56 +00:00
|
|
|
openfile->current_x = step_right(openfile->current->data,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x);
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:10:27 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (after_ends) {
|
|
|
|
/* If this is a word character, continue; else it's a separator,
|
|
|
|
* and if we've already seen a word, then it's a word end. */
|
2020-03-12 11:38:01 +00:00
|
|
|
if (is_word_char(openfile->current->data + openfile->current_x,
|
2018-04-30 20:10:27 +00:00
|
|
|
allow_punct))
|
|
|
|
seen_word = TRUE;
|
|
|
|
else if (seen_word)
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2018-05-06 18:01:53 +00:00
|
|
|
/* If this is not a word character, then it's a separator; else
|
|
|
|
* if we've already seen a separator, then it's a word start. */
|
2020-03-12 11:38:01 +00:00
|
|
|
if (!is_word_char(openfile->current->data + openfile->current_x,
|
2017-12-29 18:27:33 +00:00
|
|
|
allow_punct))
|
2018-05-06 18:01:53 +00:00
|
|
|
seen_space = TRUE;
|
|
|
|
else if (seen_space)
|
|
|
|
break;
|
2018-04-30 20:10:27 +00:00
|
|
|
}
|
2016-02-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return started_on_word;
|
2016-02-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 20:17:05 +00:00
|
|
|
/* Move to the previous word in the file, treating punctuation as part of a
|
|
|
|
* word if the WORD_BOUNDS flag is set, and update the screen afterwards. */
|
2020-03-11 18:43:03 +00:00
|
|
|
void to_prev_word(void)
|
2017-08-18 20:17:05 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2018-09-09 18:33:27 +00:00
|
|
|
|
2018-09-09 19:01:09 +00:00
|
|
|
do_prev_word(ISSET(WORD_BOUNDS));
|
2018-09-09 18:33:27 +00:00
|
|
|
|
|
|
|
edit_redraw(was_current, FLOWING);
|
2017-08-18 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 20:10:27 +00:00
|
|
|
/* Move to the next word in the file. If the AFTER_ENDS flag is set, stop
|
|
|
|
* at word ends instead of beginnings. If the WORD_BOUNDS flag is set, treat
|
|
|
|
* punctuation as part of a word. Update the screen afterwards. */
|
2020-03-11 18:43:03 +00:00
|
|
|
void to_next_word(void)
|
2016-02-22 12:49:08 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2018-09-09 18:33:27 +00:00
|
|
|
|
2018-09-09 19:01:09 +00:00
|
|
|
do_next_word(ISSET(AFTER_ENDS), ISSET(WORD_BOUNDS));
|
2018-09-09 18:33:27 +00:00
|
|
|
|
|
|
|
edit_redraw(was_current, FLOWING);
|
2016-02-22 12:49:08 +00:00
|
|
|
}
|
2005-07-20 21:31:19 +00:00
|
|
|
|
2017-01-20 06:04:44 +00:00
|
|
|
/* Move to the beginning of the current line (or softwrapped chunk).
|
2017-08-17 21:50:04 +00:00
|
|
|
* When enabled, do a smart home. When softwrapping, go the beginning
|
|
|
|
* of the full line when already at the start of a chunk. */
|
|
|
|
void do_home(void)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t was_column = xplustabs();
|
|
|
|
bool moved_off_chunk = TRUE;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
bool moved = FALSE;
|
|
|
|
size_t leftedge = 0, leftedge_x = 0;
|
|
|
|
|
|
|
|
if (ISSET(SOFTWRAP)) {
|
|
|
|
leftedge = leftedge_for(was_column, openfile->current);
|
|
|
|
leftedge_x = proper_x(openfile->current, &leftedge, FALSE, leftedge,
|
|
|
|
NULL);
|
2017-01-20 06:04:44 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (ISSET(SMART_HOME)) {
|
|
|
|
size_t indent_x = indent_length(openfile->current->data);
|
|
|
|
|
|
|
|
if (openfile->current->data[indent_x] != '\0') {
|
|
|
|
/* If we're exactly on the indent, move fully home. Otherwise,
|
|
|
|
* when not softwrapping or not after the first nonblank chunk,
|
|
|
|
* move to the first nonblank character. */
|
|
|
|
if (openfile->current_x == indent_x) {
|
|
|
|
openfile->current_x = 0;
|
|
|
|
moved = TRUE;
|
|
|
|
} else if (!ISSET(SOFTWRAP) || leftedge_x <= indent_x) {
|
|
|
|
openfile->current_x = indent_x;
|
|
|
|
moved = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 06:04:44 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (!moved && ISSET(SOFTWRAP)) {
|
|
|
|
/* If already at the left edge of the screen, move fully home.
|
|
|
|
* Otherwise, move to the left edge. */
|
|
|
|
if (openfile->current_x == leftedge_x)
|
|
|
|
openfile->current_x = 0;
|
|
|
|
else {
|
|
|
|
openfile->current_x = leftedge_x;
|
|
|
|
openfile->placewewant = leftedge;
|
|
|
|
moved_off_chunk = FALSE;
|
|
|
|
}
|
|
|
|
} else if (!moved)
|
2005-07-20 19:24:11 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = 0;
|
2016-05-27 19:22:56 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (moved_off_chunk)
|
|
|
|
openfile->placewewant = xplustabs();
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we changed chunk, we might be offscreen. Otherwise,
|
|
|
|
* update current if the mark is on or we changed "page". */
|
|
|
|
if (ISSET(SOFTWRAP) && moved_off_chunk)
|
|
|
|
edit_redraw(was_current, FLOWING);
|
|
|
|
else if (line_needs_update(was_column, openfile->placewewant))
|
|
|
|
update_line(openfile->current, openfile->current_x);
|
2005-07-20 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 08:07:15 +00:00
|
|
|
/* Move to the end of the current line (or softwrapped chunk).
|
2020-03-25 15:49:46 +00:00
|
|
|
* When softwrapping and already at the end of a chunk, go to the
|
2017-08-17 21:50:04 +00:00
|
|
|
* end of the full line. */
|
|
|
|
void do_end(void)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t was_column = xplustabs();
|
|
|
|
size_t line_len = strlen(openfile->current->data);
|
|
|
|
bool moved_off_chunk = TRUE;
|
2017-01-20 08:07:15 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP)) {
|
|
|
|
bool last_chunk = FALSE;
|
|
|
|
size_t leftedge = leftedge_for(was_column, openfile->current);
|
|
|
|
size_t rightedge = get_softwrap_breakpoint(openfile->current->data,
|
|
|
|
leftedge, &last_chunk);
|
|
|
|
size_t rightedge_x;
|
|
|
|
|
|
|
|
/* If we're on the last chunk, we're already at the end of the line.
|
|
|
|
* Otherwise, we're one column past the end of the line. Shifting
|
|
|
|
* backwards one column might put us in the middle of a multi-column
|
|
|
|
* character, but actual_x() will fix that. */
|
|
|
|
if (!last_chunk)
|
|
|
|
rightedge--;
|
|
|
|
|
|
|
|
rightedge_x = actual_x(openfile->current->data, rightedge);
|
|
|
|
|
|
|
|
/* If already at the right edge of the screen, move fully to
|
|
|
|
* the end of the line. Otherwise, move to the right edge. */
|
|
|
|
if (openfile->current_x == rightedge_x)
|
|
|
|
openfile->current_x = line_len;
|
|
|
|
else {
|
|
|
|
openfile->current_x = rightedge_x;
|
|
|
|
openfile->placewewant = rightedge;
|
|
|
|
moved_off_chunk = FALSE;
|
|
|
|
}
|
|
|
|
} else
|
2017-01-20 08:07:15 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = line_len;
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (moved_off_chunk)
|
|
|
|
openfile->placewewant = xplustabs();
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we changed chunk, we might be offscreen. Otherwise,
|
|
|
|
* update current if the mark is on or we changed "page". */
|
|
|
|
if (ISSET(SOFTWRAP) && moved_off_chunk)
|
|
|
|
edit_redraw(was_current, FLOWING);
|
|
|
|
else if (line_needs_update(was_column, openfile->placewewant))
|
|
|
|
update_line(openfile->current, openfile->current_x);
|
2005-07-20 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 17:28:44 +00:00
|
|
|
/* Move the cursor to the preceding line or chunk. */
|
|
|
|
void do_up(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t leftedge, target_column;
|
2017-01-20 17:11:17 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
get_edge_and_target(&leftedge, &target_column);
|
2016-07-26 09:47:53 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we can't move up one line or chunk, we're at top of file. */
|
2018-03-12 17:28:44 +00:00
|
|
|
if (go_back_chunks(1, &openfile->current, &leftedge) > 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return;
|
2003-09-07 23:57:24 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
set_proper_index_and_pww(&leftedge, target_column, FALSE);
|
2004-05-28 20:44:09 +00:00
|
|
|
|
2019-01-29 19:23:13 +00:00
|
|
|
if (openfile->current_y == 0 && !ISSET(JUMPY_SCROLLING))
|
2018-04-14 10:22:48 +00:00
|
|
|
edit_scroll(BACKWARD);
|
|
|
|
else
|
|
|
|
edit_redraw(was_current, FLOWING);
|
2017-08-18 23:59:38 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* <Up> should not change placewewant, so restore it. */
|
|
|
|
openfile->placewewant = leftedge + target_column;
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2018-03-12 17:28:44 +00:00
|
|
|
/* Move the cursor to next line or chunk. */
|
|
|
|
void do_down(void)
|
2002-12-22 16:30:00 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t leftedge, target_column;
|
2017-03-27 15:34:13 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
get_edge_and_target(&leftedge, &target_column);
|
2009-08-30 03:50:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we can't move down one line or chunk, we're at bottom of file. */
|
2018-03-12 17:28:44 +00:00
|
|
|
if (go_forward_chunks(1, &openfile->current, &leftedge) > 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return;
|
2017-01-20 17:11:17 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
set_proper_index_and_pww(&leftedge, target_column, TRUE);
|
2016-04-15 12:39:54 +00:00
|
|
|
|
2019-01-29 19:23:13 +00:00
|
|
|
if (openfile->current_y == editwinrows - 1 && !ISSET(JUMPY_SCROLLING))
|
2018-04-14 10:22:48 +00:00
|
|
|
edit_scroll(FORWARD);
|
|
|
|
else
|
|
|
|
edit_redraw(was_current, FLOWING);
|
2017-08-18 23:59:38 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* <Down> should not change placewewant, so restore it. */
|
|
|
|
openfile->placewewant = leftedge + target_column;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2005-10-24 02:12:09 +00:00
|
|
|
|
2018-10-24 15:30:27 +00:00
|
|
|
#if !defined(NANO_TINY) || defined(ENABLE_HELP)
|
2019-10-03 15:19:29 +00:00
|
|
|
/* Scroll up one line or chunk without moving the cursor textwise. */
|
2016-07-25 15:23:45 +00:00
|
|
|
void do_scroll_up(void)
|
|
|
|
{
|
2018-03-12 17:14:32 +00:00
|
|
|
/* When the top of the file is onscreen, we can't scroll. */
|
|
|
|
if (openfile->edittop->prev == NULL && openfile->firstcolumn == 0)
|
|
|
|
return;
|
|
|
|
|
2018-03-12 17:28:44 +00:00
|
|
|
if (openfile->current_y == editwinrows - 1)
|
|
|
|
do_up();
|
2018-03-18 14:58:46 +00:00
|
|
|
|
2018-05-13 17:24:58 +00:00
|
|
|
if (editwinrows > 1)
|
|
|
|
edit_scroll(BACKWARD);
|
2016-07-25 15:23:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 15:19:29 +00:00
|
|
|
/* Scroll down one line or chunk without moving the cursor textwise. */
|
2005-10-24 02:12:09 +00:00
|
|
|
void do_scroll_down(void)
|
|
|
|
{
|
2018-03-12 17:28:44 +00:00
|
|
|
if (openfile->current_y == 0)
|
|
|
|
do_down();
|
2018-03-18 14:58:46 +00:00
|
|
|
|
2019-06-26 10:17:29 +00:00
|
|
|
if (editwinrows > 1 && (openfile->edittop->next != NULL
|
2018-03-28 14:37:47 +00:00
|
|
|
#ifndef NANO_TINY
|
2020-05-13 10:41:17 +00:00
|
|
|
|| (ISSET(SOFTWRAP) && (extra_chunks_in(openfile->edittop) >
|
2020-05-13 10:29:44 +00:00
|
|
|
chunk_for(openfile->firstcolumn, openfile->edittop)))
|
2018-03-28 14:37:47 +00:00
|
|
|
#endif
|
2019-06-26 10:17:29 +00:00
|
|
|
))
|
2018-03-18 14:58:46 +00:00
|
|
|
edit_scroll(FORWARD);
|
2005-10-24 02:12:09 +00:00
|
|
|
}
|
2016-04-27 13:10:06 +00:00
|
|
|
|
|
|
|
/* Scroll the line with the cursor to the center of the screen. */
|
|
|
|
void do_center(void)
|
|
|
|
{
|
|
|
|
adjust_viewport(CENTERING);
|
|
|
|
draw_all_subwindows();
|
|
|
|
full_refresh();
|
|
|
|
}
|
|
|
|
#endif /* !NANO_TINY || ENABLE_HELP */
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Move left one character. */
|
2005-09-13 04:53:44 +00:00
|
|
|
void do_left(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (openfile->current_x > 0)
|
2019-06-09 17:37:56 +00:00
|
|
|
openfile->current_x = step_left(openfile->current->data,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x);
|
2019-03-21 16:23:49 +00:00
|
|
|
else if (openfile->current != openfile->filetop) {
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current = openfile->current->prev;
|
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
|
|
|
}
|
2005-07-14 18:01:08 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
edit_redraw(was_current, FLOWING);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Move right one character. */
|
2005-09-13 04:53:44 +00:00
|
|
|
void do_right(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-08-22 16:51:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->current->data[openfile->current_x] != '\0')
|
2019-06-09 17:37:56 +00:00
|
|
|
openfile->current_x = step_right(openfile->current->data,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x);
|
|
|
|
else if (openfile->current != openfile->filebot) {
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
}
|
2017-01-20 16:34:45 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
edit_redraw(was_current, FLOWING);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|