1

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 (delete-selection-mode t)?

  • 1
    I would recommend typing M-x find-library RET delsel RET and then scroll down towards the end have a look at the lines (put SYMBOL PROPNAME VALUE). Then, have a look at the doc-string for the function delete-selection-helper. I don't use helm, but would imagine you just need something like (put 'your-function 't) or (put 'your-function 'yank) or (put 'your-function 'supersede) or (put 'your-function 'kill) or (put 'your-function 'FUNCTION). You may not need anything else other than that one-liner, but I'm just guessing. – lawlist Nov 22 '17 at 8:50
  • Thanks for the info. I've tried (put 'helm-copy-to-buffer 'delete-selection t), ... 'yank and ... 'supersede. All didn't work. I guess it's because helm-copy-to-buffer uses helm-run-after-exit to insert string in another buffer. – AhLeung Dec 2 '17 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.