使用emacs作为代码片段管理工具

一、需求

    一个代码片段管理工具所需要的基本功能大概包括:

  1. 支持多语言的高亮

  2. 能够保存对代码的说明

  3. 支持TAG标签

  4. 有方便的查询功能


    而Emacs的Org-mode恰好能够完美的支持上面这些需求.

二、实现

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(defvar mode-file-map '((c++-mode .  "cpp.org" )
                         (emacs-lisp-mode .  "elisp.org" )
                         (python-mode .  "python.org" )
                         (perl-mode .  "perl.org" )
                         (dos-mode .  "bat.org" )
                         (sh-mode .  "bash.org" ))
   "映射major-mode与保存代码片段文件的对应关系"
   )
 
(defvar code-library-path  "d:/CodeLibrary/"
   "代码库文件存储的目录"
   )
(defun save-code-to-library()
   (interactive)
   ( let  (
         (code (get-region- or -thing 'defun))
         (library-file (concat code-library-path (cdr (assoc major-mode mode-file-map))))
         (head (read-string  "请输入这段代码的说明" ))
         (code-major-mode (replace-regexp-in-string  "-mode$"  ""  (format  "%s"  major-mode))))
     ( when  (string= library-file code-library-path)
       (setq library-file (concat code-library-path  "temp.org" )))
     (find-file library-file)
     ( end - of -buffer)
     (newline)
     (insert (concat  "* "  head))
     (newline- and -indent)
     (insert (concat  "#+BEGIN_SRC "  code-major-mode))
     (newline- and -indent)
     (newline- and -indent)
     (insert  "#+END_SRC" )
     (forward-line -1)                   ;上一行
     (org-edit-src-code)
     (insert code)
     (org-edit-src-exit)
     (org-set-tags-command)              ;设置代码tags
     (save-buffer)
     ;; (kill-buffer)
   ))

三、使用

    在Emacs中看到中意的代码要保存下来,只需要选中要保存的代码,然后执行M-x save-code-to-library. Emacs会自动根据代码所处与的major-mode来挑选合适的org文件保存,并提示输入代码说明和tag.


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值