Friday, January 27, 2017

Re: How to open another file and go to a search pattern

2017-01-27 15:20 GMT+03:00 Mayuresh <mayuresh@acm.org>:
> Sorry, when typing out text I have made some mistakes, which may have
> given wrong impressions of what I tried at a couple of places. I should
> have rather pasted the code I tried, but I had changed it by the time I
> thought of writing a mail. Will correct it now.
>
> On Fri, Jan 27, 2017 at 02:13:24PM +0300, Nikolay Aleksandrovich Pavlov wrote:
>> > function! GoToFile()
>> > let line = getline(".")
>> > let searchpat = split(line,":")[1]
>> > let file = split(line," ")[1]
>> > " See below what all I tried here
>> > endfunction
>> > map <C-o> :exec GoToFile()<CR>
>>
>> Given how this function looks, you should not be using `:execute`. For
>> calling functions there is `:call`. This works only because
> Point taken.
>
>> > 1. Tried loading the file and searching in it using two different
>> > statements:
>> >
>> > exec "e" file
>> > search(searchpat,"")
>> >
>> > With this, I find that such search is applied to current file instead of
>> > new file and then the file is loaded. I find this strange.
>>
>> How did you check that "search is applied to current file"? I see that
>> these commands are not going to work because there is no `:search`
>> command.
>
> Clarification: Tried exec search(searchpat)

That does not make sense, `search` already moves cursor. No need to
make `exec` move cursor afterwards (similar problem to the above
point).

> It did not search in the current file. (My mistake in above mail.)
>
> However the other thing that I tried was:
> exec "/"searchpat
> or more precisely, to avoid an extra space after "/"
> exec join(["/",searchpat],"")

If you want to avoid spaces, do use concat.

:execute arg1 arg2

is the same thing as

:execute arg1." ".arg2

for convenience. You just need to have explicit concat:

:execute arg1.arg2

does not add any spaces.

>
> On either of these the message "Search hit bottom ...." appeared in the
> current buffer itself. Only on hitting Enter the next file was opened.
> From this I thought it is searching in current file.
>
>> And you forgot `fnameescape()`. First line should be `:execute 'e'
>> fnameescape(file)` or you get problems with special characters in
>> `file`.
> Thanks for the tip. Will incorporate.
>
>> > 2. From above, assuming loading and searching as two statements won't
>> > work, tried:
>> >
>> > exec "+/" searchpat file
>> >
>> > But this breaks with searchpat having spaces (even if I use \" to cover
>> > the search pattern).
>>
>> ?! This command is not opening a file, it will run something like
>>
>> +/{space}{search pattern}{space}{filename}
>>
>> : perform search in the current file, starting from the current line
>> (same {range} without command interpretation).
>
> Precise command is:
>
> exec "e " join(["+/",searchpat],"") file
>
> This is opening the file, but to highlight the pattern I have to press
> "/<Up>"<CR>. Looking for this last step to happen automatically.

I am unable to move to the search result even with this code. You are
using :execute on a function which returns 0, it *must* bring you to
the first line once function exits, so all your attempts to move to
the search pattern are useless. To move and highlight you need to use
something like

execute 'edit' fnameescape(file)
let @/ = searchpat
normal! gg0n

. E.g. this works as I expect:

vim -u gtf.vim -i NONE -N

" gtf.vim:
function GoToFile() abort
let line = getline(".")
let [file, searchpat] = matchlist(line, '\v\C^(.{-})\ \:\ (.*)$')[1:2]
execute 'edit' fnameescape(file)
let @/ = searchpat
normal! gg0n
endfunction
nnoremap <C-o> :<C-u>call GoToFile()<CR>

call writefile(['/tmp/gtf-test : abc def'], '/tmp/gtf-test-init')
call writefile(['foo', 'abc def'], '/tmp/gtf-test')
set hlsearch
edit /tmp/gtf-test-init
call feedkeys("gg\<C-o>", "t")

. Note:

0. `nnoremap`: you must not use `*map` without `nore` unless you know
what you are doing. This avoids possible issues with other mappings.
1. `:<C-u>`: check how function behaves when you press `10<C-o>` without this.
2. `call GoToFile()`, not `execute`.
3. Original `file` and `searchpat` definition will not work correctly
with `SomeFileName : Some Multiple Words of Search Pattern`. I assumed
that code is wrong, not example line.

>
> Mayuresh
>
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment