summary refs log tree commit diff stats
path: root/emacs/elisp/php-electric.el
blob: 599b2b1d004fe73d975ee6c44740698c0ccf3745 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
;; -*- Emacs-Lisp -*-
;;
;; php-electric.el --- electric submode for the php-mode
;;
;; Version: 1.0
;; Release-Date: Sunday 04 March 2007
;;
;; Copyright (C) 2007
;;   by Nikolay V. Nemshilov aka St. <nemshilov dog gmail . com>
;;
;;
;; License
;;
;; 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;;
;;
;; Features:
;;   * autocompletion of the language contructions
;;     such as if, for, foreach, etc blocks,
;;
;;   * autocompletion of classes, interfaces and functions
;;     definitions
;;
;;   * autocompletion of the paired symbols, like [], (), "",''
;;
;;
;; Usage:
;;   Nothing magical, just place the file in a directory where
;;   Emacs can find it, and write
;;
;;   (require 'php-electric)
;;
;;   in your configuration files ~/.emacs or wherever you keep it.
;;   Then you can switch on/off the mode by the following command
;;
;;   M-x php-electric-mode <RET>
;;
;;   If you like to have it switched on automatically, you should
;;   put the command in your php-mode hook or create new one,
;;   like that
;;
;;   (add-hook 'php-mode-hook '(lambda () (php-electric-mode)))
;;
;;   That's it.
;;
;;
;; Changelog:
;;   Sunday 04 March 2007
;;     The first version 1.0 has been came out.
;;

(defgroup php-electric nil
  "Minor php-electric mode"
  :group 'php)

;; list of keywords which expandible by the {} pair
(defconst php-electric-expandible-simple-re
  "\\(try\\|else\\|do\\)")

;; list of keywords which expandible with the (){} construction
(defconst php-electric-expandible-as-struct-re
  "\\(while\\|for\\|foreach\\|if\\|elseif\\|catch\\)")

;; list of keywords which expandible with the name(){} construction
(defconst php-electric-expandible-as-func-re
  "\\(function\\)")

;; list of keywords which expandible with the name{} construction
(defconst php-electric-expandible-as-class-re
  "\\(class\\|interface\\)")

;; list of the paired chars
(defvar php-electric-matching-delimeter-alist
  '((?\[ . ?\])
    (?\( . ?\))
    (?\" . ?\")
    (?\' . ?\')))

;; the minor-mode definition
(define-minor-mode php-electric-mode
  "Minor electric-mode for the php-mode"
  nil
  "-El"
  php-mode-map
  (php-electric-keymap))

;; list of accessible keys
(defun php-electric-keymap()
  (define-key php-mode-map " " 'php-electric-space)
  (define-key php-mode-map "{" 'php-electric-curlies)
  (define-key php-mode-map "(" 'php-electric-brackets)
  (define-key php-mode-map "[" 'php-electric-matching-char)
  (define-key php-mode-map "\"" 'php-electric-matching-char)
  (define-key php-mode-map "\'" 'php-electric-matching-char))

;; handler for the spaces insertions
(defun php-electric-space(arg)
  (interactive "P")
  (self-insert-command (prefix-numeric-value arg))
  (if (php-electric-is-code-at-point-p)
      (if (php-electric-line-is-simple-expandible)
	  ;; inserting just a pair of curleis
	  (progn
	    (insert "{")(php-electric-insert-new-line-and-statement-end))
	(if (php-electric-line-is-expandible-as-struct)
	    ;; inserting a structure definition
	    (progn
	      (if (not (char-equal ?\( (preceding-char)))
		  ;; cmd () {  - style construction
		  (progn
		    (insert "(")(set-register 98 (point-marker))(insert ") {"))

		;; cmd(  ){ - style construction
		(progn
		  (insert " ")(set-register 98 (point-marker))(insert " ){")))
	      (php-electric-insert-new-line-and-statement-end)
	      (jump-to-register 98)(set-register 98 nil))
	  (if (php-electric-line-is-expandible-as-func)
	      ;; inserting the function expanding
	      (save-excursion
		(insert "(){")(php-electric-insert-new-line-and-statement-end))
	    (if (php-electric-line-is-expandible-as-class)
		;; inserting the class expanding
		(save-excursion
		  (insert "{")(php-electric-insert-new-line-and-statement-end))))))))

;; handler for the { chars
(defun php-electric-curlies(arg)
  (interactive "P")
  (self-insert-command (prefix-numeric-value arg))
  (if (php-electric-is-code-at-point-p)
      (progn
	(php-electric-insert-new-line-and-statement-end))))

;; handler for the ( chars
(defun php-electric-brackets(arg)
  (interactive "P")

  (if (php-electric-is-code-at-point-p)
      ;; checking if it's a statement
      (if (php-electric-line-is-expandible-as-struct)
	  (progn (php-electric-space arg))
	(progn (php-electric-matching-char arg)))
    (self-insert-command (prefix-numeric-value arg))))

;; handler for the paired chars, [], (), "", ''
(defun php-electric-matching-char(arg)
  (interactive "P")
  (self-insert-command (prefix-numeric-value arg))
  (if (php-electric-is-code-at-point-p)
      (save-excursion
	(insert (cdr (assoc last-command-char
			    php-electric-matching-delimeter-alist))))))

;; checks if the current pointer situated in a piece of code
(defun php-electric-is-code-at-point-p()
  (and php-electric-mode
       (let* ((properties (text-properties-at (point))))
	 (and (null (memq 'font-lock-string-face properties))
	      (null (memq 'font-lock-comment-face properties))))))

;; checks if the current line expandible with a simple {} construction
(defun php-electric-line-is-simple-expandible()
  (let* ((php-electric-expandible-simple-real-re
	  (concat php-electric-expandible-simple-re "\\s-$")))
    (save-excursion
      (backward-word 1)
      (looking-at php-electric-expandible-simple-real-re))))

;; checks if the current line expandible with the (){} construction
(defun php-electric-line-is-expandible-as-struct()
  (let* ((php-electric-expandible-as-struct-real-re
	  (concat php-electric-expandible-as-struct-re "[ ]*$"))
	 (php-electric-expandible-as-struct-with-bracket-re
	  (concat php-electric-expandible-as-struct-re "($")))
    (save-excursion
      (backward-word 1)
      (or (looking-at php-electric-expandible-as-struct-real-re)
	  (looking-at php-electric-expandible-as-struct-with-bracket-re)))))

;; checks if the current line expandible with the name(){} construction
(defun php-electric-line-is-expandible-as-func()
  (let* ((php-electric-expandible-as-func-real-re
	  (concat php-electric-expandible-as-func-re "\\s-$")))
    (save-excursion
      (backward-word 1)
      (looking-at php-electric-expandible-as-func-real-re))))

;; checks if the current line expandible with the name{} construction
(defun php-electric-line-is-expandible-as-class()
  (let* ((php-electric-expandible-as-class-real-re
	  (concat php-electric-expandible-as-class-re "\\s-$")))
    (save-excursion
      (backward-word 1)
      (looking-at php-electric-expandible-as-class-real-re))))

;; "shortcut" to insert \n} construction
(defun php-electric-insert-new-line-and-statement-end()
  (newline-and-indent)(set-register 99 (point-marker))
  (insert "\n}")(indent-according-to-mode)
  (jump-to-register 99)(set-register 99 nil))


(provide 'php-electric)

;; end of the file