自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

經(jīng)驗(yàn)總結(jié):實(shí)用的Emacs配置文件搜羅

開(kāi)發(fā) 項(xiàng)目管理
以下的Emacs配置文件是我多年積累起來(lái)的,它們?cè)谖覛v次整理配置文件(.emacs)的過(guò)程中幸存了下來(lái),經(jīng)過(guò)了時(shí)間考驗(yàn),所以現(xiàn)在我決定發(fā)出 來(lái)和大家分享一下。雖然某些功能很有可能已經(jīng)有更好的實(shí)現(xiàn)方法了,但是這些例子對(duì)讀者學(xué)習(xí)emacs lisp還是會(huì)有幫助的。

以下的Emacs配置文件是我多年積累起來(lái)的,它們?cè)谖覛v次整理配置文件(.emacs)的過(guò)程中幸存了下來(lái),經(jīng)過(guò)了時(shí)間考驗(yàn),所以現(xiàn)在我決定發(fā)出 來(lái)和大家分享一下。雖然某些功能很有可能已經(jīng)有更好的實(shí)現(xiàn)方法了,但是這些例子對(duì)讀者學(xué)習(xí)emacs lisp還是會(huì)有幫助的。

在一些文本的末尾添加遞增的數(shù)字

inc-num-region把一段文本中重復(fù)出現(xiàn)的數(shù)字替換成遞增的數(shù)字

  1. (defun inc-num-region (p m) 
  2.   "Increments the numbers in a given region" 
  3.   (interactive "r") 
  4.   (save-restriction 
  5.     (save-excursion 
  6.       (narrow-to-region p m)    
  7.       (goto-char (point-min))   
  8.       (forward-line) 
  9.       (let ((counter 1)) 
  10.         (while (not (eq (point) 
  11.                         (point-max))) 
  12.           (goto-char (point-at-eol)) 
  13.           (search-backward-regexp "[0-9]+" (point-at-bol) t) 
  14.           (let* ((this-num (string-to-number (match-string 0))) 
  15.                  (new-num-str (number-to-string (+ this-num 
  16.                                                    counter)))) 
  17.             (replace-match new-num-str) 
  18.             (incf counter) 
  19.             (forward-line))))))) 

比如在emacs選中如下的文本區(qū)域

  1. 1foo 
  2. 1foo 
  3. 1foo 
  4. 1foo 

執(zhí)行該函數(shù),那么上述文本在緩沖區(qū)中變成

  1. 1foo 
  2. 2foo 
  3. 3foo 
  4. 4foo 

再比如選中如下的文本區(qū)域

  1. foo3 
  2.  foo3 
  3.  foo3 
  4.  foo3 

執(zhí)行給函數(shù),得到

  1. foo3 
  2. foo4 
  3. foo5 
  4. foo6 

給代碼做筆記

在我們公司使用reviewboard之前,代碼審查都是面對(duì)面進(jìn)行的。我曾經(jīng)使用下面這個(gè)函數(shù)來(lái)幫助記錄意見(jiàn)所對(duì)應(yīng)的源文件和行號(hào)。

  1. defun add-code-review-note () 
  2.    "Add note for current file and line number" 
  3.    (interactive) 
  4.    (let ((file-name (buffer-file-name)) 
  5.          (file-line (line-number-at-pos))) 
  6.      (switch-to-buffer-other-window (get-buffer-create "NOTES")) 
  7.      (goto-char (point-min)) 
  8.      (when (not (search-forward "-*- mode:compilation-shell-minor" 
  9.                                 nil t)) 
  10.        (compilation-shell-minor-mode 1) 
  11.        (insert "-*- mode:compilation-shell-minor -*-\n\n")) 
  12.      (goto-char (point-max)) 
  13.      (if (/= (current-column) 0) 
  14.          (newline)) 
  15.      (insert file-name ":" (number-to-string file-line) ": "))) 

