Skip to content

Commit b644d80

Browse files
committed
Fix keymap to allow coexistence of L2U methods
with this all L2U input methods can be used at once without issues (hopefully)
1 parent 197e0e7 commit b644d80

File tree

4 files changed

+7122
-3304
lines changed

4 files changed

+7122
-3304
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Unicode symbols (e.g. `α`). By default, these substitutions must be triggered e
2727
be activated, and a method based on keymap is also available.
2828

2929
This feature also works in command mode, e.g. when searching the files with the `/` or `?` commands, but the
30-
as-you-type mode is not available (the keymap-based version is though, and it also works with some Vim
30+
as-you-type mode is not available (the keymap-based version works though, and it also works with some Vim
3131
commands like `f` and `t`).
3232

3333
By default, this feature is only active when editing Julia files. However, it can be also enabled with
@@ -112,10 +112,8 @@ and a LaTeX sequence can unambiguously be identified.
112112
For example, if you type `a \neq b` the `\neq` will be changed to `` right after the space, before you input
113113
the `b`.
114114

115-
This does not interfere with the <kbd>Tab</kbd> mapping discussed above. It only works in insert mode.
116-
117-
The `g:latex_to_unicode_auto` setting can also be changed from the Vim command-line, but you will
118-
also need to give the command `:call LaTeXtoUnicode#Init()` for the change to take effect.
115+
This does not interfere with the <kbd>Tab</kbd> mapping discussed above. It only works in insert mode, and it
116+
doesn't work with emojis.
119117

120118
This feature is not available with Vim versions lower then 7.4.
121119

@@ -124,14 +122,16 @@ This feature is not available with Vim versions lower then 7.4.
124122
A different susbstitution mode based on keymaps can be activated with `:let g:latex_to_unicode_keymap = 1`,
125123
e.g. by putting it into your `.vimrc` file. This works similarly to the as-you-type method described above,
126124
but it has the advantage that it works under more circumstances, e.g. in command-line mode when searching with
127-
`/` or `?`, and when using the `f` and `t` commands.
128-
The disadvantages are that you don't see the whole sequence as you're typing it, and no suggestions or partial
129-
completions are available, thus you need to know the substitutions (see `:help L2U`), and to type them
130-
correctly in full.
125+
`/` or `?`, and when using the `f` and `t` commands; plus it works with emojis too.
126+
The main disadvantage is that you don't see the whole sequence as you're typing it, and you can't fix mistakes
127+
with backspace, for example.
131128
Another difference is that there is a timeout like for any other mapping.
129+
In any case, it is possible to use this method in parallel with the other two methods, they don't interfere.
130+
So if you have the <kbd>Tab</kbd> mapping (discussed above) activated, you still get to see completions and
131+
suggestions. If you have the as-you-type substitution active, and you make a mistake, you can simply press
132+
backspace and keep going, at least in insert mode, and so on.
132133

133-
If you use this method, it is advisable to disable the other two methods, setting both `g:latex_to_unicode_tab`
134-
and `g:latex_to_unicode_auto` to `0`.
134+
This feature might with Vim versions lower then 7.4, but it hasn't been tested.
135135

136136
### LaTeX-to-Unicode on other file types
137137

doc/julia-vim-L2U.txt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ available:
6363
for a character with |f| or |t|, as explained in |language-mapping|. See
6464
|julia-vim-L2U-keymap|.
6565

66-
The first two methods are independent and can be used together without issues.
67-
The third one is best used by its own, disabling the other two, to avoid
68-
confusion.
66+
All of these methods are independent and can be used together without issues.
6967

7068
The default configuration is to use the first method, and it's only active
7169
when editing Julia files. It only works in |Insert| and |Command-line| modes.
@@ -75,12 +73,13 @@ It is possible to enable it with other file types, see
7573
regardless of the file type, see |julia-vim-L2U-enable-disable|.
7674

7775
In |Command-line| mode, e.g. when searching with the |/| or |?| commands, the
78-
default behavior is very similar, but slightly more limited, see
79-
|julia-vim-L2U-cmdmode|.
76+
default behavior is very similar to the default |Insert| mode behavior, but
77+
slightly more limited, see |julia-vim-L2U-cmdmode|.
8078

8179
These features only work as described with Vim version 7.4 or higher. Tab
8280
completion can still be made available on lower Vim versions, see
83-
|julia-vim-L2U-workaround|.
81+
|julia-vim-L2U-workaround|. The keymap mode might work but it hasn't been
82+
tested.
8483

8584
See |julia-vim| for the general reference about the other features of the
8685
julia-vim plug-in.
@@ -198,16 +197,17 @@ uses |keymap| to generate the mappings. This has the advantage that it works
198197
in more circumstances, e.g. in |Command-line| mode or when searching within a
199198
line with |f| or |t| (since it uses |language-mapping| underneath). It can
200199
also be easily turned on or off like any other keymap (see |i_CTRL-^| and
201-
|c_CTRL-^|).
202-
The disadvantages are that you don't see the whole sequence as you're typing
203-
it, and no suggestions or partial completions are available, thus you need to
204-
know the substitutions (see |julia-vim-L2U-reference|), and to type them
205-
correctly in full.
200+
|c_CTRL-^|). It also works with emojis.
201+
The disadvantage is that you don't see the whole sequence as you're typing
202+
it, and you can't fix mistakes with backspace, for example.
206203
Another difference is that there is a |timeout| like for any other mapping.
207204

