3

I have written a function to yank a selected region n times. I get a warning however because of the (delete-backward-char) although it seems to work. Does the (delete-backward-char) need an argument?

(defun yank-n-times ()
  "yanks the region n times"
  (interactive)
  (let ((n (string-to-number (read-string "Yank n times: "))))
    (dotimes (x n) (yank))
    (delete-backward-char)))

the updated function

(defun yank-n-times ()
  "yanks the region n times"
  (interactive)
  (let ((n (read-number "Yank n times: "))
    (txt (car kill-ring-yank-pointer)))
    (dotimes (x n) (yank))
    (when
    (or (string-suffix-p "\s" txt)
        (string-suffix-p "\n" txt)
        (string-suffix-p "\t" txt))
      (delete-char -1))))

(global-set-key (kbd "C-c C-n y") 'yank-n-times)
  • 1
    I suggest you use read-number. – Stefan Nov 7 '17 at 23:06
4

Yes; delete-backward-char requires at least one argument. Press C-h f and type "delete-backward-char" to read the docstring. An excerpt:

(delete-backward-char N &optional KILLFLAG)

This function is for interactive use only; in Lisp code use ‘delete-char’ instead.

If the function already behaves as you expect, just remove the delete-backward-char instruction. I imagine that might have been intended to remove a trailing newline. If for some reason you do want the last character of the last yank deleted you could replace the instruction with (delete-char -1).

  • I updated my function, if you or any one finds something which could be made more idiomatic i would be gratful to know. My idea with the deletion of the last char was to remove it if it is a space or a new line or a tab from the end of the yankings. – amirteymuri Nov 8 '17 at 16:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.