]> code.delx.au - gnu-emacs-elpa/blob - company-template.el
Merge pull request #101 from bbatsov/master
[gnu-emacs-elpa] / company-template.el
1 ;;; company-template.el
2
3 ;; Copyright (C) 2009, 2010, 2013 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 company-template--buffer-templates nil)
39 (make-variable-buffer-local 'company-template--buffer-templates)
40
41 ;; interactive ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42
43 (defun company-template-templates-at (pos)
44 (let (os)
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)
49 (push o os)))
50 os))
51
52 (defun company-template-move-to-first (templ)
53 (interactive)
54 (goto-char (overlay-start templ))
55 (company-template-forward-field))
56
57 (defun company-template-forward-field ()
58 (interactive)
59 (let* ((start (point))
60 (templates (company-template-templates-at (point)))
61 (minimum (apply 'max (mapcar 'overlay-end templates)))
62 (fields (cl-loop for templ in templates
63 append (overlay-get templ 'company-template-fields))))
64 (dolist (pos (mapcar 'overlay-start fields))
65 (and pos
66 (> pos (point))
67 (< pos minimum)
68 (setq minimum pos)))
69 (push-mark)
70 (goto-char minimum)
71 (company-template-remove-field (company-template-field-at start))))
72
73 (defun company-template-field-at (&optional point)
74 (cl-loop for ovl in (overlays-at (or point (point)))
75 when (overlay-get ovl 'company-template-parent)
76 return ovl))
77
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79
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)
88 ov))
89
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))
96
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."
101 (cl-assert templ)
102 (goto-char pos)
103 (insert text)
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)
111 (when display
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))
115 (push ov siblings)
116 (overlay-put templ 'company-template-fields siblings)))
117
118 (defun company-template-remove-field (ovl &optional clear)
119 (when (overlayp ovl)
120 (when (overlay-buffer ovl)
121 (when clear
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))))
128
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)))))
135
136 ;; hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137
138 (defun company-template-insert-hook (ovl after-p &rest _ignore)
139 "Called when a snippet input prompt is modified."
140 (unless after-p
141 (company-template-remove-field ovl t)))
142
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)))
147
148 ;; common ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149
150 (defun company-template-c-like-templatify (call)
151 (let* ((end (point-marker))
152 (beg (- (point) (length call)))
153 (cnt 0))
154 (when (re-search-backward ")" beg t)
155 (delete-region (match-end 0) end))
156 (goto-char beg)
157 (when (search-forward "(" end 'move)
158 (if (eq (char-after) ?\))
159 (forward-char 1)
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))
164 (save-excursion
165 (company-template-add-field templ (match-beginning 1)
166 (format "arg%d" cnt) sig))
167 (cl-incf cnt)))
168 (company-template-move-to-first templ))))))
169
170 (provide 'company-template)
171 ;;; company-template.el ends here