emacselispevil-mode

I expect this elisp function to visualize entire function, but it visualizes beginning to cursor


It looks like the function that I use is not correctly visualizing the entire function. Currently, the function only visually highlights the region from the beginning of the function to the current buffer cursor position.

I want to modify this function to select the function, from top to bottom.

(defun evil-visual-current-function ()
  "Highlight the entire current function in Evil's visual mode."
  (interactive)
  (let ((function-name (which-function)))
    (if function-name
        (save-excursion
          (beginning-of-defun)
          (evil-visual-make-selection (point)
          (end-of-defun) (point)))  ; move cursor to end of function
      (message "Not inside a function"))))

Solution

  • When you leave the save-excursion block, the cursor moves back to the original position, which changes the selected region. Change the function to:

    (defun evil-visual-current-function ()
      "Highlight the entire current function in Evil's visual mode."
      (interactive)
      (let ((function-name (which-function))
        (if function-name
            (progn
              (beginning-of-defun)
              (evil-visual-make-selection (point) (end-of-defun) (point)))
          (message "Not inside a function"))))