Add new functions flag -V/--inherit-variable

--inherit-variable takes a variable name and snapshots its current
value. When the function is executed, it will have a local variable with
this value already defined. Printing the function source will include
synthesized `set -l` lines for the values.

This is primarily useful for functions that are created on the fly, such
as in `psub`.
This commit is contained in:
Kevin Ballard
2014-10-02 15:59:24 -07:00
parent 6d7a7b00d7
commit cfc06203e7
10 changed files with 152 additions and 7 deletions

View File

@@ -11,10 +11,12 @@
#define FISH_FUNCTION_H
#include <wchar.h>
#include <map>
#include "util.h"
#include "common.h"
#include "event.h"
#include "env.h"
class parser_t;
class env_vars_snapshot_t;
@@ -48,6 +50,11 @@ struct function_data_t
List of all named arguments for this function
*/
wcstring_list_t named_arguments;
/**
List of all variables that are inherited from the function definition scope.
The variable values are snapshotted when function_add() is called.
*/
wcstring_list_t inherit_vars;
/**
Set to non-zero if invoking this function shadows the variables
of the underlying function.
@@ -79,6 +86,9 @@ class function_info_t
/** List of all named arguments for this function */
const wcstring_list_t named_arguments;
/** Mapping of all variables that were inherited from the function definition scope to their values */
const std::map<wcstring,env_var_t> inherit_vars;
/** Flag for specifying that this function was automatically loaded */
const bool is_autoload;
@@ -162,6 +172,12 @@ int function_get_definition_offset(const wcstring &name);
*/
wcstring_list_t function_get_named_arguments(const wcstring &name);
/**
Returns a mapping of all variables of the specified function that were inherited
from the scope of the function definition to their values.
*/
std::map<wcstring,env_var_t> function_get_inherit_vars(const wcstring &name);
/**
Creates a new function using the same definition as the specified function.
Returns true if copy is successful.