Loading packages installed through 'package.el' in Emacs24 [duplicate] - emacs

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Emacs 24 Package System Initialization Problems
I am using Emacs 24. I have the ELPA and Marmalade repos added. Using 'package' I installed 'auto-complete'. I have the following lines added to my init.el:
(require 'auto-complete-config)
(ac-config-default)
When I start Emacs, I get the error
File error: Cannot open load file, auto-complete-config
But then I use
M-x load-file
and load the same ~/.emacs.d/init.el file, it then works fine with the prompt saying
Loading /home/user/.emacs.d/init.el (source)...done
How is the usual loading different from the 'M-x load-file' command? In the start of the init.el file I do the following, is this somehow effecting the package from loading.
(add-to-list 'load-path "~/.emacs.d")
(load "custom_code")

As mentioned in the comment below: The answer by phils to the duplicate question is probably more helpful than this one
This almost certainly means that your init.el file is getting run before the code that sorts out the packages for package.el. The latter code adds the directory with the auto-complete library to your load path.
I'm still using ELPA, rather than package.el. With elpa, there's a snippet that looks like this that gets installed at the bottom of your .emacs.
;;; This was installed by package-install.el.
;;; This provides support for the package system and
;;; interfacing with ELPA, the package archive.
;;; Move this code earlier if you want to reference
;;; packages in your .emacs.
(when
(load
(expand-file-name "~/.emacs.d/elpa/package.el"))
(package-initialize))
As the comment suggests, you probably want to put your equivalent package.el initialization code before the stuff that loads init.el.
Finally: I notice you mention adding .emacs.d to your load-path. The Emacs load path is not recursive, so that probably won't do what you need (assuming that your libraries live in subdirectories). Years ago, I wrote this snippet to load up various libraries of elisp code that I'd written. You might find it useful. (Obviously, it'll only work on unixy systems with a shell and a find command. It's reasonably slow, but this seems to be shell-command-to-string, which takes several milliseconds even running "echo hello" or the like)
(defun find-elisp-dirs (dir)
"Find all directories below DIR containing elisp sources, ignoring those"
(split-string
(shell-command-to-string
(format "find %s -iname '*.el' -printf '%%h\\n' | sort -u"
(expand-file-name dir t)))))

Related

emacs can't find its things in its own load path

the following is output from a terminal session demonstrating that I, hopefully actually set this up right.
~ $cat .emacs
(require 'package)
(custom-set-variables
;;lots of comments generated by computer
'(package-archives (quote(("gnu" . "http://elpa.gnu.org/packages")
("marmalade" . "http://marmalade-repo.org/packages")
("melpa" . "http://melpa.milkbox.net/packages/")
("org" . "http://orgmode.org/elpa")))))
(custom-set-faces
;;again lots of comments added by the computer
)
(add-to-list 'load-path "/usr/share/emacs/24.3/site-lisp/mu4e")
~ $ ls /usr/share/emacs/24.3/site-lisp/mu43
#there are a lot of files here, but I am only going to show 2 right now
mu4e.elc
mu4e.el
... and yet emacs M-x mu4e returns [no match]. I have checked to load-path variable, and it is there. What am I doing wrong?
You need to add one more thing so that mu4e is loaded. There are two different ways to do this.
First, you could add (require 'mu4e) after you've added the path to your load-path. This will immediately load mu4e.
Alternatively, you could add the following:
(autoload 'mu4e "mu4e" "Launch mu4e and show the main window" t)
This will tell Emacs to load it lazily (i.e. not until you actually use it). Autoloading is documented here. (This is essentially done for you for packages installed via package.el - it's the same mechanism, you just don't need to specify it yourself).
The benefit of autoloads is that Emacs's initial startup is faster, since rather than loading every package right away it waits until you use them.

Let .emacs.d behaves just like a .d folder

I want to solve my “.emacs bankruptcy” issue, and I've gone through
https://help.ubuntu.com/community/EmacsHowto
http://www.emacswiki.org/emacs/DotEmacsBankruptcy
http://www.emacswiki.org/emacs/DotEmacsDotD
http://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html
and it is still unclear to me whether the .emacs.d folder is the solution. I.e., whether it will behave just like a normal .d folder, e.g., /etc/profile.d/, where you drop you scripts and they will be picked up by the system auto-magically. Please confirm.
If not, can someone give me a script that does that, or give me a solution please?
Thanks
The essential content of my ~/.emacs file is:
(require 'cl)
(loop for src in (directory-files "~/.emacs.d" 'full-path "[0-9].*\\.el$") do
(let ((byte (concat src "c")))
(when (file-newer-than-file-p src byte)
(byte-compile-file src))
(message "Loading %s.elc" byte)
(load-file byte)))
It loads configuration files from ~/.emacs.d which start with a number. If the source file (extension .el) is newer than the byte-compiled version (extension .elc) then it byte-compiles the source. Afterwards it loads the byte compiled file.
Here's my ~/.emacs:
;; base dirs
(defvar dropbox.d "~/Dropbox/")
(defvar emacs.d (concat dropbox.d "source/site-lisp/"))
;; load path
(add-to-list 'load-path emacs.d)
(defun add-subdirs-to-load-path (dir)
(let ((default-directory dir))
(normal-top-level-add-subdirs-to-load-path)))
(add-subdirs-to-load-path emacs.d)
(load "init")
All my other scripts are loaded by ~/Dropbox/source/site-lisp/init.el
and are themselves located in ~/Dropbox/source/site-lisp.
That's how I have the same config on multiple machines.
And here's how .../site-lisp/hooks.el is loaded from init.el:
(load "hooks")
My init.el is about 100 lines, .emacs about 20 lines.
The rest 8000 lines of scripts are sliced into around 20 files.
~/.emacs.d/ does not work like /etc/profile.d/ or /etc/modules-load.d/ or similar directories, i.e. Emacs does not automatically load any Emacs Lisp file in this directory.
In fact, Emacs explicitly advises against placing Emacs Lisp libraries in ~/.emacs.d/. The byte compiler emits a warning if you add ~/.emacs.d/ to the load-path.
Instead, create a new sub-directory, e.g. ~/.emacs.d/lisp. Add this directory to your load-path explicitly, with the following code in init.el:
(add-to-list 'load-path (locate-user-emacs-file "lisp"))
Then, place your Emacs Lisp files in this directory, e.g. ~/.emacs.d/lisp/foo.el, and load them in your init.el:
(load "foo" nil 'no-message)
The best approach to avoid the dreaded .emacs bankruptcy is to actually avoid large customizations! Most notably, try to avoid any custom functions and commands.
Instead, try to a find an ELPA package that comes closest to what you want, and either try to get used to it, or customize it to your needs. If you don't find any, first try to write your own and distribute it on Github, Marmalade or MELPA.
Don't be afraid of maintaining a package in the public. You'll have to maintain your customization anyway, whether in your init.el or not, so you can just as well let other Emacs users help you with this job.
Adding code to your init.el should be your very last resort!

How to install a Emacs plugin (many times it's a .el file) on Windows platform?

I'm new to Emacs. I found many emacs plugins are released as an .el file. I'm not sure how to install them. Can I just put them in my emacs installation directory?
After placing it, say myplugin.el to your ~/.emacs.d/ directory, add the following in your .emacs file:
(add-to-list 'load-path "~/.emacs.d/")
(load "myplugin.el")
Also, in many cases you would need the following instead of the second line:
(require 'myplugin)
In any case, you should consult the documentation of the package you are trying to install on which one you should use.
If you are unsure where your ~ directory is, you may see it by typing C-x d ~/ and pressing Enter.
As already stated, you'll need the location of the file to be in Emacs' load path.
Read the comments at the top of the file to see if it has any particular installation or usage instructions. Authors often provide this information, and there isn't one single correct way to do it, so it's sensible to look.
Failing that, if the file contains a (provide 'some-name) line (typically at the end of the file), then you would be expected to use (require 'some-name) to load it.
You may also wish to byte-compile the library for speed (but that's a different question).
Many times, an emacs plugin will consist of a directory of elisp files that need to be accessible from the load path. A simple way to ensure that all individual elisp files as well as subdirectories of elisp files are included in the load path and accessible is to do something similar to the following:
Create a directory called ~/.emacs.d/site-lisp.
Install any single elisp files in the ~/.emacs.d/site-lisp directory.
Install any packages that consist of multiple elisp files in a subdirectory under your ~/.emacs.d/site-lisp directory.
Add the following code to your ~/.emacs file to ensure that Emacs "sees" all the elisp files that you have installed:
(add-to-list 'load-path "~/.emacs.d/site-lisp")
(progn (cd "~/.emacs.d/site-lisp")
(normal-top-level-add-subdirs-to-load-path))
This will ensure that all elisp files that are located either in either the ~/.emacs.d/site-lisp directory or in a subdirectory under that directory are accessible.
Some supplementary information:
MATLAB.el comes from http://matlab-emacs.sourceforge.net/
On windows, use the load path that looks like this:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
If you want FULL MATLAB functionality you should use:
;;MATLAB Mode:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
(require 'matlab-load)
if you just want to edit text files:
;;MATLAB Mode:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
(autoload 'matlab-mode "matlab" "Enter MATLAB mode." t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive MATLAB mode." t)

Emacs not recognizing its own scripts in /emacs/lisp/

I'm very new to emacs and I'm using version 23.2 on Windows. I'm trying to get CEDET working, but when I require it in .emacs it fails to find the file:
File error: Cannot open load file
I was able to get cedet working by loading it manually with:
(load "C:/emacs/lisp/cedet/cedet.el")
But I still can't require other files from cedet like semantic-gcc or semantic-ia.
Here's my .emacs file:
(load "C:/emacs/lisp/cedet/cedet.el")
(global-ede-mode t)
(semantic-mode 1)
;(semantic-load-enable-excessive-code-helpers)
(require 'semantic-ia)
(require 'semantic-gcc)
It's like emacs isn't looking for these files in its own path, and I did try
(add-to-list 'load-path "C:/emacs/lisp/cedet")
With a lot of other variations but none worked.
Firstly, you need to find the reason that cedet won't load with a simple (require 'cedet).
Is Emacs installed at c:\emacs? (ie the emacs.exe you are running is c:\emacs\bin\emacs.exe)
Is something setting EMACSLOADPATH externally from Emacs (your environment, or in the registry under HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER /Software/GNU/Emacs?
Is there another installation of an older version of CEDET on your load path?
Has c:\emacs\lisp\subdirs.el been edited to remove the cedet subdirectory?
Once you've solved that, note that the paths were changed when CEDET was merged into Emacs to accommodate old systems that have limitations on file name lengths. But at the same time, the autoloads were improved, so you shouldn't need to explicitly require those files any more. If you still do, the following should work:
(require 'semantic/ia)
(require 'semantic/bovine/gcc)

Emacs and slime stopped cooperating for me

I'm trying to use slime from CVS (2009-01-05) but keep getting this error:
LOAD: A file with name
/usr/share/common-lisp/source/slime/swank-loader.lisp does not exist
I've stripped my .emacs down to just:
(setq inferior-lisp-program "/usr/bin/clisp")
(add-to-list 'load-path "/home/ssm/lisp/slime/")
(require 'slime)
(slime-setup)
I've deleted my ~/.slime directory, started with 'emacs -q' and eval'd the above code but I keep getting the LOAD error when I run slime (via M-x slime). Any ideas on how to fix this error?
FWIW, I've tried to install slime via apt-get but I keep getting errors there too about cl-swank being broken. That's a whole different story.
Have you purged the slime pkg you installed via apt-get? It looks like emacs is still reading the old site-specific configuration setup by apt-get. Try starting emacs with the -Q option, which prevents loading of site-specific (as well as user specific) customization, and see if the problem still occur.
I agree with huaiyuan that older files may be being picked up.
Try (load-file "/path/to/slime.el") instead of require. (You did remove the .elc files from your old versions, right? emacs will load from .elc files in preference to .el files, even when the .el is newer.)
The next thing to try is M-x customize-variable slime-backend and setting that to the absolute path of swank-loader.lisp. I think that will fix it for sure, but I am not sure why it doesn't work to begin with.
Thanks guys, ~/.emacs:
(setq inferior-lisp-program "<path-to-lisp-compiler>/bin/lisp")
(setq slime-backend "<path-to-slime>/swank-loader.lisp")
(add-to-list 'load-path "<path-to-slime>/")
;;(require 'slime)
(load-file "<path-to-slime>/slime.el")
;;(slime-setup)
(slime-setup '(slime-fancy))
works :)