How do I locate the error in a lisp file in Emacs? - emacs

When working with my .emacs init file, I sometimes make a mistake. When I do eval-buffer, I get the message "end of file during parsing."
How do I ask Emacs to tell me the exact location of the error?

The first thing is to check the balancing of parentheses and string quotes.
For Emacs Lisp In GNU Emacs use M-x check-parens.
Other Emacs-like editors have similar commands. In LispWorks for example one can use M-x Find Unbalanced Parentheses.

These errors are very hard to actually locate.
Better try hard to avoid mismatching parenthesis at all. There are several built-in and 3rd-party minor modes that help you in this:
electric-pair-mode: Insert matching closing parenthesis automatically (built-in)
show-paren-mode: When point is over a parenthesis, highlight the matching one (built-in)
rainbow-delimiters-mode: Highlight each level of parenthesis in a different face
paredit-mode: Keep parenthesis balanced at all time. Generally, focus editing on Sexps instead of characters and words.
I'd recommend to enable all of these. A reasonable configuration to defeat mismatched parenthesis is thus:
(add-hook 'emacs-lisp-mode-hook 'paredit-mode)
(add-hook 'emacs-lisp-mode-hook 'rainbow-delimiters-mode)
(show-paren-mode 1)
(electric-pair-mode 1)
Paredit and Rainbow Delimiters are available from MELPA.

Related

How to keep cpp-mode indenting while I use another mode in Emacs?

