Speedup and customize (tex) folding
[vimconf.git] / macros.vim
1 " Tranlate umlauts to tex-syntax umlauts
2 function TexTransUmlaute()
3 execute ':%s/ü/\\\"u/ge'
4 execute ':%s/Ü/\\\"U/&'
5 execute ':%s/ö/\\\"o/&'
6 execute ':%s/Ö/\\\"O/&'
7 execute ':%s/ä/\\\"a/&'
8 execute ':%s/Ä/\\\"A/&'
9 execute ':%s/ß/\\\"s/&'
10 execute ':%s/²/\^2/&'
11 endfunction
12
13
14 "Open current file with a specific program
15 function OpenIn(prog)
16 silent execute ":!" . a:prog . " % &"
17 endfunction
18
19
20 "Prepend the namespace to an identifier, e.g. 'std::' before 'map' excluding
21 "those in C/C++ comments.
22 function PrependCppNamespaceToIdent(ns, id)
23
24 " To match Not to match
25 "
26 "|id |// id
27 "| id |// /* */ id
28 "|/* */ /* */ id |/* */ // id
29 "|id /* */ |/* id
30 "|*/ id | * id
31 " |/* id */
32 " |::id
33 " |/**/ ::id
34 " |#include <id>
35 "
36 " In order to not match '* id' it is assumed that multi-line comment lines
37 " in the middle begin with a star.
38
39 " If #include and // and /* and ^* and :: is not prepend:
40 execute ':%s@\(\(#include\|\/\/\|\/\*\|^\s*\*[^/]\).*\|::\)\@<!\<' . a:id . '\>@' . a:ns . '::' . a:id . '@ge'
41 " If #include and // and :: is not prepend, but */ is, and no further /* or // are found
42 execute ':%s@\(\(#include\|\/\/\).*\)\@<!\*\/\(\/[^\/\*]\|[^\/]\)*\zs\(::\)\@<!\<' . a:id . '\>@' . a:ns . '::' . a:id . '@ge'
43
44 endfunction
45
46
47 "Prepend STL namespace 'std::' to several identifiers
48 function PrependSTLNamespace()
49 " This list of identifier is not complete, but adding all of them is too
50 " much. We rather like to add identifieres that are 'typical' for C++.
51 " Others, like 'move' are likely to not be C++ specific. In this cases the
52 " user is asked to call PrependCppNamespaceToIdent by hand.
53 let id = []
54 let id = id +['cin', 'cout', 'cerr', 'endl']
55 let id = id +['swap', 'sort', 'max', 'min']
56 let id = id +['vector', 'deque', 'list', 'map', 'multimap', 'set']
57 let id = id +['queue', 'stack', 'priority_queue']
58 let id = id +['ostream', 'istream', 'sstream']
59 let id = id +['pair', 'string']
60
61 for i in id
62 call PrependCppNamespaceToIdent("std", i)
63 endfor
64 endfunction
65
66
67 function EscapeHexToChar()
68 echo 'Call "x/Nxb addr" in GDB to print N bytes at addr'
69 execute '%s/^.*://'
70 execute '%s/\s*0x\(\x\x\)/\\x\1/g'
71 execute '%s/^\(.*\)$/"\1"/'
72 endfunction
73
74
75 function InsertIncludeGuardsWithoutEndif()
76 let gatename = substitute(expand("%:t"), "\\.", "_", "g") . '_' . strpart(system('pwgen -c 16 1'), 0, 16)
77 execute "normal! i#ifndef " . gatename
78 execute "normal! o#define " . gatename
79 endfunction
80
81
82 function AddIncludeGuards()
83 execute "normal! Go#endif"
84 execute "normal! gg"
85 call InsertIncludeGuardsWithoutEndif()
86 endfunction
87
88
89 function RunPandoc()
90 " If pandoc.css exists, use it
91 let cssopts = ""
92 if findfile("pandoc.css", ".") == "pandoc.css"
93 let cssopts = "-c pandoc.css"
94 endif
95
96 execute ":!pandoc " . cssopts . " --self-contained --toc '" . @% . "' -o '" . @% . "'.html"
97 endfunction
98
99
100 function RunMarkdownpy(prog)
101 execute ":!" . a:prog . " " . @% . " > " . @% . ".html"
102 endfunction
103
104
105 function RunAsciidoctor()
106 execute ":!asciidoctor " . @%
107 endfunction
108
109
110 function RunMarkdown()
111 if executable("pandoc")
112 call RunPandoc()
113 elseif executable("markdown2")
114 call RunMarkdownpy("markdown2")
115 elseif executable("markdown_py")
116 call RunMarkdownpy("markdown_py")
117 else
118 echo "No markdown implementation found."
119 endif
120 endfunction
121
122
123 function RunAsciidoc()
124 if executable("asciidoctor")
125 call RunAsciidoctor()
126 else
127 echo "No asciidoc implementation found."
128 endif
129 endfunction
130
131
132 function OnBattery()
133 if has('macunix')
134 return match(system('pmset -g batt'), "Now drawing from 'Battery Power'") != -1
135 elseif has('unix')
136 if filereadable('/sys/class/power_supply/AC/online')
137 return readfile('/sys/class/power_supply/AC/online') == ['0']
138 endif
139 endif
140 return 0
141 endfunction
142
143
144 if filereadable($HOME . '/.vim/macros-local.vim')
145 source ~/.vim/macros-local.vim
146 endif