Skip to content

Commit

Permalink
chore(eri): prefix with lean-
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed Sep 18, 2017
1 parent 8955525 commit b2fa829
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions eri.el → lean-eri.el
@@ -1,39 +1,41 @@
;;; eri.el --- Enhanced relative indentation (eri)
;;; lean-eri.el --- Enhanced relative indentation (eri)

;;; Commentary:

;; Adapted from agda-mode (https://github.com/agda/agda/blob/master/src/data/emacs-mode/eri.el)

;;; Code:

(require 'cl)

(defun eri-current-line-length nil
(defun lean-eri-current-line-length nil
"Calculate length of current line."
(- (line-end-position) (line-beginning-position)))

(defun eri-current-line-empty nil
(defun lean-eri-current-line-empty nil
"Return non-nil if the current line is empty (not counting white space)."
(equal (current-indentation)
(eri-current-line-length)))
(lean-eri-current-line-length)))

(defun eri-maximum (xs)
(defun lean-eri-maximum (xs)
"Calculate maximum element in XS.
Returns nil if the list is empty."
(if xs (apply 'max xs)))

(defun eri-take (n xs)
(defun lean-eri-take (n xs)
"Return the first N elements of XS."
(butlast xs (- (length xs) n)))

(defun eri-split (x xs)
(defun lean-eri-split (x xs)
"Return a pair of lists (XS1 . XS2).
If XS is sorted, then XS = (append XS1 XS2), and all elements in
XS1 are <= X, whereas all elements in XS2 are > X."
(let* ((pos (or (position-if (lambda (y) (> y x)) xs) (length xs)))
(xs1 (eri-take pos xs))
(xs1 (lean-eri-take pos xs))
(xs2 (nthcdr pos xs)))
(cons xs1 xs2)))

(defun eri-calculate-indentation-points-on-line (max)
(defun lean-eri-calculate-indentation-points-on-line (max)
"Calculate indentation points on current line.
Only points left of column number MAX are included. If MAX is
nil, then all points are included. Points are returned in
Expand Down Expand Up @@ -66,7 +68,7 @@ Example (positions marked with ^ are returned):
(nreverse result) ; Destructive operation.
))))

(defun eri-new-indentation-points ()
(defun lean-eri-new-indentation-points ()
"Calculate new indentation points.
Returns a singleton list containing the column number two steps
in from the indentation of the first non-empty line (white space
Expand All @@ -79,16 +81,16 @@ then the empty list is returned."
(progn
(forward-line -1)
(not (or (bobp)
(not (eri-current-line-empty))))))
(not (lean-eri-current-line-empty))))))
(if (or (equal (point) start)
(eri-current-line-empty))
(lean-eri-current-line-empty))
nil
(list (+ 2 (current-indentation)))))))

(defun eri-calculate-indentation-points (reverse)
(defun lean-eri-calculate-indentation-points (reverse)
"Calculate points used to indent the current line.
The points are given in reverse order if REVERSE is non-nil. See
`eri-indent' for a description of how the indentation points are
`lean-eri-indent' for a description of how the indentation points are
calculated; note that the current indentation is not included in
the returned list."
;; First find a bunch of indentations used above the current line.
Expand All @@ -102,31 +104,31 @@ the returned list."
; Skip the line we started from and lines with nothing but
; white space.
(unless (or (equal (point) start)
(eri-current-line-empty))
(lean-eri-current-line-empty))
(setq points
(append
(eri-calculate-indentation-points-on-line max)
(lean-eri-calculate-indentation-points-on-line max)
points))
(setq max (car points)))
;; Stop after hitting the beginning of the buffer or a
;; non-empty, non-indented line.
(not (or (bobp)
(and (equal (current-indentation) 0)
(> (eri-current-line-length) 0)))))))
(> (lean-eri-current-line-length) 0)))))))
;; Add new indentation points, but remove the current indentation.
;; Sort the indentations. Rearrange the points so that the next
;; point is the one after the current one. Reverse if necessary.
;;
;; Note: sort and nreverse are destructive.
(let* ((ps0 (remove (current-indentation)
(append (eri-new-indentation-points) points)))
(ps1 (eri-split (current-indentation) (sort ps0 '<)))
(append (lean-eri-new-indentation-points) points)))
(ps1 (lean-eri-split (current-indentation) (sort ps0 '<)))
(ps2 (append (cdr ps1) (car ps1))))
(if reverse
(nreverse ps2)
ps2))))

(defun eri-indent (&optional reverse)
(defun lean-eri-indent (&optional reverse)
"Cycle between some possible indentation points.
With prefix argument REVERSE, cycle in reverse order.
Expand Down Expand Up @@ -185,7 +187,7 @@ follows:
f w f ( x y = l b = 3 + 5
^ ^ ^ ^ ^ ^ ^ ^ ^ * ^ ^ ^ ^"
(interactive "P")
(let* ((points (eri-calculate-indentation-points reverse))
(let* ((points (lean-eri-calculate-indentation-points reverse))
(remaining-points (cdr (member (current-indentation) points)))
(indentation (if remaining-points
(car remaining-points)
Expand All @@ -195,12 +197,12 @@ follows:
(if (< (current-column) indentation)
(indent-line-to indentation)))))

(defun eri-indent-reverse nil
(defun lean-eri-indent-reverse nil
"Cycle between some possible indentation points (in reverse order).
See `eri-indent' for a description of how the indentation points
See `lean-eri-indent' for a description of how the indentation points
are calculated."
(interactive)
(eri-indent t))
(lean-eri-indent t))

(provide 'eri)
;;; eri.el ends here
(provide 'lean-eri)
;;; lean-eri.el ends here
4 changes: 2 additions & 2 deletions lean-mode.el
Expand Up @@ -31,8 +31,8 @@
(require 'cl-lib)
(require 'pcase)
(require 'lean-require)
(require 'eri)
(require 'flycheck)
(require 'lean-eri)
(require 'lean-util)
(require 'lean-settings)
(require 'lean-input)
Expand Down Expand Up @@ -92,7 +92,7 @@
(defun lean-tab-indent ()
(interactive)
(cond ((looking-back (rx line-start (* white)) nil)
(eri-indent))
(lean-eri-indent))
(t (indent-for-tab-command))))

(defun lean-set-keys ()
Expand Down

0 comments on commit b2fa829

Please sign in to comment.