Why does emacs start slowly - emacs

Recently, I found that my emacs starts slowly. Sometimes the startup process takes 30 seconds, sometimes 3 seconds. I guess this is related to network enviroment. However, emacs -q can always start up quickly.

This question is like asking "How long is a piece of string". You have not provided anywhere near enough information. Start with telling us which version of emacs and what platform you running on.
You mention that emacs -q starts quickly, so we can assume the slowness is due to your init file. There are a lot of possibilities and no way for us to tell for sure without analysing your init file. However, there are a few things you can do to help identify possible causes.
#Drew's comment about bisecting your init file is a very useful trick to identify the source of a problem in your init file, particularly when trying to track down an error. It can be useful in identifying major contributors to load time when those contributors can be linked to a specific config option or package load etc. It is less useful when the problem is an overly long inefficient init file with too many packages.
Using a profiler as suggested by #legoscia can help identify areas to focus on, but it will still be necessary to interpret and understand what the profiler is telling you.
My suggestions would be to
Additional Packages. Since the introduction of package.el and the emacs package archives, I've observed a tendency for some users to get a little carried away and install lots of packages just in case. run C-h p and go through the packages you have installed. If you don't use any or you see ones there which you no longer want or use, remove them. Each additional package (depending on how they are loaded - see below) will increase your startup time by some amount as each package will need to be loaded. Loading packages you don't need or want will slow down startup time.
Stuff you don't need or understand. Over time, our init file can grow with stuff and we don't remember why we added it or it can be stuff we have copied from others which we thought we might need and forgot about. Every few months (depending on how often you modify your init - maybe more frequently if you make frequent changes, less if you don't) go through your init file and comment out anything you don't need, don't understand or don't rmember why you added it. Just comment it out, don't delete it. Re-start restart emacs and see if it starts faster and whether features or configuration settings you want/need have gone. If there are, look through the stuff you have commented out and see if anything looks relevant. When you find possible candidates, try to work out what the code is doing (look in the emacs manual, use C-h f and C-h v or C-h m etc to try and work it out. If it relates to a specific package, try M-x customize-package and see if there is built in customization which might give you what you want. Even if you prefer to customize things by hand rather than use emacs' built in customization system, M-x customize-group is a great way to find out what customizations are supported by a package and verify you have variable names correct etc.
Learn emacs autoloads. Emacs has this very useful feature called autoload. There are two main ways of loading packages into emacs. The first is to just do a basic 'require'. This will look for an emacs library with the appropriate name and will read it in and evaluate it (it is like adding the file to your init file, so more lines to evaluate, which means increased load time for your init). The good thing about require is that it just loads the whole file and you know that everything gets evaluated. the bad things is that it can in turn require other files, which can require other files and can result in a much longer init time. To try and address this problem, emacs has the autoload facility. Basically, with autoload, you tell emacs that if it tries to execute a command with the name 'foo', it must first load (require) a specific file which you define in the autoload definition. The advantage here is that emacs doesn't try to load the library until you first try to use it, so there is no increase in load time. The disadvantage is that it can mean there is a delay the first time you use a command and it won't work if other parts of your init use the command (well, it will work, but will have no impact on improving load time).
Switch to use-pckage. There is a very useful package called use-package which can help address all of the issues I've mentioned. It helps compartmentalise your configuraiton, simplifies some common configuration tasks and has support for either setting up a package with autoloads or deferring the loading of a package until emacs is idle. It can help speed up your init time considerably provided you use it correctly. Highly recommend giving it a go. See https://github.com/jwiegley/use-package

Related

Run Specific Plugins for each Emacs Instance

Is it possible to have only certain plugins run when first starting emacs?
Let's say I develop in Python and also in Ruby. So I want to have one emacs instance running with Python plugins and another running Ruby plugins.
What I'm imagining is I can call rb-emacs or py-emacs from the command line.
So I think part of my solution lies here
http://stackoverflow.com/questions/2112256/emacs-custom-command-line-argument
And then I can alias the emacs call with the custom switches to one of the above
But then, how can I associate a specific plugin with a specific switch?
Am I on the right track with this? Or should I be doing something else entirely?
Edit:
Since my problem does not seem to be clear, I'll try to reiterate here. I'm not worried about long loading times. I'm worried about potential conflicts between plugins. I've used emacs before but only on a basic scale. Now I'd like to go more in depth with plugins. Though I don't fully understand how the plugins work.
Say I have a plugin (or two or three, I don't know how many it might be) for each language I code in. Won't those conflict with each other? Also, I don't want views / windows that are unneeded for that particular language.
You'll just need a different init file for each of your Emacs instances. Then you can create shell aliases for opening Emacs with those init files.
From the Emacs Wiki:
Start Emacs with a specific init file: emacs -q -l ~/my-init-file.el
Then you'll just set up a shell alias like:
alias rb-emacs=emacs -q -l ~/.rb-emacs-init.el
But why do this with separate Emacs processes? If you're concerned about the startup time, you can use lazy loading of packages or Emacs Server with Emacsclient.
I'm voting for "doing something else entirely", but I'm not 100% sure what the problem you're trying to solve is.
In general you can use mode hooks, eval-after-load, and autoload to ensure that you only load a particular elisp library when it is required.
If your problem is that you're forcibly loading everything and it takes too long, then you need to change your code so that you only load things when necessary. See OptimizingEmacsStartup.
If your problem is that you are setting global values for variables that need to have different values for different projects, then you want to be using buffer-local values for them, either via mode hooks, or using directory local variables.
What is the problem you're trying to solve?

Emacs: opening any file in a large repo

What I need
a fast/performant way to open any file under a large (git) repo (~9.8k files).
Context
I have tried various solutions, like Textmate.el and find-file-in-repository. I found these solutions via previous SO questions like this and this and through the LocateFilesAnywhere EmacsWiki.
While both solutions work wonderfully for small-to-mdeium repos, in this case they are practically unusable. When I start typing a filename, there's a delay of several seconds before I see any result. And changing any part of the search is very laggy too.
I think the main problem is that on typing any character, emacs/find-file-in-repository starts a shell command (git ls-files...). I really only need to do that when I have stopped typing.
Questions
is there a better library out there for this use-case?
if not, how can I introduce a delay into the command when I'm typing? i.e. while I'm in find-file-in-repository, I want the find-command to be invoked only when I stop typing (let's say a gap of 300ms).
Summary
After I received the three answers I tried them out (also answering my own question as none of the above solutions worked for me). I finally settled for helm-ls-git. Here's a comparison from my point-of-view:
Projectile
took around 30 minutes to index the repo. Since projectile is not aware of .gitignore, the actual number of files is more like 52k.
can be customized but something that just works (i.e. understands git) is preferable
may need to invalidate cache re-index time to time. That would be costly and frequent since new files are added everyday to the repo.
helm-cmd-t
looked good from the description and the source.
hard to install since it's not published in melpa/marmalade etc. More details in this issue I opened up.
GNU Global
Didn't try as it's likely to have the same problems as Projectile (git-unaware, needs it's own "index" that may need to be maintained time to time)
event-jr's answer however opened up some more options: I was unaware of helm till now. Looking at melpa for helm related plugins I found the following:
helm-git
This looked really promising
Was easy to install with package.el since it's in melpa
I also use and love magit - so this looked a good fit.
However, it kept failing with a magit-git-dir: symbol is void kind of error. Did not dive in too much but looks like it needs to be updated. Opened up an issue
helm-ls-git
As the readme says, this is magit independent.
Has been working wonderfully so far. Easy to install (melpa) and is fast.
I use GNU global for this. I have around 20K files in my project. You can run
M-x gtags-find-file and type first few characters. TAB will complete and show all the matching. You can type any characters which is part of the file name and press enter. Will show all the files that contains these characters.
I tried to use projectile for this. But it was way too slow for the 'project indexing'. It didn't complete the indexing even after 1.5 hours and I have to kill it!. Not sure some thing is wrong here. GNU global is much faster and finishes the entire tag creation within 15 min.
You can check out Projectile. It was basically created to provide something similar to C-p, but has a lot of extra project level features as well. First time project indexing will be fairly slow on such a big project, but afterwards Projectile will cache the project files (both on memory and on the hard drive) and subsequent projectile invocations should be nearly instantaneous.
Projectile also has a Helm plugin to display project files and buffers with Helm.
I use helm-cmd-t happily. It will cache the file list in memory. The cache controls are flexible enough for my needs.
I just answered your question about new repo address here:
https://stackoverflow.com/a/8025310/903943
It's https://github.com/lewang/helm-cmd-t

Emacs setup for fortran

I currently work on a big Fortran project with emacs, but I have the feeling that my current setup is inadequate to the task.
I use f90-mode with auto-complete (without fortran-specific setup, so I only have completion for opened files), and I really miss function header information on hover (as in elisp code), code-folding, lists of subroutines in the current buffer, lists of included files, info on the origin of subroutines and used variables (C-xC-f to open the source file?), …
How can I best add modern supporting functionality for fortran in my emacs?
Mostly I need tools which help me understanding the projects code.
The project uses its own build tool and copies files from different directories into a build directory before building, actually overwriting some files with different versions of the code, so I need a quite flexible tool which can cope with that.
There's a small Emacs plugin called Fortran-tags. It can find the definition of any variable or procedure under the cursor, so it's similar to Ctags, except that it is able to correctly identify all local and global variables and has some additional features. Also, it is developed with the focus on modern Fortran.
Using fortran-language-server (after installation simply start fortls in the terminal) and lsp-mode in emacs works perfectly.
I now found the f90-interface-browser in elpa.
If you use emacs 24 or later, you can just use
M-x package-list-packages
and then search for f90-interface-browser.
You write (or work on) large, modern fortran code bases. These
make heavy use of function overloading and generic interfaces. Your
brain is too small to remember what all the specialisers are
called. Therefore, your editor should help you.

Is it possible to get Semantic (emacs) to visit all files automatically?

From what I can tell from the docs, semantic works by slowly building up an idea of what's in your project by analysing each file (and possibly its neighbours) as you visit them. This is too slow. I'd like to just have it visit all the files in my project. Is there an easy way to do this? Having to visit hundreds of files before I can get decent autocomplete working seems crazy.
I've also got a etags file generated. Can I leverage that somehow?
Relevant info: Emacs on Windows, version 23.2.1
CEDET will automatically parse all files references via #include statements, thus providing pretty good completion. If you are looking to jump around in your files, you can setup CEDET to use GNU Global, CScope, to provide the database needed to move around a project by tag name.
In addition, CEDET will parse your headers and nearby files in idle time, so eventually you will have a complete database of all your local files in about 10 minutes after using the tools the first time. You can speed it up by opening a file, and calling
M-x semantic-debug-idle-work-function
which will go off and do that stuff without waiting.
In the end, I've found that the best solution is to brute-force the parsing of the files manually using a bit of elisp. The best answer I've found to this is here.

Best practices in building and deploying Clojure applications: good tutorials?

I am new to Clojure, and am beginning to experiment with building an application.
So far, everything I've seen about tutorials on compiling Clojure programs involves interactivity. For example, "load up the REPL and type (load-file "this-or-that") to run. This is fine, but it's not enough.
I am so used to the edit-compile-run idioms of languages like C or Delphi, that I am instinctively driven to make edits, then hit "M-x compile".
The problem is that "lein uberjar", which I understand is the equivalent to "make", is painfully slow to execute even for a hello world. So I'm going to have to figure out how this "interactive development" stuff works, stop using the uberjar like it's quick make, and save it only for the end of the day.
Another thing I noticed while building (using lein uberjar) is that the small GUI app I am working on pops up frames in the compilation process, as if they are executing while compiling. It just seems a bit counterintuitive to me; it is not quite as analogous to "make" as I had thought.
I know the Lisp way of developing things is interactively working in the REPL, and I am not trying to change that: I would like to adapt to this way of life. Unfortunately, I have seen little in the form of documentation on how to do so. For instance, how to reset the current state of the machine. It just seems kind of messy to just keep compiling individual snippets on the fly without being able to do some sort of reset.
Most tutorials I have seen on Clojure (and Lisp) in general seem to focus on hacking in the REPL. Best practices on the deployment of applications remains a mystery to me. My users are just going to be users; they are not going to be developers that are going to load files into a REPL.
So here is my question: any resources for good information or tutorials on the entire process of building a Clojure application, including deployment?
(Note: I have all of the prerequisites installed and working (e.g. Emacs, Slime, Leiningen, etc.), so this is not a question about that).
A couple of quick hints, then some links:
Don't use lein uberjar during development; prefer lein jar. The difference is that lein uberjar puts all your dependencies in the generated jar (including Clojure itself), so that your single jar is an entirely self contained package with your app inside; lein jar only jars your own code. The uberjar approach has obvious benefits for deployment, but for development, you should be able to just use the appropriate classpath when running your app, saving yourself the time necessary to prepare an uberjar. If you don't want to hand-manage the classpath for test runs, check out the lein run plugin.
Also, most likely the majority of your code should not actually be AOT compiled. AOT is necessary in some Java interop scenarios, but most of the time it brings one a slight boost in startup speed and annoying problems with binary compatibility with different releases of Clojure. I suppose the latter issue is not relevant to an uberjar-ed standalone app kind of project, but any library code at least should be left to be JIT-ed if at all possible. With Leiningen, you can put a :namespaces clause in the defproject form in project.clj to determine which namespaces are to be compiled; whatever you leave out will currently be JIT-ed by default. Older versions of Leiningen used to compile everything by default, which is actually a good reason to upgrade!
As for the windows popping out during compilation, I would guess that you're either running window-out-popping code during macro expansion time or outside of any function definition or similar construct. (Something like a (println "Foo!") at the top level.) That's just something you shouldn't do, I suppose -- unless you are planning to run your code as a script, anyway. To avoid the problem, wrap side-effecting code up in function definitions and provide an entry point to your application using the :main clause in project.clj. (If you say :main foo, then the -main function from the foo namespace will be used as the entry point to your app. That's the default, anyway, and at least the above mentioned lein run seems to have the name hardcoded -- not sure about lein itself.)
As for resetting the state of the REPL -- you can just restart it. With SLIME, M-x slime-restart-inferior-lisp will do just that while maintaining all other state of your Emacs session.
See also these discussions on the Clojure Google group:
Clojure for system administration
Prepping clojure for packaging (was: Re: Clojure for system administration)
Leiningen, Clojure and libraries: what am I missing?
No, you do not enter functions on the REPL.
You edit your source files, just as usual. The Lisp advantage is that you have the system running in the background at the same time, so you can compile individual functions from your source file and put them into the running system, or even replace them there.
If you use Slime, you press C-c C-c in your source file to compile and load the function at point. You can then switch to the REPL to test and explore, but anything you want to persist as source, you put into your source files.
Tutorials usually start by typing things on the REPL, because there is not much you need to set up for this, but serious development integrates the running system and source file management.
Just to illustrate, my usual workflow (I am using Common Lisp, but Clojure is similar) is like this:
Start Emacs
M-x slime to start Slime, the Lisp system, and connect the two via Swank
, (command) load-system foo to load the current project (compiling only if necessary) into the image
C-x b switch to a source buffer
C-c ~ make the source directory the current directory and the source package the current package of the REPL
Now, I'm set up with my system running in the background. Working is then:
change or add a function or class definition
C-c C-c to compile and load it into the image
switch to REPL, test
debug
There are no significant compilation pauses, because I never compile the whole thing at once, just individual definitions.