6

Suppose I'm working on a file in org-mode or enriched-mode or html-mode ..., and I wanted to see the same contents in an other window with an other mode, say text-mode, how to do that?
I tried to clone the buffer using C-x 4 c, but when I change the mode in the cloned buffer, It changes in the original buffer too, even that its mode line doesn't say so.

  • 4
    The problem with clone-indirect-buffer is that text properties are attached to the text but not to the buffer and font-lock works on text properties. From what I know the only way to have two independent font-locks on the same text is to copy the text into another non-file buffer and to synchronize all modifications that are not text property changes. – Tobias Oct 22 '18 at 10:48
  • so is there anyway to see the same contents in different modes? – Bite Bytes Oct 22 '18 at 10:50
  • 2
    Are you aware of mmm-mode. One can have multiple major-modes on one buffer. – Tobias Oct 22 '18 at 10:58
  • 1
    There is another ugly but simple hack: Create a new buffer, visit the same file there (without clone-buffer-indirect) and switch on auto-revert-mode. Therewith you can have alternate font-lock for the same modified file whenever you save. The code follows... – Tobias Oct 22 '18 at 22:36
  • 1
    (defun new-buffer-same-file (&optional buf) "Create a new buffer for the same file as BUF is visiting." (interactive) (auto-revert-mode) (let ((file (buffer-file-name buf)) (new-buf (generate-new-buffer (buffer-name buf)))) (with-current-buffer new-buf (setq buffer-file-name file) (auto-revert-mode) (display-buffer)))) – Tobias Oct 22 '18 at 22:36
0

I have written up a library jac.el that does exactly what I have described as solution in my comment:

The problem with clone-indirect-buffer is that text properties are attached to the text but not to the buffer and font-lock works on text properties. From what I know the only way to have two independent font-locks on the same text is to copy the text into another non-file buffer and to synchronize all modifications that are not text property changes.

Just put the file jac.el into your load-path and add

(autoload 'jac "jac")

into your init file. Re-evaluate the init file or restart Emacs.

Afterwards you can clone buffers with jac. The clones do not inherit the text properties from their originals.

Unlike it is the case for clone-indirect-buffer buffers cloned with jac can have different major modes and are nevertheless correctly fontified.

Disclaimer: Please use the package with caution. It is pretty new and there may be issues. Please report any issues you stumble over at the issue tracker.

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.