I have a docstring to a simple function, but the docstring uses many escape sequences to explain the reasoning for the function.
(defun sx-encoding-normalize-line-endings (string)
"Normalize the line endings for STRING.
The API returns strings that use Windows-style line endings.
These are largely useless in an Emacs environment. Windows uses
\"\\r\\n\", Unix uses just \"\\n\". Deleting \"\\r\" is
sufficient for conversion."
(delete ?\r string))
The docstring evaluates to
Normalize the line endings for STRING.
The API returns strings that use Windows-style line endings.
These are largely useless in an Emacs environment. Windows uses
"\r\n", Unix uses just "\n". Deleting "\r" is
sufficient for conversion.
Note that the next-to-last line is needlessly short; 'sufficient' can be put onto the preceding line. Even 'for' fits:
The API returns strings that use Windows-style line endings.
These are largely useless in an Emacs environment. Windows uses
"\r\n", Unix uses just "\n". Deleting "\r" is sufficient for
conversion.
How can I reach Emacs to fill according to the value of the string rather than its input syntax?
I have a solution sketch for this, but I'm having trouble implementing it.
Check to see if we're inside a string literal:
(unless (nth 3 (syntax-ppss)) (user-error "Not inside a string literal"))
Evaluate the string.
re-search-forward
for[^\\]"
(string version:"[^\\\\]\""
). Useeval-last-sexp
and insert the result into a temporary buffer.Record line endings. Push onto a list every
point
where char-at-point is a new line. Start atpoint-max
to keep the list sorted.Returning to the original string, insert the line breaks where they're needed and remove them where they do not align with the list created.
Alternatively, just evaluate the string into a temp buffer, fill it, replace instances of "
with \"
, and bring that string back in to replace the old one.