* Smarter fill-paragraph behaviour in #+XXX: comments
@ 2012-07-19 23:55 Paul Sexton
2012-07-20 7:11 ` XeCycle
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Paul Sexton @ 2012-07-19 23:55 UTC (permalink / raw)
To: emacs-orgmode
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))))))
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Smarter fill-paragraph behaviour in #+XXX: comments
2012-07-19 23:55 Smarter fill-paragraph behaviour in #+XXX: comments Paul Sexton
@ 2012-07-20 7:11 ` XeCycle
2012-07-20 9:06 ` Nicolas Goaziou
2012-07-29 8:20 ` Bastien
2 siblings, 0 replies; 4+ messages in thread
From: XeCycle @ 2012-07-20 7:11 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 880 bytes --]
Paul Sexton <psexton.2a@gmail.com> writes:
> 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.
Instead of doing these, you can try setting adaptive-fill-regexp
and adaptive-fill-first-line-regexp.
--
Carl Lei (XeCycle)
Department of Physics, Shanghai Jiao Tong University
OpenPGP public key: 7795E591
Fingerprint: 1FB6 7F1F D45D F681 C845 27F7 8D71 8EC4 7795 E591
[-- Attachment #2: Type: application/pgp-signature, Size: 489 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Smarter fill-paragraph behaviour in #+XXX: comments
2012-07-19 23:55 Smarter fill-paragraph behaviour in #+XXX: comments Paul Sexton
2012-07-20 7:11 ` XeCycle
@ 2012-07-20 9:06 ` Nicolas Goaziou
2012-07-29 8:20 ` Bastien
2 siblings, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2012-07-20 9:06 UTC (permalink / raw)
To: Paul Sexton; +Cc: emacs-orgmode
Hello,
Paul Sexton <psexton.2a@gmail.com> writes:
> 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
Provided you have contrib directory in your load-path, you may be
interested in `org-element-fill-paragraph' function (you will need to
(require 'org-element) first).
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Smarter fill-paragraph behaviour in #+XXX: comments
2012-07-19 23:55 Smarter fill-paragraph behaviour in #+XXX: comments Paul Sexton
2012-07-20 7:11 ` XeCycle
2012-07-20 9:06 ` Nicolas Goaziou
@ 2012-07-29 8:20 ` Bastien
2 siblings, 0 replies; 4+ messages in thread
From: Bastien @ 2012-07-29 8:20 UTC (permalink / raw)
To: Paul Sexton; +Cc: emacs-orgmode
Hi Paul,
Paul Sexton <psexton.2a@gmail.com> writes:
> 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
FWIW, and thanks to the recent integration of org-element.el into core,
this is now fixed in the git repo. Please pull and test if you can.
--
Bastien
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-07-29 8:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-19 23:55 Smarter fill-paragraph behaviour in #+XXX: comments Paul Sexton
2012-07-20 7:11 ` XeCycle
2012-07-20 9:06 ` Nicolas Goaziou
2012-07-29 8:20 ` Bastien
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).