Quantcast

Launching an already open file

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Launching an already open file

E. Wing
I have a small in-app feature where I can open my application's Lua
scripts with the default text editor registered in the system. I also
like to open the Lua scripts to a specific line number if I detect an
error. With MacVim, I invoke it with command line switches: -g +
(line_number)

This works great if the file is not already open. If the file is open,
Vim prompts me there is a .swp file and allows me pick an action. But
in this case, what I would prefer is that Vim just silently bring the
already existing open window to the front (ideally with the correct
tab selected if tabs are in use). Gold star if I can also get this
already open window to move to the requested line number.

Is there any way to achieve this? Perhaps there is some special
command line argument I can use to suppress prompts and already does
what I want? Or if not, is there an AppleScript way to control this?
(Incidentally, I'm working on a Lua/Cocoa bridge which has Scripting
Bridge support so in theory, I may be able to write a Lua script to
control this for me.)

Thanks,
Eric

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Launching an already open file

Björn Winckler
On 3 November 2010 09:10, E. Wing <[hidden email]> wrote:

> I have a small in-app feature where I can open my application's Lua
> scripts with the default text editor registered in the system. I also
> like to open the Lua scripts to a specific line number if I detect an
> error. With MacVim, I invoke it with command line switches: -g +
> (line_number)
>
> This works great if the file is not already open. If the file is open,
> Vim prompts me there is a .swp file and allows me pick an action. But
> in this case, what I would prefer is that Vim just silently bring the
> already existing open window to the front (ideally with the correct
> tab selected if tabs are in use). Gold star if I can also get this
> already open window to move to the requested line number.
>
> Is there any way to achieve this? Perhaps there is some special
> command line argument I can use to suppress prompts and already does
> what I want? Or if not, is there an AppleScript way to control this?
> (Incidentally, I'm working on a Lua/Cocoa bridge which has Scripting
> Bridge support so in theory, I may be able to write a Lua script to
> control this for me.)

There is an AppleScript way (this is how Xcode does it): send an odoc
event to open a file with a "keyAEPosition" descriptor the data of
which should be the following struct

struct
{
    int16_t unused1;      // 0 (not used)
    int16_t lineNum;      // line to select (< 0 to specify range)
    int32_t startRange;   // start of selection range (if line < 0)
    int32_t endRange;     // end of selection range (if line < 0)
    int32_t unused2;      // 0 (not used)
    int32_t theDate;      // modification date/time
};

See, the -[MMAppController extractArgumentsFromOdocEvent:] for more
details on how MacVim parses this.

Alternatively, you may be able to use the mvim:// URL handler, see ":h mvim://".

Finally, if you have a specific MacVim window that you wish to open a
file in then you should use the --servername and --remote family of
arguments to the "mvim" script, e.g.

mvim --servername VIM --remote-silent filename

Björn

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Launching an already open file

E. Wing

> Alternatively, you may be able to use the mvim:// URL handler, see ":h mvim://".

Thanks, the URL handler did exactly what I needed it to and only took
me a line of code.

-Eric

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Launching an already open file

ghuiber
I have a similar problem. I use Vim to edit Stata scripts (stata.com). These scripts are called do-files. They are simple text files, but with a .do extension, which Stata uses to recognize them as scripts. The Stata command for executing such a script, say called filename.do, is "do filename".

In Windows, there is a way to launch Stata from Vim to execute either a do-file, or a set of selected lines in a do-file that is being edited in Vim. This can be repeated as many times as you want, whether or not Stata is already running, inside the same instance of Stata. I would like to get MacVim to do the same.

The Windows solution consists of two parts. The first is a rundo.exe file written with an AutoIt script as described here -- http://s281191135.onlinehome.us/2008/20080427-stata.html. The second is a Vim script as described here -- http://www.stata.com/statalist/archive/2006-06/msg00905.html.

This Vim script goes into _gvimrc, and it describes two Vim functions: RunIt() for making Stata execute a do-file being edited in Vim, and RunDoLines() for making Stata execute a subset of selected lines of that file.

For my first attempt at getting this to work in MacVim, I modified the script as shown below:

" STATA DO-FILE SCRIPT

function RunIt()
  wa
  !open -a StataMP --args do "%:p"
endfunction

:map <F7> :<C-U>call RunIt()
:imap <F7> <Esc>:<C-U>call RunIt()

function RunDoLines()
  let selectedLines = getbufline('%', line("'<"), line("'>"))

 if col("'>") < strlen(getline(line("'>")))
   let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>"))
 endif
 if col("'<") != 1
   let selectedLines[0] = strpart(selectedLines[0], col("'<")-1)
 endif

 let temp = tempname() . ".do"
   call writefile(selectedLines, temp)

   exec "!open -a StataMP --args do " . temp

   " Delete the temp file after Vim closes
   au VimLeave * silent exe '!rm "'.$TEMP.'\*.do"'
endfunction

:map <F8> :<C-U>call RunDoLines()
:imap <F8> <Esc>:<C-U>call RunDoLines()

Unfortunately, this doesn't work all that well. Either RunIt() or RunDoLines() will work as expected only once. All subsequent calls to either function will be ignored.

