LoginSignup
2
0

More than 5 years have passed since last update.

vimでgithub emojiをUnicode emojiにする

Last updated at Posted at 2018-12-20

vimでgithub emojiをUnicode emojiにする

あわせて、echoする機能も。
ただ、まだ微妙にバグっているので注意。
すくなくともechoは後のほうのを拾ってしまうし、カーソル位置移動も適切とはいいがたい。

利用には、junegunn/vim-emojiプラグインが必要です。

deinならこう

[[plugins]] # completion <C-X><C-U> (user completion)
repo = 'junegunn/vim-emoji'

hook_post_sourceに以下をセットしてやる。

  " set default user completion function
  set completefunc=emoji#complete

  " replace :emoji: to <unicode-emoji>
  " try echo unicode
  function! s:emoji_unicode_echo ()
    let l:keywords=&iskeyword
    setlocal iskeyword-=:
    let l:word = expand('<cword>')
    let l:gh_word = ':'.l:word.':'
    if '' !=? emoji#for(l:word)
      echo 'emoji :'.expand('<cword>').'-'.emoji#for(l:word)
    else
      echo 'emoji :'.expand('<cword>').'-'.'(no match)'
    endif
    let &iskeyword=l:keywords
  endfunction

  nnoremap <silent><Leader>e :call <SID>emoji_unicode_echo()<CR>

  function! s:emoji_unicode_replace ()
    let l:keywords=&iskeyword
    setlocal iskeyword-=:
    let l:word = expand('<cword>')
    if word == ''
      let &iskeyword=l:keywords
      return
    endif

    let l:gh_word = ':'.l:word.':'
    if '' !=? emoji#for(l:word)
      " カーソル位置をword分前に動かしてから、その位置から後の最初のwordを置換する
      " 完了後、位置を移動
      "   123456789ABCD
      "   smile :smile:
      "   ^____ origin cursor
      "   ^____ replace match start (word match pos - colon_size (min:1))
      "   ^____ if success; search emoji start (same replace match)

      "   smile :smile:
      "   __^__ origin cursor
      "   ^____ replace match start (word match pos - colon_size (min:1))
      "   ^____ if success; search emoji start (same replace match)

      "   smile :smile:
      "   ________^__ origin cursor
      "   ___^_______ word matchs start (origin - word len(min:1))
      "   ______^____ replace match start (word match pos - colon_size (min:1))
      "   ______^____ if success; search emoji start (same replace match)

      let pos = getcurpos()
      let word_col = pos[2]
      let target_col = pos[2]
      if pos[2] != 1
        " 行頭以外は位置補正する
        let word_col = pos[2] - strlen(l:word)
        if word_col < 1 | let word_col = 1 | endif

        let target_col = word_col
        if word_col != 1
          call cursor(pos[1], word_col)
          call search(l:word)

          let target_pos = getcurpos()
          let target_col = target_pos[2] - 1 " : の分
          if target_col < 1 | let target_col = 1 | endif
        endif
      endif

      call cursor(pos[1], target_col)

      let l:success = 0
      try
        execute('substitute' . '/' . '\%#'.l:gh_word . '/' . '\=emoji#for(l:word)' . '/el')
        let l:success = 1
      finally
        call cursor(pos[1], pos[2])
      endtry

      if l:success
        call cursor(pos[1], target_col)
        call search(emoji#for(l:word))
      endif

      " debug
      " echom 'emoji:' . 'pos:'.pos[2] . ',word:'.word_col . ',target:'.target_col . ',success:'.l:success

    endif
    let &iskeyword=l:keywords
  endfunction

  nnoremap <silent><Leader>E :call <SID>emoji_unicode_replace()<CR>

ポイントは、\%#を使うことで、後のほうにある、同名のemojiを対象にしないようにすること。
あと行頭のあたりで暴発しないようにするに苦労した...

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0