Monday, December 3, 2012

Re: how to execute ranger from gvim

On 2012-12-03, ping wrote:
> On 12/03/2012 12:56 PM, Gary Johnson wrote:
> >On 2012-12-03, ping wrote:
> >
> >> On 08/09/2012 06:02 AM, Sepp Tannhuber wrote:
> >>>
> >>> I use the following function from the ranger manual:
> >>> fun! RangerChooser()
> >>> exec "silent !ranger --choosefile=/tmp/chosenfile" . expand("%:p:h")
> >>> if filereadable('/tmp/chosenfile')
> >>> exec 'edit ' . system('cat /tmp/chosenfile')
> >>> call system('rm /tmp/chosenfile')
> >>> endif
> >>> redraw!
> >>> endfun
> >>> map ,r :call RangerChooser()<CR>
> >
> >> what's the problem with just :!ranger ?
> >
> > The function uses Ranger as a file chooser. Vim will edit the file
> > you've chosen using Ranger in the current Vim instance. Using just
> > :!ranger will let you edit a file chosen using Ranger, but in a new
> > Vim instance.
> >
> > Regards,
> > Gary
> >
> oh I got it now, yes that's great ideal then!
>
> but I just did a quick test of your code on vim (not gvim), I move my
> cursor in ranger to a whatever file under my home ,say,
> "/home/ping/file1.txt" and hit enter or 'l', I got
> following error:
> [Error 2] No such file or directory: '/tmp/chosenfile/home/ping'
> guess I need to strip off "/tmp/chosenfile"?

It's not my code. The OP claimed he used the function from the
ranger manual.

I tried that function and got the same error you did. But the
function that I wrote about a year ago based on one in the ranger
man page works fine.

It's not immediately obvious to me what the problem with the OP's
function is, but here's the one I use.

----------------------------- cut here -----------------------------
" Vim plugin for using ranger as a file chooser
" File: ranger.vim
" Maintainer: Gary Johnson <garyjohn AT spocom DOT com>
" Last Change: 2011-10-31 16:25:58

" From the ranger(1) man page for ranger-1.5.2:
"
" VIM: File Chooser
" This is a vim function which allows you to use ranger to select a file
" for opening in your current vim session.
"
" fun! RangerChooser()
" silent !ranger --choosefile=/tmp/chosenfile `[ -z '%' ] && echo -n . || dirname %`
" if filereadable('/tmp/chosenfile')
" exec 'edit ' . system('cat /tmp/chosenfile')
" call system('rm /tmp/chosenfile')
" endif
" redraw!
" endfun
" map ,r :call RangerChooser()<CR>
"
" That function fails when executed in an empty, unnamed buffer with the
" following messages:
"
" Error detected while processing function RangerChooser:
" line 1:
" E499: Empty file name for '%' or '#', only works with ":p:h": silent !ranger
" --choosefile=/tmp/chosenfile `[ -z '%' ] && echo -n . || dirname %`

" 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()
if a:0 > 0 && a:1 != ""
let dir = a:1
elseif expand("%")
let dir = "."
else
let dir = expand("%:p:h")
endif
exe 'silent !ranger --choosefile='.tmpfile dir
if filereadable(tmpfile)
exe 'edit' readfile(tmpfile)[0]
call delete(tmpfile)
endif
redraw!
endfun
"map ,r :call RangerChooser()<CR>
command -nargs=? RangerChooser call RangerChooser("<args>")
--------------------------------------------------------------------

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

No comments: