ops.c: breakout shared register type formatting code

This commit is contained in:
Björn Linse 2016-02-23 20:13:09 +01:00
parent 2359f6f144
commit c1487b9685
3 changed files with 47 additions and 34 deletions

View File

@ -10623,8 +10623,6 @@ static void f_getregtype(typval_T *argvars, typval_T *rettv)
{
char_u *strregname;
int regname;
char_u buf[NUMBUFLEN + 2];
long reglen = 0;
if (argvars[0].v_type != VAR_UNKNOWN) {
strregname = get_tv_string_chk(&argvars[0]);
@ -10641,18 +10639,13 @@ static void f_getregtype(typval_T *argvars, typval_T *rettv)
if (regname == 0)
regname = '"';
buf[0] = NUL;
buf[1] = NUL;
switch (get_reg_type(regname, &reglen)) {
case MLINE: buf[0] = 'V'; break;
case MCHAR: buf[0] = 'v'; break;
case MBLOCK:
buf[0] = Ctrl_V;
sprintf((char *)buf + 1, "%" PRId64, (int64_t)(reglen + 1));
break;
}
colnr_T reglen = 0;
char buf[NUMBUFLEN + 2];
char_u reg_type = get_reg_type(regname, &reglen);
format_reg_type(reg_type, reglen, buf, ARRAY_SIZE(buf));
rettv->v_type = VAR_STRING;
rettv->vval.v_string = vim_strsave(buf);
rettv->vval.v_string = (char_u *)xstrdup(buf);
}
/*

View File

@ -2558,23 +2558,7 @@ static void yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
// the register type
char buf[NUMBUFLEN+2];
buf[0] = NUL;
buf[1] = NUL;
switch (reg->y_type) {
case MLINE:
buf[0] = 'V';
break;
case MCHAR:
buf[0] = 'v';
break;
case MBLOCK:
buf[0] = Ctrl_V;
snprintf(buf + 1, ARRAY_SIZE(buf) - 1, "%" PRId64,
(int64_t)(reg->y_width + 1));
break;
case MAUTO:
assert(false);
}
format_reg_type(reg->y_type, reg->y_width, buf, ARRAY_SIZE(buf));
dict_add_nr_str(dict, "regtype", 0, (char_u *)buf);
// name of requested register or the empty string for an unnamed operation.
@ -4703,7 +4687,7 @@ theend:
* Used for getregtype()
* Returns MAUTO for error.
*/
char_u get_reg_type(int regname, long *reglen)
char_u get_reg_type(int regname, colnr_T *reg_width)
{
switch (regname) {
case '%': /* file name */
@ -4726,13 +4710,45 @@ char_u get_reg_type(int regname, long *reglen)
yankreg_T *reg = get_yank_register(regname, YREG_PASTE);
if (reg->y_array != NULL) {
if (reglen != NULL && reg->y_type == MBLOCK)
*reglen = reg->y_width;
if (reg_width != NULL && reg->y_type == MBLOCK) {
*reg_width = reg->y_width;
}
return reg->y_type;
}
return MAUTO;
}
/// Format the register type as a string.
///
/// @param reg_type The register type.
/// @param reg_width The width, only used if "reg_type" is MBLOCK.
/// @param[out] buf Buffer to store formatted string. The allocated size should
/// be at least NUMBUFLEN+2 to always fit the value.
/// @param buf_len The allocated size of the buffer.
void format_reg_type(char_u reg_type, colnr_T reg_width,
char* buf, size_t buf_len)
FUNC_ATTR_NONNULL_ALL
{
assert(buf_len > 1);
switch (reg_type) {
case MLINE:
buf[0] = 'V';
buf[1] = NUL;
break;
case MCHAR:
buf[0] = 'v';
buf[1] = NUL;
break;
case MBLOCK:
snprintf(buf, buf_len, CTRL_V_STR "%" PRIdCOLNR, reg_width + 1);
break;
case MAUTO:
buf[0] = NUL;
break;
}
}
/// When `flags` has `kGRegList` return a list with text `s`.
/// Otherwise just return `s`.
///

View File

@ -2,7 +2,11 @@
#define NVIM_POS_H
typedef long linenr_T; // line number type
typedef int colnr_T; // column number type
/// Column number type
typedef int colnr_T;
/// Format used to print values which have colnr_T type
#define PRIdCOLNR "d"
#define MAXLNUM 0x7fffffff // maximum (invalid) line number
#define MAXCOL 0x7fffffff // maximum column number, 31 bits