Function: set-buffer-file-coding-system

set-buffer-file-coding-system is an interactive and byte-compiled function defined in mule.el.gz.

Signature

(set-buffer-file-coding-system CODING-SYSTEM &optional FORCE NOMODIFY)

Documentation

Set the file coding-system of the current buffer to CODING-SYSTEM.

This means that when you save the buffer, it will be converted according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM, use M-x list-coding-systems (list-coding-systems).

If CODING-SYSTEM leaves the text conversion unspecified, or if it leaves the end-of-line conversion unspecified, FORCE controls what to do. If FORCE is nil, get the unspecified aspect (or aspects) from the buffer's previous buffer-file-coding-system value (if it is specified there). Otherwise, leave it unspecified.

This marks the buffer modified so that the succeeding C-x C-s (save-buffer) surely saves the buffer with CODING-SYSTEM. From a program, if you don't want to mark the buffer modified, specify t for NOMODIFY. If you know exactly what coding system you want to use, just set the variable buffer-file-coding-system directly.

Probably introduced at or before Emacs version 20.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/international/mule.el.gz
(defun set-buffer-file-coding-system (coding-system &optional force nomodify)
  "Set the file coding-system of the current buffer to CODING-SYSTEM.
This means that when you save the buffer, it will be converted
according to CODING-SYSTEM.  For a list of possible values of
CODING-SYSTEM, use \\[list-coding-systems].

If CODING-SYSTEM leaves the text conversion unspecified, or if it leaves
the end-of-line conversion unspecified, FORCE controls what to do.
If FORCE is nil, get the unspecified aspect (or aspects) from the buffer's
previous `buffer-file-coding-system' value (if it is specified there).
Otherwise, leave it unspecified.

This marks the buffer modified so that the succeeding \\[save-buffer]
surely saves the buffer with CODING-SYSTEM.  From a program, if you
don't want to mark the buffer modified, specify t for NOMODIFY.
If you know exactly what coding system you want to use,
just set the variable `buffer-file-coding-system' directly."
  (interactive
   (list (read-buffer-file-coding-system)
         current-prefix-arg))
  (check-coding-system coding-system)
  (if (and coding-system buffer-file-coding-system (null force))
      (setq coding-system
	    (merge-coding-systems coding-system buffer-file-coding-system)))
  (when (and (called-interactively-p 'interactive)
             ;; FIXME: For some reason
             ;;     (coding-system-get 'iso-2022-7bit :charset-list)
             ;; returns `iso-2022' rather than returning a list!
             (let ((css (coding-system-get coding-system :charset-list)))
               (not (and (listp css) (memq 'emacs css)))))
    ;; Check whether save would succeed, and jump to the offending char(s)
    ;; if not.
    (let ((css (find-coding-systems-region (point-min) (point-max))))
      (unless (or (eq (car css) 'undecided)
                  (memq (coding-system-base coding-system) css))
        (setq coding-system (select-safe-coding-system-interactively
                             (point-min) (point-max) css
                             (list coding-system))))))
  (setq buffer-file-coding-system coding-system)
  (if buffer-file-coding-system-explicit
      (setcdr buffer-file-coding-system-explicit coding-system)
    (setq buffer-file-coding-system-explicit (cons nil coding-system)))
  (unless nomodify
    (set-buffer-modified-p t))
  (force-mode-line-update))