成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

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

inc-num-region把一段文本中重復出現(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之前,代碼審查都是面對面進行的。我曾經(jīng)使用下面這個函數(shù)來幫助記錄意見所對應的源文件和行號。

  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) ": "))) 

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

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

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

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

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

自動給C代碼頭文件的首位添加ifndef和endif

get-include-guard函數(shù)在我們要編輯一個新頭文件時,自動給文件添加上預處理指示符: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)))))) 

如果我們在emacs中要新建一個文件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之間自動的切換

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

  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中,我把這個函數(shù)和C-c,n做了綁定

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

c-macro模板

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

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

  1. (doit std::vector myContainer) 

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

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

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

  1. (doit std::vector myIt myContainer) 

生成的代碼如下:

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

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

  1. (api-fn) 

擴展之后將變成

  1. try 
  2.     // 光標將停在這里 等待具體的編輯 
  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ù),其將尋找光標前是否出現(xiàn)已定義的c-macro.在上面的例子中就是doit和api-fn。
如果出現(xiàn)了macro就做擴展,如果沒有出現(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. 下面是自定義的兩個模板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/

責任編輯:陳四芳 來源: 伯樂在線
相關推薦

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虛擬機實用

2011-07-21 13:40:17

java

2009-09-16 17:13:54

學習Linq

2009-11-16 10:57:51

PHP上傳文件代碼

2009-08-19 09:24:43

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

2009-09-29 16:32:11

OJB Hiberna

2010-03-25 13:42:14

云計算

2010-03-23 11:39:49

云計算

2010-04-21 14:53:46

Oracle游標

2010-05-19 17:24:55

MySQL編碼

2009-11-17 11:24:00

PHP應用技巧

2009-08-20 17:35:47

Servlet和JSP

2009-09-11 13:29:31

LINQ查詢操作

2009-09-16 17:44:54

LINQ to SQL

2009-10-22 15:07:12

綜合布線工程

2011-07-08 13:15:52

JSP

2010-06-12 17:37:18

UML實踐指南
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产特级毛片 | 亚洲色图网址 | 日韩福利| 久久久精品一区二区三区四季av | 亚洲免费在线播放 | 国产九九九九 | 日韩精品免费视频 | 中文字幕精品一区二区三区精品 | 久久久青草婷婷精品综合日韩 | 久久综合av | 久久久久国产 | 天天操网 | 亚洲国产欧美国产综合一区 | 不卡一区 | 国产精品69毛片高清亚洲 | www.v888av.com| 精品国产一区二区三区性色av | 国产福利精品一区 | 51ⅴ精品国产91久久久久久 | 五月婷婷色 | 久久久久亚洲精品国产 | 91麻豆精品一区二区三区 | 天堂av资源 | 亚洲日韩中文字幕一区 | 伊人超碰 | 日韩理论电影在线观看 | 中文一区二区 | 欧美videosex性极品hd | 91视频在线 | 国产亚洲精品精品国产亚洲综合 | 久久久成| 国产精品国产成人国产三级 | 免费中文字幕日韩欧美 | 成人福利在线观看 | 精品欧美一区二区在线观看 | 日本粉嫩一区二区三区视频 | 国产免费一区二区三区 | 97国产精品视频 | 精品国产鲁一鲁一区二区张丽 | 亚洲精品视频播放 | 久久69精品久久久久久国产越南 |