File: features.c

package info (click to toggle)
dav-text 0.8.5-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 188 kB
  • ctags: 117
  • sloc: ansic: 2,046; makefile: 69; sh: 10
file content (244 lines) | stat: -rw-r--r-- 6,133 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright 2001-2003 David Gucwa

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 <stdlib.h>
#include <string.h>
#include <ncurses.h>

#include "features.h"
#include "main.h"
#include "screenIO.h"
#include "fileIO.h"
#include "keyboard.h"
#include "buffers.h"
#include "move.h"

void search()
{
  unsigned char *t;
  int offset;
  struct line *l = *currentBuffer->currentLine;
  char down=1; //Whether or not the found word is below the current position
  char count=2;

  if (currentBuffer->head->next->next == currentBuffer->tail && currentBuffer->head->next->length == 1) {
    //No characters in the buffer
    return;
  }
  
  if (helpBarUpdate==0 || strcmp(currentBuffer->searchString,"")==0) {
    displayBottomRow();
    mvaddstr(maxY-1,0,"Search for what word?");
    mvgetstr(maxY-1,22,currentBuffer->searchString);
  }
  if (l == currentBuffer->tail->prev && currentBuffer->cursor.offset >= l->length - 2) {
    l = currentBuffer->head->next;
    offset = 0;
    down = 0;
  }
  else {
    offset = currentBuffer->cursor.offset + 1;
  }
  while (1) {
    t = (unsigned char *)strstr(l->data + offset, currentBuffer->searchString);
    if (t!=NULL) break;
    offset = 0;
    l = l->next;
    if (l==((*currentBuffer->currentLine)->next)) count--;
    if (l==currentBuffer->tail) { 
      l = currentBuffer->head->next; 
      down=0; 
    }
    if (count==0) break;
  }
  
  if (t==NULL) {
    displayBottomRow();
    mvaddstr(maxY-1,0,"String not found.");
    helpBarUpdate=2;
    strcpy(currentBuffer->searchString,"");
    return;
  }
  
  offset = t - l->data;
  
  if (down) {
    while(currentBuffer->cursor.l != l)
      moveDown(&currentBuffer->cursor);
  }
  else {
    while(currentBuffer->cursor.l != l)
      moveUp(&currentBuffer->cursor);
  }
  
  while(currentBuffer->cursor.offset < offset) {
    if (moveRight(&currentBuffer->cursor) == 1) break;
  }

  while(currentBuffer->cursor.offset > offset) {
    if (moveLeft(&currentBuffer->cursor) == 1) break;
  }

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

void replace()
{
  static char replaceString[80];
  static char findString[80];
  unsigned char *t;
  int offset;
  struct line *l = *currentBuffer->currentLine;
  char down=1; //Whether or not the found word is below the current position
  char count=2;

  if(helpBarUpdate==0 || !strcmp(findString,"") || !strcmp(replaceString,""))
  {
    displayBottomRow();
    mvaddstr(maxY-1, 0, "Replace what?");
    mvgetstr(maxY-1, 14, findString);
    displayBottomRow();
    mvaddstr(maxY-1, 0, "Replace with what?");
    mvgetstr(maxY-1, 19, replaceString);
  }
  offset = currentBuffer->cursor.offset + 1;
  while(1)
  {
    t = (unsigned char *)strstr(l->data + offset, findString);
    if(t!=NULL) break;
    offset = 0;
    l = l->next;
    if(l==((*currentBuffer->currentLine)->next)) count--;
    if(l==currentBuffer->tail) { l = currentBuffer->head->next; down=0; }
    if(count==0) break;
  }

  if(t==NULL)
  {
    displayBottomRow();
    mvaddstr(maxY-1,0,"String not found.");
    helpBarUpdate=2;
    strcpy(findString,"");
    return;
  }

  offset = t - l->data;

  if (down) {
    while(currentBuffer->cursor.l != l)
      moveDown(&currentBuffer->cursor);
  }
  else {
    while(currentBuffer->cursor.l != l)
      moveUp(&currentBuffer->cursor);
  }

  while(currentBuffer->cursor.offset < offset) {
    if (moveRight(&currentBuffer->cursor) == 1) break;
  }

  while(currentBuffer->cursor.offset > offset) {
    if (moveLeft(&currentBuffer->cursor) == 1) break;
  }

  //Delete the findstring
  for(offset = strlen(findString);offset;offset--)
    keyHit(330, 0);

  //Add the replacestring
  for(offset=0;offset<strlen(replaceString);offset++)
    keyHit(replaceString[offset], 0);

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

  helpBarUpdate = 2;
}

void tryCompile()
{
  FILE *fp;
  displayBottomRow();
  mvaddstr(maxY-1,0,"Running Makefile...");
  ungetch(' ');
  getch();
  if(strcmp(currentBuffer->fname, "")) save();
  system("make 2>dav.err >/dev/null");
  fp = fopen("dav.err","r");
  fgetc(fp);
  if(feof(fp))
  {
    displayBottomRow();
    mvaddstr(maxY-1,0,"Compile successful.");
    helpBarUpdate = 2;
  }
  else
  {
    displayBottomRow();
    while(currentBuffer->head->next->next != currentBuffer->tail || currentBuffer->head->next->length!=1)
      goToNextBuffer();
    load("dav.err");
    currentBuffer->lineUpdate = currentBuffer->cursor;
    currentBuffer->lineUpdate.lineNum = 0;
    currentBuffer->keepGoing=1;
    helpBarUpdate = 1;
  }
  fclose(fp);
  system("rm dav.err");
}

void gotoLine(int line)
{
  while(currentBuffer->cursor.lineNum < line - 1) {
    if(scrollDown()) {
      break;
    }
  }
  
  while (currentBuffer->cursor.lineNum < line - 1) {
    if (positionDown(&currentBuffer->cursor)) {
      break;
    }
  }
  
  currentBuffer->lineUpdate = currentBuffer->topLine;
  currentBuffer->lineUpdate.lineNum = 0;
  currentBuffer->keepGoing = 1;
  displayScreen();
}

void toggleAutoIndent()
{
  currentBuffer->lineUpdate.offset = -1;
  autoIndent = !autoIndent;
  displayBottomRow();
  mvaddstr(maxY-1,0,"Auto indenting set to");
  mvaddch(maxY-1,22,autoIndent+48);
  helpBarUpdate = 2;
}

void toggleBottomRow()
{
  bottomRowToggle = !bottomRowToggle;
  helpBar();
  currentBuffer->lineUpdate.offset = -1;
}