-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar-subr.el
1411 lines (1263 loc) · 43.1 KB
/
ar-subr.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
;;; ar-subr.el --- A reliable beginning-of-defun and other helper functions -*- lexical-binding: t; -*-
;; Author: Andreas Röhler <[email protected]>, unless indicated
;; otherwise
;; Keywords: languages
;; 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:
;; Provide a reliable jump to start and end of a defun - and some more
;; subroutines.
;;
;;; Code:
(defvar ar-verbose-p nil
"Internal use only.")
;; (setq ar-verbose-p t)
(defun ar-toggle-verbose-p ()
"Toggle `ar-verbose-p'. "
(interactive)
(setq ar-verbose-p (not ar-verbose-p))
(message "ar-verbose-p: %s" ar-verbose-p))
(defvar ar-debug-p nil
"Switch into test-buffer.")
(defun ar-toggle-debug-p ()
"Toggle `ar-debug-p'. "
(interactive)
(setq ar-debug-p (not ar-debug-p))
(message "ar-debug-p: %s" ar-debug-p))
(defvar ar-keyboard-operator-chars "<>~!@#$^&*_-+=|:;\"',.?"
"(Insert known operator chars existing on a qwerty keybord")
(defun ar-insert-keyboard-operator-chars ()
(interactive "*")
(insert ar-keyboard-operator-chars))
(defun ar-insert-keyboard-operator-char-listing ()
(interactive "*")
(let* ((chars ar-keyboard-operator-chars)
(laenge (length chars)))
(insert "(list")
(dotimes (i laenge)
(insert (concat " ?" (char-to-string (aref chars i)))))
(insert ")")
(save-excursion
(search-backward ";")
(insert "\\"))
(save-excursion
(search-backward "\"")
(insert "\\"))))
(defvar ar-line-move-forward t)
(defvar ar-max-specpdl-size max-specpdl-size
"Protect against eternal loop.")
(defvar ar-literal-delim-re "\""
"When looking at beginning of string.")
(defalias 'ar-escaped 'ar-escaped-p)
;; (defmacro ar-escaped-p ()
;; "Return t if char is preceded by an odd number of backslashes."
;; `(save-excursion
;; (< 0 (% (abs (skip-chars-backward "\\\\")) 2))))
(defun ar-escaped-p (&optional pos)
"Return t if char at POS is preceded by an odd number of backslashes. "
(save-excursion
(when pos (goto-char pos))
(< 0 (% (abs (skip-chars-backward "\\\\")) 2))))
(defun ar-backslash-escaped-p (&optional pos)
"Return t if backslash before point is escaped. "
(save-excursion
(when pos (goto-char pos))
(< 0 (% (abs (skip-chars-backward "\\\\")) 1))))
(defun ar--skip-to-comment-or-semicolon ()
"Returns position if point was moved."
(let ((orig (point)))
(cond ((while (and (< 0 (abs (skip-chars-forward "^#;" (line-end-position))))
;; (sit-for 1)
(and (nth 8 (parse-partial-sexp (point-min) (point))) (skip-chars-forward "#;" (line-end-position)))))))
(and (< orig (point))(point))))
(defmacro ar--preceding-line-backslashed-p ()
"Return t if preceding line is a backslashed continuation line."
`(save-excursion
(beginning-of-line)
(skip-chars-backward " \t\r\n\f")
(and (eq (char-before (point)) ?\\ )
(ar-escaped-p))))
(defun ar-nth1-syntax ()
"Goto beginning of list at point."
)
(defvar ar-strip-chars-before "\\`[ \t\r\n]*"
"Regexp indicating which chars shall be stripped before STRING -
which is defined by `string-chars-preserve'.")
(defvar ar-strip-chars-after "[ \t\r\n]*\\'"
"Regexp indicating which chars shall be stripped after STRING -
which is defined by `string-chars-preserve'.")
(defun ar-fix-comment-start ()
"Comment at point might not have a padding."
(if (and comment-start (string-match "[ \t]$" comment-start))
(concat comment-start "*")
comment-start))
(defun ar-string-strip (str &optional chars-before chars-after)
"Return a copy of STR, CHARS removed.
`CHARS-BEFORE' and `CHARS-AFTER' default are whitespace chars,
i.e. spaces, tabs, carriage returns, newlines and newpages."
(let* (end
(s-c-b (or chars-before
ar-strip-chars-before))
(s-c-a (or chars-after
ar-strip-chars-after))
(erg (with-temp-buffer
(switch-to-buffer (current-buffer))
(insert str)
(skip-chars-backward s-c-a)
(setq end (point))
(goto-char (point-min))
(skip-chars-forward s-c-b)
(buffer-substring-no-properties (point) end))))
erg))
(defun ar--skip-to-semicolon-backward (&optional limit)
"Fetch the beginning of expression after a semicolon.
Returns position reached if point was moved.
Optional argument LIMIT limit."
(let ((orig (point)))
(and (< 0 (abs
(skip-chars-backward "^;" (or limit (line-beginning-position)))))
(skip-chars-forward " \t" (line-end-position))
(and (< (point) orig) (point)))))
(defmacro ar--current-line-backslashed-p ()
"Return t if current line is a backslashed continuation line."
`(save-excursion
(end-of-line)
(skip-chars-backward " \t\r\n\f")
(and (eq (char-before (point)) ?\\ )
(ar-escaped-p))))
;; Comment
(defun ar--skip-to-comment-or-comma ()
"Return position if comment or semicolon found."
(let ((orig (point)))
(cond ;; ((and done (< 0 (abs (skip-chars-forward "^#," (line-end-position))))
;; (member (char-after) (list ?# ?\,)))
;; (when (eq ?\, (char-after))
;; (skip-chars-forward "," (line-end-position))))
((and (< 0 (abs (skip-chars-forward "^#," (line-end-position))))
(member (char-after) (list ?# ?\,)))
(when (eq ?\, (char-after))
(skip-chars-forward "," (line-end-position))))
(t
(end-of-line)))
(skip-chars-backward " \t" (line-beginning-position))
(< orig (point))))
(defun ar--skip-to-comma-backward (&optional limit)
"Fetch the beginning of expression after a comma.
Returns position reached if point was moved.
Optional argument LIMIT limit."
(let ((orig (point)))
(and (< 0 (abs (skip-chars-backward "^," (or limit (line-beginning-position)))))
(skip-chars-forward " \t" (line-end-position))
(and (< (point) orig) (point)))))
(defconst ar-eq-assignment-re
(concat
"\\([^=\n:]+\\):?\\([^=\n:]*\\)\\(=\\)\\([^:]*\\)$")
"Regular expression matching assigment.")
(defconst ar-vertical-line-re
(concat
".*|")
"Regular expression matching a guard.")
;;; string-strip stuff ends here
(defcustom empty-line-p-chars "^[ \t\r]*$"
"Empty-line-p-chars."
:type 'regexp
:group 'convenience)
(defcustom ar-paired-openers (list ?‘ ?` ?< ?\( ?\[ ?{ ?\〈 ?\⦑ ?\⦓ ?\【 ?\⦗ ?\⸤ ?\「 ?\《 ?\⦕ ?\⸨ ?\⧚ ?\{ ?\( ?\[ ?\⦅ ?\「 ?\❰ ?\❮ ?\“ ?\‘ ?\❲ ?\⟨ ?\⟪ ?\⟮ ?\⟦ ?\⟬ ?\❴ ?\❪ ?\❨ ?\❬ ?\᚛ ?\〈 ?\⧼ ?\⟅ ?\⸦ ?\﹛ ?\﹙ ?\﹝ ?\⁅ ?\⦏ ?\⦍ ?\⦋ ?\₍ ?\⁽ ?\༼ ?\༺ ?\⸢ ?\〔 ?\『 ?\⦃ ?\〖 ?\⦅ ?\〚 ?\〘 ?\⧘ ?\⦉ ?\⦇)
"Specify the paired delimiter opening char."
:type '(repeat character)
:group 'sytactic-close)
(unless (functionp 'empty-line-p)
(defalias 'empty-line-p 'ar-empty-line-p))
(defun ar-empty-line-p (&optional iact)
"Return t if cursor is at an empty line, nil otherwise.
Optional argument IACT saying interactively called."
(interactive "p")
(save-excursion
(beginning-of-line)
(when iact
(message "%s" (looking-at empty-line-p-chars)))
(save-match-data (looking-at empty-line-p-chars))))
(defun ar-previous-line-empty-or-BOB-p ()
(save-excursion
(beginning-of-line)
(or
(bobp)
(when (forward-line -1)
(ar-empty-line-p)))))
(defun ar-previous-line-or-comment-empty-BOB-p ()
(save-excursion
(beginning-of-line)
(or
(bobp)
(when (forward-line -1)
(or (ar-in-comment-p)
(ar-empty-line-p))))))
(defun guess-what--after-typedef-maybe (regexp)
(save-excursion
(beginning-of-line)
(or
(bobp)
(progn
(forward-line -1)
(when
(looking-at regexp)
(insert (match-string 1)))))))
(defun ar-forward-comment (&optional pos)
"Go to end of (next) commented section following point.
Optional args position and `comment-start' character
Travel empty lines
Optional argument POS orig.
Optional argument CHAR comment start."
(interactive)
(let ((orig (or pos (point)))
;; (char (or char (string-to-char comment-start)))
(pps (parse-partial-sexp (point-min) (point)))
)
(cond ((nth 8 pps)
(goto-char (nth 8 pps))
(forward-comment 99999)
(unless (eq (point) orig)
(point)))
(t (search-forward comment-start nil t 1)
(ar-forward-comment)))
))
(defun ar-in-comment-p (&optional start)
"Return the beginning of current line's comment, if inside.
Optional argument START start."
(interactive)
(let* ((pps (parse-partial-sexp (or start (point-min)) (point)))
(erg (and (nth 4 pps) (nth 8 pps))))
(unless (or erg (nth 3 pps))
(when (or (eq (car (syntax-after (point))) 11)
(ignore-errors (looking-at comment-start)))
(setq erg (point))))
erg))
(defun ar-backward-comment (&optional pos)
"Got to beginning of a commented section.
Optional argument POS start."
(interactive)
(let ((erg pos)
last)
(when erg (goto-char erg))
(while (and (not (bobp)) (setq erg (ar-in-comment-p)))
(when (< erg (point))
(goto-char erg)
(setq last (point)))
(skip-chars-backward " \t\r\n\f"))
(when last (goto-char last))
last))
;; String
(defun ar-in-string-p ()
"Return start position, if inside or at opening delimiter.
Otherwise return nil."
(interactive)
(let* ((pps (parse-partial-sexp (point-min) (point)))
(erg (and (nth 3 pps) (nth 8 pps)))
(la (unless (or erg (eobp))
(and (eq (char-syntax (char-after)) 34)
;; look for closing char
(save-excursion
(forward-char 1)
(nth 3 (parse-partial-sexp (point-min) (point))))
(point)))))
(or erg la)))
(defun ar-in-string-p-fast ()
"Return delimiting character if inside, nil otherwise."
(nth 3 (parse-partial-sexp (point-min) (point))))
(defun ar-empty-string-p (strg)
(string= "" strg))
(defun ar-forward-string (&optional start)
"Go to the end of string, if inside.
Optional argument START start."
(let ((pos (or start (ar-in-string-p))))
(goto-char pos)
(forward-sexp)))
;; Navigate
(defun ar-skip-blanks-and-comments (&optional arg pps orig)
"Go forward over empty lines and comments alike.
Stop at first non-empty char.
With negative arg go backward. "
(interactive)
(let ((arg (or arg 1))
(pos (point))
(orig (or orig (point)))
(pps (or pps (parse-partial-sexp (point-min) (point)))))
(if (< 0 arg)
(progn
(skip-chars-forward " \t\r\n")
(when (or (and pps (nth 4 pps))(ar-in-comment-p))
(end-of-line)
(skip-chars-forward " \t\r\n\f"))
(when (empty-line-p)
(forward-line arg))
(when (> (point) pos)
(ar-skip-blanks-and-comments arg nil orig))
(< orig (point)))
(skip-chars-backward " \t\r\n")
(when (or (and pps (nth 4 pps))(ar-in-comment-p))
(goto-char (or (and pps (nth 4 pps))(ar-comment-beginning-position-atpt))))
(> orig (point)))))
(defun ar-backward-line ()
"Go to indentation of current source-code line.
If already at beginning of line, go one line above.
Skip comments, empty lines and strings"
(interactive)
(unless (bobp)
(let ((orig (point))
cmm)
(back-to-indentation)
(while (and (eolp) (not (bobp)))
(forward-line -1))
(unless (bobp)
(and (not (setq cmm (ar-in-comment-p)))(eq (point) orig)
(forward-line -1))
(cond (cmm
(ar-backward-comment)
(ar-backward-line))
((nth 3 (parse-partial-sexp (point-min) (point)))
;; beginning of string
(goto-char (nth 8 (parse-partial-sexp (point-min) (point)))))))
(back-to-indentation)
(when (or (eq (car (syntax-after (point))) 11)(eolp))(ar-backward-line)))))
(defvar ar-move-line-this-column nil)
(defun ar-move-line-keep-column-intern (arg)
(unless (eq last-command this-command)
(setq ar-move-line-this-column (current-column)))
(forward-line arg)
(while (and (not (eolp)) (< (current-column) ar-move-line-this-column))
(forward-char 1)))
(defun ar-forward-line-keep-column (&optional arg)
"Go to current column of next line.
If line is shorter, go to end of line"
(interactive "p")
(ar-move-line-keep-column-intern (or arg 1)))
(defun ar-backward-line-keep-column (&optional arg)
"Go to current column of line above.
If line is shorter, go to end of line"
(interactive "p")
(ar-move-line-keep-column-intern (- (or arg 1))))
(defun ar-forward-line ()
"Go to indentation of next source-code line.
Skip comments, empty lines and strings"
(interactive)
(unless (eobp)
(let ((orig (point))
strg)
(forward-line 1)
(back-to-indentation)
(while (and (eolp) (not (eobp)))
(forward-line 1))
(when (ar-in-comment-p)
(ar-forward-comment))
(when (setq strg (ar-in-string-p))
(ar-forward-string strg))
(back-to-indentation)
(unless (or (eobp) (< orig (point)))
(forward-line 1))
(back-to-indentation))
(when (ar-in-comment-p) (ar-forward-line))))
(defun ar-forward-line-eol ()
"Down one line at EOL."
(interactive)
(forward-line 1)
(end-of-line)
(skip-chars-backward " \t\r\n\f"))
(defun ar-move-line-indent (&optional arg)
"Move line forward or upward travelling indentation.
\\[universal-argument] toggles direction
Optional argument ARG toggles direction."
(interactive "P")
(when (eq 4 (prefix-numeric-value arg))
(setq ar-line-move-forward (not ar-line-move-forward)))
(if ar-line-move-forward
(ar-forward-line)
(ar-backward-line)))
(defun ar-match-paren (&optional arg)
"Go to the matching brace, bracket or parenthesis if on its counterpart.
Otherwise insert the character, the key is assigned to, here `%'.
With universal ARG \\[universal-argument] insert a `%'."
(interactive "P")
(if arg
(self-insert-command (if (numberp arg) arg 1))
(cond ((eq 4 (car (syntax-after (point))))
(forward-sexp)
(forward-char -1))
((eq 5 (car (syntax-after (point))))
(forward-char 1)
(backward-sexp))
(t (self-insert-command 1)))))
(defun ar--skip-to-comment ()
"Return position if comment."
(let ((comment-start (ar-string-strip comment-start))
erg)
(when (and (< 0 (abs (skip-chars-forward (concat "^" comment-start) (line-end-position))))
(looking-at comment-start)
(setq erg (point)))
erg)))
(defun ar-trim-string-left (strg &optional arg)
"Remove ARG characters from beginning and end of STRING.
Return the shortened string
Argument STRG start."
(setq arg (or arg 1))
(substring strg arg))
(defun ar-trim-string-right (strg &optional arg)
"Remove ARG characters from beginning and end of STRING.
Return the shortened string
Argument STRG end."
(setq arg (or arg 1))
(let ((laenge (length strg)))
(substring strg 0 (- laenge arg))))
(defun ar-trim-string (strg &optional left right)
"Remove ARG characters from beginning and end of STRING.
With no arguments remove just one character
Return the shortened string
Argument STRG strg.
Optional argument LEFT border.
Optional argument RIGHT border."
(let ((left (or left 1))
(right (or right 1))
(laenge (length strg)))
(setq right (- laenge right))
(substring strg left right)))
(defconst ar-beginning-of-defun-re
(concat
"[ \t]*\\("
(mapconcat 'identity
(list
"(defgroup"
"(defconst"
"(defcustom"
"(defface"
"(define-.+-mode"
"(defmacro"
"(defsubst"
"(deftheme"
"(defun"
"(defvar"
"(ert-deftest"
)
"\\|")
"\\)")
"Regular expression matching beginning of defun.")
(defun ar--backward-regexp (regexp &optional indent condition)
"Search backward next regexp not in string or comment.
Return and move to match-beginning if successful"
(while (and
(re-search-backward regexp nil 'move 1)
;; (setq last (point))
(or (nth 8 (parse-partial-sexp (point-min) (point)))
(and indent
(not (funcall condition (current-indentation) indent))))))
(unless (bobp)
(back-to-indentation)
(point)))
(defun ar-check-parens ()
"Like `check-parens' but avoid error.
Just return t if parentheses in the current buffer are balanced.
Return nil if not."
(interactive)
(scan-sexps (point-min) (point-max)))
(defun ar-beginning-of-defun-DWIM (&optional outmost pps)
"A fault-tolerant backward-function command.
In case of invalid source-code at point, try some heuristics"
(interactive)
(if (ar-check-parens)
(ar-beginning-of-defun outmost pps)
(ar--backward-regexp ar-beginning-of-defun-re)))
;; (defalias 'ar-beginning-of-defun 'ar-beginning-of-defun)
(defun ar-beginning-of-defun (&optional outmost pps)
"Move to the beginning of a function definition.
With OUTMOST don't stop at a nested inner function.
When `beginning-of-defun-function' is set, call with optional ARG
If no function found inside a list, go to list-start.
Otherwise reach next list upward in buffer
Optional argument PPS result of `parse-partial-sexp'."
(interactive "P")
(unless (bobp)
(let* ((outmost (or outmost (eq 4 (prefix-numeric-value outmost))))
(pps (or pps (parse-partial-sexp (point-min) (point))))
(liststart (or (and (bobp) (point))(nth 1 pps)))
)
(cond
((and (not liststart)(looking-at ar-beginning-of-defun-re))
(unless (bobp) (skip-chars-backward " \t\r\n\f")
(ar-beginning-of-defun)))
(liststart
(goto-char liststart)
(while (and (not (looking-at ar-beginning-of-defun-re))(setq liststart (nth 1 (parse-partial-sexp (point-min) (point)))))
(goto-char liststart))
(and outmost (nth 1 (setq pps (parse-partial-sexp (point-min) (point)))) (ar-beginning-of-defun outmost pps)))
((nth 4 pps) (ar-backward-comment)
(ar-beginning-of-defun outmost))
((or (eq ?\)(char-before)) (< 0 (abs (skip-chars-backward "^)"))))
(unless (bobp) (forward-char -1))
(ar-beginning-of-defun outmost)))
liststart)))
(defalias 'ar-end-of-defun 'ar-forward-defun)
(defun ar-forward-defun ()
"Move to the end of a function definition.
Return position if successful, nil otherwise
When `end-of-defun-function' is set, call it with optional ARG"
(interactive)
(unless (eobp)
(skip-chars-forward " \t\r\n\f")
(let* ((pps (parse-partial-sexp (point-min)(point)))
(nesting (nth 0 pps))
(in-comment (or (nth 4 pps)(looking-at comment-start)))
(orig (point)))
(cond
((nth 1 pps)
;; (goto-char (nth 1 pps))
(ar-beginning-of-defun)
(forward-sexp))
(in-comment
(end-of-line)
(ar-forward-comment)
(ar-forward-defun))
((looking-at ar-beginning-of-defun-re)
(forward-sexp))
((< 0 nesting)
(ar-beginning-of-defun)
(ar-end-of-defun))
((eq (char-after) ?\()
(forward-sexp)))
(when (< orig (point))
(point)))))
(defalias 'defun-beginning-position 'function-beginning-position)
(defun function-beginning-position ()
"Return the position where the current functions definition starts"
(interactive)
(save-excursion
(let* ((orig (point)))
(ar-beginning-of-defun)
(when (< (point) orig)
(when ar-verbose-p (message "%s" (point)))
(point)
))))
(defalias 'defun-end-position 'function-end-position)
(defun function-end-position ()
"Print the position where the current functions definition ends"
(interactive)
(save-excursion
(let ((orig (point)))
(ar-end-of-defun)
(when (< orig (point))
(when ar-verbose-p (message "%s" (point)))
(point)))))
(defun ar-count-lines (&optional beg end)
"Count lines in accessible part of buffer.
See http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7115
Optional argument BEG start.
Optional argument END end."
(interactive)
(let ((beg (or beg (point-min)))
(end (or end (point)))
erg)
(if (bolp)
(setq erg (1+ (count-lines beg end)))
(setq erg (count-lines beg end)))
(when (called-interactively-p 'any) (message "%s" erg))
erg))
(defun ar-list-indents ()
"Return a list of indentations up to start of toplevel."
(save-excursion
(let ((beg (save-excursion (ar-backward-toplevel)))
(ilist nil))
(save-restriction
(narrow-to-region beg (point))
(while (ar-backward-statement)
(unless (member (current-column) ilist)
(cons (current-column) ilist)))))))
(defun ar-reverse-char (&optional char)
"Reverse reciproke chars as \"[\" to \"]\".
Change doublequotes into singlequotes et vice versa
Likewise switch uppercase/downcase
Edit at point if challed without optional CHAR
otherwise return complement char"
(interactive "*")
(let* ((ret char)
(cf (or ret (char-after)))
(erg (cond
((and (< 96 cf)(< cf 123))
(upcase cf))
((and (< 64 cf)(< cf 91))
(downcase cf))
(t (ar--return-complement-char-maybe (char-after))))))
(when erg
(if ret
erg
(delete-char 1)
(insert (char-to-string erg))))))
(defun ar--return-complement-char-maybe (char)
"Reverse reciproke CHARs as \"[\" to \"]\"."
(pcase char
;; ‘M-x append-to-register <RET> R’ can use ‘C-x r +’
(?+ ?-)
(?- ?+)
(?» ?«)
;; (92 47)
;; (47 92)
;; (?' ?\")
;; (?\" ?')
(?‘ ?’)
;; backticked is needed
(?` ?`)
;; (?´ ?`)
(?< ?>)
(?> ?<)
(?\( ?\))
(?\) ?\()
(?\] ?\[)
(?\[ ?\])
(?} ?{)
(?{ ?})
(?\〈 ?\〉)
(?\⦑ ?\⦒)
(?\⦓ ?\⦔)
(?\【 ?\】)
(?\⦗ ?\⦘)
(?\⸤ ?\⸥)
(?\「 ?\」)
(?\《 ?\》)
(?\⦕ ?\⦖)
(?\⸨ ?\⸩)
(?\⧚ ?\⧛)
(?\{ ?\})
(?\( ?\))
(?\[ ?\])
(?\⦅ ?\⦆)
(?\「 ?\」)
(?\❰ ?\❱)
(?\❮ ?\❯)
(?\“ ?\”)
(?\❲ ?\❳)
(?\⟨ ?\⟩)
(?\⟪ ?\⟫)
(?\⟮ ?\⟯)
(?\⟦ ?\⟧)
(?\⟬ ?\⟭)
(?\❴ ?\❵)
(?\❪ ?\❫)
(?\❨ ?\❩)
(?\❬ ?\❭)
(?\᚛ ?\᚜)
(?\〈 ?\〉)
(?\⧼ ?\⧽)
(?\⟅ ?\⟆)
(?\⸦ ?\⸧)
(?\﹛ ?\﹜)
(?\﹙ ?\﹚)
(?\﹝ ?\﹞)
(?\⁅ ?\⁆)
(?\⦏ ?\⦎)
(?\⦍ ?\⦐)
(?\⦋ ?\⦌)
(?\₍ ?\₎)
(?\⁽ ?\⁾)
(?\༼ ?\༽)
(?\༺ ?\༻)
(?\⸢ ?\⸣)
(?\〔 ?\〕)
(?\『 ?\』)
(?\⦃ ?\⦄)
(?\〖 ?\〗)
(?\⦅ ?\⦆)
(?\〚 ?\〛)
(?\〘 ?\〙)
(?\⧘ ?\⧙)
(?\⦉ ?\⦊)
(?\⦇ ?\⦈)
(?\〉 ?\〈)
(?\⦒ ?\⦑)
(?\⦔ ?\⦓)
(?\】 ?\【)
(?\⦘ ?\⦗)
(?\⸥ ?\⸤)
(?\」 ?\「)
(?\》 ?\《)
(?\⦖ ?\⦕)
(?\⸩ ?\⸨)
(?\⧛ ?\⧚)
(?\} ?\{)
(?\) ?\()
(?\] ?\[)
(?\⦆ ?\⦅)
(?\」 ?\「)
(?\❱ ?\❰)
(?\❯ ?\❮)
(?\” ?\“)
(?\’ ?\‘)
(?\❳ ?\❲)
(?\⟩ ?\⟨)
(?\⟫ ?\⟪)
(?\⟯ ?\⟮)
(?\⟧ ?\⟦)
(?\⟭ ?\⟬)
(?\❵ ?\❴)
(?\❫ ?\❪)
(?\❩ ?\❨)
(?\❭ ?\❬)
(?\᚜ ?\᚛)
(?\〉 ?\〈)
(?\⧽ ?\⧼)
(?\⟆ ?\⟅)
(?\⸧ ?\⸦)
(?\﹜ ?\﹛)
(?\﹚ ?\﹙)
(?\﹞ ?\﹝)
(?\⁆ ?\⁅)
(?\⦎ ?\⦏)
(?\⦐ ?\⦍)
(?\⦌ ?\⦋)
(?\₎ ?\₍)
(?\⁾ ?\⁽)
(?\༽ ?\༼)
(?\༻ ?\༺)
(?\⸣ ?\⸢)
(?\〕 ?\〔)
(?\』 ?\『)
(?\⦄ ?\⦃)
(?\〗 ?\〖)
(?\⦆ ?\⦅)
(?\〛 ?\〚)
(?\〙 ?\〘)
(?\⧙ ?\⧘)
(?\⦊ ?\⦉)
(?\⦈ ?\⦇)
(_ char)))
(defvar ar-closing-chars (list ?’ ?´ ?\] ?} ?\〉 ?\⦒ ?\⦔ ?\】 ?\⦘ ?\⸥ ?\」 ?\》 ?\⦖ ?\⸩ ?\⧛ ?\} ?\) ?\] ?\⦆ ?\」 ?\❱ ?\❯ ?\” ?\❳ ?\⟩ ?\⟫ ?\⟯ ?\⟧ ?\⟭ ?\❵ ?\❫ ?\❩ ?\❭ ?\᚜ ?\〉 ?\⧽ ?\⟆ ?\⸧ ?\﹜ ?\﹚ ?\﹞ ?\⁆ ?\⦎ ?\⦐ ?\⦌ ?\₎ ?\⁾ ?\༽ ?\༻ ?\⸣ ?\〕 ?\』 ?\⦄ ?\〗 ?\⦆ ?\〛 ?\〙 ?\⧙ ?\⦊ ?\⦈ )
"List of closing delimiters.")
(defvar ar-curly-braces-syntax-control-modes (list 'scala-mode 'java-mode)
"Internal use only.")
(defun ar-align-with-previous-line-maybe ()
(when (and (eq this-command 'self-insert-command)
(eq (char-before) ?=))
(ar-align-with-previous-line (current-column))))
(defun ar--do-align-intern (col)
(while (< (current-column) col)
(insert 32)))
(defun ar--align-with-previous-line (uppercol downcol orig upperpos)
(if (< uppercol downcol)
(let ((col downcol))
(goto-char upperpos)
(ar--do-align-intern col)
;; upper-line extended, maybe line above needs that too?
(ar-align-with-previous-line))
(let ((col uppercol))
(goto-char orig)
(forward-char -1)
(ar--do-align-intern col))))
(defun ar-align-with-previous-line (&optional regexp)
"Align with previous line.
Defaults aligning to equal and vertical bar sign
Optional argument REGEXP regexp."
(interactive "*")
(save-excursion
(let* ((orig (copy-marker (point)))
(regexp (or regexp (concat ar-eq-assignment-re "\\|" ar-vertical-line-re)))
(downcol (progn (beginning-of-line)
(when (looking-at regexp)
(ignore-errors (goto-char (match-beginning 3)))
(current-column))))
upperpos
(uppercol (progn
(beginning-of-line)
(unless (bobp)
(forward-line -1)
(unless (ar-empty-line-p)
(beginning-of-line)
(when (looking-at regexp)
(save-excursion
(goto-char (match-beginning 3))
(setq upperpos (point))
(current-column))))))))
(when (and downcol uppercol)
(ar--align-with-previous-line uppercol downcol orig upperpos)))))
(defun ar-align (beg end &optional regexp)
(interactive "r*")
(save-excursion
(goto-char beg)
(ar-align-with-previous-line regexp)
(while (not
(or
(eobp)
(progn
(forward-line 1)
(ar-align-with-previous-line)
(<= end (line-end-position))))))))
(defun ar--fetch-previous-indent (orig)
"Report the preceding indent.
Argument ORIG start."
(save-excursion
(goto-char orig)
(forward-line -1)
(end-of-line)
(skip-chars-backward " \t\r\n\f")
(current-indentation)))
(defun ar-backward-statement (&optional orig done limit ignore-in-string-p repeat maxindent)
"Go to the initial line of a simple statement.
For beginning of compound statement use `ar-backward-block'.
For beginning of clause `ar-backward-clause'.
`ignore-in-string-p' allows moves inside a docstring, used when
computing indents
ORIG - consider orignial position or point.
DONE - transaktional argument
LIMIT - honor limit
IGNORE-IN-STRING-P - also much inside a string
REPEAT - count and consider repeats
Optional MAXINDENT: don't stop if indentation is larger"
(interactive)
(save-restriction
(unless (bobp)
(let* ((repeat (or (and repeat (1+ repeat)) 0))
(orig (or orig (point)))
(pps (parse-partial-sexp (or limit (point-min))(point)))
(done done)
erg)
;; lp:1382788
(unless done
(and (< 0 (abs (skip-chars-backward " \t\r\n\f")))
(setq pps (parse-partial-sexp (or limit (point-min))(point)))))
(cond
((< ar-max-specpdl-size repeat)
(error "Py-forward-statement reached loops max. If no error, customize `ar-max-specpdl-size'"))
((and (bolp) (eolp))
(skip-chars-backward " \t\r\n\f")
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; inside string
((and (nth 3 pps) (not ignore-in-string-p))
(setq done t)
(goto-char (nth 8 pps))
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
((nth 4 pps)
(while (ignore-errors (goto-char (nth 8 pps)))
(skip-chars-backward " \t\r\n\f")
(setq pps (parse-partial-sexp (line-beginning-position) (point))))
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
((and (nth 1 pps) (not (member major-mode ar-curly-braces-syntax-control-modes)))
(goto-char (1- (nth 1 pps)))
(when (ar--skip-to-semicolon-backward (save-excursion (back-to-indentation) (point)))
(setq done t))
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
((ar-preceding-line-backslashed-p)
(forward-line -1)
(back-to-indentation)
(setq done t)
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; at raw-string
;; (and (looking-at "\"\"\"\\|'''") (member (char-before) (list ?u ?U ?r ?R)))
((and (looking-at "\"\"\"\\|'''") (member (char-before) (list ?u ?U ?r ?R)))
(forward-char -1)
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; BOL or at space before comment
((and (looking-at "[ \t]*#") (looking-back "^[ \t]*" (line-beginning-position)))
(forward-comment -1)
(while (and (not (bobp)) (looking-at "[ \t]*#") (looking-back "^[ \t]*" (line-beginning-position)))
(forward-comment -1))
(unless (bobp)
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent)))
;; at inline comment
((looking-at "[ \t]*#")
(when (ar--skip-to-semicolon-backward (save-excursion (back-to-indentation) (point)))
(setq done t))
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; at beginning of string
((looking-at ar-string-delim-re)
(when (< 0 (abs (skip-chars-backward " \t\r\n\f")))
(setq done t))
(back-to-indentation)
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; after end of statement
((and (not done) (eq (char-before) ?\;))
(skip-chars-backward ";")
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; travel until indentation or semicolon
((and (not done) (ar--skip-to-semicolon-backward))
(unless (and maxindent (< maxindent (current-indentation)))
(setq done t))
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
;; at current indent
((and (not done) (not (eq 0 (skip-chars-backward " \t\r\n\f"))))
(ar-backward-statement orig done limit ignore-in-string-p repeat maxindent))
((and maxindent (< maxindent (current-indentation)))