Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of apply in Emacs Lisp?

Tags:

emacs

elisp

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.

like image 213
malana Avatar asked Jun 10 '10 13:06

malana


People also ask

What is Emacs Lisp used for?

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.

How do you call a function in Emacs?

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.

Is Emacs functional Lisp?

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)

Should I learn Emacs Lisp?

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.


1 Answers

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))
like image 119
cobbal Avatar answered Sep 19 '22 21:09

cobbal