st

simple terminal with alpha focus highlight and selenized colorscheme
git clone https://git.beauhilton.com/st.git
Log | Files | Refs | README | LICENSE

normalMode.c (12577B)


      1 #include <X11/keysym.h>
      2 #include <X11/XKBlib.h>
      3 
      4 #include "normalMode.h"
      5 #include "utils.h"
      6 
      7 extern Glyph const styleSearch, style[];
      8 extern char const wDelS[], wDelL[], *nmKeys[];
      9 extern unsigned int bg[], fg, currentBg, highlightBg, highlightFg, amountNmKeys;
     10 
     11 typedef struct { int p[3]; } Pos;
     12 struct NormalModeState {
     13 	struct OperationState {
     14 		enum Op {noop=0, visual='v', visualLine='V', yank = 'y'} op;
     15 		enum Infix {infix_none=0, infix_i='i', infix_a='a'} infix;
     16 	} cmd;
     17 	struct MotionState {
     18 		uint32_t c; int active; Pos searchPos;
     19 		enum Search {none=0, fw='/', bw='?'} search;
     20 	} m;
     21 } defaultNormalMode, state;
     22 DynamicArray searchStr=UTF8_ARRAY, cCmd=UTF8_ARRAY, lCmd=UTF8_ARRAY;
     23 Glyph styleCmd;
     24 char posBuffer[10], brack[6][2] = { {"()"}, {"<>"}, {"{}"}, {"[]"}, {"\"\""}, {"''"}};
     25 int exited=1, overlay=1;
     26 static inline uint32_t cchar() { return term.line[term.c.y][term.c.x].u; }
     27 static inline int pos(int p, int h) {return IS_SET(MODE_ALTSCREEN)?p:rangeY(p+h*histOff-insertOff);}
     28 static inline int contains(char c, char const * values, uint32_t memSize) {
     29 	for (uint32_t i = 0; i < memSize; ++i) if (c == values[i]) return 1;
     30 	return 0;
     31 }
     32 static inline void decodeTo(char const *cs, int len, DynamicArray *darr) {
     33 	char *var = expand(darr);
     34 	if (!var) empty(darr); else utf8decode(cs, (Rune*)(var), len);
     35 }
     36 static inline void applyPos(Pos p) {
     37 	term.c.x = p.p[0], term.c.y = p.p[1];
     38 	if (!IS_SET(MODE_ALTSCREEN) && histOp) term.line = &buf[histOff = p.p[2]];
     39 }
     40 /// Find string in history buffer, and provide string-match-lookup for highlighting matches
     41 static int highlighted(int x, int y) {
     42 	int const s=term.row*term.col, i=y*term.col+x, sz=size(&searchStr);
     43 	return sz && i<s && mark[i]!=sz && i+mark[i]<s && !mark[i+mark[i]];
     44 }
     45 static void markSearchMatches(int all) {
     46 	int sz = size(&searchStr), ox = 0, oy = 0, oi=0;
     47 	for (int y=0; sz && all && y<term.row; ++y)
     48 		for (int x=0; x<term.col; ++x) term.dirty[y] |= highlighted(x, y);
     49 	for (int y = 0, wi=0, owi=0, i=0; sz && y < term.row; ++y)
     50 		for (int x=0; x<term.col; ++x, wi%=sz, ++i, owi=wi)
     51 			if (all || term.dirty[y]) {
     52 				mark[i]=sz-(wi=(getU32(&searchStr,wi,1)==term.line[y][x].u?wi+1:0));
     53 				if (wi==1) ox=x, oy=y, oi=i; else if (!wi && owi) x=ox, y=oy, i=oi;
     54 			}
     55 	for (int y=0; sz &&all &&y<term.row; ++y)
     56 		for (int x=0; x<term.col; ++x) term.dirty[y] |= highlighted(x, y);
     57 }
     58 static int findString(int8_t s, int all) {
     59 	Pos p = (Pos) {.p={term.c.x, term.c.y, IS_SET(MODE_ALTSCREEN) ? 0 : histOff}};
     60 	historyMove(s, 0, 0);
     61 	uint32_t strSz=size(&searchStr), maxIter=rows()*term.col+strSz, widx=0;
     62 	for (uint32_t i=0, wi = 0; widx<strSz && ++i<=maxIter; historyMove(s, 0, 0), wi=widx) {
     63 		widx = (getU32(&searchStr, widx, s>0)==cchar())?widx+1:0;
     64 		if (wi && !widx) historyMove(-s*wi, 0, 0);
     65 	}
     66 	if (widx == strSz && widx) historyMove(-s * strSz, 0, 0);
     67 	else applyPos(p);
     68 	markSearchMatches(all);
     69 	return widx == strSz;
     70 }
     71 /// Execute series of normal-mode commands from char array / decoded from dynamic array
     72 static ExitState pressKeys(char const* s, size_t e) {
     73 	ExitState x=succ;
     74 	for (size_t i=0; i<e && (x=(!s[i] ? x : kpressHist(&s[i], 1, 0, NULL))); ++i);
     75 	return x;
     76 }
     77 static ExitState executeCommand(uint32_t *c, size_t z) {
     78 	ExitState x=succ;
     79 	char dc [32];
     80 	for (size_t i=0; i<z && (x=kpressHist(dc,utf8encode(c[i],dc),0,NULL));++i);
     81 	return x;
     82 }
     83 /// Get character for overlay, if the overlay (st) has something to show, else normal char.
     84 static void getChar(DynamicArray *st, Glyph *glyphChange, int y, int xEnd, int width, int x) {
     85 	if (x < xEnd - min(width=min(width,xEnd), size(st))) *glyphChange = term.line[y][x];
     86 	else if (x<xEnd) glyphChange->u = *((Rune*)(st->content + (size(st)+x-xEnd)*st->elSize));
     87 }
     88 /// Expand "infix" expression: for instance (w =>)       l     b     |   | v     e    |   | y
     89 static ExitState expandExpression(char c) { //    ({ =>)       l  ?  {  \n | l | v  /  } \n | h | y
     90 	int a=state.cmd.infix==infix_a, yank=state.cmd.op=='y', lc=tolower(c), found=1;
     91 	state.cmd.infix = infix_none;
     92 	if(!yank && state.cmd.op!=visual && state.cmd.op!=visualLine) return failed;
     93 	char mot[11] = {'l', 0, 'b', 0, 0, 'v', 0, 'e', 0, 0, yank ? 'y' : 0};
     94 	if (lc == 'w') mot[2] = 'b' - lc + c, mot[7] = (a ? 'w' : 'e') - lc + c, mot[9]=a?'h':0;
     95 	else {
     96 		mot[1]='?', mot[3]=mot[8]='\n', mot[6]='/', mot[4]=a?0:'l', mot[9]=a?0:'h';
     97 		for (int i=found=0; !found && i < 6; ++i)
     98 			if ((found=contains(c,brack[i],2))) mot[2]=brack[i][0], mot[7]=brack[i][1];
     99 	}
    100 	if (!found) return failed;
    101 	assign(&lCmd, &cCmd);
    102 	empty(&cCmd);
    103 	state.cmd = defaultNormalMode.cmd;
    104 	return pressKeys(mot, 11);
    105 }
    106 
    107 int executeMotion(char const cs, int len, KeySym const *const ks) {
    108 	state.m.c = max(state.m.c, 1);
    109 	if      (ks && *ks == XK_d) historyMove(0, 0, term.row / 2);
    110 	else if (ks && *ks == XK_u) historyMove(0, 0, -term.row / 2);
    111 	else if (ks && *ks == XK_f) historyMove(0, 0, term.row-1+(term.c.y=0));
    112 	else if (ks && *ks == XK_b) historyMove(0, 0, -(term.c.y=term.row-1));
    113 	else if (ks && *ks == XK_h) overlay = !overlay;
    114 	else if (!len) return failed;
    115 	else if (cs == 'K') historyMove(0, 0, -state.m.c);
    116 	else if (cs == 'J') historyMove(0, 0,  state.m.c);
    117 	else if (cs == 'k') historyMove(0, -state.m.c, 0);
    118 	else if (cs == 'j') historyMove(0,  state.m.c, 0);
    119 	else if (cs == 'h') historyMove(-state.m.c, 0, 0);
    120 	else if (cs == 'l') historyMove( state.m.c, 0, 0);
    121 	else if (cs == 'H') term.c.y = 0;
    122 	else if (cs == 'M') term.c.y = term.bot / 2;
    123 	else if (cs == 'L') term.c.y = term.bot;
    124 	else if (cs == 's' || cs == 'S') altToggle = cs == 's' ? !altToggle : 1;
    125 	else if (cs == 'G' || cs == 'g') {
    126 		if (cs == 'G') term.c = c[0] = c[IS_SET(MODE_ALTSCREEN)+1];
    127 		if (!IS_SET(MODE_ALTSCREEN)) term.line = &buf[histOff=insertOff];
    128 	} else if (cs == '0') term.c.x = 0;
    129 	else if (cs == '$') term.c.x = term.col-1;
    130 	else if (cs == 't') sel.type = sel.type==SEL_REGULAR ? SEL_RECTANGULAR : SEL_REGULAR;
    131 	else if (cs == 'n' || cs == 'N') {
    132 		int const d = ((cs=='N')!=(state.m.search==bw))?-1:1;
    133 		for (int i = state.m.c; i && findString(d, 0); --i);
    134 	} else if (contains(cs, "wWeEbB", 6)) {
    135 		int const low=cs<=90, off=tolower(cs)!='w', sgn=(tolower(cs)=='b')?-1:1,
    136 		          l=strlen(wDelL), s=strlen(wDelS), mit=rows()*term.col;
    137 		for (int it=0, on=0; state.m.c > 0; ++it) {
    138 			if (off || it) if (!historyMove(sgn, 0, 0)) it = mit;        //< offset move
    139 			int n = 1<<(contains(cchar(),wDelS,s) ?(2-low) :!contains(cchar(),wDelL,l)),
    140 			    found = (on|=n)^n && ((off ?on^n :n)!=1); //< state change &letter state
    141 			if (found && off) historyMove(-sgn, 0, 0);       //< offset move if required
    142 			if (found || it>mit) it=-1, on=0, --state.m.c;       //< terminate iteration
    143 		}
    144 	} else return failed;
    145 	state.m.c = 0;
    146 	return state.cmd.op == yank ? exitMotion : succ;
    147 }
    148 
    149 ExitState kpressHist(char const *cs, int len, int ctrl, KeySym const *ksym) {
    150 	historyOpToggle(1, 1);
    151 	int const prevYOff=IS_SET(MODE_ALTSCREEN)?0:histOff, search=state.m.search&&state.m.active,
    152 	          prevAltToggle=altToggle, prevOverlay=overlay;
    153 	int const noOp=!state.cmd.op&&!state.cmd.infix, num=len==1&&BETWEEN(cs[0],48,57),
    154 	          esc=ksym&&*ksym==XK_Escape, ret=(ksym&&*ksym==XK_Return)||(len==1&&cs[0]=='\n'),
    155 	          quantifier=num&&(cs[0]!='0'||state.m.c), ins=!search &&noOp &&len &&cs[0]=='i';
    156 	exited = 0;
    157 	ExitState result = succ;
    158 	if (esc || ret || ins) { result = exitMotion, len = 0;
    159 	} else if (ksym && *ksym == XK_BackSpace) {
    160 		if ((search || state.m.c) && size(&cCmd)) pop(&cCmd);
    161 		if (search) {
    162 			if (size(&searchStr)) pop(&searchStr);
    163 			else result = exitMotion;
    164 			if (!size(&searchStr)) tfulldirt();
    165 			applyPos(state.m.searchPos);
    166 			findString(state.m.search==fw ? 1 : -1, 1);
    167 		} else if (state.m.c) state.m.c /= 10;
    168 		len = 0;
    169 	} else if (search) {
    170 		if (len >= 1) decodeTo(cs, len, &searchStr);
    171 		applyPos(state.m.searchPos);
    172 		findString(state.m.search==fw ? 1 : -1, 1);
    173 	} else if (len == 0) { result = failed;
    174 	} else if (quantifier) { state.m.c = min(SHRT_MAX, state.m.c*10+cs[0]-48);
    175 	} else if (state.cmd.infix && state.cmd.op && (result = expandExpression(cs[0]), len=0)) {
    176 	} else if (cs[0] == '.') {
    177 		if (size(&cCmd)) assign(&lCmd, &cCmd);
    178 		empty(&cCmd);
    179 		executeCommand((uint32_t*) lCmd.content, size(&lCmd));
    180 		empty(&cCmd);
    181 		len = 0;
    182 	} else if (cs[0] == 'r') { tfulldirt();
    183 	} else if (cs[0] == 'c') {
    184 		empty(&lCmd);
    185 		empty(&cCmd);
    186 		empty(&searchStr);
    187 		tfulldirt();
    188 		len = 0;
    189 	} else if (cs[0] == fw || cs[0] == bw) {
    190 		empty(&searchStr);
    191 		state.m.search = cs[0];
    192 		state.m.searchPos = (Pos){.p={term.c.x, term.c.y, prevYOff}};
    193 		state.m.active = 1;
    194 	} else if (cs[0]==infix_i || cs[0]==infix_a) { state.cmd.infix=cs[0];
    195 	} else if (cs[0] == 'y') {
    196 		if (state.cmd.op) {
    197 			result = (state.cmd.op == yank) ? exitOp : exitMotion;
    198 			if (state.cmd.op == yank) selstart(0, term.c.y, 0);
    199 		} else selstart(term.c.x, term.c.y, 0);
    200 		state.cmd.op = yank;
    201 	} else if (cs[0] == visual || cs[0] == visualLine) {
    202 		if (state.cmd.op != (unsigned char) cs[0]) {
    203 			state.cmd = defaultNormalMode.cmd;
    204 			state.cmd.op = cs[0];
    205 			selstart(cs[0] == visualLine ?0 :term.c.x, term.c.y, 0);
    206 		} else result = exitOp;
    207 	} else if (!(result =executeMotion(len?cs[0]:0, len, ctrl?ksym:NULL))) {
    208 		result=failed;
    209 		for (size_t i = 0; !ctrl && i < amountNmKeys; ++i)
    210 			if (cs[0]==nmKeys[i][0] &&
    211 			   failed!=(result=pressKeys(&nmKeys[i][1], strlen(nmKeys[i])-1))) goto end;
    212 	} // Operation/Motion finished if valid: update cmd string, extend selection, update search
    213 	if (result != failed) {
    214 		if (len == 1 && !ctrl) decodeTo(cs, len, &cCmd);
    215 		if ((state.cmd.op == visualLine) || ((state.cmd.op == yank) && (result == exitOp))) {
    216 			int const off = pos(term.c.y, 1) < pos(sel.ob.y, 0);
    217 			sel.ob.x = off ? term.col - 1 : 0;
    218 			selextend(off ? 0 : term.col-1, term.c.y, sel.type, 0);
    219 		} else if (sel.oe.x != -1) selextend(term.c.x, term.c.y, sel.type, 0);
    220 	} // Set repaint for motion or status bar
    221 	if (!IS_SET(MODE_ALTSCREEN) && prevYOff != histOff) tfulldirt();
    222 	// Terminate Motion / operation if thus indicated
    223 	if (result == exitMotion) {
    224 		if (!state.m.active) result = (exited=noOp) ? finished : exitOp;
    225 		state.m.active = state.m.c = 0;
    226 	}
    227 	if (result == exitOp || result == finished) {
    228 		if (state.cmd.op == yank) {
    229 			xsetsel(getsel());
    230 			xclipcopy();
    231 		}
    232 		state = defaultNormalMode;
    233 		selclear();
    234 		if (!esc) assign(&lCmd, &cCmd);
    235 		empty(&cCmd);
    236 	} // Update the content displayed in the history overlay
    237 	styleCmd = style[state.cmd.op==yank ? 1 : (state.cmd.op==visual ? 2 :
    238 	                (state.cmd.op==visualLine ? 3 :0))];
    239 	int const posLin = !IS_SET(MODE_ALTSCREEN) ? rangeY(insertOff-histOff):0, h=rows()-term.row;
    240 	if (!posLin || posLin==h || !h) strcpy(posBuffer, posLin ? " [BOT] " : " [TOP] ");
    241 	else sprintf(posBuffer, " % 3d%c  ", min(100, max(0, .5 + posLin * 100. / h)),'%');
    242 	if ((overlay || overlay!=prevOverlay) && term.col>9 && term.row>4) {
    243 		if (!term.dirty[term.row-1]) xdrawline(term.line[term.row-1], term.col*2/3, term.row-1, term.col-1);
    244 		if (!term.dirty[term.row-2]) xdrawline(term.line[term.row-2], term.col*2/3, term.row-2, term.col-1);
    245 	}
    246 	if (result==finished) altToggle = 0;
    247 	if (altToggle != prevAltToggle) tswapscreen();
    248 end:
    249 	historyOpToggle(-1, 1);
    250 	return result;
    251 }
    252 
    253 void historyOverlay(int x, int y, Glyph* g) {
    254 	if (!histMode) return;
    255 	TCursor const *cHist = histOp ? &term.c : &c[0];
    256 	if(overlay && term.col > 9 && term.row > 4 && (x > (2*term.col/3)) && (y >= (term.row-2))) {
    257 		*g = (y == term.row - 2) ? styleSearch : styleCmd;
    258 		if (y == term.row-2) getChar(&searchStr, g, term.row-2, term.col-2, term.col/3, x);
    259 		else if (x > term.col - 7) g->u = posBuffer[x - term.col + 7];
    260 		else getChar(size(&cCmd) ?&cCmd :&lCmd, g, term.row-1, term.col-7, term.col/3-6, x);
    261 	} else if (highlighted(x, y)) g->bg = highlightBg, g->fg = highlightFg;
    262 	else if ((x==cHist->x) ^ (y==cHist->y)) g->bg = currentBg;
    263 	else if (x==cHist->x) g->mode^=ATTR_REVERSE;
    264 }
    265 void historyPreDraw() {
    266 	static Pos op = {.p={0, 0, 0}};
    267 	historyOpToggle(1, 0);
    268 	// Draw the cursor cross if changed
    269 	if (term.c.y >= term.row || op.p[1] >= term.row) tfulldirt();
    270 	else if (exited || (op.p[1] != term.c.y)) term.dirty[term.c.y] = term.dirty[op.p[1]] = 1;
    271 	for (int i=0; (exited || term.c.x != op.p[0]) && i<term.row; ++i) if (!term.dirty[i]) {
    272 		xdrawline(term.line[i], term.c.x, i, term.c.x + 1);
    273 		xdrawline(term.line[i], op.p[0], i, op.p[0] + 1);
    274 	}
    275 	// Update search results either only for lines with new content or all results if exiting
    276 	markSearchMatches(exited);
    277 	op = (Pos){.p = {term.c.x, term.c.y, 0}};
    278 	historyOpToggle(-1, 0);
    279 }