]> code.delx.au - gnu-emacs-elpa/blob - company-template.el
Remove leading asterisks from docstrings
[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 [remap forward-word] 'company-template-forward-field)
35 (define-key keymap [remap subword-forward] 'company-template-forward-field)
36 ;; M-n
37 keymap))
38
39 ;; interactive ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40
41 (defsubst company-template-templates-at (pos)
42 (let (os)
43 (dolist (o (overlays-at pos))
44 (when (overlay-get o 'company-template-fields)
45 (push o os)))
46 os))
47
48 (defun company-template-move-to-first (templ)
49 (interactive)
50 (let ((fields (overlay-get templ 'company-template-fields)))
51 (push-mark)
52 (goto-char (apply 'min (mapcar 'overlay-start fields)))))
53
54 (defun company-template-forward-field ()
55 (interactive)
56 (let* ((templates (company-template-templates-at (point)))
57 (minimum (apply 'max (mapcar 'overlay-end templates)))
58 (fields (apply 'append
59 (mapcar (lambda (templ)
60 (overlay-get templ 'company-template-fields))
61 templates))))
62 (dolist (pos (mapcar 'overlay-start fields))
63 (and pos
64 (> pos (point))
65 (< pos minimum)
66 (setq minimum pos)))
67 (push-mark)
68 (goto-char minimum)))
69
70 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71
72 (defvar company-template--buffer-templates nil)
73 (make-variable-buffer-local 'company-template--buffer-templates)
74
75 (defun company-template-declare-template (beg end)
76 (let ((ov (make-overlay beg end)))
77 ;; (overlay-put ov 'face 'highlight)
78 (overlay-put ov 'keymap company-template-nav-map)
79 (overlay-put ov 'evaporate t)
80 (push ov company-template--buffer-templates)
81 (add-hook 'post-command-hook 'company-template-post-command nil t)
82 ov))
83
84 (defun company-template-remove-template (templ)
85 (mapc 'company-template-remove-field
86 (overlay-get templ 'company-template-fields))
87 (setq company-template--buffer-templates
88 (delq templ company-template--buffer-templates))
89 (delete-overlay templ))
90
91 (defun company-template-add-field (templ pos text)
92 (assert templ)
93 (save-excursion
94 ;; (goto-char pos)
95 (let ((ov (make-overlay pos pos))
96 (siblings (overlay-get templ 'company-template-fields))
97 (label (propertize text 'face 'company-template-field
98 'company-template-parent templ)))
99 (overlay-put ov 'face 'highlight)
100 (add-text-properties 0 1 '(cursor t) label)
101 (overlay-put ov 'after-string label)
102 ;; (overlay-put ov 'evaporate t)
103 (overlay-put ov 'intangible t)
104 (overlay-put ov 'company-template-parent templ)
105 (overlay-put ov 'insert-in-front-hooks '(company-template-remove))
106 (push ov siblings)
107 (overlay-put templ 'company-template-fields siblings))))
108
109 (defun company-template-remove-field (field)
110 (when (overlayp field)
111 ;; (delete-region (overlay-start field) (overlay-end field))
112 (delete-overlay field))
113 ;; TODO: unlink
114 )
115
116 (defun company-template-clean-up (&optional pos)
117 "Clean up all templates that don't contain POS."
118 (unless pos (setq pos (point)))
119 (let ((local-ovs (overlays-in (- pos 2) pos)))
120 (dolist (templ company-template--buffer-templates)
121 (unless (memq templ local-ovs)
122 (company-template-remove-template templ)))))
123
124 ;; hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125
126 (defun company-template-remove (overlay after-p beg end &optional r)
127 "Called when a snippet input prompt is modified."
128 (when after-p
129 (delete-overlay overlay)))
130
131 (defun company-template-post-command ()
132 (company-template-clean-up)
133 (unless company-template--buffer-templates
134 (remove-hook 'post-command-hook 'company-template-post-command t)))
135
136 (provide 'company-template)
137 ;;; company-template.el ends here