function help

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

function help

uidzer0

Hey everyone,

I'm trying to write a very small function that will take a parameter
and then run an external command. This is my first time writing a
function for vim so it's all a bit foreign and unfortunately I'm not
coming up with any good documentation. Does anyone know of a good
resource/tutorial for writing functions?

Either way, I'm wondering how to concatenate strings/variables within
the function.
I've currently got something like this:

func! SubmitRequest(loginURL, username, password)
    let loginURL = a:loginURL
    let username = a:username
    let password = a:password

    " get authenticated, save the cookie
    :!curl -c /tmp/request_auth -D /tmp/request_auth_headers <URL
HERE>?username=<username here>&password=<password here>
endfunc

So the part I'm missing is how to get the variables into the external
command string. Can anyone help shed some light here?

Thanks!

Ben


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

Gary Johnson

On 2007-10-26, uidzer0 <[hidden email]> wrote:
> Hey everyone,
>
> I'm trying to write a very small function that will take a parameter
> and then run an external command. This is my first time writing a
> function for vim so it's all a bit foreign and unfortunately I'm not
> coming up with any good documentation. Does anyone know of a good
> resource/tutorial for writing functions?

   :help 41.7

I found that by executing

   :help toc

which will show the User Manual table of contents, and then searched
for "function".

> Either way, I'm wondering how to concatenate strings/variables within
> the function.
> I've currently got something like this:
>
> func! SubmitRequest(loginURL, username, password)
>     let loginURL = a:loginURL
>     let username = a:username
>     let password = a:password
>
>     " get authenticated, save the cookie
>     :!curl -c /tmp/request_auth -D /tmp/request_auth_headers <URL
> HERE>?username=<username here>&password=<password here>
> endfunc
>
> So the part I'm missing is how to get the variables into the external
> command string. Can anyone help shed some light here?

Use the :execute command for this.  For example,

   :exe "!echo" &tw

or equivalently,

   :exe "!echo " . &tw

Note the two forms of concatenation.  The execute command expands
each of its arguments, concatenates them with a single space between
each pair, and executes the result.  The first form above has two
arguments, the string "!echo" and the value of the 'tw' option.  The
second form above has only one argument, a string that is the
concatenation of the string "!echo " with the value of the 'tw'
option.  The period (.) is vim's string concatenation operator.  See

   :help exe
   :help expr-.

I think you would write your curl command like this, but I haven't
tested it.

   :exe "!curl -c /tmp/request_auth -D /tmp/request_auth_headers " . loginURL .  "?username=" . username . "&password=" . password

The spaces on either side of the period are not necessary--that's
just my style preference.

HTH,
Gary

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

Andy Kittner
In reply to this post by uidzer0
On Fri, Oct 26, 2007 at 05:51:01PM -0000, uidzer0 wrote:
>
> Hey everyone,
>
> I'm trying to write a very small function that will take a parameter
> and then run an external command. This is my first time writing a
> function for vim so it's all a bit foreign and unfortunately I'm not
> coming up with any good documentation. Does anyone know of a good
> resource/tutorial for writing functions?
:help expr might be a good starting point, then I'd suggest
:help function-list to see what builtin functions vim has

> Either way, I'm wondering how to concatenate strings/variables within
> the function.
> I've currently got something like this:
>
> func! SubmitRequest(loginURL, username, password)
>     let loginURL = a:loginURL
>     let username = a:username
>     let password = a:password
>
>     " get authenticated, save the cookie
>    :!curl -c /tmp/request_auth -D /tmp/request_auth_headers <URL HERE>?username=<username here>&password=<password here>
     exe "!curl -c /tmp/request_auth -D /tmp/request_auth_headers "
     \   .loginUrl."?username=".username"&password=".password
should work.
If you dont't want to modify username, password, etc. in your function
you can also use a:username directly without copying them into local
variables first.

> endfunc
>
> So the part I'm missing is how to get the variables into the external
> command string. Can anyone help shed some light here?

You need to use :exe (short for :execute) because you want to construct
a command 'dynamically'. Then you can use any expression to build the
command you want to run. (in this case the string-concatenation
operator, see :he expr-.)

Also note that functions are parsed in 'ex-mode' so you don't need to
prefix ex-commands with a colon.
> Thanks!
>
> Ben
>

Regards Andy
--
Fashions have done more harm than revolutions.
                -- Victor Hugo

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: function help

uidzer0

Perfect, thanks!

On Oct 26, 1:17 pm, Andy Kittner <[hidden email]> wrote:

> On Fri, Oct 26, 2007 at 05:51:01PM -0000, uidzer0 wrote:
>
> > Hey everyone,
>
> > I'm trying to write a very small function that will take a parameter
> > and then run an external command. This is my first time writing a
> > function for vim so it's all a bit foreign and unfortunately I'm not
> > coming up with any good documentation. Does anyone know of a good
> > resource/tutorial for writing functions?
>
> :help expr might be a good starting point, then I'd suggest
> :help function-list to see what builtin functions vim has> Either way, I'm wondering how to concatenate strings/variables within
> > the function.
> > I've currently got something like this:
>
> > func! SubmitRequest(loginURL, username, password)
> >     let loginURL = a:loginURL
> >     let username = a:username
> >     let password = a:password
>
> >     " get authenticated, save the cookie
> >    :!curl -c /tmp/request_auth -D /tmp/request_auth_headers <URL HERE>?username=<username here>&password=<password here>
>
>      exe "!curl -c /tmp/request_auth -D /tmp/request_auth_headers "
>      \   .loginUrl."?username=".username"&password=".password
> should work.
> If you dont't want to modify username, password, etc. in your function
> you can also use a:username directly without copying them into local
> variables first.
>
> > endfunc
>
> > So the part I'm missing is how to get the variables into the external
> > command string. Can anyone help shed some light here?
>
> You need to use :exe (short for :execute) because you want to construct
> a command 'dynamically'. Then you can use any expression to build the
> command you want to run. (in this case the string-concatenation
> operator, see :he expr-.)
>
> Also note that functions are parsed in 'ex-mode' so you don't need to
> prefix ex-commands with a colon.
>
> > Thanks!
>
> > Ben
>
> Regards Andy
> --
> Fashions have done more harm than revolutions.
>                 -- Victor Hugo
>
>  application_pgp-signature_part
> 1KDownload


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

