Make fish functions private - fish

I defined several helper functions that I only use in one specific fish script. I put them inside the wrapper function but you can still find them via autocompletion.
How can I hide them from auto completion and limit their scope to private?
function outer
function inner_func
echo "I still find this function via automplete"
end
end

Fish does not have private functions, but it is possible this feature will be available in future versions. In the meantime, try using the following technique, a naming convention, or both.
functions -e function_name
Very close to what you need. You can use functions -e function_name before the end of the block to erase it from the global scope.
function outer
function inner_func
echo "I still find this function via automplete."
echo "Not anymore!!"
functions -e inner_func
end
# Let's test this!
inner_func
end
$ outer
I still find this function via automplete.
Not anymore!!
$ inner_func
fish: Unknown command 'inner_func'
Naming Convention
_my_module_func_name
This does not erase the function from the global scope, but it is a general good practice to avoid overriding existing functions unintentionally.
Notes
Functions declared inside a main function file (or functions inside other functions like inner_func) will be available only after its parent function is invoked at least once (this is fish lazy function autoloading), and exist only for the duration of that particular shell session.

Related

Can I alias the dot sourcing command?

I find myself missing the . in dot sourced files and spending a little time getting my footing when switching from C# to PS. Is there a way to alias the . or use a similar command that is more blatantly noticeable?
In short: No, you cannot define an alias for . :
., the (dot-)sourcing operator, is an operator and as such it cannot be the target of an alias (only commands can).
Defining a function is also not an option, because it would itself have to be dot-sourced when called, in order to be able to dot-source other commands (scripts whose definitions to load into the caller's scope).

What fish function controls completion of filenames, and how can I change it?

I am trying to alter the standard logic used by fish to find filename completions. In particular, I want fish not to consider any filename that ends in a tilde (~) character, as these are emacs backup files and are not interesting.
I had assumed that the list of possible completions would be provided by a fish function, which I could then edit to remove the ones ending in tildes. But I cannot find the function. I have looked in the documentation at fishshell.com, and I have also tried functions | grep complete. What function should I be editing?
Unlike most completions file name completions is not implemented as a function; it's baked into the C++ code. See the completer_t::complete_param_expand() method in src/complete.cpp. If you can make a cogent argument for how this type of customization would be implemented I'd encourage you to open an issue.
P.S., Note that functions will not show private functions unless you invoke it as functions -a. I mention this because many completion functions are marked private by beginning their name with an underscore.

How do I find out which function is used for zsh tab completion

I want to know which function zsh is using for tab completion of a command.
For many commands (make, ls, cd …) I can apparently guess _<COMMANDNAME>, but I might actually have overriden this setting with compdef _mycd cd.
The reasons why I want to know this are two:
I might want to read some function definitions to use parts of them in functions I'm writing
I want to wrote a completion function which itself calls the completion for other commands (like one does in _nice, with the exception that I might not rely on shift; CURRENT--; _normal)
The current completion rules are stored in $_comps.
So one can display the completion rule for cd with echo $_comps[cd] and display the actual function definition with functions $_comps[cd].

combine compute, loop and concat

I would like to run the following syntax on lots of variables. Thus, I'd like to loop over a bunch of variables.
The syntax is the following:
compute v3a_mit = v3a.
recode v3a_mit
(-9998=2) (sysmis=9).
exe.
In this case, however, the syntax only concerns the variable "v3a".I have some other variables (v3b, v3c, v3d...) for which I would like to execute this syntax.
So, the loop should look like this.
DO REPEAT X=v3a to v3z
compute concat(X,'_mit') = X.
recode concat(X,'_mit')
(-9998=2) (sysmis=9).
exe.
END REPEAT.
So, within the loop, new variables shall be created which get a new name depending on the variable which is executed in the loop. The "SHIFT VALUES VARIABLE" command would be ideal (with shift=0) but this command cannot be used within a loop. Unfortunately "compute concat(X,'_mit')" does not work either.
CONCAT is a function for manipulating the values of string variables. So you can't use it for defining a variable name.
However you can make use of the !CONCAT function inside of a SPSS macro.
You can use the following macro to recode a set of variables.
DEFINE !recodeVars (vars = !CMDEND)
* for every variable in the 'vars' list do the RECODE INTO command.
!DO !var !IN (!vars)
RECODE !var (-9998=2) (sysmis=9) INTO !CONCAT(!var, '_mit').
!DOEND
!ENDDEFINE.
* macro call.
!recodeVars vars = v3a v3b v3c v3d.
Here, I used the RECODE INTO command, instead of one COMPUTE and a following RECODE command. But of course the principle of how to use the !CONCAT command would be the same for the COMPUTE operation.
However you can't call the macro in way like this !recodeVars vars = v3a TO v3z. In that case the macro would try perform the RECODE for the variables "v3a", "TO" and "v3z". You have to call this macro with the whole list of variables you want to recode.
This might be a lot of typing. As an easy way to avoid the typing, you could produce a SPSS command via the SPSS Menu (for example Analize -> Descreptive Statistics -> Frequencies) Then select the variables you want to recode (select the first variable, hold the SHIFT key and select the last variable) and then press the paste button. The Frequency command with the list of variables will be pasted to your syntax. You can now copy paste the variable list to your macro call.
If you have the Python integration plugin installed you could also use this small python block to retrieve the varlist between two variables.
BEGIN PROGRAM.
import spss,spssaux
variables = 'v3a to v3z' #Specify variables
varlist = spssaux.VariableDict().expand(variables)
spss.SetMacroValue('!varlist', ' '.join(varlist))
END PROGRAM.
This creates a macro named "!varlist" which expands to the list of variables when called.
You can now call the "!recodeVars" macro the following way: !recodeVars vars = !varlist.
If you don't have the python plugin installed (and don't want to use manual typing or copy and pasting) you can get the full variable list with the use of the "!DefList" macro from Raynald's SPSS Tools.
By the way, you can also make use of a macro for the SHIFT VALUES command.

