Permalink
Browse files

patch 8.1.0166: using dict_add_nr_str() is clumsy

Problem:    Using dict_add_nr_str() is clumsy.
Solution:   Split into two functions. (Ozaki Kiichi, closes #3154)
  • Loading branch information...
brammool committed Jul 8, 2018
1 parent 4cde86c commit e0be167a805fd547c25ec1ec97fd4c7f13046236
Showing with 188 additions and 198 deletions.
  1. +16 −18 src/channel.c
  2. +25 −15 src/dict.c
  3. +7 −12 src/edit.c
  4. +64 −70 src/evalfunc.c
  5. +7 −7 src/ex_cmds2.c
  6. +13 −13 src/ops.c
  7. +3 −3 src/option.c
  8. +2 −1 src/proto/dict.pro
  9. +28 −37 src/quickfix.c
  10. +6 −6 src/tag.c
  11. +10 −11 src/terminal.c
  12. +5 −5 src/undo.c
  13. +2 −0 src/version.c
@@ -2809,7 +2809,7 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
status = "buffered";
else
status = "closed";
dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
dict_add_string(dict, namebuf, (char_u *)status);

STRCPY(namebuf + tail, "mode");
switch (chanpart->ch_mode)
@@ -2819,7 +2819,7 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
case MODE_JSON: s = "JSON"; break;
case MODE_JS: s = "JS"; break;
}
dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
dict_add_string(dict, namebuf, (char_u *)s);

STRCPY(namebuf + tail, "io");
if (part == PART_SOCK)
@@ -2832,22 +2832,22 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
case JIO_BUFFER: s = "buffer"; break;
case JIO_OUT: s = "out"; break;
}
dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
dict_add_string(dict, namebuf, (char_u *)s);

STRCPY(namebuf + tail, "timeout");
dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
dict_add_number(dict, namebuf, chanpart->ch_timeout);
}

void
channel_info(channel_T *channel, dict_T *dict)
{
dict_add_nr_str(dict, "id", channel->ch_id, NULL);
dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
dict_add_number(dict, "id", channel->ch_id);
dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));

if (channel->ch_hostname != NULL)
{
dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
dict_add_nr_str(dict, "port", channel->ch_port, NULL);
dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
dict_add_number(dict, "port", channel->ch_port);
channel_part_info(channel, dict, "sock", PART_SOCK);
}
else
@@ -5737,7 +5737,7 @@ job_info(job_T *job, dict_T *dict)
list_T *l;
int i;

dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
dict_add_string(dict, "status", (char_u *)job_status(job));

item = dictitem_alloc((char_u *)"channel");
if (item == NULL)
@@ -5755,15 +5755,13 @@ job_info(job_T *job, dict_T *dict)
#else
nr = job->jv_proc_info.dwProcessId;
#endif
dict_add_nr_str(dict, "process", nr, NULL);
dict_add_nr_str(dict, "tty_in", 0L,
job->jv_tty_in != NULL ? job->jv_tty_in : (char_u *)"");
dict_add_nr_str(dict, "tty_out", 0L,
job->jv_tty_out != NULL ? job->jv_tty_out : (char_u *)"");

dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
dict_add_number(dict, "process", nr);
dict_add_string(dict, "tty_in", job->jv_tty_in);
dict_add_string(dict, "tty_out", job->jv_tty_out);

dict_add_number(dict, "exitval", job->jv_exitval);
dict_add_string(dict, "exit_cb", job->jv_exit_cb);
dict_add_string(dict, "stoponexit", job->jv_stoponexit);

l = list_alloc();
if (l != NULL)
@@ -327,33 +327,43 @@ dict_add(dict_T *d, dictitem_T *item)
}

