]> code.delx.au - gnu-emacs-elpa/blob - company-template.el
Bump copyright
[gnu-emacs-elpa] / company-template.el
1 ;;; company-template.el --- utility library for template expansion
2
3 ;; Copyright (C) 2009, 2010, 2014-2016 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Code:
23
24 (require 'cl-lib)
25
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."
30 :group 'company)
31
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)
36 keymap))
37
38 (defvar-local company-template--buffer-templates nil)
39
40 ;; interactive ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
42 (defun company-template-templates-at (pos)
43 (let (os)
44 (dolist (o (overlays-at pos))
45 ;; FIXME: Always return the whole list of templates?
46 ;; We remove templates not at point after every command.
47 (when (memq o company-template--buffer-templates)
48 (push o os)))
49 os))
50
51 (defun company-template-move-to-first (templ)
52 (interactive)
53 (goto-char (overlay-start templ))
54 (company-template-forward-field))
55
56 (defun company-template-forward-field ()
57 (interactive)
58 (let* ((start (point))
59 (templates (company-template-templates-at (point)))
60 (minimum (apply 'max (mapcar 'overlay-end templates)))
61 (fields (cl-loop for templ in templates
62 append (overlay-get templ 'company-template-fields))))
63 (dolist (pos (mapcar 'overlay-start fields))
64 (and pos
65 (> pos (point))
66 (< pos minimum)
67 (setq minimum pos)))
68 (push-mark)
69 (goto-char minimum)
70 (company-template-remove-field (company-template-field-at start))))
71
72 (defun company-template-field-at (&optional point)
73 (cl-loop for ovl in (overlays-at (or point (point)))
74 when (overlay-get ovl 'company-template-parent)
75 return ovl))
76
77 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78
79 (defun company-template-declare-template (beg end)
80 (let ((ov (make-overlay beg end)))
81 ;; (overlay-put ov 'face 'highlight)
82 (overlay-put ov 'keymap company-template-nav-map)
83 (overlay-put ov 'priority 101)
84 (overlay-put ov 'evaporate t)
85 (push ov company-template--buffer-templates)
86 (add-hook 'post-command-hook 'company-template-post-command nil t)
87 ov))
88
89 (defun company-template-remove-template (templ)
90 (mapc 'company-template-remove-field
91 (overlay-get templ 'company-template-fields))
92 (setq company-template--buffer-templates
93 (delq templ company-template--buffer-templates))
94 (delete-overlay templ))
95
96 (defun company-template-add-field (templ beg end &optional display)
97 "Add new field to template TEMPL spanning from BEG to END.
98 When DISPLAY is non-nil, set the respective property on the overlay.
99 Leave point at the end of the field."
100 (cl-assert templ)
101 (when (> end (overlay-end templ))
102 (move-overlay templ (overlay-start templ) end))
103 (let ((ov (make-overlay beg end))
104 (siblings (overlay-get templ 'company-template-fields)))
105 ;; (overlay-put ov 'evaporate t)
106 (overlay-put ov 'intangible t)
107 (overlay-put ov 'face 'company-template-field)
108 (when display
109 (overlay-put ov 'display display))
110 (overlay-put ov 'company-template-parent templ)
111 (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
112 (push ov siblings)
113 (overlay-put templ 'company-template-fields siblings)))
114
115 (defun company-template-remove-field (ovl &optional clear)
116 (when (overlayp ovl)
117 (when (overlay-buffer ovl)
118 (when clear
119 (delete-region (overlay-start ovl) (overlay-end ovl)))
120 (delete-overlay ovl))
121 (let* ((templ (overlay-get ovl 'company-template-parent))
122 (siblings (overlay-get templ 'company-template-fields)))
123 (setq siblings (delq ovl siblings))
124 (overlay-put templ 'company-template-fields siblings))))
125
126 (defun company-template-clean-up (&optional pos)
127 "Clean up all templates that don't contain POS."
128 (let ((local-ovs (overlays-at (or pos (point)))))
129 (dolist (templ company-template--buffer-templates)
130 (unless (memq templ local-ovs)
131 (company-template-remove-template templ)))))
132
133 ;; hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134
135 (defun company-template-insert-hook (ovl after-p &rest _ignore)
136 "Called when a snippet input prompt is modified."
137 (unless after-p
138 (company-template-remove-field ovl t)))
139
140 (defun company-template-post-command ()
141 (company-template-clean-up)
142 (unless company-template--buffer-templates
143 (remove-hook 'post-command-hook 'company-template-post-command t)))
144
145 ;; common ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
146
147 (defun company-template-c-like-templatify (call)
148 (let* ((end (point-marker))
149 (beg (- (point) (length call)))
150 (templ (company-template-declare-template beg end))
151 paren-open paren-close)
152 (with-syntax-table (make-syntax-table (syntax-table))
153 (modify-syntax-entry ?< "(")
154 (modify-syntax-entry ?> ")")
155 (when (search-backward ")" beg t)
156 (setq paren-close (point-marker))
157 (forward-char 1)
158 (delete-region (point) end)
159 (backward-sexp)
160 (forward-char 1)
161 (setq paren-open (point-marker)))
162 (when (search-backward ">" beg t)
163 (let ((angle-close (point-marker)))
164 (forward-char 1)
165 (backward-sexp)
166 (forward-char)
167 (company-template--c-like-args templ angle-close)))
168 (when (looking-back "\\((\\*)\\)(" (line-beginning-position))
169 (delete-region (match-beginning 1) (match-end 1)))
170 (when paren-open
171 (goto-char paren-open)
172 (company-template--c-like-args templ paren-close)))
173 (if (overlay-get templ 'company-template-fields)
174 (company-template-move-to-first templ)
175 (company-template-remove-template templ)
176 (goto-char end))))
177
178 (defun company-template--c-like-args (templ end)
179 (let ((last-pos (point)))
180 (while (re-search-forward "\\([^,]+\\),?" end 'move)
181 (when (zerop (car (parse-partial-sexp last-pos (point))))
182 (company-template-add-field templ last-pos (match-end 1))
183 (skip-chars-forward " ")
184 (setq last-pos (point))))))
185
186 ;; objc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187
188 (defun company-template-objc-templatify (selector)
189 (let* ((end (point-marker))
190 (beg (- (point) (length selector) 1))
191 (templ (company-template-declare-template beg end))
192 (cnt 0))
193 (save-excursion
194 (goto-char beg)
195 (catch 'stop
196 (while (search-forward ":" end t)
197 (if (looking-at "\\(([^)]*)\\) ?")
198 (company-template-add-field templ (point) (match-end 1))
199 ;; Not sure which conditions this case manifests under, but
200 ;; apparently it did before, when I wrote the first test for this
201 ;; function. FIXME: Revisit it.
202 (company-template-add-field templ (point)
203 (progn
204 (insert (format "arg%d" cnt))
205 (point)))
206 (when (< (point) end)
207 (insert " "))
208 (cl-incf cnt))
209 (when (>= (point) end)
210 (throw 'stop t)))))
211 (company-template-move-to-first templ)))
212
213 (provide 'company-template)
214 ;;; company-template.el ends here