Skip to content

Commit

Permalink
vis: add basic infrastructure to store selections in registers
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Jun 15, 2017
1 parent 4b2513c commit f000d2f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct Mode {
};

typedef struct {
Array selections;
Array values;
bool linewise; /* place register content on a new line when inserting? */
bool append;
Expand Down
35 changes: 35 additions & 0 deletions vis-registers.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ static ssize_t read_buffer(void *context, char *data, size_t len) {
bool register_init(Register *reg) {
Buffer buf;
buffer_init(&buf);
array_init_sized(&reg->selections, sizeof(SelectionRegion));
array_init_sized(&reg->values, sizeof(Buffer));
return array_add(&reg->values, &buf);
}

void register_release(Register *reg) {
if (!reg)
return;
array_release(&reg->selections);
size_t n = array_capacity(&reg->values);
for (size_t i = 0; i < n; i++)
buffer_release(array_get(&reg->values, i));
Expand Down Expand Up @@ -239,6 +241,39 @@ const char *vis_register_slot_get(Vis *vis, enum VisRegister id, size_t slot, si
return NULL;
}

Array vis_register_selections_get(Vis *vis, enum VisRegister id) {
Array sel;
array_init_sized(&sel, sizeof(Filerange));
Register *reg = register_from(vis, id);
if (!reg)
return sel;
View *view = vis->win->view;
size_t len = array_length(&reg->selections);
array_reserve(&sel, len);
for (size_t i = 0; i < len; i++) {
SelectionRegion *sr = array_get(&reg->selections, i);
Filerange r = view_regions_restore(view, sr);
if (text_range_valid(&r))
array_add(&sel, &r);
}
//selections_normalize(&sel);
return sel;
}

void vis_register_selections_set(Vis *vis, enum VisRegister id, Array *sel) {
Register *reg = register_from(vis, id);
if (!reg)
return;
array_clear(&reg->selections);
View *view = vis->win->view;
for (size_t i = 0, len = array_length(sel); i < len; i++) {
SelectionRegion ss;
Filerange *r = array_get(sel, i);
if (view_regions_save(view, r, &ss))
array_add(&reg->selections, &ss);
}
}

const RegisterDef vis_registers[] = {
[VIS_REG_DEFAULT] = { '"', VIS_HELP("Unnamed register") },
[VIS_REG_ZERO] = { '0', VIS_HELP("Yank register") },
Expand Down
18 changes: 18 additions & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef struct Win Win;
#include "view.h"
#include "text-regex.h"
#include "libutf.h"
#include "array.h"

#ifndef CONFIG_HELP
#define CONFIG_HELP 1
Expand Down Expand Up @@ -754,6 +755,23 @@ const char *vis_register_slot_get(Vis*, enum VisRegister, size_t slot, size_t *l
/** Set register content. */
bool vis_register_put(Vis*, enum VisRegister, const char *data, size_t len);
bool vis_register_slot_put(Vis*, enum VisRegister, size_t slot, const char *data, size_t len);
/**
* Store a set of ``Filerange``s in a register.
*
* @param id The register to use.
* @param sel The array containing the file ranges.
*/
void vis_register_selections_set(Vis*, enum VisRegister id, Array *sel);
/**
* Get an array of file ranges stored in the register.
*
* @rst
* .. warning:: The caller must eventually free the Array by calling
* ``array_release``.
* @endrst
*/
Array vis_register_selections_get(Vis*, enum VisRegister id);

/**
* @}
* @defgroup vis_macros
Expand Down

0 comments on commit f000d2f

Please sign in to comment.