首页 > 代码库 > 【Ruby】Ruby大法第二天——帮助生成Vim添加代码头的代码。

【Ruby】Ruby大法第二天——帮助生成Vim添加代码头的代码。

脚本语言真是太强了。

我的目的是把我的默认代码头功能加到Vim里面。

 1 /****************************************************************************** 2 *       COPYRIGHT NOTICE 3 *       Copyright (c) 2014 All rights reserved 4 *       ----Stay Hungry Stay Foolish---- 5 * 6 *       @author       : Shen 7 *       @name         : 8 *       @file         : G:\My Source Code\DefaultCode.cpp 9 *       @date         : 2014/06/14 02:4410 *       @algorithm    :11 ******************************************************************************/12 13 //#pragma GCC optimize ("O2")14 //#pragma comment(linker, "/STACK:1024000000,1024000000")15 16 #include <bits/stdc++.h>17 #include <cmath>18 #include <cstdio>19 #include <string>20 #include <cstring>21 #include <iomanip>22 #include <iostream>23 #include <algorithm>24 using namespace std;25 template<class T>inline bool updateMin(T& a, T b){ return a > b ? a = b, 1: 0; }26 template<class T>inline bool updateMax(T& a, T b){ return a < b ? a = b, 1: 0; }27 28 /*//STL29 #include <map>30 #include <vector>31 #include <list>32 #include <stack>33 #include <deque>34 #include <queue>35 */36 37 /*//Computational Geometry38 #include <complex>39 #define x real()40 #define y imag()41 typedef complex<double> point;42 */43 44 typedef long long int64;45 46 void solve()47 {48 49 }50 51 int main()52 {53 54     return 0;55 }
View Code

用Ruby10行就搞定了字符串的处理。简直爽爆了。

def load( path )    File.foreach(path) do |line|        line["\n"] = ""        str = "let l = l + 1 | call setline(l, \‘#{line}\‘)"        File.open("s.txt", "a") do |f|            f << "#{str}\n"        end    endendload("DefaultCode.cpp")

最后人工把头尾一加,搞定了。

 1 "F4 添加文件头 2 map <F4> :call TitleDet()<cr> 3 function AddTitle() 4 let l = 0 5 let l = l + 1 | call setline(l, ‘/******************************************************************************‘) 6 let l = l + 1 | call setline(l, ‘*       COPYRIGHT NOTICE‘) 7 let l = l + 1 | call setline(l, ‘*       Copyright (c) 2014 All rights reserved‘) 8 let l = l + 1 | call setline(l, ‘*       ----Stay Hungry Stay Foolish----‘) 9 let l = l + 1 | call setline(l, ‘*‘)10 let l = l + 1 | call setline(l, ‘*       @author       : Shen‘)11 let l = l + 1 | call setline(l, ‘*       @name         :‘)12 let l = l + 1 | call setline(l, ‘*       @file         : ‘.expand("%:p:h")."\\".expand("%:t"))13 let l = l + 1 | call setline(l, ‘*       @date         : ‘.strftime("%Y/%m/%d %H:%M"))14 let l = l + 1 | call setline(l, ‘*       @algorithm    :‘)15 let l = l + 1 | call setline(l, ‘******************************************************************************/‘)16 let l = l + 1 | call setline(l, ‘‘)17 let l = l + 1 | call setline(l, ‘//#pragma GCC optimize ("O2")‘)18 let l = l + 1 | call setline(l, ‘//#pragma comment(linker, "/STACK:1024000000,1024000000")‘)19 let l = l + 1 | call setline(l, ‘‘)20 let l = l + 1 | call setline(l, ‘#include <bits/stdc++.h>‘)21 let l = l + 1 | call setline(l, ‘#include <cmath>‘)22 let l = l + 1 | call setline(l, ‘#include <cstdio>‘)23 let l = l + 1 | call setline(l, ‘#include <string>‘)24 let l = l + 1 | call setline(l, ‘#include <cstring>‘)25 let l = l + 1 | call setline(l, ‘#include <iomanip>‘)26 let l = l + 1 | call setline(l, ‘#include <iostream>‘)27 let l = l + 1 | call setline(l, ‘#include <algorithm>‘)28 let l = l + 1 | call setline(l, ‘using namespace std;‘)29 let l = l + 1 | call setline(l, ‘template<class T>inline bool updateMin(T& a, T b){ return a > b ? a = b, 1: 0; }‘)30 let l = l + 1 | call setline(l, ‘template<class T>inline bool updateMax(T& a, T b){ return a < b ? a = b, 1: 0; }‘)31 let l = l + 1 | call setline(l, ‘‘)32 let l = l + 1 | call setline(l, ‘/*//STL‘)33 let l = l + 1 | call setline(l, ‘#include <map>‘)34 let l = l + 1 | call setline(l, ‘#include <vector>‘)35 let l = l + 1 | call setline(l, ‘#include <list>‘)36 let l = l + 1 | call setline(l, ‘#include <stack>‘)37 let l = l + 1 | call setline(l, ‘#include <deque>‘)38 let l = l + 1 | call setline(l, ‘#include <queue>‘)39 let l = l + 1 | call setline(l, ‘*/‘)40 let l = l + 1 | call setline(l, ‘‘)41 let l = l + 1 | call setline(l, ‘/*//Computational Geometry‘)42 let l = l + 1 | call setline(l, ‘#include <complex>‘)43 let l = l + 1 | call setline(l, ‘#define x real()‘)44 let l = l + 1 | call setline(l, ‘#define y imag()‘)45 let l = l + 1 | call setline(l, ‘typedef complex<double> point;‘)46 let l = l + 1 | call setline(l, ‘*/‘)47 let l = l + 1 | call setline(l, ‘‘)48 let l = l + 1 | call setline(l, ‘typedef long long int64;‘)49 let l = l + 1 | call setline(l, ‘‘)50 let l = l + 1 | call setline(l, ‘void solve()‘)51 let l = l + 1 | call setline(l, ‘{‘)52 let l = l + 1 | call setline(l, ‘    ‘)53 let l = l + 1 | call setline(l, ‘}‘)54 let l = l + 1 | call setline(l, ‘‘)55 let l = l + 1 | call setline(l, ‘int main()‘)56 let l = l + 1 | call setline(l, ‘{‘)57 let l = l + 1 | call setline(l, ‘    ‘)58 let l = l + 1 | call setline(l, ‘    return 0;‘)59 let l = l + 1 | call setline(l, ‘}‘)60 endfunction61 62 "更新最近修改时间和文件名63 function UpdateTitle()64     call setline(8, ‘*       @file         : ‘.expand("%:p:h")."\\".expand("%:t"))65     call setline(9, ‘*       @date         : ‘.strftime("%Y/%m/%d %H:%M"))66 endfunction67 68 "判断前10行代码里面,是否有COPYRIGHT NOTICE这个单词,69 "如果没有的话,代表没有添加过作者信息,需要新添加;70 "如果有的话,那么只需要更新即可71 function TitleDet()72     let n = 273     "默认为添加74         let line = getline(n)75         let str = ‘^*       COPYRIGHT NOTICE$‘76         if line =~ str77             call UpdateTitle()78             return79         endif80     call AddTitle()81 endfunction
View Code