branch: externals/org-modern commit 72ff0b6dcd2efcf424dc5c6936bed959eacc7015 Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
Rename variable to org-modern-block-indent to be more in line with the others --- README.org | 20 ++-- org-modern-indent.el | 285 +++++++++++++++++++++++++-------------------------- org-modern.el | 12 +-- 3 files changed, 158 insertions(+), 159 deletions(-) diff --git a/README.org b/README.org index 1c87a05c18..7be2112115 100644 --- a/README.org +++ b/README.org @@ -39,9 +39,9 @@ Since version 2.0, this package also incorporates ~org-modern-indent~, which pro #+html: <img width="716" alt="image" src="https://github.com/user-attachments/assets/7ca42ce7-dcfb-4c66-b5f4-1798a4fd4df5" /> -~org-modern-indent~ is enabled by ~org-modern~ by default if you use ~org-indent~; configure ~org-modern-indent-block=nil~ to disable this behavior. To activate ~org-indent-mode~ in all org files, set ~org-startup-indented=t~. +~org-modern-indent~ is enabled by ~org-modern~ by default if you use ~org-indent~; configure ~org-modern-block-indent=nil~ to disable this behavior. To activate ~org-indent-mode~ in all org files, set ~org-startup-indented=t~. -*Note*: Non-nil ~line-spacing~ is not recommended with ~org-modern-indent~, as it leads to gaps in the vertical bars drawn to indicate blocks. +*Note*: Non-nil ~line-spacing~ is not recommended with ~org-modern-indent~, as it leads to gaps in the vertical bars drawn to indicate blocks. * Configuration @@ -148,27 +148,27 @@ Bulk-indented blocks have "real" (space/tab) indentation applied and managed by As an important principle, ~org-modern-indent~ does not alter the contents of the text in your org documents, not even indentation. It just styles what is there. To help achieve proper block bulk-indented alignment, here are a few ways to alter blocks indentation using org and other commands: - **Start things right**: Hit return after your last line of text (e.g in a list item), then immediately hit =C-c C,= to create the desired block. It will be indented at the right level: - + #+begin_src org - This list item contains a: - sublist, which holds a block: [C-c C-,] here #+end_src - + - *Move flush left*: Note: =M-{= will get you to the start of a block quickly. =M-\= at block start will move the block's first header line to column 0. Then =M-S-left= (or =right=) will indent the full block. - *Indent rigidly*: =M-h= selects the entire block. Then =C-x TAB= enters "rigid indent" mode, after which left/right moves the entire block. - *Re-indent a block*: If you have a block that is partially aligned, perhaps with a "hanging end", like so: - + #+begin_src org - + - List 1 - List 2 ,#+begin_src lang foo_lang(x) ,#+end_src #+end_src - - you can simply use =M-S-left/right= at block start (or in fact anywhere on the block header/footer) to ~org-indent-block~. Note that ~org-src-preserve-indentation=nil~ is an important setting, to allow org to (re-)indent blocks to respect the local indentation inside list and other elements. Also note that (from ~org-indent-region~): + + you can simply use =M-S-left/right= at block start (or in fact anywhere on the block header/footer) to ~org-indent-block~. Note that ~org-src-preserve-indentation=nil~ is an important setting, to allow org to (re-)indent blocks to respect the local indentation inside list and other elements. Also note that (from ~org-indent-region~): #+begin_quote The function will not indent contents of example blocks, verse blocks and export blocks as leading white spaces are assumed to be significant there. @@ -176,13 +176,13 @@ As an important principle, ~org-modern-indent~ does not alter the contents of th ** Font spacing and faces -The default ~fixed-pitch~ font (from which ~org-meta-line~ inherits) has line spacing >1.0 on some systems. This will introduce gaps _even if your default font is changed_, and ~line-space~ is nil. To correct it, add: +The default ~fixed-pitch~ font (from which ~org-meta-line~ inherits) has line spacing >1.0 on some systems. This will introduce gaps _even if your default font is changed_, and ~line-space~ is nil. To correct it, add: #+begin_src emacs-lisp (set-face-attribute 'fixed-pitch nil :family "Hack" :height 1.0) ; or whatever font family #+end_src -*** The bracket style +*** The bracket style If you'd like a different face than ~org-meta-line~ for the "bracket", configure the ~org-modern-indent-bracket-line~ face. diff --git a/org-modern-indent.el b/org-modern-indent.el index 0f70513852..63a1cd62dd 100644 --- a/org-modern-indent.el +++ b/org-modern-indent.el @@ -77,30 +77,30 @@ face), and EXTRA-INDENT (unstyled). See `org-modern-indent--prefix' for the contents." (unless (and (= org-indent 0) (= extra-indent 0)) (let* ((key (+ org-indent (* 1000 extra-indent))) - (cached (gethash key omi/-prefixes))) + (cached (gethash key omi/-prefixes))) (or cached - (puthash key - (cl-loop for type in '("begin" "guide" "end") - for tstr = (symbol-value - (intern (concat "org-modern-indent-" type))) - with olen = (if (> extra-indent 0) org-indent - (1- org-indent)) - with ostr = (and (> olen 0) - (propertize (make-string olen ?\s) - 'face '(org-indent default))) - with pstr = (and (> extra-indent 0) - (make-string (1- extra-indent) ?\s)) - collect (concat ostr pstr tstr)) - omi/-prefixes))))) + (puthash key + (cl-loop for type in '("begin" "guide" "end") + for tstr = (symbol-value + (intern (concat "org-modern-indent-" type))) + with olen = (if (> extra-indent 0) org-indent + (1- org-indent)) + with ostr = (and (> olen 0) + (propertize (make-string olen ?\s) + 'face '(org-indent default))) + with pstr = (and (> extra-indent 0) + (make-string (1- extra-indent) ?\s)) + collect (concat ostr pstr tstr)) + omi/-prefixes))))) ;;;; Finding and operating on blocks (defsubst omi/-block-p (el) "Return non-nil if EL is an org block element." (when-let ((el) (type (car el))) (memq type - '(center-block comment-block dynamic-block - example-block export-block quote-block - special-block src-block verse-block)))) + '(center-block comment-block dynamic-block + example-block export-block quote-block + special-block src-block verse-block)))) (defun omi/-block-at-point (&optional pos) "Return the org block element at POS, if any. @@ -109,20 +109,20 @@ affiliated content like title as part of the block." (let ((pos (or pos (point)))) (when-let ((element (org-element-at-point pos))) (when (or (omi/-block-p element) - (omi/-block-p - (setq element (org-element-property :parent element)))) - (unless (< pos (org-element-property :post-affiliated element)) - element))))) + (omi/-block-p + (setq element (org-element-property :parent element)))) + (unless (< pos (org-element-property :post-affiliated element)) + element))))) (defsubst omi/-block-beg-end (node) "Return a cons of NODE's (BEG . END). BEG is at the beginning of the #+BEGIN line, and END is at the end of the #+END line." (let* ((bbeg (org-element-property :post-affiliated node)) ; on #+begin - (bend (save-excursion - (goto-char (org-element-property :end node)) - (skip-chars-backward "\n\t ") - (point)))) + (bend (save-excursion + (goto-char (org-element-property :end node)) + (skip-chars-backward "\n\t ") + (point)))) (cons bbeg bend))) (defun omi/-walk-blocks (beg end fun) @@ -137,14 +137,14 @@ partially overlaps the region." (let ((node (omi/-block-at-point)) finished) (while (not finished) (when node - (cl-destructuring-bind (bbeg . bend) (omi/-block-beg-end node) - (funcall fun bbeg bend) - (goto-char bend) - (when (>= bend end) (setq finished t)))) ; last block + (cl-destructuring-bind (bbeg . bend) (omi/-block-beg-end node) + (funcall fun bbeg bend) + (goto-char bend) + (when (>= bend end) (setq finished t)))) ; last block (unless finished - (if (re-search-forward omi/begin-re end t) - (setq node (omi/-block-at-point)) - (setq finished t))))))) + (if (re-search-forward omi/begin-re end t) + (setq node (omi/-block-at-point)) + (setq finished t))))))) ;;;; Drawing block brackets (defun omi/-draw-block (beg end beg0 end0 org-indent real-indent) @@ -156,40 +156,40 @@ and REAL-INDENT is the amount of \"real\" (hard space) block indentation. REAL-INDENT may be zero." (with-silent-modifications (pcase-let* (((seq beg-prefix guide-prefix end-prefix) - (omi/-prefix org-indent real-indent)) - ((seq beg-display guide-display end-display) - (and (> real-indent 0) (omi/-prefix real-indent 0))) - (beg-bol (omi/-lbp beg)) - (body-start (max (omi/-lbp beg0 2) beg-bol))) + (omi/-prefix org-indent real-indent)) + ((seq beg-display guide-display end-display) + (and (> real-indent 0) (omi/-prefix real-indent 0))) + (beg-bol (omi/-lbp beg)) + (body-start (max (omi/-lbp beg0 2) beg-bol))) (cl-macrolet ((add-prefixes (pbeg pend line-prefix wrap-prefix) - `(add-text-properties ,pbeg ,pend - `(,@(and (= real-indent 0) `(line-prefix ,,line-prefix)) - wrap-prefix ,,wrap-prefix))) - (add-guides (pbeg pend display-str) - `(add-text-properties ,pbeg ,pend - `( omi/display ,,display-str rear-nonsticky t)))) - (when (> body-start beg) ;; BEGIN - (add-prefixes beg-bol body-start beg-prefix guide-prefix) - (when (> real-indent 0) - (add-guides beg-bol (+ beg-bol real-indent) beg-display))) - (when (> end body-start) ;; GUIDE BODY - (let* ((end-bol (omi/-lbp end)) - (end0-bol (omi/-lbp end0)) - (after-end (omi/-lbp end 2)) - (body-final (if (= end-bol end0-bol) end-bol after-end))) - (add-prefixes body-start body-final guide-prefix guide-prefix) - (when (> real-indent 0) - (goto-char body-start) - (while (< (point) body-final) - (when (>= (current-indentation) real-indent) - (add-guides (point) (+ (point) real-indent) guide-display)) - (forward-line))) - (when (= end-bol end0-bol) ;; END - (if (= real-indent 0) - (add-prefixes end-bol after-end end-prefix end-prefix) - (goto-char end-bol) - (when (>= (current-indentation) real-indent) - (add-guides end-bol (+ end-bol real-indent) end-display)))))))))) + `(add-text-properties ,pbeg ,pend + `(,@(and (= real-indent 0) `(line-prefix ,,line-prefix)) + wrap-prefix ,,wrap-prefix))) + (add-guides (pbeg pend display-str) + `(add-text-properties ,pbeg ,pend + `( omi/display ,,display-str rear-nonsticky t)))) + (when (> body-start beg) ;; BEGIN + (add-prefixes beg-bol body-start beg-prefix guide-prefix) + (when (> real-indent 0) + (add-guides beg-bol (+ beg-bol real-indent) beg-display))) + (when (> end body-start) ;; GUIDE BODY + (let* ((end-bol (omi/-lbp end)) + (end0-bol (omi/-lbp end0)) + (after-end (omi/-lbp end 2)) + (body-final (if (= end-bol end0-bol) end-bol after-end))) + (add-prefixes body-start body-final guide-prefix guide-prefix) + (when (> real-indent 0) + (goto-char body-start) + (while (< (point) body-final) + (when (>= (current-indentation) real-indent) + (add-guides (point) (+ (point) real-indent) guide-display)) + (forward-line))) + (when (= end-bol end0-bol) ;; END + (if (= real-indent 0) + (add-prefixes end-bol after-end end-prefix end-prefix) + (goto-char end-bol) + (when (>= (current-indentation) real-indent) + (add-guides end-bol (+ end-bol real-indent) end-display)))))))))) ;;;; org-indent interfacing (defvar-local omi/-level-change-end nil) @@ -200,9 +200,9 @@ END, and R are its arguments." ;; sadly org-indent-refresh-maybe modifies beg/end but does not return (let ((oiap-orig (symbol-function 'org-indent-add-properties))) (cl-letf (((symbol-function 'org-indent-add-properties) - (lambda (b e &optional delay) - (setq omi/-level-change-end (and (> e end) e)) - (funcall oiap-orig b e delay)))) + (lambda (b e &optional delay) + (setq omi/-level-change-end (and (> e end) e)) + (funcall oiap-orig b e delay)))) (apply fun beg end r)))) (defun omi/-strip-display (beg end) @@ -213,23 +213,23 @@ END, and R are its arguments." (defun omi/-wait-and-refresh (buf) "Wait for org-indent to finish initializing BUF, then refresh." (if (or (not (bound-and-true-p org-indent-agentized-buffers)) - (not (memq buf org-indent-agentized-buffers))) + (not (memq buf org-indent-agentized-buffers))) (omi/init buf) ;; still waiting (when (buffer-live-p buf) (with-current-buffer buf - (if omi/-init - (let ((cnt (cl-incf (cadr omi/-init)))) - (if (> cnt 5) - (user-error - "org-modern-indent: Gave up waiting for %s to initialize" - buf) - (timer-activate - (timer-set-time (car omi/-init) - (time-add (current-time) 0.2))))) - (setq omi/-init - (list (run-at-time 0.1 nil #'omi/-wait-and-refresh buf) - 1))))))) + (if omi/-init + (let ((cnt (cl-incf (cadr omi/-init)))) + (if (> cnt 5) + (user-error + "org-modern-indent: Gave up waiting for %s to initialize" + buf) + (timer-activate + (timer-set-time (car omi/-init) + (time-add (current-time) 0.2))))) + (setq omi/-init + (list (run-at-time 0.1 nil #'omi/-wait-and-refresh buf) + 1))))))) (defun omi/-refresh () "Strip custom display property and refresh line prefix in entire buffer." @@ -246,24 +246,24 @@ END, and R are its arguments." (goto-char target) (back-to-indentation) (let ((beg-targ (point)) - (end-targ (progn (skip-chars-forward "^[:blank:]\n") (point)))) + (end-targ (progn (skip-chars-forward "^[:blank:]\n") (point)))) (and (<= beg end-targ) (<= beg-targ end))))) (defun omi/-before-change (beg end) "Check the BEG..END range for modified block header/footer lines. -Adds an `:org-modern-indent-block' text property to the block, with +Adds an `:org-modern-block-indent' text property to the block, with value `damaged' for damaged block." (cl-labels ((detect-block-damage (bl-beg bl-end) - "Detect and mark damaged blocks." - (unless (and (<= beg bl-beg) (>= end bl-end)) ; entire block replaced/deleted - (with-silent-modifications - (if (or (omi/-intersects-leader-at bl-beg beg end) - (omi/-intersects-leader-at bl-end beg end)) - (progn - (put-text-property bl-beg (1+ bl-beg) :omi/indent nil) ; now unknown! - (put-text-property bl-beg (omi/-lbp bl-end 2) :omi/block 'damaged)) - (put-text-property bl-beg (omi/-lbp bl-end 2) :omi/block t)))))) + "Detect and mark damaged blocks." + (unless (and (<= beg bl-beg) (>= end bl-end)) ; entire block replaced/deleted + (with-silent-modifications + (if (or (omi/-intersects-leader-at bl-beg beg end) + (omi/-intersects-leader-at bl-end beg end)) + (progn + (put-text-property bl-beg (1+ bl-beg) :omi/indent nil) ; now unknown! + (put-text-property bl-beg (omi/-lbp bl-end 2) :omi/block 'damaged)) + (put-text-property bl-beg (omi/-lbp bl-end 2) :omi/block t)))))) (omi/-walk-blocks beg end #'detect-block-damage))) (defun omi/-pos-in-leader-p (pos) @@ -272,7 +272,7 @@ value `damaged' for damaged block." (org-with-wide-buffer (cl-destructuring-bind (bbeg . bend) (omi/-block-beg-end node) (or (omi/-intersects-leader-at bbeg pos pos) - (omi/-intersects-leader-at bend pos pos)))))) + (omi/-intersects-leader-at bend pos pos)))))) (defun omi/-after-change (beg end _len) "Perform after-change operations on changed text from BEG..END. @@ -280,17 +280,17 @@ To be added after `org-indent-refresh-maybe' on `after-change-functions'." (with-silent-modifications (let ((beg-block (get-text-property beg :omi/block)) - (end-block (get-text-property end :omi/block)) - extend) + (end-block (get-text-property end :omi/block)) + extend) (when (and beg-block (or (eq beg-block 'damaged) (omi/-pos-in-leader-p beg))) - (setq extend t - beg (or (previous-single-property-change beg :omi/block) beg))) + (setq extend t + beg (or (previous-single-property-change beg :omi/block) beg))) (when (and end-block (or (eq end-block 'damaged) (omi/-pos-in-leader-p end))) - (setq extend t - end (or (next-single-property-change end :omi/block) end))) + (setq extend t + end (or (next-single-property-change end :omi/block) end))) (when extend - (remove-text-properties beg end '(:omi/block nil :omi/indent nil)) - (org-indent-add-properties beg end))) + (remove-text-properties beg end '(:omi/block nil :omi/indent nil)) + (org-indent-add-properties beg end))) ;; If we had a level change, extend down to the next heading (when omi/-level-change-end (setq end (max end omi/-level-change-end))) (setq beg (omi/-lbp beg) end (omi/-lbp end 2)) @@ -298,26 +298,26 @@ To be added after `org-indent-refresh-maybe' on (omi/-walk-blocks beg end (lambda (bl-beg bl-end) (let ((beg0 bl-beg) (end0 bl-end) - (pf (get-text-property bl-beg 'line-prefix)) - oi-pos oi ci full-block) - (save-excursion - (goto-char bl-beg) - (back-to-indentation) - (setq oi-pos (point) - oi (get-text-property oi-pos :omi/indent) - ci (current-indentation))) - (unless (eq oi ci) - (put-text-property oi-pos (1+ oi-pos) :omi/indent ci)) - (when (and oi (= oi 0) (/= oi ci)) ; was flush, is now indented: - (org-indent-add-properties bl-beg bl-end)) ; repair block's prefixes - (when (< bl-beg beg) ; 1st block straddles BEG - (if (eq oi ci) (setq bl-beg beg) (setq full-block t))) - (when (> bl-end end) ; last block straddles END - (if (eq oi ci) (setq bl-end end) (setq full-block t))) - (when full-block - (with-silent-modifications - (remove-text-properties bl-beg bl-end '(omi/display nil)))) - (omi/-draw-block bl-beg bl-end beg0 end0 (length pf) ci)))))) + (pf (get-text-property bl-beg 'line-prefix)) + oi-pos oi ci full-block) + (save-excursion + (goto-char bl-beg) + (back-to-indentation) + (setq oi-pos (point) + oi (get-text-property oi-pos :omi/indent) + ci (current-indentation))) + (unless (eq oi ci) + (put-text-property oi-pos (1+ oi-pos) :omi/indent ci)) + (when (and oi (= oi 0) (/= oi ci)) ; was flush, is now indented: + (org-indent-add-properties bl-beg bl-end)) ; repair block's prefixes + (when (< bl-beg beg) ; 1st block straddles BEG + (if (eq oi ci) (setq bl-beg beg) (setq full-block t))) + (when (> bl-end end) ; last block straddles END + (if (eq oi ci) (setq bl-end end) (setq full-block t))) + (when full-block + (with-silent-modifications + (remove-text-properties bl-beg bl-end '(omi/display nil)))) + (omi/-draw-block bl-beg bl-end beg0 end0 (length pf) ci)))))) ;;;; Mode/setup (defun omi/init (&optional buf) @@ -327,14 +327,14 @@ To be added to `org-indent-post-buffer-init-functions'." (let ((buf (or buf (current-buffer)))) (when (buffer-live-p buf) ; org-capture buffers vanish fast (with-current-buffer buf - (add-hook 'before-change-functions #'omi/-before-change nil t) - (or (memq #'omi/-after-change after-change-functions) - (cl-loop for func on after-change-functions - if (eq (car func) 'org-indent-refresh-maybe) do - (setcdr func (cons #'omi/-after-change (cdr func))) and return t) - (add-hook 'after-change-functions #'omi/-after-change 98 t)) - (org-with-wide-buffer (omi/-after-change (point-min) (point-max) nil)) - (setq omi/-init t))))) + (add-hook 'before-change-functions #'omi/-before-change nil t) + (or (memq #'omi/-after-change after-change-functions) + (cl-loop for func on after-change-functions + if (eq (car func) 'org-indent-refresh-maybe) do + (setcdr func (cons #'omi/-after-change (cdr func))) and return t) + (add-hook 'after-change-functions #'omi/-after-change 98 t)) + (org-with-wide-buffer (omi/-after-change (point-min) (point-max) nil)) + (setq omi/-init t))))) ;;;###autoload (define-minor-mode omi/mode @@ -343,24 +343,24 @@ To be added to `org-indent-post-buffer-init-functions'." :group 'org-modern-indent (if omi/mode (progn - (advice-add 'org-indent-refresh-maybe :around - #'omi/-refresh-maybe-watch) - (cond - ;; already registered before, just toggle - ((or (called-interactively-p 'any) omi/-init) (omi/init)) - ;; Register with buffer init - ((boundp 'org-indent-post-buffer-init-functions) - (add-hook 'org-indent-post-buffer-init-functions #'omi/init nil t)) - ;; No hook available, use the less reliable method - (t (omi/-wait-and-refresh (current-buffer)))) - (cl-pushnew 'omi/display - (alist-get 'display char-property-alias-alist))) + (advice-add 'org-indent-refresh-maybe :around + #'omi/-refresh-maybe-watch) + (cond + ;; already registered before, just toggle + ((or (called-interactively-p 'any) omi/-init) (omi/init)) + ;; Register with buffer init + ((boundp 'org-indent-post-buffer-init-functions) + (add-hook 'org-indent-post-buffer-init-functions #'omi/init nil t)) + ;; No hook available, use the less reliable method + (t (omi/-wait-and-refresh (current-buffer)))) + (cl-pushnew 'omi/display + (alist-get 'display char-property-alias-alist))) ;; Disabling (advice-remove 'org-indent-refresh-maybe #'omi/-refresh-maybe-watch) (remove-hook 'before-change-functions #'omi/-before-change t) (remove-hook 'after-change-functions #'omi/-after-change t) (if (boundp 'org-indent-post-buffer-init-functions) - (remove-hook 'org-indent-post-buffer-init-functions #'omi/init t) + (remove-hook 'org-indent-post-buffer-init-functions #'omi/init t) (cancel-timer (car omi/-init)) (setq omi/-init nil)) (omi/-refresh))) @@ -371,4 +371,3 @@ To be added to `org-indent-post-buffer-init-functions'." ;; read-symbol-shorthands: (("omi/" . "org-modern-indent-") (":omi/" . ":org-modern-indent-")) ;; package-lint--sane-prefixes: "^omi/" ;; End: - diff --git a/org-modern.el b/org-modern.el index 152e96f3ac..584a279df2 100644 --- a/org-modern.el +++ b/org-modern.el @@ -225,8 +225,8 @@ which specifies the offset of the block border from the edge of the window." :type '(choice boolean natnum)) -(defcustom org-modern-indent-block t - "Whether to style indented blocks when using `org-indent'. +(defcustom org-modern-block-indent t + "Whether to style indented blocks when using `org-indent-mode'. See `org-modern-indent-mode'." :type 'boolean) @@ -383,8 +383,7 @@ the font.") (defconst org-modern--table-sp '((space :width (org-modern--table-sp-width)) (space :width (org-modern--table-sp-width)))) (defvar org-indent-mode) -(defvar org-modern-indent-mode) -(declare-function org-modern-indent-mode "org-modern-indent") +(declare-function org-modern-indent-mode "ext:org-modern-indent") (defun org-modern--checkbox () "Prettify checkboxes according to `org-modern-checkbox'." @@ -871,7 +870,7 @@ whole buffer; otherwise, for the line at point." (add-hook 'org-cycle-hook #'org-modern--cycle nil 'local)) (org-modern--update-faces) (org-modern--update-bitmaps) - (when (and org-indent-mode org-modern-indent-block) + (when (and org-indent-mode org-modern-block-indent) (org-modern-indent-mode 1))) (t (remove-from-invisibility-spec 'org-modern) @@ -883,7 +882,8 @@ whole buffer; otherwise, for the line at point." (remove-hook 'org-after-demote-entry-hook #'org-modern--unfontify-line 'local) (when (eq org-modern-star 'fold) (remove-hook 'org-cycle-hook #'org-modern--cycle 'local)) - (when org-modern-indent-mode (org-modern-indent-mode 0)))) + (when (bound-and-true-p org-modern-indent-mode) + (org-modern-indent-mode 0)))) (without-restriction (with-silent-modifications (org-modern--unfontify (point-min) (point-max)))