2

So, I am writing a function to italize/unitalize the current line (without the surrounding blanks) by surrounding it with slashes /.

  (defun my-italize-line-dwim ()
    (interactive)
    (save-excursion
      (if (and (progn (evil-last-non-blank) (equal (following-char) ?/))
               (progn (back-to-indentation) (equal (following-char) ?/)))
          (progn (delete-char 1) (evil-last-non-blank) (delete-char 1))
        (progn (back-to-indentation)
               (insert "/")
               (evil-last-non-blank)
               (evil-append 1)
               (insert "/")
               (evil-normal-state t)))))

While this may seem to work, there are some problems. If I want to use undo after the operation, undo-tree-undo undoes slashes one by one, instead of both at once. What's more, going to the previous line always means the cursor will jump to its first character. I feel like there should be a way to avoid both problems by somehow "atomizing" the operation, maybe by doing a copy/replace of the whole line at once. How should this be done?

2

In Emacs 26 I think you can do this:

(defun my-italize-line-dwim ()
  (interactive)
  (let ((handle (prepare-change-group)))
    (save-excursion
      (if (and (progn (evil-last-non-blank) (equal (following-char) ?/))
               (progn (back-to-indentation) (equal (following-char) ?/)))
          (progn (delete-char 1) (evil-last-non-blank) (delete-char 1))
    (progn (back-to-indentation)
               (insert "/")
               (evil-last-non-blank)
               (evil-append 1)
               (insert "/")
               (evil-normal-state t))))
    (undo-amalgamate-change-group handle)))
  • fixed both issues, thank you! – user3496846 Jul 10 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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