branch: externals/embark commit 666a3c598b57dbc089fb29311b385eb77246befa Author: Omar Antolín <omar.anto...@gmail.com> Commit: Omar Antolín <omar.anto...@gmail.com>
Have embark-insert insert some whitespace if needed For a while I've been thinking that embark-insert should put some space around the string it inserts to avoid having it run together with the text in the buffer. The lack of padding made it very awkward to insert several string in a row. For example, I often write comment for GitHub or Reddit about Emacs in a Markdown buffer and use C-h f to collect the function names I want to mention. Inserting more than one of them in a single C-h f session made them run together. With the new embark-act-all command this problem is even worse! It seems unlikely that some people may prefer the old mash-em-up behavior, so I'm not initially making this configurable. If a user is happy with the padding but for some specific use case wants no spaces it is easy to get to the space with motion commands and delete them. By contrast, with the old behavior it was unnecessarily hard to insert the missing spaces since you could't use easy word navigation commands to get to the right spot: it required moving by characters or using isearch or clicking with the mouse, etc. --- embark.el | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/embark.el b/embark.el index f24d182..72654c1 100644 --- a/embark.el +++ b/embark.el @@ -3233,7 +3233,7 @@ Return the category metadatum as the type of the target." (add-hook 'embark-target-finders #'embark--ivy-selected) (add-hook 'embark-candidate-collectors #'embark--ivy-candidates)) -;;; Custom actions +;;; Custom actions open-line open-line (defun embark-keymap-help () "Prompt for an action to perform or command to become and run it." @@ -3241,12 +3241,38 @@ Return the category metadatum as the type of the target." (user-error "Not meant to be called directly")) (defun embark-insert (string) - "Insert STRING at point." + "Insert STRING at point. +Some whitespace is also inserted if necessary to avoid having the +inserted string blend into the existing buffer text. More +precisely: + +1. If the inserted string does not contain newlines, a space may +be added before or after it as needed to avoid inserting a word +constituent character next to an existing word constituent. + +2. For a multiline inserted string, newlines may be added before +or after as needed to ensure the inserted string is on lines of +its own." (interactive "sInsert: ") - (if buffer-read-only - (with-selected-window (other-window-for-scrolling) - (insert string)) - (insert string))) + (let ((multiline (string-match-p "\n" string))) + (cl-flet* ((maybe-space () + (and (looking-at "\\w") (looking-back "\\w" 1) + (insert " "))) + (maybe-newline () + (or (looking-back "^[ \t]*" 40) (looking-at "\n\n") + (newline-and-indent))) + (maybe-whitespace () + (if multiline (maybe-newline) (maybe-space))) + (insert-string () + (save-excursion + (insert string) + (maybe-whitespace) + (delete-blank-lines)) + (maybe-whitespace))) + (if buffer-read-only + (with-selected-window (other-window-for-scrolling) + (insert-string)) + (insert-string))))) (define-obsolete-function-alias 'embark-save