void wordright(editor_t *ed, int select) { int pos, end, phase, next; update_selection(ed, select); pos = ed->linepos + ed->col; end = text_length(ed); next = next_line(ed, ed->linepos); phase = 0; while (pos < end) { int ch = get(ed, pos); if (phase == 0) { if (wordchar(ch)) phase = 1; } else { if (!wordchar(ch)) break; } pos++; if (pos == next) { ed->linepos = next; next = next_line(ed, ed->linepos); ed->line++; ed->refresh = 1; } } ed->col = pos - ed->linepos; if (ed->line >= ed->topline + ed->lines) { ed->toppos = next_line(ed, ed->toppos); ed->topline++; } ed->lastcol = ed->col; adjust(ed); }
void wordleft(editor_t *ed, int select) { int pos, phase; update_selection(ed, select); pos = ed->linepos + ed->col; phase = 0; while (pos > 0) { int ch = get(ed, pos - 1); if (phase == 0) { if (wordchar(ch)) phase = 1; } else { if (!wordchar(ch)) break; } pos--; if (pos < ed->linepos) { ed->linepos = prev_line(ed, ed->linepos); ed->line--; ed->refresh = 1; } } ed->col = pos - ed->linepos; if (ed->line < ed->topline) { ed->toppos = ed->linepos; ed->topline = ed->line; } ed->lastcol = ed->col; adjust(ed); }
int countWords (const char string[]) { int i, wordCount = 0; bool lookingForWord = true, wordchar (const char c), numchar (const char c); for ( i = 0; string[i] != '\0'; ++i ) if ( wordchar (string[i]) || numchar (string[i]) ) { if ( lookingForWord ) { ++wordCount; lookingForWord = false; } } else lookingForWord = true; return wordCount; }
int weditstr(WINDOW *win, char *buf, int field) { char org[MAXSTRLEN], *tp, *bp = buf; bool defdisp = TRUE, stop = FALSE, insert = FALSE; int cury, curx, begy, begx; chtype oldattr; WINDOW *wedit; int c = 0; if ((field >= MAXSTRLEN) || (buf == NULL) || ((int)strlen(buf) > field - 1)) return ERR; strcpy(org, buf); /* save original */ wrefresh(win); getyx(win, cury, curx); getbegyx(win, begy, begx); wedit = subwin(win, 1, field, begy + cury, begx + curx); oldattr = wedit->_attrs; colorbox(wedit, EDITBOXCOLOR, 0); keypad(wedit, TRUE); curs_set(1); while (!stop) { idle(); repainteditbox(wedit, bp - buf, buf); switch (c = wgetch(wedit)) { case ERR: break; case KEY_ESC: strcpy(buf, org); /* restore original */ stop = TRUE; break; case '\n': case KEY_UP: case KEY_DOWN: stop = TRUE; break; case KEY_LEFT: if (bp > buf) bp--; break; case KEY_RIGHT: defdisp = FALSE; if (bp - buf < (int)strlen(buf)) bp++; break; case '\t': /* TAB -- because insert is broken on HPUX */ case KEY_IC: /* enter insert mode */ case KEY_EIC: /* exit insert mode */ defdisp = FALSE; insert = !insert; curs_set(insert ? 2 : 1); break; default: if (c == erasechar()) /* backspace, ^H */ { if (bp > buf) { memmove((void *)(bp - 1), (const void *)bp, strlen(bp) + 1); bp--; } } else if (c == killchar()) /* ^U */ { bp = buf; *bp = '\0'; } else if (c == wordchar()) /* ^W */ { tp = bp; while ((bp > buf) && (*(bp - 1) == ' ')) bp--; while ((bp > buf) && (*(bp - 1) != ' ')) bp--; memmove((void *)bp, (const void *)tp, strlen(tp) + 1); } else if (isprint(c)) { if (defdisp) { bp = buf; *bp = '\0'; defdisp = FALSE; } if (insert) { if ((int)strlen(buf) < field - 1) { memmove((void *)(bp + 1), (const void *)bp, strlen(bp) + 1); *bp++ = c; } } else if (bp - buf < field - 1) { /* append new string terminator */ if (!*bp) bp[1] = '\0'; *bp++ = c; } } } } curs_set(0); wattrset(wedit, oldattr); repainteditbox(wedit, bp - buf, buf); delwin(wedit); return c; }
/* Move backwards/forwards from ptr (no longer than to start) in an * UTF8 text until the beginning of an extended token or (if space, * until beginning of the next/last word) * A token in this function means some combination of words linked * together with a single character of any of the configured set of * legal connector characters. * @return The number of bytes moved */ int SummaryDesc::complete_extended_token(unsigned char* start, ssize_t length, const unsigned char*& ptr, off_t increment) { int moved = 0; const unsigned char *old_ptr = NULL; for (;;) { // Start by moving to the start/end of the word.. moved += complete_word(start, length, ptr, increment); // Ensure that there is a quick way out of this at the end: if (start >= ptr || start + length <= ptr || ptr == old_ptr) return moved; // If we end up at the same place as last iteration, we need // to bail (done above) to avoid an infinite loop. old_ptr = ptr; // Store a pointer to the found break: const unsigned char* preptr = ptr; int prelen; // Position to previous/next character to check if this is a // "real" break: if (increment < 0) { prelen = Fast_UnicodeUtil::UTF8move(start, length, preptr, increment); if (!prelen) return moved; } else { prelen = 0; } // Handle default case ("ordinary" space) if (!word_connector(preptr)) { LOG(spam, "Not a word connector case (%c)", *preptr); return moved; } char wconn = *preptr; (void) wconn; LOG(spam, "Found word connector case candidate (%c)", wconn); // Read the character before/after the connector character: int addlen = Fast_UnicodeUtil::UTF8move(start, length, preptr, increment); if (!addlen) return moved; // Not possible to extend anything here // Only a single connector character that connects word // characters should lead us to include more words in the // normal sense: if (!wordchar(preptr)) return moved; // If a block of chinese data does not contain any spaces we have to return // here in order to avoid searching all the way to the start/end. return moved; // Ok, found a separator case, include another word.. moved += prelen + addlen; // If going forward, the word completer will look at the // previous char to see if we are at the start of a word, so // we have to move forward once here: if (increment > 0) { addlen = Fast_UnicodeUtil::UTF8move(start, length, preptr, increment); if (!addlen) return moved; moved += addlen; } ptr = preptr; LOG(spam, "Found proper word connector case (%c,%c) yet moved %d", wconn, *preptr, moved); } }