comparison .emacs.d/groovy-electric.el @ 0:2764b4f45f9f

1st commit
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Mon, 21 Apr 2014 04:30:59 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2764b4f45f9f
1 ;;; groovy-electric.el --- Electric mode for Groovy
2 ;; -*-Emacs-Lisp-*-
3
4 ;; Author: Jim Morris <morris@wolfman.com>
5 ;; Created: 2009-12-11
6
7 ;; Copyright (C) 2009 Jim Morris
8
9 ;; This program is free software; you can redistribute it and/or modify it under the terms of the GNU
10 ;; General Public License as published by the Free Software Foundation; either version 2 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
14 ;; the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 ;; License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License along with this program; if not, write
18 ;; to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ;;
20 ;; Based on ruby-electric.el Copyright (C) 2005 by Dee Zsombor <dee dot zsombor at gmail dot com>.
21 ;; Due credit: original work was inspired by a code snippet posted by
22 ;; Frederick Ros at http://rubygarden.org/ruby?EmacsExtensions.
23
24 ;;; Commentary:
25 ;;
26 ;; By default automatically inserts closing delimiter for {[('"
27 ;; Additionally when in a GString typing a $ will insert { } and place
28 ;; cursor between the braces. All these can be turned on or off
29 ;; individually in the customization window for groovy-electric
30 ;;
31 ;;
32 ;; Usage:
33 ;;
34 ;; 0) copy groovy-electric.el into directory where emacs can find it.
35 ;;
36 ;; 1) modify your startup file (.emacs or whatever) by adding
37 ;; following lines to load and enable the mode when groovy-mode loads
38 ;;
39 ;; (add-hook 'groovy-mode-hook
40 ;; '(lambda ()
41 ;; (require 'groovy-electric)
42 ;; (groovy-electric-mode)))
43 ;;
44 ;; or add this to your init file
45 ;;
46 ;; (require 'groovy-electric)
47 ;;
48 ;; note that you need to have font lock enabled beforehand.
49 ;;
50 ;; 2) toggle Groovy Electric Mode on/off with groovy-electric-mode.
51
52 ;;; History:
53
54 ;;; Code:
55 (require 'groovy-mode)
56 (defgroup groovy-electric nil
57 "Minor mode providing electric editing commands for groovy files"
58 :group 'groovy)
59
60 (defvar groovy-electric-matching-delimeter-alist
61 '((?\[ . ?\])
62 (?\( . ?\))
63 (?\' . ?\')
64 (?\" . ?\")))
65
66 (defcustom groovy-electric-expand-delimiters-list '(all)
67 "*List of contexts where matching delimiter should be inserted.
68 The word 'all' will do all insertions."
69 :type '(set :extra-offset 8
70 (const :tag "Everything" all )
71 (const :tag "Curly brace" ?\{ )
72 (const :tag "Square brace" ?\[ )
73 (const :tag "Round brace" ?\( )
74 (const :tag "Quote" ?\' )
75 (const :tag "Double quote" ?\" )
76 (const :tag "Dollar in GStrings" ?\$ ))
77 :group 'groovy-electric)
78
79 (defcustom groovy-electric-newline-before-closing-bracket nil
80 "*Controls whether a newline should be inserted before the
81 closing bracket or not."
82 :type 'boolean :group 'groovy-electric)
83
84 (define-minor-mode groovy-electric-mode
85 "Toggle Groovy Electric minor mode.
86 With no argument, this command toggles the mode. Non-null prefix
87 argument turns on the mode. Null prefix argument turns off the
88 mode.
89
90 When Groovy Electric mode is enabled, simple, double and back
91 quotes as well as braces are paired auto-magically. Expansion
92 does not occur inside comments and strings. Note that you must
93 have Font Lock enabled. ${ } is expanded when in a GString"
94 ;; initial value.
95 nil
96 ;;indicator for the mode line.
97 " Ge"
98 ;;keymap
99 groovy-mode-map
100 (groovy-electric-setup-keymap))
101
102 (defun groovy-electric-setup-keymap()
103 (define-key groovy-mode-map "{" 'groovy-electric-curlies)
104 (define-key groovy-mode-map "(" 'groovy-electric-matching-char)
105 (define-key groovy-mode-map "[" 'groovy-electric-matching-char)
106 (define-key groovy-mode-map "\"" 'groovy-electric-matching-char)
107 (define-key groovy-mode-map "\'" 'groovy-electric-matching-char)
108 (define-key groovy-mode-map "\$" 'groovy-electric-pound)
109 )
110
111 (defun groovy-electric-code-at-point-p()
112 (and groovy-electric-mode
113 (let* ((properties (text-properties-at (point))))
114 (and (null (memq 'font-lock-string-face properties))
115 (null (memq 'font-lock-comment-face properties))))))
116
117 (defun groovy-electric-string-at-point-p()
118 (and groovy-electric-mode
119 (consp (memq 'font-lock-string-face (text-properties-at (point))))))
120
121 ;; This checks it is a GString ("...") not normal string '...'
122 (defun groovy-electric-gstring-at-point-p()
123 (and groovy-electric-mode
124 (consp (memq 'font-lock-string-face (text-properties-at (point))))
125 (save-excursion
126 (char-equal ?\" (char-after (car (c-literal-limits)))))))
127
128 (defun groovy-electric-is-last-command-char-expandable-punct-p()
129 (or (memq 'all groovy-electric-expand-delimiters-list)
130 (memq last-command-char groovy-electric-expand-delimiters-list)))
131
132 (defun groovy-electric-curlies(arg)
133 (interactive "P")
134 (self-insert-command (prefix-numeric-value arg))
135 (when (and (groovy-electric-is-last-command-char-expandable-punct-p)
136 (groovy-electric-code-at-point-p))
137 (insert " ")
138 (save-excursion
139 (if groovy-electric-newline-before-closing-bracket
140 (newline))
141 (insert "}"))))
142
143 (defun groovy-electric-matching-char(arg)
144 (interactive "P")
145 (self-insert-command (prefix-numeric-value arg))
146 (and (groovy-electric-is-last-command-char-expandable-punct-p)
147 (groovy-electric-code-at-point-p)
148 (save-excursion
149 (insert (cdr (assoc last-command-char
150 groovy-electric-matching-delimeter-alist))))))
151
152 (defun groovy-electric-pound(arg)
153 (interactive "P")
154 (self-insert-command (prefix-numeric-value arg))
155 (when (and (groovy-electric-is-last-command-char-expandable-punct-p)
156 (groovy-electric-gstring-at-point-p)
157 (not (save-excursion ; make sure it is not escaped
158 (backward-char 1)
159 (char-equal ?\\ (preceding-char)))))
160 (insert "{}")
161 (backward-char 1)))
162
163
164 (provide 'groovy-electric)
165
166 ;;; groovy-electric.el ends here