aboutsummaryrefslogtreecommitdiffhomepage
path: root/.vimrc
blob: 58843ec5ae9362d87aa18f2f33d7f5f3ac4eca61 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
" Set Vim's internal encodding.
" Note: This option must be set before 'scriptencoding' is set.
set encoding=utf-8
" Set this file's encoding.
scriptencoding utf-8


" Global settings {{{1

" The ideom to get |<SID>|. {{{2

function! s:get_sid() abort
    return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_get_sid$')
endfunction

let s:SID = s:get_sid()
let s:SNR = '<SNR>' . s:SID . '_'

delfunction s:get_sid



" Global variables. {{{2

let g:MY_ENV = {}

if has('mac')
    let g:MY_ENV.os = 'mac'
elseif has('win32unix') || has('win32') || has('win64')
    let g:MY_ENV.os = 'windows'
else
    let g:MY_ENV.os = 'unknown'
endif

let g:MY_ENV.vim_dir = !empty($XDG_CONFIG_HOME) ? expand('$XDG_CONFIG_HOME/vim') :
    \                 g:MY_ENV.os ==# 'windows' ? expand('$HOME/vimfiles') :
    \                                             expand('$HOME/.vim')
let g:MY_ENV.my_dir = g:MY_ENV.vim_dir . '/my'
let g:MY_ENV.plug_dir = g:MY_ENV.vim_dir . '/plugged'
let g:MY_ENV.cache_dir = !empty($XDG_CACHE_HOME) ? expand('$XDG_CACHE_HOME/vim') :
    \                                              g:MY_ENV.vim_dir . '/cache'
let g:MY_ENV.undo_dir      = g:MY_ENV.cache_dir . '/undo'
let g:MY_ENV.backup_dir    = g:MY_ENV.cache_dir . '/backup'
let g:MY_ENV.swap_dir      = g:MY_ENV.cache_dir . '/swap'
let g:MY_ENV.yankround_dir = g:MY_ENV.cache_dir . '/yankround'

for [s:k, s:v] in items(g:MY_ENV)
    if s:k =~# '_dir$' && !isdirectory(s:v)
        call mkdir(s:v, 'p')
    endif
endfor
unlet s:k s:v




" The autogroup used in .vimrc {{{2

augroup Vimrc
    autocmd!
augroup END


" Note: |:autocmd| does not accept |-bar|.
command! -nargs=*
    \ Autocmd
    \ autocmd Vimrc <args>


" Variable for re-entrancy of .vimrc {{{2

" If an operation cannot be executed twice, enclose it with if block.
"
" if !s:is_reloading_vimrc
"     ...
" end
"
" See "SourceThisFile()" defined in this file for practical example.
let s:is_reloading_vimrc = v:vim_did_enter


" Language {{{2

" Disable L10N.
language messages C
language time C





" Options {{{1

" * Use |:set|, not |:setglobal|.
"   |:setglobal| does not set local options, so options are not set in
"   the starting buffer you specified as commandline arguments like
"   "$ vim ~/.vimrc".

" Initialize all options with Vim's default values.
set all&



" Moving around, searching and patterns {{{2

set nowrapscan
set incsearch
set ignorecase
set smartcase



" Tags {{{2

set tags+=../tags
    \ tags+=../../tags
set tagcase=match



" Displaying text {{{2

set scrolloff=7
set wrap
set linebreak
set breakindent
set breakindentopt+=sbr
" Use let statement to avoid trailing space.
let &showbreak = '> '
set sidescrolloff=20
set display=lastline
" Use let statement to avoid trailing space.
let &fillchars = 'stl: ,stlnc: ,vert: ,fold: ,diff: '
set cmdheight=2
set list
let &listchars="eol:\u00ac,tab:\u25b8 ,extends:\u00bb,precedes:\u00ab"
set conceallevel=2
set concealcursor=cnv



" Syntax, highlighting and spelling {{{2

set background=dark
set synmaxcol=500
set hlsearch
" Execute nohlsearch to avoid highlighting last searched pattern when reloading
" .vimrc.
nohlsearch
if exists('+termguicolors')
    let &t_8f="\<Esc>[38;2;%lu;%lu;%lum"
    let &t_8b="\<Esc>[48;2;%lu;%lu;%lum"
    set termguicolors
endif
set t_Co=256
set nocursorline
set colorcolumn=+1


" Multiple windows {{{2

set laststatus=2
set equalalways
set eadirection=both
set winminheight=0
set hidden
set switchbuf=usetab



" Multiple tabpages {{{2

set showtabline=2



" Terminal {{{2

set notitle



" Using the mouse {{{2

set mouse=



" GUI {{{2

if has('gui_running')
    set guifont=Ricty\ 14
    " set guifontwide=Ricty\ 14
    if exists('+antialias')
        set antialias
    endif
    set guioptions+=M
        \ guioptions+=c
        \ guioptions+=i
        \ guioptions-=A
        \ guioptions-=L
        \ guioptions-=P
        \ guioptions-=R
        \ guioptions-=T
        \ guioptions-=a
        \ guioptions-=b
        \ guioptions-=e
        \ guioptions-=h
        \ guioptions-=l
        \ guioptions-=m
        \ guioptions-=r
    set langmenu=none
    set winaltkeys=no
    set guicursor+=a:blinkon0
endif



" Messages and info {{{2

set shortmess+=a
    \ shortmess+=s
    \ shortmess+=I
    \ shortmess+=c
set showcmd
set noshowmode
set report=999
set confirm
set belloff=all
set helplang=en,ja



" Selecting text {{{2

set clipboard=



" Editing text {{{2

set undofile
let &undodir = g:MY_ENV.undo_dir
set textwidth=0
set backspace=indent,eol,start
"TODO: set formatoptions+=
set complete+=t
set completeopt-=preview
set pumheight=10
set noshowmatch
set matchpairs+=<:>
set nojoinspaces
set nrformats-=octal



" Tabs and indenting {{{2
" Note: you should also set them for each file types.
"       These following settings are global, used for unknown file types.

set tabstop=4
set shiftwidth=4
set smarttab
set softtabstop=4
set expandtab
set autoindent
set smartindent
set copyindent
set preserveindent



" Folding {{{2
" TODO

set foldlevelstart=0
set foldopen+=insert
set foldmethod=marker



" Diff mode {{{2

set diffopt+=vertical
    \ diffopt+=foldcolumn:3



" Mapping {{{2

set maxmapdepth=10
set notimeout
set ttimeout
set ttimeoutlen=100



" Reading and writing files {{{2

