1

I want to use imenu in matlab-mode in emacs. So I add something like

(setq imenu-generic-expression
      '(("Function"
         "\\(function\\)[\n\t ]*\\[?[ ,_A-Za-z0-9]+\\]?[\n\t ]*=[\n\t ]*\\([_A-Za-z0-9]+\\)" 2)
        ("Anonymous Function"
         "\\([_A-Za-z0-9]+\\)[\n\t ]*=[\n\t ]*@[\n\t ]*([, _A-Za-z0-9]+)" 1)))

in a function for matlab-mode-hook.

The matlab function definitions are something like

function y = func1 (x)
end

function [y, z] = func2 (a, b, c)
end

func3 = @(x) sin(x) + cos(x);

The above config results in something like

Function / func1
Function / func2
Anonymous Funtion / 3

after the helm-semantic-or-imenu or ido-imenu commands are launched.

My question is why the regular expression for anonymous function did not capture the function name fun3 as expected but 3?

Any help will be appreciated.

  • 2
    Try to anchor the name with a \_<. – politza Jul 13 '15 at 20:50
0
+50

I don't know why it happens, but imenu seems to match the regexp in a non-greedy manner, so it matched 3 rather than func3.

If you change the regexp to the following, it will match func3. The difference is the \\_< in front of the regexp matching the variable name:

(setq imenu-generic-expression
  '(("Function"
     "\\(function\\)[\n\t ]*\\[?[ ,_A-Za-z0-9]+\\]?[\n\t ]*=[\n\t ]*\\([_A-Za-z0-9]+\\)" 2)
    ("Anonymous Function"
     "\\_<\\([_A-Za-z0-9]+\\)[\n\t ]*=[\n\t ]*@[\n\t ]*([, _A-Za-z0-9]+)" 1)))
  • Imenu searches bottom up. – politza Jul 16 '15 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.