-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvilpy.el
More file actions
5703 lines (5300 loc) · 203 KB
/
Copy pathvilpy.el
File metadata and controls
5703 lines (5300 loc) · 203 KB
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
;;; vilpy.el --- vi-like Paredit. -*- lexical-binding: t -*-
;; Author: André Peric Tavares <andre.peric.tavares@gmail.com>
;; URL: https://github.com/Andre0991/vilpy
;; Version: 0.1.6 (beta)
;; Keywords: lisp
;; This file is not part of GNU Emacs
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is a stripped-down fork of the excellent [lispy](https://github.com/abo-abo/lispy), a paredit-like mode.
;;; Code:
;;* Requires
; built-in
(eval-when-compile
(require 'eldoc))
(require 'mode-local)
(require 'help-fns)
(require 'outline)
(require 'newcomment)
(require 'delsel)
(require 'pcase)
(require 'cl-lib)
; external
(require 'avy)
;;; Langugages configuration
(defvar vilpy--handlers-alist
'((:emacs-lisp . ((:decider-fn . (lambda () (or (derived-mode-p 'emacs-lisp-mode)
(derived-mode-p 'lisp-interaction-mode))))
(:eval-last-sexp . eval-last-sexp)
(:eval-defun . eval-defun)
(:eval-region . eval-region)
(:eval-buffer . eval-buffer)
(:describe-symbol . vilpy--emacs-lisp-describe-symbol)
(:indent-sexp . vilpy--prettify-emacs-lisp-sexp)))
(:inf-clojure . ((:decider-fn . (lambda () (bound-and-true-p inf-clojure-minor-mode)))
(:eval-last-sexp . inf-clojure-eval-last-sexp)
(:describe-symbol . vilpy--inf-clojure-describe-symbol)
(:eval-defun . inf-clojure-eval-defun)
(:eval-region . inf-clojure-eval-region)
(:eval-buffer . inf-clojure-eval-buffer)
(:indent-sexp . vilpy-clojure-indent)))
(:cider . ((:decider-fn . (lambda () (bound-and-true-p cider-mode)))
(:eval-last-sexp . cider-eval-last-sexp)
(:describe-symbol . vilpy--cider-describe-symbol)
(:eval-defun . cider-eval-defun-at-point)
(:eval-region . cider-eval-region)
(:eval-buffer . cider-eval-buffer)
(:indent-sexp . vilpy-clojure-indent)))
;; Fallback for clojure, in case `cider` and `inf-clojure` are not activated
;; Do not move this up to in this list - that would always ignore cider and inf-clojure,
;; which should have higher priority.
(:clojure . ((:decider-fn . (lambda () (memq major-mode vilpy-clojure-modes)))
(:indent-sexp . vilpy-clojure-indent))))
"An alist that determine which functions will run for language specific features.
Some commands (eg. `vilpy-eval`) consider this list for deciding the appropriate handler
for some feature.
The alist keys are arbitrary, but they tipically represent major or minor modes.
The values are describe below:
`decider-fn`: A function with no arguments that returns non-nil if the set of commands
in this list is appropriate for the current buffer.
`eval-last-sexp`, `eval-defun`, `eval-region`, `describe-symbol` and
`indent-sexp` should be interactive functions.")
(defvar vilpy-elisp-modes
'(emacs-lisp-mode
lisp-interaction-mode
minibuffer-inactive-mode)
"Modes for which emacs-lisp related functions are appropriate.")
(defvar vilpy-clojure-modes
'(clojure-mode
clojure-ts-mode
clojurescript-mode
clojurec-mode)
"Modes for which clojure related functions are appropriate.")
(defvar vilpy-map-input-overlay nil
"The input overlay for mapping transformations.")
;; TODO: Should this be suspect to comment-char handling as well?
(defvar-local vilpy-outline-header ";;"
"Store the buffer-local outline start.")
;;* Customization
(defgroup vilpy nil
"List navigation and editing for the Lisp family."
:group 'bindings
:prefix "vilpy-")
(defvar vilpy-left "[([{]"
"Opening delimiter.")
(defvar vilpy-right "[])}]"
"Closing delimiter.")
(defvar vilpy-outline "^;;\\(?:;[^#]\\|\\*+\\)"
"Outline delimiter.")
(defcustom vilpy-no-space nil
"When non-nil, don't insert a space before parens/brackets/braces/colons."
:type 'boolean
:group 'vilpy)
(make-variable-buffer-local 'vilpy-no-space)
(defcustom vilpy-lax-eval t
"When non-nil, fix \"unbound variable\" error by setting the it to nil.
This is useful when hacking functions with &optional arguments.
So evaling (setq mode (or mode major-mode)) will set mode to nil on
the first eval, and to major-mode on the second eval."
:type 'boolean
:group 'vilpy)
(defcustom vilpy-verbose t
"If t, vilpy will display some messages on error state.
These messages are similar to \"Beginning of buffer\" error for
`backward-char' and can safely be ignored."
:type 'boolean
:group 'vilpy)
(defcustom vilpy-close-quotes-at-end-p nil
"If t, when pressing the `\"' at the end of a quoted string, it will move you past the end quote."
:type 'boolean
:group 'vilpy)
(defcustom vilpy-avy-style-char 'pre
"Method of displaying the overlays for a char during visual selection."
:type '(choice
(const :tag "Pre" pre)
(const :tag "At" at)
(const :tag "At full" at-full)
(const :tag "Post" post)))
(defcustom vilpy-avy-style-paren 'at
"Method of displaying the overlays for a paren during visual selection."
:type '(choice
(const :tag "Pre" pre)
(const :tag "At" at)
(const :tag "At full" at-full)
(const :tag "Post" post)))
(defcustom vilpy-avy-style-symbol 'pre
"Method of displaying the overlays for a symbol during visual selection."
:type '(choice
(const :tag "Pre" pre)
(const :tag "At" at)
(const :tag "At full" at-full)
(const :tag "Post" post)))
(defcustom vilpy-avy-keys (number-sequence ?a ?z)
"Keys for jumping."
:type '(repeat :tag "Keys" (character :tag "char")))
(defvar vilpy-mode-map (make-sparse-keymap))
(defvar vilpy-ignore-whitespace nil
"When set to t, function `vilpy-right' will not clean up whitespace.")
(defcustom vilpy-compat '()
"List of package compatibility options.
Enabling them adds overhead, so make sure that you are actually
using those packages."
:type '(repeat
(choice
(const :tag "god-mode" god-mode)
(const :tag "magit-blame-mode" magit-blame-mode)
(const :tag "cider" cider)
(const :tag "macrostep" macrostep))))
(defvar-local vilpy-old-outline-settings nil
"Store the old values of `outline-regexp' and `outline-level'.
`vilpy-mode' overrides those while it's on.")
(defcustom vilpy-safe-delete nil
"When non-nil, killing/deleting an active region keeps delimiters balanced.
This applies to `vilpy-delete', `vilpy-paste', and
`vilpy-delete-backward'."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-safe-copy nil
"When non-nil, `vilpy-copy' won't copy unbalanced delimiters in a region."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-safe-paste nil
"When non-nil, `vilpy-paste' will add missing delimiters."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-safe-threshold 1500
"The max size of an active region that vilpy will try to keep balanced.
This only applies when `vilpy-safe-delete', `vilpy-safe-copy', and/or
`vilpy-safe-paste' are non-nil."
:group 'vilpy
:type 'number)
(defcustom vilpy-safe-actions-ignore-strings t
"When non-nil, don't try to act safely in strings.
Any unmatched delimiters inside of strings will be copied or deleted. This only
applies when `vilpy-safe-delete', `vilpy-safe-copy', and/or `vilpy-safe-paste'
are non-nil."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-safe-actions-ignore-comments t
"When non-nil, don't try to act safely in comments.
Any unmatched delimiters inside of comments will be copied or deleted. This only
applies when `vilpy-safe-delete', `vilpy-safe-copy', and/or `vilpy-safe-paste'
are non-nil."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-safe-actions-no-pull-delimiters-into-comments nil
"When non-nil, don't pull unmatched delimiters into comments when deleting.
This prevents the accidental unbalancing of expressions from commenting out
delimiters. This only applies when `vilpy-safe-delete', `vilpy-safe-copy',
and/or `vilpy-safe-paste' are non-nil."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-insert-space-after-wrap t
"When non-nil, insert a space after the point when wrapping.
This applies to the commands that use `vilpy-pair'."
:group 'vilpy
:type 'boolean)
(defcustom vilpy-thread-last-macro "thread-last"
"Threading macro to use by default in command `vilpy-thread-last'."
:type '(radio
(const :tag "Elisp" "thread-last")
(const :tag "Clojure" "->>")
(string :tag "Custom")))
(defun vilpy-comment-char (&optional level postfix)
"Get the `comment-start' character, or `;' if nil, repeated LEVEL times concated with POSTFIX."
(concat
(apply #'concat (make-list (or level 1) (or comment-start ";")))
(or postfix "")))
;;;###autoload
(define-minor-mode vilpy-mode
"Minor mode for navigating and editing LISP dialects.
When `vilpy-mode' is on, most unprefixed keys,
i.e. [a-zA-Z+-./<>], conditionally call commands instead of
self-inserting. The condition (called special further on) is one
of:
- the point is before \"(\"
- the point is after \")\"
- the region is active
For instance, when special, \"j\" moves down one sexp, otherwise
it inserts itself.
When special, [0-9] call `digit-argument'.
When `vilpy-mode' is on, \"[\" and \"]\" move forward and
backward through lists, which is useful to move into special.
\\{vilpy-mode-map}"
:keymap vilpy-mode-map
:group 'vilpy
:lighter " VP"
(if vilpy-mode
(progn
(require 'eldoc)
(eldoc-remove-command 'special-vilpy-eval)
(eldoc-remove-command 'special-vilpy-x)
(eldoc-add-command 'vilpy-space)
(setq vilpy-old-outline-settings
(cons outline-regexp outline-level))
(setq-local outline-level 'vilpy-outline-level)
(cond ((eq major-mode 'latex-mode)
(setq-local vilpy-outline "^\\(?:%\\*+\\|\\\\\\(?:sub\\)?section{\\)")
(setq vilpy-outline-header "%")
(setq-local outline-regexp "\\(?:%\\*+\\|\\\\\\(?:sub\\)?section{\\)"))
((eq major-mode 'python-mode)
(setq-local vilpy-outline "^#\\*+")
(setq vilpy-outline-header "#")
(setq-local outline-regexp "#\\*+")
(setq-local outline-heading-end-regexp "\n"))
(t
(setq-local outline-regexp (substring vilpy-outline 1)))))
(when vilpy-old-outline-settings
(setq outline-regexp (car vilpy-old-outline-settings))
(setq outline-level (cdr vilpy-old-outline-settings))
(setq vilpy-old-outline-settings nil))))
;;* Macros
(defmacro vilpy-dotimes (n &rest bodyform)
"Execute N times the BODYFORM unless an error is signaled.
Return nil if couldn't execute BODYFORM at least once.
Otherwise return the amount of times executed."
(declare (indent 1)
(debug (form body)))
`(let ((i 0))
(catch 'result
(condition-case e
(progn
(while (<= (cl-incf i) ,n)
,@bodyform)
,n)
(error
(when (eq (car e) 'buffer-read-only)
(message "Buffer is read-only: %s" (current-buffer)))
(cl-decf i)
(and (> i 0) i))))))
(defmacro vilpy-save-excursion (&rest body)
"More intuitive (`save-excursion' BODY)."
(declare (indent 0))
`(let ((out (save-excursion
,@body)))
(when (vilpy-bolp)
(back-to-indentation))
out))
(defmacro vilpy-from-left (&rest body)
"Ensure that BODY is executed from start of list."
(declare (debug (body)))
(let ((at-start (cl-gensym "at-start")))
`(let ((,at-start (vilpy--leftp)))
(unless ,at-start
(vilpy-other))
(unwind-protect
(vilpy-save-excursion
,@body)
(unless (eq ,at-start (vilpy--leftp))
(vilpy-other))))))
(defmacro vilpy-flet (binding &rest body)
"Temporarily override BINDING and execute BODY."
(declare (indent 1))
(let* ((name (car binding))
(old (cl-gensym (symbol-name name))))
`(let ((,old (symbol-function ',name)))
(unwind-protect
(progn
(fset ',name (lambda ,@(cdr binding)))
,@body)
(fset ',name ,old)))))
(defmacro vilpy-multipop (lst n)
"Remove LST's first N elements and return them."
`(if (<= (length ,lst) ,n)
(prog1 ,lst
(setq ,lst nil))
(prog1 ,lst
(setcdr
(nthcdr (1- ,n) (prog1 ,lst (setq ,lst (nthcdr ,n ,lst))))
nil))))
;;* Globals: navigation
(defsubst vilpy-right-p ()
"Return t if after variable `vilpy-right'."
(looking-back vilpy-right
(line-beginning-position)))
(defsubst vilpy-left-p ()
"Return t if before variable `vilpy-left'."
(looking-at vilpy-left))
(defsubst vilpy-looking-back (regexp)
"Forward to (`looking-back' REGEXP)."
(looking-back regexp (line-beginning-position)))
(defun vilpy-forward (arg)
"Move forward list ARG times or until error.
Return t if moved at least once,
otherwise call function `vilpy-right' and return nil."
(interactive "p")
(when (= arg 0)
(setq arg 2000))
(vilpy--exit-string)
(let ((bnd (vilpy--bounds-comment)))
(when bnd
(goto-char (1+ (cdr bnd)))))
(let ((pt (point))
(r (vilpy-dotimes arg
(when (= (point) (point-max))
(error "Reached end of buffer"))
(forward-list))))
;; `forward-list' returns true at and of buffer
(if (or (null r)
(= pt (point))
(and (not (vilpy-right-p))
(progn
(backward-list)
(forward-list)
(= pt (point)))))
(prog1 nil
(vilpy--out-forward 1))
(point))))
(defun vilpy-backward (arg)
"Move backward list ARG times or until error.
If couldn't move backward at least once, move up backward and return nil."
(interactive "p")
(when (= arg 0)
(setq arg 2000))
(vilpy--exit-string)
(let ((bnd (vilpy--bounds-comment)))
(when bnd
(goto-char (car bnd))))
(let ((pt (point))
(r (vilpy-dotimes arg
(when (= (point) (point-min))
(error "Reached beginning of buffer"))
(backward-list))))
;; `backward-list' returns true at beginning of buffer
(if (or (null r)
(= pt (point))
(and (not (vilpy-left-p))
(progn
(forward-list)
(backward-list)
(= pt (point)))))
(prog1 nil
(condition-case nil
(progn
(vilpy--out-forward 1)
(backward-list))
(error
(progn
(goto-char pt)
(up-list -1)))))
(point))))
(defun vilpy-right (arg)
"Move outside list forwards ARG times.
Return nil on failure, t otherwise."
(interactive "p")
(vilpy--remember)
(when (bound-and-true-p abbrev-mode)
(ignore-errors (expand-abbrev)))
(cond ((region-active-p)
(vilpy-mark-right arg))
((looking-at vilpy-outline)
(vilpy-outline-right))
(t
(vilpy--out-forward arg))))
(defun vilpy-step-out (arg)
"Move outside list forwards ARG times.
Return nil on failure, t otherwise."
(interactive "p")
(vilpy--remember)
(cond ((region-active-p)
(vilpy-mark-left arg))
((looking-at vilpy-outline)
(vilpy-outline-left))
(t
(or (vilpy--out-backward arg)
(ignore-errors
(up-list -1))))))
(defun vilpy-out-forward-newline (arg)
"Call `vilpy--out-forward', then ARG times `newline-and-indent'."
(interactive "p")
(vilpy--out-forward 1)
(vilpy-dotimes arg
(newline-and-indent)))
(defun vilpy--re-search-in-code (regexp direction &optional count)
"Move to the next REGEXP in DIRECTION, COUNT times.
DIRECTION is either 'forward or 'backward.
Return the amount of successful moves, or nil otherwise."
(setq count (or count 1))
(let ((to-move (abs count))
(advancer
(if (eq direction 'forward)
(if (> count 0)
#'re-search-forward
#'re-search-backward)
(if (> count 0)
#'re-search-backward
#'re-search-forward)))
(pt (point)))
(if (and (eq direction 'forward) (> count 0))
(when (looking-at regexp)
(goto-char (match-end 0))))
(while (and (> to-move 0)
(funcall advancer regexp nil t))
(unless (vilpy--in-string-or-comment-p)
(cl-decf to-move)))
(if (= to-move (abs count))
(progn
(goto-char pt)
nil)
(if (eq direction 'forward)
(goto-char (match-beginning 0)))
(- count to-move))))
;;* Locals: navigation
(defun vilpy-step-in (arg)
"Move inside list ARG times.
Don't enter strings or comments.
Return nil if can't move."
(interactive "p")
(vilpy--remember)
(let ((pt (point))
r)
(cond
((and (vilpy-bolp)
(looking-at (vilpy-comment-char)))
(setq r (vilpy--re-search-in-code vilpy-left 'forward arg)))
((vilpy-left-p)
(setq r (vilpy--re-search-in-code vilpy-left 'forward arg)))
((vilpy-right-p)
(backward-char)
(when (setq r (vilpy--re-search-in-code vilpy-right 'backward arg))
(forward-char))))
(or r
(progn
(goto-char pt)
nil))))
(defun vilpy-down (arg)
"Move down ARG times inside current list."
(interactive "p")
(vilpy--remember)
(cond ((region-active-p)
(let ((leftp (= (point) (region-beginning))))
(when leftp
(exchange-point-and-mark))
(cond ((save-excursion
(skip-chars-forward " \n")
(eobp)))
((vilpy--symbolp (vilpy--string-dwim))
(vilpy-dotimes arg
(when (vilpy-slurp 1)
(vilpy-other)
(vilpy-barf 1)
(vilpy-other))))
((looking-at "[\n ]+\\(;\\)")
(deactivate-mark)
(goto-char (match-beginning 1))
(vilpy--mark (vilpy--bounds-comment)))
(t
(vilpy-dotimes arg
(forward-sexp 1)
(vilpy-other)
(if (vilpy--in-comment-p)
(progn
(goto-char (1+ (cdr (vilpy--bounds-comment))))
(skip-chars-forward "\n"))
(forward-sexp 2)
(forward-sexp -1))
(vilpy-other))))
(when leftp
(exchange-point-and-mark))))
((vilpy-left-p)
(vilpy-forward arg)
(let ((pt (point))
(vilpy-ignore-whitespace t))
(if (vilpy-forward 1)
(vilpy-backward 1)
(goto-char pt)
(vilpy-other))))
((vilpy-right-p)
(let ((pt (point)))
(unless (vilpy-forward arg)
(goto-char pt))))
((or (looking-at vilpy-outline)
(and (bolp) (looking-at (vilpy-comment-char))))
(let ((pt (point))
(outline-regexp vilpy-outline))
(vilpy-dotimes arg
(outline-next-visible-heading 1)
(if (looking-at vilpy-outline)
(setq pt (point))
(goto-char pt)
(error "Last outline reached")))))
(t
(vilpy-forward 1)
(vilpy-backward 1)))
(vilpy--ensure-visible))
(defun vilpy-up (arg)
"Move up ARG times inside current list."
(interactive "p")
(vilpy--remember)
(cond ((region-active-p)
(let ((leftp (= (point) (region-beginning))))
(unless leftp
(exchange-point-and-mark))
(cond ((save-excursion
(skip-chars-backward "\n ")
(bobp)))
((looking-back "^ *\\(;\\)[^\n]*[\n ]*"
(save-excursion
(ignore-errors
(backward-sexp 1))
(point)))
(deactivate-mark)
(goto-char (match-beginning 1))
(vilpy--mark (vilpy--bounds-comment))
(exchange-point-and-mark))
((vilpy--symbolp (vilpy--string-dwim))
(vilpy-dotimes arg
(when (vilpy-slurp 1)
(vilpy-other)
(vilpy-barf 1)
(vilpy-other))))
(t
(vilpy-dotimes arg
(backward-sexp 1)
(vilpy-other)
(if (vilpy--in-comment-p)
(progn
(goto-char (1- (car (vilpy--bounds-comment))))
(skip-chars-backward "\n"))
(backward-sexp 2)
(backward-sexp -1))
(vilpy-other))))
(unless leftp
(exchange-point-and-mark))))
((vilpy-left-p)
(let ((pt (point)))
(unless (vilpy-backward arg)
(goto-char pt))))
((vilpy-right-p)
(vilpy-backward arg)
(let ((pt (point)))
(if (vilpy-backward 1)
(vilpy-forward 1)
(goto-char pt)
(vilpy-other))))
((or (looking-at vilpy-outline)
(and (bolp) (looking-at (vilpy-comment-char))))
(let ((pt (point))
(outline-regexp vilpy-outline))
(vilpy-dotimes arg
(outline-previous-visible-heading 1)
(if (looking-at vilpy-outline)
(setq pt (point))
(goto-char pt)
(error "First outline reached")))))
(t
(vilpy-backward 1)
(vilpy-forward 1)))
(vilpy--ensure-visible))
(defvar vilpy-pos-ring (make-ring 100)
"Ring for point/mark position and restriction history.")
(defun vilpy--remember ()
"Store the current point and mark in history."
(let* ((emptyp (zerop (ring-length vilpy-pos-ring)))
(top (unless emptyp
(ring-ref vilpy-pos-ring 0)))
(restriction (when (buffer-narrowed-p)
(cons (set-marker (make-marker)
(point-min))
(set-marker (make-marker)
(point-max))))))
(if (region-active-p)
(let* ((bnd (vilpy--bounds-dwim))
(bnd (cons
(move-marker (make-marker) (car bnd))
(move-marker (make-marker) (cdr bnd)))))
(when (or emptyp
(not (equal bnd top)))
(ring-insert vilpy-pos-ring (list bnd restriction))))
(when (or emptyp
(not (equal (point-marker) top)))
(ring-insert vilpy-pos-ring (list (point-marker) restriction))))))
(defvar vilpy-back-restore-restriction t
"When non-nil, restore buffer restriction on `vilpy-back'.")
(defun vilpy-back (arg)
"Move point to ARGth previous position.
If position isn't special, move to previous or error."
(interactive "p")
(when (buffer-narrowed-p)
(widen))
(vilpy-dotimes arg
(if (zerop (ring-length vilpy-pos-ring))
(vilpy--complain "At beginning of point history")
(let* ((data (ring-remove vilpy-pos-ring 0))
(marker (pop data))
(restriction (pop data))
(beg (car restriction))
(end (cdr restriction)))
;; After deleting some text, markers that point to it converge
;; to one point
(while (and (not (zerop (ring-length vilpy-pos-ring)))
(equal (ring-ref vilpy-pos-ring 0)
marker))
(ring-remove vilpy-pos-ring 0))
(if (consp marker)
(vilpy--mark marker)
(deactivate-mark)
(switch-to-buffer (marker-buffer marker))
(goto-char marker))
(when (and vilpy-back-restore-restriction
restriction)
(narrow-to-region beg end)
(set-marker beg nil)
(set-marker end nil))))))
(defun vilpy-knight-down ()
"Make a knight-like move: down and right."
(interactive)
(cond ((vilpy-right-p)
(vilpy-other))
((vilpy-left-p))
(t (vilpy-backward 1)))
(let ((pt (point))
(bnd (save-excursion
(vilpy-beginning-of-defun)
(vilpy--bounds-list))))
(catch 'done
(while t
(forward-line)
(cond ((>= (point) (cdr bnd))
(goto-char pt)
(throw 'done nil))
((looking-at (concat "\\s-*" vilpy-left))
(goto-char (1- (match-end 0)))
(throw 'done t)))))))
(defun vilpy-knight-up ()
"Make a knight-like move: up and right."
(interactive)
(cond ((vilpy-right-p)
(vilpy-other))
((vilpy-left-p))
(t (vilpy-backward 1)))
(let ((pt (point))
(bnd (save-excursion
(vilpy-beginning-of-defun)
(vilpy--bounds-list))))
(catch 'done
(while t
(beginning-of-line 0)
(cond ((< (point) (car bnd))
(goto-char pt)
(throw 'done nil))
((looking-at (concat "\\s-*" vilpy-left))
(goto-char (1- (match-end 0)))
(throw 'done t)))))))
(defun vilpy-other ()
"Switch to the different side of current sexp."
(interactive)
(cond ((and (region-active-p)
(not (= (region-beginning) (region-end))))
(exchange-point-and-mark))
((vilpy-right-p)
(backward-list))
((vilpy-left-p)
(forward-list))
(t
(user-error "Unexpected"))))
(defun vilpy-go-to-first-defun ()
"Sets the mark and moves the point to the first defun."
(interactive)
(push-mark)
(vilpy-beginning-of-defun)
(let ((previous-point (point)))
(vilpy-up 1)
(while (not (= previous-point (point)))
(setq previous-point (point))
(vilpy-up 1)))
(message "Mark saved where command was called"))
(defun vilpy-go-to-last-defun ()
"Sets the mark and moves the point to the last defun."
(interactive)
(push-mark)
(vilpy-beginning-of-defun)
(let ((previous-point (point)))
(vilpy-down 1)
(while (not (= previous-point (point)))
(setq previous-point (point))
(vilpy-down 1)))
(message "Mark saved where command was called"))
;;* Globals: kill, yank, delete, mark, copy
(defun vilpy-kill ()
"Kill line, keeping parens consistent."
(interactive)
(let (bnd)
(cond ((or (vilpy--in-comment-p)
(and (looking-at " *;")
(save-excursion
(goto-char (match-end 0))
(vilpy--in-comment-p))))
(kill-line))
((and (setq bnd (vilpy--bounds-string))
(or
(not (eq (point) (car bnd)))
(> (count-lines (car bnd) (cdr bnd)) 1)))
(if (> (cdr bnd) (line-end-position))
(if (eq (point) (car bnd))
(kill-region (car bnd) (cdr bnd))
(kill-line))
(kill-region (point) (1- (cdr bnd)))))
((looking-at " *\n")
(kill-region
(match-beginning 0)
(match-end 0))
(vilpy--indent-for-tab))
((and (looking-at vilpy-right) (looking-back vilpy-left
(line-beginning-position)))
(delete-char 1)
(backward-delete-char 1))
((vilpy-left-p)
(if (progn
(setq bnd (vilpy--bounds-list))
(> (count-lines (car bnd) (cdr bnd)) 1))
(kill-region (car bnd)
(cdr bnd))
(narrow-to-region (car bnd) (line-end-position))
(let ((pt (point)))
(while (and (ignore-errors
(forward-list))
(> (point) pt))
(setq pt (point)))
(when (looking-at "[\t ]*;[^\n]*$")
(setq pt (match-end 0)))
(goto-char (point-min))
(widen)
(kill-region (point) pt))))
(t
(let ((beg (point))
(end (line-end-position))
bnd)
(while (and (< (point) end)
(ignore-errors
(forward-sexp 1)
(skip-chars-forward " ")
t))
(when (setq bnd (vilpy--bounds-comment))
(goto-char (cdr bnd))))
(skip-chars-forward " \t")
(kill-region beg (point)))))))
(defun vilpy-kill-word (arg)
"Kill ARG words, keeping parens consistent."
(interactive "p")
(if (< arg 0)
(vilpy-backward-kill-word (- arg))
(let (bnd)
(vilpy-dotimes arg
(while (not (or (eobp)
(memq (char-syntax (char-after))
'(?w ?_))))
(forward-char 1))
(when (or (vilpy-looking-back (concat vilpy-left " +"))
(vilpy-looking-back (vilpy-comment-char 1 " +")))
(delete-horizontal-space))
(if (setq bnd (vilpy--bounds-string))
(save-restriction
(narrow-to-region (1+ (car bnd)) (1- (cdr bnd)))
(kill-word 1)
(widen))
(kill-word 1))))))
(defun vilpy-backward-kill-word (arg)
"Kill ARG words backward, keeping parens consistent."
(interactive "p")
(let (bnd
(pt (point))
(last-command (if (eq last-command 'vilpy-backward-kill-word)
'kill-region
last-command)))
(vilpy-dotimes arg
(when (vilpy--in-comment-p)
(skip-chars-backward " \n"))
(if (memq (char-syntax (char-before))
'(?w ?_ ?\s))
(if (vilpy-looking-back "\\_<\\s_+")
(delete-region (match-beginning 0)
(match-end 0))
(backward-kill-word 1)
(when (and (vilpy--in-string-p)
(not (vilpy-looking-back "\\\\\\\\"))
(vilpy-looking-back "\\\\"))
(delete-char -1)))
(delete-region (point) pt)
(while (not (or (bobp)
(memq (char-syntax (char-before))
'(?w ?_))))
(backward-char 1))
(if (setq bnd (vilpy--bounds-string))
(progn
(save-restriction
(if (and (looking-at "\\s-+\"")
(eq (match-end 0) (cdr bnd)))
(goto-char (1- (cdr bnd)))
(when (and (> pt (car bnd))
(< pt (cdr bnd)))
(goto-char pt)))
(narrow-to-region (1+ (car bnd)) (point))
(kill-region (progn
(forward-word -1)
(when (and (not (vilpy-looking-back "\\\\\\\\"))
(vilpy-looking-back "\\\\"))
(backward-char))
(point))
(point-max))
(widen)))
(backward-kill-word 1))))))
(defvar vilpy-delete-sexp-from-within nil
"When cursor is adjacent to an opening or closing pair,
`vilpy-delete' or `vilpy-delete-backward' toward the delimiter
will kill the whole sexp (string or list).")
(defun vilpy-delete (arg)
"Delete ARG sexps."
(interactive "p")
(let (bnd)
(cond ((< arg 0)
(vilpy-delete-backward (- arg)))
((region-active-p)
(vilpy--maybe-safe-delete-region (region-beginning) (region-end)))
((setq bnd (vilpy--bounds-string))
(cond ((eq (1+ (point)) (cdr bnd))
(goto-char (car bnd))
(when vilpy-delete-sexp-from-within
(vilpy-delete arg)))
((looking-at "\\\\\"")
(if (eq (+ (point) 2) (cdr bnd))
(goto-char (car bnd))
(delete-char 2)))
((and (looking-at "\"")
(vilpy-looking-back "\\\\"))
(backward-char 1)
(delete-char 2))
((vilpy--delete-pair-in-string "\\\\\\\\(" "\\\\\\\\)"))
((looking-at "\\\\\\\\")
(delete-char 2))
((and (looking-at "\\\\")
(vilpy-looking-back "\\\\"))
(backward-char 1)
(delete-char 2))
((eq (point) (car bnd))
(delete-region (car bnd)
(cdr bnd))
(let ((pt (point)))
(skip-chars-forward " ")
(delete-region pt (point))))
((save-excursion
(forward-char 1)
(vilpy--in-string-or-comment-p))
(delete-char arg))
(t
(vilpy--exit-string))))
((vilpy--in-comment-p)
(if (vilpy-bolp)
(let ((bnd (vilpy--bounds-comment)))
(delete-region (car bnd) (cdr bnd)))
(delete-char arg)))
((looking-at vilpy-right)
(vilpy-step-out 1)
(when vilpy-delete-sexp-from-within
(vilpy-delete arg)))
((vilpy-left-p)
(vilpy--delete-leading-garbage)