File listing in custom autocomplete for Vim - function

I'm using the Utl plugin for Vim and I'm looking for a method of creating a custom autocomplete function to generate a link to an id tag within the file. The format I want to use is:
:CommandName <file> <id tag in file>
I'd like the function to act like the standard directory completion for the first argument. For the second argument I'd like it to search the file specified in the first argument for all strings beginning with "id=" and return the values.
I've copied a similar function out of the main Utl package, but I've yet to get close to making it work, it currently looks like this:
fu! CompleteArgs(dummy_argLead, cmdLine, dummy_cursorPos)
" Split cmdLine to figure out how many arguments have been provided by user
" so far. If Using argument keepempty=1 for split will provide empty last
" arg in case of a new arg is about to begin or an non empty last argument
" in case of an incomplete last argument. So can just remove the last arg.
exe "echo \"cmdline:\" \"".a:cmdLine."\""
let utlArgs=split(a:cmdLine, '\s\+', 1)
execute "echo" string(utlArgs)
echo "echo" "test complete"
"remove the function name
call remove(utlArgs, -1)
" 1st arg to complete
if len(utlArgs)==1
return string(glob("*"))
endi
" 2nd arg to complete
if len(utlArgs)==2
"insert code here
endif
endfun
Has anyone got any ideas?

You can try out frawor. If you install it code will be the following:
execute frawor#Setup('0.0', {'#/fwc': '0.2',
\ '#/commands': '0.0',})
" No need to write bang here: above command will forbid script to be sourced
" twice, see :h frawor#Reload for how it can be updated.
function s:F.cmdfunc(file, tag)
" It will be called when the command launches. Alternatively you can replace
" `s:F.cmdfunc' in the below command.add call with a string you already had
" before. Note that you will have to replace s: in function names with <SID>
" and s:* variables will be just unaccessible.
endfunction
function s:F.gettags(file)
" This assumes that format is the same as used in ~/.vim/doc/tags. Note that
" if there may be any spaces, then you must escape them.
return map(readfile(a:file), 'matchstr(v:val, "\\v^.{-}\\t")[:-2]')
endfunction
" This replaces direct :command call, see :h frawor-f-command.add
call s:_f.command.add('CommandName', s:F.cmdfunc,
\{ 'nargs': '+',
\'complete': ['path in*F.gettags(#<)']})

While answering a very similar question, I have written a
complete function that determines the number of a command argument to
complete. Below is a version of that function, adapted to your case.
command! -nargs=* -complete=custom,FooComplete Foo echo [<f-args>]
function! FooComplete(arg, line, pos)
let l = split(a:line[:a:pos-1], '\%(\%(\%(^\|[^\\]\)\\\)\#<!\s\)\+', 1)
let n = len(l) - index(l, 'Foo') - 1
if n == 1
return string(glob('*'))
endif
return "1\n2\n3" " Replace this with appropriate id-completion logic.
endfunction
The function properly handles escaped whitespace (as a part of an
argument, not a separator) and whitespace before the command name.
Note that whitespace characters in suggestion candidates should be
escaped, otherwise a single argument would be treated by Vim as two
arguments.

Related

How to understand tcl's return

I wanted to get the number of parameters of a proc from tcl and didn't want it to be printed in tclsh, so I used return
When I execute the following statement in the c environment
string tclCmd = "info args " + tclProcName + ";return";
Tcl_Eval(interp, tclCmd.c_str());
string res = Tcl_GetStringResult(interp);
The result of the execution is not printed in tclsh, but at the same time it is not possible to get the correct value
So I tried to write it like this:
string tclCmd = "info args " + tclProcName;
Tcl_Eval(interp, tclCmd.c_str());
string res = Tcl_GetStringResult(interp);
Tcl_Eval(interp, "return");
This works, but I don't understand why it doesn't print out immediately when Tcl_Eval is executed, and the return statement is still valid afterwards
The return command is not helpful at all there. It triggers an exception condition that comes out of Tcl_Eval as a return code of TCL_RETURN instead of TCL_OK (that is converted by the outer structure of a procedure, if that is relevant). Instead, you should call Tcl_ResetResult(interp); after you have finished with the return value (or have taken another reference to it) to put things back to how they were before your command implementation was invoked.

How to remove duplicate results from a cts:uris when distinct values does not work

