[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: shell-script mode index

From: Drew Adams
Subject: RE: shell-script mode index
Date: Fri, 27 Jun 2014 08:00:23 -0700 (PDT)

> >> When I tried to do it outside a hook, I got an error for
> >> fundamental-mode.
> >
> > Wrap the calls to `imenu*' in `ignore-errors'.
> I've been following this as I need an Imenu for latex-mode, but I don't
> understand this.  Could you give a code example please?

If you tell some mode to invoke `imenu' or `imenu-add-to-menubar' or
`imenu-add-menubar-index', and if that mode does not support
Imenu (e.g., it has no usable value for `imenu-generic-expression'),
then the function raises an error when it is invoked.

You can wrap any code in `ignore-errors' to have it just return nil
if it raises an error when evaluated:

(something-that-raises an error)                 ; raises an error
(ignore-errors (something-that-raises an error)) ; returns nil

If you have an older version of Emacs, which does not have
macro `ignore-errors', then use `condition-case' (this works in
all Emacs versions):

(condition-case nil
    (something-that-raises an error)
  (error nil)) ; Do nothing and return nil.

I do this, for instance, in `imenu+.el'

(defun imenup-add-defs-to-menubar ()
  "Add \"Defs\" imenu entry to menu bar for current local keymap.
See `imenu' for more information."
  (imenu-add-to-menubar "Defs"))

(add-hook 'lisp-mode-hook
          (lambda ()
            (setq imenu-generic-expression  lisp-imenu-generic-expression)
            (condition-case nil (imenup-add-defs-to-menubar) (error nil))))

Here, there should be no need to wrap with `condition-case', since
`imenu-generic-expression' is defined (so Imenu is supported in Lisp
mode). But I do it anyway.

The point is that if you invoke some code that might raise an
error, you can inhibit raising an error by wrapping that code in
`ignore-errors'. That doesn't make the code magically "work"; it
just prevents it from raising an error.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]