Improve error handling in snippet commands

Fixes wrong-type-argument errors when fields or overlays are not
actually fields or overlays. Such heresy!
This commit is contained in:
Henrik Lissner 2018-06-16 12:22:50 +02:00
parent 3f2318bc69
commit b5baeb81a7
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -9,7 +9,8 @@
"Go to the beginning of the current field." "Go to the beginning of the current field."
(interactive) (interactive)
(let* ((snippet (car (yas-active-snippets))) (let* ((snippet (car (yas-active-snippets)))
(position (yas--field-start (yas--snippet-active-field snippet)))) (active-field (yas--snippet-active-field snippet))
(position (if (yas--field-p active-field) (yas--field-start active-field) -1)))
(if (= (point) position) (if (= (point) position)
(move-beginning-of-line 1) (move-beginning-of-line 1)
(goto-char position)))) (goto-char position))))
@ -19,7 +20,8 @@
"Go to the end of the current field." "Go to the end of the current field."
(interactive) (interactive)
(let* ((snippet (car (yas-active-snippets))) (let* ((snippet (car (yas-active-snippets)))
(position (yas--field-end (yas--snippet-active-field snippet)))) (active-field (yas--snippet-active-field snippet))
(position (if (yas--field-p active-field) (yas--field-end active-field) -1)))
(if (= (point) position) (if (= (point) position)
(move-end-of-line 1) (move-end-of-line 1)
(goto-char position)))) (goto-char position))))
@ -28,11 +30,12 @@
(defun +snippets/delete-backward-char (&optional field) (defun +snippets/delete-backward-char (&optional field)
"Prevents Yas from interfering with backspace deletion." "Prevents Yas from interfering with backspace deletion."
(interactive) (interactive)
(let ((field (or field (and yas--active-field-overlay (let ((field (or field (and (overlayp yas--active-field-overlay)
(overlay-buffer yas--active-field-overlay) (overlay-buffer yas--active-field-overlay)
(overlay-get yas--active-field-overlay 'yas--field))))) (overlay-get yas--active-field-overlay 'yas--field)))))
(cond ((eq (point) (marker-position (yas--field-start field))) nil) (unless (and (yas--field-p field)
(t (call-interactively #'delete-backward-char))))) (eq (point) (marker-position (yas--field-start field))))
(call-interactively #'delete-backward-char))))
;;;###autoload ;;;###autoload
(defun +snippets/delete-forward-char-or-field (&optional field) (defun +snippets/delete-forward-char-or-field (&optional field)
@ -42,21 +45,24 @@ buggy behavior when <delete> is pressed in an empty field."
(let ((field (or field (and yas--active-field-overlay (let ((field (or field (and yas--active-field-overlay
(overlay-buffer yas--active-field-overlay) (overlay-buffer yas--active-field-overlay)
(overlay-get yas--active-field-overlay 'yas--field))))) (overlay-get yas--active-field-overlay 'yas--field)))))
(cond ((and field (cond ((not (yas--field-p field))
(not (yas--field-modified-p field)) (delete-char 1))
((and (not (yas--field-modified-p field))
(eq (point) (marker-position (yas--field-start field)))) (eq (point) (marker-position (yas--field-start field))))
(yas--skip-and-clear field) (yas--skip-and-clear field)
(yas-next-field 1)) (yas-next-field 1))
((eq (point) (marker-position (yas--field-end field))) nil) ((eq (point) (marker-position (yas--field-end field))) nil)
(t (delete-char 1))))) ((delete-char 1)))))
;;;###autoload ;;;###autoload
(defun +snippets/delete-to-start-of-field (&optional field) (defun +snippets/delete-to-start-of-field (&optional field)
"Delete to start-of-field." "Delete to start-of-field."
(interactive) (interactive)
(let* ((field (or field (and yas--active-field-overlay (unless field
(overlay-buffer yas--active-field-overlay) (setq field (and (overlayp yas--active-field-overlay)
(overlay-get yas--active-field-overlay 'yas--field)))) (overlay-buffer yas--active-field-overlay)
(sof (marker-position (yas--field-start field)))) (overlay-get yas--active-field-overlay 'yas--field))))
(when (and field (> (point) sof)) (when (yas--field-p field)
(delete-region sof (point))))) (let ((sof (marker-position (yas--field-start field))))
(when (and field (> (point) sof))
(delete-region sof (point))))))