0

I am trying to create a Regex for Imenu indexing for my CSS modes but I'm having trouble getting it exactly right as I'm no regular expression guru. Here's a snippet of LESS CSS I'm using (regexp-builder) to test against:

@font-face {
  font-family: 'sorts_mill_goudyregular';
  src: url('/webfonts/GoudyStM-webfont.eot');
  font-weight: normal;
  font-style: normal;
}

body {
  margin: 0;
  padding: 0;
}

#foo {
  color: blue;
}

h1,
h2,
h3,
.CheltenhamBT-BoldHeadline {
  font-family: CheltenhamBT-BoldHeadline;
  font-weight: normal;
  font-style: normal;
}

.ClarendonText-Bold {
  font-family: ClarendonText-Bold;
  font-weight: normal;
  font-style: normal;
  .bold {
    font-weight: bold;
  }  
}

Given this, I would like the menu to contain the following entries:

  • @font-face
  • body
  • #foo
  • h1, h2, h3, .CheltenhamBT-BoldHeadline
  • .ClarendonText-Bold

Note, I'm not interested in the nested .bold declaration.

I've tried a few approaches and have it mainly working, but I'm having trouble accounting for the occasional ,\n. Here's my current regex:

"^\\([\\.#@]?[a-z0-9_-]+\\(,\n\\)*\\)\\s-{$"

Can anyone help me out?

  • Wouldn't something like "^[^{]+{" work? (It probably will, if you always put the open curly brace on the same line with selector, but if you will ever decide to move it to the next line, then, I'm afraid, regexp won't do it at all). – wvxvw Mar 18 '15 at 16:36
2

You don't have to create a one-liner. The following list of definitions should create an imenu items:

(setq less-imenu-generic-expression
      '((nil "\\(^@font-face\\)" 1)
        (nil "\\(^body\\)" 1)
        (nil "\\(^#foo\\)" 1)
        (nil "\\(^h[123]\\)" 1)
        (nil "\\(^\.CheltenhamBT-BoldHeadline\\)" 1)
        (nil "\\(^.ClarendonText-Bold\\)" 1)))
(add-hook 'css-mode-hook 
          (lambda ()
            (setq imenu-generic-expression less-imenu-generic-expression)))

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.