Search code examples
emacselisp

Elisp suppress "Select coding system" when write buffer to file


I write my function. In this function I insert some data to buffer and then write this buffer to file. Here my code snippet:

          (set-buffer my-buffer-name)
          (set-language-environment "UTF-8")
          (setq make-backup-files nil)
          (write-file (expand-file-name my-file-name my-current-dir))
          (kill-buffer)

But every time when try to write to file, the elisp prompt:

Select coding system (default raw-text)

How I can set coding system (e.g. utf-8) to not prompt me every time when run my function? Thanks.


Solution

  • You should set coding-system-for-write, or buffer-file-coding-system instead of using set-language-environment.

    PS. Generally speaking, you should use the lowest-level function possible in your code (e.g., help for next-line explicitly recommends that one use forward-line in code instead).

    In your case, you might want to use basic-save-buffer or even write-region instead.

    PPS. You should probably use with-current-buffer instead of set-buffer.