Skip to content

Commit

Permalink
Origami for python.
Browse files Browse the repository at this point in the history
Based on code published in issue
gregsexton/origami.el#69.
  • Loading branch information
millejoh committed Oct 9, 2018
1 parent b7c0fec commit 22c7ac2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions imenu-origami.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
;; -*- mode: emacs-lisp-mode; lexical-binding: t -*-

;; Imenu based folding for origami, per issue #[69](https://github.com/gregsexton/origami.el/issues/69)

(defun origami-parser-imenu-flat (create)
"Origami parser producing folds for each imenu entry, without nesting."
(require 'imenu)
(lambda (content)
(let ((orig-major-mode major-mode))
(with-temp-buffer
(insert content)
(funcall orig-major-mode)
(let* ((items
(-as-> (imenu--make-index-alist t) items
(-flatten items)
(-filter 'listp items)))
(positions
(-as-> (-map #'cdr items) positions
(-filter 'identity positions)
(-map-when 'markerp 'marker-position positions)
(-filter 'natnump positions)
(cons (point-min) positions)
(-snoc positions (point-max))
(-sort '< positions)
(-uniq positions)))
(ranges
(-zip-pair positions (-map '1- (cdr positions))))
(fold-nodes
(--map
(-let*
(((range-beg . range-end) it)
(line-beg
(progn (goto-char range-beg)
(line-beginning-position)))
(offset
(- (min (line-end-position) range-end) line-beg))
(fold-node
(funcall create line-beg range-end offset nil)))
fold-node)
ranges)))
fold-nodes)))))

(provide 'imenu-origami)

0 comments on commit 22c7ac2

Please sign in to comment.