-
Notifications
You must be signed in to change notification settings - Fork 39
/
groovy-electric.el
193 lines (162 loc) · 6.99 KB
/
groovy-electric.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
;;; groovy-electric.el --- Electric mode for Groovy
;; Copyright (C) 2009 Jim Morris
;; Author: Jim Morris <[email protected]>
;; Created: 2009-12-11
;; Maintainer: Russel Winder <[email protected]>
;; Version: 201605040906
;; Keywords: languages
;;;; NB Version number is date and time yyyymmddhhMM UTC.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; Based on ruby-electric.el Copyright (C) 2005 by Dee Zsombor <dee dot zsombor at gmail dot com>.
;; Due credit: original work was inspired by a code snippet posted by
;; Frederick Ros at http://rubygarden.org/ruby?EmacsExtensions.
;;; Usage:
;; If you install using the packaging system no further set up should be needed. If you install this mode
;; manually then you will likely need to add some extra lines to your init file:
;;
;; 0) copy groovy-electric.el into directory where emacs can find it.
;;
;; 1) modify your startup file (.emacs or whatever) by adding
;; following lines to load and enable the mode when groovy-mode loads
;;
;; (add-hook 'groovy-mode-hook
;; '(lambda ()
;; (require 'groovy-electric)
;; (groovy-electric-mode)))
;;
;; or add this to your init file
;;
;; (require 'groovy-electric)
;;
;; note that you need to have font lock enabled beforehand.
;;
;; 2) toggle Groovy Electric Mode on/off with groovy-electric-mode.
;;; Commentary:
;; By default automatically inserts closing delimiter for {[('"
;; Additionally when in a GString typing a $ will insert { } and place
;; cursor between the braces. All these can be turned on or off
;; individually in the customization window for groovy-electric
;;; Bugs:
;; Bug tracking is currently handled using the GitHub issue tracker at
;; https://github.com/Groovy-Emacs-Modes/groovy-emacs-modes/issues
;;; Versions:
;; This mode is available on MELPA which tracks the mainline Git repository on GitHub, so there is a rolling release
;; system based on commits to the mainline.
;;; Notes:
;;; TODO:
;; Issues with this code are managed via the project issue management
;; on GitHub: https://github.com/Groovy-Emacs-Modes/groovy-emacs-modes/issues?state=open
;;; History:
;; History is tracked in the Git repository rather than in this file.
;; See https://github.com/Groovy-Emacs-Modes/groovy-emacs-modes/commits/master
;;----------------------------------------------------------------------------
;;; Code:
(require 'groovy-mode)
(defgroup groovy-electric nil
"Minor mode providing electric editing commands for groovy files"
:group 'groovy)
(defvar groovy-electric-matching-delimeter-alist
'((?\[ . ?\])
(?\( . ?\))
(?\' . ?\')
(?\" . ?\")))
(defcustom groovy-electric-expand-delimiters-list '(all)
"*List of contexts where matching delimiter should be inserted.
The word 'all' will do all insertions."
:type '(set :extra-offset 8
(const :tag "Everything" all )
(const :tag "Curly brace" ?\{ )
(const :tag "Square brace" ?\[ )
(const :tag "Round brace" ?\( )
(const :tag "Quote" ?\' )
(const :tag "Double quote" ?\" )
(const :tag "Dollar in GStrings" ?\$ ))
:group 'groovy-electric)
(defcustom groovy-electric-newline-before-closing-bracket nil
"*Controls whether a newline should be inserted before the
closing bracket or not."
:type 'boolean :group 'groovy-electric)
;;;###autoload
(define-minor-mode groovy-electric-mode
"Toggle Groovy Electric minor mode.
With no argument, this command toggles the mode. Non-null prefix
argument turns on the mode. Null prefix argument turns off the
mode.
When Groovy Electric mode is enabled, simple, double and back
quotes as well as braces are paired auto-magically. Expansion
does not occur inside comments and strings. Note that you must
have Font Lock enabled. ${ } is expanded when in a GString"
;; initial value.
nil
;;indicator for the mode line.
" Ge"
;;keymap
groovy-mode-map
(groovy-electric-setup-keymap))
(defun groovy-electric-setup-keymap()
(define-key groovy-mode-map "{" 'groovy-electric-curlies)
(define-key groovy-mode-map "(" 'groovy-electric-matching-char)
(define-key groovy-mode-map "[" 'groovy-electric-matching-char)
(define-key groovy-mode-map "\"" 'groovy-electric-matching-char)
(define-key groovy-mode-map "\'" 'groovy-electric-matching-char)
(define-key groovy-mode-map "\$" 'groovy-electric-pound)
)
(defun groovy-electric-code-at-point-p()
(and groovy-electric-mode
(let* ((properties (text-properties-at (point))))
(and (null (memq 'font-lock-string-face properties))
(null (memq 'font-lock-comment-face properties))))))
(defun groovy-electric-string-at-point-p()
(and groovy-electric-mode
(consp (memq 'font-lock-string-face (text-properties-at (point))))))
;; This checks it is a GString ("...") not normal string '...'
(defun groovy-electric-gstring-at-point-p()
(and groovy-electric-mode
(consp (memq 'font-lock-string-face (text-properties-at (point))))
(save-excursion
(char-equal ?\" (char-after (car (c-literal-limits)))))))
(defun groovy-electric-is-last-command-event-expandable-punct-p()
(or (memq 'all groovy-electric-expand-delimiters-list)
(memq last-command-event groovy-electric-expand-delimiters-list)))
(defun groovy-electric-curlies(arg)
(interactive "P")
(self-insert-command (prefix-numeric-value arg))
(when (and (groovy-electric-is-last-command-event-expandable-punct-p)
(groovy-electric-code-at-point-p))
(insert " ")
(save-excursion
(if groovy-electric-newline-before-closing-bracket
(newline))
(insert "}"))))
(defun groovy-electric-matching-char(arg)
(interactive "P")
(self-insert-command (prefix-numeric-value arg))
(and (groovy-electric-is-last-command-event-expandable-punct-p)
(groovy-electric-code-at-point-p)
(save-excursion
(insert (cdr (assoc last-command-event
groovy-electric-matching-delimeter-alist))))))
(defun groovy-electric-pound(arg)
(interactive "P")
(self-insert-command (prefix-numeric-value arg))
(when (and (groovy-electric-is-last-command-event-expandable-punct-p)
(groovy-electric-gstring-at-point-p)
(not (save-excursion ; make sure it is not escaped
(backward-char 1)
(char-equal ?\\ (preceding-char)))))
(insert "{}")
(backward-char 1)))
;;----------------------------------------------------------------------------
(provide 'groovy-electric)
;;; groovy-electric.el ends here