summaryrefslogtreecommitdiff
path: root/start/signify/autoload/sy.vim
blob: b994aff6c6fd40460eec5d206a976b8243c715c4 (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
" vim: et sw=2 sts=2

scriptencoding utf-8

" Init: values {{{1
let s:has_doau_modeline = v:version > 703 || v:version == 703 && has('patch442')

" Function: #start {{{1
function! sy#start() abort
  if g:signify_locked
    call sy#verbose('Locked.')
    return
  endif

  let sy_path = resolve(expand('%:p'))
  if has('win32')
    let sy_path = substitute(sy_path, '\v^(\w):\\\\', '\1:\\', '')
  endif

  if s:skip(sy_path)
    call sy#verbose('Skip file: '. sy_path)
    if exists('b:sy')
      call sy#sign#remove_all_signs(bufnr(''))
      unlet! b:sy
    endif
    return
  endif

  if !exists('b:sy') || b:sy.path != sy_path
    call sy#verbose('Register new file: '. sy_path)
    let b:sy = {
          \ 'path':       sy_path,
          \ 'buffer':     bufnr(''),
          \ 'active':     0,
          \ 'detecting':  0,
          \ 'vcs':        [],
          \ 'hunks':      [],
          \ 'signid':     0x100,
          \ 'updated_by': '',
          \ 'stats':      [-1, -1, -1],
          \ 'info':       {
          \    'dir':  fnamemodify(sy_path, ':p:h'),
          \    'path': sy#util#escape(sy_path),
          \    'file': sy#util#escape(fnamemodify(sy_path, ':t'))
          \ }}
    if get(g:, 'signify_disable_by_default')
      call sy#verbose('Disabled by default.')
      return
    endif
    let b:sy.active = 1
    call sy#repo#detect()
  elseif has('vim_starting')
    call sy#verbose("Don't run Sy more than once during startup.")
    return
  elseif !b:sy.active
    call sy#verbose('Inactive buffer.')
    return
  elseif empty(b:sy.vcs)
    if get(b:sy, 'retry')
      let b:sy.retry = 0
      call sy#verbose('Redetecting VCS.')
      call sy#repo#detect()
    else
      if get(b:sy, 'detecting')
        call sy#verbose('Detection is already in progress.')
      else
        call sy#verbose('No VCS found. Disabling.')
        call sy#disable()
      endif
    endif
  else
    for vcs in b:sy.vcs
      let job_id = get(b:, 'sy_job_id_'. vcs)
      if type(job_id) != type(0) || job_id > 0
        call sy#verbose('Update is already in progress.', vcs)
      else
        call sy#verbose('Updating signs.', vcs)
        call sy#repo#get_diff_start(vcs)
      endif
    endfor
  endif
endfunction

" Function: #set_signs {{{1
function! sy#set_signs(sy, vcs, diff) abort
  call sy#verbose('set_signs()', a:vcs)

  if a:sy.stats == [-1, -1, -1]
    let a:sy.stats = [0, 0, 0]
  endif

  if empty(a:diff)
    call sy#verbose('No changes found.', a:vcs)
    let a:sy.stats = [0, 0, 0]
    call sy#sign#remove_all_signs(a:sy.buffer)
    return
  endif

  if get(g:, 'signify_line_highlight')
    call sy#highlight#line_enable()
  else
    call sy#highlight#line_disable()
  endif

  call sy#sign#process_diff(a:sy, a:vcs, a:diff)

  if exists('#User#Signify')
    execute 'doautocmd' (s:has_doau_modeline ? '<nomodeline>' : '') 'User Signify'
  endif
endfunction

" Function: #stop {{{1
function! sy#stop(bufnr) abort
  let sy = getbufvar(a:bufnr, 'sy')
  if empty(sy)
    return
  endif

  call sy#sign#remove_all_signs(a:bufnr)
endfunction

" Function: #enable {{{1
function! sy#enable() abort
  if !exists('b:sy')
    call sy#start()
    return
  endif

  if !b:sy.active
    let b:sy.active = 1
    let b:sy.retry  = 1
    call sy#start()
  endif
endfunction

" Function: #disable {{{1
function! sy#disable() abort
  if exists('b:sy') && b:sy.active
    call sy#stop(b:sy.buffer)
    let b:sy.active = 0
    let b:sy.stats = [-1, -1, -1]
  endif
endfunction

" Function: #toggle {{{1
function! sy#toggle() abort
  if !exists('b:sy') || !b:sy.active
    call sy#enable()
  else
    call sy#disable()
  endif
endfunction

" Function: #buffer_is_active {{{1
function! sy#buffer_is_active()
  return exists('b:sy') && b:sy.active
endfunction

" Function: #verbose {{{1
function! sy#verbose(msg, ...) abort
  if &verbose
    if type(a:msg) == type([])
      for msg in a:msg
        echomsg printf('[sy%s] %s', (a:0 ? ':'.a:1 : ''), msg)
      endfor
    else
      echomsg printf('[sy%s] %s', (a:0 ? ':'.a:1 : ''), a:msg)
    endif
  endif
endfunction

" Function: s:skip {{{1
function! s:skip(path)
  if &diff || !filereadable(a:path)
    return 1
  endif

  if exists('g:signify_skip_filetype')
    if has_key(g:signify_skip_filetype, &filetype)
      return 1
    elseif has_key(g:signify_skip_filetype, 'help') && (&buftype == 'help')
      return 1
    endif
  endif

  if exists('g:signify_skip_filename') && has_key(g:signify_skip_filename, a:path)
    return 1
  endif

  if exists('g:signify_skip_filename_pattern')
    for pattern in g:signify_skip_filename_pattern
      if a:path =~ pattern
        return 1
      endif
    endfor
  endif

  return 0
endfunction