branch: scratch/editorconfig-cc commit fbbcb58e5b48024b328cde7f9d58dceecc7dc53c Author: 10sr <8.slas...@gmail.com> Commit: Stefan Monnier <monn...@iro.umontreal.ca>
Use revert-buffer-with-coding-system to set coding system (#236) * Use revert-buffer-with-coding-system to set coding system * Avoid infinite call * Do not ask user for revert when buffer is not modified * Refactor -set-coding-system * Do not use revert when the file does not exist yet * Refactor -set-coding-system --- editorconfig.el | 55 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/editorconfig.el b/editorconfig.el index 5e74fb9049..6fbde1e678 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -335,26 +335,45 @@ number - `lisp-indent-offset' is not set only if indent_size is (when (not (equal size "tab")) (setq size nil))) ) -(defun editorconfig-set-coding-system (end-of-line charset) +(defvar editorconfig--apply-coding-system-currently nil + "Used internally.") +(make-variable-buffer-local 'editorconfig--apply-coding-system-currently) +(put 'editorconfig--apply-coding-system-currently + 'permanent-local + t) + +(cl-defun editorconfig-set-coding-system (end-of-line charset) "Set buffer coding system by END-OF-LINE and CHARSET." - (let ((eol (cond - ((equal end-of-line "lf") 'undecided-unix) - ((equal end-of-line "cr") 'undecided-mac) - ((equal end-of-line "crlf") 'undecided-dos) + (let* ((eol (cond + ((equal end-of-line "lf") 'undecided-unix) + ((equal end-of-line "cr") 'undecided-mac) + ((equal end-of-line "crlf") 'undecided-dos) + (t 'undecided))) + (cs (cond + ((equal charset "latin1") 'iso-latin-1) + ((equal charset "utf-8") 'utf-8) + ((equal charset "utf-8-bom") 'utf-8-with-signature) + ((equal charset "utf-16be") 'utf-16be-with-signature) + ((equal charset "utf-16le") 'utf-16le-with-signature) (t 'undecided))) - (cs (cond - ((equal charset "latin1") 'iso-latin-1) - ((equal charset "utf-8") 'utf-8) - ((equal charset "utf-8-bom") 'utf-8-with-signature) - ((equal charset "utf-16be") 'utf-16be) - ((equal charset "utf-16le") 'utf-16le) - (t 'undecided)))) - (unless (and (eq eol 'undecided) - (eq cs 'undecided)) - (set-buffer-file-coding-system (merge-coding-systems - cs - eol) - nil t)))) + (coding-system (merge-coding-systems cs eol))) + (when (eq coding-system 'undecided) + (cl-return-from editorconfig-set-coding-system)) + (unless (file-readable-p buffer-file-name) + (set-buffer-file-coding-system coding-system) + (cl-return-from editorconfig-set-coding-system)) + (unless (eq coding-system + editorconfig--apply-coding-system-currently) + ;; Revert functions might call editorconfig-apply again + (unwind-protect + (progn + (setq editorconfig--apply-coding-system-currently + coding-system) + ;; Revert without query if buffer is not modified + (let ((revert-without-query '("."))) + (revert-buffer-with-coding-system coding-system))) + (setq editorconfig--apply-coding-system-currently + nil))))) (defun editorconfig-set-trailing-nl (final-newline) "Set up requiring final newline by FINAL-NEWLINE.