-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-par-insert.el
1409 lines (1166 loc) · 44.4 KB
/
json-par-insert.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.el --- Inserting values 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 inserting values in JSON Par mode.
;;; Code:
(require 'cl-lib)
(require 'json-par-lexer)
(require 'json-par-motion)
(require 'json-par-indent)
(defvar json-par--fixup-adviced-functions nil
"Functions to be adviced with `json-par--fixup-advice'.")
(defvar-local json-par--inhibit-fixup-tick nil
"Suppress fixing if this variable equal to `buffer-chars-modified-tick'.")
;;; Customizations
(defcustom json-par-action-when-inserting-double-quotes-at-end 'exit
"Action for inserting a double quote before a closing double quote.
- `insert': insert an escaped double quote.
- `exit': move the point after the string."
:type '(choice (const :tag "Insert escaped double quotes" insert)
(const :tag "Exit from the string" exit))
:group 'json-par
:safe #'symbolp)
(defcustom json-par-default-brackets-style 'multiline
"Whether or not to insert line breaks when inserting an empty array or object.
This value is used only if the style cannot be guessed from the context.
- `one-line': no line breaks.
Example (`|' is the point): [|]
- `multiline': insert line breaks and indentation.
Example:
[
|
]"
:type '(choice (const :tag "Single line" one-line)
(const :tag "Multiline" multiline))
:group 'json-par
:safe #'symbolp)
;;; Functions
(defun json-par--insert-value
(value &optional place-point-before-comma omit-key)
"Insert VALUE before or after the current member.
If the point is on or after the value of the current member, insert the value
after the current member. Otherwise, insert the value before the current
member. If the point is where a value is expected but missing, insert to there.
Insert commas, spaces, and line breaks if needed.
If the point is inside an object, the current member lacks a key, and OMIT-KEY
is nil, insert an empty key and a colon before the value.
Move the point after the value. If PLACE-POINT-BEFORE-COMMA is nil and a comma
is inserted after the value, move the point after the comma.
Examples (`|' is the point):
// After a value
{
\"a\": \"aaa\"|,
\"b\": \"bbb\"
}
↓ (json-par--insert-value \"true\")
{
\"a\": \"aaa\",
\"\": true,|
\"b\": \"bbb\"
}
// Before a member (\"b\")
{
\"a\": \"aaa\",|
\"b\": \"bbb\"
}
↓ (json-par--insert-value \"true\")
{
\"a\": \"aaa\",
\"\": true,|
\"b\": \"bbb\"
}
// Before a value
{
\"a\": |\"aaa\",
\"b\": \"bbb\"
}
↓ (json-par--insert-value \"true\")
{
\"\": true,|
\"a\": \"aaa\",
\"b\": \"bbb\"
}"
(json-par--out-comment)
(json-par--out-atom t)
(let* ((next-token (save-excursion (json-par-forward-token)))
(previous-token (save-excursion (json-par-backward-token)))
(parent-token (json-par--parent-token))
(point-is-just-after-previous-token
(eq (json-par-token-end previous-token) (point)))
(inside-object (json-par-token-open-curly-bracket-p parent-token))
(one-line
(<= (save-excursion (goto-char (json-par-token-start next-token))
(line-beginning-position))
(json-par-token-end parent-token))))
(cond
;; Inside an empty brackets, or between two commas or other places
;; expecting a value (but not before a colon)
((or (and (memq (json-par-token-type previous-token)
'({ \( \[ \, : outside-of-buffer))
(memq (json-par-token-type next-token)
'(} \) \] \, outside-of-buffer)))
(json-par-token-outside-of-buffer-p parent-token))
(when (and point-is-just-after-previous-token
(not (json-par-token-outside-of-buffer-p previous-token)))
(if (memq (char-after) '(?\s ?\t))
(forward-char)
(insert-char ?\s)))
(when (and inside-object
(not (json-par-token-colon-p previous-token))
(not omit-key))
(insert "\"\": "))
(insert value)
(when (memq (char-after) '(?} ?\) ?\]))
(if one-line
(save-excursion (insert-char ?\s))
(json-par--open-line-and-indent-both))))
;; Between a colon and the next key or another colon (comma missing)
((and (eq (json-par-token-type previous-token) ':)
(or (eq (json-par-token-type next-token) ':)
(json-par--object-key-p next-token t)))
(when (and point-is-just-after-previous-token
(not (json-par-token-outside-of-buffer-p previous-token)))
(if (memq (char-after) '(?\s ?\t))
(forward-char)
(insert-char ?\s)))
(insert value)
(unless (memq (char-after) '(?\s ?\t ?\n nil))
(insert-char ?\s))
(json-par-insert-comma)
(when place-point-before-comma
(json-par--backward-spaces)
(backward-char)))
;; Otherwise
(t
(when (json-par-token-comma-p next-token)
(setq place-point-before-comma t))
(while (not (json-par-insert-comma))
t)
(json-par--insert-value value place-point-before-comma omit-key)
(when (and (not place-point-before-comma) (eq (char-after) ?\,))
(forward-char))))))
(defun json-par-insert-true ()
"Insert a `true' before or after the current member.
See `json-par--insert-value' for details."
(interactive)
(json-par--insert-value "true"))
(push #'json-par-insert-true json-par--fixup-adviced-functions)
(defun json-par-insert-false ()
"Insert a `false' before or after the current member.
See `json-par--insert-value' for details."
(interactive)
(json-par--insert-value "false"))
(push #'json-par-insert-false json-par--fixup-adviced-functions)
(defun json-par-insert-null ()
"Insert a `null' before or after the current member.
See `json-par--insert-value' for details."
(interactive)
(json-par--insert-value "null"))
(push #'json-par-insert-null json-par--fixup-adviced-functions)
(defun json-par-insert-self-as-value ()
"Insert a char invoked this command before or after the current member.
See `json-par--insert-value' for details."
(interactive)
(json-par--insert-value (string last-command-event)))
(push #'json-par-insert-self-as-value json-par--fixup-adviced-functions)
(defun json-par-insert-self-as-number ()
"Insert a char invoked this command before or after the current member.
See `json-par--insert-value' for details.
This function doesn't move the point after a comma, if any, after the number."
(interactive)
(json-par--insert-value (string last-command-event) t))
(push #'json-par-insert-self-as-number json-par--fixup-adviced-functions)
(defun json-par-insert-brackets (open close default-brackets-style)
"Insert a pair of brackets OPEN and CLOSE before or after the current member.
See `json-par--insert-value' for details.
If the region is active, wrap it with the brackets instead.
See `json-par--wrap-region-with-brackets' for details.
Return a list (FINAL-START FINAL-END) where FINAL-START and FINAL-END are the
start and end of the brackets.
DEFAULT-BRACKETS-STYLE affects whether or not to insert line breaks when
inserting an empty array or object. See `json-par-default-brackets-style' for
details."
(if (use-region-p)
(json-par--wrap-region-with-brackets
open
close
(region-beginning)
(region-end)
default-brackets-style)
(json-par--insert-value (concat open close) t)
(let* ((should-break-line
(json-par--should-break-line-after-inserting-brackets
(- (point) (length close) (length open))
(point)
default-brackets-style))
start
end)
(backward-char (length close))
(setq start (- (point) (length open)))
(setq end (copy-marker (+ (point) (length close))))
(when should-break-line
(newline)
(json-par--open-line-and-indent-both))
(list start (json-par--free-marker end)))))
(defun json-par--wrap-region-with-brackets
(open close start end default-brackets-style)
"Wrap region from START toe END with brackets OPEN and CLOSE.
Insert spaces and line breaks if needed.
If the point is inside an object, insert an empty key and a colon before the
brackets.
Indent the region after wrapping.
Return a list (FINAL-START FINAL-END) where FINAL-START and FINAL-END are the
start and end of the wrapped brackets."
(setq end (copy-marker end))
(let* ((point-marker (point-marker))
(parent-token (json-par--parent-token))
(inside-object (json-par-token-open-curly-bracket-p parent-token)))
(goto-char end)
(unless (memq (char-before) '(?\s ?\t ?\n nil))
(insert-before-markers " "))
(insert-before-markers close)
(goto-char start)
(insert open)
(unless (memq (char-after) '(?\s ?\t ?\n nil))
(insert-char ?\s))
(goto-char start)
(when (and inside-object
(save-excursion
(json-par--backward-spaces)
(not (eq (char-before) ?:))))
(setq start (copy-marker start))
(set-marker-insertion-type start t)
(set-marker-insertion-type point-marker t)
(json-par--insert-key-or-value "\"\"")
(set-marker-insertion-type point-marker nil)
(setq start (json-par--free-marker start)))
(when (json-par--should-break-line-after-inserting-brackets
start
end
default-brackets-style)
(goto-char start)
(forward-char (length open))
(skip-chars-forward "\s\t")
(unless (eolp)
(skip-chars-backward "\s\t")
(insert-char ?\n))
(goto-char end)
(backward-char (length close))
(skip-chars-backward "\s\t")
(unless (bolp)
(insert-char ?\n)))
(goto-char point-marker)
(json-par--free-marker point-marker)
(json-par-indent-region start end)
(list start (json-par--free-marker end))))
(defun json-par--should-break-line-after-inserting-brackets
(start end default-brackets-style)
"Return non-nil if we should break line between brackets just inserted.
START and END is the start and end of the brackets.
Use DEFAULT-BRACKETS-STYLE if no idea. See `json-par-default-brackets-style'
for details."
(save-excursion
(goto-char end)
(let* (token
(previous-value (save-excursion
(json-par-beginning-of-member)
(setq token (json-par-backward-token-or-list))
(when (json-par-token-comma-p token)
(setq token (json-par-backward-token-or-list)))
token))
(next-value (save-excursion
(setq token (json-par-forward-token-or-list))
(when (json-par-token-comma-p token)
(json-par-end-of-member)
(setq token (json-par-backward-token-or-list)))
token))
(previous-is-brackets
(json-par-token-matching-brackets-p previous-value))
(next-is-brackets
(json-par-token-matching-brackets-p next-value))
(previous-is-multiline-brackets
(and previous-is-brackets
(not (json-par-token-one-line-p previous-value))))
(next-is-multiline-brackets
(and next-is-brackets
(not (json-par-token-one-line-p next-value)))))
(or previous-is-multiline-brackets
next-is-multiline-brackets
(not (json-par--same-line-p start end))
(and
;; If both the previous and next is one-line brackets, this should be
;; one-line too.
(not previous-is-brackets)
(not next-is-brackets)
;; If the parent is one-line, this should be one-line too.
(or
(json-par-token-outside-of-buffer-p previous-value)
(/= (save-excursion
(json-par-up-backward)
(line-beginning-position))
(save-excursion
(json-par-up-forward)
(line-beginning-position))))
(not (eq default-brackets-style 'one-line)))))))
(defun json-par-insert-square-brackets (&optional default-brackets-style)
"Insert a pair of square brackets before or after the current member.
See `json-par--insert-value' for details.
If the region is active, wrap it with the brackets instead.
See `json-par--wrap-region-with-brackets' for details.
Return a list (FINAL-START FINAL-END) where FINAL-START and FINAL-END are the
start and end of the brackets.
DEFAULT-BRACKETS-STYLE affects whether or not to insert line breaks when
inserting an empty array or object. It is one of the following:
- `one-line': no line breaks.
Example (`|' is the point): [|]
- `multiline': insert line breaks and indentation.
Example:
[
|
]
It is used only if the style cannot be guessed from the context:
- If the previous value or the next value is an array/object, the style is same
to those array/object. If one is multiline and another is one-line, the style
is multiline.
- If the parent object/array is one-line, the style is one-line.
- Otherwise, `default-brackets-style' is used.
If DEFAULT-BRACKETS-STYLE is omitted, `one-line' is used when called from Lisp
program. If called interactively, the value of
`json-par-default-brackets-style' is used."
(interactive
(list
json-par-default-brackets-style))
(unless default-brackets-style
(setq default-brackets-style 'one-line))
(json-par-insert-brackets "[" "]" default-brackets-style))
(push #'json-par-insert-square-brackets json-par--fixup-adviced-functions)
(defun json-par-insert-curly-brackets (&optional default-brackets-style)
"Insert a pair of curly brackets before or after the current member.
See `json-par--insert-value' for details.
If the region is active, wrap it with the brackets instead.
See `json-par--wrap-region-with-brackets' for details.
Then, prepend an empty key and a colon for each member if missing.
Return a list (FINAL-START FINAL-END) where FINAL-START and FINAL-END are the
start and end of the brackets.
DEFAULT-BRACKETS-STYLE affects whether or not to insert line breaks when
inserting an empty array or object. It is one of the following:
- `one-line': no line breaks.
Example (`|' is the point): [|]
- `multiline': insert line breaks and indentation.
Example:
[
|
]
It is used only if the style cannot be guessed from the context:
- If the previous value or the next value is an array/object, the style is same
to those array/object. If one is multiline and another is one-line, the style
is multiline.
- If the parent object/array is one-line, the style is one-line.
- Otherwise, `default-brackets-style' is used.
If DEFAULT-BRACKETS-STYLE is omitted, `one-line' is used when called from Lisp
program. If called interactively, the value of
`json-par-default-brackets-style' is used."
(interactive
(list
json-par-default-brackets-style))
(unless default-brackets-style
(setq default-brackets-style 'one-line))
(let* ((positions (json-par-insert-brackets "{" "}" default-brackets-style))
(start (nth 0 positions)))
(json-par--prepend-empty-keys-to-values-if-missing start)))
(push #'json-par-insert-curly-brackets json-par--fixup-adviced-functions)
(defun json-par--prepend-empty-keys-to-values-if-missing (start)
"Prepend an empty key and a colon for each members if missing.
START is the start position of the object."
(save-excursion
(goto-char start)
(forward-char)
(let (token
last-token)
(while (progn
(setq token (json-par-forward-token-or-list))
(not (json-par-token-close-curly-bracket-p token)))
(unless (or (json-par--object-key-p token)
(memq (json-par-token-type token) '(\, other :))
(eq (json-par-token-type last-token) ':))
(save-excursion
(goto-char (json-par-token-start token))
(insert "\"\": ")))
(setq last-token token)))))
(defun json-par-insert-double-quotes
(&optional arg action-when-inserting-double-quotes-at-end)
"Insert double quotes before or after the current member.
- If ARG is given or called with a prefix arg, unwrap the string.
See `json-par-destringify' for details.
Example (`|' is the point):
[ \"|1, 2, 3\" ]
↓
[ |1, 2, 3 ]
- Inside a string, insert an escaped double quote. If the point is just
after escape (backslash), insert just one double quote without backslash.
\"abc |def ghi\"
↓
\"abc \\\"|def ghi\"
If the string is not closed, insert a unescaped double quote.
- Inside a comment, insert just a double quote.
- If the point is just before the closing double quote, action depends on
ACTION-WHEN-INSERTING-DOUBLE-QUOTES-AT-END
- `insert': insert an escaped double quote.
- `exit': move the point after the string.
Defaults to `exit' when called from Lisp program, or the value of
`json-par-action-when-inserting-double-quotes-at-end' variable when called
interactively.
- If the region is active, wrap it with the double quotes.
[ |1, 2, 3| ]
↓
[ |\"1, 2, 3\"| ]
- Otherwise, insert a pair of double quotes.
See `json-par--insert-value' for details."
(interactive
(list
current-prefix-arg
json-par-action-when-inserting-double-quotes-at-end))
(unless action-when-inserting-double-quotes-at-end
(setq action-when-inserting-double-quotes-at-end 'insert))
(let (parser-state)
(if arg
(json-par-destringify)
(cond
;; Region is active
((use-region-p)
(json-par-stringify-region (region-beginning) (region-end)))
;; Inside a string
((save-excursion (nth 3 (setq parser-state (syntax-ppss))))
(cond
;; After escape
((json-par--escaped-p)
(insert-char ?\"))
;; Unclosed string
((save-excursion
(goto-char (json-par--string-like-beginning-position
parser-state))
(forward-sexp)
(or (not (eq (char-before) ?\"))
(not (memq (char-after)
'(nil ?: ?\] ?\) ?} ?\, ?\s ?\t ?\n ?/)))))
(insert-char ?\"))
;; Before closing quote
((eq (char-after) ?\")
(if (eq action-when-inserting-double-quotes-at-end 'exit)
(forward-char)
(insert "\\\"")))
;; Otherwise
(t
(insert "\\\""))))
;; Inside a comment
((json-par--string-like-beginning-position parser-state)
(insert "\""))
;; Otherwise
(t
(json-par--insert-key-or-value "\"\"")
(backward-char))))))
(push #'json-par-insert-double-quotes json-par--fixup-adviced-functions)
(defun json-par--escaped-p ()
"Return non-nil if the point is just after odd number of escape characters.
Return nil otherwise."
(and (eq (char-before) ?\\)
(save-excursion
(= (mod (skip-chars-backward "\\\\") 2) 1))))
(defun json-par-stringify-region (start end)
"Wrap the region from START to END with double quotes.
Special characters in the region is escaped.
If START or END is inside strings, escaped double quotes are inserted."
(interactive "r")
(save-excursion
(let* ((start-atom (json-par--current-atom start))
(end-atom (json-par--current-atom end))
(start-in-string (and (json-par-token-string-p start-atom)
(json-par-token-inside-p start-atom)))
(end-in-string (and (json-par-token-string-p end-atom)
(json-par-token-inside-p end-atom))))
(setq end (copy-marker (json-par-escape-region-for-string start end)))
(goto-char end)
(insert-before-markers (if end-in-string "\\\"" "\""))
(goto-char start)
(insert (if start-in-string "\\\"" "\""))
(json-par--free-marker end))))
(push #'json-par-stringify-region json-par--fixup-adviced-functions)
(defun json-par-destringify ()
"Unwrap strings in the region or at the point.
If the region is active, unwrap all strings in it.
Otherwise, unwrap the nearest string.
Escaped characters in the strings are unescaped.
Inhibit fixing up until next modification."
(interactive)
(let* ((parser-state (save-excursion (syntax-ppss)))
(current-atom (json-par--current-atom parser-state))
(string-like-beginning-position
(json-par--string-like-beginning-position parser-state)))
(cond
;; Region is active
((use-region-p)
(json-par-destringify-region (region-beginning) (region-end)))
;; Inside a string
((and (json-par-token-string-p current-atom)
(json-par-token-inside-p current-atom))
(save-excursion
(goto-char (json-par-token-start current-atom))
(json-par--destringify-after)))
;; Inside a comment
(string-like-beginning-position
nil)
;; Before or after a string
((or
(save-excursion
(json-par--forward-spaces)
(eq (char-after) ?\"))
(save-excursion
(json-par--backward-spaces)
(eq (char-before) ?\")))
;; Prefer nearer string
(if (or (eq (char-after) ?\")
(and
(not (eq (char-before) ?\"))
(or (save-excursion
(skip-chars-forward "\s\t")
(eq (char-after) ?\"))
(and
(not (save-excursion
(skip-chars-backward "\s\t")
(eq (char-before) ?\")))
(save-excursion
(json-par--forward-spaces)
(eq (char-after) ?\"))))))
(save-excursion
(json-par--forward-spaces)
(json-par--destringify-after))
(save-excursion
(json-par-backward-token)
(json-par--destringify-after))))
;; Otherwise; do nothing
(t
nil))))
(push #'json-par-destringify json-par--fixup-adviced-functions)
(defun json-par-destringify-region (start end)
"Unwrap strings overlapping the region from START to END.
Escaped characters in the strings are unescaped.
Inhibit fixing up until next modification.
Return the end position."
(interactive "r")
(save-excursion
(let ((start-string-like-beginning-position
(json-par--string-like-beginning-position start))
(end-string-like-beginning-position
(json-par--string-like-beginning-position end))
token)
(when start-string-like-beginning-position
(setq start start-string-like-beginning-position))
(when end-string-like-beginning-position
(goto-char end-string-like-beginning-position)
(if (eq (char-after) ?\")
(json-par-forward-token)
(json-par--forward-spaces))
(setq end (point)))
(setq end (copy-marker end))
(goto-char start)
(while (progn
(setq token (json-par-forward-token))
(and (< (json-par-token-start token) end)))
(when (json-par-token-string-p token)
(goto-char (json-par-token-start token))
(json-par--destringify-after)))
(json-par--free-marker end))))
(push #'json-par-destringify-region json-par--fixup-adviced-functions)
(defun json-par--destringify-after ()
"Unwrap a string just after the point.
Inhibit fixing up until next modification."
(let ((start (point-marker))
(end (save-excursion (json-par-forward-token) (point-marker))))
(goto-char end)
(delete-char -1)
(goto-char start)
(delete-char 1)
(goto-char (json-par-unescape-region-for-string start end))
(setq json-par--inhibit-fixup-tick (buffer-chars-modified-tick))))
(defun json-par--insert-key-or-value (value)
"Insert VALUE as a key or a value.
It is almost same as `json-par--insert-value' but if the point is inside an
object and not where a value is expected but missing, insert as a key instead.
Examples (`|' is the point):
Where a value is expected but not a key:
{ \"a\": | }
↓
{ \"a\": \"new_value\"| }
{ |\"a\": 1 }
↓
{ \"\": \"new_value\"|, \"a\": 1 }
Where a key is expected:
{ |: 1 }
↓
{ \"new_key\": 1| }
{ \"a\": 1, | }
↓
{ \"a\": 1, \"new_key\": | }
{ \"a\": 1| }
↓
{ \"a\": 1, \"new_key\": | }"
(json-par--out-comment)
(json-par--out-atom t)
(let* ((next-token (save-excursion (json-par-forward-token)))
(previous-token (save-excursion (json-par-backward-token)))
(parent-token (json-par--parent-token))
(inside-object (json-par-token-open-curly-bracket-p parent-token)))
(cond
;; Not inside an object.
((not inside-object)
(json-par--insert-value value t))
;; Place expecting a value but not a key.
;;
;; A value is expected:
;; {
;; "a": |
;; : 1
;; }
;;
;; A key is expected:
;; {
;; "a":
;; |: 1
;; }
;;
;; A Value is expected:
;; { "a": | : 1 }
((and (json-par-token-colon-p previous-token)
(or (not (json-par-token-atom-p next-token))
(json-par--object-key-p next-token t))
(not (json-par-token-open-bracket-p next-token))
(or (not (json-par-token-colon-p next-token))
(not (json-par--same-line-p (json-par-token-start next-token)
(point)))
(json-par--same-line-p (json-par-token-end previous-token)
(point))))
(json-par--insert-value value t))
;; Before colon without a key.
((and (json-par-token-colon-p next-token)
(not (json-par--object-key-p previous-token)))
(json-par--insert-key value previous-token next-token))
;; Value without key.
((and (or (json-par-token-open-bracket-p next-token)
(json-par-token-atom-p next-token))
(not (json-par--object-key-p next-token))
(not (json-par-token-colon-p previous-token)))
(json-par--insert-key value previous-token next-token))
;; Otherwise; insert a new member and replace the key with the `value'.
(t
(json-par--insert-value "0" t)
(delete-char -1)
(json-par-backward-token)
(let ((key-token (json-par-backward-token)))
(delete-region (json-par-token-start key-token)
(json-par-token-end key-token)))
(insert value)))))
(defun json-par--insert-key (key previous-token next-token)
"Insert KEY before or after the current member as a object key.
Insert spaces, commas, a colon if needed.
Example (`|' is the point):
{ |: 1 }
↓
{ \"new_key\": 1| }
{ \"a\": 1| }
↓
{ \"a\": 1, \"new_key\": | }
{ \"a\": |1 }
↓
{ \"new_key\": | , \"a\": 1}
PREVIOUS-TOKEN and NEXT-TOKEN is the previous token and the next token around
the point."
(unless
(or (json-par-token-open-bracket-p previous-token)
(json-par-token-comma-p previous-token)
(json-par-token-outside-of-buffer-p previous-token))
(json-par-insert-comma))
(unless (memq (char-before) '(?\s ?\t ?\n nil))
(insert-char ?\s))
(insert key)
(save-excursion
(unless (json-par-token-colon-p next-token)
(insert-char ?:)
(unless (memq (char-after) '(?\s ?\t ?\n))
(insert-char ?\s)))))
(defconst json-par--escape-table
(let ((table (make-char-table nil)))
(set-char-table-range table ?\" "\\\"")
(set-char-table-range table ?\\ "\\\\")
(set-char-table-range table ?\u0008 "\\b")
(set-char-table-range table ?\u000C "\\f")
(set-char-table-range table ?\u000A "\\n")
(set-char-table-range table ?\u000D "\\r")
(set-char-table-range table ?\u0009 "\\t")
table)
"Character table from raw characters to two-character escape sequences.
Only mandatory characters; solidus (\"/\") is not mandatory to escape.")
(defconst json-par--unescape-table
(let ((table (make-char-table nil)))
(set-char-table-range table ?\" "\"")
(set-char-table-range table ?\\ "\\")
(set-char-table-range table ?/ "/")
(set-char-table-range table ?b "\u0008")
(set-char-table-range table ?f "\u000C")
(set-char-table-range table ?n "\u000A")
(set-char-table-range table ?r "\u000D")
(set-char-table-range table ?t "\u0009")
table)
"Character table from two-character escape sequences to raw characters.
The keys are the second characters of escape sequences.")
(defun json-par-escape-region-for-string (start end)
"Escape the region from START to END to be a valid string.
Escape only mandatory characters; quotation mark, reverse solidus, and the
control characters (U+0000 through U+001F). Solidus (\"/\") is left as is.
Return the end position of the region."
(interactive "r")
(save-excursion
(save-match-data
(goto-char start)
(setq end (copy-marker end))
(while (re-search-forward "[\u0000-\u001F\"\\]" end t)
(let* ((char (char-after (match-beginning 0)))
(short-form (char-table-range json-par--escape-table char)))
(replace-match (or short-form (format "\\u%04x" char)) t t)))
(json-par--free-marker end))))
(push #'json-par-escape-region-for-string json-par--fixup-adviced-functions)
(defun json-par-unescape-region-for-string (start end)
"Unescape the region from START to END as a string.
Invalid sequences are left as is.
Return the end position of the region."
(interactive "r")
(save-excursion
(save-match-data
(goto-char start)
(setq end (copy-marker end))
(while (re-search-forward "\\\\." end t)
(let* ((char (char-after (1+ (match-beginning 0))))
(short-form (char-table-range json-par--unescape-table char))
(match-beginning (match-beginning 0))
(match-end (match-end 0))
high-surrogate
low-surrogate
replacement)
(cond
;; Two-character sequence escape
(short-form
(replace-match short-form t t))
;; Six-character sequence escape
((and (eq char ?u)
(looking-at "[0-9a-zA-Z]\\{4\\}"))
(setq match-end (match-end 0))
(setq high-surrogate
(string-to-number (match-string-no-properties 0) 16))
(if (and (<= #xD800 high-surrogate #xDBFF)
(save-excursion
(goto-char match-end)
(looking-at "\\\\u\\([0-9a-zA-Z]\\{4\\}\\)")))
;; This may be a surrogate pair; proceeding.
(progn
(setq match-end (match-end 0))
(setq low-surrogate
(string-to-number (match-string-no-properties 1) 16))
(if (<= #xDC00 low-surrogate #xDFFF)
;; This is actually a surrogate pair.
(setq replacement
(string (logior
#x10000
(ash (- high-surrogate #xD800) 10)
(- low-surrogate #xDC00))))
;; Not a surrogate pair; go back to the first sequence
(setq match-end (- match-end 6))
(setq replacement (string high-surrogate))))
;; Not a surrogate pair
(setq replacement (string high-surrogate)))
(goto-char match-beginning)
(delete-region match-beginning match-end)
(insert-before-markers replacement))
;; Invalid sequence; leave it as is
(t nil))))
(json-par--free-marker end))))
(push #'json-par-unescape-region-for-string json-par--fixup-adviced-functions)
(defvar-local json-par--protection-markers nil
"List of markers protecting commas from fixing up.
Markers are placed before new commas or open brackets before them.
Commas with markers or commas immediately following the commas, excluding
spaces and newlinews, are protected.
When a value is inserted after the comma or the open bracket, its maker is
removed.")
(defun json-par--add-protection-marker (&optional position)
"Add marker at POSITION to the list of protection markers.
If POSITION is omitted or nil, the point is used.
See also `json-par--protection-markers'."
(let ((marker (copy-marker (or position (point)))))
(set-marker-insertion-type marker t)
(push marker json-par--protection-markers)))
(defun json-par--record-protection-markers-to-undo-list
(&optional protection-markers)
"Record current PROTECTION-MARKERS to the undo list.
If PROTECTION-MARKERS is omitted, default to `json-par--protection-markers'."
(push (list
'apply
(lambda (protection-markers)
(setq json-par--protection-markers protection-markers))
(or protection-markers json-par--protection-markers))