使用方法是,光標(biāo)停在源代碼的需要做批注的位置,然后執(zhí)行該函數(shù),emacs會(huì)創(chuàng)建一個(gè)新的叫做NOTES的緩沖區(qū),其中記錄源代碼的路徑和光標(biāo)所在的行 號(hào),用戶在接下來(lái)的區(qū)域中輸入筆記。這個(gè)函數(shù)的好處是,該新建的buffer的工作模式是compilation-shell-minor-mode。所 以可以直接點(diǎn)擊其路徑和行號(hào),就可以直接打源文件跳到相應(yīng)的行上去。比如

  1. #include 
  2.   
  3.  int main() 
  4.  { 
  5.    std::cout << "Hello Word!" << std::endl;  //光標(biāo)停在這里 
  6.    return 0; 
  7.  } 

執(zhí)行該函數(shù),在新buffer中得到如下內(nèi)容,在compilation-shell-minor-mode模式下,筆記前面的內(nèi)容將呈現(xiàn)出一個(gè)鏈接,可以點(diǎn)擊直接打開(kāi)main.cpp

  1. /home/iamxuxiao/main.cpp:5: miss spelling "word" 

在我的.emacs中,我把這個(gè)函數(shù)和C-c、r做了綁定

自動(dòng)給C代碼頭文件的首位添加ifndef和endif

get-include-guard函數(shù)在我們要編輯一個(gè)新頭文件時(shí),自動(dòng)給文件添加上預(yù)處理指示符:ifndef和endif

  1. defun get-include-guard () 
  2.    "Return a string suitable for use in a C/C++ include guard" 
  3.    (let* ((fname (buffer-file-name (current-buffer))) 
  4.           (fbasename (replace-regexp-in-string ".*/" "" fname)) 
  5.           (inc-guard-base (replace-regexp-in-string "[.-]" 
  6.                                                     "_" 
  7.                                                     fbasename))) 
  8.      (concat (upcase inc-guard-base) "_"))) 
  9.   
  10.  (add-hook 'find-file-not-found-hooks 
  11.            '(lambda () 
  12.               (let ((file-name (buffer-file-name (current-buffer)))) 
  13.                 (when (string".h" (substring file-name -2)) 
  14.                   (let ((include-guard (get-include-guard))) 
  15.                     (insert "#ifndef " include-guard) 
  16.                     (newline) 
  17.                     (insert "#define " include-guard) 
  18.                     (newline 4) 
  19.                     (insert "#endif") 
  20.                     (newline) 
  21.                     (previous-line 3) 
  22.                     (set-buffer-modified-p nil)))))) 

如果我們?cè)趀macs中要新建一個(gè)文件foo.h(C-x,C-f foo.h),emacs新創(chuàng)建的foo.h緩沖區(qū)中看上去將是這樣的

  1. #ifndef FOO_H_ 
  2.  #define FOO_H_ 
  3.   
  4.  #endif 

在foo.cpp和foo.h之間自動(dòng)的切換

如果一個(gè)文件夾中同時(shí)含有foo.h和foo.cpp兩個(gè)文件的話,下面的函數(shù)幫助你在這兩個(gè)文件之間切換

  1. (defun next-file-with-basename () 
  2.  "Cycles between files with the same basename as the given file. 
  3.   Usefull for cycling between header .h/.cpp/.hpp files etc." 
  4.  (interactive) 
  5.  (let* ((buf-file-name (replace-regexp-in-string 
  6.                         "^.*/" "" 
  7.                         (buffer-file-name))) 
  8.         (current-dir (replace-regexp-in-string 
  9.                       "[a-zA-Z0-9._-]+$" "" 
  10.                       (buffer-file-name))) 
  11.         (no-basename (equal ?. (aref buf-file-name 0))) 
  12.         (has-extension (find ?. buf-file-name))) 
  13.    ;; If the file is a .dot-file or it doesn't have an 
  14.    ;; extension, then there's nothing to do here. 
  15.    (unless (or no-basename (not has-extension)) 
  16.      (let* ((basename (replace-regexp-in-string 
  17.                        "\\..*" "" 
  18.                        buf-file-name)) 
  19.             (files-with-basename (directory-files 
  20.                                   current-dir f 
  21.                                   (concat "^" basename "\\.")))) 
  22.        ;; If there's only 1 file with this basename, nothing to 
  23.        ;; do 
  24.        (unless (= (length files-with-basename) 1) 
  25.          ;; By making the list circular, we're guaranteed that 
  26.          ;; there will always be a next list element (ie. no 
  27.          ;; need for special case when file is at the end of 
  28.          ;; the list). 
  29.          (setf (cdr (last files-with-basename)) 
  30.                files-with-basename) 
  31.          (find-file (cadr (member (buffer-file-name) 
  32.                                   files-with-basename)))))))) 

