vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

text-motions.h

(5972B)


      1 #ifndef TEXT_MOTIONS_H
      2 #define TEXT_MOTIONS_H
      3 
      4 /* these functions all take a position in bytes from the start of the file,
      5  * perform a certain movement and return the new position. If the movement
      6  * is not possible the original position is returned unchanged. */
      7 
      8 #include <stddef.h>
      9 #include "text.h"
     10 #include "text-regex.h"
     11 
     12 size_t text_begin(Text*, size_t pos);
     13 size_t text_end(Text*, size_t pos);
     14 
     15 /* char refers to a grapheme (might skip over multiple Unicode codepoints) */
     16 size_t text_char_next(Text*, size_t pos);
     17 size_t text_char_prev(Text*, size_t pos);
     18 
     19 size_t text_codepoint_next(Text*, size_t pos);
     20 size_t text_codepoint_prev(Text*, size_t pos);
     21 
     22 /* find the given substring either in forward or backward direction.
     23  * does not wrap around at file start / end. If no match is found return
     24  * original position */
     25 size_t text_find_next(Text*, size_t pos, const char *s);
     26 size_t text_find_prev(Text*, size_t pos, const char *s);
     27 /* same as above but limit searched range to the line containing pos */
     28 size_t text_line_find_next(Text*, size_t pos, const char *s);
     29 size_t text_line_find_prev(Text*, size_t pos, const char *s);
     30 
     31 /*    begin            finish    next
     32  *    v                v         v
     33  *  \n      I am a line!       \n
     34  *  ^       ^                  ^
     35  *  prev    start              end
     36  */
     37 size_t text_line_prev(Text*, size_t pos);
     38 size_t text_line_begin(Text*, size_t pos);
     39 size_t text_line_start(Text*, size_t pos);
     40 size_t text_line_finish(Text*, size_t pos);
     41 size_t text_line_end(Text*, size_t pos);
     42 size_t text_line_next(Text*, size_t pos);
     43 size_t text_line_offset(Text*, size_t pos, size_t off);
     44 /* get grapheme count of the line upto `pos' */
     45 int text_line_char_get(Text*, size_t pos);
     46 /* get position of the `count' grapheme in the line containing `pos' */
     47 size_t text_line_char_set(Text*, size_t pos, int count);
     48 /* get display width of line upto `pos' */
     49 int text_line_width_get(Text*, size_t pos);
     50 /* get position of character being displayed at `width' in line containing `pos' */
     51 size_t text_line_width_set(Text*, size_t pos, int width);
     52 /* move to the next/previous grapheme on the same line */
     53 size_t text_line_char_next(Text*, size_t pos);
     54 size_t text_line_char_prev(Text*, size_t pos);
     55 /* move to the next/previous empty line */
     56 size_t text_line_empty_next(Text*, size_t pos);
     57 size_t text_line_empty_prev(Text*, size_t pos);
     58 /* move to start of next/previous blank line */
     59 size_t text_line_blank_next(Text*, size_t pos);
     60 size_t text_line_blank_prev(Text*, size_t pos);
     61 /* move to same offset in previous/next line */
     62 size_t text_line_up(Text*, size_t pos);
     63 size_t text_line_down(Text*, size_t pos);
     64 /* functions to iterate over all line beginnings in a given range */
     65 size_t text_range_line_first(Text*, Filerange*);
     66 size_t text_range_line_last(Text*, Filerange*);
     67 size_t text_range_line_next(Text*, Filerange*, size_t pos);
     68 size_t text_range_line_prev(Text*, Filerange*, size_t pos);
     69 /*
     70  * A longword consists of a sequence of non-blank characters, separated with
     71  * white space. TODO?: An empty line is also considered to be a word.
     72  * This is equivalent to a WORD in vim terminology.
     73  */
     74 size_t text_longword_end_next(Text*, size_t pos);
     75 size_t text_longword_end_prev(Text*, size_t pos);
     76 size_t text_longword_start_next(Text*, size_t pos);
     77 size_t text_longword_start_prev(Text*, size_t pos);
     78 /*
     79  * A word consists of a sequence of letters, digits and underscores, or a
     80  * sequence of other non-blank characters, separated with white space.
     81  * TODO?: An empty line is also considered to be a word.
     82  * This is equivalent to a word (lowercase) in vim terminology.
     83  */
     84 size_t text_word_end_next(Text*, size_t pos);
     85 size_t text_word_end_prev(Text*, size_t pos);
     86 size_t text_word_start_next(Text*, size_t pos);
     87 size_t text_word_start_prev(Text*, size_t pos);
     88 /*
     89  * More general versions of the above, define your own word boundaries.
     90  */
     91 size_t text_customword_start_next(Text*, size_t pos, int (*isboundary)(int));
     92 size_t text_customword_start_prev(Text*, size_t pos, int (*isboundary)(int));
     93 size_t text_customword_end_next(Text*, size_t pos, int (*isboundary)(int));
     94 size_t text_customword_end_prev(Text*, size_t pos, int (*isboundary)(int));
     95 /* TODO: implement the following semantics
     96  * A sentence is defined as ending at a '.', '!' or '?' followed by either the
     97  * end of a line, or by a space or tab.  Any number of closing ')', ']', '"'
     98  * and ''' characters may appear after the '.', '!' or '?' before the spaces,
     99  * tabs or end of line.  A paragraph and section boundary is also a sentence
    100  * boundary.
    101  */
    102 size_t text_sentence_next(Text*, size_t pos);
    103 size_t text_sentence_prev(Text*, size_t pos);
    104 /* TODO: implement the following semantics
    105  * A paragraph begins after each empty line. A section boundary is also a
    106  * paragraph boundary. Note that a blank line (only containing white space)
    107  * is NOT a paragraph boundary.
    108  */
    109 size_t text_paragraph_next(Text*, size_t pos);
    110 size_t text_paragraph_prev(Text*, size_t pos);
    111 /* A section begins after a form-feed in the first column.
    112 size_t text_section_next(Text*, size_t pos);
    113 size_t text_section_prev(Text*, size_t pos);
    114 */
    115 size_t text_block_start(Text*, size_t pos);
    116 size_t text_block_end(Text*, size_t pos);
    117 size_t text_parenthesis_start(Text*, size_t pos);
    118 size_t text_parenthesis_end(Text*, size_t pos);
    119 /* search corresponding '(', ')', '{', '}', '[', ']', '>', '<', '"', ''' */
    120 size_t text_bracket_match(Text*, size_t pos, const Filerange *limits);
    121 /* same as above but explicitly specify symbols to match */
    122 size_t text_bracket_match_symbol(Text*, size_t pos, const char *symbols, const Filerange *limits);
    123 
    124 /* search the given regex pattern in either forward or backward direction,
    125  * starting from pos. Does wrap around if no match was found. */
    126 size_t text_search_forward(Text *txt, size_t pos, Regex *regex);
    127 size_t text_search_backward(Text *txt, size_t pos, Regex *regex);
    128 
    129 /* is c a special symbol delimiting a word? */
    130 int is_word_boundary(int c);
    131 
    132 #endif