From: Andre Noll Date: Sun, 28 Aug 2011 21:36:17 +0000 (+0200) Subject: string: Introduce compute_word_num(). X-Git-Tag: v0.4.9~1^2~4 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=b5e5c97342576b3a5f2b09feb68b38abea5dad22;ds=sidebyside string: Introduce compute_word_num(). The completion code needs to determine the word the curser is currently on. Libreadline only provides the start and end position of the current word in the line buffer, but not the word number. This patch adds compute_word_num() to string.c which uses the same algorithm as create_argv() to determine the word boundaries. --- diff --git a/string.c b/string.c index cefb45d6..d226af96 100644 --- a/string.c +++ b/string.c @@ -711,6 +711,32 @@ out: return ret; } +/** + * Get the number of the word the cursor is on. + * + * \param buf The zero-terminated line buffer. + * \param delim Characters that separate words. + * \param point The cursor position. + * + * \return Zero-based word number. + */ +int compute_word_num(const char *buf, const char *delim, int point) +{ + int ret, num_words; + const char *p; + char *word; + + for (p = buf, num_words = 0; ; p += ret, num_words++) { + ret = get_next_word(p, delim, &word); + if (ret <= 0) + break; + free(word); + if (p + ret >= buf + point) + break; + } + return num_words; +} + /** * Free an array of words created by create_argv(). * diff --git a/string.h b/string.h index 06f4d3e6..23b79002 100644 --- a/string.h +++ b/string.h @@ -83,3 +83,4 @@ int create_argv(const char *buf, const char *delim, char ***result); void free_argv(char **argv); int para_regcomp(regex_t *preg, const char *regex, int cflags); void freep(void *arg); +int compute_word_num(const char *buf, const char *delim, int offset);