]> code.delx.au - gnu-emacs-elpa/blob - company-template.el
company-template-c-like-templatify: Remove text after the closing paren
[gnu-emacs-elpa] / company-template.el
1 ;;; company-template.el
2
3 ;; Copyright (C) 2009, 2010 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 (eval-when-compile (require 'cl))
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 keymap))
36
37 ;; interactive ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38
39 (defun company-template-templates-at (pos)
40 (let (os)
41 (dolist (o (overlays-at pos))
42 ;; FIXME: Always return the whole list of templates?
43 ;; We remove templates not at point after every command.
44 (when (memq o company-template--buffer-templates)
45 (push o os)))
46 os))
47
48 (defun company-template-move-to-first (templ)
49 (interactive)
50 (goto-char (overlay-start templ))
51 (company-template-forward-field))
52
53 (defun company-template-forward-field ()
54 (interactive)
55 (let* ((start (point))
56 (templates (company-template-templates-at (point)))
57 (minimum (apply 'max (mapcar 'overlay-end templates)))
58 (fields (loop for templ in templates
59 append (overlay-get templ 'company-template-fields))))
60 (dolist (pos (mapcar 'overlay-start fields))
61 (and pos
62 (> pos (point))
63 (< pos minimum)
64 (setq minimum pos)))
65 (push-mark)
66 (goto-char minimum)
67 (company-template-remove-field (company-template-field-at start))))
68
69 (defun company-template-field-at (&optional point)
70 (loop for ovl in (overlays-at (or point (point)))
71 when (overlay-get ovl 'company-template-parent)
72 return ovl))
73
74 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75
76 (defvar company-template--buffer-templates nil)
77 (make-variable-buffer-local 'company-template--buffer-templates)
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 pos text &optional display)
97 "Add new field to template TEMPL at POS, inserting TEXT.
98 When DISPLAY is non-nil, set the respective property on the overlay.
99 Leave point at the end of the field."
100 (assert templ)
101 (goto-char pos)
102 (insert text)
103 (when (> (point) (overlay-end templ))
104 (move-overlay templ (overlay-start templ) (point)))
105 (let ((ov (make-overlay pos (+ pos (length text))))
106 (siblings (overlay-get templ 'company-template-fields)))
107 ;; (overlay-put ov 'evaporate t)
108 (overlay-put ov 'intangible t)
109 (overlay-put ov 'face 'company-template-field)
110 (when display
111 (overlay-put ov 'display display))
112 (overlay-put ov 'company-template-parent templ)
113 (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
114 (push ov siblings)
115 (overlay-put templ 'company-template-fields siblings)))
116
117 (defun company-template-remove-field (ovl &optional clear)
118 (when (overlayp ovl)
119 (when (overlay-buffer ovl)
120 (when clear
121 (delete-region (overlay-start ovl) (overlay-end ovl)))
122 (delete-overlay ovl))
123 (let* ((templ (overlay-get ovl 'company-template-parent))
124 (siblings (overlay-get templ 'company-template-fields)))
125 (setq siblings (delq ovl siblings))
126 (overlay-put templ 'company-template-fields siblings))))
127
128 (defun company-template-clean-up (&optional pos)
129 "Clean up all templates that don't contain POS."
130 (let ((local-ovs (overlays-at (or pos (point)))))
131 (dolist (templ company-template--buffer-templates)
132 (unless (memq templ local-ovs)
133 (company-template-remove-template templ)))))
134
135 ;; hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136
137 (defun company-template-insert-hook (ovl after-p &rest ignore)
138 "Called when a snippet input prompt is modified."
139 (unless after-p
140 (company-template-remove-field ovl t)))
141
142 (defun company-template-post-command ()
143 (company-template-clean-up)
144 (unless company-template--buffer-templates
145 (remove-hook 'post-command-hook 'company-template-post-command t)))
146
147 ;; common ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148
149 (defun company-template-c-like-templatify (call)
150 (let* ((end (point-marker))
151 (beg (- (point) (length call)))
152 (cnt 0))
153 (when (re-search-backward ")" beg t)
154 (delete-region (match-end 0) end))
155 (goto-char beg)
156 (when (search-forward "(" end 'move)
157 (if (eq (char-after) ?\))
158 (forward-char 1)
159 (let ((templ (company-template-declare-template beg end)))
160 (while (re-search-forward (concat " *\\([^,)]*\\)[,)]") end t)
161 (let ((sig (match-string 1)))
162 (delete-region (match-beginning 1) (match-end 1))
163 (save-excursion
164 (company-template-add-field templ (match-beginning 1)
165 (format "arg%d" cnt) sig))
166 (incf cnt)))
167 (company-template-move-to-first templ))))))
168
169 (provide 'company-template)
170 ;;; company-template.el ends here