uidzer0
In reply to this post by Andy Kittner

One more quick question... How do I get the current filename?

On Oct 26, 1:17 pm, Andy Kittner <[hidden email]> wrote:

> On Fri, Oct 26, 2007 at 05:51:01PM -0000, uidzer0 wrote:
>
> > Hey everyone,
>
> > I'm trying to write a very small function that will take a parameter
> > and then run an external command. This is my first time writing a
> > function for vim so it's all a bit foreign and unfortunately I'm not
> > coming up with any good documentation. Does anyone know of a good
> > resource/tutorial for writing functions?
>
> :help expr might be a good starting point, then I'd suggest
> :help function-list to see what builtin functions vim has> Either way, I'm wondering how to concatenate strings/variables within
> > the function.
> > I've currently got something like this:
>
> > func! SubmitRequest(loginURL, username, password)
> >     let loginURL = a:loginURL
> >     let username = a:username
> >     let password = a:password
>
> >     " get authenticated, save the cookie
> >    :!curl -c /tmp/request_auth -D /tmp/request_auth_headers <URL HERE>?username=<username here>&password=<password here>
>
>      exe "!curl -c /tmp/request_auth -D /tmp/request_auth_headers "
>      \   .loginUrl."?username=".username"&password=".password
> should work.
> If you dont't want to modify username, password, etc. in your function
> you can also use a:username directly without copying them into local
> variables first.
>
> > endfunc
>
> > So the part I'm missing is how to get the variables into the external
> > command string. Can anyone help shed some light here?
>
> You need to use :exe (short for :execute) because you want to construct
> a command 'dynamically'. Then you can use any expression to build the
> command you want to run. (in this case the string-concatenation
> operator, see :he expr-.)
>
> Also note that functions are parsed in 'ex-mode' so you don't need to
> prefix ex-commands with a colon.
>
> > Thanks!
>
> > Ben
>
> Regards Andy
> --
> Fashions have done more harm than revolutions.
>                 -- Victor Hugo
>
>  application_pgp-signature_part
> 1KDownload


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

Gary Johnson

On 2007-10-26, uidzer0 <[hidden email]> wrote:
> One more quick question... How do I get the current filename?

   expand("%")

You can also use file name modifiers to get the full path, or just
the base name, or some other variation of the file name.  See

   :help expand()
   :help fnamemodify()
   :help filename-modifiers

Gary

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

uidzer0

Thats exactly what I was looking for; thanks!

On Oct 26, 2:02 pm, Gary Johnson <[hidden email]> wrote:

> On 2007-10-26, uidzer0 <[hidden email]> wrote:
>
> > One more quick question... How do I get the current filename?
>
>    expand("%")
>
> You can also use file name modifiers to get the full path, or just
> the base name, or some other variation of the file name.  See
>
>    :help expand()
>    :help fnamemodify()
>    :help filename-modifiers
>
> Gary


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

Tony Mechelynck
In reply to this post by uidzer0

uidzer0 wrote:
> One more quick question... How do I get the current filename?
>

In some ex-commands (such as the most common "file-oriented" commands), and in
external commands, you can use % when you want to refer to the current
editfile. This is not necessarily a full path though: it may be a relative
pathfilename. If you want a full path in these cases, use %:p

In the general case, expand('%') will give you the current editfile (possibly
with a relative path), and fnamemodify(expand('%'),':p') will expand it to a
full path.

See
        :help current-file
        :help expand()
        :help fnamemodify()
        :help filename-modifiers


Best regards,
Tony.
--
Bacchus, n.:
        A convenient deity invented by the ancients as an excuse for
getting drunk.
                -- Ambrose Bierce, "The Devil's Dictionary"


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: function help

Ben Schmidt
In reply to this post by uidzer0

> I'm trying to write a very small function that will take a parameter
> and then run an external command. This is my first time writing a
> function for vim so it's all a bit foreign and unfortunately I'm not
> coming up with any good documentation. Does anyone know of a good
> resource/tutorial for writing functions?

I don't know of any tutorial. But the Vim help is quite good. It seems you've
found it but a lot of useful stuff is at

:help expr

> Either way, I'm wondering how to concatenate strings/variables within
> the function.

The '.' operator concatenates strings.

> I've currently got something like this:
>
> func! SubmitRequest(loginURL, username, password)
>     let loginURL = a:loginURL
>     let username = a:username
>     let password = a:password
>
>     " get authenticated, save the cookie
>     :!curl -c /tmp/request_auth -D /tmp/request_auth_headers <URL
> HERE>?username=<username here>&password=<password here>
> endfunc

Often you need to 'construct' a command using the '.' and use it as the argument
to the 'execute' ('exe') command to run something complicated. You want something
like:

exe "!curl blah_blah_blah " . urlVariable . " more_command"

Happy hacking!

Ben.



Send instant messages to your online friends http://au.messenger.yahoo.com 


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---