summaryrefslogtreecommitdiff
path: root/gnosis-string-edit.el
diff options
context:
space:
mode:
authorThanos Apollo <[email protected]>2024-03-10 12:41:50 +0200
committerThanos Apollo <[email protected]>2024-03-10 12:41:50 +0200
commitbf8f3f5164a893a7100b5457e907ea4315a69ffc (patch)
tree4fcaceeccb6839d9842d788ca65498d6ac240f37 /gnosis-string-edit.el
parent3b8c89081f4b84829042bfead22a5cddf50e673a (diff)
Add gnosis-string-edit-ensure-empty-lines
- ensure-empty-lines is not available on older emacs versions
Diffstat (limited to 'gnosis-string-edit.el')
-rw-r--r--gnosis-string-edit.el28
1 files changed, 27 insertions, 1 deletions
diff --git a/gnosis-string-edit.el b/gnosis-string-edit.el
index 6c53f11..e6a90e4 100644
--- a/gnosis-string-edit.el
+++ b/gnosis-string-edit.el
@@ -40,6 +40,32 @@
(defvar gnosis-string-edit--success-callback)
(defvar gnosis-string-edit--abort-callback)
+(defun gnosis-string-edit-ensure-empty-lines (&optional lines)
+ "Ensure that there are LINES number of empty lines before point.
+If LINES is nil or omitted, ensure that there is a single empty
+line before point.
+
+If called interactively, LINES is given by the prefix argument.
+
+If there are more than LINES empty lines before point, the number
+of empty lines is reduced to LINES.
+
+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))))))
+
;;;###autoload
(cl-defun gnosis-string-edit (prompt string success-callback
&key abort-callback)
@@ -59,7 +85,7 @@ Also see `read-string-from-buffer'."
(when prompt
(let ((inhibit-read-only t))
(insert prompt)
- (ensure-empty-lines 0)
+ (gnosis-string-edit-ensure-empty-lines 0)
(add-text-properties (point-min) (point)
(list 'intangible t
'face 'gnosis-string-edit-prompt