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.
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.
Calling a function is also known as invocation. The most common way of invoking a function is by evaluating a list. For example, evaluating the list (concat "a" "b") calls the function concat with arguments "a" and "b" . See Evaluation, for a description of evaluation.
Emacs Lisp is not a purely functional programming language since side effects are common. Instead, Emacs Lisp is considered an early functional flavored language. The following features contribute to the functional flavor: its notation is functional (including a lambda calculus-like notation – see LambdaExpression)
Learning a little Emacs Lisp will help you use Emacs more effectively: You will better understand the documentation and online help for functions and variables. You will be able to consult the Lisp source code for a function or variable, in order to understand it still better.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With