1

To set functions on a local buffer I use the following technique:

* Local Variables :noexport:
# Local Variables:
# eval: (view-mode 1)
# eval: (visual-line-mode 0)
...
# End:

However, when I try to set org-image-actual-width variable the following way:

* Local Variables :noexport:
# Local Variables:
# eval: (org-image-actual-width 600)
# End:

I have an error. And when I do:

* Local Variables :noexport:
# Local Variables:
# eval: (setq org-image-actual-width 600)
# End:

The variable is not only applied to the current buffer, but to all buffers.

What would be the proper way to set a variable on a single buffer only?

EDIT:

I found both Tobias' and NickD's answers very useful.

Tobias answered to the general question: How to set a variable on a single buffer?"

NickD answered to the specific question: why org-image-actual-width isn't working as it should?

14
  • Not recommended (@Tobias's answer is the way to do it), but you can try setq-local instead of setq.
    – NickD
    Sep 13, 2022 at 13:54
  • setq-local doesn't work either.
    – crocefisso
    Sep 13, 2022 at 14:57
  • I meant in the eval: (setq-local org-image-actual-width 600) - that should set the buffer-local value to 600 but leave the global value alone. Why it doesn't affect the displayed image size is a separate (and much less clear) question.
    – NickD
    Sep 13, 2022 at 15:22
  • Yes, I meant eval: (setq-local org-image-actual-width 600) doesn't work. And when I say doesn't work I mean, doesn't affect the displayed image. As I said, only setq affects the displayed image.
    – crocefisso
    Sep 13, 2022 at 15:31
  • 1
    Does this answer your question? Proper way to set file variables in an org-file. Also [this question] (emacs.stackexchange.com/q/47502). See tag file-local-variables for more.
    – Drew
    Sep 13, 2022 at 19:09

2 Answers 2

3

eval: is a special form in the Local Variables section that evaluates the given Elisp-form. That is not what you want.

The actual syntax for setting file local variables is:

MAYBE_COMMENT VARIABLE_NAME: VALUE

So you set org-image-actual-width file-locally in the following way:

* Local Variables :noexport:
# Local Variables:
# org-image-actual-width: 600
# End:

See the man-page about file local variables.

10
  • I tried # org-image-actual-width: 600 already, but it doesn't affect the image size. Contrary to # eval: (setq org-image-actual-width 600).
    – crocefisso
    Sep 13, 2022 at 13:29
  • Worked fine here. Two things: did you check the value of the variable in the buffer (C-h v org-image-actual-width)? and did you reapply the mode (M-x normal-mode) after you added the local variables section? Alternatively, did you save and kill the buffer and revisit the file? Local variables are only applied when the mode is applied.
    – NickD
    Sep 13, 2022 at 13:52
  • C-h v org-image-actual-width says :Its value is 600 Original value was t Local in buffer index.org; global value is t. But it is not displayed as 600. After adding the local variable, I deleted .emacs content, only leaving (require 'org) and the link to my init.org, then I restarted emacs.
    – crocefisso
    Sep 13, 2022 at 14:06
  • I don't know what M-x normal-mode does, but applying it didn't change the display, even after closing and reopening the file multiple times.
    – crocefisso
    Sep 13, 2022 at 14:13
  • Just to be complete, the image is displayed through a link (eg [[./image.gif]]), and my init contains the two following lines : (setq org-startup-with-inline-images t) and (setq-local org-image-actual-width '(1024)).
    – crocefisso
    Sep 13, 2022 at 14:17
2

[Not an answer: just a summary and linearization of the comments, which are pretty confusing as they stand and mostly deal with a different problem.]

  1. The way to set a local variable is as described in @Tobias' answer and that IS the answer to the question (which the OP has accepted).

  2. The additional problem was that the OP wanted the buffer to start with inline images displayed but at the same time, he wanted the size of those displayed images to be controlled by the locally set org-image-actual-width. That is a different problem and that was what the OP was referring to, in most cases, when he stated that it "did not work" (that's one reason why "it did not work" is not a good description of any problem: you have to say exactly what did not work - in this case, the local setting of the variable did work: but it required some additional digging to figure out what did not).

  3. The reason that part 2. above did not work is that the org-mode function (which is called automatically to set the major mode of the buffer when you first open the file) decides whether to display inline images (based on the global value of org-startup-with-inline-images or whether the keyword setting #+STARTUP: inlineimages is found in the file) and does so before the local file variables are looked at, so setting org-image-actual-width as a local file variable is not going to affect that initial display. Only after doing M-x org-toggle-inline-images twice (once to disable inline images and once to re-enable them) are the images redisplayed at the specified size.

A workaround for this sequencing problem is to augment the Local Variables section as follows:

* Local Variables :noexport:
# Local Variables:
# org-image-actual-width: 600
# eval: (org-display-inline-images)
# End:

Apparently, the local variables are evaluated top-to-bottom, so we first set org-image-actual-width and then do the eval which calls the function org-display-inline-images to display the images, but now at the specified width. The OP confirmed in the chat that that worked for him.

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.