How to debug Emacs lisp that makes Emacs crash/use 100% CPU? - emacs

There's an external library I'm working with that frequently pegs my CPU. I'd like to help the author fix it (as I really like the library), but I don't know how to debug the crash properly.
Any tips for debugging Emacs lisp? Bear in mind when it crashes Emacs doesn't work anymore and I have to kill it (so solutions within Emacs itself might not be helpful).
Edit: I should clarify that it is byte-compiled, and this issue doesn't always happen for others, so it may be specific to my architecture/init files. It is definitely related to this library though.

First, always debug the uncompiled version of a Emacs-Lisp program, unless you're convinced the problem is introduced by the byte-compiler.
Second, if the code is hanging Emacs then the code is probably in an infinite loop with inhibit-quit bound non-nil. So the first thing to do is go through the source for the library and change all inhibit-quit references to something else so that C-g will work to stop the looping. After that, load up the library, set debug-on-quit to t and you should get a nice debug trace when you press C-g that shows you where the code is looping. From there, debugging the problem should be as straightforward as debugging any other infinite loop.

Related

Why does emacs start slowly

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

Emacs indentation screwed up

I recently upgraded to emacs 24.3.1 in order to use a new mode for programming in D. I seem to have introduced some problem with my other modes now (Java, C++ C I'm just using whatever comes with emacs). When I scroll up/down in a buffer, the text displayed is totally messed up. As in lines mixed together, indentation screwy, cursor not modifying where it looks like it is. If I hit ctrl-l the screen refreshes and all is good. I noticed this problem every once in a while with my previous version when editing C .h files. But now it's happening on every single buffer. I can't work with it. Every time I move up/down I have to refresh the screen. I can't find anything similar to my problem with a google search. Might end up being an issue with some incompatible version of a dependency. Here's my system stats.
CentOS release 5.4 (Final)
linux 2.6.18-164.el5
emacs 24.3.1
I don't even know what other libs emacs might depend on. I'll be happy to post versions of anything that might be relevant.
Thanks
Since you say that this happens also with emacs -Q, i.e., without your init file, consider filing a bug report: M-x report-emacs-bug. For that, try to give a short, reprocible recipe, starting with emacs -Q. Emacs Dev will take a look and let you know whether they think there is a bug.
Using report-emacs-bug also automatically gathers info about your Emacs version etc., and includes it with your bug description.

Xemacs does not show compilation errors anymore

I know this sounds really stupid, but:
I used to compile an Android jni project under Xemacs. The only feature that I needed was the ability to locate the place reported by gcc in the source code, edit and save.
But now this does not work anymore. Probably Xemacs caught some keypresses intended for another window (that is, I did not mention which one has the focus) and switched into a different mode. When I restart Xemacs, it works initially and after a while stops working again.
Question: how do I make it functional again?
What happens when you try this in GNU Emacs? It seems you don't haven't
heavily customized XEmacs so switching shouldn't be painful.
I hope someone with XEmacs expertise can help you, but XEmacs has 13
THIRTEEN followers on Stackoverflow. Clearly your chances of getting help will
improve is you try the Emacs that more people use.

In Emacs how to prevent the same buffer from showing up in different windows in the same frame?

I'm not sure how to comprehensively accomplish this.
Currently I build my own bzr Emacs on Windows, so I can see that `display-buffer' now takes a SPECIFIERS option, which could be interesting, but I couldn't find concrete examples of how to use it.
But this problem really has to be solved before display-buffer is called.
For example a Help window previously was visiting Buffer-A, but I've visited Buffer-A in another window while reading the help. Now when I quit the Help window, Buffer-A appears there as well. I want some other useful buffer to appear there.
I have some experimental code that appears to work here.
I emphasize experimental. This could melt your Emacs.
I'd appreciate it if you could contact me on github or here to let me know your experiences with this.
Emacs 24 is not yet released. They have changed the buffer-display/window behavior and Lisp interfaces several times over the release's development period. The current status of the release is pretest, so development is supposedly stopped, except for bug fixes.
However, ongoing emacs-devel#gnu.org discussions show that things are still in flux wrt buffer display and windows.
Your best bet is to check the latest doc and code (which might not correspond exactly, at this point).

Emacs switches to old buffers when a stale command is quit

I must be in the habit of leaving the mini-buffer in the middle of a command. From there I will change buffers and continue working. Later (sometimes much later) I'll notice that I had a command dangling incomplete in the mini-buffer. If I go to cancel it with a C-g, Emacs will often swap buffers in my open windows to an earlier state.
I suppose this may be a feature, but in my flow of work I find it rather annoying. Is there a way to stop this behavior or prevent it from happening, such as a warning that There is an incomplete command?
I think that the answer to another question will help you solve this. It can be an annoying behavior, but the solution limits some of the flexibility found in Emacs.
The answer is: How can I prevent the mini-buffer from displaying previous commands in Emacs? (I call it stop-using-minibuffer).