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
|
|
|
* *
|
2018-01-24 09:13:28 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2018 Free Software Foundation, Inc. *
|
2017-11-12 09:42:29 +00:00
|
|
|
* Copyright (C) 2014-2017 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
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.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
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current = openfile->fileage;
|
|
|
|
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;
|
|
|
|
openfile->current_x = strlen(openfile->filebot->data);
|
|
|
|
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. */
|
2017-08-24 20:03:07 +00:00
|
|
|
size_t proper_x(filestruct *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' &&
|
|
|
|
((forward && strnlenpt(line->data, index) < *leftedge) ||
|
|
|
|
(!forward && column / tabsize == (*leftedge - 1) / tabsize &&
|
|
|
|
column / tabsize < (*leftedge + editwincols - 1) / tabsize))) {
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (shifted != NULL)
|
|
|
|
*shifted = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ISSET(SOFTWRAP))
|
|
|
|
*leftedge = leftedge_for(strnlenpt(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
|
|
|
}
|
|
|
|
|
2017-03-28 09:24:32 +00:00
|
|
|
/* Move up nearly 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;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (!ISSET(SMOOTH_SCROLL)) {
|
|
|
|
openfile->current = openfile->edittop;
|
|
|
|
leftedge = openfile->firstcolumn;
|
|
|
|
openfile->current_y = 0;
|
|
|
|
target_column = 0;
|
|
|
|
} else
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-28 09:24:32 +00:00
|
|
|
/* Move down nearly 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;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (!ISSET(SMOOTH_SCROLL)) {
|
|
|
|
openfile->current = openfile->edittop;
|
|
|
|
leftedge = openfile->firstcolumn;
|
|
|
|
openfile->current_y = 0;
|
|
|
|
target_column = 0;
|
|
|
|
} else
|
|
|
|
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
|
2017-04-17 10:01:03 +00:00
|
|
|
/* Move to the beginning of the last beginning-of-paragraph line before the
|
|
|
|
* current line. If update_screen is TRUE, update the screen afterwards. */
|
|
|
|
void do_para_begin(bool update_screen)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->current != openfile->fileage)
|
|
|
|
openfile->current = openfile->current->prev;
|
2017-08-30 19:57:35 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (!begpar(openfile->current))
|
|
|
|
openfile->current = openfile->current->prev;
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = 0;
|
2005-11-09 04:20:55 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (update_screen)
|
|
|
|
edit_redraw(was_current, CENTERING);
|
2005-07-20 19:24:11 +00:00
|
|
|
}
|
|
|
|
|
2005-11-10 05:26:37 +00:00
|
|
|
/* Move down to the beginning of the last line of the current paragraph.
|
|
|
|
* Then move down one line farther if there is such a line, or to the
|
2017-04-17 10:01:03 +00:00
|
|
|
* end of the current line if not. If update_screen is TRUE, update the
|
2005-12-08 07:09:08 +00:00
|
|
|
* screen afterwards. A line is the last line of a paragraph if it is
|
|
|
|
* in a paragraph, and the next line either is the beginning line of a
|
|
|
|
* paragraph or isn't in a paragraph. */
|
2017-04-17 10:01:03 +00:00
|
|
|
void do_para_end(bool update_screen)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (openfile->current != openfile->filebot &&
|
|
|
|
!inpar(openfile->current))
|
|
|
|
openfile->current = openfile->current->next;
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (openfile->current != openfile->filebot &&
|
|
|
|
inpar(openfile->current->next) &&
|
|
|
|
!begpar(openfile->current->next)) {
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
}
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->current != openfile->filebot) {
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
} else
|
|
|
|
openfile->current_x = strlen(openfile->current->data);
|
2005-07-20 19:24:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (update_screen)
|
|
|
|
edit_redraw(was_current, CENTERING);
|
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. */
|
|
|
|
void do_para_begin_void(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
do_para_begin(TRUE);
|
2017-08-18 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 19:40:33 +00:00
|
|
|
/* Move down to just after the first end of a paragraph. */
|
2005-07-20 19:24:11 +00:00
|
|
|
void do_para_end_void(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
do_para_end(TRUE);
|
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. */
|
2016-06-25 13:16:52 +00:00
|
|
|
void do_prev_block(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step forward one line again if this one is blank. */
|
|
|
|
if (openfile->current->next != NULL &&
|
|
|
|
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. */
|
2016-06-25 13:16:52 +00:00
|
|
|
void do_next_block(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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
|
|
|
|
* as part of a word. When requested, update the screen afterwards. */
|
2017-04-17 10:01:03 +00:00
|
|
|
void do_prev_word(bool allow_punct, bool update_screen)
|
2005-07-20 21:31:19 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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. */
|
|
|
|
openfile->current_x = move_mbleft(openfile->current->data,
|
|
|
|
openfile->current_x);
|
|
|
|
|
|
|
|
if (is_word_mbchar(openfile->current->data + openfile->current_x,
|
|
|
|
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. */
|
|
|
|
openfile->current_x = move_mbright(openfile->current->data,
|
|
|
|
openfile->current_x);
|
2005-07-20 21:31:19 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (update_screen)
|
|
|
|
edit_redraw(was_current, FLOWING);
|
2005-07-20 21:31:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:57:35 +00:00
|
|
|
/* Move to the next word. If allow_punct is TRUE, treat punctuation
|
|
|
|
* as part of a word. When requested, update the screen afterwards.
|
|
|
|
* Return TRUE if we started on a word, and FALSE otherwise. */
|
2017-04-17 10:01:03 +00:00
|
|
|
bool do_next_word(bool allow_punct, bool update_screen)
|
2016-02-22 12:49:08 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
bool started_on_word = is_word_mbchar(openfile->current->data +
|
|
|
|
openfile->current_x, allow_punct);
|
|
|
|
bool seen_space = !started_on_word;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
openfile->current_x = move_mbright(openfile->current->data,
|
|
|
|
openfile->current_x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (!is_word_mbchar(openfile->current->data + openfile->current_x,
|
|
|
|
allow_punct))
|
|
|
|
seen_space = TRUE;
|
|
|
|
else if (seen_space)
|
|
|
|
break;
|
2016-02-22 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (update_screen)
|
|
|
|
edit_redraw(was_current, FLOWING);
|
2016-02-22 12:49:08 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Return whether we started on a word. */
|
|
|
|
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. */
|
|
|
|
void do_prev_word_void(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
do_prev_word(ISSET(WORD_BOUNDS), TRUE);
|
2017-08-18 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 12:49:08 +00:00
|
|
|
/* Move to the next word in the file, treating punctuation as part of a word
|
|
|
|
* if the WORD_BOUNDS flag is set, and update the screen afterwards. */
|
|
|
|
void do_next_word_void(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
do_next_word(ISSET(WORD_BOUNDS), TRUE);
|
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
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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).
|
2017-08-17 21:50:04 +00:00
|
|
|
* When softwrapping and alredy at the end of a chunk, go to the
|
|
|
|
* end of the full line. */
|
|
|
|
void do_end(void)
|
2005-07-20 19:24:11 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
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
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
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
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
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-03-28 14:37:47 +00:00
|
|
|
#ifdef ENABLE_HELP
|
2017-01-20 17:11:17 +00:00
|
|
|
/* Scroll up one line or chunk without scrolling the cursor. */
|
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
|
|
|
|
|
|
|
edit_scroll(BACKWARD);
|
2016-07-25 15:23:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 17:11:17 +00:00
|
|
|
/* Scroll down one line or chunk without scrolling the cursor. */
|
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
|
|
|
|
2018-03-28 14:37:47 +00:00
|
|
|
if (openfile->edittop->next != NULL
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
|| chunk_for(openfile->firstcolumn, openfile->edittop) <
|
|
|
|
number_of_chunks_in(openfile->edittop)
|
|
|
|
#endif
|
|
|
|
)
|
2018-03-18 14:58:46 +00:00
|
|
|
edit_scroll(FORWARD);
|
2005-10-24 02:12:09 +00:00
|
|
|
}
|
2006-07-06 22:17:47 +00:00
|
|
|
#endif
|
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
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *was_current = openfile->current;
|
|
|
|
|
|
|
|
if (openfile->current_x > 0)
|
|
|
|
openfile->current_x = move_mbleft(openfile->current->data,
|
|
|
|
openfile->current_x);
|
|
|
|
else if (openfile->current != openfile->fileage) {
|
|
|
|
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
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
filestruct *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')
|
|
|
|
openfile->current_x = move_mbright(openfile->current->data,
|
|
|
|
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
|
|
|
}
|