1 ;;; company-template.el
3 ;; Copyright (C) 2009, 2010, 2013 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 (eval-when-compile (require 'cl))
26 (defface company-template-field
27 '((((background dark)) (:background "yellow" :foreground "black"))
28 (((background light)) (:background "orange" :foreground "black")))
29 "Face used for editable text in template fields."
32 (defvar company-template-nav-map
33 (let ((keymap (make-sparse-keymap)))
34 (define-key keymap [tab] 'company-template-forward-field)
35 (define-key keymap (kbd "TAB") 'company-template-forward-field)
38 (defvar company-template--buffer-templates nil)
39 (make-variable-buffer-local 'company-template--buffer-templates)
41 ;; interactive ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 (defun company-template-templates-at (pos)
45 (dolist (o (overlays-at pos))
46 ;; FIXME: Always return the whole list of templates?
47 ;; We remove templates not at point after every command.
48 (when (memq o company-template--buffer-templates)
52 (defun company-template-move-to-first (templ)
54 (goto-char (overlay-start templ))
55 (company-template-forward-field))
57 (defun company-template-forward-field ()
59 (let* ((start (point))
60 (templates (company-template-templates-at (point)))
61 (minimum (apply 'max (mapcar 'overlay-end templates)))
62 (fields (loop for templ in templates
63 append (overlay-get templ 'company-template-fields))))
64 (dolist (pos (mapcar 'overlay-start fields))
71 (company-template-remove-field (company-template-field-at start))))
73 (defun company-template-field-at (&optional point)
74 (loop for ovl in (overlays-at (or point (point)))
75 when (overlay-get ovl 'company-template-parent)
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 (defun company-template-declare-template (beg end)
81 (let ((ov (make-overlay beg end)))
82 ;; (overlay-put ov 'face 'highlight)
83 (overlay-put ov 'keymap company-template-nav-map)
84 (overlay-put ov 'priority 101)
85 (overlay-put ov 'evaporate t)
86 (push ov company-template--buffer-templates)
87 (add-hook 'post-command-hook 'company-template-post-command nil t)
90 (defun company-template-remove-template (templ)
91 (mapc 'company-template-remove-field
92 (overlay-get templ 'company-template-fields))
93 (setq company-template--buffer-templates
94 (delq templ company-template--buffer-templates))
95 (delete-overlay templ))
97 (defun company-template-add-field (templ pos text &optional display)
98 "Add new field to template TEMPL at POS, inserting TEXT.
99 When DISPLAY is non-nil, set the respective property on the overlay.
100 Leave point at the end of the field."
104 (when (> (point) (overlay-end templ))
105 (move-overlay templ (overlay-start templ) (point)))
106 (let ((ov (make-overlay pos (+ pos (length text))))
107 (siblings (overlay-get templ 'company-template-fields)))
108 ;; (overlay-put ov 'evaporate t)
109 (overlay-put ov 'intangible t)
110 (overlay-put ov 'face 'company-template-field)
112 (overlay-put ov 'display display))
113 (overlay-put ov 'company-template-parent templ)
114 (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
116 (overlay-put templ 'company-template-fields siblings)))
118 (defun company-template-remove-field (ovl &optional clear)
120 (when (overlay-buffer ovl)
122 (delete-region (overlay-start ovl) (overlay-end ovl)))
123 (delete-overlay ovl))
124 (let* ((templ (overlay-get ovl 'company-template-parent))
125 (siblings (overlay-get templ 'company-template-fields)))
126 (setq siblings (delq ovl siblings))
127 (overlay-put templ 'company-template-fields siblings))))
129 (defun company-template-clean-up (&optional pos)
130 "Clean up all templates that don't contain POS."
131 (let ((local-ovs (overlays-at (or pos (point)))))
132 (dolist (templ company-template--buffer-templates)
133 (unless (memq templ local-ovs)
134 (company-template-remove-template templ)))))
136 ;; hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 (defun company-template-insert-hook (ovl after-p &rest _ignore)
139 "Called when a snippet input prompt is modified."
141 (company-template-remove-field ovl t)))
143 (defun company-template-post-command ()
144 (company-template-clean-up)
145 (unless company-template--buffer-templates
146 (remove-hook 'post-command-hook 'company-template-post-command t)))
148 ;; common ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 (defun company-template-c-like-templatify (call)
151 (let* ((end (point-marker))
152 (beg (- (point) (length call)))
154 (when (re-search-backward ")" beg t)
155 (delete-region (match-end 0) end))
157 (when (search-forward "(" end 'move)
158 (if (eq (char-after) ?\))
160 (let ((templ (company-template-declare-template beg end)))
161 (while (re-search-forward (concat " *\\([^,)]*\\)[,)]") end t)
162 (let ((sig (match-string 1)))
163 (delete-region (match-beginning 1) (match-end 1))
165 (company-template-add-field templ (match-beginning 1)
166 (format "arg%d" cnt) sig))
168 (company-template-move-to-first templ))))))
170 (provide 'company-template)
171 ;;; company-template.el ends here