added menu support

This commit is contained in:
Zhang Chiyuan 2008-03-06 09:15:58 +00:00
parent be0493a0cb
commit 47e01c676f
17 changed files with 157 additions and 10 deletions

View File

@ -0,0 +1,3 @@
#name : v.begin(), v.end()
# --
${1:v}.begin(), $1.end

8
snippets/c++-mode/class Normal file
View File

@ -0,0 +1,8 @@
#name : class Ą­ { Ą­ }
# --
class ${1:Name}
{
public:
$1($2);
virtual ~$1();
};

6
snippets/c++-mode/do Normal file
View File

@ -0,0 +1,6 @@
#name : do { ¡­ } while (¡­)
# --
do
{
$0
} while (${1:condition});

6
snippets/c++-mode/for Normal file
View File

@ -0,0 +1,6 @@
#name : for (¡­; ¡­; ¡­) { ¡­ }
# --
for (${1:int i = 0}; ${2:i < N}; ${3:++i})
{
$0
}

6
snippets/c++-mode/if Normal file
View File

@ -0,0 +1,6 @@
#name : if ( ¡­ ) { ¡­ }
# --
if (${1:condition})
{
$0
}

7
snippets/c++-mode/main Normal file
View File

@ -0,0 +1,7 @@
#name: int main(argc, argv) { ¡­ }
# --
int main(int argc, char const *argv)
{
$0
return 0;
}

8
snippets/c++-mode/once Normal file
View File

@ -0,0 +1,8 @@
#name : #ifndef XXX; #define XXX; #endif
# --
#ifndef ${1:_`(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`_H_}
#define $1
$0
#endif /* $1 */

6
snippets/c++-mode/struct Normal file
View File

@ -0,0 +1,6 @@
#name : struct Ą­ { Ą­ }
# --
struct ${1:name}
{
$0
};

View File

@ -1,3 +1,3 @@
#name : #include <...>
#name : #include <¡­>
# --
#include <$1>

6
snippets/c-mode/do Normal file
View File

@ -0,0 +1,6 @@
#name : do { ¡­ } while (¡­)
# --
do
{
$0
} while (${1:condition});

6
snippets/c-mode/for Normal file
View File

@ -0,0 +1,6 @@
#name : for (¡­; ¡­; ¡­) { ¡­ }
# --
for (${1:int i = 0}; ${2:i < N}; ${3:++i})
{
$0
}

6
snippets/c-mode/if Normal file
View File

@ -0,0 +1,6 @@
#name : if ( ¡­ ) { ¡­ }
# --
if (${1:condition})
{
$0
}

View File

@ -1,3 +1,3 @@
#name : #include "..."
#name : #include "¡­"
# --
#include "$1"

7
snippets/c-mode/main Normal file
View File

@ -0,0 +1,7 @@
#name: int main(argc, argv) { ¡­ }
# --
int main(int argc, char *argv)
{
$0
return 0;
}

8
snippets/c-mode/once Normal file
View File

@ -0,0 +1,8 @@
#name : #ifndef XXX; #define XXX; #endif
# --
#ifndef ${1:_`(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`_H_}
#define $1
$0
#endif /* $1 */

6
snippets/c-mode/struct Normal file
View File

@ -0,0 +1,6 @@
#name : struct Ą­ { Ą­ }
# --
struct ${1:name}
{
$0
};

View File

