aboutsummaryrefslogtreecommitdiff
path: root/autoload/digraph_search.vim
blob: 172b17c26700c316515ab9bce50b3669d20183f6 (plain) (blame)
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
" Search for digraphs
function! digraph_search#Search() abort

  " Get the search string
  let l:search = input('Digraph search: ')
  if !strlen(l:search)
    return
  endif

  " Look for the uppercased search in the table
  let l:results = []
  for l:digraph in digraph_search#Digraphs()
    if stridx(l:digraph['name'], toupper(l:search)) != -1
      call add(l:results, l:digraph)
    endif
  endfor

  " Print results, or if there weren't any, say so
  redraw
  echo 'Digraphs matching '.toupper(l:search).':'
  if len(l:results)
    for l:result in l:results
      echo l:result['char']
            \.'  '.l:result['keys']
            \.'  '.l:result['name']
    endfor
  else
    echo 'None!'
  endif

endfunction

" Get private memoized list of digraph dictionary objects
function! digraph_search#Digraphs() abort

  " We haven't been called yet; get the digraph list
  if !exists('s:digraphs')
    let s:digraphs = []
    let l:table = 0
    for l:line in readfile($VIMRUNTIME.'/doc/digraph.txt')

      " Flag whether we're in one of the digraph tables; look for the heading
      let l:table = l:table && strlen(l:line)
            \ || l:line =~# '\C\*digraph-table\%(-mbyte\)\=\*$'
      " Skip to next line if not in a table
      if !l:table
        continue
      endif

      " Check whether this row matches a parseable digraph row
      let l:match = matchlist(l:line,
             \   '^\(\S\)\+\s\+'
             \ . '\(\S\S\)\s\+'
             \ . '\%(0x\)\=\x\+\s\+'
             \ . '\d\+\s\+'
             \ . '\(.\+\)'
             \ )
      " Skip to next line if not a table row match
      if !len(l:match)
        continue
      endif

      " Add to the digraphs list; key is digraph, value is full name
      call add(s:digraphs, {
            \ 'char': l:match[1],
            \ 'keys': l:match[2],
            \ 'name': l:match[3],
            \ })
    endfor
  endif

  " Return the list, whether newly-generated or from the first call
  return s:digraphs

endfunction