When I call
helm-copy-to-buffer (which calls
insert) with active region in my current buffer, the marked helm candidate is added after the active region. But I expect the active region to be replaced by the helm candidate, as I had
(delete-selection-mode t) in my init.el.
I tried to fix it by following advice:
(defun delete-region-before-insert (orig-fun &rest args) (cl-letf (((symbol-function 'insert) (lambda (arg) (if (region-active-p) (delete-region (region-beginning) (region-end))) (insert arg)))) (apply orig-fun args))) (advice-add 'helm-copy-to-buffer :around #'delete-region-before-insert)
But it doesn't change anything, so I have to redefine
helm-copy-to-buffer to fix it:
(defun helm-copy-to-buffer () "Copy selection or marked candidates to `helm-current-buffer'. Note that the real values of candidates are copied and not the display values." (interactive) (with-helm-alive-p (helm-run-after-exit (lambda (cands) (with-helm-current-buffer (if (region-active-p) (delete-region (region-beginning) (region-end))) ; added to delete region if any. (insert (mapconcat (lambda (c) (format "%s" c)) cands "\n")))) (helm-marked-candidates))))
Is there any better way to fix it?
In general, how to make sure
insert comply with