7

I'm trying to figure out a way to change the behavior of org-fill-paragraph for a list with checkboxes. I would like the indentation to take into account the checkbox, but here is what I see:

- This is a long line in a plain list and fill-paragraph works as I expect
  it would (it takes into account the marker for the list in the indentation) 

- [ ] But when I write a long line with a checkboxed list, the behavior
  of fill-paragraph is to ignore the checkbox and indent as if the checkbox
  is not there

- [ ] This is the way I would like to see a long line in a checkboxed
      list. Currently, the only way I can achieve it is by manually adding 
      spaces in the beginning of the line

I realize that some people may prefer the current behavior, but is there a way for me to achieve my desired behavior?

Thanks

0

The key is to play with adaptive-fill-function which should return a fill-prefix, a string which prefix newlines of the filled item. It will work because the call chain is org-fill-paragraph > org-fill-element > fill-region-as-paragraph > fill-context-prefix.

If you C-h f fill-context-prefix, it says

Compute a fill prefix from the text between FROM and TO. This uses the variables ‘adaptive-fill-regexp’ and ‘adaptive-fill-function’ and ‘adaptive-fill-first-line-regexp’. ‘paragraph-start’ also plays a role; we reject a prefix based on a one-line paragraph if that prefix would act as a paragraph-separator.

and C-h v adaptive-fill-function says

Function to call to choose a fill prefix for a paragraph. A nil return value means the function has not determined the fill prefix.

First, define the function below. Its job is returning a string of spaces whose the length is the one of - [ ] if your are in a item, otherwise nil.

(defun my/item-cb-fill-function ()
  (save-excursion
    (when-let* ((beg (org-in-item-p))
        (beg-line-end (save-excursion (goto-char beg) (line-end-position)))
        (line (buffer-substring-no-properties beg beg-line-end))
        (match (string-match "\\([[:space:]]*-[[:space:]]*[][ ]+\\).*" line)))
      (make-string (length (match-string 1 line)) ? ))))

Now you can define your custom org-fill-paragraph :

(defun my/org-fill-paragraph (&optional justify region)
  (interactive)
  (let ((adaptive-fill-function #'my/item-cb-fill-function))
    (org-fill-paragraph justify region)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.