Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Keyloop #276

Closed
wants to merge 5 commits into from

3 participants

@mattn

CtrlP has mapping to ASCII or control characters. So we can't type multi-byte characters into CtrlP's prompt. This patch provides key event loop to handle multi-byte characters with IME(input method editor).
If set g:ctrlp_key_loop = 1, enable this feature.
I guess this is awesome useful for users who using multi-byte characters. (me too)

autoload/ctrlp.vim
@@ -529,6 +535,20 @@ fu! s:BuildPrompt(upd)
exe 'echoh' hibase '| echon "_" | echoh None'
en
endf
+
+fu! s:KeyLoop()
+ wh exists('s:focus') && s:focus
+ redraw
+ let n = getchar()
+ let c = type(n) == 0 ? nr2char(n) : n
+ if n >=# 0x20
+ cal s:PrtFocusMap(c)
+ el
+ exe "keepj normal" c
@kien Owner
kien added a note

Is this supposed to be normal! with an !?

Edit: nvm, should have read it more carefully instead of reflexively asking where the ! went.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kien
Owner

OK, since I can't really test this, I'll trust you on it. I'll merge and push it out soon.

@d11wtq

If I understand what it's supposed to do, I can probably test it. I work with some romance and Nordic languages that include accented vowels like é and è and Nordic characters like ð, þ and æ. Not sure if the idea is to allow those characters to be typed in ctrlp or not though. Currently they don't type correctly, at least on OS X's Terminal.app.

@d11wtq

Holy crap it works :) I can type all those characters from European languages just fine with this patch merged in. I can't type Japanese Hiragana (こんにちわ) or Katakana (コンニチワ) using the Kotoeri keyboard on OS X, but that does some weird stuff where you have to type sequences of English characters in order to type a single Japanese character.

@d11wtq

The fuzzy search doesn't trip on the multibyte characters neither.

@mattn
@d11wtq

@mattn I was saying that without your patch the characters can't be typed. With your patch (which I only tried after writing that first comment) I can confirm that the characters are typed as expected. I think it should be merged, since it's definitely a big improvement in the i18n department :)

@kien
Owner

That's awesome. Thanks, @d11wtq!

I encountered a few problems with the control mappings. For example, pressing <tab> (9) throws an E471, and pressing <c-\> (28) or <c-o> (15) does nothing.

@d11wtq

@kien, no problem. I just tried the mappings you mention and got different behaviour. Tab does nothing (might be because I also have supertab installed), but <c-\> brings up "Insert: c[w]ord/c[f]ile/[s]earch/[v]isual/[c]lipboard/[r]egister?" and <c-o> brings up the "Open:" prompt. I wonder if it makes a difference what Terminal is used.

@mattn

I modified code to handle few mapped key codes.

@d11wtq

@mattn @kien tested the update, tab now works, as do <c-o> and <c-\>. The multibyte chars still work too.

@kien
Owner

Yep, that seems to fix it. But I just noticed, this stops working after toggling the focus with <s-tab> then back again. I tried adding another s:KeyLoop() call after toggling but this in turn causes errors when closing ctrlp. Any idea?

@mattn

@kein could you please show me the code you modified? and please let me know way to reproduce.

@kien
Owner

This solves the above problem for me, not sure if there's any side effect yet:

+++ b/autoload/ctrlp.vim
@@ -561,7 +561,7 @@ fu! s:BuildPrompt(upd)
 endf

 fu! s:KeyLoop()
-   wh exists('s:focus') && s:focus
+   wh exists('s:init')
        redr
        let n = getchar()
        let c = type(n) == 0 ? nr2char(n) : n
@@ -739,7 +739,7 @@ fu! s:PrtSelectJump(char)
            let npos = match(lines, smartcs.'^'.chr, s:jmpchr[1] + 1)
            let [jmpln, s:jmpchr] = [npos == -1 ? pos : npos, [chr, npos]]
        en
-       keepj exe jmpln + 1
+       exe 'keepj norm!' ( jmpln + 1 ).'G'
        if s:nolim != 1 | let s:cline = line('.') | en
        if line('$') > winheight(0) | cal s:BuildPrompt(0) | en
    en
@mattn

