Hi Bram,

On Sun, Aug 27, 2017 at 9:49 AM, Yegappan Lakshmanan
<[email protected]> wrote:
> Hi Bram,
>
> On Sun, Aug 27, 2017 at 6:23 AM, Bram Moolenaar <[email protected]> wrote:
>>
>> Yegappan Lakshmanan wrote:
>>
>>> Currently all the available quickfix commands and functions that parse
>>> text using 'erroformat' add the resulting entries to a quickfix list in the
>>> stack. There have been several requests in the past to add support for
>>> parsing text using 'erroformat' without modifying the quickfix lists in
>>> the stack. This is useful for Vim plugins that deal with quickfix lists.
>>> I am attaching a patch to add this feature.
>>>
>>> To parse text without modifying the quickfix lists, the getqflist() function
>>> can be used as shown below:
>>>
>>>       echo getqflist({'text': "File10:10:Line10\nFile20:20:Line20"})
>>> or
>>>       echo getqflist({'text': ["File10:10:Line10", "File10:20:Line20"]})
>>>
>>> The above call returns a dictionary where the 'items' key will contain
>>> all the parsed entries.
>>>
>>> The getloclist() function can also be used instead of getqflist().
>>
>> Thanks, I'll include it.
>>
>> This still uses the existing 'errorformat'.  Would there be use in
>> passing another 'errorformat' for this?
>>
>

I am attaching a patch to use a custom 'efm' for both the getqflist()
and setqflist() functions.

To parse lines using a custom efm and get a quickfix list (without
modifying the quickfix stack):

      let l = getqflist({'efm' : '%f#%l#%m', 'lines' : ['F1#10#L10']})

To parse liens using a custom efm and add entries to a quickfix list:

     call setqflist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ['F1#10#L10']})

- Yegappan

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index aea1cce3a..e0cf178eb 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -4632,16 +4632,19 @@ getqflist([{what}])                                     
*getqflist()*
                returns only the items listed in {what} as a dictionary. The
                following string items are supported in {what}:
                        context get the context stored with |setqflist()|
+                       efm     errorformat to use when parsing "lines". If
+                               not present, then the 'erroformat' option
+                               value is used.
                        id      get information for the quickfix list with
                                |quickfix-ID|; zero means the id for the
-                               current list or the list specifed by 'nr'
+                               current list or the list specifed by "nr"
                        items   quickfix list entries
                        lines   use 'errorformat' to extract items from a list
                                of lines and return the resulting entries.
                                Only a |List| type is accepted.  The current
                                quickfix list is not modified.
                        nr      get information for this quickfix list; zero
-                               means the current quickfix list and '$' means
+                               means the current quickfix list and "$" means
                                the last quickfix list
                        title   get the list title
                        winid   get the |window-ID| (if opened)
@@ -4650,12 +4653,12 @@ getqflist([{what}])                                     
*getqflist()*
                If "nr" is not present then the current quickfix list is used.
                If both "nr" and a non-zero "id" are specified, then the list
                specified by "id" is used.
-               To get the number of lists in the quickfix stack, set 'nr' to
-               '$' in {what}. The 'nr' value in the returned dictionary
+               To get the number of lists in the quickfix stack, set "nr" to
+               "$" in {what}. The "nr" value in the returned dictionary
                contains the quickfix stack size.
-               When 'text' is specified, all the other items are ignored. The
-               returned dictionary contains the entry 'items' with the list
-               of entries.
+               When "lines" is specified, all the other items are ignored.
+               The returned dictionary contains the entry "items" with the
+               list of entries.
                In case of error processing {what}, an empty dictionary is
                returned.
 
@@ -7064,13 +7067,16 @@ setqflist({list} [, {action}[, {what}]])                
*setqflist()*
                is created. The new quickfix list is added after the current
                quickfix list in the stack and all the following lists are
                freed. To add a new quickfix list at the end of the stack,
-               set "nr" in {what} to '$'.
+               set "nr" in {what} to "$".
 
                If the optional {what} dictionary argument is supplied, then
                only the items listed in {what} are set. The first {list}
                argument is ignored.  The following items can be specified in
                {what}:
                    context     any Vim type can be stored as a context
+                   efm         errorformat to use when parsing text from
+                               "lines". If this is not present, then the
+                               'errorformat' option value is used.
                    id          quickfix list identifier |quickfix-ID|
                    items       list of quickfix entries. Same as the {list}
                                argument.
@@ -7078,7 +7084,7 @@ setqflist({list} [, {action}[, {what}]])          
*setqflist()*
                                add the resulting entries to the quickfix list
                                {nr} or {id}.  Only a |List| value is supported.
                    nr          list number in the quickfix stack; zero
