3

I would like to override the default Emacs cc-mode.el with another file. In my .emacs, I have the following line:

(add-to-list 'load-path "/usr/home/smooth/emacs" t)

This works correctly to load local Emacs configuration files. For example

(require 'go-mode-autoloads)

works to load go-mode.el from the above directory.

I tried placing the required cc-mode.el file into the above directory in the hope that it would override the default Emacs c-mode. However, it did not load the cc-mode.el file which I had put in /usr/home/smooth/emacs. Instead it continued to use the default one.

How can I make Emacs use my cc-mode.el file and not the default one? I have already extensively searched with Google and tried the Emacs Wiki, but I couldn't find the correct page.

Alternatively, how I can I set Emacs to use another file for c-mode than the system default? I don't mind renaming the current cc-mode.el to another name.

My version of Emacs is 25.2.

  • Generally, what you did would work, provided `cc-mode` wasn't loaded *before* your `load-path` modification. – phils May 05 '17 at 11:17
  • Looking at Emacs Wiki, I noticed there was no "t" at the end of the "load-path", so I removed that and it started working. The "t" above may be the origin of the problem. What is very strange is that the misbehaviour in cc-mode I was trying to get around has also disappeared. The above "t" may have caused some sort of other errors. See https://www.emacswiki.org/emacs/LoadPath for the Emacs Wiki page. – Smooth Criminal May 05 '17 at 11:22
  • I found this page: https://www.gnu.org/software/emacs/manual/html_node/elisp/List-Variables.html. It says `Normally, if element is added, it is added to the front of symbol, but if the optional argument append is non-nil, it is added at the end.` It seems I was adding at the end. I'm not sure why that broke cc-mode so horribly, since it was working until I switched to emacs 25.2 a few days ago. It's now working well. – Smooth Criminal May 05 '17 at 11:46
  • "The "t" above may be the origin of the problem." Quite right -- I didn't notice the APPEND argument in your code. It's unusual to use that with `load-path`, as Emacs will then only look in that directory if it's failed to find the library in any of the earlier directories in the list. You've managed to answer your own question, so you may as well post it as an answer and accept it. – phils May 05 '17 at 12:30

2 Answers2

1

Looking at Emacs Wiki, I noticed there was no "t" at the end of the "load-path", so I removed that and it started working. The "t" above may be the origin of the problem. What is very strange is that the misbehaviour in cc-mode I was trying to get around has also disappeared. The above "t" may have caused some sort of other errors. See emacswiki.org/emacs/LoadPath for the Emacs Wiki page.

I found this page: gnu.org/software/emacs/manual/html_node/elisp/…. It says Normally, if element is added, it is added to the front of symbol, but if the optional argument append is non-nil, it is added at the end. It seems I was adding at the end. I'm not sure why that broke cc-mode so horribly, since it was working until I switched to emacs 25.2 a few days ago. It's now working well.

  • Often, optional parameters like APPEND in `add-to-list` are boolean, i.e. it only matters if they're `nil` or not `nil`. I like to give them descriptive values in that case, e.g. `(add-to-list 'load-path "foo" 'append)`. – jpkotta May 05 '17 at 16:26
0

You can use load-library (or load) to unconditionally load a library, even if the feature(s) it provides have already been provided (e.g. by another library or another version of the same library already having been loaded).

Just put the library you want to load in a directory that is near the beginning of your load-path and then call load-library.

[Note, however, that loading (or reloading) a library does NOT cancel out defvars and defcustoms that might already have been evaluated. For example, if library foo.el has a defcustom for foo-option and it has been loaded, and then you load your own version of foo.el that has such a defcustom but with a different default value, yours will be ignored. This is part of the behavior of defvar and defcustom.]

Drew
  • 29,895
  • 7
  • 74
  • 104