-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar-navigate.el
1801 lines (1549 loc) · 62.7 KB
/
ar-navigate.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-navigate.el --- Provide generic navigation -*- lexical-binding: t; -*-
;; URL: https://github.com/andreas-roehler/emacs-generics
;; Copyright (C) 2015-2024 Andreas Röhler
;; Author: Andreas Röhler <[email protected]>
;; Keywords: lisp, 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:
;;
;;; Code:
(require 'ar-subr)
(require 'ar-beg-end)
(defvar ar-block-closing-keywords-re ""
"Closing keywords.")
(defvar ar-string-delim-re "\\(\"\"\"\\|'''\\|\"\\|'\\)"
"When looking at beginning of string.")
(defvar ar-smart-indentation nil)
(defvar ar-block-re "")
(make-variable-buffer-local 'ar-block-re)
(defvar ar-clause-re "")
(make-variable-buffer-local 'ar-clause-re)
(defvar ar-extended-block-or-clause-re "")
(make-variable-buffer-local 'ar-extended-block-or-clause-re)
(defvar ar-labelled-re "[ \\t]*:[[:graph:]]+"
"When looking at label.")
;; (setq ar-labelled-re "[ \\t]*:[[:graph:]]+")
(defvar ar-expression-skip-regexp "[^ (=:#\t\r\n\f]"
"Possible not composing an expression.")
(make-variable-buffer-local 'ar-expression-skip-regexp)
(defvar ar-expression-skip-chars "^ (:=#\t\r\n\f"
"Chars indicated not composing an expression.")
(make-variable-buffer-local 'ar-expression-skip-chars)
(defvar ar-expression-re "[^ =#\t\r\n\f]+"
"Chars indicated possible composing an expression, when ‘looking-at’ or -back.")
(make-variable-buffer-local 'ar-expression-re)
(defvar ar-not-expression-regexp "[ .=#\t\r\n\f)]+"
"Expression assumes chars indicated probably will not compose an expression.")
(make-variable-buffer-local 'ar-not-expression-regexp)
(defvar ar-not-expression-chars " #\t\r\n\f"
"Ar-expression assumes chars indicated probably will not compose an expression.")
(make-variable-buffer-local 'ar-not-expression-chars)
(defvar ar-partial-expression-backward-chars "^ =,\"'()[]{}:#\t\r\n\f"
"Chars indicated possible composing a partial-expression.")
(make-variable-buffer-local 'ar-partial-expression-backward-chars)
(defvar ar-partial-expression-forward-chars "^ \"')}]:#\t\r\n\f")
(make-variable-buffer-local 'ar-partial-expression-forward-chars)
(defvar ar-operator-regexp "[ \t]*\\(\\.\\|+\\|-\\|*\\|//\\|//\\|&\\|%\\||\\|\\^\\|>>\\|<<\\|<\\|<=\\|>\\|>=\\|==\\|!=\\)[ \t]*"
"Matches most of operators inclusive whitespaces around.
See also `ar-assignment-regexp'")
(defvar ar-assignment-regexp "[ \t]*=[^=]"
"Matches assignment operator inclusive whitespaces around.
See also `ar-operator-regexp'")
(make-variable-buffer-local 'ar-operator-regexp)
(defvar ar-delimiter-regexp "\\(\\.[[:alnum:]]\\|,\\|;\\|:\\)[ \t\n]"
"Delimiting elements of lists or other programming constructs.")
(make-variable-buffer-local 'ar-delimiter-regexp)
(defvar ar-line-number-offset 0
"Store the line number where the region started.")
(make-variable-buffer-local 'ar-line-number-offset)
(defvar ar-match-paren-no-use-syntax-pps nil)
(make-variable-buffer-local 'ar-match-paren-no-use-syntax-pps)
(defvar ar-traceback-line-re
"[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)"
"Regular expression that describes tracebacks.")
(defvar ar-block-or-clause-re ""
"Describe beginning of block-or-clause")
(defvar ar-class-re ""
"Describe beginning of class")
(defvar ar-def-re ""
"Describe beginning of def")
(defvar ar-def-or-class-re ""
"Describe beginning of def-or-class")
(defvar ar-minor-block-re ""
"Describe beginning of minor-block")
(defvar ar-clause-re ""
"Describe beginning of clause")
(defvar ar-else-re ""
"Describe beginning of else")
(defvar ar-try-re ""
"Describe beginning of try")
(defvar ar-elif-re ""
"Describe beginning of elif")
(defvar ar-except-re ""
"Describe beginning of except")
(defvar ar-if-re ""
"Describe beginning of if")
(defvar ar-finally-re ""
"Describe beginning of finally")
(defvar ar-decorator-re "@\\w+"
"Describe beginning of decorator")
(defvar ar-section-re ""
"Describe beginning of section")
(defvar ar-XXX-tag-face 'ar-XXX-tag-face)
(defvar ar-pseudo-keyword-face 'ar-pseudo-keyword-face)
(make-variable-buffer-local 'ar-XXX-tag-face)
(defvar ar-variable-name-face 'ar-variable-name-face)
(defvar ar-number-face 'ar-number-face)
(make-variable-buffer-local 'ar-variable-name-face)
(defvar ar-decorators-face 'ar-decorators-face)
(defvar ar-object-reference-face 'ar-object-reference-face)
(make-variable-buffer-local 'ar-decorators-face)
(defvar ar-builtins-face 'ar-builtins-face)
(defvar ar-class-name-face 'ar-class-name-face)
(make-variable-buffer-local 'ar-builtins-face)
(defvar ar-exception-name-face 'ar-exception-name-face)
(defvar ar-import-from-face 'ar-import-from-face)
(make-variable-buffer-local 'ar-exception-name-face)
(defvar ar-def-class-face 'ar-def-class-face)
(defvar ar-try-if-face 'ar-try-if-face)
(make-variable-buffer-local 'ar-def-class-face)
(defvar ar-vertical-move-start-column nil)
(defvar ar-honor-decorators nil)
(make-variable-buffer-local 'ar-honor-decorators)
(defcustom ar-indent-offset 4
"Amount of offset per level of indentation."
:type 'integer
:tag "ar-indent-offset")
(make-variable-buffer-local 'ar-indent-offset)
(defun ar-previous-line (arg)
"Moving ARG upwards.
Keep the column if possible."
(interactive "p")
(unless (bobp)
(let ((col
(if
(eq last-command 'ar-previous-line)
ar-vertical-move-start-column
(setq ar-vertical-move-start-column (current-column)))))
(forward-line (- arg))
(if (eq major-mode 'dired-mode)
(dired-move-to-filename)
(when (< col (save-excursion (end-of-line) (current-column)))
(move-to-column col))))))
(defun ar-next-line (arg)
"Moving ARG upwards.
Keep the column if possible."
(interactive "p")
(unless (eobp)
(let ((col
(if
(eq last-command 'ar-next-line)
ar-vertical-move-start-column
(setq ar-vertical-move-start-column (current-column)))))
(forward-line arg)
(if (eq major-mode 'dired-mode)
(dired-move-to-filename)
(when (< col (save-excursion (end-of-line) (current-column)))
(move-to-column col))))))
(defun ar-toggle-indent-tabs-mode ()
"Toggle `indent-tabs-mode'.
Returns value of `indent-tabs-mode' switched to."
(interactive)
(when
(setq indent-tabs-mode (not indent-tabs-mode))
(setq tab-width ar-indent-offset))
(when (and ar-verbose-p (called-interactively-p 'any)) (message "indent-tabs-mode %s ar-indent-offset %s" indent-tabs-mode ar-indent-offset))
indent-tabs-mode)
(defun ar-indent-tabs-mode (arg &optional iact)
"With positive ARG switch `indent-tabs-mode' on.
With negative ARG switch `indent-tabs-mode' off.
Returns value of `indent-tabs-mode' switched to.
Optional argument IACT Interactively called."
(interactive "p")
(if (< 0 arg)
(progn
(setq indent-tabs-mode t)
(setq tab-width ar-indent-offset))
(setq indent-tabs-mode nil))
(when (and ar-verbose-p (or iact (called-interactively-p 'any))) (message "indent-tabs-mode %s ar-indent-offset %s" indent-tabs-mode ar-indent-offset))
indent-tabs-mode)
(defun ar-indent-tabs-mode-on (arg)
"Switch `indent-tabs-mode' on.
Argument ARG sent by p."
(interactive "p")
(ar-indent-tabs-mode (abs arg)(called-interactively-p 'any)))
(defun ar-indent-tabs-mode-off (arg)
"Switch `indent-tabs-mode' off.
Argument ARG sent by p."
(interactive "p")
(ar-indent-tabs-mode (- (abs arg))(called-interactively-p 'any)))
;; Guess indent offset
(defun ar-guessed-sanity-check (guessed)
(and (>= guessed 2)(<= guessed 8)(eq 0 (% guessed 2))))
(defun ar--guess-indent-final (indents)
"Calculate and do sanity-check.
Argument INDENTS Previous indents.
Argument ORIG Position."
(let* ((first (car indents))
(second (cadr indents))
(erg (if (and first second)
(if (< second first)
;; (< (point) orig)
(- first second)
(- second first))
(default-value 'ar-indent-offset))))
(setq erg (and (ar-guessed-sanity-check erg) erg))
erg))
(defun ar-beginning-of-statement-p ()
"Return position, if cursor is at the beginning of a `statement', nil otherwise."
(when (eq (current-column) (current-indentation))
(let ((pps (parse-partial-sexp (point-min) (point))))
(unless (or (nth 3 pps) (nth 4 pps)(empty-line-p))
(point)))))
;; (defun ar-beginning-of-statement-p (&optional pps)
;; "Return position, if cursor is at the beginning of a ‘statement’, nil otherwise."
;; (interactive)
;; (save-excursion
;; (let ((pps (or pps (parse-partial-sexp (point-min) (point)))))
;; (and (not (or (nth 8 pps) (nth 1 pps)))
;; (looking-at ar-statement-re)
;; (looking-back "[^ \t]*" (line-beginning-position))
;; (eq (current-column) (current-indentation))
;; (eq (point) (progn (ar-forward-statement) (ar-backward-statement)))
;; (point)))))
(defun ar--guess-indent-forward ()
"Called when moving to end of a form and `ar-smart-indentation' is on."
(let* ((first (if
(ar-beginning-of-statement-p)
(current-indentation)
(progn
(ar-forward-statement)
(ar-backward-statement)
(current-indentation))))
(second (if (or (looking-at ar-extended-block-or-clause-re)(eq 0 first))
(progn
(ar-forward-statement)
(ar-forward-statement)
(ar-backward-statement)
(current-indentation))
;; when not starting from block, look above
(while (and (re-search-backward ar-extended-block-or-clause-re nil 'movet 1)
(or (>= (current-indentation) first)
(nth 8 (parse-partial-sexp (point-min) (point))))))
(current-indentation))))
(list first second)))
(defun ar--guess-indent-backward ()
"Called when moving to beginning of a form and `ar-smart-indentation' is on."
(let* ((cui (current-indentation))
(indent (if (< 0 cui) cui 999))
(pos (progn (while (and (re-search-backward ar-extended-block-or-clause-re nil 'move 1)
(or (>= (current-indentation) indent)
(nth 8 (parse-partial-sexp (point-min) (point))))))
(unless (bobp) (point))))
(first (and pos (current-indentation)))
(second (and pos (ar-forward-statement) (ar-forward-statement) (ar-backward-statement)(current-indentation))))
(list first second)))
(defun ar-guess-indent-offset (&optional direction)
"Guess `ar-indent-offset'.
Set local value of `ar-indent-offset', return it
Might change local value of `ar-indent-offset' only when called
downwards from beginning of block followed by a statement.
Otherwise ‘default-value’ is returned.
Optional argument DIRECTION where to move."
(interactive)
(save-excursion
(let* (;;(orig (point))
(indents
(cond (direction
(if (eq 'forward direction)
(ar--guess-indent-forward)
(ar--guess-indent-backward)))
;; guess some usable indent is above current position
((eq 0 (current-indentation))
(ar--guess-indent-forward))
(t (ar--guess-indent-backward))))
(erg (ar--guess-indent-final indents)))
(if erg (setq ar-indent-offset erg)
(setq ar-indent-offset
(default-value 'ar-indent-offset)))
(when (called-interactively-p 'any) (message "%s" ar-indent-offset))
ar-indent-offset)))
(defun ar-up-statement ()
"Go to the beginning of next statement upwards in buffer.
Return position if statement found, nil otherwise."
(interactive)
(let (;;(orig (point))
erg)
(if (ar-beginning-of-statement-p)
(setq erg (ar-backward-statement))
(setq erg (and (ar-backward-statement) (ar-backward-statement))))
(when (and ar-verbose-p (called-interactively-p 'any)) (message "%s" erg))
erg))
(make-variable-buffer-local 'ar-beginning-of-defun-re)
(defun ar-down-statement ()
"Go to the beginning of next statement downwards in buffer.
Return position if statement found, nil otherwise."
(interactive)
(let* ((orig (point))
(erg
(cond ((ar--end-of-statement-p)
(and (ar-forward-statement) (ar-backward-statement)))
((< orig (progn (ar-forward-statement) (ar-backward-statement)))
(point))
(t (and (ar-forward-statement) (ar-forward-statement)(ar-backward-statement))))))
(when (and ar-verbose-p (called-interactively-p 'any)) (message "%s" erg))
erg))
(defun ar-down-base (regexp)
"Go to the beginning of next form below in buffer.
Return position if form found, nil otherwise.
Argument REGEXP determined by form."
(unless (eobp)
(forward-line 1)
(beginning-of-line)
(let* (;;(orig (point))
erg)
(if (eobp)
(setq erg nil)
(while (and (re-search-forward regexp nil t 1)
(nth 8 (parse-partial-sexp (point-min) (point)))))
(back-to-indentation)
(when (looking-at regexp) (setq erg (point)))
;; (when ar-verbose-p (message "%s" erg))
erg))))
(defun ar-down-base-bol (regexp)
"Go to the beginning of next form below in buffer.
Return position if form found, nil otherwise.
Argument REGEXP determined by form"
(unless (eobp)
(forward-line 1)
(beginning-of-line)
(let* (;;(orig (point))
erg)
(if (eobp)
(setq erg nil)
(while (and (re-search-forward regexp nil t 1)
(nth 8 (parse-partial-sexp (point-min) (point)))))
(beginning-of-line)
(when (looking-at regexp) (setq erg (point)))
;; (when ar-verbose-p (message "%s" erg))
erg))))
(defun ar-up-block ()
"Go to the beginning of next block upwards in buffer.
Return position if block found, nil otherwise."
(interactive)
(ar-up-base ar-block-re))
(defun ar-up-block-or-clause ()
"Go to the beginning of next block-or-clause upwards in buffer.
Return position if block-or-clause found, nil otherwise."
(interactive)
(ar-up-base ar-block-or-clause-re))
(defun ar-up-class ()
"Go to the beginning of next class upwards in buffer.
Return position if class found, nil otherwise."
(interactive)
(ar-up-base ar-class-re))
(defun ar-up-clause ()
"Go to the beginning of next clause upwards in buffer.
Return position if clause found, nil otherwise."
(interactive)
(ar-up-base ar-clause-re))
(defun ar-up-def ()
"Go to the beginning of next def upwards in buffer.
Return position if def found, nil otherwise."
(interactive)
(ar-up-base ar-def-re))
(defun ar-up-def-or-class ()
"Go to the beginning of next def-or-class upwards in buffer.
Return position if def-or-class found, nil otherwise."
(interactive)
(ar-up-base ar-def-or-class-re))
(defun ar-up-minor-block ()
"Go to the beginning of next minor-block upwards in buffer.
Return position if minor-block found, nil otherwise."
(interactive)
(ar-up-base ar-minor-block-re))
(defun ar-up-section ()
"Go to the beginning of next section upwards in buffer.
Return position if section found, nil otherwise."
(interactive)
(ar-up-base ar-section-re))
(defun ar-down-block ()
"Go to the beginning of next block below in buffer.
Return position if block found, nil otherwise."
(interactive)
(ar-down-base ar-block-re))
(defun ar-down-block-or-clause ()
"Go to the beginning of next block-or-clause below in buffer.
Return position if block-or-clause found, nil otherwise."
(interactive)
(ar-down-base ar-block-or-clause-re))
(defun ar-down-class ()
"Go to the beginning of next class below in buffer.
Return position if class found, nil otherwise."
(interactive)
(ar-down-base ar-class-re))
(defun ar-down-clause ()
"Go to the beginning of next clause below in buffer.
Return position if clause found, nil otherwise."
(interactive)
(ar-down-base ar-clause-re))
(defun ar-down-def ()
"Go to the beginning of next def below in buffer.
Return position if def found, nil otherwise."
(interactive)
(ar-down-base ar-def-re))
(defun ar-down-def-or-class ()
"Go to the beginning of next def-or-class below in buffer.
Return position if def-or-class found, nil otherwise."
(interactive)
(ar-down-base ar-def-or-class-re))
(defun ar-down-minor-block ()
"Go to the beginning of next minor-block below in buffer.
Return position if minor-block found, nil otherwise."
(interactive)
(ar-down-base ar-minor-block-re))
(defun ar-down-section ()
"Go to the beginning of next section below in buffer.
Return position if section found, nil otherwise."
(interactive)
(ar-down-base ar-section-re))
(defun ar-up-block-bol ()
"Go to the beginning of next block upwards in buffer.
Go to beginning of line.
Return position if block found, nil otherwise."
(interactive)
(ar-up-base-bol ar-block-re))
(defun ar-up-block-or-clause-bol ()
"Go to the beginning of next block-or-clause upwards in buffer.
Go to beginning of line.
Return position if block-or-clause found, nil otherwise."
(interactive)
(ar-up-base-bol ar-block-or-clause-re))
(defun ar-up-class-bol ()
"Go to the beginning of next class upwards in buffer.
Go to beginning of line.
Return position if class found, nil otherwise."
(interactive)
(ar-up-base-bol ar-class-re))
(defun ar-up-clause-bol ()
"Go to the beginning of next clause upwards in buffer.
Go to beginning of line.
Return position if clause found, nil otherwise."
(interactive)
(ar-up-base-bol ar-clause-re))
(defun ar-up-def-bol ()
"Go to the beginning of next def upwards in buffer.
Go to beginning of line.
Return position if def found, nil otherwise."
(interactive)
(ar-up-base-bol ar-def-re))
(defun ar-up-def-or-class-bol ()
"Go to the beginning of next def-or-class upwards in buffer.
Go to beginning of line.
Return position if def-or-class found, nil otherwise."
(interactive)
(ar-up-base-bol ar-def-or-class-re))
(defun ar-up-minor-block-bol ()
"Go to the beginning of next minor-block upwards in buffer.
Go to beginning of line.
Return position if minor-block found, nil otherwise."
(interactive)
(ar-up-base-bol ar-minor-block-re))
(defun ar-up-section-bol ()
"Go to the beginning of next section upwards in buffer.
Go to beginning of line.
Return position if section found, nil otherwise."
(interactive)
(ar-up-base-bol ar-section-re))
(defun ar-down-block-bol ()
"Go to the beginning of next block below in buffer.
Go to beginning of line
Return position if block found, nil otherwise"
(interactive)
(ar-down-base-bol ar-block-re))
(defun ar-down-block-or-clause-bol ()
"Go to the beginning of next block-or-clause below in buffer.
Go to beginning of line
Return position if block-or-clause found, nil otherwise"
(interactive)
(ar-down-base-bol ar-block-or-clause-re))
(defun ar-down-class-bol ()
"Go to the beginning of next class below in buffer.
Go to beginning of line
Return position if class found, nil otherwise"
(interactive)
(ar-down-base-bol ar-class-re))
(defun ar-down-clause-bol ()
"Go to the beginning of next clause below in buffer.
Go to beginning of line
Return position if clause found, nil otherwise"
(interactive)
(ar-down-base-bol ar-clause-re))
(defun ar-down-def-bol ()
"Go to the beginning of next def below in buffer.
Go to beginning of line
Return position if def found, nil otherwise"
(interactive)
(ar-down-base-bol ar-def-re))
(defun ar-down-def-or-class-bol ()
"Go to the beginning of next def-or-class below in buffer.
Go to beginning of line
Return position if def-or-class found, nil otherwise"
(interactive)
(ar-down-base-bol ar-def-or-class-re))
(defun ar-down-minor-block-bol ()
"Go to the beginning of next minor-block below in buffer.
Go to beginning of line
Return position if minor-block found, nil otherwise"
(interactive)
(ar-down-base-bol ar-minor-block-re))
(defun ar-down-section-bol ()
"Go to the beginning of next section below in buffer.
Go to beginning of line
Return position if section found, nil otherwise"
(interactive)
(ar-down-base-bol ar-section-re))
(defun ar-forward-comment-or-defun ()
"Not implemented yet.
If inside a comment, go to end of commented section.
If inside a function def, go to its end"
)
(defun ar-end-of-string (&optional beginning-of-string-position)
"Go to end of string at point if any, if successful return position.
Optional argument BEGINNING-OF-STRING-POSITION Position."
(interactive)
;; (when ar-debug-p (message "(current-buffer): %s" (current-buffer)))
;; (when ar-debug-p (message "major-mode): %s" major-mode))
(let ((orig (point))
(beginning-of-string-position (or beginning-of-string-position (and (nth 3 (parse-partial-sexp 1 (point)))(nth 8 (parse-partial-sexp 1 (point))))
(and (looking-at "\"\"\"\\|'''\\|\"\\|\'")(match-beginning 0))))
erg)
(if beginning-of-string-position
(progn
(goto-char beginning-of-string-position)
(when
;; work around parse-partial-sexp error
(and (nth 3 (parse-partial-sexp 1 (point)))(nth 8 (parse-partial-sexp 1 (point))))
(goto-char (nth 3 (parse-partial-sexp 1 (point)))))
(if (ignore-errors (setq erg (scan-sexps (point) 1)))
(goto-char erg)
(goto-char orig)))
(error (concat "ar-end-of-string: don't see end-of-string at " (buffer-name (current-buffer)) "at pos " (point))))
(when (and ar-verbose-p (called-interactively-p 'any)) (message "%s" erg))
erg))
;; Expression
(defun ar-backward-expression ()
"Go to the beginning of a compound expression.
A a compound expression might be concatenated by \".\" operator,
thus composed by minor expressions.
If already at the beginning or before a expression,
go to next expression in buffer upwards"
(interactive)
(let (erg)
(setq erg (ar--beginning-of-expression-intern))
(when (and ar-verbose-p (called-interactively-p 'any)) (message "%s" erg))
erg))
(defun ar--beginning-of-expression-intern (&optional orig)
(unless (bobp)
(let ((orig (or orig (point)))
(pps (syntax-ppss))
erg)
(cond
( ;; (empty-line-p)
(eq 9 (char-after))
(while
(and ;; (empty-line-p)
(eq 9 (char-after))(not (bobp)))
(forward-line -1)
(end-of-line))
(ar--beginning-of-expression-intern orig))
;; lists
((nth 1 pps)
(goto-char (nth 1 pps))
(skip-chars-backward ar-expression-skip-chars))
((and (nth 3 pps)(nth 8 pps)
(goto-char (nth 8 pps)))
(cond (;; consider expression a string starting at BOL
(bolp))
((looking-back ar-assignment-regexp (line-beginning-position)))
((looking-back ar-operator-regexp (line-beginning-position))
(when (nth 2 pps)
(goto-char (nth 2 pps))))
(t (ar--beginning-of-expression-intern orig))))
;; comments left
((nth 8 pps)
(goto-char (nth 8 pps))
(unless (bobp)
(ar--beginning-of-expression-intern orig)))
;; concatenated strings
((looking-back (concat ar-string-delim-re ar-expression-re ar-string-delim-re ar-operator-regexp ar-string-delim-re ar-expression-re ar-string-delim-re) (line-beginning-position))
(goto-char (match-beginning 0))
(while (looking-back (concat ar-string-delim-re ar-expression-re ar-string-delim-re ar-operator-regexp) (line-beginning-position) t)
(goto-char (match-beginning 0)))
(skip-chars-backward ar-expression-skip-chars))
;; before comment
((and (looking-at "[ \t]*#") (looking-back "^[ \t]*" (line-beginning-position)))
(forward-line -1)
(end-of-line)
(skip-chars-backward " \t\r\n\f")
(unless (bobp)
(forward-char -1)
(ar--beginning-of-expression-intern orig)))
((and (< (point) orig)(looking-at (concat ar-expression-re ar-delimiter-regexp))))
((looking-back (concat "[^ \t\n\r\f]+" ar-delimiter-regexp) (line-beginning-position))
(goto-char (match-beginning 0))
(skip-chars-backward ar-expression-skip-chars)
(unless (or (looking-back ar-assignment-regexp (line-beginning-position)) (looking-back "^[ \t]*" (line-beginning-position)))
(ar--beginning-of-expression-intern orig)))
;; before assignment
((looking-back ar-assignment-regexp (line-beginning-position))
(goto-char (1- (match-beginning 0)))
(forward-char -1)
(ar--beginning-of-expression-intern orig))
((looking-back ar-operator-regexp (line-beginning-position))
(goto-char (1- (match-beginning 0)))
(forward-char -1)
(unless (< 0 (abs (skip-chars-backward ar-expression-skip-chars)))
(ar--beginning-of-expression-intern orig)))
((looking-back "\"\\|'" (line-beginning-position))
(forward-char -1)
(skip-chars-backward "\"'")
(unless (looking-back ar-assignment-regexp (line-beginning-position))
(ar--beginning-of-expression-intern orig)))
((looking-back "(\\|\\[" (line-beginning-position))
(forward-char -1)
(unless (looking-back ar-assignment-regexp (line-beginning-position))
(ar--beginning-of-expression-intern orig)))
((looking-back "[\])}]" (line-beginning-position))
(forward-char -1)
(unless (looking-back ar-assignment-regexp (line-beginning-position))
(ar--beginning-of-expression-intern orig)))
;; inside expression
((looking-back ar-expression-re (line-beginning-position))
(skip-chars-backward ar-expression-skip-chars)
(unless (or (looking-back "^[ \t]*" (line-beginning-position)) (looking-back ar-assignment-regexp (line-beginning-position)))
(ar--beginning-of-expression-intern orig)))
((looking-back (concat "[ \t]*" "[[:alnum:]_]*" ar-operator-regexp "[[:alnum:]_]*") (line-beginning-position) t)
(goto-char (match-beginning 0))
(unless (looking-back "^[ \t]*" (line-beginning-position))
(ar--beginning-of-expression-intern orig)))
((and (eq (point) orig) (looking-back "[ \t\r\n\f]" (line-beginning-position)))
(skip-chars-backward " \t\r\n\f")
(unless (bobp)
(forward-char -1)
(ar--beginning-of-expression-intern orig)))
((and (eq (point) orig) (not (bobp)) (looking-back ar-expression-re (line-beginning-position)))
(forward-char -1)
(when (< 0 (abs (skip-chars-backward ar-expression-skip-chars)))
(ar--beginning-of-expression-intern orig)))
((and (looking-at ar-expression-re) (not (looking-back "[ \t\r\n\f]" (line-beginning-position))))
(unless (< 0 (abs (skip-chars-backward ar-expression-skip-chars)))
(ar--beginning-of-expression-intern orig)))
((and (eq (point) orig)(looking-back "[ \t]*=" (line-beginning-position)))
(goto-char (match-beginning 0))
(skip-chars-backward " \t\r\n\f")
(ar--beginning-of-expression-intern orig)))
(unless (or (eq (point) orig)(looking-at "[ \t]*#"))
(setq erg (point)))
erg)))
(defun ar-forward-of-expression (&optional arg)
"Go to the end of a compound expression.
With numeric ARG do it that many times.
A a compound expression might be concatenated by \".\" operator,
thus composed by minor expressions."
(interactive "p")
(or arg (setq arg 1))
(let (erg)
(if (< 0 arg)
(save-restriction
(widen)
(while (< 0 arg)
(setq erg (ar--end-of-expression-intern))
(setq arg (1- arg))))
(setq arg (abs arg))
(setq erg (ar-backward-expression)))
(when (and ar-verbose-p (called-interactively-p 'any)) (message "%s" erg))
erg))
(defun ar--end-of-expression-intern (&optional orig)
(unless (eobp)
(let* ((orig (or orig (point)))
(pps (syntax-ppss))
erg
;; use by scan-lists
parse-sexp-ignore-comments)
(cond
((nth 1 pps)
(goto-char (nth 1 pps))
(let ((parse-sexp-ignore-comments t))
(forward-list))
(unless (or (looking-at "[ \t]*$")(looking-at ar-assignment-regexp))
(ar--end-of-expression-intern orig)))
;; in comment
((nth 4 pps)
(or (< (point) (progn (forward-comment 1)(point)))(forward-line 1))
(ar--end-of-expression-intern orig))
( ;; (empty-line-p)
(eq 9 (char-after))
(while
(and ;; (empty-line-p)
(eq 9 (char-after))(not (eobp)))
(forward-line 1))
(ar--end-of-expression-intern orig))
((looking-at (concat ar-string-delim-re ar-expression-re ar-string-delim-re ar-operator-regexp ar-string-delim-re ar-expression-re ar-string-delim-re))
(goto-char (match-end 0))
(while (looking-at (concat ar-operator-regexp ar-string-delim-re ar-expression-re ar-string-delim-re))
(goto-char (match-end 0))))
;; inside string
((ar-in-string-p)
(when (looking-at "\"\"\"\\|'''\\|\"\\|'")
(goto-char (match-end 0)))
(while
(nth 3 (syntax-ppss))
(forward-char 1))
(unless (looking-at "[ \t]*$")
(ar--end-of-expression-intern orig)))
((looking-at "[(\[]")
(forward-list)
(unless (looking-at "[ \t]*$")
(ar--end-of-expression-intern orig)))
((and (looking-at "[ \t]*#")(looking-back "^[ \t]*" (line-beginning-position)))
(while (and (looking-at "[ \t]*#") (not (eobp)))
(forward-line 1))
(ar--end-of-expression-intern orig))
((and (eq orig (point)) (looking-at ar-assignment-regexp))
(goto-char (match-end 0))
(if (looking-at "[(\[]")
(forward-list 1)
(ar--end-of-expression-intern orig)))
((looking-at (concat "[^ \t\n\r\f]*" ar-delimiter-regexp))
(goto-char (match-end 0))
(while (looking-at (concat "[^ \t\n\r\f]*" ar-delimiter-regexp))
(goto-char (match-end 0)))
(forward-char -1)
(unless (looking-at (concat ar-assignment-regexp "\\|[ \t]*$\\|" ar-delimiter-regexp))
(ar--end-of-expression-intern orig)))
((looking-at (concat "\\([[:alnum:] ]+ \\)" ar-assignment-regexp))
(goto-char (match-end 1))
(skip-chars-backward " \t\r\n\f"))
((and (eq orig (point)) (looking-at (concat "[ \t]*" "[^(\t\n\r\f]+" ar-operator-regexp)))
(skip-chars-forward " \t\r\n\f")
(when (< 0 (skip-chars-forward ar-expression-skip-chars))
(ar--end-of-expression-intern orig)))
((and (eq orig (point)) (looking-at ar-not-expression-regexp))
(skip-chars-forward ar-not-expression-chars)
(unless (or (looking-at "[ \t]*$")(looking-at ar-assignment-regexp))
(ar--end-of-expression-intern orig)))
((looking-at ar-expression-skip-regexp)
(skip-chars-forward ar-expression-skip-chars)
(unless (or (looking-at "[ \n\t\r\f]*$")(looking-at ar-assignment-regexp))
(ar--end-of-expression-intern orig)))
((and (eq (point) orig)
(skip-chars-forward " \t\r\n\f")
(< 0 (skip-chars-forward ar-expression-skip-chars)))
(ar--end-of-expression-intern orig)))
(unless (or (eq (point) orig)(and (eobp)(bolp)))
(setq erg (point)))
erg)))
(defun ar-backward-partial-expression ()
"Go to the beginning of a partial expression.
Optional argument ORIG Position."
(interactive)
(let ((orig (point))
erg)
(and (< 0 (abs (skip-chars-backward " \t\r\n\f")))(not (bobp))(forward-char -1))
(when (ar-in-comment-p)
(ar-backward-comment)
(skip-chars-backward " \t\r\n\f"))
;; part of ar-partial-expression-forward-chars
(when (member (char-after) (list ?\ ?\" ?' ?\) ?} ?\] ?: ?#))
(forward-char -1))
(skip-chars-backward ar-partial-expression-forward-chars)
(when (< 0 (abs (skip-chars-backward ar-partial-expression-backward-chars)))
(while (and (not (bobp)) (ar-in-comment-p)(< 0 (abs (skip-chars-backward ar-partial-expression-backward-chars))))))
(when (< (point) orig)
(unless
(and (bobp) (member (char-after) (list ?\ ?\t ?\r ?\n ?\f)))
(setq erg (point))))
(when (called-interactively-p 'any) (message "%s" erg))
erg))
(defun ar-forward-of-partial-expression ()
"Go to the end of a partial expression.
Optional argument ORIG Position."
(interactive)
(let (erg)
(skip-chars-forward ar-partial-expression-backward-chars)
;; group arg
(and
(looking-at "[\[{(]")
(goto-char (scan-sexps (point) 1)))
(setq erg (point))
(when (called-interactively-p 'any) (message "%s" erg))
erg))
(defun ar-forward-expression (&optional orig done repeat pps)
"Go to the end of a compound expression.
Operators are ignored.
Optional argument ORIG Position.
Optional argument DONE status.
Optional argument REPEAT counter.
Optional argument PPS result of ‘parse-partial-sexp’."
(interactive)
(unless done (skip-chars-forward " \t\r\n\f"))
(unless (eobp)
(let ((comment-start (ar-fix-comment-start))
(repeat (or (and repeat (1+ repeat)) 0))
(pps (or pps (parse-partial-sexp (point-min) (point))))
(orig (or orig (point)))
erg)
(if (< ar-max-specpdl-size repeat)
(error "`ar-forward-expression' reached loops max")
(cond
;; in comment
((nth 4 pps)
(or (< (point) (progn (forward-comment 1)(point)))(forward-line 1))
(ar-forward-expression orig done repeat))
;; empty before comment
((and comment-start (looking-at (concat "[ \t]*" comment-start))(looking-back "^[ \t]*" (line-beginning-position)))
(while (and (looking-at (concat "[ \t]*" comment-start)) (not (eobp)))
(forward-line 1))
(ar-forward-expression orig done repeat))
;; inside string
((nth 3 pps)
(goto-char (nth 8 pps))
(goto-char (scan-sexps (point) 1))
(setq done t)
(ar-forward-expression orig done repeat))
((looking-at "\"\"\"\\|'''\\|\"\\|'")
(goto-char (scan-sexps (point) 1))
(setq done t)
(ar-forward-expression orig done repeat))
((nth 1 pps)
(goto-char (nth 1 pps))