summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
408458a)
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.
+/**
+ * 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().
*
/**
* Free an array of words created by create_argv().
*
void free_argv(char **argv);
int para_regcomp(regex_t *preg, const char *regex, int cflags);
void freep(void *arg);
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);