在我的.emacs中,我把這個(gè)函數(shù)和C-c,n做了綁定

注:Reddit網(wǎng)友提出ff-find-other-file實(shí)現(xiàn)了非常類似的功能

c-macro模板

我們?cè)趯?xiě)C++代碼的時(shí)候,經(jīng)常要鍵入一些重復(fù)的操作,比如歷遍容器,try catch等等。而這些代碼的特點(diǎn),可以歸結(jié)成一個(gè)不變的模板+幾個(gè)變化參數(shù),下面的emacs函數(shù)自動(dòng)幫你擴(kuò)展這個(gè)模板,打印代碼。

我們先描述該函數(shù)的效果,在C++代碼中插入如下待擴(kuò)展的句子

  1. (doit std::vector myContainer) 

然后在該行的末尾執(zhí)行我們的函數(shù),該行被自動(dòng)替換成如下的C++代碼

  1. for (std::vector::iterator it = myContainer.begin(); 
  2.      it != myContainer.end(); 
  3.      ++it) 
  4.    // 光標(biāo)將停在這里 等待具體的編輯  

該c-macro還可以接受變長(zhǎng)參數(shù),比如下面的模板接受兩個(gè)參數(shù)

  1. (doit std::vector myIt myContainer) 

生成的代碼如下:

  1. for (std::vector::iterator myIt = myContainer.begin(); 
  2.       myIt != myContainer.end(); 
  3.       ++myIt) 
  4.  { 
  5.     // 光標(biāo)將停在這里 等待具體的編輯 
  6.  } 

下面的macro將幫助用戶自己打印try catch block

  1. (api-fn) 

擴(kuò)展之后將變成

  1. try 
  2.     // 光標(biāo)將停在這里 等待具體的編輯 
  3. catch(const std::exception& e) 
  4.    TRACE("Unhandled exception in function %s: %s\n", 
  5.          __func__, e.what()); 
  6.    return -1; 

