expreg

Homepage: https://github.com/casouri/expreg

Author: Yuan Fu

Updated:

Summary

Simple expand region

Commentary

This is just like expand-region, but (1) we generate all regions at
once, and (2) should be easier to debug, and (3) we out-source
language-specific expansions to tree-sitter. Bind ‘expreg-expand’
and ‘expreg-contract’ and start using it.

Note that if point is in between two possible regions, we only keep
the region after point. In the example below, only region B is kept
(“|” represents point):

    (region A)|(region B)

Expreg also recognizes subwords if ‘subword-mode’ is on.

By default, the sentence expander ‘expreg--sentence’ is not
enabled. I suggest enabling it (by adding it to ‘expreg-functions’)
in text modes only.

TODO

- Support list/string in comment.

Developer

It works roughly as follows: ‘expreg-expand’ collects a list of
possible expansions on startup with functions in
‘expreg-functions’. Then it sorts them by each region’s size. It
also removes duplicates, etc. Then this list is stored in
‘expreg--next-regions’. (There could be better sorting algorithms,
but so far I haven’t seen the need for one.)

To expand, we pop a region from ‘expreg--next-regions’, set point
and mark accordingly, and push this region to
‘expreg--prev-regions’. So the head of ‘expreg--prev-regions’
should always equal the current region.

‘expreg-contract’ does just the opposite: it pops a region from
‘expreg--prev-regions’, push it to ‘expreg--next-regions’, and set
the current region to the head of ‘expreg--prev-regions’.

For better debugability, each region is of the form

    (FN . (BEG . END))

where FN is the function produced this region. So accessing BEG is
‘cadr’, accessing END is ‘cddr’. Sometimes FN is the function name
plus some further descriptions, eg, word, word--symbol,
word--within-space are all produced by ‘expreg--word’. I use double
dash to indicate the additional descriptor.

Credit: I stole a lot of ideas on how to expand lists and strings
from ‘expand-region’ :-)

Dependencies