-(defun company-template-add-field (templ pos text)
- (assert templ)
- (save-excursion
- ;; (goto-char pos)
- (let ((ov (make-overlay pos pos))
- (siblings (overlay-get templ 'company-template-fields))
- (label (propertize text 'face 'company-template-field
- 'company-template-parent templ)))
- (overlay-put ov 'face 'highlight)
- (add-text-properties 0 1 '(cursor t) label)
- (overlay-put ov 'after-string label)
- ;; (overlay-put ov 'evaporate t)
- (overlay-put ov 'intangible t)
- (overlay-put ov 'company-template-parent templ)
- (overlay-put ov 'insert-in-front-hooks '(company-template-remove))
- (push ov siblings)
+(defun company-template-add-field (templ pos text &optional display)
+ "Add new field to template TEMPL at POS, inserting TEXT.
+When DISPLAY is non-nil, set the respective property on the overlay.
+Leave point at the end of the field."
+ (cl-assert templ)
+ (goto-char pos)
+ (insert text)
+ (when (> (point) (overlay-end templ))
+ (move-overlay templ (overlay-start templ) (point)))
+ (let ((ov (make-overlay pos (+ pos (length text))))
+ (siblings (overlay-get templ 'company-template-fields)))
+ ;; (overlay-put ov 'evaporate t)
+ (overlay-put ov 'intangible t)
+ (overlay-put ov 'face 'company-template-field)
+ (when display
+ (overlay-put ov 'display display))
+ (overlay-put ov 'company-template-parent templ)
+ (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
+ (push ov siblings)
+ (overlay-put templ 'company-template-fields siblings)))
+
+(defun company-template-remove-field (ovl &optional clear)
+ (when (overlayp ovl)
+ (when (overlay-buffer ovl)
+ (when clear
+ (delete-region (overlay-start ovl) (overlay-end ovl)))
+ (delete-overlay ovl))
+ (let* ((templ (overlay-get ovl 'company-template-parent))
+ (siblings (overlay-get templ 'company-template-fields)))
+ (setq siblings (delq ovl siblings))