vim: refactor PrependCppNamespaceToIdent
authorStefan Huber <shuber@sthu.org>
Sat, 12 Oct 2013 17:45:27 +0000 (19:45 +0200)
committerStefan Huber <shuber@sthu.org>
Sat, 12 Oct 2013 17:45:27 +0000 (19:45 +0200)
dotfiles/vim/.vimrc

index edee0137ebe7313b8edba881630119e341b4b6e4..7a05e1d5250798c9e3d6c6457632be1c28112158 100644 (file)
@@ -31,9 +31,9 @@ set tabstop=4
 set shiftwidth=4
 
 if version >= 703
-       set spelllang=de_at,en
-       set tabpagemax=20
-       set colorcolumn=+1
+    set spelllang=de_at,en
+    set tabpagemax=20
+    set colorcolumn=+1
 endif
 
 " Pathogen runtime path manipulation
@@ -60,11 +60,11 @@ colorscheme shuber-wombat
 " Use the below highlight group when displaying bad whitespace is desired.
 highlight BadWhitespace ctermbg=red guibg=red
 " Make trailing whitespace be flagged as bad.
-au Filetype python,tex,c,cpp,cs,objc,java syn match BadWhitespace /\s\+$/ containedin=ALL
+au Filetype python,tex,c,cpp,cs,objc,java,vim syn match BadWhitespace /\s\+$/ containedin=ALL
 
 
 if exists(":DetectIndent")
-       au BufReadPost * :DetectIndent
+    au BufReadPost * :DetectIndent
 endif
 
 
@@ -77,7 +77,7 @@ let g:syntastic_mode_map = { 'mode' : 'active', 'active_filetypes' : [], 'passiv
 
 "Open current file with a specific program
 function OpenIn(prog)
-       execute ":!" . a:prog . " % &"
+    execute ":!" . a:prog . " % &"
 endfunction
 
 
@@ -98,8 +98,8 @@ imap <c-s> <c-o><c-s>
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
 "let fortran_free_source=1
-au BufNewFile *.f90            set fortran_free_source=1
-au Filetype fortran            set cindent cst csto=0
+au BufNewFile *.f90     set fortran_free_source=1
+au Filetype fortran     set cindent cst csto=0
 
 
 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@@ -116,46 +116,52 @@ au Filetype java map <F7> :!ant -f ../build.xml <CR>
 au Filetype c,cpp,cs,obj set makeprg=make
 
 
-"Adds STL prefix 'std::' to 'id'
-function AddNamespaceDeclTo(ns, id)
-       echo "Add '" . a:ns . "' to " . a:id
-       "Line starts with a:id
-       execute ":%s!^\\(\\s*\\)" . a:id . "\\>!\\1" . a:ns . "::" . a:id . "!e"
-       "Line beginning, white space, not # or / (preprocessor or comment)
-       execute ":%s!^\\(\\s*[^#/].*[^:]\\)\\<" . a:id . "\\>!\\1" . a:ns . "::" . a:id . "!ge"
+"Prepend the namespace to an identifier, e.g. 'std::' before 'map' excluding
+"those in C/C++ comments.
+function PrependCppNamespaceToIdent(ns, id)
+
+    "     To match                   Not to match
+    "
+    "|id                        |// id
+    "|    id                    |// /* */ id
+    "|/* */ /* */ id            |/* */ // id
+    "|id /* */                  |/* id
+    "|*/ id                     | * id
+    "                           |/* id */
+    "                           |::id
+    "                           |/**/ ::id
+    "
+    " In order to not match '* id' it is assumed that multi-line comment lines
+    " in the middle begin with a star.
+
+    " If // and /* and ^* and :: is not prepend:
+    execute ':%s_\(\(\/\/\|\/\*\|^\s*\*[^/]\).*\|::\)\@<!\<' . a:id . '\>_' . a:ns . '::' . a:id . '_ge'
+    " If // and :: is not prepend, but */ is, and no further /* or // are found
+    execute ':%s_\(\/\/.*\)\@<!\*\/\(\/[^\/\*]\|[^\/]\)*\zs\(::\)\@<!\<' . a:id . '\>_' . a:ns . '::' . a:id . '_ge'
+
 endfunction
 
-"Adds STL prefix 'std::' to several identifiers
-function AddSTLNamespaceDecl()
-       call AddNamespaceDeclTo("std","cin")
-       call AddNamespaceDeclTo("std","cout")
-       call AddNamespaceDeclTo("std","cerr")
-
-       call AddNamespaceDeclTo("std","swap")
-       call AddNamespaceDeclTo("std","sort")
-       call AddNamespaceDeclTo("std","max")
-       call AddNamespaceDeclTo("std","min")
-
-       call AddNamespaceDeclTo("std","deque")
-       call AddNamespaceDeclTo("std","endl")
-       call AddNamespaceDeclTo("std","list")
-       call AddNamespaceDeclTo("std","map")
-       call AddNamespaceDeclTo("std","multimap")
-       call AddNamespaceDeclTo("std","ostream")
-       call AddNamespaceDeclTo("std","pair")
-       call AddNamespaceDeclTo("std","priority_queue")
-       call AddNamespaceDeclTo("std","set")
-       call AddNamespaceDeclTo("std","queue")
-       call AddNamespaceDeclTo("std","stack")
-       call AddNamespaceDeclTo("std","string")
-       call AddNamespaceDeclTo("std","vector")
+"Prepend STL namespace 'std::' to several identifiers
+function PrependSTLNamespace()
+    let id = []
+    let id = id +['cin', 'cout', 'cerr', 'endl']
+    let id = id +['swap', 'sort', 'max', 'min']
+    let id = id +['vector', 'deque', 'list', 'map', 'multimap', 'set']
+    let id = id +['queue', 'stack', 'priority_queue']
+    let id = id +['ostream', 'istream']
+    let id = id +['pair', 'string']
+    let id = ['map']
+
+    for i in id
+        call PrependCppNamespaceToIdent("std", i)
+    endfor
 endfunction
 
 function EscapeHexToChar()
-       echo 'Call "x/Nxb addr" in GDB to print N bytes at addr' 
-       execute '%s/^.*://'
-       execute '%s/\s*0x\(\x\x\)/\\x\1/g'
-       execute '%s/^\(.*\)$/"\1"/'
+    echo 'Call "x/Nxb addr" in GDB to print N bytes at addr'
+    execute '%s/^.*://'
+    execute '%s/\s*0x\(\x\x\)/\\x\1/g'
+    execute '%s/^\(.*\)$/"\1"/'
 endfunction
 
 
@@ -172,7 +178,7 @@ au Filetype mail set expandtab
 "a and w reformat the paragraph automatically and a new paragraph
 "is indicated by lines not ending with white-space
 if version>=700
-       au Filetype mail,text set fo+=n spell
+    au Filetype mail,text set fo+=n spell
 endif
 
 "Scissor line
@@ -186,7 +192,7 @@ au Filetype mail syn match Statement /^\s*-*\s*8<\s*-*\s*$/
 
 "au Filetype php set fo=tcqn
 if version >= 700
-       au Filetype php set spell
+    au Filetype php set spell
 endif
 
 au Filetype php set textwidth=80
@@ -202,18 +208,18 @@ au Filetype tex set textwidth=80
 
 "Remove Umlaute
 function TexTransUmlaute()
-       execute ':%s/ü/\\\"u/ge'
-       execute ':%s/Ü/\\\"U/&'
-       execute ':%s/ö/\\\"o/&'
-       execute ':%s/Ö/\\\"O/&'
-       execute ':%s/ä/\\\"a/&'
-       execute ':%s/Ä/\\\"A/&'
-       execute ':%s/ß/\\\"s/&'
-       execute ':%s/²/\^2/&'
+    execute ':%s/ü/\\\"u/ge'
+    execute ':%s/Ü/\\\"U/&'
+    execute ':%s/ö/\\\"o/&'
+    execute ':%s/Ö/\\\"O/&'
+    execute ':%s/ä/\\\"a/&'
+    execute ':%s/Ä/\\\"A/&'
+    execute ':%s/ß/\\\"s/&'
+    execute ':%s/²/\^2/&'
 endfunction
 
 function FindWordRepeatings()
-       execute '/\(\<\S\+\>\)\s\+\1\>'
+    execute '/\(\<\S\+\>\)\s\+\1\>'
 "        let pos = search('\(\<\S\+\>\)\s\+\1\>', "cw")
 "        call cursor(pos)
 endfunction
@@ -225,7 +231,7 @@ let g:LatexBox_latexmk_options="-pvc"
 au Filetype tex set smartindent
 
 if version >= 700
-       au Filetype tex set spell
+    au Filetype tex set spell
 endif
 
 
@@ -272,7 +278,7 @@ let g:ycm_key_list_select_completion = ['<Down>']
 
 
 if filereadable($HOME . "/.vimrc-local")
-       source ~/.vimrc-local
+    source ~/.vimrc-local
 endif