I am pretty sure there was a way to remove backup suffixes like ~ and .bak before mode detection, but I just can't find it anymore...
1 Answer
M-x elisp-index-search RET auto-mode-alist says:
When you visit a file whose expanded file name (*note File Name Expansion::), with version numbers and backup suffixes removed using ‘file-name-sans-versions’ (*note File Name Components::), matches a REGEXP, ‘set-auto-mode’ calls the corresponding MODE-FUNCTION. This feature enables Emacs to select the proper major mode for most files. file-name-sans-versions:
This function returns FILENAME with any file version numbers, backup version numbers, or trailing tildes discarded. ... See also `file-name-version-regexp'. file-name-version-regexp:
Regular expression matching the backup/version part of a file name. Used by `file-name-sans-versions'. If you can't get the behaviour you want using only the regexp, then you would redefine file-name-sans-versions to have the behaviour that you require.
My build of Emacs has the following default regexp:
(defvar file-name-version-regexp "\\(?:~\\|\\.~[-[:alnum:]:#@^._]+\\(?:~[[:digit:]]+\\)?~\\)" ;; The last ~[[:digit]]+ matches relative versions in git, ;; e.g. `foo.js.~HEAD~1~'. "Regular expression matching the backup/version part of a file name. Used by `file-name-sans-versions'.")