uris77
10/7/2015 - 4:35 PM

helm-clojure-headlines

helm-clojure-headlines

(defun helm-headlines (headline buffer-name good-regex exception-regex)
  "Display headlines for the current file.

   Displays lines where good-regex matches, except for those
   which also match exception-regex."
  ;; Fixes bug where the current buffer sometimes isn't used
  (setq helm-current-buffer (current-buffer))
  
  ;; https://groups.google.com/forum/#!topic/emacs-helm/YwqsyRRHjY4
  (jit-lock-fontify-now)
  (helm :sources (helm-build-in-buffer-source headline
                   :data (with-helm-current-buffer
                           (goto-char (point-min))
                           (cl-loop while (re-search-forward
                                           good-regex nil t)
                                    for line = (buffer-substring (point-at-bol)
                                                                 (point-at-eol))
                                    for pos = (line-number-at-pos)
                                    unless (and exception-regex
                                                (string-match-p exception-regex line))
                                    collect (propertize line 'helm-realvalue pos)))
                   :get-line 'buffer-substring
                   :action (lambda (c) (helm-goto-line c)))
        :buffer buffer-name))

(defun helm-clojure-headlines ()
  "Display headlines for the current Clojure file."
  (interactive)
  (helm-headlines "Clojure Headlines"
                  "helm-clojure-headlines"
                  "^(\\|^;.*[a-zA-Z]+"
                  "^(.*comment"))

(defun helm-python-headlines ()
  "Display headlines for the current Python file."
  (interactive)
  (helm-headlines "Python Headlines"
                  "helm-python-headlines"
                  "^def.*[a-zA-Z]+\\|^#"
                  nil))