-
Notifications
You must be signed in to change notification settings - Fork 22
/
puni.el
2420 lines (2168 loc) · 93.6 KB
/
puni.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
;;; puni.el --- Parentheses Universalistic -*- lexical-binding: t -*-
;; Copyright (C) 2021 Hao Wang
;; Author: Hao Wang <amaikinono@gmail.com>
;; Maintainer: Hao Wang <amaikinono@gmail.com>
;; Created: 08 Aug 2021
;; Keywords: convenience, lisp, tools
;; Homepage: https://github.com/AmaiKinono/puni
;; Version: 0
;; Package-Requires: ((emacs "26.1"))
;; 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:
;; Puni is a package for structured editing. Its main features are:
;; - A set of customizable soft deletion commands, enabled by `puni-mode'.
;; Soft deletion means deleting while keeping expressions balanced.
;; - A simple API `puni-soft-delete-by-move', for defining your own soft
;; deletion commands.
;; - Sexp navigating and manipulating commands.
;; - Completely based on Emacs built-in mechanisms, doesn't contain any
;; language-specific logic, yet work on many major modes.
;; It's recommended to use Puni with `electric-pair-mode'. Read README.md to
;; know more about Puni. If you haven't received such a file, please visit
;; https://github.com/AmaiKinono/puni.
;;; Code:
;; To see the outline of this file, run `outline-minor-mode', then
;; `outline-hide-body'. Another way is to run `occur' with the query:
;; ^;;;;* \|^(
;;;; Libraries
(require 'cl-lib)
(require 'rx)
(require 'pulse)
(require 'subr-x)
;;;; User options
(defgroup puni nil
"Customizable soft deletion."
:group 'convenience
:group 'lisp
:group 'tools
:prefix "puni-"
:link '(url-link "https://github.com/AmaiKinono/puni"))
(defcustom puni-blink-region-face nil
"A symbol of the face used for blinking region.
Nil means use `pulse-highlight-start-face'."
:type '(choice (const :tag "Default" nil)
(symbol :tag "Face"))
:group 'puni)
(defcustom puni-confirm-when-delete-unbalanced-active-region t
"Whether deleting unbalanced active regions needs confirmation."
:type 'boolean
:group 'puni)
(defcustom puni-blink-for-slurp-barf t
"Whether blinking the moved delimiter when slurping & barfing."
:type 'boolean
:group 'puni)
;;;; Internals
(defvar puni--debug nil
"Turn on debug mode when non-nil.")
;;;;; Probes
(defun puni--line-empty-p ()
"Return t if current line is empty or contains only spaces."
(save-excursion
(beginning-of-line)
(looking-at (rx line-start (* space) line-end))))
(defun puni--in-string-p ()
"Return t if point is in a string.
Notice this returns nil if point is before the opening quote, or
after the end quote."
(eq (syntax-ppss-context (syntax-ppss)) 'string))
(defun puni--in-comment-p ()
"Return t if point is in a comment.
Notice this returns nil if point is before/in the opening
delimiter, or after/in the end delimiter."
(eq (syntax-ppss-context (syntax-ppss)) 'comment))
;;;;; Errors
(defun puni--error-if-before-point (bound)
"Error if BOUND is non-nil and is before point."
(when (and bound (< bound (point)))
(error "BOUND is before point")))
(defun puni--error-if-after-point (bound)
"Error if BOUND is non-nil and is after point."
(when (and bound (> bound (point)))
(error "BOUND is after point")))
;;;;; Syntax
;; Ref: https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Class-Table.html
;; and https://debbugs.gnu.org/cgi/bugreport.cgi?bug=37452
(defun puni--syntax-class-to-char (syntax-class)
"Return the designator char of SYNTAX-CLASS."
(aref " .w_()'\"$\\/<>@!|" syntax-class))
(defun puni--syntax-char-after (&optional point)
"Return the syntax code after POINT, described by a char.
When POINT is nil, return the syntax code after the current
point. When POINT doesn't exist, or there's no char after POINT,
return nil.
For the meaning of the returned char, see `modify-syntax-entry'."
(let ((point (or point (point))))
(unless (or (< point (point-min))
(>= point (point-max)))
(puni--syntax-class-to-char (syntax-class (syntax-after point))))))
;;;;; Helpers
(defun puni--move-within (action limit)
"Call ACTION. Return t if it moves within LIMIT, otherwise return nil.
ACTION is a function that moves the point. LIMIT should be a
position after point if ACTION moves forward, or a position
before point if ACTION moves backward.
When ACTION moves within LIMIT, return t. Otherwise go back to
the start and return nil.
When LIMIT is nil, simply call ACTION."
(if (null limit)
(funcall action)
(let ((from (point))
(to (progn (funcall action) (point))))
(cond
((> to from) (if (<= to limit) t
(goto-char from)
nil))
((< to from) (if (>= to limit) t
(goto-char from)
nil))
(t nil)))))
;;;;; Basic move: blank
;; NOTE: ALl "basic moves" (take those move forward as example), except those
;; explicitely deals with blanks, assumes they starts in a position where no
;; spaces are after point, and will go to a position where no spaces are before
;; point.
(defun puni--forward-blanks (&optional bound)
"Jump forward whitespaces and newlines.
Return t if success. When BOUND is non-nil, don't go further
than BOUND."
(puni--error-if-before-point bound)
(when (looking-at (rx (+ (or space "\n"))))
(goto-char (match-end 0))
(when (and bound (> (point) bound))
(goto-char bound))
t))
(defun puni--backward-blanks (&optional bound)
"Backward version of `puni--forward-blanks'."
(puni--error-if-after-point bound)
(unless (bobp)
(let ((from (point)))
(while (and (or (null bound) (> (point) bound))
(or (puni--backward-syntax " " bound)
(when (bolp) (forward-char -1) t))))
(let ((to (point)))
(unless (eq from to) to)))))
;;;;; Basic move: char
(defun puni--skip-chars-forward (string &optional bound)
"Go forawrd accross chars in STRING.
When BOUND is non-nil, stop before BOUND.
This is the same as `skip-chars-forward', except that:
- It signals an error is BOUND is before point at the first place.
- If it fails, return nil.
- If sucess, return the point after move.
See `skip-chars-forward' for the real meaning of STRING."
(puni--error-if-before-point bound)
(pcase (skip-chars-forward string bound)
(0 nil)
(_ (point))))
(defun puni--skip-chars-backward (string &optional bound)
"Backward version of `puni--skip-chars-forward'."
(puni--error-if-after-point bound)
(pcase (skip-chars-backward string bound)
(0 nil)
(_ (point))))
(defun puni--forward-same-char (&optional bound)
"Go forward consecutive same characters.
It returns the point after move. If BOUND is non-nil, stop
before BOUND. If it fails, return nil."
(unless (eobp)
;; See `skip-chars-forward'. I've tested that `regexp-quote' works for the
;; situations mentioned there.
(puni--skip-chars-forward (regexp-quote (char-to-string (char-after)))
bound)))
(defun puni--backward-same-char (&optional bound)
"Backward version of `puni--forward-same-char'."
(unless (bobp)
(puni--skip-chars-backward (regexp-quote (char-to-string (char-before)))
bound)))
;;;;; Basic move: syntax
(defun puni--forward-syntax (syntax &optional bound)
"Go forward across chars in specified syntax classes.
SYNTAX is a string of syntax code chars. When BOUND is non-nil,
stop before BOUND.
This is the same as `skip-syntax-forward', except that:
- It signals an error is BOUND is before point at the first place.
- If it fails, return nil.
- If sucess, return the point after move."
(puni--error-if-before-point bound)
(pcase (skip-syntax-forward syntax bound)
(0 nil)
(_ (point))))
(defun puni--backward-syntax (syntax &optional bound)
"Backward version of `puni--forward-syntax'."
(puni--error-if-after-point bound)
(pcase (skip-syntax-backward syntax bound)
(0 nil)
(_ (point))))
(defun puni--forward-same-syntax (&optional bound)
"Move point past all characters with the same syntax class.
It returns the point after move. If BOUND is non-nil, stop
before BOUND.
This is more robust than `forward-same-syntax' because it takes
`syntax-table' text properties into account. See the docstring
of `char-syntax'."
(when-let ((syntax (puni--syntax-char-after)))
(puni--forward-syntax (char-to-string syntax) bound)))
(defun puni--backward-same-syntax (&optional bound)
"Backward version of `puni--forward-same-syntax'."
(when-let (syntax (puni--syntax-char-after (1- (point))))
(puni--backward-syntax (char-to-string syntax) bound)))
;;;;; Basic move: symbol
(defun puni--symbol-syntax-p (&optional point)
"Check if char at POINT has symbol or word syntax.
This includes the situation where it's a char with \"escape\"
syntax (like backslash), but followed by a char with symbol or
word syntax."
(let ((point (or point (point))))
(or (memq (puni--syntax-char-after point) '(?_ ?w))
(and (eq (puni--syntax-char-after point) ?\\)
(memq (puni--syntax-char-after (1+ point)) '(?_ ?w))))))
(defun puni--symbol-prefix-p (&optional point)
"Check if char at POINT is a symbol prefix.
This means it's a char with \"'\" syntax, and the point after it
satisfies `puni--symbol-syntax-p'."
(let ((point (or point (point))))
(save-excursion
(goto-char point)
;; In case ther are multiple prefixes... Could that be a case?
(and (puni--forward-syntax "'")
(puni--symbol-syntax-p)))))
(defun puni--forward-symbol (&optional bound)
"Move forward an symbol if there's one in front.
A symbol is a symbol in Emacs convention, allowing escaped chars
with symbol syntax in it, plus an expression prefix before it (if
exist).
If BOUND is non-nil, stop before BOUND."
;; It may be a good idea to treat a series of punctuations as a symbol (think
;; of operators). Unfortunately there are major modes where "<>" should be
;; delimiters, but are given the punctuation syntax.
(puni--error-if-before-point bound)
(let ((from (point)))
(when (puni--symbol-prefix-p)
(puni--forward-same-syntax bound))
(while (and (puni--symbol-syntax-p)
(puni--forward-same-syntax bound)))
(let ((to (point)))
(unless (eq from to)
to))))
(defun puni--backward-symbol (&optional bound)
"Backward version of `puni--forward-symbol'."
(puni--error-if-after-point bound)
(let ((from (point)))
(while (and (puni--symbol-syntax-p (1- (point)))
(puni--backward-same-syntax bound)))
(let ((to (point)))
(unless (eq from to)
(puni--backward-syntax "'" bound)
(point)))))
;;;;; Basic move: string
(defun puni--forward-string ()
"Move forward a string.
Return the point if success, otherwise return nil."
(let ((from (point))
after-quote to)
(unless (puni--in-string-p)
(save-excursion
(when (progn (puni--forward-syntax "\\")
(or (puni--forward-syntax "\"")
(puni--forward-syntax "|")))
(setq after-quote (point))
;; The default `forward-sexp' could jump over a string.
;; `forward-sexp-function' from the major-mode sometimes doesn't,
;; when they jump to the end of a further delimiter.
(let ((forward-sexp-function nil))
(goto-char from)
(or (puni--primitive-forward-sexp)
;; This happens when there's no closing quote. In this
;; situation, it's safe to delete the opening quote.
(goto-char after-quote)))
(setq to (point))))
(when (and to (not (eq from to)))
(goto-char to)))))
(defun puni--backward-string ()
"Backward version of `puni--forward-string'."
(let ((from (point))
before-quote to)
(unless (puni--in-string-p)
(save-excursion
(when (or (puni--backward-syntax "\"")
(puni--backward-syntax "|"))
(setq before-quote (point))
(let ((forward-sexp-function nil))
(goto-char from)
(or (puni--primitive-backward-sexp)
(goto-char before-quote)))
(setq to (point))))
(when (and to (not (eq from to)))
(goto-char to)))))
;;;;; Basic move: comment
(defun puni--forward-comment-block ()
"Jump forward a whole comment block.
Return the point if success."
(let ((from (point))
to)
(save-excursion
;; `forward-comment' could do its work but return nil, e.g., when we are
;; before a single line comment at the end of the file, and there's no
;; trailing newline.
(when (progn (forward-comment 1)
(not (eq (point) from)))
(setq to (point))))
(when to (goto-char to))))
(defun puni--backward-comment-block ()
"Jump backward a whole comment block.
Return the point if success."
(when (forward-comment -1)
(puni--forward-blanks)
(point)))
;;;;; Basic move: single line comment
;; This section has nothing to do with core functionality, but only the
;; interactive `puni-forward/backward-sexp' commands.
(defun puni--begin-of-single-line-comment-p ()
"Return t if point is at the opening delimiter of a single line comment."
(save-excursion
(and (not (puni--forward-blanks))
(puni--forward-comment-block)
(or (eq (char-before) ?\n)
(and (eobp)
(not (memq (puni--syntax-char-after (1- (point)))
'(?> ?!))))))))
(defun puni--end-of-single-line-comment-p ()
"Return t if point is after the end delimiter of a single line comment.
The end delimiter of such a comment is a newline, so this means
the point is at the beginning of next line (or next n lines, if
next n-1 lines are empty)."
(and (eq (puni--syntax-char-after (1- (point))) ?>)
(eq (char-before) ?\n)
(save-excursion (forward-comment -1))))
(defun puni--forward-consecutive-single-line-comments ()
"Jump forward a series of single-line comments.
Return the point if success."
(let ((from (point))
to)
(save-excursion
(while (and (puni--begin-of-single-line-comment-p)
(progn (forward-line)
(unless (eobp) (beginning-of-line))
(puni--forward-syntax " ")
t)))
(unless (eq from (point))
(puni--backward-blanks from)
(setq to (point))))
(when to (goto-char to))))
(defun puni--backward-consecutive-single-line-comments ()
"Jump backward a series of single-line comments.
Return the point if success."
(let ((from (point))
to)
(save-excursion
(while (and (puni--end-of-single-line-comment-p)
(forward-comment -1)
(progn (puni--backward-syntax " ")
(puni--end-of-single-line-comment-p))
(not (bobp))
(save-excursion (forward-line -1)
(puni--begin-of-single-line-comment-p))))
(unless (>= (point) from)
(puni--forward-blanks from)
(setq to (point))))
(when to (goto-char to))))
;;;;; Basic move: sexp
;; In this section, we build necessary components for the final
;; `puni-strict-forward/backward-sexp' functions.
(defun puni--forward-same-char-and-syntax (&optional bound)
"Move forward consecutive same chars with same syntax.
When BOUND is non-nil, stop before BOUND.
Return the point if success, otherwise return nil."
(let* ((char-bound (save-excursion
(puni--forward-same-char bound)))
(syntax-bound (save-excursion
(puni--forward-same-syntax bound))))
(when (and char-bound syntax-bound)
(goto-char (min char-bound syntax-bound)))))
(defun puni--backward-same-char-and-syntax (&optional bound)
"Backward version of `puni--forward-same-char-and-syntax'."
(let* ((char-bound (save-excursion
(puni--backward-same-char bound)))
(syntax-bound (save-excursion
(puni--backward-same-syntax bound))))
(when (and char-bound syntax-bound)
(goto-char (max char-bound syntax-bound)))))
(defun puni--forward-syntax-block (&optional limit)
"Move forward a syntax block.
Moving forward the following things are tried in turn:
- a symbol, string, or comment block
- a pair of parentheses (defined by the syntax table)
- a punctuation forward (if there is one)
- chars with the same syntax
Return the point if success, otherwise return nil.
When LIMIT is non-nil, and there are multiple syntax constructs
after point, choose one that ends before LIMIT. One example is:
\"|;\"
Emacs thinks there's a comment after the point to the line end.
But when we move inside the string, we want to move forward the
\";\"."
(let ((syntax-char (puni--syntax-char-after)))
(or (puni--move-within #'puni--forward-symbol limit)
(puni--move-within #'puni--forward-string limit)
(puni--move-within #'puni--forward-comment-block limit)
(when (memq syntax-char '(?\( ?$))
(let ((forward-sexp-function nil))
(puni--move-within #'puni--primitive-forward-sexp limit)))
(when (eq syntax-char ?.)
(progn (forward-char) (point)))
(puni--move-within #'puni--forward-same-char-and-syntax limit))))
(defun puni--backward-syntax-block (&optional limit)
"Backward version of `puni--forward-syntax-block'."
(let ((syntax-char (puni--syntax-char-after (1- (point)))))
(or (puni--move-within #'puni--backward-symbol limit)
(puni--move-within #'puni--backward-string limit)
(when (memq syntax-char '(?\) ?$))
(let ((forward-sexp-function nil))
(puni--move-within #'puni--primitive-backward-sexp limit)))
(when (eq syntax-char ?.)
(progn (forward-char -1) (point)))
(puni--move-within #'puni--backward-same-char-and-syntax limit))))
(defun puni--forward-sexp-wrapper (&optional n)
"A wrapper around `forward-sexp'.
Move forward N sexp, return the point if success, otherwise
return nil.
This wrapper is here since `forward-sexp' can fail differently in
different major modes, e.g., the built-in one for Lisp will throw
a `scan-error', the one from `nxml-mode' throws a plain error,
while the one from `web-mode' just does nothing and returns nil."
(condition-case _
(let ((from (point))
(to (progn (forward-sexp n) (point))))
(unless (eq from to) to))
(error nil)))
(defun puni--primitive-forward-sexp ()
"Move forward a sexp by `forward-sexp'.
This fixes some of its behaviors, see the implementation."
;; In Lisp mode:
;;
;; symbol|,__
;;
;; "_" means space, and its end is the end of buffer. Try
;; `forward/backward-sexp'.
(if (puni--forward-sexp-wrapper)
(progn (puni--backward-syntax " ")
(point))
;; In lisp mode, it's common to have this when editing:
;;
;; (foo |')
;;
;; `forward-sexp' can't parse forward since there's no expression after the
;; "'", but we should be able to delete it.
(when (eq (puni--syntax-char-after) ?')
(forward-char) (point))))
(defun puni--primitive-backward-sexp ()
"Backward version of `puni--primitive-forward-sexp'."
(if (puni--forward-sexp-wrapper -1)
(progn (puni--forward-syntax " ")
(point))
(when (eq (puni--syntax-char-after (1- (point))) ?')
(backward-char) (point))))
;; NOTE: The real challenge is what to do when the delimiter is not the
;; symbol/char at point, but something like "bound of symbol" or "newline"?
(defun puni--pair-or-in-delim-p (beg end)
"Return t if BEG and END is a pair of delimiter, or in the same delimiter.
This uses syntax table and some heuristic, and is not completely
reliable. It's also not generic, as it's designed only for a
branch in `puni--inside-delim-p'. So it assumes one of BEG and
END is the bound of delimiter."
(when (eq beg end) (error "BEG is the same as END"))
(or (eq beg (1- end))
(eq (save-excursion (goto-char beg)
(puni--forward-same-syntax end))
end)
(let ((beg-syntax (puni--syntax-char-after beg))
(end-syntax (puni--syntax-char-after (1- end)))
(beg-char (char-after beg))
(end-char (char-before end)))
;; If beg & end are a pair of delimiters, we think beg is paired with
;; end.
(or (and (eq beg-syntax ?\() (eq end-syntax ?\)))
(and (eq beg-syntax ?<) (eq end-syntax ?>))
;; If we've reached here, we need to consider the situations
;; where BOUND is a delimiter (as assumed), but doesn't have
;; delimiter syntax.
(and (eq beg-syntax ?.) (eq end-syntax ?.)
(eq beg-char ?<) (eq end-char ?>))
(and (eq beg-syntax end-syntax)
(eq beg-char end-char))))))
(defun puni--inside-delim-p (pt beg end direction)
"See if PT is inside the delimiters at BEG or END.
if DIRECTION is `forward', check if the char after PT is inside
any of the delimiters, or if DIRECTION is `backward', check the
char before PT instead.
By \"inside\" we mean: It's part of, or paired with the
beginning/end delimiter, and deleting it will cause unbalanced
state."
(unless (< beg pt end) (error "PT is not between BEG and END"))
;; Assume a string can't be a delimiter. We also assume a symbol can't be a
;; delimiter. This is not true, but many major modes thinks "a = b" is a
;; sexp, where it's actually safe to delete a or b. In this situation, it's
;; the "bound of symbol" being the delimiter, not the symbol itself.
(unless (or (save-excursion (goto-char beg) (or (puni--forward-string)
(puni--forward-symbol)))
(save-excursion (goto-char end) (or (puni--backward-string)
(puni--backward-symbol)))
;; If BEG is the beginning of a single line comment, and pt is
;; inside consecutive comment-opening delimiter chars, we also
;; don't think it's inside the delimiter, as it's common to
;; delete them one by one.
(save-excursion
(goto-char beg)
(and (puni--begin-of-single-line-comment-p)
(looking-at (concat (regexp-quote (char-to-string
(char-after)))
"*"))
(> (match-end 0) (pcase direction
('forward pt)
('backward (1- pt))
(_ (error "Invalid DIRECTION")))))))
(pcase direction
('forward (or (puni--pair-or-in-delim-p beg (1+ pt))
(puni--pair-or-in-delim-p pt end)))
('backward (or (puni--pair-or-in-delim-p beg pt)
(puni--pair-or-in-delim-p (1- pt) end)))
(_ (error "Invalid DIRECTION")))))
(defun puni--strict-primitive-forward-sexp ()
"Move forward a sexp, return the point if success, otherwise return nil.
If there's a balanced sexp in front, but jumping over it will
move us to a different depth in the whole syntax tree, this won't
go over it.
Notice this doesn't work well in strings, as the built-in
`forward-sexp' thinks the closing quote of this string, and the
opening quote of the next one, forms a string. It also doesn't
work well for balanced comment blocks. So we'll build on top of
this function until we have `puni-strict-forward-sexp', which
works on these situations."
(let* (beg end beg-of-maybe-another-sexp end-of-maybe-another-sexp
(unhandled-branch-handler
(lambda ()
(if puni--debug
(error "You've found an unhandled branch in Puni.
Direction: forward
beg: %s, end: %s, another-beg: %s, another-end: %s
Please report relevant part of the buffer, with the location of these points"
beg end
beg-of-maybe-another-sexp
end-of-maybe-another-sexp)
(setq end nil))))
(skipped-part-handler
(lambda ()
(setq end (save-excursion (puni--forward-syntax-block)))))
(inside-sexp-handler
(lambda ()
(if (puni--inside-delim-p beg beg-of-maybe-another-sexp end
'forward)
(setq end nil)
(setq end (save-excursion (puni--forward-syntax-block))))))
(no-sexp-forward-handler
(lambda ()
(unless (eobp)
(let ((after-syntax-block (save-excursion
(puni--forward-syntax-block))))
(save-excursion
(goto-char after-syntax-block)
(when (eq (puni--strict-primitive-backward-sexp) beg)
(setq end after-syntax-block))))))))
(save-excursion
(setq beg (point))
(setq end (puni--primitive-forward-sexp))
(setq beg-of-maybe-another-sexp (puni--primitive-backward-sexp))
(setq end-of-maybe-another-sexp (puni--primitive-forward-sexp)))
(cond
((null end)
;; `forward-sexp' thinks there's no sexp forward, but it can be wrong
;; when a punctuation is forward, and there's no sexp after that punct,
;; e.g., in `c-mode':
;;
;; { foo|; } // Call `forward-sexp'
(funcall no-sexp-forward-handler))
((or (null beg-of-maybe-another-sexp)
(null end-of-maybe-another-sexp))
(funcall unhandled-branch-handler))
((< beg-of-maybe-another-sexp beg)
;; Try this in `python-mode':
;;
;; n|.
;; # (forward -> backward -> forward sexp)
;;
;; The cause of this problem is not clear. What I know is: "." is a
;; punctuation, but is part of "n.", which is a sexp. The first jump
;; forward ignores the punctuation and jumps to the end of sexp after it.
;; The second jump forward jumps over the whole sexp "n.". If this
;; happens, we set the end of sexp at point to be after "s.".
(when (< beg end-of-maybe-another-sexp end)
(setq end end-of-maybe-another-sexp))
(cond
;; Try:
;;
;; foo bar |. (forward -> backward -> forward sexp)
;;
;; The cause is "." is completely ignored when searching for the bound
;; of a sexp. This is seen more clear when there are other words and
;; puncts after ".". If this happens, we consider the syntax block at
;; the beginning of the ignored part a sexp.
((<= end-of-maybe-another-sexp beg)
(funcall skipped-part-handler))
;; Shouldn't happen as we've handled it above.
((< beg end-of-maybe-another-sexp end)
(funcall unhandled-branch-handler))
;; This means the part between beg-of-maybe-another-sexp and end is a
;; sexp. e.g.:
;;
;; <p|>something</p>
;;
;; or
;;
;; <p>something|</p>
;;
;; We should also consider the situation where BEG is after a
;; expression prefix:
;;
;; '|()
;;
;; For this we don't need to anything.
((>= end-of-maybe-another-sexp end)
(unless (save-excursion
(goto-char beg-of-maybe-another-sexp)
(puni--forward-syntax "'" beg))
(funcall inside-sexp-handler)))))
;; This means there's a sexp between BEG and END. That's perfect, we
;; don't need to do anything more.
((eq beg-of-maybe-another-sexp beg) nil)
;; (> beg-of-maybe-another-sexp beg). e.g.,
;;
;; bar|. foo
(t
(funcall skipped-part-handler)))
(when end (goto-char end))))
(defun puni--strict-primitive-backward-sexp ()
"Backward version of `puni--strict-primitive-forward-sexp'."
(let* (beg end beg-of-maybe-another-sexp end-of-maybe-another-sexp
(unhandled-branch-handler
(lambda ()
(if puni--debug
(error "You've found an unhandled branch in Puni.
Direction: backward
beg: %s, end: %s, another-beg: %s, another-end: %s
Please report relevant part of the buffer, with the location of these points"
beg end
beg-of-maybe-another-sexp
end-of-maybe-another-sexp)
(setq beg nil))))
(skipped-part-handler
(lambda ()
(setq beg (save-excursion
(goto-char end)
(puni--backward-syntax-block)))))
(inside-sexp-handler
(lambda ()
(if (puni--inside-delim-p end beg end-of-maybe-another-sexp
'backward)
(setq beg nil)
(setq beg (save-excursion (puni--backward-syntax-block)))))))
(save-excursion
(setq end (point))
(setq beg (puni--primitive-backward-sexp))
(setq end-of-maybe-another-sexp (puni--primitive-forward-sexp))
(setq beg-of-maybe-another-sexp (puni--primitive-backward-sexp)))
(when (and beg end beg-of-maybe-another-sexp end-of-maybe-another-sexp)
(cond
((> end-of-maybe-another-sexp end)
(when (> end beg-of-maybe-another-sexp beg)
(setq beg beg-of-maybe-another-sexp))
(cond
((>= beg-of-maybe-another-sexp end)
(funcall skipped-part-handler))
((> end beg-of-maybe-another-sexp beg)
(funcall unhandled-branch-handler))
((<= beg-of-maybe-another-sexp end)
(unless (save-excursion
(goto-char beg)
(puni--forward-syntax "'" end))
(funcall inside-sexp-handler)))))
((eq end-of-maybe-another-sexp end) nil)
(t
(funcall skipped-part-handler)))
(when beg (goto-char beg)))))
(defun puni--strict-primitive-forward-sexp-in-thing (probe thing)
"Move strict forward a sexp in certain thing.
PROBE is a function that should return non-nil when the point is
in that thing, and nil when it's not.
Return the point after move. When we can't move forward, return
nil.
When the point is not in the construct in the first place, throw
and error \"Not in a THING\"."
(when (not (funcall probe))
(error "Not in a %s" thing))
(let ((to (save-excursion (puni--strict-primitive-forward-sexp)))
pos)
(when to
(save-excursion
(while (and (puni--forward-same-syntax to)
(funcall probe)
(setq pos (point))
(< (point) to))))
;; We've successfully reached TO, while keeping inside the thing.
(if (eq pos to)
(goto-char to)
;; If that's not the case, that means we jumped out of the thing by
;; `forward-sexp'. Try `forward-sexp' in:
;;
;; "| " ""
;;
;; When this happens, we go forward one syntax block while keeping in
;; the thing.
(let (goal)
(save-excursion
(puni--forward-syntax-block pos)
(when (funcall probe)
(setq goal (point))))
(when goal (goto-char goal)))))))
(defun puni--strict-primitive-backward-sexp-in-thing (probe thing)
"Backward version of `puni--strict-primitive-forward-sexp-in-thing'."
(when (not (funcall probe))
(error "Not in a %s" thing))
(let ((to (save-excursion (puni--strict-primitive-backward-sexp)))
pos)
(when to
(save-excursion
(while (and (puni--backward-same-syntax to)
(funcall probe)
(setq pos (point))
(> (point) to))))
(if (eq pos to)
(goto-char to)
(let (goal)
(save-excursion
(puni--backward-syntax-block pos)
(when (funcall probe)
(setq goal (point))))
(when goal (goto-char goal)))))))
(defun puni-strict-forward-sexp-in-string ()
"Move strict forward a sexp in when point is in string.
The default `(forward-sexp)' thinks the end quote of a string and
a beginning quote of the next string wraps a sexp. This fixed
that.
Return the point after move. When we can't move forward (i.e.,
hit the ending quote), return nil."
(puni--strict-primitive-forward-sexp-in-thing #'puni--in-string-p "string"))
(defun puni-strict-backward-sexp-in-string ()
"Backward version of `puni-strict-forward-sexp-in-string'."
(puni--strict-primitive-backward-sexp-in-thing #'puni--in-string-p "string"))
(defun puni-strict-forward-sexp-in-comment ()
"Move strict forward a sexp in when point is in a comment.
The default `(forward-sexp)' goes to the end of the sexp after
the end quote of the comment block. This fixed that.
Return the point after move. When we can't move forward (i.e.,
hit the ending quote), return nil.
Notice that a point inside the (multichar) quote is not
considered as in the comment."
(puni--strict-primitive-forward-sexp-in-thing #'puni--in-comment-p
"comment"))
(defun puni-strict-backward-sexp-in-comment ()
"Backward version of `puni-strict-forward-sexp-in-comment'."
(puni--strict-primitive-backward-sexp-in-thing #'puni--in-comment-p
"comment"))
;;;;; Indent
(defun puni--indent-line ()
"Indent current line.
This calls `indent-line-function' internally. However, if it
can't decide the exact column to indent to, and cycle through
possible indent offsets, this doesn't change the indentation.
In any situation, this tries to restore the cursor to a
reasonable position, and returns the change of indentation (can
be zero)."
;; `indent-for-tab-command' and some of the functions it calls checks if
;; `this-command' equals to `last-command', and the "cycle through possible
;; offsets" behavior may only be triggered if this is true.
(let* ((this-command 'indent-for-tab-command)
(last-command 'indent-for-tab-command)
(bol (line-beginning-position))
(orig-indentation (current-indentation))
(orig-col-relative-to-indentation (- (current-column)
orig-indentation))
(orig-spaces (buffer-substring bol
(save-excursion (back-to-indentation)
(point))))
(new-indentation (lambda () (progn
(indent-according-to-mode)
(current-indentation))))
(1st-indentation (funcall new-indentation))
(2nd-indentation (funcall new-indentation)))
(if (eq 1st-indentation 2nd-indentation)
(progn
(move-to-column (max 0 (+ 1st-indentation
orig-col-relative-to-indentation)))
(- 1st-indentation orig-indentation))
(delete-region bol (save-excursion (back-to-indentation)
(point)))
(goto-char bol)
(insert orig-spaces)
(move-to-column (+ orig-indentation
orig-col-relative-to-indentation))
0)))
(defun puni--column-of-position (pos)
"Column of position POS."
(save-excursion (goto-char pos)
(current-column)))
(defun puni--reindent-region (beg end original-column &optional no-recalculate)
"Reindent region between BEG and END.
Notice this doesn't reindent the region line-by-line like
`indent-region'. Rather, it assumes the region was originally
properly indented, and the column at its beginning was
ORIGINAL-COLUMN. Then, this column changed, because the text
before it changed, or the whole region was inserted at BEG. This
function then adjust the indentation of the lines in region, to
keep their relative indentation unchanged.
When necessary, the indentation is recalculated by
`puni--indent-line', and the relative indentation of the lines is
still kept unchanged. The \"necessary\" situations are:
- When the first non-empty line is not the line of BEG.
- When there are only blanks between BEG and the start of its line.
This can be overrided by NO-RECALCULATE.
This function tries to restore the cursor to a reasonable
position (if it's between BEG and END), and returns the change of
indentation of the rest lines in region (can be zero).
It's designed like this to keep manually adjusted indentation by
the user. When that's desired, this works better than
`indent-region'."
(let ((orig-col-relative-to-indentation
(when (<= beg (point) end)
(- (current-column) (current-indentation))))
offset)
;; Point at the end of region will change when we adjust indentation line
;; by line, so we use a marker.
(setq end (save-excursion (goto-char end)
(point-marker)))
(save-excursion
(goto-char beg)
(setq offset (- (current-column) original-column))
(unless no-recalculate
(cond
((looking-at (rx (* blank) line-end))
(progn (puni--forward-blanks end)
(if (eq (point) end)
(setq offset 0)
(setq offset (puni--indent-line)))))
((looking-back (rx line-start (* blank))
(line-beginning-position))
(setq offset (+ offset (puni--indent-line))))))
(unless (eq offset 0)
(while (and (eq (forward-line) 0)
(bolp)
(< (point) end))
(unless (puni--line-empty-p)
(let* ((orig-indentation (current-indentation))
(new-indentation (+ orig-indentation offset)))
(when (wholenump new-indentation)
(indent-line-to new-indentation)))))))
(when orig-col-relative-to-indentation
(move-to-column (max 0 (+ (current-indentation)
orig-col-relative-to-indentation))))
offset))
;;;;; Active region
(defun puni--active-region-direction ()
"Return the direction of active region.