@ -36,6 +36,12 @@ expanded.")
current column if this variable is non-`nil'.")
(make-variable-buffer-local 'yas/indent-line)
(defvar yas/trigger-key (kbd "TAB")
"The key to bind as a trigger of snippet.")
(defvar yas/trigger-fallback 'indent-according-to-mode
"The fallback command to call when there's no snippet to expand.")
(make-variable-buffer-local 'yas/trigger-fallback)
(defvar yas/keymap (make-sparse-keymap)
"The keymap of snippet.")
(define-key yas/keymap (kbd "TAB") 'yas/next-field-group)
@ -43,11 +49,25 @@ current column if this variable is non-`nil'.")
(define-key yas/keymap (kbd "<S-iso-lefttab>") 'yas/prev-field-group)
(define-key yas/keymap (kbd "<S-tab>") 'yas/prev-field-group)
(defvar yas/use-menu t
"If this is set to `t', all snippet template of the current
mode will be listed under the menu \"yasnippet\".")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Internal variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar yas/version "0.1")
(defvar yas/snippet-tables (make-hash-table)
"A hash table of snippet tables corresponding to each major-mode.")
(defvar yas/menu-table (make-hash-table)
"A hash table of menus of corresponding major-mode.")
(defvar yas/menu-keymap (make-sparse-keymap))
;; empty menu will cause problems, so we insert some items
(define-key yas/menu-keymap [yas/about]
'(menu-item "About" yas/about))
(define-key yas/menu-keymap [yas/separator]
'(menu-item "--"))
(defconst yas/escape-backslash
(concat "YASESCAPE" "BACKSLASH" "PROTECTGUARD"))
@ -165,6 +185,7 @@ have, compare through the start point of the overlay."
(goto-char (point-min))
(while (search-forward from nil t)
(replace-match to t t)))
(defun yas/snippet-table (mode)
"Get the snippet table corresponding to MODE."
(let ((table (gethash mode yas/snippet-tables)))
@ -176,6 +197,14 @@ have, compare through the start point of the overlay."
"Get the snippet table for current major-mode."
(yas/snippet-table major-mode))
(defun yas/menu-keymap-for-mode (mode)
"Get the menu keymap correspondong to MODE."
(let ((keymap (gethash mode yas/menu-table)))
(unless keymap
(setq table (make-sparse-keymap))
(puthash mode keymap yas/menu-table))
table))
(defsubst yas/template (key snippet-table)
"Get template for KEY in SNIPPET-TABLE."
(gethash key snippet-table))
@ -499,15 +528,45 @@ an example:
(file-directory-p file))))
(directory-files directory t)))
(defun yas/make-menu-binding (template)
(lexical-let ((template (yas/template-content template)))
(lambda ()
(interactive)
(yas/expand-snippet (point)
(point)
template))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User level functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun yas/about ()
(interactive)
(message (concat "yasnippet (version "
yas/version
") -- pluskid <pluskid@gmail.com>")))
(defun yas/initialize ()
"Do necessary initialization."
(global-set-key yas/trigger-key 'yas/expand)
(when yas/use-menu
(global-set-key [menu-bar yasnippet]
(cons "yasnippet" yas/menu-keymap))))
(defun yas/define (mode key template &optional name)
"Define a snippet. Expanding KEY into TEMPLATE.
NAME is a description to this template."
(puthash key
(yas/make-template template (or name key))
(yas/snippet-table mode)))
NAME is a description to this template. Also update
the menu if `yas/use-menu' is `t'."
(let ((template (yas/make-template template (or name key))))
(puthash key
template
(yas/snippet-table mode))
(when yas/use-menu
(let ((keymap (yas/menu-keymap-for-mode mode)))
(define-key yas/menu-keymap (vector mode)
`(menu-item ,(symbol-name mode) ,keymap))
(define-key keymap (vector (make-symbol key))
`(menu-item ,(yas/template-name template)
,(yas/make-menu-binding template)
:keys ,(concat key " ->")))))))
(defun yas/expand ()
"Expand a snippet. When a snippet is expanded, t is returned,
@ -516,10 +575,9 @@ otherwise, nil returned."
(multiple-value-bind (key start end) (yas/current-key)
(let ((template (yas/template key (yas/current-snippet-table))))
(if template
(progn
(yas/expand-snippet start end (yas/template-content template))
t)
nil))))
(yas/expand-snippet start end (yas/template-content template))
(when yas/trigger-fallback
(call-interactively yas/trigger-fallback))))))
(defun yas/next-field-group ()
"Navigate to next field group. If there's none, exit the snippet."