0
void parse(std::vector<float>& io_vertices, std::vector<unsigned>& io_indices) {}

Oftentimes when we code we need to break lines repeatedly as new expressions and statements enter the picture and our rows veer ever-closer to the column limit. We break function signatures, calls, etc., and line up arguments nicely:

void parse(std::vector<float>& io_vertices,
           std::vector<unsigned>& io_indices) {}

When it comes to plain text or comments, fill-paragraph is a lifesaver. Auto-formatting code, on the other hand, is notoriously difficult but not impossible. Surely someone must've made the effort in Emacs!

I've been (fruitlessly) trying to find a set of commands or a package that can do this for me, in particular for C and C++ like languages. Either attempts are rare or options so obvious that they elude me. This is the simplest solution to doing what I want:

(defun lazy-break-args ()
  (interactive)
  (let ((tokens (split-string (thing-at-point 'line t) ", ")))
    (unless (= (length tokens) 1)
      (let* ((breaks (mapcar (lambda (p) (format "%s,\n" p)) (butlast tokens)))
             (lines (append breaks (last tokens))))
        (move-beginning-of-line nil)
        (kill-line 1)
        (mapc (lambda (l) (insert l) (indent-according-to-mode)) lines)))))

Clearly this task depends on whatever language it is you're using, but my question is purposefully open to both language-specific packages (up to full-fledged formatters) and home-brew hacks like mine ("break and indent at commas, what the heck!").

Are there any functions in Emacs already, or any packages, that can do this? I don't expect perfection.

  • Please specify just what you mean by such a break-up. Presumably it's about breaking lines (or perhaps escaping newline chars), but what kind of breaking -- where? Otherwise, although you might get some helpful replies, the question is a bit too broad. Please say what you mean, to help others help you better. – Drew Aug 29 '17 at 3:45
  • Hey, @Drew, I added some more detail to make my question less vague, but really it's meant to be somewhat open-ended. I hope it's clear why! – Caterpillar Aug 29 '17 at 14:46
1

I've had good luck with multi-line

which, without configuration, allows you to cycle through different white space policies with a single repeated key press.

(use-package "multi-line" :bind ("C-;"))
  • There's a related question specifically for Python that's left unanswered. This package should help; I think you should drop an answer there too! – Caterpillar Sep 18 '17 at 21:13
1

CC mode provides for automatically filling text in comments and strings out-of-the-box. See the description in the manual: Filling and Breaking

By default, filling in CC mode ignores code. If you want, you can enable it by editing the value of c-ignore-auto-fill, as explained in the manual.

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.