8

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.

Bite Bytes
  • 291
  • 1
  • 8
  • 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
  • @BiteBytes Last-resort : ugly approach : why not simply create temp01.txt (for example) as an empty file and then manually copy paste the entire buffer of your html file into temp01.txt? Won't the .txt extension keep the temp01.txt buffer in text mode, regardless of the copied html code? – zugzwang Oct 22 '18 at 15:42
  • @zugzwang I want see the changes dynamically in the cloned buffer as I type in the original buffer, what you've suggested is an isolated snapshot of the original buffer. – Bite Bytes Oct 22 '18 at 15:48
  • @BiteBytes good point. I was going to ask about manually forcing the mode in the indirect buffer (e.g. M-x text-mode), but I see from your question that you have already tried that. – zugzwang Oct 22 '18 at 17:04
  • 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
  • @Tobias I tought about it, but unfortunately, it needs saving before the cloned buffer get updated. What actually bothers me, is what's the point of this "cloned buffer" feature, if it can't be used for this particular reason, in a straightforward manner. – Bite Bytes Oct 23 '18 at 11:12
  • @BiteBytes, I haven't yet seen a single good use of indirect buffers, mainly due to the font-lock problem. I would write it off as one of those ideas that seemed good but didn't work. – Lindydancer Jun 10 '20 at 18:21
  • I use indirect buffers in conjunction with narrowing all the time, and it's great. I can easily have a variety of differently-narrowed views on the same buffer simultaneously. I rarely ever want to change the mode or faces in any of them. – phils Jun 11 '20 at 05:53

1 Answers1

4

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.

Tobias
  • 33,167
  • 1
  • 37
  • 77