Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set condition for snippet to only expand at beginning of line? #1000

Open
haqle314 opened this issue Apr 30, 2019 · 1 comment
Open

Comments

@haqle314
Copy link

Hi, I looked through the document but I'm not sure how can I achieve this? Let's say I have a snippet with a single character ?n as key. But I want this snippet to only be active when ?n is at the beginning of the line, not when it's in the middle of the line. I'm trying to write a function to be used as the snippet condition, something like this. But there's no yas--current-key, I tried using yas--current-template but it's not bound during condition checking.

(defun key-at-bol-p ()
    (equal yas--current-key (buffer-substring (line-beginning-position) (point)))))
@npostavs
Copy link
Collaborator

npostavs commented May 4, 2019

Currently expansion conditions are global, not per-snippet. But you could sort of hack it in with yas-key-syntaxes (see below). If #988 is implemented, an alternative would be to use ^n as the snippet key.

(defconst my-yas-bol-keys-regexp
  (regexp-opt '("n")))

(defun my-yas-key-syntax-wrapper (orig-method pos)
  (let ((ret (cond ((stringp orig-method)
                    (skip-syntax-backward orig-method)
                    nil)
                   ((functionp orig-method)
                    (funcall orig-method original))
                   (t (error "Bad `yas-key-syntax' method: `%s'"
                             orig-method)))))
    (if (and (not (bolp))
             (looking-at my-yas-bol-keys-regexp)
             (= (match-end 0) pos))
        ;; Don't match anything else.
        (progn (goto-char pos)
               nil)
      ret)))

(setq yas-key-syntaxes
      (mapcar (lambda (method)
                (apply-partially #'my-yas-key-syntax-wrapper method))
              ;; Default value.
              (list #'yas-try-key-from-whitespace
                    "w_.()" "w_." "w_" "w")))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants