2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * George V. Neville-Neil. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "@(#)api.c 8.26 (Berkeley) 10/14/96";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
32 extern GS
*__global_list
; /* XXX */
36 * Return a pointer to the screen specified by the screen id
39 * PUBLIC: SCR *api_fscreen __P((int, char *));
51 /* Search the displayed list. */
52 for (tsp
= gp
->dq
.cqh_first
;
53 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
)
57 } else if (!strcmp(name
, tsp
->frp
->name
))
60 /* Search the hidden list. */
61 for (tsp
= gp
->hq
.cqh_first
;
62 tsp
!= (void *)&gp
->hq
; tsp
= tsp
->q
.cqe_next
)
66 } else if (!strcmp(name
, tsp
->frp
->name
))
75 * PUBLIC: int api_aline __P((SCR *, recno_t, char *, size_t));
78 api_aline(sp
, lno
, line
, len
)
84 return (db_append(sp
, 1, lno
, line
, len
));
91 * PUBLIC: int api_dline __P((SCR *, recno_t));
98 return (db_delete(sp
, lno
));
105 * PUBLIC: int api_gline __P((SCR *, recno_t, char **, size_t *));
108 api_gline(sp
, lno
, linepp
, lenp
)
116 if (db_eget(sp
, lno
, linepp
, lenp
, &isempty
)) {
118 msgq(sp
, M_ERR
, "209|The file is empty");
128 * PUBLIC: int api_iline __P((SCR *, recno_t, char *, size_t));
131 api_iline(sp
, lno
, line
, len
)
137 return (db_insert(sp
, lno
, line
, len
));
142 * Return the line number of the last line in the file.
144 * PUBLIC: int api_lline __P((SCR *, recno_t *));
151 return (db_last(sp
, lnop
));
158 * PUBLIC: int api_sline __P((SCR *, recno_t, char *, size_t));
161 api_sline(sp
, lno
, line
, len
)
167 return (db_set(sp
, lno
, line
, len
));
174 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
177 api_getmark(sp
, markname
, mp
)
182 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
189 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
192 api_setmark(sp
, markname
, mp
)
197 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
202 * Return the first mark if next not set, otherwise return the
205 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
208 api_nextmark(sp
, next
, namep
)
215 mp
= sp
->ep
->marks
.lh_first
;
217 for (; mp
!= NULL
; mp
= mp
->q
.le_next
)
218 if (mp
->name
== *namep
) {
232 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
235 api_getcursor(sp
, mp
)
248 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
251 api_setcursor(sp
, mp
)
257 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
259 if (mp
->cno
< 0 || mp
->cno
> len
) {
260 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
264 /* Set the cursor. */
272 * Print an error message.
274 * PUBLIC: void api_emessage __P((SCR *, char *));
277 api_emessage(sp
, text
)
281 msgq(sp
, M_ERR
, "%s", text
);
286 * Print an informational message.
288 * PUBLIC: void api_imessage __P((SCR *, char *));
291 api_imessage(sp
, text
)
295 msgq(sp
, M_INFO
, "%s", text
);
300 * Create a new screen and return its id
301 * or edit a new file in the current screen.
303 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
306 api_edit(sp
, file
, spp
, newscreen
)
316 ex_cinit(&cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0, ap
);
317 ex_cadd(&cmd
, &a
, file
, strlen(file
));
319 ex_cinit(&cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0, NULL
);
321 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
322 if (cmd
.cmd
->fn(sp
, &cmd
))
332 * PUBLIC: int api_escreen __P((SCR *));
342 * If the interpreter exits anything other than the current
343 * screen, vi isn't going to update everything correctly.
345 ex_cinit(&cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0, NULL
);
346 return (cmd
.cmd
->fn(sp
, &cmd
));
351 * Switch to a new screen.
353 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
356 api_swscreen(sp
, new)
361 * If the interpreter switches from anything other than the
362 * current screen, vi isn't going to update everything correctly.
365 F_SET(sp
, SC_SSWITCH
);
374 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
377 api_map(sp
, name
, map
, len
)
385 ex_cinit(&cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0, ap
);
386 ex_cadd(&cmd
, &a
, name
, strlen(name
));
387 ex_cadd(&cmd
, &b
, map
, len
);
388 return (cmd
.cmd
->fn(sp
, &cmd
));
395 * PUBLIC: int api_unmap __P((SCR *, char *));
405 ex_cinit(&cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0, ap
);
406 ex_cadd(&cmd
, &a
, name
, strlen(name
));
407 return (cmd
.cmd
->fn(sp
, &cmd
));
412 * Return a option value as a string, in allocated memory.
413 * If the option is of type boolean, boolvalue is (un)set
414 * according to the value; otherwise boolvalue is -1.
416 * PUBLIC: int api_opts_get __P((SCR *, char *, char **, int *));
419 api_opts_get(sp
, name
, value
, boolvalue
)
427 if ((op
= opts_search(name
)) == NULL
) {
428 opts_nomatch(sp
, name
);
432 offset
= op
- optlist
;
433 if (boolvalue
!= NULL
)
438 MALLOC_RET(sp
, *value
, char *, strlen(op
->name
) + 2 + 1);
439 (void)sprintf(*value
,
440 "%s%s", O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
441 if (boolvalue
!= NULL
)
442 *boolvalue
= O_ISSET(sp
, offset
);
445 MALLOC_RET(sp
, *value
, char *, 20);
446 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
449 if (O_STR(sp
, offset
) == NULL
) {
450 MALLOC_RET(sp
, *value
, char *, 2);
454 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
455 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
466 * PUBLIC: int api_opts_set __P((SCR *, char *, char *, u_long, int));
469 api_opts_set(sp
, name
, str_value
, num_value
, bool_value
)
471 char *name
, *str_value
;
481 if ((op
= opts_search(name
)) == NULL
) {
482 opts_nomatch(sp
, name
);
489 GET_SPACE_RET(sp
, bp
, blen
, 64);
490 a
.len
= snprintf(bp
, 64, "%s%s", bool_value
? "" : "no", name
);
493 GET_SPACE_RET(sp
, bp
, blen
, 64);
494 a
.len
= snprintf(bp
, 64, "%s=%lu", name
, num_value
);
497 GET_SPACE_RET(sp
, bp
, blen
, 1024);
498 a
.len
= snprintf(bp
, 1024, "%s=%s", name
, str_value
);
506 rval
= opts_set(sp
, ap
, NULL
);
508 FREE_SPACE(sp
, bp
, blen
);
515 * Execute a string as an ex command.
517 * PUBLIC: int api_run_str __P((SCR *, char *));
524 return (ex_run_str(sp
, NULL
, cmd
, strlen(cmd
), 0, 0));