1" Vim completion script
2" Language:	XHTML 1.0 Strict
3" Maintainer:	Mikolaj Machowski ( mikmach AT wp DOT pl )
4" Last Change:	2005 Sep 23
5
6function! htmlcomplete#CompleteTags(findstart, base)
7  if a:findstart
8    " locate the start of the word
9    let line = getline('.')
10    let start = col('.') - 1
11    while start >= 0 && line[start - 1] !~ '<'
12      let start -= 1
13    endwhile
14	if start < 0
15		let curpos = line('.')
16		let stylestart = searchpair('<style\>', '', '<\/style\>', "bnW")
17		let styleend   = searchpair('<style\>', '', '<\/style\>', "nW")
18		if stylestart != 0 && styleend != 0
19			if stylestart <= curpos && styleend >= curpos
20				let b:csscompl = 1
21				let start = 0
22			endif
23		endif
24	endif
25    return start
26  else
27	" Check if we should do CSS completion inside of <style> tag
28	if exists("b:csscompl")
29		unlet! b:csscompl
30		return csscomplete#CompleteCSS(0, a:base)
31	endif
32	if a:base =~ '>'
33		" Generally if a:base contains > it means we are outside of tag and
34		" should abandon action - with one exception: <style> span { bo
35		if a:base =~ 'style[^>]\{-}>[^<]\{-}$'
36			return csscomplete#CompleteCSS(0, a:base)
37		else
38			return []
39		endif
40	endif
41
42	" Set attribute groups
43    let coreattrs = ["id", "class", "style", "title"]
44    let i18n = ["lang", "xml:lang", "dir=\"ltr\" ", "dir=\"rtl\" "]
45    let events = ["onclick", "ondblclick", "onmousedown", "onmouseup", "onmousemove",
46    			\ "onmouseout", "onkeypress", "onkeydown", "onkeyup"]
47    let focus = ["accesskey", "tabindex", "onfocus", "onblur"]
48    let coregroup = coreattrs + i18n + events
49    let res = []
50    let res2 = []
51    " find tags matching with "a:base"
52	" If a:base contains > it means we are already outside of tag and we
53	" should abandon action
54	" If a:base contains white space it is attribute.
55	" It could be also value of attribute...
56	" We have to get first word to offer
57	" proper completions
58	let tag = split(a:base)[0]
59	" Get last word, it should be attr name
60	let attr = matchstr(a:base, '.*\s\zs.*')
61	" Possible situations where any prediction would be difficult:
62	" 1. Events attributes
63	if a:base =~ '\s'
64		" Sort out style, class, and on* cases
65		" Perfect solution for style would be switching for CSS completion. Is
66		" it possible?
67		" Also retrieving class names from current file and linked
68		" stylesheets.
69		if a:base =~ "\\(on[a-z]*\\|id\\|style\\|class\\)\\s*=\\s*[\"']"
70			if a:base =~ "\\(id\\|class\\)\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
71				if a:base =~ "class\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
72					let search_for = "class"
73				elseif a:base =~ "id\\s*=\\s*[\"'][a-zA-Z0-9_ -]*$"
74					let search_for = "id"
75				endif
76				" Handle class name completion
77				" 1. Find lines of <link stylesheet>
78				" 1a. Check file for @import
79				" 2. Extract filename(s?) of stylesheet,
80				call cursor(1,1)
81				let head = getline(search('<head\>'), search('<\/head>'))
82				let headjoined = join(copy(head), ' ')
83				if headjoined =~ '<style'
84					let stylehead = substitute(headjoined, '+>\*[,', ' ', 'g')
85					if search_for == 'class'
86						let styleheadlines = split(stylehead)
87						let headclasslines = filter(copy(styleheadlines), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
88					else
89						let stylesheet = split(headjoined, '[{}]')
90						" Get all lines which fit id syntax
91						let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
92						" Filter out possible color definitions
93						call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
94						" Filter out complex border definitions
95						call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
96						let templines = join(classlines, ' ')
97						let headclasslines = split(templines)
98						call filter(headclasslines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
99					endif
100					let internal = 1
101				else
102					let internal = 0
103				endif
104				let styletable = []
105				let secimportfiles = []
106				let filestable = filter(copy(head), "v:val =~ '\\(@import\\|link.*stylesheet\\)'")
107				for line in filestable
108					if line =~ "@import"
109						let styletable += [matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")]
110					elseif line =~ "<link"
111						let styletable += [matchstr(line, "href\\s*=\\s*[\"']\\zs\\f\\+\\ze")]
112					endif
113				endfor
114				for file in styletable
115					if filereadable(file)
116						let stylesheet = readfile(file)
117						let secimport = filter(copy(stylesheet), "v:val =~ '@import'")
118						if len(secimport) > 0
119							for line in secimport
120								let secfile = matchstr(line, "import\\s\\+\\(url(\\)\\?[\"']\\?\\zs\\f\\+\\ze")
121								let secfile = fnamemodify(file, ":p:h").'/'.secfile
122								let secimportfiles += [secfile]
123							endfor
124						endif
125					endif
126				endfor
127				let cssfiles = styletable + secimportfiles
128				let classes = []
129				for file in cssfiles
130					if filereadable(file)
131						let stylesheet = readfile(file)
132						let stylefile = join(stylesheet, ' ')
133						let stylefile = substitute(stylefile, '+>\*[,', ' ', 'g')
134						if search_for == 'class'
135							let stylesheet = split(stylefile)
136							let classlines = filter(copy(stylesheet), "v:val =~ '\\([a-zA-Z0-9:]\\+\\)\\?\\.[a-zA-Z0-9_-]\\+'")
137						else
138							let stylesheet = split(stylefile, '[{}]')
139							" Get all lines which fit id syntax
140							let classlines = filter(copy(stylesheet), "v:val =~ '#[a-zA-Z0-9_-]\\+'")
141							" Filter out possible color definitions
142							call filter(classlines, "v:val !~ ':\\s*#[a-zA-Z0-9_-]\\+'")
143							" Filter out complex border definitions
144							call filter(classlines, "v:val !~ '\\(none\\|hidden\\|dotted\\|dashed\\|solid\\|double\\|groove\\|ridge\\|inset\\|outset\\)\\s*#[a-zA-Z0-9_-]\\+'")
145							let templines = join(classlines, ' ')
146							let stylelines = split(templines)
147							let classlines = filter(stylelines, "v:val =~ '#[a-zA-Z0-9_-]\\+'")
148
149						endif
150					endif
151					" We gathered classes definitions from all external files
152					let classes += classlines
153				endfor
154				if internal == 1
155					let classes += headclasslines
156				endif
157
158				if search_for == 'class'
159					let elements = {}
160					for element in classes
161						if element =~ '^\.'
162							let class = matchstr(element, '^\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
163							let class = substitute(class, ':.*', '', '')
164							if has_key(elements, 'common')
165								let elements['common'] .= ' '.class
166							else
167								let elements['common'] = class
168							endif
169						else
170							let class = matchstr(element, '[a-zA-Z1-6]*\.\zs[a-zA-Z][a-zA-Z0-9_-]*\ze')
171							let tagname = tolower(matchstr(element, '[a-zA-Z1-6]*\ze.'))
172							if tagname != ''
173								if has_key(elements, tagname)
174									let elements[tagname] .= ' '.class
175								else
176									let elements[tagname] = class
177								endif
178							endif
179						endif
180					endfor
181
182					if has_key(elements, tag) && has_key(elements, 'common')
183						let values = split(elements[tag]." ".elements['common'])
184					elseif has_key(elements, tag) && !has_key(elements, 'common')
185						let values = split(elements[tag])
186					elseif !has_key(elements, tag) && has_key(elements, 'common')
187						let values = split(elements['common'])
188					else
189						return []
190					endif
191
192				elseif search_for == 'id'
193					" Find used IDs
194					" 1. Catch whole file
195					let filelines = getline(1, line('$'))
196					" 2. Find lines with possible id
197					let used_id_lines = filter(filelines, 'v:val =~ "id\\s*=\\s*[\"''][a-zA-Z0-9_-]\\+"')
198					" 3a. Join all filtered lines
199					let id_string = join(used_id_lines, ' ')
200					" 3b. And split them to be sure each id is in separate item
201					let id_list = split(id_string, 'id\s*=\s*')
202					" 4. Extract id values
203					let used_id = map(id_list, 'matchstr(v:val, "[\"'']\\zs[a-zA-Z0-9_-]\\+\\ze")')
204					let joined_used_id = ','.join(used_id, ',').','
205
206					let allvalues = map(classes, 'matchstr(v:val, ".*#\\zs[a-zA-Z0-9_-]\\+")')
207
208					let values = []
209
210					for element in classes
211						if joined_used_id !~ ','.element.','
212							let values += [element]
213						endif
214
215					endfor
216
217				endif
218
219				" We need special version of sbase
220				let classbase = matchstr(a:base, ".*[\"']")
221				let classquote = matchstr(classbase, '.$')
222
223				let entered_class = matchstr(attr, ".*=\\s*[\"']\\zs.*")
224
225				for m in sort(values)
226					if m =~? '^'.entered_class
227						call add(res, classbase . m . classquote . ' ')
228					elseif m =~? entered_class
229						call add(res2, classbase . m . classquote . ' ')
230					endif
231				endfor
232
233				return res + res2
234
235			elseif a:base =~ "style\\s*=\\s*[\"'][^\"']*$"
236				return csscomplete#CompleteCSS(0, a:base)
237
238			endif
239			let stripbase = matchstr(a:base, ".*\\(on[a-z]*\\|style\\|class\\)\\s*=\\s*[\"']\\zs.*")
240			" Now we have a:base stripped from all chars up to style/class.
241			" It may fail with some strange style value combinations.
242			if stripbase !~ "[\"']"
243				return []
244			endif
245		endif
246		" If attr contains =\s*[\"'] we catched value of attribute
247		if attr =~ "=\s*[\"']"
248			" Let do attribute specific completion
249			let attrname = matchstr(attr, '.*\ze\s*=')
250			let entered_value = matchstr(attr, ".*=\\s*[\"']\\zs.*")
251			let values = []
252			if attrname == 'media'
253				let values = ["screen", "tty", "tv", "projection", "handheld", "print", "braille", "aural", "all"]
254			elseif attrname == 'xml:space'
255				let values = ["preserve"]
256			elseif attrname == 'shape'
257				if a:base =~ '^a\>'
258					let values = ["rect"]
259				else
260					let values = ["rect", "circle", "poly", "default"]
261				endif
262			elseif attrname == 'valuetype'
263				let values = ["data", "ref", "object"]
264			elseif attrname == 'method'
265				let values = ["get", "post"]
266			elseif attrname == 'dir'
267				let values = ["ltr", "rtl"]
268			elseif attrname == 'frame'
269				let values = ["void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border"]
270			elseif attrname == 'rules'
271				let values = ["none", "groups", "rows", "all"]
272			elseif attrname == 'align'
273				let values = ["left", "center", "right", "justify", "char"]
274			elseif attrname == 'valign'
275				let values = ["top", "middle", "bottom", "baseline"]
276			elseif attrname == 'scope'
277				let values = ["row", "col", "rowgroup", "colgroup"]
278			elseif attrname == 'href'
279				" Now we are looking for local anchors defined by name or id
280				if entered_value =~ '^#'
281					let file = join(getline(1, line('$')), ' ')
282					" Split it be sure there will be one id/name element in
283					" item, it will be also first word [a-zA-Z0-9_-] in element
284					let oneelement = split(file, "\\(meta \\)\\@<!\\(name\\|id\\)\\s*=\\s*[\"']")
285					for i in oneelement
286						let values += ['#'.matchstr(i, "^[a-zA-Z][a-zA-Z0-9%_-]*")]
287					endfor
288				endif
289			elseif attrname == 'type'
290				if a:base =~ '^input'
291					let values = ["text", "password", "checkbox", "radio", "submit", "reset", "file", "hidden", "image", "button"]
292				elseif a:base =~ '^button'
293					let values = ["button", "submit", "reset"]
294				elseif a:base =~ '^style'
295					let values = ["text/css"]
296				elseif a:base =~ '^script'
297					let values = ["text/javascript"]
298				endif
299			else
300				return []
301			endif
302
303			if len(values) == 0
304				return []
305			endif
306
307			" We need special version of sbase
308			let attrbase = matchstr(a:base, ".*[\"']")
309			let attrquote = matchstr(attrbase, '.$')
310
311			for m in values
312				" This if is needed to not offer all completions as-is
313				" alphabetically but sort them. Those beginning with entered
314				" part will be as first choices
315				if m =~ '^'.entered_value
316					call add(res, attrbase . m . attrquote.' ')
317				elseif m =~ entered_value
318					call add(res2, attrbase . m . attrquote.' ')
319				endif
320			endfor
321
322			return res + res2
323
324		endif
325		" Shorten a:base to not include last word
326		let sbase = matchstr(a:base, '.*\ze\s.*')
327		if tag =~ '^\(abbr\|acronym\|b\|bdo\|big\|caption\|cite\|code\|dd\|dfn\|div\|dl\|dt\|em\|fieldset\|h\d\|kbd\|li\|noscript\|ol\|p\|samp\|small\|span\|strong\|sub\|sup\|tt\|ul\|var\)$'
328			let attrs = coregroup
329		elseif tag == 'a'
330			let attrs = coregroup + focus + ["charset", "type", "name", "href", "hreflang", "rel", "rev", "shape", "coords"]
331		elseif tag == 'area'
332			let attrs = coregroup
333		elseif tag == 'base'
334			let attrs = ["href", "id"]
335		elseif tag == 'blockquote'
336			let attrs = coregroup + ["cite"]
337		elseif tag == 'body'
338			let attrs = coregroup + ["onload", "onunload"]
339		elseif tag == 'br'
340			let attrs = coreattrs
341		elseif tag == 'button'
342			let attrs = coreattrs + focus + ["name", "value", "type"]
343		elseif tag == '^\(col\|colgroup\)$'
344			let attrs = coreattrs + ["span", "width", "align", "char", "charoff", "valign"]
345		elseif tag =~ '^\(del\|ins\)$'
346			let attrs = coreattrs + ["cite", "datetime"]
347		elseif tag == 'form'
348			let attrs = coreattrs + ["action", "method=\"get\" ", "method=\"post\" ", "enctype", "onsubmit", "onreset", "accept", "accept-charset"]
349		elseif tag == 'head'
350			let attrs = i18n + ["id", "profile"]
351		elseif tag == 'html'
352			let attrs = i18n + ["id", "xmlns"]
353		elseif tag == 'img'
354			let attrs = coreattrs + ["src", "alt", "longdesc", "height", "width", "usemap", "ismap"]
355		elseif tag == 'input'
356			let attrs = coreattrs + focus + ["type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "onselect", "onchange", "accept"]
357		elseif tag == 'label'
358			let attrs = coreattrs + ["for", "accesskey", "onfocus", "onblur"]
359		elseif tag == 'legend'
360			let attrs = coreattrs + ["accesskey"]
361		elseif tag == 'link'
362			let attrs = coreattrs + ["charset", "href", "hreflang", "type", "rel", "rev", "media"]
363		elseif tag == 'map'
364			let attrs = i18n + events + ["id", "class", "style", "title", "name"]
365		elseif tag == 'meta'
366			let attrs = i18n + ["id", "http-equiv", "content", "scheme", "name"]
367		elseif tag == 'title'
368			let attrs = i18n + ["id"]
369		elseif tag == 'object'
370			let attrs = coreattrs + ["declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex"]
371		elseif tag == 'optgroup'
372			let attrs = coreattrs + ["disbled", "label"]
373		elseif tag == 'option'
374			let attrs = coreattrs + ["disbled", "selected", "value", "label"]
375		elseif tag == 'param'
376			let attrs = ["id", "name", "value", "valuetype", "type"]
377		elseif tag == 'pre'
378			let attrs = coreattrs + ["xml:space"]
379		elseif tag == 'q'
380			let attrs = coreattrs + ["cite"]
381		elseif tag == 'script'
382			let attrs = ["id", "charset", "type=\"text/javascript\"", "type", "src", "defer", "xml:space"]
383		elseif tag == 'select'
384			let attrs = coreattrs + ["name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange"]
385		elseif tag == 'style'
386			let attrs = coreattrs + ["id", "type=\"text/css\"", "type", "media", "title", "xml:space"]
387		elseif tag == 'table'
388			let attrs = coreattrs + ["summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding"]
389		elseif tag =~ '^\(thead\|tfoot\|tbody\|tr\)$'
390			let attrs = coreattrs + ["align", "char", "charoff", "valign"]
391		elseif tag == 'textarea'
392			let attrs = coreattrs + focus + ["name", "rows", "cols", "disabled", "readonly", "onselect", "onchange"]
393		elseif tag =~ '^\(th\|td\)$'
394			let attrs = coreattrs + ["abbr", "headers", "scope", "rowspan", "colspan", "align", "char", "charoff", "valign"]
395		else
396			return []
397		endif
398
399		for m in sort(attrs)
400			if m =~ '^'.attr
401				if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
402					call add(res, sbase.' '.m)
403				else
404					call add(res, sbase.' '.m.'="')
405				endif
406			elseif m =~ attr
407				if m =~ '^\(ismap\|defer\|declare\|nohref\|checked\|disabled\|selected\|readonly\)$' || m =~ '='
408					call add(res2, sbase.' '.m)
409				else
410					call add(res2, sbase.' '.m.'="')
411				endif
412			endif
413		endfor
414
415		return res + res2
416
417	endif
418	" Close tag
419	let b:unaryTagsStack = "base meta link hr br param img area input col"
420	if a:base =~ '^\/'
421		let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
422		return ["/".opentag.">"]
423	endif
424	" Deal with tag completion.
425	let opentag = htmlcomplete#GetLastOpenTag("b:unaryTagsStack")
426	" Clusters
427	let special = "br span bdo map object img"
428	let phrase =  "em strong dfn code q samp kbd var cite abbr acronym sub sup"
429	let inlineforms = "input select textarea label button"
430	let miscinline = "ins del script"
431	let inline = "a ".special." ".phrase." ".inlineforms." tt i b big small"
432	let misc = "noscript ".miscinline
433	let block = "p h1 h2 h3 h4 h5 h6 div ul ol dl pre hr blockquote address fieldset table"
434
435	if opentag == 'a'
436		let tags = split("tt i b big small ".special." ".phrase." ".inlineforms." ".miscinline)
437	elseif opentag =~ '^\(abbr\|acronym\|address\|b\|p\|h\d\|dt\|span\|bdo\|em\|strong\|dfn\|code\|samp\|kbd\|var\|cite\|q\|sub\|sup\|tt\|i\|big\|small\|label\|caption\)$'
438		let tags = split(inline." ".miscinline)
439	elseif opentag == 'pre'
440		let tags = split("a tt i b big small br span bdo map ".phrase." ".miscinline." ".inlineforms)
441	elseif opentag == 'html'
442		let tags = ["head", "body"]
443	elseif opentag == 'legend'
444		let tags = split(inline." ".miscinline)
445	elseif opentag == 'head'
446		let tags = ["title", "base", "scipt", "style", "meta", "link", "object"]
447	elseif opentag =~ '^\(noscript\|body\|blockquote\)$'
448		let tags = split("form ".block." ".misc)
449	elseif opentag =~ '^\(ul\|ol\)$'
450		let tags = ["li"]
451	elseif opentag == 'dl'
452		let tags = ["dt", "dd"]
453	elseif opentag =~ '^\(ins\|del\|th\|td\|dd\|div\|li\)$'
454		let tags = split("form ".block." ".inline." ".misc)
455	elseif opentag == 'object'
456		let tags = split("param form ".block." ".inline." ".misc)
457	elseif opentag == 'fieldset'
458		let tags = split("legend form ".block." ".inline." ".misc)
459	elseif opentag == 'map'
460		let tags = split("area form ".block." ".misc)
461	elseif opentag == 'form'
462		let tags = split(block." ".misc)
463	elseif opentag == 'select'
464		let tags = ["optgroup", "option"]
465	elseif opentag == 'optgroup'
466		let tags = ["option"]
467	elseif opentag == 'colgroup'
468		let tags = ["col"]
469	elseif opentag == '^\(textarea\|option\|script\|style\|title\)$'
470		let tags = ['empty']
471	elseif opentag == 'button'
472		let tags = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "ul", "ol", "dl", "table"]
473	elseif opentag =~ '^\(thead\|tfoot\|tbody\)$'
474		let tags = ["tr"]
475	elseif opentag == 'tr'
476		let tags = ["th", "td"]
477	elseif opentag == 'table'
478		let tags = ["caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr"]
479	else
480		return []
481	endif
482
483	for m in tags
484		if m =~ '^'.a:base
485			call add(res, m)
486		elseif m =~ a:base
487			call add(res2, m)
488		endif
489	endfor
490
491	return res + res2
492
493  endif
494endfunction
495
496" MM: This is greatly reduced closetag.vim used with kind permission of Steven
497"     Mueller
498"     Changes: strip all comments; delete error messages
499" Author: Steven Mueller <[email protected]>
500" Last Modified: Tue May 24 13:29:48 PDT 2005
501" Version: 0.9.1
502
503function! htmlcomplete#GetLastOpenTag(unaryTagsStack)
504	let linenum=line('.')
505	let lineend=col('.') - 1 " start: cursor position
506	let first=1              " flag for first line searched
507	let b:TagStack=''        " main stack of tags
508	let startInComment=s:InComment()
509
510	let tagpat='</\=\(\k\|[-:]\)\+\|/>'
511	while (linenum>0)
512		let line=getline(linenum)
513		if first
514			let line=strpart(line,0,lineend)
515		else
516			let lineend=strlen(line)
517		endif
518		let b:lineTagStack=''
519		let mpos=0
520		let b:TagCol=0
521		while (mpos > -1)
522			let mpos=matchend(line,tagpat)
523			if mpos > -1
524				let b:TagCol=b:TagCol+mpos
525				let tag=matchstr(line,tagpat)
526
527				if exists('b:closetag_disable_synID') || startInComment==s:InCommentAt(linenum, b:TagCol)
528					let b:TagLine=linenum
529					call s:Push(matchstr(tag,'[^<>]\+'),'b:lineTagStack')
530				endif
531				let lineend=lineend-mpos
532				let line=strpart(line,mpos,lineend)
533			endif
534		endwhile
535		while (!s:EmptystackP('b:lineTagStack'))
536			let tag=s:Pop('b:lineTagStack')
537			if match(tag, '^/') == 0		"found end tag
538				call s:Push(tag,'b:TagStack')
539			elseif s:EmptystackP('b:TagStack') && !s:Instack(tag, a:unaryTagsStack)	"found unclosed tag
540				return tag
541			else
542				let endtag=s:Peekstack('b:TagStack')
543				if endtag == '/'.tag || endtag == '/'
544					call s:Pop('b:TagStack')	"found a open/close tag pair
545				elseif !s:Instack(tag, a:unaryTagsStack) "we have a mismatch error
546					return ''
547				endif
548			endif
549		endwhile
550		let linenum=linenum-1 | let first=0
551	endwhile
552return ''
553endfunction
554
555function! s:InComment()
556	return synIDattr(synID(line('.'), col('.'), 0), 'name') =~ 'Comment'
557endfunction
558
559function! s:InCommentAt(line, col)
560	return synIDattr(synID(a:line, a:col, 0), 'name') =~ 'Comment'
561endfunction
562
563function! s:SetKeywords()
564	let g:IsKeywordBak=&iskeyword
565	let &iskeyword='33-255'
566endfunction
567
568function! s:RestoreKeywords()
569	let &iskeyword=g:IsKeywordBak
570endfunction
571
572function! s:Push(el, sname)
573	if !s:EmptystackP(a:sname)
574		exe 'let '.a:sname."=a:el.' '.".a:sname
575	else
576		exe 'let '.a:sname.'=a:el'
577	endif
578endfunction
579
580function! s:EmptystackP(sname)
581	exe 'let stack='.a:sname
582	if match(stack,'^ *$') == 0
583		return 1
584	else
585		return 0
586	endif
587endfunction
588
589function! s:Instack(el, sname)
590	exe 'let stack='.a:sname
591	call s:SetKeywords()
592	let m=match(stack, '\<'.a:el.'\>')
593	call s:RestoreKeywords()
594	if m < 0
595		return 0
596	else
597		return 1
598	endif
599endfunction
600
601function! s:Peekstack(sname)
602	call s:SetKeywords()
603	exe 'let stack='.a:sname
604	let top=matchstr(stack, '\<.\{-1,}\>')
605	call s:RestoreKeywords()
606	return top
607endfunction
608
609function! s:Pop(sname)
610	if s:EmptystackP(a:sname)
611		return ''
612	endif
613	exe 'let stack='.a:sname
614	call s:SetKeywords()
615	let loc=matchend(stack,'\<.\{-1,}\>')
616	exe 'let '.a:sname.'=strpart(stack, loc+1, strlen(stack))'
617	let top=strpart(stack, match(stack, '\<'), loc)
618	call s:RestoreKeywords()
619	return top
620endfunction
621
622function! s:Clearstack(sname)
623	exe 'let '.a:sname."=''"
624endfunction
625