malana
malana

Reputation: 5220

Correct use of apply in Emacs Lisp?

I'm not sure whether the use of apply is recommended here. Is there a better/standard solution for setting the major mode dynamically? I couldn't find any other.

Background:

Whenever I get the

X has auto save data; consider M-x recover-this-file

message in Emacs, I wonder what the difference between the current file and the auto-save version is. Since most of the time I can't be bothered to look it up, I tried to automate the task:

(defun ediff-auto-save ()
  "Ediff current file and its auto-save pendant."
  (interactive)
  (let ((auto-file-name (make-auto-save-file-name))
        (file-major-mode major-mode))
    (ediff-files buffer-file-name auto-file-name)
    (switch-to-buffer-other-window (file-name-nondirectory auto-file-name))
    (apply file-major-mode '())
    (other-window 1))) ;; back to ediff panel

The code does what I want, it opens the auto-save file and starts ediff. I also set the auto-save file's major mode to the major mode of the original file for consistent font lock.

Upvotes: 8

Views: 2024

Answers (2)

cobbal
cobbal

Reputation: 70733

While apply can certainly be used for this, funcall might be better suited

(funcall file-major-mode)

it differs from apply in that it doesn't take a list of arguments, just the arguments. Both of the following are equivalent:

(funcall '+ 1 2)
(apply '+ '(1 2))

Upvotes: 5

G__
G__

Reputation: 7111

It looks fine to me - that's what apply is for.

Besides, you said it yourself: the code does what you want! :-)

Upvotes: 2

Related Questions