I put 'portable' in quotes because, as we all know, Emacs works with plain text and this plain text is innately portable. However, the format of this plain text is not.

My particular use-case is with Org documents. I have various customizations set up for Org, but I would like to use the defaults for one particular document as to use a 'neutral third-party' format. Normally I would use my defaults, but this document is being shared among peer developers – each likely to have their own customizations.

How can I get a list of variables that have been customized in the buffer and insert either (with a C-u prefix) their default values or their local values?

  • 1
    Interesting question! Two points (or "partial solutions"): (1) you can always use emacs -Q and (2) you can also take your init.el on a flash drive. Of course, these do not answer your question, but someone looking for a similar thing might find these tips helpful. – mbork Nov 16 '14 at 15:46
  • @mbork Good points :) And yes – it doesn't answer my question, but perhaps I should edit it with an important detail… ;) – Sean Allred Nov 16 '14 at 15:47
  • Also, one thought: you might try to advice make-variable-buffer-local and/or make-local-variable so that they create a list of buffer-local variables. – mbork Nov 16 '14 at 15:49
  • Not a solution, but a thought: desktop-restore-frames in 24.4 added some functionality pretty similar to this. Could you use desktops for this out of the box (probably some big portability issues here)? If not, you might scope the source for some inspiration: repo.or.cz/w/emacs.git/blob/HEAD:/lisp/desktop.el#l745 – Mian Nov 17 '14 at 14:10

You could start with the following. Be sure to go through the list of setq statements and remove buffer-undo-list or mark-ring and the like.

(defun my-local-variables (&optional buffer)
  (setq buffer (or buffer (current-buffer)))
  (mapatoms (lambda (sym)
              (condition-case err
                  (when (and (boundp sym)
                             (not (equal (buffer-local-value sym buffer)
                                         (default-value sym))))
                    (insert (format "(setq %S %s%S); default: %S\n"
                                    (if (listp (buffer-local-value sym buffer)) "'" "")
                                    (buffer-local-value sym buffer)
                                    (default-value sym))))
                ;; handle "Local in buffer *scratch*; globally void" variables
                (error nil)))))
| improve this answer | |

Evaluating this at the end of the file will give you a Local Variables block to work from.

The important bit is the function buffer-local-variables.

The list will likely be much bigger than you expected, and the output most likely wouldn't actually evaluate as-is, so you'll certainly need to manually edit out all the bits you don't need.

M-: (progn (insert "# Local Variables:\n") (mapcar (lambda (x) (insert (format "# %s: %S\n" (car x) (cdr x)))) (buffer-local-variables)) (insert "# End:\n")) RET

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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