From: Matthew Dempsky <matthew@dempsky.org>
To: emacs-orgmode@gnu.org
Subject: Smart(er) word wrapping with org-mode and visual-line-mode
Date: Thu, 3 Dec 2009 02:14:23 -0800 [thread overview]
Message-ID: <d791b8790912030214y3be354ap148836695cc0d474@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 895 bytes --]
While playing around with the different options for handling word
wrapping in org-mode, I decided to instead hack an extension that
improves visual-line-mode's word wrapping when used in conjunction
with org-mode. In particular, the mrd-org-smartwrap minor mode
automatically sets the `wrap-prefix' property on all section headlines
and plain list entries so that visual-line-mode's native word wrapping
looks identical to what careful and continual usage of TAB and M-q
should produce, without any of the quirks of using auto-fill-mode or
refill-mode.
Just thought I'd share in case anyone else found it useful / interesting.
Caveats: It's incompatible with org-indent-mode, doesn't do anything
to support org-adapt-indentation, and results in tables being word
wrapped. I've also noticed linum-mode causes it to go crazy if the
top visible line is the continuation of a soft wrapped line.
[-- Attachment #2: mrd-org-smartwrap.el --]
[-- Type: application/octet-stream, Size: 5457 bytes --]
;;; mrd-org-smartwrap.el --- Smart word-wrapping for org-mode
;; version 20091202
;; Matthew Dempsky
;;
;; Derived from org-indent.el:
;;
;; Copyright (C) 2009 Free Software Foundation, Inc.
;;
;; Author: Carsten Dominik <carsten at orgmode dot org>
;; Keywords: outlines, hypermedia, calendar, wp
;; Homepage: http://orgmode.org
;; Version: 6.33x
;;
;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;; XXX: Somehow re-enable truncate-lines for lines containing tables.
(require 'org-macs)
(require 'org-compat)
(require 'org)
(eval-when-compile
(require 'cl))
(defconst mrd-org-smartwrap-max 40
"Maximum indentation level")
(defvar mrd-org-smartwrap-strings nil
"Vector with all prefix strings.
It will be set in `mrd-org-smartwrap-initialize'.")
(defun mrd-org-smartwrap-initialize ()
"Initialize the prefix strings."
(unless mrd-org-smartwrap-strings
(setq mrd-org-smartwrap-strings
(make-vector (1+ mrd-org-smartwrap-max) nil))
(loop for i from 1 to mrd-org-smartwrap-max do
(aset mrd-org-smartwrap-strings i (make-string i ?\ )))))
;;;###autoload
(define-minor-mode mrd-org-smartwrap-mode
"When active, wrap text according to outline and list structure.
This mode is intended to be used with `visual-line-mode'.
Internally this works by adding `wrap-prefix' properties on
section headlines and plain list entries."
nil " sw" nil
(if (org-bound-and-true-p org-inhibit-startup)
(setq mrd-org-smartwrap-mode nil)
(if mrd-org-smartwrap-mode
(progn
(mrd-org-smartwrap-initialize)
(make-local-variable 'buffer-substring-filters)
(add-hook 'buffer-substring-filters
'mrd-org-smartwrap-remove-properties-from-string nil t)
(add-hook 'after-change-functions
'mrd-org-smartwrap-after-change nil t)
(save-restriction
(widen)
(mrd-org-smartwrap-add-properties (point-min) (point-max))))
(save-restriction
(widen)
(mrd-org-smartwrap-remove-properties (point-min) (point-max)))
(remove-hook 'buffer-substring-filters
'mrd-org-smartwrap-remove-properties-from-string t)
(remove-hook 'after-change-functions
'mrd-org-smartwrap-after-change t))))
;; The regexps here are based on org-outline-regexp and the regexps
;; from org-adaptive-fill-function.
(defconst mrd-org-smartwrap-outline-re
"^\\(\\*+ \\|[ \t]*\\([-*+] \\|[0-9]+[.)] \\)\\)"
"Outline or plain list regexp.")
(defun mrd-org-smartwrap-add-properties (beg end)
"Add `wrap-prefix' properties between BEG and END.
Assumes that BEG is at the beginning of a line."
(org-unmodified
(save-excursion
(goto-char beg)
;; XXX: Use the BOUND argument to RE-SEARCH-FORWARD?
(while (and (re-search-forward mrd-org-smartwrap-outline-re nil t)
(<= (point) end))
(beginning-of-line)
;; XXX: For definitions with terms that span multiple lines,
;; the result is somewhat peculiar; namely, the rest of the
;; term is indented 5 spaces as well. It might make sense to
;; indent all of the term (up and including the " :: ") just
;; past the bullet point, and then only indent the rest of the
;; string any further. Not sure.
;; XXX: Doesn't support org-description-max-indent being
;; ridiculously large... probably doesn't need to.
;; XXX: Doesn't support numbered definition lists: this matches
;; M-q's behavior, but not the syntax highlighting...
(let ((level (if (and (looking-at "[ \t]*\\([-*+] .*? :: \\)")
(> (- (match-end 1) (match-beginning 1))
org-description-max-indent))
(- (+ (match-beginning 1) 5) (match-beginning 0))
(- (match-end 0) (match-beginning 0)))))
(set-text-properties
(point-at-bol) (point-at-eol)
`(wrap-prefix ,(aref mrd-org-smartwrap-strings level))))
(end-of-line)))))
(defun mrd-org-smartwrap-remove-properties (beg end)
"Remove `wrap-prefix' properties between BEG and END."
(org-unmodified
(remove-text-properties beg end '(wrap-prefix nil))))
(defun mrd-org-smartwrap-remove-properties-from-string (string)
"Remove `wrap-prefix' properties between BEG and END."
(remove-text-properties 0 (length string)
'(wrap-prefix nil) string)
string)
(defun mrd-org-smartwrap-refresh-region (beg end)
"Refresh `wrap-prefix' properties between BEG and END.
Assumes that BEG is at the beginning of a line."
(mrd-org-smartwrap-remove-properties beg end)
(mrd-org-smartwrap-add-properties beg end))
(defun mrd-org-smartwrap-after-change (beg end len)
"Refresh `wrap-prefix' properties between BEG and END."
(mrd-org-smartwrap-refresh-region
(save-excursion (goto-char beg) (point-at-bol))
(save-excursion (goto-char end) (point-at-eol))))
(provide 'mrd-org-smartwrap)
;;; mrd-org-smartwrap.el ends here
[-- Attachment #3: Type: text/plain, Size: 201 bytes --]
_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode
next reply other threads:[~2009-12-03 10:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-12-03 10:14 Matthew Dempsky [this message]
2009-12-03 14:23 ` Smart(er) word wrapping with org-mode and visual-line-mode Carsten Dominik
2009-12-03 19:10 ` Matthew Dempsky
2010-01-06 15:25 ` Martin Pohlack
2010-01-08 17:24 ` Carsten Dominik
2010-01-10 19:09 ` Martin Pohlack
2010-01-10 21:07 ` Carsten Dominik
2010-01-15 7:25 ` Carsten Dominik
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d791b8790912030214y3be354ap148836695cc0d474@mail.gmail.com \
--to=matthew@dempsky.org \
--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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.