]> code.delx.au - gnu-emacs-elpa/blobdiff - yasnippet.el
a more proper popup position under Linux
[gnu-emacs-elpa] / yasnippet.el
index 0f389f7ae2b9a5d519d258c9a3d50424d8a369a7..72bd3833e147412e03e12c24126f2b5b72e80c5e 100644 (file)
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; User customizable variables
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(defvar yas/key-syntax "w"
-  "Syntax of a key. This is used to determine the current key being 
-expanded.")
+(defvar yas/key-syntaxes (list "w" "w_" "w_." "^ ")
+  "A list of syntax of a key. This list is tried in the order
+to try to find a key. For example, if the list is '(\"w\" \"w_\").
+And in emacs-lisp-mode, where \"-\" has the syntax of \"_\":
+
+foo-bar
+
+will first try \"bar\", if not found, then \"foo-bar\" is tried.")
 
 (defvar yas/indent-line t
   "Each (except the 1st) line of the snippet template is indented to
@@ -132,8 +137,9 @@ mode will be listed under the menu \"yasnippet\".")
                     (yas/snippet-groups snippet)
                     :test
                     '(lambda (field group)
-                       (= (yas/field-number field)
-                          (yas/group-number group))))))
+                       (and (not (null (yas/field-number field)))
+                            (= (yas/field-number field)
+                               (yas/group-number group)))))))
     (if group
        (yas/group-add-field group field)
       (push (yas/make-group field snippet)
@@ -210,13 +216,21 @@ have, compare through the start point of the overlay."
   "Get the key under current position. A key is used to find
 the template of a snippet in the current snippet-table."
   (let ((start (point))
-       (end (point)))
-    (save-excursion
-      (skip-syntax-backward yas/key-syntax)
-      (setq start (point))
-      (list (buffer-substring-no-properties start end)
-           start
-           end))))
+       (end (point))
+       (syntaxes yas/key-syntaxes)
+       syntax done)
+    (while (and (not done) syntaxes)
+      (setq syntax (car syntaxes))
+      (setq syntaxes (cdr syntaxes))
+      (save-excursion
+       (skip-syntax-backward syntax)
+       (when (gethash (buffer-substring-no-properties (point) end)
+                      (yas/current-snippet-table))
+         (setq done t)
+         (setq start (point)))))
+    (list (buffer-substring-no-properties start end)
+         start
+         end)))
 
 (defun yas/synchronize-fields (field-group)
   "Update all fields' text according to the primary field."
@@ -546,18 +560,27 @@ an example:
 (defun yas/fake-keymap-for-popup (templates)
   "Create a fake keymap for popup menu usage."
   (cons 'keymap 
-       (cons "Select a template:"
-             (mapcar (lambda (pair)
-                       (let* ((template (cdr pair))
-                              (name (yas/template-name template))
-                              (content (yas/template-content template)))
-                         (list content 'menu-item name t)))
-                     templates))))
+       (mapcar (lambda (pair)
+                 (let* ((template (cdr pair))
+                        (name (yas/template-name template))
+                        (content (yas/template-content template)))
+                   (list content 'menu-item name t)))
+               templates)))
+
+(defun yas/point-to-coord (&optional point)
+  "Get the xoffset/yoffset information of POINT.
+If POINT is not given, default is to current point."
+  (let* ((pn (posn-at-point (or point (point))))
+        (x-y (posn-x-y pn))
+        (x (car x-y))
+        (y (cdr x-y))
+        (coord (list (list (+ x 10) (+ y 20)) (selected-window))))
+    coord))
 
 (defun yas/popup-for-template (templates)
   "Show a popup menu listing templates to let the user select one."
   (if window-system
-      (car (x-popup-menu t (yas/fake-keymap-for-popup templates)))
+      (car (x-popup-menu (yas/point-to-coord) (yas/fake-keymap-for-popup templates)))
     ;; no window system, simply select the first one
     (cdar templates)))