1 /* @(#)takecmds.c 1.31 09/07/09 Copyright 1984-2009 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static UConst char sccsid[] =
5 "@(#)takecmds.c 1.31 09/07/09 Copyright 1984-2009 J. Schilling";
6 #endif
7 /*
8 * Commands that deal with take buffers.
9 *
10 * Copyright (c) 1984-2009 J. Schilling
11 */
12 /*
13 * The contents of this file are subject to the terms of the
14 * Common Development and Distribution License, Version 1.0 only
15 * (the "License"). You may not use this file except in compliance
16 * with the License.
17 *
18 * See the file CDDL.Schily.txt in this distribution for details.
19 * A copy of the CDDL is also available via the Internet at
20 * http://www.opensource.org/licenses/cddl1.txt
21 *
22 * When distributing Covered Code, include this CDDL HEADER in each
23 * file and include the License file CDDL.Schily.txt from this distribution.
24 */
25
26 #include "ved.h"
27 #include "movedot.h"
28 extern char TAKEBUF[];
29
30 EXPORT void vcleartake __PR((ewin_t *wp));
31 LOCAL void wsave __PR((ewin_t *wp, epos_t start, epos_t size));
32 EXPORT void vlsave __PR((ewin_t *wp));
33 EXPORT void vpsave __PR((ewin_t *wp));
34 EXPORT void vcsave __PR((ewin_t *wp));
35 EXPORT void vwsave __PR((ewin_t *wp));
36 EXPORT void vssave __PR((ewin_t *wp));
37 EXPORT void vgetclr __PR((ewin_t *wp));
38 EXPORT void vget __PR((ewin_t *wp));
39 EXPORT void vsgetclr __PR((ewin_t *wp));
40 EXPORT void vsget __PR((ewin_t *wp));
41 EXPORT void vtname __PR((ewin_t *wp));
42 EXPORT void vwrtake __PR((ewin_t *wp));
43
44 /*
45 * Clear the curent take buffer
46 */
47 EXPORT void
vcleartake(wp)48 vcleartake(wp)
49 ewin_t *wp;
50 {
51 takesize = 0L;
52 writemsg(wp, "Buffer cleared");
53 }
54
55 /*
56 * Save 'size' characters from 'start' position into current take buffer
57 */
58 LOCAL void
wsave(wp,start,size)59 wsave(wp, start, size)
60 ewin_t *wp;
61 epos_t start;
62 epos_t size;
63 {
64 lseek(fdown(takefile), (off_t)takesize, SEEK_SET);
65 savefile(wp, start, start+size, takefile, TAKEBUF);
66 takesize += size;
67 }
68
69 /*
70 * Put from cursor to the end of the 'curnum-1' th line
71 * into the current take buffer
72 */
73 EXPORT void
vlsave(wp)74 vlsave(wp)
75 ewin_t *wp;
76 {
77 epos_t new;
78
79 if (wp->dot == wp->eof) {
80 ringbell();
81 } else {
82 new = forwline(wp, wp->dot, wp->curnum);
83 wsave(wp, wp->dot, (epos_t)(new-wp->dot));
84 wp->eflags &= ~SAVEDEL;
85 vkill(wp);
86 }
87 }
88
89 /*
90 * Put from cursor to the end of the 'curnum-1' th paragraph
91 * into the current take buffer
92 */
93 EXPORT void
vpsave(wp)94 vpsave(wp)
95 ewin_t *wp;
96 {
97 epos_t new;
98
99 if (wp->dot == wp->eof) {
100 ringbell();
101 } else {
102 new = forwpara(wp, wp->dot, wp->curnum);
103 wsave(wp, wp->dot, (epos_t)(new-wp->dot));
104 wp->eflags &= ~SAVEDEL;
105 vpkill(wp);
106 }
107 }
108
109 /*
110 * Put 'curnum' characters after the cursor into the current take buffer
111 */
112 EXPORT void
vcsave(wp)113 vcsave(wp)
114 ewin_t *wp;
115 {
116 if (wp->dot == wp->eof) {
117 ringbell();
118 } else {
119 if (wp->curnum + wp->dot > wp->eof)
120 wp->curnum = wp->eof - wp->dot;
121 wsave(wp, wp->dot, (epos_t)wp->curnum);
122 wp->eflags &= ~SAVEDEL;
123 vdel(wp);
124 }
125 }
126
127 /*
128 * Put 'curnum' words after the cursor into the current take buffer
129 */
130 EXPORT void
vwsave(wp)131 vwsave(wp)
132 ewin_t *wp;
133 {
134 epos_t new;
135
136 if (wp->dot == wp->eof) {
137 ringbell();
138 } else {
139 new = forwword(wp, wp->dot, wp->curnum);
140 wsave(wp, wp->dot, (epos_t)(new-wp->dot));
141 wp->eflags &= ~SAVEDEL;
142 vwdel(wp);
143 }
144 }
145
146 /*
147 * Put characters in selection (between cursor and mark)
148 * into the current take buffer
149 */
150 EXPORT void
vssave(wp)151 vssave(wp)
152 ewin_t *wp;
153 {
154 if (wp->dot > wp->mark)
155 wsave(wp, wp->mark, (epos_t)(wp->dot-wp->mark));
156 else
157 wsave(wp, wp->dot, (epos_t)(wp->mark-wp->dot));
158 wp->eflags &= ~SAVEDEL;
159 vskill(wp);
160 }
161
162 /*
163 * Insert the content of the current take buffer at cursor position,
164 * then clear the take buffer
165 */
166 EXPORT void
vgetclr(wp)167 vgetclr(wp)
168 ewin_t *wp;
169 {
170 vget(wp);
171 takesize = 0L;
172 }
173
174 /*
175 * Insert the content of the current take buffer at cursor position
176 */
177 EXPORT void
vget(wp)178 vget(wp)
179 ewin_t *wp;
180 {
181 getfile(wp, takefile, takesize, TAKEBUF);
182 }
183
184 /*
185 * Delete all characters between cursor and mark,
186 * then insert the content of the current take buffer at cursor position,
187 * then clear the take buffer
188 */
189 EXPORT void
vsgetclr(wp)190 vsgetclr(wp)
191 ewin_t *wp;
192 {
193 if (!wp->markvalid) {
194 nomarkmsg(wp);
195 } else {
196 vsget(wp);
197 takesize = 0L;
198 }
199 }
200
201 /*
202 * Delete all characters between cursor and mark,
203 * then insert the content of the current take buffer at cursor position
204 */
205 EXPORT void
vsget(wp)206 vsget(wp)
207 ewin_t *wp;
208 {
209 BOOL markfirst;
210 epos_t savesize = takesize;
211
212 if (!wp->markvalid) {
213 nomarkmsg(wp);
214 return;
215 }
216 markfirst = wp->dot >= wp->mark;
217
218 wp->eflags &= ~SAVEDEL;
219 vskill(wp);
220 update(wp);
221 vget(wp);
222
223 /*
224 * Now restore old position of cursor and mark
225 */
226 if (markfirst) {
227 setmark(wp, wp->dot);
228 wp->dot += savesize;
229 } else {
230 setmark(wp, wp->dot+savesize);
231 }
232 }
233
234 /*
235 * Set name of current take buffer
236 */
237 EXPORT void
vtname(wp)238 vtname(wp)
239 ewin_t *wp;
240 {
241 Uchar name[NAMESIZE];
242
243 if (! getcmdline(wp, name, sizeof (name), "Take Buffer: "))
244 return;
245 settakename(wp, name);
246 }
247
248 /*
249 * Write the content of the current take buffer into a file
250 */
251 EXPORT void
vwrtake(wp)252 vwrtake(wp)
253 ewin_t *wp;
254 {
255 FILE *file;
256 Uchar name[NAMESIZE];
257
258 if (! getcmdline(wp, name, sizeof (name), "\\ to file: "))
259 return;
260 if ((file = opensyserr(wp, name, "ctwub")) == NULL)
261 return;
262 fcopy(wp, takefile, file, takesize, TAKEBUF, "FILE");
263 fclose(file);
264 }
265