195c476
To: vim-dev@vim.org
195c476
Subject: Patch 7.2.295
195c476
Fcc: outbox
195c476
From: Bram Moolenaar <Bram@moolenaar.net>
195c476
Mime-Version: 1.0
195c476
Content-Type: text/plain; charset=UTF-8
195c476
Content-Transfer-Encoding: 8bit
195c476
------------
195c476
195c476
Patch 7.2.295
195c476
Problem:    When using map() on a List the index is not known.
195c476
Solution:   Set v:key to the  index. (Hari Krishna Dara)
195c476
Files:	    runtime/doc/eval.txt, src/eval.c
195c476
195c476
195c476
*** ../vim-7.2.294/runtime/doc/eval.txt	2009-11-11 14:21:48.000000000 +0100
195c476
--- runtime/doc/eval.txt	2009-11-11 18:22:54.000000000 +0100
195c476
***************
195c476
*** 3802,3808 ****
195c476
  		Replace each item in {expr} with the result of evaluating
195c476
  		{string}.
195c476
  		Inside {string} |v:val| has the value of the current item.
195c476
! 		For a |Dictionary| |v:key| has the key of the current item.
195c476
  		Example: >
195c476
  			:call map(mylist, '"> " . v:val . " <"')
195c476
  <		This puts "> " before and " <" after each item in "mylist".
195c476
--- 3812,3819 ----
195c476
  		Replace each item in {expr} with the result of evaluating
195c476
  		{string}.
195c476
  		Inside {string} |v:val| has the value of the current item.
195c476
! 		For a |Dictionary| |v:key| has the key of the current item
195c476
! 		and for a |List| |v:key| has the index of the current item.
195c476
  		Example: >
195c476
  			:call map(mylist, '"> " . v:val . " <"')
195c476
  <		This puts "> " before and " <" after each item in "mylist".
195c476
*** ../vim-7.2.294/src/eval.c	2009-11-11 14:21:48.000000000 +0100
195c476
--- src/eval.c	2009-11-11 18:22:49.000000000 +0100
195c476
***************
195c476
*** 9928,9933 ****
195c476
--- 9928,9934 ----
195c476
      int		todo;
195c476
      char_u	*ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
195c476
      int		save_did_emsg;
195c476
+     int		index = 0;
195c476
  
195c476
      if (argvars[0].v_type == VAR_LIST)
195c476
      {
195c476
***************
195c476
*** 9961,9969 ****
195c476
  	save_did_emsg = did_emsg;
195c476
  	did_emsg = FALSE;
195c476
  
195c476
  	if (argvars[0].v_type == VAR_DICT)
195c476
  	{
195c476
- 	    prepare_vimvar(VV_KEY, &save_key);
195c476
  	    vimvars[VV_KEY].vv_type = VAR_STRING;
195c476
  
195c476
  	    ht = &d->dv_hashtab;
195c476
--- 9962,9970 ----
195c476
  	save_did_emsg = did_emsg;
195c476
  	did_emsg = FALSE;
195c476
  
195c476
+ 	prepare_vimvar(VV_KEY, &save_key);
195c476
  	if (argvars[0].v_type == VAR_DICT)
195c476
  	{
195c476
  	    vimvars[VV_KEY].vv_type = VAR_STRING;
195c476
  
195c476
  	    ht = &d->dv_hashtab;
195c476
***************
195c476
*** 9987,10010 ****
195c476
  		}
195c476
  	    }
195c476
  	    hash_unlock(ht);
195c476
- 
195c476
- 	    restore_vimvar(VV_KEY, &save_key);
195c476
  	}
195c476
  	else
195c476
  	{
195c476
  	    for (li = l->lv_first; li != NULL; li = nli)
195c476
  	    {
195c476
  		if (tv_check_lock(li->li_tv.v_lock, ermsg))
195c476
  		    break;
195c476
  		nli = li->li_next;
195c476
  		if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
195c476
  								  || did_emsg)
195c476
  		    break;
195c476
  		if (!map && rem)
195c476
  		    listitem_remove(l, li);
195c476
  	    }
195c476
  	}
195c476
  
195c476
  	restore_vimvar(VV_VAL, &save_val);
195c476
  
195c476
  	did_emsg |= save_did_emsg;
195c476
--- 9988,10014 ----
195c476
  		}
195c476
  	    }
195c476
  	    hash_unlock(ht);
195c476
  	}
195c476
  	else
195c476
  	{
195c476
+ 	    vimvars[VV_KEY].vv_type = VAR_NUMBER;
195c476
+ 
195c476
  	    for (li = l->lv_first; li != NULL; li = nli)
195c476
  	    {
195c476
  		if (tv_check_lock(li->li_tv.v_lock, ermsg))
195c476
  		    break;
195c476
  		nli = li->li_next;
195c476
+ 		vimvars[VV_KEY].vv_nr = index;
195c476
  		if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
195c476
  								  || did_emsg)
195c476
  		    break;
195c476
  		if (!map && rem)
195c476
  		    listitem_remove(l, li);
195c476
+ 		++index;
195c476
  	    }
195c476
  	}
195c476
  
195c476
+ 	restore_vimvar(VV_KEY, &save_key);
195c476
  	restore_vimvar(VV_VAL, &save_val);
195c476
  
195c476
  	did_emsg |= save_did_emsg;
195c476
*** ../vim-7.2.294/src/version.c	2009-11-17 12:08:48.000000000 +0100
195c476
--- src/version.c	2009-11-17 12:18:08.000000000 +0100
195c476
***************
195c476
*** 683,684 ****
195c476
--- 683,686 ----
195c476
  {   /* Add new patch number below this line */
195c476
+ /**/
195c476
+     295,
195c476
  /**/
195c476
195c476
-- 
195c476
ARTHUR:       You are indeed brave Sir knight, but the fight is mine.
195c476
BLACK KNIGHT: Had enough?
195c476
ARTHUR:       You stupid bastard.  You havn't got any arms left.
195c476
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
195c476
195c476
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
195c476
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
195c476
\\\        download, build and distribute -- http://www.A-A-P.org        ///
195c476
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///