Saturday, May 24, 2014

Re: Get expansion of digraph in VimL

On 23/05/14 13:19, John Little wrote:
> 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
>

Note that nothing forbids having more than one digraph for the same
character, and in fact by default some characters have both an RFC1345
digraph and a "legacy Vim" digraph (as the latter was used before Vim
digraphs were standardized to RFC1345). Having more than one character
for a single digraph, however, is of course not possible: trying to
define a new equivalent for an existing digraph replaces it.

The above function would always return the last character-pair in the
list for any given character, for instance (with the default digraphs)
n~ (the legacy digraph) and not n? (the RFC1345 digraph) for U+00F1
LATIN SMALL LETTER N WITH TILDE. (Any previous digraph for the same
character would be replaced when creating the Dictionary.)


Best regards,
Tony.
--
Execution

If people were not afraid of death,
Then what would be the use of an executioner?
If people were only afraid of death,
And you executed everyone who did not obey,
No one would dare to disobey you.
Then what would be the use of an executioner?
People fear death because death is an instrument of fate.
When people are killed by execution rather than by fate,
This is like carving wood in the place of a carpenter.
Those who carve wood in place of a carpenter
Often injure their hands.
-- Lao Tse, "Tao Te Ching"

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