-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added arbark (arbitrary marks), these are marks designed for
developers who want to build plugins or embed neovim.
- Loading branch information
Showing
5 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Same function but names are not limited to one char | ||
* There is also no ex_command, just viml functions | ||
*/ | ||
|
||
#include "nvim/vim.h" | ||
#include "nvim/globals.h" | ||
#include "nvim/mark.h" | ||
#include "nvim/memory.h" | ||
#include "nvim/map.h" | ||
#include "nvim/lib/kvec.h" | ||
#include "nvim/editor/arbmark.h" | ||
|
||
# define FOR_ALL_ARBMARKS(buf) \ | ||
kbitr_t = itr; \ | ||
arbmark_T = *arbmark; \ | ||
for (;kb_itr_valid(&itr) \ | ||
;kb_itr_next(arbmark_T, buf->b_arbmarks_tree; &itr)){ | ||
|
||
static arbmark_T *_find_pos(pos_t pos, bool FORWARD); | ||
static int _arbmark_create(buf_T *buf, cstr_t *name, pos_T *pos); | ||
static int _arbmark_update(buf_T *buf, arbmark_T *arbmark, pos_T *pos); | ||
static int _arbmark_delete(buf_T *buf, cstr_t *name); | ||
static arbmark_T *get_arbmark(buf_T *buf, cstr_t *name); | ||
static int pos_lt(pos_T *pos, pos_T *pos2); | ||
static int pos_eq(pos_T *pos, pos_T *pos2); | ||
|
||
/* Create or update an arbmark, */ | ||
int arbmark_set(buf_T *buf, char_u *name, pos_T *pos) | ||
{ | ||
arbmark_T *arbmark = get_arbmark(buf, name); | ||
if (!arbmark){ | ||
return _arbmark_create(buf, name, pos); | ||
} | ||
else { | ||
return _arbmark_update(buf, arbmark, name, pos); | ||
} | ||
} | ||
|
||
/* Will fail silently on missing name */ | ||
int arbmark_unset(buf_T *buf, cstr_t *name){ | ||
arbmark_T *arbmark = get_arbmark(buf, name); | ||
if (!arbmark){ | ||
return FAIL; | ||
} | ||
return _arbmark_delete(buf, name); | ||
} | ||
|
||
/* Given a text position, finds the next mark */ | ||
arbmark_T *arbmark_next(pos_T pos){ | ||
arbmark_T *arbmark = _find_pos(pos, 1); | ||
return arbmark; | ||
} | ||
|
||
arbmark_T *arbmark_prev(pos_T pos){ | ||
arbmark_T *arbmark = _find_pos(pos, 0); | ||
return arbmark; | ||
} | ||
|
||
/* Get all mark names */ | ||
kvec_t(cstr_t) arbmark_names(char_u *name){ | ||
kvec_t(cstr_t) array; | ||
FOR_ALL_ARBMARKS{ | ||
arbmark = &kb_itr_key(arbmark_T, &itr); | ||
kv_push(array, arbmark->name); | ||
} | ||
return array; | ||
|
||
static arbmark_T *_find_pos(pos_t pos, bool FORWARD){ | ||
int found = -1; | ||
if (FORWARD == 1){ | ||
found = 1; | ||
} | ||
|
||
FOR_ALL_ARBMARKS{ | ||
arbmark = &kb_itr_key(arbmark_T, &itr); | ||
int cmp = kb_generic_cmp(pos, arbmark->fmark.mark); | ||
switch (cmp){ | ||
case !found: | ||
continue; | ||
case found: | ||
return arbmark.prev; | ||
case 0: | ||
return arbmark; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
static int _arbmark_create(buf_T *buf, cstr_t *name, pos_T *pos){ | ||
arbmark_T arbmark; | ||
/* fmark_T fmark; */ | ||
/* arbmark.fmark = fmark; */ | ||
kb_putp(arbmark_T, buf->b_arbmarks_tree, &arbmark); | ||
pmap_put(cstr_t)(buf->b_arbmarks, name, &arbmark); | ||
SET_FMARK(&buf->b_arbmarks, *pos, buf->b_fnum, *name); | ||
return OK; | ||
} | ||
|
||
static int _arbmark_update(buf_T *buf, arbmark_T *arbmark, pos_T *pos){ | ||
arbmark->mark.lnum = pos->lnum; | ||
arbmark->mark.col = pos->col; | ||
return OK; | ||
} | ||
|
||
static int _arbmark_delete(buf_T *buf, cstr_t *name){ | ||
pmap_del(ptr_t)(buf->b_arbmarks, name); | ||
return OK; | ||
} | ||
|
||
// TODO start with current buffer | ||
// TODO use a lru cache, | ||
buf_T *arbmark_buf_from_fnum(int fnum){ | ||
buf_T *buf; | ||
FOR_ALL_BUFFERS(buf){ | ||
if (fnum == buf->fnum){ | ||
return buf; | ||
} | ||
} | ||
return buf; | ||
} | ||
|
||
/* returns an arbmark from it's buffer*/ | ||
static arbmark_T *get_arbmark(buf_T *buf, cstr_t *name){ | ||
return pmap_get(arbmark_T)(buf->b_arbmarks, name); | ||
} | ||
|
||
int _pos_cmp(pos_T a, pos_T b){ | ||
if (pos_lt(a, b) == OK){ | ||
return -1; | ||
} | ||
else if (pos_eq(a, b) == OK){ | ||
return 0; | ||
} | ||
else { | ||
return 1; | ||
} | ||
} | ||
|
||
static int pos_lt(pos_T *pos, pos_T *pos2){ | ||
if (pos->lnum < pos2->lnum){ | ||
if (pos->col < pos2->col){ | ||
return OK; | ||
} | ||
} | ||
return FAIL; | ||
} | ||
|
||
static int pos_eq(pos_T *pos, pos_T *pos2){ | ||
if (pos->lnum == pos2->lnum){ | ||
if (pos->col == pos2->col){ | ||
return OK; | ||
} | ||
} | ||
return FAIL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef NVIM_ARBMARK_H | ||
#define NVIM_ARBMARK_H | ||
|
||
/* #include "nvim/vim.h" */ | ||
/* #include "nvim/lib/kvec.h" */ | ||
|
||
#define ARBMARK_MAXLEN 24 | ||
#define pos_cmp(a, b) (_pos_cmp((a).fmark.mark, (b).fmark.mark)) | ||
|
||
typedef struct { | ||
fmark_T fmark; | ||
fmark_T *next; | ||
fmark_T *prev; | ||
} arbmark_T; | ||
|
||
int arbmark_set(buf_T *buf, char_u *name, pos_T *pos); | ||
int arbmark_unset(buf_T, cstr_t *name); | ||
arbmark_T *arbmark_next(pos_T pos); | ||
arbmark_T *arbmark_prev(pos_T pos); | ||
kvec_t(cstr_t) arbmark_names(cstr_t *name); | ||
int _pos_cmp(pos_T a, pos_T b); | ||
buf_T *arbmark_buf_from_fnum(int fnum); | ||
|
||
/* #ifdef INCLUDE_GENERATED_DECLARATIONS */ | ||
/* # include "arbmark.h.generated.h" */ | ||
/* #endif */ | ||
#endif // NVIM_ARBMARK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- Sanity checks for buffer_* API calls via msgpack-rpc | ||
local helpers = require('test.functional.helpers')(after_each) | ||
local clear, nvim, buffer = helpers.clear, helpers.nvim, helpers.buffer | ||
local curbuf, curwin, eq = helpers.curbuf, helpers.curwin, helpers.eq | ||
local curbufmeths, ok = helpers.curbufmeths, helpers.ok | ||
local funcs = helpers.funcs | ||
|
||
describe('arbitrary marks set', function() | ||
it('works', function() | ||
curbuf('insert', -1, {'a', 'bit of', 'text'}) | ||
nvim('command', 'arb_mark haha 0 0') | ||
curwin('set_cursor', {0, 0}) | ||
curbuf('insert', -1, {'12345'}) | ||
pos = nvim('eval', 'arbmark_index(haha)') | ||
eq(pos, {0, 6}) | ||
end) | ||
end) | ||
|