/*
* Add a number or string entry to dictionary "d".
* When "str" is NULL use number "nr", otherwise use "str".
* Add a number entry to dictionary "d".
* Returns FAIL when out of memory and when key already exists.
*/
int
dict_add_nr_str(
dict_T *d,
char *key,
varnumber_T nr,
char_u *str)
dict_add_number(dict_T *d, char *key, varnumber_T nr)
{
dictitem_T *item;

item = dictitem_alloc((char_u *)key);
if (item == NULL)
return FAIL;
item->di_tv.v_lock = 0;
if (str == NULL)
{
item->di_tv.v_type = VAR_NUMBER;
item->di_tv.vval.v_number = nr;
}
else
item->di_tv.v_type = VAR_NUMBER;
item->di_tv.vval.v_number = nr;
if (dict_add(d, item) == FAIL)
{
item->di_tv.v_type = VAR_STRING;
item->di_tv.vval.v_string = vim_strsave(str);
dictitem_free(item);
return FAIL;
}
return OK;
}

/*
* Add a string entry to dictionary "d".
* Returns FAIL when out of memory and when key already exists.
*/
int
dict_add_string(dict_T *d, char *key, char_u *str)
{
dictitem_T *item;

item = dictitem_alloc((char_u *)key);
if (item == NULL)
return FAIL;
item->di_tv.v_lock = 0;
item->di_tv.v_type = VAR_STRING;
item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL;
if (dict_add(d, item) == FAIL)
{
dictitem_free(item);
@@ -4884,18 +4884,13 @@ ins_compl_insert(int in_compl_func)
dict = dict_alloc_lock(VAR_FIXED);
if (dict != NULL)
{
dict_add_nr_str(dict, "word", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_str));
dict_add_nr_str(dict, "abbr", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
dict_add_nr_str(dict, "menu", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
dict_add_nr_str(dict, "kind", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
dict_add_nr_str(dict, "info", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
dict_add_nr_str(dict, "user_data", 0L,
EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_USER_DATA]));
dict_add_string(dict, "word", compl_shown_match->cp_str);
dict_add_string(dict, "abbr", compl_shown_match->cp_text[CPT_ABBR]);
dict_add_string(dict, "menu", compl_shown_match->cp_text[CPT_MENU]);
dict_add_string(dict, "kind", compl_shown_match->cp_text[CPT_KIND]);
dict_add_string(dict, "info", compl_shown_match->cp_text[CPT_INFO]);
dict_add_string(dict, "user_data",
compl_shown_match->cp_text[CPT_USER_DATA]);
}
set_vim_var_dict(VV_COMPLETED_ITEM, dict);
if (!in_compl_func)
@@ -4338,9 +4338,9 @@ get_buffer_signs(buf_T *buf, list_T *l)

if (d != NULL)
{
dict_add_nr_str(d, "id", sign->id, NULL);
dict_add_nr_str(d, "lnum", sign->lnum, NULL);
dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
dict_add_number(d, "id", sign->id);
dict_add_number(d, "lnum", sign->lnum);
dict_add_string(d, "name", sign_typenr2name(sign->typenr));

list_append_dict(l, d);
}
@@ -4363,18 +4363,16 @@ get_buffer_info(buf_T *buf)
if (dict == NULL)
return NULL;

dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
dict_add_nr_str(dict, "name", 0L,
buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
: buflist_findlnum(buf), NULL);
dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
dict_add_nr_str(dict, "hidden",
buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
NULL);
dict_add_number(dict, "bufnr", buf->b_fnum);
dict_add_string(dict, "name", buf->b_ffname);
dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
: buflist_findlnum(buf));
dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
dict_add_number(dict, "listed", buf->b_p_bl);
dict_add_number(dict, "changed", bufIsChanged(buf));
dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
dict_add_number(dict, "hidden",
buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);

/* Get a reference to buffer variables */
dict_add_dict(dict, "variables", buf->b_vars);
@@ -4663,10 +4661,10 @@ f_getchangelist(typval_T *argvars, typval_T *rettv)
return;
if (list_append_dict(l, d) == FAIL)
return;
dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
dict_add_number(d, "col", (long)buf->b_changelist[i].col);
# ifdef FEAT_VIRTUALEDIT
dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
# endif
}
#endif
@@ -4790,9 +4788,9 @@ f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
{
dict_T *dict = rettv->vval.v_dict;

dict_add_nr_str(dict, "char", 0L, last_csearch());
dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
dict_add_string(dict, "char", last_csearch());
dict_add_number(dict, "forward", last_csearch_forward());
dict_add_number(dict, "until", last_csearch_until());
}
}

