emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Paul Sexton <psexton.2a@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Smarter fill-paragraph behaviour in #+XXX: comments
Date: Thu, 19 Jul 2012 23:55:40 +0000 (UTC)	[thread overview]
Message-ID: <loom.20120720T012131-161@post.gmane.org> (raw)

I became annoyed at how, when pressing M-Q (fill-paragraph) while in a 
block like this:

#+CAPTION: This is a really long caption that I need
#+CAPTION: to describe my table in excruciating and unnecessary detail

Org takes no account of the #+ directives and simply smooshes all the 
lines together like this:


#+CAPTION: This is a really long caption that I need +CAPTION: to describe my
#table in excruciating and unnecessary detail


So I wrote the following function. I have it bound to M-q in org-mode.
Its behaviour:
1. It wraps a series of #+CAPTION lines as if the caption directives are
not there -- ie it turns the above example into:


#+CAPTION: This is a really long caption that I need to describe my table in
#+CAPTION: excruciating and unnecessary detail

2. It ignores other #+XXX: directives, ie no filling is performed.
3. In every other context it behaves as 'fill-paragraph'.

--------

(defun org-smart-wrap (&optional width)
  (interactive)
  (let ((lines nil)
        (width (or width (- fill-column 11)))
        (start nil) (end nil))
    (save-match-data
      (cond
       ((and (eql major-mode 'org-mode)
             (save-excursion
               (beginning-of-line)
               (looking-at "#\\+\\([A-Za-z0-9_]+\\):")))
        (cond
         ((not (string-equal (match-string 1) "CAPTION"))
          (message "`#+%s' directive found, fill command ignored."
                   (substring-no-properties (match-string 1))))
         (t
          ;; Wrap caption.
          (save-excursion
            (while (line-starts-with-p "#\\+CAPTION:")
              (forward-line -1))
            (unless (line-starts-with-p "#\\+CAPTION:")
              (forward-line))
            (setf start (point))
            (while (line-starts-with-p "#\\+CAPTION:")
              (beginning-of-line)
              (search-forward "#+CAPTION:" (end-of-line-pos) t)
              (push (org-trim
                     (buffer-substring-no-properties (point) (end-of-line-pos)))
                    lines)
              (forward-line))
            (beginning-of-line)
            (setf end (point))
            (setf lines (mapcar
                         (lambda (line) (concat "#+CAPTION: " line))
                         (org-wrap
                          (apply 'concat
                                 (mapcar (lambda (line) (concat line " "))
                                         (reverse lines)))
                          width)))
            (delete-region start end)
            (dolist (line lines)
              (insert line)
              (newline))))))
       (t
        (fill-paragraph))))))

             reply	other threads:[~2012-07-19 23:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-19 23:55 Paul Sexton [this message]
2012-07-20  7:11 ` Smarter fill-paragraph behaviour in #+XXX: comments XeCycle
2012-07-20  9:06 ` Nicolas Goaziou
2012-07-29  8:20 ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=loom.20120720T012131-161@post.gmane.org \
    --to=psexton.2a@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).