Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the definition of paragraph (e.g. in org-mode)

I'd like to use mark-paragraph (also for movement with forward/backward-paragraph) in org-mode buffers in the same way as in other major modes, i.e. to mark a continuous region delimited by empty lines. This should apply also to headings, list items, lines starting with '#' etc. – i.e. I'd like for the purposes of paragraph editing to everything be treated as regular text.

Is this possible?

like image 839
user673592 Avatar asked Mar 29 '12 10:03

user673592


2 Answers

You can try customising the paragraph-start variable. I'm not sure what would be appropriate here, org sets it to something quite elaborate as you see in the quoted docstring below. Setting it to the default might work, or you could try use-hard-newlines as mentioned in the quote below.

paragraph-start is a variable defined in `paragraphs.el'.

Its value is "\f\|[ ]$\|\+ \|[ ]#\|\([ ]\([-+]\|\(\([0-9]+\)[.)]\)\)\|[ ]+\*\)\([ ]+\|$\)\|[ ]*[:|]\|\$\$\|\\\(begin\|end\|[][]\)"

Original value was "\f\|[ ]*$"

Local in buffer coding.org; global value is "\f\|[ ]*$"

This variable is safe as a file local variable if its value satisfies the predicate `stringp'.

Documentation: Regexp for beginning of a line that starts OR separates paragraphs. This regexp should match lines that separate paragraphs and should also match lines that start a paragraph (and are part of that paragraph).

This is matched against the text at the left margin, which is not necessarily the beginning of the line, so it should never use "^" as an anchor. This ensures that the paragraph functions will work equally well within a region of text indented by a margin setting.

The variable `paragraph-separate' specifies how to distinguish lines that start paragraphs from lines that separate them.

If the variable `use-hard-newlines' is non-nil, then only lines following a hard newline are considered to match.

like image 37
suvayu Avatar answered Sep 30 '22 06:09

suvayu


See the variables paragraph-start and paragraph-separate, and possibly also the use-hard-newlines function which is related (but probably not actually relevant in this case).

(defun use-default-paragraph-delimiters ()
  (setq paragraph-start (default-value 'paragraph-start)
        paragraph-separate (default-value 'paragraph-separate)))

(add-hook 'org-mode-hook 'use-default-paragraph-delimiters)

Edit: Admittedly, org-mode might depend upon its paragraph definitions for more than just interactive marking and moving, so here is a more targeted approach for customising the paragraph definitions for those commands only when called interactively using their key bindings.

(defmacro with-default-paragraph-definition (&rest body)
  "Evaluate body forms using the default definition of a paragraph."
  `(let ((paragraph-start (default-value 'paragraph-start))
         (paragraph-separate (default-value 'paragraph-separate)))
     ,@body))

(defalias 'my-org-mark-paragraph 'mark-paragraph)
(defadvice my-org-mark-paragraph 
  (around my-org-mark-paragraph-advice activate)
  (with-default-paragraph-definition ad-do-it))

(defalias 'my-org-forward-paragraph 'forward-paragraph)
(defadvice my-org-forward-paragraph
  (around my-org-forward-paragraph-advice activate)
  (with-default-paragraph-definition ad-do-it))

(defalias 'my-org-backward-paragraph 'backward-paragraph)
(defadvice my-org-backward-paragraph
  (around my-org-backward-paragraph-advice activate)
  (with-default-paragraph-definition ad-do-it))

(defun my-org-paragraph-overrides ()
  "Use the default paragraph definitions in org-mode
        when marking or moving by paragraph."
  (local-set-key [remap mark-paragraph] 'my-org-mark-paragraph)
  (local-set-key [remap forward-paragraph] 'my-org-forward-paragraph)
  (local-set-key [remap backward-paragraph] 'my-org-backward-paragraph))

(add-hook 'org-mode-hook 'my-org-paragraph-overrides)
like image 139
phils Avatar answered Sep 30 '22 07:09

phils