move.c 7.8 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   move.c                                                               *
 *                                                                        *
5
 *   Copyright (C) 1999-2004 Chris Allegretta                             *
Chris Allegretta's avatar
Chris Allegretta committed
6
7
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
8
 *   the Free Software Foundation; either version 2, or (at your option)  *
Chris Allegretta's avatar
Chris Allegretta committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 *   any later version.                                                   *
 *                                                                        *
 *   This program 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.                         *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

22
23
#include "config.h"

Chris Allegretta's avatar
Chris Allegretta committed
24
25
#include <stdlib.h>
#include <string.h>
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
26
#include <ctype.h>
Chris Allegretta's avatar
Chris Allegretta committed
27
#include <assert.h>
Chris Allegretta's avatar
Chris Allegretta committed
28
29
30
#include "proto.h"
#include "nano.h"

31
void do_first_line(void)
32
{
33
    size_t old_pww = placewewant;
34
35
36
    current = fileage;
    placewewant = 0;
    current_x = 0;
37
38
    if (edittop != fileage || need_vertical_update(old_pww))
	edit_update(current, TOP);
39
40
}

41
void do_last_line(void)
42
{
43
    size_t old_pww = placewewant;
44
45
46
    current = filebot;
    placewewant = 0;
    current_x = 0;
47
48
49
    if (edittop->lineno + (editwinrows / 2) != filebot->lineno ||
	need_vertical_update(old_pww))
	edit_update(current, CENTER);
50
51
}

