1 /*-
2  * Copyright (c) 2002 Jordan DeLong
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of contributors may be
14  *    used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #ifndef BUFFER_H
30 #define BUFFER_H
31 
32 /*
33  * buffer structure
34  */
35 struct buffer {
36 	TAILQ_ENTRY(buffer) b_list;
37 
38 	u_char		*dispname;	/* the displayed name */
39 	u_char		*name;		/* buffer name/filename */
40 	linelist_t	lines;		/* list of lines */
41 	int		linecnt;	/* number of lines in the buffer */
42 	undolist_t	undolist;	/* list of undoable operations */
43 	undo_t		*undoitem;	/* current undo item in the list */
44 	undo_t		*undodisk;	/* the undo loc that is unmodified */
45 	int		refcnt;		/* buffer reference count */
46 
47 	/* style of line-terminators used */
48 	enum {
49 		NLSTYLE_UNKNOWN		= -1,
50 		NLSTYLE_LF,
51 		NLSTYLE_CRLF,
52 	} nlstyle;
53 
54 	/*
55 	 * buffer position marks; buffer marks are positions in a buffer
56 	 * marked as being important enough to reference again in the
57 	 * near future.  some commands may choose to set marks for the
58 	 * user, or the user may explicitly set the mark to a position
59 	 * in the buffer.  mark_implicit is set if the mark was set
60 	 * implicitly without user interaction, and some commands may
61 	 * see fit to treat an implicit mark differently than a mark
62 	 * which the user set explicitly.
63 	 */
64 	struct mark {
65 		line_t	*line;
66 		int	linenum;
67 		int	col;
68 		TAILQ_ENTRY(mark) m_list;
69 	};
70 	TAILQ_HEAD(marklist, mark) marklist;
71 	mark_t		*mark;
72 	u_int		markcnt;
73 
74 	/* buffer flags */
75 	u_int	mark_implicit:1;
76 	u_int	modified:1;
77 	u_int	internal:1;
78 	u_int	dirty:1;	/* changed since last draw */
79 };
80 
81 TAILQ_HEAD(bufferlist, buffer);
82 
83 extern bufferlist_t	buffer_list;
84 
85 buffer_t	*buffer_create(u_char *name, int internal);
86 void		buffer_destroy(buffer_t *);
87 int		buffer_save(buffer_t *);
88 void		buffer_pushmark(buffer_t *, line_t *, int linenum, int col);
89 void		buffer_popmark(buffer_t *);
90 void		buffer_empty(buffer_t *, int undoable);
91 line_t		*buffer_addline(buffer_t *, int undoable, line_t *,
92 			int linenum);
93 void		buffer_rmline(buffer_t *, int undoable, line_t *,
94 			int linenum);
95 void		buffer_rmstr(buffer_t *, int undoable, line_t *,
96 			int linenum, int loc, int slen);
97 void		buffer_rmch(buffer_t *, int undoable, line_t *,
98 			int linenum, int loc);
99 void		buffer_putc(buffer_t *, int undoable, line_t *,
100 			int linenum, int loc, u_char);
101 void		buffer_nputs(buffer_t *, int undoable, line_t *,
102 			int linenum, int loc, u_char *s, int slen);
103 
104 /* add a reference to a buffer */
buffer_ref(buffer_t * buffer)105 static __inline buffer_t *buffer_ref(buffer_t *buffer) {
106 	assert(buffer->refcnt >= 0);
107 	buffer->refcnt++;
108 	return buffer;
109 }
110 
111 /* remove a buffer reference, destroy if the count is 0 */
buffer_rele(buffer_t * buffer)112 static __inline void buffer_rele(buffer_t *buffer) {
113 	assert(buffer->refcnt > 0);
114 	if (--buffer->refcnt == 0)
115 		buffer_destroy(buffer);
116 }
117 
118 /*
119  * find a buffer line by linenumber, this is slow, so don't use
120  * it unless it makes sense to.
121  */
buffer_lineat(buffer_t * buffer,int linenum,int * rlinenum)122 static __inline line_t *buffer_lineat(buffer_t *buffer, int linenum,
123 		int *rlinenum) {
124 	line_t *line;
125 	int num;
126 
127 	line = TAILQ_FIRST(&buffer->lines);
128 	for (num = 0; num < linenum; num++) {
129 		if (!TAILQ_NEXT(line, l_list))
130 			break;
131 		line = TAILQ_NEXT(line, l_list);
132 	}
133 	if (rlinenum)
134 		*rlinenum = num;
135 
136 	return line;
137 }
138 
139 /* remove dirty flags on all buffers */
buffer_cleanse()140 static __inline void buffer_cleanse() {
141 	buffer_t *buffer;
142 
143 	TAILQ_FOREACH(buffer, &buffer_list, b_list)
144 		buffer->dirty = 0;
145 }
146 
147 /* lookup a buffer by dispname */
buffer_lookup(u_char * name)148 static __inline buffer_t *buffer_lookup(u_char *name) {
149 	buffer_t *buffer;
150 
151 	TAILQ_FOREACH(buffer, &buffer_list, b_list)
152 		if (!strcmp(buffer->dispname, name))
153 			return buffer;
154 	return NULL;
155 }
156 
157 #endif
158