-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathobjed-objects.el
2994 lines (2588 loc) · 98.1 KB
/
objed-objects.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
;;; objed-objects.el --- Part of the objed package -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2019 Free Software Foundation, Inc.
;; Author: Clemens Radermacher <[email protected]>
;; Package-Requires: ((emacs "25") (cl-lib "0.5"))
;; Version: 0.8.1
;; Maintainer: Clemens Radermacher <[email protected]>
;; URL: https://github.com/clemera/objed
;; 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 for text objects used by `objed'.
;;; Code:
;; * Bytecomp
(require 'cl-lib)
(require 'subword)
(require 'face-remap)
;; info for byte-comp
(defvar objed-map)
(defvar objed-object-map)
(declare-function avy-process "ext:avy")
(declare-function avy--style-fn "ext:avy")
(declare-function avy-goto-char "ext:avy")
(declare-function stripe-buffer-mode "ext:stripe-buffer")
(declare-function sgml-skip-tag-backward "ext:sgml-mode")
(declare-function sgml-skip-tag-forward "ext:sgml-mode")
(declare-function org-at-heading-p "ext:org")
(declare-function org-back-to-heading "ext:org")
(declare-function outline-back-to-heading "ext:outline")
(declare-function outline-next-heading "ext:outline")
(declare-function outline-previous-heading "ext:outline")
(declare-function outline-next-visible-heading "ext:outline")
(declare-function outline-previous-visible-heading "ext:outline")
(declare-function objed--object-dispatch "ext:objed")
(declare-function objed-current-or-next-context "ext:objed")
(declare-function objed-current-or-previous-context "ext:objed")
(declare-function objed--get-current-state "ext:objed")
(declare-function objed--install-advices "ext:objed")
(declare-function objed--install-advices-for "ext:objed")
(declare-function objed-goto-next-identifier "ext:objed")
(declare-function objed-goto-prev-identifier "ext:objed")
(declare-function objed-next-identifier "ext:objed")
(declare-function objed-prev-identifier "ext:objed")
(declare-function objed-first-identifier "ext:objed")
(declare-function objed-last-identifier "ext:objed")
;; * Macros
(defvar-local objed--object nil
"The symbol of the current object.")
(eval-and-compile
(require 'rx)
(defun objed--get-regex-object (bregex eregex)
"Return regex object between BREGEX and EREGEX.
The inner object part will be the text between the matches for
those two expressions.
BREGEX is the regular expression for the start of the object. If
the regular expressions contains a group, any text which is part
of this group will belong to the inner object part.
EREGEX is the regular expression for the end of the object. If
the regular expressions contains a group, any text which is part
of this group will belong to the inner object part.
EREGEX can also be an empty string. In this case objects are
separated by the BREGEX expression and reach until the next one
or until the buffer end if no next instance can be found."
(let* ((obounds ())
(ibounds ())
(opos (point)))
(save-mark-and-excursion
;; try to move into object when at boundary
(if (looking-at bregex)
(goto-char (or (match-end 1)
(match-end 0)))
(if (looking-back eregex (line-beginning-position))
(goto-char (or (match-beginning 1)
(match-beginning 0)))
(re-search-forward eregex nil t)
(goto-char (or (match-beginning 1)
(match-beginning 0)))))
(when (and ;; goto possible start
(re-search-backward bregex nil t)
(push (or (match-beginning 1)
(match-end 0))
ibounds)
(push (match-beginning 0)
obounds)
;; goto possible end
(goto-char (or (match-end 1)
(match-end 0)))
(cond ((string= "" eregex)
;; no end provided, objects are separated by beginning
;; regex
(cond ((re-search-forward bregex nil t)
(push (match-beginning 0) obounds)
;; no way to tell so just skip ws
;; to have a sensible default
(goto-char (car obounds))
(objed--skip-ws t)
(push (point) ibounds))
(t
;; if there is no match that means the buffer is
;; the object end
(goto-char (point-max))
(push (point) obounds)
(objed--skip-ws t)
(push (point) ibounds))))
(t
(re-search-forward eregex nil t)
(push (or (match-end 1)
(match-beginning 0))
ibounds)
(push (match-end 0)
obounds)))
;; when point was within start and end
(<= (cadr obounds) opos (car obounds)))
(list (nreverse obounds)
(nreverse ibounds))))))
(defun objed--transform-pos-data (plist)
(cond ((and (plist-get plist :beg)
(or (plist-get plist :end)
(stringp (plist-get plist :beg))))
(let ((np nil)
(alt nil)
(skip nil))
;; filter :beg :end keywords
(dolist (item plist)
(if (memq item '(:beg :ibeg :end :iend))
(progn (push item alt)
(setq skip t))
(if (and skip
(not (keywordp item)))
(push item alt)
(push item np)
(setq skip nil))))
;; new and alternate plists
(setq np (nreverse np))
(setq alt (nreverse alt))
;; merge ... :get-obj "regex search"
(if (and (stringp (plist-get plist :beg))
(or (stringp (plist-get plist :end))
(not (plist-get plist :end))))
(let ((bregex (plist-get plist :beg))
(eregex (or (plist-get plist :end)
"")))
(append np
(list :try-prev)
(if (string= "" eregex)
(list `(when (re-search-backward ,bregex)
(goto-char (match-end 0))))
(list `(when (re-search-backward ,eregex)
(goto-char (match-beginning 0)))))
(list :try-next)
(list `(when (re-search-forward ,bregex)
(goto-char (match-end 0))))
(list :get-obj)
(list
`(objed--get-regex-object ,bregex
,eregex))))
;; merge ... :get-obj (objed-make-object :beg ... :end...)
(let ((make nil))
(dolist (el alt)
(when (keywordp el)
(progn
(push el make)
(push (plist-get alt el) make))))
(setq make (nreverse make))
(push 'objed-make-object make)
(append np
(list :get-obj)
;; TODO:save-mark-and-excursion still needed?
;; is wrapped already?
(list (append (list 'save-mark-and-excursion)
(list make))))))))
(t
(user-error "Malformed macro"))))
(defun objed--get-arg-plist (keylst valid &optional wrapped)
"Wraps any forms of keys in keylst in `progn' and returns property list.
KEYLST is the list of keys and forms for object creation. VALID
is a list of valid keyword for the returned list whic is a
property list where each key has an associated progn."
(let* ((keyw (pop keylst))
(vkeyw (and keyw (keywordp keyw) (memq keyw valid) keyw))
forms)
(cond ((memq vkeyw '(:mode :no-skip :commands))
;; skip
(objed--get-arg-plist (cdr keylst) valid wrapped))
(vkeyw
(while (and (not (keywordp (car keylst)))
keylst)
(push (pop keylst) forms))
(push keyw wrapped)
;; allowed to move point
(cond ((memq vkeyw '(:try-next :try-prev :ref))
(push `(let ((objed--block-p t))
,@(nreverse forms))
wrapped))
((memq vkeyw '(:beg :end :ibeg :iend))
(if (and (not (cdr forms))
(stringp (car forms)))
(push (car forms) wrapped)
(if (and (not (cdr forms))
(eq (caar forms) 'rx))
(push (macroexpand-1 (car forms))
wrapped)
(push `(let ((objed--block-p t))
,@(nreverse forms))
wrapped))))
(t
;; objed--block-p: dont run objeds advices here
(push `(let ((objed--block-p t))
(save-mark-and-excursion
,@(nreverse forms)))
wrapped)))
(objed--get-arg-plist keylst valid wrapped))
(keylst
(error "Malformed Object. Keyword %s not recognized" keyw))
(t
(nreverse wrapped))))))
(defmacro objed-define-object (package name &rest args)
"Declare a text object for `objed'.
Usage:
(objed-define-object package name
[:keyword [code-form]...]...)
This macro creates a command named objed-<name>-object. This
command can be used to activate objed for the defined object and
is used internally to query for information needed for objed
commands.
PACKAGE is the name of the package the object should be loaded
for. If non-nil this will defer loading until PACKAGE is
available.
NAME is a symbol which defines the name which will be used to
refer to this object. ARGS is a list of keyword arguments and
corresponding values according to the following descriptions:
:get-obj
Code to run which returns object positions as a list of the form:
((object-start object-end)
(inner-start inner-end))
The function `objed-make-object' can be used to create such a
list. For convenience it is also possible that the code returns a
cons cell of the bounds of object (like what the built-in
`bounds-of-thing-at-point' variations return). If inner positions
are omitted they are determined by `objed--inner-default'. If
there is no object at point the code should return nil.
:beg, :ibeg, :end, :iend
These keywords can be used instead of :get-obj above. The value
for each is the code to run which should return the point
position corresponding to the keyword. Point is allword to move
between the keyword expression. The code runs in the same order
the keywords are provided.
It is also possible to use only :beg and :end with regular
expressions to define an object. See `objed--get-regex-object'
for details of their format. If :end is omitted the regexp
provided by :beg separates the objects on its own. This can be
used for text objects which don't have an end marker.
:try-next (optional)
Code to run which moves point to the next available object.
If :no-skip is not set the code can assume it runs after point is
moved out to the end of the current one if any. This will get
called until :get-obj returns non-nil. To indicate that search
needs to be stopped, throw an error.
:try-prev (optional)
Code to run which moves point to the previous available object.
The code can assume it runs after point is moved to the beginning
of the current one if any. This will get called until :get-obj
returns non-nil. To indicate that search needs to be stopped,
throw an error.
:mode (optional)
Object defintions which use this keyword derive from an already
existing object with the same NAME. If given it should be a
symbol of a `major-mode'. Any keyword definitions of the mode
specific version will override the ones from the non mode
specific version.
:atp (optional)
Code to run which returns non-nil if point is right before the
object.
:ref (optional)
Code to run which returns an object symbol which can be used to
navigate references of an object. This defaults to the textual
content of an object.
:max-search-forward (optional)
Code to run which returns the maximal position an object of this type
is searched for (in forward direction).
:no-skip (optional)
If this keyword is provided with a non-nil value, the current object
is not skipped before search for the next one via :try-next.
:commands (optional)
If given the value should be a list of commands for which objed
should activate (when variable `objed-mode' is on) with the object beeing
defined."
(declare (indent 2))
(let* ((mode (plist-get args :mode))
(noskip (plist-get args :no-skip))
(commands (plist-get args :commands))
(fname (if mode
(intern (format "objed-%s-%s-object" name mode))
(intern (format "objed-%s-object" name))))
;; wrap code chunks
(args (objed--get-arg-plist
args
'(:mode :no-skip :max-search-forward :commands :atp :ref :get-obj :try-next :try-prev
:beg :ibeg :iend :end)))
;; transform to final form if necessary
(args (if (plist-get args :get-obj) args
(objed--transform-pos-data args)))
(arg (make-symbol "arg"))
(cbody nil)
(doc (format "%s object." (capitalize (symbol-name name))))
(atp (plist-get args :atp))
(max (plist-get args :max-search-forward))
(obj (plist-get args :get-obj))
(next (plist-get args :try-next))
(prev (plist-get args :try-prev))
(ref (plist-get args :ref)))
(unless mode
(push `((and (eq real-this-command ',fname)
(not ,arg))
(objed--object-dispatch ',name))
cbody))
(when atp
(push `((eq ,arg :atp)
,atp)
cbody))
(when max
(push `((eq ,arg :max-search-forward)
,max)
cbody))
(when ref
(push `((eq ,arg :ref)
,ref)
cbody))
(when obj
(push `((eq ,arg :get-obj)
(let ((pdata ,obj))
(if (and (consp pdata)
(not (consp (cdr pdata))))
(objed-make-object :obounds pdata)
pdata)))
cbody))
(when next
(push `((eq ,arg :try-next)
,next)
cbody))
(when prev
(push `((eq ,arg :try-prev)
,prev)
cbody))
(cond (mode
(let ((res (if package
(list `',package 'with-eval-after-load)
(list 'progn))))
(when noskip
(push `(put ',fname 'objed-no-skip t)
res))
(when commands
(push `(with-eval-after-load 'objed
(objed--install-advices-for ',commands ',name))
res))
;; catch all return arg if not present
(push `(t ,arg) cbody)
(push `(defun ,fname (,arg)
,doc
(cond ,@(nreverse cbody)))
res)
(nreverse res)))
(t
(let ((res (if package
(list `',package 'with-eval-after-load)
(list 'progn))))
(when noskip
(push `(put ',fname 'objed-no-skip t)
res))
(when commands
(push `(objed--install-advices-for ',commands ',name)
res))
(push `(defun ,fname (,arg)
,doc
(interactive "i")
(cond ,@(nreverse cbody)))
res)
(nreverse res))))))
(defun objed--define-kpair (map key name)
"Use MAP to define KEY for object NAME."
(let ((cmd (objed--name2func name 'nomode)))
(define-key map key cmd)))
(defun objed-define-global-object-keys (&rest kpairs)
"Define global object keys.
KPAIRS are pairs of the key and the object name."
(let ((map (default-value 'objed-object-map)))
(while kpairs
(objed--define-kpair map (pop kpairs) (pop kpairs)))))
(defun objed-define-local-object-keys (&rest kpairs)
"Define object keys locally for current buffer.
This function is intended to be used inside mode hooks to add
mode specific object bindings to the currently existing ones. If
you want to replace the object key bindings entirely with local
ones see `objed-define-local-object-keys*'.
KPAIRS are pairs of the key and the object name."
(unless (local-variable-p 'objed-map)
(setq-local objed-map
(make-composed-keymap nil (default-value 'objed-map))))
(unless (local-variable-p 'objed-object-map)
(setq-local objed-object-map
(make-composed-keymap nil (default-value 'objed-object-map))))
(while kpairs
(objed--define-kpair objed-object-map (pop kpairs) (pop kpairs)))
(let ((switchk (where-is-internal (default-value 'objed-object-map)
(default-value 'objed-map) t)))
(define-key objed-map switchk objed-object-map)))
(defun objed--init-local-map (lmap map)
"Initilize local LMAP to shadow bindings in MAP."
(map-keymap (lambda (ev def)
(if (keymapp def)
(objed--init-local-map lmap def)
(define-key lmap (vector ev) nil)))
map))
(defun objed-define-local-object-keys* (&rest kpairs)
"Define object keys locally for current buffer.
This function is intended to be used inside mode hooks and
creates new object key bindings for the current buffer. This
means any previous object key bindings bindings are replaced by
the bindings defined in KPAIRS.
If you only want to add or change a few mode specific bindings
see `objed-define-local-object-keys'.
KPAIRS are pairs of the key and the object name."
(unless (local-variable-p 'objed-map)
(setq-local objed-map
(make-composed-keymap nil (default-value 'objed-map))))
(setq-local objed-object-map (make-sparse-keymap))
(objed--init-local-map objed-object-map (default-value 'objed-map))
(while kpairs
(objed--define-kpair objed-object-map (pop kpairs) (pop kpairs)))
(let ((switchk (where-is-internal (default-value 'objed-object-map)
(default-value 'objed-map) t)))
(define-key objed-map switchk objed-object-map)))
(defmacro objed--with-narrow-for-text (&rest body)
"Execute BODY narrowed to string or comment."
`(save-restriction
;; stay inside
(unless (derived-mode-p 'text-mode)
(objed--narrow-if-string-or-comment))
,@body))
;; * Global vars saving object information
;; TODO: use defstruct object instead
(defvar objed--current-obj nil
"The current object position data.
Positions are stored in a list of the form:
((object-start object-end)
(inner-start inner-end))")
(defvar-local objed--obj-state nil
"The state used to get object positions.
Either the symbol `whole' or `inner'.")
(defvar objed--marked-ovs nil
"List of overlays of marked objects.")
;; * Internal object access functions
(defun objed--inside-object-p (obj)
"Return non-nil if point point inside object OBJ."
(let* ((objed--object obj)
(objed--obj-state 'whole)
(obj (if (symbolp obj)
(objed--get)
obj)))
(when (and obj (not (objed--distant-p obj)))
obj)))
(defun objed--beg (&optional obj)
"Get beginning position of object.
Ignores current object state. OBJ is the object to use and
defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(caar obj)))
(defun objed--end (&optional obj)
"Get end position of object.
Ignores current object state. OBJ is the object to use and
defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(cadr (car obj))))
(defun objed--object-at-point (obj &optional state)
"Return object data for OBJ and STATE a point.
Does return nil when there is no such object."
(let* ((objed--object obj)
(objed--obj-state (or state 'whole))
(o (ignore-errors (objed--object :get-obj))))
(when o
(if (eq state 'inner)
(nreverse o)
o))))
(defun objed--other (&optional obj)
"Return object position opposite to point.
Ignores current object state. OBJ is the object to use and
defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (/= (point) (objed--end))
(objed--end obj)
(objed--beg obj))))
(defun objed--min (&optional obj)
"Get minimal position of current object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(objed--apply #'min obj)))
(defun objed--max (&optional obj)
"Get maximal position of current object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(objed--apply #'max obj)))
(defvar objed--basic-objects
'(sexp line identifier word char region buffer)
"Basic objects.
Basic object are objects which have no next/previous or which
have their own movement commands.")
(defun objed--basic-p ()
"Return non-nil if current object is a basic object.
From basic objects `objed' starts expanding to context objects.
Thus this should be objects which have their own movement
commands."
(memq objed--object objed--basic-objects))
(defun objed--current (&optional obj)
"Get the current range of interest.
If the region is active the range is defined by the region bounds
otherwise the its the head of object OBJ which defaults to
`objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(cond ((region-active-p)
(list (region-beginning)
(if (objed--basic-p)
(region-end)
(max (objed--end)
(region-end)))))
(t
(car obj)))))
(defun objed--bounds (&optional obj)
"Get the current object bounds.
If the region is active bounds are the region bounds otherwise
the head of object OBJ which defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (region-active-p)
(cons (region-beginning) (region-end))
(cons (objed--beg obj)
(objed--end obj)))))
(defun objed--alt (&optional obj)
"Get the current tail of `objed--current-obj'.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(cadr obj)))
(defun objed--alt-beg (&optional obj)
"Get beginning position of tail of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let* ((obj (or obj objed--current-obj))
(posn (objed--alt obj)))
(car posn)))
(defun objed--alt-end (&optional obj)
"Get end position of tail of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let* ((obj (or obj objed--current-obj))
(posn (objed--alt obj)))
(cadr posn)))
(defun objed--obeg (&optional obj)
"Get beginning position of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--alt-beg obj)
(objed--beg obj))))
(defun objed--oend (&optional obj)
"Get end position of object.
OBJ is the object to use and defaults to `objed--current-obj'"
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--alt-end obj)
(objed--end obj))))
(defun objed--ibeg (&optional obj)
"Get inner beginning position of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--beg obj)
(objed--alt-beg obj))))
(defun objed--iend (&optional obj)
"Get inner end position of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--end obj)
(objed--alt-end obj))))
(defun objed--get-left-boundary ()
"Get left boundary of current object."
(buffer-substring (objed--obeg) (objed--ibeg)))
(defun objed--get-right-boundary ()
"Get right boundary of current object."
(buffer-substring (objed--iend) (objed--oend)))
(defun objed--object-string ()
"Return object string content."
(filter-buffer-substring (objed--beg)
(objed--end)))
(defun objed--at-object-p (obj)
"Return non nil when point is at object OBJ."
(funcall (objed--name2func obj) :atp))
(defun objed--goto-char (pos)
"Move to position POS possibly skipping leading whitespace."
(goto-char
(if (eq objed--object 'char)
pos
(objed--skip-forward pos 'ws))))
(defun objed--collect-backward (pos min &optional collf)
"Collect object positions in backward direction.
Start from position POS and stop at MIN position.
The returned list contains cons cells of the start positions of
the objects and the current window. If COLLF is t collect the
end positions instead. Leading or trailing ws are ignored by
default.
If COLLF is function it recieves the object as argument and
should return the data to collect."
(let ((cw (get-buffer-window))
(sobj nil)
(posns nil)
(objed--obj-state 'whole)
(obj nil))
(save-excursion
(goto-char pos)
(while (and (> pos min)
(setq obj (objed--get-prev (point)))
(not (equal obj sobj)))
(setq sobj obj)
(goto-char (setq pos (objed--beg obj)))
(cond ((and collf
(not (eq collf t)))
(push (funcall collf obj) posns))
(t
(push (cons (if (eq collf t)
(objed--skip-backward
(objed--max obj) 'ws)
(objed--skip-forward pos 'ws))
cw)
posns))))
posns)))
(defun objed--collect-forward (pos max &optional collf)
"Collect object positions in forward direction.
Start from position POS and stop at MAX position.
The returned list contains cons cells of the start positions of
the objects and the current window. If COLLF is t collect the
end positions instead. Leading or trailing ws are ignored by
default.
If COLLF is function it recieves the object as argument and
should return the data to collect."
(let ((cw (get-buffer-window))
(sobj nil)
(posns nil)
(objed--obj-state 'whole)
(obj nil))
(save-excursion
(goto-char pos)
(while (and (< pos max)
(setq obj (objed--get-next (point)))
(not (equal obj sobj)))
(setq sobj obj)
(if (objed--no-skipper-p)
(goto-char (setq pos (objed--beg obj)))
(goto-char (setq pos (objed--end obj))))
(cond ((and collf (not (eq collf t)))
(push (funcall collf obj) posns))
(t
(push (cons (if (eq collf t)
(objed--skip-backward
(objed--max obj) 'ws)
(objed--skip-forward (objed--beg obj) 'ws))
cw)
posns)))))
(setq posns (nreverse posns))))
(defun objed--no-skipper-p ()
"If current object should be skipped."
(get (objed--name2func objed--object)
'objed-no-skip))
(defun objed--collect-object-positions (beg end &optional fromp collf)
"Collect object positions.
Returns object positions between BEG and END.
If FROMP is non-nil collect from that position otherwise collect before
and after current object.
By default the returned list contains cons cells of the start
positions of the objects and the current window.
COLLF has the same meaning as for `objed--collect-forward' and
`objed--collect-backward'."
(save-restriction
(narrow-to-region beg end)
(append (objed--collect-backward
(or fromp (objed--min))
beg collf)
(objed--collect-forward
(or fromp (if (objed--no-skipper-p)
(objed--min) (objed--max)))
end collf))))
(defun objed--map (fun &optional obj beg end)
"Call FUN with object data for each object of current type.
Return a list of the results.
If OBJ is on-nil it should be the symbol of the object type to
search for. By default search the whole buffer, alternatively
provide BEG and END position for region to search."
(let ((objed--object (or obj objed--object)))
(objed--collect-object-positions
(or beg (point-min))
(or end (point-max))
(point) fun)))
(defun objed--objects (&optional obj beg end)
"Return list of all objects of current type.
If OBJ is on-nil it should be the symbol of the object type to
search for. By default search the whole buffer, alternatively
provide BEG and END position for region to search."
(objed--map #'identity obj beg end))
(defun objed--collect-object-lines ()
"Collect first lines of objects before and after current object.
Each string has its position as property pos on the first
character of the string."
(let (lines llb lle lb le)
(dolist (pos2win (objed--collect-object-positions
(point-min)
(point-max))
(nreverse lines))
(save-excursion
(setq lle le)
(setq llb lb)
(goto-char (car pos2win))
(let ((str (buffer-substring
(setq lb (objed--skip-forward
(line-beginning-position) 'ws))
(setq le (line-end-position)))))
;; one object per line
(when (and (or (not llb)
(< lle lb)
(not lle)
(> llb le))
(not (string= "" str)))
(push (cons str (point)) lines)))))))
(defvar avy-action)
(defvar avy-all-windows)
(defun objed--ace-until (&optional start back)
"Get position of object using `avy'.
Start at pos START. Default to forward unless BACK is non-nil."
(let* ((avy-action #'identity)
(avy-style 'at-full)
(avy-all-windows t)
(posns (if back
(objed--collect-backward
(or start (point))
(window-start) t)
(objed--collect-forward
(or start (point))
(window-end)))))
(save-excursion
(cond (posns
(let ((pos (if (> (length posns) 1)
(avy-process posns (avy--style-fn avy-style))
(caar posns))))
(when (integer-or-marker-p pos)
(objed--get back pos))))
(t
(prog1 nil
(message "No objects found.")))))))
(defun objed--get (&optional dir pos)
"Get object at current position.
Direction defaults to forward unless DIR is non-nil which means
to search backwards.
POS defaults to point. When no object is found at current
position returns the next accessible one in DIR. Object position
order depends on `objed--obj-state'. To exit early from search
objects can throw an error."
(save-excursion
(let ((darg (if dir :try-prev :try-next))
(max (if dir nil (objed--object :max-search-forward)))
(stop (if dir #'bobp #'eobp)))
(when pos
(goto-char pos))
(let (invisible break nobj obj)
;; while there is no new object found which is visible
;; and the buffer boundary is not reached
(while (and (not break)
(or (not max)
(<= (point) max))
(or (not (and (setq nobj (objed--object :get-obj))
(and nobj (not (equal obj nobj)))))
(and nobj
;; update the last seen one
(setq obj nobj)
(setq invisible
(objed--invisible-p (objed--beg nobj)))))
(not (funcall stop)))
(cond (invisible
(setq invisible nil)
(goto-char (if dir
(objed--prev-visible-point)
(objed--next-visible-point))))
(t
(let ((f (if dir '> '<))
(step (if dir -1 1)))
(unless (funcall stop)
(let ((pos (point)))
(objed--object darg)
;; if point has not moved
;; fallback to move one char
;; in right direction
(when (= (point) pos)
(forward-char step))
;; check for valid move direction to avoid inf. loop if
;; the code of object misbehaves
(when (funcall f (point) pos)
(setq break t))))))))
(if (objed--inner-p)
(nreverse nobj)
nobj)))))
(defun objed--copy-object (&optional obj)
"Make a deep copy of object OBJ.
OBJ defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(list (copy-sequence (car obj))
(copy-sequence (cadr obj)))))
(defun objed--get-object (o &optional s)
"Get object O with state S.
When object O is already current return it."
(if (eq objed--object o)
objed--current-obj
(let ((objed--object o)
(objed--obj-state (or s 'whole)))
(objed--get))))
(defun objed--name2func (name &optional no-mode)
"Return function name for object with NAME.
If NO-MODE is non-nil, ignore mode specific versions."