emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [RFC] org-style
@ 2019-05-10 11:55 Marco Wahl
  2019-05-10 14:25 ` J. David Boyd
  2019-05-10 15:17 ` Gregor Zattler
  0 siblings, 2 replies; 4+ messages in thread
From: Marco Wahl @ 2019-05-10 11:55 UTC (permalink / raw)
  To: emacs-orgmode

Hi all,

For some time now I enforce a simple "style" for my org-mode files on
save.  Concretely the style is just assuring the number of blank lines
immediately before a heading and the number of blank lines after the
meta section, e.g. a property-drawer, in each subtree.

This unifies my org files and I don't change blank lines by hand very
often any more.

What do you think about this?  Is this worth to merge into org mode?


Ciao,  Marco


P.S.: I have the code below in my init.el.

#v+
(defcustom org-style-blanklines-num-before-heading 1
"Number of blank lines before each heading.")

(defcustom org-style-blanklines-num-after-meta 1
"Number of blank lines after the heading meta section.
If this happens to be a section before a heading the value of
`num-blanklines-before-heading' is taken.")

(defun org-style-blanklines-assure (num)
  "Assure exactly NUM blanklines and set point to the end of those.
The buffer remains unchanged when the blanklines are already there."
  (skip-chars-backward "\t \n")
  (let ((numplusone-newlines (make-string (1+ num) ?\n)))
    (if (looking-at (concat numplusone-newlines  "[^\n]"))
        (goto-char (1- (match-end 0)))
      (delete-region (point) (progn (skip-chars-forward "\t \n") (point)))
      (insert numplusone-newlines))))

(defun org-style-blanklines-at-headings ()
"Apply blank lines style.
This is the number of blank lines:

. before a heading`org-style-num-blanklines-before-heading' and

. after the org meta data `org-style-num-blanklines-after-meta'."
  (goto-char (point-min))
  (unless (org-at-heading-p)
    (outline-next-heading))
  (while (and (not (eobp)) (< (point) (point-max)))
    (cl-assert (org-at-heading-p) "programming logic error.  shoot a programmer, but not me")
    (org-style-blanklines-assure org-style-blanklines-num-before-heading)
    (org-style-blanklines-after-next-meta-data org-style-blanklines-num-after-meta)
    (unless (org-at-heading-p)
      (outline-next-heading))))

(defun org-style-blanklines-after-next-meta-data (num)
  "Assure exactly NUM blanklines after next meta data.
When end of meta data is a headline, then leave point there and
do nothing else."
  (org-end-of-meta-data t)
  (unless (org-at-heading-p)
    (org-style-blanklines-assure num)))

;; style the org-mode buffer before save.
(add-hook 'before-save-hook
          (lambda () (when (equal 'org-mode major-mode)
                  (save-excursion
                    (org-style-blanklines-at-headings)))))
#v-

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] org-style
  2019-05-10 11:55 [RFC] org-style Marco Wahl
@ 2019-05-10 14:25 ` J. David Boyd
  2019-05-10 15:17 ` Gregor Zattler
  1 sibling, 0 replies; 4+ messages in thread
From: J. David Boyd @ 2019-05-10 14:25 UTC (permalink / raw)
  To: emacs-orgmode

I like it, and I'll be using it.  I like a certain amount of whitespace in my
files, and your code works great for me. Thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] org-style
  2019-05-10 11:55 [RFC] org-style Marco Wahl
  2019-05-10 14:25 ` J. David Boyd
@ 2019-05-10 15:17 ` Gregor Zattler
  2019-05-13 22:41   ` Marco Wahl
  1 sibling, 1 reply; 4+ messages in thread
From: Gregor Zattler @ 2019-05-10 15:17 UTC (permalink / raw)
  To: emacs-orgmode

Hi Marco, org-mode developers and users,
* Marco Wahl <marcowahlsoft@gmail.com> [2019-05-10; 13:55]:

> What do you think about this?  Is this worth to merge into org mode?

This is nice, I played with it and see it's merit, but I think
this should not be enabled by default.

Even if the user had to enable it first via customization, this
version of the code has another drawback: if it actually touches
the buffer in order to change the number of blank lines it at the
same time removes the indentation of the first non blank line.
This might not be what the user wishes and should either not be
the case or also disabled per default:

Ciao; Gregor
--
 -... --- .-. . -.. ..--.. ...-.-

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] org-style
  2019-05-10 15:17 ` Gregor Zattler
@ 2019-05-13 22:41   ` Marco Wahl
  0 siblings, 0 replies; 4+ messages in thread
From: Marco Wahl @ 2019-05-13 22:41 UTC (permalink / raw)
  To: emacs-orgmode

Ciao Gregor,

>> What do you think about this?  Is this worth to merge into org mode?
>
> This is nice, I played with it and see it's merit, but I think
> this should not be enabled by default.
>
> Even if the user had to enable it first via customization, this
> version of the code has another drawback: if it actually touches
> the buffer in order to change the number of blank lines it at the
> same time removes the indentation of the first non blank line.
> This might not be what the user wishes and should either not be
> the case or also disabled per default:

Thanks for the feedback.

I fully agree to just offer this styling as an option.

The other issue, i.e. accidential deletion of leading whitespace and
thanks for pointing that out, can be solved by using the following code
AFAICS.

#v+
(defun org-style-blanklines-assure (num)
  "Assure exactly NUM blanklines and set point to the end of those.
The buffer remains unchanged when the blanklines are already
there and empty."
  (skip-chars-backward "\t \n")
  (let ((numplusone-newlines (make-string (1+ num) ?\n)))
    (if (looking-at (concat numplusone-newlines  "[^\n]"))
        (goto-char (1- (match-end 0)))
      (when (looking-at "\\(\\(\t\\| \\)*\n\\)+")
        (delete-region (match-beginning 0) (match-end 0)))
      (insert numplusone-newlines))))
#v-


Ciao,  Marco

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-05-13 22:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-05-10 11:55 [RFC] org-style Marco Wahl
2019-05-10 14:25 ` J. David Boyd
2019-05-10 15:17 ` Gregor Zattler
2019-05-13 22:41   ` Marco Wahl

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).