Plan 9 from Bell Labs’s /usr/web/sources/contrib/quanstro/src/9term-nocolor/look.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <draw.h>
#include <keyboard.h>
#include <cursor.h>
#include <mouse.h>
#include <frame.h>
#include <thread.h>
#include "dat.h"
#include "fns.h"

extern Window* w;

static Rune*
xrunestrrchr(Rune *s, Rune* endp, Rune c)
{
	Rune *r;

	r = endp;
	while(r >= s && c != *r)
		r--;

	if(c != *r)
		return 0;

	return r;
}

static Rune* 
xrunestrrstr(Rune*s, Rune* endp, Rune* c, long len){
	Rune* r;

	for(r = endp-1; r>=s; r--){
		r = xrunestrrchr(s,r,*c);
		if(0 == r)
			return 0;
		if(0 ==runestrncmp(r,c,len))
			return r;
	}
	return 0;
}

void
wlook(Window* w, int reverse)
{
	Rune* match;

	/* save lookbuffer first, if not current selection */
	if(w->q1 == w->q0){
		if(0==nlook)
			return;
	} else {
		nlook = w->q1-w->q0;
		look = runerealloc(look, nlook + 1);
		memcpy(look, w->r+w->q0, nlook*sizeof(Rune));
		look[nlook] = 0;
	}
	
	/* search */
	if(0 == reverse){
		match = runestrstr(w->r+w->q1, look);
		if(0 == match)
			match = runestrstr(w->r, look);
	} else {
		match = xrunestrrstr(w->r,w->r+w->q0, look, nlook);
		if(0 == match)
			match = xrunestrrstr(w->r, w->r+w->maxr, look, nlook);
	}

	if(0==match)
		return;
	
	w->q0=match-w->r;
	w->q1=w->q0+nlook;
	/* assert w->q1 < w->endrune */
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].