File: move.c

package info (click to toggle)
dav-text 0.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 252 kB
  • sloc: ansic: 2,895; makefile: 27; sh: 10
file content (171 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2001-2003 David Gucwa
Copyright 2017-2019 Adam Bilbrough

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
the Free Software Foundation; either version 2 of the License, or
(at your option) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "move.h"
#include "main.h"

void moveDown(struct position *p) {
  positionDown(p);
  if(p->cursY==maxY-1 && p==&currentBuffer->cursor) { 
    scrollDown(); 
  }
}

void moveUp(struct position *p) {
  positionUp(p);
  if(p->cursY==-1 && p==&currentBuffer->cursor) { 
    scrollUp(); 
  }
}

char moveLeft(struct position *p) {
  /*
  Returns 0 upon normal movement
  Returns 1 if the position could not be moved left
  Returns 2 if moving left caused the position to enter the previous line
  Returns 3 if moving left caused the position to wrap on the same line
  */
  
  char r = 0;
  if(p->l == currentBuffer->head->next && p->offset == 0) {
    /* Beginning of file */
    return 1;
  }
  if(!p->offset) {
  /* Beginning of line */
    p->l = p->l->prev;
    p->offset = p->l->length - 1;
    if(p->l->hasTabs) {
      determineCursX(p);
    }
    else {
      p->cursX = p->offset % maxX;
    }
    p->cursY--;
    p->lineNum--;
    r = 2;
  }
  else {
    p->offset--;
    if(!p->cursX) {
      r = 3;
      p->cursY--;
      if(p->l->hasTabs) {
        determineCursX(p);
      }
      else {
        p->cursX = maxX - 1;
      }
    }
    else {
      if(p->l->hasTabs)
        determineCursX(p);
      else
        p->cursX--;
    }

  }
  if(p->cursY==-1 && p==&currentBuffer->cursor) { 
    scrollUp(); 
  }
  return r;
}

char moveRight(struct position *p) {
  /* 
  Returns 0 upon normal movement
  Returns 1 if the position could not be moved right (end of file)
  Returns 2 if moving right caused the position to enter the next line
  Returns 3 if moving right caused the position to wrap on the same line
  */

  char r=0;
  if(p->l->next == currentBuffer->tail && p->offset == p->l->length-1) {
    /* End of file */
    return 1;
  }
  
  if(p->offset < p->l->length - 1) {
    /* Normal movement */
    int temp = p->cursX;
    p->offset++;
    if (p->l->hasTabs) {
      determineCursX(p);
    } else {
      p->cursX++;
      if (p->cursX == maxX) {
        p->cursX=0;
      }
    }
    if (p->cursX < temp) {
      p->cursY++;
      r = 3;
    }
  }
  else {
    /* Next line */
    p->l = p->l->next;
    p->cursX = p->offset = 0;
    p->cursY++;
    p->lineNum++;
    r = 2; //Hit a new line
  }
  if(p->cursY == maxY-1 && p==&currentBuffer->cursor) { 
    scrollDown(); 
  }
  return r;
}

char scrollDown()
{
  /* Returns 1 if the screen did not scroll down */

  if(currentBuffer->topLine.lineNum + (maxY>>1) > currentBuffer->numLines) {
    return 1;
  }

  if(currentBuffer->cursor.cursY==0) {
    positionDown(&currentBuffer->cursor);
  }

  if(!positionDown(&currentBuffer->topLine)) {
    currentBuffer->cursor.cursY--;
  }

  currentBuffer->lineUpdate = currentBuffer->topLine;
  currentBuffer->lineUpdate.lineNum = 0;
  currentBuffer->keepGoing = 1;
  return 0;
}

char scrollUp()
{
  /* Returns 1 if the screen did not scroll up */
  if(currentBuffer->cursor.cursY==maxY-1) {
    positionUp(&currentBuffer->cursor);
  }
  
  if(!positionUp(&currentBuffer->topLine)) {
    currentBuffer->cursor.cursY++;
  }
  
  currentBuffer->lineUpdate = currentBuffer->topLine;
  currentBuffer->lineUpdate.lineNum = 0;
  currentBuffer->keepGoing = 1;
  return 0;
}