@@ -5193,17 +5191,14 @@ f_getjumplist(typval_T *argvars, typval_T *rettv)
return;
if (list_append_dict(l, d) == FAIL)
return;
dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
NULL);
dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
NULL);
dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
# ifdef FEAT_VIRTUALEDIT
dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
NULL);
dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
# endif
dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
if (wp->w_jumplist[i].fname != NULL)
dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
dict_add_string(d, "filename", wp->w_jumplist[i].fname);
}
#endif
}
@@ -5321,18 +5316,18 @@ f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
}
else
{
dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
dict_add_string(dict, "pattern", cur->pattern);
}
dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
dict_add_nr_str(dict, "id", (long)cur->id, NULL);
dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
dict_add_number(dict, "priority", (long)cur->priority);
dict_add_number(dict, "id", (long)cur->id);
# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
if (cur->conceal_char)
{
char_u buf[MB_MAXBYTES + 1];

buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
dict_add_string(dict, "conceal", (char_u *)&buf);
}
# endif
list_append_dict(rettv->vval.v_list, dict);
@@ -5533,7 +5528,7 @@ get_tabpage_info(tabpage_T *tp, int tp_idx)
if (dict == NULL)
return NULL;

dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
dict_add_number(dict, "tabnr", tp_idx);

l = list_alloc();
if (l != NULL)
@@ -5649,23 +5644,23 @@ get_win_info(win_T *wp, short tpnr, short winnr)
if (dict == NULL)
return NULL;

dict_add_nr_str(dict, "tabnr", tpnr, NULL);
dict_add_nr_str(dict, "winnr", winnr, NULL);
dict_add_nr_str(dict, "winid", wp->w_id, NULL);
dict_add_nr_str(dict, "height", wp->w_height, NULL);
dict_add_number(dict, "tabnr", tpnr);
dict_add_number(dict, "winnr", winnr);
dict_add_number(dict, "winid", wp->w_id);
dict_add_number(dict, "height", wp->w_height);
#ifdef FEAT_MENU
dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
dict_add_number(dict, "winbar", wp->w_winbar_height);
#endif
dict_add_nr_str(dict, "width", wp->w_width, NULL);
dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
dict_add_number(dict, "width", wp->w_width);
dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);

#ifdef FEAT_TERMINAL
dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
#endif
#ifdef FEAT_QUICKFIX
dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
dict_add_nr_str(dict, "loclist",
(bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
dict_add_number(dict, "loclist",
(bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
#endif

/* Add a reference to window variables */
@@ -7652,15 +7647,15 @@ get_maparg(typval_T *argvars, typval_T *rettv, int exact)
char_u *mapmode = map_mode_to_chars(mp->m_mode);
dict_T *dict = rettv->vval.v_dict;

dict_add_nr_str(dict, "lhs", 0L, lhs);
dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
dict_add_nr_str(dict, "mode", 0L, mapmode);
dict_add_string(dict, "lhs", lhs);
dict_add_string(dict, "rhs", mp->m_orig_str);
dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
dict_add_number(dict, "sid", (long)mp->m_script_ID);
dict_add_number(dict, "buffer", (long)buffer_local);
dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
dict_add_string(dict, "mode", mapmode);

vim_free(lhs);
vim_free(mapmode);
@@ -13652,13 +13647,12 @@ f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
dict_T *dict = rettv->vval.v_dict;
list_T *list;

dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
dict_add_nr_str(dict, "save_last",
(long)curbuf->b_u_save_nr_last, NULL);
dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);

list = list_alloc();
if (list != NULL)
@@ -13882,20 +13876,20 @@ f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
return;
dict = rettv->vval.v_dict;

dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
dict_add_number(dict, "col", (long)curwin->w_cursor.col);
#ifdef FEAT_VIRTUALEDIT
dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
#endif
update_curswant();
dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
dict_add_number(dict, "curswant", (long)curwin->w_curswant);

dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
dict_add_number(dict, "topline", (long)curwin->w_topline);
#ifdef FEAT_DIFF
dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
dict_add_number(dict, "topfill", (long)curwin->w_topfill);
#endif
dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
}

/*
Oops, something went wrong.

0 comments on commit e0be167

Please sign in to comment.