Skip to content

Commit

Permalink
Avoid shadowing the tab => TAB remapping
Browse files Browse the repository at this point in the history
* yasnippet.el (yas-keymap): Don't bind `tab`.
(yas--read-keybinding): Prefer the more precise \` and \' regexps.
(yas--parse-template): Use `pcase`.
  • Loading branch information
monnier committed Jan 19, 2024
1 parent d7a79d4 commit 25f5d88
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions yasnippet.el
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,12 @@ The condition will respect the value of `yas-keymap-disable-hook'."

(defvar yas-keymap
(let ((map (make-sparse-keymap)))
(define-key map [(tab)] (yas-filtered-definition 'yas-next-field-or-maybe-expand))
;; Modes should always bind to TAB instead of `tab', so as not to override
;; bindings that should take higher precedence but which bind to `TAB`
;; instead (relying on `function-key-map` to remap `tab` to TAB).
;; If this causes problem because of another package that binds to `tab`,
;; complain to that other package!
;; (define-key map [tab] (yas-filtered-definition 'yas-next-field-or-maybe-expand))
(define-key map (kbd "TAB") (yas-filtered-definition 'yas-next-field-or-maybe-expand))
(define-key map [(shift tab)] (yas-filtered-definition 'yas-prev-field))
(define-key map [backtab] (yas-filtered-definition 'yas-prev-field))
Expand Down Expand Up @@ -651,7 +656,7 @@ expanded.")
;; instead (relying on `function-key-map` to remap `tab` to TAB).
;; If this causes problem because of another package that binds to `tab`,
;; complain to that other package!
;;(define-key map [(tab)] yas-maybe-expand)
;;(define-key map [tab] yas-maybe-expand)
(define-key map (kbd "TAB") yas-maybe-expand)
(define-key map "\C-c&\C-s" #'yas-insert-snippet)
(define-key map "\C-c&\C-n" #'yas-new-snippet)
Expand Down Expand Up @@ -1510,7 +1515,7 @@ return an expression that when evaluated will issue an error."
(when (and keybinding
(not (string-match "keybinding" keybinding)))
(condition-case err
(let ((res (or (and (string-match "^\\[.*\\]$" keybinding)
(let ((res (or (and (string-match "\\`\\[.*\\]\\'" keybinding)
(read keybinding))
(read-kbd-macro keybinding 'need-vector))))
res)
Expand Down Expand Up @@ -1594,39 +1599,33 @@ Here's a list of currently recognized directives:
(file-name-nondirectory file)))
(key nil)
template
bound
condition
(group (and file
(yas--calculate-group file)))
expand-env
binding
uuid)
(if (re-search-forward "^# --\\s-*\n" nil t)
(progn (setq template
(buffer-substring-no-properties (point)
(point-max)))
(setq bound (point))
(goto-char (point-min))
(while (re-search-forward "^# *\\([^ ]+?\\) *: *\\(.*?\\)[[:space:]]*$" bound t)
(when (string= "uuid" (match-string-no-properties 1))
(setq uuid (match-string-no-properties 2)))
(when (string= "type" (match-string-no-properties 1))
(setq type (if (string= "command" (match-string-no-properties 2))
'command
'snippet)))
(when (string= "key" (match-string-no-properties 1))
(setq key (match-string-no-properties 2)))
(when (string= "name" (match-string-no-properties 1))
(setq name (match-string-no-properties 2)))
(when (string= "condition" (match-string-no-properties 1))
(setq condition (yas--read-lisp (match-string-no-properties 2))))
(when (string= "group" (match-string-no-properties 1))
(setq group (match-string-no-properties 2)))
(when (string= "expand-env" (match-string-no-properties 1))
(setq expand-env (yas--read-lisp (match-string-no-properties 2)
'nil-on-error)))
(when (string= "binding" (match-string-no-properties 1))
(setq binding (match-string-no-properties 2)))))
(let ((bound (point)))
(setq template
(buffer-substring-no-properties (point)
(point-max)))
(goto-char (point-min))
(while (re-search-forward
"^# *\\([^ ]+?\\) *: *\\(.*?\\)[[:space:]]*$" bound t)
(let ((val (match-string-no-properties 2)))
(pcase (match-string-no-properties 1)
("uuid" (setq uuid val))
("type" (setq type (intern val)))
("key" (setq key val))
("name" (setq name val))
("condition" (setq condition (yas--read-lisp val)))
("group" (setq group val))
("expand-env"
(setq expand-env (yas--read-lisp val 'nil-on-error)))
("binding" (setq binding val))
("contributor" nil) ;Documented in `snippet-development.org'.
(dir (message "Ignoring unknown directive: %s" dir))))))
(setq template
(buffer-substring-no-properties (point-min) (point-max))))
(unless (or key binding)
Expand Down

0 comments on commit 25f5d88

Please sign in to comment.