Skip to content

Commit

Permalink
Cleanup register implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Jan 30, 2016
1 parent 979ab79 commit f0e0c09
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
6 changes: 3 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1396,9 +1396,9 @@ static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
enum VisRegister regid;
keys = key2register(vis, keys, &regid);
Register *reg = vis_register_get(vis, regid);
if (reg)
vis_insert_key(vis, reg->data, reg->len);
size_t len;
const char *data = vis_register_get(vis, regid, &len);
vis_insert_key(vis, data, len);
return keys;
}

Expand Down
20 changes: 12 additions & 8 deletions register.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
#include "util.h"

void register_release(Register *reg) {
buffer_release((Buffer*)reg);
buffer_release(&reg->buf);
}

const char *register_get(Register *reg, size_t *len) {
*len = reg->buf.len;
return reg->buf.data;
}

bool register_put(Register *reg, Text *txt, Filerange *range) {
size_t len = range->end - range->start;
if (!buffer_grow((Buffer*)reg, len))
size_t len = text_range_size(range);
if (!buffer_grow(&reg->buf, len))
return false;
reg->len = text_bytes_get(txt, range->start, len, reg->data);
reg->buf.len = text_bytes_get(txt, range->start, len, reg->buf.data);
return true;
}

bool register_append(Register *reg, Text *txt, Filerange *range) {
size_t rem = reg->size - reg->len;
size_t len = range->end - range->start;
if (len > rem && !buffer_grow((Buffer*)reg, reg->size + len - rem))
size_t len = text_range_size(range);
if (!buffer_grow(&reg->buf, reg->buf.len + len))
return false;
reg->len += text_bytes_get(txt, range->start, len, reg->data + reg->len);
reg->buf.len += text_bytes_get(txt, range->start, len, reg->buf.data + reg->buf.len);
return true;
}
7 changes: 3 additions & 4 deletions register.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
#include <stddef.h>
#include <stdbool.h>
#include "buffer.h"
#include "text-util.h"

/* definition has to match Buffer */
typedef struct {
char *data; /* NULL if empty */
size_t len; /* current length of data */
size_t size; /* maximal capacity of the register */
Buffer buf;
bool linewise; /* place register content on a new line when inserting? */
} Register;

void register_release(Register *reg);
const char *register_get(Register *reg, size_t *len);
bool register_put(Register *reg, Text *txt, Filerange *range);
bool register_append(Register *reg, Text *txt, Filerange *range);

Expand Down
1 change: 1 addition & 0 deletions vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <setjmp.h>
#include "vis.h"
#include "register.h"
#include "text.h"
#include "text-regex.h"
#include "map.h"
Expand Down
7 changes: 5 additions & 2 deletions vis-operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ static size_t op_put(Vis *vis, Text *txt, OperatorContext *c) {
break;
}

size_t len;
const char *data = register_get(c->reg, &len);

for (int i = 0; i < c->count; i++) {
text_insert(txt, pos, c->reg->data, c->reg->len);
pos += c->reg->len;
text_insert(txt, pos, data, len);
pos += len;
}

if (c->reg->linewise) {
Expand Down
5 changes: 3 additions & 2 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,10 @@ void vis_register_set(Vis *vis, enum VisRegister reg) {
vis->action.reg = &vis->registers[reg];
}

Register *vis_register_get(Vis *vis, enum VisRegister reg) {
const char *vis_register_get(Vis *vis, enum VisRegister reg, size_t *len) {
if (reg < LENGTH(vis->registers))
return &vis->registers[reg];
return register_get(&vis->registers[reg], len);
*len = 0;
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ typedef struct Win Win;

#include "ui.h"
#include "view.h"
#include "register.h"

typedef struct {
void (*vis_start)(Vis*);
Expand Down Expand Up @@ -355,7 +354,8 @@ enum VisRegister {

/* set the register to use, if none is given the DEFAULT register is used */
void vis_register_set(Vis*, enum VisRegister);
Register *vis_register_get(Vis*, enum VisRegister);
/* get register content */
const char *vis_register_get(Vis*, enum VisRegister, size_t *len);

/* repeat last operator, possibly with a new count if one was provided in the meantime */
void vis_repeat(Vis*);
Expand Down

0 comments on commit f0e0c09

Please sign in to comment.