Fork me on GitHub
#spacemacs
<
2019-11-01
>
johanatan17:11:45

@jack.crawley92 this is what i came up with:

(defun join-to-previous (prefix)
  (interactive "p")
  (save-excursion
    (evil-previous-line prefix)
    (dotimes (var prefix)
      (join-line 1))))
(define-key evil-normal-state-map (kbd "K") 'join-to-previous)

johanatan17:11:53

this preserves undo semantics

Jcaw17:11:01

Looks good

Jcaw17:11:46

Oh - convention for custom defuns is to use my- or your username (or a contraction of it) as the prefix, to avoid potential namespace clashes.

👍 4
johanatan18:11:14

weirdly, calling (evil-join (- (line-number-at-pos) prefix) (line-number-at-pos)) did not work. it joined lines 14 and 15 even though i was at like line 447 in the file.

johanatan18:11:36

hence my inlining of the evil-join guts there

Jcaw18:11:23

evil-join probably takes point position, not line number

Jcaw18:11:54

Yep, looking at the source it seems to.

johanatan19:11:02

well that's a bummer

Jcaw19:11:43

Yeah, looks like a restriction of the macro used to define evil commands. Does seem a bit opaque.

johanatan19:11:52

but my code above is fine. no need to over optimise this

Jcaw19:11:08

Yep. And internally, it uses the same method you did:

(evil-define-operator evil-join (beg end)
  "Join the selected lines."
  :motion evil-line
  (let ((count (count-lines beg end)))
    (when (> count 1)
      (setq count (1- count)))
    (goto-char beg)
    (dotimes (var count)
      (join-line 1))))

johanatan19:11:22

yea, that's where i got my code from lol