Merge branch 'master' of git.ganneff.de:emacs
[emacs.git] / .emacs.d / elisp / local / puppet-ext.el
1 ;;; puppet-ext --- Extensions to puppet.el
2
3 ;; Copyright (C) 2012 John Wiegley
4
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 18 Jul 2012
7 ;; Version: 1.0
8 ;; Keywords: ruby puppet
9 ;; X-URL: https://github.com/jwiegley/dot-emacs
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 \f
26 ;;; Commentary:
27
28 ;; Extensions to puppet.el.
29
30 (require 'align)
31 (require 'puppet-mode)
32
33 (eval-when-compile
34 (require 'cl))
35
36 (defgroup puppet-ext nil
37 "Extensions to puppet.el."
38 :group 'puppet)
39
40 (add-to-list 'align-rules-list
41 '(ruby-arrow
42 (regexp . "\\(\\s-*\\)=>\\(\\s-*\\)")
43 (group . (1 2))
44 (modes . '(ruby-mode puppet-mode))))
45
46 (defvar puppet-anchor-point nil)
47
48 (defun puppet-set-anchor ()
49 (interactive)
50 (setq puppet-anchor-point (point-marker))
51 (message "puppet-mode anchor set at %s"
52 (marker-position puppet-anchor-point)))
53
54 (defun puppet-resource-beginning ()
55 (save-excursion
56 (and (re-search-backward
57 "^\\s-*\\(\\S-+\\)\\s-+{\\s-+\\([^:]+\\):" nil t)
58 (list (match-beginning 0)
59 (match-string 1) (match-string 2)))))
60
61 (defun puppet-resource-end ()
62 (save-excursion
63 (and (re-search-forward "^\\s-*}" nil t)
64 (match-end 0))))
65
66 (defun puppet-create-require ()
67 (interactive)
68 (require 'align)
69 (if (null puppet-anchor-point)
70 (error "Anchor point has not been set")
71 (destructuring-bind (anchored-start resource name)
72 (save-excursion
73 (goto-char puppet-anchor-point)
74 (puppet-resource-beginning))
75 (save-excursion
76 (let ((beginning (car (puppet-resource-beginning)))
77 (end (puppet-resource-end)))
78 (goto-char end)
79 (backward-char)
80 (let ((current-requires
81 (when (re-search-backward
82 "^\\s-*require\\s-*=>\\s-*" beginning t)
83 (let ((start (match-beginning 0))
84 (beg (match-end 0)))
85 (if (looking-at "\\[")
86 (forward-sexp))
87 (re-search-forward "\\([,;]\\)?[ \t]*\n")
88 (prog1
89 (buffer-substring-no-properties
90 beg (match-beginning 0))
91 (delete-region start (point)))))))
92 (save-excursion
93 (skip-chars-backward " \t\n\r")
94 (when (looking-back ";")
95 (delete-char -1)
96 (insert ?,)))
97 (insert " require => ")
98 (if current-requires
99 (insert "[ " current-requires ", "))
100 (insert (capitalize (substring resource 0 1))
101 (substring resource 1) "[" name "]")
102 (if current-requires
103 (insert " ]"))
104 (insert ";\n")
105 (mark-paragraph)
106 (align-code (region-beginning) (region-end))))))))
107
108 (define-key puppet-mode-map [(control ?x) ? ] 'puppet-set-anchor)
109 (define-key puppet-mode-map [(control ?x) space] 'puppet-set-anchor)
110
111 (define-key puppet-mode-map [(control ?c) (control ?r)] 'puppet-create-require)
112
113 (provide 'puppet-ext)
114
115 ;;; puppet-ext.el ends here