aboutsummaryrefslogtreecommitdiff
path: root/autoload/digraph_search.vim
blob: bae49733e7974a4907d168de5257995516268b40 (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#() abort

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

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

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

endfunction

" Get private memoized list of digraph dictionary objects
function! s:Digraphs() abort

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

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

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

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

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

endfunction