Search code examples
emacslispbufferelispregion

Emacs Lisp function, that write-region to file and delete it


I want to write a ELisp function that will: append current region to a new file (it is already written in ELisp standard library and it is called write-region) and then open this file as new buffer in background so I can edit it later.

but I also want the current selected region to be deleted.

I want to do something like this:

(defun write-region-delete-and-open(start end filename)
  "function takes current region, and writes it to specified file"
  (interactive "r\nsFilename: ")
  (write-region start end filename t)
  (kill-region start end))

But It isn't working as I assume. When I cut the region, it always kills all after that region in buffer. I also want to use (ido-find-file) to select file to write a buffet to. I completely have no idea how to open created file in background. Please help me.


Solution

  • To use ido-find-file, change the interactive specification for the filename argument to F. That is:

    (interactive "r\nFFilename: ")
    

    That's assuming you're already using ido for opening other files, as by (ido-mode 1) in your .emacs file.

    The function to open a file in the background, by which I assume you mean to open a file without displaying it immediately, is called find-file-noselect.

    I don't know why the entire buffer after the region would be killed by that call to kill-region; I don't see that happen when I use your code.