-                               means the current quickfix list and '$' means
+                               means the current quickfix list and "$" means
                                the last quickfix list
                    title       quickfix list title text
                Unsupported keys in {what} are ignored.
@@ -7086,7 +7092,7 @@ setqflist({list} [, {action}[, {what}]])          
*setqflist()*
                is modified. When creating a new quickfix list, "nr" can be
                set to a value one greater than the quickfix stack size.
                When modifying a quickfix list, to guarantee that the correct
-               list is modified, 'id' should be used instead of 'nr' to
+               list is modified, "id" should be used instead of "nr" to
                specify the list.
 
                Examples: >
diff --git a/src/quickfix.c b/src/quickfix.c
index 5cbb1519a..ee871fc46 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -4643,16 +4643,29 @@ enum {
  * Parse text from 'di' and return the quickfix list items
  */
     static int
-qf_get_list_from_lines(dictitem_T *di, dict_T *retdict)
+qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
 {
     int                status = FAIL;
     qf_info_T  *qi;
+    char_u     *errorformat = p_efm;
+    dictitem_T *efm_di;
+    list_T     *l;
 
     /* Only a List value is supported */
     if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
     {
-       list_T  *l = list_alloc();
+       /* If errorformat is supplied then use it, otherwise use the 'efm'
+        * option setting
+        */
+       if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
+       {
+           if (efm_di->di_tv.v_type != VAR_STRING ||
+                   efm_di->di_tv.vval.v_string == NULL)
+               return FAIL;
+           errorformat = efm_di->di_tv.vval.v_string;
+       }
 
+       l = list_alloc();
        if (l == NULL)
            return FAIL;
 
@@ -4662,7 +4675,7 @@ qf_get_list_from_lines(dictitem_T *di, dict_T *retdict)
            vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
            qi->qf_refcount++;
 
-           if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, p_efm,
+           if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
                        TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
            {
                (void)get_errorlist(qi, NULL, 0, l);
@@ -4692,7 +4705,7 @@ get_errorlist_properties(win_T *wp, dict_T *what, dict_T 
*retdict)
     int                flags = QF_GETLIST_NONE;
 
     if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
-       return qf_get_list_from_lines(di, retdict);
+       return qf_get_list_from_lines(what, di, retdict);
 
     if (wp != NULL)
        qi = GET_LOC_LIST(wp);
@@ -4962,6 +4975,7 @@ qf_set_properties(qf_info_T *qi, dict_T *what, int 
action, char_u *title)
     int                retval = FAIL;
     int                qf_idx;
     int                newlist = FALSE;
+    char_u     *errorformat = p_efm;
 
     if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
        newlist = TRUE;
@@ -5039,6 +5053,7 @@ qf_set_properties(qf_info_T *qi, dict_T *what, int 
action, char_u *title)
            retval = OK;
        }
     }
+
     if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
     {
        if (di->di_tv.v_type == VAR_LIST)
@@ -5051,6 +5066,13 @@ qf_set_properties(qf_info_T *qi, dict_T *what, int 
action, char_u *title)
        }
     }
 
+    if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL)
+    {
+       if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL)
+           return FAIL;
+       errorformat = di->di_tv.vval.v_string;
+    }
+
     if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
     {
        /* Only a List value is supported */
@@ -5058,7 +5080,7 @@ qf_set_properties(qf_info_T *qi, dict_T *what, int 
action, char_u *title)
        {
            if (action == 'r')
                qf_free_items(qi, qf_idx);
-           if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, p_efm,
+           if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
                        FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
                retval = OK;
        }
diff --git a/src/testdir/test_quickfix.vim b/src/testdir/test_quickfix.vim
index 64f1baea2..6a8d0e7bf 100644
--- a/src/testdir/test_quickfix.vim
+++ b/src/testdir/test_quickfix.vim
@@ -2321,6 +2321,17 @@ func Xsetexpr_tests(cchar)
   call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
   call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
   call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
+
+  " Adding entries using a custom efm
+  set efm&
+  call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
+                               \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
+  call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
+  call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
+  call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
+  call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
+  call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
+                               \ 'lines' : ['F1:10:L10']}))
 endfunc
 
 func Test_setexpr()
@@ -2537,6 +2548,17 @@ func XgetListFromLines(cchar)
   call assert_equal([], g:Xgetlist({'lines' : []}).items)
   call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
 
+  " Parse text using a custom efm
+  set efm&
+  let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
+  call assert_equal('Line30', l[0].text)
+  let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
+  call assert_equal('File3:30:Line30', l[0].text)
+  let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
+  call assert_equal({}, l)
+  call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
+  call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
+
   " Make sure that the quickfix stack is not modified
   call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
 endfunc

Raspunde prin e-mail lui