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?

user3496846
  • 388
  • 1
  • 11

1 Answers1

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)))
John Kitchin
  • 11,891
  • 1
  • 20
  • 42