rhysd / clever-f.vim

Extended f, F, t and T key mappings for Vim.
https://rhysd.github.io/clever-f.vim
1.01k stars 44 forks source link

Clever-f conflicts with wordcount function #31

Closed eater closed 7 years ago

eater commented 7 years ago

I use this function to keep a live wordcount in the statusline: https://gist.github.com/cormacrelf/d0bee254f5630b0e93c3

When I try to use Clever-f, I get an error

Error detected while processing function clever_f#find_with[12]..WordCount:
E523: Not allowed here

Is there an easy way to resolve the conflict?

rhysd commented 7 years ago

Do you know which line causes the error? I could not find the code related to clever-f.vim in your gist.

At least it looks that you should use normal! instead of normal.

eater commented 7 years ago

No, I'm not sure where the conflict is! I didn't write the function, I just found the gist. Changing it to normal! is a good idea but doesn't fix the error.

Here's a simplified version of the function that still conflicts:

function! WordCount()
  if line('$') == 1 && getline(1) == '' 
    return 
  elseif mode() == "i"  || mode() == "n"
    let s:old_status = v:statusmsg
    exe "silent normal! g\<c-g>"
    let b:word_count = str2nr(split(v:statusmsg)[11])
    let v:statusmsg = s:old_status
  endif
  return b:word_count
endfunction
rhysd commented 7 years ago

Thank you for reducing code. But I can still not identify the cause of this bug... Could you show me your status line configuration? I need to reproduce.

eater commented 7 years ago

From .vimrc:

let mystatusline = ""
let mystatusline .= "\ %n\ %*" |                                        " buffer number
let mystatusline .= "\ %<%F%*" |                                        " full path
let mystatusline .= "\ %y%*" |                                          " file type
let mystatusline .= " \%M" |                                            " modified flag
let mystatusline .= "\ %#vimWarn#\%{(&key==\"\"?\"\":\"ENCR\")}%*" |    " encrypted?
let mystatusline .= "\ \[%{v:register}\]" |                             " current register
let mystatusline .= "\ %{getcwd()} "                                   " current working dir
let &statusline = mystatusline
autocmd FileType txt if &statusline !~ "WC: " | setlocal statusline+=\ \ WC:\ %{WordCount()}\ 
rhysd commented 7 years ago

Thanks. I'll try it.

rhysd commented 7 years ago

I could not see the error as below (it seems that it breaks cursor position when entering insert mode, but it looks not related to this issue)

tmp

eater commented 7 years ago

Here's a longer set of error messages.

Error detected while processing function clever_f#find_with[12]..WordCount:
line   11:
E523: Not allowed here
Error detected while processing function clever_f#find_with[12]..WordCount:
line   17:
E684: list index out of range: 11
Error detected while processing function clever_f#find_with[12]..WordCount:
line   17:
E116: Invalid arguments for function str2nr(s:split_wc[11])
Error detected while processing function clever_f#find_with[12]..WordCount:
line   17:
E15: Invalid expression: str2nr(s:split_wc[11])
Error detected while processing function clever_f#find_with[12]..WordCount:
line   11:
E523: Not allowed here
Error detected while processing function clever_f#find_with[12]..WordCount:
line   17:
E684: list index out of range: 11
Error detected while processing function clever_f#find_with[12]..WordCount:
line   17:
E116: Invalid arguments for function str2nr(s:split_wc[11])
Error detected while processing function clever_f#find_with[12]..WordCount:
line   17:
E15: Invalid expression: str2nr(s:split_wc[11])
chrisbra commented 7 years ago

instead of using a selfmade wordcount function, you can use the builtin wordcount() function, which has been included in Vim 7.4.1042

eater commented 7 years ago

Amazing! Thanks, @chrisbra.

rhysd commented 7 years ago

Thank you @chrisbra. @eater, did it solve this issue?

rhysd commented 7 years ago

It looks it is a problem of implementation of the function WordCount(). It seems to be called while clever-f.vim's f is used because of status line update, but not related to this plugin directly. Closing this.