My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ConfigExample  
An example of smart-snippet config.
Featured
Updated Feb 4, 2010 by pluskid....@gmail.com

This is my own config of smart-snippet.el, the latest version can always be downloaded from here.

;;;; my smart-snippet config
(require 'smart-snippet)
(setq save-abbrevs nil)

;;; for c++-mode
(require 'cc-mode)
(smart-snippet-with-abbrev-tables
 (c++-mode-abbrev-table
  java-mode-abbrev-table)
  ("if" "if ($${condition})\n{$>\n$>$.\n}$>" 'bol?)
  ("elsif" "else if ($${condition})\n{$>\n$>$.\n}$>" 'bol?)
  ("else" "else\n{$>\n$>$.\n}$>" 'bol?)
  ("for" "for ($${init}; $${cond}; $${step})\n{$>\n$>$.\n}$>" 'bol?)
  ("namespace" "namespace $${name} {\n$.\n} // namespace $${name}" 'bol?))

;; those non-word snippet can't be triggered by abbrev expand, we
;; need to bind them explicitly to some key
(smart-snippet-with-abbrev-tables
 (c++-mode-abbrev-table
  java-mode-abbrev-table)
  ("{" "{$.}" '(not (c-in-literal)))
  ("{" "{$>\n$>$.\n}$>" 'bol?)
  ;; if not in comment or other stuff(see `c-in-literal'), then
  ;; inser a pair of quote. if already in string, insert `\"'
  ("\"" "\"$.\"" '(not (c-in-literal)))	
  ("\"" "\\\"$." '(eq (c-in-literal) 'string))
  ;; insert a pair of parenthesis, useful everywhere
  ("(" "($.)" t)
  ;; insert a pair of angular bracket if we are writing templates
  ("<" "<$.>" '(and (not (c-in-literal))
			 (looking-back "template[[:blank:]]*")))
  ;; a pair of square bracket, also useful everywhere
  ("[" "[$.]" t)
  ;; a pair of single quote, if not in literal
  ("'" "'$.'" '(not (c-in-literal)))
  )

(smart-snippet-with-keymaps
 ((c++-mode-map c++-mode-abbrev-table)
  (java-mode-map java-mode-abbrev-table))
  ("{" "{")
  ("\"" "\"")
  ("(" "(")
  ("<" "<")
  ("[" "[")
  ("'" "'")
  )

;; jump out from a pair(like quote, parenthesis, etc.)
(defun kid-c-escape-pair ()
  (interactive)
  (let ((pair-regexp "[^])}\"'>]*[])}\"'>]"))
    (if (looking-at pair-regexp)
	(progn
	  ;; be sure we can use C-u C-@ to jump back
	  ;; if we goto the wrong place
	  (push-mark) 
	  (goto-char (match-end 0)))
      (c-indent-command))))
;; note TAB can be different to <tab> in X mode(not -nw mode).
;; the formal is C-i while the latter is the real "Tab" key in
;; your keyboard.
(define-key c++-mode-map (kbd "TAB") 'kid-c-escape-pair)
(define-key c++-mode-map (kbd "<tab>") 'c-indent-command)
;; snippet.el use TAB, now we need to use <tab>
(define-key snippet-map  (kbd "<tab>") 'snippet-next-field)
Comment by ILove...@gmail.com, Aug 13, 2007

我把上面的代码粘贴到我的.emacs文件中,为什么就不行呢?一点反映都没有。不知道为什么?在我的.emacs文件中只有上面的代码,没有其他的代码。

Comment by project member plus...@gmail.com, Nov 1, 2007

to ILoveYLF: 你把 smart-snippet.el 放到 load-path 里面了吗?

Comment by ahei0802, Nov 20, 2007

我这上面显示Symbol's function definition is void: looking-back looking-back这个函数在什么插件里面啊?

Comment by ahei0802, Nov 21, 2007

为什么我这样配置的 (require 'smart-snippet)

(smart-snippet-with-abbrev-tables

(c++-mode-abbrev-table)
("for" "for ($$(as) $$(abc) $$)" 'bol?)
) 但我输入的时候却是这样: for (asasfsafsfasd abcasfsafsfasd asfsafsfasd)

Comment by project member plus...@gmail.com, Nov 22, 2007

to ahei0802: looking-back 这个应该是在比较高的版本的 Emacs 里面才有的函数。

Comment by project member plus...@gmail.com, Nov 22, 2007

to ahei: 请问你修改过了 snippet-field-default-beg-char 这些变量了吗?如果没有改过的话,应该是 “for ($${a} $${b} $${c})” 这样的吧。如果改过了仍然有问题的话,能否把你的关于 smart-snippet 和 c++-mode 的完整配置贴出来我看看可能是哪里的问题?

ps:多谢你对 smart-snippet.el 的兴趣! :)

Comment by pite...@gmail.com, Nov 28, 2007

请问:如果是fortran代码,是不是可以这样改: (require 'smart-snippet)

(smart-snippet-with-abbrev-tables

(f90-mode-abbrev-table)
("for" "for ($$(as) $$(abc) $$)" 'bol?)
Comment by pite...@gmail.com, Nov 28, 2007

我是写fortran代码的,将c++-mod改成f90-mode一直不行,老是报错。希望能够提供一个fortran程序的例子,谢谢

Comment by changwei...@gmail.com, Jan 29, 2008

这个工具太棒了,不过配置起来还是比较麻烦。

我想能不能把这些配置写成一个模板文件,文件名是缩写, 文件内容是代码模板。 例如: for.snippet

for ($$(as) $$(abc) $$)

把这些模板文件,根据模板所对应的mode存放,这样smart-snippet就可以自动根据emacs打开的文件mode对应相关配置。

希望本项目的开发人员,继续努力。


Sign in to add a comment
Powered by Google Project Hosting