New minor mode center-line-mode

* lisp/textmodes/text-mode.el (center-line-mode--track-changes):
New local variable for storing the id of the change tracker registered
for the current buffer.
(center-line-mode--track-changes-signal): New function to be called by
the track-changes library whenever there is a change in the current
buffer.
(center-line-mode--track-changes-function): New function called from the
above signal function, iterates over the lines of the modified region,
calling 'center-line' for each non-empty line.
(center-line-mode): New minor mode.
* etc/NEWS: Document the new minor mode.
This commit is contained in:
Amin Bandali 2026-01-04 23:50:51 -05:00 committed by Eli Zaretskii
parent cc3e6f368f
commit 5020d89104
2 changed files with 55 additions and 0 deletions

View file

@ -949,6 +949,11 @@ These commands did not previously accept a prefix argument.
Now a numeric prefix argument specifies a repeat count, just like it
already did for 'undo'.
** New minor mode 'center-line-mode'.
This mode keeps modified lines centered horizontally according to the
value of 'fill-column', by calling 'center-line' on each non-empty line
of the modified region.
* Changes in Specialized Modes and Packages in Emacs 31.1

View file

@ -269,6 +269,56 @@ The argument NLINES says how many lines to center."
(setq nlines (1+ nlines))
(forward-line -1)))))
;; Actually defined in track-changes.el.
(defvar track-changes-undo-only)
(declare-function track-changes-register "track-changes"
( signal &optional &key nobefore disjoint immediate))
(declare-function track-changes-unregister "track-changes" (id))
(declare-function track-changes-fetch "track-changes" (id func))
(defvar-local center-line-mode--track-changes nil)
(defun center-line-mode--track-changes-signal (tracker)
(track-changes-fetch
tracker
#'center-line-mode--track-changes-function))
(defun center-line-mode--track-changes-function (beg end _before)
(unless track-changes-undo-only
(save-excursion
(let ((beg-line (line-number-at-pos beg))
(end-line (line-number-at-pos end))
(should-center-last-line-p
(progn
(goto-char end)
(null
(or (bolp)
(and (eolp)
(looking-back "[\r\n\t ]" (1- (point)))))))))
(goto-char beg)
(dotimes (_ (- end-line beg-line)) ; all but last line
(unless (and (bolp) (eolp))
(center-line))
(forward-line 1))
(when should-center-last-line-p
(center-line)))))
;; Disregard our own changes.
(track-changes-fetch center-line-mode--track-changes #'ignore))
(define-minor-mode center-line-mode
"Minor mode for keeping modified lines centered horizontally.
Calls `center-line' on each line of the modified region to center the
text within the width specified by `fill-column'."
:lighter " Center-Line"
(require 'track-changes)
(if center-line-mode
(setq center-line-mode--track-changes
(track-changes-register
#'center-line-mode--track-changes-signal
:nobefore t))
(when center-line-mode--track-changes
(track-changes-unregister center-line-mode--track-changes))))
(define-obsolete-function-alias 'indented-text-mode #'text-mode "29.1")