0

I want to write function that will create new empty buffer, populates it with current term buffer contents, preserving point and window-start positions, and switch to it right away. Basically I want to make my own term-line-mode. Function below do it all great, with only problem that its not preserving coloring of term buffer:

(defun sandric/duplicate-current-term-buffer ()
  (interactive)
  (let ((duplicate-buffer (get-buffer-create "NEWBUF"))
        (term-point (point))
        (term-window-start (window-start))
        (term-overlay (make-overlay (point-min) (point-max))))
    (copy-to-buffer duplicate-buffer (point-min) (point-max))
    (switch-to-buffer duplicate-buffer)
    (set-window-start (selected-window) term-window-start)
    (goto-char term-point)
    (move-overlay term-overlay (point-min) (point-max) duplicate-buffer)))

I know nothing about coloring in emacs, and how its actually works, as I understand, it is done via overlays? So in that code I tried to get current term buffer overlay and copy it to newly created duplicate buffer. But I see no color. Can anyone explain how should I tackle this?

1
  • Are you not overthinking this? Remember the current buffer, point, start etc, create the new buffer, switch to it, delete everything in it in case it already existed, insert the remembered buffer, set the window start and point. – icarus Nov 5 '16 at 2:58
2
(defun sandric/duplicate-current-term-buffer ()
  (interactive)
  (let ((duplicate-buffer (get-buffer-create "NEWBUF"))
        (term-point (point))
        (term-window-start (window-start))
        (term-window (current-buffer)))
    (switch-to-buffer duplicate-buffer)
    (delete-region (point-min) (point-max))
    (insert-buffer term-window)
    (set-window-start (selected-window) term-window-start)
    (goto-char term-point)
    (fundamental-mode)))

edit: Add call to fundamental-mode

5
  • It makes no difference, no color. – sandric Nov 5 '16 at 3:27
  • Works for me if I set the mode of NEWBUF to lisp-interaction-mode. Maybe something to do with fundamental mode? Do you see color if you switch to \*scratch\* and M-x insert-buffer \*terminal\* ? – icarus Nov 5 '16 at 4:07
  • Even switching to fundamental-mode gets me color. Answer edited to do this. – icarus Nov 5 '16 at 4:16
  • Yes, last change made the trick, thanks a lot. although I havent really understood what was the deal with color. Insert-buffer copy with overlay? – sandric Nov 5 '16 at 16:42
  • Insert-buffer copies everything. I need to figure out why setting the mode on the buffer makes it work. – icarus Nov 5 '16 at 17:50

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.