-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-par-delete.el
3614 lines (3148 loc) · 120 KB
/
json-par-delete.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; json-par-delete.el --- Deleting/marking things 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 deleting or marking things in JSON Par mode.
;;; Code:
(require 'cl-lib)
(require 'json-par-lexer)
(require 'json-par-motion)
(require 'json-par-keymap)
(require 'json-par-indent)
(require 'json-par-insert)
(declare-function
json-par--oneline-after
"json-par-oneline-multiline"
(min-level &optional keep-newlines-around-brackets delete-spaces-if-oneline))
(declare-function
json-par-oneline
"json-par-oneline-multiline"
(&optional min-level))
(defvar json-par--fixup-adviced-functions nil
"Functions to be adviced with `json-par--fixup-advice'.")
;;; Customizations
(defcustom json-par-hungry-delete t
"When non-nil, delete successive spaces at once.
`json-par-delete-backward-char' and `json-par-delete-forward-char' are
affected."
:type 'boolean
:group 'json-par
:safe #'booleanp)
(defcustom json-par-action-when-deleting-successive-empty-lines 'leave-one
"Action when deleting successive empty lines.
- `delete-one': just delete one line.
- `leave-one': leave one and delete the rest.
- `delete-all': delete all lines.
This affects `json-par-delete-backward-char', `json-par-delete-forward-char',
and `json-par-join-line'."
:type '(choice (const :tag "Just delete one line" delete-one)
(const :tag "Leave one and delete the rest" leave-one)
(const :tag "Delete all lines" delete-all))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-joining-non-empty-lines 'just-delete
"Action when joining non-empty lines.
- `just-delete': just delete the line break.
- `delete-line-breaks-between-members': delete all line breaks between members
(but not before/after members).
- `delete-line-breaks-inside-brackets': delete all line breaks inside the
current array/object.
This affects `json-par-delete-backward-char', `json-par-delete-forward-char',
and `json-par-join-line'."
:type '(choice (const :tag "Just delete one line" delete-one)
(const :tag "Leave one and delete the rest" leave-one)
(const :tag "Delete all lines" delete-all))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-value-or-member 'mark-or-delete
"Action for deleting a value or member.
- `delete': delete the value/member unconditionally.
- `mark-or-delete': delete only if the region to be deleted is marked.
Otherwise, mark the region.
- `mark': mark the region to be deleted unconditionally.
This affects the following commands:
- `json-par-delete-current-member'
- `json-par-delete-backward-member'
- `json-par-delete-forward-member'
- `json-par-delete-current-value-or-key'"
:type '(choice (const :tag "Delete" delete)
(const :tag "Mark or delete" mark-or-delete)
(const :tag "Mark" mark))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-brackets-from-inside
'mark-or-delete-inner
"Action for deleting a bracket from inside.
Action for deleting an open bracket backward, or an close bracket forward.
- `delete-outer': delete whole object/array.
- `delete-inner': delete contents of the object/array if not empty. Delete
whole object/array otherwise.
- `mark-outer': mark whole object/array.
- `mark-inner': mark contents of the object/array if not empty. Mark whole
object/array otherwise.
- `mark-or-delete-inner': mark contents of the object/array if not empty. Mark
whole object/array otherwise. If already marked, delete it.
- `exit': move the point out of the object/array.
- `none': do nothing.
This affects the following commands:
- `json-par-delete-backward-member'
- `json-par-delete-forward-member'
- `json-par-delete-backward-token-or-list'
- `json-par-delete-forward-token-or-list'
- `json-par-delete-backward-char'
- `json-par-delete-forward-char'"
:type '(choice (const :tag "Delete outer" delete-outer)
(const :tag "Delete inner" delete-inner)
(const :tag "Mark outer" mark-outer)
(const :tag "Mark inner" mark-inner)
(const :tag "Mark or delete inner" mark-or-delete-inner)
(const :tag "Exit" exit)
(const :tag "None" none))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-brackets-from-outside 'mark-inner
"Action for deleting a bracket from outside.
Action for deleting an open bracket forward, or an close bracket backward.
- `delete-outer': delete whole object/array.
- `delete-inner': delete contents of the object/array if not empty. Delete
whole object/array otherwise.
- `mark-outer': mark whole object/array.
- `mark-or-delete-outer': mark whole object/array. If it is already marked,
delete it.
- `mark-inner': mark contents of the object/array if not empty. Mark whole
object/array otherwise.
- `enter': move the point into the object/array.
This affects the following commands:
- `json-par-delete-backward-token-or-list'
- `json-par-delete-forward-token-or-list'
- `json-par-delete-backward-char'
- `json-par-delete-forward-char'"
:type '(choice (const :tag "Delete outer" delete-outer)
(const :tag "Delete inner" delete-inner)
(const :tag "Mark outer" mark-outer)
(const :tag "Mark or delete outer" mark-or-delete-outer)
(const :tag "Mark inner" mark-inner)
(const :tag "Enter" enter))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-string-from-inside
'mark-or-delete-inner
"Action for deleting a double quote from inside of a string.
- `delete-outer': delete whole string.
- `delete-inner': delete contents of the string if not empty. Delete whole
string otherwise.
- `mark-outer': mark whole string.
- `mark-inner': mark contents of the string if not empty. Mark whole string
otherwise.
- `mark-or-delete-inner': mark contents of the string. Mark whole string
otherwise. If already marked, delete it.
- `exit': move the point out of the string.
- `none': do nothing.
This affects the following commands:
- `json-par-delete-backward-char'
- `json-par-delete-forward-char'"
:type '(choice (const :tag "Delete outer" delete-outer)
(const :tag "Delete inner" delete-inner)
(const :tag "Mark outer" mark-outer)
(const :tag "Mark inner" mark-inner)
(const :tag "Mark or delete inner" mark-or-delete-inner)
(const :tag "Exit" exit)
(const :tag "None" none))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-string-from-outside 'mark-inner
"Action for deleting a double quote from outside of a string.
- `delete-outer': delete whole string.
- `delete-inner': delete contents of the string if not empty. Delete whole
string otherwise.
- `mark-outer': mark whole string.
- `mark-or-delete-outer': mark whole string. If it is already marked, delete
it.
- `mark-inner': mark contents of the string if not empty. Mark whole string
otherwise.
- `enter': move the point into the string.
This affects the following commands:
- `json-par-delete-backward-char'
- `json-par-delete-forward-char'"
:type '(choice (const :tag "Delete outer" delete-outer)
(const :tag "Delete inner" delete-inner)
(const :tag "Mark outer" mark-outer)
(const :tag "Mark or delete outer" mark-or-delete-outer)
(const :tag "Mark inner" mark-inner)
(const :tag "Enter" enter))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-colon 'mark-or-delete
"Action for deleting a colon.
- `delete': delete whole member (key-value pair) unconditionally.
- `mark-or-delete': delete only if the whole member is marked. Otherwise, mark
the whole member.
- `mark': mark whole member unconditionally.
- `skip': move the point to the opposite site.
This affects the following commands:
- `json-par-delete-backward-token-or-list'
- `json-par-delete-forward-token-or-list'
- `json-par-delete-backward-char'
- `json-par-delete-forward-char'"
:type '(choice (const :tag "Delete member" delete)
(const :tag "Mark or delete member" mark-or-delete)
(const :tag "Mark member" mark)
(const :tag "Skip" skip))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-action-when-deleting-comma 'mark-or-delete
"Action for deleting a comma.
- `delete': delete whole member (key-value pair) beyond the comma
unconditionally.
- `mark-or-delete': delete only if the whole member beyond the comma is marked.
Otherwise, mark the whole member.
- `mark': mark whole member beyond the comma unconditionally.
- `skip': move the point to the opposite site.
This affects the following commands:
- `json-par-delete-backward-token-or-list'
- `json-par-delete-forward-token-or-list'
- `json-par-delete-backward-char'
- `json-par-delete-forward-char'"
:type '(choice (const :tag "Delete member" delete)
(const :tag "Mark or delete member" mark-or-delete)
(const :tag "Mark member" mark)
(const :tag "Skip" skip))
:group 'json-par
:safe #'symbolp)
;;; Functions
(defun json-par--delete-or-mark-region
(start end action &optional mark-direction)
"Delete or mark region from START to END depending on ACTION.
See `json-par-action-when-deleting-value-or-member' for details of ACTION.
If the point is middle of the region to be marked, mark backward if
MARK-DIRECTION is a symbol `backward' and mark forward otherwise.
Return symbol `delete' if the region is deleted. Return symbol `mark'
otherwise."
(when (eq action 'mark-or-delete)
(setq action (if (and
(use-region-p)
(= (region-beginning) start)
(= (region-end) end))
'delete
'mark)))
(if (eq action 'mark)
(progn
(setq mark-direction
(cond
((<= (point) start)
'forward)
((<= end (point))
'backward)
(t
mark-direction)))
(if (eq mark-direction 'backward)
(progn
(goto-char end)
(push-mark start t t)
(setq deactivate-mark nil))
(goto-char start)
(push-mark end t t)
(setq deactivate-mark nil)))
(delete-region start end))
action)
(defun json-par--delete-or-mark-or-exit-list (action direction)
"Delete, mark, or exit from an object/array depending on ACTION.
See `json-par-action-when-deleting-brackets-from-inside' for details of ACTION.
Assuming the point is after an open bracket if DIRECTION is `backward', or
before a close bracket otherwise. This also affects the direction of marking."
(cond
((eq action 'none)
nil)
((eq action 'exit)
(if (eq direction 'backward)
(json-par-backward-token)
(json-par-forward-token)))
(t
(json-par--delete-or-mark-list
(save-excursion
(if (eq direction 'backward)
(progn
(json-par-backward-token)
(json-par-forward-token-or-list))
(json-par-forward-token)
(json-par-backward-token-or-list)))
action
(if (eq direction 'backward) 'forward 'backward)
nil))))
(defun json-par--delete-or-mark-or-enter-list (action direction)
"Delete, mark, or enter into an object/array depending on ACTION.
See `json-par-action-when-deleting-brackets-from-outside' for details of ACTION.
Assuming the point is after an close bracket if DIRECTION is `backward', or
before a open bracket otherwise. This also affects the direction of marking."
(if (eq action 'enter)
(if (eq direction 'backward)
(progn
(json-par-backward-token)
(skip-chars-backward "\s\t\n"))
(json-par-forward-token)
(skip-chars-forward "\s\t\n"))
(json-par--delete-or-mark-list
(save-excursion
(if (eq direction 'backward)
(json-par-backward-token-or-list)
(json-par-forward-token-or-list)))
action
direction
nil)))
(defun json-par--delete-or-mark-list (whole-list action direction allow-empty)
"Delete or mark an array/object depending on ACTION.
WHOLE-LIST is a token representing the array/object.
See `json-par-action-when-deleting-brackets-from-inside' and
`json-par-action-when-deleting-brackets-from-outside' for details of ACTION.
ACTION must not be `exit' or `enter'."
(let (start
end)
(cond
;; Delete outer
((eq action 'delete-outer)
(json-par-delete-current-value-or-key
(if (eq direction 'backward)
(json-par-token-end whole-list)
(json-par-token-start whole-list))
nil
(if (eq direction 'backward) 'preceding 'following)
'delete
direction))
;; Mark outer
((memq action '(mark-outer mark-or-delete-outer))
(if (eq direction 'backward)
(progn
(setq start (json-par-token-start whole-list))
(if (json-par-token-matching-brackets-p whole-list)
(setq end (json-par-token-end whole-list))
(setq end (point-max))))
(setq end (json-par-token-end whole-list))
(if (json-par-token-matching-brackets-p whole-list)
(setq start (json-par-token-start whole-list))
(setq start (point-min))))
(json-par--delete-or-mark-region
start
end
(if (eq action 'mark-outer) 'mark 'mark-or-delete)
direction))
;; Delete/mark inner
((memq action '(delete-inner mark-inner mark-or-delete-inner))
(if (eq direction 'backward)
(progn
(setq start (1+ (json-par-token-start whole-list)))
(if (json-par-token-matching-brackets-p whole-list)
(setq end (1- (json-par-token-end whole-list)))
(setq end (point-max))))
(setq end (1- (json-par-token-end whole-list)))
(if (json-par-token-matching-brackets-p whole-list)
(setq start (1+ (json-par-token-start whole-list)))
(setq start (point-min))))
(setq start (save-excursion
(goto-char start)
(skip-chars-forward "\s\t\n")
(point)))
(setq end (save-excursion
(goto-char end)
(skip-chars-backward "\s\t\n")
(point)))
(if (and (not allow-empty) (<= end start))
(json-par--delete-or-mark-list
whole-list
(cond
((eq action 'delete-inner) 'delete-outer)
((eq action 'mark-inner) 'mark-outer)
((eq action 'mark-or-delete-inner) 'mark-or-delete-outer))
direction
allow-empty)
(when (<= end start)
(setq start (save-excursion
(goto-char start)
(skip-chars-backward "\s\t\n")
(point)))
(setq end (save-excursion
(goto-char end)
(skip-chars-forward "\s\t\n")
(point))))
(when (eq (json-par--delete-or-mark-region
start
end
(cond
((eq action 'delete-inner) 'delete)
((eq action 'mark-inner) 'mark)
((eq action 'mark-or-delete-inner) 'mark-or-delete))
direction)
'delete)
(goto-char start)))))))
(cl-defun json-par-delete-backward-token-or-list
(&optional
(action 'delete)
&key
(action-when-deleting-brackets-from-inside 'delete-outer)
(action-when-deleting-brackets-from-outside 'delete-outer)
(action-when-deleting-colon 'delete)
(action-when-deleting-comma 'delete))
"Delete or mark the preceding token or object/array.
ACTION is one of `delete', `mark-or-delete', or `mark', defaults to `delete'
when called from Lisp program, or the value of
`json-par-action-when-deleting-value-or-member' variable when called
interactively.
- `delete': delete the token/object/array unconditionally.
- `mark-or-delete': delete only if the region to be deleted is marked.
Otherwise, mark the region.
- `mark': mark the region to be deleted unconditionally.
At the beginning of the buffer, delete/mark the spaces and line breaks before
the point.
After a comma, delete/mark the previous member.
After an open bracket, action depends on
ACTION-WHEN-DELETING-BRACKETS-FROM-INSIDE. It is one of the following:
- `delete-outer': delete whole object/array.
- `delete-inner': delete contents of the object/array if not empty. Delete
whole object/array otherwise.
- `mark-outer': mark whole object/array.
- `mark-inner': mark contents of the object/array if not empty. Mark whole
object/array otherwise.
- `mark-or-delete-inner': mark contents of the object/array if not empty. Mark
whole object/array otherwise. If already marked, delete it.
- `exit': move the point out of the object/array.
- `none': do nothing.
The default value is `delete-outer' when called from Lisp program, or the value
of variable `json-par-action-when-deleting-brackets-from-inside' when called
interactively.
After a close bracket, action depends on
ACTION-WHEN-DELETING-BRACKETS-FROM-OUTSIDE. It is one of the following:
- `delete-outer': delete whole object/array.
- `delete-inner': delete contents of the object/array if not empty. Delete
whole object/array otherwise.
- `mark-outer': mark whole object/array.
- `mark-or-delete-outer': mark whole object/array. If it is already marked,
delete it.
- `mark-inner': mark contents of the object/array if not empty. Mark whole
object/array otherwise.
- `enter': move the point into the object/array.
The default value is `delete-outer' when called from Lisp program, or the value
of variable `json-par-action-when-deleting-brackets-from-outside' when called
interactively.
After a colon, action depends on ACTION-WHEN-DELETING-COLON. It is one of the
following:
- `delete': delete whole member (key-value pair) unconditionally.
- `mark-or-delete': delete only if the whole member is marked. Otherwise, mark
the whole member.
- `mark': mark whole member unconditionally.
- `skip': move the point to the opposite site.
The default value is `delete' when called from Lisp program, or the value of
variable `json-par-action-when-deleting-colon' when called interactively.
After a comma, action depends on ACTION-WHEN-DELETING-COMMA. It is one of the
following:
- `delete': delete whole member (key-value pair) beyond the comma
unconditionally.
- `mark-or-delete': delete only if the whole member beyond the comma is marked.
Otherwise, mark the whole member.
- `mark': mark whole member beyond the comma unconditionally.
- `skip': move the point to the opposite site.
The default value is `delete' when called from Lisp program, or the value of
variable `json-par-action-when-deleting-comma' when called interactively.
Otherwise, delete/mark the preceding value."
(interactive
(list
json-par-action-when-deleting-value-or-member
:action-when-deleting-brackets-from-inside
json-par-action-when-deleting-brackets-from-inside
:action-when-deleting-brackets-from-outside
json-par-action-when-deleting-brackets-from-outside
:action-when-deleting-colon
json-par-action-when-deleting-colon
:action-when-deleting-comma
json-par-action-when-deleting-comma))
(let ((pos (point))
before-comment-pos
previous-token)
(json-par--out-comment)
(setq before-comment-pos (point))
(json-par--out-atom)
(setq previous-token (save-excursion (json-par-backward-token)))
(cond
;; Beginning of the buffer.
((json-par-token-outside-of-buffer-p previous-token)
(goto-char before-comment-pos)
(when (/= before-comment-pos pos)
(forward-comment 1))
(json-par--delete-or-mark-region
(json-par-token-end previous-token)
(point)
action
'backward))
;; After comma.
((json-par-token-comma-p previous-token)
(json-par--delete-or-mark-or-skip-comma
action-when-deleting-comma
'backward))
;; After open bracket.
((json-par-token-open-bracket-p previous-token)
(json-par--delete-or-mark-or-exit-list
action-when-deleting-brackets-from-inside
'backward))
;; After close bracket.
((json-par-token-close-bracket-p previous-token)
(json-par--delete-or-mark-or-enter-list
action-when-deleting-brackets-from-outside
'backward))
;; After colon.
((json-par-token-colon-p previous-token)
(json-par--delete-or-mark-or-skip-colon
action-when-deleting-colon
'backward))
;; After `other' token.
((json-par-token-other-p previous-token)
(json-par--delete-or-mark-region
(json-par-token-start previous-token)
(json-par-token-end previous-token)
action
'backward))
;; After value.
((json-par-token-atom-p previous-token)
(json-par-delete-current-value-or-key
(point)
nil
'preceding
action
'backward)))))
(push #'json-par-delete-backward-token-or-list
json-par--fixup-adviced-functions)
(cl-defun json-par-delete-forward-token-or-list
(&optional
(action 'delete)
&key
(action-when-deleting-brackets-from-inside 'delete-outer)
(action-when-deleting-brackets-from-outside 'delete-outer)
(action-when-deleting-colon 'delete)
(action-when-deleting-comma 'delete))
"Delete or mark the following token or object/array.
ACTION is one of `delete', `mark-or-delete', or `mark', defaults to `delete'
when called from Lisp program, or the value of
`json-par-action-when-deleting-value-or-member' variable when called
interactively.
- `delete': delete the token/object/array unconditionally.
- `mark-or-delete': delete only if the region to be deleted is marked.
Otherwise, mark the region.
- `mark': mark the region to be deleted unconditionally.
At the end of the buffer, delete/mark the spaces and line breaks after the
point.
Before a comma, delete/mark the next member.
Before a close bracket, action depends on
ACTION-WHEN-DELETING-BRACKETS-FROM-INSIDE. It is one of the following:
- `delete-outer': delete whole object/array.
- `delete-inner': delete contents of the object/array if not empty. Delete
whole object/array otherwise.
- `mark-outer': mark whole object/array.
- `mark-inner': mark contents of the object/array if not empty. Mark whole
object/array otherwise.
- `mark-or-delete-inner': mark contents of the object/array if not empty. Mark
whole object/array otherwise. If already marked, delete it.
- `exit': move the point out of the object/array.
- `none': do nothing.
The default value is `delete-outer' when called from Lisp program, or the value
of variable `json-par-action-when-deleting-brackets-from-inside' when called
interactively.
Before an open bracket, action depends on
ACTION-WHEN-DELETING-BRACKETS-FROM-OUTSIDE. It is one of the following:
- `delete-outer': delete whole object/array.
- `delete-inner': delete contents of the object/array if not empty. Delete
whole object/array otherwise.
- `mark-outer': mark whole object/array.
- `mark-or-delete-outer': mark whole object/array. If it is already marked,
delete it.
- `mark-inner': mark contents of the object/array if not empty. Mark whole
object/array otherwise.
- `enter': move the point into the object/array.
The default value is `delete-outer' when called from Lisp program, or the value
of variable `json-par-action-when-deleting-brackets-from-outside' when called
interactively.
Before a colon, action depends on ACTION-WHEN-DELETING-COLON. It is one of the
following:
- `delete': delete whole member (key-value pair) unconditionally.
- `mark-or-delete': delete only if the whole member is marked. Otherwise, mark
the whole member.
- `mark': mark whole member unconditionally.
- `skip': move the point to the opposite site.
The default value is `delete' when called from Lisp program, or the value of
variable `json-par-action-when-deleting-colon' when called interactively.
Before a comma, action depends on ACTION-WHEN-DELETING-COMMA. It is one of the
following:
- `delete': delete whole member (key-value pair) beyond the comma
unconditionally.
- `mark-or-delete': delete only if the whole member beyond the comma is marked.
Otherwise, mark the whole member.
- `mark': mark whole member beyond the comma unconditionally.
- `skip': move the point to the opposite site.
The default value is `delete' when called from Lisp program, or the value of
variable `json-par-action-when-deleting-comma' when called interactively.
Otherwise, delete/mark the preceding value."
(interactive
(list
json-par-action-when-deleting-value-or-member
:action-when-deleting-brackets-from-inside
json-par-action-when-deleting-brackets-from-inside
:action-when-deleting-brackets-from-outside
json-par-action-when-deleting-brackets-from-outside
:action-when-deleting-colon
json-par-action-when-deleting-colon
:action-when-deleting-comma
json-par-action-when-deleting-comma))
(json-par--out-comment)
(when (looking-at "/[/*]")
(forward-comment 1))
(let* ((current-atom (json-par--current-atom))
(next-token
(if (json-par-token-inside-p current-atom)
current-atom
(save-excursion
(json-par--out-atom)
(json-par-forward-token)))))
(cond
;; End of the buffer.
((json-par-token-outside-of-buffer-p next-token)
(json-par--delete-or-mark-region
(point)
(json-par-token-end next-token)
action
'forward))
;; Before comma.
((json-par-token-comma-p next-token)
(json-par--delete-or-mark-or-skip-comma
action-when-deleting-comma
'forward))
;; Before close bracket.
((json-par-token-close-bracket-p next-token)
(json-par--delete-or-mark-or-exit-list
action-when-deleting-brackets-from-inside
'forward))
;; Before open bracket.
((json-par-token-open-bracket-p next-token)
(json-par--delete-or-mark-or-enter-list
action-when-deleting-brackets-from-outside
'forward))
;; Before colon.
((json-par-token-colon-p next-token)
(json-par--delete-or-mark-or-skip-colon
action-when-deleting-colon
'forward))
;; Before `other' token.
((json-par-token-other-p next-token)
(json-par--delete-or-mark-region
(json-par-token-start next-token)
(json-par-token-end next-token)
action
'forward))
;; Before value.
((json-par-token-atom-p next-token)
(json-par-delete-current-value-or-key
(point)
nil
'following
action
'forward)))))
(push #'json-par-delete-forward-token-or-list
json-par--fixup-adviced-functions)
(defun json-par-delete-current-value-or-key
(&optional pos keep-member-if-empty preferred-comma action mark-direction)
"Delete or mark the current value or key.
If POS is given, the value or key at the POS is deleted.
Inside empty brackets, delete the spaces and line breaks between it.
If the point is where a value is expected but missing, delete the whole member
unless KEEP-MEMBER-IF-EMPTY is non-nil. PREFERRED-COMMA affects which comma is
deleted.
Before or after a key, delete it. If the key is missing, delete the whole
member.
Otherwise, delete the value.
ACTION is one of `delete', `mark-or-delete', or `mark', defaults to `delete'
when called from Lisp program, or the value of
`json-par-action-when-deleting-value-or-member' variable when called
interactively.
- `delete': delete the value/member unconditionally.
- `mark-or-delete': delete only if the region to be deleted is marked.
Otherwise, mark the region.
- `mark': mark the region to be deleted unconditionally.
If the point is middle of the region to be marked, mark backward if
MARK-DIRECTION is a symbol `backward' and mark forward otherwise."
(interactive
(list
nil
nil
nil
json-par-action-when-deleting-value-or-member
nil))
(unless pos
(setq pos (point)))
(unless preferred-comma
(setq preferred-comma 'following))
(unless action
(setq action 'delete))
(unless mark-direction
(setq mark-direction 'forward))
(save-excursion
(goto-char pos)
(json-par--out-comment)
(json-par--out-atom)
(setq pos (point)))
(let* ((next-token (save-excursion
(goto-char pos)
(json-par-forward-token)))
(previous-token (save-excursion
(goto-char pos)
(json-par-backward-token)))
token-before-value
token-after-value
end-position)
(cond
;; Inside an empty brackets
((and (or (json-par-token-open-bracket-p previous-token)
(json-par-token-outside-of-buffer-p previous-token))
(or (json-par-token-close-bracket-p next-token)
(json-par-token-outside-of-buffer-p next-token)))
(unless keep-member-if-empty
(json-par--delete-or-mark-region
(json-par-token-end previous-token)
(json-par-token-start next-token)
action
mark-direction)))
;; Between two commas or other places expecting a value (but not before a
;; colon)
((and (memq (json-par-token-type previous-token)
'({ \( \[ \, : outside-of-buffer))
(memq (json-par-token-type next-token)
'(} \) \] \, outside-of-buffer)))
(unless keep-member-if-empty
(json-par-delete-current-member
1
action
pos
preferred-comma
mark-direction)))
;; Before a key
((json-par--object-key-p next-token)
(json-par--delete-key
next-token
keep-member-if-empty
action
mark-direction))
;; After a key
((json-par--object-key-p previous-token)
(json-par--delete-key
previous-token
keep-member-if-empty
action
mark-direction))
;; Before colon (missing key)
((json-par-token-colon-p next-token)
(unless keep-member-if-empty
(json-par-delete-current-member 1 action pos 'following 'forward)))
;; otherwise
(t
(setq token-before-value
(save-excursion
(goto-char pos)
(json-par-end-of-member)
(json-par-backward-token-or-list)
(json-par-backward-token)))
(setq token-after-value
(save-excursion
(goto-char pos)
(json-par-end-of-member)
(json-par-forward-token)))
(if (and (or (json-par-token-open-bracket-p token-before-value)
(json-par-token-outside-of-buffer-p token-before-value))
(or (json-par-token-close-bracket-p token-after-value)
(json-par-token-outside-of-buffer-p token-after-value)))
(json-par-delete-current-member
1
action
pos
preferred-comma
mark-direction)
(setq end-position (save-excursion
(goto-char pos)
(json-par-end-of-member)
(json-par--forward-spaces)
(skip-chars-backward "\s\t\n")
(point)))
(json-par--delete-or-mark-region
(save-excursion
(goto-char end-position)
(json-par-backward-token-or-list)
(json-par--backward-spaces)
(skip-chars-forward "\s\t\n")
(point))
end-position
action
mark-direction))))))
(push #'json-par-delete-current-value-or-key json-par--fixup-adviced-functions)
(defun json-par--delete-key (token keep-member-if-empty action mark-direction)
"Delete a object key TOKEN.
If the key is empty, delete the whole member unless KEEP-MEMBER-IF-EMPTY.
If the key is not empty, replace it with am empty key.
For ACTION and MARK-DIRECTION, see `json-par-delete-current-value-or-key'."
(if (and (json-par-token-empty-string-p token) (not keep-member-if-empty))
(json-par-delete-current-member
1
action
(json-par-token-start token)
'following
'forward)
(setq action (json-par--delete-or-mark-region
(1+ (json-par-token-start token))
(1- (json-par-token-end token))
action
mark-direction))
(when (eq action 'delete)
(goto-char (1+ (json-par-token-start token))))))
(defun json-par--delete-or-mark-or-skip-colon (action direction)
"Delete, mark, or skip a colon depending on ACTION.
See `json-par-action-when-deleting-colon' for details of ACTION.
DIRECTION is the direction of mark."
(if (eq action 'skip)
(if (eq direction 'backward)
(progn
(json-par-backward-token)
(json-par--backward-spaces)
(when (memq (char-before) '(nil ?\, ?\[ ?\( ?{))
(json-par--forward-spaces)))
(json-par-forward-token)
(json-par--forward-spaces t))
(json-par-delete-current-member
1
action
(point)
(if (eq direction 'backward) 'preceding 'following)
direction)))
(defun json-par--delete-or-mark-or-skip-comma (action direction)
"Delete, mark, or skip a comma depending on ACTION.
See `json-par-action-when-deleting-comma' for details of ACTION.
DIRECTION is the direction of mark."
(if (eq action 'skip)
(if (eq direction 'backward)
(progn
(json-par-backward-token)
(json-par--backward-spaces)
(when (memq (char-before) '(nil ?\, ?\[ ?\( ?{))
(json-par--forward-spaces)))
(json-par-forward-token)
(json-par--forward-spaces))
(if (eq direction 'backward)
(if (save-excursion
(json-par--forward-spaces)
(memq (char-after) '(nil ?\, ?\] ?\) ?})))
(json-par-delete-current-member
1
action
(point)
'preceding
direction)
(json-par-delete-backward-member 1 action))
(if (save-excursion
(json-par--backward-spaces)