-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewline-dwim.el
175 lines (142 loc) · 5.97 KB
/
newline-dwim.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
;;; newline-dwim.el --- Insert a newline and DWIM characters -*- lexical-binding: t -*-
;; Copyright (C) 2017 Friends of Emacs-PHP development
;; Author: USAMI Kenta <[email protected]>
;; Created: 26 Sep 2017
;; Version: 0.0.1
;; Keywords: convenience
;; URL: https://github.com/emacs-php/newline-dwim
;; Package-Requires: ((emacs "25.1") (cl-lib "0.5"))
;; This file is NOT part of GNU Emacs.
;; 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/>.
;;; Commentary:
;; This package is in develoment.
;;
;; ## Enable
;;
;; (bind-key "RET" #'newline-dwim)
;;
;; ## Disable
;;
;; (bind-key "RET" #'newline)
;;
;;; Code:
(require 'cl-lib)
(require 'newcomment)
;; Customize
(defgroup newline-dwim nil
"Insert a newline and DWIM charactors."
:group 'convenience)
(defvar newline-dwim-lighter "dNL ")
;;
(defvar-local newline-dwim-php-comments-alist
'(("\\(\\s-*\\)//\\(\\s-*\\)" . ("//" . ""))
("\\(\\s-*\\)\\*\\(\\s-*\\)" . ("*" . ""))))
;; Variables for let
(defvar newline-dwim--before-pos nil)
(defvar newline-dwim--before-ppss nil)
(defvar newline-dwim--current-pos nil)
(defvar newline-dwim--current-ppss nil)
(defvar newline-dwim--in-process nil)
(defvar newline-dwim--last-line-string nil)
;; Buffer local variables
(defvar-local newline-dwim--setup nil)
(defvar-local newline-dwim-functions nil)
(defvar-local newline-dwim-newline-function #'newline)
(defvar-local newline-dwim-comments-alist nil)
;; Utility
(defun newline-dwim--setup (force)
"Setup for `newline-dwim' command. Do refresh when `FORCE' is non-NIL."
(interactive "p")
(when (or force (null newline-dwim--setup))
(setq newline-dwim-functions
(list (cons #'newline-dwim-continue-comment-test #'newline-dwim-continue-comment)
))
(setq newline-dwim-comments-alist newline-dwim-php-comments-alist)
(setq newline-dwim--setup t)))
(defun newline-dwim-before-pos ()
""
(let ((before-pos (or newline-dwim--before-pos
(save-excursion (end-of-line 0) (point)))))
(when (and newline-dwim--in-process (null newline-dwim--before-pos))
(setq newline-dwim--before-pos before-pos))
before-pos))
(defun newline-dwim-before-ppss ()
""
(let ((before-ppss (or newline-dwim--before-ppss
(save-excursion (end-of-line 0) (syntax-ppss)))))
(when (and newline-dwim--in-process (null newline-dwim--before-ppss))
(setq newline-dwim--before-ppss before-ppss))
before-ppss))
(defun newline-dwim-current-pos ()
""
(let ((current-pos (or newline-dwim--current-pos (point))))
(when (and newline-dwim--in-process (null newline-dwim--current-pos))
(setq newline-dwim--current-pos current-pos))
current-pos))
(defun newline-dwim-current-ppss ()
""
(let ((current-ppss (or newline-dwim--current-ppss (syntax-ppss))))
(when (and newline-dwim--in-process (null newline-dwim--current-ppss))
(setq newline-dwim--current-ppss current-ppss))
current-ppss))
(defun newline-dwim-last-line-string ()
"Return string of last line."
(let ((last-line-string
(or newline-dwim--last-line-string
(save-excursion
(goto-char (newline-dwim-before-pos))
(buffer-substring-no-properties (progn (beginning-of-line) (point))
(progn (end-of-line) (point)))))))
(when (and newline-dwim--in-process (null newline-dwim--last-line-string))
(setq newline-dwim--last-line-string last-line-string))
last-line-string))
(defun newline-dwim-string-state (&optional ppss)
"Return string parse state in `PPSS', `newline-dwim--current-ppss' or current position ppss."
(nth 3 (or ppss newline-dwim-current-ppss (syntax-ppss))))
(defun newline-dwim-comment-state (&optional ppss)
"Return comment parse state in `PPSS', `newline-dwim--current-ppss' or current position ppss."
(nth 3 (or ppss newline-dwim-current-ppss (syntax-ppss))))
(defun newline-dwim-continue-comment-test ()
""
(assoc-default (newline-dwim-last-line-string) newline-dwim-comments-alist 'string-match))
(defun newline-dwim-continue-comment (test-result)
""
(insert (match-string 1 (newline-dwim-last-line-string)) "" (car test-result) (match-string 1 (newline-dwim-last-line-string)) (cdr test-result)))
(defun newline-dwim--1 (func-or-list test-result)
"Run function or insert charactors by `FUNC-OR-LIST' and pass `TEST-RESULT'."
(if (functionp func-or-list)
(funcall func-or-list test-result)
""))
;; Command
;;;###autoload
(defun newline-dwim ()
"Insert a newline and DWIM characters."
(interactive)
(newline-dwim--setup nil)
(save-match-data
(let ((newline-dwim--in-process t)
(newline-dwim--before-pos (point))
(newline-dwim--before-ppss (syntax-ppss))
newline-dwim--current-pos newline-dwim--current-ppss newline-dwim--last-line-string)
(when nil ; suppress warning for unused lexial variables.
(list newline-dwim--in-process newline-dwim--last-line-string
newline-dwim--before-pos newline-dwim--before-ppss newline-dwim--current-pos newline-dwim--current-ppss))
(funcall newline-dwim-newline-function)
;; (call-interactively newline-dwim-newline-function)
(cl-loop for (test . func-or-list) in newline-dwim-functions
for result = (funcall test)
if result
return (newline-dwim--1 func-or-list result)))))
(provide 'newline-dwim)
;;; newline-dwim.el ends here