2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)emacs.c 8.1 (Berkeley) 6/4/93
40 * emacs.c: Emacs functions
45 /* em_delete_or_list():
46 * Delete character under cursor or list completions if at end of line
51 em_delete_or_list(el, c)
55 if (el->el_line.cursor == el->el_line.lastchar) { /* if I'm at the end */
56 if (el->el_line.cursor == el->el_line.buffer) { /* and the beginning */
57 term_overwrite(el, STReof, 4);/* then do a EOF */
62 /* Here we could list completions, but it is an error right now */
68 c_delafter(el, el->el_state.argument); /* delete after dot */
69 if (el->el_line.cursor > el->el_line.lastchar)
70 el->el_line.cursor = el->el_line.lastchar; /* bounds check */
76 /* em_delete_next_word():
77 * Cut from cursor to end of current word
82 em_delete_next_word(el, c)
88 if (el->el_line.cursor == el->el_line.lastchar)
91 cp = c__next_word(el->el_line.cursor, el->el_line.lastchar,
92 el->el_state.argument, ce__isword);
94 for (p = el->el_line.cursor, kp = el->el_chared.c_kill.buf; p < cp; p++)
97 el->el_chared.c_kill.last = kp;
99 c_delafter(el, cp - el->el_line.cursor); /* delete after dot */
100 if (el->el_line.cursor > el->el_line.lastchar)
101 el->el_line.cursor = el->el_line.lastchar; /* bounds check */
107 * Paste cut buffer at cursor position
110 protected el_action_t
118 if (el->el_chared.c_kill.last == el->el_chared.c_kill.buf)
121 if (el->el_line.lastchar +
122 (el->el_chared.c_kill.last - el->el_chared.c_kill.buf) >=
126 el->el_chared.c_kill.mark = el->el_line.cursor;
127 cp = el->el_line.cursor;
129 /* open the space, */
130 c_insert(el, el->el_chared.c_kill.last - el->el_chared.c_kill.buf);
132 for (kp = el->el_chared.c_kill.buf; kp < el->el_chared.c_kill.last; kp++)
135 /* if an arg, cursor at beginning else cursor at end */
136 if (el->el_state.argument == 1)
137 el->el_line.cursor = cp;
144 * Cut the entire line and save in cut buffer
147 protected el_action_t
155 cp = el->el_line.buffer;
156 kp = el->el_chared.c_kill.buf;
157 while (cp < el->el_line.lastchar)
158 *kp++ = *cp++; /* copy it */
159 el->el_chared.c_kill.last = kp;
160 el->el_line.lastchar = el->el_line.buffer; /* zap! -- delete all of it */
161 el->el_line.cursor = el->el_line.buffer;
167 * Cut area between mark and cursor and save in cut buffer
170 protected el_action_t
172 em_kill_region(el, c)
178 if (!el->el_chared.c_kill.mark)
181 if (el->el_chared.c_kill.mark > el->el_line.cursor) {
182 cp = el->el_line.cursor;
183 kp = el->el_chared.c_kill.buf;
184 while (cp < el->el_chared.c_kill.mark)
185 *kp++ = *cp++; /* copy it */
186 el->el_chared.c_kill.last = kp;
187 c_delafter(el, cp - el->el_line.cursor);
189 else { /* mark is before cursor */
190 cp = el->el_chared.c_kill.mark;
191 kp = el->el_chared.c_kill.buf;
192 while (cp < el->el_line.cursor)
193 *kp++ = *cp++; /* copy it */
194 el->el_chared.c_kill.last = kp;
195 c_delbefore(el, cp - el->el_chared.c_kill.mark);
196 el->el_line.cursor = el->el_chared.c_kill.mark;
203 * Copy area between mark and cursor to cut buffer
206 protected el_action_t
208 em_copy_region(el, c)
214 if (el->el_chared.c_kill.mark)
217 if (el->el_chared.c_kill.mark > el->el_line.cursor) {
218 cp = el->el_line.cursor;
219 kp = el->el_chared.c_kill.buf;
220 while (cp < el->el_chared.c_kill.mark)
221 *kp++ = *cp++; /* copy it */
222 el->el_chared.c_kill.last = kp;
225 cp = el->el_chared.c_kill.mark;
226 kp = el->el_chared.c_kill.buf;
227 while (cp < el->el_line.cursor)
228 *kp++ = *cp++; /* copy it */
229 el->el_chared.c_kill.last = kp;
235 /* em_gosmacs_traspose():
236 * Exchange the two characters before the cursor
237 * Gosling emacs transpose chars [^T]
239 protected el_action_t
240 em_gosmacs_traspose(el, c)
245 if (el->el_line.cursor > &el->el_line.buffer[1]) {
246 /* must have at least two chars entered */
247 c = el->el_line.cursor[-2];
248 el->el_line.cursor[-2] = el->el_line.cursor[-1];
249 el->el_line.cursor[-1] = c;
258 * Move next to end of current word
261 protected el_action_t
267 if (el->el_line.cursor == el->el_line.lastchar)
270 el->el_line.cursor = c__next_word(el->el_line.cursor, el->el_line.lastchar,
271 el->el_state.argument,
274 if (el->el_map.type == MAP_VI)
275 if (el->el_chared.c_vcmd.action & DELETE) {
284 * Uppercase the characters from cursor to end of current word
287 protected el_action_t
295 ep = c__next_word(el->el_line.cursor, el->el_line.lastchar,
296 el->el_state.argument, ce__isword);
298 for (cp = el->el_line.cursor; cp < ep; cp++)
299 if (islower((unsigned char)*cp))
300 *cp = toupper((unsigned char)*cp);
302 el->el_line.cursor = ep;
303 if (el->el_line.cursor > el->el_line.lastchar)
304 el->el_line.cursor = el->el_line.lastchar;
309 /* em_capitol_case():
310 * Capitalize the characters from cursor to end of current word
313 protected el_action_t
315 em_capitol_case(el, c)
321 ep = c__next_word(el->el_line.cursor, el->el_line.lastchar,
322 el->el_state.argument, ce__isword);
324 for (cp = el->el_line.cursor; cp < ep; cp++) {
325 if (isalpha((unsigned char)*cp)) {
326 if (islower((unsigned char)*cp))
327 *cp = toupper((unsigned char)*cp);
332 for (; cp < ep; cp++)
333 if (isupper((unsigned char)*cp))
334 *cp = tolower((unsigned char)*cp);
336 el->el_line.cursor = ep;
337 if (el->el_line.cursor > el->el_line.lastchar)
338 el->el_line.cursor = el->el_line.lastchar;
343 * Lowercase the characters from cursor to end of current word
346 protected el_action_t
354 ep = c__next_word(el->el_line.cursor, el->el_line.lastchar,
355 el->el_state.argument, ce__isword);
357 for (cp = el->el_line.cursor; cp < ep; cp++)
358 if (isupper((unsigned char)*cp))
359 *cp = tolower((unsigned char)*cp);
361 el->el_line.cursor = ep;
362 if (el->el_line.cursor > el->el_line.lastchar)
363 el->el_line.cursor = el->el_line.lastchar;
369 * Set the mark at cursor
372 protected el_action_t
378 el->el_chared.c_kill.mark = el->el_line.cursor;
383 /* em_exchange_mark():
384 * Exchange the cursor and mark
387 protected el_action_t
389 em_exchange_mark(el, c)
395 cp = el->el_line.cursor;
396 el->el_line.cursor = el->el_chared.c_kill.mark;
397 el->el_chared.c_kill.mark = cp;
401 /* em_universal_argument():
402 * Universal argument (argument times 4)
405 protected el_action_t
407 em_universal_argument(el, c)
410 { /* multiply current argument by 4 */
411 if (el->el_state.argument > 1000000)
413 el->el_state.doingarg = 1;
414 el->el_state.argument *= 4;
419 * Add 8th bit to next character typed
422 protected el_action_t
428 el->el_state.metanext = 1;
433 /* em_toggle_overwrite():
434 * Switch from insert to overwrite mode or vice versa
436 protected el_action_t
438 em_toggle_overwrite(el, c)
442 el->el_state.inputmode =
443 (el->el_state.inputmode == MODE_INSERT) ? MODE_REPLACE : MODE_INSERT;
448 /* em_copy_prev_word():
449 * Copy current word to cursor
451 protected el_action_t
453 em_copy_prev_word(el, c)
457 char *cp, *oldc, *dp;
459 if (el->el_line.cursor == el->el_line.buffer)
462 oldc = el->el_line.cursor;
463 /* does a bounds check */
464 cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
465 el->el_state.argument, ce__isword);
467 c_insert(el, oldc - cp);
468 for (dp = oldc; cp < oldc && dp < el->el_line.lastchar; cp++)
471 el->el_line.cursor = dp; /* put cursor at end */
477 /* em_inc_search_next():
478 * Emacs incremental next search
480 protected el_action_t
482 em_inc_search_next(el, c)
486 el->el_search.patlen = 0;
487 return ce_inc_search(el, ED_SEARCH_NEXT_HISTORY);
491 /* em_inc_search_prev():
492 * Emacs incremental reverse search
494 protected el_action_t
496 em_inc_search_prev(el, c)
500 el->el_search.patlen = 0;
501 return ce_inc_search(el, ED_SEARCH_PREV_HISTORY);