bug fix: wired behavior of synchronize field.

This commit is contained in:
Zhang Chiyuan 2008-03-04 13:17:16 +00:00
parent 748d767cf1
commit 5a1f0a5c21

View File

@ -67,6 +67,13 @@ current column if this variable is non-`nil'.")
(incf yas/snippet-id-seed) (incf yas/snippet-id-seed)
id)) id))
(defvar yas/overlay-modification-hooks
(list 'yas/overlay-modification-hook)
"The list of hooks to the overlay modification event.")
(defvar yas/overlay-insert-in-front-hooks
(list 'yas/overlay-insert-in-front-hook)
"The list of hooks of the overlay inserted in front event.")
(defun yas/snippet-new () (defun yas/snippet-new ()
"Create a new snippet." "Create a new snippet."
(cons nil (cons nil (yas/snippet-next-id)))) (cons nil (cons nil (yas/snippet-next-id))))
@ -207,13 +214,48 @@ have, compare through the start point of the overlay."
start start
end)))) end))))
(defun yas/synchronize-fields (field-group)
"Update all fields' text according to the primary field."
(save-excursion
(let* ((inhibit-modification-hooks t)
(primary (yas/snippet-field-group-primary field-group))
(primary-overlay (yas/snippet-field-overlay primary))
(text (buffer-substring-no-properties (overlay-start primary-overlay)
(overlay-end primary-overlay))))
(dolist (field (yas/snippet-field-group-fields field-group))
(let* ((field-overlay (yas/snippet-field-overlay field))
(original-length (- (overlay-end field-overlay)
(overlay-start field-overlay))))
(unless (eq field-overlay primary-overlay)
(goto-char (overlay-start field-overlay))
(insert text)
(delete-char original-length)))))))
(defun yas/overlay-modification-hook (overlay after? beg end &optional length)
"Modification hook for snippet field overlay."
(when after?
(yas/synchronize-fields (overlay-get overlay 'yas/snippet-field-group))))
(defun yas/overlay-insert-in-front-hook (overlay after? beg end &optional length)
"Hook for snippet overlay when text is inserted in front of snippet."
(let ((field-group (overlay-get overlay 'yas/snippet-field-group)))
(when after?
(when (and (= length 0)
(overlay-get overlay 'yas/snippet-field-initial-value))
(let ((inhibit-modification-hooks t))
(overlay-put overlay 'yas/snippet-field-initial-value nil)
(save-excursion
(goto-char end)
(delete-char (- (overlay-end overlay) end)))))
(yas/synchronize-fields field-group))))
(defun yas/expand-snippet (start end template) (defun yas/expand-snippet (start end template)
"Expand snippet at current point. Text between START and END "Expand snippet at current point. Text between START and END
will be deleted before inserting template." will be deleted before inserting template."
(goto-char start) (goto-char start)
(let ((length (- end start)) (let ((length (- end start))
(column (current-column))) (column (current-column))
(inhibit-modification-hooks t))
(save-restriction (save-restriction
(narrow-to-region start start) (narrow-to-region start start)
@ -286,6 +328,9 @@ will be deleted before inserting template."
(overlay-put overlay 'keymap yas/keymap) (overlay-put overlay 'keymap yas/keymap)
(overlay-put overlay 'yas/snippet snippet) (overlay-put overlay 'yas/snippet snippet)
(overlay-put overlay 'yas/snippet-field-group group) (overlay-put overlay 'yas/snippet-field-group group)
(overlay-put overlay 'yas/snippet-field-initial-value t)
(overlay-put overlay 'modification-hooks yas/overlay-modification-hooks)
(overlay-put overlay 'insert-in-front-hooks yas/overlay-insert-in-front-hooks)
(dolist (field (yas/snippet-field-group-fields group)) (dolist (field (yas/snippet-field-group-fields group))
(overlay-put (yas/snippet-field-overlay field) (overlay-put (yas/snippet-field-overlay field)
'face 'face