1 04/15/88 "EDITOR" EXAMPLE
2
3 /*
4 * EDITOR. A screen-oriented editor. The user
5 * interface is similar to a subset of vi.
6 * The buffer is kept in stdscr itself to simplify
7 * the program.
8 */
9
10 #include <curses.h>
11
12 FILE *outf;
13
14 #define CTRLc c - 'A' + 1
15
16
17 int len, edit;
18
19 mainargc argv
20 char **argv;
21
22 int i n l;
23 int c;
24 FILE *fd;
25
26
27 ifargc !=2
28 fprintfstderr "Usage: edit file0\n";
29 exit1;
30
31
32 fd = fopenargv1 "r";
33 if fd == NULL
34 perrorargv1;
35 exit2;
36
37
38
39 initscr;
40 cbreak;
41 nonl;
42 noecho;
43 idlokstdscr TRUE;
44 keypadstdscr TRUE;
45
46 /* Read in the file */
47
48 while c = getcfd != EOF
49 addchc;
50 fclosefd;
51
52
53 move00;
54 refresh;
55 edit;
56
57 /* Write out the file */
58 fd = fopenargv1 "w";
59
60 for l=0; l<23; l++
61 n = lenl;
62 for i=0; i<n; i++
63 putcmvinchl i fd;
64 putc'0' fd;
65
66 fclosefd;
67
68
69 echo;
70 endwin;
71 exit0;
72
73
74 int lenlineno
75 int lineno;
76
77 int linelen = COLS-1;
78
79 while linelen >=0 && mvinchlineno linelen == ' '
80 linelen--;
81
82 return linelen + 1;
83
84
85
86 /* Global value of current cursor position */
87
88 int row, col;
89
90 int edit
91
92 int c;
93
94 for ;;
95 move row col;
96 refresh;
97 c=getch;
98
99 switch c /* Editor commands */
100
101
102 /* hjkl and arrow keys: move cursor */
103 /* in direction indicated */
104 case 'h':
105 case KEY_LEFT:
106 case CTRL'H':
107
108 if col>0
109 col--;
110 break;
111
112 case 'j':
113 case KEY_DOWN:
114 if row<LINES-1
115 row ++;
116 break;
117
118
119 case 'k':
120 case KEY_UP:
121 if row>0
122 row--;
123 break;
124
125 case 'l':
126 case KEY_RIGHT:
127 if col < COLS-1
128 col++;
129 break;
130
131
132 /* i: enter input mode */
133
134 case KEY_IC:
135 case 'i':
136 moverowcol;
137 input;
138 break;
139
140 /* x: delete current character */
141 case KEY_DC:
142 case 'x':
143 moverowcol;
144 delch;
145 refresh;
146 break;
147
148
149 /* o: open up a new line and enter input mode */
150
151 case KEY_IL:
152 case 'o':
153 row++;col=0;
154 moverow col;
155 insertln;
156 refresh;
157 input;
158 break;
159
160
161 /* d: delete current line */
162
163 case KEY_DL:
164 case 'd':
165 moverowcol;
166 deleteln;
167 refresh;
168 break;
169
170
171 /* ^R: redraw screen */
172
173 case KEY_CLEAR:
174 case CTRL'R':
175 clearokcurscr;
176 refresh;
177 break;
178
179 /* w: write and quit */
180 case 'w':
181 return;
182
183
184 /* q: quit without writing */
185 case 'q':
186 endwin;
187 exit1;
188 default:
189 flash;
190 break;
191
192
193
194
195 /*
196 *Insert mode: accept characters and insert them.
197 * End with ^D or EIC
198 */
199
200
201 int input
202
203 int c;
204
205 standout;
206 mvaddstrLINES-1 COLS-20 "INPUT MODE";
207 standend;
208 moverow col;
209 refresh;
210
211 for;;
212 c = 0;
213 c = getch;
214
215
216 if c == CTRL'D' || c == KEY_EIC
217 || c == '.'
218 break;
219
220 /* for Multics echo cr nl or lf cr */
221
222 else if c == CTRL'J' || c == CTRL'M'
223
224 row++;
225 col=0;
226 moverowcol;
227
228
229
230 else
231 inschc;
232 moverow ++ col;
233 refresh;
234
235
236 moveLINES-1 COLS-20;
237 clrtoeol;
238 moverow col;
239 refresh;
240
241