Issue #17 resolved

normal-state interacts poorly with eval-last-sexp

Anonymous created an issue

When a sexp closes at the end of a line, you can't use eval-last-sexp in normal-state to evaluate it because you can't get to the end of the line.

My solution is this command: {{{ (evil-define-command my-evil-eval-sexp-at-point () "Eval sexp at point. Like eval-last-sexp, but one to the right." :repeat nil (interactive) (save-excursion (if (not (eolp)) (forward-char)) (call-interactively 'eval-last-sexp))) }}} but I haven't used it much yet, and I don't know if there are edge cases. I have it on :eval and ;v.

Comments (5)

  1. Frank Fischer repo owner

    You may try the following code which advices some function when evil is activated. If you use at least 35ff006 those modifications should be activated if and only if evil is active. If those modifications work they could go into evil's code.

    ;;; eval-last-sexp
    (defadvice eval-last-sexp (around evil)
      "Last sexp ends at point."
      (when (evil-normal-state-p)
        (save-excursion
          (unless (or (eobp) (eolp)) (forward-char))
          ad-do-it)))
    
    ;;; pp-eval-last-sexp
    (defadvice pp-eval-last-sexp (around evil)
      "Last sexp ends at point."
      (when (evil-normal-state-p)
        (save-excursion
          (unless (or (eobp) (eolp)) (forward-char))
          ad-do-it)))
    
  2. Anonymous

    Thinks. I had to change them slightly so that they continued to work in insert-mode (I'm not sure if that was the intent, but I like it like that). But I have discovered one edge case (which also affects my solution): save-excursion means you can't move the cursor when doing eval-last-sexp in normal-mode. My current fix is to advise the "find the last sexp" functions instead:

    (defadvice preceding-sexp (around evil)
      "In normal-state, last sexp ends at point."
      (if (evil-normal-state-p)
          (save-excursion
    	(unless (or (eobp) (eolp)) (forward-char))
    	ad-do-it)
        ad-do-it))
    
    (defadvice pp-last-sexp (around evil)
      "In normal-state, last sexp ends at point."
      (if (evil-normal-state-p)
          (save-excursion
    	(unless (or (eobp) (eolp)) (forward-char))
    	ad-do-it)
        ad-do-it))
    

    (I'm not sure why both of these functions exist.) I haven't noticed any problems with this. It's possible that if someone uses these functions noninteractively, things would break; in that case, I can't think of a better solution than "reimplement [pp-]eval-last-sexp". But in core, they don't seem to be used anywhere bad: just a helper function of eval-last-sexp, and pp-eval-last-sexp and pp-macroexpand-last-sexp, respectively.

  3. Log in to comment