emacs

Stupid Emacs Tricks 1 โ€“ Searching in LessCSS with CSS selectors

(defun less-search (search-string)
  "Search a LessCSS buffer for an expression such as
\".content .inner .banner img\",
matching both full expressions as well as nested ones.
 
This means that it should match anything in the form:
 
.content .inner {
    .banner {
        img {
 
        }
    }
}
 
TODO: handle the case of .block.banner matching .block { &.banner { ... } }
TODO: handle looping like a normal search function
TODO: interactive search??"
  (interactive "sSearch: \n")
  (let ((search-components (split-string search-string " "))
        (end-point (point-max))
        (found-point nil))
    (save-excursion
      (goto-char (point-min))
      (setq found-point (dolist (item search-components ret)
                                        ; Search is here limited to a
                                        ; range that starts as the
                                        ; whole buffer and gets
                                        ; gradually restricted by the
                                        ; matched elements.
                          (unless (equal end-point (point-max))
                            (save-excursion
                              (forward-list)
                              (setq end-point (point))))
                          (let ((search-point (search-forward item end-point t)))
                            (unless search-point
                              (error "Search failed"))
                            (setq ret search-point)))))
    (when found-point
      (goto-char found-point))
    found-point))