-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheel-kabayaki.el
231 lines (206 loc) · 7.26 KB
/
eel-kabayaki.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
;;; eel-kabayaki.el --- Roasted eel.el.
;;
;; Author: Akihiro Kuroiwa
;; Maintainer: Akihiro Kuroiwa
;; Created: 4 Jan 2017
;; Keywords: regexp, comments, java, python
;; Package-Requires: ((python-mode))
;; URL: https://github.com/akuroiwa/eel-el
;; Version: 0.0.1
;; This file is not part of GNU Emacs.
;; This code 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, or
;; (at your option) any later version.
;; This code 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Please see commentary of `eel.el'.
;;; Code:
(require 'python-mode)
(defun eel-python-comment-or-string-p ()
"Return non-nil if point is on Python comment, string, or docstring.
This function does not change the match data like `string-match-p'
with `inhibit-changing-match-data'."
(eq t (or
(looking-at-p "\\(\"\"\"\\|'''\\|\"\\|'\\)") ;`py-string-delim-re' in `python-mode'.
(looking-at-p "#+")
(nth 3 (syntax-ppss))
(nth 4 (syntax-ppss)))))
;;;###autoload
(defun eel-skip-python-comment-forward (&optional n)
"Skip Python comment, string or docstring.
Move point N characters forward (backward if N is negative)."
(interactive "p")
(while (eel-python-comment-or-string-p)
(forward-char n)
(point))) ;Return value.
;;;###autoload
(defun eel-character-address-of-next-start-of-comment (&optional n)
"Character address of next start of Python comment, string or docstring.
Move point N characters forward (backward if N is negative)."
(interactive "p")
(while (not (eel-python-comment-or-string-p))
(forward-char n)
(point))) ;Return value of point to repeat `while' TEST.
;https://www.emacswiki.org/emacs/ElispCookbook
;;;###autoload
(defun eel-skip-docstring-forward (&optional limit)
"Skip Python docstring forward.
With an optional argument, Restrict editing to LIMIT.
This is based on the code `skip-string-forward' at:
https://www.emacswiki.org/emacs/NavigatingParentheses"
(interactive)
(if (or
(eq (char-after) ?')
(eq (char-after) ?\"))
(catch 'done
(forward-char 1)
(while t
(skip-syntax-forward "^'\"" limit)
(cond ((eq (point) limit)
(throw 'done nil))
((looking-at "\\(\"\"\"\\|'''\\)")
(goto-char (match-end 0))
(throw 'done nil))
(t
(if (eq (point) limit)
(throw 'done nil)
(forward-char 1))))))))
;;;###autoload
(defun eel-replace-java-comment-start-with-python-one (beg end)
"Replace Java `comment-start' with Python one outside Python docstring \
in region between BEG and END or in whole buffer.
This is based on the code `skip-string-forward' at:
https://www.emacswiki.org/emacs/NavigatingParentheses"
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(catch 'break
(while t
(cond
((looking-at-p "\\(\"\"\"\\|'''\\)") ;Python docstring.
(eel-skip-docstring-forward)
(throw 'break nil)
)
((looking-at "//") ;`comment-start' of Java.
(replace-match "##")
(end-of-line)
(throw 'break nil))
(t
(if (eq (point) (point-max))
(throw 'break t)
(forward-char)
(skip-chars-forward " \t\n"))))))))))
(defun eel-py--escape-doublequotes (start end)
"Escape quote character including single quote or apostrophe \
between START and END.
This is based on the code `py--escape-doublequotes'."
(let ((end (copy-marker end)))
(save-excursion
(goto-char start)
(while (and (not (eobp)) (< 0 (abs (skip-chars-forward "^\'" end))))
(when (eq (char-after) ?\')
(unless (py-escaped)
(insert "\\")
(forward-char 1))))
(goto-char start)
(while (and (not (eobp)) (< 0 (abs (skip-chars-forward "^\"" end))))
(when (eq (char-after) ?\")
(unless (py-escaped)
(insert "\\")
(forward-char 1)))))))
;;;###autoload
(defun eel-replace-java-docstring-with-python-one (beg end)
"Replace Java docstring delimiters with Python one between BEG and END \
or in whole buffer."
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(let (
(re-alist '(
("/\\*+\\(.*?\\)\\*+/" . "\"\"\"\\1\"\"\"") ;match one line.
("/\\*+\\(.*?\\(.*\n\\)*?.*?\\)\\*+/" . "\"\"\"\\1\"\"\"") ;match multiple lines.
)))
(save-excursion
(save-restriction
(narrow-to-region beg end)
(mapc
(lambda (element)
;; (goto-char (point-min))
;; (while (re-search-forward (car element) nil t)
;; (eel-py--escape-doublequotes (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward (car element) nil t)
(replace-match (cdr element) nil nil)
(syntax-ppss-flush-cache (match-beginning 0))
(while (re-search-forward "^ \\*[^/]" nil t)
(replace-match ""))))
re-alist)))))
;;;###autoload
(defun eel-replace-java-class-with-python-one (beg end)
"Replace Java class with Python one outside Python docstring in whole buffer \
or in region between BEG and END."
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(let (
(re-alist '(
("\\(public\\|private\\|protected\\) class\\(.*?\\)\\(extends\\) \\(.*?\\) *\{" . "class\\2\(\\4\):\{")
("\\(public\\|private\\|protected\\) class\\(.*?\\) *\{" . "class\\2:\{")))
)
(save-excursion
(save-restriction
(narrow-to-region beg end)
(mapc
(lambda (element)
(goto-char (point-min))
(while (not (eobp))
(catch 'break
(while t
(cond
((looking-at-p "\\(\"\"\"\\|'''\\)") ;Python docstring.
(eel-skip-docstring-forward)
(throw 'break nil))
((looking-at-p "#")
(eel-skip-python-comment-forward))
((looking-at (car element))
(replace-match (cdr element))
(goto-char (match-end 0))
(backward-char)
(when (char-equal ?\{ (char-after))
(delete-pair))
(throw 'break nil))
(t
(if (eq (point) (point-max))
(throw 'break t)
(forward-char)
(skip-chars-forward " \t\n"))))))))
re-alist)))))
;;;###autoload
(defun eel-convert-from-java-to-python (beg end)
"Convert code from Java to Python in the region between BEG and END.
This function has been left unfinished.
It doesn't work correctly since there are so many bugs.
Please select a region manually and execute each function."
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(python-mode)
(save-excursion
(save-restriction
(narrow-to-region beg end)
(eel-replace-java-docstring-with-python-one (point-min) (point-max))
(eel-replace-java-comment-start-with-python-one (point-min) (point-max))
(eel-replace-java-class-with-python-one (point-min) (point-max)))))
(provide 'eel-kabayaki)
;;; eel-kabayaki.el ends here