Skip to content

Commit

Permalink
Make idris-format compatible with emacs 25*
Browse files Browse the repository at this point in the history
  • Loading branch information
keram committed Feb 2, 2023
1 parent 202a611 commit b2d6a9f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
46 changes: 46 additions & 0 deletions idris-compat.el
Expand Up @@ -41,5 +41,51 @@ attention to case differences."
(concat (apply 'concat (mapcar 'file-name-as-directory dirs))
(car (reverse components))))))

(if (fboundp 'string-limit)
(defalias 'idris-string-limit 'string-limit)
;; Extracted from Emacs 28* 'subr-x
(defun idris-string-limit (string length &optional end coding-system)
"Return a substring of STRING that is (up to) LENGTH characters long.
If STRING is shorter than or equal to LENGTH characters, return the
entire string unchanged.
If STRING is longer than LENGTH characters, return a substring
consisting of the first LENGTH characters of STRING. If END is
non-nil, return the last LENGTH characters instead.
If CODING-SYSTEM is non-nil, STRING will be encoded before
limiting, and LENGTH is interpreted as the number of bytes to
limit the string to. The result will be a unibyte string that is
shorter than LENGTH, but will not contain \"partial\" characters,
even if CODING-SYSTEM encodes characters with several bytes per
character.
When shortening strings for display purposes,
`truncate-string-to-width' is almost always a better alternative
than this function."
(unless (natnump length)
(signal 'wrong-type-argument (list 'natnump length)))
(if coding-system
(let ((result nil)
(result-length 0)
(index (if end (1- (length string)) 0)))
(while (let ((encoded (encode-coding-char
(aref string index) coding-system)))
(and (<= (+ (length encoded) result-length) length)
(progn
(push encoded result)
(cl-incf result-length (length encoded))
(setq index (if end (1- index)
(1+ index))))
(if end (> index -1)
(< index (length string)))))
;; No body.
)
(apply #'concat (if end result (nreverse result))))
(cond
((<= (length string) length) string)
(end (substring string (- (length string) length)))
(t (substring string 0 length))))))

(provide 'idris-compat)
;;; idris-compat.el ends here
12 changes: 7 additions & 5 deletions idris-format.el
Expand Up @@ -25,13 +25,14 @@
(require 'rx)
(require 'subr-x)
(require 'cl-seq)
(require 'idris-compat)

(defun idris-split-line-to-lhs-rhs (line)
"Split LINE string to two parts starting at first occurence of equal sign (=).
The equal sign is part of the right hand side. If no equal sign found
the `car' of result (LHS) is empty string and `cdr' of result (RHS) the LINE."
(let* ((lhs-length (or (string-match "=" line) 0))
(lhs (string-trim-right (string-limit line lhs-length)))
(lhs (string-trim-right (idris-string-limit line lhs-length)))
(rhs (substring line lhs-length)))
(cons lhs rhs)))

Expand Down Expand Up @@ -106,9 +107,10 @@ Example: (idris-split-words \"foo (S k) k\")
(lambda (lhs-rhs)
(if (and prev-lhs-delta (null (car lhs-rhs)))
(concat (make-string prev-lhs-delta ? ) (cdr lhs-rhs))
(let ((new-lhs (string-join (idris-mapcar-with-index (lambda (word j)
(string-pad word (nth j mvl)))
(car lhss-words))
(let ((new-lhs (string-join (idris-mapcar-with-index
(lambda (word j)
(concat word (make-string (nth j mvl) ?\s)))
(car lhss-words))
" ")))
(setq prev-lhs-delta (- (length new-lhs) (length (car lhs-rhs))))
(setq lhss-words (cdr lhss-words))
Expand Down Expand Up @@ -159,7 +161,7 @@ before line containing colon symbol (:) that contains equal sign (=)."
(let* ((start (idris-beginning-of-clause))
(end (idris-end-of-clause))
(indent (make-string (idris-clause-point-column start) ? ))
(lines (string-lines (buffer-substring-no-properties start end)))
(lines (split-string (buffer-substring-no-properties start end) "\n"))
(padded-lines (mapcar (lambda (line) (concat indent line))
(idris-pad-lines lines)))
(new-region (string-join padded-lines "\n")))
Expand Down

0 comments on commit b2d6a9f

Please sign in to comment.