Skip to content

Commit

Permalink
text-object: add new functions to search for words
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Jul 26, 2015
1 parent 5b9b153 commit 5e01623
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions text-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <ctype.h>
#include "text-motions.h"
#include "text-objects.h"
Expand Down Expand Up @@ -165,6 +166,36 @@ Filerange text_object_word_outer(Text *txt, size_t pos) {
return r;
}

Filerange text_object_word_find_next(Text *txt, size_t pos, const char *word) {
size_t len = strlen(word);
for (;;) {
size_t match_pos = text_find_next(txt, pos, word);
if (match_pos != pos) {
Filerange match_word = text_object_word(txt, match_pos);
if (text_range_size(&match_word) == len)
return match_word;
pos = match_pos;
} else {
return text_range_empty();
}
}
}

Filerange text_object_word_find_prev(Text *txt, size_t pos, const char *word) {
size_t len = strlen(word);
for (;;) {
size_t match_pos = text_find_prev(txt, pos, word);
if (match_pos != pos) {
Filerange match_word = text_object_word(txt, match_pos);
if (text_range_size(&match_word) == len)
return match_word;
pos = match_pos;
} else {
return text_range_empty();
}
}
}

Filerange text_object_line(Text *txt, size_t pos) {
Filerange r;
r.start = text_line_begin(txt, pos);
Expand Down
3 changes: 3 additions & 0 deletions text-objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Filerange text_object_word(Text*, size_t pos);
/* includes trailing white spaces. if at pos happens to be a white space
* include all neighbouring leading white spaces and the following word. */
Filerange text_object_word_outer(Text*, size_t pos);
/* find next occurance of `word' (as word not substring) in forward/backward direction */
Filerange text_object_word_find_next(Text*, size_t pos, const char *word);
Filerange text_object_word_find_prev(Text*, size_t pos, const char *word);
/* same semantics as above but for a longword (i.e. delimited by white spaces) */
Filerange text_object_longword(Text*, size_t pos);
Filerange text_object_longword_outer(Text*, size_t pos);
Expand Down

0 comments on commit 5e01623

Please sign in to comment.