208205
In order to use this method, set |g:latex_to_unicode_keymap| to `1`.
209-
If you do so, it is advisable to disable the other two methods,
210-
setting both |g:latex_to_unicode_tab| and |g:latex_to_unicode_auto| to `0`.
206+
You can use it in parallel with the other methods, they don't interfere. For
207+
example, typing a partial sequence and pressing <Tab> still triggers
208+
completions and suggestions if |g:latex_to_unicode_tab| is active.
209+
210+
If you use this feature, it's also useful to set |lCursor|.
211211

212212
------------------------------------------------------------------------------
213213
LATEX TO UNICODE ON DIFFERENT FILE TYPES *julia-vim-L2U-file-types*
@@ -337,8 +337,6 @@ g:latex_to_unicode_keymap
337337
in your |.vimrc| file. You can change this setting at any
338338
moment while editing, but you need to invoke
339339
|LaTeXtoUnicode#Init()| for the change to take effect.
340-
It's advisable to disable |g:latex_to_unicode_tab| and
341-
|g:latex_to_unicde_auto| if you use this.
342340

343341
*g:latex_to_unicode_file_types*
344342
g:latex_to_unicode_file_types

keymap/generate_L2U_keymap.jl

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
const filename = "latex2unicode_utf-8"
44

5+
# We want to avoid situations in which the user types e.g. \delt and pauses,
6+
# and the result is "∇t" because "\del" gets recognized and then there is some leftover "t".
7+
# This allows us to get completions with <Tab> for example.
8+
function fix_completions(completions::Dict{String,String})
9+
allinputs = sort!(collect(keys(completions)))
10+
new_completions = copy(completions)
11+
12+
for input in allinputs
13+
chars = completions[input]
14+
l = length(input)
15+
longer = filter(x->startswith(x, input)&&length(x)>l, allinputs)
16+
n = length(longer)
17+
n == 0 && continue
18+
new_completions[input * "<Tab>"] = chars
19+
for other in longer
20+
for j = (l+1):(length(other)-1)
21+
haskey(new_completions, other[1:j]) && continue
22+
new_completions[other[1:j]] = other[1:j]
23+
end
24+
end
25+
end
26+
return new_completions
27+
end
28+
529
function unicode_data()
630
file = normpath(Sys.BINDIR, "..", "..", "doc", "UnicodeData.txt")
731
names = Dict{UInt32, String}()
@@ -27,12 +51,18 @@ function table_entries(completions::Dict{String,String}, unicode_dict)
2751
unicode = String[]
2852
desc = String[]
2953

30-
for (input, chars) in sort!(collect(completions), by = last)
54+
for (input, chars) in sort!(collect(completions))
3155
code_points, unicode_names, characters = String[], String[], String[]
32-
for char in chars
33-
push!(code_points, "<char-0x$(uppercase(string(UInt32(char), base = 16, pad = 5)))>")
34-
push!(unicode_names, get(unicode_dict, UInt32(char), "(No Unicode name)"))
35-
push!(characters, isempty(characters) ? fix_combining_chars(char) : "$char")
56+
if startswith(chars, "\\")
57+
push!(code_points, replace(chars, "\\" => "\\\\"))
58+
push!(unicode_names, "(Incomplete sequence)")
59+
push!(characters, "")
60+
else
61+
for char in chars
62+
push!(code_points, "<char-0x$(uppercase(string(UInt32(char), base = 16, pad = 5)))>")
63+
push!(unicode_names, get(unicode_dict, UInt32(char), "(No Unicode name)"))
64+
push!(characters, isempty(characters) ? fix_combining_chars(char) : "$char")
65+
end
3666
end
3767
push!(latex, replace(input, "\\"=>"\\\\"))
3868
push!(code, join(code_points))
@@ -56,10 +86,10 @@ open("$filename.vim","w") do f
5686

5787
latex, code, unicode, desc =
5888
table_entries(
59-
merge(
89+
fix_completions(merge(
6090
REPL.REPLCompletions.latex_symbols,
6191
REPL.REPLCompletions.emoji_symbols
62-
),
92+
)),
6393
unicode_data()
6494
)
6595

0 commit comments

Comments
 (0)