1

In the course of using Emacs I got a dozen of lines that simply make underscore part of a word, and most of them upon byte-compilation are causing warnings like reference to free variable ‘php-mode-syntax-table’.

I asked on IRC how to deal with that, and it seems the most optimal way is to use eval-after-load to modify the syntax table. So I made up the code below:

(eval-after-load 'text-mode
  '(modify-syntax-entry ?_ "w" text-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'rust-mode
  '(modify-syntax-entry ?_ "w" rust-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'cc-mode
  '(modify-syntax-entry ?_ "w" c++-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'cc-mode
  '(modify-syntax-entry ?_ "w" c-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'python
  '(modify-syntax-entry ?_ "w" python-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'lisp-mode
  (modify-syntax-entry ?- "w" lisp-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'gud
  (modify-syntax-entry ?_ "w" gud-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'php-mode
  (modify-syntax-entry ?_ "w" php-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'haskell-mode
  (modify-syntax-entry ?_ "w" haskell-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'shell-mode
  (modify-syntax-entry ?_ "w")) ;; make underscore part of a word
(eval-after-load 'markdown-mode
  (modify-syntax-entry ?_ "w" markdown-mode-syntax-table)) ;; make underscore part of a word
(eval-after-load 'diff-mode
  (modify-syntax-entry ?_ "w" diff-mode-syntax-table)) ;; make underscore part of a word

For some reason, Emacs fails at runtime with error like

Warning (initialization): An error occurred while loading ‘/home/constantine/.emacs’:

Symbol's value as variable is void: php-mode-syntax-table

This error not only happens for php-mode, but also for gud-mode, haskell-mode. I can't understand what's causing it — if Emacs really loaded the file, it would clearly have the syntax table. What should I do?

Hi-Angel
  • 525
  • 6
  • 18
  • To future readers: I ended up reducing the above bunch of code into the following, hope this helps `(dolist (mode '(text-mode rust-mode cc-mode python lisp-mode gud php-mode haskell-mode shell-mode markdown-mode diff-mode)) (eval-after-load mode '(modify-syntax-entry ?_ "w")))` – Hi-Angel Dec 24 '18 at 20:47
  • Nvm the above comment, although I leave it in case somebody else attempts the like: the `(modify-syntax-entry ?_ "w")` modifies syntax table of major mode of current buffer, but it not necessarily matches the mode being loaded aka one of those in the `dolist` list. I don't think the code in question can be further reduced. – Hi-Angel Dec 25 '18 at 09:00

1 Answers1

1

You are missing quotes ' on many of your examples. i.e.,

(eval-after-load 'haskell-mode
  (modify-syntax-entry ?_ "w" haskell-mode-syntax-table))

That means the unquoted bit, i.e., (modify-syntax-entry ...) is getting evaluated immediately, it isn't deferred until after haskell-mode is loaded. You can correct this by adding all the missing ' characters. eg:

(eval-after-load 'haskell-mode
  '(modify-syntax-entry ?_ "w" haskell-mode-syntax-table))
Tyler
  • 21,719
  • 1
  • 52
  • 92