set nofixendofline
" Note: if 'fileformat' is empty, the first item of 'fileformats' is used.
set fileformats=unix,dos,mac
" Note: these settings make one backup. If you want more backups, see
"       |'backupext'|.
set backup
let &backupdir = g:MY_ENV.backup_dir
set autowrite
" Instead, use 'confirm'.
" set noautowriteall&
set autoread



" The swap file {{{2

" Note: 'dictionary' is swap files' directory.
" '//' is converted to swap file's path.
" If you are editing 'a/b.vim', Vim makes '{g:MY_ENV.cache_dir}/swap/a/b.vim'.
let &directory = g:MY_ENV.swap_dir . '//'



" Command line editing {{{2
set history=2000
set wildignore+=*.o,*.obj,*.lib
set wildignorecase
set wildmenu



" Executing external commands {{{2

set shell=zsh

set keywordprg=


" multi-byte characters {{{2

" Note: if 'fileencoding' is empty, 'encoding' is used.
set fileencodings=utf-8,cp932,euc-jp
set termencoding=utf-8
set ambiwidth=double



" Misc. {{{2

set maxfuncdepth=50
set sessionoptions+=localoptions
    \ sessionoptions+=resize
    \ sessionoptions+=winpos
let &viminfo .= ',n' . g:MY_ENV.cache_dir . '/viminfo'



" Note: List of options that may break plugins' behaviors. {{{2

" I will check these options in my plugins.
"     * autochdir (as much as possible)
"     * gdefault
"     * magic
"     * selection
"     * startofline
"     * whichwrap

" I will not check these options in my plugins, and suppose that they are left
" default.
"     * compatible
"     * cpoptions (i.e., do not use |use-cpo-save| ideom)
"     * edcompatible
"     * remap
"     * cedit (map to <C-f>)






" Installed plugins {{{1

call plug#begin(g:MY_ENV.plug_dir)

" Filetype-specific plugins are placed at the first letter of the filetype.
" E.g., plugins for Vim Script are placed in "V", even if their name don't
" begin with "V".

" A {{{2
" Switch to related files.
Plug 'kana/vim-altr'
" Async job control and completion API for vim-lsp.
" Plug 'prabirshrestha/async.vim'
" Plug 'prabirshrestha/asyncomplete.vim'
" Plug 'prabirshrestha/asyncomplete-lsp.vim'
" Extend * and #.
Plug 'haya14busa/vim-asterisk'
" If a directory is missing, make it automatically.
Plug 'mopp/autodirmake.vim'
" Auto-format python code by using autopep8.
Plug 'tell-k/vim-autopep8'

" B {{{2

" C {{{2
" Capture the output of a command.
Plug 'tyru/capture.vim'
" Comment out.
Plug 'tyru/caw.vim'
" Integrate with clang-format, the formater for C/C++.
Plug 'rhysd/vim-clang-format'
" Show highlight.
Plug 'cocopon/colorswatch.vim'
" Write git commit message.
Plug 'rhysd/committia.vim'
" Make gui-only color schemes 256colors-compatible.
Plug 'godlygeek/csapprox'

" D {{{2
" Filer for minimalists.
Plug 'justinmk/vim-dirvish'

" E {{{2
" Align text.
Plug 'junegunn/vim-easy-align'
" Motion on speed.
Plug 'easymotion/vim-easymotion'

" F {{{2
" Makes folding text cool.
Plug 'LeafCage/foldCC.vim'

" G {{{2

" H {{{2
" Syntax for HCL.
Plug 'b4b4r07/vim-hcl'

" I {{{2
" Extend incremental search.
Plug 'haya14busa/incsearch.vim'
" Incremntal search on EasyMotion.
Plug 'haya14busa/incsearch-easymotion.vim'
" Show indent.
Plug 'Yggdroot/indentLine'

" J {{{2
" Modern JavaScript
Plug 'pangloss/vim-javascript'
" Extend J.
Plug 'osyo-manga/vim-jplus'
" Filetype JSON5
Plug 'GutenYe/json5.vim'

" K {{{2

" L {{{2
" Cool status line.
Plug 'itchyny/lightline.vim'
" LSP; Language Server Protocol
" Plug 'prabirshrestha/vim-lsp'

" M {{{2
" MoonScript
Plug 'leafo/moonscript-vim'

" N {{{2
" Improve behaviors of I, A and gI in Blockwise-Visual mode.
Plug 'kana/vim-niceblock'
" Syntax highlighting for Nginx conf file.
Plug 'chr4/nginx.vim'

" O {{{2
" Extract expression and make assingment statement.
Plug 'tek/vim-operator-assign'
" Replace text without updating registers.
Plug 'kana/vim-operator-replace'
" Reverse text.
Plug 'tyru/operator-reverse.vim'
" Search in a specific region.
Plug 'osyo-manga/vim-operator-search'
" Shiffle text.
Plug 'pekepeke/vim-operator-shuffle'
" Sort text characterwise and linewise.
Plug 'emonkak/vim-operator-sort'
" Support to define user-defined operator.
Plug 'kana/vim-operator-user'
" Emacs' org-mode
Plug 'jceb/vim-orgmode'

" P {{{2
" Highlight matched parentheses.
Plug 'itchyny/vim-parenmatch'
" Pretty printing for Vim script.
Plug 'thinca/vim-prettyprint'

" Q {{{2
" Edit QuickFix and reflect to original buffers.
Plug 'thinca/vim-qfreplace'
" Highlight specified words.
Plug 't9md/vim-quickhl'
" Run anything.
Plug 'thinca/vim-quickrun'

" R {{{2
" Extend dot-repeat.
Plug 'tpope/vim-repeat'
" Rust the programming language.
Plug 'rust-lang/rust.vim'

" S {{{2
" Super surround.
Plug 'machakann/vim-sandwich'
" Judge Vim power.
Plug 'thinca/vim-scouter'
" Split one line format and Join multiline format.
Plug 'AndrewRadev/splitjoin.vim'
" Introduce user-defined mode.
Plug 'kana/vim-submode'
" Swap arguments.
Plug 'machakann/vim-swap'

" T {{{2
Plug 'osyo-manga/vim-textobj-blockwise'
" Text object for comment.
Plug 'thinca/vim-textobj-comment'
" Text object for continuous line.
Plug 'rhysd/vim-textobj-continuous-line'
" Text object for entire file.
Plug 'kana/vim-textobj-entire'
" Text object for function.
Plug 'kana/vim-textobj-function'
" Text object for indent.
Plug 'kana/vim-textobj-indent'
" Text object for last inserted text.
Plug 'rhysd/vim-textobj-lastinserted'
" Text object for last pasted text.
Plug 'gilligan/textobj-lastpaste'
" Text object for last searched pattern.
Plug 'kana/vim-textobj-lastpat'
" Text object for line.
Plug 'kana/vim-textobj-line'
" Text object for parameter.
Plug 'sgur/vim-textobj-parameter'
" Text object for space.
Plug 'saihoooooooo/vim-textobj-space'
" Text object for syntax.
Plug 'kana/vim-textobj-syntax'
" Text object for URL.
Plug 'mattn/vim-textobj-url'
" Support to define user-defined text object.
Plug 'kana/vim-textobj-user'
" Text object for words in words.
Plug 'h1mesuke/textobj-wiw'
" TOML.
Plug 'cespare/vim-toml'
" TypeScript
Plug 'leafgarland/typescript-vim'

