Friday, May 23, 2014

Re: Get expansion of digraph in VimL

On Friday, May 23, 2014 10:36:56 AM UTC+12, Nate Soares wrote:
> Is there a way to get the expansion of a digraph (entered e.g. with ^K in insert mode) programatically?

There isn't a vim script way to do this directly. I can think of two ways, one somewhat unclean, the other pedestrian.

Firstly, this approach may have side effects, might spoil your screen layout, and harm innocent animals:

function! ExpandDigraph(dig)
if a:dig !~ '^..$'
return ""
endif
new
exe "norm! a\<c-k>" . a:dig . "\<esc>"
let result = getline(".")
close!
return result
endfunc

Secondly: capture the output of the command :digraph, (:help redir) and reformat it to one column (that's tricky because there's lots of funny characters), and write it to a file, say "digraphs.txt". Then,

function! ExpandDigraph(dig)
if !exists("s:digs")
let s:digs = {}
for line in readfile("digraphs.txt")
let s:digs[line[0:1]] = line[3:]
endfor
endif
return has_key(s:digs, a:dig) ? s:digs[a:dig] : ""
endfunc

> For example, I have vim set up to insert the ellipsis character '…' when I type "^K..". Is there a way, programatically, to write a function ExpandDigraph such that ExpandDigraph("..") yields "…"?

BTW, with my vim 7.4.274 on linux ".." is a digraph for "‥" U+2025 TWO DOT LEADER, not an ellipsis, "…" U+2026 HORIZONTAL ELLIPSIS. My vim only has digraphs for U+22EF MIDLINE HORIZONTAL ELLIPSIS and U+22EE VERTICAL ELLIPSIS. If you've defined your own digraphs, and you use my second approach you'd have to add yours to the file.

Regards, John Little

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