mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-14 05:23:04 +00:00
Refactor: remove horrible 'yas--trigger-key-for-fallback' and rework 'yas--fallback'
This commit is contained in:
parent
9503c332f5
commit
38db5aa9fb
70
yasnippet.el
70
yasnippet.el
@ -2257,18 +2257,15 @@ expand immediately. Common gateway for
|
|||||||
end
|
end
|
||||||
(yas--template-expand-env yas--current-template)))))
|
(yas--template-expand-env yas--current-template)))))
|
||||||
|
|
||||||
(defun yas--trigger-key-for-fallback ()
|
;; Apropos the trigger key and the fallback binding:
|
||||||
;; When `yas-trigger-key' is <tab> it correctly overrides
|
;;
|
||||||
;; org-mode's <tab>, for example and searching for fallbacks
|
;; When `yas-trigger-key' is <tab> it correctly overrides
|
||||||
;; correctly returns `org-cycle'. However, most other modes bind
|
;; org-mode's <tab>, for example and searching for fallbacks
|
||||||
;; "TAB" (which is translated from <tab>), and calling
|
;; correctly returns `org-cycle'. However, most other modes bind
|
||||||
;; (key-binding "TAB") does not place return that command into
|
;; "TAB" (which is translated from <tab>), and calling
|
||||||
;; our command-2 local. So we cheat.
|
;; (key-binding "TAB") does not place return that command into
|
||||||
;;
|
;; our command-2 local. So we cheat.
|
||||||
(if (string= yas-trigger-key "<tab>")
|
;;
|
||||||
"TAB"
|
|
||||||
yas-trigger-key))
|
|
||||||
|
|
||||||
(defun yas--fallback (&optional from-trigger-key-p)
|
(defun yas--fallback (&optional from-trigger-key-p)
|
||||||
"Fallback after expansion has failed.
|
"Fallback after expansion has failed.
|
||||||
|
|
||||||
@ -2280,25 +2277,13 @@ Common gateway for `yas-expand-from-trigger-key' and
|
|||||||
((eq yas-fallback-behavior 'call-other-command)
|
((eq yas-fallback-behavior 'call-other-command)
|
||||||
(let* ((yas-minor-mode nil)
|
(let* ((yas-minor-mode nil)
|
||||||
(yas--direct-keymaps nil)
|
(yas--direct-keymaps nil)
|
||||||
(yas-trigger-key (yas--trigger-key-for-fallback))
|
(keys (this-single-command-keys))
|
||||||
(keys-1 (this-command-keys-vector))
|
(beyond-yasnippet (or (key-binding keys t)
|
||||||
(keys-2 (and yas-trigger-key
|
(key-binding (yas--fallback-translate-input keys) t))))
|
||||||
from-trigger-key-p
|
(yas--message 4 "Falling back to %s" beyond-yasnippet)
|
||||||
(stringp yas-trigger-key)
|
(when (commandp beyond-yasnippet)
|
||||||
(read-kbd-macro yas-trigger-key)))
|
(setq this-original-command beyond-yasnippet)
|
||||||
(command-1 (and keys-1 (key-binding keys-1)))
|
(call-interactively beyond-yasnippet))))
|
||||||
(command-2 (and keys-2 (key-binding keys-2)))
|
|
||||||
;; An (ugly) safety: prevents infinite recursion of
|
|
||||||
;; yas-expand* calls.
|
|
||||||
(command (or (and (symbolp command-1)
|
|
||||||
(not (string-match "yas-expand" (symbol-name command-1)))
|
|
||||||
command-1)
|
|
||||||
(and (symbolp command-2)
|
|
||||||
command-2))))
|
|
||||||
(when (and (commandp command)
|
|
||||||
(not (string-match "yas-expand" (symbol-name command))))
|
|
||||||
(setq this-command command)
|
|
||||||
(call-interactively command))))
|
|
||||||
((and (listp yas-fallback-behavior)
|
((and (listp yas-fallback-behavior)
|
||||||
(cdr yas-fallback-behavior)
|
(cdr yas-fallback-behavior)
|
||||||
(eq 'apply (car yas-fallback-behavior)))
|
(eq 'apply (car yas-fallback-behavior)))
|
||||||
@ -2312,6 +2297,29 @@ Common gateway for `yas-expand-from-trigger-key' and
|
|||||||
;; also return nil if all the other fallbacks have failed
|
;; also return nil if all the other fallbacks have failed
|
||||||
nil)))
|
nil)))
|
||||||
|
|
||||||
|
(defun yas--fallback-translate-input (keys)
|
||||||
|
"Emulate `read-key-sequence', at least what I think it does.
|
||||||
|
|
||||||
|
Keys should be an untranslated key vector. Returns a translated
|
||||||
|
vector of keys. XXX not working yet"
|
||||||
|
(let ((retval [])
|
||||||
|
(i 0))
|
||||||
|
(while (< i (length keys))
|
||||||
|
(let ((j i)
|
||||||
|
(translated local-function-key-map))
|
||||||
|
(while (and (< j (length keys))
|
||||||
|
translated
|
||||||
|
(keymapp translated))
|
||||||
|
(setq translated (aget (remove 'keymap translated) (aref keys j))
|
||||||
|
j (1+ j)))
|
||||||
|
(setq retval (vconcat retval (cond ((symbolp translated)
|
||||||
|
`[,translated])
|
||||||
|
((vectorp translated)
|
||||||
|
translated)
|
||||||
|
(t
|
||||||
|
(substring keys i j)))))
|
||||||
|
(setq i j)))
|
||||||
|
retval))
|
||||||
|
|
||||||
|
|
||||||
;;; Utils for snippet development:
|
;;; Utils for snippet development:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user