Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7023155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:40:30+00:00 2026-05-27T23:40:30+00:00

I have the following code in my .emacs file, which works as you’d expect:

  • 0

I have the following code in my .emacs file, which works as you’d expect:

;; Ruby
(global-font-lock-mode 1)
(autoload 'ruby-mode "ruby-mode" "Ruby editing mode." t)
(setq auto-mode-alist (cons '("\\.rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.rsel$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.rhtml$" . html-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.erb$" . html-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.prawn$" . html-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("Rakefile$" . ruby-mode) auto-mode-alist))

However, my attempts to DRY it up a bit fail:

(defun set-mode-for-filename-patterns (mode filename-pattern-list)
  (mapcar
    (lambda (filename-pattern)
      (setq 
        auto-mode-alist 
        (cons '(filename-pattern . mode) auto-mode-alist)))
    filename-pattern-list))

;; Ruby
(global-font-lock-mode 1)
(autoload 'ruby-mode "ruby-mode" "Ruby editing mode." t)
(set-mode-for-filename-patterns 
  ruby-mode
  '("\\.rb$"
    "\\.rsel$"
    "\\.rhtml$"
    "\\.erb$" 
    "\\.prawn$"
    "Rakefile$"
    "Gemfile$"))

… with the following error:

Debugger entered--Lisp error: (void-variable ruby-mode)
  (set-mode-for-filename-patterns ruby-mode (quote ("\\.rb$" "\\.rsel$" "\\.rhtml$" "\\.erb$" "\\.prawn$" "Rakefile$" "Gemfile$")))
  eval-buffer(#<buffer  *load*> nil "/home/duncan/.emacs" nil t)  ; Reading at buffer position 1768
  load-with-code-conversion("/home/duncan/.emacs" "/home/duncan/.emacs" t t)
  load("~/.emacs" t t)
  #[nil "\205\264

I’m a bit confused here … in particular, I don’t understand how ruby-mode is void & so can’t be passed to a function, but can be consed into a pair?

Any pointers (heh) would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:40:31+00:00Added an answer on May 27, 2026 at 11:40 pm

    In the form:

    (cons '("\\.rb$" . ruby-mode) ...
    

    ruby-mode is part of a quoted list. That means it is read as a symbol name, not evaluated as a variable. In other words, Emacs sees it as the symbol ruby-mode and accepts it as is.

    In the form:

    (set-mode-for-filename-patterns 
       ruby-mode
       '("\\.rb$"
         "\\.rsel$"
         ...
    

    ruby-mode is not quoted, and so is read as the argument to a function. Function arguments are evaluated. Which means Emacs reads ruby-mode, recognizes it as a symbol, and tries to evaluate it. Evaluating a symbol means looking for the value that it points to, which in this case doesn’t exist.

    EDIT:

    Your function still doesn’t work, there’s another problem. You’ve used a quoted list inside set-mode-for-filename-patterns. This works fine in your original code:

    (setq auto-mode-alist (cons '("\\.rb$" . ruby-mode) auto-mode-alist))
    

    as you are in effect manually supplying the value for filename-pattern and mode. Inside your function, you need these symbols to be evaluated, which doesn’t happen when they’re quoted! The result is that instead of adding each different string from your list to auto-mode-alist, you get the symbol filename-pattern instead.

    To fix this, you need to recognize that the ‘(filename-pattern . mode) is meant to produce a cons cell with the values of filename-pattern and mode. Which we can produce with (cons filename-pattern mode). So the corrected function would be:

    (defun set-mode-for-filename-patterns (mode filename-pattern-list)
      (mapcar
        (lambda (filename-pattern)
          (setq 
            auto-mode-alist 
            (cons (cons filename-pattern mode) auto-mode-alist)))
        filename-pattern-list))
    

    And the corrected function call is:

    (set-mode-for-filename-patterns 
      'ruby-mode
      '("\\.rb$"
        "\\.rsel$"
        "\\.rhtml$"
        "\\.erb$" 
        "\\.prawn$"
        "Rakefile$"
        "Gemfile$"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Emacs, and I have the following code as a sample.
Let's say I have my own elisp code in ~/bin/hello.el . The ~/.emacs file
Let's say I have the following line in .emacs file. (setq-default default-directory ~/Desktop/mag) How
I have the following code in my .emacs: (if (null window-system) (progn (require 'color-theme)
I am using Emacs with php-mode.el: http://php-mode.sourceforge.net/php-mode.el.html I have the following lines in my
On top of evil-mode in emacs, I have written an extension mode which allows
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code to have auto-fill mode as minor mode when I
I have the following code in ~/.emacs for running scheme (gosh and mit-scheme). ;(setq
I have Ruby Electric mode installed via ELPA. I have visited a ruby file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.