aboutsummaryrefslogtreecommitdiffstats
path: root/bundle/insert-layout/plugin/insert_layout.vim
blob: f5b93d381b659268d0f08af19853a4b871726005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
vim9script

if exists('g:loaded_insert_layout')
	finish
endif
g:loaded_insert_layout = 1

const layout_names = {
	us: 'US',
	qwazerty: 'QWAZERTY2',
	fr: 'French-numerical',
	dvorak: 'Dvorak',
}

var normal_layout: string

def IsLayoutSwitcherAvailable(): bool
	return executable('xkbswitch')
enddef

def CurrentInputLayout(): string
	return system('xkbswitch -ge')
enddef

def SetInputLayout(layout: string): void
	system('xkbswitch -se ' .. layout)
enddef

def OnInsertEnter(layout: string): void
	normal_layout = CurrentInputLayout()

	SetInputLayout(layout)
enddef

def OnInsertLeave(): void
	SetInputLayout(normal_layout)
enddef

def InsertLayoutOff(): void
	# autocmd! insert_layout InsertEnter <buffer>
	# autocmd! insert_layout InsertLeave <buffer>

	autocmd_delete([
		{
			group: 'insert_layout',
			event: 'InsertEnter',
			bufnr: bufnr(),
		},
		{
			group: 'insert_layout',
			event: 'InsertLeave',
			bufnr: bufnr(),
		},
	])
enddef

def InsertLayoutOn(layout: string): void
	# autocmd insert_layout InsertEnter <buffer> OnInsertEnter(layout)
	# autocmd insert_layout InsertLeave <buffer> OnInsertLeave()
	autocmd_add([
		{
			replace: true,
			group: 'insert_layout',
			event: 'InsertEnter',
			bufnr: bufnr(),
			cmd: 'OnInsertEnter("' .. layout_names[layout] .. '")',
		},
		{
			replace: true,
			group: 'insert_layout',
			event: 'InsertLeave',
			bufnr: bufnr(),
			cmd: 'OnInsertLeave()',
		},
	])
enddef

def Complete(arg_lead: string, cmd_line: string, cursor_pos: number): string
	return join(keys(layout_names), "\n")
enddef

command! -nargs=1 -complete=custom,Complete InsertLayout InsertLayoutOn(<q-args>)
command! InsertLayoutOff InsertLayoutOff()

# SetInputLayout(layout_names['fr'])

# augroup frinsert
# 	autocmd!
# 	autocmd InsertEnter * :call system('xkbswitch -se French-numerical')
# 	autocmd InsertLeave * :call system('xkbswitch -se US')
# augroup END