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.

kuzzooroo
  • 301
  • 1
  • 7

1 Answers1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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 '15 at 00: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 '15 at 02: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. – Kaushal Modi Jun 27 '15 at 02:41