I use a custom mode for a certain type of files just for syntax highlighting. But c-style indentation does not work when I load that mode. I don't want to write the rules for indentation for this mode.
Here is the link for the mode I'm using
How can I use the Emacs default c-style indentation in this custom mode?
Thanks.
c-mode installs its own parser to calculate indentation. Therefore, switching on c-indentation is not as simple as it seems to be (you cannot simply set indent-line-function and indent-region-function).
You can try the following:
(add-hook 'ned-mode-hook (lambda ()
(c-init-language-vars c-mode)
(c-basic-common-init 'c-mode "user")))
I noticed regular expressions for comments and strings in ned-font-lock-keywords. As far as I understand font-lock, this is wrong. Comments and strings are handled by the Parse-Partial-Sexp parser (see syntax-ppss) and not via regular expressions.
Furthermore, ned-mode should be derived from prog-mode and not from text-mode.

Long Argument Indentation in emacs

I am currently using "bsd" style indentation in emacs. I also prefer to limit myself to 80 character lines.
There are situations where I am passing many arguments to a function and exceed the 80 characters. When this happen, I insert a newline. Currently, if I use the automated indentation for the continuation of my line, I get the following:
function __TimeBlock($inputDay, $inputStartHour, $inputStartMinute,
$inputEndHour, $inputEndMinute)
however, what I want is:
function __TimeBlock($inputDay, $inputStartHour, $inputStartMinute,
$inputEndHour, $inputEndMinute)
It should line up so that it is directly below the first character after the paren above.
Is there a way to edit the indentations so that I will automatically get that behavior when I have a newline within a set of parens "()"?
I would like this to apply across any whitespace-ignoring languages I code in within emacs.
This is probably a good start for you:
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
"Customisations for C-mode and its derivatives."
(c-set-style "bsd"))
n.b. All the PHP modes I've seen derive from c-mode.
Or if you prefer:
M-x customize-variable RET c-default-style RET
and specify bsd as appropriate.
FWIW, the most specific syntactic context element symbol (for c-offsets-alist) for this question appears to be:
arglist-cont-nonempty -- Subsequent argument list lines when at
least one argument follows on the same
line as the arglist opening paren.
(but it doesn't look like you'll need to override its value.)

matching opening and closing braces in emacs

I am a newbie to emacs and have been using it only for a month.
My queries are:
I want to track the matching opening and closing braces in the editor. Is there a command for this?
When trying to use emacs using cygwin in Windows, C-x C-c to close emacs does not work!
Is there a way I can get this to work?
I personally like mic-paren, which has many options for customizing how to show matching parentheses, including showing you the line of the matching paren in the minibuffer when it is off-screen.
I set it up as follows:
(setq paren-dont-touch-blink t)
(require 'mic-paren)
(paren-activate)
(setq paren-match-face 'highlight)
(setq paren-sexp-mode t)
show-paren-mode will highlight matching parentheses, at least when the cursor is on one or the other.
A more powerful alternative is highlight parentheses mode. This will highlight all the parentheses around point (the cursor), so you see the scope of all the enclosing parentheses as you navigate your code.
There are other options as well, listed on the wiki.

Scheme editing in Emacs - modes and keyboard layout

Recently I started using Emacs as my Scheme (Lisp) editor. I'm thinking what extensions should I use in order to achieve the best performance. Currently I'm using Paredit and it helps a lot. I know that there are numerous Scheme extensions for Emacs: Geiser, Quack to name the two that seem very popular, and EmacsWiki lists many more. Which of these have you guys used and which ones do you find the best? At the moment my biggest problem is lack of parentheses colouring, which makes it vary hard to pair them visually - indentations are not enough when you have a line of code ending with ))))))))
I'm also thinking how could I improve the keyboard layout of Emacs in order to do better in Scheme editing? I've found some good advice on CLiki. I swapped [] with () on the keyboard and that's helpful. I'm also considering swapping Alt and Ctrl keys.
Do you have any other tips and suggestions that make it easier to edit Scheme in Emacs?
I've found rainbow delimiters mode really helpful for highlighting different levels of parentheses.
Among other modes that help me write lisp are hideshow mode for folding of sexps, slime which is primarily for Common Lisp but I use it's indentation capabilities in scheme too, low-contrast color theme called solarized with which my eyes don't fatigue any more and heavily mutated vim mode which permits me to keep my keybindings manageable through editing modes.
I use show-paren-mode, a minor mode, with these in my .emacs:
(show-paren-mode t)
(setq show-paren-delay 0)
(setq show-paren-style 'expression) ; alternatives are 'parenthesis' and 'mixed'
Relevant faces to modify are show-paren-match and show-paren-mismatch.
It only highlights a sexp when point is immediately before or after it, but I like that it's not so in-your-face.
I use autopair to get parenthesis right, show-paren-mode to see the end and beginning of s-expressions and expand-region to mark s-expression (It works on a lot more than that).
I think as you keep playing with paredit you may see less and less need for parenthesis coloring. For example, type ')' within any sexp, and the opening and closing parens will be momentarily highlighted; then point will move to the end of the sexp. Being able to navigate the nested sexp structure easily - for example, C-M-u and C-M-d to navigate up and down one paren level - also takes away some of the need to visually pair parenthesis.

Emacs putting a newline after closing parenthesis

So far this is all I have in my .emacs:
(when
(load
(expand-file-name "~/.emacs.d/elpa/package.el"))
(package-initialize))
(require 'paredit)
(add-hook 'c++-mode-hook '(lambda () (paredit-mode 1)))
And so with paredit set up, I start typing some code in a new C++ file: (where the [] represents the cursor)
if (someFunc([)]))
Now instead of pressing the right arrow key or something similar, I usually enter in the right paren since it is located right next to the left paren and is a lot more convenient.
However, upon doing that, paredit makes a newline:
if (someFunc ()
[)]
How do I prevent paredit from doing this?
Upgrade to a recent version of paredit-mode. From the comment header here:
;;; The key bindings are designed so that when typing new code in
;;; Paredit Mode, you can generally use exactly the same keystrokes as
;;; you would have used without Paredit Mode. Earlier versions of
;;; paredit.el did not conform to this, because Paredit Mode bound `)'
;;; to a command that would insert a newline. Now `)' is bound to a
;;; command that does not insert a newline, and `M-)' is bound to the
;;; command that inserts a newline.
ELPA may be out of date. The latest version is 22, and is available at http://marmalade-repo.org/
Also note that paredit-mode is designed to work with s-exp based languages like lisp or scheme. It might not be the best choice for C++ mode. If you're using paredit mainly to insert matching parentheses rather than the s-exp shortcuts and navigation, you might be better off with electric-pair-mode.
You might also want to try autopair available at http://autopair.googlecode.com/ which works nicely with C/C++, where paredit might not be the best choice.