Wednesday, December 5, 2012

Re: how to execute ranger from gvim


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"'


No comments: