9front - general discussion about 9front
 help / color / mirror / Atom feed
* mothra incremental regexp search
@ 2013-12-28  7:02 Nick Owens
  2013-12-28  7:15 ` [9front] " Nick Owens
  0 siblings, 1 reply; 2+ messages in thread
From: Nick Owens @ 2013-12-28  7:02 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 1933 bytes --]

here's a patch that's been on the backburner for a while. improvements and comments are appreciated.

diff -r 9098c8bcd4d3 sys/src/cmd/mothra/mothra.c
--- a/sys/src/cmd/mothra/mothra.c	Fri Nov 01 23:39:41 2013 +0100
+++ b/sys/src/cmd/mothra/mothra.c	Fri Dec 27 06:49:42 2013 -0800
@@ -9,6 +9,8 @@
 #include <plumb.h>
 #include <cursor.h>
 #include <panel.h>
+#include <regexp.h>
 #include "mothra.h"
 #include "rtext.h"
 int debug=0;		/* -d flag causes debug messages to appear in mothra.err */
@@ -86,6 +88,9 @@
 void hit3(int, int);
 void mothon(Www *, int);
 void killpix(Www *w);
+int incsearch(char *, int);
 char *buttons[]={
 	"alt display",
 	"moth mode",
@@ -691,6 +696,36 @@
 		}
 		save(urlget(selection, -1), s);
 		break;
+	case '/': /* begin incremental search */
+		s = arg(s);
+		if (s==0 || *s=='\0') {
+			if(!incsearch(s, 1))
+				message("no next match");
+		} else if(!incsearch(s, 0)) {
+			message("no match for %s", s);
+		}
+
+		break;
 	case 'q':
 		exits(0);
 	}
@@ -1154,3 +1189,137 @@
 		break;
 	}
 }
+
+int
+incsearch(char *what, int next)
+{
+	Rtext *tp;
+	static char *lastwhat;
+	static int lastidx;
+	Reprog *re;
+	int found, idx;
+
+	found = 0;
+	idx = 0;
+
+	if(next) {
+		what = lastwhat;
+	}
+
+	lastwhat = what;
+
+	if(current == nil || text == nil || what == nil || *what == '\0')
+		return 0;
+
+	re = regcompnl(what);
+	if(re){
+		for(tp=current->text;tp;tp=tp->next){
+			tp->flags &= ~PL_SEL;
+			idx++;
+			if(next && idx <= lastidx)
+				continue;
+
+			if(!tp->text || strlen(tp->text) <= 0)
+				continue;
+
+			if(regexec(re, tp->text, nil, 0)){
+				tp->flags |= PL_SEL;
+				found = 1;
+				current->yoffs=tp->topy;
+				plsetpostextview(text, current->yoffs);
+				flushimage(display, 1);
+				lastidx = idx;
+				message("match: %s", tp->text);
+				break;
+			}
+		}
+
+		free(re);
+	}
+
+	updtext(current);
+
+	// wrap around
+	if(!found)
+		lastidx = 0;
+
+	return found;
+}


[-- Attachment #2: Type: application/pgp-signature, Size: 834 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9front] mothra incremental regexp search
  2013-12-28  7:02 mothra incremental regexp search Nick Owens
@ 2013-12-28  7:15 ` Nick Owens
  0 siblings, 0 replies; 2+ messages in thread
From: Nick Owens @ 2013-12-28  7:15 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 396 bytes --]

i should note that for usage of this patch, you must use a space between / and the regular expression, e.g. '/ cat.*', otherwise it will be interpreted as a uri. to repeat a search, a single / without a space is sufficient.

On Fri, Dec 27, 2013 at 11:02:44PM -0800, Nick Owens wrote:
> here's a patch that's been on the backburner for a while. improvements and comments are appreciated.
> 

[-- Attachment #2: Type: application/pgp-signature, Size: 834 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-12-28  7:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-28  7:02 mothra incremental regexp search Nick Owens
2013-12-28  7:15 ` [9front] " Nick Owens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).