1270096Strasz/*-
2270096Strasz * Copyright (c) 1992, 1993, 1994
3270096Strasz *	The Regents of the University of California.  All rights reserved.
4270096Strasz * Copyright (c) 1992, 1993, 1994, 1995, 1996
5270096Strasz *	Keith Bostic.  All rights reserved.
6270096Strasz *
7270096Strasz * See the LICENSE file for redistribution information.
8270096Strasz */
9270096Strasz
10270096Strasz#include "config.h"
11270096Strasz
12270096Strasz#ifndef lint
13270096Straszstatic const char sccsid[] = "$Id: v_util.c,v 10.14 2001/06/25 15:19:36 skimo Exp $";
14270096Strasz#endif /* not lint */
15270096Strasz
16270096Strasz#include <sys/types.h>
17270096Strasz#include <sys/queue.h>
18270096Strasz#include <sys/time.h>
19270096Strasz
20270096Strasz#include <bitstring.h>
21270096Strasz#include <ctype.h>
22270096Strasz#include <limits.h>
23270096Strasz#include <stdio.h>
24270096Strasz#include <stdlib.h>
25270096Strasz#include <string.h>
26270096Strasz#include <unistd.h>
27270096Strasz
28270096Strasz#include "../common/common.h"
29270096Strasz#include "vi.h"
30270096Strasz
31270096Strasz/*
32270096Strasz * v_eof --
33270096Strasz *	Vi end-of-file error.
34270096Strasz *
35270096Strasz * PUBLIC: void v_eof __P((SCR *, MARK *));
36270897Strasz */
37270897Straszvoid
38270897Straszv_eof(SCR *sp, MARK *mp)
39270096Strasz{
40270096Strasz	recno_t lno;
41270096Strasz
42270096Strasz	if (mp == NULL)
43270096Strasz		v_emsg(sp, NULL, VIM_EOF);
44270096Strasz	else {
45270096Strasz		if (db_last(sp, &lno))
46270096Strasz			return;
47270096Strasz		if (mp->lno >= lno)
48270096Strasz			v_emsg(sp, NULL, VIM_EOF);
49270096Strasz		else
50270096Strasz			msgq(sp, M_BERR, "195|Movement past the end-of-file");
51270096Strasz	}
52270096Strasz}
53270096Strasz
54270096Strasz/*
55270096Strasz * v_eol --
56270096Strasz *	Vi end-of-line error.
57270096Strasz *
58270096Strasz * PUBLIC: void v_eol __P((SCR *, MARK *));
59270096Strasz */
60270096Straszvoid
61270096Straszv_eol(SCR *sp, MARK *mp)
62270096Strasz{
63270096Strasz	size_t len;
64270096Strasz
65270096Strasz	if (mp == NULL)
66270096Strasz		v_emsg(sp, NULL, VIM_EOL);
67270096Strasz	else {
68270096Strasz		if (db_get(sp, mp->lno, DBG_FATAL, NULL, &len))
69270096Strasz			return;
70270096Strasz		if (mp->cno == len - 1)
71270096Strasz			v_emsg(sp, NULL, VIM_EOL);
72270096Strasz		else
73270096Strasz			msgq(sp, M_BERR, "196|Movement past the end-of-line");
74270096Strasz	}
75270096Strasz}
76270096Strasz
77270096Strasz/*
78270096Strasz * v_nomove --
79270096Strasz *	Vi no cursor movement error.
80270096Strasz *
81270096Strasz * PUBLIC: void v_nomove __P((SCR *));
82270096Strasz */
83270096Straszvoid
84270096Straszv_nomove(SCR *sp)
85270096Strasz{
86270096Strasz	msgq(sp, M_BERR, "197|No cursor movement made");
87270096Strasz}
88270096Strasz
89270096Strasz/*
90270096Strasz * v_sof --
91270096Strasz *	Vi start-of-file error.
92270096Strasz *
93270096Strasz * PUBLIC: void v_sof __P((SCR *, MARK *));
94270096Strasz */
95270096Straszvoid
96270096Straszv_sof(SCR *sp, MARK *mp)
97270096Strasz{
98270096Strasz	if (mp == NULL || mp->lno == 1)
99270096Strasz		msgq(sp, M_BERR, "198|Already at the beginning of the file");
100270096Strasz	else
101270096Strasz		msgq(sp, M_BERR, "199|Movement past the beginning of the file");
102270096Strasz}
103270096Strasz
104270096Strasz/*
105270096Strasz * v_sol --
106270096Strasz *	Vi start-of-line error.
107270096Strasz *
108270096Strasz * PUBLIC: void v_sol __P((SCR *));
109270096Strasz */
110270096Straszvoid
111270096Straszv_sol(SCR *sp)
112270096Strasz{
113270096Strasz	msgq(sp, M_BERR, "200|Already in the first column");
114270096Strasz}
115270096Strasz
116270096Strasz/*
117270096Strasz * v_isempty --
118270096Strasz *	Return if the line contains nothing but white-space characters.
119270096Strasz *
120270096Strasz * PUBLIC: int v_isempty __P((CHAR_T *, size_t));
121270096Strasz */
122270096Straszint
123270096Straszv_isempty(CHAR_T *p, size_t len)
124270096Strasz{
125270096Strasz	for (; len--; ++p)
126270096Strasz		if (!isblank(*p))
127270096Strasz			return (0);
128270096Strasz	return (1);
129270096Strasz}
130270096Strasz
131270096Strasz/*
132270096Strasz * v_emsg --
133270096Strasz *	Display a few common vi messages.
134270096Strasz *
135270096Strasz * PUBLIC: void v_emsg __P((SCR *, char *, vim_t));
136270096Strasz */
137270096Straszvoid
138270096Straszv_emsg(SCR *sp, char *p, vim_t which)
139270096Strasz{
140270096Strasz	switch (which) {
141270096Strasz	case VIM_COMBUF:
142270096Strasz		msgq(sp, M_ERR,
143270096Strasz		    "201|Buffers should be specified before the command");
144270096Strasz		break;
145270096Strasz	case VIM_EMPTY:
146270096Strasz		msgq(sp, M_BERR, "209|The file is empty");
147270096Strasz		break;
148270096Strasz	case VIM_EOF:
149270096Strasz		msgq(sp, M_BERR, "202|Already at end-of-file");
150270096Strasz		break;
151270096Strasz	case VIM_EOL:
152270096Strasz		msgq(sp, M_BERR, "203|Already at end-of-line");
153270096Strasz		break;
154270096Strasz	case VIM_NOCOM:
155270096Strasz	case VIM_NOCOM_B:
156270096Strasz		msgq(sp,
157270096Strasz		    which == VIM_NOCOM_B ? M_BERR : M_ERR,
158270096Strasz		    "204|%s isn't a vi command", p);
159270096Strasz		break;
160270096Strasz	case VIM_WRESIZE:
161270096Strasz		msgq(sp, M_ERR, "Window resize interrupted text input mode");
162270096Strasz		break;
163270096Strasz	case VIM_USAGE:
164270096Strasz		msgq(sp, M_ERR, "205|Usage: %s", p);
165270096Strasz		break;
166270096Strasz	}
167270096Strasz}
168270096Strasz