To: vim_dev@googlegroups.com Subject: Patch 7.4.2273 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4.2273 Problem: getwininfo() and getbufinfo() are inefficient. Solution: Do not make a copy of all window/buffer-local options. Make it possible to get them with gettabwinvar() or getbufvar(). Files: src/evalfunc.c, src/eval.c, src/testdir/test_bufwintabinfo.vim, runtime/doc/eval.txt *** ../vim-7.4.2272/src/evalfunc.c 2016-08-27 20:33:56.880602817 +0200 --- src/evalfunc.c 2016-08-27 21:22:48.275375727 +0200 *************** *** 3921,3927 **** get_buffer_info(buf_T *buf) { dict_T *dict; - dict_T *opts; tabpage_T *tp; win_T *wp; list_T *windows; --- 3921,3926 ---- *************** *** 3945,3955 **** /* Get a reference to buffer variables */ dict_add_dict(dict, "variables", buf->b_vars); - /* Copy buffer options */ - opts = get_winbuf_options(TRUE); - if (opts != NULL) - dict_add_dict(dict, "options", opts); - /* List of windows displaying this buffer */ windows = list_alloc(); if (windows != NULL) --- 3944,3949 ---- *************** *** 4156,4164 **** save_curbuf = curbuf; curbuf = buf; ! if (*varname == '&') /* buffer-local-option */ { ! if (get_option_tv(&varname, rettv, TRUE) == OK) done = TRUE; } else if (STRCMP(varname, "changedtick") == 0) --- 4150,4172 ---- save_curbuf = curbuf; curbuf = buf; ! if (*varname == '&') { ! if (varname[1] == NUL) ! { ! /* get all buffer-local options in a dict */ ! dict_T *opts = get_winbuf_options(TRUE); ! ! if (opts != NULL) ! { ! rettv->v_type = VAR_DICT; ! rettv->vval.v_dict = opts; ! ++opts->dv_refcount; ! done = TRUE; ! } ! } ! else if (get_option_tv(&varname, rettv, TRUE) == OK) ! /* buffer-local-option */ done = TRUE; } else if (STRCMP(varname, "changedtick") == 0) *************** *** 5112,5118 **** get_win_info(win_T *wp, short tpnr, short winnr) { dict_T *dict; - dict_T *opts; dict = dict_alloc(); if (dict == NULL) --- 5120,5125 ---- *************** *** 5131,5144 **** (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL); #endif ! /* Make a reference to window variables */ dict_add_dict(dict, "variables", wp->w_vars); - /* Copy window options */ - opts = get_winbuf_options(FALSE); - if (opts != NULL) - dict_add_dict(dict, "options", opts); - return dict; } #endif --- 5138,5146 ---- (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL); #endif ! /* Add a reference to window variables */ dict_add_dict(dict, "variables", wp->w_vars); return dict; } #endif *** ../vim-7.4.2272/src/eval.c 2016-08-26 16:29:44.800892431 +0200 --- src/eval.c 2016-08-27 20:57:13.104579291 +0200 *************** *** 8470,8478 **** || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK) #endif { ! if (*varname == '&') /* window-local-option */ { ! if (get_option_tv(&varname, rettv, 1) == OK) done = TRUE; } else --- 8470,8492 ---- || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK) #endif { ! if (*varname == '&') { ! if (varname[1] == NUL) ! { ! /* get all window-local options in a dict */ ! dict_T *opts = get_winbuf_options(FALSE); ! ! if (opts != NULL) ! { ! rettv->v_type = VAR_DICT; ! rettv->vval.v_dict = opts; ! ++opts->dv_refcount; ! done = TRUE; ! } ! } ! else if (get_option_tv(&varname, rettv, 1) == OK) ! /* window-local-option */ done = TRUE; } else *** ../vim-7.4.2272/src/testdir/test_bufwintabinfo.vim 2016-08-18 21:22:00.966648696 +0200 --- src/testdir/test_bufwintabinfo.vim 2016-08-27 21:14:58.803413272 +0200 *************** *** 18,24 **** let b:editor = 'vim' let l = getbufinfo('%') call assert_equal(bufnr('%'), l[0].bufnr) - call assert_equal(8, l[0].options.tabstop) call assert_equal('vim', l[0].variables.editor) call assert_notequal(-1, index(l[0].windows, bufwinid('%'))) --- 18,23 ---- *************** *** 49,57 **** call assert_equal(winbufnr(2), winlist[1].bufnr) call assert_equal(winheight(2), winlist[1].height) call assert_equal(1, winlist[2].winnr) - if has('signs') - call assert_equal('auto', winlist[0].options.signcolumn) - endif call assert_equal(2, winlist[3].tabnr) call assert_equal('green', winlist[2].variables.signal) call assert_equal(winwidth(1), winlist[0].width) --- 48,53 ---- *************** *** 83,85 **** --- 79,103 ---- call assert_false(winlist[2].loclist) wincmd t | only endfunction + + function Test_get_buf_options() + let opts = getbufvar(bufnr('%'), '&') + call assert_equal(v:t_dict, type(opts)) + call assert_equal(8, opts.tabstop) + endfunc + + function Test_get_win_options() + let opts = getwinvar(1, '&') + call assert_equal(v:t_dict, type(opts)) + call assert_equal(0, opts.linebreak) + if has('signs') + call assert_equal('auto', opts.signcolumn) + endif + + let opts = gettabwinvar(1, 1, '&') + call assert_equal(v:t_dict, type(opts)) + call assert_equal(0, opts.linebreak) + if has('signs') + call assert_equal('auto', opts.signcolumn) + endif + endfunc *** ../vim-7.4.2272/runtime/doc/eval.txt 2016-08-18 21:22:00.962648731 +0200 --- runtime/doc/eval.txt 2016-08-27 21:20:44.056444014 +0200 *************** *** 3957,3963 **** *getbufinfo()* getbufinfo([{expr}]) getbufinfo([{dict}]) ! Get information aobut buffers as a List of Dictionaries. Without an argument information about all the buffers is returned. --- 3988,3994 ---- *getbufinfo()* getbufinfo([{expr}]) getbufinfo([{dict}]) ! Get information about buffers as a List of Dictionaries. Without an argument information about all the buffers is returned. *************** *** 3983,4008 **** lnum current line number in buffer. loaded TRUE if the buffer is loaded. name full path to the file in the buffer. - options dictionary of buffer local options. signs list of signs placed in the buffer. Each list item is a dictionary with the following fields: id sign identifier lnum line number name sign name ! variables dictionary of buffer local variables. ! windows list of window IDs with this buffer Examples: > for buf in getbufinfo() echo buf.name endfor for buf in getbufinfo({'buflisted':1}) ! if buf.options.filetype == 'java' .... endif endfor < *getbufline()* getbufline({expr}, {lnum} [, {end}]) Return a |List| with the lines starting from {lnum} to {end} --- 4014,4044 ---- lnum current line number in buffer. loaded TRUE if the buffer is loaded. name full path to the file in the buffer. signs list of signs placed in the buffer. Each list item is a dictionary with the following fields: id sign identifier lnum line number name sign name ! variables a reference to the dictionary with ! buffer-local variables. ! windows list of |window-ID|s that display this ! buffer Examples: > for buf in getbufinfo() echo buf.name endfor for buf in getbufinfo({'buflisted':1}) ! if buf.changed .... endif endfor < + To get buffer-local options use: > + getbufvar({bufnr}, '&') + + < *getbufline()* getbufline({expr}, {lnum} [, {end}]) Return a |List| with the lines starting from {lnum} to {end} *************** *** 4034,4039 **** --- 4070,4079 ---- must be used. When {varname} is empty returns a dictionary with all the buffer-local variables. + When {varname} is equal to "&" returns a dictionary with all + the buffer-local options. + Otherwise, when {varname} starts with "&" returns the value of + a buffer-local option. This also works for a global or buffer-local option, but it doesn't work for a global variable, window-local variable or window-local option. *************** *** 4485,4492 **** Each List item is a Dictionary with the following entries: tabnr tab page number. ! variables dictionary of tabpage local variables. ! windows List of window IDs in the tag page. gettabvar({tabnr}, {varname} [, {def}]) *gettabvar()* Get the value of a tab-local variable {varname} in tab page --- 4541,4549 ---- Each List item is a Dictionary with the following entries: tabnr tab page number. ! variables a reference to the dictionary with ! tabpage-local variables ! windows List of |window-ID|s in the tag page. gettabvar({tabnr}, {varname} [, {def}]) *gettabvar()* Get the value of a tab-local variable {varname} in tab page *************** *** 4501,4514 **** gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()* Get the value of window-local variable {varname} in window {winnr} in tab page {tabnr}. - When {varname} starts with "&" get the value of a window-local - option. When {varname} is empty a dictionary with all window-local variables is returned. Note that {varname} must be the name without "w:". Tabs are numbered starting with one. For the current tabpage use |getwinvar()|. ! {winnr} can be the window number or the window ID. When {winnr} is zero the current window is used. This also works for a global option, buffer-local option and window-local option, but it doesn't work for a global variable --- 4558,4573 ---- gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()* Get the value of window-local variable {varname} in window {winnr} in tab page {tabnr}. When {varname} is empty a dictionary with all window-local variables is returned. + When {varname} is equal to "&" get the values of all + window-local options in a Dictionary. + Otherwise, when {varname} starts with "&" get the value of a + window-local option. Note that {varname} must be the name without "w:". Tabs are numbered starting with one. For the current tabpage use |getwinvar()|. ! {winnr} can be the window number or the |window-ID|. When {winnr} is zero the current window is used. This also works for a global option, buffer-local option and window-local option, but it doesn't work for a global variable *************** *** 4536,4558 **** is returned. If the window does not exist the result is an empty list. ! Without an information about all the windows in all the tab ! pages is returned. Each List item is a Dictionary with the following entries: bufnr number of buffer in the window height window height loclist 1 if showing a location list {only with the +quickfix feature} - options dictionary of window local options quickfix 1 if quickfix or location list window {only with the +quickfix feature} tabnr tab page number ! variables dictionary of window local variables width window width ! winid window ID winnr window number getwinvar({winnr}, {varname} [, {def}]) *getwinvar()* Like |gettabwinvar()| for the current tabpage. Examples: > --- 4595,4620 ---- is returned. If the window does not exist the result is an empty list. ! Without {winid} information about all the windows in all the ! tab pages is returned. Each List item is a Dictionary with the following entries: bufnr number of buffer in the window height window height loclist 1 if showing a location list {only with the +quickfix feature} quickfix 1 if quickfix or location list window {only with the +quickfix feature} tabnr tab page number ! variables a reference to the dictionary with ! window-local variables width window width ! winid |window-ID| winnr window number + To obtain all window-local variables use: > + gettabwinvar({tabnr}, {winnr}, '&') + getwinvar({winnr}, {varname} [, {def}]) *getwinvar()* Like |gettabwinvar()| for the current tabpage. Examples: > *** ../vim-7.4.2272/src/version.c 2016-08-27 20:33:56.884602784 +0200 --- src/version.c 2016-08-27 21:18:46.221457411 +0200 *************** *** 765,766 **** --- 765,768 ---- { /* Add new patch number below this line */ + /**/ + 2273, /**/ -- From "know your smileys": %-) After staring at screen for 15 hours /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///