" U {{{2

" V {{{2
" Vim documentation in Japanese.
Plug 'vim-jp/vimdoc-ja'

" W {{{2
" Adjust window size.
Plug 'rhysd/vim-window-adjuster'

" X {{{2

" Y {{{2
" Remember yank history and paste them.
Plug 'LeafCage/yankround.vim'

" Z {{{2

" Mine {{{2
Plug g:MY_ENV.my_dir

" Fork version of jremmen/vim-ripgrep
" Integration with ripgrep, fast alternative of grep command.
Plug 'nsfisis/vim-ripgrep'

call plug#end()





" Autocommands {{{1

Autocmd VimResized * wincmd =


" Calculate 'numberwidth' to fit file size.
" Note: extra 2 is the room of left and right spaces.
Autocmd BufEnter,WinEnter,BufWinEnter *
    \ let &l:numberwidth = len(line('$')) + 2


" Jump to the last cursor position when you open a file.
Autocmd BufRead *
    \ if 0 < line("'\"") && line("'\"") <= line('$') && &filetype !~# 'commit' |
    \     execute "normal g`\"" |
    \ endif


" Syntax highlight for .vimrc {{{2

Autocmd Filetype vim
    \ if expand('%') =~? 'vimrc' | call s:highlight_vimrc() | endif


function! s:highlight_vimrc() abort
    " Autocmd
    syn keyword vimrcAutocmd Autocmd skipwhite nextgroup=vimAutoEventList

    " Plugin manager command
    syn keyword vimrcPluginManager Plug

    hi def link vimrcAutocmd       vimAutocmd
    hi def link vimrcPluginManager vimCommand
endfunction



Autocmd ColorScheme ocean call s:extra_highlight()


function! s:extra_highlight() abort
    if &background != 'dark'
        return
    endif

    hi! link YankRoundRegion DiffChange

    hi! link OperatorSandwichBuns   DiffChange
    hi! link OperatorSandwichStuff  DiffChange
    hi! link OperatorSandwichDelete DiffChange
    hi! link OperatorSandwichAdd    OperatorSandwichBuns

    hi EasyMotionShade guifg=#4d4d4d guibg=NONE gui=NONE cterm=NONE
    hi EasyMotionTarget guifg=#ff7100 guibg=NONE gui=underline cterm=underline
    hi! link EasyMotionMoveHL IncSearch
endfunction




" Mappings {{{1

" Note: |:noremap| defines mappings in |Normal|, |Visual|, |Operator-Pending|
" and |Select| mode. Because I don't use |Select| mode, |:noremap| is executed
" as substitute of |:nnoremap|, |:xnoremap| and |:onoremap| for simplicity.


" Fix the search direction. {{{2

noremap <expr>  gn  v:searchforward ? 'gn' : 'gN'
noremap <expr>  gN  v:searchforward ? 'gN' : 'gn'

noremap <expr>  n  v:searchforward ? 'n' : 'N'
noremap <expr>  N  v:searchforward ? 'N' : 'n'



nnoremap <silent>  &  :%&&<CR>
xnoremap <silent>  &  :%&&<CR>



" Registers and macros. {{{2


" Access an resister in the same way in Insert and Commandline mode.
nnoremap  <C-r>  "
xnoremap  <C-r>  "


" Paste clipboard content with 'paste' enabled
function! s:paste_clipboard_content_with_paste_opt() abort
    let old_paste = &paste
    set paste
    set pastetoggle=<Plug>(pastetoggle)
    if old_paste
        return "\<C-r>+"
    else
        " 'paste' was off when the function was called. Then, 'paste' will be
        " disabled via 'pastetoggle'.
        return "\<C-r>+\<Plug>(pastetoggle)"
    endif
endfunction

" Automatically enable 'paste' and disable it after pasting clipboard's
" content.
inoremap <expr>  <C-r>+  <SID>paste_clipboard_content_with_paste_opt()


let @j = 'j.'
let @k = 'k.'
let @n = 'n.'
nnoremap   @N  N.

" Repeat the last executed macro as many times as possible.
" a => all
" Note: "let @a = '@@'" does not work well.
nnoremap  @a  9999@@


