diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2021-10-04 13:15:41 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2021-10-04 13:23:22 +0200 |
commit | 8b4a6a722a3982024fc87b26dbc7ef7e2043f6e1 (patch) | |
tree | 8a0c19130d8b9c3a89467b497c853fbc2b9adfbb /lisp/emacs-lisp | |
parent | 909f2a4b926ccc1b86d60341e44ca83c4d24fa0f (diff) | |
download | emacs-8b4a6a722a3982024fc87b26dbc7ef7e2043f6e1.tar.gz emacs-8b4a6a722a3982024fc87b26dbc7ef7e2043f6e1.tar.bz2 emacs-8b4a6a722a3982024fc87b26dbc7ef7e2043f6e1.zip |
Add new command 'ensure-empty-lines'.
* doc/lispref/text.texi (Commands for Insertion): Document it.
* lisp/emacs-lisp/subr-x.el (ensure-empty-lines): New command.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/subr-x.el | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 91ebbf9fb92..ecd3ca831e8 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -412,6 +412,32 @@ and return the value found in PLACE instead." ,(funcall setter val) ,val))))) +;;;###autoload +(defun ensure-empty-lines (&optional lines) + "Ensure that there's LINES number of empty lines before point. +If LINES is nil or missing, a this ensures that there's a single +empty line before point. + +Interactively, this command uses the numerical prefix for LINES. + +If there's already more empty lines before point than LINES, the +number of blank lines will be reduced. + +If point is not at the beginning of a line, a newline character +is inserted before adjusting the number of empty lines." + (interactive "p") + (unless (bolp) + (insert "\n")) + (let ((lines (or lines 1)) + (start (save-excursion + (if (re-search-backward "[^\n]" nil t) + (+ (point) 2) + (point-min))))) + (cond + ((> (- (point) start) lines) + (delete-region (point) (- (point) (- (point) start lines)))) + ((< (- (point) start) lines) + (insert (make-string (- lines (- (point) start)) ?\n)))))) (provide 'subr-x) |