Skip to content

Latest commit

 

History

History
155 lines (118 loc) · 3.46 KB

alignr.asciidoc

File metadata and controls

155 lines (118 loc) · 3.46 KB

alignr.asciidoc

This script is a wrapper around the default selection primitives built into kakoune. It simplifies the process of aligning selections in buffers, and allows users to avoid manually selecting or splitting lines in order to align selections, by simply using a description (string) that will dictate how the selections will be aligned.

Note that the goal of this script isn’t to be entirely generic (complicated data structures will probably need to be selected using the usual primitives), but to save some brain cycle when dealing with simple constructs that we might want aligned without too much thinking.

Commands

alignr

This function takes a list of strings as arguments, and will process them in order, one after another. Unless a critical error occurs (such as an invalid syntax), the strings will all be processed. The role of the arguments of this function is to describe what data will be selected, and how they will be aligned with each other. Refer to the Examples section for a quick preview of how to use them.

Arguments syntax:

  • < or >: align direction (< = left, > = right, default left)

  • w: remove all whitespace from the selections prior to aligning (default no)

  • [0-9]: positive index of the selected or splitted group to keep

  • s or S: selection method (s = select, S = split)

  • .+: selection pattern (regular expression)

The above characters have to appear in order, one after the other, to form what is refered to as a "description string". Only the selection method as well as the pattern are mandatory, the others can be ignored (the default value will be used if that’s the case).

Examples

Variables declarations

Example 1

Data:

int i;
char path[512];
double d = 0x2A;

Command: %:alignr 2S\h

Result:

int    i;
char   path[512];
double d = 0x2A;

Example 2

Data:

int i; /* An iterator */
char path[512]; // Path to a file
double d = 0x2A;

Command: %:alignr 2S\h 1s/

Result:

int    i;         /* An iterator */
char   path[512]; // Path to a file
double d = 0x2A;

Example 3

Data:

int n; // simple declaration
char *s = "helo"; // pointer declaration
timer_t t;
struct foo_s const *f = NULL; // more complicated declaration
timer_t const *pt;

Command: %:alignr 1s\w+\s*[=\;] 1s/

Result:

int                 n;          // simple declaration
char *              s = "helo"; // pointer declaration
timer_t             t;
struct foo_s const *f = NULL;   // more complicated declaration
timer_t const *     pt;

Structure initializations

Example 1

Data:

{ "Hello World!", FOO, 0x1234 },
{ "kakoune", FOOBAR, 1234 },
{ NULL, -1, 0 },

Command: %s\{<ret><a-i>{:alignr S,

Result:

{ "Hello World!", FOO,    0x1234 },
{ "kakoune",      FOOBAR, 1234 },
{ NULL,           -1,     0 },

Example 2

Data:

{ "Hello World!", FOO, 0x1234 },
{ "kakoune", FOOBAR, 1234 },
{ NULL, -1, 0 },

Command: %s\{<ret><a-i>{:alignr >S,

Result:

{ "Hello World!",    FOO, 0x1234 },
{      "kakoune", FOOBAR,   1234 },
{           NULL,     -1,      0 },

Example 3

Data:

{ "Hello World!", FOO, 0x1234 },
{ "kakoune", FOOBAR, 1234 },
{ NULL, -1, 0 },

Command: %s\{<ret><a-i>{:alignr s,

Result:

{ "Hello World!", FOO   , 0x1234 },
{ "kakoune"     , FOOBAR, 1234 },
{ NULL          , -1    , 0 },

1 2 3 abc def ghi 45 67 89