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