-
Notifications
You must be signed in to change notification settings - Fork 13
/
lentic-cookie.el
187 lines (155 loc) · 5.65 KB
/
lentic-cookie.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
;;; lentic-cookie.el -- Lentic with a magic cookie -*- lexical-binding: t -*-
;;; Header:
;; This file is not part of Emacs
;; Author: Phillip Lord <phillip.lord@russet.org.uk>
;; Maintainer: Phillip Lord <phillip.lord@russet.org.uk>
;; The contents of this file are subject to the GPL License, Version 3.0.
;; Copyright (C) 2016-2022 Free Software Foundation, Inc.
;; 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:
;;; Code:
;; #+begin_src emacs-lisp
(require 'lentic)
(require 'lentic-chunk)
(defclass lentic-cookie-uncommented-configuration
(lentic-configuration)
()
:documentation "Configuration for a magic cookie containing
lentic buffer that is not commented.")
(defun lentic-cookie--uncommented-fixup-first-line-1 (buffer first-line-end
comment)
"Fixup the first line.
BUFFER is the buffer.
FIRST-LINE-END is the location of the end of the line.
BUFFER is the buffer *with* the comments rather than *without*
despite the name of the function!"
(m-buffer-nil-marker
(m-buffer-replace-match
(m-buffer-match
buffer
(rx-to-string
`(and line-start
(or
;; the line may have been commented during the update
,comment
;; the line may have the comment from org-mode
"# ")
;; and this is the actual start
"#!"))
:end first-line-end)
"#!")))
(defun lentic-cookie-uncommented-fixup-first-line (conf first-line-end)
"Fixup the first line.
CONF is the `lentic-configuration' object.
FIRST-LINE-END is the location of the end of the line."
(lentic-cookie--uncommented-fixup-first-line-1
(lentic-that conf) first-line-end
(oref conf comment)))
(cl-defmethod lentic-clone
((conf lentic-cookie-uncommented-configuration)
&optional start stop length-before
start-converted stop-converted)
(let ((clone-return
(cl-call-next-method conf start stop
length-before start-converted stop-converted)))
(if (lentic-cookie-uncommented-fixup-first-line
conf
(cl-cadar
(m-buffer-match-first-line
(lentic-this conf)
:numeric t)))
nil
clone-return)))
(defclass lentic-cookie-commented-configuration
(lentic-configuration)
()
:documentation "Configuration for magic cookie containing lentic file that is
commented.")
(defun lentic-cookie--commented-fixup-first-line-1 (buffer first-line-end)
"Fixup the first line.
BUFFER is the buffer.
FIRST-LINE-END is the location of the end of the line.
BUFFER is the buffer *without* the comments rather than *with*
despite the name of the function!"
(m-buffer-nil-marker
(m-buffer-replace-match
(m-buffer-match
buffer
(rx
(and line-start
(0+ anything)
"#!"))
:end first-line-end)
"# #!")))
(defun lentic-cookie-commented-fixup-first-line (conf first-line-end)
"Fixup the first line.
CONF is the `lentic-configuration' object.
FIRST-LINE-END is the location of the end of the line."
(lentic-cookie--commented-fixup-first-line-1
(lentic-that conf) first-line-end))
(cl-defmethod lentic-clone
((conf lentic-cookie-commented-configuration)
&optional start stop &rest _)
(let ((clone-return (cl-call-next-method)))
(if
(or
;; next method has done strange things
(not clone-return)
;; calling method is broad
(not start)
(not stop)
(m-buffer-with-markers
((first-line
(m-buffer-match-first-line
(lentic-this conf))))
(or
(m-buffer-in-match-p
first-line start)
(m-buffer-in-match-p
first-line stop))))
(progn
(lentic-cookie-commented-fixup-first-line
conf
(cl-cadar
(m-buffer-match-first-line
(lentic-this conf)
:numeric t)))
nil)
clone-return)))
(defclass lentic-cookie-unmatched-uncommented-chunk-configuration
(lentic-unmatched-uncommented-chunk-configuration
lentic-cookie-uncommented-configuration)
())
(cl-defmethod lentic-invert
((conf lentic-cookie-unmatched-uncommented-chunk-configuration))
(lentic-cookie-unmatched-commented-chunk-configuration
;; FIXME: Factor this out
:this-buffer (lentic-that conf)
:that-buffer (lentic-this conf)
:comment (oref conf comment)
:comment-start (oref conf comment-start)
:comment-stop (oref conf comment-stop)))
(defclass lentic-cookie-unmatched-commented-chunk-configuration
(lentic-unmatched-commented-chunk-configuration
lentic-cookie-commented-configuration)
())
(cl-defmethod lentic-invert
((conf lentic-cookie-unmatched-commented-chunk-configuration))
(lentic-cookie-unmatched-uncommented-chunk-configuration
;; FIXME: Factor this out
:this-buffer (lentic-that conf)
:that-buffer (lentic-this conf)
:comment (oref conf comment)
:comment-start (oref conf comment-start)
:comment-stop (oref conf comment-stop)))
(provide 'lentic-cookie)
;;; lentic-cookie ends here