Reply to message «[PATCH] Add third argument to sort (same as third argument to 
call())», 
sent 16:24:44 15 May 2011, Sunday
by ZyX:

Sorry, forgot the patch itself.

Original message:
> The attached patch adds a third argument to sort so that sort will be able
> to use dictionary functions. Here are two reasons for doing this:
> 1. Anonymous functions are only dictionary functions (this is what I need).
> 2. You will be able to pass additional arguments or store some static
> values in this dictionary without using some global variable (and this is
> why I prefer this over adding `nodict' extra argument to ex_function).
# HG changeset patch
# User ZyX <[email protected]>
# Date 1305461263 -14400
# Branch sort_dict
# Node ID 1ac54eae17c28675d77903e58dcdf1c57d91a3ab
# Parent  d161a7f704f6135781c10fd3c7e62f7001f90d7c
Added third argument to sort()

diff -r d161a7f704f6 -r 1ac54eae17c2 runtime/doc/eval.txt
--- a/runtime/doc/eval.txt	Tue May 10 17:29:34 2011 +0200
+++ b/runtime/doc/eval.txt	Sun May 15 16:07:43 2011 +0400
@@ -1918,7 +1918,8 @@
 simplify( {filename})		String	simplify filename as much as possible
 sin( {expr})			Float	sine of {expr}
 sinh( {expr})			Float	hyperbolic sine of {expr}
-sort( {list} [, {func}])	List	sort {list}, using {func} to compare
+sort( {list} [, {func} [, {dict}]])
+				List	sort {list}, using {func} to compare
 soundfold( {word})		String	sound-fold {word}
 spellbadword()			String	badly spelled word at cursor
 spellsuggest( {word} [, {max} [, {capital}]])
@@ -5273,7 +5274,7 @@
 		{only available when compiled with the |+float| feature}
 
 
-sort({list} [, {func}])					*sort()* *E702*
+sort({list} [, {func} [, {dict}]])			*sort()* *E702*
 		Sort the items in {list} in-place.  Returns {list}.  If you
 		want a list to remain unmodified make a copy first: >
 			:let sortedlist = sort(copy(mylist))
@@ -5281,6 +5282,8 @@
 		Numbers sort after Strings, |Lists| after Numbers.
 		For sorting text in the current buffer use |:sort|.
 		When {func} is given and it is one then case is ignored.
+		{dict} is for functions with the "dict" attribute.  It will be
+		used to set the local variable "self". |Dictionary-function|
 		When {func} is a |Funcref| or a function name, this function
 		is called to compare items.  The function is invoked with two
 		items as argument and must return zero if they are equal, 1 or
diff -r d161a7f704f6 -r 1ac54eae17c2 src/eval.c
--- a/src/eval.c	Tue May 10 17:29:34 2011 +0200
+++ b/src/eval.c	Sun May 15 16:07:43 2011 +0400
@@ -7929,7 +7929,7 @@
     {"sin",		1, 1, f_sin},
     {"sinh",		1, 1, f_sinh},
 #endif
-    {"sort",		1, 2, f_sort},
+    {"sort",		1, 3, f_sort},
     {"soundfold",	1, 1, f_soundfold},
     {"spellbadword",	0, 1, f_spellbadword},
     {"spellsuggest",	1, 3, f_spellsuggest},
@@ -16365,6 +16365,7 @@
 
 static int	item_compare_ic;
 static char_u	*item_compare_func;
+static dict_T	*item_compare_selfdict;
 static int	item_compare_func_err;
 #define ITEM_COMPARE_FAIL 999
 
@@ -16424,7 +16425,8 @@
 
     rettv.v_type = VAR_UNKNOWN;		/* clear_tv() uses this */
     res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
-				 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
+				 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
+				 item_compare_selfdict);
     clear_tv(&argv[0]);
     clear_tv(&argv[1]);
 
@@ -16488,6 +16490,17 @@
 	    }
 	}
 
+	item_compare_selfdict = NULL;
+	if (argvars[2].v_type != VAR_UNKNOWN)
+	{
+	    if (argvars[2].v_type != VAR_DICT)
+	    {
+		EMSG(_(e_dictreq));
+		return;
+	    }
+	    item_compare_selfdict = argvars[2].vval.v_dict;
+	}
+
 	/* Make an array with each entry pointing to an item in the List. */
 	ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
 	if (ptrs == NULL)

Attachment: signature.asc
Description: This is a digitally signed message part.

Raspunde prin e-mail lui