" Execute the laste executed macro again.
nnoremap  `  @@



" Emacs like key mappings in Insert and CommandLine mode. {{{2

function! s:go_to_beginning_of_line() abort
    if col('.') == match(getline('.'), '\S') + 1
        return repeat("\<C-g>U\<Left>", col('.') - 1)
    else
        return (col('.') < match(getline('.'), '\S')) ?
            \ repeat("\<C-g>U\<Right>", match(getline('.'), '\S') - col('.') + 1) :
            \ repeat("\<C-g>U\<Left>", col('.') - 1 - match(getline('.'), '\S'))
    endif
endfunction


inoremap  <C-d>  <Del>

" Go elsewhere without deviding the undo history.
inoremap <expr>  <C-a>  <SID>go_to_beginning_of_line()
inoremap <expr>  <C-e>  repeat('<C-g>U<Right>', col('$') - col('.'))
inoremap  <C-b>  <C-g>U<Left>
inoremap  <C-f>  <C-g>U<Right>

" Delete something deviding the undo history.
inoremap  <C-u>  <C-g>u<C-u>
inoremap  <C-w>  <C-g>u<C-w>

cnoremap  <C-a>  <Home>
cnoremap  <C-e>  <End>
cnoremap  <C-f>  <Right>
cnoremap  <C-b>  <Left>
cnoremap  <C-n>  <Down>
cnoremap  <C-p>  <Up>
cnoremap  <C-d>  <Del>

cnoremap  <Left>  <Nop>
inoremap  <Left>  <Nop>
cnoremap  <Right>  <Nop>
inoremap  <Right>  <Nop>




function! s:my_gA()
    let line = getline('.')
    if match(line, ';;$') != -1 " For OCaml.
        return "A\<C-g>U\<Left>\<C-g>U\<Left>"
    elseif match(line, '[,;)]$') != -1
        return "A\<C-g>U\<Left>"
    else
        return 'A'
    endif
endfunction

nnoremap <expr>  gA  <SID>my_gA()



" QuickFix or location list. {{{2

nnoremap <silent> bb  :<C-u>cc<CR>

nnoremap <silent>  bn  :<C-u><C-r>=v:count1<CR>cnext<CR>
nnoremap <silent>  bp  :<C-u><C-r>=v:count1<CR>cprevious<CR>

nnoremap <silent>  bf  :<C-u>cfirst<CR>
nnoremap <silent>  bl  :<C-u>clast<CR>

nnoremap <silent>  bS  :colder<CR>
nnoremap <silent>  bs  :cnewer<CR>



" Operators {{{2

" Throw deleted text into the black hole register ("_).
nnoremap  c  "_c
xnoremap  c  "_c
nnoremap  C  "_C
xnoremap  C  "_C


noremap  g=  =


noremap  ml  gu
noremap  mu  gU

noremap  gu  <Nop>
noremap  gU  <Nop>
xnoremap  u  <Nop>
xnoremap  U  <Nop>


xnoremap  x  "_x


nnoremap  Y  y$
" In Blockwise-Visual mode, select text linewise.
" In default, select text characterwise, neither blockwise nor linewise.
xnoremap <expr>  Y  mode() ==# 'V' ? 'y' : 'Vy'



" Swap the keys entering Replace mode and Visual-Replace mode.
nnoremap  R  gR
nnoremap  gR  R
nnoremap  r  gr
nnoremap  gr  r


nnoremap  U  <C-r>




" Motions {{{2

noremap  H  ^
noremap  L  $
noremap  M  %

noremap  gw  b
noremap  gW  B

noremap  k  gk
noremap  j  gj
noremap  gk  k
noremap  gj  j

nnoremap  gff  gF



" Tabpages and windows. {{{2

function! s:move_current_window_to_tabpage() abort
    if winnr('$') == 1
        " Leave the current window and open it in a new tabpage.
        " Because :wincmd T fails when the current tabpage has only one window.
        tab split
    else
        " Close the current window and re-open it in a new tabpage.
        wincmd T
    endif
endfunction


function! s:bdelete_bang_with_confirm() abort
    if s:getchar_with_prompt('Delete? (y/n) ') ==? 'y'
        bdelete!
    else
        echo 'Canceled'
    endif
endfunction


nnoremap <silent>  tt  :<C-u>tabnew<CR>
nnoremap <silent>  tT  :<C-u>call <SID>move_current_window_to_tabpage()<CR>

nnoremap <silent>  tn  :<C-u><C-r>=(tabpagenr() + v:count1 - 1) % tabpagenr('$') + 1<CR>tabnext<CR>
nnoremap <silent>  tp  :<C-u><C-r>=(tabpagenr('$') * 10 + tabpagenr() - v:count1 - 1) % tabpagenr('$') + 1<CR>tabnext<CR>

nnoremap <silent>  tN  :<C-u>tabmove +<CR>
nnoremap <silent>  tP  :<C-u>tabmove -<CR>

nnoremap <silent>  tsh  :<C-u>leftabove vsplit<CR>
nnoremap <silent>  tsj  :<C-u>rightbelow split<CR>
nnoremap <silent>  tsk  :<C-u>leftabove split<CR>
nnoremap <silent>  tsl  :<C-u>rightbelow vsplit<CR>

nnoremap <silent>  tsH  :<C-u>topleft vsplit<CR>
nnoremap <silent>  tsJ  :<C-u>botright split<CR>
nnoremap <silent>  tsK  :<C-u>topleft split<CR>
nnoremap <silent>  tsL  :<C-u>botright vsplit<CR>

nnoremap <silent>  twh  :<C-u>leftabove vnew<CR>
nnoremap <silent>  twj  :<C-u>rightbelow new<CR>
nnoremap <silent>  twk  :<C-u>leftabove new<CR>
nnoremap <silent>  twl  :<C-u>rightbelow vnew<CR>

nnoremap <silent>  twH  :<C-u>topleft vnew<CR>
nnoremap <silent>  twJ  :<C-u>botright new<CR>
nnoremap <silent>  twK  :<C-u>topleft new<CR>
nnoremap <silent>  twL  :<C-u>botright vnew<CR>

nnoremap  th  <C-w>h
nnoremap  tj  <C-w>j
nnoremap  tk  <C-w>k
nnoremap  tl  <C-w>l

nnoremap  tH  <C-w>H
nnoremap  tJ  <C-w>J
nnoremap  tK  <C-w>K
nnoremap  tL  <C-w>L

nnoremap  tx  <C-w>x

" r => manual resize.
" R => automatic resize.
nnoremap  tRH  <C-w>_
nnoremap  tRW  <C-w><Bar>
nnoremap  tRR  <C-w>_<C-w><Bar>

nnoremap  t=  <C-w>=

nnoremap <silent>  tq  :<C-u>bdelete<CR>
nnoremap <silent>  tQ  :<C-u>call <SID>bdelete_bang_with_confirm()<CR>

nnoremap  tc  <C-w>c

nnoremap  to  <C-w>o
nnoremap <silent>  tO  :<C-u>tabonly<CR>



function! s:smart_open(command) abort
    if winwidth(winnr()) < 150
        let modifiers = 'topleft'
    else
        let modifiers = 'vertical botright'
    endif

    try
        execute modifiers a:command
    catch
        echohl Error
        echo v:exception
        echohl None
        return
    endtry

    if &filetype ==# 'help'
        if &l:textwidth > 0
            execute 'vertical resize' &l:textwidth
        endif
        " Help tags are often right-justfied, moving the cursor to BoL.
        normal! 0
    endif
endfunction


command! -nargs=+ -complete=command
    \ SmartOpen
    \ call s:smart_open(<q-args>)




" Disable unuseful mappings. {{{2

" Avoid entering Select mode.
nnoremap  gh  <Nop>
nnoremap  gH  <Nop>
nnoremap  g<C-h>  <Nop>

nnoremap  ZZ  <Nop>
nnoremap  ZQ  <Nop>


" Help {{{2

" Search help.
nnoremap  <C-h>  :<C-u>SmartOpen help<Space>



" For writing Vim script. {{{2


if !s:is_reloading_vimrc
    " Define this function only when Vim is starting up. |v:vim_did_enter|
    function! SourceThisFile() abort
        let filename = expand('%')
        if &filetype ==# 'vim' || expand('%:e') ==# 'vim' || filename =~# '\.g\?vimrc'
            if &filetype !=# 'vim'
                setfiletype vim
            endif
            update
            unlet! g:loaded_{expand('%<')}
            source %
        else
            echo filename ' is not a Vim Script.'
        endif
    endfunction
endif



nnoremap <silent>  XV  :<C-u>tabedit $MYVIMRC<CR>

nnoremap <silent>  XX  :<C-u>call SourceThisFile()<CR>

" See |numbered-function|.
nnoremap <silent>  XF  :<C-u>function {<C-r>=v:count<CR>}<CR>

nnoremap <silent>  XM  :<C-u>messages<CR>




" Misc. {{{2

onoremap <silent>  gv  :<C-u>normal! gv<CR>

" Swap : and ;.
nnoremap  ;  :
nnoremap  :  ;
xnoremap  ;  :
xnoremap  :  ;
nnoremap  @;  @:
xnoremap  @;  @:
cnoremap <C-r>;  <C-r>:
inoremap <C-r>;  <C-r>:


" Since <ESC> may be mapped to something else somewhere, it should be :map, not
" :noremap.
imap  jk  <ESC>
cmap  jk  <ESC>



nnoremap <silent>  <C-c>  :<C-u>nohlsearch<CR>



nnoremap <silent>  <Space>o  :<C-u>call <SID>insert_blank_line(0)<CR>
nnoremap <silent>  <Space>O  :<C-u>call <SID>insert_blank_line(1)<CR>

function! s:insert_blank_line(offset) abort
    for i in range(v:count1)
        call append(line('.') - a:offset, '')
    endfor
endfunction


nnoremap <silent>  <Space>w  :<C-u>write<CR>


" Abbreviations {{{1

inoreabbrev  retrun  return
inoreabbrev  reutrn  return
inoreabbrev  tihs  this





" Commands {{{1

" Set 'makeprg' to `ninja` if `build.ninja` exists in the current working
" directory. Then, execute :make command.
command! -bang -bar -nargs=*
    \ Make
    \ if filereadable('build.ninja') |
    \     let &makeprg = 'ninja' |
    \ endif |
    \ make<bang> <args>


" Reverse a selected range in line-wise.
" Note: directly calling `g/^/m` will overwrite the current search pattern with
" '^' and highlight it, which is not expected.
" :h :keeppatterns
command! -bar -range=%
    \ Reverse
    \ keeppatterns <line1>,<line2>g/^/m<line1>-1




" ftplugin {{{1

" This command do the followings:
"     * Execute |:setlocal| for each options.
"     * Set information to restore the original setting to b:|undo_ftplugin|.

" This command is used in ftplugin/*.vim.

" Note: specify only single option.

command! -nargs=+
    \ FtpluginSetLocal
    \ call s:ftplugin_setlocal(<q-args>)

function! s:ftplugin_setlocal(qargs) abort
    execute 'setlocal' a:qargs

    let option_name = substitute(a:qargs, '\L.*', '', '')

    if option_name ==# 'shiftwidth' && exists(':IndentLinesReset') ==# 2
        IndentLinesReset
    endif

    if exists('b:undo_ftplugin')
        let b:undo_ftplugin .= '|setlocal ' . option_name . '<'
    else
        let b:undo_ftplugin = 'setlocal ' . option_name . '<'
    endif
endfunction




" Color scheme {{{1

" A command which changes color scheme with fall back.
command! -bang -nargs=?
    \ ColorScheme
    \ call s:colorscheme(<bang>0, <q-args>)


function! s:colorscheme(bang, name) abort
    try
        if get(g:, 'colors_name') isnot# a:name || a:bang
            execute 'colorscheme' a:name
        endif
    catch
        " Loading colorscheme failed.
        " The color scheme, "desert", is one of the built-in ones. Probably, it
        " will be loaded without any errors.
        colorscheme desert
    endtry
endfunction


ColorScheme! ocean




" Plugins configuration {{{1

" Load builtin plugins.

" :help :Man
runtime ftplugin/man.vim


" Disable standard plugins. {{{2

let g:loaded_2html_plugin     = 1
let g:loaded_getscriptPlugin  = 1
let g:loaded_gzip             = 1
let g:loaded_logiPat          = 1
let g:loaded_matchparen       = 1
let g:loaded_netrwPlugin      = 1
let g:loaded_rrhelper         = 1
let g:loaded_spellfile_plugin = 1
let g:loaded_tarPlugin        = 1
let g:loaded_vimballPlugin    = 1
let g:loaded_zipPlugin        = 1



" altr {{{2

" For vim
call altr#define('autoload/%.vim', 'doc/%.txt', 'plugin/%.vim')
" For C and C++
call altr#define('%.c', '%.cpp', '%.cc', '%.h', '%.hh', '%.hpp')

" Go to File Alternative
nmap  gfa  <Plug>(altr-forward)




" asterisk {{{2

function! s:asterisk(ret, keeppos)
    let g:asterisk#keeppos = a:keeppos
    return a:ret
endfunction

" Do not keep the relative cursor position.
nmap <expr>  *  <SID>asterisk('<Plug>(asterisk-z*)', 0)
omap <expr>  *  <SID>asterisk('<Plug>(asterisk-z*)', 0)
xmap <expr>  *  <SID>asterisk('<Plug>(asterisk-z*)', 0)
nmap <expr>  g*  <SID>asterisk('<Plug>(asterisk-gz*)', 0)
omap <expr>  g*  <SID>asterisk('<Plug>(asterisk-gz*)', 0)
xmap <expr>  g*  <SID>asterisk('<Plug>(asterisk-gz*)', 0)

" Keep the relative cursor position (use offset like /s+1).
" Note: I fix the search direction in typing 'n' and 'N', so there is no
" difference between '*' and '#'.
nmap <expr>  #  <SID>asterisk('<Plug>(asterisk-z*)', 1)
omap <expr>  #  <SID>asterisk('<Plug>(asterisk-z*)', 1)
xmap <expr>  #  <SID>asterisk('<Plug>(asterisk-z*)', 1)
nmap <expr>  g#  <SID>asterisk('<Plug>(asterisk-gz*)', 1)
omap <expr>  g#  <SID>asterisk('<Plug>(asterisk-gz*)', 1)
xmap <expr>  g#  <SID>asterisk('<Plug>(asterisk-gz*)', 1)



" autodirmake {{{2

let g:autodirmake#msg_highlight = 'Question'



" autopep8 {{{2

let g:autopep8_on_save = 1
let g:autopep8_disable_show_diff = 1

command!
    \ Autopep8Disable
    \ let g:autopep8_on_save = 0



" caw {{{2

let g:caw_no_default_keymappings = 1

nmap  m//  <Plug>(caw:hatpos:toggle)
xmap  m//  <Plug>(caw:hatpos:toggle)
nmap  m/w  <Plug>(caw:wrap:comment)
xmap  m/w  <Plug>(caw:wrap:comment)
nmap  m/W  <Plug>(caw:wrap:uncomment)
xmap  m/W  <Plug>(caw:wrap:uncomment)
nmap  m/b  <Plug>(caw:box:comment)
xmap  m/b  <Plug>(caw:box:comment)



" clang-format {{{2

let g:clang_format#auto_format = 1
Autocmd FileType javascript ClangFormatAutoDisable
Autocmd FileType typescript ClangFormatAutoDisable



" dirvish {{{2

" Prevent dirvish from mapping hyphen key to "<Plug>(dirvish_up)".
nmap  <Plug>(nomap-dirvish_up)  <Plug>(dirvish_up)



" easyalign {{{2

nmap  =  <Plug>(EasyAlign)
xmap  =  <Plug>(EasyAlign)



" easymotion {{{2

let g:EasyMotion_keys = 'asdfghweryuiocvbnmjkl;'
let g:EasyMotion_space_jump_first = 1
let g:EasyMotion_do_shade = 0
let g:EasyMotion_do_mappings = 0
let g:EasyMotion_smartcase = 1
let g:EasyMotion_verbose = 0
let g:EasyMotion_startofline = 0

nmap  f  <Plug>(easymotion-fl)
omap  f  <Plug>(easymotion-fl)
xmap  f  <Plug>(easymotion-fl)
nmap  F  <Plug>(easymotion-Fl)
omap  F  <Plug>(easymotion-Fl)
xmap  F  <Plug>(easymotion-Fl)
omap  t  <Plug>(easymotion-tl)
xmap  t  <Plug>(easymotion-tl)
omap  T  <Plug>(easymotion-Tl)
xmap  T  <Plug>(easymotion-Tl)

" Note: Don't use the following key sequences! It is used 'vim-sandwich'.
"  * sa
"  * sd
"  * sr
nmap  ss  <Plug>(easymotion-s2)
omap  ss  <Plug>(easymotion-s2)
xmap  ss  <Plug>(easymotion-s2)
nmap  sw  <Plug>(easymotion-bd-w)
omap  sw  <Plug>(easymotion-bd-w)
xmap  sw  <Plug>(easymotion-bd-w)
nmap  sn  <Plug>(easymotion-n)
omap  sn  <Plug>(easymotion-n)
xmap  sn  <Plug>(easymotion-n)
nmap  sN  <Plug>(easymotion-N)
omap  sN  <Plug>(easymotion-N)
xmap  sN  <Plug>(easymotion-N)
nmap  sj  <Plug>(easymotion-j)
omap  sj  <Plug>(easymotion-j)
xmap  sj  <Plug>(easymotion-j)
nmap  sk  <Plug>(easymotion-k)
omap  sk  <Plug>(easymotion-k)
xmap  sk  <Plug>(easymotion-k)



" foldcc {{{2

set foldtext=FoldCCtext()
let g:foldCCtext_head = 'repeat(">", v:foldlevel) . " "'



" incsearch {{{2

" let g:incsearch#magic = '\v'

nmap  /  <Plug>(incsearch-forward)
omap  /  <Plug>(incsearch-forward)
xmap  /  <Plug>(incsearch-forward)
nmap  ?  <Plug>(incsearch-backward)
omap  ?  <Plug>(incsearch-backward)
xmap  ?  <Plug>(incsearch-backward)
nmap  g/  <Plug>(incsearch-stay)
omap  g/  <Plug>(incsearch-stay)
xmap  g/  <Plug>(incsearch-stay)



" incsearch-easymotion {{{2

nmap  s/  <Plug>(incsearch-easymotion-stay)
omap  s/  <Plug>(incsearch-easymotion-stay)
xmap  s/  <Plug>(incsearch-easymotion-stay)
nmap  s?  <Plug>(incsearch-easymotion-stay)
omap  s?  <Plug>(incsearch-easymotion-stay)
xmap  s?  <Plug>(incsearch-easymotion-stay)
nmap  sg/  <Plug>(incsearch-easymotion-stay)
omap  sg/  <Plug>(incsearch-easymotion-stay)
xmap  sg/  <Plug>(incsearch-easymotion-stay)



" indentline {{{2

let g:indentLine_fileTypeExclude = ['help', 'chart']



" jplus {{{2

let g:jplus#input_config = {
    \ '__DEFAULT__': {'delimiter_format': ' %d '},
    \ '__EMPTY__': {'delimiter_format': ''},
    \ ' ': {'delimiter_format': ' '},
    \ ',': {'delimiter_format': '%d '},
    \ ';': {'delimiter_format': '%d '},
    \ 'l': {'delimiter_format': ''},
    \ 'L': {'delimiter_format': ''},
    \ "\<Tab>": {'delimiter_format': '%d'},
    \ }
nmap  J  <Plug>(jplus-getchar)
xmap  J  <Plug>(jplus-getchar)
nmap  gJ  <Plug>(jplus-input)
xmap  gJ  <Plug>(jplus-input)



" lightline {{{2

let g:lightline = {
    \ 'colorscheme': 'jellybeans',
    \ 'active': {
    \     'left': [['mode', 'paste'], ['readonly', 'filename', 'modified']],
    \     'right': [['linenum'], ['fileencoding', 'fileformat', 'filetype']]
    \ },
    \ 'inactive': {
    \     'left': [['readonly', 'filename', 'modified']],
    \     'right': [['linenum'], ['fileencoding', 'fileformat', 'filetype']]
    \ },
    \ 'component_function': {
    \   'linenum': s:SNR .. 'lightline_linenum',
    \   'fileformat': s:SNR .. 'lightline_fileformat',
    \ },
    \ 'mode_map': {
    \     'n' : 'N',
    \     'i' : 'I',
    \     'R' : 'R',
    \     'v' : 'V',
    \     'V' : 'V-L',
    \     "\<C-v>": 'V-B',
    \     'c' : 'C',
    \     's' : 'S',
    \     'S' : 'S-L',
    \     "\<C-s>": 'S-B',
    \     't': 'T',
    \ },
    \ 'tabline': {
    \     'left': [['tabs']],
    \     'right': [],
    \ },
    \ 'tab': {
    \     'active': ['tabnum', 'filename', 'modified'],
    \     'inactive': ['tabnum', 'filename', 'modified'],
    \ },
    \ }

function! s:lightline_linenum()
    return line('.') . '/' . line('$')
endfunction

function! s:lightline_fileformat()
    if &fileformat ==# 'unix'
        return 'LF'
    elseif &fileformat ==# 'dos'
        return 'CRLF'
    elseif &fileformat ==# 'mac'
        return 'CR'
    else
        return '-'
    endif
endfunction



" vim-lsp {{{2

" TODO



" niceblock {{{2

xmap  I  <Plug>(niceblock-I)
xmap  gI  <Plug>(niceblock-gI)
xmap  A  <Plug>(niceblock-A)






" operator-replace {{{2

nmap  <C-p>  <Plug>(operator-replace)
omap  <C-p>  <Plug>(operator-replace)
xmap  <C-p>  <Plug>(operator-replace)



" operator-search {{{2

" Note: m/ is the prefix of comment out.
nmap  m?  <Plug>(operator-search)
omap  m?  <Plug>(operator-search)
xmap  m?  <Plug>(operator-search)



" prettyprint {{{2

let g:prettyprint_indent = 2
let g:prettyprint_string = ['split']
let g:prettyprint_show_expression = 1



" qfreplace {{{2

nnoremap <silent>  br  :<C-u>Qfreplace SmartOpen<CR>



" quickhl {{{2

" TODO: CUI
let g:quickhl_manual_colors = [
    \ 'guifg=#101020 guibg=#8080c0 gui=bold',
    \ 'guifg=#101020 guibg=#80c080 gui=bold',
    \ 'guifg=#101020 guibg=#c08080 gui=bold',
    \ 'guifg=#101020 guibg=#80c0c0 gui=bold',
    \ 'guifg=#101020 guibg=#c0c080 gui=bold',
    \ 'guifg=#101020 guibg=#a0a0ff gui=bold',
    \ 'guifg=#101020 guibg=#a0ffa0 gui=bold',
    \ 'guifg=#101020 guibg=#ffa0a0 gui=bold',
    \ 'guifg=#101020 guibg=#a0ffff gui=bold',
    \ 'guifg=#101020 guibg=#ffffa0 gui=bold',
    \ ]

nmap  "  <Plug>(quickhl-manual-this)
xmap  "  <Plug>(quickhl-manual-this)
nnoremap <silent>  <C-c>  :<C-u>nohlsearch <Bar> QuickhlManualReset<CR>



" quickrun {{{2

let g:quickrun_no_default_key_mappings = 1

if !exists('g:quickrun_config')
    let g:quickrun_config = {}
endif
let g:quickrun_config.cpp = {
    \ 'cmdopt': '--std=c++17 -Wall -Wextra',
    \ }
let g:quickrun_config.d = {
    \ 'exec': 'dub run',
    \ }
let g:quickrun_config.haskell = {
    \ 'exec': ['stack build', 'stack exec %{matchstr(globpath(".,..,../..,../../..", "*.cabal"), "\\w\\+\\ze\\.cabal")}'],
    \ }
let g:quickrun_config.python = {
    \ 'command': 'python3',
    \ }


nmap  BB  <Plug>(quickrun)
xmap  BB  <Plug>(quickrun)
" nnoremap  BB  make<CR>




" repeat {{{2

nmap U <Plug>(RepeatRedo)
" Work around. vim-repeatの内部構造に大きく依存する。
" repeat#setregの呼び出しが(ほぼ)副作用を持たないことが必要
" Autoload vim-repeat immediately in order to make <Plug>(RepeatRedo) available.
" repeat#setreg() does nothing here.
call repeat#setreg('', '')


" ripgrep {{{2

" Workaround: do not open quickfix window.
" exe g:rg_window_location 'copen'
let g:rg_window_location = 'silent! echo'
let g:rg_jump_to_first = 1

command! -bang -nargs=* -complete=file -bar
    \ RG
    \ Rg<bang> <args>


" rust {{{2

let g:rustfmt_autosave = 1




" sandwich {{{2






" splitjoin {{{2

" Note: Don't use J{any sign}, 'Jl' and 'JL'. They will conflict with 'vim-jplus'.
let g:splitjoin_split_mapping = ''
let g:splitjoin_join_mapping = ''

nnoremap <silent>  JS  :<C-u>SplitjoinSplit<CR>
nnoremap <silent>  JJ  :<C-u>SplitjoinJoin<CR>



" submode {{{2

" Global settings {{{3
let g:submode_always_show_submode = 1
let g:submode_keyseqs_to_leave = ['<C-c>', '<ESC>']
let g:submode_keep_leaving_key = 1

" yankround {{{3
call submode#enter_with('YankRound', 'nv', 'rs', 'gp', '<Plug>(yankround-p)')
call submode#enter_with('YankRound', 'nv', 'rs', 'gP', '<Plug>(yankround-P)')
call submode#map('YankRound', 'nv', 'rs', 'p', '<Plug>(yankround-prev)')
call submode#map('YankRound', 'nv', 'rs', 'P', '<Plug>(yankround-next)')

" swap {{{3
call submode#enter_with('Swap', 'n', 'r', 'g>', '<Plug>(swap-next)')
call submode#map('Swap', 'n', 'r', '<', '<Plug>(swap-prev)')
call submode#enter_with('Swap', 'n', 'r', 'g<', '<Plug>(swap-prev)')
call submode#map('Swap', 'n', 'r', '>', '<Plug>(swap-next)')

" Resizing a window (height) {{{3
call submode#enter_with('WinResizeH', 'n', '', 'trh')
call submode#enter_with('WinResizeH', 'n', '', 'trh')
call submode#map('WinResizeH', 'n', '', '+', '<C-w>+')
call submode#map('WinResizeH', 'n', '', '-', '<C-w>-')

" Resizing a window (width) {{{3
call submode#enter_with('WinResizeW', 'n', '', 'trw')
call submode#enter_with('WinResizeW', 'n', '', 'trw')
call submode#map('WinResizeW', 'n', '', '+', '<C-w>>')
call submode#map('WinResizeW', 'n', '', '-', '<C-w><Lt>')

" Super undo/redo {{{3
call submode#enter_with('Undo/Redo', 'n', '', 'gu', 'g-')
call submode#map('Undo/Redo', 'n', '', 'u', 'g-')
call submode#enter_with('Undo/Redo', 'n', '', 'gU', 'g+')
call submode#map('Undo/Redo', 'n', '', 'U', 'g+')



" swap {{{2

let g:swap_no_default_key_mappings = 1



" textobj-continuousline {{{2

let g:textobj_continuous_line_no_default_key_mappings = 1

omap  aL  <Plug>(textobj-continuous-cpp-a)
xmap  aL  <Plug>(textobj-continuous-cpp-a)
omap  iL  <Plug>(textobj-continuous-cpp-i)
xmap  iL  <Plug>(textobj-continuous-cpp-i)

Autocmd FileType vim omap <buffer>  aL  <Plug>(textobj-continuous-vim-a)
Autocmd FileType vim xmap <buffer>  aL  <Plug>(textobj-continuous-vim-a)
Autocmd FileType vim omap <buffer>  iL  <Plug>(textobj-continuous-vim-i)
Autocmd FileType vim xmap <buffer>  iL  <Plug>(textobj-continuous-vim-i)



" textobj-lastpaste {{{2

let g:textobj_lastpaste_no_default_key_mappings = 1

omap  iP  <Plug>(textobj-lastpaste-i)
xmap  iP  <Plug>(textobj-lastpaste-i)
omap  aP  <Plug>(textobj-lastpaste-a)
xmap  aP  <Plug>(textobj-lastpaste-a)



" textobj-space {{{2

let g:textobj_space_no_default_key_mappings = 1

omap  a<Space>  <Plug>(textobj-space-a)
xmap  a<Space>  <Plug>(textobj-space-a)
omap  i<Space>  <Plug>(textobj-space-i)
xmap  i<Space>  <Plug>(textobj-space-i)


" textobj-wiw {{{2

let g:textobj_wiw_no_default_key_mappings = 1

nmap  <C-w>  <Plug>(textobj-wiw-n)
omap  <C-w>  <Plug>(textobj-wiw-n)
xmap  <C-w>  <Plug>(textobj-wiw-n)
nmap  g<C-w>  <Plug>(textobj-wiw-p)
omap  g<C-w>  <Plug>(textobj-wiw-p)
xmap  g<C-w>  <Plug>(textobj-wiw-p)
nmap  <C-e>  <Plug>(textobj-wiw-N)
omap  <C-e>  <Plug>(textobj-wiw-N)
xmap  <C-e>  <Plug>(textobj-wiw-N)
nmap  g<C-e>  <Plug>(textobj-wiw-P)
omap  g<C-e>  <Plug>(textobj-wiw-P)
xmap  g<C-e>  <Plug>(textobj-wiw-P)

omap  a<C-w>  <Plug>(textobj-wiw-a)
xmap  a<C-w>  <Plug>(textobj-wiw-a)
omap  i<C-w>  <Plug>(textobj-wiw-i)
xmap  i<C-w>  <Plug>(textobj-wiw-i)



" window-adjuster {{{2

nnoremap <silent>  tRw  :<C-u>AdjustScreenWidth<CR>
nnoremap <silent>  tRh  :<C-u>AdjustScreenHeight<CR>
nnoremap <silent>  tRr  :<C-u>AdjustScreenWidth <Bar> AdjustScreenHeight<CR>





" yankround {{{2

let g:yankround_dir = g:MY_ENV.cache_dir . '/yankround'
let g:yankround_use_region_hl = 1











" Utilities {{{1



"" Wrapper of |getchar()|.
function! s:getchar()
    let ch = getchar()
    while ch == "\<CursorHold>"
        let ch = getchar()
    endwhile
    return type(ch) == v:t_number ? nr2char(ch) : ch
endfunction



"" Wrapper of |:echo| and |:echohl|.
function! s:echo(message, ...) abort
    let highlight = get(a:000, 0, 'None')
    redraw
    execute 'echohl' highlight
    echo a:message
    echohl None
endfunction



"" Wrapper of |getchar()|.
function! s:getchar_with_prompt(prompt) abort
    call s:echo(a:prompt)
    return s:getchar()
endfunction



"" Wrapper of |input()|.
"" Only when it is used in a mapping, |inputsave()| and |inputstore()| are
"" required.
function! s:input(...) abort
    call inputsave()
    let result = call('input', a:000)
    call inputrestore()
    return result
endfunction




" They are not used any longer. {{{2
if 0
    "" @param nr (WinNr or WinId) 0 means the current window.
    "" @return (Number) The window width applied 'numberwidth' and 'foldcolumn' to.
    function! s:displayed_winwidth(nr) abort
        let winwidth = winwidth(a:nr)
        let foldcolumn = getwinvar(a:nr, '&foldcolumn')
        let numberwidth = getwinvar(a:nr, '&number') ?
            \ max([getwinvar(a:nr, '&numberwidth'), len(line('$'))]) : 0

        return winwidth - foldcolumn - numberwidth
    endfunction



    "" Wrapper of |char2nr()|.
    function! s:char2nr(x, ...) abort
        return type(a:x) == v:t_number ?
            \ a:x :
            \ char2nr(a:x, get(a:000, 0, 0))
    endfunction



    "" Wrapper of |nr2char()|.
    function! s:nr2char(x, ...) abort
        return type(a:x) == v:t_string ?
            \ a:x :
            \ nr2char(a:x, get(a:000, 0, 0))
    endfunction



    "" @param begin (Char)
    "" @param end (Char) Included
    "" @return ([Char])
    "" e.g.) s:char_range('1', '5')
    ""       => ['1', '2', '3', '4', '5']
    function! s:char_range(begin, end) abort
        return map(range(s:char2nr(a:begin), s:char2nr(a:end)), 's:nr2char(v:val)')
    endfunction



    "" Splits a string character by character.
    "" @param str (String)
    "" @return ([Char])
    "" e.g.) s:each_char('bar')
    ""       => ['b', 'a', 'r']
    function! s:each_char(str) abort
        return split(a:str, '\zs')
    endfunction



    "" Returns a list of pairs of index and item.
    "" @param list (List)
    "" @return ([[Number, Any]])
    "" e.g.) s:with_index(['a', 'b', 'c'])
    ""       => [[0, 'a'], [1, 'b'], [2, 'c']]
    function! s:with_index(list) abort
        return map(a:list, '[v:key, v:val]')
    endfunction



    "" Concatenates all passed lists and returns it.
    "" @param ... ([Any])
    "" @return ([List])
    function! s:extend_list(...) abort
        let ret = []
        for list in a:000
            call extend(ret, list)
        endfor
        return ret
    endfunction



    "" @param list ([Any])
    "" @param length (Number)
    "" @param [filled_item] (Any)
    function! s:normalize_list_length(list, length, ...) abort
        let delta = len(a:list) - a:length
        if delta < 0 " too short
            let filled_item = a:1
            return expand(a:list, repeat(filled_item, -delta))
        else " too long
            return a:list[:(delta - 1)]
        endif
    endfunction



    " If the current line contains any multibyte characters, It doesn't work:
    "     getline('.')[col('.') - 1]
    " This function can handle multibyte characters.
    function! s:get_cursor_char() abort
        return matchstr(getline('.'), '.' , col('.') - 1)
    endfunction
endif





" Modelines {{{1
" vim: expandtab:softtabstop=4:shiftwidth=4:textwidth=80:colorcolumn=+1:
" vim: foldenable:foldmethod=marker:foldlevel=0: