【Ruby】Ruby大法第二天——協助產生Vim添加代碼頭的代碼。

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   檔案   

指令碼語言真是太強了。

我的目的是把我的預設代碼頭功能加到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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.