From b2d6a9f44109c22f1bb96558411c7d910cfe8eaf Mon Sep 17 00:00:00 2001 From: Marek L Date: Thu, 2 Feb 2023 19:54:57 +0000 Subject: [PATCH] Make idris-format compatible with emacs 25* --- idris-compat.el | 46 ++++++++++++++++++++++++++++++++++++++++++++++ idris-format.el | 12 +++++++----- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/idris-compat.el b/idris-compat.el index d248d7f8..9f0dc28f 100644 --- a/idris-compat.el +++ b/idris-compat.el @@ -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 diff --git a/idris-format.el b/idris-format.el index cb04513b..4ef2f36f 100644 --- a/idris-format.el +++ b/idris-format.el @@ -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))) @@ -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)) @@ -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")))