macros: Add TexTableBfEntries
[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 let gatename = substitute(expand("%:t"), "\\.", "_", "g") . '_' . strpart(system('pwgen -c 16 1'), 0, 16)
82 execute "normal! i#ifndef " . gatename
83 execute "normal! o#define " . gatename
84 endfunction
85
86
87 function AddIncludeGuards()
88 execute "normal! Go#endif"
89 execute "normal! gg"
90 call InsertIncludeGuardsWithoutEndif()
91 endfunction
92
93
94 function RunPandoc()
95 " If pandoc.css exists, use it
96 let cssopts = ""
97 if findfile("pandoc.css", ".") == "pandoc.css"
98 let cssopts = "-c pandoc.css"
99 endif
100
101 execute ":!pandoc " . cssopts . " --self-contained --toc '" . @% . "' -o '" . @% . "'.html"
102 endfunction
103
104
105 function RunMarkdownpy(prog)
106 execute ":!" . a:prog . " " . @% . " > " . @% . ".html"
107 endfunction
108
109
110 function RunAsciidoctor()
111 execute ":!asciidoctor " . @%
112 endfunction
113
114
115 function RunMarkdown()
116 if executable("pandoc")
117 call RunPandoc()
118 elseif executable("markdown2")
119 call RunMarkdownpy("markdown2")
120 elseif executable("markdown_py")
121 call RunMarkdownpy("markdown_py")
122 else
123 echo "No markdown implementation found."
124 endif
125 endfunction
126
127
128 function RunAsciidoc()
129 if executable("asciidoctor")
130 call RunAsciidoctor()
131 else
132 echo "No asciidoc implementation found."
133 endif
134 endfunction
135
136
137 function OnBattery()
138 if has('macunix')
139 return match(system('pmset -g batt'), "Now drawing from 'Battery Power'") != -1
140 elseif has('unix')
141 if filereadable('/sys/class/power_supply/AC/online')
142 return readfile('/sys/class/power_supply/AC/online') == ['0']
143 endif
144 endif
145 return 0
146 endfunction
147
148
149 if filereadable($HOME . '/.vim/macros-local.vim')
150 source ~/.vim/macros-local.vim
151 endif