-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-par-clone.el
639 lines (520 loc) · 22.2 KB
/
json-par-clone.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
;;; json-par-clone.el --- Cloning members in JSON Par mode -*- lexical-binding: t -*-
;; Copyright (C) 2021 taku0
;;
;; Author: taku0 (http://github.com/taku0)
;; URL: https://github.com/taku0/json-par
;; 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:
;; Functions for cloning members in JSON Par mode.
;;; Code:
(require 'json-par-lexer)
(require 'json-par-motion)
(require 'json-par-insert)
(require 'json-par-keymap)
(require 'json-par-delete)
;;; Variables
(defvar-local json-par--clone-level-locations '()
"List of the locations of the ancestor members to be cloned.
The first element is the start position of the member to be cloned. The last
element is the start position the parent member.")
(defvar-local json-par--clone-level-overlay nil
"Overlay to indicate the member to be cloned.")
(defvar json-par--fixup-adviced-functions nil
"Functions to be adviced with `json-par--fixup-advice'.")
;;; Functions
(defun json-par-clone-member-forward
(&optional arg level delete-values mark-key)
"Clone the current member and insert after the current member.
If ARG is given, repeat that times. If the ARG is negative, insert before
the current member.
If LEVEL is non-nil, clone LEVEL-th parent instead. Use
`json-par-clone-increase-level' to set LEVEL for interactive use.
If DELETE-VALUES is non-nil and the cloned member is an object/array, delete the
values of the object/array but not the keys, colons, nor commas. If LEVEL is
positive, delete only the values of the current innermost object. If LEVEL is
nil or zero, delete the values of the cloned member.
Move to the last member cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
nil
t))
(unless arg
(setq arg 1))
(if (< arg 0)
(json-par-clone-member-backward (- arg) level delete-values mark-key)
(let (after-cloned)
(dotimes (_ arg)
(setq after-cloned (json-par--clone-member-1 t level delete-values)))
(when (and mark-key
after-cloned
(save-excursion
(goto-char after-cloned)
(json-par-beginning-of-member)
(json-par--object-key-p (json-par-forward-token))))
(goto-char after-cloned)
(json-par-beginning-of-member)
(json-par--delete-or-mark-string
(save-excursion (json-par-forward-token))
'mark-inner
'forward
t)))))
(push #'json-par-clone-member-forward json-par--fixup-adviced-functions)
(defun json-par-clone-member-backward
(&optional arg level delete-values mark-key)
"Clone the current member and insert before the current member.
If ARG is given, repeat that times. If the ARG is negative, insert after
the current member.
If LEVEL is non-nil, clone LEVEL-th parent instead. Use
`json-par-clone-increase-level' to set LEVEL for interactive use.
If DELETE-VALUES is non-nil and the cloned member is an object/array, delete the
values of the object/array but not the keys, colons, nor commas. If LEVEL is
positive, delete only the values of the current innermost object. If LEVEL is
nil or zero, delete the values of the cloned member.
Move to the last member cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
nil
t))
(unless arg
(setq arg 1))
(if (< arg 0)
(json-par-clone-member-forward (- arg) level delete-values mark-key)
(let (after-cloned)
(dotimes (_ arg)
(setq after-cloned (json-par--clone-member-1 nil level delete-values)))
(when (and mark-key
after-cloned
(save-excursion
(goto-char after-cloned)
(json-par-beginning-of-member)
(json-par--object-key-p (json-par-forward-token))))
(goto-char after-cloned)
(json-par-beginning-of-member)
(json-par--delete-or-mark-string
(save-excursion (json-par-forward-token))
'mark-inner
'forward
t)))))
(push #'json-par-clone-member-backward json-par--fixup-adviced-functions)
(defun json-par--clone-member-1 (forward level delete-values)
"Clone the current member and insert before/after the current member.
If FORWARD is non-nil, insert after the current member. Insert before it
otherwise.
If LEVEL is non-nil, clone LEVEL-th parent instead.
If DELETE-VALUES is non-nil and the cloned member is an object/array, delete the
values of the object/array but not the keys, colons, nor commas. If LEVEL is
positive, delete only the values of the current innermost object. If LEVEL is
nil or zero, delete the values of the cloned member.
Move to the cloned member.
Return the location after the cloned member."
(let* ((pos (point))
start
end
offset
member-string
after-cloned)
(json-par--out-comment)
(json-par--out-atom)
(when level
(json-par-up-backward level))
(setq start (progn
(json-par-beginning-of-member)
(json-par--backward-spaces)
(skip-chars-forward "\s\t\n")
(point)))
(setq end (progn
(goto-char start)
(json-par-end-of-member)
(json-par--forward-spaces)
(skip-chars-backward "\s\t\n")
(point)))
(setq offset (- pos end))
(setq member-string (buffer-substring start end))
(goto-char (if forward end start))
(json-par--insert-value member-string t t)
(setq after-cloned (copy-marker (point)))
(goto-char (+ (point) offset))
(when delete-values
(save-excursion
(if (or (null level) (zerop level))
(progn
(goto-char after-cloned)
(when (member (char-before) '(?} ?\]))
(backward-char)
(json-par-delete-object-values t)))
(json-par-delete-object-values t))))
(json-par--free-marker after-cloned)))
(defun json-par-clone-member-forward-without-value
(&optional arg level mark-key)
"Clone the current member and insert after it without values inside.
If ARG is given, repeat that times. If the ARG is negative, insert before
the current member.
If LEVEL is non-nil, clone LEVEL-th parent instead.
If the cloned member is an object/array, delete the values of the object/array
but not the keys, colons, nor commas. If LEVEL is positive, delete only the
values of the current innermost object. If LEVEL is nil or zero, delete the
values of the cloned member.
Move to the last member cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
t))
(json-par-clone-member-forward arg level t mark-key))
(push #'json-par-clone-member-forward-without-value
json-par--fixup-adviced-functions)
(defun json-par-clone-member-backward-without-value
(&optional arg level mark-key)
"Clone the current member and insert before it without values inside.
If ARG is given, repeat that times. If the ARG is negative, insert after
the current member.
If LEVEL is non-nil, clone LEVEL-th parent instead.
If the cloned member is an object/array, delete the values of the object/array
but not the keys, colons, nor commas. If LEVEL is positive, delete only the
values of the current innermost object. If LEVEL is nil or zero, delete the
values of the cloned member.
Move to the last member cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
t))
(json-par-clone-member-backward arg level t mark-key))
(push #'json-par-clone-member-backward-without-value
json-par--fixup-adviced-functions)
(defun json-par-clone-parent-forward
(&optional arg level delete-values mark-key)
"Clone the containing object/array and insert after it.
If ARG is given, repeat that times. If the ARG is negative, insert before
the containing object/array.
If LEVEL is non-nil, clone (1+ LEVEL)-th parent instead.
If DELETE-VALUES is non-nil, delete the values of the object/array but not the
keys, colons, nor commas. If LEVEL is non-nil, delete only the values of the
current innermost object.
Move to the same member in the last object/array cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
nil
t))
(json-par-clone-member-forward arg (1+ (or level 0)) delete-values mark-key))
(push #'json-par-clone-parent-forward json-par--fixup-adviced-functions)
(defun json-par-clone-parent-backward
(&optional arg level delete-values mark-key)
"Clone the containing object/array and insert before it.
If ARG is given, repeat that times. If the ARG is negative, insert after
the containing object/array.
If LEVEL is non-nil, clone (1+ LEVEL)-th parent instead.
If DELETE-VALUES is non-nil, delete the values of the object/array but not the
keys, colons, nor commas. If LEVEL is non-nil, delete only the values of the
current innermost object.
Move to the same member in the last object/array cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
nil
t))
(json-par-clone-member-backward arg (1+ (or level 0)) delete-values mark-key))
(push #'json-par-clone-parent-backward json-par--fixup-adviced-functions)
(defun json-par-clone-parent-forward-without-value
(&optional arg level mark-key)
"Clone the containing object/array and insert after it without values.
If ARG is given, repeat that times. If the ARG is negative, insert before
the containing object/array.
If LEVEL is non-nil, clone (1+ LEVEL)-th parent instead. Delete only the
values of the current innermost object.
Move to the same member in the last object/array cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
t))
(json-par-clone-parent-forward arg level t mark-key))
(push #'json-par-clone-parent-forward-without-value
json-par--fixup-adviced-functions)
(defun json-par-clone-parent-backward-without-value
(&optional arg level mark-key)
"Clone the containing object/array and insert before it without values.
If ARG is given, repeat that times. If the ARG is negative, insert after
the containing object/array.
If LEVEL is non-nil, clone (1+ LEVEL)-th parent instead. Delete only the
values of the current innermost object.
Move to the same member in the last object/array cloned.
If the cloned member has an object key and MARK-KEY is non-nil or called
interactively, mark the contents of the key."
(interactive
(list
(prefix-numeric-value current-prefix-arg)
(length json-par--clone-level-locations)
t))
(json-par-clone-parent-backward arg level t mark-key))
(push #'json-par-clone-parent-backward-without-value
json-par--fixup-adviced-functions)
;;; Keymaps
(defvar json-par-clone-prefix-map
(let ((map (make-sparse-keymap)))
(define-key map "c" #'json-par-clone-member-forward)
(define-key map "v" #'json-par-clone-without-value-prefix-command)
(define-key map "j" #'json-par-clone-member-forward)
(define-key map "k" #'json-par-clone-member-backward)
(define-key map "h" #'json-par-clone-increase-level-prefix-command)
(define-key map "l" #'json-par-clone-increase-level-prefix-command)
(define-key map "i" #'json-par-clone-decrease-level-prefix-command)
(define-key map "b" #'json-par-clone-decrease-level-prefix-command)
(define-key map "J" #'json-par-clone-parent-forward)
(define-key map "K" #'json-par-clone-parent-backward)
(dolist (prefix '("" "C-" "M-" "C-M-"))
(dolist (key '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
(define-key
map
(kbd (concat prefix key))
#'json-par-prefix-digit-argument))
(define-key
map
(kbd (concat prefix "-"))
#'json-par-prefix-negative-argument))
(define-key map "\C-u" #'json-par-prefix-universal-argument)
(define-key map [t] #'json-par-prefix-default)
map)
"Temporary keymap for `json-par-clone-prefix-command'.")
(defvar json-par-clone-without-value-prefix-map
(let ((map (make-sparse-keymap)))
(define-key map "c" #'json-par-clone-member-forward-without-value)
(define-key map "C" #'json-par-clone-member-forward-without-value)
(define-key map "j" #'json-par-clone-member-forward-without-value)
(define-key map "k" #'json-par-clone-member-backward-without-value)
(define-key map "h" #'json-par-clone-increase-level-prefix-command)
(define-key map "l" #'json-par-clone-increase-level-prefix-command)
(define-key map "i" #'json-par-clone-decrease-level-prefix-command)
(define-key map "b" #'json-par-clone-decrease-level-prefix-command)
(define-key map "J" #'json-par-clone-parent-forward-without-value)
(define-key map "K" #'json-par-clone-parent-backward-without-value)
(dolist (prefix '("" "C-" "M-" "C-M-"))
(dolist (key '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
(define-key
map
(kbd (concat prefix key))
#'json-par-prefix-digit-argument))
(define-key
map
(kbd (concat prefix "-"))
#'json-par-prefix-negative-argument))
(define-key map "\C-u" #'json-par-prefix-universal-argument)
(define-key map [t] #'json-par-prefix-default)
map)
"Temporary keymap for `json-par-clone-without-value-prefix-command'.")
;;; Handling key sequence for cloning members
;; Commands for cloning members can be invoked with key sequence.
;;
;; Examples:
;;
;; - Key sequence "c j": clone the current member below.
;; - Key sequence "C J": clone the parent object below without values.
;; - Key sequence "c v h h k":
;; clone the grand-parent object above without values.
;;
;; This is implemented with temporary keymap installed by `set-transient-map'
;; via `json-par-prefix-command'.
;;
;; "h" key and "i" key, mapped to
;; `json-par-clone-increase-level-prefix-command' and
;; `json-par-clone-decrease-level-prefix-command' respectively, sets the
;; level parameter. Those commands also shows an overlay to indicate the member
;; to be cloned.
;;
;; The level parameter and overlay is valid only for the immediately following
;; invocation of the cloning commands, and used only if invoked
;; interactively.
;;
;; Why not `read-multiple-choice':
;;
;; - I don't like the style of the help message.
;; - The keymap should be customizable.
(defun json-par-clone-prefix-command (arg)
"Clone current member or parent object/array.
Actually, set a temporary keymap `json-par-clone-prefix-map' to read direction
and target.
ARG is passed to the commands bound in the keymap."
(interactive "P")
(json-par-prefix-command
arg
(when (called-interactively-p 'interactive)
"clone (? for help)%s: ")
(concat
"Clone current member or parent object/array.
Actually, set the following temporary keymap to read direction and target.\n\n"
(json-par--format-command-help json-par-clone-prefix-map))
json-par-clone-prefix-map))
(defun json-par-clone-without-value-prefix-command (arg)
"Clone current member or parent object/array without values inside.
Actually, set a temporary keymap `json-par-clone-without-value-prefix-map'
to read direction and target.
ARG is passed to the commands bound in the keymap."
(interactive "P")
(json-par-prefix-command
arg
(when (called-interactively-p 'interactive)
"clone without value (? for help)%s: ")
(concat
"Clone current member or parent object/array without values.
Actually, set the following temporary keymap to read direction and target.\n\n"
(json-par--format-command-help json-par-clone-without-value-prefix-map))
json-par-clone-without-value-prefix-map))
(defun json-par-clone-increase-level ()
"Increase the level parameter of the next invocation of cloning commands.
Those commands are affected:
- `json-par-clone-member-forward'
- `json-par-clone-member-backward'
- `json-par-clone-member-forward-without-value'
- `json-par-clone-member-backward-without-value'
- `json-par-clone-parent-forward'
- `json-par-clone-parent-backward'
- `json-par-clone-parent-forward-without-value'
- `json-par-clone-parent-backward-without-value'
Only immediately following invocation is affected.
Intended to be invoked directory by user, like \N{U+4D}-x
json-par\N{U+2D}clone-increase-level or other custom key bindings."
(interactive)
(unless json-par--clone-level-overlay
(setq json-par--clone-level-overlay
(make-overlay (point) (point)))
(overlay-put json-par--clone-level-overlay 'display "*")
(overlay-put json-par--clone-level-overlay 'face 'highlight))
(save-excursion
(goto-char (or (car json-par--clone-level-locations) (point)))
(json-par--out-comment)
(json-par--out-atom)
(json-par-up-backward)
(push (point) json-par--clone-level-locations)
(move-overlay json-par--clone-level-overlay (point) (1+ (point)))
(add-hook
'post-command-hook
#'json-par--delete-clone-level-overlay
nil
t)))
(defun json-par-clone-increase-level-prefix-command (arg)
"Increase the level parameter of the next invocation of cloning commands.
Those commands are affected:
- `json-par-clone-member-forward'
- `json-par-clone-member-backward'
- `json-par-clone-member-forward-without-value'
- `json-par-clone-member-backward-without-value'
- `json-par-clone-parent-forward'
- `json-par-clone-parent-backward'
- `json-par-clone-parent-forward-without-value'
- `json-par-clone-parent-backward-without-value'
Only immediately following invocation is affected.
ARG is passed to the next command.
Intended to be invoked via the keymap of `json-par-clone-prefix-command' or
`json-par-clone-without-value-prefix-command'."
(interactive "P")
(json-par-clone-increase-level)
(json-par-prefix-command
arg
json-par--prefix-prompt
json-par--prefix-help-form
json-par--prefix-keymap))
(defun json-par-clone-decrease-level ()
"Decrease the level parameter of the next invocation of cloning commands.
Those commands are affected:
- `json-par-clone-member-forward'
- `json-par-clone-member-backward'
- `json-par-clone-member-forward-without-value'
- `json-par-clone-member-backward-without-value'
- `json-par-clone-parent-forward'
- `json-par-clone-parent-backward'
- `json-par-clone-parent-forward-without-value'
- `json-par-clone-parent-backward-without-value'
Only immediately following invocation is affected.
Intended to be invoked directory by user, like \N{U+4D}-x
json-par\N{U+2D}clone-decrease-level or other custom key bindings."
(interactive)
(when json-par--clone-level-locations
(pop json-par--clone-level-locations)
(let ((pos (car json-par--clone-level-locations)))
(when pos
(move-overlay json-par--clone-level-overlay pos (1+ pos))
(add-hook
'post-command-hook
#'json-par--delete-clone-level-overlay
nil
t))))
(unless json-par--clone-level-locations
(delete-overlay json-par--clone-level-overlay)))
(defun json-par-clone-decrease-level-prefix-command (arg)
"Decrease the level parameter of the next invocation of cloning commands.
Those commands are affected:
- `json-par-clone-member-forward'
- `json-par-clone-member-backward'
- `json-par-clone-member-forward-without-value'
- `json-par-clone-member-backward-without-value'
- `json-par-clone-parent-forward'
- `json-par-clone-parent-backward'
- `json-par-clone-parent-forward-without-value'
- `json-par-clone-parent-backward-without-value'
Only immediately following invocation is affected.
ARG is passed to the next command.
Intended to be invoked via the keymap of `json-par-clone-prefix-command' or
`json-par-clone-without-value-prefix-command'."
(interactive "P")
(json-par-clone-decrease-level)
(json-par-prefix-command
arg
json-par--prefix-prompt
json-par--prefix-help-form
json-par--prefix-keymap))
(defun json-par--delete-clone-level-overlay ()
"Delete overlay installed by `json-par-clone-increase-level'.
Intended to be a `post-command-hook'."
(unless (memq this-command
'(json-par-clone-increase-level
json-par-clone-decrease-level
json-par-clone-increase-level-prefix-command
json-par-clone-decrease-level-prefix-command
json-par-clone-prefix-command
json-par-clone-without-value-prefix-command
json-par-prefix-universal-argument
json-par-prefix-digit-argument
json-par-prefix-negative-argument
universal-argument
universal-argument-more
digit-argument
negative-argument))
(remove-hook
'post-command-hook
#'json-par--delete-clone-level-overlay
t)
(delete-overlay json-par--clone-level-overlay)
(setq json-par--clone-level-locations '())))
(provide 'json-par-clone)
;;; json-par-clone.el ends here