Difference between &function and function() in perl [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When should I use the & to call a Perl subroutine?
In perl scripts, why is that method of invoking the function is written differently some times. I see &function and sometimes function(). Are they both the same and this is just a style that one would want to flaunt? If they are the same, why are they both available, would not one just suffice? I am guessing there is some semantic difference between the both the methods which distinguishes them from each other ... but at what kind of circumstances?
--- Since I cannot answer my own question for timeout reasons -- I am updating the answer in the section of question itself. When I get a chance to update the answer block, I will put it there.
I found the relevant text in 'Learning Perl' book..thanks for the tip though. Chapter 4: Subroutines -- Omitting the Ampersand.
I was more interested in ampersand & usage for perl functions. If a subroutine is already defined before being invoked, then subroutine can be invoked without using & while calling the function similar to invoking the builtin functions. & is also used to distinguish between the builtin functions and the user defined functions if the function to be invoked uses the same name that of one of the builtin function, provided it is defined before being invoked.
Usage of (), is merely to justify the passing of the arguments to the subroutines, while if not used, the default list of current arguments are passed in the form #_. If the arguments are specified in () for a subroutine, it is assumed to be a subroutine and is invoked even if not previously defined while parsing.
It has very specific uses:
& tells Perl to ignore the sub's prototype.
& can allow you to use the caller's #_. (&foo; with no parens or arguments).
goto &foo; and defined &foo.
Getting a reference (e.g. \&foo).
Some people (mostly beginners) use it to distinguish user subs from builtin functions, since builtin functions cannot be preceded by &.
As mentioned by #manatwork and #KeithThompson you can find information in these articles:
A general description - What's the difference between calling a function as &foo and foo()?
Subtle information about using & or not for a function call - perlsub: Perl Subroutines: Description.