What am I missing?

Thank you,
Gabi
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Launching an already open file

Ben Schmidt
> " STATA DO-FILE SCRIPT
>
> function RunIt()
>    wa
>    !open -a StataMP --args do "%:p"
> endfunction
>
> :map <F7> :<C-U>call RunIt()
> :imap <F7> <Esc>:<C-U>call RunIt()

I don't really think this is the answer to your problem, but you
probably want <CR> at the end of your mappings.

:map <F7> :<C-U>call RunIt()<CR>
:imap <F7> <Esc>:<C-U>call RunIt()<CR>

> Unfortunately, this doesn't work all that well. Either RunIt() or
> RunDoLines() will work as expected only once. All subsequent calls to either
> function will be ignored.

Does StataMP quit after running once or remain open? If it remains open,
perhaps that is the problem, as it can't be given arguments if it isn't
being launched. You may need to find another way to communicate with it
if it is already open, e.g. AppleScript:

!osascript -e 'tell app "StataMP" to open "%:p"'

That will probably launch the app if it isn't already running, too.

Ben.



--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fwd: Launching an already open file

ghuiber
Meant to send this here. Sorry,

Gabi

---------- Forwarded message ----------
From: Gabi Huiber <[hidden email]>
Date: Thu, Aug 4, 2011 at 5:30 PM
Subject: Re: Launching an already open file
To: Ben Schmidt <[hidden email]>


Thanks for the <CR> pointer. it was driving me nuts that I kept having
to hit CR after fn+F7+CR.  Then I saw in :map that a bunch of key
bindings are defined with <CR> and some even with <CR><CR>,
experimented with both, and decided on the latter.

I'm making some progress. StataMP will respond to this:

function RunIt()
 wa
 !rm -Rf ~/Library/Saved*/com.stata*
 !osascript -e 'tell app "StataMP" to open "%:p"'
endfunction

:map <F7> :<C-U>call RunIt()<CR><CR>
:imap <F7> <Esc>:<C-U>call RunIt()<CR><CR>

But it will not actually do the do-file (that is, execute it
verbosely). Instead, it opens it for editing in its own editor. Still,
I get no error messages, no hangs, and RunIt() can be launched as many
times as needed. It won't open another instance of Stata, which is
good. Now I only need to figure out the Apple event that will
interpret "tell app to open" as "make app execute it" instead of "make
app edit it" when app=StataMP.

Thank you,

Gabi

On Wed, Aug 3, 2011 at 9:05 PM, Ben Schmidt
<[hidden email]> wrote:

>> " STATA DO-FILE SCRIPT
>>
>> function RunIt()
>>   wa
>>   !open -a StataMP --args do "%:p"
>> endfunction
>>
>> :map <F7> :<C-U>call RunIt()
>> :imap <F7> <Esc>:<C-U>call RunIt()
>
> I don't really think this is the answer to your problem, but you
> probably want <CR> at the end of your mappings.
>
> :map <F7> :<C-U>call RunIt()<CR>
> :imap <F7> <Esc>:<C-U>call RunIt()<CR>
>
>> Unfortunately, this doesn't work all that well. Either RunIt() or
>> RunDoLines() will work as expected only once. All subsequent calls to
>> either
>> function will be ignored.
>
> Does StataMP quit after running once or remain open? If it remains open,
> perhaps that is the problem, as it can't be given arguments if it isn't
> being launched. You may need to find another way to communicate with it
> if it is already open, e.g. AppleScript:
>
> !osascript -e 'tell app "StataMP" to open "%:p"'
>
> That will probably launch the app if it isn't already running, too.
>
> Ben.
>
>
>
>

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Launching an already open file

Charles Campbell
In reply to this post by ghuiber
ghuiber wrote:

> I have a similar problem. I use Vim to edit Stata scripts (stata.com). These
> scripts are called do-files. They are simple text files, but with a .do
> extension, which Stata uses to recognize them as scripts. The Stata command
> for executing such a script, say called filename.do, is "do filename".
>
> In Windows, there is a way to launch Stata from Vim to execute either a
> do-file, or a set of selected lines in a do-file that is being edited in
> Vim. This can be repeated as many times as you want, whether or not Stata is
> already running, inside the same instance of Stata. I would like to get
> MacVim to do the same.
>
> The Windows solution consists of two parts. The first is a rundo.exe file
> written with an AutoIt script as described here --
> http://s281191135.onlinehome.us/2008/20080427-stata.html. The second is a
> Vim script as described here --
> http://www.stata.com/statalist/archive/2006-06/msg00905.html.
>
> This Vim script goes into _gvimrc, and it describes two Vim functions:
> RunIt() for making Stata execute a do-file being edited in Vim, and
> RunDoLines() for making Stata execute a subset of selected lines of that
> file.
>
> For my first attempt at getting this to work in MacVim, I modified the
> script as shown below:
>
> " STATA DO-FILE SCRIPT
>
> function RunIt()
>    wa
>    !open -a StataMP --args do "%:p"
> endfunction
>
> :map<F7>  :<C-U>call RunIt()
> :imap<F7>  <Esc>:<C-U>call RunIt()
>
> function RunDoLines()
>    let selectedLines = getbufline('%', line("'<"), line("'>"))
>
>   if col("'>")<  strlen(getline(line("'>")))
>     let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>"))
>   endif
>   if col("'<") != 1
>     let selectedLines[0] = strpart(selectedLines[0], col("'<")-1)
>   endif
>
>   let temp = tempname() . ".do"
>     call writefile(selectedLines, temp)
>
>     exec "!open -a StataMP --args do " . temp
>
>     " Delete the temp file after Vim closes
>     au VimLeave * silent exe '!rm "'.$TEMP.'\*.do"'
> endfunction
>
> :map<F8>  :<C-U>call RunDoLines()
> :imap<F8>  <Esc>:<C-U>call RunDoLines()
>
>    
I'm more curious than otherwise; have you considered using RunVIew with
let b:runview_filtcmd="open -a StataMP --args do"?

You may get a new version of RunView from:

    http://mysite.verizon.net/astronaut/vim/index.html#RUNVIEW     (beta)

To install, simply:

    vim RunView.vba.gz
    :so %
    :q

I don't have StataMP to test this out, so the b:runview_filtcmd may or
may not be correct for RunView and StatMP.
If it is correct, you'd want to have it in
.vim/ftplugin/[pick-a-suffix].vim or
.vim/ftplugin/[pick-a-suffix]/statmp.vim .

Regards,
Chip Campbell

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Launching an already open file

ghuiber
In case anybody is curious, and for the sake of thorough
documentation: I found a solution, with help from the Statalist.
Rather than the Vim plugin Chip Campbell suggested, I ended up leaving
it to the OS. Two very minimalistic Vim functions call a bash script
each. This does the job.

The whole conversation can be traced back from here:
http://www.stata.com/statalist/archive/2011-09/msg00556.html. The
entire code is shown as well.

Best,
Gabi


On Fri, Aug 5, 2011 at 11:09 AM, Charles Campbell
<[hidden email]> wrote:

> ghuiber wrote:
>>
>> I have a similar problem. I use Vim to edit Stata scripts (stata.com).
>> These
>> scripts are called do-files. They are simple text files, but with a .do
>> extension, which Stata uses to recognize them as scripts. The Stata
>> command
>> for executing such a script, say called filename.do, is "do filename".
>>
>> In Windows, there is a way to launch Stata from Vim to execute either a
>> do-file, or a set of selected lines in a do-file that is being edited in
>> Vim. This can be repeated as many times as you want, whether or not Stata
>> is
>> already running, inside the same instance of Stata. I would like to get
>> MacVim to do the same.
>>
>> The Windows solution consists of two parts. The first is a rundo.exe file
>> written with an AutoIt script as described here --
>> http://s281191135.onlinehome.us/2008/20080427-stata.html. The second is a
>> Vim script as described here --
>> http://www.stata.com/statalist/archive/2006-06/msg00905.html.
>>
>> This Vim script goes into _gvimrc, and it describes two Vim functions:
>> RunIt() for making Stata execute a do-file being edited in Vim, and
>> RunDoLines() for making Stata execute a subset of selected lines of that
>> file.
>>
>> For my first attempt at getting this to work in MacVim, I modified the
>> script as shown below:
>>
>> " STATA DO-FILE SCRIPT
>>
>> function RunIt()
>>   wa
>>   !open -a StataMP --args do "%:p"
>> endfunction
>>
>> :map<F7>  :<C-U>call RunIt()
>> :imap<F7>  <Esc>:<C-U>call RunIt()
>>
>> function RunDoLines()
>>   let selectedLines = getbufline('%', line("'<"), line("'>"))
>>
>>  if col("'>")<  strlen(getline(line("'>")))
>>    let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>"))
>>  endif
>>  if col("'<") != 1
>>    let selectedLines[0] = strpart(selectedLines[0], col("'<")-1)
>>  endif
>>
>>  let temp = tempname() . ".do"
>>    call writefile(selectedLines, temp)
>>
>>    exec "!open -a StataMP --args do " . temp
>>
>>    " Delete the temp file after Vim closes
>>    au VimLeave * silent exe '!rm "'.$TEMP.'\*.do"'
>> endfunction
>>
>> :map<F8>  :<C-U>call RunDoLines()
>> :imap<F8>  <Esc>:<C-U>call RunDoLines()
>>
>>
>
> I'm more curious than otherwise; have you considered using RunVIew with let
> b:runview_filtcmd="open -a StataMP --args do"?
>
> You may get a new version of RunView from:
>
>   http://mysite.verizon.net/astronaut/vim/index.html#RUNVIEW     (beta)
>
> To install, simply:
>
>   vim RunView.vba.gz
>   :so %
>   :q
>
> I don't have StataMP to test this out, so the b:runview_filtcmd may or may
> not be correct for RunView and StatMP.
> If it is correct, you'd want to have it in .vim/ftplugin/[pick-a-suffix].vim
> or .vim/ftplugin/[pick-a-suffix]/statmp.vim .
>
> Regards,
> Chip Campbell
>
> --
> You received this message from the "vim_mac" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Loading...