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

Re: how to execute ranger from gvim

Expand Messages
  • Gary Johnson
    ... I think I must have intended that to be the following, which is the same as above but with the last two dependent clauses interchanged. if a:0 0 && a:1
    Message 1 of 16 , Dec 3, 2012
    • 0 Attachment
      On 2012-12-03, Gary Johnson wrote:
      > On 2012-12-03, ping wrote:
      > >
      > > On 12/03/2012 01:44 PM, Gary Johnson wrote:
      >
      > > > " RangerChooser()
      > > > "
      > > > " Ranger version 1.4.2 or later is required to run this function. That's when
      > > > " the --choosefile option was added.
      > > >
      > > > fun! RangerChooser(...)
      > > > let tmpfile = tempname() " Get the name of a temporary
      > > > " file.
      > > > if a:0 > 0 && a:1 != "" " If the user has supplied an
      > > > " argument,
      > > > let dir = a:1 " set dir to that argument,
      > > > elseif expand("%") " otherwise if the current
      > > > " buffer has a name,
      > > > let dir = "." " set dir to the current
      > > > " working directory.
      > > > else " Otherwise,
      > > > let dir = expand("%:p:h") " set dir to the directory
      > > > " containing the current
      > > > " buffer.
      >
      > Setting dir that way doesn't make sense to me. Either I wasn't
      > thinking straight when I wrote it or I'm not thinking straight now.

      I think I must have intended that to be the following, which is the
      same as above but with the last two dependent clauses interchanged.

      if a:0 > 0 && a:1 != "" " If the user has supplied an
      " argument,
      let dir = a:1 " set dir to that argument,
      elseif expand("%") " otherwise if the current
      " buffer has a name,
      let dir = expand("%:p:h") " set dir to the directory
      " containing the current
      " buffer.
      else " Otherwise,
      let dir = "." " set dir to the current
      " working directory.

      Regards,
      Gary

      --
      You received this message from the "vim_use" 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
    • ping
      ... thanks for the line by line annotation, now I understand! just one last small thing, in the above line, won t it suffice just: exe readfile(tmpfile) ? my
      Message 2 of 16 , Dec 3, 2012
      • 0 Attachment
        On 12/3/2012 8:06 PM, Gary Johnson wrote:
        > exe 'edit' readfile(tmpfile)[0]
        > > > " edit the file whose name
        > > > " is in the first line of
        > > > " tmpfile. (readfile()
        > > > " returns the contents of the
        > > > " file as a list of lines.
        > > > " List element 0 is the
        > > > " first line.
        thanks for the line by line annotation, now I understand!

        just one last small thing, in the above line, won't it suffice just:
        exe readfile(tmpfile)
        ?

        my test shows that will only give me a line of the file name, but now
        the file contents so you are right,
        but help says:
        readfile({fname} [, {binary} [, {max}]])
        Read file {fname} and return a List, each line of the file
        as an item. Lines broken at NL characters. Macintosh files
        separated with CR will result in a single long line
        (unless a
        NL appears somewhere).

        so readfile should have "read" the "file", why I only get the filename
        instead?



        thanks.

        regards
        ping

        --
        You received this message from the "vim_use" 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
      • Gary Johnson
        ... I m not sure what you are expecting that command to do. The :exe command executes its argument string, so its argument string must be an executable Vim
        Message 3 of 16 , Dec 3, 2012
        • 0 Attachment
          On 2012-12-03, ping wrote:
          > On 12/3/2012 8:06 PM, Gary Johnson wrote:
          > > exe 'edit' readfile(tmpfile)[0]
          > >> > " edit the file whose name
          > >> > " is in the first line of
          > >> > " tmpfile. (readfile()
          > >> > " returns the contents of the
          > >> > " file as a list of lines.
          > >> > " List element 0 is the
          > >> > " first line.
          > thanks for the line by line annotation, now I understand!
          >
          > just one last small thing, in the above line, won't it suffice just:
          > exe readfile(tmpfile)
          > ?

          I'm not sure what you are expecting that command to do. The :exe
          command executes its argument string, so its argument string must be
          an executable Vim command. The readline() function returns a list
          (which is not a string), and the first element of that list will in
          this case be a file name. A file name is not an executable Vim
          command.

          > my test shows that will only give me a line of the file name, but now
          > the file contents so you are right,
          > but help says:
          > readfile({fname} [, {binary} [, {max}]])
          > Read file {fname} and return a List, each line of the file
          > as an item. Lines broken at NL characters. Macintosh files
          > separated with CR will result in a single long line
          > (unless a
          > NL appears somewhere).
          >
          > so readfile should have "read" the "file", why I only get the
          > filename instead?

          The readfile() function read the file whose name was the value of
          the tmpfile variable. That file contained the name of the file you
          selected in ranger.

          Maybe an example would be clearer.

          Let's say you execute :RangerChooser in an unnamed buffer. The
          tempname() function returns the name of a temporary file, something
          like /tmp/vuZJYgI/2, so ranger is executed with arguments like this:

          ranger --choosefile=/tmp/vuZJYgI/2 .

          You browse your home directory and select a file named hello.txt.
          Ranger saves the string "/home/ping/hello.txt" into the file
          /tmp/vuZJYgI/2 and exits. Vim now executes
          readfile("/tmp/vuZJYgI/2") which returns the list

          ['/home/ping/hello.txt']

          The zeroth element of that list, specified in the function as

          readfile(tmpfile)[0]

          is the string '/home/ping/hello.txt'. The command

          exe 'edit' readfile(tmpfile)[0]

          evaluates to

          edit '/home/ping/hello.txt'

          which is the desired result.

          Regards,
          Gary

          --
          You received this message from the "vim_use" 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
        • ping
          ... that is super clear !(I also got it this morning on my way driving to work :D ) good learning for me, thanks for all the explanations. -- You received this
          Message 4 of 16 , Dec 4, 2012
          • 0 Attachment
            On 12/04/2012 01:19 AM, Gary Johnson wrote:
            > On 2012-12-03, ping wrote:
            >> On 12/3/2012 8:06 PM, Gary Johnson wrote:
            >>> exe 'edit' readfile(tmpfile)[0]
            >>>>> " edit the file whose name
            >>>>> " is in the first line of
            >>>>> " tmpfile. (readfile()
            >>>>> " returns the contents of the
            >>>>> " file as a list of lines.
            >>>>> " List element 0 is the
            >>>>> " first line.
            >> thanks for the line by line annotation, now I understand!
            >>
            >> just one last small thing, in the above line, won't it suffice just:
            >> exe readfile(tmpfile)
            >> ?
            > I'm not sure what you are expecting that command to do. The :exe
            > command executes its argument string, so its argument string must be
            > an executable Vim command. The readline() function returns a list
            > (which is not a string), and the first element of that list will in
            > this case be a file name. A file name is not an executable Vim
            > command.
            >
            >> my test shows that will only give me a line of the file name, but now
            >> the file contents so you are right,
            >> but help says:
            >> readfile({fname} [, {binary} [, {max}]])
            >> Read file {fname} and return a List, each line of the file
            >> as an item. Lines broken at NL characters. Macintosh files
            >> separated with CR will result in a single long line
            >> (unless a
            >> NL appears somewhere).
            >>
            >> so readfile should have "read" the "file", why I only get the
            >> filename instead?
            > The readfile() function read the file whose name was the value of
            > the tmpfile variable. That file contained the name of the file you
            > selected in ranger.
            >
            > Maybe an example would be clearer.
            >
            > Let's say you execute :RangerChooser in an unnamed buffer. The
            > tempname() function returns the name of a temporary file, something
            > like /tmp/vuZJYgI/2, so ranger is executed with arguments like this:
            >
            > ranger --choosefile=/tmp/vuZJYgI/2 .
            >
            > You browse your home directory and select a file named hello.txt.
            > Ranger saves the string "/home/ping/hello.txt" into the file
            > /tmp/vuZJYgI/2 and exits. Vim now executes
            > readfile("/tmp/vuZJYgI/2") which returns the list
            >
            > ['/home/ping/hello.txt']
            >
            > The zeroth element of that list, specified in the function as
            >
            > readfile(tmpfile)[0]
            >
            > is the string '/home/ping/hello.txt'. The command
            >
            > exe 'edit' readfile(tmpfile)[0]
            >
            > evaluates to
            >
            > edit '/home/ping/hello.txt'
            >
            > which is the desired result.
            >
            > Regards,
            > Gary
            that is super clear !(I also got it this morning on my way driving to
            work :D )
            good learning for me, thanks for all the explanations.

            --
            You received this message from the "vim_use" 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
          • ping
            ... a little bit enhance -- to make ranger continue with where it left off. I think there should be also a way to make the cursor stay on the last exited
            Message 5 of 16 , Dec 5, 2012
            • 0 Attachment

              On 12/04/2012 01:19 AM, Gary Johnson wrote:
              > On 2012-12-03, ping wrote:
              >> On 12/3/2012 8:06 PM, Gary Johnson wrote:
              >>> exe 'edit' readfile(tmpfile)[0]
              >>>>> " edit the file whose name
              >>>>> " is in the first line of
              >>>>> " tmpfile. (readfile()
              >>>>> " returns the contents of the
              >>>>> " file as a list of lines.
              >>>>> " List element 0 is the
              >>>>> " first line.
              >> thanks for the line by line annotation, now I understand!
              >>
              >> just one last small thing, in the above line, won't it suffice just:
              >> exe readfile(tmpfile)
              >> ?
              >
              > I'm not sure what you are expecting that command to do. The :exe
              > command executes its argument string, so its argument string must be
              > an executable Vim command. The readline() function returns a list
              > (which is not a string), and the first element of that list will in
              > this case be a file name. A file name is not an executable Vim
              > command.
              >
              >> my test shows that will only give me a line of the file name, but now
              >> the file contents so you are right,
              >> but help says:
              >> readfile({fname} [, {binary} [, {max}]])
              >> Read file {fname} and return a List, each line of the file
              >> as an item. Lines broken at NL characters. Macintosh files
              >> separated with CR will result in a single long line
              >> (unless a
              >> NL appears somewhere).
              >>
              >> so readfile should have "read" the "file", why I only get the
              >> filename instead?
              >
              > The readfile() function read the file whose name was the value of
              > the tmpfile variable. That file contained the name of the file you
              > selected in ranger.
              >
              > Maybe an example would be clearer.
              >
              > Let's say you execute :RangerChooser in an unnamed buffer. The
              > tempname() function returns the name of a temporary file, something
              > like /tmp/vuZJYgI/2, so ranger is executed with arguments like this:
              >
              > ranger --choosefile=/tmp/vuZJYgI/2 .
              >
              > You browse your home directory and select a file named hello.txt.
              > Ranger saves the string "/home/ping/hello.txt" into the file
              > /tmp/vuZJYgI/2 and exits. Vim now executes
              > readfile("/tmp/vuZJYgI/2") which returns the list
              >
              > ['/home/ping/hello.txt']
              >
              > The zeroth element of that list, specified in the function as
              >
              > readfile(tmpfile)[0]
              >
              > is the string '/home/ping/hello.txt'. The command
              >
              > exe 'edit' readfile(tmpfile)[0]
              >
              > evaluates to
              >
              > edit '/home/ping/hello.txt'
              >
              > which is the desired result.
              >
              > Regards,
              > Gary
              >


              a little bit enhance -- to make ranger continue with where it left off.


              I think there should be also a way to make the cursor stay on the last
              exited file(but not open) when re-run?

              if !exists("Rangerdir")
                  let RangerDir='.'
              endif

              fun! RangerChooser(...)
               
              "define a temp var to store name of a temp file, which was use
              "by ranger to store name of the file to be opened
              "
                  let tmpfile = tempname()     "generate a temp name,like '/tmp/vuZJYgI/2'
                  let tmpdir = tempname()     "for file and dir, for ranger use

              "determine what dir that ranger will start with
                  if g:RangerDir != '.'     "if dir get changed (by ranger)
                  let dir = g:RangerDir     "continue from the last changed dir
                  elseif a:0 > 0 && a:1 != ""    "if not changed,if supplied an non-empty
                  let dir = a:1         "argument from user,use it
                  elseif expand("%")         "if current buffer has a name
                  let dir = expand("%:p:h") "use dir containing curr buffer
                  else             "otherwise(there is no even curr named buffer)
                  let dir = '.'         "start from current working dir
                  endif

              "call ranger: ranger --choosefile=tmpfile $dir
              "start from 'dir', when exit,
              "save selected filename/foldername to temp file named 'tmpfile'/'tmpdir'
                  exe 'silent !ranger --choosefile='.tmpfile '--choosedir='.tmpdir dir
                  "read and save the real folder name from the saved temp file
                  if filereadable(tmpdir)     "if tempfile is readable
                  "read tmpfile for the filename, then edit in curr buff
                  let g:RangerDir=readfile(tmpdir)[0]
                  call delete(tmpdir)     "delete the tmpfile
                  endif

                  "retrieve and save the real file name out of the saved temp file
                  if filereadable(tmpfile)     "if tempfile is readable
                  "read tmpfile for the filename, then edit in curr buff
                  exe 'edit' readfile(tmpfile)[0]
                  call delete(tmpfile)     "delete the tmpfile
                  endif
                  redraw!
              endfun
              map ,r :call RangerChooser()<CR>
              command -nargs=? RangerChooser call RangerChooser("<args>")

              B.t.w, per man ranger there is also a bashrc way to do it.

              Bash: cd to last path after exit
                  This is a bash function (for ~/.bashrc) to change the directory to
                  the last visited one after ranger quits.  You can always type "cd -"
                  to go back to the original one.

                   function ranger-cd {
                     tempfile='/tmp/chosendir'
                     /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}"
                     test -f "$tempfile" &&
                     if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
                       cd -- "$(cat "$tempfile")"
                     fi
                     rm -f -- "$tempfile"
                   }

                   # This binds Ctrl-O to ranger-cd:
                   bind '"\C-o":"ranger-cd\C-m"'


              --
              You received this message from the "vim_use" 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.