5

I have Emacs doing a bunch of smart things to display text for me. In particular, some text has the invisible property set, and I'm running adaptive-wrap with <!-- adaptive-wrap-extra-indent: 3 --> set. I'm wondering whether I can copy the text as it's rendered in Emacs, with newlines to re-create the smart line wrapping and with the invisible text gone. Is there a way I can access the text from my buffer in that form?

What I want is essentially the same as running emacs -nw, selecting the text using the terminal's text selection capabilities, and then using the terminal app's copy function (not marking using an Emacs region and then yanking). The chief problem with this technique for me is that I have to do it one screen at a time.

Running htmlfontify-buffer gets me part of the way there. My visible text is shown by I can make it go away by clicking on each individual instance, but the line-wrapping behavior is lost.

1 Answer 1

2

Library subr+.el has functions that help with this. This one, for instance:

(defun buffer-substring-of-visible (start end)
  "Return contents of visible part of buffer from START to END, as a string.
START and END can be in either order."
  (buffer-substring-of-unpropertied start end 'invisible))

Here are some others, which are similar:

  • buffer-substring-of-invisible - Return contents of invisible part of buffer from START to END, as a string.

  • buffer-substring-of-faced - eturn faced contents of buffer from START to END, as a string. That is, include only text that has property `face'.

  • buffer-substring-of-unfaced - Return unfaced contents of buffer from START to END, as a string. That is, include only text that has no `face' property.

And the more general functions:

  • buffer-substring-of-propertied - Return PROPERTY'ed contents of buffer from START to END, as a string. Only text from START to END that has PROPERTY is included.

  • buffer-substring-of-unpropertied - Return unpropertied contents of buffer from START to END, as a string. Text from START to END that has PROPERTY is excluded from the string.

3
  • Nice. I'm able to throw away the invisible text with the buffer-substring-of-visible function you highlighted. I believe this library won't handle the line wrapping/hanging indent. Do I have that right?
    – kuzzooroo
    Jun 27, 2015 at 0:03
  • Dunno; I don't know anything about adaptive-wrap, and don't find it in the Emacs manuals. If it uses text properties to simulate newline chars then you can use subr+.el to ignore those properties, but nothing there helps you replace them with newline chars. (You might want to provide a reference/link to the code that defines adaptive-wrap, so that others can help you with that.)
    – Drew
    Jun 27, 2015 at 2:15
  • Updated the original question with link to adaptive-wrap. On a side note, one must at least try it out if they haven't. Wrapped lines have never looked better. Jun 27, 2015 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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