日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
經(jīng)驗總結(jié):實用的Emacs配置文件搜羅

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

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、撫州網(wǎng)絡(luò)推廣、小程序定制開發(fā)、撫州網(wǎng)絡(luò)營銷、撫州企業(yè)策劃、撫州品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供撫州建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com

在一些文本的末尾添加遞增的數(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之前,代碼審查都是面對面進行的。我曾經(jīng)使用下面這個函數(shù)來幫助記錄意見所對應(yīng)的源文件和行號。

 
 
 
  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。所 以可以直接點擊其路徑和行號,就可以直接打源文件跳到相應(yīng)的行上去。比如

 
 
 
  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ù)在我們要編輯一個新頭文件時,自動給文件添加上預(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)))))) 

如果我們在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)常要鍵入一些重復(fù)的操作,比如歷遍容器,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")) 

網(wǎng)站欄目:經(jīng)驗總結(jié):實用的Emacs配置文件搜羅
網(wǎng)址分享:http://www.dlmjj.cn/article/djjhpoh.html