]> code.delx.au - gnu-emacs-elpa/commitdiff
added menu support
authorZhang Chiyuan <pluskid@gmail.com>
Thu, 6 Mar 2008 09:15:58 +0000 (09:15 +0000)
committerZhang Chiyuan <pluskid@gmail.com>
Thu, 6 Mar 2008 09:15:58 +0000 (09:15 +0000)
17 files changed:
snippets/c++-mode/beginend [new file with mode: 0644]
snippets/c++-mode/class [new file with mode: 0644]
snippets/c++-mode/do [new file with mode: 0644]
snippets/c++-mode/for [new file with mode: 0644]
snippets/c++-mode/if [new file with mode: 0644]
snippets/c++-mode/main [new file with mode: 0644]
snippets/c++-mode/once [new file with mode: 0644]
snippets/c++-mode/struct [new file with mode: 0644]
snippets/c-mode/Inc.yas
snippets/c-mode/do [new file with mode: 0644]
snippets/c-mode/for [new file with mode: 0644]
snippets/c-mode/if [new file with mode: 0644]
snippets/c-mode/inc
snippets/c-mode/main [new file with mode: 0644]
snippets/c-mode/once [new file with mode: 0644]
snippets/c-mode/struct [new file with mode: 0644]
yasnippet.el

diff --git a/snippets/c++-mode/beginend b/snippets/c++-mode/beginend
new file mode 100644 (file)
index 0000000..aa45495
--- /dev/null
@@ -0,0 +1,3 @@
+#name : v.begin(), v.end()\r
+# --\r
+${1:v}.begin(), $1.end
\ No newline at end of file
diff --git a/snippets/c++-mode/class b/snippets/c++-mode/class
new file mode 100644 (file)
index 0000000..1aefd41
--- /dev/null
@@ -0,0 +1,8 @@
+#name : class ¡­ { ¡­ }
+# --
+class ${1:Name}
+{
+public:
+    $1($2);
+    virtual ~$1();
+};
\ No newline at end of file
diff --git a/snippets/c++-mode/do b/snippets/c++-mode/do
new file mode 100644 (file)
index 0000000..e0bc20b
--- /dev/null
@@ -0,0 +1,6 @@
+#name : do { ¡­ } while (¡­)
+# --
+do
+{
+    $0
+} while (${1:condition});
\ No newline at end of file
diff --git a/snippets/c++-mode/for b/snippets/c++-mode/for
new file mode 100644 (file)
index 0000000..911107c
--- /dev/null
@@ -0,0 +1,6 @@
+#name : for (¡­; ¡­; ¡­) { ¡­ }
+# --
+for (${1:int i = 0}; ${2:i < N}; ${3:++i})
+{
+    $0
+}
\ No newline at end of file
diff --git a/snippets/c++-mode/if b/snippets/c++-mode/if
new file mode 100644 (file)
index 0000000..6a93419
--- /dev/null
@@ -0,0 +1,6 @@
+#name : if ( ¡­ ) { ¡­ }
+# --
+if (${1:condition})
+{
+    $0
+}
\ No newline at end of file
diff --git a/snippets/c++-mode/main b/snippets/c++-mode/main
new file mode 100644 (file)
index 0000000..e1824b2
--- /dev/null
@@ -0,0 +1,7 @@
+#name: int main(argc, argv) { ¡­ }
+# --
+int main(int argc, char const *argv)
+{
+    $0
+    return 0;
+}
\ No newline at end of file
diff --git a/snippets/c++-mode/once b/snippets/c++-mode/once
new file mode 100644 (file)
index 0000000..1b63c39
--- /dev/null
@@ -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 */
\ No newline at end of file
diff --git a/snippets/c++-mode/struct b/snippets/c++-mode/struct
new file mode 100644 (file)
index 0000000..8cc1a25
--- /dev/null
@@ -0,0 +1,6 @@
+#name : struct ¡­ { ¡­ }
+# --
+struct ${1:name}
+{
+    $0
+};
\ No newline at end of file
index de64090673523b94a99958016522ceed8bace4db..cdcc8f1a3ac024d581601817f1fecfb05b8d977f 100644 (file)
@@ -1,3 +1,3 @@
-#name : #include <...>\r
+#name : #include <¡­>\r
 # --\r
 #include <$1>
\ No newline at end of file
diff --git a/snippets/c-mode/do b/snippets/c-mode/do
new file mode 100644 (file)
index 0000000..e0bc20b
--- /dev/null
@@ -0,0 +1,6 @@
+#name : do { ¡­ } while (¡­)
+# --
+do
+{
+    $0
+} while (${1:condition});
\ No newline at end of file
diff --git a/snippets/c-mode/for b/snippets/c-mode/for
new file mode 100644 (file)
index 0000000..911107c
--- /dev/null
@@ -0,0 +1,6 @@
+#name : for (¡­; ¡­; ¡­) { ¡­ }
+# --
+for (${1:int i = 0}; ${2:i < N}; ${3:++i})
+{
+    $0
+}
\ No newline at end of file
diff --git a/snippets/c-mode/if b/snippets/c-mode/if
new file mode 100644 (file)
index 0000000..6a93419
--- /dev/null
@@ -0,0 +1,6 @@
+#name : if ( ¡­ ) { ¡­ }
+# --
+if (${1:condition})
+{
+    $0
+}
\ No newline at end of file
index 61fd09b9b5ea463cd9d65ca68b3dd520d28c2617..d43d6bb0b9a3e64eecb2a408a301eac5df5384d9 100644 (file)
@@ -1,3 +1,3 @@
-#name : #include "..."\r
+#name : #include "¡­"\r
 # --\r
 #include "$1"
\ No newline at end of file
diff --git a/snippets/c-mode/main b/snippets/c-mode/main
new file mode 100644 (file)
index 0000000..2be6ccb
--- /dev/null
@@ -0,0 +1,7 @@
+#name: int main(argc, argv) { ¡­ }
+# --
+int main(int argc, char *argv)
+{
+    $0
+    return 0;
+}
\ No newline at end of file
diff --git a/snippets/c-mode/once b/snippets/c-mode/once
new file mode 100644 (file)
index 0000000..1b63c39
--- /dev/null
@@ -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 */
\ No newline at end of file
diff --git a/snippets/c-mode/struct b/snippets/c-mode/struct
new file mode 100644 (file)
index 0000000..8cc1a25
--- /dev/null
@@ -0,0 +1,6 @@
+#name : struct ¡­ { ¡­ }
+# --
+struct ${1:name}
+{
+    $0
+};
\ No newline at end of file
index 2ffb926867df0733da8915af3bf1bae6ddc29894..9f993851bd555c07f5e374d353fedeeee922dc5e 100644 (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."