4

When I visit a new file, ^xf, that has an extension of .sql or .dsql I would like the coding system to be set to utf-8-with-signature-dos. The default is currently iso-latin-1-dos.

I'm running (version) of "GNU Emacs 24.3.1 (i386-mingw-nt6.1.7601)\n of 2013-03-17 on MARVIN"

Both the .sql and .dsql extension start up sql-mode, but I doubt a hook off of sql mode is the appropriate place to change the coding system for new files.

  • Do you have a specific reason not to use prefer-coding-system? – Andrew Swann Mar 11 '15 at 19:11
  • This is only case where I want to change coding system preferences. – Shannon Severance Mar 11 '15 at 19:24
  • Do you have files of this type that are not coded in this way? It looks like you should be customizing file-coding-system-alist – Andrew Swann Mar 11 '15 at 19:37
  • Yes, there are some that are UTF-16. :(. I want to be intentional about changing those, so I don't want emacs to do it automagically. – Shannon Severance Mar 11 '15 at 19:47
  • @AndrewSwann Does file-coding-system-alist only apply to buffers for nonexistent files? I want to change just the default coding system for new files in general but not change existing ones, and the current answer seems more hacky than should be necessary. – binki May 24 '17 at 4:53
4

I wrote a function and added to find-file-not-found-functions

(defun srs-set-new-sql-file-coding-system ()
  (with-current-buffer (current-buffer)
    (if (string-match "\\.d?sql\\'" buffer-file-name)
        (setq buffer-file-coding-system 'utf-8-with-signature-dos)
      nil)))

(add-hook 'find-file-not-found-functions 'srs-set-new-sql-file-coding-system)

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.