Skip to content

Instantly share code, notes, and snippets.

@tjg
Forked from juxtin/helm-clojure-headlines.el
Last active July 5, 2022 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tjg/4903f00a62e02bbe6217 to your computer and use it in GitHub Desktop.
Save tjg/4903f00a62e02bbe6217 to your computer and use it in GitHub Desktop.
helm-clojure-headlines
;; Might want to customize helm-candidate-number-limit, because by
;; default it shows 100 matches maximum.
;; FIXME: collect line count, and display if it's past helm-candidate-number-limit.
(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)
(let* ((line-count 0)
(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)
and do (incf line-count))))
(headline (if (< helm-candidate-number-limit line-count)
(format "%s (initially showing only %s lines; %s is %s)"
headline
line-count
'helm-candidate-number-limit
helm-candidate-number-limit)
headline)))
(helm :sources (helm-build-in-buffer-source headline
:data data
: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]+"
nil))
(defun helm-python-headlines ()
"Display headlines for the current Python file."
(interactive)
(helm-headlines "Python headlines"
"helm-python-headlines"
"\\(^[[:space:]]*\\(def\\|class\\)\\)\\|^#"
nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment