Loading ...
Sorry, an error occurred while loading the content.

Re: mvim: bring forward window/tab of already open window instead of Alert

Expand Messages
  • björn
    ... I forgot to answer this: when using --remote you have to pass arguments before the --remote switch. All arguments after --remote are assumed to be file
    Message 1 of 8 , Mar 7, 2011
    • 0 Attachment
      On 6 March 2011 00:36, E. Wing wrote:
      >
      > I looked at the --remote-tab-silent switch. It seems close to what I
      > want, but I don't want these things in new tabs, but windows when not
      > already open. It also seems to get confused if I have an additional
      > parameter like the line number:
      > mvim --remote-tab-silent-switch +60
      > Instead of jumping to line 60, it opens a second tab called +60.

      I forgot to answer this: when using --remote you have to pass
      arguments before the --remote switch. All arguments after --remote
      are assumed to be file names, see ":h --remote".

      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
    • Eric Wing
      ... Is there any documentation on how to use expressions? I m unfamiliar with the concept/syntax. Out of curiosity, is the Scripting Bridge support robust
      Message 2 of 8 , Mar 9, 2011
      • 0 Attachment
        On 3/7/11, björn <bjorn.winckler@...> wrote:
        > On 6 March 2011 00:36, E. Wing wrote:
        >> I use mvim a lot to open files. I often get into situations where I
        >> have so many open files, I don't remember which I left open and which
        >> I closed. And when I try to open an already open file, I get the
        >> Alert: "Swap file "foo.swp" already exists!" and a bunch of options,
        >> none of which brings the current open window/tab forward.
        >>
        >> Is there a way to get mvim to bring forward the already open window or
        >> tab of the file I request (assuming it is open)? And if the file isn't
        >> already open, I would expect mvim to do what it currently does.
        >>
        >> I looked at the --remote-tab-silent switch. It seems close to what I
        >> want, but I don't want these things in new tabs, but windows when not
        >> already open. It also seems to get confused if I have an additional
        >> parameter like the line number:
        >> mvim --remote-tab-silent-switch +60
        >> Instead of jumping to line 60, it opens a second tab called +60.
        >>
        >> Any suggestions?
        >
        > The only easy way to achieve this is to use "open -a MacVim filename"
        > but that won't let you pass parameters.
        >
        > I don't have an easy solution if you need to pass parameters as well.
        > The only thing I can think of is to write a script which queries all
        > open Vim servers for a list of open filenames using --serverlist to
        > get all Vim servers, then --remote-expr to query each server for open
        > files. If a file is found to be open, then raise that server with
        > --remote-send. This is done by MacVim, the relevant code is inside
        > MMAppController.m -- search for "addVimInput" (which is basically
        > --remote-send) and "evaluateVimExpression" (which is basically
        > --remote-expr).
        >
        > Björn

        Is there any documentation on how to use expressions? I'm unfamiliar
        with the concept/syntax.

        Out of curiosity, is the Scripting Bridge support robust enough to
        attempt it that way? Is that a bad idea?


        Thanks,
        Eric
        --
        Beginning iPhone Games Development
        http://playcontrol.net/iphonegamebook/

        --
        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
      • Seth Milliken
        You might also want to look at :help switchbuf . -- You received this message from the vim_mac maillist. Do not top-post! Type your reply below the text you
        Message 3 of 8 , Mar 9, 2011
        • 0 Attachment
          You might also want to look at :help 'switchbuf'.

          --
          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
        • björn
          ... Expressions means Vim script expressions . So you ll have to read up on scripting Vim. The reference is at :h eval , the guide is at :h usr_41 . I
          Message 4 of 8 , Mar 10, 2011
          • 0 Attachment
            On 10 March 2011 00:22, Eric Wing wrote:
            > On 3/7/11, björn <bjorn.winckler@...> wrote:
            >> On 6 March 2011 00:36, E. Wing wrote:
            >>> I use mvim a lot to open files. I often get into situations where I
            >>> have so many open files, I don't remember which I left open and which
            >>> I closed. And when I try to open an already open file, I get the
            >>> Alert: "Swap file "foo.swp" already exists!" and a bunch of options,
            >>> none of which brings the current open window/tab forward.
            >>>
            >>> Is there a way to get mvim to bring forward the already open window or
            >>> tab of the file I request (assuming it is open)? And if the file isn't
            >>> already open, I would expect mvim to do what it currently does.
            >>>
            >>> I looked at the --remote-tab-silent switch. It seems close to what I
            >>> want, but I don't want these things in new tabs, but windows when not
            >>> already open. It also seems to get confused if I have an additional
            >>> parameter like the line number:
            >>> mvim --remote-tab-silent-switch +60
            >>> Instead of jumping to line 60, it opens a second tab called +60.
            >>>
            >>> Any suggestions?
            >>
            >> The only easy way to achieve this is to use "open -a MacVim filename"
            >> but that won't let you pass parameters.
            >>
            >> I don't have an easy solution if you need to pass parameters as well.
            >> The only thing I can think of is to write a script which queries all
            >> open Vim servers for a list of open filenames using --serverlist to
            >> get all Vim servers, then --remote-expr to query each server for open
            >> files.  If a file is found to be open, then raise that server with
            >> --remote-send.  This is done by MacVim, the relevant code is inside
            >> MMAppController.m -- search for "addVimInput" (which is basically
            >> --remote-send) and "evaluateVimExpression" (which is basically
            >> --remote-expr).
            >
            > Is there any documentation on how to use expressions? I'm unfamiliar
            > with the concept/syntax.

            Expressions means "Vim script expressions". So you'll have to read up
            on scripting Vim. The reference is at ":h eval", the guide is at ":h
            usr_41". I believe all the scripts you need are in the source code I
            pointed you to (it can be a bit hard to "extract" however).

            > Out of curiosity, is the Scripting Bridge support robust enough to
            > attempt it that way? Is that a bad idea?

            Do you mean is it possible to use Apple Script to control MacVim?
            Then the answer is mostly no. You can do things like quit the app and
            change window size I believe, but that's about it. (I've never used
            it.)

            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
          • Eric Wing
            I thought I would share what I have so far. I wasn t able to figure out how to pass parameters like line numbers for already open files, but for my usage, it s
            Message 5 of 8 , Mar 12, 2011
            • 0 Attachment
              I thought I would share what I have so far. I wasn't able to figure
              out how to pass parameters like line numbers for already open files,
              but for my usage, it's not that important to me. The avoidance of the
              alert/lock and bringing forward the already open window/tab was key
              for me.

              Maybe others are interested in this or can improve it. And maybe it is
              reasonable enough for inclusion into mainline?

              Script pasted below.

              Thanks,
              Eric
              --
              Beginning iPhone Games Development
              http://playcontrol.net/iphonegamebook/



              #!/bin/sh
              #
              # This shell script passes all its arguments to the binary inside the
              # MacVim.app application bundle. If you make links to this script as view,
              # gvim, etc., then it will peek at the name used to call it and set options
              # appropriately.
              #
              # Based on a script by Wout Mertens and suggestions from Laurent Bihanic. This
              # version is the fault of Benji Fisher, 16 May 2005 (with modifications by Nico
              # Weber and Bjorn Winckler, Aug 13 2007).
              # First, check "All the Usual Suspects" for the location of the Vim.app bundle.
              # You can short-circuit this by setting the VIM_APP_DIR environment variable
              # or by un-commenting and editing the following line:
              # VIM_APP_DIR=/Applications

              if [ -z "$VIM_APP_DIR" ]
              then
              myDir="`dirname "$0"`"
              myAppDir="$myDir/../Applications"
              for i in ~/Applications ~/Applications/vim $myDir $myDir/vim
              $myAppDir $myAppDir/vim /Applications /Applications/vim
              /Applications/Utilities /Applications/Utilities/vim; do
              if [ -x "$i/MacVim.app" ]; then
              VIM_APP_DIR="$i"
              break
              fi
              done
              fi
              if [ -z "$VIM_APP_DIR" ]
              then
              echo "Sorry, cannot find MacVim.app. Try setting the VIM_APP_DIR
              environment variable to the directory containing MacVim.app."
              exit 1
              fi
              binary="$VIM_APP_DIR/MacVim.app/Contents/MacOS/Vim"

              # Next, peek at the name used to invoke this script, and set options
              # accordingly.

              name="`basename "$0"`"
              gui=
              opts=

              # GUI mode, implies forking
              case "$name" in m*|g*|rg*) gui=true ;; esac

              # Restricted mode
              case "$name" in r*) opts="$opts -Z";; esac

              # vimdiff and view
              case "$name" in
              *vimdiff)
              opts="$opts -dO"
              ;;
              *view)
              opts="$opts -R"
              ;;
              esac

              # Last step: fire up vim.
              # The program should fork by default when started in GUI mode, but it does
              # not; we work around this when this script is invoked as "gvim" or "rgview"
              # etc., but not when it is invoked as "vim -g".
              if [ "$gui" ]; then
              # Note: this isn't perfect, because any error output goes to the
              # terminal instead of the console log.
              # But if you use open instead, you will need to fully qualify the
              # path names for any filenames you specify, which is hard.
              did_find_file=0
              declare -a found_file_list


              # The following code tries to handle the case where a file is already
              # open in a window/tab. Instead of alerting the user the file is
              # already open, we should just bring the window/tab forward.

              # Get the list of VIM servers so we can query each one to find if it
              # contains the file we are looking for
              serverlist=`"$binary" --serverlist`
              for server in $serverlist; do
              # echo "server is $server"

              # Lookup each file in this server to see if it contains the an already
              # open window/tab we are looking for.
              for a_file in "${1:+"$@"}"; do
              # echo "a_file $a_file"

              # This remote expression found in MMAppController.m,
              # (see evaluateVimExpression) will return the number of
              # buffers matching our request. If we get more than 0 back,
              # the server already has the file we are looking for open.
              # We request one file here, so the max we get back should be 1.
              ret_num=`"$binary" --servername $server --remote-expr
              "map([\"$a_file\"],\"bufloaded(v:val)\")"`
              # echo "ret_num $ret_num"

              if [ "$ret_num" != "" ] && [ "$ret_num" -gt 0 ]; then
              # The server has the window/tab we are looking for.
              # This remote-send command will send some commands to
              # make the window/tab come forward.
              `"$binary" -g $opts --servername $server --remote-send
              "<C-\\><C-N>:let oldswb=&swb|let &swb=\"useopen,usetab\" | tab sb
              $a_file |let &swb=oldswb|unl oldswb|cal foreground()<CR><CR>"`

              did_find_file=1
              # echo "found file: $a_file, adding to found_file_list"
              # Because multiple files could be passed as arguments,
              # we need to keep track of which files we found so we
              # can later distinguish which files we need to create/open.
              found_file_list[${#found_file_list[*]}]=$a_file
              #found_file_list=( "${found_file_list[@]}" "$a_file" )
              fi
              done

              done

              # If multiple files were listed as arguments, there is a possibility
              # there is a mixture of both types.
              # So we need to determine the list of files we didn't find already open
              # and open those up.

              if [ "$did_find_file" == 0 ]; then
              # If we found no files at all, we have a trivial case and can just
              # do the standard behavior.
              exec "$binary" -g $opts ${1:+"$@"}
              else
              declare -a new_file_list

              # We will loop through all files and compare to all files we found
              # to be open. If the file was not opened, we add this to our
              # new_file_list so we can act on it later.
              for a_file in "${1:+"$@"}"; do
              # echo "a_file2 $a_file"
              # Initialize flag...assume we need to open new file until we find it
              need_to_create_file=1

              # for-in syntax doesn't seem to work with arrays
              # ...can only get first value.
              # Using numeric for instead
              for ((i=0; i<${#found_file_list[*]}; i++)); do
              found_file=${found_file_list[$i]}
              # echo "found_file2 $found_file"
              if [ "$found_file" == "$a_file" ]; then
              # echo "$found_file == $a_file"
              # We can skip this file
              need_to_create_file=0
              break
              fi
              done
              if [ $need_to_create_file == 1 ]; then
              # Append the file to the list
              new_file_list[${#new_file_list[*]}]=$a_file
              fi
              done

              # If there are remaining files, pass the list to open new windows
              if (( ${#new_file_list[@]} > 0 )); then
              exec "$binary" -g $opts "${new_file_list[@]}"
              fi
              fi
              else
              exec "$binary" $opts ${1:+"$@"}
              fi

              --
              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
            • Eric Wing
              I found a bug in my script. First open a file in a directory like, main.c Then go to another directory with a file by the same name (e.g. main.c) and open it.
              Message 6 of 8 , Mar 14, 2011
              • 0 Attachment
                I found a bug in my script.
                First open a file in a directory like, main.c
                Then go to another directory with a file by the same name (e.g.
                main.c) and open it.
                My script gets confused and brings the first main.c forward instead of
                recognizing that I am requesting a different file.

                Any recommendations on how to fix this?

                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
              Your message has been successfully submitted and would be delivered to recipients shortly.