-
Notifications
You must be signed in to change notification settings - Fork 6
/
multi-line-shared.el
126 lines (101 loc) · 4.28 KB
/
multi-line-shared.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
;;; multi-line-shared.el --- multi-line statements -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2023 Ivan Malison
;; 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:
;; multi-line-shared defines functions that are generally useful in
;; building multi-line strategies.
;;; Code:
(require 'cl-lib)
(require 'eieio)
(require 'dash)
(require 's)
(defun multi-line-clear-whitespace-at-point ()
"Clear the whitespace at point."
(interactive)
(cl-destructuring-bind (start . end)
(multi-line-space-markers)
(delete-region (marker-position start) (marker-position end))))
(cl-defun multi-line-space-markers
(&optional (space-matches-string "[:space:]\n"))
"Get markers delimiting whitespace at point.
SPACE-MATCHES-STRING is as a string containing concatenated
character classes that will be used to find whitespace."
(let ((space-excludes-string (format "[^%s]" space-matches-string)))
(re-search-backward space-excludes-string)
(forward-char)
(let* ((start (point-marker))
(end (progn
(re-search-forward space-excludes-string)
(backward-char)
(point-marker))))
(cons start end))))
(defun multi-line-add-remove-or-leave-final-comma ()
(save-excursion
(if (looking-at ",?[[:space:]]*\n")
(when (not (or (looking-at ",")
(save-excursion
(re-search-backward "[^[:space:]\n]")
(looking-at ","))))
(insert ","))
(when (or (looking-at ",")
(progn (re-search-backward "[^[:space:]\n]")
(looking-at ",")))
(delete-char 1)))))
(defun multi-line-lparenthesis-advance ()
"Advance to the beginning of a statement that can be multi-lined."
(re-search-forward "[[{(]"))
(defun multi-line-up-list-back ()
"Go to the beginning of a statement from inside the statement."
(interactive)
;; TODO: This could really used some explanation. I have no idea what is going
;; on here now.
(let ((string-start (nth 8 (syntax-ppss))))
(when string-start
(goto-char string-start)))
(up-list) (backward-sexp))
(defun multi-line-comma-advance ()
"Pass over a comma when it is present."
(when (looking-at "[[:space:]\n]*,")
(re-search-forward ",")))
(defun multi-line-is-newline-between-markers (first second)
(s-contains? "\n"
(buffer-substring (marker-position first)
(marker-position second))))
(defmacro multi-line-predicate-or (&rest predicates)
`(lambda (&rest args)
(or ,@(cl-loop for predicate in predicates
collect `(apply ,predicate args)))))
(defmacro multi-line-predicate-and (&rest predicates)
`(lambda (&rest args)
(and ,@(cl-loop for predicate in predicates
collect `(apply ,predicate args)))))
(defun multi-line-last-predicate (index candidates)
(equal index (- (length candidates) 1)))
(defun multi-line-first-predicate (index _candidates)
(equal index 0))
(defalias 'multi-line-first-or-last-predicate
(multi-line-predicate-or 'multi-line-first-predicate
'multi-line-last-predicate))
(defun multi-line-remove-at-indices (skip-indices list)
(-remove-at-indices (multi-line-actual-indices skip-indices list) list))
(defun multi-line-actual-indices (skip-indices list)
(let ((list-length (length list)))
(cl-loop for index in skip-indices
collect (mod index list-length))))
(defun multi-line-interpret-prefix-as-number (prefix)
(cond
((numberp prefix) prefix)
((and (-non-nil prefix) (listp prefix))
(truncate (log (car prefix) 4)))
(0)))
(provide 'multi-line-shared)
;;; multi-line-shared.el ends here