more use of CHAR_T
[nvi.git] / vi / v_event.c
blob7961d64b66f4548c9c68e1985ba154f6279b4ed1
1 /*-
2 * Copyright (c) 1996
3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "$Id: v_event.c,v 8.19 2001/06/09 18:26:31 skimo Exp $ (Berkeley) $Date: 2001/06/09 18:26:31 $";
12 #endif /* not lint */
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
18 #include <bitstring.h>
19 #include <ctype.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "../common/common.h"
28 #include "../ipc/ip.h"
29 #include "vi.h"
32 * v_c_settop --
33 * Scrollbar position.
35 static int
36 v_c_settop(sp, vp)
37 SCR *sp;
38 VICMD *vp;
40 SMAP *smp;
41 size_t x = 0, y = LASTLINE(sp); /* Future: change to -1 to not
42 * display the cursor
44 size_t tx, ty = -1;
47 * We want to scroll the screen, without changing the cursor position.
48 * So, we fill the screen map and then flush it to the screen. Then,
49 * set the VIP_S_REFRESH flag so the main vi loop doesn't update the
50 * screen. When the next real command happens, the refresh code will
51 * notice that the screen map is way wrong and fix it.
53 * XXX
54 * There may be a serious performance problem here -- we're doing no
55 * optimization whatsoever, which means that we're copying the entire
56 * screen out to the X11 screen code on each change.
58 if (vs_sm_fill(sp, vp->ev.e_lno, P_TOP))
59 return (1);
60 for (smp = HMAP; smp <= TMAP; ++smp) {
61 SMAP_FLUSH(smp);
62 if (vs_line(sp, smp, &ty, &tx))
63 return (1);
64 if (ty != -1) {
65 y = ty;
66 x = tx;
69 (void)sp->gp->scr_move(sp, y, x);
71 F_SET(VIP(sp), VIP_S_REFRESH);
73 return (sp->gp->scr_refresh(sp, 0));
77 * v_edit --
78 * Edit command.
80 static int
81 v_edit(sp, vp)
82 SCR *sp;
83 VICMD *vp;
85 EXCMD cmd;
87 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
88 argv_exp0(sp, &cmd, vp->ev.e_csp, vp->ev.e_len);
89 return (v_exec_ex(sp, vp, &cmd));
93 * v_editopt --
94 * Set an option value.
96 static int
97 v_editopt(sp, vp)
98 SCR *sp;
99 VICMD *vp;
101 int rval;
102 char *np;
103 size_t nlen;
104 char *p1, *p2;
106 INT2CHAR(sp, vp->ev.e_str1, STRLEN(vp->ev.e_str1), np, nlen);
107 p1 = strdup(np);
108 INT2CHAR(sp, vp->ev.e_str2, STRLEN(vp->ev.e_str2), np, nlen);
109 p2 = strdup(np);
110 rval = api_opts_set(sp, p1, p2, vp->ev.e_val1, vp->ev.e_val1);
111 if (sp->gp->scr_reply != NULL)
112 (void)sp->gp->scr_reply(sp, rval, NULL);
113 free(p1);
114 free(p2);
115 return (rval);
119 * v_editsplit --
120 * Edit in a split screen.
122 static int
123 v_editsplit(sp, vp)
124 SCR *sp;
125 VICMD *vp;
127 EXCMD cmd;
129 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
130 F_SET(&cmd, E_NEWSCREEN);
131 argv_exp0(sp, &cmd, vp->ev.e_csp, vp->ev.e_len);
132 return (v_exec_ex(sp, vp, &cmd));
136 * v_tag --
137 * Tag command.
139 static int
140 v_tag(sp, vp)
141 SCR *sp;
142 VICMD *vp;
144 EXCMD cmd;
146 if (v_curword(sp))
147 return (1);
149 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, OOBLNO, 0);
150 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw));
151 return (v_exec_ex(sp, vp, &cmd));
155 * v_tagas --
156 * Tag on the supplied string.
158 static int
159 v_tagas(sp, vp)
160 SCR *sp;
161 VICMD *vp;
163 EXCMD cmd;
165 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, OOBLNO, 0);
166 argv_exp0(sp, &cmd, vp->ev.e_csp, vp->ev.e_len);
167 return (v_exec_ex(sp, vp, &cmd));
171 * v_tagsplit --
172 * Tag in a split screen.
174 static int
175 v_tagsplit(sp, vp)
176 SCR *sp;
177 VICMD *vp;
179 EXCMD cmd;
181 if (v_curword(sp))
182 return (1);
184 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, OOBLNO, 0);
185 F_SET(&cmd, E_NEWSCREEN);
186 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw));
187 return (v_exec_ex(sp, vp, &cmd));
191 * v_quit --
192 * Quit command.
194 static int
195 v_quit(sp, vp)
196 SCR *sp;
197 VICMD *vp;
199 EXCMD cmd;
201 ex_cinit(sp, &cmd, C_QUIT, 0, OOBLNO, OOBLNO, 0);
202 return (v_exec_ex(sp, vp, &cmd));
206 * v_erepaint --
207 * Repaint selected lines from the screen.
209 * PUBLIC: int v_erepaint __P((SCR *, EVENT *));
212 v_erepaint(sp, evp)
213 SCR *sp;
214 EVENT *evp;
216 SMAP *smp;
218 for (; evp->e_flno <= evp->e_tlno; ++evp->e_flno) {
219 smp = HMAP + evp->e_flno - 1;
220 SMAP_FLUSH(smp);
221 if (vs_line(sp, smp, NULL, NULL))
222 return (1);
224 return (0);
228 * v_sel_end --
229 * End selection.
232 v_sel_end(sp, evp)
233 SCR *sp;
234 EVENT *evp;
236 SMAP *smp;
237 VI_PRIVATE *vip;
239 smp = HMAP + evp->e_lno;
240 if (smp > TMAP) {
241 /* XXX */
242 return (1);
245 vip = VIP(sp);
246 vip->sel.lno = smp->lno;
247 vip->sel.cno =
248 vs_colpos(sp, smp->lno, evp->e_cno + (smp->soff - 1) * sp->cols);
249 return (0);
253 * v_sel_start --
254 * Start selection.
257 v_sel_start(sp, evp)
258 SCR *sp;
259 EVENT *evp;
261 SMAP *smp;
262 VI_PRIVATE *vip;
264 smp = HMAP + evp->e_lno;
265 if (smp > TMAP) {
266 /* XXX */
267 return (1);
270 vip = VIP(sp);
271 vip->sel.lno = smp->lno;
272 vip->sel.cno =
273 vs_colpos(sp, smp->lno, evp->e_cno + (smp->soff - 1) * sp->cols);
274 return (0);
278 * v_wq --
279 * Write and quit command.
281 static int
282 v_wq(sp, vp)
283 SCR *sp;
284 VICMD *vp;
286 EXCMD cmd;
288 ex_cinit(sp, &cmd, C_WQ, 0, OOBLNO, OOBLNO, 0);
290 cmd.addr1.lno = 1;
291 if (db_last(sp, &cmd.addr2.lno))
292 return (1);
293 return (v_exec_ex(sp, vp, &cmd));
297 * v_write --
298 * Write command.
300 static int
301 v_write(sp, vp)
302 SCR *sp;
303 VICMD *vp;
305 EXCMD cmd;
307 ex_cinit(sp, &cmd, C_WRITE, 0, OOBLNO, OOBLNO, 0);
309 cmd.addr1.lno = 1;
310 if (db_last(sp, &cmd.addr2.lno))
311 return (1);
312 return (v_exec_ex(sp, vp, &cmd));
316 * v_writeas --
317 * Write command.
319 static int
320 v_writeas(sp, vp)
321 SCR *sp;
322 VICMD *vp;
324 EXCMD cmd;
326 ex_cinit(sp, &cmd, C_WRITE, 0, OOBLNO, OOBLNO, 0);
327 argv_exp0(sp, &cmd, vp->ev.e_csp, vp->ev.e_len);
329 cmd.addr1.lno = 1;
330 if (db_last(sp, &cmd.addr2.lno))
331 return (1);
332 return (v_exec_ex(sp, vp, &cmd));
336 * v_event --
337 * Find the event associated with a function.
339 * PUBLIC: int v_event __P((SCR *, VICMD *));
342 v_event(sp, vp)
343 SCR *sp;
344 VICMD *vp;
346 /* This array maps events to vi command functions. */
347 static VIKEYS const vievents[] = {
348 #define V_C_SETTOP 0 /* VI_C_SETTOP */
349 {v_c_settop, 0},
350 #define V_EDIT 1 /* VI_EDIT */
351 {v_edit, 0},
352 #define V_EDITOPT 2 /* VI_EDITOPT */
353 {v_editopt, 0},
354 #define V_EDITSPLIT 3 /* VI_EDITSPLIT */
355 {v_editsplit, 0},
356 #define V_EMARK 4 /* VI_MOUSE_MOVE */
357 {v_emark, V_ABS_L|V_MOVE},
358 #define V_QUIT 5 /* VI_QUIT */
359 {v_quit, 0},
360 #define V_SEARCH 6 /* VI_SEARCH */
361 {v_esearch, V_ABS_L|V_MOVE},
362 #define V_TAG 7 /* VI_TAG */
363 {v_tag, 0},
364 #define V_TAGAS 8 /* VI_TAGAS */
365 {v_tagas, 0},
366 #define V_TAGSPLIT 9 /* VI_TAGSPLIT */
367 {v_tagsplit, 0},
368 #define V_WQ 10 /* VI_WQ */
369 {v_wq, 0},
370 #define V_WRITE 11 /* VI_WRITE */
371 {v_write, 0},
372 #define V_WRITEAS 12 /* VI_WRITEAS */
373 {v_writeas, 0},
376 switch (vp->ev.e_ipcom) {
377 case VI_C_BOL:
378 vp->kp = &vikeys['0'];
379 break;
380 case VI_C_BOTTOM:
381 vp->kp = &vikeys['G'];
382 break;
383 case VI_C_DEL:
384 vp->kp = &vikeys['x'];
385 break;
386 case VI_C_DOWN:
387 F_SET(vp, VC_C1SET);
388 vp->count = vp->ev.e_lno;
389 vp->kp = &vikeys['\012'];
390 break;
391 case VI_C_EOL:
392 vp->kp = &vikeys['$'];
393 break;
394 case VI_C_INSERT:
395 vp->kp = &vikeys['i'];
396 break;
397 case VI_C_LEFT:
398 vp->kp = &vikeys['\010'];
399 break;
400 case VI_C_PGDOWN:
401 F_SET(vp, VC_C1SET);
402 vp->count = vp->ev.e_lno;
403 vp->kp = &vikeys['\006'];
404 break;
405 case VI_C_PGUP:
406 F_SET(vp, VC_C1SET);
407 vp->count = vp->ev.e_lno;
408 vp->kp = &vikeys['\002'];
409 break;
410 case VI_C_RIGHT:
411 vp->kp = &vikeys['\040'];
412 break;
413 case VI_C_SEARCH:
414 vp->kp = &vievents[V_SEARCH];
415 break;
416 case VI_C_SETTOP:
417 vp->kp = &vievents[V_C_SETTOP];
418 break;
419 case VI_C_TOP:
420 F_SET(vp, VC_C1SET);
421 vp->count = 1;
422 vp->kp = &vikeys['G'];
423 break;
424 case VI_C_UP:
425 F_SET(vp, VC_C1SET);
426 vp->count = vp->ev.e_lno;
427 vp->kp = &vikeys['\020'];
428 break;
429 case VI_EDIT:
430 vp->kp = &vievents[V_EDIT];
431 break;
432 case VI_EDITOPT:
433 vp->kp = &vievents[V_EDITOPT];
434 break;
435 case VI_EDITSPLIT:
436 vp->kp = &vievents[V_EDITSPLIT];
437 break;
438 case VI_MOUSE_MOVE:
439 vp->kp = &vievents[V_EMARK];
440 break;
441 case VI_SEL_END:
442 v_sel_end(sp, &vp->ev);
443 /* XXX RETURN IGNORE */
444 break;
445 case VI_SEL_START:
446 v_sel_start(sp, &vp->ev);
447 /* XXX RETURN IGNORE */
448 break;
449 case VI_QUIT:
450 vp->kp = &vievents[V_QUIT];
451 break;
452 case VI_TAG:
453 vp->kp = &vievents[V_TAG];
454 break;
455 case VI_TAGAS:
456 vp->kp = &vievents[V_TAGAS];
457 break;
458 case VI_TAGSPLIT:
459 vp->kp = &vievents[V_TAGSPLIT];
460 break;
461 case VI_UNDO:
462 vp->kp = &vikeys['u'];
463 break;
464 case VI_WQ:
465 vp->kp = &vievents[V_WQ];
466 break;
467 case VI_WRITE:
468 vp->kp = &vievents[V_WRITE];
469 break;
470 case VI_WRITEAS:
471 vp->kp = &vievents[V_WRITEAS];
472 break;
473 default:
474 return (1);
476 return (0);