From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paul Sexton Subject: Smarter fill-paragraph behaviour in #+XXX: comments Date: Thu, 19 Jul 2012 23:55:40 +0000 (UTC) Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([208.118.235.92]:58147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ss0Zi-0008Rr-To for emacs-orgmode@gnu.org; Thu, 19 Jul 2012 19:56:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ss0Zh-0002we-S2 for emacs-orgmode@gnu.org; Thu, 19 Jul 2012 19:56:06 -0400 Received: from plane.gmane.org ([80.91.229.3]:34656) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ss0Zh-0002wQ-L5 for emacs-orgmode@gnu.org; Thu, 19 Jul 2012 19:56:05 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Ss0ZV-0006o4-Sm for emacs-orgmode@gnu.org; Fri, 20 Jul 2012 01:55:58 +0200 Received: from rp.young.med.auckland.ac.nz ([130.216.140.20]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 20 Jul 2012 01:55:53 +0200 Received: from psexton.2a by rp.young.med.auckland.ac.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 20 Jul 2012 01:55:53 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org 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))))))