I have the following code that returns an array of array of results I try to use distinct-values to remove duplicates and it does nothing I have tried also removing using looping functions by comparing values with no success.
I have tried converting to "xs anyAtomicType" and using distinct values
I have tried putting in json array and extracting the sub-array
I have tried tokenizing, xdmp quote, string-before/after and many others
declare function local:verify-user-uri($dir as xs:string)
{
for $each in cts:uris($dir, ())
let $uIds := (for $d in $each
where contains($d, "/profile.xml")
return $d)
return $uIds
};
I get back duplicated result in form of:
/users/123-343-/profile.xml
/users/122-222-/profile.xml
/users/123-343-/profile.xml
/users/122-222-/profile.xml
/users/123-343-/profile.xml
/users/122-222-/profile.xml
I am expecting:
/users/123-343-/profile.xml
/users/122-222-/profile.xml
Is it possible that you have simply invoked this function 3 times and didn't realize it?
You have declared $dir to be a single xs:string. If your $dir happened to be a sequence of strings of the same directory, or if you otherwise invoked the function 3 times with the directory variable.
It can easily happen with function mapping enabled (default behavior). https://docs.marklogic.com/guide/xquery/enhanced#id_55459
There are a couple of things that you can do as a diagnostic:
1.) Remove the explicit type on the $dir parameter in the function:
declare function local:verify-user-uri($dir)
{
for $each in cts:uris($dir, ())
let $uIds := (for $d in $each
where contains($d, "/profile.xml")
return $d)
return $uIds
};
do you get an error executing cts:uris() that looks like this:
[1.0-ml] XDMP-ARGTYPE: )err:XPT0004) cts:uris(("/users/", "/users/", "/users/"), ()) -- arg1 is not of type xs:string?
2.) try disabling function mapping by adding the following to the prolog:
declare option xdmp:mapping "false";
and see if you then get an invalid coercion error like:
[1.0-ml] XDMP-AS (err:XPTY0004) $dir as xs:string -- Invalid coersion ("/users/", "/users/", "/users/") as xs:string
3.) You could also add something to the end of the sequence of values returned from the function to indicate how many times it has executed:
declare function local:verify-user-uri($dir as xs:string)
{
for $each in cts:uris($dir, ())
let $uIds := (for $d in $each
where contains($d, "/profile.xml")
return $d)
return $uIds, "#"
};
And see how many times you see "#" in the result. If more than one, you are invoking the function multiple times.
Next to the good suggestions from Mads, I notice a couple of other things about your code:
It doesn't make sense to iterate over $each as it contains one uri only. Keep in mind that a FLWOR statement ends with a return, which tells what should be the result per item
Beware that the first arg to cts:uris only marks a start, not an end. If you feed in /aaa/, you also get back /bbb/ etc, though not vice versa.
To be honest, I think you are looking for cts:uri-match() instead, which would reduce your function to a one-liner:
declare function local:verify-user-uri($dir as xs:string) {
cts:uri-match($dir || "*/profile.xml")
};
HTH!
PS: I do recommend always disabling function mapping as Mads recommends. It can prevent a lot of confusion.

error: can't perform indexing operations for <unknown type> type when sourcing

With this code, saved as test.m
function test()
x = 1;
endfunction
I get the following error message when sourcing it via source(test.m) in the GUI:
>> clear
>> source (test.m)
x = 1
error: can't perform indexing operations for <unknown type> type
error: evaluating argument list element number 1
>>
Calling the function test via >> test works fine, but I'd like to know what I'm doing wrong here.
Progress:
calling just test, in the right dir seems to do it, but then what are we sourcing for?
If you run
source(test.m)
the interpreter tries to evaluate the "." subscript on the variable "test" (which is a function in your case) and then call source with the result.
What you want is to call the function source with the string "test.m" so you have to use quotes:
source ("test.m")
or don't use () in which case all arguments are passed as strings:
source test.m

subfunctions in vim

I created a lot of functions in menu.vim.
I noted that in many functions the same code is used that's why I decided to clean up my file with the use of
subfunctions.
p.e this is code what often returns in my functions:
let zoek = #/
if a:type == "'<,'>"
let r = substitute(zoek, '\\%V', '', 'g')
elseif a:type == "%"
let r = zoek
endif
let a = substitute(r, '\', '', 'g')
if matchstr(d, '>') == '>' || matchstr(d, '<') == '<'
let e = substitute(d, '\zs>\(\d\+\)%<\ze', '\1-', 'g')
endif
How can I create a subfunction from it? How can I invoke it?
Does Vim have subfunctions?
You can have «local» functions by defining them in the dictionary: in the following code
function MyFunc()
let d={}
function d.function()
echo "Foo"
endfunction
call d.function()
endfunction
function d.function is accessible only inside s:MyFunc and is destroyed after s:MyFunc exits. I put «local» in quotes because d.function is really global function named 42 (or another number, it does not matter). It cannot be called without a reference to it and the only way to create a reference is to use function dict.key() (references may be copied after creation, but you can't create a reference using call to function(), though it is possible for MyFunc: function("MyFunc")). Note that number (in this case 42) is incremented each time you create a function and I know neither what is the maximum number nor what will happen when it will be reached. I personally use dictionary functions because they have two other advantages:
Dictionary function defined inside a script-local dictionary cannot be reached without a debugger or explicit passing the function reference (possibly as a part of its container) somewhere.
If more then one function is defined inside a dictionary in order to purge them all you need is to unlet this dictionary. Useful for reloading plugins.
There is only one type of function in Vimscript, but I'm not sure if this is what you are already using in your menu.vim. A user-defined function is defined thus:
function! MyNewFunction()
" your code here
endfunction
You can then call this function elsewhere in your scripts (and inside other functions) using
call MyNewFunction()
Or set a variable equal to the return value of your function using
let my_variable = MyNewFunction()
Of course this is an incredibly simplistic overview, since you say your are already using functions. Much more information, including the use of variables, here:
help user-functions
Apologies if I have not answered your question.

powershell - Use a function with unknown number of variables and other parameters

I need to create a function.
First variable must be an array with an unknown number of parameters plus another variable.
My problem is that I don't know how distinguish between them.
I post an example:
function conc-str {
param([string[]]$array,[string]$name)
foreach($item in $array) {
$str+= $item
}
write-host $str
}
conc-str(#("aa","bb","cc"),"dd")
The result of this function is
aa bb ccdd
but as you can see I loop array elements and concatenate them. I thought to get just
aa bb cc
Where is my mistake?
The way you call it is:
conc-str #("aa","bb","cc") "dd"
You don't use "," as a parameter seperator in PowerShell. It is just a space. The moment you put a "," it becomes a single parameter.