I don't get any problems with your changes.

kien++

@kien kien closed this in 6089090
@kien
Owner

Merged. Thanks, @mattn! This is a very nice addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 12, 2012
  1. @mattn

    add code for key event loop.

    mattn authored
  2. @mattn

    enable IM.

    mattn authored
  3. @mattn

    check type of keys.

    mattn authored
Commits on Sep 13, 2012
  1. @mattn

    call map argument if defined.

    mattn authored
  2. @mattn

    remove needless code.

    mattn authored
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 0 deletions.
  1. +26 −0 autoload/ctrlp.vim
  2. +7 −0 doc/ctrlp.txt
View
26 autoload/ctrlp.vim
@@ -63,6 +63,7 @@ let [s:pref, s:opts, s:new_opts] = ['g:ctrlp_', {
\ 'highlight_match': ['s:mathi', [1, 'CtrlPMatch']],
\ 'jump_to_buffer': ['s:jmptobuf', 'Et'],
\ 'lazy_update': ['s:lazy', 0],
+ \ 'key_loop': ['s:keyloop', 0],
\ 'match_func': ['s:matcher', {}],
\ 'match_window_bottom': ['s:mwbottom', 1],
\ 'match_window_reversed': ['s:mwreverse', 1],
@@ -196,6 +197,11 @@ fu! s:opts() "{{{2
let s:mxheight = max([s:mxheight, 1])
let s:glob = s:dotfiles ? '.*\|*' : '*'
let s:igntype = empty(s:usrign) ? -1 : type(s:usrign)
+ if s:keyloop
+ let s:lazy = 0
+ let s:glbs['imd'] = 0
+ en
+ if s:keyloop | let s:lazy = 0 | en
if s:lazy
cal extend(s:glbs, { 'ut': ( s:lazy > 1 ? s:lazy : 250 ) })
en
@@ -529,6 +535,25 @@ fu! s:BuildPrompt(upd)
exe 'echoh' hibase '| echon "_" | echoh None'
en
endf
+
+fu! s:KeyLoop()
+ wh exists('s:focus') && s:focus
+ redr
+ let n = getchar()
+ let c = type(n) == 0 ? nr2char(n) : n
+ if n >=# 0x20
+ cal s:PrtFocusMap(c)
+ else
+ let ma = matchstr(maparg(c), ':<C-U>\zs.*\ze<CR>$')
+ if len(ma) > 0
+ exe ma
+ else
+ exe "keepj norm" c
+ en
+ endif
+ endw
+endf
+
" - SetDefTxt() {{{1
fu! s:SetDefTxt()
if s:deftxt == '0' || ( s:deftxt == 1 && !s:ispath ) | retu | en
@@ -1980,6 +2005,7 @@ fu! ctrlp#init(type, ...)
cal ctrlp#setlines(s:settype(a:type))
cal s:SetDefTxt()
cal s:BuildPrompt(1)
+ if s:keyloop | call s:KeyLoop() | en
endf
" - Autocmds {{{1
if has('autocmd')
View
7 doc/ctrlp.txt
@@ -62,6 +62,7 @@ Overview:~
|ctrlp_arg_map| Intercept <c-y> and <c-o> or not.
|ctrlp_follow_symlinks| Follow symbolic links or not.
|ctrlp_lazy_update| Only update when typing has stopped.
+ |ctrlp_key_loop| Use event loop. Useful for multi-bytes input.
|ctrlp_default_input| Seed the prompt with an initial string.
|ctrlp_use_migemo| Use Migemo patterns for Japanese filenames.
|ctrlp_prompt_mappings| Change the mappings in the prompt.
@@ -391,6 +392,12 @@ after typing's been stopped for a certain amount of time: >
If is 1, update after 250ms. If bigger than 1, the number will be used as the
delay time in milliseconds.
+ *'g:ctrlp_key_loop'*
+Set this to 1 to enable the key event loop. Useful for multi-byte input: >
+ let g:ctrlp_key_loop = 0
+<
+Note that This is experimental feature.
+
*'g:ctrlp_default_input'*
Set this to 1 to enable seeding the prompt with the current file's relative
path: >
Something went wrong with that request. Please try again.