Klimi's new dotfiles with stow.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

272 rader
11 KiB

4 år sedan
  1. ;;; company-template.el --- utility library for template expansion
  2. ;; Copyright (C) 2009, 2010, 2014-2017 Free Software Foundation, Inc.
  3. ;; Author: Nikolaj Schumacher
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'cl-lib)
  17. (defface company-template-field
  18. '((((background dark)) (:background "yellow" :foreground "black"))
  19. (((background light)) (:background "orange" :foreground "black")))
  20. "Face used for editable text in template fields."
  21. :group 'company)
  22. (defvar company-template-forward-field-item
  23. '(menu-item "" company-template-forward-field
  24. :filter company-template--keymap-filter))
  25. (defvar company-template-nav-map
  26. (let ((keymap (make-sparse-keymap)))
  27. (define-key keymap [tab] company-template-forward-field-item)
  28. (define-key keymap (kbd "TAB") company-template-forward-field-item)
  29. keymap))
  30. (defvar company-template-clear-field-item
  31. '(menu-item "" company-template-clear-field
  32. :filter company-template--keymap-filter))
  33. (defvar company-template-field-map
  34. (let ((keymap (make-sparse-keymap)))
  35. (set-keymap-parent keymap company-template-nav-map)
  36. (define-key keymap (kbd "C-d") company-template-clear-field-item)
  37. keymap))
  38. (defvar-local company-template--buffer-templates nil)
  39. ;; interactive ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40. (defun company-template-templates-at (pos)
  41. (let (os)
  42. (dolist (o (overlays-at pos))
  43. ;; FIXME: Always return the whole list of templates?
  44. ;; We remove templates not at point after every command.
  45. (when (memq o company-template--buffer-templates)
  46. (push o os)))
  47. os))
  48. (defun company-template-move-to-first (templ)
  49. (interactive)
  50. (goto-char (overlay-start templ))
  51. (company-template-forward-field))
  52. (defun company-template-forward-field ()
  53. (interactive)
  54. (let ((start (point))
  55. (next-field-start (company-template-find-next-field)))
  56. (push-mark)
  57. (goto-char next-field-start)
  58. (company-template-remove-field (company-template-field-at start))))
  59. (defun company-template-clear-field ()
  60. "Clear the field at point."
  61. (interactive)
  62. (let ((ovl (company-template-field-at (point))))
  63. (when ovl
  64. (company-template-remove-field ovl t)
  65. (let ((after-clear-fn
  66. (overlay-get ovl 'company-template-after-clear)))
  67. (when (functionp after-clear-fn)
  68. (funcall after-clear-fn))))))
  69. (defun company-template--keymap-filter (cmd)
  70. (unless (run-hook-with-args-until-success 'yas-keymap-disable-hook)
  71. cmd))
  72. (defun company-template--after-clear-c-like-field ()
  73. "Function that can be called after deleting a field of a c-like template.
  74. For c-like templates it is set as `after-post-fn' property on fields in
  75. `company-template-add-field'. If there is a next field, delete everything
  76. from point to it. If there is no field after point, remove preceding comma
  77. if present."
  78. (let* ((pos (point))
  79. (next-field-start (company-template-find-next-field))
  80. (last-field-p (not (company-template-field-at next-field-start))))
  81. (cond ((and (not last-field-p)
  82. (< pos next-field-start)
  83. (string-match "^[ ]*,+[ ]*$" (buffer-substring-no-properties
  84. pos next-field-start)))
  85. (delete-region pos next-field-start))
  86. ((and last-field-p
  87. (looking-back ",+[ ]*" (line-beginning-position)))
  88. (delete-region (match-beginning 0) pos)))))
  89. (defun company-template-find-next-field ()
  90. (let* ((start (point))
  91. (templates (company-template-templates-at start))
  92. (minimum (apply 'max (mapcar 'overlay-end templates)))
  93. (fields (cl-loop for templ in templates
  94. append (overlay-get templ 'company-template-fields))))
  95. (dolist (pos (mapcar 'overlay-start fields) minimum)
  96. (and pos
  97. (> pos start)
  98. (< pos minimum)
  99. (setq minimum pos)))))
  100. (defun company-template-field-at (&optional point)
  101. (cl-loop for ovl in (overlays-at (or point (point)))
  102. when (overlay-get ovl 'company-template-parent)
  103. return ovl))
  104. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  105. (defun company-template-declare-template (beg end)
  106. (let ((ov (make-overlay beg end)))
  107. ;; (overlay-put ov 'face 'highlight)
  108. (overlay-put ov 'keymap company-template-nav-map)
  109. (overlay-put ov 'priority 101)
  110. (overlay-put ov 'evaporate t)
  111. (push ov company-template--buffer-templates)
  112. (add-hook 'post-command-hook 'company-template-post-command nil t)
  113. ov))
  114. (defun company-template-remove-template (templ)
  115. (mapc 'company-template-remove-field
  116. (overlay-get templ 'company-template-fields))
  117. (setq company-template--buffer-templates
  118. (delq templ company-template--buffer-templates))
  119. (delete-overlay templ))
  120. (defun company-template-add-field (templ beg end &optional display after-clear-fn)
  121. "Add new field to template TEMPL spanning from BEG to END.
  122. When DISPLAY is non-nil, set the respective property on the overlay.
  123. Leave point at the end of the field.
  124. AFTER-CLEAR-FN is a function that can be used to apply custom behavior
  125. after deleting a field in `company-template-remove-field'."
  126. (cl-assert templ)
  127. (when (> end (overlay-end templ))
  128. (move-overlay templ (overlay-start templ) end))
  129. (let ((ov (make-overlay beg end))
  130. (siblings (overlay-get templ 'company-template-fields)))
  131. ;; (overlay-put ov 'evaporate t)
  132. (overlay-put ov 'intangible t)
  133. (overlay-put ov 'face 'company-template-field)
  134. (when display
  135. (overlay-put ov 'display display))
  136. (overlay-put ov 'company-template-parent templ)
  137. (overlay-put ov 'insert-in-front-hooks '(company-template-insert-hook))
  138. (when after-clear-fn
  139. (overlay-put ov 'company-template-after-clear after-clear-fn))
  140. (overlay-put ov 'keymap company-template-field-map)
  141. (overlay-put ov 'priority 101)
  142. (push ov siblings)
  143. (overlay-put templ 'company-template-fields siblings)))
  144. (defun company-template-remove-field (ovl &optional clear)
  145. (when (overlayp ovl)
  146. (when (overlay-buffer ovl)
  147. (when clear
  148. (delete-region (overlay-start ovl) (overlay-end ovl)))
  149. (delete-overlay ovl))
  150. (let* ((templ (overlay-get ovl 'company-template-parent))
  151. (siblings (overlay-get templ 'company-template-fields)))
  152. (setq siblings (delq ovl siblings))
  153. (overlay-put templ 'company-template-fields siblings))))
  154. (defun company-template-clean-up (&optional pos)
  155. "Clean up all templates that don't contain POS."
  156. (let ((local-ovs (overlays-at (or pos (point)))))
  157. (dolist (templ company-template--buffer-templates)
  158. (unless (memq templ local-ovs)
  159. (company-template-remove-template templ)))))
  160. ;; hooks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  161. (defun company-template-insert-hook (ovl after-p &rest _ignore)
  162. "Called when a snippet input prompt is modified."
  163. (unless after-p
  164. (company-template-remove-field ovl t)))
  165. (defun company-template-post-command ()
  166. (company-template-clean-up)
  167. (unless company-template--buffer-templates
  168. (remove-hook 'post-command-hook 'company-template-post-command t)))
  169. ;; common ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  170. (defun company-template-c-like-templatify (call)
  171. (let* ((end (point-marker))
  172. (beg (- (point) (length call)))
  173. (templ (company-template-declare-template beg end))
  174. paren-open paren-close)
  175. (with-syntax-table (make-syntax-table (syntax-table))
  176. (modify-syntax-entry ?< "(")
  177. (modify-syntax-entry ?> ")")
  178. (when (search-backward ")" beg t)
  179. (setq paren-close (point-marker))
  180. (forward-char 1)
  181. (delete-region (point) end)
  182. (backward-sexp)
  183. (forward-char 1)
  184. (setq paren-open (point-marker)))
  185. (when (search-backward ">" beg t)
  186. (let ((angle-close (point-marker)))
  187. (forward-char 1)
  188. (backward-sexp)
  189. (forward-char)
  190. (company-template--c-like-args templ angle-close)))
  191. (when (looking-back "\\((\\*)\\)(" (line-beginning-position))
  192. (delete-region (match-beginning 1) (match-end 1)))
  193. (when paren-open
  194. (goto-char paren-open)
  195. (company-template--c-like-args templ paren-close)))
  196. (if (overlay-get templ 'company-template-fields)
  197. (company-template-move-to-first templ)
  198. (company-template-remove-template templ)
  199. (goto-char end))))
  200. (defun company-template--c-like-args (templ end)
  201. (let ((last-pos (point)))
  202. (while (re-search-forward "\\([^,]+\\),?" end 'move)
  203. (when (zerop (car (parse-partial-sexp last-pos (point))))
  204. (company-template-add-field templ last-pos (match-end 1) nil
  205. #'company-template--after-clear-c-like-field)
  206. (skip-chars-forward " ")
  207. (setq last-pos (point))))))
  208. ;; objc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  209. (defun company-template-objc-templatify (selector)
  210. (let* ((end (point-marker))
  211. (beg (- (point) (length selector) 1))
  212. (templ (company-template-declare-template beg end))
  213. (cnt 0))
  214. (save-excursion
  215. (goto-char beg)
  216. (catch 'stop
  217. (while (search-forward ":" end t)
  218. (if (looking-at "\\(([^)]*)\\) ?")
  219. (company-template-add-field templ (point) (match-end 1))
  220. ;; Not sure which conditions this case manifests under, but
  221. ;; apparently it did before, when I wrote the first test for this
  222. ;; function. FIXME: Revisit it.
  223. (company-template-add-field templ (point)
  224. (progn
  225. (insert (format "arg%d" cnt))
  226. (point)))
  227. (when (< (point) end)
  228. (insert " "))
  229. (cl-incf cnt))
  230. (when (>= (point) end)
  231. (throw 'stop t)))))
  232. (company-template-move-to-first templ)))
  233. (provide 'company-template)
  234. ;;; company-template.el ends here