📄 chared.c
字号:
/* $NetBSD: chared.c,v 1.22 2004/08/13 12:10:38 mycroft Exp $ *//*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Christos Zoulas of Cornell University. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include <config.h>/* * chared.c: Character editor utilities */#include <stdlib.h>#include "el.h"/* value to leave unused in line buffer */#define EL_LEAVE 2/* cv_undo(): * Handle state for the vi undo command */protected voidcv_undo(EditLine *el){ c_undo_t *vu = &el->el_chared.c_undo; c_redo_t *r = &el->el_chared.c_redo; int size; /* Save entire line for undo */ size = el->el_line.lastchar - el->el_line.buffer; vu->len = size; vu->cursor = el->el_line.cursor - el->el_line.buffer; memcpy(vu->buf, el->el_line.buffer, (size_t)size); /* save command info for redo */ r->count = el->el_state.doingarg ? el->el_state.argument : 0; r->action = el->el_chared.c_vcmd.action; r->pos = r->buf; r->cmd = el->el_state.thiscmd; r->ch = el->el_state.thisch;}/* cv_yank(): * Save yank/delete data for paste */protected voidcv_yank(EditLine *el, const char *ptr, int size){ c_kill_t *k = &el->el_chared.c_kill; memcpy(k->buf, ptr, size +0u); k->last = k->buf + size;}/* c_insert(): * Insert num characters */protected voidc_insert(EditLine *el, int num){ char *cp; if (el->el_line.lastchar + num >= el->el_line.limit) { if (!ch_enlargebufs(el, num +0u)) return; /* can't go past end of buffer */ } if (el->el_line.cursor < el->el_line.lastchar) { /* if I must move chars */ for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--) cp[num] = *cp; } el->el_line.lastchar += num;}/* c_delafter(): * Delete num characters after the cursor */protected voidc_delafter(EditLine *el, int num){ if (el->el_line.cursor + num > el->el_line.lastchar) num = el->el_line.lastchar - el->el_line.cursor; if (el->el_map.current != el->el_map.emacs) { cv_undo(el); cv_yank(el, el->el_line.cursor, num); } if (num > 0) { char *cp; for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++) *cp = cp[num]; el->el_line.lastchar -= num; }}/* c_delafter1(): * Delete the character after the cursor, do not yank */protected voidc_delafter1(EditLine *el){ char *cp; for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++) *cp = cp[1]; el->el_line.lastchar--;}/* c_delbefore(): * Delete num characters before the cursor */protected voidc_delbefore(EditLine *el, int num){ if (el->el_line.cursor - num < el->el_line.buffer) num = el->el_line.cursor - el->el_line.buffer; if (el->el_map.current != el->el_map.emacs) { cv_undo(el); cv_yank(el, el->el_line.cursor - num, num); } if (num > 0) { char *cp; for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++) *cp = cp[num]; el->el_line.lastchar -= num; }}/* c_delbefore1(): * Delete the character before the cursor, do not yank */protected voidc_delbefore1(EditLine *el){ char *cp; for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++) *cp = cp[1]; el->el_line.lastchar--;}/* ce__isword(): * Return if p is part of a word according to emacs */protected intce__isword(int p){ return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);}/* cv__isword(): * Return if p is part of a word according to vi */protected intcv__isword(int p){ if (isalnum(p) || p == '_') return 1; if (isgraph(p)) return 2; return 0;}/* cv__isWord(): * Return if p is part of a big word according to vi */protected intcv__isWord(int p){ return (!isspace(p));}/* c__prev_word(): * Find the previous word */protected char *c__prev_word(char *p, char *low, int n, int (*wtest)(int)){ p--; while (n--) { while ((p >= low) && !(*wtest)((unsigned char) *p)) p--; while ((p >= low) && (*wtest)((unsigned char) *p)) p--; } /* cp now points to one character before the word */ p++; if (p < low) p = low; /* cp now points where we want it */ return (p);}/* c__next_word(): * Find the next word */protected char *c__next_word(char *p, char *high, int n, int (*wtest)(int)){ while (n--) { while ((p < high) && !(*wtest)((unsigned char) *p)) p++; while ((p < high) && (*wtest)((unsigned char) *p)) p++; } if (p > high) p = high; /* p now points where we want it */ return (p);}/* cv_next_word(): * Find the next word vi style */protected char *cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int)){ int test; while (n--) { test = (*wtest)((unsigned char) *p); while ((p < high) && (*wtest)((unsigned char) *p) == test) p++; /* * vi historically deletes with cw only the word preserving the * trailing whitespace! This is not what 'w' does.. */ if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT)) while ((p < high) && isspace((unsigned char) *p)) p++; } /* p now points where we want it */ if (p > high) return (high); else return (p);}/* cv_prev_word(): * Find the previous word vi style */protected char *cv_prev_word(char *p, char *low, int n, int (*wtest)(int)){ int test; p--; while (n--) { while ((p > low) && isspace((unsigned char) *p)) p--; test = (*wtest)((unsigned char) *p); while ((p >= low) && (*wtest)((unsigned char) *p) == test) p--; } p++; /* p now points where we want it */ if (p < low) return (low); else return (p);}#ifdef notdef/* c__number(): * Ignore character p points to, return number appearing after that. * A '$' by itself means a big number; "$-" is for negative; '^' means 1. * Return p pointing to last char used. */protected char *c__number( char *p, /* character position */ int *num, /* Return value */ int dval) /* dval is the number to subtract from like $-3 */{ int i; int sign = 1; if (*++p == '^') { *num = 1; return (p); } if (*p == '$') { if (*++p != '-') { *num = 0x7fffffff; /* Handle $ */ return (--p); } sign = -1; /* Handle $- */ ++p; } for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0') continue; *num = (sign < 0 ? dval - i : i); return (--p);}#endif/* cv_delfini(): * Finish vi delete action */protected voidcv_delfini(EditLine *el){ int size; int action = el->el_chared.c_vcmd.action; if (action & INSERT) el->el_map.current = el->el_map.key; if (el->el_chared.c_vcmd.pos == 0) /* sanity */ return; size = el->el_line.cursor - el->el_chared.c_vcmd.pos; if (size == 0) size = 1; el->el_line.cursor = el->el_chared.c_vcmd.pos; if (action & YANK) { if (size > 0) cv_yank(el, el->el_line.cursor, size); else cv_yank(el, el->el_line.cursor + size, -size); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -