1

I have problems when using the suggestion from here Code faster by extending Emacs EVIL text object

(require 'dash)
(defun jds~evil-paren-range (count beg end type inclusive)
  (->> '((?\( . ?\))
           (?\[ . ?\])
           (?{ . ?})
           (?< . ?>))
         (--keep (save-excursion
                     (ignore-errors
                       (evil-select-paren (car it) (cdr it)
                                        beg end type
                                        count inclusive)) ))
         (-min-by (-on #'> (lambda (x)
                             (abs (- (car x) (point) )))))
         (-take 2)))

(evil-define-text-object jds~evil-a-paren (count &optional beg end type)
  "Select a paren."
  :extend-selection nil 
  (jds~evil-paren-range count beg end type t))

(evil-define-text-object jds~evil-inner-paren (count &optional beg end type)
  "Select inner paren."
    :extend-selection nil
        (jds~evil-paren-range count beg end type nil))

(define-key evil-inner-text-objects-map "d" #'jds~evil-inner-paren)
(define-key evil-outer-text-objects-map "d" #'jds~evil-a-paren)



This works great for visually selecting various delimiters in a dwim fasion but for some reason it doesnt work well for deleting, yanking or changing. Here is an example:

Say the point (indicated by |) is on a line as follows

    |{asdf}(bar)

vad works exactly as expected and visually selects {asdf}.

Yet with the point at same place, dad deletes (bar) leaving

    {asdf}|

What is going on? How do I fix this text object?

1 Answer 1

0

I don't use evil; I am barely 3 days in to switching to evil. So, I am responding to this question as part of my evil learning process.

Try this

(require 'dash)
(defun jds~evil-paren-range (count beg end type inclusive)
  (->> '((?\( . ?\))
           (?\[ . ?\])
           (?{ . ?})
           (?< . ?>))
         (--keep (ignore-errors
                   (save-excursion
                     (evil-select-paren (car it) (cdr it)
                                        beg end type
                                        count inclusive))))
         (-min-by (-on #'> (lambda (x)
                             (abs (- (car x) (point) )))))
         (-take 2)))

(evil-define-text-object jds~evil-a-paren (count &optional beg end type)
  "Select a paren."
  :extend-selection nil 
  (jds~evil-paren-range count beg end type t))

(evil-define-text-object jds~evil-inner-paren (count &optional beg end type)
  "Select 'inner' paren."
    :extend-selection nil
        (jds~evil-paren-range count beg end type nil))

(define-key evil-inner-text-objects-map "d" #'jds~evil-inner-paren)
(define-key evil-outer-text-objects-map "d" #'jds~evil-a-paren)


  • Both evil-a-paren and evil-inner-paren has :extend-selection nil.

  • evil-select-paren moves point (or errs out). The original code should use save-excursion

  • evil-up-block is something you have added, and the corresponding branch is never taken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.