1

By default, auto-fill will only break a line at a whitespace character. Usually this is what you want. However, I'm writing a major mode for editing data files (DNA sequences) that contain long lines of text with no spaces, and I want them to auto-fill at exactly 80 columns. How can I achieve this?

i.e., I want to use auto-fill (or similar) to fill this:

ACCATCGAAACTTTGAACGCAAGTTGCGCCTCAAGCCATTAGGTTGAGGGCACGTTTGCTTGGGTGTCATTCGTTATGTC

to this (using 20-character lines instead of 80 for a shorter example):

ACCATCGAAACTTTGAACGC
AAGTTGCGCCTCAAGCCATT
AGGTTGAGGGCACGTTTGCT
TGGGTGTCATTCGTTATGTC
  • Is the purpose of this for easy reading only? If so, you can size the buffer to any convenient width and do M-x toggle-truncate-lines. Having (setq-default truncate-partial-width-windows nil) in your config would help if you want the line wrapping to work in frames with split windows. – Kaushal Modi Feb 2 '16 at 19:40
  • @KaushalModi no, I want to be able to re-wrap the sequences after changing them. For instance, I may need to delete the first few letters from a row, and then re-fill what's left into 80-column lines. – Tyler Feb 2 '16 at 20:01
  • Toggling truncation will give you the same effect. That's why I asked if it is important for you to see the wrapping just visually or is it also important to save the newlines to the file as well. – Kaushal Modi Feb 2 '16 at 20:18
  • @KaushalModi oh, I've been sloppy with my language. When I said 're-wrap' (i.e., temporary visual change) I meant 're-fill' (i.e., add and remove newlines as necessary for a permanent change). – Tyler Feb 2 '16 at 20:31
3

You could modify the category of characters G, C, A, and T to make them line-breakable:

(defun break-at-gcat ()
  "Make characters G, C, A, and T line-breakable to simplify
wrapping DNA sequence strings."
  (dolist (char '(?G ?C ?A ?T))
    (modify-category-entry char ?|)))

Now if I evaluate (break-at-gcat), set fill-column to 20,

(setq-local fill-column 20)

put the cursor on this line,

ACCATCGAAACTTTGAACGCAAGTTGCGCCTCAAGCCATTAGGTTGAGGGCACGTTTGCTTGGGTGTCATTCGTTATGTC

and press M-q (fill-paragraph), I get this:

ACCATCGAAACTTTGAACGC
AAGTTGCGCCTCAAGCCATT
AGGTTGAGGGCACGTTTGCT
TGGGTGTCATTCGTTATGTC

To allow re-wrapping without inserting spaces we can use

(setq-local sentence-end "[GCAT]")

This tricks canonically-space-region in fill.el into thinking that characters G, C, A, and T match the end a sentence (including the break we want) so it removes any extra spaces.

Note that both modifying character categories and the sentence-end regexp will mess with wrapping English text in the same buffer!

  • 1
    Neat, but once the string is splitted in lines, changing fill-column and using M-q again inserts white space inside the text. Can this be avoided? – JeanPierre Feb 4 '16 at 9:41
  • Thanks Constantine. I hadn't come across character categories before. I found the same problem mentioned by @JeanPierre. If there isn't a way to correct that, it shouldn't be hard to make a custom function to clean up the extra spaces. – Tyler Feb 4 '16 at 14:30
  • @Tyler: Please see the updated answer. – Constantine Feb 4 '16 at 18:16
  • @JeanPierre: Yes, I saw this too. I think setting sentence-end takes care of it, though -- please see the updated answer. – Constantine Feb 4 '16 at 18:17
  • Great, thanks! These files won't have any English text, so this won't cause any problems. – Tyler Feb 4 '16 at 22:16

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.