I'm working on some compiler shortcuts: I want to write a function (cmp FILE-NAME &optional args) that will use FILE-NAME and args to construct the appropriate COMMAND to call (compile COMMAND) with.

For example: (cmp "Test.java" "-Xlint:all") will run (compile "javac Test.java -Xlint:all")

This is mostly so I can write my default args that I'll want to compile specific programs with.

As part of this, I'll need my function to determine the programming language of the given file. I could do this with some simple expressions like:

(setq file-ext (substring file-name (string-match "\.[^.]*$" file-name)))
(setq programming-language (cond ((equal ".java" file-ext) "java")
                                 ((equal ".c" file-ext) "c")
                                 ((equal ".sc" file-ext) "scala")))

This would work but I'd have to add a new condition for any new programming language, and it seems overkill because I know that Emacs is already able to determine a file's programming language: it's how it knows to, for example, load java-mode when I open a Java file.

So I know it's probably a simple question, but I can't find documentation to show me how Emacs determines a file's programming language, and how I can leverage that same function for my own customization.

  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Jul 7, 2022 at 16:30
  • Emacs's ability to "determine a file's programming language" is based on a simple list, just a bit more complicated than the one you conteplated. Do C-h v auto-mode-alist.
    – NickD
    Commented Jul 7, 2022 at 16:34
  • Do you maybe want to use the external file utility? E.g. in your shell, run file <path>. That's the best way I know of to identify the purpose of a file.
    – phils
    Commented Aug 2, 2023 at 7:52

1 Answer 1


Emacs doesn't determine a file's programming language. What it does is look at the file buffer's major mode, and that mode is often (by default) associated with a given programming language.

See (C-h v) variable auto-mode-alist for the association of file-name suffixes with major modes.

So the first question to ask yourself is this: Do you really need to know the "programming language"? Or does it suffice to know the major mode?

Note that there's no real way (no way possible) to know/obtain the name of a programming language from a major mode. You can try various ad hoc means of getting it, by parsing some doc or function/variable names, but there's no real association that's dependable.

See if you can do what you're (really) trying to accomplish just by using the major-mode. That you have access to.

See the Elisp manual, node Major Mode Conventions for some info about major modes.

  • Thank you! The auto-mode-alist looks like a great option: it's basically a pre-filled-out list of what I was looking for. I'm looking into the major-mode and I can definitely see its use, but if I'm understanding it correctly, its value will depend on what my current open buffer is. So if I want to compile "desktop/Test.java," I'd have to open the file first for the mode to switch to java-mode Commented Jul 7, 2022 at 18:23
  • Not sure what you're saying, in that last part. You can get the mode for a file extension from auto-mode-alist. You can get to all info about that mode from C-h f <the-mode-name>. From that *Help* buffer you have a link to the source code that defines the mode.
    – Drew
    Commented Jul 7, 2022 at 18:37
  • I think we're on the same page. I can use auto-mode-alist to get the mode for any file (or at least most of them: it's a pretty big list). I was wondering if I could use major-mode similarly, but it seems major-mode is defined for the current buffer, so I could only use it when I'm trying to compile the file in my current buffer Commented Jul 7, 2022 at 18:43
  • @andy_programs (with-current-buffer BUF major-mode) or (buffer-local-value 'major-mode BUF) gives you the major-mode value in the buffer BUF.
    – phils
    Commented Aug 2, 2023 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.