1 /* $Id: search.c,v 1.31 2004/03/23 16:44:02 ukai Exp $ */
9 set_mark(Line *l, int pos, int epos)
11 for (; pos < epos && pos < l->size; pos++)
12 l->propBuf[pos] |= PE_MARK;
16 /* Migemo: romaji --> kana+kanji in regexp */
17 static FILE *migemor = NULL, *migemow = NULL;
18 static int migemo_running;
19 static int migemo_pid = 0;
24 migemo_active = migemo_running = use_migemo;
29 migemor = migemow = NULL;
31 kill(migemo_pid, SIGKILL);
36 open_migemo(char *migemo_command)
38 migemo_pid = open_pipe_rw(&migemor, &migemow);
41 if (migemo_pid == 0) {
43 setup_child(FALSE, 2, -1);
44 myExec(migemo_command);
45 /* XXX: ifdef __EMX__, use start /f ? */
50 migemo_active = migemo_running = 0;
58 if (migemor == NULL || migemow == NULL)
59 if (open_migemo(migemo_command) == 0)
61 fprintf(migemow, "%s\n", conv_to_system(str));
63 if (fflush(migemow) != 0) {
71 tmp = Str_conv_from_system(Strfgets(migemor));
75 return conv_search_string(tmp->ptr, SystemCharset);
77 /* XXX: backend migemo is not working? */
79 migemo_active = migemo_running = 0;
82 #endif /* USE_MIGEMO */
85 /* normalize search string */
87 conv_search_string(char *str, wc_ces f_ces)
89 if (SearchConv && !WcOption.pre_conv &&
90 Currentbuf->document_charset != f_ces)
91 str = wtf_conv_fit(str, Currentbuf->document_charset);
97 forwardSearch(Buffer *buf, char *str)
99 char *p, *first, *last;
105 if (migemo_active > 0) {
106 if (((p = regexCompile(migemostr(str), IgnoreCase)) != NULL)
107 && ((p = regexCompile(str, IgnoreCase)) != NULL)) {
114 if ((p = regexCompile(str, IgnoreCase)) != NULL) {
118 l = buf->currentLine;
125 while (l->bpos && l->prev)
130 while (pos < l->size && l->propBuf[pos] & PC_WCHAR2)
133 if (pos < l->size && regexMatch(&l->lineBuf[pos], l->size - pos, 0) == 1) {
134 matchedPosition(&first, &last);
135 pos = first - l->lineBuf;
136 while (pos >= l->len && l->next && l->next->bpos) {
141 if (l != buf->currentLine)
142 gotoLine(buf, l->linenumber);
144 set_mark(l, pos, pos + last - first);
147 for (l = l->next;; l = l->next) {
149 if (buf->pagerSource) {
150 l = getNextPage(buf, 1);
152 if (WrapSearch && !wrapped) {
161 else if (WrapSearch) {
171 if (regexMatch(l->lineBuf, l->size, 1) == 1) {
172 matchedPosition(&first, &last);
173 pos = first - l->lineBuf;
174 while (pos >= l->len && l->next && l->next->bpos) {
179 buf->currentLine = l;
180 gotoLine(buf, l->linenumber);
182 set_mark(l, pos, pos + last - first);
183 return SR_FOUND | (wrapped ? SR_WRAPPED : 0);
185 if (wrapped && l == begin) /* no match */
192 backwardSearch(Buffer *buf, char *str)
194 char *p, *q, *found, *found_last, *first, *last;
200 if (migemo_active > 0) {
201 if (((p = regexCompile(migemostr(str), IgnoreCase)) != NULL)
202 && ((p = regexCompile(str, IgnoreCase)) != NULL)) {
209 if ((p = regexCompile(str, IgnoreCase)) != NULL) {
213 l = buf->currentLine;
220 while (l->bpos && l->prev)
227 while (pos > 0 && l->propBuf[pos] & PC_WCHAR2)
230 p = &l->lineBuf[pos];
234 while (regexMatch(q, &l->lineBuf[l->size] - q, q == l->lineBuf) == 1) {
235 matchedPosition(&first, &last);
240 if (q - l->lineBuf >= l->size)
244 while (q - l->lineBuf < l->size
245 && l->propBuf[q - l->lineBuf] & PC_WCHAR2)
252 pos = found - l->lineBuf;
253 while (pos >= l->len && l->next && l->next->bpos) {
258 if (l != buf->currentLine)
259 gotoLine(buf, l->linenumber);
261 set_mark(l, pos, pos + found_last - found);
265 for (l = l->prev;; l = l->prev) {
278 while (regexMatch(q, &l->lineBuf[l->size] - q, q == l->lineBuf) == 1) {
279 matchedPosition(&first, &last);
282 if (q - l->lineBuf >= l->size)
286 while (q - l->lineBuf < l->size
287 && l->propBuf[q - l->lineBuf] & PC_WCHAR2)
292 pos = found - l->lineBuf;
293 while (pos >= l->len && l->next && l->next->bpos) {
298 gotoLine(buf, l->linenumber);
300 set_mark(l, pos, pos + found_last - found);
301 return SR_FOUND | (wrapped ? SR_WRAPPED : 0);
303 if (wrapped && l == begin) /* no match */