下面的j-newline-and-indent是以上功能的入口函數(shù),其將尋找光標(biāo)前是否出現(xiàn)已定義的c-macro.在上面的例子中就是doit和api-fn。
如果出現(xiàn)了macro就做擴(kuò)展,如果沒(méi)有出現(xiàn),j-newline-and-indent等于內(nèi)置的newline-and-indent函數(shù):加入新行,并且indent

  1. (defun j-newline-and-indent () 
  2.    "Same as \"newline-and-indent\" except it also expands 
  3.     c-macros if it sees one." 
  4.    (interactive) 
  5.    (if (and (equal (char-before) ?\)) 
  6.             (macro-function (car (preceding-sexp)))) 
  7.        ;; This is a c-macro 
  8.        (expand-c-macro-in-place) 
  9.      (newline-and-indent))) 
  10.   
  11.  (defun macro-function (name) 
  12.    "Given a name, returns the c-macro-name symbol if it 
  13.     exists as a function" 
  14.    (let ((macro-sym (intern (concat "c-macro-" 
  15.                                     (symbol-name name))))) 
  16.      (if (fboundp macro-sym) 
  17.          macro-sym 
  18.        nil))) 
  19.   
  20.  (defun expand-c-macro-in-place () 
  21.    "Given that point is at the end of a c-macro, expands 
  22.     it in-place" 
  23.    (let* ((sexp (preceding-sexp)) 
  24.           (macro-name (car sexp)) 
  25.           (replacement-text (apply (macro-function macro-name) 
  26.                                    (cdr sexp))) 
  27.           (jump-to (string-match "!!!BODY!!!;" replacement-text))) 
  28.      ;; Delete macro invocation 
  29.      (backward-list) 
  30.      (let ((start-del (point))) 
  31.        (forward-list) 
  32.        (kill-region start-del (point)) 
  33.   
  34.       ;; Insert macro expansion and indent appropriately 
  35.       (insert replacement-text) 
  36.       (indent-region start-del (point)) 
  37.       (when jump-to 
  38.         (search-backward "!!!BODY!!!;") 
  39.         (kill-line)))) 
  40.   (c-indent-command)) 
  41.  
  42. 下面是自定義的兩個(gè)模板c-macro,讀者可以根據(jù)需要定義自己的macro 
  43. 10 
  44. 11 
  45. 12 
  46. 13 
  47. 14 
  48. 15 
  49. 16 
  50. 17 
  51. 18 
  52. 19 
  53. 20 
  54. 21 
  55. 22 
  56. 23 
  57. 24 
  58. 25 
  59. 26 
  60. 27 
  61. 28 
  62. 29 
  63.      
  64. (defun c-macro-doit (container-type arg1 &optional arg2) 
  65.    "Emits code for iterating over an stl (or stl-like) structure" 
  66.    (let ((iterator-name  (if arg2 arg1 "it")) 
  67.          (container-name (if arg2 arg2 arg1))) 
  68.      (format (concat "for (%s::iterator %s = %s.begin();\n" 
  69.                      "     %s != %s.end();\n" 
  70.                      "     ++%s)\n" 
  71.                      "{\n" 
  72.                      "   !!!BODY!!!;\n" 
  73.                      "}\n") 
  74.              container-type 
  75.              iterator-name 
  76.              container-name 
  77.              iterator-name 
  78.              container-name 
  79.              iterator-name))) 
  80.   
  81.  (defun c-macro-api-fn () 
  82.    "Emits code for wrapping an api function in a try/catch block" 
  83.    (concat "try\n" 
  84.            "{\n" 
  85.            "   !!!BODY!!!;\n" 
  86.            "}\n" 
  87.            "catch(const std::exception& e)\n" 
  88.            "{\n" 
  89.            "   TRACE(\"Unhandled exception in function %s: %s\\n\",\n" 
  90.            "         __func__, e.what());\n" 
  91.            "   return -1;\n" 
  92.            "}\n")) 

原文鏈接:http://blog.jobbole.com/47027/

責(zé)任編輯:陳四芳 來(lái)源: 伯樂(lè)在線
相關(guān)推薦

2009-10-15 09:27:00

2010-01-19 18:52:08

VB.NET處理數(shù)據(jù)行

2010-07-13 16:07:18

Perl

2009-08-17 14:45:18

VMware虛擬機(jī)實(shí)用

2011-07-21 13:40:17

java

2009-09-16 17:13:54

學(xué)習(xí)Linq

2009-11-16 10:57:51

PHP上傳文件代碼

2009-08-19 09:24:43

AJAX引擎經(jīng)驗(yàn)總結(jié)

2009-09-29 16:32:11

OJB Hiberna

2010-03-25 13:42:14

云計(jì)算

2010-03-23 11:39:49

云計(jì)算

2010-05-19 17:24:55

MySQL編碼

2010-04-21 14:53:46

Oracle游標(biāo)

2009-12-15 17:10:26

路由器配置

2010-04-28 10:45:24

Oracle10g

2010-01-18 16:49:36

VB.NET基本數(shù)據(jù)類

2009-09-16 17:44:54

LINQ to SQL

2009-09-08 10:57:55

LINQ查詢操作

2011-07-08 13:15:52

JSP

2009-09-11 13:29:31

LINQ查詢操作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)