9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Nick Owens <mischief@9.offblast.org>
To: 9front@9front.org
Subject: mothra incremental regexp search
Date: Fri, 27 Dec 2013 23:02:44 -0800	[thread overview]
Message-ID: <20131228070244.GB31476@iota.offblast.org> (raw)

[-- 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 --]

             reply	other threads:[~2013-12-28  7:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-28  7:02 Nick Owens [this message]
2013-12-28  7:15 ` [9front] " Nick Owens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20131228070244.GB31476@iota.offblast.org \
    --to=mischief@9.offblast.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).