1

Is it possible to force Emacs to only take into account the last (resp. the first) appearance of a customization?

For example if there are the following lines in the init file, is there a way that Emacs only takes into account the second (resp. the first) one?

(set-frame-height (selected-frame) 36)
(set-frame-height (selected-frame) 46)
3
  • I don't quite get this question. The first sentence is requesting the standard behavior, and the second sentence is answered by a "Yes".
    – Malabarba
    Commented Jan 24, 2015 at 14:06
  • @Malabarba I edited the question. I was not aware that taking into account the last customization is a standard behavior. So my question is essentially reduced in asking is there any way to force Emacs to take into account the first appearance of a customization?
    – Name
    Commented Jan 24, 2015 at 14:09
  • 1
    Name: Be aware that Emacs customisations are programs written in the Emacs Lisp (elisp) programming language, and consequently the order of the instructions can be extremely important.
    – phils
    Commented Jan 25, 2015 at 8:54

2 Answers 2

7

The init file is just a series of commands, that are run in turn. So the first command will be executed setting the height to 36, then the second will be run setting it to 46. The final effect is that you get a frame with the last mentioned size (provide there are no commands in between that change the frame selection). You may or may not notice the intermediate setting taking effect.

Be aware that standard customisations as written out by the custom interface are saved in alphabetical order and evaluated in that order.

2

In addition to what @AndrewSwann said -

There is a difference between customizations in the sense of custom-set-variables and custom-set-faces, on the one hand, and using isolated sexps such as (set-frame-height (selected-frame) 36) in your init file, on the other hand.

The former are designed to work with the Customize interface, and they are pretty solid. The latter depend on the state that is current when they are evaluated, and you are on your own wrt managing and keeping track of such state.

In particular, if you use something like (set-frame-height (selected-frame) 36) at top level in your init file then it is up to you to ensure that the frame you want to modify is the one that is selected when that sexp is evaluated. Typically you would instead customize an option such as default-frame-alist, or you would at least make sure of the frame you are operating on.

Sexp order always matters, including in your init file (and including the position of custom-set-variables etc.), but the more you depend on the order the more you need to take it into account and manage it.

In general, you are better off following the conventional practices. If you want to do something beyond what Customize offers, then define mode or other functions that do exactly what you want, and invoke those from your init file. IOW, encapsulate your fiddling a bit, to ensure that the context is correct.

(In addition to recommending that you do as much as you can using Customize, I would also recommend that you separate what it does from your init file, by taking advantage of variable custom-file.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.