forked from timvisher/emacs-groovy-mode-mirror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroovy-electric.el
177 lines (154 loc) · 6.67 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
;;; groovy-electric.el --- Electric mode for Groovy
;; -*-Emacs-Lisp-*-
;; Author: Jim Morris <[email protected]>
;; Created: 2009-12-11
;; Copyright (C) 2009 Jim Morris
;; 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 2 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, write
;; to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;;
;; 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.
;;; 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
;;
;;
;; Usage:
;;
;; 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.
;;; History:
;;; 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)
(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-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-next-char-is-p (arg)
(if (> (buffer-size) (point))
(let ((next-char (buffer-substring (point)
(+ 1 (point)))))
(equal (char-to-string arg) next-char))
nil))
(defun groovy-electric-is-last-command-char-expandable-punct-p()
(or (memq 'all groovy-electric-expand-delimiters-list)
(memq last-command-char 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-char-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")
(if (not (groovy-electric-next-char-is-p last-command-char))
(progn (self-insert-command (prefix-numeric-value arg))
(and (groovy-electric-is-last-command-char-expandable-punct-p)
(groovy-electric-code-at-point-p)
(save-excursion
(insert (cdr (assoc last-command-char
groovy-electric-matching-delimeter-alist))))))
(goto-char (+ 1 (point)))))
(defun groovy-electric-pound(arg)
(interactive "P")
(self-insert-command (prefix-numeric-value arg))
(when (and (groovy-electric-is-last-command-char-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