52
void do_home(void)
Chris Allegretta's avatar
Chris Allegretta committed
53
{
54
    size_t old_pww = placewewant;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef NANO_SMALL
    if (ISSET(SMART_HOME)) {
	int old_current_x = current_x;

	for (current_x = 0; isblank(current->data[current_x]) &&
		current->data[current_x] != '\0'; current_x++)
	    ;

	if (current_x == old_current_x || current->data[current_x] == '\0')
	    current_x = 0;

	placewewant = xplustabs();
    } else {
#endif
	current_x = 0;
	placewewant = 0;
#ifndef NANO_SMALL
    }
#endif
74
    check_statblank();
75
76
    if (need_horizontal_update(old_pww))
	update_line(current, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
77
78
}

79
void do_end(void)
Chris Allegretta's avatar
Chris Allegretta committed
80
{
81
    size_t old_pww = placewewant;
Chris Allegretta's avatar
Chris Allegretta committed
82
83
    current_x = strlen(current->data);
    placewewant = xplustabs();
84
    check_statblank();
85
86
    if (need_horizontal_update(old_pww))
	update_line(current, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
87
88
}

89
void do_page_up(void)
90
{
91
    size_t old_pww = placewewant;
92
    const filestruct *old_current = current;
93
#ifndef DISABLE_WRAPPING
94
    wrap_reset();
95
#endif
96

97
    /* If the first line of the file is onscreen, move current up there
98
99
100
     * and put the cursor at the beginning of the line. */
    if (edittop == fileage) {
	current = fileage;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
101
	placewewant = 0;
102
    } else {
103
104
	edit_scroll(UP, editwinrows - 2);

105
#ifndef NANO_SMALL
106
	/* If we're in smooth scrolling mode and there's at least one
107
108
	 * page of text left, move the current line of the edit window
	 * up a page. */
109
110
	if (ISSET(SMOOTHSCROLL) && current->lineno > editwinrows - 2) {
	    int i;
111
112
	    for (i = 0; i < editwinrows - 2; i++)
		current = current->prev;
113
114
	}
	/* If we're not in smooth scrolling mode or there isn't at least
115
116
117
118
119
	 * one page of text left, put the cursor at the beginning of the
	 * top line of the edit window, as Pico does. */
	else {
#endif
	    current = edittop;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
120
	    placewewant = 0;
121
122
123
124
#ifndef NANO_SMALL
	}
#endif
    }
125

126
    /* Get the equivalent x-coordinate of the new line. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
127
    current_x = actual_x(current->data, placewewant);
128

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
129
130
    /* Update all the lines that need to be updated. */
    edit_redraw(old_current, old_pww);
131
132
133
134

    check_statblank();
}

135
void do_page_down(void)
136
{
137
    size_t old_pww = placewewant;
138
    const filestruct *old_current = current;
139
#ifndef DISABLE_WRAPPING
140
    wrap_reset();
141
#endif
142

143
144
145
146
    /* If the last line of the file is onscreen, move current down
     * there and put the cursor at the beginning of the line. */
    if (edittop->lineno + editwinrows > filebot->lineno) {
	current = filebot;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
147
	placewewant = 0;
148
    } else {
149
150
	edit_scroll(DOWN, editwinrows - 2);

151
#ifndef NANO_SMALL
152
	/* If we're in smooth scrolling mode and there's at least one
153
154
	 * page of text left, move the current line of the edit window
	 * down a page. */
155
156
157
	if (ISSET(SMOOTHSCROLL) && current->lineno + editwinrows - 2 <=
		filebot->lineno) {
	    int i;
158
159
	    for (i = 0; i < editwinrows - 2; i++)
		current = current->next;
160
161
	}
	/* If we're not in smooth scrolling mode or there isn't at least
162
163
164
	 * one page of text left, put the cursor at the beginning of the
	 * top line of the edit window, as Pico does. */
	else {
165
#endif
166
	    current = edittop;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
167
	    placewewant = 0;
168
#ifndef NANO_SMALL
169
	}
170
#endif
171
    }
172

173
    /* Get the equivalent x-coordinate of the new line. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
174
    current_x = actual_x(current->data, placewewant);
175

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
176
177
    /* Update all the lines that need to be updated. */
    edit_redraw(old_current, old_pww);
178
179
180
181

    check_statblank();
}

182
void do_up(void)
Chris Allegretta's avatar
Chris Allegretta committed
183
{
184
#ifndef DISABLE_WRAPPING
Chris Allegretta's avatar
Chris Allegretta committed
185
    wrap_reset();
186
#endif
187
188
189
    check_statblank();

    if (current->prev == NULL)
190
	return;
191
192
193

    assert(current_y == current->lineno - edittop->lineno);
    current = current->prev;
194
    current_x = actual_x(current->data, placewewant);
195
196
197
198
199
200

    /* If we're on the first row of the edit window, scroll up one line
     * if we're in smooth scrolling mode, or up half a page if we're
     * not. */
    if (current_y == 0)
	edit_scroll(UP,
201
#ifndef NANO_SMALL
202
		ISSET(SMOOTHSCROLL) ? 1 :
203
#endif
204
205
206
207
208
209
210
211
212
		editwinrows / 2);

    /* Update the lines left alone by edit_scroll(): the line we were on
     * before and the line we're on now.  The former needs to be redrawn
     * if we're not on the first page, and the latter needs to be
     * drawn. */
    if (need_vertical_update(0))
	update_line(current->next, 0);
    update_line(current, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
213
}
Chris Allegretta's avatar
Chris Allegretta committed
214

215
void do_down(void)
216
{
217
#ifndef DISABLE_WRAPPING
Chris Allegretta's avatar
Chris Allegretta committed
218
    wrap_reset();
219
#endif
Chris Allegretta's avatar
Chris Allegretta committed
220
    check_statblank();
Chris Allegretta's avatar
Chris Allegretta committed
221
222

    if (current->next == NULL)
223
	return;
Chris Allegretta's avatar
Chris Allegretta committed
224

225
    assert(current_y == current->lineno - edittop->lineno);
Chris Allegretta's avatar
Chris Allegretta committed
226
    current = current->next;
227
    current_x = actual_x(current->data, placewewant);
Chris Allegretta's avatar
Chris Allegretta committed
228

229
230
231
232
233
    /* If we're on the last row of the edit window, scroll down one line
     * if we're in smooth scrolling mode, or down half a page if we're
     * not. */
    if (current_y == editwinrows - 1)
	edit_scroll(DOWN,
234
#ifndef NANO_SMALL
235
		ISSET(SMOOTHSCROLL) ? 1 :
236
#endif
237
238
239
240
241
242
243
244
245
		editwinrows / 2);

    /* Update the lines left alone by edit_scroll(): the line we were on
     * before and the line we're on now.  The former needs to be redrawn
     * if we're not on the first page, and the latter needs to be
     * drawn. */
    if (need_vertical_update(0))
	update_line(current->prev, 0);
    update_line(current, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
246
247
}

248
void do_left(int allow_update)
Chris Allegretta's avatar
Chris Allegretta committed
249
{
250
    size_t old_pww = placewewant;
251
252
253
254
255
    if (current_x > 0)
	current_x--;
    else if (current != fileage) {
	do_up();
	current_x = strlen(current->data);
Chris Allegretta's avatar
Chris Allegretta committed
256
257
258
    }
    placewewant = xplustabs();
    check_statblank();
259
260
    if (allow_update && need_horizontal_update(old_pww))
	update_line(current, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
261
262
}

263
void do_left_void(void)
264
{
265
    do_left(TRUE);
266
267
}

268
void do_right(int allow_update)
Chris Allegretta's avatar
Chris Allegretta committed
269
{
270
    size_t old_pww = placewewant;
271
272
273
274
    assert(current_x <= strlen(current->data));

    if (current->data[current_x] != '\0')
	current_x++;
275
    else if (current->next != NULL) {
276
277
	do_down();
	current_x = 0;
Chris Allegretta's avatar
Chris Allegretta committed
278
279
280
    }
    placewewant = xplustabs();
    check_statblank();
281
282
    if (allow_update && need_horizontal_update(old_pww))
	update_line(current, current_x);
Chris Allegretta's avatar
Chris Allegretta committed
283
}
284

285
void do_right_void(void)
286
{
287
    do_right(TRUE);
288
}