d74406055ece5b04f6c1184bcbfe2f7c9340adfd
[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 endfunction
116
117
118 function RunMarkdown()
119 if executable("pandoc")
120 call RunPandoc()
121 elseif executable("markdown2")
122 call RunMarkdownpy("markdown2")
123 elseif executable("markdown_py")
124 call RunMarkdownpy("markdown_py")
125 else
126 echo "No markdown implementation found."
127 endif
128 endfunction
129
130
131 function RunAsciidoc()
132 if executable("asciidoctor")
133 call RunAsciidoctor()
134 else
135 echo "No asciidoc implementation found."
136 endif
137 endfunction
138
139
140 function OnBattery()
141 if has('macunix')
142 return match(system('pmset -g batt'), "Now drawing from 'Battery Power'") != -1
143 elseif has('unix')
144 if filereadable('/sys/class/power_supply/AC/online')
145 return readfile('/sys/class/power_supply/AC/online') == ['0']
146 endif
147 endif
148 return 0
149 endfunction
150
151
152 if filereadable($HOME . '/.vim/macros-local.vim')
153 source ~/.vim/macros-local.vim
154 endif