summaryrefslogtreecommitdiff
path: root/config/vim/autoload/emmet/util.vim
blob: 78960c0689e94c9cb155872b435f05466255e135 (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
"==============================================================================
" region utils
"==============================================================================
" deleteContent : delete content in region
"   if region make from between '<foo>' and '</foo>'
"   --------------------
"   begin:<foo>
"   </foo>:end
"   --------------------
"   this function make the content as following
"   --------------------
"   begin::end
"   --------------------
function! emmet#util#deleteContent(region) abort
  let lines = getline(a:region[0][0], a:region[1][0])
  call setpos('.', [0, a:region[0][0], a:region[0][1], 0])
  silent! exe 'delete '.(a:region[1][0] - a:region[0][0])
  call setline(line('.'), lines[0][:a:region[0][1]-2] . lines[-1][a:region[1][1]])
endfunction

" change_content : change content in region
"   if region make from between '<foo>' and '</foo>'
"   --------------------
"   begin:<foo>
"   </foo>:end
"   --------------------
"   and content is
"   --------------------
"   foo
"   bar
"   baz
"   --------------------
"   this function make the content as following
"   --------------------
"   begin:foo
"   bar
"   baz:end
"   --------------------
function! emmet#util#setContent(region, content) abort
  let newlines = split(a:content, '\n', 1)
  let oldlines = getline(a:region[0][0], a:region[1][0])
  call setpos('.', [0, a:region[0][0], a:region[0][1], 0])
  silent! exe 'delete '.(a:region[1][0] - a:region[0][0])
  if len(newlines) == 0
    let tmp = ''
    if a:region[0][1] > 1
      let tmp = oldlines[0][:a:region[0][1]-2]
    endif
    if a:region[1][1] >= 1
      let tmp .= oldlines[-1][a:region[1][1]:]
    endif
    call setline(line('.'), tmp)
  elseif len(newlines) == 1
    if a:region[0][1] > 1
      let newlines[0] = oldlines[0][:a:region[0][1]-2] . newlines[0]
    endif
    if a:region[1][1] >= 1
      let newlines[0] .= oldlines[-1][a:region[1][1]:]
    endif
    call setline(line('.'), newlines[0])
  else
    if a:region[0][1] > 1
      let newlines[0] = oldlines[0][:a:region[0][1]-2] . newlines[0]
    endif
    if a:region[1][1] >= 1
      let newlines[-1] .= oldlines[-1][a:region[1][1]:]
    endif
    call setline(line('.'), newlines[0])
    call append(line('.'), newlines[1:])
  endif
endfunction

" select_region : select region
"   this function make a selection of region
function! emmet#util#selectRegion(region) abort
  call setpos('.', [0, a:region[1][0], a:region[1][1], 0])
  normal! v
  call setpos('.', [0, a:region[0][0], a:region[0][1], 0])
endfunction

" point_in_region : check point is in the region
"   this function return 0 or 1
function! emmet#util#pointInRegion(point, region) abort
  if !emmet#util#regionIsValid(a:region) | return 0 | endif
  if a:region[0][0] > a:point[0] | return 0 | endif
  if a:region[1][0] < a:point[0] | return 0 | endif
  if a:region[0][0] == a:point[0] && a:region[0][1] > a:point[1] | return 0 | endif
  if a:region[1][0] == a:point[0] && a:region[1][1] < a:point[1] | return 0 | endif
  return 1
endfunction

" cursor_in_region : check cursor is in the region
"   this function return 0 or 1
function! emmet#util#cursorInRegion(region) abort
  if !emmet#util#regionIsValid(a:region) | return 0 | endif
  let cur = emmet#util#getcurpos()[1:2]
  return emmet#util#pointInRegion(cur, a:region)
endfunction

" region_is_valid : check region is valid
"   this function return 0 or 1
function! emmet#util#regionIsValid(region) abort
  if a:region[0][0] == 0 || a:region[1][0] == 0 | return 0 | endif
  return 1
endfunction

" search_region : make region from pattern which is composing start/end
"   this function return array of position
function! emmet#util#searchRegion(start, end) abort
  let b = searchpairpos(a:start, '', a:end, 'bcnW')
  if b == [0, 0]
    return [searchpairpos(a:start, '', a:end, 'bnW'), searchpairpos(a:start, '\%#', a:end, 'nW')]
  else
    return [b, searchpairpos(a:start, '', a:end. '', 'nW')]
  endif
endfunction

" get_content : get content in region
"   this function return string in region
function! emmet#util#getContent(region) abort
  if !emmet#util#regionIsValid(a:region)
    return ''
  endif
  let lines = getline(a:region[0][0], a:region[1][0])
  if a:region[0][0] == a:region[1][0]
    let lines[0] = lines[0][a:region[0][1]-1:a:region[1][1]-1]
  else
    let lines[0] = lines[0][a:region[0][1]-1:]
    let lines[-1] = lines[-1][:a:region[1][1]-1]
  endif
  return join(lines, "\n")
endfunction

" region_in_region : check region is in the region
"   this function return 0 or 1
function! emmet#util#regionInRegion(outer, inner) abort
  if !emmet#util#regionIsValid(a:inner) || !emmet#util#regionIsValid(a:outer)
    return 0
  endif
  return emmet#util#pointInRegion(a:inner[0], a:outer) && emmet#util#pointInRegion(a:inner[1], a:outer)
endfunction

" get_visualblock : get region of visual block
"   this function return region of visual block
function! emmet#util#getVisualBlock() abort
  return [[line("'<"), col("'<")], [line("'>"), col("'>")]]
endfunction

"==============================================================================
" html utils
"==============================================================================
function! emmet#util#getContentFromURL(url) abort
  let res = system(printf('%s -i %s', g:emmet_curl_command, shellescape(substitute(a:url, '#.*', '', ''))))
  while res =~# '^HTTP/1.\d 3' || res =~# '^HTTP/1\.\d 200 Connection established' || res =~# '^HTTP/1\.\d 100 Continue'
    let pos = stridx(res, "\r\n\r\n")
    if pos != -1
      let res = strpart(res, pos+4)
    else
      let pos = stridx(res, "\n\n")
      let res = strpart(res, pos+2)
    endif
  endwhile
  let pos = stridx(res, "\r\n\r\n")
  if pos != -1
    let content = strpart(res, pos+4)
  else
    let pos = stridx(res, "\n\n")
    let content = strpart(res, pos+2)
  endif
  let header = res[:pos-1]
  let charset = matchstr(content, '<meta[^>]\+content=["''][^;"'']\+;\s*charset=\zs[^;"'']\+\ze["''][^>]*>')
  if len(charset) == 0
    let charset = matchstr(content, '<meta\s\+charset=["'']\?\zs[^"'']\+\ze["'']\?[^>]*>')
  endif
  if len(charset) == 0
    let charset = matchstr(header, '\nContent-Type:.* charset=[''"]\?\zs[^''";\n]\+\ze')
  endif
  if len(charset) == 0
    let s1 = len(split(content, '?'))
    let utf8 = iconv(content, 'utf-8', &encoding)
    let s2 = len(split(utf8, '?'))
    return (s2 == s1 || s2 >= s1 * 2) ? utf8 : content
  endif
  return iconv(content, charset, &encoding)
endfunction

function! emmet#util#getTextFromHTML(buf) abort
  let threshold_len = 100
  let threshold_per = 0.1
  let buf = a:buf

  let buf = strpart(buf, stridx(buf, '</head>'))
  let buf = substitute(buf, '<style[^>]*>.\{-}</style>', '', 'g')
  let buf = substitute(buf, '<script[^>]*>.\{-}</script>', '', 'g')
  let res = ''
  let max = 0
  let mx = '\(<td[^>]\{-}>\)\|\(<\/td>\)\|\(<div[^>]\{-}>\)\|\(<\/div>\)'
  let m = split(buf, mx)
  for str in m
    let c = split(str, '<[^>]*?>')
    let str = substitute(str, '<[^>]\{-}>', ' ', 'g')
    let str = substitute(str, '&gt;', '>', 'g')
    let str = substitute(str, '&lt;', '<', 'g')
    let str = substitute(str, '&quot;', '"', 'g')
    let str = substitute(str, '&apos;', '''', 'g')
    let str = substitute(str, '&nbsp;', ' ', 'g')
    let str = substitute(str, '&yen;', '\&#65509;', 'g')
    let str = substitute(str, '&amp;', '\&', 'g')
    let str = substitute(str, '^\s*\(.*\)\s*$', '\1', '')
    let str = substitute(str, '\s\+', ' ', 'g')
    let l = len(str)
    if l > threshold_len
      let per = (l+0.0) / len(c)
      if max < l && per > threshold_per
        let max = l
        let res = str
      endif
    endif
  endfor
  let res = substitute(res, '^\s*\(.*\)\s*$', '\1', 'g')
  return res
endfunction

function! emmet#util#getImageSize(fn) abort
  let fn = a:fn

  if emmet#util#isImageMagickInstalled()
    return emmet#util#imageSizeWithImageMagick(fn)
  endif

  if filereadable(fn)
    let hex = substitute(system('xxd -p "'.fn.'"'), '\n', '', 'g')
  else
    if fn !~# '^\w\+://'
      let path = fnamemodify(expand('%'), ':p:gs?\\?/?')
      if has('win32') || has('win64') |
        let path = tolower(path)
      endif
      for k in keys(g:emmet_docroot)
        let root = fnamemodify(k, ':p:gs?\\?/?')
        if has('win32') || has('win64') |
          let root = tolower(root)
        endif
        if stridx(path, root) == 0
          let v = g:emmet_docroot[k]
          let fn = (len(v) == 0 ? k : v) . fn
          break
        endif
      endfor
    endif
    let hex = substitute(system(g:emmet_curl_command.' "'.fn.'" | xxd -p'), '\n', '', 'g')
  endif

  let [width, height] = [-1, -1]
  if hex =~# '^89504e470d0a1a0a'
    let width = eval('0x'.hex[32:39])
    let height = eval('0x'.hex[40:47])
  endif
  if hex =~# '^ffd8'
    let pos = 4
    while pos < len(hex)
      let bs = hex[pos+0:pos+3]
      let pos += 4
      if bs ==# 'ffc0' || bs ==# 'ffc2'
        let pos += 6
        let height = eval('0x'.hex[pos+0:pos+1])*256 + eval('0x'.hex[pos+2:pos+3])
        let pos += 4
        let width = eval('0x'.hex[pos+0:pos+1])*256 + eval('0x'.hex[pos+2:pos+3])
        break
      elseif bs =~# 'ffd[9a]'
        break
      elseif bs =~# 'ff\(e[0-9a-e]\|fe\|db\|dd\|c4\)'
        let pos += (eval('0x'.hex[pos+0:pos+1])*256 + eval('0x'.hex[pos+2:pos+3])) * 2
      endif
    endwhile
  endif
  if hex =~# '^47494638'
    let width = eval('0x'.hex[14:15].hex[12:13])
    let height = eval('0x'.hex[18:19].hex[16:17])
  endif

  return [width, height]
endfunction

function! emmet#util#imageSizeWithImageMagick(fn) abort
  let img_info = system('identify -format "%wx%h" "'.a:fn.'"')
  let img_size = split(substitute(img_info, '\n', '', ''), 'x')
  if len(img_size) != 2
    return [-1, -1]
  endif
  return img_size
endfunction

function! emmet#util#isImageMagickInstalled() abort
  if !get(g:, 'emmet_use_identify', 1)
    return 0
  endif
  return executable('identify')
endfunction

function! s:b64encode(bytes, table, pad)
  let b64 = []
  for i in range(0, len(a:bytes) - 1, 3)
    let n = a:bytes[i] * 0x10000
          \ + get(a:bytes, i + 1, 0) * 0x100
          \ + get(a:bytes, i + 2, 0)
    call add(b64, a:table[n / 0x40000])
    call add(b64, a:table[n / 0x1000 % 0x40])
    call add(b64, a:table[n / 0x40 % 0x40])
    call add(b64, a:table[n % 0x40])
  endfor
  if len(a:bytes) % 3 == 2
    let b64[-1] = a:pad
  elseif len(a:bytes) % 3 == 1
    let b64[-1] = a:pad
    let b64[-2] = a:pad
  endif
  return b64
endfunction

function! emmet#util#imageEncodeDecode(fn, flag) abort
  let fn = a:fn

  if filereadable(fn)
    let hex = substitute(system('xxd -p "'.fn.'"'), '\n', '', 'g')
  else
    if fn !~# '^\w\+://'
      let path = fnamemodify(expand('%'), ':p:gs?\\?/?')
      if has('win32') || has('win64') |
        let path = tolower(path)
      endif
      for k in keys(g:emmet_docroot)
        let root = fnamemodify(k, ':p:gs?\\?/?')
        if has('win32') || has('win64') |
          let root = tolower(root)
        endif
        if stridx(path, root) == 0
          let v = g:emmet_docroot[k]
          let fn = (len(v) == 0 ? k : v) . fn
          break
        endif
      endfor
    endif
    let hex = substitute(system(g:emmet_curl_command.' "'.fn.'" | xxd -p'), '\n', '', 'g')
  endif

  let bin = map(split(hex, '..\zs'), 'eval("0x" . v:val)')
  let table = split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', '\zs')
  let ret = 'data:image/'
  if hex =~# '^89504e470d0a1a0a'
    let ret .= 'png'
  elseif hex =~# '^ffd8'
    let ret .= 'jpeg'
  elseif hex =~# '^47494638'
    let ret .= 'gif'
  else
    let ret .= 'unknown'
  endif
  return ret . ';base64,' . join(s:b64encode(bin, table, '='), '')
endfunction

function! emmet#util#unique(arr) abort
  let m = {}
  let r = []
  for i in a:arr
    if !has_key(m, i)
      let m[i] = 1
      call add(r, i)
    endif
  endfor
  return r
endfunction

let s:seed = localtime()
function! emmet#util#srand(seed) abort
  let s:seed = a:seed
endfunction

function! emmet#util#rand() abort
  let s:seed = s:seed * 214013 + 2531011
  return (s:seed < 0 ? s:seed - 0x80000000 : s:seed) / 0x10000 % 0x8000
endfunction

function! emmet#util#cache(name, ...) abort
  let content = get(a:000, 0, '')
  let dir = expand('~/.emmet/cache')
  if !isdirectory(dir)
    call mkdir(dir, 'p', 0700)
  endif
  let file = dir . '/' . substitute(a:name, '\W', '_', 'g')
  if len(content) == 0
    if !filereadable(file)
      return ''
    endif
	return join(readfile(file), "\n")
  endif
  call writefile(split(content, "\n"), file)
endfunction

function! emmet#util#getcurpos() abort
  let pos = getpos('.')
  if mode(0) ==# 'i' && pos[2] > 0
    let pos[2] -=1
  endif
  return pos
endfunction

function! emmet#util#closePopup() abort
  return pumvisible() ? "\<c-e>" : ''
endfunction